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
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
2006-10-01 21:55:13 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->obj = new HTMLPurifier_Strategy_ValidateAttributes();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testEmptyInput()
|
|
|
|
{
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult('');
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testSelectivelyRemoveInvalidClasses()
|
|
|
|
{
|
2009-05-26 05:07:40 +00:00
|
|
|
$this->config->set('HTML.Doctype', 'XHTML 1.1');
|
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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testOnlyXMLLangInXHTML11()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testBasicURI()
|
|
|
|
{
|
2007-08-06 06:22:23 +00:00
|
|
|
$this->assertResult('<a href="http://www.google.com/">Google</a>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testBdoAlternateDefaultDir()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testColSpanIsNonZero()
|
|
|
|
{
|
2007-05-05 16:18:04 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<col span="0" />',
|
|
|
|
'<col />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testImgAddDefaults()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public 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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testImgAddDefaultSrc()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testImgRemoveNonRetrievableProtocol()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testPreserveRel()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('Attr.AllowedRel', 'nofollow');
|
2007-08-06 06:22:23 +00:00
|
|
|
$this->assertResult('<a href="foo" rel="nofollow" />');
|
2007-05-05 16:18:04 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testPreserveTarget()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('Attr.AllowedFrameTargets', '_top');
|
|
|
|
$this->config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
|
2017-01-13 16:21:24 +00:00
|
|
|
$this->assertResult('<a href="foo" target="_top" rel="noreferrer noopener" />');
|
2006-07-30 18:37:42 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testRemoveTargetWhenNotSupported()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$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-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testKeepAbsoluteCSSWidthAndHeightOnImg()
|
|
|
|
{
|
2008-04-22 22:28:54 +00:00
|
|
|
$this->assertResult(
|
2008-05-21 01:56:48 +00:00
|
|
|
'<img src="" alt="" style="width:10px;height:10px;border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testRemoveLargeCSSWidthAndHeightOnImg()
|
|
|
|
{
|
2008-05-21 01:56:48 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />',
|
|
|
|
'<img src="" alt="" style="border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testRemoveLargeCSSWidthAndHeightOnImgWithUserConf()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('CSS.MaxImgLength', '1px');
|
2008-05-21 01:56:48 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:1mm;height:1mm;border:1px solid #000;" />',
|
|
|
|
'<img src="" alt="" style="border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testKeepLargeCSSWidthAndHeightOnImgWhenToldTo()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('CSS.MaxImgLength', null);
|
2008-05-21 01:56:48 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testKeepPercentCSSWidthAndHeightOnImgWhenToldTo()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('CSS.MaxImgLength', null);
|
2008-05-23 02:09:43 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testRemoveRelativeCSSWidthAndHeightOnImg()
|
|
|
|
{
|
2008-05-21 01:56:48 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:10em;height:10em;border:1px solid #000;" />',
|
|
|
|
'<img src="" alt="" style="border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testRemovePercentCSSWidthAndHeightOnImg()
|
|
|
|
{
|
2008-05-21 01:56:48 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />',
|
2008-04-22 22:28:54 +00:00
|
|
|
'<img src="" alt="" style="border:1px solid #000;" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2022-09-12 14:53:24 +00:00
|
|
|
public function testContentEditableAttribute()
|
|
|
|
{
|
|
|
|
$this->assertResult(
|
|
|
|
'<div contenteditable="false"></div>',
|
|
|
|
'<div contenteditable="false"></div>'
|
|
|
|
);
|
|
|
|
}
|
2006-07-30 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|