Skip to content
Snippets Groups Projects
Commit e8e883f9 authored by Alex Pott's avatar Alex Pott
Browse files

Revert "Issue #2510104 by pwolanin, nod_, Fabianx, Wim Leers, droplet, Pere...

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 94615a13.
parent 341f0e4c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -52,10 +52,6 @@ drupal: ...@@ -52,10 +52,6 @@ drupal:
drupalSettings: drupalSettings:
version: VERSION 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: drupalSettings:
# These placeholder values will be set by system_js_settings_alter(). # These placeholder values will be set by system_js_settings_alter().
path: path:
......
...@@ -51,6 +51,12 @@ public function render(array $js_assets) { ...@@ -51,6 +51,12 @@ public function render(array $js_assets) {
// query-string instead, to enforce reload on every page request. // query-string instead, to enforce reload on every page request.
$default_query_string = $this->state->get('system.css_js_query_string') ?: '0'; $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<!--//--><![CDATA[//><!--\n";
$embed_suffix = "\n//--><!]]>\n";
// Defaults for each SCRIPT element. // Defaults for each SCRIPT element.
$element_defaults = array( $element_defaults = array(
'#type' => 'html_tag', '#type' => 'html_tag',
...@@ -67,13 +73,9 @@ public function render(array $js_assets) { ...@@ -67,13 +73,9 @@ public function render(array $js_assets) {
// Element properties that depend on item type. // Element properties that depend on item type.
switch ($js_asset['type']) { switch ($js_asset['type']) {
case 'setting': case 'setting':
$element['#attributes'] = array( $element['#value_prefix'] = $embed_prefix;
// This type attribute prevents this from being parsed as an $element['#value'] = 'var drupalSettings = ' . Json::encode($js_asset['data']) . ";";
// inline script. $element['#value_suffix'] = $embed_suffix;
'type' => 'application/json',
'data-drupal-selector' => 'drupal-settings-json',
);
$element['#value'] = Json::encode($js_asset['data']);
break; break;
case 'file': case 'file':
......
...@@ -11,6 +11,14 @@ ...@@ -11,6 +11,14 @@
* @prop {number} length=0 * @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. * Variable generated by Drupal that holds all translated strings from PHP.
* *
......
/**
* @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);
}
})();
...@@ -66,7 +66,7 @@ protected function setRawContent($content) { ...@@ -66,7 +66,7 @@ protected function setRawContent($content) {
$this->plainTextContent = NULL; $this->plainTextContent = NULL;
$this->elements = NULL; $this->elements = NULL;
$this->drupalSettings = array(); $this->drupalSettings = array();
if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $content, $matches)) { if (preg_match('/var drupalSettings = (.*?);$/m', $content, $matches)) {
$this->drupalSettings = Json::decode($matches[1]); $this->drupalSettings = Json::decode($matches[1]);
} }
} }
......
...@@ -185,9 +185,9 @@ function testAggregation() { ...@@ -185,9 +185,9 @@ function testAggregation() {
$rendered_footer_js = \Drupal::service('asset.js.collection_renderer')->render($footer_js); $rendered_footer_js = \Drupal::service('asset.js.collection_renderer')->render($footer_js);
$this->assertTrue( $this->assertTrue(
count($rendered_footer_js) == 2 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://', && 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 @@ function testSettings() { ...@@ -206,9 +206,9 @@ function testSettings() {
$rendered_js = $this->renderer->renderPlain($js_render_array); $rendered_js = $this->renderer->renderPlain($js_render_array);
// Parse the generated drupalSettings <script> back to a PHP representation. // Parse the generated drupalSettings <script> back to a PHP representation.
$startToken = '{'; $startToken = 'drupalSettings = ';
$endToken = '}'; $endToken = '}';
$start = strpos($rendered_js, $startToken); $start = strpos($rendered_js, $startToken) + strlen($startToken);
$end = strrpos($rendered_js, $endToken); $end = strrpos($rendered_js, $endToken);
$json = Unicode::substr($rendered_js, $start, $end - $start + 1); $json = Unicode::substr($rendered_js, $start, $end - $start + 1);
$parsed_settings = Json::decode($json); $parsed_settings = Json::decode($json);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment