-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add a has_class
method to the public WP_HTML_Tag_Processor API
#46232
Conversation
As a broad thought I'm a bit skeptical of this even though I suggested it. For one, I feel like it's starting to get confusing the way we have For example, the example in the docblock shouldn't find I'd like to be sure we really need this before we introduce a second way to query class names. Im worried that the normative use-case while ( $tags->next_tag() ) {
if ( $tags->has_class( '…' ) ) {
…
}
} If we do go this route I'd be curious about building it as a separate system than the one shared by the query engine. Build a proper decoded, though of course as we saw earlier that's much more complicated. It's something I think we could make a more justified case for out of the main loop, but again something I want to know that we need before we put it out there and encourage people to use it. |
@dmsnell I refactored this one to use
What do you mean by not accurate? It used the same code as the query matcher, so should be accurate to that extent.
The code example was correct, it used the string offset arguments to limit the search to the
If that happened then yeah, it would be problematic. I don't think it will happen, though. People can already ditch the query matcher and manually match the tag names, and I didn't see it happening. We'll know for sure after 6.2 is released and the WP_HTML_Tag_processor gets noticed in the wider community, but I remain optimistic.
This may be worth a shot. At the same time, I'd wait for more feature requests to help shape the scope. |
} | ||
|
||
$classes = $this->get_attribute( 'class' ); | ||
if ( null === $classes ) { |
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.
note that if $this->is_closing_tag()
then get_attribute()
will return null
as well, so we may not need to double-check $this->is_closing_tag
Concerning being more wrong I'm glad it was just the example code and not the real code that was wrong. Concerning the value of the API I'm still skeptical, especially as long as we're not handling entities properly. I'd really like to know this is wanted and necessary before we add it. Counterpoint is something like most operations I've seen related to classes simply need to seek to a tag with a given class, and then they add or remove classes not based on whether a class exists, but whether or not some value in the attribute set exists. |
I'll close this PR for now. |
Part of #44410
What?
Adds a
has_class
method to WP_HTML_Tag_Processor to provide an idiomatic, nuance-aware way of testing for the presence of a CSS class name:Testing Instructions
cc @dmsnell @getdave @aristath