1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Attribute; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
7
|
|
|
use Groundskeeper\Tokens\NonParticipating; |
8
|
|
|
use Groundskeeper\Tokens\Text; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* "option" element |
13
|
|
|
* |
14
|
|
|
* https://html.spec.whatwg.org/multipage/forms.html#the-option-element |
15
|
|
|
*/ |
16
|
|
|
class Option extends OpenElement |
17
|
|
|
{ |
18
|
5 |
|
protected function getAllowedAttributes() |
19
|
|
|
{ |
20
|
|
|
$optionAllowedAttributes = array( |
21
|
5 |
|
'/^disabled$/i' => Attribute::BOOL, |
22
|
|
|
'/^label$/i' => Attribute::CS_STRING, |
23
|
|
|
'/^selected$/i' => Attribute::BOOL, |
24
|
|
|
'/^value$/i' => Attribute::CS_STRING |
25
|
|
|
); |
26
|
|
|
|
27
|
5 |
|
return array_merge( |
28
|
5 |
|
$optionAllowedAttributes, |
29
|
5 |
|
parent::getAllowedAttributes() |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
protected function removeInvalidChildren(LoggerInterface $logger) |
34
|
|
|
{ |
35
|
4 |
|
if ($this->hasAttribute('label')) { |
36
|
2 |
|
if ($this->hasAttribute('value')) { |
37
|
|
|
// If both the "label" and "value" attributes are present, |
38
|
|
|
// then no content is allowed. |
39
|
2 |
|
foreach ($this->children as $child) { |
40
|
2 |
|
if ($child instanceof NonParticipating) { |
41
|
2 |
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
$logger->debug('Removing ' . $child . '. No content allowed inside an "option" element that contains both "label" and "value" attribute.'); |
45
|
2 |
|
$this->removeChild($child); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
// If the "label" and not "value" attribute is present, |
49
|
|
|
// then onyl text is allowed. |
50
|
2 |
|
foreach ($this->children as $child) { |
51
|
2 |
|
if ($child instanceof NonParticipating || |
52
|
2 |
|
$child instanceof Text) { |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that contains only a "label" attribute.'); |
57
|
2 |
|
$this->removeChild($child); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
// If no "label" attribute is present, |
62
|
|
|
// then only text is allowed. |
63
|
4 |
|
foreach ($this->children as $child) { |
64
|
4 |
|
if ($child instanceof NonParticipating || |
65
|
4 |
|
$child instanceof Text) { |
66
|
4 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that does not contain a "label" attribute.'); |
70
|
1 |
|
$this->removeChild($child); |
71
|
|
|
} |
72
|
|
|
} |
73
|
4 |
|
} |
74
|
|
|
|
75
|
5 |
|
protected function removeInvalidSelf(LoggerInterface $logger) : bool |
76
|
|
|
{ |
77
|
5 |
|
$parent = $this->getParent(); |
78
|
5 |
|
if ($parent !== null && |
79
|
5 |
|
!$parent instanceof Select && |
80
|
5 |
|
!$parent instanceof Datalist && |
81
|
5 |
|
!$parent instanceof Optgroup) { |
82
|
1 |
|
$logger->debug('Removing ' . $this . '. Only "select", "datalist", and "optgroup" elements allowed as parents of "option" element.'); |
83
|
|
|
|
84
|
1 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|