Completed
Push — master ( e0ac7a...8eddfc )
by Kevin
03:21
created

Picture::doClean()   C

Complexity

Conditions 9
Paths 2

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 47
ccs 0
cts 34
cp 0
rs 5.2941
cc 9
eloc 24
nc 2
nop 1
crap 90
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 doClean(LoggerInterface $logger)
22
    {
23
        // Children can consist of zero or more "source" elements,
24
        // followed by one "img" element, optionally intermixed
25
        // with script-supporting elements.
26
        if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
27
            $encounteredImgElement = false;
28
            foreach ($this->children as $child) {
29
                if ($child->getType() == Token::COMMENT) {
30
                    continue;
31
                }
32
33
                if ($child->getType() !== Token::ELEMENT) {
34
                    $logger->debug('Removing ' . $child);
35
                    $this->removeChild($child);
36
37
                    continue;
38
                }
39
40
                if ($child instanceof ScriptSupporting) {
41
                    continue;
42
                }
43
44
                if ($child->getName() == 'img') {
45
                    if ($encounteredImgElement) {
46
                        $logger->debug('Removing ' . $child . '. Only one "img" element allowed as child of "picture" element.');
47
                        $this->removeChild($child);
48
49
                        continue;
50
                    }
51
52
                    $encounteredImgElement = true;
53
54
                    continue;
55
                }
56
57
                if ($child->getName() != 'source') {
58
                    $logger->debug('Removing ' . $child . '. Only one "img" element and zero to many "source" elements allowed as children of the "picture" element.');
59
                    $this->removeChild($child);
60
61
                    continue;
62
                }
63
            }
64
        }
65
66
        return true;
67
    }
68
}
69