From e8e883f9c855cd419ac045c30d6ccb7f20c9993e Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 23 Jul 2015 10:13:45 +0100 Subject: [PATCH] Revert "Issue #2510104 by pwolanin, nod_, Fabianx, Wim Leers, droplet, Pere Orga: Convert drupalSettings from JavaScript to JSON, to allow for CSP in the future" This reverts commit 94615a137b1a4d83d0329a53563d5e7bd2265644. --- core/core.libraries.yml | 4 ---- .../Core/Asset/JsCollectionRenderer.php | 16 +++++++------ core/misc/drupal.js | 8 +++++++ core/misc/drupalSettingsLoader.js | 24 ------------------- .../simpletest/src/AssertContentTrait.php | 2 +- .../src/Tests/Common/AttachedAssetsTest.php | 8 +++---- 6 files changed, 22 insertions(+), 40 deletions(-) delete mode 100644 core/misc/drupalSettingsLoader.js diff --git a/core/core.libraries.yml b/core/core.libraries.yml index 179382bf0c9..cfbaf473de5 100644 --- a/core/core.libraries.yml +++ b/core/core.libraries.yml @@ -52,10 +52,6 @@ drupal: drupalSettings: version: VERSION - js: - # Need to specify a negative weight like drupal.js until - # https://www.drupal.org/node/1945262 is resolved. - misc/drupalSettingsLoader.js: { weight: -18 } drupalSettings: # These placeholder values will be set by system_js_settings_alter(). path: diff --git a/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php b/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php index 70832e9d9d1..01f75ac8875 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php @@ -51,6 +51,12 @@ class JsCollectionRenderer implements AssetCollectionRendererInterface { // query-string instead, to enforce reload on every page request. $default_query_string = $this->state->get('system.css_js_query_string') ?: '0'; + // For inline JavaScript to validate as XHTML, all JavaScript containing + // XHTML needs to be wrapped in CDATA. To make that backwards compatible + // with HTML 4, we need to comment out the CDATA-tag. + $embed_prefix = "\n\n"; + // Defaults for each SCRIPT element. $element_defaults = array( '#type' => 'html_tag', @@ -67,13 +73,9 @@ class JsCollectionRenderer implements AssetCollectionRendererInterface { // Element properties that depend on item type. switch ($js_asset['type']) { case 'setting': - $element['#attributes'] = array( - // This type attribute prevents this from being parsed as an - // inline script. - 'type' => 'application/json', - 'data-drupal-selector' => 'drupal-settings-json', - ); - $element['#value'] = Json::encode($js_asset['data']); + $element['#value_prefix'] = $embed_prefix; + $element['#value'] = 'var drupalSettings = ' . Json::encode($js_asset['data']) . ";"; + $element['#value_suffix'] = $embed_suffix; break; case 'file': diff --git a/core/misc/drupal.js b/core/misc/drupal.js index 8ef8167b736..75f85bbacde 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -11,6 +11,14 @@ * @prop {number} length=0 */ +/** + * Variable generated by Drupal with all the configuration created from PHP. + * + * @global + * + * @var {object} drupalSettings + */ + /** * Variable generated by Drupal that holds all translated strings from PHP. * diff --git a/core/misc/drupalSettingsLoader.js b/core/misc/drupalSettingsLoader.js deleted file mode 100644 index ab3911dea88..00000000000 --- a/core/misc/drupalSettingsLoader.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @file - * Parse inline JSON and initialize the drupalSettings global object. - */ - -(function () { - - "use strict"; - - var settingsElement = document.querySelector('script[type="application/json"][data-drupal-selector="drupal-settings-json"]'); - - /** - * Variable generated by Drupal with all the configuration created from PHP. - * - * @global - * - * @type {object} - */ - window.drupalSettings = {}; - - if (settingsElement !== null) { - window.drupalSettings = JSON.parse(settingsElement.textContent); - } -})(); diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php index a425f4f954c..1918d424545 100644 --- a/core/modules/simpletest/src/AssertContentTrait.php +++ b/core/modules/simpletest/src/AssertContentTrait.php @@ -66,7 +66,7 @@ trait AssertContentTrait { $this->plainTextContent = NULL; $this->elements = NULL; $this->drupalSettings = array(); - if (preg_match('@@', $content, $matches)) { + if (preg_match('/var drupalSettings = (.*?);$/m', $content, $matches)) { $this->drupalSettings = Json::decode($matches[1]); } } diff --git a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php index 40c6830f188..4e2a095ee08 100644 --- a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php +++ b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php @@ -185,9 +185,9 @@ class AttachedAssetsTest extends KernelTestBase { $rendered_footer_js = \Drupal::service('asset.js.collection_renderer')->render($footer_js); $this->assertTrue( count($rendered_footer_js) == 2 - && $rendered_footer_js[0]['#attributes']['data-drupal-selector'] === 'drupal-settings-json' + && substr($rendered_footer_js[0]['#value'], 0, 20) === 'var drupalSettings =' && substr($rendered_footer_js[1]['#attributes']['src'], 0, 7) === 'http://', - 'There are 2 JavaScript assets in the footer: one with drupal settings, one with the sole aggregated JavaScript asset.' + 'There are 2 JavaScript assets in the footer: one with drupalSettings, one with the sole aggregated JavaScript asset.' ); } @@ -206,9 +206,9 @@ class AttachedAssetsTest extends KernelTestBase { $rendered_js = $this->renderer->renderPlain($js_render_array); // Parse the generated drupalSettings