2006-07-30 18:37:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
2006-08-05 00:30:31 +00:00
|
|
|
HTMLPurifier_StrategyHarness
|
2006-07-30 18:37:42 +00:00
|
|
|
{
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->obj = new HTMLPurifier_Strategy_ValidateAttributes();
|
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testEmptyInput() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('');
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testRemoveIDByDefault() {
|
2006-11-17 01:05:41 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id="valid">Kill the ID.</div>',
|
|
|
|
'<div>Kill the ID.</div>'
|
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveInvalidDir() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<span dir="up-to-down">Bad dir.</span>',
|
|
|
|
'<span>Bad dir.</span>'
|
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testPreserveValidClass() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('<div class="valid">Valid</div>');
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSelectivelyRemoveInvalidClasses() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div class="valid 0invalid">Keep valid.</div>',
|
|
|
|
'<div class="valid">Keep valid.</div>'
|
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testPreserveTitle() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>'
|
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testAddXMLLang() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<span lang="fr">La soupe.</span>',
|
|
|
|
'<span lang="fr" xml:lang="fr">La soupe.</span>'
|
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testOnlyXMLLangInXHTML11() {
|
|
|
|
$this->config->set('HTML', 'Doctype', 'XHTML 1.1');
|
2007-05-05 16:18:04 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<b lang="en">asdf</b>',
|
2007-08-06 06:22:23 +00:00
|
|
|
'<b xml:lang="en">asdf</b>'
|
2007-05-05 16:18:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testBasicURI() {
|
|
|
|
$this->assertResult('<a href="http://www.google.com/">Google</a>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testInvalidURI() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<a href="javascript:badstuff();">Google</a>',
|
|
|
|
'<a>Google</a>'
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testBdoAddMissingDir() {
|
2007-05-05 15:48:41 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<bdo>Go left.</bdo>',
|
|
|
|
'<bdo dir="ltr">Go left.</bdo>'
|
2007-05-05 15:48:41 +00:00
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testBdoReplaceInvalidDirWithDefault() {
|
2007-05-05 15:48:41 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<bdo dir="blahblah">Invalid value!</bdo>',
|
|
|
|
'<bdo dir="ltr">Invalid value!</bdo>'
|
2007-05-05 15:48:41 +00:00
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testBdoAlternateDefaultDir() {
|
|
|
|
$this->config->set('Attr', 'DefaultTextDir', 'rtl');
|
2007-05-05 15:48:41 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<bdo>Go right.</bdo>',
|
|
|
|
'<bdo dir="rtl">Go right.</bdo>'
|
2007-05-05 15:48:41 +00:00
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveDirWhenNotRequired() {
|
2007-05-05 15:48:41 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<span dir="blahblah">Invalid value!</span>',
|
|
|
|
'<span>Invalid value!</span>'
|
2007-05-05 15:48:41 +00:00
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testTableAttributes() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
2006-08-14 23:11:28 +00:00
|
|
|
'<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
|
2006-08-06 01:41:18 +00:00
|
|
|
<col align="right" width="4*" />
|
2007-02-17 17:43:44 +00:00
|
|
|
<col charoff="5" align="char" width="*" />
|
2006-08-05 22:14:19 +00:00
|
|
|
<tr valign="top">
|
2006-08-06 01:30:54 +00:00
|
|
|
<th abbr="name">Fiddly name</th>
|
|
|
|
<th abbr="price">Super-duper-price</th>
|
2006-08-05 22:14:19 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2006-08-06 01:30:54 +00:00
|
|
|
<td abbr="carrot">Carrot Humungous</td>
|
|
|
|
<td>$500.23</td>
|
2006-08-05 22:14:19 +00:00
|
|
|
</tr>
|
2006-08-06 03:58:48 +00:00
|
|
|
<tr>
|
|
|
|
<td colspan="2">Taken off the market</td>
|
|
|
|
</tr>
|
2006-10-01 21:55:13 +00:00
|
|
|
</table>'
|
|
|
|
);
|
2007-08-06 06:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testColSpanIsNonZero() {
|
2007-05-05 16:18:04 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<col span="0" />',
|
|
|
|
'<col />'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testImgAddDefaults() {
|
|
|
|
$this->config->set('Core', 'RemoveInvalidImg', false);
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<img />',
|
|
|
|
'<img src="" alt="Invalid image" />'
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testImgGenerateAlt() {
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="foobar.jpg" />',
|
|
|
|
'<img src="foobar.jpg" alt="foobar.jpg" />'
|
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testImgAddDefaultSrc() {
|
|
|
|
$this->config->set('Core', 'RemoveInvalidImg', false);
|
2007-04-30 21:19:15 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<img alt="pretty picture" />',
|
|
|
|
'<img alt="pretty picture" src="" />'
|
2007-04-30 21:19:15 +00:00
|
|
|
);
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testImgRemoveNonRetrievableProtocol() {
|
|
|
|
$this->config->set('Core', 'RemoveInvalidImg', false);
|
2007-05-05 16:18:04 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<img src="mailto:foo@example.com" />',
|
|
|
|
'<img alt="mailto:foo@example.com" src="" />'
|
2007-05-05 16:18:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testPreserveRel() {
|
|
|
|
$this->config->set('Attr', 'AllowedRel', 'nofollow');
|
|
|
|
$this->assertResult('<a href="foo" rel="nofollow" />');
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testPreserveTarget() {
|
|
|
|
$this->config->set('Attr', 'AllowedFrameTargets', '_top');
|
|
|
|
$this->config->set('HTML', 'Doctype', 'XHTML 1.0 Transitional');
|
|
|
|
$this->assertResult('<a href="foo" target="_top" />');
|
2006-07-30 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 06:22:23 +00:00
|
|
|
function testRemoveTargetWhenNotSupported() {
|
|
|
|
$this->config->set('HTML', 'Doctype', 'XHTML 1.0 Strict');
|
|
|
|
$this->config->set('Attr', 'AllowedFrameTargets', '_top');
|
2007-05-05 19:13:52 +00:00
|
|
|
$this->assertResult(
|
2007-08-06 06:22:23 +00:00
|
|
|
'<a href="foo" target="_top" />',
|
|
|
|
'<a href="foo" />'
|
2007-05-05 19:13:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-04-22 22:28:54 +00:00
|
|
|
function testRemoveCSSWidthAndHeightOnImg() {
|
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:10px;height:10px;border:1px solid #000;" />',
|
|
|
|
'<img src="" alt="" style="border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-07-30 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 13:58:32 +00:00
|
|
|
|