Completed
Push — master ( 041c4d...dec97d )
by Tim
03:47
created

RemoveComments::manipulate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 9.2
cc 4
eloc 12
nc 6
nop 2
crap 20
1
<?php
2
/**
3
 * RemoveComments
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace HTML\Sourceopt\Manipulation;
9
10
/**
11
 * RemoveComments
12
 */
13
class RemoveComments implements ManipulationInterface
14
{
15
16
    /**
17
     * Patterns for white-listing comments inside content
18
     *
19
     * @var array
20
     */
21
    protected $whiteListCommentsPatterns = [];
22
23
    /**
24
     * @param string $html          The original HTML
25
     * @param array  $configuration Configuration
26
     *
27
     * @return string the manipulated HTML
28
     */
29
    public function manipulate($html, array $configuration = [])
30
    {
31
        if (isset($configuration['keep.'])) {
32
            $this->whiteListCommentsPatterns = $configuration['keep.'];
33
        }
34
35
        // match all styles, scripts and comments
36
        $matches = [];
37
        preg_match_all(
38
            '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im',
39
            $html,
40
            $matches
41
        );
42
        foreach ($matches[0] as $tag) {
43
            if ($this->keepComment($tag) === false) {
44
                $html = str_replace($tag, '', $html);
45
            }
46
        }
47
        return $html;
48
    }
49
50
    /**
51
     * Check if a comment is defined to be kept in a pattern whiteListOfComments
52
     *
53
     * @param string $commentHtml
54
     *
55
     * @return boolean
56
     */
57
    protected function keepComment($commentHtml)
58
    {
59
        // if not even a comment, skip this
60
        if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) {
61
            return true;
62
        }
63
64
        // if not defined in white list
65
        if (!empty($this->whiteListCommentsPatterns)) {
66
            $commentHtml = str_replace("<!--", "", $commentHtml);
67
            $commentHtml = str_replace("-->", "", $commentHtml);
68
            $commentHtml = trim($commentHtml);
69
            foreach ($this->whiteListCommentsPatterns as $pattern) {
70
                if (preg_match($pattern, $commentHtml)) {
71
                    return true;
72
                }
73
            }
74
        }
75
        return false;
76
    }
77
}
78