<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59757] branches/6.7: HTML API: Fix extensibility of WP_HTML_Processor::next_token().</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59757">59757</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59757","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jorbin</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-02-04 02:47:41 +0000 (Tue, 04 Feb 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>HTML API: Fix extensibility of WP_HTML_Processor::next_token().
Break out logic from the next_token() method into a private method which may call itself recursively. This allows for subclasses to override the next_token() method and be assured that each call to next_token() corresponds with the consumption of one single token. This also parallels how WP_HTML_Tag_Processor::next_token() wraps a private base_class_next_token() method.
Reviewed by jonsurrell.
Merges <a href="https://core.trac.wordpress.org/changeset/59285">[59285]</a>, <a href="https://core.trac.wordpress.org/changeset/59364">[59364]</a>, and <a href="https://core.trac.wordpress.org/changeset/59747">[59747]</a> to 6.7 branch.
Props westonruter, jonsurrell, dmsnell, jorbin.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches67srcwpincludeshtmlapiclasswphtmlprocessorphp">branches/6.7/src/wp-includes/html-api/class-wp-html-processor.php</a></li>
<li><a href="#branches67testsphpunittestshtmlapiwpHtmlProcessorphp">branches/6.7/tests/phpunit/tests/html-api/wpHtmlProcessor.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>branches/6.7/tests/phpunit/data/html-api/</li>
<li><a href="#branches67testsphpunitdatahtmlapitokencountinghtmlprocessorphp">branches/6.7/tests/phpunit/data/html-api/token-counting-html-processor.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches67srcwpincludeshtmlapiclasswphtmlprocessorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.7/src/wp-includes/html-api/class-wp-html-processor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.7/src/wp-includes/html-api/class-wp-html-processor.php 2025-02-04 00:30:06 UTC (rev 59756)
+++ branches/6.7/src/wp-includes/html-api/class-wp-html-processor.php 2025-02-04 02:47:41 UTC (rev 59757)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -608,6 +608,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
+ *
+ * @since 6.5.0 Added for internal support; do not use.
+ * @since 6.7.2 Refactored so subclasses may extend.
+ *
+ * @return bool Whether a token was parsed.
+ */
+ public function next_token(): bool {
+ return $this->next_visitable_token();
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Ensures internal accounting is maintained for HTML semantic rules while
</span><span class="cx" style="display: block; padding: 0 10px"> * the underlying Tag Processor class is seeking to a bookmark.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -615,13 +631,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * semantic rules for text nodes. For access to the raw tokens consider using
</span><span class="cx" style="display: block; padding: 0 10px"> * WP_HTML_Tag_Processor instead.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 6.5.0 Added for internal support; do not use.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.7.2 Added for internal support.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function next_token(): bool {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function next_visitable_token(): bool {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->current_element = null;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $this->last_error ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -639,7 +660,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * tokens works in the meantime and isn't obviously wrong.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $this->element_queue ) && $this->step() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->next_token();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $this->next_visitable_token();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Process the next event on the queue.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -650,7 +671,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return empty( $this->element_queue ) ? false : $this->next_token();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return empty( $this->element_queue ) ? false : $this->next_visitable_token();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -661,7 +682,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * the breadcrumbs.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'root-node' === $this->current_element->token->bookmark_name ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->next_token();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $this->next_visitable_token();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Adjust the breadcrumbs for this event.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -673,7 +694,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Avoid sending close events for elements which don't expect a closing.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->next_token();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $this->next_visitable_token();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span></span></pre></div>
<a id="branches67testsphpunitdatahtmlapitokencountinghtmlprocessorphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/6.7/tests/phpunit/data/html-api/token-counting-html-processor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.7/tests/phpunit/data/html-api/token-counting-html-processor.php (rev 0)
+++ branches/6.7/tests/phpunit/data/html-api/token-counting-html-processor.php 2025-02-04 02:47:41 UTC (rev 59757)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,35 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class Token_Counting_HTML_Processor extends WP_HTML_Processor {
+
+ /**
+ * List of tokens that have already been seen.
+ *
+ * @var array<string, int>
+ */
+ public $token_seen_count = array();
+
+ /**
+ * Gets next token.
+ *
+ * @return bool Whether next token was matched.
+ */
+ public function next_token(): bool {
+ $result = parent::next_token();
+
+ if ( $this->get_token_type() === '#tag' ) {
+ $token_name = ( $this->is_tag_closer() ? '-' : '+' ) . $this->get_tag();
+ } else {
+ $token_name = $this->get_token_name();
+ }
+
+ if ( ! isset( $this->token_seen_count[ $token_name ] ) ) {
+ $this->token_seen_count[ $token_name ] = 1;
+ } else {
+ ++$this->token_seen_count[ $token_name ];
+ }
+
+ return $result;
+ }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: branches/6.7/tests/phpunit/data/html-api/token-counting-html-processor.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches67testsphpunittestshtmlapiwpHtmlProcessorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.7/tests/phpunit/tests/html-api/wpHtmlProcessor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.7/tests/phpunit/tests/html-api/wpHtmlProcessor.php 2025-02-04 00:30:06 UTC (rev 59756)
+++ branches/6.7/tests/phpunit/tests/html-api/wpHtmlProcessor.php 2025-02-04 02:47:41 UTC (rev 59757)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -909,6 +909,141 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_html_processor_with_extended_next_token() {
+ return array(
+ 'single_instance_per_tag' => array(
+ 'html' => '
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <title>Hello World</title>
+ </head>
+ <body>
+ <h1>Hello World!</h1>
+ <img src="example.png">
+ <p>Each tag should occur only once in this document.<!--Closing P tag omitted intentionally.-->
+ <footer>The end.</footer>
+ </body>
+ </html>
+ ',
+ 'expected_token_counts' => array(
+ '+HTML' => 1,
+ '+HEAD' => 1,
+ '#text' => 14,
+ '+META' => 1,
+ '+TITLE' => 1,
+ '-HEAD' => 1,
+ '+BODY' => 1,
+ '+H1' => 1,
+ '-H1' => 1,
+ '+IMG' => 1,
+ '+P' => 1,
+ '#comment' => 1,
+ '-P' => 1,
+ '+FOOTER' => 1,
+ '-FOOTER' => 1,
+ '-BODY' => 1,
+ '-HTML' => 1,
+ '' => 1,
+ ),
+ ),
+
+ 'multiple_tag_instances' => array(
+ 'html' => '
+ <html>
+ <body>
+ <h1>Hello World!</h1>
+ <p>First
+ <p>Second
+ <p>Third
+ <ul>
+ <li>1
+ <li>2
+ <li>3
+ </ul>
+ </body>
+ </html>
+ ',
+ 'expected_token_counts' => array(
+ '+HTML' => 1,
+ '+HEAD' => 1,
+ '-HEAD' => 1,
+ '+BODY' => 1,
+ '#text' => 13,
+ '+H1' => 1,
+ '-H1' => 1,
+ '+P' => 3,
+ '-P' => 3,
+ '+UL' => 1,
+ '+LI' => 3,
+ '-LI' => 3,
+ '-UL' => 1,
+ '-BODY' => 1,
+ '-HTML' => 1,
+ '' => 1,
+ ),
+ ),
+
+ 'extreme_nested_formatting' => array(
+ 'html' => '
+ <html>
+ <body>
+ <p>
+ <strong><em><strike><i><b><u>FORMAT</u></b></i></strike></em></strong>
+ </p>
+ </body>
+ </html>
+ ',
+ 'expected_token_counts' => array(
+ '+HTML' => 1,
+ '+HEAD' => 1,
+ '-HEAD' => 1,
+ '+BODY' => 1,
+ '#text' => 7,
+ '+P' => 1,
+ '+STRONG' => 1,
+ '+EM' => 1,
+ '+STRIKE' => 1,
+ '+I' => 1,
+ '+B' => 1,
+ '+U' => 1,
+ '-U' => 1,
+ '-B' => 1,
+ '-I' => 1,
+ '-STRIKE' => 1,
+ '-EM' => 1,
+ '-STRONG' => 1,
+ '-P' => 1,
+ '-BODY' => 1,
+ '-HTML' => 1,
+ '' => 1,
+ ),
+ ),
+ );
+ }
+
+ /**
+ * Ensures that subclasses to WP_HTML_Processor can do bookkeeping by extending the next_token() method.
+ *
+ * @ticket 62269
+ * @dataProvider data_html_processor_with_extended_next_token
+ */
+ public function test_ensure_next_token_method_extensibility( $html, $expected_token_counts ) {
+ require_once DIR_TESTDATA . '/html-api/token-counting-html-processor.php';
+
+ $processor = Token_Counting_HTML_Processor::create_full_parser( $html );
+ while ( $processor->next_tag() ) {
+ continue;
+ }
+
+ $this->assertEquals( $expected_token_counts, $processor->token_seen_count, 'Snapshot: ' . var_export( $processor->token_seen_count, true ) );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Ensure that lowercased tag_name query matches tags case-insensitively.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @group 62427
</span></span></pre>
</div>
</div>
</body>
</html>