Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
KeyframesAtRuleSanitizer
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
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%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
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\Alternative;
12use Wikimedia\CSS\Grammar\KeywordMatcher;
13use Wikimedia\CSS\Grammar\Matcher;
14use Wikimedia\CSS\Grammar\MatcherFactory;
15use Wikimedia\CSS\Grammar\Quantifier;
16use Wikimedia\CSS\Objects\AtRule;
17use Wikimedia\CSS\Objects\CSSObject;
18use Wikimedia\CSS\Objects\Rule;
19use Wikimedia\CSS\Util;
20
21/**
22 * Sanitizes a CSS \@keyframes rule
23 * @see https://www.w3.org/TR/2023/WD-css-animations-1-20230302/#keyframes
24 */
25class KeyframesAtRuleSanitizer extends RuleSanitizer {
26
27    /** @var Matcher */
28    protected $nameMatcher;
29
30    /** @var Sanitizer */
31    protected $ruleSanitizer;
32
33    /**
34     * @param MatcherFactory $matcherFactory
35     * @param PropertySanitizer $propertySanitizer Sanitizer for declarations
36     */
37    public function __construct(
38        MatcherFactory $matcherFactory, PropertySanitizer $propertySanitizer
39    ) {
40        $this->nameMatcher = new Alternative( [
41            $matcherFactory->customIdent( [ 'none' ] ),
42            $matcherFactory->string(),
43        ] );
44        $this->ruleSanitizer = new StyleRuleSanitizer(
45            Quantifier::hash( new Alternative( [
46                new KeywordMatcher( [ 'from', 'to' ] ), $matcherFactory->rawPercentage()
47            ] ) ),
48            $propertySanitizer
49        );
50    }
51
52    /** @inheritDoc */
53    public function handlesRule( Rule $rule ) {
54        return $rule instanceof AtRule && !strcasecmp( $rule->getName(), 'keyframes' );
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, [ 'keyframes' ] );
61            return null;
62        }
63
64        if ( $object->getBlock() === null ) {
65            $this->sanitizationError( 'at-rule-block-required', $object, [ 'keyframes' ] );
66            return null;
67        }
68
69        // Test the keyframe name
70        if ( !$this->nameMatcher->matchAgainst( $object->getPrelude(), [ 'mark-significance' => true ] ) ) {
71            $cv = Util::findFirstNonWhitespace( $object->getPrelude() );
72            if ( $cv ) {
73                $this->sanitizationError( 'invalid-keyframe-name', $cv );
74            } else {
75                $this->sanitizationError( 'missing-keyframe-name', $object );
76            }
77            return null;
78        }
79
80        $ret = clone $object;
81        $this->fixPreludeWhitespace( $ret, false );
82        $this->sanitizeRuleBlock( $ret->getBlock(), [ $this->ruleSanitizer ] );
83
84        // @phan-suppress-next-line PhanTypeMismatchReturn generics weakness
85        return $ret;
86    }
87}