HTML-API: Introduce minimal HTML Processor.
This patch introduces the //first// of //many// iterations on the evolution of the HTML API, the HTML Processor, which is built in order to understand HTML structure including nesting, misnesting, and complicated semantic rules. In the first iteration, the HTML Processor is arbitrarily limited to a minimal subset of functionality so that we can review it, ship it, test it, and collect feedback before moving forward. This means that this patch is more or less an extension to the Tag Processor query language, providing the ability not only to scan for a tag of a given name, but also to find an HTML element in a specific nesting path. The HTML Processor also aborts any time it encounters: - a tag that isn't a `P`, `DIV`, `FIGURE`, `FIGCAPTION`, `IMG`, `STRONG`, `B`, `EM`, `I`, `A`, `BIG`, `CODE`, `FONT`, `SMALL`, `STRIKE`, `TT`, or `U` tag. this limit exists because many HTML elements require specific rules and we are trying to limit the number of rules introduced at once. this work is targeted at existing work in places like the image block. - certain misnesting constructs that evoke complicated resolution inside the HTML spec. where possible and where simple to do reliably, certain parse errors are handled. in most cases the HTML Processor aborts. The structure of the HTML Processor is established in this patch. Further spec-compliance comes through filling out //more of the same// kind and nature of code as is found in this patch. Certain critical HTML algorithms are partially supported, and where support requires more than is present, the HTML Processor acknowledges this and refuses to operate. In this patch are explorations for how to verify that new HTML support is fully added (instead of allowing for partial updates that leave some code paths non-compliant). Performance is hard to measure since support is so limited at the current time, but it should generally follow the performance of the Tag Processor somewhat close as the overhead is minimized as much as practical. Props dmsnell, zieladam, costdev. Fixes #58517. Built from https://develop.svn.wordpress.org/trunk@56274 git-svn-id: http://core.svn.wordpress.org/trunk@55786 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
3a065bfb2c
commit
44629e6286
|
@ -0,0 +1,188 @@
|
|||
<?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 ];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,413 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* @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 ) {
|
||||
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on list item scope.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(
|
||||
/*
|
||||
* 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 table scope.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/#has-an-element-in-table-scope
|
||||
*
|
||||
* @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.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @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.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
$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 ) {
|
||||
$this->pop();
|
||||
|
||||
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 ) {
|
||||
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
|
||||
*/
|
||||
public function walk_up() {
|
||||
for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
|
||||
yield $this->stack[ $i ];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 'P':
|
||||
$this->has_p_in_button_scope = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 'P':
|
||||
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -522,7 +522,7 @@ class WP_HTML_Tag_Processor {
|
|||
* @type string|null $class_name Tag must contain this whole class name to match.
|
||||
* @type string|null $tag_closers "visit" or "skip": whether to stop on tag closers, e.g. </div>.
|
||||
* }
|
||||
* @return boolean Whether a tag was matched.
|
||||
* @return bool Whether a tag was matched.
|
||||
*/
|
||||
public function next_tag( $query = null ) {
|
||||
$this->parse_query( $query );
|
||||
|
@ -673,7 +673,7 @@ class WP_HTML_Tag_Processor {
|
|||
return false;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( $name, $this->bookmarks ) && count( $this->bookmarks ) >= self::MAX_BOOKMARKS ) {
|
||||
if ( ! array_key_exists( $name, $this->bookmarks ) && count( $this->bookmarks ) >= static::MAX_BOOKMARKS ) {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'Too many bookmarks: cannot create any more.' ),
|
||||
|
@ -1538,7 +1538,7 @@ class WP_HTML_Tag_Processor {
|
|||
return false;
|
||||
}
|
||||
|
||||
if ( ++$this->seek_count > self::MAX_SEEK_OPS ) {
|
||||
if ( ++$this->seek_count > static::MAX_SEEK_OPS ) {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'Too many calls to seek() - this can lead to performance issues.' ),
|
||||
|
@ -2221,7 +2221,7 @@ class WP_HTML_Tag_Processor {
|
|||
*
|
||||
* @since 6.2.0
|
||||
*
|
||||
* @return boolean Whether the given tag and its attribute match the search criteria.
|
||||
* @return bool Whether the given tag and its attribute match the search criteria.
|
||||
*/
|
||||
private function matches() {
|
||||
if ( $this->is_closing_tag && ! $this->stop_on_tag_closers ) {
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
<?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 {@see WP_HTML_Processor::get_tag}.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.4-alpha-56273';
|
||||
$wp_version = '6.4-alpha-56274';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
|
@ -239,6 +239,12 @@ require ABSPATH . WPINC . '/html-api/class-wp-html-attribute-token.php';
|
|||
require ABSPATH . WPINC . '/html-api/class-wp-html-span.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-text-replacement.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-tag-processor.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-unsupported-exception.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-active-formatting-elements.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-open-elements.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-token.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-processor-state.php';
|
||||
require ABSPATH . WPINC . '/html-api/class-wp-html-processor.php';
|
||||
require ABSPATH . WPINC . '/class-wp-http.php';
|
||||
require ABSPATH . WPINC . '/class-wp-http-streams.php';
|
||||
require ABSPATH . WPINC . '/class-wp-http-curl.php';
|
||||
|
|
Loading…
Reference in New Issue