CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
HTML API: Introduce wp_split_class_names(). #10043
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
base: trunk
Are you sure you want to change the base?
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
b1fa4c5
to
e0a9bff
Compare
* @return Generator Use this in a foreach loop to iterate over the class names. | ||
*/ | ||
function wp_split_class_names( $class_attribute_string ) { |
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.
* @return Generator Use this in a foreach loop to iterate over the class names. | |
*/ | |
function wp_split_class_names( $class_attribute_string ) { | |
* @return Generator<non-empty-string> Use this in a foreach loop to iterate over the class names. | |
*/ | |
function wp_split_class_names( $class_attribute_string ): Generator { |
Naturally, the same should be applied to \WP_HTML_Tag_Processor::class_list()
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.
Something
function wp_split_class_names( $class_attribute_string ): ?Generator {
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.
Given that this function contains yield
statements, I don't think it ever can return null
. It will always return Generator
. See https://3v4l.org/YgDnb
@@ -0,0 +1,59 @@ | |||
<?php | |||
|
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.
* @return Generator Use this in a foreach loop to iterate over the class names. | ||
*/ | ||
function wp_split_class_names( $class_attribute_string ) { | ||
if ( '' === $class_attribute_string ) { |
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.
if ( '' === $class_attribute_string ) { | |
if ( '' === $class_attribute_string || ! is_string( $class_attribute_string ) ) { |
} | ||
|
||
// Get these from the HTML API to avoid ad-hoc parsing HTML or CSS class names. | ||
$processor = new WP_HTML_Tag_Processor( '<wp-noop>' ); |
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.
If WP_HTML_Tag_Processor
or class_list()
can throw exceptions? Consider try/catch or input validation to avoid fatal errors.
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.
They should not throw exceptions.
e2b7b9b
to
a561a69
Compare
This patch introduces a new CSS helper module containing a new function, `wp_split_class_names()`. This function wraps some code to rely on the HTML API to take an HTML `class` attribute value and return a `Generator` to iterate over the classes in that value. Many existing functions perform ad-hoc parsing of CSS class names, usually by splitting on a space character. However, there are issues with this approach: - There is no decoding of HTML character references, which is normative inside HTML attributes. - There is no handling of null bytes. - Class names can be split by more than just the space character. - There is no handling of duplicates, and while mostly benign, code forgetting to account for duplicates can lead to defects. The new function handles the nuances to let developers focus on reading CSS class names, adding new class names, and removing class names. This serves a middleground between legacy code interacting with CSS class names in isolation and code processing full HTML documents.
a561a69
to
a5c2582
Compare
Trac ticket: Core-63694.
This patch introduces a new CSS helper module containing a new function,
wp_split_class_names()
. This function wraps some code to rely on the HTML API to take an HTMLclass
attribute value and return aGenerator
to iterate over the classes in that value.Design review requests
class
attribute no-quirks-mode HTML and it decodes HTML entities. It also MUST represent a fullclass
attribute because the parsing of trailing character references which are missing a semicolon or otherwise incomplete is dependent on whether they fall at the end of a string.wp_classname_walker()
wp_walk_class_attribute()
wp_unique_classnames()
classnames()
in JS? We could pass varargs which arestring|false
or an array of additional class names to add.Background
Many existing functions perform ad-hoc parsing of CSS class names, usually by splitting on a space character. However, there are issues with this approach:
The new function handles the nuances to let developers focus on reading CSS class names, adding new class names, and removing class names. This serves a middleground between legacy code interacting with CSS class names in isolation and code processing full HTML documents.