[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/html-api/ -> class-wp-html-processor.php (summary)

HTML API: WP_HTML_Processor class

File Size: 6622 lines (212 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

WP_HTML_Processor:: (79 methods):
  create_fragment()
  create_full_parser()
  __construct()
  create_fragment_at_current_node()
  bail()
  get_last_error()
  get_unsupported_exception()
  next_tag()
  next_token()
  next_visitable_token()
  is_tag_closer()
  is_virtual()
  matches_breadcrumbs()
  expects_closer()
  step()
  get_breadcrumbs()
  get_current_depth()
  normalize()
  serialize()
  serialize_token()
  step_initial()
  step_before_html()
  step_before_head()
  step_in_head()
  step_in_head_noscript()
  step_after_head()
  step_in_body()
  step_in_table()
  step_in_table_text()
  step_in_caption()
  step_in_column_group()
  step_in_table_body()
  step_in_row()
  step_in_cell()
  step_in_select()
  step_in_select_in_table()
  step_in_template()
  step_after_body()
  step_in_frameset()
  step_after_frameset()
  step_after_after_body()
  step_after_after_frameset()
  step_in_foreign_content()
  bookmark_token()
  get_namespace()
  get_tag()
  has_self_closing_flag()
  get_token_name()
  get_token_type()
  get_attribute()
  set_attribute()
  remove_attribute()
  get_attribute_names_with_prefix()
  add_class()
  remove_class()
  has_class()
  class_list()
  get_modifiable_text()
  get_comment_type()
  release_bookmark()
  seek()
  set_bookmark()
  has_bookmark()
  close_a_p_element()
  generate_implied_end_tags()
  generate_implied_end_tags_thoroughly()
  get_adjusted_current_node()
  reconstruct_active_formatting_elements()
  reset_insertion_mode_appropriately()
  run_adoption_agency_algorithm()
  close_cell()
  insert_html_element()
  insert_foreign_element()
  insert_virtual_node()
  is_mathml_integration_point()
  is_html_integration_point()
  is_special()
  is_void()
  get_encoding()


Class: WP_HTML_Processor  - X-Ref

Core class used to safely parse and modify an HTML document.

The HTML Processor class properly parses and modifies HTML5 documents.

It supports a subset of the HTML5 specification, and when it encounters
unsupported markup, it aborts early to avoid unintentionally breaking
the document. The HTML Processor should never break an HTML document.

While the `WP_HTML_Tag_Processor` is a valuable tool for modifying
attributes on individual HTML tags, the HTML Processor is more capable
and useful for the following operations:

- Querying based on nested HTML structure.

Eventually the HTML Processor will also support:
- Wrapping a tag in surrounding HTML.
- Unwrapping a tag by removing its parent.
- Inserting and removing nodes.
- Reading and changing inner content.
- Navigating up or around HTML structure.

## Usage

Use of this class requires three steps:

1. Call a static creator method with your input HTML document.
2. Find the location in the document you are looking for.
3. Request changes to the document at that location.

Example:

$processor = WP_HTML_Processor::create_fragment( $html );
if ( $processor->next_tag( array( 'breadcrumbs' => array( 'DIV', 'FIGURE', 'IMG' ) ) ) ) {
$processor->add_class( 'responsive-image' );
}

#### Breadcrumbs

Breadcrumbs represent the stack of open elements from the root
of the document or fragment down to the currently-matched node,
if one is currently selected. Call WP_HTML_Processor::get_breadcrumbs()
to inspect the breadcrumbs for a matched tag.

Breadcrumbs can specify nested HTML structure and are equivalent
to a CSS selector comprising tag names separated by the child
combinator, such as "DIV > FIGURE > IMG".

Since all elements find themselves inside a full HTML document
when parsed, the return value from `get_breadcrumbs()` will always
contain any implicit outermost elements. For example, when parsing
with `create_fragment()` in the `BODY` context (the default), any
tag in the given HTML document will contain `array( 'HTML', 'BODY', … )`
in its breadcrumbs.

Despite containing the implied outermost elements in their breadcrumbs,
tags may be found with the shortest-matching breadcrumb query. That is,
`array( 'IMG' )` matches all IMG elements and `array( 'P', 'IMG' )`
matches all IMG elements directly inside a P element. To ensure that no
partial matches erroneously match it's possible to specify in a query
the full breadcrumb match all the way down from the root HTML element.

Example:

$html = '<figure><img><figcaption>A <em>lovely</em> day outside</figcaption></figure>';
//               ----- Matches here.
$processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'IMG' ) ) );

$html = '<figure><img><figcaption>A <em>lovely</em> day outside</figcaption></figure>';
//                                  ---- Matches here.
$processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'FIGCAPTION', 'EM' ) ) );

$html = '<div><img></div><img>';
//                       ----- Matches here, because IMG must be a direct child of the implicit BODY.
$processor->next_tag( array( 'breadcrumbs' => array( 'BODY', 'IMG' ) ) );

## HTML Support

This class implements a small part of the HTML5 specification.
It's designed to operate within its support and abort early whenever
encountering circumstances it can't properly handle. This is
the principle way in which this class remains as simple as possible
without cutting corners and breaking compliance.

### Supported elements

If any unsupported element appears in the HTML input the HTML Processor
will abort early and stop all processing. This draconian measure ensures
that the HTML Processor won't break any HTML it doesn't fully understand.

The HTML Processor supports all elements other than a specific set:

- Any element inside a TABLE.
- Any element inside foreign content, including SVG and MATH.
- Any element outside the IN BODY insertion mode, e.g. doctype declarations, meta, links.

### Supported markup

Some kinds of non-normative HTML involve reconstruction of formatting elements and
re-parenting of mis-nested elements. For example, a DIV tag found inside a TABLE
may in fact belong _before_ the table in the DOM. If the HTML Processor encounters
such a case it will stop processing.

The following list illustrates some common examples of unexpected HTML inputs that
the HTML Processor properly parses and represents:

- HTML with optional tags omitted, e.g. `<p>one<p>two`.
- HTML with unexpected tag closers, e.g. `<p>one </span> more</p>`.
- Non-void tags with self-closing flag, e.g. `<div/>the DIV is still open.</div>`.
- Heading elements which close open heading elements of another level, e.g. `<h1>Closed by </h2>`.
- Elements containing text that looks like other tags but isn't, e.g. `<title>The <img> is plaintext</title>`.
- SCRIPT and STYLE tags containing text that looks like HTML but isn't, e.g. `<script>document.write('<p>Hi</p>');</script>`.
- SCRIPT content which has been escaped, e.g. `<script><!-- document.write('<script>console.log("hi")</script>') --></script>`.

### Unsupported Features

This parser does not report parse errors.

Normally, when additional HTML or BODY tags are encountered in a document, if there
are any additional attributes on them that aren't found on the previous elements,
the existing HTML and BODY elements adopt those missing attribute values. This
parser does not add those additional attributes.

In certain situations, elements are moved to a different part of the document in
a process called "adoption" and "fostering." Because the nodes move to a location
in the document that the parser had already processed, this parser does not support
these situations and will bail.

create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' )   X-Ref
Creates an HTML processor in the fragment parsing mode.

Use this for cases where you are processing chunks of HTML that
will be found within a bigger HTML document, such as rendered
block output that exists within a post, `the_content` inside a
rendered site layout.

Fragment parsing occurs within a context, which is an HTML element
that the document will eventually be placed in. It becomes important
when special elements have different rules than others, such as inside
a TEXTAREA or a TITLE tag where things that look like tags are text,
or inside a SCRIPT tag where things that look like HTML syntax are JS.

The context value should be a representation of the tag into which the
HTML is found. For most cases this will be the body element. The HTML
form is provided because a context element may have attributes that
impact the parse, such as with a SCRIPT tag and its `type` attribute.

## Current HTML Support

- The only supported context is `<body>`, which is the default value.
- The only supported document encoding is `UTF-8`, which is the default value.

return: static|null The created processor if successful, otherwise null.
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'.

create_full_parser( $html, $known_definite_encoding = 'UTF-8' )   X-Ref
Creates an HTML processor in the full parsing mode.

It's likely that a fragment parser is more appropriate, unless sending an
entire HTML document from start to finish. Consider a fragment parser with
a context node of `<body>`.

UTF-8 is the only allowed encoding. If working with a document that
isn't UTF-8, first convert the document to UTF-8, then pass in the
converted HTML.

return: static|null The created processor if successful, otherwise null.
param: string      $html                    Input HTML document to process.
param: string|null $known_definite_encoding Optional. If provided, specifies the charset used

__construct( $html, $use_the_static_create_methods_instead = null )   X-Ref
Constructor.

Do not use this method. Use the static creator methods instead.

param: string      $html                                  HTML to process.
param: string|null $use_the_static_create_methods_instead This constructor should not be called manually.

create_fragment_at_current_node( string $html )   X-Ref
No description

bail( string $message )   X-Ref
Stops the parser and terminates its execution when encountering unsupported markup.

param: string $message Explains support is missing in order to parse the current node.

get_last_error()   X-Ref
Returns the last error, if any.

Various situations lead to parsing failure but this class will
return `false` in all those cases. To determine why something
failed it's possible to request the last error. This can be
helpful to know to distinguish whether a given tag couldn't
be found or if content in the document caused the processor
to give up and abort processing.

Example

$processor = WP_HTML_Processor::create_fragment( '<template><strong><button><em><p><em>' );
false === $processor->next_tag();
WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error();

return: string|null The last error, if one exists, otherwise null.

get_unsupported_exception()   X-Ref
Returns context for why the parser aborted due to unsupported HTML, if it did.

This is meant for debugging purposes, not for production use.

return: WP_HTML_Unsupported_Exception|null

next_tag( $query = null )   X-Ref
Finds the next tag matching the $query.

return: bool Whether a tag was matched.
param: array|string|null $query {

next_token()   X-Ref
Finds the next token in the HTML document.

This doesn't currently have a way to represent non-tags and doesn't process
semantic rules for text nodes. For access to the raw tokens consider using
WP_HTML_Tag_Processor instead.

return: bool Whether a token was parsed.

next_visitable_token()   X-Ref
Ensures internal accounting is maintained for HTML semantic rules while
the underlying Tag Processor class is seeking to a bookmark.

This doesn't currently have a way to represent non-tags and doesn't process
semantic rules for text nodes. For access to the raw tokens consider using
WP_HTML_Tag_Processor instead.

Note that this method may call itself recursively. This is why it is not
implemented as {@see WP_HTML_Processor::next_token()}, which instead calls
this method similarly to how {@see WP_HTML_Tag_Processor::next_token()}
calls the {@see WP_HTML_Tag_Processor::base_class_next_token()} method.

return: bool

is_tag_closer()   X-Ref
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;

return: bool Whether the current tag is a tag closer.

is_virtual()   X-Ref
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.

return: bool Whether the current token is virtual.

matches_breadcrumbs( $breadcrumbs )   X-Ref
Indicates if the currently-matched tag matches the given breadcrumbs.

A "*" represents a single tag wildcard, where any tag matches, but not no tags.

At some point this function _may_ support a `**` syntax for matching any number
of unspecified tags in the breadcrumb stack. This has been intentionally left
out, however, to keep this function simple and to avoid introducing backtracking,
which could open up surprising performance breakdowns.

Example:

$processor = WP_HTML_Processor::create_fragment( '<div><span><figure><img></figure></span></div>' );
$processor->next_tag( 'img' );
true  === $processor->matches_breadcrumbs( array( 'figure', 'img' ) );
true  === $processor->matches_breadcrumbs( array( 'span', 'figure', 'img' ) );
false === $processor->matches_breadcrumbs( array( 'span', 'img' ) );
true  === $processor->matches_breadcrumbs( array( 'span', '*', 'img' ) );

return: bool Whether the currently-matched tag is found at the given nested structure.
param: string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`.

expects_closer( ?WP_HTML_Token $node = null )   X-Ref
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.

return: bool|null Whether to expect a closer for the currently-matched node,
param: WP_HTML_Token|null $node Optional. Node to examine, if provided.

step( $node_to_process = self::PROCESS_NEXT_NODE )   X-Ref
Steps through the HTML document and stop at the next tag, if any.

return: bool Whether a tag was matched.
param: string $node_to_process Whether to parse the next node or reprocess the current node.

get_breadcrumbs()   X-Ref
Computes the HTML breadcrumbs for the currently-matched node, if matched.

Breadcrumbs start at the outermost parent and descend toward the matched element.
They always include the entire path from the root HTML node to the matched element.

Example:

$processor = WP_HTML_Processor::create_fragment( '<p><strong><em><img></em></strong></p>' );
$processor->next_tag( 'IMG' );
$processor->get_breadcrumbs() === array( 'HTML', 'BODY', 'P', 'STRONG', 'EM', 'IMG' );

return: string[] Array of tag names representing path to matched node.

get_current_depth()   X-Ref
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();

return: int Nesting-depth of current location in the document.

normalize( string $html )   X-Ref
Normalizes an HTML fragment by serializing it.

This method assumes that the given HTML snippet is found in BODY context.
For normalizing full documents or fragments found in other contexts, create
a new processor using {@see WP_HTML_Processor::create_fragment} or
{@see WP_HTML_Processor::create_full_parser} and call {@see WP_HTML_Processor::serialize}
on the created instances.

Many aspects of an input HTML fragment may be changed during normalization.

- Attribute values will be double-quoted.
- Duplicate attributes will be removed.
- Omitted tags will be added.
- Tag and attribute name casing will be lower-cased,
except for specific SVG and MathML tags or attributes.
- Text will be re-encoded, null bytes handled,
and invalid UTF-8 replaced with U+FFFD.
- Any incomplete syntax trailing at the end will be omitted,
for example, an unclosed comment opener will be removed.

Example:

echo WP_HTML_Processor::normalize( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' );
// <a href="#anchor" v="5" enabled>One</a>

echo WP_HTML_Processor::normalize( '<div></p>fun<table><td>cell</div>' );
// <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div>

echo WP_HTML_Processor::normalize( '<![CDATA[invalid comment]]> syntax < <> "oddities"' );
// <!--[CDATA[invalid comment]]--> syntax &lt; &lt;&gt; &quot;oddities&quot;

return: string|null Normalized output, or `null` if unable to normalize.
param: string $html Input HTML to normalize.

serialize()   X-Ref
Returns normalized HTML for a fragment by serializing it.

This differs from {@see WP_HTML_Processor::normalize} in that it starts with
a specific HTML Processor, which _must_ not have already started scanning;
it must be in the initial ready state and will be in the completed state once
serialization is complete.

Many aspects of an input HTML fragment may be changed during normalization.

- Attribute values will be double-quoted.
- Duplicate attributes will be removed.
- Omitted tags will be added.
- Tag and attribute name casing will be lower-cased,
except for specific SVG and MathML tags or attributes.
- Text will be re-encoded, null bytes handled,
and invalid UTF-8 replaced with U+FFFD.
- Any incomplete syntax trailing at the end will be omitted,
for example, an unclosed comment opener will be removed.

Example:

$processor = WP_HTML_Processor::create_fragment( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' );
echo $processor->serialize();
// <a href="#anchor" v="5" enabled>One</a>

$processor = WP_HTML_Processor::create_fragment( '<div></p>fun<table><td>cell</div>' );
echo $processor->serialize();
// <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div>

$processor = WP_HTML_Processor::create_fragment( '<![CDATA[invalid comment]]> syntax < <> "oddities"' );
echo $processor->serialize();
// <!--[CDATA[invalid comment]]--> syntax &lt; &lt;&gt; &quot;oddities&quot;

return: string|null Normalized HTML markup represented by processor,

serialize_token()   X-Ref
Serializes the currently-matched token.

This method produces a fully-normative HTML string for the currently-matched token,
if able. If not matched at any token or if the token doesn't correspond to any HTML
it will return an empty string (for example, presumptuous end tags are ignored).

return: string Serialization of token, or empty string if no serialization exists.

step_initial()   X-Ref
Parses next element in the 'initial' insertion mode.

This internal function performs the 'initial' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_before_html()   X-Ref
Parses next element in the 'before html' insertion mode.

This internal function performs the 'before html' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_before_head()   X-Ref
Parses next element in the 'before head' insertion mode.

This internal function performs the 'before head' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_head()   X-Ref
Parses next element in the 'in head' insertion mode.

This internal function performs the 'in head' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_head_noscript()   X-Ref
Parses next element in the 'in head noscript' insertion mode.

This internal function performs the 'in head noscript' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_after_head()   X-Ref
Parses next element in the 'after head' insertion mode.

This internal function performs the 'after head' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_body()   X-Ref
Parses next element in the 'in body' insertion mode.

This internal function performs the 'in body' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_table()   X-Ref
Parses next element in the 'in table' insertion mode.

This internal function performs the 'in table' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_table_text()   X-Ref
Parses next element in the 'in table text' insertion mode.

This internal function performs the 'in table text' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_caption()   X-Ref
Parses next element in the 'in caption' insertion mode.

This internal function performs the 'in caption' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_column_group()   X-Ref
Parses next element in the 'in column group' insertion mode.

This internal function performs the 'in column group' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_table_body()   X-Ref
Parses next element in the 'in table body' insertion mode.

This internal function performs the 'in table body' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_row()   X-Ref
Parses next element in the 'in row' insertion mode.

This internal function performs the 'in row' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_cell()   X-Ref
Parses next element in the 'in cell' insertion mode.

This internal function performs the 'in cell' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_select()   X-Ref
Parses next element in the 'in select' insertion mode.

This internal function performs the 'in select' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_select_in_table()   X-Ref
Parses next element in the 'in select in table' insertion mode.

This internal function performs the 'in select in table' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_template()   X-Ref
Parses next element in the 'in template' insertion mode.

This internal function performs the 'in template' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_after_body()   X-Ref
Parses next element in the 'after body' insertion mode.

This internal function performs the 'after body' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_frameset()   X-Ref
Parses next element in the 'in frameset' insertion mode.

This internal function performs the 'in frameset' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_after_frameset()   X-Ref
Parses next element in the 'after frameset' insertion mode.

This internal function performs the 'after frameset' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_after_after_body()   X-Ref
Parses next element in the 'after after body' insertion mode.

This internal function performs the 'after after body' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_after_after_frameset()   X-Ref
Parses next element in the 'after after frameset' insertion mode.

This internal function performs the 'after after frameset' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

step_in_foreign_content()   X-Ref
Parses next element in the 'in foreign content' insertion mode.

This internal function performs the 'in foreign content' insertion mode
logic for the generalized WP_HTML_Processor::step() function.

return: bool Whether an element was found.

bookmark_token()   X-Ref
Creates a new bookmark for the currently-matched token and returns the generated name.

return: string|false Name of created bookmark, or false if unable to create.

get_namespace()   X-Ref
Indicates the namespace of the current token, or "html" if there is none.

return: string One of "html", "math", or "svg".

get_tag()   X-Ref
Returns the uppercase name of the matched tag.

The semantic rules for HTML specify that certain tags be reprocessed
with a different tag name. Because of this, the tag name presented
by the HTML Processor may differ from the one reported by the HTML
Tag Processor, which doesn't apply these semantic rules.

Example:

$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$processor->next_tag() === true;
$processor->get_tag() === 'DIV';

$processor->next_tag() === false;
$processor->get_tag() === null;

return: string|null Name of currently matched tag in input HTML, or `null` if none found.

has_self_closing_flag()   X-Ref
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.

return: bool Whether the currently matched tag contains the self-closing flag.

get_token_name()   X-Ref
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.

return: string|null Name of the matched token.

get_token_type()   X-Ref
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.

return: string|null What kind of token is matched, or null.

get_attribute( $name )   X-Ref
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;

return: string|true|null Value of attribute or `null` if not available. Boolean attributes return `true`.
param: string $name Name of attribute whose value is requested.

set_attribute( $name, $value )   X-Ref
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.

return: bool Whether an attribute value was set.
param: string      $name  The attribute name to target.
param: string|bool $value The new attribute value.

remove_attribute( $name )   X-Ref
Remove an attribute from the currently-matched tag.

return: bool Whether an attribute was removed.
param: string $name The attribute name to remove.

get_attribute_names_with_prefix( $prefix )   X-Ref
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;

return: array|null List of attribute names, or `null` when no tag opener is matched.
param: string $prefix Prefix of requested attribute names.

add_class( $class_name )   X-Ref
Adds a new class name to the currently matched tag.

return: bool Whether the class was set to be added.
param: string $class_name The class name to add.

remove_class( $class_name )   X-Ref
Removes a class name from the currently matched tag.

return: bool Whether the class was set to be removed.
param: string $class_name The class name to remove.

has_class( $wanted_class )   X-Ref
Returns if a matched tag contains the given ASCII case-insensitive class name.

return: bool|null Whether the matched tag contains the given class name, or null if not matched.
param: string $wanted_class Look for this CSS class name, ASCII case-insensitive.

class_list()   X-Ref
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 "


get_modifiable_text()   X-Ref
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).

return: string

get_comment_type()   X-Ref
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.

return: string|null

release_bookmark( $bookmark_name )   X-Ref
Removes a bookmark that is no longer needed.

Releasing a bookmark frees up the small
performance overhead it requires.

return: bool Whether the bookmark already existed before removal.
param: string $bookmark_name Name of the bookmark to remove.

seek( $bookmark_name )   X-Ref
Moves the internal cursor in the HTML Processor to a given bookmark's location.

Be careful! Seeking backwards to a previous location resets the parser to the
start of the document and reparses the entire contents up until it finds the
sought-after bookmarked location.

In order to prevent accidental infinite loops, there's a
maximum limit on the number of times seek() can be called.

return: bool Whether the internal cursor was successfully moved to the bookmark's location.
param: string $bookmark_name Jump to the place in the document identified by this bookmark name.

set_bookmark( $bookmark_name )   X-Ref
Sets a bookmark in the HTML document.

Bookmarks represent specific places or tokens in the HTML
document, such as a tag opener or closer. When applying
edits to a document, such as setting an attribute, the
text offsets of that token may shift; the bookmark is
kept updated with those shifts and remains stable unless
the entire span of text in which the token sits is removed.

Release bookmarks when they are no longer needed.

Example:

<main><h2>Surprising fact you may not know!</h2></main>
^  ^
\-|-- this `H2` opener bookmark tracks the token

<main class="clickbait"><h2>Surprising fact you may no…
^  ^
\-|-- it shifts with edits

Bookmarks provide the ability to seek to a previously-scanned
place in the HTML document. This avoids the need to re-scan
the entire document.

Example:

<ul><li>One</li><li>Two</li><li>Three</li></ul>
^^^^
want to note this last item

$p = new WP_HTML_Tag_Processor( $html );
$in_list = false;
while ( $p->next_tag( array( 'tag_closers' => $in_list ? 'visit' : 'skip' ) ) ) {
if ( 'UL' === $p->get_tag() ) {
if ( $p->is_tag_closer() ) {
$in_list = false;
$p->set_bookmark( 'resume' );
if ( $p->seek( 'last-li' ) ) {
$p->add_class( 'last-li' );
}
$p->seek( 'resume' );
$p->release_bookmark( 'last-li' );
$p->release_bookmark( 'resume' );
} else {
$in_list = true;
}
}

if ( 'LI' === $p->get_tag() ) {
$p->set_bookmark( 'last-li' );
}
}

Bookmarks intentionally hide the internal string offsets
to which they refer. They are maintained internally as
updates are applied to the HTML document and therefore
retain their "position" - the location to which they
originally pointed. The inability to use bookmarks with
functions like `substr` is therefore intentional to guard
against accidentally breaking the HTML.

Because bookmarks allocate memory and require processing
for every applied update, they are limited and require
a name. They should not be created with programmatically-made
names, such as "li_{$index}" with some loop. As a general
rule they should only be created with string-literal names
like "start-of-section" or "last-paragraph".

Bookmarks are a powerful tool to enable complicated behavior.
Consider double-checking that you need this tool if you are
reaching for it, as inappropriate use could lead to broken
HTML structure or unwanted processing overhead.

Bookmarks cannot be set on tokens that do no appear in the original
HTML text. For example, the HTML `<table><td>` stops at tags `TABLE`,
`TBODY`, `TR`, and `TD`. The `TBODY` and `TR` tags do not appear in
the original HTML and cannot be used as bookmarks.

return: bool Whether the bookmark was successfully created.
param: string $bookmark_name Identifies this particular bookmark.

has_bookmark( $bookmark_name )   X-Ref
Checks whether a bookmark with the given name exists.

return: bool Whether that bookmark exists.
param: string $bookmark_name Name to identify a bookmark that potentially exists.

close_a_p_element()   X-Ref
Closes a P element.


generate_implied_end_tags( ?string $except_for_this_element = null )   X-Ref
Closes elements that have implied end tags.

param: string|null $except_for_this_element Perform as if this element doesn't exist in the stack of open elements.

generate_implied_end_tags_thoroughly()   X-Ref
Closes elements that have implied end tags, thoroughly.

See the HTML specification for an explanation why this is
different from generating end tags in the normal sense.


get_adjusted_current_node()   X-Ref
Returns the adjusted current node.

> The adjusted current node is the context element if the parser was created as
> part of the HTML fragment parsing algorithm and the stack of open elements
> has only one element in it (fragment case); otherwise, the adjusted current
> node is the current node.

return: WP_HTML_Token|null The adjusted current node.

reconstruct_active_formatting_elements()   X-Ref
Reconstructs the active formatting elements.

> This has the effect of reopening all the formatting elements that were opened
> in the current body, cell, or caption (whichever is youngest) that haven't
> been explicitly closed.

return: bool Whether any formatting elements needed to be reconstructed.

reset_insertion_mode_appropriately()   X-Ref
Runs the reset the insertion mode appropriately algorithm.


run_adoption_agency_algorithm()   X-Ref
Runs the adoption agency algorithm.


close_cell()   X-Ref
Runs the "close the cell" algorithm.

> Where the steps above say to close the cell, they mean to run the following algorithm:
>   1. Generate implied end tags.
>   2. If the current node is not now a td element or a th element, then this is a parse error.
>   3. Pop elements from the stack of open elements stack until a td element or a th element has been popped from the stack.
>   4. Clear the list of active formatting elements up to the last marker.
>   5. Switch the insertion mode to "in row".


insert_html_element( WP_HTML_Token $token )   X-Ref
Inserts an HTML element on the stack of open elements.

param: WP_HTML_Token $token Name of bookmark pointing to element in original input HTML.

insert_foreign_element( WP_HTML_Token $token, bool $only_add_to_element_stack )   X-Ref
Inserts a foreign element on to the stack of open elements.

param: WP_HTML_Token $token                     Insert this token. The token's namespace and
param: bool          $only_add_to_element_stack Whether to skip the "insert an element at the adjusted

insert_virtual_node( $token_name, $bookmark_name = null )   X-Ref
Inserts a virtual element on the stack of open elements.

return: WP_HTML_Token Newly-created virtual token.
param: string      $token_name    Name of token to create and insert into the stack of open elements.
param: string|null $bookmark_name Optional. Name to give bookmark for created virtual node.

is_mathml_integration_point()   X-Ref
Indicates if the current token is a MathML integration point.

return: bool Whether the current token is a MathML integration point.

is_html_integration_point()   X-Ref
Indicates if the current token is an HTML integration point.

Note that this method must be an instance method with access
to the current token, since it needs to examine the attributes
of the currently-matched tag, if it's in the MathML namespace.
Otherwise it would be required to scan the HTML and ensure that
no other accounting is overlooked.

return: bool Whether the current token is an HTML integration point.

is_special( $tag_name )   X-Ref
Returns whether an element of a given name is in the HTML special category.

return: bool Whether the element of the given name is in the special category.
param: WP_HTML_Token|string $tag_name Node to check, or only its name if in the HTML namespace.

is_void( $tag_name )   X-Ref
Returns whether a given element is an HTML Void Element

> area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr

return: bool Whether the given tag is an HTML Void Element.
param: string $tag_name Name of HTML tag to check.

get_encoding( string $label )   X-Ref
Gets an encoding from a given string.

This is an algorithm defined in the WHAT-WG specification.

Example:

'UTF-8' === self::get_encoding( 'utf8' );
'UTF-8' === self::get_encoding( "  \tUTF-8 " );
null    === self::get_encoding( 'UTF-7' );
null    === self::get_encoding( 'utf8; charset=' );

return: string|null Known encoding if matched, otherwise null.
param: string $label A string which may specify a known encoding.



Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref