Skip to content

Commit 2861660

Browse files
committed
Document the decoder functions for text nodes and attribute values.
1 parent faae963 commit 2861660

1 file changed

Lines changed: 63 additions & 2 deletions

File tree

src/wp-includes/html-api/class-wp-html-decoder.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WP_HTML_Decoder {
2323
* true === WP_HTML_Decoder::attribute_starts_with( $value, 0, null, 'http:', 'case-insensitive' );
2424
* false === WP_HTML_Decoder::attribute_starts_with( $value, 0, null, 'https:', 'case-insensitive' );
2525
*
26-
* The `$value_at` and `$value_length` parameters may be used to avoid string allocations when the
26+
* Use the `$value_at` and `$value_length` parameters to avoid string allocations when the
2727
* attribute value is found within a larger string.
2828
*
2929
* Example:
@@ -41,13 +41,14 @@ class WP_HTML_Decoder {
4141
* false === WP_HTML_Decode::attribute_starts_with( $html, $href[0], $href[1], 'http://', 'case-insensitive' );
4242
* false === WP_HTML_Decode::attribute_starts_with( $html, $title[0], $title[1], '&copy' );
4343
*
44+
* @since 6.6.0
45+
*
4446
* @param string $raw_haystack String containing the raw non-decoded attribute value.
4547
* @param int $value_at How many bytes into the haystack where the attribute value begins.
4648
* @param int $value_length How many bytes the attribute value spans.
4749
* Passing `null` indicates that the value spans to the end of the string.
4850
* @param string $search_text Does the attribute value start with this plain string.
4951
* @param ?string $case_sensitivity Set to `case-insensitive` to ignore ASCII case when matching.
50-
*
5152
* @return bool Whether the attribute value starts with the given string.
5253
*/
5354
public static function attribute_starts_with( $raw_haystack, $value_at, $value_length, $search_text, $case_sensitivity = 'case-sensitive' ) {
@@ -91,14 +92,74 @@ public static function attribute_starts_with( $raw_haystack, $value_at, $value_l
9192
return true;
9293
}
9394

95+
/**
96+
* Returns a string containing the decoded value of a given HTML text node,
97+
* supplied with the optional starting byte offset and length of node.
98+
*
99+
* Example:
100+
*
101+
* '“😄”' === WP_HTML_Decode::decode_text_node( '“😄&#x94' );
102+
*
103+
* Use the `$at` and `$length` parameters to avoid string allocations when the
104+
* text node is found within a larger string. Note that the `$at` and `$length`
105+
* values should specify the full span of the text node and not end before the
106+
* text node ends. This is because the rules for decoding character references
107+
* change at the end of a string vs. in the middle of one.
108+
*
109+
* Example:
110+
*
111+
* 'Read more…' === WP_HTML_Decoder::decode_text_node( '<a>Read more&hellip;</a>', 3, 17 );
112+
*
113+
* @since 6.6.0
114+
*
115+
* @param string $text Text containing raw and non-decoded text node to decode.
116+
* @param ?int $at Byte offset into `$text` where text node begins. Defaults to start of `$text`.
117+
* @param ?int $length How many bytes the text node spans. Defaults to end of `$text`.
118+
* @return string Decoded value of given text node.
119+
*/
94120
public static function decode_text_node( $text, $at = 0, $length = null ) {
95121
return static::decode( true, $text, $at, $length );
96122
}
97123

124+
/**
125+
* Returns a string containing the decoded value of a given HTML attribute,
126+
* supplied with the optional starting byte offset and length of attribute value.
127+
*
128+
* Example:
129+
*
130+
* '“😄”' === WP_HTML_Decode::decode_attribute( '&#x93;&#x1f604;&#x94' );
131+
*
132+
* Use the `$at` and `$length` parameters to avoid string allocations when the
133+
* attribute value is found within a larger string. Note that the `$at` and
134+
* `$length` values should specify the full span of the attribute value and not
135+
* end before the attribute value ends. This is because the rules for decoding
136+
* character references change at the end of a string vs. in the middle of one.
137+
*
138+
* Example:
139+
*
140+
* $link = WP_HTML_Decoder::decode_attribute( '<a href="https&colon;//wordpress.org">', 9, 27 );
141+
* $link === 'https://wordpress.org';
142+
*
143+
* @since 6.6.0
144+
*
145+
* @param string $text Text containing raw and non-decoded attribute value to decode.
146+
* @param ?int $at Byte offset into `$text` where attribute value begins. Defaults to start of `$text`.
147+
* @param ?int $length How many bytes the attribute value spans. Defaults to end of `$text`.
148+
* @return string Decoded value of given attribute value.
149+
*/
98150
public static function decode_attribute( $text, $at = 0, $length = null ) {
99151
return static::decode( false, $text, $at, $length );
100152
}
101153

154+
/**
155+
* Decodes a span of HTML text, respecting the ambiguous ampersand rule.
156+
*
157+
* @param $allow_ambiguous_ampersands
158+
* @param $text
159+
* @param $at
160+
* @param $length
161+
* @return mixed|string
162+
*/
102163
public static function decode( $allow_ambiguous_ampersands, $text, $at = 0, $length = null ) {
103164
$decoded = '';
104165
$end = isset( $length ) ? $at + $length : strlen( $text );

0 commit comments

Comments
 (0)