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\InlineElement; |
9
|
|
|
use Groundskeeper\Tokens\ElementTypes\InteractiveContent; |
10
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
11
|
|
|
use Groundskeeper\Tokens\ElementTypes\PhrasingContent; |
12
|
|
|
use Groundskeeper\Tokens\Token; |
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 |
21
|
|
|
{ |
22
|
5 |
View Code Duplication |
protected function getAllowedAttributes() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$aAllowedAttributes = array( |
25
|
5 |
|
'/^href$/i' => Element::ATTR_URI, |
26
|
5 |
|
'/^target$/i' => Element::ATTR_CS_STRING, |
27
|
5 |
|
'/^download$/i' => Element::ATTR_CS_STRING, |
28
|
5 |
|
'/^ping$/i' => Element::ATTR_URI, |
29
|
5 |
|
'/^rel$/i' => Element::ATTR_CS_STRING, |
30
|
5 |
|
'/^hreflang$/i' => Element::ATTR_CS_STRING, |
31
|
5 |
|
'/^type$/i' => Element::ATTR_CS_STRING, |
32
|
|
|
'/^referrerpolicy$/i' => Element::ATTR_CS_STRING |
33
|
5 |
|
); |
34
|
|
|
|
35
|
5 |
|
return array_merge( |
36
|
5 |
|
$aAllowedAttributes, |
37
|
5 |
|
parent::getAllowedAttributes() |
38
|
5 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
protected function doClean(LoggerInterface $logger) |
42
|
|
|
{ |
43
|
|
|
// If the "itemprop" attribute is specified on an "a" element, then |
44
|
|
|
// the "href" attribute must also be specified. |
45
|
5 |
|
if ($this->hasAttribute('itemprop') && !$this->hasAttribute('href')) { |
46
|
1 |
|
$logger->debug('Element "a" with "itemprop" attribute requires the "href" attribute also. Adding empty "href" attribute.'); |
47
|
1 |
|
$this->addAttribute('href', ''); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
5 |
|
if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) { |
51
|
|
|
// There must be no interactive content or "a" element descendants. |
52
|
5 |
|
foreach ($this->children as $child) { |
53
|
5 |
|
if ($child->getType() == Token::COMMENT) { |
54
|
1 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
if ($child->getType() == Token::ELEMENT && |
58
|
1 |
|
($child->getName() == 'a' || |
59
|
5 |
|
($child instanceof InteractiveContent && $child->isInteractiveContent()))) { |
60
|
1 |
|
$logger->debug('Removing ' . $child . '. Element "a" cannot contain "a" or interactive content elements.'); |
61
|
1 |
|
$this->removeChild($child); |
62
|
1 |
|
} |
63
|
5 |
|
} |
64
|
|
|
|
65
|
|
|
// The target, download, ping, rel, hreflang, type, and |
66
|
|
|
// referrerpolicy attributes must be omitted if the href |
67
|
|
|
// attribute is not present. |
68
|
5 |
|
if (!$this->hasAttribute('href')) { |
69
|
1 |
|
if ($this->hasAttribute('target') || |
70
|
1 |
|
$this->hasAttribute('download') || |
71
|
1 |
|
$this->hasAttribute('ping') || |
72
|
1 |
|
$this->hasAttribute('rel') || |
73
|
1 |
|
$this->hasAttribute('hreflang') || |
74
|
1 |
|
$this->hasAttribute('type') || |
75
|
1 |
|
$this->hasAttribute('referrerpolicy')) { |
76
|
1 |
|
$logger->debug('Removing invalid attributes. Element "a" without "href" attribute cannot contain "target", "download", "ping", "rel", "hreflang", "type", or "referrerpolicy" attributes.'); |
77
|
1 |
|
$this->removeAttribute('target'); |
78
|
1 |
|
$this->removeAttribute('download'); |
79
|
1 |
|
$this->removeAttribute('ping'); |
80
|
1 |
|
$this->removeAttribute('rel'); |
81
|
1 |
|
$this->removeAttribute('hreflang'); |
82
|
1 |
|
$this->removeAttribute('type'); |
83
|
1 |
|
$this->removeAttribute('referrerpolicy'); |
84
|
1 |
|
} |
85
|
1 |
|
} |
86
|
5 |
|
} |
87
|
|
|
|
88
|
5 |
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isInteractiveContent() |
92
|
|
|
{ |
93
|
|
|
return $this->hasAttribute('href'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.