3 require_once 'HTMLPurifier/ChildDef.php';
4 require_once 'HTMLPurifier/Lexer/DirectLex.php';
5 require_once 'HTMLPurifier/Generator.php';
7 class HTMLPurifier_ChildDefTest
extends UnitTestCase
14 function HTMLPurifier_ChildDefTest() {
15 // it is vital that the tags be treated as literally as possible
16 $this->lex
= new HTMLPurifier_Lexer_DirectLex();
17 $this->gen
= new HTMLPurifier_Generator();
18 parent
::UnitTestCase();
21 function assertSeries($inputs, $expect, $config, $context = array()) {
22 foreach ($inputs as $i => $input) {
24 if (!isset($context[$i])) {
27 if (!isset($config[$i])) {
28 $config[$i] = HTMLPurifier_Config
::createDefault();
31 $tokens = $this->lex
->tokenizeHTML($input, $config[$i]);
32 $result = $this->def
->validateChildren($tokens, $config[$i], $context[$i]);
34 if (is_bool($expect[$i])) {
35 $this->assertIdentical($expect[$i], $result, "Test $i: %s");
37 $result_html = $this->gen
->generateFromTokens($result, $config[$i]);
38 $this->assertIdentical($expect[$i], $result_html, "Test $i: %s");
39 paintIf($result_html, $result_html != $expect[$i]);
44 function test_custom() {
46 $this->def
= new HTMLPurifier_ChildDef_Custom(
47 '(a, b?, c*, d+, (a, b)*)');
56 $inputs[1] = '<a /><b /><c /><d /><a /><b />';
59 $inputs[2] = '<a /><d>Dob</d><a /><b>foo</b><a href="moo" /><b>foo</b>';
62 $inputs[3] = '<a /><a />';
65 $this->assertSeries($inputs, $expect, $config);
68 function test_table() {
70 // currently inactive, awaiting augmentation
72 // the table definition
73 $this->def
= new HTMLPurifier_ChildDef_Table();
75 $inputs = $expect = $config = array();
80 // we're using empty tags to compact the tests: under real circumstances
81 // there would be contents in them
83 $inputs[1] = '<tr />';
86 $inputs[2] = '<caption /><col /><thead /><tfoot /><tbody>'.
87 '<tr><td>asdf</td></tr></tbody>';
90 $inputs[3] = '<col /><col /><col /><tr />';
94 $inputs[4] = '<col /><colgroup /><tbody /><tfoot /><thead /><tr>1</tr><caption /><tr />';
95 $expect[4] = '<caption /><col /><colgroup /><thead /><tfoot /><tbody /><tr>1</tr><tr />';
97 // duplicates of singles
98 // - first caption serves
99 // - trailing tfoots/theads get turned into tbodys
100 $inputs[5] = '<caption>1</caption><caption /><tbody /><tbody /><tfoot>1</tfoot><tfoot />';
101 $expect[5] = '<caption>1</caption><tfoot>1</tfoot><tbody /><tbody /><tbody />';
103 // errant text dropped (until bubbling is implemented)
107 // whitespace sticks to the previous element, last whitespace is
109 $inputs[7] = "\n <tr />\n <tr />\n ";
112 $inputs[8] = "\n\t<tbody />\n\t\t<tfoot />\n\t\t\t";
113 $expect[8] = "\n\t\t<tfoot />\n\t<tbody />\n\t\t\t";
115 $this->assertSeries($inputs, $expect, $config);
119 function test_parsing() {
121 $def = new HTMLPurifier_ChildDef_Required('foobar | bang |gizmo');
122 $this->assertEqual($def->elements
,
129 $def = new HTMLPurifier_ChildDef_Required(array('href', 'src'));
130 $this->assertEqual($def->elements
,
137 function test_required_pcdata_forbidden() {
139 $this->def
= new HTMLPurifier_ChildDef_Required('dt | dd');
140 $inputs = $expect = $config = array();
145 $inputs[1] = '<dt>Term</dt>Text in an illegal location'.
146 '<dd>Definition</dd><b>Illegal tag</b>';
148 $expect[1] = '<dt>Term</dt><dd>Definition</dd>';
150 $inputs[2] = 'How do you do!';
153 // whitespace shouldn't trigger it
154 $inputs[3] = "\n<dd>Definition</dd> ";
157 $inputs[4] ='<dd>Definition</dd> <b></b> ';
158 $expect[4] = '<dd>Definition</dd> ';
163 $this->assertSeries($inputs, $expect, $config);
167 function test_required_pcdata_allowed() {
168 $this->def
= new HTMLPurifier_ChildDef_Required('#PCDATA | b');
170 $inputs = $expect = $config = array();
172 $inputs[0] = '<b>Bold text</b><img />';
173 $expect[0] = '<b>Bold text</b>';
175 // with child escaping on
176 $inputs[1] = '<b>Bold text</b><img />';
177 $expect[1] = '<b>Bold text</b><img />';
178 $config[1] = HTMLPurifier_Config
::createDefault();
179 $config[1]->set('Core', 'EscapeInvalidChildren', true);
181 $this->assertSeries($inputs, $expect, $config);
184 function test_optional() {
185 $this->def
= new HTMLPurifier_ChildDef_Optional('b | i');
187 $inputs = $expect = $config = array();
189 $inputs[0] = '<b>Bold text</b><img />';
190 $expect[0] = '<b>Bold text</b>';
192 $inputs[1] = 'Not allowed text';
195 $this->assertSeries($inputs, $expect, $config);
198 function test_chameleon() {
200 $this->def
= new HTMLPurifier_ChildDef_Chameleon(
201 'b | i', // allowed only when in inline context
202 'b | i | div' // allowed only when in block context
205 $inputs = $expect = $config = array();
208 $inputs[0] = '<b>Allowed.</b>';
210 $context[0] = 'inline';
212 $inputs[1] = '<div>Not allowed.</div>';
214 $context[1] = 'inline';
216 $inputs[2] = '<div>Allowed.</div>';
218 $context[2] = 'block';
220 $this->assertSeries($inputs, $expect, $config, $context);