[BUGFIX] CKEditor: Avoid JS error when opening an existing link 91/51691/2
authorBenni Mack <benni@typo3.org>
Wed, 15 Feb 2017 05:47:09 +0000 (06:47 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 15 Feb 2017 18:38:48 +0000 (19:38 +0100)
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>
typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/Plugins/typo3link.js

index a7907c3..4a42d96 100644 (file)
        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,
                        // 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 [];
+               }
+       }
+
 })();