Skip to content

Commit c69bde1

Browse files
committed
Merge pull request #84 from Masterminds/boolean-attribiutes
Improved boolean attribute handling
2 parents 6222ad4 + e36b070 commit c69bde1

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

src/HTML5/Serializer/OutputRules.php

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,86 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface
7373
7474
'nodeName'=>'img', 'nodeName'=>array('img', 'a'),
7575
'attrName'=>'alt', 'attrName'=>array('title', 'alt'),
76-
77-
78-
'prefixes'=>['xh'=>'http://www.w3.org/1999/xhtml'),
79-
'xpath' => "@checked[../../xh:input[@type='radio' or @type='checkbox']]",
8076
),
8177
*/
8278
array(
83-
'nodeNamespace'=>'http://www.w3.org/1999/xhtml',
84-
'attrName'=>array('alt', 'title'),
79+
'nodeNamespace' => 'http://www.w3.org/1999/xhtml',
80+
'attrName' => array('href',
81+
'hreflang',
82+
'http-equiv',
83+
'icon',
84+
'id',
85+
'keytype',
86+
'kind',
87+
'label',
88+
'lang',
89+
'language',
90+
'list',
91+
'maxlength',
92+
'media',
93+
'method',
94+
'name',
95+
'placeholder',
96+
'rel',
97+
'rows',
98+
'rowspan',
99+
'sandbox',
100+
'spellcheck',
101+
'scope',
102+
'seamless',
103+
'shape',
104+
'size',
105+
'sizes',
106+
'span',
107+
'src',
108+
'srcdoc',
109+
'srclang',
110+
'srcset',
111+
'start',
112+
'step',
113+
'style',
114+
'summary',
115+
'tabindex',
116+
'target',
117+
'title',
118+
'type',
119+
'value',
120+
'width',
121+
'border',
122+
'charset',
123+
'cite',
124+
'class',
125+
'code',
126+
'codebase',
127+
'color',
128+
'cols',
129+
'colspan',
130+
'content',
131+
'coords',
132+
'data',
133+
'datetime',
134+
'default',
135+
'dir',
136+
'dirname',
137+
'enctype',
138+
'for',
139+
'form',
140+
'formaction',
141+
'headers',
142+
'height',
143+
'accept',
144+
'accept-charset',
145+
'accesskey',
146+
'action',
147+
'align',
148+
'alt',
149+
'bgcolor',
150+
),
151+
),
152+
array(
153+
'nodeNamespace' => 'http://www.w3.org/1999/xhtml',
154+
'xpath' => 'starts-with(local-name(), \'data-\')',
85155
),
86-
87156
);
88157

89158
const DOCTYPE = '<!DOCTYPE html>';
@@ -328,7 +397,7 @@ protected function nonBooleanAttribute(\DOMAttr $attr)
328397
$xp->registerNamespace($nsPrefix, $ns);
329398
}
330399
}
331-
if(!$xp->query($rule['xpath'], $attr->ownerElement)->length){
400+
if(!$xp->evaluate($rule['xpath'], $attr)){
332401
continue;
333402
}
334403
}

test/HTML5/Serializer/OutputRulesTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,10 @@ public function booleanAttributes()
460460
array('<input type="radio" readonly>'),
461461
array('<input type="radio" checked disabled>'),
462462
array('<input type="checkbox" checked disabled>'),
463+
array('<input type="radio" value="" checked disabled>'),
464+
array('<div data-value=""></div>'),
463465
array('<select disabled></select>'),
464-
array('<div ng-app>foo</div>'),
466+
array('<div ng-app></div>'),
465467
array('<script defer></script>'),
466468
);
467469
}
@@ -482,7 +484,11 @@ public function testBooleanAttrs($html)
482484
$m->invoke($r, $node);
483485

484486
$content = stream_get_contents($stream, - 1, 0);
485-
$this->assertContains($content, $html);
487+
488+
$html = preg_replace('~<[a-z]+(.*)></[a-z]+>~', '\1', $html);
489+
$html = preg_replace('~<[a-z]+(.*)/?>~', '\1', $html);
490+
491+
$this->assertEquals($content, $html);
486492

487493
}
488494

0 commit comments

Comments
 (0)