Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateStylesFontFaceAtRuleSanitizer
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
14 / 14
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        $this->propertySanitizer->setKnownProperties( [
31            'font-family' => new Alternative( [
32                new TokenMatcher( Token::T_STRING, static function ( Token $t ) {
33                    return substr( $t->value(), 0, 14 ) === 'TemplateStyles';
34                } ),
35                new Juxtaposition( [
36                    new TokenMatcher( Token::T_IDENT, static function ( Token $t ) {
37                        return substr( $t->value(), 0, 14 ) === 'TemplateStyles';
38                    } ),
39                    Quantifier::star( $matcherFactory->ident() ),
40                ] ),
41            ] ),
42        ] + $this->propertySanitizer->getKnownProperties() );
43    }
44}