Skip to content

Commit

Permalink
Adapt tree-shaker to keep CSS that is targeted by toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Oct 8, 2019
1 parent 81a15cf commit 1f86538
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 1 addition & 3 deletions includes/sanitizers/class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,11 +977,9 @@ static function() use ( $args ) {
<?php endif; ?>

<?php if ( 'twentytwenty' === get_template() ) : ?>
.cover-modal,
.active {
.cover-modal {
display: inherit;
}
}
<?php elseif ( 'twentyseventeen' === get_template() ) : ?>
/* Show the button*/
.no-js .menu-toggle {
Expand Down
24 changes: 23 additions & 1 deletion includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,30 @@ private function get_used_class_names() {
$classes .= ' ' . $class_attribute->nodeValue;
}

// Find all [class] attributes and capture the contents of any single- or double-quoted strings.
// Find all [class] attributes and capture the contents of any single- or double-quoted strings,
// as well as <amp-state> content that might be used.
foreach ( $this->xpath->query( '//*/@' . AMP_DOM_Utils::AMP_BIND_DATA_ATTR_PREFIX . 'class' ) as $bound_class_attribute ) {
if ( preg_match_all( '/([a-zA-Z0-9_]+?)\[\s*([\'"])([^\2]*?)\2\s*\]/', $bound_class_attribute->nodeValue, $matches ) ) {
$amp_states = $matches[1];
$properties = $matches[3];

foreach ( $amp_states as $index => $amp_state ) {
$state_element = $this->xpath->query( "//amp-state[@id = '{$amp_state}' ]/script" )->item( 0 );

if ( ! $state_element ) {
continue;
}

$state_data = json_decode( $state_element->textContent, true );

if ( isset( $state_data[ $properties[ $index ] ] ) ) {
$classes .= ' ' . $state_data[ $properties[ $index ] ];
}
}

continue;
}

if ( preg_match_all( '/([\'"])([^\1]*?)\1/', $bound_class_attribute->nodeValue, $matches ) ) {
$classes .= ' ' . implode( ' ', $matches[2] );
}
Expand Down

0 comments on commit 1f86538

Please sign in to comment.