Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.842
diff -u -p -r1.842 common.inc
--- includes/common.inc 5 Jan 2009 22:23:58 -0000 1.842
+++ includes/common.inc 8 Jan 2009 09:57:10 -0000
@@ -2301,7 +2301,9 @@ function drupal_clear_css_cache() {
* performed using this function:
*
* - Add a file ('file'):
- * Adds a reference to a JavaScript file to the page.
+ * Adds a reference to a JavaScript file to the page. This may either be a file
+ * relative to base_path(), or the absolute path to a JavaScript file hosted
+ * externally.
*
* - Add inline JavaScript code ('inline'):
* Executes a piece of JavaScript code on the current page by placing the code
@@ -2322,11 +2324,13 @@ function drupal_clear_css_cache() {
* drupal_add_js('$(document).ready(function(){alert("Hello!");});',
* array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
* );
+ * drupal_add_js('http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js');
* @endcode
*
* @param $data
* (optional) If given, the value depends on the $options parameter:
- * - 'file': Path to the file relative to base_path().
+ * - 'file': Path to the file relative to base_path(), or the absolute path
+ * to a JavaScript file hosted externally.
* - 'inline': The JavaScript code that should be placed in the given scope.
* - 'setting': An array with configuration options as associative array. The
* array is directly placed in Drupal.settings. All modules should wrap
@@ -2371,7 +2375,8 @@ function drupal_clear_css_cache() {
* a JavaScript file. Defaults to TRUE.
* - preprocess
* Aggregate the JavaScript if the JavaScript optimization setting has
- * been toggled in admin/settings/performance. Defaults to TRUE.
+ * been toggled in admin/settings/performance. Defaults to TRUE for
+ * internal files and FALSE for external files.
* @param $reset
* (optional) Resets the currently loaded JavaScript.
* @return
@@ -2390,8 +2395,17 @@ function drupal_add_js($data = NULL, $op
else {
$options = array();
}
+
+ // Store the original value of preprocess for external file JavaScript default.
+ $preprocess = isset($options['preprocess']) ? $options['preprocess'] : NULL;
+
$options += drupal_js_defaults($data);
+ // Default external JavaScript files preprocess to FALSE.
+ if (is_null($preprocess) && $options['type'] == 'file' && filter_var($data, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
+ $options['preprocess'] = FALSE;
+ }
+
// Preprocess can only be set if caching is enabled.
$options['preprocess'] = $options['cache'] ? $options['preprocess'] : FALSE;
@@ -2559,7 +2573,8 @@ function drupal_get_js($scope = 'header'
case 'file':
if (!$item['preprocess'] || !$is_writable || !$preprocess_js) {
- $no_preprocess .= '\n";
+ $prefix = filter_var($item['data'], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) ? '' : base_path();
+ $no_preprocess .= '\n";
}
else {
$files[$item['data']] = $item;
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.19
diff -u -p -r1.19 common.test
--- modules/simpletest/tests/common.test 26 Dec 2008 21:01:57 -0000 1.19
+++ modules/simpletest/tests/common.test 8 Jan 2009 09:57:11 -0000
@@ -470,6 +470,17 @@ class JavaScriptTestCase extends DrupalW
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'simpletest.js') < strpos($javascript, 'misc/tableselect.js'), t('Altering JavaScript weight through the alter hook.'));
}
+
+ /**
+ * Test rendering an external JavaScript file.
+ */
+ function testRenderExternal() {
+ $external = 'http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js';
+ drupal_add_js($external);
+ $javascript = drupal_get_js();
+ // Local files have a base_path() prefix, external files should not.
+ $this->assertTrue(strpos($javascript, 'src="' . $external) > 0, t('Rendering an external JavaScript file.'));
+ }
}
/**