1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Attribute; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\FlowContent; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\InlineElement; |
8
|
|
|
use Groundskeeper\Tokens\ElementTypes\InteractiveContent; |
9
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
10
|
|
|
use Groundskeeper\Tokens\ElementTypes\PhrasingContent; |
11
|
|
|
use Groundskeeper\Tokens\ElementTypes\TransparentElement; |
12
|
|
|
use Groundskeeper\Tokens\NonParticipating; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* "a" element |
17
|
|
|
* |
18
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#the-a-element |
19
|
|
|
*/ |
20
|
|
|
class A extends OpenElement implements FlowContent, InteractiveContent, PhrasingContent, InlineElement, TransparentElement |
21
|
|
|
{ |
22
|
15 |
|
protected function getAllowedAttributes() |
23
|
|
|
{ |
24
|
|
|
$aAllowedAttributes = array( |
25
|
15 |
|
'/^href$/i' => Attribute::URI, |
26
|
|
|
'/^target$/i' => Attribute::CS_STRING, |
27
|
|
|
'/^download$/i' => Attribute::CS_STRING, |
28
|
|
|
'/^ping$/i' => Attribute::URI, |
29
|
|
|
'/^rel$/i' => Attribute::CS_STRING, |
30
|
|
|
'/^hreflang$/i' => Attribute::CS_STRING, |
31
|
|
|
'/^type$/i' => Attribute::CS_STRING, |
32
|
|
|
'/^referrerpolicy$/i' => Attribute::CS_STRING |
33
|
|
|
); |
34
|
|
|
|
35
|
15 |
|
return array_merge( |
36
|
15 |
|
$aAllowedAttributes, |
37
|
15 |
|
parent::getAllowedAttributes() |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
15 |
View Code Duplication |
protected function fixSelf(LoggerInterface $logger) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
// If the "itemprop" attribute is specified on an "a" element, then |
44
|
|
|
// the "href" attribute must also be specified. |
45
|
15 |
|
if ($this->hasAttribute('itemprop') && !$this->hasAttribute('href')) { |
46
|
1 |
|
$logger->debug($this . ' with "itemprop" attribute requires the "href" attribute also. Adding empty "href" attribute.'); |
47
|
1 |
|
$this->addAttribute('href', ''); |
48
|
|
|
} |
49
|
15 |
|
} |
50
|
|
|
|
51
|
10 |
|
protected function removeInvalidChildren(LoggerInterface $logger) |
52
|
|
|
{ |
53
|
|
|
// There must be no interactive content or "a" element descendants. |
54
|
10 |
|
foreach ($this->children as $child) { |
55
|
10 |
|
if ($child instanceof NonParticipating) { |
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
if ($child instanceof self && |
60
|
10 |
|
($child instanceof InteractiveContent && $child->isInteractiveContent())) { |
61
|
1 |
|
$logger->debug('Removing ' . $child . '. Element "a" cannot contain "a" or interactive content elements.'); |
62
|
10 |
|
$this->removeChild($child); |
63
|
|
|
} |
64
|
|
|
} |
65
|
10 |
|
} |
66
|
|
|
|
67
|
10 |
|
protected function removeInvalidSelf(LoggerInterface $logger) : bool |
68
|
|
|
{ |
69
|
|
|
// The target, download, ping, rel, hreflang, type, and |
70
|
|
|
// referrerpolicy attributes must be omitted if the href |
71
|
|
|
// attribute is not present. |
72
|
10 |
|
if (!$this->hasAttribute('href')) { |
73
|
1 |
|
if ($this->hasAttribute('target') || |
74
|
1 |
|
$this->hasAttribute('download') || |
75
|
1 |
|
$this->hasAttribute('ping') || |
76
|
1 |
|
$this->hasAttribute('rel') || |
77
|
1 |
|
$this->hasAttribute('hreflang') || |
78
|
1 |
|
$this->hasAttribute('type') || |
79
|
1 |
|
$this->hasAttribute('referrerpolicy') |
80
|
|
|
) { |
81
|
1 |
|
$logger->debug('Removing invalid attributes. ' . $this . ' without "href" attribute cannot contain "target", "download", "ping", "rel", "hreflang", "type", or "referrerpolicy" attributes.'); |
82
|
1 |
|
$this->removeAttribute('target'); |
83
|
1 |
|
$this->removeAttribute('download'); |
84
|
1 |
|
$this->removeAttribute('ping'); |
85
|
1 |
|
$this->removeAttribute('rel'); |
86
|
1 |
|
$this->removeAttribute('hreflang'); |
87
|
1 |
|
$this->removeAttribute('type'); |
88
|
1 |
|
$this->removeAttribute('referrerpolicy'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
10 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function isInteractiveContent() : bool |
96
|
|
|
{ |
97
|
2 |
|
return $this->hasAttribute('href'); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
public function isTransparentElement() : bool |
101
|
|
|
{ |
102
|
3 |
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.