Skip to content

Commit c6c5dbb

Browse files
adamzieldmsnellgzioloSoeancostdev
authored
WP_HTML_Tag_Processor: Inject dynamic data to block HTML markup in PHP (#42485)
Introduce WP_HTML_Tag_Processor for reliably modifying HTML attributes. Dynamic blocks often need to inject a CSS class name or set <img src /> in the rendered block HTML markup but lack the means to do so. WP_HTML_Tag_Processor solves this problem. It scans through an HTML document to find specific tags, then transforms those tags by adding, removing, or updating the values of the HTML attributes within that tag (opener). Importantly, it does not fully parse HTML or _recurse_ into the HTML structure. Instead WP_HTML_Tag_Processor scans linearly through a document and only parses the HTML tag openers. Example: ``` $p = new WP_HTML_Tag_Processor('<div id="first"><img /></div>'); $p->next_tag('img')->set_attribute('src', '/wp-content/logo.png'); echo $p; // <div id="first"><img src="/wp-content/logo.png" /></div> ``` For more details and context, see the original GitHub Pull Request at #42485 and the overview issue at #44410. Co-authored-by: Adam Zieliński <[email protected]> Co-authored-by: Dennis Snell <[email protected]> Co-authored-by: Grzegorz Ziółkowski <[email protected]> Co-authored-by: Sören Wrede <[email protected]> Co-authored-by: Colin Stewart <[email protected]>
1 parent 4152d53 commit c6c5dbb

File tree

6 files changed

+2614
-0
lines changed

6 files changed

+2614
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* HTML Tag Processor: Attribute token structure class.
4+
*
5+
* @package WordPress
6+
* @subpackage HTML
7+
* @since 6.1.0
8+
*/
9+
10+
/**
11+
* Data structure for the attribute token that allows to drastically improve performance.
12+
*
13+
* This class is for internal usage of the WP_HTML_Tag_Processor class.
14+
*
15+
* @access private
16+
* @since 6.1.0
17+
*
18+
* @see WP_HTML_Tag_Processor
19+
*/
20+
class WP_HTML_Attribute_Token {
21+
/**
22+
* Attribute name.
23+
*
24+
* @since 6.1.0
25+
* @var string
26+
*/
27+
public $name;
28+
29+
/**
30+
* Attribute value.
31+
*
32+
* @since 6.1.0
33+
* @var int
34+
*/
35+
public $value_starts_at;
36+
37+
/**
38+
* How many bytes the value occupies in the input HTML.
39+
*
40+
* @since 6.1.0
41+
* @var int
42+
*/
43+
public $value_length;
44+
45+
/**
46+
* The string offset where the attribute name starts.
47+
*
48+
* @since 6.1.0
49+
* @var int
50+
*/
51+
public $start;
52+
53+
/**
54+
* The string offset after the attribute value or its name.
55+
*
56+
* @since 6.1.0
57+
* @var int
58+
*/
59+
public $end;
60+
61+
/**
62+
* Whether the attribute is a boolean attribute with value `true`.
63+
*
64+
* @since 6.1.0
65+
* @var bool
66+
*/
67+
public $is_true;
68+
69+
/**
70+
* Constructor.
71+
*
72+
* @since 6.1.0
73+
*
74+
* @param string $name Attribute name.
75+
* @param int $value_start Attribute value.
76+
* @param int $value_length Number of bytes attribute value spans.
77+
* @param int $start The string offset where the attribute name starts.
78+
* @param int $end The string offset after the attribute value or its name.
79+
* @param bool $is_true Whether the attribute is a boolean attribute with true value.
80+
*/
81+
public function __construct( $name, $value_start, $value_length, $start, $end, $is_true ) {
82+
$this->name = $name;
83+
$this->value_starts_at = $value_start;
84+
$this->value_length = $value_length;
85+
$this->start = $start;
86+
$this->end = $end;
87+
$this->is_true = $is_true;
88+
}
89+
}

0 commit comments

Comments
 (0)