@@ -153,11 +153,77 @@ private static function should_skip_test( ?string $test_context_element, string
153153 * @return string|null Tree structure of parsed HTML, if supported, else null.
154154 */
155155 private static function build_tree_representation ( ?string $ fragment_context , string $ html ) {
156- $ processor = $ fragment_context
157- ? WP_HTML_Processor::create_fragment ( $ html , "< {$ fragment_context }> " )
158- : WP_HTML_Processor::create_full_parser ( $ html );
159- if ( null === $ processor ) {
160- throw new WP_HTML_Unsupported_Exception ( "Could not create a parser with the given fragment context: {$ fragment_context }. " , '' , 0 , '' , array (), array () );
156+ $ processor = null ;
157+ if ( $ fragment_context ) {
158+ if ( 'body ' === $ fragment_context ) {
159+ $ processor = WP_HTML_Processor::create_fragment ( $ html );
160+ } else {
161+
162+ /*
163+ * If the string of characters starts with "svg ", the context
164+ * element is in the SVG namespace and the substring after
165+ * "svg " is the local name. If the string of characters starts
166+ * with "math ", the context element is in the MathML namespace
167+ * and the substring after "math " is the local name.
168+ * Otherwise, the context element is in the HTML namespace and
169+ * the string is the local name.
170+ */
171+ if ( str_starts_with ( $ fragment_context , 'svg ' ) ) {
172+ $ tag_name = substr ( $ fragment_context , 4 );
173+ if ( 'svg ' === $ tag_name ) {
174+ $ parent_processor = WP_HTML_Processor::create_full_parser ( '<!DOCTYPE html><svg> ' );
175+ } else {
176+ $ parent_processor = WP_HTML_Processor::create_full_parser ( "<!DOCTYPE html><svg>< {$ tag_name }> " );
177+ }
178+ $ parent_processor ->next_tag ( $ tag_name );
179+ } elseif ( str_starts_with ( $ fragment_context , 'math ' ) ) {
180+ $ tag_name = substr ( $ fragment_context , 5 );
181+ if ( 'math ' === $ tag_name ) {
182+ $ parent_processor = WP_HTML_Processor::create_full_parser ( '<!DOCTYPE html><math> ' );
183+ } else {
184+ $ parent_processor = WP_HTML_Processor::create_full_parser ( "<!DOCTYPE html><math>< {$ tag_name }> " );
185+ }
186+ $ parent_processor ->next_tag ( $ tag_name );
187+ } else {
188+ if ( in_array (
189+ $ fragment_context ,
190+ array (
191+ 'caption ' ,
192+ 'col ' ,
193+ 'colgroup ' ,
194+ 'tbody ' ,
195+ 'td ' ,
196+ 'tfoot ' ,
197+ 'th ' ,
198+ 'thead ' ,
199+ 'tr ' ,
200+ ),
201+ true
202+ ) ) {
203+ $ parent_processor = WP_HTML_Processor::create_full_parser ( "<!DOCTYPE html><table>< {$ fragment_context }> " );
204+ $ parent_processor ->next_tag ();
205+ } else {
206+ $ parent_processor = WP_HTML_Processor::create_full_parser ( "<!DOCTYPE html>< {$ fragment_context }> " );
207+ }
208+ $ parent_processor ->next_tag ( $ fragment_context );
209+ }
210+ if ( null !== $ parent_processor ->get_unsupported_exception () ) {
211+ throw $ parent_processor ->get_unsupported_exception ();
212+ }
213+ if ( null !== $ parent_processor ->get_last_error () ) {
214+ throw new Exception ( $ parent_processor ->get_last_error () );
215+ }
216+ $ processor = $ parent_processor ->spawn_fragment_parser ( $ html );
217+ }
218+
219+ if ( null === $ processor ) {
220+ throw new WP_HTML_Unsupported_Exception ( "Could not create a parser with the given fragment context: {$ fragment_context }. " , '' , 0 , '' , array (), array () );
221+ }
222+ } else {
223+ $ processor = WP_HTML_Processor::create_full_parser ( $ html );
224+ if ( null === $ processor ) {
225+ throw new Exception ( 'Could not create a full parser. ' );
226+ }
161227 }
162228
163229 /*
0 commit comments