From fb5f03637f9fde2b6f3fce7cd218810ea80cf9c1 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 2 Jan 2025 14:02:06 +0100 Subject: [PATCH 1/2] Simplify the class_list loop --- .../html-api/class-wp-html-tag-processor.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php index 39390621e86a6..34825562dec37 100644 --- a/src/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php @@ -1183,25 +1183,15 @@ public function class_list() { $is_quirks = self::QUIRKS_MODE === $this->compat_mode; - $at = 0; + $at = strspn( $class, " \t\f\r\n" ); while ( $at < strlen( $class ) ) { - // Skip past any initial boundary characters. - $at += strspn( $class, " \t\f\r\n", $at ); - if ( $at >= strlen( $class ) ) { - return; - } - // Find the byte length until the next boundary. $length = strcspn( $class, " \t\f\r\n", $at ); - if ( 0 === $length ) { - return; - } - - $name = str_replace( "\x00", "\u{FFFD}", substr( $class, $at, $length ) ); + $name = str_replace( "\x00", "\u{FFFD}", substr( $class, $at, $length ) ); if ( $is_quirks ) { $name = strtolower( $name ); } - $at += $length; + $at += $length + strspn( $class, " \t\f\r\n", $at + $length ); /* * It's expected that the number of class names for a given tag is relatively small. From 0076a5c3d7da0aa384e3a3e86bfe43636db0015e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 11 Aug 2026 11:23:05 +0400 Subject: [PATCH 2/2] HTML API: Preserve lazy class list iteration Defer whitespace scanning until class_list() resumes. This avoids scanning a long whitespace suffix when callers stop after a match. --- .../html-api/class-wp-html-tag-processor.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php index 156854319b519..52b58b63995bd 100644 --- a/src/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php @@ -1215,19 +1215,19 @@ public function class_list() { if ( $is_quirks ) { $name = strtolower( $name ); } - $at += $length + strspn( $class, " \t\f\r\n", $at + $length ); + $at += $length; /* * It's expected that the number of class names for a given tag is relatively small. * Given this, it is probably faster overall to scan an array for a value rather * than to use the class name as a key and check if it's a key of $seen. */ - if ( in_array( $name, $seen, true ) ) { - continue; + if ( ! in_array( $name, $seen, true ) ) { + $seen[] = $name; + yield $name; } - $seen[] = $name; - yield $name; + $at += strspn( $class, " \t\f\r\n", $at ); } }