diff --git a/core/modules/image/js/plugins/drupalimagestyle/plugin.js b/core/modules/image/js/plugins/drupalimagestyle/plugin.js index 7b673ca..89c2755 100644 --- a/core/modules/image/js/plugins/drupalimagestyle/plugin.js +++ b/core/modules/image/js/plugins/drupalimagestyle/plugin.js @@ -1,11 +1,10 @@ /** * @file - * Drupal Image Caption plugin. + * Drupal Image Style plugin. * * This alters the existing CKEditor image2 widget plugin, which is already - * altered by the Drupal Image plugin, to: - * - allow for the data-caption and data-align attributes to be set - * - mimic the upcasting behavior of the caption_filter filter. + * altered by the Drupal Image plugin, to allow for the data-image-style + * attribute to be set. * * @ignore */ @@ -19,13 +18,13 @@ beforeInit: function (editor) { // Override the image2 widget definition to handle the additional - // data-responsive-image-style attributes. + // data-image-style attributes. editor.on('widgetDefinition', function (event) { var widgetDefinition = event.data; if (widgetDefinition.name !== 'image') { return; } - // Override default features definitions for drupalimagecaption. + // Override default features definitions for drupalimagestyle. CKEDITOR.tools.extend(widgetDefinition.features, { responsiveimage: { requiredContent: 'img[data-image-style]' @@ -61,7 +60,7 @@ } element = originalUpcast.call(this, element, data); - // Parse the data-responsive-image-style attribute. + // Parse the data-image-style attribute. data['data-image-style'] = element.attributes['data-image-style']; return element; @@ -71,7 +70,7 @@ // Append to the values defined by the drupalimage plugin. // @see core/modules/ckeditor/js/plugins/drupalimage/plugin.js CKEDITOR.tools.extend(widgetDefinition._mapDataToDialog, { - 'data-image-style': 'data-image-style', + 'data-image-style': 'data-image-style' }); // Low priority to ensure drupalimage's event handler runs first. }, null, null, 20); diff --git a/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php b/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php index 2b87534..9a8c390 100644 --- a/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php +++ b/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php @@ -13,7 +13,7 @@ use Drupal\ckeditor\CKEditorPluginContextualInterface; /** - * Defines the "drupalresponsiveimage" plugin. + * Defines the "drupalimagestyle" plugin. * * @CKEditorPlugin( * id = "drupalimagestyle", @@ -34,14 +34,14 @@ public function isInternal() { * {@inheritdoc} */ public function getDependencies(Editor $editor) { - return array(); + return []; } /** * {@inheritdoc} */ public function getLibraries(Editor $editor) { - return array(); + return []; } /** @@ -55,7 +55,7 @@ public function getFile() { * {@inheritdoc} */ public function getConfig(Editor $editor) { - return array(); + return []; } /** @@ -67,8 +67,8 @@ public function isEnabled(Editor $editor) { } // Automatically enable this plugin if the text format associated with this - // text editor uses the filter_responsive_image_style filter and the - // DrupalImage button is enabled. + // text editor uses the filter_imagestyle filter and the DrupalImage button + // is enabled. $format = $editor->getFilterFormat(); if ($format->filters('filter_imagestyle')->status) { $enabled = FALSE; diff --git a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php index bd6eea0..8475fcb 100644 --- a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php +++ b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php @@ -41,7 +41,7 @@ public function process($text, $langcode) { $dom = HTML::load($text); $xpath = new \DOMXPath($dom); - foreach ($xpath->query('//*[@data-entity-uuid and @data-image-style]') as $node) { + foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid and @data-image-style]') as $node) { $file_uuid = $node->getAttribute('data-entity-uuid'); $node->removeAttribute('data-entity-uuid'); $image_style_id = $node->getAttribute('data-image-style'); diff --git a/core/modules/responsive_image/js/plugins/drupalresponsiveimage/plugin.js b/core/modules/responsive_image/js/plugins/drupalresponsiveimage/plugin.js deleted file mode 100644 index fe2bf47..0000000 --- a/core/modules/responsive_image/js/plugins/drupalresponsiveimage/plugin.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * @file - * Drupal Image Caption plugin. - * - * This alters the existing CKEditor image2 widget plugin, which is already - * altered by the Drupal Image plugin, to: - * - allow for the data-caption and data-align attributes to be set - * - mimic the upcasting behavior of the caption_filter filter. - * - * @ignore - */ - -(function (CKEDITOR) { - - "use strict"; - - CKEDITOR.plugins.add('drupalresponsiveimage', { - requires: 'drupalimage', - - beforeInit: function (editor) { - // Override the image2 widget definition to handle the additional - // data-responsive-image-style attributes. - editor.on('widgetDefinition', function (event) { - var widgetDefinition = event.data; - if (widgetDefinition.name !== 'image') { - return; - } - // Override default features definitions for drupalimagecaption. - CKEDITOR.tools.extend(widgetDefinition.features, { - responsiveimage: { - requiredContent: 'img[data-responsive-image-style]' - } - }, true); - - // Override requiredContent & allowedContent. - var requiredContent = widgetDefinition.requiredContent.getDefinition(); - requiredContent.attributes['data-responsive-image-style'] = ''; - widgetDefinition.requiredContent = new CKEDITOR.style(requiredContent); - widgetDefinition.allowedContent.img.attributes += ',!data-responsive-image-style'; - - // Override downcast(). - var originalDowncast = widgetDefinition.downcast; - widgetDefinition.downcast = function (element) { - var img = originalDowncast.call(this, element); - if (!img) { - img = findElementByName(element, 'img'); - } - img.attributes['data-responsive-image-style'] = this.data['data-responsive-image-style']; - return img; - }; - - // Override upcast(). - var originalUpcast = widgetDefinition.upcast; - widgetDefinition.upcast = function (element, data) { - if (element.name !== 'img' || !element.attributes['data-entity-type'] || !element.attributes['data-entity-uuid']) { - return; - } - // Don't initialize on pasted fake objects. - else if (element.attributes['data-cke-realelement']) { - return; - } - element = originalUpcast.call(this, element, data); - - // Parse the data-responsive-image-style attribute. - data['data-responsive-image-style'] = element.attributes['data-responsive-image-style']; - - return element; - }; - - // Protected; keys of the widget data to be sent to the Drupal dialog. - // Append to the values defined by the drupalimage plugin. - // @see core/modules/ckeditor/js/plugins/drupalimage/plugin.js - CKEDITOR.tools.extend(widgetDefinition._mapDataToDialog, { - 'data-responsive-image-style': 'data-responsive-image-style', - }); - // Low priority to ensure drupalimage's event handler runs first. - }, null, null, 20); - } - }); -})(CKEDITOR); diff --git a/core/modules/responsive_image/js/plugins/drupalresponsiveimagestyle/plugin.js b/core/modules/responsive_image/js/plugins/drupalresponsiveimagestyle/plugin.js new file mode 100644 index 0000000..975097d --- /dev/null +++ b/core/modules/responsive_image/js/plugins/drupalresponsiveimagestyle/plugin.js @@ -0,0 +1,79 @@ +/** + * @file + * Drupal Responsive Image Style plugin. + * + * This alters the existing CKEditor image2 widget plugin, which is already + * altered by the Drupal Image plugin, to data-responsive-image-style attribute + * to be set. + * + * @ignore + */ + +(function (CKEDITOR) { + + "use strict"; + + CKEDITOR.plugins.add('drupalresponsiveimagestyle', { + requires: 'drupalimage', + + beforeInit: function (editor) { + // Override the image2 widget definition to handle the additional + // data-responsive-image-style attributes. + editor.on('widgetDefinition', function (event) { + var widgetDefinition = event.data; + if (widgetDefinition.name !== 'image') { + return; + } + // Override default features definitions for drupalresponsiveimagestyle. + CKEDITOR.tools.extend(widgetDefinition.features, { + responsiveimage: { + requiredContent: 'img[data-responsive-image-style]' + } + }, true); + + // Override requiredContent & allowedContent. + var requiredContent = widgetDefinition.requiredContent.getDefinition(); + requiredContent.attributes['data-responsive-image-style'] = ''; + widgetDefinition.requiredContent = new CKEDITOR.style(requiredContent); + widgetDefinition.allowedContent.img.attributes += ',!data-responsive-image-style'; + + // Override downcast(). + var originalDowncast = widgetDefinition.downcast; + widgetDefinition.downcast = function (element) { + var img = originalDowncast.call(this, element); + if (!img) { + img = findElementByName(element, 'img'); + } + img.attributes['data-responsive-image-style'] = this.data['data-responsive-image-style']; + return img; + }; + + // Override upcast(). + var originalUpcast = widgetDefinition.upcast; + widgetDefinition.upcast = function (element, data) { + if (element.name !== 'img' || !element.attributes['data-entity-type'] || !element.attributes['data-entity-uuid']) { + return; + } + // Don't initialize on pasted fake objects. + else if (element.attributes['data-cke-realelement']) { + return; + } + element = originalUpcast.call(this, element, data); + + // Parse the data-responsive-image-style attribute. + data['data-responsive-image-style'] = element.attributes['data-responsive-image-style']; + + return element; + }; + + // Protected; keys of the widget data to be sent to the Drupal dialog. + // Append to the values defined by the drupalimage plugin. + // @see core/modules/ckeditor/js/plugins/drupalimage/plugin.js + CKEDITOR.tools.extend(widgetDefinition._mapDataToDialog, { + 'data-responsive-image-style': 'data-responsive-image-style', + }); + // Low priority to ensure drupalimage's event handler runs first. + }, null, null, 20); + } + }); +})(CKEDITOR); diff --git a/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImage.php b/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImage.php deleted file mode 100644 index 7fd4a67..0000000 --- a/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImage.php +++ /dev/null @@ -1,91 +0,0 @@ -hasAssociatedFilterFormat()) { - return FALSE; - } - - // Automatically enable this plugin if the text format associated with this - // text editor uses the filter_responsive_image_style filter and the - // DrupalImage button is enabled. - $format = $editor->getFilterFormat(); - if ($format->filters('filter_responsive_image_style')->status) { - $enabled = FALSE; - $settings = $editor->getSettings(); - foreach ($settings['toolbar']['rows'] as $row) { - foreach ($row as $group) { - foreach ($group['items'] as $button) { - if ($button === 'DrupalImage') { - $enabled = TRUE; - } - } - } - } - return $enabled; - } - - return FALSE; - } - -} diff --git a/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImageStyle.php b/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImageStyle.php new file mode 100644 index 0000000..fc9486b --- /dev/null +++ b/core/modules/responsive_image/src/Plugin/CKEditorPlugin/DrupalResponsiveImageStyle.php @@ -0,0 +1,91 @@ +hasAssociatedFilterFormat()) { + return FALSE; + } + + // Automatically enable this plugin if the text format associated with this + // text editor uses the filter_responsive_image_style filter and the + // DrupalImage button is enabled. + $format = $editor->getFilterFormat(); + if ($format->filters('filter_responsive_image_style')->status) { + $enabled = FALSE; + $settings = $editor->getSettings(); + foreach ($settings['toolbar']['rows'] as $row) { + foreach ($row as $group) { + foreach ($group['items'] as $button) { + if ($button === 'DrupalImage') { + $enabled = TRUE; + } + } + } + } + return $enabled; + } + + return FALSE; + } + +} diff --git a/core/modules/responsive_image/src/Plugin/Filter/FilterResponsiveImageStyle.php b/core/modules/responsive_image/src/Plugin/Filter/FilterResponsiveImageStyle.php index 8745b22..3e7341f 100644 --- a/core/modules/responsive_image/src/Plugin/Filter/FilterResponsiveImageStyle.php +++ b/core/modules/responsive_image/src/Plugin/Filter/FilterResponsiveImageStyle.php @@ -92,6 +92,7 @@ public function process($text, $langcode) { ->item(0) ->childNodes ->item(0); + // Import the updated node from the new DOMDocument into the original // one, importing also the child nodes of the updated node. $updated_node = $dom->importNode($updated_node, TRUE);