Skip to content

Commit bf8168a

Browse files
authored
Refactor attributes validation for <progress> element (#23)
1 parent efe17ab commit bf8168a

File tree

5 files changed

+75
-45
lines changed

5 files changed

+75
-45
lines changed

library/HTMLPurifier/AttrDef/HTML/ProgressValue.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Post-transform performing validations for <progress> elements ensuring
5+
* that if value is present, it is within a valid range (0..1) or (0..max)
6+
*/
7+
class HTMLPurifier_AttrTransform_Progress extends HTMLPurifier_AttrTransform
8+
{
9+
/**
10+
* @param array $attr
11+
* @param HTMLPurifier_Config $config
12+
* @param HTMLPurifier_Context $context
13+
* @return array
14+
*/
15+
public function transform($attr, $config, $context)
16+
{
17+
if (isset($attr['value'])) {
18+
$max = isset($attr['max']) ? (float) $attr['max'] : 1;
19+
$value = (float) $attr['value'];
20+
21+
if ($value < 0 || $value > $max) {
22+
$this->confiscateAttr($attr, 'value');
23+
}
24+
}
25+
26+
return $attr;
27+
}
28+
}

library/HTMLPurifier/HTML5Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class HTMLPurifier_HTML5Config extends HTMLPurifier_Config
44
{
5-
const REVISION = 2018061501;
5+
const REVISION = 2018061701;
66

77
/**
88
* @param string|array|HTMLPurifier_Config $config

library/HTMLPurifier/HTML5Definition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public static function setup(HTMLPurifier_HTMLDefinition $def)
117117
$def->addElement('summary', false, 'Flow', 'Common');
118118

119119
// https://html.spec.whatwg.org/dev/form-elements.html#the-progress-element
120-
$def->manager->attrTypes->set('ProgressValue', new HTMLPurifier_AttrDef_HTML_ProgressValue());
121-
$def->addElement('progress', 'Flow', new HTMLPurifier_ChildDef_Progress(), 'Common', array(
122-
'value' => 'ProgressValue',
120+
$progress = $def->addElement('progress', 'Flow', new HTMLPurifier_ChildDef_Progress(), 'Common', array(
121+
'value' => 'Float#min:0',
123122
'max' => 'Float#min:0',
124123
));
124+
$progress->attr_transform_post[] = new HTMLPurifier_AttrTransform_Progress();
125125
$def->getAnonymousModule()->addElementToContentSet('progress', 'Inline');
126126

127127
return $def;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
class HTMLPurifier_AttrTransform_ProgressTest extends PHPUnit_Framework_TestCase
4+
{
5+
/**
6+
* @var HTMLPurifier_HTML5Config
7+
*/
8+
protected $config;
9+
10+
/**
11+
* @var HTMLPurifier_Context
12+
*/
13+
protected $context;
14+
15+
/**
16+
* @var HTMLPurifier_AttrTransform_Progress
17+
*/
18+
protected $progress;
19+
20+
public function setUp()
21+
{
22+
$this->config = HTMLPurifier_HTML5Config::createDefault();
23+
$this->context = new HTMLPurifier_Context();
24+
$this->progress = new HTMLPurifier_AttrTransform_Progress();
25+
}
26+
27+
protected function assertTransform($expected, array $input)
28+
{
29+
$this->assertEquals($expected, $this->progress->transform($input, $this->config, $this->context));
30+
}
31+
32+
public function testTransform()
33+
{
34+
$this->assertTransform(array(), array());
35+
$this->assertTransform(array('value' => 0), array('value' => 0));
36+
$this->assertTransform(array('value' => 1), array('value' => 1));
37+
$this->assertTransform(array(), array('value' => 10));
38+
$this->assertTransform(array(), array('value' => -1));
39+
40+
$this->assertTransform(array('value' => 10, 'max' => 10), array('value' => 10, 'max' => 10));
41+
$this->assertTransform(array('max' => 10), array('value' => 100, 'max' => 10));
42+
}
43+
}

0 commit comments

Comments
 (0)