Skip to content

Commit

Permalink
Discontinue preserving original style[amp-custom]
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 31, 2020
1 parent 2e7fa75 commit d4c266a
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,13 +1233,8 @@ private function process_style_element( DOMElement $element ) {
'imported_font_urls' => $parsed['imported_font_urls'],
];

if ( $element->hasAttribute( 'amp-custom' ) && ! $this->amp_custom_style_element ) {
$this->amp_custom_style_element = $element;
} else {

// Remove from DOM since we'll be adding it to amp-custom.
$element->parentNode->removeChild( $element );
}
// Remove from DOM since we'll be adding it to a newly-created style[amp-custom] element later.
$element->parentNode->removeChild( $element );

$this->set_current_node( null );
}
Expand Down Expand Up @@ -2624,14 +2619,6 @@ private function finalize_styles() {

// Add style[amp-custom] to document.
if ( $stylesheet_groups[ self::STYLE_AMP_CUSTOM_GROUP_INDEX ]['included_count'] > 0 ) {

// Ensure style[amp-custom] is present in the document.
if ( ! $this->amp_custom_style_element ) {
$this->amp_custom_style_element = $this->dom->createElement( 'style' );
$this->amp_custom_style_element->setAttribute( 'amp-custom', '' );
$this->dom->head->appendChild( $this->amp_custom_style_element );
}

/*
* On AMP-first themes when there are new/rejected validation errors present, a parsed stylesheet may include
* @import rules. These must be moved to the beginning to be honored.
Expand All @@ -2641,16 +2628,11 @@ private function finalize_styles() {
$css .= implode( '', $this->get_stylesheets() );
$css .= $stylesheet_groups[ self::STYLE_AMP_CUSTOM_GROUP_INDEX ]['source_map_comment'];

/*
* Let the style[amp-custom] be populated with the concatenated CSS.
* !important: Updating the contents of this style element by setting textContent is not
* reliable across PHP/libxml versions, so this is why the children are removed and the
* text node is then explicitly added containing the CSS.
*/
while ( $this->amp_custom_style_element->firstChild ) {
$this->amp_custom_style_element->removeChild( $this->amp_custom_style_element->firstChild );
}
// Create the style[amp-custom] element and add it to the <head>.
$this->amp_custom_style_element = $this->dom->createElement( 'style' );
$this->amp_custom_style_element->setAttribute( 'amp-custom', '' );
$this->amp_custom_style_element->appendChild( $this->dom->createTextNode( $css ) );
$this->dom->head->appendChild( $this->amp_custom_style_element );
}

/*
Expand Down

0 comments on commit d4c266a

Please sign in to comment.