diff --git a/core/modules/filter/filter.filter_html.admin.js b/core/modules/filter/filter.filter_html.admin.js index 7ddca90bba..e2047f3684 100644 --- a/core/modules/filter/filter.filter_html.admin.js +++ b/core/modules/filter/filter.filter_html.admin.js @@ -3,7 +3,7 @@ * Attaches behavior for updating filter_html's settings automatically. */ -(function ($, Drupal, _, document) { +(function ($, Drupal, CKEDITOR, _, document) { 'use strict'; @@ -230,45 +230,40 @@ * tag name. */ _parseSetting: function (setting) { - var node; var tag; var rule; - var attributes; - var attribute; - var allowedTags = setting.match(/(<[^>]+>)/g); - var sandbox = document.createElement('div'); var rules = {}; - for (var t = 0; t < allowedTags.length; t++) { + + var parser = new CKEDITOR.htmlParser(); + parser.onTagOpen = function (tagName, attributes, selfClosing) { // Let the browser do the parsing work for us. - sandbox.innerHTML = allowedTags[t]; - node = sandbox.firstChild; - tag = node.tagName.toLowerCase(); + tag = tagName.toLowerCase(); // Build the Drupal.FilterHtmlRule object. rule = new Drupal.FilterHTMLRule(); // We create one rule per allowed tag, so always one tag. rule.restrictedTags.tags = [tag]; + // Add the attribute restrictions. - attributes = node.attributes; - for (var i = 0; i < attributes.length; i++) { - attribute = attributes.item(i); - var attributeName = attribute.nodeName; + Object.keys(attributes).forEach(function (nodeName) { // @todo Drupal.FilterHtmlRule does not allow for generic attribute // value restrictions, only for the "class" and "style" attribute's // values. The filter_html filter always disallows the "style" // attribute, so we only need to support "class" attribute value // restrictions. Fix once https://www.drupal.org/node/2567801 lands. - if (attributeName === 'class') { - var attributeValue = attribute.textContent; + if (nodeName === 'class') { + var attributeValue = attributes[nodeName]; rule.restrictedTags.allowed.classes = attributeValue.split(' '); } else { - rule.restrictedTags.allowed.attributes.push(attributeName); + rule.restrictedTags.allowed.attributes.push(nodeName); } - } + }); rules[tag] = rule; - } + }; + parser.parse(setting); + return rules; }, @@ -325,4 +320,4 @@ return html; }; -})(jQuery, Drupal, _, document); +})(jQuery, Drupal, CKEDITOR, _, document); diff --git a/core/modules/filter/filter.libraries.yml b/core/modules/filter/filter.libraries.yml index 91aea0be5b..3ce684e85c 100644 --- a/core/modules/filter/filter.libraries.yml +++ b/core/modules/filter/filter.libraries.yml @@ -19,6 +19,7 @@ drupal.filter.filter_html.admin: - core/jquery - core/jquery.once - core/underscore + - core/ckeditor drupal.filter: version: VERSION diff --git a/core/modules/filter/tests/src/FunctionalJavascript/FilterIntegrationTest.php b/core/modules/filter/tests/src/FunctionalJavascript/FilterIntegrationTest.php new file mode 100644 index 0000000000..47e90bd4a3 --- /dev/null +++ b/core/modules/filter/tests/src/FunctionalJavascript/FilterIntegrationTest.php @@ -0,0 +1,106 @@ + 'basic_html', + 'name' => 'Basic HTML', + 'filters' => [ + 'filter_html' => [ + 'status' => 1, + 'settings' => [ + 'allowed_html' => '