[1.1.2]
[htmlpurifier.git] / tests / HTMLPurifier / ChildDefTest.php
blobdb00bd209fae3d6b305c453464ce0be5d744806a
1 <?php
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
10 var $def;
11 var $lex;
12 var $gen;
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])) {
25 $context[$i] = null;
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");
36 } else {
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)*)');
49 $inputs = array();
50 $expect = array();
51 $config = array();
53 $inputs[0] = '';
54 $expect[0] = false;
56 $inputs[1] = '<a /><b /><c /><d /><a /><b />';
57 $expect[1] = true;
59 $inputs[2] = '<a /><d>Dob</d><a /><b>foo</b><a href="moo" /><b>foo</b>';
60 $expect[2] = true;
62 $inputs[3] = '<a /><a />';
63 $expect[3] = false;
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();
77 $inputs[0] = '';
78 $expect[0] = false;
80 // we're using empty tags to compact the tests: under real circumstances
81 // there would be contents in them
83 $inputs[1] = '<tr />';
84 $expect[1] = true;
86 $inputs[2] = '<caption /><col /><thead /><tfoot /><tbody>'.
87 '<tr><td>asdf</td></tr></tbody>';
88 $expect[2] = true;
90 $inputs[3] = '<col /><col /><col /><tr />';
91 $expect[3] = true;
93 // mixed up order
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)
104 $inputs[6] = 'foo';
105 $expect[6] = false;
107 // whitespace sticks to the previous element, last whitespace is
108 // stationary
109 $inputs[7] = "\n <tr />\n <tr />\n ";
110 $expect[7] = true;
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,
123 array(
124 'foobar' => true
125 ,'bang' => true
126 ,'gizmo' => true
129 $def = new HTMLPurifier_ChildDef_Required(array('href', 'src'));
130 $this->assertEqual($def->elements,
131 array(
132 'href' => true
133 ,'src' => true
137 function test_required_pcdata_forbidden() {
139 $this->def = new HTMLPurifier_ChildDef_Required('dt | dd');
140 $inputs = $expect = $config = array();
142 $inputs[0] = '';
143 $expect[0] = false;
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!';
151 $expect[2] = false;
153 // whitespace shouldn't trigger it
154 $inputs[3] = "\n<dd>Definition</dd> ";
155 $expect[3] = true;
157 $inputs[4] ='<dd>Definition</dd> <b></b> ';
158 $expect[4] = '<dd>Definition</dd> ';
160 $inputs[5] = "\t ";
161 $expect[5] = false;
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>&lt;img /&gt;';
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';
193 $expect[1] = '';
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();
206 $context = array();
208 $inputs[0] = '<b>Allowed.</b>';
209 $expect[0] = true;
210 $context[0] = 'inline';
212 $inputs[1] = '<div>Not allowed.</div>';
213 $expect[1] = '';
214 $context[1] = 'inline';
216 $inputs[2] = '<div>Allowed.</div>';
217 $expect[2] = true;
218 $context[2] = 'block';
220 $this->assertSeries($inputs, $expect, $config, $context);