From 81e8304711e58797bf62eebc8de684b598c50b79 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Wed, 15 Feb 2017 06:47:09 +0100
Subject: [PATCH] [BUGFIX] CKEditor: Avoid JS error when opening an existing
 link

When opening an existing link by double-clicking on the
a tag element in CKEditor the popup should open, however
if no additional attributes are registered, JS throws an
error.

Resolves: #79818
Releases: master
Change-Id: I9f9b838862ab76d21161d4962f75f88d70c44cdd
Reviewed-on: https://review.typo3.org/51691
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
---
 .../Public/JavaScript/Plugins/typo3link.js    | 28 ++++++++++++++-----
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/Plugins/typo3link.js b/typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/Plugins/typo3link.js
index a7907c31b419..4a42d96e4397 100644
--- a/typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/Plugins/typo3link.js
+++ b/typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/Plugins/typo3link.js
@@ -18,17 +18,18 @@
 	CKEDITOR.plugins.add('typo3link', {
 		elementBrowser: null,
 		init: function (editor) {
-			var allowed = 'a[!href,title,class,target,rel]',
+			var allowedAttributes = ['!href', 'title', 'class', 'target', 'rel'],
 				required = 'a[href]';
 
-			if (editor.config.typo3link.additionalAttributes && editor.config.typo3link.additionalAttributes.length) {
-				allowed = allowed.replace( ']', ',' + editor.config.typo3link.additionalAttributes.join(',') + ']');
+			var additionalAttributes = getAdditionalAttributes(editor);
+			if (additionalAttributes.length) {
+				allowedAttributes.push.apply(allowedAttributes, additionalAttributes);
 			}
 
 			// Override link command
 			editor.addCommand('link', {
 				exec: openLinkBrowser,
-				allowedContent: allowed,
+				allowedContent: 'a[' + allowedAttributes.join(',') + ']',
 				requiredContent: required
 			});
 
@@ -67,7 +68,7 @@
 				}
 			}
 
-			var additionalAttributes = editor.config.typo3link.additionalAttributes;
+			var additionalAttributes = getAdditionalAttributes(editor);
 			for (i = additionalAttributes.length; --i >= 0;) {
 				if (element.hasAttribute(additionalAttributes[i])) {
 					additionalParameters += '&curUrl[' + additionalAttributes[i] + ']=';
@@ -79,7 +80,7 @@
 		openElementBrowser(
 			editor,
 			editor.lang.link.toolbar,
-			TYPO3.settings.Textarea.RTEPopupWindow.height - 20,
+			TYPO3.settings.Textarea.RTEPopupWindow.height,
 			makeUrlFromModulePath(
 				editor,
 				editor.config.typo3link.routeUrl,
@@ -132,8 +133,21 @@
 			// TODO: add this to less/css (.rte-ckeditor-window .modal-body)
 			// 		 further, make modal wider and maybe resize-able
 			elementBrowser.find('.modal-body').css('padding', 0);
-
 		});
 	}
 
+	/**
+	 * Fetch attributes for the <a> tag which are allowed additionally
+	 * @param {Object} editor The CKEditor instance
+	 *
+	 * @return {Array} registered attributes available for the link
+	 */
+	function getAdditionalAttributes(editor) {
+		if (editor.config.typo3link.additionalAttributes && editor.config.typo3link.additionalAttributes.length) {
+			return editor.config.typo3link.additionalAttributes;
+		} else {
+			return [];
+		}
+	}
+
 })();
-- 
GitLab