Debian Codegraph @

vendor/wikimedia/css-sanitizer/README.md

raw
1 [![Latest Stable Version]](https://packagist.org/packages/wikimedia/css-sanitizer) [![License]](https://packagist.org/packages/wikimedia/css-sanitizer)
2
3 Wikimedia CSS Parser & Sanitizer
4 ================================
5
6 This library implements a CSS tokenizer, parser and grammar matcher in PHP.
7
8 Usage
9 -----
10
11 ```php
12 use Wikimedia\CSS\Parser\Parser;
13 use Wikimedia\CSS\Sanitizer\StylesheetSanitizer;
14
15 /** Parse a stylesheet from a string **/
16
17 $parser = Parser::newFromString( $cssText );
18 $stylesheet = $parser->parseStylesheet();
19
20 /** Report any parser errors **/
21
22 foreach ( $parser->getParseErrors() as list( $code, $line, $pos ) ) {
23 // $code is a string that should be suitable as a key for an i18n library.
24 // See errors.md for details.
25 $error = lookupI18nMessage( "css-parse-error-$code" );
26 echo "Parse error: $error at line $line character $pos\n";
27 }
28
29 /** Apply sanitization to the stylesheet **/
30
31 // If you need to customize the defaults, copy the code of this method and
32 // modify it.
33 $sanitizer = StylesheetSanitizer::newDefault();
34 $newStylesheet = $sanitizer->sanitize( $stylesheet );
35
36 /** Report any sanitizer errors **/
37
38 foreach ( $sanitizer->getSanitizationErrors() as list( $code, $line, $pos ) ) {
39 // $code is a string that should be suitable as a key for an i18n library.
40 // See errors.md for details.
41 $error = lookupI18nMessage( "css-sanitization-error-$code" );
42 echo "Sanitization error: $error at line $line character $pos\n";
43 }
44
45 /** Convert the sanitized stylesheet back to text **/
46
47 $newText = (string)$newStylesheet;
48
49 // Or if you'd rather have it minified too
50 $minifiedText = Wikimedia\CSS\Util::stringify( $newStylesheet, [ 'minify' => true ] );
51 ```
52
53 Conformance
54 -----------
55
56 The library follows the following grammar specifications:
57
58 * [CSS Syntax Level 3, 2019-07-16][CSSSYN]
59 * [CSS Values and Units Module Level 4, 2024-03-12][CSSVAL]
60 * [CSS Selectors Level 3, 2018-11-06][CSSSEL]
61
62 The sanitizer recognizes the following CSS modules:
63
64 * [Align Level 3, 2025-03-11](https://www.w3.org/TR/2025/WD-css-align-3-20250311/)
65 * [Animations Level 1, 2023-03-02](https://www.w3.org/TR/2023/WD-css-animations-1-20230302/)
66 * [Backgrounds Level 3, 2024-03-11](https://www.w3.org/TR/2024/CRD-css-backgrounds-3-20240311/)
67 * [Break Level 3, 2018-12-04](https://www.w3.org/TR/2018/CR-css-break-3-20181204/)
68 * [Cascade Level 4, 2022-01-13](https://www.w3.org/TR/2022/CR-css-cascade-4-20220113/)
69 * [Color Level 4, 2025-04-24](https://www.w3.org/TR/2025/CRD-css-color-4-20250424)
70 * [Compositing Level 1, 2024-03-21](https://www.w3.org/TR/2024/CRD-compositing-1-20240321/)
71 * [Counter Styles Level 3, 2021-07-27](https://www.w3.org/TR/2021/CR-css-counter-styles-3-20210727/)
72 * [CSS Level 2, 2011-06-07](https://www.w3.org/TR/2011/REC-CSS2-20110607/)
73 * [Display Level 3, 2023-03-30](https://www.w3.org/TR/2023/CR-css-display-3-20230330/)
74 * [Easing Level 1, 2023-02-13](https://www.w3.org/TR/2023/CRD-css-easing-1-20230213/)
75 * [Filter Effects Level 1, 2018-12-18](https://www.w3.org/TR/2018/WD-filter-effects-1-20181218)
76 * [Flexbox Level 1, 2018-11-19](https://www.w3.org/TR/2018/CR-css-flexbox-1-20181119)
77 * [Fonts Level 3, 2018-09-20](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920)
78 * [Grid Level 1, 2025-03-26](https://www.w3.org/TR/2025/CRD-css-grid-1-20250326/)
79 * [Images Level 3, 2023-12-18](https://www.w3.org/TR/2023/CRD-css-images-3-20231218/)
80 * [Lists and Counters Level 3, 2020-11-17](https://www.w3.org/TR/2020/WD-css-lists-3-20201117/)
81 * [Logical Properties and Values Level 1, 2018-08-27](https://www.w3.org/TR/2018/WD-css-logical-1-20180827/)
82 * [Masking Level 1, 2021-08-05](https://www.w3.org/TR/2021/CRD-css-masking-1-20210805/)
83 * [Multicol Level 1, 2019-10-15](https://www.w3.org/TR/2024/CR-css-multicol-1-20240516/)
84 * [Overflow Level 3, 2023-03-29](https://www.w3.org/TR/2023/WD-css-overflow-3-20230329/)
85 * [Overflow Level 4, 2023-03-21](https://www.w3.org/TR/2023/WD-css-overflow-4-20230321/)
86 * [Page Level 3, 2023-09-14](https://www.w3.org/TR/2023/WD-css-page-3-20230914/)
87 * [Position Level 3, 2025-03-11](https://www.w3.org/TR/2025/WD-css-position-3-20250311/)
88 * [Pseudo-Elements Level 4, 2022-12-30](https://www.w3.org/TR/2022/WD-css-pseudo-4-20221230/)
89 * [Ruby Level 1, 2022-12-31](https://www.w3.org/TR/2022/WD-css-ruby-1-20221231/)
90 * [Scroll Snap Module Level 1](https://www.w3.org/TR/2021/CR-css-scroll-snap-1-20210311/)
91 * [Shapes Level 1, 2022-11-15](https://www.w3.org/TR/2022/CRD-css-shapes-1-20221115/)
92 * [Sizing Level 3, 2021-12-17](https://www.w3.org/TR/2021/WD-css-sizing-3-20211217/)
93 * [Sizing Level 4, 2025-02-24](https://drafts.csswg.org/css-sizing-4/)
94 * [Text Level 3, 2024-09-30](https://www.w3.org/TR/2024/CRD-css-text-3-20240930/)
95 * [Text Decorations Level 3, 2022-05-05](https://www.w3.org/TR/2022/CRD-css-text-decor-3-20220505/)
96 * [Transforms Level 1, 2019-02-14](https://www.w3.org/TR/2019/CR-css-transforms-1-20190214)
97 * [Transforms Level 2, 2021-11-09](https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/)
98 * [Transitions Level 1, 2018-10-11](https://www.w3.org/TR/2018/WD-css-transitions-1-20181011)
99 * [UI 3 Level 3, 2018-06-21](https://www.w3.org/TR/2018/REC-css-ui-3-20180621)
100 * [UI 4 Level 4, 2021-03-16](https://www.w3.org/TR/2021/WD-css-ui-4-20210316/)
101 * [Writing Modes Level 4, 2019-07-30](https://www.w3.org/TR/2019/CR-css-writing-modes-4-20190730)
102
103 And also,
104 * The `touch-action` property from
105 [Pointer Events Level 2, 2019-04-04](https://www.w3.org/TR/2019/REC-pointerevents2-20190404/)
106 * `:dir()` pseudo-class from [Selectors Level 4, 2022-11-11](https://www.w3.org/TR/2022/WD-selectors-4-20221111/#the-dir-pseudo)
107 * Accessibility related media features from [Media Queries Level 5](https://drafts.csswg.org/mediaqueries-5/#mf-user-preferences) including prefers-reduced-motion, prefers-reduced-transparency, prefers-contrast and forced-colors.
108 * `light-dark()` color function from [Color Module Level 5, 2024-02-29](https://www.w3.org/TR/2024/WD-css-color-5-20240229/#funcdef-light-dark)
109
110 Running tests
111 -------------
112
113 composer install --prefer-dist
114 composer test
115
116 Adding properties
117 -----------------
118
119 CSS specifications typically contain a summary of value grammars in the property
120 index section. These value grammars map directly to PHP code.
121
122 [Component value types](https://www.w3.org/TR/css-values-4/#component-types)
123
124 | Syntax | css-sanitizer code |
125 |----------------|------------------------------------------|
126 | `foo` | `new KeywordMatcher( 'foo' )` |
127 | `foo \| bar` | `new KeywordMatcher( [ 'foo', 'bar' ] )` |
128 | `<string>` | `$matcherFactory->string()` |
129 | `<url>` | `$matcherFactory->url()` |
130 | `<integer>` | `$matcherFactory->integer()` |
131 | `<number>` | `$matcherFactory->number()` |
132 | `<ratio>` | `$matcherFactory->ratio()` |
133 | `<percentage>` | `$matcherFactory->percentage()` |
134 | `<length>` | `$matcherFactory->length()` |
135 | `<frequency>` | `$matcherFactory->frequency()` |
136 | `<angle>` | `$matcherFactory->angle()` |
137 | `<time>` | `$matcherFactory->time()` |
138 | `<resolution>` | `$matcherFactory->resolution()` |
139
140 [Component value combinators](https://www.w3.org/TR/css-values-4/#component-combinators)
141
142 | Syntax | css-sanitizer code |
143 |-------------|--------------------------------------|
144 | `a b` | `new Juxtaposition( [ a, b ] )` |
145 | `a && b` | `UnorderedGroup::allOf( [ a, b ] )` |
146 | `a \|\| b` | `UnorderedGroup::someOf( [ a, b ] )` |
147 | `a \| b` | `new Alternative( [ a, b ] )` |
148
149 [Component value multipliers](https://www.w3.org/TR/css-values-4/#component-multipliers)
150
151 | Syntax | css-sanitizer code |
152 |----------|--------------------------------|
153 | `a*` | `Quantifier::star( a )` |
154 | `a+` | `Quantifier::plus( a )` |
155 | `a?` | `Quantifier::optional( a )` |
156 | `a{3,4}` | `Quantifier::count( a, 3, 4 )` |
157 | `a#` | `Quantifier::hash( a )` |
158 | `a!` | `new NonEmpty( a )` |
159
160
161 Releasing a new version
162 -----------------------
163
164 This package uses `wikimedia/update-history` and its conventions.
165
166 See https://www.mediawiki.org/wiki/UpdateHistory for details.
167
168 History
169 -------
170
171 We required a CSS sanitizer with several properties:
172
173 * Strict parsing according to modern standards.
174 * Includes line and character position for all errors.
175 * Configurable to limit unsafe constructs such as external URL references.
176 * Errors are easily localizable.
177
178 We could not find a library that fit these requirements, so we created one.
179
180 Additional release history is in [`HISTORY.md`](./HISTORY.md).
181
182 ---
183 [Latest Stable Version]: https://poser.pugx.org/wikimedia/css-sanitizer/v/stable.svg
184 [License]: https://poser.pugx.org/wikimedia/css-sanitizer/license.svg
185 [CSSSYN]: https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/
186 [CSSVAL]: https://www.w3.org/TR/2024/WD-css-values-4-20240312/
187 [CSSSEL]: https://www.w3.org/TR/2018/REC-selectors-3-20181106/
188 [CSSWORK]: https://www.w3.org/Style/CSS/current-work