Debian Codegraph @

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

raw
1 <?php
2 /**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 * @phan-file-suppress PhanTemplateTypeNotUsedInFunctionReturn
6 */
7
8 namespace Wikimedia\CSS\Sanitizer;
9
10 use Wikimedia\CSS\Objects\CSSObject;
11 use Wikimedia\CSS\Objects\CSSObjectList;
12 use Wikimedia\CSS\Objects\RuleList;
13 use Wikimedia\ScopedCallback;
14
15 /**
16 * Base class for CSS sanitizers
17 */
18 abstract class Sanitizer {
19
20 /** @var array Sanitization errors. Each error is [ string $tag, int $line, int $pos ] */
21 protected $sanitizationErrors = [];
22
23 /**
24 * Return all sanitization errors seen so far
25 * @return array Array of [ string $tag, int $line, int $pos, ... ]
26 */
27 public function getSanitizationErrors() {
28 return $this->sanitizationErrors;
29 }
30
31 /**
32 * Temporarily clear sanitization errors
33 *
34 * Errors will be cleared, then restored when the returned ScopedCallback
35 * goes out of scope or is consumed.
36 *
37 * @return ScopedCallback
38 */
39 public function stashSanitizationErrors() {
40 $reset = new ScopedCallback( function ( $e ) {
41 $this->sanitizationErrors = $e;
42 }, [ $this->sanitizationErrors ] );
43 $this->sanitizationErrors = [];
44 return $reset;
45 }
46
47 /**
48 * Clear sanitization errors
49 */
50 public function clearSanitizationErrors() {
51 $this->sanitizationErrors = [];
52 }
53
54 /**
55 * Record a sanitization error
56 * @param string $tag Error tag
57 * @param CSSObject $object Report the error starting at this object
58 * @param array $data Extra data about the error.
59 */
60 protected function sanitizationError( $tag, CSSObject $object, array $data = [] ) {
61 [ $line, $pos ] = $object->getPosition();
62 $this->sanitizationErrors[] = array_merge( [ $tag, $line, $pos ], $data );
63 }
64
65 /**
66 * Run another sanitizer over a CSSObject
67 * @template T of CSSObject
68 * @param Sanitizer $sanitizer
69 * @param T $object
70 * @return T|null
71 */
72 protected function sanitizeObj( Sanitizer $sanitizer, CSSObject $object ) {
73 $newObj = $sanitizer->doSanitize( $object );
74 $errors = $sanitizer->getSanitizationErrors();
75 if ( $errors && $sanitizer !== $this ) {
76 $this->sanitizationErrors = array_merge( $this->sanitizationErrors, $errors );
77 $sanitizer->clearSanitizationErrors();
78 }
79 return $newObj;
80 }
81
82 /**
83 * Run a sanitizer over all CSSObjects in a CSSObjectList
84 * @template T of CSSObjectList
85 * @param Sanitizer $sanitizer
86 * @param T $list
87 * @return T
88 */
89 protected function sanitizeList( Sanitizer $sanitizer, CSSObjectList $list ) {
90 $class = get_class( $list );
91 $ret = new $class;
92 '@phan-var T $ret';
93 foreach ( $list as $obj ) {
94 $newObj = $sanitizer->doSanitize( $obj );
95 if ( $newObj ) {
96 $ret->add( $newObj );
97 }
98 }
99
100 $errors = $sanitizer->getSanitizationErrors();
101 if ( $errors && $sanitizer !== $this ) {
102 $this->sanitizationErrors = array_merge( $this->sanitizationErrors, $errors );
103 $sanitizer->clearSanitizationErrors();
104 }
105
106 return $ret;
107 }
108
109 /**
110 * Run a set of RuleSanitizers over all rules in a RuleList
111 * @param RuleSanitizer[] $ruleSanitizers
112 * @param RuleList $list
113 * @return RuleList
114 */
115 protected function sanitizeRules( array $ruleSanitizers, RuleList $list ) {
116 $ret = new RuleList();
117 $curIndex = -INF;
118 foreach ( $list as $rule ) {
119 foreach ( $ruleSanitizers as $sanitizer ) {
120 if ( $sanitizer->handlesRule( $rule ) ) {
121 $indexes = $sanitizer->getIndex();
122 if ( is_array( $indexes ) ) {
123 [ $testIndex, $setIndex ] = $indexes;
124 } else {
125 $testIndex = $setIndex = $indexes;
126 }
127 if ( $testIndex < $curIndex ) {
128 $this->sanitizationError( 'misordered-rule', $rule );
129 } else {
130 $curIndex = $setIndex;
131 $rule = $this->sanitizeObj( $sanitizer, $rule );
132 if ( $rule ) {
133 $ret->add( $rule );
134 }
135 }
136 continue 2;
137 }
138 }
139 $this->sanitizationError( 'unrecognized-rule', $rule );
140 }
141 return $ret;
142 }
143
144 /**
145 * Sanitize a CSS object
146 * @template T of CSSObject
147 * @param T $object
148 * @return ?T Sanitized version of the object, or null if
149 * sanitization failed
150 */
151 abstract protected function doSanitize( CSSObject $object );
152
153 /**
154 * Sanitize a CSS object
155 * @template T of CSSObject
156 * @param T $object
157 * @return ?T Sanitized version of the object, or null if
158 * sanitization failed
159 */
160 public function sanitize( CSSObject $object ) {
161 return $this->doSanitize( $object );
162 }
163 }