<!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>[60617] trunk: HTML API: Reduce length checks in skip_script_data.</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/60617">60617</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/60617","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jonsurrell</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-08-07 08:31:07 +0000 (Thu, 07 Aug 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: Reduce length checks in skip_script_data.
Apply an optimization to remove several repeated string length checks in `WP_HTML_Tag_Processor::skip_script_data()`.
Developed in https://github.com/WordPress/wordpress-develop/pull/9230.
Props jonsurrell, dmsnell.
See <a href="https://core.trac.wordpress.org/ticket/63738">#63738</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeshtmlapiclasswphtmltagprocessorphp">trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php</a></li>
<li><a href="#trunktestsphpunittestshtmlapiwpHtmlTagProcessorphp">trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeshtmlapiclasswphtmltagprocessorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php 2025-08-07 07:58:33 UTC (rev 60616)
+++ trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php 2025-08-07 08:31:07 UTC (rev 60617)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1497,12 +1497,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $at += strcspn( $html, '-<', $at );
</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">+ * Optimization: Terminating a complete script element requires at least eight
+ * additional bytes in the document. Some checks below may cause local escaped
+ * state transitions when processing shorter strings, but those transitions are
+ * irrelevant if the script tag is incomplete and the function must return false.
+ *
+ * This may need updating if those transitions become significant or exported from
+ * this function in some way, such as when building safe methods to embed JavaScript
+ * or data inside a SCRIPT element.
+ *
+ * $at may be here.
+ * ↓
+ * ...</script>
+ * ╰──┬───╯
+ * $at + 8 additional bytes are required for a non-false return value.
+ *
+ * This single check eliminates the need to check lengths for the shorter spans:
+ *
+ * $at may be here.
+ * ↓
+ * <script><!-- --></script>
+ * ├╯
+ * $at + 2 additional characters does not require a length check.
+ *
+ * The transition from "escaped" to "unescaped" is not relevant if the document ends:
+ *
+ * $at may be here.
+ * ↓
+ * <script><!-- -->[[END-OF-DOCUMENT]]
+ * ╰──┬───╯
+ * $at + 8 additional bytes is not satisfied, return false.
+ */
+ if ( $at + 8 >= $doc_length ) {
+ return false;
+ }
+
+ /*
</ins><span class="cx" style="display: block; padding: 0 10px"> * For all script states a "-->" transitions
</span><span class="cx" style="display: block; padding: 0 10px"> * back into the normal unescaped script mode,
</span><span class="cx" style="display: block; padding: 0 10px"> * even if that's the current state.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $at + 2 < $doc_length &&
</del><span class="cx" style="display: block; padding: 0 10px"> '-' === $html[ $at ] &&
</span><span class="cx" style="display: block; padding: 0 10px"> '-' === $html[ $at + 1 ] &&
</span><span class="cx" style="display: block; padding: 0 10px"> '>' === $html[ $at + 2 ]
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1512,10 +1547,6 @@
</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">- if ( $at + 1 >= $doc_length ) {
- return false;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Everything of interest past here starts with "<".
</span><span class="cx" style="display: block; padding: 0 10px"> * Check this character and advance position regardless.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1537,7 +1568,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * parsing after updating the state.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $at + 2 < $doc_length &&
</del><span class="cx" style="display: block; padding: 0 10px"> '!' === $html[ $at ] &&
</span><span class="cx" style="display: block; padding: 0 10px"> '-' === $html[ $at + 1 ] &&
</span><span class="cx" style="display: block; padding: 0 10px"> '-' === $html[ $at + 2 ]
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1561,7 +1591,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * proceed scanning to the next potential token in the text.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $at + 6 < $doc_length &&
</del><span class="cx" style="display: block; padding: 0 10px"> ( 's' === $html[ $at ] || 'S' === $html[ $at ] ) &&
</span><span class="cx" style="display: block; padding: 0 10px"> ( 'c' === $html[ $at + 1 ] || 'C' === $html[ $at + 1 ] ) &&
</span><span class="cx" style="display: block; padding: 0 10px"> ( 'r' === $html[ $at + 2 ] || 'R' === $html[ $at + 2 ] ) &&
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1579,9 +1608,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * "<script123" should not end a script region even though
</span><span class="cx" style="display: block; padding: 0 10px"> * "<script" is found within the text.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $at + 6 >= $doc_length ) {
- continue;
- }
</del><span class="cx" style="display: block; padding: 0 10px"> $at += 6;
</span><span class="cx" style="display: block; padding: 0 10px"> $c = $html[ $at ];
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ' ' !== $c && "\t" !== $c && "\r" !== $c && "\n" !== $c && '/' !== $c && '>' !== $c ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1611,8 +1637,6 @@
</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"> if ( $this->bytes_already_parsed >= $doc_length ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->parser_state = self::STATE_INCOMPLETE_INPUT;
-
</del><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestshtmlapiwpHtmlTagProcessorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php 2025-08-07 07:58:33 UTC (rev 60616)
+++ trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php 2025-08-07 08:31:07 UTC (rev 60617)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1982,6 +1982,49 @@
</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">+ * Test that script tags are parsed correctly.
+ *
+ * Script tag parsing is very complicated, see the following resources for more details:
+ *
+ * - https://html.spec.whatwg.org/multipage/parsing.html#script-data-state
+ * - https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
+ *
+ * @ticket 63738
+ *
+ * @dataProvider data_script_tag
+ */
+ public function test_script_tag_parsing( string $input, bool $closes ) {
+ $processor = new WP_HTML_Tag_Processor( $input );
+
+ if ( $closes ) {
+ $this->assertTrue( $processor->next_token(), 'Expected to find complete script tag.' );
+ $this->assertSame( 'SCRIPT', $processor->get_tag() );
+ return;
+ }
+
+ $this->assertFalse( $processor->next_token(), 'Expected to fail next_token().' );
+ $this->assertTrue( $processor->paused_at_incomplete_token(), 'Expected an incomplete SCRIPT tag token.' );
+ }
+
+ /**
+ * Data provider.
+ */
+ public static function data_script_tag(): array {
+ return array(
+ 'Basic script tag' => array( '<script></script>', true ),
+ 'Script with type attribute' => array( '<script type="text/javascript"></script>', true ),
+ 'Script data escaped' => array( '<script><!--</script>', true ),
+ 'Script data double-escaped exit (comment)' => array( '<script><!--<script>--></script>', true ),
+ 'Script data double-escaped exit (closed)' => array( '<script><!--<script></script></script>', true ),
+ 'Script data double-escaped exit (closed/truncated)' => array( '<script><!--<script></script </script>', true ),
+ 'Script data no double-escape' => array( '<script><!-- --><script></script>', true ),
+
+ 'Script tag with self-close flag (ignored)' => array( '<script />', false ),
+ 'Script data double-escaped' => array( '<script><!--<script></script>', false ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Invalid tag names are comments on tag closers.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 58007
</span></span></pre>
</div>
</div>
</body>
</html>