[3.1.0] Landed modified patch by Braden Anderson for %CSS.AllowedProperties
[htmlpurifier/bfroehle.git] / tests / HTMLPurifierTest.php
blob3b645055a50554ea0fc29e27d6dbb7f8220bed22
1 <?php
3 class HTMLPurifierTest extends HTMLPurifier_Harness
5 protected $purifier;
7 function setUp() {
8 $this->purifier = new HTMLPurifier();
11 function assertPurification($input, $expect = null, $config = array()) {
12 if ($expect === null) $expect = $input;
13 $result = $this->purifier->purify($input, $config);
14 $this->assertIdentical($expect, $result);
17 function testNull() {
18 $this->assertPurification("Null byte\0", "Null byte");
21 function testStrict() {
22 $config = HTMLPurifier_Config::createDefault();
23 $config->set('HTML', 'Strict', true);
24 $this->purifier = new HTMLPurifier( $config ); // verbose syntax
26 $this->assertPurification(
27 '<u>Illegal underline</u>',
28 '<span style="text-decoration:underline;">Illegal underline</span>'
31 $this->assertPurification(
32 '<blockquote>Illegal contents</blockquote>',
33 '<blockquote><p>Illegal contents</p></blockquote>'
38 function testDifferentAllowedElements() {
40 $this->purifier = new HTMLPurifier(array(
41 'HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
42 'HTML.AllowedAttributes' => array('a.href', '*.id')
43 ));
45 $this->assertPurification(
46 '<p>Par.</p><p>Para<a href="http://google.com/">gr</a>aph</p>Text<b>Bol<i>d</i></b>'
49 $this->assertPurification(
50 '<span>Not allowed</span><a class="mef" id="foobar">Foobar</a>',
51 'Not allowed<a>Foobar</a>' // no ID!!!
56 function testDifferentAllowedCSSProperties() {
58 $this->purifier = new HTMLPurifier(array(
59 'CSS.AllowedProperties' => array('color', 'background-color')
60 ));
62 $this->assertPurification(
63 '<div style="color:#f00;background-color:#ded;">red</div>'
66 $this->assertPurification(
67 '<div style="color:#f00;border:1px solid #000">red</div>',
68 '<div style="color:#f00;">red</div>'
73 function testDisableURI() {
75 $this->purifier = new HTMLPurifier( array('Attr.DisableURI' => true) );
77 $this->assertPurification(
78 '<img src="foobar"/>',
84 function test_purifyArray() {
86 $this->purifier = new HTMLPurifier();
88 $this->assertIdentical(
89 $this->purifier->purifyArray(
90 array('Good', '<b>Sketchy', 'foo' => '<script>bad</script>')
92 array('Good', '<b>Sketchy</b>', 'foo' => '')
95 $this->assertIsA($this->purifier->context, 'array');
99 function testEnableAttrID() {
101 $this->purifier = new HTMLPurifier();
103 $this->assertPurification(
104 '<span id="moon">foobar</span>',
105 '<span>foobar</span>'
108 $this->purifier = new HTMLPurifier(array('HTML.EnableAttrID' => true));
109 $this->assertPurification('<span id="moon">foobar</span>');
110 $this->assertPurification('<img id="folly" src="folly.png" alt="Omigosh!" />');
114 function testScript() {
115 $this->purifier = new HTMLPurifier(array('HTML.Trusted' => true));
116 $ideal = '<script type="text/javascript"><!--//--><![CDATA[//><!--
117 alert("<This is compatible with XHTML>");
118 //--><!]]></script>';
120 $this->assertPurification($ideal);
122 $this->assertPurification(
123 '<script type="text/javascript"><![CDATA[
124 alert("<This is compatible with XHTML>");
125 ]]></script>',
126 $ideal
129 $this->assertPurification(
130 '<script type="text/javascript">alert("<This is compatible with XHTML>");</script>',
131 $ideal
134 $this->assertPurification(
135 '<script type="text/javascript"><!--
136 alert("<This is compatible with XHTML>");
137 //--></script>',
138 $ideal
141 $this->assertPurification(
142 '<script type="text/javascript"><![CDATA[
143 alert("<This is compatible with XHTML>");
144 //]]></script>',
145 $ideal
149 function testGetInstance() {
150 $purifier = HTMLPurifier::getInstance();
151 $purifier2 = HTMLPurifier::getInstance();
152 $this->assertReference($purifier, $purifier2);
155 function testMakeAbsolute() {
156 $this->assertPurification(
157 '<a href="foo.txt">Foobar</a>',
158 '<a href="http://example.com/bar/foo.txt">Foobar</a>',
159 array(
160 'URI.Base' => 'http://example.com/bar/baz.php',
161 'URI.MakeAbsolute' => true