Issue #1140356 by iamEAP, ericduran, Rob Loach, arnested: Added support for async property to script tags.
parent
9ba8724f3a
commit
b29fc4b26b
|
@ -3967,6 +3967,8 @@ function drupal_region_class($region) {
|
||||||
* which drupal_add_js() happened earlier in the page request.
|
* which drupal_add_js() happened earlier in the page request.
|
||||||
* - defer: If set to TRUE, the defer attribute is set on the <script>
|
* - defer: If set to TRUE, the defer attribute is set on the <script>
|
||||||
* tag. Defaults to FALSE.
|
* tag. Defaults to FALSE.
|
||||||
|
* - async: If set to TRUE, the async attribute is set on the <script>
|
||||||
|
* tag. Defaults to FALSE.
|
||||||
* - cache: If set to FALSE, the JavaScript file is loaded anew on every page
|
* - cache: If set to FALSE, the JavaScript file is loaded anew on every page
|
||||||
* call; in other words, it is not cached. Used only when 'type' references
|
* call; in other words, it is not cached. Used only when 'type' references
|
||||||
* a JavaScript file. Defaults to TRUE.
|
* a JavaScript file. Defaults to TRUE.
|
||||||
|
@ -4039,6 +4041,7 @@ function drupal_add_js($data = NULL, $options = NULL) {
|
||||||
'preprocess' => TRUE,
|
'preprocess' => TRUE,
|
||||||
'cache' => TRUE,
|
'cache' => TRUE,
|
||||||
'defer' => FALSE,
|
'defer' => FALSE,
|
||||||
|
'async' => FALSE,
|
||||||
'browsers' => array(),
|
'browsers' => array(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -4086,6 +4089,7 @@ function drupal_js_defaults($data = NULL) {
|
||||||
'scope' => 'header',
|
'scope' => 'header',
|
||||||
'cache' => TRUE,
|
'cache' => TRUE,
|
||||||
'defer' => FALSE,
|
'defer' => FALSE,
|
||||||
|
'async' => FALSE,
|
||||||
'preprocess' => TRUE,
|
'preprocess' => TRUE,
|
||||||
'version' => NULL,
|
'version' => NULL,
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
|
@ -4280,10 +4284,17 @@ function drupal_pre_render_scripts($elements) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The defer attribute must not be specified if src is not defined.
|
// The defer and async attributes must not be specified if the src
|
||||||
if (!empty($item['defer']) && isset($element['#attributes']['src'])) {
|
// attribute is not present.
|
||||||
|
if (!empty($element['#attributes']['src'])) {
|
||||||
|
// Both may be specified for legacy browser fallback purposes.
|
||||||
|
if (!empty($item['async'])) {
|
||||||
|
$element['#attributes']['async'] = 'async';
|
||||||
|
}
|
||||||
|
if (!empty($item['defer'])) {
|
||||||
$element['#attributes']['defer'] = 'defer';
|
$element['#attributes']['defer'] = 'defer';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$elements[] = $element;
|
$elements[] = $element;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1180,6 +1180,40 @@ class CommonJavaScriptTestCase extends WebTestBase {
|
||||||
$this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), t('Added an external JavaScript file.'));
|
$this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), t('Added an external JavaScript file.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests adding external JavaScript Files with the async attribute.
|
||||||
|
*/
|
||||||
|
function testAsyncAttribute() {
|
||||||
|
$default_query_string = variable_get('css_js_query_string', '0');
|
||||||
|
|
||||||
|
drupal_add_js('http://example.com/script.js', array('async' => TRUE));
|
||||||
|
drupal_add_js('core/misc/collapse.js', array('async' => TRUE));
|
||||||
|
$javascript = drupal_get_js();
|
||||||
|
|
||||||
|
$expected_1 = '<script type="text/javascript" src="http://example.com/script.js?' . $default_query_string . '" async="async"></script>';
|
||||||
|
$expected_2 = '<script type="text/javascript" src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '" async="async"></script>';
|
||||||
|
|
||||||
|
$this->assertTrue(strpos($javascript, $expected_1) > 0, t('Rendered external JavaScript with correct async attribute.'));
|
||||||
|
$this->assertTrue(strpos($javascript, $expected_2) > 0, t('Rendered internal JavaScript with correct async attribute.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests adding external JavaScript Files with the defer attribute.
|
||||||
|
*/
|
||||||
|
function testDeferAttribute() {
|
||||||
|
$default_query_string = variable_get('css_js_query_string', '0');
|
||||||
|
|
||||||
|
drupal_add_js('http://example.com/script.js', array('defer' => TRUE));
|
||||||
|
drupal_add_js('core/misc/collapse.js', array('defer' => TRUE));
|
||||||
|
$javascript = drupal_get_js();
|
||||||
|
|
||||||
|
$expected_1 = '<script type="text/javascript" src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>';
|
||||||
|
$expected_2 = '<script type="text/javascript" src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '" defer="defer"></script>';
|
||||||
|
|
||||||
|
$this->assertTrue(strpos($javascript, $expected_1) > 0, t('Rendered external JavaScript with correct defer attribute.'));
|
||||||
|
$this->assertTrue(strpos($javascript, $expected_2) > 0, t('Rendered internal JavaScript with correct defer attribute.'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test drupal_get_js() for JavaScript settings.
|
* Test drupal_get_js() for JavaScript settings.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue