Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
created

HtmlFilter::filter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Util;
13
14
final class HtmlFilter
15
{
16
    // Return the entire string as-is
17
    public const ALLOW = 'allow';
18
    // Escape the entire string so any HTML/JS won't be interpreted as such
19
    public const ESCAPE = 'escape';
20
    // Return an empty string
21
    public const STRIP = 'strip';
22
23
    /**
24
     * Runs the given HTML through the given filter
25
     *
26
     * @param string $html   HTML input to be filtered
27
     * @param string $filter One of the HtmlFilter constants
28
     *
29
     * @return string Filtered HTML
30
     *
31
     * @throws \InvalidArgumentException when an invalid $filter is given
32
     */
33 285
    public static function filter(string $html, string $filter): string
34
    {
35
        switch ($filter) {
36 285
            case self::STRIP:
37 12
                return '';
38 273
            case self::ESCAPE:
39 12
                return \htmlspecialchars($html, \ENT_NOQUOTES);
40 261
            case self::ALLOW:
41 258
                return $html;
42
            default:
43 3
                throw new \InvalidArgumentException(sprintf('Invalid filter provided: "%s"', $filter));
44
        }
45
    }
46
}
47