WP_HTML_Processor::create_fragment_at_current_node( string $html ): static|null
- Since
- 6.8.0
- Source
wp-includes/html-api/class-wp-html-processor.php:484
Description
HTML Fragment parsing always happens with a context node. HTML Fragment Processors can be instantiated with a BODY context node via WP_HTML_Processor::create_fragment( $html ).
The context node may impact how a fragment of HTML is parsed. For example, consider the HTML fragment <td />Inside TD?</td>.
A BODY context node will produce the following tree:
└─#text Inside TD? Notice that the <td> tags are completely ignored.
Compare that with an SVG context node that produces the following tree:
├─svg:td
└─#text Inside TD? Here, a td node in the svg namespace is created, and its self-closing flag is respected.
This is a peculiarity of parsing HTML in foreign content like SVG.
Finally, consider the tree produced with a TABLE context node:
└─TBODY
└─TR
└─TD
└─#text Inside TD? These examples demonstrate how important the context node may be when processing an HTML fragment. Special care must be taken when processing fragments that are expected to appear in specific contexts. SVG and TABLE are good examples, but there are others.
Compatibility
- WordPress
- since 6.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 4 of the 5 tracked releases, added in 6.8.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$htmlstring- Input HTML fragment to process.
Return value
static|null- The created processor if successful, otherwise null.
Performance profile
How much work a call to WP_HTML_Processor::create_fragment_at_current_node() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 14–124
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 160.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_HTML_Processor::create_fragment_at_current_node()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTML_Processor::create_fragment_at_current_node() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | ->get_token_type(), __(), _doing_it_wrong() |
->is_tag_closer() | 17 | ->get_token_type(), ->is_tag_closer(), __(), _doing_it_wrong() |
!->is_tag_closer() && $tag_name | 33 | ->get_token_type(), ->is_tag_closer(), __(), sprintf(), _doing_it_wrong() |
!->is_tag_closer() && $namespace === "html" | 33–37 | ->get_token_type(), ->is_tag_closer(), ::is_void(), __(), sprintf(), _doing_it_wrong() |
!->is_tag_closer() && $namespace !== "html" | 93–120 | ->get_token_type(), ->is_tag_closer(), ->push(), ->reset_insertion_mode_appropriately(), ->walk_up(), ->change_parsing_namespace() |
!->is_tag_closer() && $namespace === "html" && !::is_void() | 97–124 | ->get_token_type(), ->is_tag_closer(), ::is_void(), ->push(), ->reset_insertion_mode_appropriately(), ->walk_up(), ->change_parsing_namespace() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 160 instructions, 14–124 executed per call, 12 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 8
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- WP_HTML_Processor::get_token_type()Indicates the kind of matched token, if any.
- WP_HTML_Processor::is_tag_closer()Indicates if the current tag token is a tag closer.
- WP_HTML_Processor::is_void()Returns whether a given element is an HTML Void Element
- static::__construct()
- WP_HTML_Span::__construct()Constructor.
- WP_HTML_Token::__construct()Constructor - creates a reference to a token in some external HTML string.
Source code
private function create_fragment_at_current_node( string $html ) { if ( $this->get_token_type() !== '#tag' || $this->is_tag_closer() ) { _doing_it_wrong( __METHOD__, __( 'The context element must be a start tag.' ), '6.8.0' ); return null; } $tag_name = $this->current_element->token->node_name; $namespace = $this->current_element->token->namespace; if ( 'html' === $namespace && self::is_void( $tag_name ) ) { _doing_it_wrong( __METHOD__, sprintf( // translators: %s: A tag name like INPUT or BR. __( 'The context element cannot be a void element, found "%s".' ), $tag_name ), '6.8.0' ); return null; } /* * Prevent creating fragments at nodes that require a special tokenizer state. * This is unsupported by the HTML Processor. */ if ( 'html' === $namespace && in_array( $tag_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP', 'PLAINTEXT' ), true ) ) { _doing_it_wrong( __METHOD__, sprintf( // translators: %s: A tag name like IFRAME or TEXTAREA. __( 'The context element "%s" is not supported.' ), $tag_name ), '6.8.0' ); return null; } $fragment_processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE ); $fragment_processor->compat_mode = $this->compat_mode; // @todo Create "fake" bookmarks for non-existent but implied nodes. $fragment_processor->bookmarks['root-node'] = new WP_HTML_Span( 0, 0 ); $root_node = new WP_HTML_Token( 'root-node', 'HTML', false ); $fragment_processor->state->stack_of_open_elements->push( $root_node ); $fragment_processor->bookmarks['context-node'] = new WP_HTML_Span( 0, 0 ); $fragment_processor->context_node = clone $this->current_element->token; $fragment_processor->context_node->bookmark_name = 'context-node'; $fragment_processor->context_node->on_destroy = null; $fragment_processor->breadcrumbs = array( 'HTML', $fragment_processor->context_node->node_name ); if ( 'TEMPLATE' === $fragment_processor->context_node->node_name ) { $fragment_processor->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE; } $fragment_processor->reset_insertion_mode_appropriately(); /* * > Set the parser's form element pointer to the nearest node to the context element that * > is a form element (going straight up the ancestor chain, and including the element * > itself, if it is a form element), if any. (If there is no such form element, the * > form element pointer keeps its initial value, null.) */ foreach ( $this->state->stack_of_open_elements->walk_up() as $element ) { if ( 'FORM' === $element->node_name && 'html' === $element->namespace ) {Changelog
Introduced in 6.8.0. Unchanged from 6.8.8 through 7.1.0.
Signature, return type and hooks compared across 4 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/html-api/class-wp-html-processor.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.