Description
openedon Jan 4, 2023
What problem does this address?
The WP_HTML_Tag_processor
class allows setting and getting of generic attribute values via its set_attribute
and get_attribute
methods, respectively. (Changes made via set_attribute
can be persisted to the underlying HTML by calling the get_updated_html
method.)
For class names, it provides the add_class
and remove_class
methods. They include some extra logic to make sure that e.g. class names are deduplicated, and that upon removing the last existing class name, the class
attribute is removed altogether.
This comes in very handy for the Block Interactivity API project, where we're implementing a wp-class
directive, with semantics as follows:
<div wp-class:red="myblock.someValue" class="blue"></div>
is transformed to:
<div wp-class:red="myblock.truthyValue" class="blue red"></div>
As a next step, we'd like to implement a wp-style
directive:
<div wp-style:color="myblock.someValue" style="background: blue;"></div>
should be transformed to:
<div
wp-style:color="myblock.greenValue"
style="background: blue; color: green;"
></div>
I can of course implement the relevant logic manually in the Block Interactivity API SSR handling code, but I wanted to ask if y'all @adamziel @dmsnell would be open to implement it "natively" in the WP_HTML_Tag_Processor
. Arguably, it's just syntactic sugar, but since there's already some logic for class
, I thought it might make sense to at least discuss style
😄
What is your proposed solution?
Probably some API like
$p->get_style( 'color' );
$p->set_style( 'color', 'green' );
$p->remove_style( 'color' );