ContentPurifier::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GitHub;
4
5
use HTMLPurifier;
6
use HTMLPurifier_Config;
7
8
/**
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class ContentPurifier {
13
14
	/**
15
	 * @var HTMLPurifier
16
	 */
17
	private $purifier;
18
19
	const ALLOWED_HTML_TAGS = '
20
		h1,h2,h3,h4,h5,h6,
21
		p,
22
		br,hr,
23
		ul,ol,li,
24
		span,b,i,u,strong,em,
25
		a[href|target],
26
		img[src|alt],
27
		table[class],thead,tbody,tr,th[scope],td[scope],
28
		code,pre
29
	';
30
31 4
	public function __construct() {
32 4
		$config = HTMLPurifier_Config::createDefault();
33
34 4
		$config->set( 'HTML.Allowed', self::ALLOWED_HTML_TAGS );
35 4
		$config->set( 'Attr.AllowedFrameTargets', [ '_blank' ] ); // allow target="_blank" hrefs
36
37 4
		$this->purifier = new HTMLPurifier( $config );
38 4
	}
39
40 4
	public function purify( string $html ): string {
41 4
		return $this->purifier->purify( $html );
42
	}
43
44
}
45