Skip to content

Commit 5137da0

Browse files
authored
HTML API: Backport updates from Core, fix Interactivity API
Fix: Update Interactivity API Use of Private HTML API Classes to avoid Crash Previously the HTML API was using a mixture of approaches in its helper classes for describing spans of text. One was (start, end) and the other was (start, length). In WordPress/wordpress-develop#5721 these were all normalized to (start, length) but the Interactivity API was not updated in concert with that change. In this patch the `inner_html` methods are updated to use the appropriate formats. this PR also includes the necessary back port of the HTML API to provide the new methods so that older WP installs don’t crash
1 parent 419d754 commit 5137da0

6 files changed

Lines changed: 2727 additions & 4 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* HTML API: WP_HTML_Attribute_Token class
4+
*
5+
* @package WordPress
6+
* @subpackage HTML-API
7+
* @since 6.2.0
8+
*/
9+
10+
/**
11+
* Core class used by the HTML tag processor as a data structure for the attribute token,
12+
* allowing to drastically improve performance.
13+
*
14+
* This class is for internal usage of the WP_HTML_Tag_Processor class.
15+
*
16+
* @access private
17+
* @since 6.2.0
18+
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
19+
*
20+
* @see WP_HTML_Tag_Processor
21+
*/
22+
class Gutenberg_HTML_Attribute_Token_6_5 {
23+
/**
24+
* Attribute name.
25+
*
26+
* @since 6.2.0
27+
*
28+
* @var string
29+
*/
30+
public $name;
31+
32+
/**
33+
* Attribute value.
34+
*
35+
* @since 6.2.0
36+
*
37+
* @var int
38+
*/
39+
public $value_starts_at;
40+
41+
/**
42+
* How many bytes the value occupies in the input HTML.
43+
*
44+
* @since 6.2.0
45+
*
46+
* @var int
47+
*/
48+
public $value_length;
49+
50+
/**
51+
* The string offset where the attribute name starts.
52+
*
53+
* @since 6.2.0
54+
*
55+
* @var int
56+
*/
57+
public $start;
58+
59+
/**
60+
* Byte length of text spanning the attribute inside a tag.
61+
*
62+
* This span starts at the first character of the attribute name
63+
* and it ends after one of three cases:
64+
*
65+
* - at the end of the attribute name for boolean attributes.
66+
* - at the end of the value for unquoted attributes.
67+
* - at the final single or double quote for quoted attributes.
68+
*
69+
* Example:
70+
*
71+
* <div class="post">
72+
* ------------ length is 12, including quotes
73+
*
74+
* <input type="checked" checked id="selector">
75+
* ------- length is 6
76+
*
77+
* <a rel=noopener>
78+
* ------------ length is 11
79+
*
80+
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
81+
*
82+
* @var int
83+
*/
84+
public $length;
85+
86+
/**
87+
* Whether the attribute is a boolean attribute with value `true`.
88+
*
89+
* @since 6.2.0
90+
*
91+
* @var bool
92+
*/
93+
public $is_true;
94+
95+
/**
96+
* Constructor.
97+
*
98+
* @since 6.2.0
99+
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
100+
*
101+
* @param string $name Attribute name.
102+
* @param int $value_start Attribute value.
103+
* @param int $value_length Number of bytes attribute value spans.
104+
* @param int $start The string offset where the attribute name starts.
105+
* @param int $length Byte length of the entire attribute name or name and value pair expression.
106+
* @param bool $is_true Whether the attribute is a boolean attribute with true value.
107+
*/
108+
public function __construct( $name, $value_start, $value_length, $start, $length, $is_true ) {
109+
$this->name = $name;
110+
$this->value_starts_at = $value_start;
111+
$this->value_length = $value_length;
112+
$this->start = $start;
113+
$this->length = $length;
114+
$this->is_true = $is_true;
115+
}
116+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* HTML API: WP_HTML_Span class
4+
*
5+
* @package WordPress
6+
* @subpackage HTML-API
7+
* @since 6.2.0
8+
*/
9+
10+
/**
11+
* Core class used by the HTML tag processor to represent a textual span
12+
* inside an HTML document.
13+
*
14+
* This is a two-tuple in disguise, used to avoid the memory overhead
15+
* involved in using an array for the same purpose.
16+
*
17+
* This class is for internal usage of the WP_HTML_Tag_Processor class.
18+
*
19+
* @access private
20+
* @since 6.2.0
21+
* @since 6.5.0 Replaced `end` with `length` to more closely align with `substr()`.
22+
*
23+
* @see WP_HTML_Tag_Processor
24+
*/
25+
class Gutenberg_HTML_Span_6_5 {
26+
/**
27+
* Byte offset into document where span begins.
28+
*
29+
* @since 6.2.0
30+
*
31+
* @var int
32+
*/
33+
public $start;
34+
35+
/**
36+
* Byte length of this span.
37+
*
38+
* @since 6.5.0
39+
*
40+
* @var int
41+
*/
42+
public $length;
43+
44+
/**
45+
* Constructor.
46+
*
47+
* @since 6.2.0
48+
*
49+
* @param int $start Byte offset into document where replacement span begins.
50+
* @param int $length Byte length of span.
51+
*/
52+
public function __construct( $start, $length ) {
53+
$this->start = $start;
54+
$this->length = $length;
55+
}
56+
}

0 commit comments

Comments
 (0)