Skip to content
This repository
Browse code

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
Fred Condo authored
10  tests/forms/RequirementsTest.php
@@ -18,8 +18,10 @@ function testExternalUrls() {
18 18
 
19 19
 		$backend->javascript('http://www.mydomain.com/test.js');
20 20
 		$backend->javascript('https://www.mysecuredomain.com/test.js');
  21
+		$backend->javascript('//scheme-relative.example.com/test.js');
21 22
 		$backend->css('http://www.mydomain.com/test.css');
22 23
 		$backend->css('https://www.mysecuredomain.com/test.css');
  24
+		$backend->css('//scheme-relative.example.com/test.css');
23 25
 		
24 26
 		$html = $backend->includeInHTML(false, self::$html_template);
25 27
 		
@@ -32,6 +34,10 @@ function testExternalUrls() {
32 34
 			'Load external secure javascript URL'
33 35
 		);
34 36
 		$this->assertTrue(
  37
+			(strpos($html, '//scheme-relative.example.com/test.js') !== false), 
  38
+			'Load external scheme-relative javascript URL'
  39
+		);
  40
+		$this->assertTrue(
35 41
 			(strpos($html, 'http://www.mydomain.com/test.css') !== false), 
36 42
 			'Load external CSS URL'
37 43
 		);
@@ -39,6 +45,10 @@ function testExternalUrls() {
39 45
 			(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false), 
40 46
 			'Load external secure CSS URL'
41 47
 		);
  48
+		$this->assertTrue(
  49
+			(strpos($html, '//scheme-relative.example.com/test.css') !== false), 
  50
+			'Load scheme-relative CSS URL'
  51
+		);
42 52
 	}
43 53
 
44 54
 	protected function setupCombinedRequirements($backend) {
2  view/Requirements.php
@@ -784,7 +784,7 @@ public function add_i18n_javascript($langDir, $return = false, $langOnly = false
784 784
 	 * @return string|boolean 
785 785
 	 */
786 786
 	protected function path_for_file($fileOrUrl) {
787  
-		if(preg_match('/^http[s]?/', $fileOrUrl)) {
  787
+		if(preg_match('{^//|http[s]?}', $fileOrUrl)) {
788 788
 			return $fileOrUrl;
789 789
 		} elseif(Director::fileExists($fileOrUrl)) {
790 790
 			$prefix = Director::baseURL();

0 notes on commit 3e07822

Please sign in to comment.
Something went wrong with that request. Please try again.