first commit
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Active_Formatting_Elements class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML processor during HTML parsing
|
||||
* for managing the stack of active formatting elements.
|
||||
*
|
||||
* This class is designed for internal use by the HTML processor.
|
||||
*
|
||||
* > Initially, the list of active formatting elements is empty.
|
||||
* > It is used to handle mis-nested formatting element tags.
|
||||
* >
|
||||
* > The list contains elements in the formatting category, and markers.
|
||||
* > The markers are inserted when entering applet, object, marquee,
|
||||
* > template, td, th, and caption elements, and are used to prevent
|
||||
* > formatting from "leaking" into applet, object, marquee, template,
|
||||
* > td, th, and caption elements.
|
||||
* >
|
||||
* > In addition, each element in the list of active formatting elements
|
||||
* > is associated with the token for which it was created, so that
|
||||
* > further elements can be created for that token if necessary.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#list-of-active-formatting-elements
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Active_Formatting_Elements {
|
||||
/**
|
||||
* Holds the stack of active formatting element references.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @var WP_HTML_Token[]
|
||||
*/
|
||||
private $stack = array();
|
||||
|
||||
/**
|
||||
* Reports if a specific node is in the stack of active formatting elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $token Look for this node in the stack.
|
||||
* @return bool Whether the referenced node is in the stack of active formatting elements.
|
||||
*/
|
||||
public function contains_node( $token ) {
|
||||
foreach ( $this->walk_up() as $item ) {
|
||||
if ( $token->bookmark_name === $item->bookmark_name ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns how many nodes are currently in the stack of active formatting elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @return int How many node are in the stack of active formatting elements.
|
||||
*/
|
||||
public function count() {
|
||||
return count( $this->stack );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the node at the end of the stack of active formatting elements,
|
||||
* if one exists. If the stack is empty, returns null.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @return WP_HTML_Token|null Last node in the stack of active formatting elements, if one exists, otherwise null.
|
||||
*/
|
||||
public function current_node() {
|
||||
$current_node = end( $this->stack );
|
||||
|
||||
return $current_node ? $current_node : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes a node onto the stack of active formatting elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#push-onto-the-list-of-active-formatting-elements
|
||||
*
|
||||
* @param WP_HTML_Token $token Push this node onto the stack.
|
||||
*/
|
||||
public function push( $token ) {
|
||||
/*
|
||||
* > If there are already three elements in the list of active formatting elements after the last marker,
|
||||
* > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and
|
||||
* > attributes as element, then remove the earliest such element from the list of active formatting
|
||||
* > elements. For these purposes, the attributes must be compared as they were when the elements were
|
||||
* > created by the parser; two elements have the same attributes if all their parsed attributes can be
|
||||
* > paired such that the two attributes in each pair have identical names, namespaces, and values
|
||||
* > (the order of the attributes does not matter).
|
||||
*
|
||||
* @todo Implement the "Noah's Ark clause" to only add up to three of any given kind of formatting elements to the stack.
|
||||
*/
|
||||
// > Add element to the list of active formatting elements.
|
||||
$this->stack[] = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a node from the stack of active formatting elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $token Remove this node from the stack, if it's there already.
|
||||
* @return bool Whether the node was found and removed from the stack of active formatting elements.
|
||||
*/
|
||||
public function remove_node( $token ) {
|
||||
foreach ( $this->walk_up() as $position_from_end => $item ) {
|
||||
if ( $token->bookmark_name !== $item->bookmark_name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$position_from_start = $this->count() - $position_from_end - 1;
|
||||
array_splice( $this->stack, $position_from_start, 1 );
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Steps through the stack of active formatting elements, starting with the
|
||||
* top element (added first) and walking downwards to the one added last.
|
||||
*
|
||||
* This generator function is designed to be used inside a "foreach" loop.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $html = '<em><strong><a>We are here';
|
||||
* foreach ( $stack->walk_down() as $node ) {
|
||||
* echo "{$node->node_name} -> ";
|
||||
* }
|
||||
* > EM -> STRONG -> A ->
|
||||
*
|
||||
* To start with the most-recently added element and walk towards the top,
|
||||
* see WP_HTML_Active_Formatting_Elements::walk_up().
|
||||
*
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public function walk_down() {
|
||||
$count = count( $this->stack );
|
||||
|
||||
for ( $i = 0; $i < $count; $i++ ) {
|
||||
yield $this->stack[ $i ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Steps through the stack of active formatting elements, starting with the
|
||||
* bottom element (added last) and walking upwards to the one added first.
|
||||
*
|
||||
* This generator function is designed to be used inside a "foreach" loop.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $html = '<em><strong><a>We are here';
|
||||
* foreach ( $stack->walk_up() as $node ) {
|
||||
* echo "{$node->node_name} -> ";
|
||||
* }
|
||||
* > A -> STRONG -> EM ->
|
||||
*
|
||||
* To start with the first added element and walk towards the bottom,
|
||||
* see WP_HTML_Active_Formatting_Elements::walk_down().
|
||||
*
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public function walk_up() {
|
||||
for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
|
||||
yield $this->stack[ $i ];
|
||||
}
|
||||
}
|
||||
}
|
||||
116
wp-includes/html-api/class-wp-html-attribute-token.php
Normal file
116
wp-includes/html-api/class-wp-html-attribute-token.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Attribute_Token class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML tag processor as a data structure for the attribute token,
|
||||
* allowing to drastically improve performance.
|
||||
*
|
||||
* This class is for internal usage of the WP_HTML_Tag_Processor class.
|
||||
*
|
||||
* @access private
|
||||
* @since 6.2.0
|
||||
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
|
||||
*
|
||||
* @see WP_HTML_Tag_Processor
|
||||
*/
|
||||
class WP_HTML_Attribute_Token {
|
||||
/**
|
||||
* Attribute name.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Attribute value.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $value_starts_at;
|
||||
|
||||
/**
|
||||
* How many bytes the value occupies in the input HTML.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $value_length;
|
||||
|
||||
/**
|
||||
* The string offset where the attribute name starts.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $start;
|
||||
|
||||
/**
|
||||
* Byte length of text spanning the attribute inside a tag.
|
||||
*
|
||||
* This span starts at the first character of the attribute name
|
||||
* and it ends after one of three cases:
|
||||
*
|
||||
* - at the end of the attribute name for boolean attributes.
|
||||
* - at the end of the value for unquoted attributes.
|
||||
* - at the final single or double quote for quoted attributes.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* <div class="post">
|
||||
* ------------ length is 12, including quotes
|
||||
*
|
||||
* <input type="checked" checked id="selector">
|
||||
* ------- length is 6
|
||||
*
|
||||
* <a rel=noopener>
|
||||
* ------------ length is 11
|
||||
*
|
||||
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $length;
|
||||
|
||||
/**
|
||||
* Whether the attribute is a boolean attribute with value `true`.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $is_true;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
|
||||
*
|
||||
* @param string $name Attribute name.
|
||||
* @param int $value_start Attribute value.
|
||||
* @param int $value_length Number of bytes attribute value spans.
|
||||
* @param int $start The string offset where the attribute name starts.
|
||||
* @param int $length Byte length of the entire attribute name or name and value pair expression.
|
||||
* @param bool $is_true Whether the attribute is a boolean attribute with true value.
|
||||
*/
|
||||
public function __construct( $name, $value_start, $value_length, $start, $length, $is_true ) {
|
||||
$this->name = $name;
|
||||
$this->value_starts_at = $value_start;
|
||||
$this->value_length = $value_length;
|
||||
$this->start = $start;
|
||||
$this->length = $length;
|
||||
$this->is_true = $is_true;
|
||||
}
|
||||
}
|
||||
461
wp-includes/html-api/class-wp-html-decoder.php
Normal file
461
wp-includes/html-api/class-wp-html-decoder.php
Normal file
@@ -0,0 +1,461 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* HTML API: WP_HTML_Decoder class
|
||||
*
|
||||
* Decodes spans of raw text found inside HTML content.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.6.0
|
||||
*/
|
||||
class WP_HTML_Decoder {
|
||||
/**
|
||||
* Indicates if an attribute value starts with a given raw string value.
|
||||
*
|
||||
* Use this method to determine if an attribute value starts with a given string, regardless
|
||||
* of how it might be encoded in HTML. For instance, `http:` could be represented as `http:`
|
||||
* or as `http:` or as `http:` or as `http:`, or in many other ways.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $value = 'http://wordpress.org/';
|
||||
* true === WP_HTML_Decoder::attribute_starts_with( $value, 'http:', 'ascii-case-insensitive' );
|
||||
* false === WP_HTML_Decoder::attribute_starts_with( $value, 'https:', 'ascii-case-insensitive' );
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param string $haystack String containing the raw non-decoded attribute value.
|
||||
* @param string $search_text Does the attribute value start with this plain string.
|
||||
* @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching.
|
||||
* Default 'case-sensitive'.
|
||||
* @return bool Whether the attribute value starts with the given string.
|
||||
*/
|
||||
public static function attribute_starts_with( $haystack, $search_text, $case_sensitivity = 'case-sensitive' ) {
|
||||
$search_length = strlen( $search_text );
|
||||
$loose_case = 'ascii-case-insensitive' === $case_sensitivity;
|
||||
$haystack_end = strlen( $haystack );
|
||||
$search_at = 0;
|
||||
$haystack_at = 0;
|
||||
|
||||
while ( $search_at < $search_length && $haystack_at < $haystack_end ) {
|
||||
$chars_match = $loose_case
|
||||
? strtolower( $haystack[ $haystack_at ] ) === strtolower( $search_text[ $search_at ] )
|
||||
: $haystack[ $haystack_at ] === $search_text[ $search_at ];
|
||||
|
||||
$is_introducer = '&' === $haystack[ $haystack_at ];
|
||||
$next_chunk = $is_introducer
|
||||
? self::read_character_reference( 'attribute', $haystack, $haystack_at, $token_length )
|
||||
: null;
|
||||
|
||||
// If there's no character reference and the characters don't match, the match fails.
|
||||
if ( null === $next_chunk && ! $chars_match ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If there's no character reference but the character do match, then it could still match.
|
||||
if ( null === $next_chunk && $chars_match ) {
|
||||
++$haystack_at;
|
||||
++$search_at;
|
||||
continue;
|
||||
}
|
||||
|
||||
// If there is a character reference, then the decoded value must exactly match what follows in the search string.
|
||||
if ( 0 !== substr_compare( $search_text, $next_chunk, $search_at, strlen( $next_chunk ), $loose_case ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The character reference matched, so continue checking.
|
||||
$haystack_at += $token_length;
|
||||
$search_at += strlen( $next_chunk );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string containing the decoded value of a given HTML text node.
|
||||
*
|
||||
* Text nodes appear in HTML DATA sections, which are the text segments inside
|
||||
* and around tags, excepting SCRIPT and STYLE elements (and some others),
|
||||
* whose inner text is not decoded. Use this function to read the decoded
|
||||
* value of such a text span in an HTML document.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* '“😄”' === WP_HTML_Decode::decode_text_node( '“😄”' );
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param string $text Text containing raw and non-decoded text node to decode.
|
||||
* @return string Decoded UTF-8 value of given text node.
|
||||
*/
|
||||
public static function decode_text_node( $text ) {
|
||||
return static::decode( 'data', $text );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string containing the decoded value of a given HTML attribute.
|
||||
*
|
||||
* Text found inside an HTML attribute has different parsing rules than for
|
||||
* text found inside other markup, or DATA segments. Use this function to
|
||||
* read the decoded value of an HTML string inside a quoted attribute.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* '“😄”' === WP_HTML_Decode::decode_attribute( '“😄”' );
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param string $text Text containing raw and non-decoded attribute value to decode.
|
||||
* @return string Decoded UTF-8 value of given attribute value.
|
||||
*/
|
||||
public static function decode_attribute( $text ) {
|
||||
return static::decode( 'attribute', $text );
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a span of HTML text, depending on the context in which it's found.
|
||||
*
|
||||
* This is a low-level method; prefer calling WP_HTML_Decoder::decode_attribute() or
|
||||
* WP_HTML_Decoder::decode_text_node() instead. It's provided for cases where this
|
||||
* may be difficult to do from calling code.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* '©' = WP_HTML_Decoder::decode( 'data', '©' );
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
|
||||
* @param string $text Text document containing span of text to decode.
|
||||
* @return string Decoded UTF-8 string.
|
||||
*/
|
||||
public static function decode( $context, $text ) {
|
||||
$decoded = '';
|
||||
$end = strlen( $text );
|
||||
$at = 0;
|
||||
$was_at = 0;
|
||||
|
||||
while ( $at < $end ) {
|
||||
$next_character_reference_at = strpos( $text, '&', $at );
|
||||
if ( false === $next_character_reference_at || $next_character_reference_at >= $end ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$character_reference = self::read_character_reference( $context, $text, $next_character_reference_at, $token_length );
|
||||
if ( isset( $character_reference ) ) {
|
||||
$at = $next_character_reference_at;
|
||||
$decoded .= substr( $text, $was_at, $at - $was_at );
|
||||
$decoded .= $character_reference;
|
||||
$at += $token_length;
|
||||
$was_at = $at;
|
||||
continue;
|
||||
}
|
||||
|
||||
++$at;
|
||||
}
|
||||
|
||||
if ( 0 === $was_at ) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
if ( $was_at < $end ) {
|
||||
$decoded .= substr( $text, $was_at, $end - $was_at );
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to read a character reference at the given location in a given string,
|
||||
* depending on the context in which it's found.
|
||||
*
|
||||
* If a character reference is found, this function will return the translated value
|
||||
* that the reference maps to. It will then set `$match_byte_length` the
|
||||
* number of bytes of input it read while consuming the character reference. This
|
||||
* gives calling code the opportunity to advance its cursor when traversing a string
|
||||
* and decoding.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* null === WP_HTML_Decoder::read_character_reference( 'attribute', 'Ships…', 0 );
|
||||
* '…' === WP_HTML_Decoder::read_character_reference( 'attribute', 'Ships…', 5, $token_length );
|
||||
* 8 === $token_length; // `…`
|
||||
*
|
||||
* null === WP_HTML_Decoder::read_character_reference( 'attribute', '¬in', 0 );
|
||||
* '∉' === WP_HTML_Decoder::read_character_reference( 'attribute', '∉', 0, $token_length );
|
||||
* 7 === $token_length; // `∉`
|
||||
*
|
||||
* '¬' === WP_HTML_Decoder::read_character_reference( 'data', '¬in', 0, $token_length );
|
||||
* 4 === $token_length; // `¬`
|
||||
* '∉' === WP_HTML_Decoder::read_character_reference( 'data', '∉', 0, $token_length );
|
||||
* 7 === $token_length; // `∉`
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
|
||||
* @param string $text Text document containing span of text to decode.
|
||||
* @param int $at Optional. Byte offset into text where span begins, defaults to the beginning (0).
|
||||
* @param int &$match_byte_length Optional. Set to byte-length of character reference if provided and if a match
|
||||
* is found, otherwise not set. Default null.
|
||||
* @return string|false Decoded character reference in UTF-8 if found, otherwise `false`.
|
||||
*/
|
||||
public static function read_character_reference( $context, $text, $at = 0, &$match_byte_length = null ) {
|
||||
/**
|
||||
* Mappings for HTML5 named character references.
|
||||
*
|
||||
* @var WP_Token_Map $html5_named_character_references
|
||||
*/
|
||||
global $html5_named_character_references;
|
||||
|
||||
$length = strlen( $text );
|
||||
if ( $at + 1 >= $length ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( '&' !== $text[ $at ] ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Numeric character references.
|
||||
*
|
||||
* When truncated, these will encode the code point found by parsing the
|
||||
* digits that are available. For example, when `🅰` is truncated
|
||||
* to `DZ` it will encode `DZ`. It does not:
|
||||
* - know how to parse the original `🅰`.
|
||||
* - fail to parse and return plaintext `DZ`.
|
||||
* - fail to parse and return the replacement character `<60>`
|
||||
*/
|
||||
if ( '#' === $text[ $at + 1 ] ) {
|
||||
if ( $at + 2 >= $length ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Tracks inner parsing within the numeric character reference. */
|
||||
$digits_at = $at + 2;
|
||||
|
||||
if ( 'x' === $text[ $digits_at ] || 'X' === $text[ $digits_at ] ) {
|
||||
$numeric_base = 16;
|
||||
$numeric_digits = '0123456789abcdefABCDEF';
|
||||
$max_digits = 6; // 
|
||||
++$digits_at;
|
||||
} else {
|
||||
$numeric_base = 10;
|
||||
$numeric_digits = '0123456789';
|
||||
$max_digits = 7; // 
|
||||
}
|
||||
|
||||
// Cannot encode invalid Unicode code points. Max is to U+10FFFF.
|
||||
$zero_count = strspn( $text, '0', $digits_at );
|
||||
$digit_count = strspn( $text, $numeric_digits, $digits_at + $zero_count );
|
||||
$after_digits = $digits_at + $zero_count + $digit_count;
|
||||
$has_semicolon = $after_digits < $length && ';' === $text[ $after_digits ];
|
||||
$end_of_span = $has_semicolon ? $after_digits + 1 : $after_digits;
|
||||
|
||||
// `&#` or `&#x` without digits returns into plaintext.
|
||||
if ( 0 === $digit_count && 0 === $zero_count ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Whereas `&#` and only zeros is invalid.
|
||||
if ( 0 === $digit_count ) {
|
||||
$match_byte_length = $end_of_span - $at;
|
||||
return '<27>';
|
||||
}
|
||||
|
||||
// If there are too many digits then it's not worth parsing. It's invalid.
|
||||
if ( $digit_count > $max_digits ) {
|
||||
$match_byte_length = $end_of_span - $at;
|
||||
return '<27>';
|
||||
}
|
||||
|
||||
$digits = substr( $text, $digits_at + $zero_count, $digit_count );
|
||||
$code_point = intval( $digits, $numeric_base );
|
||||
|
||||
/*
|
||||
* Noncharacters, 0x0D, and non-ASCII-whitespace control characters.
|
||||
*
|
||||
* > A noncharacter is a code point that is in the range U+FDD0 to U+FDEF,
|
||||
* > inclusive, or U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF,
|
||||
* > U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE,
|
||||
* > U+6FFFF, U+7FFFE, U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF,
|
||||
* > U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF, U+DFFFE,
|
||||
* > U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE, or U+10FFFF.
|
||||
*
|
||||
* A C0 control is a code point that is in the range of U+00 to U+1F,
|
||||
* but ASCII whitespace includes U+09, U+0A, U+0C, and U+0D.
|
||||
*
|
||||
* These characters are invalid but still decode as any valid character.
|
||||
* This comment is here to note and explain why there's no check to
|
||||
* remove these characters or replace them.
|
||||
*
|
||||
* @see https://infra.spec.whatwg.org/#noncharacter
|
||||
*/
|
||||
|
||||
/*
|
||||
* Code points in the C1 controls area need to be remapped as if they
|
||||
* were stored in Windows-1252. Note! This transformation only happens
|
||||
* for numeric character references. The raw code points in the byte
|
||||
* stream are not translated.
|
||||
*
|
||||
* > If the number is one of the numbers in the first column of
|
||||
* > the following table, then find the row with that number in
|
||||
* > the first column, and set the character reference code to
|
||||
* > the number in the second column of that row.
|
||||
*/
|
||||
if ( $code_point >= 0x80 && $code_point <= 0x9F ) {
|
||||
$windows_1252_mapping = array(
|
||||
0x20AC, // 0x80 -> EURO SIGN (€).
|
||||
0x81, // 0x81 -> (no change).
|
||||
0x201A, // 0x82 -> SINGLE LOW-9 QUOTATION MARK (‚).
|
||||
0x0192, // 0x83 -> LATIN SMALL LETTER F WITH HOOK (ƒ).
|
||||
0x201E, // 0x84 -> DOUBLE LOW-9 QUOTATION MARK („).
|
||||
0x2026, // 0x85 -> HORIZONTAL ELLIPSIS (…).
|
||||
0x2020, // 0x86 -> DAGGER (†).
|
||||
0x2021, // 0x87 -> DOUBLE DAGGER (‡).
|
||||
0x02C6, // 0x88 -> MODIFIER LETTER CIRCUMFLEX ACCENT (ˆ).
|
||||
0x2030, // 0x89 -> PER MILLE SIGN (‰).
|
||||
0x0160, // 0x8A -> LATIN CAPITAL LETTER S WITH CARON (Š).
|
||||
0x2039, // 0x8B -> SINGLE LEFT-POINTING ANGLE QUOTATION MARK (‹).
|
||||
0x0152, // 0x8C -> LATIN CAPITAL LIGATURE OE (Œ).
|
||||
0x8D, // 0x8D -> (no change).
|
||||
0x017D, // 0x8E -> LATIN CAPITAL LETTER Z WITH CARON (Ž).
|
||||
0x8F, // 0x8F -> (no change).
|
||||
0x90, // 0x90 -> (no change).
|
||||
0x2018, // 0x91 -> LEFT SINGLE QUOTATION MARK (‘).
|
||||
0x2019, // 0x92 -> RIGHT SINGLE QUOTATION MARK (’).
|
||||
0x201C, // 0x93 -> LEFT DOUBLE QUOTATION MARK (“).
|
||||
0x201D, // 0x94 -> RIGHT DOUBLE QUOTATION MARK (”).
|
||||
0x2022, // 0x95 -> BULLET (•).
|
||||
0x2013, // 0x96 -> EN DASH (–).
|
||||
0x2014, // 0x97 -> EM DASH (—).
|
||||
0x02DC, // 0x98 -> SMALL TILDE (˜).
|
||||
0x2122, // 0x99 -> TRADE MARK SIGN (™).
|
||||
0x0161, // 0x9A -> LATIN SMALL LETTER S WITH CARON (š).
|
||||
0x203A, // 0x9B -> SINGLE RIGHT-POINTING ANGLE QUOTATION MARK (›).
|
||||
0x0153, // 0x9C -> LATIN SMALL LIGATURE OE (œ).
|
||||
0x9D, // 0x9D -> (no change).
|
||||
0x017E, // 0x9E -> LATIN SMALL LETTER Z WITH CARON (ž).
|
||||
0x0178, // 0x9F -> LATIN CAPITAL LETTER Y WITH DIAERESIS (Ÿ).
|
||||
);
|
||||
|
||||
$code_point = $windows_1252_mapping[ $code_point - 0x80 ];
|
||||
}
|
||||
|
||||
$match_byte_length = $end_of_span - $at;
|
||||
return self::code_point_to_utf8_bytes( $code_point );
|
||||
}
|
||||
|
||||
/** Tracks inner parsing within the named character reference. */
|
||||
$name_at = $at + 1;
|
||||
// Minimum named character reference is two characters. E.g. `GT`.
|
||||
if ( $name_at + 2 > $length ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$name_length = 0;
|
||||
$replacement = $html5_named_character_references->read_token( $text, $name_at, $name_length );
|
||||
if ( false === $replacement ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$after_name = $name_at + $name_length;
|
||||
|
||||
// If the match ended with a semicolon then it should always be decoded.
|
||||
if ( ';' === $text[ $name_at + $name_length - 1 ] ) {
|
||||
$match_byte_length = $after_name - $at;
|
||||
return $replacement;
|
||||
}
|
||||
|
||||
/*
|
||||
* At this point though there's a match for an entry in the named
|
||||
* character reference table but the match doesn't end in `;`.
|
||||
* It may be allowed if it's followed by something unambiguous.
|
||||
*/
|
||||
$ambiguous_follower = (
|
||||
$after_name < $length &&
|
||||
$name_at < $length &&
|
||||
(
|
||||
ctype_alnum( $text[ $after_name ] ) ||
|
||||
'=' === $text[ $after_name ]
|
||||
)
|
||||
);
|
||||
|
||||
// It's non-ambiguous, safe to leave it in.
|
||||
if ( ! $ambiguous_follower ) {
|
||||
$match_byte_length = $after_name - $at;
|
||||
return $replacement;
|
||||
}
|
||||
|
||||
// It's ambiguous, which isn't allowed inside attributes.
|
||||
if ( 'attribute' === $context ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$match_byte_length = $after_name - $at;
|
||||
return $replacement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a code point number into the UTF-8 encoding.
|
||||
*
|
||||
* This encoder implements the UTF-8 encoding algorithm for converting
|
||||
* a code point into a byte sequence. If it receives an invalid code
|
||||
* point it will return the Unicode Replacement Character U+FFFD `<60>`.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* '🅰' === WP_HTML_Decoder::code_point_to_utf8_bytes( 0x1f170 );
|
||||
*
|
||||
* // Half of a surrogate pair is an invalid code point.
|
||||
* '<27>' === WP_HTML_Decoder::code_point_to_utf8_bytes( 0xd83c );
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc3629 For the UTF-8 standard.
|
||||
*
|
||||
* @param int $code_point Which code point to convert.
|
||||
* @return string Converted code point, or `<60>` if invalid.
|
||||
*/
|
||||
public static function code_point_to_utf8_bytes( $code_point ) {
|
||||
// Pre-check to ensure a valid code point.
|
||||
if (
|
||||
$code_point <= 0 ||
|
||||
( $code_point >= 0xD800 && $code_point <= 0xDFFF ) ||
|
||||
$code_point > 0x10FFFF
|
||||
) {
|
||||
return '<27>';
|
||||
}
|
||||
|
||||
if ( $code_point <= 0x7F ) {
|
||||
return chr( $code_point );
|
||||
}
|
||||
|
||||
if ( $code_point <= 0x7FF ) {
|
||||
$byte1 = ( $code_point >> 6 ) | 0xC0;
|
||||
$byte2 = $code_point & 0x3F | 0x80;
|
||||
|
||||
return pack( 'CC', $byte1, $byte2 );
|
||||
}
|
||||
|
||||
if ( $code_point <= 0xFFFF ) {
|
||||
$byte1 = ( $code_point >> 12 ) | 0xE0;
|
||||
$byte2 = ( $code_point >> 6 ) & 0x3F | 0x80;
|
||||
$byte3 = $code_point & 0x3F | 0x80;
|
||||
|
||||
return pack( 'CCC', $byte1, $byte2, $byte3 );
|
||||
}
|
||||
|
||||
// Any values above U+10FFFF are eliminated above in the pre-check.
|
||||
$byte1 = ( $code_point >> 18 ) | 0xF0;
|
||||
$byte2 = ( $code_point >> 12 ) & 0x3F | 0x80;
|
||||
$byte3 = ( $code_point >> 6 ) & 0x3F | 0x80;
|
||||
$byte4 = $code_point & 0x3F | 0x80;
|
||||
|
||||
return pack( 'CCCC', $byte1, $byte2, $byte3, $byte4 );
|
||||
}
|
||||
}
|
||||
541
wp-includes/html-api/class-wp-html-open-elements.php
Normal file
541
wp-includes/html-api/class-wp-html-open-elements.php
Normal file
@@ -0,0 +1,541 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Open_Elements class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML processor during HTML parsing
|
||||
* for managing the stack of open elements.
|
||||
*
|
||||
* This class is designed for internal use by the HTML processor.
|
||||
*
|
||||
* > Initially, the stack of open elements is empty. The stack grows
|
||||
* > downwards; the topmost node on the stack is the first one added
|
||||
* > to the stack, and the bottommost node of the stack is the most
|
||||
* > recently added node in the stack (notwithstanding when the stack
|
||||
* > is manipulated in a random access fashion as part of the handling
|
||||
* > for misnested tags).
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#stack-of-open-elements
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Open_Elements {
|
||||
/**
|
||||
* Holds the stack of open element references.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @var WP_HTML_Token[]
|
||||
*/
|
||||
public $stack = array();
|
||||
|
||||
/**
|
||||
* Whether a P element is in button scope currently.
|
||||
*
|
||||
* This class optimizes scope lookup by pre-calculating
|
||||
* this value when elements are added and removed to the
|
||||
* stack of open elements which might change its value.
|
||||
* This avoids frequent iteration over the stack.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $has_p_in_button_scope = false;
|
||||
|
||||
/**
|
||||
* A function that will be called when an item is popped off the stack of open elements.
|
||||
*
|
||||
* The function will be called with the popped item as its argument.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
private $pop_handler = null;
|
||||
|
||||
/**
|
||||
* A function that will be called when an item is pushed onto the stack of open elements.
|
||||
*
|
||||
* The function will be called with the pushed item as its argument.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
private $push_handler = null;
|
||||
|
||||
/**
|
||||
* Sets a pop handler that will be called when an item is popped off the stack of
|
||||
* open elements.
|
||||
*
|
||||
* The function will be called with the pushed item as its argument.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param Closure $handler The handler function.
|
||||
*/
|
||||
public function set_pop_handler( Closure $handler ) {
|
||||
$this->pop_handler = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a push handler that will be called when an item is pushed onto the stack of
|
||||
* open elements.
|
||||
*
|
||||
* The function will be called with the pushed item as its argument.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param Closure $handler The handler function.
|
||||
*/
|
||||
public function set_push_handler( Closure $handler ) {
|
||||
$this->push_handler = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports if a specific node is in the stack of open elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $token Look for this node in the stack.
|
||||
* @return bool Whether the referenced node is in the stack of open elements.
|
||||
*/
|
||||
public function contains_node( $token ) {
|
||||
foreach ( $this->walk_up() as $item ) {
|
||||
if ( $token->bookmark_name === $item->bookmark_name ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns how many nodes are currently in the stack of open elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @return int How many node are in the stack of open elements.
|
||||
*/
|
||||
public function count() {
|
||||
return count( $this->stack );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the node at the end of the stack of open elements,
|
||||
* if one exists. If the stack is empty, returns null.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @return WP_HTML_Token|null Last node in the stack of open elements, if one exists, otherwise null.
|
||||
*/
|
||||
public function current_node() {
|
||||
$current_node = end( $this->stack );
|
||||
|
||||
return $current_node ? $current_node : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether an element is in a specific scope.
|
||||
*
|
||||
* ## HTML Support
|
||||
*
|
||||
* This function skips checking for the termination list because there
|
||||
* are no supported elements which appear in the termination list.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-the-specific-scope
|
||||
*
|
||||
* @param string $tag_name Name of tag check.
|
||||
* @param string[] $termination_list List of elements that terminate the search.
|
||||
* @return bool Whether the element was found in a specific scope.
|
||||
*/
|
||||
public function has_element_in_specific_scope( $tag_name, $termination_list ) {
|
||||
foreach ( $this->walk_up() as $node ) {
|
||||
if ( $node->node_name === $tag_name ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
'(internal: H1 through H6 - do not use)' === $tag_name &&
|
||||
in_array( $node->node_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true )
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch ( $node->node_name ) {
|
||||
case 'HTML':
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( in_array( $node->node_name, $termination_list, true ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a particular element is in scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-scope
|
||||
*
|
||||
* @param string $tag_name Name of tag to check.
|
||||
* @return bool Whether given element is in scope.
|
||||
*/
|
||||
public function has_element_in_scope( $tag_name ) {
|
||||
return $this->has_element_in_specific_scope(
|
||||
$tag_name,
|
||||
array(
|
||||
|
||||
/*
|
||||
* Because it's not currently possible to encounter
|
||||
* one of the termination elements, they don't need
|
||||
* to be listed here. If they were, they would be
|
||||
* unreachable and only waste CPU cycles while
|
||||
* scanning through HTML.
|
||||
*/
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a particular element is in list item scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @since 6.5.0 Implemented: no longer throws on every invocation.
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-list-item-scope
|
||||
*
|
||||
* @param string $tag_name Name of tag to check.
|
||||
* @return bool Whether given element is in scope.
|
||||
*/
|
||||
public function has_element_in_list_item_scope( $tag_name ) {
|
||||
return $this->has_element_in_specific_scope(
|
||||
$tag_name,
|
||||
array(
|
||||
// There are more elements that belong here which aren't currently supported.
|
||||
'OL',
|
||||
'UL',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a particular element is in button scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-button-scope
|
||||
*
|
||||
* @param string $tag_name Name of tag to check.
|
||||
* @return bool Whether given element is in scope.
|
||||
*/
|
||||
public function has_element_in_button_scope( $tag_name ) {
|
||||
return $this->has_element_in_specific_scope( $tag_name, array( 'BUTTON' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a particular element is in table scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-table-scope
|
||||
*
|
||||
* @throws WP_HTML_Unsupported_Exception Always until this function is implemented.
|
||||
*
|
||||
* @param string $tag_name Name of tag to check.
|
||||
* @return bool Whether given element is in scope.
|
||||
*/
|
||||
public function has_element_in_table_scope( $tag_name ) {
|
||||
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on table scope.' );
|
||||
|
||||
return false; // The linter requires this unreachable code until the function is implemented and can return.
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a particular element is in select scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-select-scope
|
||||
*
|
||||
* @throws WP_HTML_Unsupported_Exception Always until this function is implemented.
|
||||
*
|
||||
* @param string $tag_name Name of tag to check.
|
||||
* @return bool Whether given element is in scope.
|
||||
*/
|
||||
public function has_element_in_select_scope( $tag_name ) {
|
||||
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on select scope.' );
|
||||
|
||||
return false; // The linter requires this unreachable code until the function is implemented and can return.
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a P is in BUTTON scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-button-scope
|
||||
*
|
||||
* @return bool Whether a P is in BUTTON scope.
|
||||
*/
|
||||
public function has_p_in_button_scope() {
|
||||
return $this->has_p_in_button_scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a node off of the stack of open elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#stack-of-open-elements
|
||||
*
|
||||
* @return bool Whether a node was popped off of the stack.
|
||||
*/
|
||||
public function pop() {
|
||||
$item = array_pop( $this->stack );
|
||||
if ( null === $item ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'context-node' === $item->bookmark_name ) {
|
||||
$this->stack[] = $item;
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->after_element_pop( $item );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops nodes off of the stack of open elements until one with the given tag name has been popped.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see WP_HTML_Open_Elements::pop
|
||||
*
|
||||
* @param string $tag_name Name of tag that needs to be popped off of the stack of open elements.
|
||||
* @return bool Whether a tag of the given name was found and popped off of the stack of open elements.
|
||||
*/
|
||||
public function pop_until( $tag_name ) {
|
||||
foreach ( $this->walk_up() as $item ) {
|
||||
if ( 'context-node' === $item->bookmark_name ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->pop();
|
||||
|
||||
if (
|
||||
'(internal: H1 through H6 - do not use)' === $tag_name &&
|
||||
in_array( $item->node_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true )
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( $tag_name === $item->node_name ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes a node onto the stack of open elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#stack-of-open-elements
|
||||
*
|
||||
* @param WP_HTML_Token $stack_item Item to add onto stack.
|
||||
*/
|
||||
public function push( $stack_item ) {
|
||||
$this->stack[] = $stack_item;
|
||||
$this->after_element_push( $stack_item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a specific node from the stack of open elements.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $token The node to remove from the stack of open elements.
|
||||
* @return bool Whether the node was found and removed from the stack of open elements.
|
||||
*/
|
||||
public function remove_node( $token ) {
|
||||
if ( 'context-node' === $token->bookmark_name ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $this->walk_up() as $position_from_end => $item ) {
|
||||
if ( $token->bookmark_name !== $item->bookmark_name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$position_from_start = $this->count() - $position_from_end - 1;
|
||||
array_splice( $this->stack, $position_from_start, 1 );
|
||||
$this->after_element_pop( $item );
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Steps through the stack of open elements, starting with the top element
|
||||
* (added first) and walking downwards to the one added last.
|
||||
*
|
||||
* This generator function is designed to be used inside a "foreach" loop.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $html = '<em><strong><a>We are here';
|
||||
* foreach ( $stack->walk_down() as $node ) {
|
||||
* echo "{$node->node_name} -> ";
|
||||
* }
|
||||
* > EM -> STRONG -> A ->
|
||||
*
|
||||
* To start with the most-recently added element and walk towards the top,
|
||||
* see WP_HTML_Open_Elements::walk_up().
|
||||
*
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public function walk_down() {
|
||||
$count = count( $this->stack );
|
||||
|
||||
for ( $i = 0; $i < $count; $i++ ) {
|
||||
yield $this->stack[ $i ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Steps through the stack of open elements, starting with the bottom element
|
||||
* (added last) and walking upwards to the one added first.
|
||||
*
|
||||
* This generator function is designed to be used inside a "foreach" loop.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $html = '<em><strong><a>We are here';
|
||||
* foreach ( $stack->walk_up() as $node ) {
|
||||
* echo "{$node->node_name} -> ";
|
||||
* }
|
||||
* > A -> STRONG -> EM ->
|
||||
*
|
||||
* To start with the first added element and walk towards the bottom,
|
||||
* see WP_HTML_Open_Elements::walk_down().
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @since 6.5.0 Accepts $above_this_node to start traversal above a given node, if it exists.
|
||||
*
|
||||
* @param ?WP_HTML_Token $above_this_node Start traversing above this node, if provided and if the node exists.
|
||||
*/
|
||||
public function walk_up( $above_this_node = null ) {
|
||||
$has_found_node = null === $above_this_node;
|
||||
|
||||
for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
|
||||
$node = $this->stack[ $i ];
|
||||
|
||||
if ( ! $has_found_node ) {
|
||||
$has_found_node = $node === $above_this_node;
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $node;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal helpers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Updates internal flags after adding an element.
|
||||
*
|
||||
* Certain conditions (such as "has_p_in_button_scope") are maintained here as
|
||||
* flags that are only modified when adding and removing elements. This allows
|
||||
* the HTML Processor to quickly check for these conditions instead of iterating
|
||||
* over the open stack elements upon each new tag it encounters. These flags,
|
||||
* however, need to be maintained as items are added and removed from the stack.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $item Element that was added to the stack of open elements.
|
||||
*/
|
||||
public function after_element_push( $item ) {
|
||||
/*
|
||||
* When adding support for new elements, expand this switch to trap
|
||||
* cases where the precalculated value needs to change.
|
||||
*/
|
||||
switch ( $item->node_name ) {
|
||||
case 'BUTTON':
|
||||
$this->has_p_in_button_scope = false;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
$this->has_p_in_button_scope = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if ( null !== $this->push_handler ) {
|
||||
( $this->push_handler )( $item );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates internal flags after removing an element.
|
||||
*
|
||||
* Certain conditions (such as "has_p_in_button_scope") are maintained here as
|
||||
* flags that are only modified when adding and removing elements. This allows
|
||||
* the HTML Processor to quickly check for these conditions instead of iterating
|
||||
* over the open stack elements upon each new tag it encounters. These flags,
|
||||
* however, need to be maintained as items are added and removed from the stack.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param WP_HTML_Token $item Element that was removed from the stack of open elements.
|
||||
*/
|
||||
public function after_element_pop( $item ) {
|
||||
/*
|
||||
* When adding support for new elements, expand this switch to trap
|
||||
* cases where the precalculated value needs to change.
|
||||
*/
|
||||
switch ( $item->node_name ) {
|
||||
case 'BUTTON':
|
||||
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( null !== $this->pop_handler ) {
|
||||
( $this->pop_handler )( $item );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wakeup magic method.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*/
|
||||
public function __wakeup() {
|
||||
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
|
||||
}
|
||||
}
|
||||
143
wp-includes/html-api/class-wp-html-processor-state.php
Normal file
143
wp-includes/html-api/class-wp-html-processor-state.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Processor_State class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML processor during HTML parsing
|
||||
* for managing the internal parsing state.
|
||||
*
|
||||
* This class is designed for internal use by the HTML processor.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Processor_State {
|
||||
/*
|
||||
* Insertion mode constants.
|
||||
*
|
||||
* These constants exist and are named to make it easier to
|
||||
* discover and recognize the supported insertion modes in
|
||||
* the parser.
|
||||
*
|
||||
* Out of all the possible insertion modes, only those
|
||||
* supported by the parser are listed here. As support
|
||||
* is added to the parser for more modes, add them here
|
||||
* following the same naming and value pattern.
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#the-insertion-mode
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initial insertion mode for full HTML parser.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#the-initial-insertion-mode
|
||||
* @see WP_HTML_Processor_State::$insertion_mode
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const INSERTION_MODE_INITIAL = 'insertion-mode-initial';
|
||||
|
||||
/**
|
||||
* In body insertion mode for full HTML parser.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#parsing-main-inbody
|
||||
* @see WP_HTML_Processor_State::$insertion_mode
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const INSERTION_MODE_IN_BODY = 'insertion-mode-in-body';
|
||||
|
||||
/**
|
||||
* Tracks open elements while scanning HTML.
|
||||
*
|
||||
* This property is initialized in the constructor and never null.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#stack-of-open-elements
|
||||
*
|
||||
* @var WP_HTML_Open_Elements
|
||||
*/
|
||||
public $stack_of_open_elements = null;
|
||||
|
||||
/**
|
||||
* Tracks open formatting elements, used to handle mis-nested formatting element tags.
|
||||
*
|
||||
* This property is initialized in the constructor and never null.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#list-of-active-formatting-elements
|
||||
*
|
||||
* @var WP_HTML_Active_Formatting_Elements
|
||||
*/
|
||||
public $active_formatting_elements = null;
|
||||
|
||||
/**
|
||||
* Refers to the currently-matched tag, if any.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @var WP_HTML_Token|null
|
||||
*/
|
||||
public $current_token = null;
|
||||
|
||||
/**
|
||||
* Tree construction insertion mode.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#insertion-mode
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $insertion_mode = self::INSERTION_MODE_INITIAL;
|
||||
|
||||
/**
|
||||
* Context node initializing fragment parser, if created as a fragment parser.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#concept-frag-parse-context
|
||||
*
|
||||
* @var [string, array]|null
|
||||
*/
|
||||
public $context_node = null;
|
||||
|
||||
/**
|
||||
* The frameset-ok flag indicates if a `FRAMESET` element is allowed in the current state.
|
||||
*
|
||||
* > The frameset-ok flag is set to "ok" when the parser is created. It is set to "not ok" after certain tokens are seen.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#frameset-ok-flag
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $frameset_ok = true;
|
||||
|
||||
/**
|
||||
* Constructor - creates a new and empty state value.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->stack_of_open_elements = new WP_HTML_Open_Elements();
|
||||
$this->active_formatting_elements = new WP_HTML_Active_Formatting_Elements();
|
||||
}
|
||||
}
|
||||
2472
wp-includes/html-api/class-wp-html-processor.php
Normal file
2472
wp-includes/html-api/class-wp-html-processor.php
Normal file
File diff suppressed because it is too large
Load Diff
56
wp-includes/html-api/class-wp-html-span.php
Normal file
56
wp-includes/html-api/class-wp-html-span.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Span class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML tag processor to represent a textual span
|
||||
* inside an HTML document.
|
||||
*
|
||||
* This is a two-tuple in disguise, used to avoid the memory overhead
|
||||
* involved in using an array for the same purpose.
|
||||
*
|
||||
* This class is for internal usage of the WP_HTML_Tag_Processor class.
|
||||
*
|
||||
* @access private
|
||||
* @since 6.2.0
|
||||
* @since 6.5.0 Replaced `end` with `length` to more closely align with `substr()`.
|
||||
*
|
||||
* @see WP_HTML_Tag_Processor
|
||||
*/
|
||||
class WP_HTML_Span {
|
||||
/**
|
||||
* Byte offset into document where span begins.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $start;
|
||||
|
||||
/**
|
||||
* Byte length of this span.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $length;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @param int $start Byte offset into document where replacement span begins.
|
||||
* @param int $length Byte length of span.
|
||||
*/
|
||||
public function __construct( $start, $length ) {
|
||||
$this->start = $start;
|
||||
$this->length = $length;
|
||||
}
|
||||
}
|
||||
82
wp-includes/html-api/class-wp-html-stack-event.php
Normal file
82
wp-includes/html-api/class-wp-html-stack-event.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Stack_Event class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML Processor as a record for stack operations.
|
||||
*
|
||||
* This class is for internal usage of the WP_HTML_Processor class.
|
||||
*
|
||||
* @access private
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Stack_Event {
|
||||
/**
|
||||
* Refers to popping an element off of the stack of open elements.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*/
|
||||
const POP = 'pop';
|
||||
|
||||
/**
|
||||
* Refers to pushing an element onto the stack of open elements.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*/
|
||||
const PUSH = 'push';
|
||||
|
||||
/**
|
||||
* References the token associated with the stack push event,
|
||||
* even if this is a pop event for that element.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @var WP_HTML_Token
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* Indicates which kind of stack operation this event represents.
|
||||
*
|
||||
* May be one of the class constants.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @see self::POP
|
||||
* @see self::PUSH
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* Indicates if the stack element is a real or virtual node.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $provenance;
|
||||
|
||||
/**
|
||||
* Constructor function.
|
||||
*
|
||||
* @since 6.6.0
|
||||
*
|
||||
* @param WP_HTML_Token $token Token associated with stack event, always an opening token.
|
||||
* @param string $operation One of self::PUSH or self::POP.
|
||||
* @param string $provenance "virtual" or "real".
|
||||
*/
|
||||
public function __construct( $token, $operation, $provenance ) {
|
||||
$this->token = $token;
|
||||
$this->operation = $operation;
|
||||
$this->provenance = $provenance;
|
||||
}
|
||||
}
|
||||
3559
wp-includes/html-api/class-wp-html-tag-processor.php
Normal file
3559
wp-includes/html-api/class-wp-html-tag-processor.php
Normal file
File diff suppressed because it is too large
Load Diff
64
wp-includes/html-api/class-wp-html-text-replacement.php
Normal file
64
wp-includes/html-api/class-wp-html-text-replacement.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Text_Replacement class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML tag processor as a data structure for replacing
|
||||
* existing content from start to end, allowing to drastically improve performance.
|
||||
*
|
||||
* This class is for internal usage of the WP_HTML_Tag_Processor class.
|
||||
*
|
||||
* @access private
|
||||
* @since 6.2.0
|
||||
* @since 6.5.0 Replace `end` with `length` to more closely match `substr()`.
|
||||
*
|
||||
* @see WP_HTML_Tag_Processor
|
||||
*/
|
||||
class WP_HTML_Text_Replacement {
|
||||
/**
|
||||
* Byte offset into document where replacement span begins.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $start;
|
||||
|
||||
/**
|
||||
* Byte length of span being replaced.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $length;
|
||||
|
||||
/**
|
||||
* Span of text to insert in document to replace existing content from start to end.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $text;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @param int $start Byte offset into document where replacement span begins.
|
||||
* @param int $length Byte length of span in document being replaced.
|
||||
* @param string $text Span of text to insert in document to replace existing content from start to end.
|
||||
*/
|
||||
public function __construct( $start, $length, $text ) {
|
||||
$this->start = $start;
|
||||
$this->length = $length;
|
||||
$this->text = $text;
|
||||
}
|
||||
}
|
||||
106
wp-includes/html-api/class-wp-html-token.php
Normal file
106
wp-includes/html-api/class-wp-html-token.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Token class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML processor during HTML parsing
|
||||
* for referring to tokens in the input HTML string.
|
||||
*
|
||||
* This class is designed for internal use by the HTML processor.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Token {
|
||||
/**
|
||||
* Name of bookmark corresponding to source of token in input HTML string.
|
||||
*
|
||||
* Having a bookmark name does not imply that the token still exists. It
|
||||
* may be that the source token and underlying bookmark was wiped out by
|
||||
* some modification to the source HTML.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $bookmark_name = null;
|
||||
|
||||
/**
|
||||
* Name of node; lowercase names such as "marker" are not HTML elements.
|
||||
*
|
||||
* For HTML elements/tags this value should come from WP_HTML_Processor::get_tag().
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see WP_HTML_Processor::get_tag()
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $node_name = null;
|
||||
|
||||
/**
|
||||
* Whether node contains the self-closing flag.
|
||||
*
|
||||
* A node may have a self-closing flag when it shouldn't. This value
|
||||
* only reports if the flag is present in the original HTML.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#self-closing-flag
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $has_self_closing_flag = false;
|
||||
|
||||
/**
|
||||
* Called when token is garbage-collected or otherwise destroyed.
|
||||
*
|
||||
* @var callable|null
|
||||
*/
|
||||
public $on_destroy = null;
|
||||
|
||||
/**
|
||||
* Constructor - creates a reference to a token in some external HTML string.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param string $bookmark_name Name of bookmark corresponding to location in HTML where token is found.
|
||||
* @param string $node_name Name of node token represents; if uppercase, an HTML element; if lowercase, a special value like "marker".
|
||||
* @param bool $has_self_closing_flag Whether the source token contains the self-closing flag, regardless of whether it's valid.
|
||||
* @param callable $on_destroy Function to call when destroying token, useful for releasing the bookmark.
|
||||
*/
|
||||
public function __construct( $bookmark_name, $node_name, $has_self_closing_flag, $on_destroy = null ) {
|
||||
$this->bookmark_name = $bookmark_name;
|
||||
$this->node_name = $node_name;
|
||||
$this->has_self_closing_flag = $has_self_closing_flag;
|
||||
$this->on_destroy = $on_destroy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public function __destruct() {
|
||||
if ( is_callable( $this->on_destroy ) ) {
|
||||
call_user_func( $this->on_destroy, $this->bookmark_name );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wakeup magic method.
|
||||
*
|
||||
* @since 6.4.2
|
||||
*/
|
||||
public function __wakeup() {
|
||||
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
|
||||
}
|
||||
}
|
||||
31
wp-includes/html-api/class-wp-html-unsupported-exception.php
Normal file
31
wp-includes/html-api/class-wp-html-unsupported-exception.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* HTML API: WP_HTML_Unsupported_Exception class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage HTML-API
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used by the HTML processor during HTML parsing
|
||||
* for indicating that a given operation is unsupported.
|
||||
*
|
||||
* This class is designed for internal use by the HTML processor.
|
||||
*
|
||||
* The HTML API aims to operate in compliance with the HTML5
|
||||
* specification, but does not implement the full specification.
|
||||
* In cases where it lacks support it should not cause breakage
|
||||
* or unexpected behavior. In the cases where it recognizes that
|
||||
* it cannot proceed, this class is used to abort from any
|
||||
* operation and signify that the given HTML cannot be processed.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @see WP_HTML_Processor
|
||||
*/
|
||||
class WP_HTML_Unsupported_Exception extends Exception {
|
||||
|
||||
}
|
||||
1313
wp-includes/html-api/html5-named-character-references.php
Normal file
1313
wp-includes/html-api/html5-named-character-references.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user