From d4c266a230d702daaf5a6358b0af17b08f4c5b34 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 30 Jan 2020 20:23:52 -0800 Subject: [PATCH] Discontinue preserving original style[amp-custom] --- .../sanitizers/class-amp-style-sanitizer.php | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/includes/sanitizers/class-amp-style-sanitizer.php b/includes/sanitizers/class-amp-style-sanitizer.php index 872843b54f9..fe12a766767 100644 --- a/includes/sanitizers/class-amp-style-sanitizer.php +++ b/includes/sanitizers/class-amp-style-sanitizer.php @@ -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 ); } @@ -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. @@ -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 . + $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 ); } /*