Completed
Push — master ( 45904d...9fbe45 )
by Kevin
03:18
created

Li::doClean()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 5.3846
cc 8
eloc 14
nc 6
nop 1
crap 8
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Tokens\Element;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\Token;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * "li" element
13
 *
14
 * https://html.spec.whatwg.org/multipage/semantics.html#the-li-element
15
 */
16
class Li extends OpenElement
17
{
18 2
    protected function getAllowedAttributes()
19
    {
20
        // "value" attribute for "li" which are children of "ol" elements.
21 2
        $parent = $this->getParent();
22 2
        if ($parent == null) {
23 1
            return parent::getAllowedAttributes();
24
        }
25
26 2
        if ($parent->getType() === Token::ELEMENT &&
27 2
            $parent->getName() == 'ol') {
28
            $liAllowedAttributes = array(
29
                '/^value$/i' => Element::ATTR_INT
30 1
            );
31
32 1
            return array_merge(
33 1
                $liAllowedAttributes,
34 1
                parent::getAllowedAttributes()
35 1
            );
36
        }
37
38 1
        return parent::getAllowedAttributes();
39
    }
40
41 4
    protected function doClean(LoggerInterface $logger)
42
    {
43 4
        if ($this->configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_LENIENT) {
44 4
            return true;
45
        }
46
47
        // Only allowed:
48
        // Inside ol elements.
49
        // Inside ul elements.
50
        // Inside menu elements whose type attribute is in the toolbar state.
51 4
        $parent = $this->getParent();
52 4
        if ($parent === null) {
53 1
            return true;
54
        }
55
56 4
        if ($parent->getName() == 'ol' || $parent->getName() == 'ul') {
57 2
            return true;
58
        }
59
60 2
        if ($parent->getName() == 'menu' && $parent->hasAttribute('type')) {
61 1
            $typeAttributeValue = $parent->getAttribute('type');
62 1
            if ($typeAttributeValue == 'toolbar') {
63 1
                return true;
64
            }
65 1
        }
66
67 2
        $logger->debug('Element "li" only allowed inside "ol", "ul" and "menu[type=toolbar]" elements.');
68
69 2
        return false;
70
    }
71
}
72