Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
StyleAttributeSanitizer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newDefault
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 doSanitize
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 sanitizeString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types = 1 );
3
4/**
5 * @file
6 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 */
8
9namespace Wikimedia\CSS\Sanitizer;
10
11use Wikimedia\CSS\Grammar\MatcherFactory;
12use Wikimedia\CSS\Objects\CSSObject;
13use Wikimedia\CSS\Objects\DeclarationList;
14use Wikimedia\CSS\Parser\Parser;
15
16/**
17 * Sanitizes a CSS style attribute (i.e. `<tag style="...">`)
18 * @see https://www.w3.org/TR/2013/REC-css-style-attr-20131107/
19 */
20class StyleAttributeSanitizer extends Sanitizer {
21
22    /** @var Sanitizer */
23    protected $propertySanitizer;
24
25    /**
26     * @param PropertySanitizer $propertySanitizer Sanitizer to test property declarations.
27     *  Probably an instance of StylePropertySanitizer.
28     */
29    public function __construct( PropertySanitizer $propertySanitizer ) {
30        $this->propertySanitizer = $propertySanitizer;
31    }
32
33    /**
34     * Create and return a default StyleAttributeSanitizer.
35     * @note This method exists more to be an example of how to put everything
36     *  together than to be used directly.
37     * @return StyleAttributeSanitizer
38     */
39    public static function newDefault() {
40        // First, we need a matcher factory for the stuff all the sanitizers
41        // will need.
42        $matcherFactory = MatcherFactory::singleton();
43
44        // This is the sanitizer for a single "property: value"
45        $propertySanitizer = new StylePropertySanitizer( $matcherFactory );
46
47        // StyleAttributeSanitizer brings it all together
48        return new StyleAttributeSanitizer( $propertySanitizer );
49    }
50
51    /** @inheritDoc */
52    protected function doSanitize( CSSObject $object ) {
53        if ( !$object instanceof DeclarationList ) {
54            '@phan-var CSSObject $object';
55            $this->sanitizationError( 'expected-declaration-list', $object );
56            return null;
57        }
58        // @phan-suppress-next-line PhanTypeMismatchReturn generics weakness
59        return $this->sanitizeList( $this->propertySanitizer, $object );
60    }
61
62    /**
63     * Sanitize a string value.
64     * @param string $string
65     * @return DeclarationList
66     */
67    public function sanitizeString( $string ) {
68        $parser = Parser::newFromString( $string );
69        $declarations = $parser->parseDeclarationList();
70        $this->sanitizationErrors = array_merge( $this->sanitizationErrors, $parser->getParseErrors() );
71        return $this->sanitizeList( $this->propertySanitizer, $declarations );
72    }
73}