Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateStylesFontFaceAtRuleSanitizer
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\TemplateStyles;
4
5/**
6 * @file
7 * @license GPL-2.0-or-later
8 */
9
10use Wikimedia\CSS\Grammar\Alternative;
11use Wikimedia\CSS\Grammar\Juxtaposition;
12use Wikimedia\CSS\Grammar\MatcherFactory;
13use Wikimedia\CSS\Grammar\Quantifier;
14use Wikimedia\CSS\Grammar\TokenMatcher;
15use Wikimedia\CSS\Objects\Token;
16use Wikimedia\CSS\Sanitizer\FontFaceAtRuleSanitizer;
17
18/**
19 * Extend the standard `@font-face` matcher to require a prefix on families.
20 */
21class TemplateStylesFontFaceAtRuleSanitizer extends FontFaceAtRuleSanitizer {
22
23    public function __construct( MatcherFactory $matcherFactory ) {
24        parent::__construct( $matcherFactory );
25
26        // Only allow the font-family if it begins with "TemplateStyles"
27        $validator = static fn ( Token $t ) => str_starts_with( $t->value(), 'TemplateStyles' );
28        $this->propertySanitizer->setKnownProperties( [
29            'font-family' => new Alternative( [
30                new TokenMatcher( Token::T_STRING, $validator ),
31                new Juxtaposition( [
32                    new TokenMatcher( Token::T_IDENT, $validator ),
33                    Quantifier::star( $matcherFactory->ident() ),
34                ] ),
35            ] ),
36        ] + $this->propertySanitizer->getKnownProperties() );
37    }
38}