1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Tokens\Element; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\FlowContent; |
8
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
9
|
|
|
use Groundskeeper\Tokens\NonParticipating; |
10
|
|
|
use Groundskeeper\Tokens\Token; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* "menu" element |
15
|
|
|
* |
16
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#the-menu-element |
17
|
|
|
*/ |
18
|
|
|
class Menu extends OpenElement implements FlowContent |
19
|
|
|
{ |
20
|
1 |
|
protected function getAllowedAttributes() |
21
|
|
|
{ |
22
|
|
|
$menuAllowedAttributes = array( |
23
|
1 |
|
'/^type$/i' => Element::ATTR_CI_ENUM . '("","context","toolbar"|"context")', |
24
|
|
|
'/^label$/i' => Element::ATTR_CS_STRING |
25
|
1 |
|
); |
26
|
|
|
|
27
|
1 |
|
return array_merge( |
28
|
1 |
|
$menuAllowedAttributes, |
29
|
1 |
|
parent::getAllowedAttributes() |
30
|
1 |
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
View Code Duplication |
protected function fixSelf(LoggerInterface $logger) |
|
|
|
|
34
|
|
|
{ |
35
|
1 |
|
if (!$this->hasAttribute('type')) { |
36
|
1 |
|
$logger->debug('Modifying ' . $this . '. Adding the default "type" attribute for the "menu" element.'); |
37
|
1 |
|
$this->addAttribute('type', 'context'); |
38
|
1 |
|
} |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
protected function removeInvalidChildren(LoggerInterface $logger) |
42
|
|
|
{ |
43
|
|
|
// Only "li" and ScriptSupporting elements allowed. |
44
|
1 |
|
foreach ($this->children as $child) { |
45
|
1 |
|
if ($child instanceof NonParticipating) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if (!$child instanceof Li && |
50
|
1 |
|
!$child instanceof Menuitem && |
|
|
|
|
51
|
1 |
|
!$child instanceof Hr && |
52
|
1 |
|
!$child instanceof Menu && |
53
|
1 |
|
!$child instanceof ScriptSupporting) { |
|
|
|
|
54
|
|
|
$logger->debug('Removing ' . $child . '. Only elements "li", "menuitem", "hr", "menu", and script supporting elements allowed as children of "menu" element.'); |
55
|
|
|
$this->removeChild($child); |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
1 |
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.