Skip to content

Commit

Permalink
Eliminate obsolete -moz-document url-prefix() Firefox hack transforma…
Browse files Browse the repository at this point in the history
…tion

Fixes #5801
  • Loading branch information
westonruter committed Mar 20, 2021
1 parent 23415aa commit df59c71
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
21 changes: 2 additions & 19 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ private function get_parsed_stylesheet( $stylesheet, $options = [] ) {
$parsed = null;
$cache_key = null;
$cached = true;
$cache_group = 'amp-parsed-stylesheet-v35'; // This should be bumped whenever the PHP-CSS-Parser is updated or parsed format is updated.
$cache_group = 'amp-parsed-stylesheet-v36'; // This should be bumped whenever the PHP-CSS-Parser is updated or parsed format is updated.
$use_transients = $this->should_use_transient_caching();

$cache_impacting_options = array_merge(
Expand Down Expand Up @@ -2157,24 +2157,7 @@ private function process_css_list( CSSList $css_list, $options ) {
$this->process_css_declaration_block( $css_item, $css_list, $options )
);
} elseif ( $css_item instanceof AtRuleBlockList ) {
if (
'-moz-document' === $css_item->atRuleName()
&&
'url-prefix()' === $css_item->atRuleArgs()
&&
in_array( 'supports', $options['allowed_at_rules'], true )
) {
// Replace `@-moz-document url-prefix()` with `@supports (-moz-appearance:meterbar)` as an alternative
// way to provide Firefox-specific style rules. This is a workaround since @-moz-document is not
// yet allowed in AMP, and this use of @supports is another recognized Firefox-specific CSS hack,
// per <http://browserhacks.com/#hack-8e9b5504d9fda44ec75169381b3c3157>.
// For adding @-moz-document to AMP, see <https://github.com/ampproject/amphtml/issues/26406>.
$new_css_item = new AtRuleBlockList( 'supports', '(-moz-appearance:meterbar)' );
$new_css_item->setContents( $css_item->getContents() );
$this->replace_inside_css_list( $css_list, $css_item, [ $new_css_item ] );
$css_item = $new_css_item; // To process_css_list below.
$sanitized = false;
} elseif ( ! in_array( $css_item->atRuleName(), $options['allowed_at_rules'], true ) ) {
if ( ! in_array( $css_item->atRuleName(), $options['allowed_at_rules'], true ) ) {
$error = [
'code' => self::CSS_SYNTAX_INVALID_AT_RULE,
'at_rule' => $css_item->atRuleName(),
Expand Down
6 changes: 3 additions & 3 deletions tests/php/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ public function get_body_style_attribute_data() {
'<style>@charset "UTF-8"; @charset "UTF-8"; @charset "UTF-8"; html:lang(zz){ color: gray; } @media screen and ( max-width: 640px ) { body { font-size: small; } } @font-face { font-family: "Open Sans"; src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"); } @-moz-document url-prefix() { body { color:red; } } @supports (display: grid) { div { display: grid; } } @-moz-keyframes appear { from { opacity: 0.0; } to { opacity: 1.0; } } @keyframes appear { from { opacity: 0.0; } to { opacity: 1.0; } }</style><div></div>',
'<div></div>',
[
'@media screen and ( max-width: 640px ){body{font-size:small}}@font-face{font-family:"Open Sans";src:url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2")}@supports (-moz-appearance:meterbar){body{color:red}}@supports (display: grid){div{display:grid}}@-moz-keyframes appear{from{opacity:0}to{opacity:1}}@keyframes appear{from{opacity:0}to{opacity:1}}',
'@media screen and ( max-width: 640px ){body{font-size:small}}@font-face{font-family:"Open Sans";src:url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2")}@-moz-document url-prefix(){body{color:red}}@supports (display: grid){div{display:grid}}@-moz-keyframes appear{from{opacity:0}to{opacity:1}}@keyframes appear{from{opacity:0}to{opacity:1}}',
],
],

'moz_document_transformed' => [
'moz_document_allowed' => [
'
<style>
@-moz-document url-prefix() {
Expand All @@ -228,7 +228,7 @@ public function get_body_style_attribute_data() {
<div class="entry"><div class="entry-content"><p class="has-drop-cap">Hello</p></div></div>
',
[
'@supports (-moz-appearance:meterbar){.entry .entry-content .has-drop-cap:not(:focus):first-letter{margin-top:.2em}}',
'@-moz-document url-prefix(){.entry .entry-content .has-drop-cap:not(:focus):first-letter{margin-top:.2em}}',
],
[
AMP_Style_Sanitizer::CSS_SYNTAX_INVALID_PROPERTY_NOLIST,
Expand Down

0 comments on commit df59c71

Please sign in to comment.