Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
76 / 76
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PageAtRuleSanitizer
100.00% covered (success)
100.00%
76 / 76
100.00% covered (success)
100.00%
3 / 3
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
43 / 43
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%
32 / 32
100.00% covered (success)
100.00%
1 / 1
11
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\Juxtaposition;
13use Wikimedia\CSS\Grammar\KeywordMatcher;
14use Wikimedia\CSS\Grammar\Matcher;
15use Wikimedia\CSS\Grammar\MatcherFactory;
16use Wikimedia\CSS\Grammar\Quantifier;
17use Wikimedia\CSS\Grammar\TokenMatcher;
18use Wikimedia\CSS\Grammar\UnorderedGroup;
19use Wikimedia\CSS\Objects\AtRule;
20use Wikimedia\CSS\Objects\CSSObject;
21use Wikimedia\CSS\Objects\Declaration;
22use Wikimedia\CSS\Objects\DeclarationOrAtRule;
23use Wikimedia\CSS\Objects\DeclarationOrAtRuleList;
24use Wikimedia\CSS\Objects\Rule;
25use Wikimedia\CSS\Objects\Token;
26use Wikimedia\CSS\Parser\Parser;
27use Wikimedia\CSS\Util;
28
29/**
30 * Sanitizes a CSS \@page rule
31 * @see https://www.w3.org/TR/2023/WD-css-page-3-20230914/
32 */
33class PageAtRuleSanitizer extends RuleSanitizer {
34
35    /** @var Matcher */
36    protected $pageSelectorMatcher;
37
38    /** @var PropertySanitizer */
39    protected $propertySanitizer;
40
41    /** @var MarginAtRuleSanitizer */
42    protected $ruleSanitizer;
43
44    /**
45     * @param MatcherFactory $matcherFactory
46     * @param PropertySanitizer $propertySanitizer Sanitizer for declarations
47     */
48    public function __construct(
49        MatcherFactory $matcherFactory, PropertySanitizer $propertySanitizer
50    ) {
51        $ows = $matcherFactory->optionalWhitespace();
52        $pseudoPage = new Juxtaposition( [
53            new TokenMatcher( Token::T_COLON ),
54            new KeywordMatcher( [ 'left', 'right', 'first', 'blank' ] ),
55        ] );
56        $this->pageSelectorMatcher = new Alternative( [
57            Quantifier::hash( new Juxtaposition( [
58                $ows,
59                new Alternative( [
60                    Quantifier::plus( $pseudoPage ),
61                    new Juxtaposition( [ $matcherFactory->ident(), Quantifier::star( $pseudoPage ) ] ),
62                ] ),
63                $ows,
64            ] ) ),
65            $ows
66        ] );
67        $this->pageSelectorMatcher->setDefaultOptions( [ 'skip-whitespace' => false ] );
68
69        // Clone the $propertySanitizer and inject the special properties
70        $this->propertySanitizer = clone $propertySanitizer;
71        $this->propertySanitizer->addKnownProperties( [
72            'size' => new Alternative( [
73                Quantifier::count( $matcherFactory->length(), 1, 2 ),
74                new KeywordMatcher( 'auto' ),
75                UnorderedGroup::someOf( [
76                    new KeywordMatcher( [
77                        'A5', 'A4', 'A3', 'B5', 'B4', 'JIS-B5', 'JIS-B4', 'letter', 'legal', 'ledger',
78                    ] ),
79                    new KeywordMatcher( [ 'portrait', 'landscape' ] ),
80                ] ),
81            ] ),
82            'page-orientation' => new KeywordMatcher( [ 'upright', 'rotate-left', 'rotate-right' ] ),
83            'marks' => new Alternative( [
84                new KeywordMatcher( 'none' ),
85                UnorderedGroup::someOf( [
86                    new KeywordMatcher( 'crop' ),
87                    new KeywordMatcher( 'cross' ),
88                ] ),
89            ] ),
90            'bleed' => new Alternative( [
91                new KeywordMatcher( 'auto' ),
92                $matcherFactory->length(),
93            ] ),
94        ] );
95
96        $this->ruleSanitizer = new MarginAtRuleSanitizer( $propertySanitizer );
97    }
98
99    /** @inheritDoc */
100    public function handlesRule( Rule $rule ) {
101        return $rule instanceof AtRule && !strcasecmp( $rule->getName(), 'page' );
102    }
103
104    /** @inheritDoc */
105    protected function doSanitize( CSSObject $object ) {
106        if ( !$object instanceof AtRule || !$this->handlesRule( $object ) ) {
107            $this->sanitizationError( 'expected-at-rule', $object, [ 'page' ] );
108            return null;
109        }
110
111        if ( $object->getBlock() === null ) {
112            $this->sanitizationError( 'at-rule-block-required', $object, [ 'page' ] );
113            return null;
114        }
115
116        // Test the page selector
117        $match = $this->pageSelectorMatcher->matchAgainst(
118            $object->getPrelude(), [ 'mark-significance' => true ]
119        );
120        if ( !$match ) {
121            $cv = Util::findFirstNonWhitespace( $object->getPrelude() ) ?: $object->getPrelude();
122            $this->sanitizationError( 'invalid-page-selector', $cv );
123            return null;
124        }
125
126        $ret = clone $object;
127        $this->fixPreludeWhitespace( $ret, false );
128
129        // Parse the block's contents into a list of declarations and at-rules,
130        // sanitize it, and put it back into the block.
131        $blockContents = $ret->getBlock()->getValue();
132        $parser = Parser::newFromTokens( $blockContents->toTokenArray() );
133        $oldList = $parser->parseDeclarationOrAtRuleList();
134        $this->sanitizationErrors = array_merge( $this->sanitizationErrors, $parser->getParseErrors() );
135        $newList = new DeclarationOrAtRuleList();
136        foreach ( $oldList as $thing ) {
137            if ( $thing instanceof Declaration ) {
138                $thing = $this->sanitizeObj( $this->propertySanitizer, $thing );
139            } elseif ( $thing instanceof AtRule && $this->ruleSanitizer->handlesRule( $thing ) ) {
140                $thing = $this->sanitizeObj( $this->ruleSanitizer, $thing );
141            } else {
142                $this->sanitizationError( 'invalid-page-rule-content', $thing );
143                $thing = null;
144            }
145            '@phan-var DeclarationOrAtRule $thing';
146            if ( $thing ) {
147                $newList->add( $thing );
148            }
149        }
150        $blockContents->clear();
151        $blockContents->add( $newList->toComponentValueArray() );
152
153        // @phan-suppress-next-line PhanTypeMismatchReturn generics weakness
154        return $ret;
155    }
156}