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    /**
24     * @param MatcherFactory $matcherFactory
25     */
26    public function __construct( MatcherFactory $matcherFactory ) {
27        parent::__construct( $matcherFactory );
28
29        // Only allow the font-family if it begins with "TemplateStyles"
30        $validator = static fn ( Token $t ) => str_starts_with( $t->value(), 'TemplateStyles' );
31        $this->propertySanitizer->setKnownProperties( [
32            'font-family' => new Alternative( [
33                new TokenMatcher( Token::T_STRING, $validator ),
34                new Juxtaposition( [
35                    new TokenMatcher( Token::T_IDENT, $validator ),
36                    Quantifier::star( $matcherFactory->ident() ),
37                ] ),
38            ] ),
39        ] + $this->propertySanitizer->getKnownProperties() );
40    }
41}