-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore admin bar on AMP pages and improve AMP menu items #1219
Changes from all commits
1be7ae1
475642d
eed2c26
3188501
80ebbcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -542,6 +542,12 @@ private function process_style_element( DOMElement $element ) { | |
$stylesheet = trim( $element->textContent ); | ||
$cdata_spec = $is_keyframes ? $this->style_keyframes_cdata_spec : $this->style_custom_cdata_spec; | ||
|
||
// Honor the style's media attribute. | ||
$media = $element->getAttribute( 'media' ); | ||
if ( $media && 'all' !== $media ) { | ||
$stylesheet = sprintf( '@media %s { %s }', $media, $stylesheet ); | ||
} | ||
|
||
$stylesheet = $this->process_stylesheet( $stylesheet, array( | ||
'allowed_at_rules' => $cdata_spec['css_spec']['allowed_at_rules'], | ||
'property_whitelist' => $cdata_spec['css_spec']['allowed_declarations'], | ||
|
@@ -693,7 +699,7 @@ private function fetch_external_stylesheet( $url ) { | |
private function process_stylesheet( $stylesheet, $options = array() ) { | ||
$parsed = null; | ||
$cache_key = null; | ||
$cache_group = 'amp-parsed-stylesheet-v8'; | ||
$cache_group = 'amp-parsed-stylesheet-v9'; | ||
|
||
$cache_impacting_options = array_merge( | ||
wp_array_slice_assoc( | ||
|
@@ -1306,6 +1312,10 @@ private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_l | |
|
||
if ( $ruleset instanceof DeclarationBlock ) { | ||
$this->ampify_ruleset_selectors( $ruleset ); | ||
if ( 0 === count( $ruleset->getSelectors() ) ) { | ||
$css_list->remove( $ruleset ); | ||
return $results; | ||
} | ||
} | ||
|
||
// Remove disallowed properties. | ||
|
@@ -1606,7 +1616,13 @@ function( Selector $old_selector ) { | |
$ruleset->getSelectors() | ||
) ); | ||
$important_ruleset->setRules( $importants ); | ||
$css_list->append( $important_ruleset ); // @todo It would be preferable if the important ruleset were inserted adjacent to the original rule. | ||
|
||
$i = array_search( $ruleset, $css_list->getContents(), true ); | ||
if ( false !== $i ) { | ||
$css_list->splice( $i + 1, 0, array( $important_ruleset ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was originally an error from this line, but running
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is expected 👍 |
||
} else { | ||
$css_list->append( $important_ruleset ); | ||
} | ||
|
||
return $results; | ||
} | ||
|
@@ -1827,15 +1843,23 @@ private function finalize_styles() { | |
} | ||
|
||
/** | ||
* Convert CSS selectors. | ||
* Convert CSS selectors and remove obsolete selector hacks for IE. | ||
* | ||
* @param DeclarationBlock $ruleset Ruleset. | ||
*/ | ||
private function ampify_ruleset_selectors( $ruleset ) { | ||
$selectors = array(); | ||
$replacements = 0; | ||
$selectors = array(); | ||
$changes = 0; | ||
foreach ( $ruleset->getSelectors() as $old_selector ) { | ||
$edited_selectors = array( $old_selector->getSelector() ); | ||
$selector = $old_selector->getSelector(); | ||
|
||
// Automatically tree-shake IE6/IE7 hacks for selectors with `* html` and `*+html`. | ||
if ( preg_match( '/^\*\s*\+?\s*html/', $selector ) ) { | ||
$changes++; | ||
continue; | ||
} | ||
|
||
$edited_selectors = array( $selector ); | ||
foreach ( $this->selector_mappings as $html_selector => $amp_selectors ) { // Note: The $selector_mappings array contains ~6 items. | ||
$html_pattern = '/(?<=^|[^a-z0-9_-])' . preg_quote( $html_selector ) . '(?=$|[^a-z0-9_-])/i'; | ||
foreach ( $edited_selectors as &$edited_selector ) { // Note: The $edited_selectors array contains only item in the normal case. | ||
|
@@ -1850,7 +1874,7 @@ private function ampify_ruleset_selectors( $ruleset ) { | |
if ( ! $count ) { | ||
continue; | ||
} | ||
$replacements += $count; | ||
$changes += $count; | ||
while ( ! empty( $amp_selectors ) ) { // Note: This array contains only a couple items. | ||
$amp_selector = array_shift( $amp_selectors ); | ||
$edited_selectors[] = preg_replace( $html_pattern, $amp_selector, $original_selector, -1, $count ); | ||
|
@@ -1860,7 +1884,7 @@ private function ampify_ruleset_selectors( $ruleset ) { | |
$selectors = array_merge( $selectors, $edited_selectors ); | ||
} | ||
|
||
if ( $replacements > 0 ) { | ||
if ( $changes > 0 ) { | ||
$ruleset->setSelectors( $selectors ); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great to have the admin bar enabled by default, with this checkbox to disable it.
Admins might be willing to ignore the AMP validation error of CSS over 50KB in order to have the admin bar. Of course, that error wouldn't even appear for non logged-in users