Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Sanitizer/PageAtRuleSanitizer.php

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