Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaAtRuleSanitizer
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
11
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
 getRuleSanitizers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRuleSanitizers
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 handlesRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 doSanitize
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7namespace Wikimedia\CSS\Sanitizer;
8
9use Wikimedia\CSS\Grammar\Matcher;
10use Wikimedia\CSS\Objects\AtRule;
11use Wikimedia\CSS\Objects\CSSObject;
12use Wikimedia\CSS\Objects\Rule;
13use Wikimedia\CSS\Util;
14
15/**
16 * Sanitizes a CSS \@media rule
17 * @see https://www.w3.org/TR/2013/CR-css3-conditional-20130404/#at-media
18 */
19class MediaAtRuleSanitizer extends RuleSanitizer {
20
21    /** @var Matcher */
22    protected $mediaQueryListMatcher;
23
24    /** @var RuleSanitizer[] */
25    protected $ruleSanitizers = [];
26
27    /**
28     * @param Matcher $mediaQueryListMatcher Matcher for media query lists.
29     *  Probably from MatcherFactory::cssMediaQueryList().
30     */
31    public function __construct( Matcher $mediaQueryListMatcher ) {
32        $this->mediaQueryListMatcher = $mediaQueryListMatcher;
33    }
34
35    /**
36     * Access the list of rule sanitizers
37     * @return RuleSanitizer[]
38     */
39    public function getRuleSanitizers() {
40        return $this->ruleSanitizers;
41    }
42
43    /**
44     * Set the list of rule sanitizers
45     * @param RuleSanitizer[] $ruleSanitizers
46     */
47    public function setRuleSanitizers( array $ruleSanitizers ) {
48        Util::assertAllInstanceOf( $ruleSanitizers, RuleSanitizer::class, '$ruleSanitizers' );
49        $this->ruleSanitizers = $ruleSanitizers;
50    }
51
52    /** @inheritDoc */
53    public function handlesRule( Rule $rule ) {
54        return $rule instanceof AtRule && !strcasecmp( $rule->getName(), 'media' );
55    }
56
57    /** @inheritDoc */
58    protected function doSanitize( CSSObject $object ) {
59        if ( !$object instanceof AtRule || !$this->handlesRule( $object ) ) {
60            $this->sanitizationError( 'expected-at-rule', $object, [ 'media' ] );
61            return null;
62        }
63
64        if ( $object->getBlock() === null ) {
65            $this->sanitizationError( 'at-rule-block-required', $object, [ 'media' ] );
66            return null;
67        }
68
69        // Test the media query
70        $match = $this->mediaQueryListMatcher->matchAgainst(
71            $object->getPrelude(), [ 'mark-significance' => true ]
72        );
73        if ( !$match ) {
74            $cv = Util::findFirstNonWhitespace( $object->getPrelude() ) ?: $object->getPrelude();
75            $this->sanitizationError( 'invalid-media-query', $cv );
76            return null;
77        }
78
79        $ret = clone $object;
80        $this->fixPreludeWhitespace( $ret, false );
81        $this->sanitizeRuleBlock( $ret->getBlock(), $this->ruleSanitizers );
82
83        return $ret;
84    }
85}