Use native str_contains and such where possible

I think this makes this code much easier to read.

Change-Id: I0e4c0c8e4b9e32d1e612575ee136c89f0491c9c4
This commit is contained in:
thiemowmde 2024-11-07 11:05:46 +01:00
parent 8642b5fcb5
commit 736a93a92e
2 changed files with 4 additions and 7 deletions

View file

@ -27,15 +27,12 @@ class TemplateStylesFontFaceAtRuleSanitizer extends FontFaceAtRuleSanitizer {
parent::__construct( $matcherFactory ); parent::__construct( $matcherFactory );
// Only allow the font-family if it begins with "TemplateStyles" // Only allow the font-family if it begins with "TemplateStyles"
$validator = static fn ( Token $t ) => str_starts_with( $t->value(), 'TemplateStyles' );
$this->propertySanitizer->setKnownProperties( [ $this->propertySanitizer->setKnownProperties( [
'font-family' => new Alternative( [ 'font-family' => new Alternative( [
new TokenMatcher( Token::T_STRING, static function ( Token $t ) { new TokenMatcher( Token::T_STRING, $validator ),
return substr( $t->value(), 0, 14 ) === 'TemplateStyles';
} ),
new Juxtaposition( [ new Juxtaposition( [
new TokenMatcher( Token::T_IDENT, static function ( Token $t ) { new TokenMatcher( Token::T_IDENT, $validator ),
return substr( $t->value(), 0, 14 ) === 'TemplateStyles';
} ),
Quantifier::star( $matcherFactory->ident() ), Quantifier::star( $matcherFactory->ident() ),
] ), ] ),
] ), ] ),

View file

@ -46,7 +46,7 @@ class TemplateStylesMatcherFactory extends \Wikimedia\CSS\Grammar\MatcherFactory
// Don't allow unescaped \ or /../ in the non-query part of the URL // Don't allow unescaped \ or /../ in the non-query part of the URL
$tmp = preg_replace( '<[#?].*$>', '', $url ); $tmp = preg_replace( '<[#?].*$>', '', $url );
if ( strpos( $tmp, '\\' ) !== false || preg_match( '<(?:^|/|%2[fF])\.+(?:/|%2[fF]|$)>', $tmp ) ) { if ( str_contains( $tmp, '\\' ) || preg_match( '<(?:^|/|%2[fF])\.+(?:/|%2[fF]|$)>', $tmp ) ) {
return false; return false;
} }