summaryrefslogtreecommitdiffstats
path: root/wp-includes/html-api/class-wp-html-processor.php
diff options
context:
space:
mode:
Diffstat (limited to 'wp-includes/html-api/class-wp-html-processor.php')
-rw-r--r--wp-includes/html-api/class-wp-html-processor.php617
1 files changed, 580 insertions, 37 deletions
diff --git a/wp-includes/html-api/class-wp-html-processor.php b/wp-includes/html-api/class-wp-html-processor.php
index c76cc19..29f1c7a 100644
--- a/wp-includes/html-api/class-wp-html-processor.php
+++ b/wp-includes/html-api/class-wp-html-processor.php
@@ -201,6 +201,52 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*/
private $release_internal_bookmark_on_destruct = null;
+ /**
+ * Stores stack events which arise during parsing of the
+ * HTML document, which will then supply the "match" events.
+ *
+ * @since 6.6.0
+ *
+ * @var WP_HTML_Stack_Event[]
+ */
+ private $element_queue = array();
+
+ /**
+ * Current stack event, if set, representing a matched token.
+ *
+ * Because the parser may internally point to a place further along in a document
+ * than the nodes which have already been processed (some "virtual" nodes may have
+ * appeared while scanning the HTML document), this will point at the "current" node
+ * being processed. It comes from the front of the element queue.
+ *
+ * @since 6.6.0
+ *
+ * @var ?WP_HTML_Stack_Event
+ */
+ private $current_element = null;
+
+ /**
+ * Context node if created as a fragment parser.
+ *
+ * @var ?WP_HTML_Token
+ */
+ private $context_node = null;
+
+ /**
+ * Whether the parser has yet processed the context node,
+ * if created as a fragment parser.
+ *
+ * The context node will be initially pushed onto the stack of open elements,
+ * but when created as a fragment parser, this context element (and the implicit
+ * HTML document node above it) should not be exposed as a matched token or node.
+ *
+ * This boolean indicates whether the processor should skip over the current
+ * node in its initial search for the first node created from the input HTML.
+ *
+ * @var bool
+ */
+ private $has_seen_context_node = false;
+
/*
* Public Interface Functions
*/
@@ -230,18 +276,19 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* - The only supported document encoding is `UTF-8`, which is the default value.
*
* @since 6.4.0
+ * @since 6.6.0 Returns `static` instead of `self` so it can create subclass instances.
*
* @param string $html Input HTML fragment to process.
* @param string $context Context element for the fragment, must be default of `<body>`.
* @param string $encoding Text encoding of the document; must be default of 'UTF-8'.
- * @return WP_HTML_Processor|null The created processor if successful, otherwise null.
+ * @return static|null The created processor if successful, otherwise null.
*/
public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
if ( '<body>' !== $context || 'UTF-8' !== $encoding ) {
return null;
}
- $processor = new self( $html, self::CONSTRUCTOR_UNLOCK_CODE );
+ $processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE );
$processor->state->context_node = array( 'BODY', array() );
$processor->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
@@ -257,14 +304,15 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
)
);
- $processor->state->stack_of_open_elements->push(
- new WP_HTML_Token(
- 'context-node',
- $processor->state->context_node[0],
- false
- )
+ $context_node = new WP_HTML_Token(
+ 'context-node',
+ $processor->state->context_node[0],
+ false
);
+ $processor->state->stack_of_open_elements->push( $context_node );
+ $processor->context_node = $context_node;
+
return $processor;
}
@@ -299,6 +347,24 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
$this->state = new WP_HTML_Processor_State();
+ $this->state->stack_of_open_elements->set_push_handler(
+ function ( WP_HTML_Token $token ) {
+ $is_virtual = ! isset( $this->state->current_token ) || $this->is_tag_closer();
+ $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name;
+ $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real';
+ $this->element_queue[] = new WP_HTML_Stack_Event( $token, WP_HTML_Stack_Event::PUSH, $provenance );
+ }
+ );
+
+ $this->state->stack_of_open_elements->set_pop_handler(
+ function ( WP_HTML_Token $token ) {
+ $is_virtual = ! isset( $this->state->current_token ) || ! $this->is_tag_closer();
+ $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name;
+ $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real';
+ $this->element_queue[] = new WP_HTML_Stack_Event( $token, WP_HTML_Stack_Event::POP, $provenance );
+ }
+ );
+
/*
* Create this wrapper so that it's possible to pass
* a private method into WP_HTML_Token classes without
@@ -342,6 +408,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* @todo Support matching the class name and tag name.
*
* @since 6.4.0
+ * @since 6.6.0 Visits all tokens, including virtual ones.
*
* @throws Exception When unable to allocate a bookmark for the next token in the input HTML document.
*
@@ -349,6 +416,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* Optional. Which tag name to find, having which class, etc. Default is to find any tag.
*
* @type string|null $tag_name Which tag to find, or `null` for "any tag."
+ * @type string $tag_closers 'visit' to pause at tag closers, 'skip' or unset to only visit openers.
* @type int|null $match_offset Find the Nth tag matching all search criteria.
* 1 for "first" tag, 3 for "third," etc.
* Defaults to first tag.
@@ -359,13 +427,15 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* @return bool Whether a tag was matched.
*/
public function next_tag( $query = null ) {
+ $visit_closers = isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers'];
+
if ( null === $query ) {
- while ( $this->step() ) {
+ while ( $this->next_token() ) {
if ( '#tag' !== $this->get_token_type() ) {
continue;
}
- if ( ! $this->is_tag_closer() ) {
+ if ( ! $this->is_tag_closer() || $visit_closers ) {
return true;
}
}
@@ -386,13 +456,21 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return false;
}
+ $needs_class = ( isset( $query['class_name'] ) && is_string( $query['class_name'] ) )
+ ? $query['class_name']
+ : null;
+
if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) {
- while ( $this->step() ) {
+ while ( $this->next_token() ) {
if ( '#tag' !== $this->get_token_type() ) {
continue;
}
- if ( ! $this->is_tag_closer() ) {
+ if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) {
+ continue;
+ }
+
+ if ( ! $this->is_tag_closer() || $visit_closers ) {
return true;
}
}
@@ -400,20 +478,15 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return false;
}
- if ( isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers'] ) {
- _doing_it_wrong(
- __METHOD__,
- __( 'Cannot visit tag closers in HTML Processor.' ),
- '6.4.0'
- );
- return false;
- }
-
$breadcrumbs = $query['breadcrumbs'];
$match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1;
- while ( $match_offset > 0 && $this->step() ) {
- if ( '#tag' !== $this->get_token_type() ) {
+ while ( $match_offset > 0 && $this->next_token() ) {
+ if ( '#tag' !== $this->get_token_type() || $this->is_tag_closer() ) {
+ continue;
+ }
+
+ if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) {
continue;
}
@@ -440,7 +513,92 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* @return bool
*/
public function next_token() {
- return $this->step();
+ $this->current_element = null;
+
+ if ( isset( $this->last_error ) ) {
+ return false;
+ }
+
+ if ( 'done' !== $this->has_seen_context_node && 0 === count( $this->element_queue ) && ! $this->step() ) {
+ while ( 'context-node' !== $this->state->stack_of_open_elements->current_node()->bookmark_name && $this->state->stack_of_open_elements->pop() ) {
+ continue;
+ }
+ $this->has_seen_context_node = 'done';
+ return $this->next_token();
+ }
+
+ $this->current_element = array_shift( $this->element_queue );
+ while ( isset( $this->context_node ) && ! $this->has_seen_context_node ) {
+ if ( isset( $this->current_element ) ) {
+ if ( $this->context_node === $this->current_element->token && WP_HTML_Stack_Event::PUSH === $this->current_element->operation ) {
+ $this->has_seen_context_node = true;
+ return $this->next_token();
+ }
+ }
+ $this->current_element = array_shift( $this->element_queue );
+ }
+
+ if ( ! isset( $this->current_element ) ) {
+ if ( 'done' === $this->has_seen_context_node ) {
+ return false;
+ } else {
+ return $this->next_token();
+ }
+ }
+
+ if ( isset( $this->context_node ) && WP_HTML_Stack_Event::POP === $this->current_element->operation && $this->context_node === $this->current_element->token ) {
+ $this->element_queue = array();
+ $this->current_element = null;
+ return false;
+ }
+
+ // Avoid sending close events for elements which don't expect a closing.
+ if (
+ WP_HTML_Stack_Event::POP === $this->current_element->operation &&
+ ! static::expects_closer( $this->current_element->token )
+ ) {
+ return $this->next_token();
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Indicates if the current tag token is a tag closer.
+ *
+ * Example:
+ *
+ * $p = WP_HTML_Processor::create_fragment( '<div></div>' );
+ * $p->next_tag( array( 'tag_name' => 'div', 'tag_closers' => 'visit' ) );
+ * $p->is_tag_closer() === false;
+ *
+ * $p->next_tag( array( 'tag_name' => 'div', 'tag_closers' => 'visit' ) );
+ * $p->is_tag_closer() === true;
+ *
+ * @since 6.6.0 Subclassed for HTML Processor.
+ *
+ * @return bool Whether the current tag is a tag closer.
+ */
+ public function is_tag_closer() {
+ return $this->is_virtual()
+ ? ( WP_HTML_Stack_Event::POP === $this->current_element->operation && '#tag' === $this->get_token_type() )
+ : parent::is_tag_closer();
+ }
+
+ /**
+ * Indicates if the currently-matched token is virtual, created by a stack operation
+ * while processing HTML, rather than a token found in the HTML text itself.
+ *
+ * @since 6.6.0
+ *
+ * @return bool Whether the current token is virtual.
+ */
+ private function is_virtual() {
+ return (
+ isset( $this->current_element->provenance ) &&
+ 'virtual' === $this->current_element->provenance
+ );
}
/**
@@ -497,6 +655,45 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
}
/**
+ * Indicates if the currently-matched node expects a closing
+ * token, or if it will self-close on the next step.
+ *
+ * Most HTML elements expect a closer, such as a P element or
+ * a DIV element. Others, like an IMG element are void and don't
+ * have a closing tag. Special elements, such as SCRIPT and STYLE,
+ * are treated just like void tags. Text nodes and self-closing
+ * foreign content will also act just like a void tag, immediately
+ * closing as soon as the processor advances to the next token.
+ *
+ * @since 6.6.0
+ *
+ * @todo When adding support for foreign content, ensure that
+ * this returns false for self-closing elements in the
+ * SVG and MathML namespace.
+ *
+ * @param ?WP_HTML_Token $node Node to examine instead of current node, if provided.
+ * @return bool Whether to expect a closer for the currently-matched node,
+ * or `null` if not matched on any token.
+ */
+ public function expects_closer( $node = null ) {
+ $token_name = $node->node_name ?? $this->get_token_name();
+ if ( ! isset( $token_name ) ) {
+ return null;
+ }
+
+ return ! (
+ // Comments, text nodes, and other atomic tokens.
+ '#' === $token_name[0] ||
+ // Doctype declarations.
+ 'html' === $token_name ||
+ // Void elements.
+ self::is_void( $token_name ) ||
+ // Special atomic elements.
+ in_array( $token_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true )
+ );
+ }
+
+ /**
* Steps through the HTML document and stop at the next tag, if any.
*
* @since 6.4.0
@@ -531,16 +728,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* is provided in the opening tag, otherwise it expects a tag closer.
*/
$top_node = $this->state->stack_of_open_elements->current_node();
- if (
- $top_node && (
- // Void elements.
- self::is_void( $top_node->node_name ) ||
- // Comments, text nodes, and other atomic tokens.
- '#' === $top_node->node_name[0] ||
- // Doctype declarations.
- 'html' === $top_node->node_name
- )
- ) {
+ if ( isset( $top_node ) && ! static::expects_closer( $top_node ) ) {
$this->state->stack_of_open_elements->pop();
}
}
@@ -604,14 +792,79 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*/
public function get_breadcrumbs() {
$breadcrumbs = array();
+
foreach ( $this->state->stack_of_open_elements->walk_down() as $stack_item ) {
$breadcrumbs[] = $stack_item->node_name;
}
+ if ( ! $this->is_virtual() ) {
+ return $breadcrumbs;
+ }
+
+ foreach ( $this->element_queue as $queue_item ) {
+ if ( $this->current_element->token->bookmark_name === $queue_item->token->bookmark_name ) {
+ break;
+ }
+
+ if ( 'context-node' === $queue_item->token->bookmark_name ) {
+ break;
+ }
+
+ if ( 'real' === $queue_item->provenance ) {
+ break;
+ }
+
+ if ( WP_HTML_Stack_Event::PUSH === $queue_item->operation ) {
+ $breadcrumbs[] = $queue_item->token->node_name;
+ } else {
+ array_pop( $breadcrumbs );
+ }
+ }
+
+ if ( null !== parent::get_token_name() && ! parent::is_tag_closer() ) {
+ array_pop( $breadcrumbs );
+ }
+
+ // Add the virtual node we're at.
+ if ( WP_HTML_Stack_Event::PUSH === $this->current_element->operation ) {
+ $breadcrumbs[] = $this->current_element->token->node_name;
+ }
+
return $breadcrumbs;
}
/**
+ * Returns the nesting depth of the current location in the document.
+ *
+ * Example:
+ *
+ * $processor = WP_HTML_Processor::create_fragment( '<div><p></p></div>' );
+ * // The processor starts in the BODY context, meaning it has depth from the start: HTML > BODY.
+ * 2 === $processor->get_current_depth();
+ *
+ * // Opening the DIV element increases the depth.
+ * $processor->next_token();
+ * 3 === $processor->get_current_depth();
+ *
+ * // Opening the P element increases the depth.
+ * $processor->next_token();
+ * 4 === $processor->get_current_depth();
+ *
+ * // The P element is closed during `next_token()` so the depth is decreased to reflect that.
+ * $processor->next_token();
+ * 3 === $processor->get_current_depth();
+ *
+ * @since 6.6.0
+ *
+ * @return int Nesting-depth of current location in the document.
+ */
+ public function get_current_depth() {
+ return $this->is_virtual()
+ ? count( $this->get_breadcrumbs() )
+ : $this->state->stack_of_open_elements->count();
+ }
+
+ /**
* Parses next element in the 'in body' insertion mode.
*
* This internal function performs the 'in body' insertion mode
@@ -629,7 +882,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
private function step_in_body() {
$token_name = $this->get_token_name();
$token_type = $this->get_token_type();
- $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : '';
+ $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : '';
$op = "{$op_sigil}{$token_name}";
switch ( $op ) {
@@ -1152,7 +1405,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
throw new WP_HTML_Unsupported_Exception( "Cannot process {$token_name} element." );
}
- if ( ! $this->is_tag_closer() ) {
+ if ( ! parent::is_tag_closer() ) {
/*
* > Any other start tag
*/
@@ -1248,6 +1501,10 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return null;
}
+ if ( $this->is_virtual() ) {
+ return $this->current_element->token->node_name;
+ }
+
$tag_name = parent::get_tag();
switch ( $tag_name ) {
@@ -1264,6 +1521,286 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
}
/**
+ * Indicates if the currently matched tag contains the self-closing flag.
+ *
+ * No HTML elements ought to have the self-closing flag and for those, the self-closing
+ * flag will be ignored. For void elements this is benign because they "self close"
+ * automatically. For non-void HTML elements though problems will appear if someone
+ * intends to use a self-closing element in place of that element with an empty body.
+ * For HTML foreign elements and custom elements the self-closing flag determines if
+ * they self-close or not.
+ *
+ * This function does not determine if a tag is self-closing,
+ * but only if the self-closing flag is present in the syntax.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @return bool Whether the currently matched tag contains the self-closing flag.
+ */
+ public function has_self_closing_flag() {
+ return $this->is_virtual() ? false : parent::has_self_closing_flag();
+ }
+
+ /**
+ * Returns the node name represented by the token.
+ *
+ * This matches the DOM API value `nodeName`. Some values
+ * are static, such as `#text` for a text node, while others
+ * are dynamically generated from the token itself.
+ *
+ * Dynamic names:
+ * - Uppercase tag name for tag matches.
+ * - `html` for DOCTYPE declarations.
+ *
+ * Note that if the Tag Processor is not matched on a token
+ * then this function will return `null`, either because it
+ * hasn't yet found a token or because it reached the end
+ * of the document without matching a token.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @return string|null Name of the matched token.
+ */
+ public function get_token_name() {
+ return $this->is_virtual()
+ ? $this->current_element->token->node_name
+ : parent::get_token_name();
+ }
+
+ /**
+ * Indicates the kind of matched token, if any.
+ *
+ * This differs from `get_token_name()` in that it always
+ * returns a static string indicating the type, whereas
+ * `get_token_name()` may return values derived from the
+ * token itself, such as a tag name or processing
+ * instruction tag.
+ *
+ * Possible values:
+ * - `#tag` when matched on a tag.
+ * - `#text` when matched on a text node.
+ * - `#cdata-section` when matched on a CDATA node.
+ * - `#comment` when matched on a comment.
+ * - `#doctype` when matched on a DOCTYPE declaration.
+ * - `#presumptuous-tag` when matched on an empty tag closer.
+ * - `#funky-comment` when matched on a funky comment.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @return string|null What kind of token is matched, or null.
+ */
+ public function get_token_type() {
+ if ( $this->is_virtual() ) {
+ /*
+ * This logic comes from the Tag Processor.
+ *
+ * @todo It would be ideal not to repeat this here, but it's not clearly
+ * better to allow passing a token name to `get_token_type()`.
+ */
+ $node_name = $this->current_element->token->node_name;
+ $starting_char = $node_name[0];
+ if ( 'A' <= $starting_char && 'Z' >= $starting_char ) {
+ return '#tag';
+ }
+
+ if ( 'html' === $node_name ) {
+ return '#doctype';
+ }
+
+ return $node_name;
+ }
+
+ return parent::get_token_type();
+ }
+
+ /**
+ * Returns the value of a requested attribute from a matched tag opener if that attribute exists.
+ *
+ * Example:
+ *
+ * $p = WP_HTML_Processor::create_fragment( '<div enabled class="test" data-test-id="14">Test</div>' );
+ * $p->next_token() === true;
+ * $p->get_attribute( 'data-test-id' ) === '14';
+ * $p->get_attribute( 'enabled' ) === true;
+ * $p->get_attribute( 'aria-label' ) === null;
+ *
+ * $p->next_tag() === false;
+ * $p->get_attribute( 'class' ) === null;
+ *
+ * @since 6.6.0 Subclassed for HTML Processor.
+ *
+ * @param string $name Name of attribute whose value is requested.
+ * @return string|true|null Value of attribute or `null` if not available. Boolean attributes return `true`.
+ */
+ public function get_attribute( $name ) {
+ return $this->is_virtual() ? null : parent::get_attribute( $name );
+ }
+
+ /**
+ * Updates or creates a new attribute on the currently matched tag with the passed value.
+ *
+ * For boolean attributes special handling is provided:
+ * - When `true` is passed as the value, then only the attribute name is added to the tag.
+ * - When `false` is passed, the attribute gets removed if it existed before.
+ *
+ * For string attributes, the value is escaped using the `esc_attr` function.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @param string $name The attribute name to target.
+ * @param string|bool $value The new attribute value.
+ * @return bool Whether an attribute value was set.
+ */
+ public function set_attribute( $name, $value ) {
+ return $this->is_virtual() ? false : parent::set_attribute( $name, $value );
+ }
+
+ /**
+ * Remove an attribute from the currently-matched tag.
+ *
+ * @since 6.6.0 Subclassed for HTML Processor.
+ *
+ * @param string $name The attribute name to remove.
+ * @return bool Whether an attribute was removed.
+ */
+ public function remove_attribute( $name ) {
+ return $this->is_virtual() ? false : parent::remove_attribute( $name );
+ }
+
+ /**
+ * Gets lowercase names of all attributes matching a given prefix in the current tag.
+ *
+ * Note that matching is case-insensitive. This is in accordance with the spec:
+ *
+ * > There must never be two or more attributes on
+ * > the same start tag whose names are an ASCII
+ * > case-insensitive match for each other.
+ * - HTML 5 spec
+ *
+ * Example:
+ *
+ * $p = new WP_HTML_Tag_Processor( '<div data-ENABLED class="test" DATA-test-id="14">Test</div>' );
+ * $p->next_tag( array( 'class_name' => 'test' ) ) === true;
+ * $p->get_attribute_names_with_prefix( 'data-' ) === array( 'data-enabled', 'data-test-id' );
+ *
+ * $p->next_tag() === false;
+ * $p->get_attribute_names_with_prefix( 'data-' ) === null;
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @see https://html.spec.whatwg.org/multipage/syntax.html#attributes-2:ascii-case-insensitive
+ *
+ * @param string $prefix Prefix of requested attribute names.
+ * @return array|null List of attribute names, or `null` when no tag opener is matched.
+ */
+ public function get_attribute_names_with_prefix( $prefix ) {
+ return $this->is_virtual() ? null : parent::get_attribute_names_with_prefix( $prefix );
+ }
+
+ /**
+ * Adds a new class name to the currently matched tag.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @param string $class_name The class name to add.
+ * @return bool Whether the class was set to be added.
+ */
+ public function add_class( $class_name ) {
+ return $this->is_virtual() ? false : parent::add_class( $class_name );
+ }
+
+ /**
+ * Removes a class name from the currently matched tag.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @param string $class_name The class name to remove.
+ * @return bool Whether the class was set to be removed.
+ */
+ public function remove_class( $class_name ) {
+ return $this->is_virtual() ? false : parent::remove_class( $class_name );
+ }
+
+ /**
+ * Returns if a matched tag contains the given ASCII case-insensitive class name.
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @param string $wanted_class Look for this CSS class name, ASCII case-insensitive.
+ * @return bool|null Whether the matched tag contains the given class name, or null if not matched.
+ */
+ public function has_class( $wanted_class ) {
+ return $this->is_virtual() ? null : parent::has_class( $wanted_class );
+ }
+
+ /**
+ * Generator for a foreach loop to step through each class name for the matched tag.
+ *
+ * This generator function is designed to be used inside a "foreach" loop.
+ *
+ * Example:
+ *
+ * $p = WP_HTML_Processor::create_fragment( "<div class='free &lt;egg&lt;\tlang-en'>" );
+ * $p->next_tag();
+ * foreach ( $p->class_list() as $class_name ) {
+ * echo "{$class_name} ";
+ * }
+ * // Outputs: "free <egg> lang-en "
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ */
+ public function class_list() {
+ return $this->is_virtual() ? null : parent::class_list();
+ }
+
+ /**
+ * Returns the modifiable text for a matched token, or an empty string.
+ *
+ * Modifiable text is text content that may be read and changed without
+ * changing the HTML structure of the document around it. This includes
+ * the contents of `#text` nodes in the HTML as well as the inner
+ * contents of HTML comments, Processing Instructions, and others, even
+ * though these nodes aren't part of a parsed DOM tree. They also contain
+ * the contents of SCRIPT and STYLE tags, of TEXTAREA tags, and of any
+ * other section in an HTML document which cannot contain HTML markup (DATA).
+ *
+ * If a token has no modifiable text then an empty string is returned to
+ * avoid needless crashing or type errors. An empty string does not mean
+ * that a token has modifiable text, and a token with modifiable text may
+ * have an empty string (e.g. a comment with no contents).
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @return string
+ */
+ public function get_modifiable_text() {
+ return $this->is_virtual() ? '' : parent::get_modifiable_text();
+ }
+
+ /**
+ * Indicates what kind of comment produced the comment node.
+ *
+ * Because there are different kinds of HTML syntax which produce
+ * comments, the Tag Processor tracks and exposes this as a type
+ * for the comment. Nominally only regular HTML comments exist as
+ * they are commonly known, but a number of unrelated syntax errors
+ * also produce comments.
+ *
+ * @see self::COMMENT_AS_ABRUPTLY_CLOSED_COMMENT
+ * @see self::COMMENT_AS_CDATA_LOOKALIKE
+ * @see self::COMMENT_AS_INVALID_HTML
+ * @see self::COMMENT_AS_HTML_COMMENT
+ * @see self::COMMENT_AS_PI_NODE_LOOKALIKE
+ *
+ * @since 6.6.0 Subclassed for the HTML Processor.
+ *
+ * @return string|null
+ */
+ public function get_comment_type() {
+ return $this->is_virtual() ? null : parent::get_comment_type();
+ }
+
+ /**
* Removes a bookmark that is no longer needed.
*
* Releasing a bookmark frees up the small
@@ -1304,6 +1841,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
? $this->bookmarks[ $this->state->current_token->bookmark_name ]->start
: 0;
$bookmark_starts_at = $this->bookmarks[ $actual_bookmark_name ]->start;
+ $bookmark_length = $this->bookmarks[ $actual_bookmark_name ]->length;
$direction = $bookmark_starts_at > $processor_started_at ? 'forward' : 'backward';
/*
@@ -1359,6 +1897,8 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
parent::seek( 'context-node' );
$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
$this->state->frameset_ok = true;
+ $this->element_queue = array();
+ $this->current_element = null;
}
// When moving forwards, reparse the document until reaching the same location as the original bookmark.
@@ -1366,8 +1906,11 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return true;
}
- while ( $this->step() ) {
+ while ( $this->next_token() ) {
if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) {
+ while ( isset( $this->current_element ) && WP_HTML_Stack_Event::POP === $this->current_element->operation ) {
+ $this->current_element = array_shift( $this->element_queue );
+ }
return true;
}
}