Skip to content

Commit b1eb70f

Browse files
committed
HTML API: Detect CDATA by adjusted node namespace
1 parent 23a6bbf commit b1eb70f

3 files changed

Lines changed: 152 additions & 19 deletions

File tree

‎src/wp-includes/html-api/class-wp-html-processor.php‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ function ( WP_HTML_Token $token ): void {
406406
$provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real';
407407
$this->element_queue[] = new WP_HTML_Stack_Event( $token, WP_HTML_Stack_Event::PUSH, $provenance );
408408

409-
$this->change_parsing_namespace( $token->integration_node_type ? 'html' : $token->namespace );
409+
$this->change_parsing_namespace_for_node( $token );
410410
}
411411
);
412412

@@ -419,11 +419,7 @@ function ( WP_HTML_Token $token ): void {
419419

420420
$adjusted_current_node = $this->get_adjusted_current_node();
421421

422-
if ( $adjusted_current_node ) {
423-
$this->change_parsing_namespace( $adjusted_current_node->integration_node_type ? 'html' : $adjusted_current_node->namespace );
424-
} else {
425-
$this->change_parsing_namespace( 'html' );
426-
}
422+
$this->change_parsing_namespace_for_node( $adjusted_current_node );
427423
}
428424
);
429425

@@ -437,6 +433,28 @@ function ( WP_HTML_Token $token ): void {
437433
};
438434
}
439435

436+
/**
437+
* Switches tokenizer namespace state for the next token.
438+
*
439+
* HTML integration points parse start tags and character tokens according to
440+
* HTML rules, but CDATA detection follows the adjusted current node's actual
441+
* namespace.
442+
*
443+
* @since 7.1.0
444+
* @ignore
445+
*
446+
* @param WP_HTML_Token|null $node Node controlling the next token's parsing context.
447+
*/
448+
private function change_parsing_namespace_for_node( ?WP_HTML_Token $node ): void {
449+
if ( null === $node ) {
450+
$this->change_parsing_namespace( 'html' );
451+
return;
452+
}
453+
454+
$this->change_parsing_namespace( $node->integration_node_type ? 'html' : $node->namespace );
455+
$this->change_cdata_parsing_namespace( $node->namespace );
456+
}
457+
440458
/**
441459
* Creates a fragment processor at the current node.
442460
*
@@ -572,9 +590,7 @@ private function create_fragment_at_current_node( string $html ) {
572590
* This is important so that any push/pop from the stack of open
573591
* elements does not change the parsing namespace.
574592
*/
575-
$fragment_processor->change_parsing_namespace(
576-
$this->current_element->token->integration_node_type ? 'html' : $namespace
577-
);
593+
$fragment_processor->change_parsing_namespace_for_node( $this->current_element->token );
578594

579595
return $fragment_processor;
580596
}
@@ -5734,11 +5750,7 @@ public function seek( $bookmark_name ): bool {
57345750
)
57355751
);
57365752

5737-
$this->change_parsing_namespace(
5738-
$this->context_node->integration_node_type
5739-
? 'html'
5740-
: $this->context_node->namespace
5741-
);
5753+
$this->change_parsing_namespace_for_node( $this->context_node );
57425754

57435755
if ( 'TEMPLATE' === $this->context_node->node_name ) {
57445756
$this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE;

‎src/wp-includes/html-api/class-wp-html-tag-processor.php‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,28 @@ class WP_HTML_Tag_Processor {
544544
* One of 'html', 'svg', or 'math'.
545545
*
546546
* Several parsing rules change based on whether the parser
547-
* is inside foreign content, including whether CDATA sections
548-
* are allowed and whether a self-closing flag indicates that
549-
* an element has no content.
547+
* is inside foreign content, including whether a self-closing
548+
* flag indicates that an element has no content.
550549
*
551550
* @since 6.7.0
552551
*
553552
* @var string
554553
*/
555554
private $parsing_namespace = 'html';
556555

556+
/**
557+
* Indicates the current node's namespace for CDATA section detection.
558+
*
559+
* HTML integration points follow HTML tokenization for start tags and
560+
* character tokens, but CDATA sections are allowed based on the adjusted
561+
* current node's actual namespace.
562+
*
563+
* @since 7.1.0
564+
*
565+
* @var string
566+
*/
567+
private $cdata_parsing_namespace = 'html';
568+
557569
/**
558570
* What kind of syntax token became an HTML comment.
559571
*
@@ -860,7 +872,26 @@ public function change_parsing_namespace( string $new_namespace ): bool {
860872
return false;
861873
}
862874

863-
$this->parsing_namespace = $new_namespace;
875+
$this->parsing_namespace = $new_namespace;
876+
$this->cdata_parsing_namespace = $new_namespace;
877+
return true;
878+
}
879+
880+
/**
881+
* Switches the namespace context used for detecting CDATA sections.
882+
*
883+
* @since 7.1.0
884+
*
885+
* @param string $new_namespace One of 'html', 'svg', or 'math' indicating whether
886+
* the adjusted current node can contain CDATA sections.
887+
* @return bool Whether the namespace was valid and changed.
888+
*/
889+
protected function change_cdata_parsing_namespace( string $new_namespace ): bool {
890+
if ( ! in_array( $new_namespace, array( 'html', 'math', 'svg' ), true ) ) {
891+
return false;
892+
}
893+
894+
$this->cdata_parsing_namespace = $new_namespace;
864895
return true;
865896
}
866897

@@ -1914,7 +1945,7 @@ private function parse_next_tag(): bool {
19141945
}
19151946

19161947
if (
1917-
'html' !== $this->parsing_namespace &&
1948+
'html' !== $this->cdata_parsing_namespace &&
19181949
strlen( $html ) > $at + 8 &&
19191950
'[' === $html[ $at + 2 ] &&
19201951
'C' === $html[ $at + 3 ] &&

‎tests/phpunit/tests/html-api/wpHtmlProcessor.php‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,51 @@ public function test_adjusts_for_html_integration_points_in_svg() {
851851
);
852852
}
853853

854+
/**
855+
* Ensures that CDATA sections remain available inside SVG HTML integration points.
856+
*
857+
* @ticket 61576
858+
*/
859+
public function test_cdata_sections_in_svg_html_integration_points() {
860+
$processor = WP_HTML_Processor::create_fragment(
861+
'<svg><foreignObject><![CDATA[foo]]></foreignObject></svg>'
862+
);
863+
864+
$this->assertTrue(
865+
$processor->next_tag( 'foreignObject' ),
866+
'Failed to find "foreignObject" under test: check test setup.'
867+
);
868+
869+
$this->assertSame(
870+
'svg',
871+
$processor->get_namespace(),
872+
'Found the wrong namespace for the "foreignObject" element.'
873+
);
874+
875+
$this->assertTrue(
876+
$processor->next_token(),
877+
'Failed to find expected CDATA section.'
878+
);
879+
880+
$this->assertSame(
881+
'#cdata-section',
882+
$processor->get_token_name(),
883+
"Should have found a CDATA section but found {$processor->get_token_name()} instead."
884+
);
885+
886+
$this->assertSame(
887+
'svg',
888+
$processor->get_namespace(),
889+
'Found the wrong namespace for the CDATA section.'
890+
);
891+
892+
$this->assertSame(
893+
'foo',
894+
$processor->get_modifiable_text(),
895+
'Found incorrect CDATA content.'
896+
);
897+
}
898+
854899
/**
855900
* Ensures that the processor correctly adjusts the namespace
856901
* for elements inside MathML integration points.
@@ -911,6 +956,51 @@ public function test_adjusts_for_mathml_integration_points() {
911956
);
912957
}
913958

959+
/**
960+
* Ensures that CDATA sections remain available inside MathML HTML integration points.
961+
*
962+
* @ticket 61576
963+
*/
964+
public function test_cdata_sections_in_mathml_html_integration_points() {
965+
$processor = WP_HTML_Processor::create_fragment(
966+
'<math><annotation-xml encoding="text/html"><![CDATA[x]]></annotation-xml></math>'
967+
);
968+
969+
$this->assertTrue(
970+
$processor->next_tag( 'ANNOTATION-XML' ),
971+
'Failed to find "annotation-xml" under test: check test setup.'
972+
);
973+
974+
$this->assertSame(
975+
'math',
976+
$processor->get_namespace(),
977+
'Found the wrong namespace for the "annotation-xml" element.'
978+
);
979+
980+
$this->assertTrue(
981+
$processor->next_token(),
982+
'Failed to find expected CDATA section.'
983+
);
984+
985+
$this->assertSame(
986+
'#cdata-section',
987+
$processor->get_token_name(),
988+
"Should have found a CDATA section but found {$processor->get_token_name()} instead."
989+
);
990+
991+
$this->assertSame(
992+
'math',
993+
$processor->get_namespace(),
994+
'Found the wrong namespace for the CDATA section.'
995+
);
996+
997+
$this->assertSame(
998+
'x',
999+
$processor->get_modifiable_text(),
1000+
'Found incorrect CDATA content.'
1001+
);
1002+
}
1003+
9141004
/**
9151005
* Ensures that the processor stops correctly on a FORM tag closer token.
9161006
*

0 commit comments

Comments
 (0)