Add %HTML.TargetNoreferrer, which adds rel="noreferrer" when target attribute is set
[htmlpurifier.git] / tests / HTMLPurifier / Strategy / ValidateAttributesTest.php
blob19fca6acaac04b58890a8ce14cba8a3b6eced5e0
1 <?php
3 class HTMLPurifier_Strategy_ValidateAttributesTest extends
4 HTMLPurifier_StrategyHarness
7 public function setUp()
9 parent::setUp();
10 $this->obj = new HTMLPurifier_Strategy_ValidateAttributes();
13 public function testEmptyInput()
15 $this->assertResult('');
18 public function testRemoveIDByDefault()
20 $this->assertResult(
21 '<div id="valid">Kill the ID.</div>',
22 '<div>Kill the ID.</div>'
26 public function testRemoveInvalidDir()
28 $this->assertResult(
29 '<span dir="up-to-down">Bad dir.</span>',
30 '<span>Bad dir.</span>'
34 public function testPreserveValidClass()
36 $this->assertResult('<div class="valid">Valid</div>');
39 public function testSelectivelyRemoveInvalidClasses()
41 $this->config->set('HTML.Doctype', 'XHTML 1.1');
42 $this->assertResult(
43 '<div class="valid 0invalid">Keep valid.</div>',
44 '<div class="valid">Keep valid.</div>'
48 public function testPreserveTitle()
50 $this->assertResult(
51 '<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>'
55 public function testAddXMLLang()
57 $this->assertResult(
58 '<span lang="fr">La soupe.</span>',
59 '<span lang="fr" xml:lang="fr">La soupe.</span>'
63 public function testOnlyXMLLangInXHTML11()
65 $this->config->set('HTML.Doctype', 'XHTML 1.1');
66 $this->assertResult(
67 '<b lang="en">asdf</b>',
68 '<b xml:lang="en">asdf</b>'
72 public function testBasicURI()
74 $this->assertResult('<a href="http://www.google.com/">Google</a>');
77 public function testInvalidURI()
79 $this->assertResult(
80 '<a href="javascript:badstuff();">Google</a>',
81 '<a>Google</a>'
85 public function testBdoAddMissingDir()
87 $this->assertResult(
88 '<bdo>Go left.</bdo>',
89 '<bdo dir="ltr">Go left.</bdo>'
93 public function testBdoReplaceInvalidDirWithDefault()
95 $this->assertResult(
96 '<bdo dir="blahblah">Invalid value!</bdo>',
97 '<bdo dir="ltr">Invalid value!</bdo>'
101 public function testBdoAlternateDefaultDir()
103 $this->config->set('Attr.DefaultTextDir', 'rtl');
104 $this->assertResult(
105 '<bdo>Go right.</bdo>',
106 '<bdo dir="rtl">Go right.</bdo>'
110 public function testRemoveDirWhenNotRequired()
112 $this->assertResult(
113 '<span dir="blahblah">Invalid value!</span>',
114 '<span>Invalid value!</span>'
118 public function testTableAttributes()
120 $this->assertResult(
121 '<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
122 <col align="right" width="4*" />
123 <col charoff="5" align="char" width="*" />
124 <tr valign="top">
125 <th abbr="name">Fiddly name</th>
126 <th abbr="price">Super-duper-price</th>
127 </tr>
128 <tr>
129 <td abbr="carrot">Carrot Humungous</td>
130 <td>$500.23</td>
131 </tr>
132 <tr>
133 <td colspan="2">Taken off the market</td>
134 </tr>
135 </table>'
139 public function testColSpanIsNonZero()
141 $this->assertResult(
142 '<col span="0" />',
143 '<col />'
147 public function testImgAddDefaults()
149 $this->config->set('Core.RemoveInvalidImg', false);
150 $this->assertResult(
151 '<img />',
152 '<img src="" alt="Invalid image" />'
156 public function testImgGenerateAlt()
158 $this->assertResult(
159 '<img src="foobar.jpg" />',
160 '<img src="foobar.jpg" alt="foobar.jpg" />'
164 public function testImgAddDefaultSrc()
166 $this->config->set('Core.RemoveInvalidImg', false);
167 $this->assertResult(
168 '<img alt="pretty picture" />',
169 '<img alt="pretty picture" src="" />'
173 public function testImgRemoveNonRetrievableProtocol()
175 $this->config->set('Core.RemoveInvalidImg', false);
176 $this->assertResult(
177 '<img src="mailto:foo@example.com" />',
178 '<img alt="mailto:foo@example.com" src="" />'
182 public function testPreserveRel()
184 $this->config->set('Attr.AllowedRel', 'nofollow');
185 $this->assertResult('<a href="foo" rel="nofollow" />');
188 public function testPreserveTarget()
190 $this->config->set('Attr.AllowedFrameTargets', '_top');
191 $this->config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
192 $this->assertResult('<a href="foo" target="_top" rel="noreferrer" />');
195 public function testRemoveTargetWhenNotSupported()
197 $this->config->set('HTML.Doctype', 'XHTML 1.0 Strict');
198 $this->config->set('Attr.AllowedFrameTargets', '_top');
199 $this->assertResult(
200 '<a href="foo" target="_top" />',
201 '<a href="foo" />'
205 public function testKeepAbsoluteCSSWidthAndHeightOnImg()
207 $this->assertResult(
208 '<img src="" alt="" style="width:10px;height:10px;border:1px solid #000;" />'
212 public function testRemoveLargeCSSWidthAndHeightOnImg()
214 $this->assertResult(
215 '<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />',
216 '<img src="" alt="" style="border:1px solid #000;" />'
220 public function testRemoveLargeCSSWidthAndHeightOnImgWithUserConf()
222 $this->config->set('CSS.MaxImgLength', '1px');
223 $this->assertResult(
224 '<img src="" alt="" style="width:1mm;height:1mm;border:1px solid #000;" />',
225 '<img src="" alt="" style="border:1px solid #000;" />'
229 public function testKeepLargeCSSWidthAndHeightOnImgWhenToldTo()
231 $this->config->set('CSS.MaxImgLength', null);
232 $this->assertResult(
233 '<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />'
237 public function testKeepPercentCSSWidthAndHeightOnImgWhenToldTo()
239 $this->config->set('CSS.MaxImgLength', null);
240 $this->assertResult(
241 '<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />'
245 public function testRemoveRelativeCSSWidthAndHeightOnImg()
247 $this->assertResult(
248 '<img src="" alt="" style="width:10em;height:10em;border:1px solid #000;" />',
249 '<img src="" alt="" style="border:1px solid #000;" />'
253 public function testRemovePercentCSSWidthAndHeightOnImg()
255 $this->assertResult(
256 '<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />',
257 '<img src="" alt="" style="border:1px solid #000;" />'
263 // vim: et sw=4 sts=4