From 4987bff1f3fa55b246b01fad4119a7756c3e3caa Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 5 Jun 2018 11:38:03 +0200 Subject: [PATCH] Test a series of CSS Hacks to verify removal. --- tests/test-amp-style-sanitizer.php | 189 +++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/tests/test-amp-style-sanitizer.php b/tests/test-amp-style-sanitizer.php index a0f67374fbe..cad9deba9a5 100644 --- a/tests/test-amp-style-sanitizer.php +++ b/tests/test-amp-style-sanitizer.php @@ -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 = ""; + $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. *