reverted: --- b/core/misc/ajax.js +++ a/core/misc/ajax.js @@ -1326,18 +1326,16 @@ * The XMLHttpRequest status. */ add_css: function (ajax, response, status) { - // Allow the response to set the document context. - var context = response.document || window.document; // Add the styles in the normal way. + $('head').prepend(response.data); - $(context).find('head').prepend(response.data); // Add imports in the styles using the addImport method if available. var match; var importMatch = /^@import url\("(.*)"\);$/igm; + if (document.styleSheets[0].addImport && importMatch.test(response.data)) { - if (context.styleSheets[0].addImport && importMatch.test(response.data)) { importMatch.lastIndex = 0; do { match = importMatch.exec(response.data); + document.styleSheets[0].addImport(match[1]); - context.styleSheets[0].addImport(match[1]); } while (match); } } diff -u b/core/modules/ckeditor/js/ckeditor.js b/core/modules/ckeditor/js/ckeditor.js --- b/core/modules/ckeditor/js/ckeditor.js +++ b/core/modules/ckeditor/js/ckeditor.js @@ -277,7 +277,7 @@ CKEDITOR.timestamp = drupalSettings.ckeditor.timestamp; /** - * Command to add CSS to a CKEditor instance. + * Command to add CSS styles to a CKEditor instance. * * @param {Drupal.Ajax} [ajax] * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. @@ -289,11 +289,35 @@ * The XMLHttpRequest status. */ - Drupal.AjaxCommands.prototype.add_css_ckeditor = function (ajax, response, status) { + Drupal.AjaxCommands.prototype.ckeditor_add_style = function (ajax, response, status) { var editor = CKEDITOR.instances[response.editor_id]; + + if (editor) { + response.styles.forEach(function (style) { + editor.document.appendStyleText(style); + }); + } + }; + + /** + * Command to add style sheets to a CKEditor instance. + * + * @param {Drupal.Ajax} [ajax] + * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. + * @param {object} response + * The response from the Ajax request. + * @param {string} response.editor_id + * The CKEditor instance ID. + * @param {number} [status] + * The XMLHttpRequest status. + */ + Drupal.AjaxCommands.prototype.ckeditor_add_stylesheet = function (ajax, response, status) { + var editor = CKEDITOR.instances[response.editor_id]; + if (editor) { - response.document = editor.document.$; + response.stylesheets.forEach(function (url) { + editor.document.appendStyleSheet(url); + }); } - this.add_css.apply(this, arguments); - } + }; })(Drupal, Drupal.debounce, CKEDITOR, jQuery); diff -u b/core/modules/ckeditor/tests/modules/ckeditor_test.module b/core/modules/ckeditor/tests/modules/ckeditor_test.module --- b/core/modules/ckeditor/tests/modules/ckeditor_test.module +++ b/core/modules/ckeditor/tests/modules/ckeditor_test.module @@ -7,6 +7,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\ckeditor\Ajax\AddCssCommand; +use Drupal\ckeditor\Ajax\AddStylesCommand; use Drupal\editor\Entity\Editor; /** @@ -23,12 +24,9 @@ } function ckeditor_test_send_add_css_command() { - // Force all text in the editor to be red. - $css = ''; - return (new AjaxResponse) ->addCommand( - new AddCssCommand($css, 'edit-body-0-value') + new AddStylesCommand('edit-body-0-value', ['body { color: red; }']) ); } only in patch2: unchanged: --- /dev/null +++ b/core/modules/ckeditor/src/Ajax/AddStyleSheetCommand.php @@ -0,0 +1,34 @@ +editorId = $editor_id; + $this->styleSheets = $stylesheets; + } + + public function addStyleSheet($stylesheet) { + $this->styleSheets[] = $stylesheet; + return $this; + } + + /** + * {@inheritdoc} + */ + public function render() { + return [ + 'command' => 'ckeditor_add_stylesheet', + 'editor_id' => $this->editorId, + 'stylesheets' => $this->styleSheets, + ]; + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/modules/ckeditor/src/Ajax/AddStylesCommand.php @@ -0,0 +1,34 @@ +editorId = $editor_id; + $this->styles = $styles; + } + + public function addStyle($style) { + $this->styles[] = $style; + return $this; + } + + /** + * {@inheritdoc} + */ + public function render() { + return [ + 'command' => 'ckeditor_add_style', + 'editor_id' => $this->editorId, + 'styles' => $this->styles, + ]; + } + +}