Skip to content

Commit ed3bb54

Browse files
sirrealdmsnell
andcommitted
Improve documentation for create_fragment
Inspired by WordPress#7141 Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
1 parent 479c0b3 commit ed3bb54

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,37 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
279279
* form is provided because a context element may have attributes that
280280
* impact the parse, such as with a SCRIPT tag and its `type` attribute.
281281
*
282+
* Example:
283+
*
284+
* // Usually, snippets of HTML ought to be processed in the default `<body>` context:
285+
* $processor = WP_HTML_Processor::create_fragment( '<p>Hi</p>' );
286+
*
287+
* // Some fragments should be processed in the correct context like this SVG:
288+
* $processor = WP_HTML_Processor::create_fragment( '<rect width="10" height="10" />', '<svg>' );
289+
*
290+
* // This fragment with TD tags should be processed in a TR context:
291+
* $processor = WP_HTML_Processor::create_fragment(
292+
* '<td>1<td>2<td>3',
293+
* '<table><tbody><tr>'
294+
* );
295+
*
296+
* In order to create a fragment processor at the correct location, the
297+
* provided fragment will be processed as part of a full HTML document.
298+
* The processor will search for the last opener tag in the document and
299+
* create a fragment processor at that location. The document will be
300+
* forced into "no-quirks" mode by including the HTML5 doctype.
301+
*
302+
* For advanced usage and precise control over the context element, use
303+
* `WP_HTML_Processor::create_full_processor()` and
304+
* `WP_HTML_Processor::create_fragment_at_current_node()`.
305+
*
282306
* UTF-8 is the only allowed encoding. If working with a document that
283307
* isn't UTF-8, first convert the document to UTF-8, then pass in the
284308
* converted HTML.
285309
*
286310
* @since 6.4.0
287311
* @since 6.6.0 Returns `static` instead of `self` so it can create subclass instances.
312+
* @since 6.8.0 Can create fragments with any context element.
288313
*
289314
* @param string $html Input HTML fragment to process.
290315
* @param string $context Context element for the fragment. Defaults to `<body>`.

0 commit comments

Comments
 (0)