-
Notifications
You must be signed in to change notification settings - Fork 385
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
Fix extraction of class names from string literals in [class] attributes #5403
Conversation
$classes = array_merge( $classes, $matches[2] ); | ||
$classes = array_merge( | ||
$classes, | ||
preg_split( '/\s+/', trim( implode( ' ', $matches[2] ) ) ) |
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.
Perhaps better to use PREG_SPLIT_NO_EMPTY
here than trim()
?
preg_split( '/\s+/', trim( implode( ' ', $matches[2] ) ) ) | |
preg_split( '/\s+/', implode( ' ', $matches[2] ), -1, PREG_SPLIT_NO_EMPTY ) |
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.
Maybe doesn't matter anyway since below we are filtering out values anyway:
array_unique( array_filter( $classes ) ) |
Plugin builds for 8c5ca67 are ready 🛎️!
|
<style>.sidebar2.visible { display:block }</style> | ||
<style>.sidebar2.visible, .sidebar2.displayed, .sidebar2.shown { display:block }</style> | ||
<style>.sidebar3.open, .sidebar3.abierto { display:block }</style> | ||
<style>.sidebar3.cerrado { display:none }</style> |
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.
We should have tests with tabs and newlines in between classes as well here.
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.
@schlessera The test here is regarding the [class]
attributes. I do indeed have newlines and tabs below:
amp-wp/tests/php/test-amp-style-sanitizer.php
Lines 1616 to 1619 in ee55256
<aside class="sidebar3" [class]='mySidebar.expanded ? " open abierto " : " | |
closed | |
cerrado | |
"'>...</aside> |
One Tiktok embed handler test in |
TikTok's oEmbed endpoint seems to be up now: https://www.tiktok.com/oembed?url=https://www.tiktok.com/@scout2015/video/6718335390845095173 |
Summary
Fixes #5401.
This fixes a regression introduced in #5356 for 2.0.2. Certain sites using
amp-bind
to control whether the mobile nav menu is displayed (for example) would find that if the CSS to toggle visibility would be tree-shaken if the string literals in the amp-bind expression had multiple class names (e.g.'sidebar open'
) or whitespace padding (e.g.' opened '
). Impacted themes include Twenty Seventeen given the[class]
attribute value:"main-navigation" + ( navMenuToggledOn ? " toggled-on" : '' )
. Actually, any theme relying on theAMP_Nav_Menu_Toggle_Sanitizer
(via declaring thenav-menu-toggle
flag foramp
theme support) will have this problem given the padding of' '
used:amp-wp/includes/sanitizers/class-amp-nav-menu-toggle-sanitizer.php
Line 111 in 4cc0990
This regression was introduced (by me) in 0cd5776 with this change:
In particular, the string literals in the
amp-bind
expression were no longer getting split by whitespace when added to the$classes
array.The fix is just to make sure that we split
$matches[2]
into tokenized class names before merging with$classes
Checklist