Skip to content

Commit

Permalink
Merge pull request #1198 from Automattic/add/ie-hacks-tests
Browse files Browse the repository at this point in the history
Test a series of CSS Hacks to verify removal.
  • Loading branch information
westonruter authored Jun 5, 2018
2 parents 3443973 + 4987bff commit 5f635d2
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,195 @@ public function test_amp_selector_conversion( $markup, $input, $output ) {
$this->assertEquals( $output, $stylesheets[0] );
}

/**
* Data for testing CSS hack removal.
*
* @return array
*/
public function get_amp_css_hacks_data() {
return array(
array(
'.selector { !property: value; }',
),
array(
'.selector { $property: value; }',
),
array(
'.selector { &property: value; }',
),
array(
'.selector { *property: value; }',
),
array(
'.selector { )property: value; }',
),
array(
'.selector { =property: value; }',
),
array(
'.selector { %property: value; }',
),
array(
'.selector { +property: value; }',
),
array(
'.selector { @property: value; }',
),
array(
'.selector { ,property: value; }',
),
array(
'.selector { .property: value; }',
),
array(
'.selector { /property: value; }',
),
array(
'.selector { `property: value; }',
),
array(
'.selector { ]property: value; }',
),
array(
'.selector { #property: value; }',
),
array(
'.selector { ~property: value; }',
),
array(
'.selector { ?property: value; }',
),
array(
'.selector { :property: value; }',
),
array(
'.selector { |property: value; }',
),
array(
'_::selection, .selector:not([attr*=\'\']) {}',
),
array(
':root .selector {}',
),
array(
'body:last-child .selector {}',
),
array(
'body:nth-of-type(1) .selector {}',
),
array(
'body:first-of-type .selector {}',
),
array(
'.selector:not([attr*=\'\']) {}',
),
array(
'.selector:not(*:root) {}',
),
array(
'.selector:not(*:root) {}',
),
array(
'body:empty .selector {}',
),
array(
'body:last-child .selector, x:-moz-any-link {}',
),
array(
'body:last-child .selector, x:-moz-any-link, x:default {}',
),
array(
'body:not(:-moz-handler-blocked) .selector {}',
),
array(
'_::-moz-progress-bar, body:last-child .selector {}',
),
array(
'_::-moz-range-track, body:last-child .selector {}',
),
array(
'_:-moz-tree-row(hover), .selector {}',
),
array(
'_::selection, .selector:not([attr*=\'\']) {}',
),
array(
'* html .selector {}',
),
array(
'.unused-class.selector {}',
),
array(
'html > body .selector {}',
),
array(
'.selector, {}',
),
array(
'*:first-child+html .selector {}',
),
array(
'.selector, x:-IE7 {}',
),
array(
'*+html .selector {}',
),
array(
'body*.selector {}',
),
array(
'.selector\ {}',
),
array(
'html > /**/ body .selector {}',
),
array(
'head ~ /**/ body .selector {}',
),
array(
'_::selection, .selector:not([attr*=\'\']) {}',
),
array(
':root .selector {}',
),
array(
'body:last-child .selector {}',
),
array(
'body:nth-of-type(1) .selector {}',
),
array(
'body:first-of-type .selector {}',
),
array(
'.selector:not([attr*=\'\']) {}',
),
);
}

/**
* Test removal of IE and Other Browser CSS Hacks
*
* @dataProvider get_amp_css_hacks_data
* @param string $input Hack input CSS rule.
*/
public function test_browser_css_hacks( $input ) {
$html = "<html amp><head><meta charset=utf-8><style amp-custom>$input</style></head><body></body></html>";
$dom = AMP_DOM_Utils::get_dom( $html );

$error_codes = array();
$sanitizer = new AMP_Style_Sanitizer( $dom, array(
'use_document_element' => true,
'remove_unused_rules' => 'never',
'validation_error_callback' => function( $error ) use ( &$error_codes ) {
$error_codes[] = $error['code'];
},
) );
$sanitizer->sanitize();
$actual_stylesheets = array_values( $sanitizer->get_stylesheets() );
$this->assertEmpty( $actual_stylesheets[0] );
}

/**
* Test handling of stylesheets with @font-face that have data: url source.
*
Expand Down

0 comments on commit 5f635d2

Please sign in to comment.