@@ -1187,25 +1187,70 @@ public function class_list() {
11871187 return ;
11881188 }
11891189
1190- $ seen = array ();
1190+ return self ::parse_class_list ( $ class , $ this ->compat_mode );
1191+ }
11911192
1192- $ is_quirks = self ::QUIRKS_MODE === $ this ->compat_mode ;
1193+ /**
1194+ * Generator for a foreach loop to step through each class name for the matched tag.
1195+ *
1196+ * This generator function is designed to be used inside a "foreach" loop.
1197+ *
1198+ * Example:
1199+ *
1200+ * $class_list = 'free <egg<\tlang-en';
1201+ * foreach ( WP_HTML_Tag_Processor::parse_class_list( $class_list ) as $class_name ) {
1202+ * echo "{$class_name} ";
1203+ * }
1204+ * // Outputs: "free <egg> lang-en "
1205+ *
1206+ * The default behavior is normative for HTML5 documents in “no-quirks” mode. For
1207+ * rare documents with “quirks mode” DOCTYPE declarations, pass {@see self::QUIRKS_MODE}
1208+ * as the compatibility mode for ASCII-case-insensitive comparison of class names. Use
1209+ * this only when certain that the containing document is in no-quirks mode.
1210+ *
1211+ * Example:
1212+ *
1213+ * $class_list = 'wide naRRow WIDE Wide narrow';
1214+ * $classes = WP_HTML_Tag_Processor::parse_class_list( $class_list );
1215+ * $classes = iterator_to_array( $classes );
1216+ * $classes === array( 'wide', 'naRRow', 'WIDE', 'Wide', 'narrow' );
1217+ *
1218+ * $class_list = 'wide WIDE Wide';
1219+ * $classes = WP_HTML_Tag_Processor::parse_class_list( $class_list, WP_HTML_Tag_Processor::QUIRKS_MODE );
1220+ * $classes = iterator_to_array( $classes );
1221+ * $classes === array( 'wide', 'naRRow' );
1222+ *
1223+ * @since 6.9.0
1224+ *
1225+ * @param string $class_list Contains a full decoded HTML `class` attribute, or plain
1226+ * list of space-separated CSS class names.
1227+ * @param string|null $compat_mode Optional. Specifies how to compare class names, whether
1228+ * byte-for-byte or ASCII-case-insensitively. Default is
1229+ * NO_QUIRKS_MODE, which compares byte for byte.
1230+ * @return Generator Iterates over each unique CSS class name in the given input list in order.
1231+ */
1232+ public static function parse_class_list ( $ class_list , $ compat_mode = self ::NO_QUIRKS_MODE ) {
1233+ if ( '' === $ class_list || ! is_string ( $ class_list ) ) {
1234+ return ;
1235+ }
11931236
1194- $ at = 0 ;
1195- while ( $ at < strlen ( $ class ) ) {
1237+ $ seen = array ();
1238+ $ is_quirks = self ::QUIRKS_MODE === $ compat_mode ;
1239+ $ at = 0 ;
1240+ while ( $ at < strlen ( $ class_list ) ) {
11961241 // Skip past any initial boundary characters.
1197- $ at += strspn ( $ class , " \t\f\r\n" , $ at );
1198- if ( $ at >= strlen ( $ class ) ) {
1242+ $ at += strspn ( $ class_list , " \t\f\r\n" , $ at );
1243+ if ( $ at >= strlen ( $ class_list ) ) {
11991244 return ;
12001245 }
12011246
12021247 // Find the byte length until the next boundary.
1203- $ length = strcspn ( $ class , " \t\f\r\n" , $ at );
1248+ $ length = strcspn ( $ class_list , " \t\f\r\n" , $ at );
12041249 if ( 0 === $ length ) {
12051250 return ;
12061251 }
12071252
1208- $ name = str_replace ( "\x00" , "\u{FFFD}" , substr ( $ class , $ at , $ length ) );
1253+ $ name = str_replace ( "\x00" , "\u{FFFD}" , substr ( $ class_list , $ at , $ length ) );
12091254 if ( $ is_quirks ) {
12101255 $ name = strtolower ( $ name );
12111256 }
0 commit comments