@@ -1068,7 +1068,7 @@ public function test_ensure_next_token_method_extensibility( $html, $expected_to
10681068 /**
10691069 * Ensure that lowercased tag_name query matches tags case-insensitively.
10701070 *
1071- * @group 62427
1071+ * @ticket 62427
10721072 */
10731073 public function test_next_tag_lowercase_tag_name () {
10741074 // The upper case <DIV> is irrelevant but illustrates the case-insentivity.
@@ -1079,4 +1079,106 @@ public function test_next_tag_lowercase_tag_name() {
10791079 $ processor = WP_HTML_Processor::create_fragment ( '<svg><RECT> ' );
10801080 $ this ->assertTrue ( $ processor ->next_tag ( array ( 'tag_name ' => 'rect ' ) ) );
10811081 }
1082+
1083+ /**
1084+ * Ensure that the processor does not throw errors in cases of extreme HTML nesting.
1085+ *
1086+ * @ticket 64394
1087+ *
1088+ * @expectedIncorrectUsage WP_HTML_Tag_Processor::set_bookmark
1089+ */
1090+ public function test_deep_nesting_fails_process_without_error () {
1091+ $ html = str_repeat ( '<i> ' , WP_HTML_Processor::MAX_BOOKMARKS * 2 );
1092+ $ processor = WP_HTML_Processor::create_fragment ( $ html );
1093+
1094+ while ( $ processor ->next_token () ) {
1095+ // Process tokens.
1096+ }
1097+
1098+ $ this ->assertSame (
1099+ WP_HTML_Processor::ERROR_EXCEEDED_MAX_BOOKMARKS ,
1100+ $ processor ->get_last_error (),
1101+ 'Failed to report exceeded-max-bookmarks error. '
1102+ );
1103+ }
1104+
1105+ /**
1106+ * @ticket 64394
1107+ *
1108+ * @expectedIncorrectUsage WP_HTML_Tag_Processor::set_bookmark
1109+ */
1110+ public function test_deep_nesting_fails_processing_virtual_tokens_without_error () {
1111+ /*
1112+ * This test has some variability depending on how the virtual tokens align.
1113+ * In order to ensure that bookmarks are exhausted on a virtual token
1114+ * without throwing an error, 3 documents are parsed with different "offsets"
1115+ * to ensure that the bookmarks are exhaused on a virtual token in at least one of the runs.
1116+ *
1117+ * "<table><td><table><td>…" produces:
1118+ * └─TABLE (real)
1119+ * └─TBODY (virtual)
1120+ * └─TR (virtual)
1121+ * └─TD (real)
1122+ * └─TABLE (real)
1123+ * └─TBODY (virtual)
1124+ * └─TR (virtual)
1125+ * └─TD (real)
1126+ * └─…
1127+ */
1128+ $ html_table_td = str_repeat ( '<table><td> ' , WP_HTML_Processor::MAX_BOOKMARKS * 2 );
1129+
1130+ // Offset 0
1131+ $ processor = WP_HTML_Processor::create_fragment ( $ html_table_td );
1132+ while ( $ processor ->next_token () ) {
1133+ // Process tokens.
1134+ }
1135+ $ this ->assertSame (
1136+ WP_HTML_Processor::ERROR_EXCEEDED_MAX_BOOKMARKS ,
1137+ $ processor ->get_last_error (),
1138+ 'Failed to report exceeded-max-bookmarks error. '
1139+ );
1140+
1141+ // Offset 1
1142+ $ processor = WP_HTML_Processor::create_fragment ( "<div> {$ html_table_td }" );
1143+ while ( $ processor ->next_token () ) {
1144+ // Process tokens.
1145+ }
1146+ $ this ->assertSame (
1147+ WP_HTML_Processor::ERROR_EXCEEDED_MAX_BOOKMARKS ,
1148+ $ processor ->get_last_error (),
1149+ 'Failed to report exceeded-max-bookmarks error. '
1150+ );
1151+
1152+ // Offset 2
1153+ $ processor = WP_HTML_Processor::create_fragment ( "<div><div> {$ html_table_td }" );
1154+ while ( $ processor ->next_token () ) {
1155+ // Process tokens.
1156+ }
1157+ $ this ->assertSame (
1158+ WP_HTML_Processor::ERROR_EXCEEDED_MAX_BOOKMARKS ,
1159+ $ processor ->get_last_error (),
1160+ 'Failed to report exceeded-max-bookmarks error. '
1161+ );
1162+ }
1163+
1164+ /**
1165+ * @ticket 64394
1166+ *
1167+ * @expectedIncorrectUsage WP_HTML_Tag_Processor::set_bookmark
1168+ */
1169+ public function test_prevents_unbounded_bookmarking () {
1170+ $ processor = WP_HTML_Processor::create_full_parser ( '<!DOCTYPE html><html> ' );
1171+ $ processor ->next_tag ();
1172+
1173+ // This might fail before the MAX_BOOKMARK limit, which is okay.
1174+ foreach ( range ( 0 , WP_HTML_Processor::MAX_BOOKMARKS ) as $ n ) {
1175+ if ( ! $ processor ->set_bookmark ( "{$ n }" ) ) {
1176+ break ;
1177+ }
1178+ }
1179+
1180+ $ this ->assertFalse (
1181+ $ processor ->set_bookmark ( 'beyond the limit ' )
1182+ );
1183+ }
10821184}
0 commit comments