Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow scheme-relative URLs in requirements

The Requirements class currently treats only absolute URLs as URLs, and
tries to interpret anything else as a filesystem path. This prevents
using scheme-relative URLs for requirements.

Example:
<% require javascript(//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js) %>

This forces the unfortunate choice of not using a CDN for common
scripts, always using an https absolute URL, or accepting that some
browsers will throw a security warning when viewing the site in https.

This change allows scheme-relative URLs & updates RequirementsTest.
  • Loading branch information...
commit 3e0782267c80407e7ce7ee025ce8ea209cd9d597 1 parent 9ebac90
@oddnoc oddnoc authored
Showing with 11 additions and 1 deletion.
  1. +10 −0 tests/forms/RequirementsTest.php
  2. +1 −1  view/Requirements.php
View
10 tests/forms/RequirementsTest.php
@@ -18,8 +18,10 @@ function testExternalUrls() {
$backend->javascript('http://www.mydomain.com/test.js');
$backend->javascript('https://www.mysecuredomain.com/test.js');
+ $backend->javascript('//scheme-relative.example.com/test.js');
$backend->css('http://www.mydomain.com/test.css');
$backend->css('https://www.mysecuredomain.com/test.css');
+ $backend->css('//scheme-relative.example.com/test.css');
$html = $backend->includeInHTML(false, self::$html_template);
@@ -32,6 +34,10 @@ function testExternalUrls() {
'Load external secure javascript URL'
);
$this->assertTrue(
+ (strpos($html, '//scheme-relative.example.com/test.js') !== false),
+ 'Load external scheme-relative javascript URL'
+ );
+ $this->assertTrue(
(strpos($html, 'http://www.mydomain.com/test.css') !== false),
'Load external CSS URL'
);
@@ -39,6 +45,10 @@ function testExternalUrls() {
(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false),
'Load external secure CSS URL'
);
+ $this->assertTrue(
+ (strpos($html, '//scheme-relative.example.com/test.css') !== false),
+ 'Load scheme-relative CSS URL'
+ );
}
protected function setupCombinedRequirements($backend) {
View
2  view/Requirements.php
@@ -784,7 +784,7 @@ public function add_i18n_javascript($langDir, $return = false, $langOnly = false
* @return string|boolean
*/
protected function path_for_file($fileOrUrl) {
- if(preg_match('/^http[s]?/', $fileOrUrl)) {
+ if(preg_match('{^//|http[s]?}', $fileOrUrl)) {
return $fileOrUrl;
} elseif(Director::fileExists($fileOrUrl)) {
$prefix = Director::baseURL();
Please sign in to comment.
Something went wrong with that request. Please try again.