|   |   | 
| (9 intermediate revisions by the same user not shown) | 
| Line 1: | Line 1: | 
|  | use Wikimedia\CSS\Parser\Parser;
 |  | /* empty */ | 
|  | use Wikimedia\CSS\Sanitizer\StylesheetSanitizer;
 |  | 
|  |   |  | 
|  | /** Parse a stylesheet from a string **/ |  | 
|  |   |  | 
|  | $parser = Parser::newFromString( $cssText );
 |  | 
|  | $stylesheet = $parser->parseStylesheet();
 |  | 
|  |   |  | 
|  | /** Report any parser errors **/
 |  | 
|  |   |  | 
|  | foreach ( $parser->getParseErrors() as list( $code, $line, $pos ) ) {
 |  | 
|  | 	// $code is a string that should be suitable as a key for an i18n library.
 |  | 
|  | 	// See errors.md for details.
 |  | 
|  | 	$error = lookupI18nMessage( "css-parse-error-$code" );
 |  | 
|  | 	echo "Parse error: $error at line $line character $pos\n";
 |  | 
|  | }
 |  | 
|  |   |  | 
|  | /** Apply sanitization to the stylesheet **/
 |  | 
|  |   |  | 
|  | // If you need to customize the defaults, copy the code of this method and
 |  | 
|  | // modify it.
 |  | 
|  | $sanitizer = StylesheetSanitizer::newDefault();
 |  | 
|  | $newStylesheet = $sanitizer->sanitize( $stylesheet );
 |  | 
|  |   |  | 
|  | /** Report any sanitizer errors **/
 |  | 
|  |   |  | 
|  | foreach ( $sanitizer->getSanitizationErrors() as list( $code, $line, $pos ) ) {
 |  | 
|  | 	// $code is a string that should be suitable as a key for an i18n library.
 |  | 
|  | 	// See errors.md for details.
 |  | 
|  | 	$error = lookupI18nMessage( "css-sanitization-error-$code" );
 |  | 
|  | 	echo "Sanitization error: $error at line $line character $pos\n";
 |  | 
|  | }
 |  | 
|  |   |  | 
|  | /** Convert the sanitized stylesheet back to text **/
 |  | 
|  |   |  | 
|  | $newText = (string)$newStylesheet;
 |  | 
|  |   |  | 
|  | @media screen {
 |  | 
|  | 	html.skin-theme-clientpref-night .infobox { background-color: black; color: white; }
 |  | 
|  | }
 |  | 
|  | @media screen and (prefers-color-scheme: dark) {
 |  | 
|  | 	html.skin-theme-clientpref-os .infobox { background-color: black; color: white; }
 |  | 
|  | }
 |  |