2006-07-30 18:37:42 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-04 01:47:48 +00:00
|
|
|
require_once('HTMLPurifier/Config.php');
|
2006-08-05 00:30:31 +00:00
|
|
|
require_once('HTMLPurifier/StrategyHarness.php');
|
2006-07-30 18:37:42 +00:00
|
|
|
require_once('HTMLPurifier/Strategy/ValidateAttributes.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();
|
|
|
|
}
|
|
|
|
|
2006-07-30 18:37:42 +00:00
|
|
|
function test() {
|
|
|
|
|
2006-08-14 23:11:28 +00:00
|
|
|
// attribute order is VERY fragile, perhaps we should define
|
|
|
|
// an ordering scheme!
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('');
|
2006-07-30 18:37:42 +00:00
|
|
|
|
2006-08-04 02:48:20 +00:00
|
|
|
// test ids
|
2006-11-17 01:05:41 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id="valid">Kill the ID.</div>',
|
|
|
|
'<div>Kill the ID.</div>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertResult('<div id="valid">Preserve the ID.</div>', true,
|
|
|
|
array('HTML.EnableAttrID' => true));
|
2006-08-04 02:48:20 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id="0invalid">Kill the ID.</div>',
|
2006-11-17 01:05:41 +00:00
|
|
|
'<div>Kill the ID.</div>',
|
|
|
|
array('HTML.EnableAttrID' => true)
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2006-07-30 18:37:42 +00:00
|
|
|
|
2006-08-04 02:48:20 +00:00
|
|
|
// test id accumulator
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id="valid">Valid</div><div id="valid">Invalid</div>',
|
2006-11-17 01:05:41 +00:00
|
|
|
'<div id="valid">Valid</div><div>Invalid</div>',
|
|
|
|
array('HTML.EnableAttrID' => true)
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertResult(
|
|
|
|
'<span dir="up-to-down">Bad dir.</span>',
|
|
|
|
'<span>Bad dir.</span>'
|
|
|
|
);
|
|
|
|
|
|
|
|
// test attribute key case sensitivity
|
|
|
|
$this->assertResult(
|
|
|
|
'<div ID="valid">Convert ID to lowercase.</div>',
|
2006-11-17 01:05:41 +00:00
|
|
|
'<div id="valid">Convert ID to lowercase.</div>',
|
|
|
|
array('HTML.EnableAttrID' => true)
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2006-08-02 02:43:52 +00:00
|
|
|
|
2006-08-04 00:11:54 +00:00
|
|
|
// test simple attribute substitution
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id=" valid ">Trim whitespace.</div>',
|
2006-11-17 01:05:41 +00:00
|
|
|
'<div id="valid">Trim whitespace.</div>',
|
|
|
|
array('HTML.EnableAttrID' => true)
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2006-08-04 00:11:54 +00:00
|
|
|
|
2006-08-04 01:47:48 +00:00
|
|
|
// test configuration id blacklist
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div id="invalid">Invalid</div>',
|
|
|
|
'<div>Invalid</div>',
|
2006-11-17 01:05:41 +00:00
|
|
|
array(
|
|
|
|
'Attr.IDBlacklist' => array('invalid'),
|
|
|
|
'HTML.EnableAttrID' => true
|
|
|
|
)
|
2006-10-01 21:55:13 +00:00
|
|
|
);
|
2006-08-04 01:47:48 +00:00
|
|
|
|
2006-08-04 02:48:20 +00:00
|
|
|
// test classes
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('<div class="valid">Valid</div>');
|
2006-08-04 02:48:20 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div class="valid 0invalid">Keep valid.</div>',
|
|
|
|
'<div class="valid">Keep valid.</div>'
|
|
|
|
);
|
2006-08-04 03:13:04 +00:00
|
|
|
|
|
|
|
// test title
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>'
|
|
|
|
);
|
2006-08-04 03:13:04 +00:00
|
|
|
|
2006-08-05 02:56:57 +00:00
|
|
|
// test lang
|
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>'
|
|
|
|
);
|
2006-08-05 01:50:13 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
// test align
|
|
|
|
$this->assertResult(
|
|
|
|
'<h1 align="center">Centered Headline</h1>',
|
|
|
|
'<h1 style="text-align:center;">Centered Headline</h1>'
|
|
|
|
);
|
2006-08-05 02:56:57 +00:00
|
|
|
|
2006-08-05 22:14:19 +00:00
|
|
|
// test table
|
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>'
|
|
|
|
);
|
2006-08-05 22:14:19 +00:00
|
|
|
|
2006-08-12 19:22:57 +00:00
|
|
|
// test URI
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('<a href="http://www.google.com/">Google</a>');
|
2006-08-12 19:22:57 +00:00
|
|
|
|
|
|
|
// test invalid URI
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<a href="javascript:badstuff();">Google</a>',
|
|
|
|
'<a>Google</a>'
|
|
|
|
);
|
2006-08-12 19:22:57 +00:00
|
|
|
|
2006-08-14 23:11:28 +00:00
|
|
|
// test required attributes for img
|
2006-11-23 23:59:20 +00:00
|
|
|
|
|
|
|
// (this should never happen, as RemoveForeignElements
|
|
|
|
// should have removed the offending image tag)
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img />',
|
|
|
|
'<img src="" alt="Invalid image" />'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="foobar.jpg" />',
|
|
|
|
'<img src="foobar.jpg" alt="foobar.jpg" />'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img alt="pretty picture" />',
|
|
|
|
'<img alt="pretty picture" src="" />'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
|
|
|
// test required attributes for bdo
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<bdo>Go left.</bdo>',
|
|
|
|
'<bdo dir="ltr">Go left.</bdo>'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<bdo dir="blahblah">Invalid value!</bdo>',
|
|
|
|
'<bdo dir="ltr">Invalid value!</bdo>'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
2007-01-20 03:48:39 +00:00
|
|
|
// see above, behavior is subtly different
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<span dir="blahblah">Invalid value!</span>',
|
|
|
|
'<span>Invalid value!</span>'
|
|
|
|
);
|
2006-08-14 23:11:28 +00:00
|
|
|
|
2006-08-25 02:48:49 +00:00
|
|
|
// test col.span is non-zero
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<col span="0" />',
|
|
|
|
'<col />'
|
|
|
|
);
|
2006-07-30 18:37:42 +00:00
|
|
|
|
2006-11-17 23:09:10 +00:00
|
|
|
// mailto in image is not allowed
|
|
|
|
$this->assertResult(
|
|
|
|
'<img src="mailto:foo@example.com" />',
|
|
|
|
'<img src="" alt="Invalid image" />'
|
|
|
|
);
|
|
|
|
|
2007-03-29 23:19:53 +00:00
|
|
|
// name rewritten as id
|
|
|
|
$this->assertResult(
|
|
|
|
'<a name="foobar" />',
|
|
|
|
'<a id="foobar" />',
|
|
|
|
array('HTML.EnableAttrID' => true)
|
|
|
|
);
|
|
|
|
|
2007-03-29 23:48:54 +00:00
|
|
|
// lengths
|
|
|
|
$this->assertResult(
|
|
|
|
'<td height="10" width="5%" /><th height="5%" width="10" /><hr width="10" height="10" />',
|
|
|
|
'<td style="height:10px;width:5%;" /><th style="height:5%;width:10px;" /><hr style="width:10px;" />'
|
|
|
|
);
|
|
|
|
|
2007-03-31 02:58:16 +00:00
|
|
|
// link types
|
|
|
|
$this->assertResult(
|
|
|
|
'<a href="foo" rel="nofollow" />',
|
|
|
|
true,
|
|
|
|
array('Attr.AllowedRel' => 'nofollow')
|
|
|
|
);
|
|
|
|
|
2007-04-30 19:39:42 +00:00
|
|
|
// border
|
|
|
|
$this->assertResult(
|
|
|
|
'<img src="foo" alt="foo" hspace="1" vspace="3" />',
|
|
|
|
'<img src="foo" alt="foo" style="margin-top:3px;margin-bottom:3px;margin-left:1px;margin-right:1px;" />',
|
|
|
|
array('Attr.AllowedRel' => 'nofollow')
|
|
|
|
);
|
|
|
|
|
2007-04-30 21:19:15 +00:00
|
|
|
// link targets
|
|
|
|
$this->assertResult(
|
|
|
|
'<a href="foo" target="_top" />',
|
|
|
|
true,
|
|
|
|
array('Attr.AllowedFrameTargets' => '_top')
|
|
|
|
);
|
|
|
|
$this->assertResult(
|
|
|
|
'<a href="foo" target="_top" />',
|
|
|
|
'<a href="foo" />'
|
|
|
|
);
|
|
|
|
$this->assertResult(
|
|
|
|
'<a href="foo" target="_top" />',
|
|
|
|
'<a href="foo" />',
|
|
|
|
array('Attr.AllowedFrameTargets' => '_top', 'HTML.Strict' => true)
|
|
|
|
);
|
2006-07-30 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-20 03:48:39 +00:00
|
|
|
?>
|