1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Tokens\Element; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\ClosedElement; |
8
|
|
|
use Groundskeeper\Tokens\ElementTypes\MetadataContent; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class Link extends ClosedElement implements MetadataContent |
12
|
|
|
{ |
13
|
6 |
View Code Duplication |
protected function getAllowedAttributes() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$linkAllowedAttributes = array( |
16
|
6 |
|
'/^href$/i' => Element::ATTR_URI, |
17
|
6 |
|
'/^crossorigin$/i' => Element::ATTR_CS_STRING, |
18
|
6 |
|
'/^rel$/i' => Element::ATTR_CI_SSENUM . '("alternate","author","help","icon","license","next","pingback","prefetch","prev","search","stylesheet")', |
19
|
6 |
|
'/^media$/i' => Element::ATTR_CI_STRING, |
20
|
6 |
|
'/^hreflang$/i' => Element::ATTR_CS_STRING, |
21
|
6 |
|
'/^type$/i' => Element::ATTR_CI_STRING, |
22
|
|
|
'/^sizes$/i' => Element::ATTR_CI_STRING |
23
|
6 |
|
); |
24
|
|
|
|
25
|
6 |
|
return array_merge( |
26
|
6 |
|
$linkAllowedAttributes, |
27
|
6 |
|
parent::getAllowedAttributes() |
28
|
6 |
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
protected function doClean(LoggerInterface $logger) |
32
|
|
|
{ |
33
|
6 |
|
if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) { |
34
|
|
|
// Must have "href" attribute. |
35
|
6 |
|
if (!$this->hasAttribute('href')) { |
36
|
1 |
|
$logger->debug('Element "link" requires "href" attribute.'); |
37
|
|
|
|
38
|
1 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Must have either "rel" or "itemprop" attribute, but not both. |
42
|
5 |
|
$attrCount = 0; |
43
|
5 |
|
foreach ($this->attributes as $key => $value) { |
44
|
5 |
|
if ($key == 'rel' || $key == 'itemprop') { |
45
|
4 |
|
$attrCount++; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
5 |
|
if ($attrCount > 1) { |
49
|
|
|
// If both, then we don't know which one should be kept, |
50
|
|
|
// so we recommend to delete the entire element. |
51
|
1 |
|
$logger->debug('Element "link" requires either "rel" or "itemprop" attribute, but not both.'); |
52
|
|
|
|
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
5 |
|
} |
56
|
|
|
|
57
|
4 |
|
if ($attrCount == 0) { |
58
|
1 |
|
$logger->debug('Element "link" requires either "rel" or "itemprop" attribute.'); |
59
|
|
|
|
60
|
1 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// If inside "body" element, then we check if allowed. |
64
|
3 |
|
$body = new Body($this->configuration, 'body'); |
65
|
3 |
|
if ($this->hasAncestor($body) && !$this->isAllowedInBody()) { |
66
|
1 |
|
$logger->debug('Element "link" does not have the correct attributes to be allowed inside the "body" element.'); |
67
|
|
|
|
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
6 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Is this element allowed inside a body element? |
77
|
|
|
* |
78
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#allowed-in-the-body |
79
|
|
|
* |
80
|
|
|
* @return boolean True if allowed. |
81
|
|
|
*/ |
82
|
2 |
|
public function isAllowedInBody() |
83
|
|
|
{ |
84
|
2 |
|
if ($this->hasAttribute('itemprop')) { |
85
|
1 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
if ($this->hasAttribute('rel') && |
89
|
2 |
|
($this->attributes['rel'] == 'pingback' || |
90
|
2 |
|
$this->attributes['rel'] == 'prefetch' || |
91
|
2 |
|
$this->attributes['rel'] == 'stylesheet')) { |
92
|
1 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.