Skip to content

Commit d2e4814

Browse files
sirrealdmsnell
andcommitted
Add tests for unsupported contexts
Inspired by WordPress#7141 Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
1 parent ed3bb54 commit d2e4814

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Unit tests covering WP_HTML_Processor fragment parsing functionality.
4+
*
5+
* @package WordPress
6+
* @subpackage HTML-API
7+
*
8+
* @since 6.8.0
9+
*
10+
* @group html-api
11+
*
12+
* @coversDefaultClass WP_HTML_Processor
13+
*/
14+
class Tests_HtmlApi_WpHtmlProcessorFragmentParsing extends WP_UnitTestCase {
15+
/**
16+
* Verifies that the fragment parser doesn't allow invalid context nodes.
17+
*
18+
* This includes void elements and self-contained elements because they can
19+
* contain no inner HTML. Operations on self-contained elements should occur
20+
* through methods such as {@see WP_HTML_Tag_Processor::set_modifiable_text}.
21+
*
22+
* @ticket 62357
23+
*
24+
* @dataProvider data_invalid_fragment_contexts
25+
*
26+
* @param string $context Invalid context node for fragment parser.
27+
*/
28+
public function test_rejects_invalid_fragment_contexts( string $context, string $doing_it_wrong_method_name ) {
29+
$this->setExpectedIncorrectUsage( "WP_HTML_Processor::{$doing_it_wrong_method_name}" );
30+
$this->assertNull(
31+
WP_HTML_Processor::create_fragment( 'just a test', $context ),
32+
"Should not have been able to create a fragment parser with context node {$context}"
33+
);
34+
}
35+
36+
/**
37+
* Data provider.
38+
*
39+
* @ticket 62357
40+
*
41+
* @return array[]
42+
*/
43+
public static function data_invalid_fragment_contexts() {
44+
return array(
45+
/*
46+
* Invalid contexts.
47+
*/
48+
/*
49+
* The text node is confused with a virtual body open tag.
50+
* This should fail to set a bookmark in `create_fragment`
51+
* but currently does not, it slips through and fails in
52+
* `create_fragment_at_current_node`.
53+
*/
54+
'Invalid text' => array( 'just some text', 'create_fragment_at_current_node' ),
55+
'Invalid comment' => array( '<!-- comment -->', 'create_fragment' ),
56+
'Invalid closing' => array( '</div>', 'create_fragment' ),
57+
'Invalid DOCTYPE' => array( '<!DOCTYPE html>', 'create_fragment' ),
58+
/*
59+
* PLAINTEXT should appear in the unsupported elements, but at the
60+
* moment it's completely unsupported by the processor so
61+
* the context element cannot be found.
62+
*/
63+
'Unsupported PLAINTEXT' => array( '<plaintext>', 'create_fragment' ),
64+
65+
/*
66+
* Invalid contexts.
67+
*/
68+
'AREA' => array( '<area>', 'create_fragment_at_current_node' ),
69+
'BASE' => array( '<base>', 'create_fragment_at_current_node' ),
70+
'BASEFONT' => array( '<basefont>', 'create_fragment_at_current_node' ),
71+
'BGSOUND' => array( '<bgsound>', 'create_fragment_at_current_node' ),
72+
'BR' => array( '<br>', 'create_fragment_at_current_node' ),
73+
'COL' => array( '<table><colgroup><col>', 'create_fragment_at_current_node' ),
74+
'EMBED' => array( '<embed>', 'create_fragment_at_current_node' ),
75+
'FRAME' => array( '<frameset><frame>', 'create_fragment_at_current_node' ),
76+
'HR' => array( '<hr>', 'create_fragment_at_current_node' ),
77+
'IMG' => array( '<img>', 'create_fragment_at_current_node' ),
78+
'INPUT' => array( '<input>', 'create_fragment_at_current_node' ),
79+
'KEYGEN' => array( '<keygen>', 'create_fragment_at_current_node' ),
80+
'LINK' => array( '<link>', 'create_fragment_at_current_node' ),
81+
'META' => array( '<meta>', 'create_fragment_at_current_node' ),
82+
'PARAM' => array( '<param>', 'create_fragment_at_current_node' ),
83+
'SOURCE' => array( '<source>', 'create_fragment_at_current_node' ),
84+
'TRACK' => array( '<track>', 'create_fragment_at_current_node' ),
85+
'WBR' => array( '<wbr>', 'create_fragment_at_current_node' ),
86+
87+
/*
88+
* Unsupported elements. Include a tag closer to ensure the element can be found
89+
* and does not pause the parser at an incomplete token.
90+
*/
91+
'IFRAME' => array( '<iframe></iframe>', 'create_fragment_at_current_node' ),
92+
'NOEMBED' => array( '<noembed></noembed>', 'create_fragment_at_current_node' ),
93+
'NOFRAMES' => array( '<noframes></noframes>', 'create_fragment_at_current_node' ),
94+
'SCRIPT' => array( '<script></script>', 'create_fragment_at_current_node' ),
95+
'SCRIPT with type' => array( '<script type="javascript"></script>', 'create_fragment_at_current_node' ),
96+
'STYLE' => array( '<style></style>', 'create_fragment_at_current_node' ),
97+
'TEXTAREA' => array( '<textarea></textarea>', 'create_fragment_at_current_node' ),
98+
'TITLE' => array( '<title></title>', 'create_fragment_at_current_node' ),
99+
'XMP' => array( '<xmp></xmp>', 'create_fragment_at_current_node' ),
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)