TYPO3 CMS  TYPO3_6-2
HtmlParserTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $fixture = NULL;
28 
29  public function setUp() {
30  $this->fixture = new \TYPO3\CMS\Core\Html\HtmlParser();
31  }
32 
37  $template = '###SINGLEMARKER1###
38 <!-- ###FOO### begin -->
39 <!-- ###BAR### begin -->
40 ###SINGLEMARKER2###
41 <!-- ###BAR### end -->
42 <!-- ###FOOTER### begin -->
43 ###SINGLEMARKER3###
44 <!-- ###FOOTER### end -->
45 <!-- ###FOO### end -->';
46 
47  $expected ='Value 1
48 
49 
50 Value 2.1
51 
52 Value 2.2
53 
54 
55 Value 3.1
56 
57 Value 3.2
58 
59 ';
60 
61  return array(
62  'Single marker' => array(
63  '###SINGLEMARKER###',
64  array(
65  '###SINGLEMARKER###' => 'Value 1'
66  ),
67  '',
68  FALSE,
69  FALSE,
70  'Value 1'
71  ),
72  'Subpart marker' => array(
73  $template,
74  array(
75  '###SINGLEMARKER1###' => 'Value 1',
76  '###FOO###' => array(
77  array(
78  '###BAR###' => array(
79  array(
80  '###SINGLEMARKER2###' => 'Value 2.1'
81  ),
82  array(
83  '###SINGLEMARKER2###' => 'Value 2.2'
84  )
85  ),
86  '###FOOTER###' => array(
87  array(
88  '###SINGLEMARKER3###' => 'Value 3.1'
89  ),
90  array(
91  '###SINGLEMARKER3###' => 'Value 3.2'
92  )
93  )
94  )
95  )
96  ),
97  '',
98  FALSE,
99  FALSE,
100  $expected
101  ),
102  'Subpart marker with wrap' => array(
103  $template,
104  array(
105  'SINGLEMARKER1' => 'Value 1',
106  'FOO' => array(
107  array(
108  'BAR' => array(
109  array(
110  'SINGLEMARKER2' => 'Value 2.1'
111  ),
112  array(
113  'SINGLEMARKER2' => 'Value 2.2'
114  )
115  ),
116  'FOOTER' => array(
117  array(
118  'SINGLEMARKER3' => 'Value 3.1'
119  ),
120  array(
121  'SINGLEMARKER3' => 'Value 3.2'
122  )
123  )
124  )
125  )
126  ),
127  '###|###',
128  FALSE,
129  FALSE,
130  $expected
131  ),
132  'Subpart marker with lower marker array keys' => array(
133  $template,
134  array(
135  '###singlemarker1###' => 'Value 1',
136  '###foo###' => array(
137  array(
138  '###bar###' => array(
139  array(
140  '###singlemarker2###' => 'Value 2.1'
141  ),
142  array(
143  '###singlemarker2###' => 'Value 2.2'
144  )
145  ),
146  '###footer###' => array(
147  array(
148  '###singlemarker3###' => 'Value 3.1'
149  ),
150  array(
151  '###singlemarker3###' => 'Value 3.2'
152  )
153  )
154  )
155  )
156  ),
157  '',
158  TRUE,
159  FALSE,
160  $expected
161  ),
162  'Subpart marker with unused markers' => array(
163  $template,
164  array(
165  '###FOO###' => array(
166  array(
167  '###BAR###' => array(
168  array(
169  '###SINGLEMARKER2###' => 'Value 2.1'
170  )
171  ),
172  '###FOOTER###' => array(
173  array(
174  '###SINGLEMARKER3###' => 'Value 3.1'
175  )
176  )
177  )
178  )
179  ),
180  '',
181  FALSE,
182  TRUE,
183  '
184 
185 
186 Value 2.1
187 
188 
189 Value 3.1
190 
191 '
192  ),
193  'Subpart marker with empty subpart' => array(
194  $template,
195  array(
196  '###SINGLEMARKER1###' => 'Value 1',
197  '###FOO###' => array(
198  array(
199  '###BAR###' => array(
200  array(
201  '###SINGLEMARKER2###' => 'Value 2.1'
202  ),
203  array(
204  '###SINGLEMARKER2###' => 'Value 2.2'
205  )
206  ),
207  '###FOOTER###' => array()
208  )
209  )
210  ),
211  '',
212  FALSE,
213  FALSE,
214  'Value 1
215 
216 
217 Value 2.1
218 
219 Value 2.2
220 
221 
222 '
223  )
224  );
225  }
226 
231  public function substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused, $expected) {
232  $this->assertSame($expected, $this->fixture->substituteMarkerAndSubpartArrayRecursive($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused));
233  }
234 
239  return array(
240  'single-line CDATA' => array(
241  '/*<![CDATA[*/ <hello world> /*]]>*/',
242  '/*<![CDATA[*/ <hello world> /*]]>*/',
243  ),
244  'multi-line CDATA #1' => array(
245  '/*<![CDATA[*/' . LF . '<hello world> /*]]>*/',
246  '/*<![CDATA[*/' . LF . '<hello world> /*]]>*/',
247  ),
248  'multi-line CDATA #2' => array(
249  '/*<![CDATA[*/ <hello world>' . LF . '/*]]>*/',
250  '/*<![CDATA[*/ <hello world>' . LF . '/*]]>*/',
251  ),
252  'multi-line CDATA #3' => array(
253  '/*<![CDATA[*/' . LF . '<hello world>' . LF . '/*]]>*/',
254  '/*<![CDATA[*/' . LF . '<hello world>' . LF . '/*]]>*/',
255  ),
256  );
257  }
258 
265  public function xHtmlCleaningDoesNotModifyCDATA($source, $expected) {
266  $result = $this->fixture->XHTML_clean($source);
267  $this->assertSame($expected, $result);
268  }
269 
274  return array(
275  'Span tag with no attrib' => array(
276  '<span>text</span>',
277  'text'
278  ),
279  'Span tag with allowed id attrib' => array(
280  '<span id="id">text</span>',
281  '<span id="id">text</span>'
282  ),
283  'Span tag with disallowed style attrib' => array(
284  '<span style="line-height: 12px;">text</span>',
285  'text'
286  )
287  );
288  }
289 
294  public function tagCorrectlyRemovedWhenRmTagIfNoAttribIsConfigured($content, $expectedResult) {
295  $tsConfig = array(
296  'allowTags' => 'span',
297  'tags.' => array(
298  'span.' => array(
299  'allowedAttribs' => 'id',
300  'rmTagIfNoAttrib' => 1
301  )
302  )
303  );
304  $this->assertEquals($expectedResult, $this->parseConfigAndCleanHtml($tsConfig, $content));
305  }
306 
311  $tsConfig = array(
312  'allowTags' => 'div,span',
313  'rmTagIfNoAttrib' => 'span',
314  'globalNesting' => 'div,span'
315  );
316  $content = '<span></span><span id="test"><div></span></div>';
317  $expectedResult = '<span id="test"></span>';
318  $this->assertEquals($expectedResult, $this->parseConfigAndCleanHtml($tsConfig, $content));
319  }
320 
325  return array(
326  'Valid nesting is untouched' => array(
327  '<B><I></B></I>',
328  '<B><I></B></I>'
329  ),
330  'Valid nesting with content is untouched' => array(
331  'testa<B>test1<I>test2</B>test3</I>testb',
332  'testa<B>test1<I>test2</B>test3</I>testb'
333  ),
334  'Superflous tags are removed' => array(
335  '</B><B><I></B></I></B>',
336  '<B><I></B></I>'
337  ),
338  'Superflous tags with content are removed' => array(
339  'test1</B>test2<B>test3<I>test4</B>test5</I>test6</B>test7',
340  'test1test2<B>test3<I>test4</B>test5</I>test6test7'
341  ),
342  'Another valid nesting test' => array(
343  '<span><div></span></div>',
344  '<span><div></span></div>',
345  ),
346  );
347  }
348 
355  public function localNestingCorrectlyRemovesInvalidTags($content, $expectedResult) {
356  $tsConfig = array(
357  'allowTags' => 'div,span,b,i',
358  'localNesting' => 'div,span,b,i',
359  );
360  $this->assertEquals($expectedResult, $this->parseConfigAndCleanHtml($tsConfig, $content));
361  }
362 
367  return array(
368  'Valid nesting is untouched' => array(
369  '<B><I></I></B>',
370  '<B><I></I></B>'
371  ),
372  'Valid nesting with content is untouched' => array(
373  'testa<B>test1<I>test2</I>test3</B>testb',
374  'testa<B>test1<I>test2</I>test3</B>testb'
375  ),
376  'Invalid nesting is cleaned' => array(
377  '</B><B><I></B></I></B>',
378  '<B></B>'
379  ),
380  'Invalid nesting with content is cleaned' => array(
381  'test1</B>test2<B>test3<I>test4</B>test5</I>test6</B>test7',
382  'test1test2<B>test3test4</B>test5test6test7'
383  ),
384  'Another invalid nesting test' => array(
385  '<span><div></span></div>',
386  '<span></span>',
387  ),
388  );
389  }
390 
397  public function globalNestingCorrectlyRemovesInvalidTags($content, $expectedResult) {
398  $tsConfig = array(
399  'allowTags' => 'span,div,b,i',
400  'globalNesting' => 'span,div,b,i',
401  );
402  $this->assertEquals($expectedResult, $this->parseConfigAndCleanHtml($tsConfig, $content));
403  }
404 
412  protected function parseConfigAndCleanHtml(array $tsConfig, $content) {
413  $config = $this->fixture->HTMLparserConfig($tsConfig);
414  return $this->fixture->HTMLcleaner($content, $config[0], $config[1], $config[2], $config[3]);
415  }
416 }
tagCorrectlyRemovedWhenRmTagIfNoAttribIsConfigured($content, $expectedResult)
globalNestingCorrectlyRemovesInvalidTags($content, $expectedResult)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
parseConfigAndCleanHtml(array $tsConfig, $content)
localNestingCorrectlyRemovesInvalidTags($content, $expectedResult)
substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused, $expected)