-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* Keywords that are body-ok affect whether link elements are allowed in the body. | ||
* The body-ok keywords are dns-prefetch, modulepreload, pingback, preconnect, prefetch, preload, prerender, | ||
* and stylesheet. | ||
* | ||
* https://html.spec.whatwg.org/multipage/links.html#body-ok | ||
* | ||
* @note We cannot use Enum because multiple values are allowed. | ||
*/ | ||
class HTMLPurifier_AttrDef_HTML_LinkRel extends HTMLPurifier_AttrDef_HTML5_ARel | ||
{ | ||
/** | ||
* Lookup table for valid values | ||
* @var array | ||
*/ | ||
protected static $values = array( | ||
'dns-prefetch' => true, | ||
'modulepreload' => true, | ||
'pingback' => true, | ||
'preconnect' => true, | ||
'prefetch' => true, | ||
'preload' => true, | ||
'prerender' => true, | ||
'stylesheet' => true, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* https://html.spec.whatwg.org/dev/semantics.html#the-link-element | ||
*/ | ||
class HTMLPurifier_HTMLModule_HTML5_Link extends HTMLPurifier_HTMLModule | ||
{ | ||
/** | ||
* @type string | ||
*/ | ||
public $name = 'HTML5_Link'; | ||
|
||
/** | ||
* @param HTMLPurifier_Config $config | ||
*/ | ||
public function setup($config) | ||
{ | ||
$allowed = $config->get('HTML.Link'); | ||
|
||
// https://html.spec.whatwg.org/dev/semantics.html#the-link-element | ||
$this->addElement('link', 'Flow', 'Empty', null, array( | ||
'rel*' => new HTMLPurifier_AttrDef_HTML_LinkRel, | ||
'type' => new HTMLPurifier_AttrDef_Enum(array('text/css')), | ||
'href*' => new HTMLPurifier_AttrDef_Enum($allowed, true), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
class HTMLPurifier_HTMLModule_HTML5_LinkTest extends BaseTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->config->set('Attr.AllowedRel', array('stylesheet', 'preload')); | ||
$this->config->set('HTML.Link', array('https://localhost/foo.css')); | ||
} | ||
|
||
public function testClosingTag() | ||
{ | ||
$this->assertPurification( | ||
'<link></link>', | ||
'' | ||
); | ||
} | ||
|
||
public function testSelfClosing() | ||
{ | ||
$this->assertPurification( | ||
'<link>', | ||
'' | ||
); | ||
} | ||
|
||
public function testNoHref() | ||
{ | ||
$this->assertPurification( | ||
'<link rel="stylesheet">', | ||
'' | ||
); | ||
} | ||
|
||
public function testNoRel() | ||
{ | ||
$this->assertPurification( | ||
'<link href="foo">', | ||
'' | ||
); | ||
} | ||
|
||
public function testInvalidUrl() | ||
{ | ||
$this->assertPurification( | ||
'<link href="foo" rel="stylesheet">', | ||
'' | ||
); | ||
} | ||
|
||
public function testGood() | ||
{ | ||
$this->assertPurification( | ||
'<link href="https://localhost/foo.css" rel="stylesheet">', | ||
'<link href="https://localhost/foo.css" rel="stylesheet">' | ||
); | ||
} | ||
|
||
public function testWhitelistCapitalised() | ||
{ | ||
$this->assertPurification( | ||
'<link href="https://localhost/FOO.css" rel="stylesheet">', | ||
'' | ||
); | ||
} | ||
|
||
public function testGoodWithAutoclosedTag() | ||
{ | ||
$this->assertPurification( | ||
'<link href="https://localhost/foo.css" rel="stylesheet"></link>', | ||
'<link href="https://localhost/foo.css" rel="stylesheet">' | ||
); | ||
|
||
$this->assertPurification( | ||
'<link href="https://localhost/foo.css" rel="stylesheet" />', | ||
'<link href="https://localhost/foo.css" rel="stylesheet">' | ||
); | ||
} | ||
|
||
public function testMultiRel() | ||
{ | ||
$this->assertPurification( | ||
'<link href="https://localhost/foo.css" rel="stylesheet preload">', | ||
'<link href="https://localhost/foo.css" rel="stylesheet preload">' | ||
); | ||
} | ||
} |