Skip to content

Commit

Permalink
Add data: url to test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
amedina committed May 26, 2018
1 parent 46eb0dd commit 9fef5b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
42 changes: 20 additions & 22 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,17 @@ private function parse_stylesheet( $stylesheet_string, $options = array() ) {
$css_parser = new Sabberworm\CSS\Parser( $stylesheet_string, $parser_settings );
$css_document = $css_parser->parse();

$this->normalize_urls(
array_filter(
$css_document->getAllValues(),
function ( $value ) {
return $value instanceof URL;
}
),
$options['stylesheet_url']
);
if ( ! empty( $options['stylesheet_url'] ) ) {
$this->real_path_urls(
array_filter(
$css_document->getAllValues(),
function ( $value ) {
return $value instanceof URL;
}
),
$options['stylesheet_url']
);
}

$validation_errors = $this->process_css_list( $css_document, $options );

Expand Down Expand Up @@ -794,7 +796,7 @@ private function add_calc_placeholders( $css ) {
*/
private function remove_spaces_from_data_urls( $css ) {
return preg_replace_callback(
'/\burl\([^;}]*?\)/',
'/\burl\([^}]*?\)/',
function( $matches ) {
return preg_replace( '/\s+/', '', $matches[0] );
},
Expand Down Expand Up @@ -886,27 +888,23 @@ private function process_css_list( CSSList $css_list, $options ) {
}

/**
* Convert URLs in to non-relative normalized paths and prevent there being any spaces.
* Convert URLs in to non-relative real-paths.
*
* @param URL[] $urls URLs.
* @param string $stylesheet_url Stylesheet URL. Base URL is obtained from this. Optional.
* @param string $stylesheet_url Stylesheet URL.
*/
private function normalize_urls( $urls, $stylesheet_url = '' ) {
$base_url = $stylesheet_url ? preg_replace( ':[^/]+(\?.*)?(#.*)?$:', '', $stylesheet_url ) : '';
private function real_path_urls( $urls, $stylesheet_url = '' ) {
$base_url = preg_replace( ':[^/]+(\?.*)?(#.*)?$:', '', $stylesheet_url );
if ( empty( $base_url ) ) {
return;
}

foreach ( $urls as $url ) {
// URLs cannot have spaces in them, so strip them (especially when spaces get erroneously injected in data: URLs).
$url_string = preg_replace( '/\s+/', '', $url->getURL()->getString() );
$url_string = $url->getURL()->getString();

// For data: URLs, all that is needed is to remove spaces so set and continue.
if ( 'data:' === substr( $url_string, 0, 5 ) ) {
$url->getURL()->setString( $url_string );
continue;
}

// If the base URL is empty, there is nothing more to do.
if ( empty( $base_url ) ) {
$url->getURL()->setString( $url_string );
continue;
}

Expand Down
10 changes: 7 additions & 3 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,23 +686,27 @@ public function test_get_validated_url_file_path( $source, $expected, $error_cod
*/
public function get_data_urls() {
return array(
'url_with_spaces' => array(
'url_with_spaces' => array(
'html { background-image:url(url with spaces.png); }',
'html{background-image:url("urlwithspaces.png")}',
),
'data_url_with_spaces' => array(
'html { background: url(data:image/png; base64, ivborw0kggoaaaansuheugaaacwaaaascamaaaapwqozaaaabgdbtueaalgpc/xhbqaaaafzukdcak7ohokaaaamuexurczmzpf399fx1+bm5mzy9amaaadisurbvdjlvzxbesmgces5/p8/t9furvcrmu73jwlzosgsiizurcjo/ad+eqjjb4hv8bft+idpqocx1wjosbfhh2xssxeiyn3uli/6mnree07uiwjev8ueowds88ly97kqytlijkktuybbruayvh5wohixmpi5we58ek028czwyuqdlkpg1bkb4nnm+veanfhqn1k4+gpt6ugqcvu2h2ovuif/gwufyy8owepdyzsa3avcqpvovvzzz2vtnn2wu8qzvjddeto90gsy9mvlqtgysy231mxry6i2ggqjrty0l8fxcxfcbbhwrsyyaaaaaelftksuqmcc); }',
'html{background:url("data:image/png;base64,ivborw0kggoaaaansuheugaaacwaaaascamaaaapwqozaaaabgdbtueaalgpc/xhbqaaaafzukdcak7ohokaaaamuexurczmzpf399fx1+bm5mzy9amaaadisurbvdjlvzxbesmgces5/p8/t9furvcrmu73jwlzosgsiizurcjo/ad+eqjjb4hv8bft+idpqocx1wjosbfhh2xssxeiyn3uli/6mnree07uiwjev8ueowds88ly97kqytlijkktuybbruayvh5wohixmpi5we58ek028czwyuqdlkpg1bkb4nnm+veanfhqn1k4+gpt6ugqcvu2h2ovuif/gwufyy8owepdyzsa3avcqpvovvzzz2vtnn2wu8qzvjddeto90gsy9mvlqtgysy231mxry6i2ggqjrty0l8fxcxfcbbhwrsyyaaaaaelftksuqmcc")}',
),
);
}

/**
* Test handling of stylesheets with spaces in the background-image URLs.
*
* @dataProvider get_data_urls
* @covers AMP_Style_Sanitizer::normalize_urls()
* @covers AMP_Style_Sanitizer::remove_spaces_from_data_urls()
*
* @param string $source Source URL string.
* @param string|null $expected Expected normalized URL string.
*/
public function test_normalize_urls( $source, $expected ) {
public function test_remove_spaces_from_data_urls( $source, $expected ) {
$html = '<html><head><style>';
$html .= $source;
$html .= '</style></head</html>';
Expand Down

0 comments on commit 9fef5b0

Please sign in to comment.