diff --git a/advagg.inc b/advagg.inc index d72f293..668aa21 100644 --- a/advagg.inc +++ b/advagg.inc @@ -1226,8 +1226,8 @@ function advagg_load_css_stylesheet($file, $optimize, $aggregate_settings = NULL _advagg_build_css_path(NULL, $css_base_url . '/', $aggregate_settings); // Anchor all paths in the CSS with its base URL, ignoring external, - // absolute paths, and urls that start with # (SVG). - $contents = preg_replace_callback('%url\(\s*+[\'"]?+(?![a-z]++:|/|\#+)([^\'"()\s]++)[\'"]?+\s*+\)%i', '_advagg_build_css_path', $contents); + // absolute paths, and urls that start with # or %23 (SVG). + $contents = preg_replace_callback('%url\(\s*+[\'"]?+(?![a-z]++:|/|\#|\%23+)([^\'"()\s]++)[\'"]?+\s*+\)%i', '_advagg_build_css_path', $contents); // Change context back. advagg_context_css($aggregate_settings, 1); diff --git a/tests/advagg.test b/tests/advagg.test index c929484..c8cb4ae 100644 --- a/tests/advagg.test +++ b/tests/advagg.test @@ -18,6 +18,7 @@ class AdvAggCascadingStylesheetsUnitTest extends DrupalUnitTestCase { parent::setUp(array('advagg')); // Include the advagg.module file. drupal_load('module', 'advagg'); + module_load_include('inc', 'advagg', 'advagg'); } function testLoadCssBasic() { @@ -77,6 +78,23 @@ class AdvAggCascadingStylesheetsUnitTest extends DrupalUnitTestCase { // File. Tests: charset*.css // - Any @charaset declaration at the beginning of a file should be // removed without breaking subsequent CSS. + $testfiles = array( + 'charset.css', + 'charset_newline.css', + 'charset_sameline.css', + ); + foreach ($testfiles as $file) { + $file_path = $path . '/' . $file; + $file_url = $GLOBALS['base_url'] . '/' . $file_path; + + $expected = file_get_contents($file_path . '.optimized.css'); + $optimized_output = advagg_load_stylesheet($file_path, TRUE); + $this->assertEqual($optimized_output, $expected, format_string('Optimized CSS file has expected contents (@file)', array('@file' => $file))); + + $expected = file_get_contents($file_path . '.optimized.css'); + $optimized_output_url = advagg_load_stylesheet($file_url, TRUE); + $this->assertEqual($optimized_output_url, $expected, format_string('Optimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file))); + } // File. Tests: advagg*.css // - Various url() tests. @@ -85,21 +103,28 @@ class AdvAggCascadingStylesheetsUnitTest extends DrupalUnitTestCase { // https://www.drupal.org/node/2362643 // https://www.drupal.org/node/2112067 $testfiles = array( - 'charset.css', - 'charset_newline.css', - 'charset_sameline.css', 'advagg.css', ); foreach ($testfiles as $file) { $file_path = $path . '/' . $file; $file_url = $GLOBALS['base_url'] . '/' . $file_path; + $aggregate_settings = array( + 'variables' => array( + 'is_https' => FALSE, + 'base_path' => '/', + 'advagg_convert_absolute_to_relative_path' => TRUE, + 'advagg_convert_absolute_to_protocol_relative_path' => TRUE, + ), + ); $expected = file_get_contents($file_path . '.optimized.css'); - $optimized_output = advagg_load_stylesheet($file_path, TRUE); + $optimized_output = advagg_load_css_stylesheet($file_path, TRUE, $aggregate_settings); + $optimized_output = str_replace('/' . drupal_get_path('module', 'advagg') . '/', '', $optimized_output); $this->assertEqual($optimized_output, $expected, format_string('Optimized CSS file has expected contents (@file)', array('@file' => $file)) . '
' . nl2br($optimized_output)); $expected = file_get_contents($file_path . '.optimized.css'); - $optimized_output_url = advagg_load_stylesheet($file_url, TRUE); + $optimized_output_url = advagg_load_css_stylesheet($file_url, TRUE, $aggregate_settings); + $optimized_output_url = str_replace('/' . drupal_get_path('module', 'advagg') . '/', '', $optimized_output_url); $this->assertEqual($optimized_output_url, $expected, format_string('Optimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file))); } } diff --git a/tests/css_test_files/advagg.css b/tests/css_test_files/advagg.css index 6f0a355..9a570b5 100644 --- a/tests/css_test_files/advagg.css +++ b/tests/css_test_files/advagg.css @@ -14,7 +14,7 @@ home { home { background: url( geo-md-webfont.eot?#iefix ); } -/* https://www.drupal.org/node/2362643*/ +/* https://www.drupal.org/node/2362643 */ home { background: url(#SVGID_1_); } @@ -32,4 +32,10 @@ home { } home { background: url( #a ); -} \ No newline at end of file +} +html { + background-image: url('data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%222000%22%20height%3D%2221%22%20viewBox%3D%22-423.5%200%202000%2021%22%20enable-background%3D%22new%20-423.5%200%202000%2021%22%3E%3ClinearGradient%20id%3D%22SVGID_1_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%221393.788%22%20y1%3D%22404.834%22%20x2%3D%221395.121%22%20y2%3D%22404.834%22%20gradientTransform%3D%22matrix(1500%200%200%20-538%20-2091105%20217811)%22%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22%23fff%22%2F%3E%3Cstop%20offset%3D%22.55%22%20stop-color%3D%22%23fff%22%2F%3E%3Cstop%20offset%3D%22.8%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3C%2FlinearGradient%3E%3Cpath%20fill%3D%22url(%23SVGID_1_)%22%20d%3D%22M-423. +5%200h2000v21h-2000V0z%22%2F%3E%3C%2Fsvg%3E'); +} + + diff --git a/tests/css_test_files/advagg.css.optimized.css b/tests/css_test_files/advagg.css.optimized.css index 2defdc1..9368ec5 100644 --- a/tests/css_test_files/advagg.css.optimized.css +++ b/tests/css_test_files/advagg.css.optimized.css @@ -1 +1,2 @@ -home{background:url('geo-md-webfont.eot?#iefix');}home{background:url('geo-md-webfont.eot?#iefix');}home{background:url("geo-md-webfont.eot?#iefix");}home{background:url(geo-md-webfont.eot?#iefix);}home{background:url(geo-md-webfont.eot?#iefix);}home{background:url(#SVGID_1_);}home{background:url(#a);}home{background:url('#a');}home{background:url("#a");}home{background:url("#a");}home{background:url(#a);} \ No newline at end of file +home{background:url(tests/css_test_files/geo-md-webfont.eot?#iefix);}home{background:url(tests/css_test_files/geo-md-webfont.eot?#iefix);}home{background:url(tests/css_test_files/geo-md-webfont.eot?#iefix);}home{background:url(tests/css_test_files/geo-md-webfont.eot?#iefix);}home{background:url(tests/css_test_files/geo-md-webfont.eot?#iefix);}home{background:url(#SVGID_1_);}home{background:url(#a);}home{background:url('#a');}home{background:url("#a");}home{background:url("#a");}home{background:url(#a);}html{background-image:url('data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%222000%22%20height%3D%2221%22%20viewBox%3D%22-423.5%200%202000%2021%22%20enable-background%3D%22new%20-423.5%200%202000%2021%22%3E%3ClinearGradient%20id%3D%22SVGID_1_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%221393.788%22%20y1%3D%22404.834%22%20x2%3D%221395.121%22%20y2%3D%22404.834%22%20gradientTransform%3D%22matrix(1500%200%200%20-538%20-2091105%20217811)%22%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3Cstop%20offset%3D%220%22%20stop-color%3D%22%23fff%22%2F%3E%3Cstop%20offset%3D%22.55%22%20stop-color%3D%22%23fff%22%2F%3E%3Cstop%20offset%3D%22.8%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22%23fff%22%20stop-opacity%3D%220%22%2F%3E%3C%2FlinearGradient%3E%3Cpath%20fill%3D%22url(%23SVGID_1_)%22%20d%3D%22M-423. +5%200h2000v21h-2000V0z%22%2F%3E%3C%2Fsvg%3E');} \ No newline at end of file