1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\EmbeddedContent; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\FlowContent; |
8
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
9
|
|
|
use Groundskeeper\Tokens\ElementTypes\PhrasingContent; |
10
|
|
|
use Groundskeeper\Tokens\ElementTypes\ScriptSupporting; |
11
|
|
|
use Groundskeeper\Tokens\Token; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* "picture" element |
16
|
|
|
* |
17
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#the-picture-element |
18
|
|
|
*/ |
19
|
|
|
class Picture extends OpenElement implements FlowContent, PhrasingContent, EmbeddedContent |
20
|
|
|
{ |
21
|
|
|
protected function removeInvalidChildren(LoggerInterface $logger) |
22
|
|
|
{ |
23
|
|
|
$encounteredImgElement = false; |
24
|
|
|
foreach ($this->children as $child) { |
25
|
|
|
if ($child->getType() == Token::COMMENT) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($child->getType() !== Token::ELEMENT) { |
30
|
|
|
$logger->debug('Removing ' . $child); /// @todo better message |
31
|
|
|
$this->removeChild($child); |
32
|
|
|
|
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($child instanceof ScriptSupporting) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($child->getName() == 'img') { |
41
|
|
|
if ($encounteredImgElement) { |
42
|
|
|
$logger->debug('Removing ' . $child . '. Only one "img" element allowed as child of "picture" element.'); |
43
|
|
|
$this->removeChild($child); |
44
|
|
|
|
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$encounteredImgElement = true; |
49
|
|
|
|
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
if ($child->getName() != 'source') { |
|
|
|
|
54
|
|
|
$logger->debug('Removing ' . $child . '. Only one "img" element and zero to many "source" elements allowed as children of the "picture" element.'); |
55
|
|
|
$this->removeChild($child); |
56
|
|
|
|
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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.