EscapeCDATATest.php
5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: EscapeCDATATest.php 579 2009-04-25 23:14:46Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
class EscapeCDATATest extends PHPTAL_TestCase {
private function executeString($str, $params = array())
{
$tpl = $this->newPHPTAL();
foreach ($params as $k => $v) $tpl->set($k,$v);
$tpl->setSource($str);
return $tpl->execute();
}
function testTrimString()
{
$this->assertEquals(
trim_string('<foo><bar>]]> foo ]> bar</bar></foo>'),
trim_string('<foo> <bar>]]> foo ]> bar </bar> </foo>')
);
$this->assertNotEquals(
trim_string('foo]]>bar'),
trim_string('foo]]>bar')
);
}
function testDoesEscapeHTMLContent(){
$tpl = $this->newPHPTAL('input/escape.html');
$exp = trim_file('output/escape.html');
$res = trim_string($tpl->execute());
$this->assertEquals($exp, $res);
}
function testEntityTextInPath()
{
$res = $this->executeString('<div><![CDATA[${text \'"< & & " <\'},${false | string:"< & & " <}]]></div>');
// either way is good
if (false !== strpos($res,'<![CDATA[')) {
$this->assertEquals('<div><![CDATA["< & & " <,"< & & " <]]></div>', $res);
} else {
$this->assertEquals('<div>"< & &amp; &quot; &lt;,"< & &amp; &quot; &lt;</div>', $res);
}
}
function testEntityStructureInPath()
{
$res = $this->executeString('<div><![CDATA[${structure \'"< & & " <\'},${structure false | string:"< & & " <}]]></div>');
$this->assertEquals('<div><![CDATA["< & & " <,"< & & " <]]></div>', $res);
}
function testEntityInContentPHP()
{
$res = $this->executeString('<div><![CDATA[${php:strlen(\'"&<\')},${php:strlen(\'<"&\')}]]></div>');
$this->assertEquals('<div>15,3</div>',$res);
}
function testEntityInScriptPHP()
{
$res = $this->executeString('<script><![CDATA[${php:strlen(\'"&<\')},${php:strlen(\'<"&\')}]]></script>');
$this->assertEquals('<script><![CDATA[15,3]]></script>',$res);
}
function testEntityInPHP2()
{
$res = $this->executeString('<div><![CDATA[${structure php:strlen(\'"&<\')},${structure php:strlen(\'<"&\')}]]></div>');
$this->assertEquals('<div><![CDATA[15,3]]></div>',$res);
}
function testEntityInPHP3()
{
$res = $this->executeString('<div><![CDATA[<?php echo strlen(\'"&<\')?>,<?php echo strlen(\'<"&\') ?>]]></div>');
$this->assertEquals('<div><![CDATA[15,3]]></div>',$res);
}
function testNoEncodingAfterPHP()
{
$res = $this->executeString('<div><![CDATA[${php:urldecode(\'%26%22%3C\')},${structure php:urldecode(\'%26%22%3C\')},<?php echo urldecode(\'%26%22%3C\') ?>]]></div>');
$this->assertEquals('<div><![CDATA[&"<,&"<,&"<]]></div>',$res);
}
/**
* normal XML behavior expected
*/
function testEscapeCDATAXML()
{
$tpl = $this->newPHPTAL();
$tpl->setOutputMode(PHPTAL::XML);
$tpl->setSource('<y><![CDATA[${cdata}; ${php:cdata};]]></y> <y><![CDATA[${structure cdata}]]></y>');
$tpl->cdata = ']]></x>';
$res = $tpl->execute();
$this->assertEquals('<y>]]></x>; ]]></x>;</y> <y><![CDATA[]]></x>]]></y>',$res);
}
/**
* ugly hybrid between HTML (XHTML as text/html) and XML
*/
function testEscapeCDATAXHTML()
{
$tpl = $this->newPHPTAL();
$tpl->setOutputMode(PHPTAL::XHTML);
$tpl->setSource('<script><![CDATA[${cdata}; ${php:cdata};]]></script> <y><![CDATA[${structure cdata}]]></y>');
$tpl->cdata = ']]></x>';
$res = $tpl->execute();
$this->assertEquals('<script><![CDATA[]]]]><![CDATA[><\/x>; ]]]]><![CDATA[><\/x>;]]></script> <y><![CDATA[]]></x>]]></y>',$res);
}
function testEscapeCDATAHTML()
{
$tpl = $this->newPHPTAL();
$tpl->setOutputMode(PHPTAL::HTML5);
$tpl->setSource('<y><![CDATA[${cdata}; ${php:cdata};]]></y> <y><![CDATA[${structure cdata}]]></y>');
$tpl->cdata = ']]></x>';
$res = $tpl->execute();
$this->assertEquals('<y>]]></x>; ]]></x>;</y> <y>]]></x></y>',$res);
}
function testAutoCDATA()
{
$res = $this->executeString('<script> 1 < 2 </script>');
$this->assertEquals('<script>/*<![CDATA[*/ 1 < 2 /*]]>*/</script>',$res);
}
function testAutoCDATA2()
{
$res = $this->executeString('<xhtmlz:script xmlns:xhtmlz="http://www.w3.org/1999/xhtml"> 1 < 2 ${php:\'&\' . \'&\'} </xhtmlz:script>');
$this->assertEquals('<xhtmlz:script xmlns:xhtmlz="http://www.w3.org/1999/xhtml">/*<![CDATA[*/ 1 < 2 && /*]]>*/</xhtmlz:script>',$res);
}
function testNoAutoCDATA()
{
$res = $this->executeString('<script> "1 \' 2" </script><script xmlns="foo"> 1 < 2 </script>');
$this->assertEquals('<script> "1 \' 2" </script><script xmlns="foo"> 1 < 2 </script>',$res);
}
function testNoAutoCDATA2()
{
$res = $this->executeString('<script> a && ${structure foo} </script><script xmlns="foo"> 1 < 2 </script>', array('foo'=>'<foo/>'));
$this->assertEquals('<script> a && <foo/> </script><script xmlns="foo"> 1 < 2 </script>',$res);
}
function testNoAutoCDATA3()
{
$res = $this->executeString('<style> html > body </style>');
$this->assertEquals('<style> html > body </style>',$res);
}
}