2021-03-19 20:00:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\TemplateStylesExtender;
|
|
|
|
|
2021-03-19 21:48:47 +00:00
|
|
|
use ConfigException;
|
2021-03-19 20:00:11 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use MediaWiki\Extension\TemplateStylesExtender\Matcher\VarNameMatcher;
|
2021-03-19 21:48:47 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-03-19 20:00:11 +00:00
|
|
|
use Wikimedia\CSS\Grammar\Alternative;
|
2021-03-19 21:48:47 +00:00
|
|
|
use Wikimedia\CSS\Grammar\AnythingMatcher;
|
2021-03-19 20:00:11 +00:00
|
|
|
use Wikimedia\CSS\Grammar\FunctionMatcher;
|
|
|
|
use Wikimedia\CSS\Grammar\Juxtaposition;
|
|
|
|
use Wikimedia\CSS\Grammar\KeywordMatcher;
|
|
|
|
use Wikimedia\CSS\Grammar\MatcherFactory;
|
2021-03-19 20:57:10 +00:00
|
|
|
use Wikimedia\CSS\Grammar\Quantifier;
|
2021-03-19 20:00:11 +00:00
|
|
|
use Wikimedia\CSS\Grammar\WhitespaceMatcher;
|
|
|
|
use Wikimedia\CSS\Sanitizer\StylePropertySanitizer;
|
|
|
|
|
|
|
|
class TemplateStylesExtender {
|
|
|
|
|
2021-03-19 20:41:33 +00:00
|
|
|
/**
|
|
|
|
* Adds a css var name matcher
|
|
|
|
*
|
|
|
|
* @param StylePropertySanitizer $propertySanitizer
|
|
|
|
*/
|
2021-03-19 20:00:11 +00:00
|
|
|
public function addVarSelector( StylePropertySanitizer $propertySanitizer ): void {
|
|
|
|
$propertySanitizer->setCssWideKeywordsMatcher(
|
2021-03-19 21:48:47 +00:00
|
|
|
new Juxtaposition( [
|
|
|
|
Quantifier::optional(
|
|
|
|
new AnythingMatcher()
|
|
|
|
),
|
|
|
|
new WhitespaceMatcher( [ 'significant' => false ] ),
|
|
|
|
Quantifier::plus(
|
|
|
|
new FunctionMatcher(
|
|
|
|
'var',
|
|
|
|
new Juxtaposition( [
|
|
|
|
new WhitespaceMatcher( [ 'significant' => false ] ),
|
|
|
|
new VarNameMatcher(),
|
|
|
|
new WhitespaceMatcher( [ 'significant' => false ] ),
|
|
|
|
] )
|
|
|
|
),
|
|
|
|
),
|
|
|
|
new WhitespaceMatcher( [ 'significant' => false ] ),
|
|
|
|
Quantifier::optional(
|
|
|
|
new AnythingMatcher()
|
|
|
|
),
|
|
|
|
Quantifier::optional(
|
|
|
|
new KeywordMatcher( [ '!important' ] )
|
|
|
|
)
|
|
|
|
] ),
|
|
|
|
|
2021-03-19 20:00:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:41:33 +00:00
|
|
|
/**
|
|
|
|
* Adds the image-rendering matcher
|
|
|
|
* T222678
|
|
|
|
*
|
|
|
|
* @param StylePropertySanitizer $propertySanitizer
|
|
|
|
*/
|
2021-03-19 20:00:11 +00:00
|
|
|
public function addImageRendering( StylePropertySanitizer $propertySanitizer ): void {
|
|
|
|
try {
|
|
|
|
$propertySanitizer->addKnownProperties( [
|
|
|
|
'image-rendering' => new KeywordMatcher( [
|
|
|
|
'auto',
|
|
|
|
'crisp-edges',
|
|
|
|
'pixelated',
|
|
|
|
'inherit',
|
|
|
|
'initial',
|
|
|
|
'unset',
|
|
|
|
] )
|
|
|
|
] );
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
// Fail silently
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:41:33 +00:00
|
|
|
/**
|
|
|
|
* Adds the ruby-position and ruby-align matcher
|
|
|
|
* T277755
|
|
|
|
*
|
|
|
|
* @param StylePropertySanitizer $propertySanitizer
|
|
|
|
*/
|
2021-03-19 20:00:11 +00:00
|
|
|
public function addRuby( StylePropertySanitizer $propertySanitizer ): void {
|
|
|
|
try {
|
|
|
|
$propertySanitizer->addKnownProperties( [
|
|
|
|
'ruby-position' => new KeywordMatcher( [
|
|
|
|
'start',
|
|
|
|
'center',
|
|
|
|
'space-between',
|
|
|
|
'space-around',
|
|
|
|
'inherit',
|
|
|
|
'initial',
|
|
|
|
'unset',
|
|
|
|
] )
|
|
|
|
] );
|
|
|
|
|
|
|
|
$propertySanitizer->addKnownProperties( [
|
|
|
|
'ruby-align' => new KeywordMatcher( [
|
|
|
|
'over',
|
|
|
|
'under',
|
|
|
|
'inter-character',
|
|
|
|
'inherit',
|
|
|
|
'initial',
|
|
|
|
'unset',
|
|
|
|
] )
|
|
|
|
] );
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
// Fail silently
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:41:33 +00:00
|
|
|
/**
|
|
|
|
* Adds scroll-margin-* and scroll-padding-* matcher
|
|
|
|
* TODO: This is not well tested
|
|
|
|
* T271598
|
|
|
|
*
|
|
|
|
* @param StylePropertySanitizer $propertySanitizer
|
|
|
|
* @param MatcherFactory $factory
|
|
|
|
*/
|
2021-03-19 21:48:47 +00:00
|
|
|
public function addScrollMarginProperties( $propertySanitizer, $factory ): void {
|
2021-03-19 20:00:11 +00:00
|
|
|
$suffixes = [
|
|
|
|
'margin-block-end',
|
|
|
|
'margin-block-start',
|
|
|
|
'margin-block',
|
|
|
|
'margin-bottom',
|
|
|
|
'margin-inline-end',
|
|
|
|
'margin-inline-start',
|
|
|
|
'margin-inline',
|
|
|
|
'margin-left',
|
|
|
|
'margin-right',
|
|
|
|
'margin-top',
|
|
|
|
'margin',
|
|
|
|
'padding-block-end',
|
|
|
|
'padding-block-start',
|
|
|
|
'padding-block',
|
|
|
|
'padding-bottom',
|
|
|
|
'padding-inline-end',
|
|
|
|
'padding-inline-start',
|
|
|
|
'padding-inline',
|
|
|
|
'padding-left',
|
|
|
|
'padding-right',
|
|
|
|
'padding-top',
|
|
|
|
'padding',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $suffixes as $suffix ) {
|
|
|
|
try {
|
|
|
|
$propertySanitizer->addKnownProperties( [
|
|
|
|
sprintf( 'scroll-%s', $suffix ) => new Alternative( [
|
|
|
|
$factory->length()
|
|
|
|
] )
|
|
|
|
] );
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
// Fail silently
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 21:48:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a config value for a given key from the main config
|
|
|
|
* Returns null on if an ConfigException was thrown
|
|
|
|
*
|
|
|
|
* @param string $key The config key
|
|
|
|
* @param null $default
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public static function getConfigValue( string $key, $default = null ) {
|
|
|
|
try {
|
|
|
|
$value = MediaWikiServices::getInstance()->getMainConfig()->get( $key );
|
|
|
|
} catch ( ConfigException $e ) {
|
|
|
|
wfLogWarning(
|
|
|
|
sprintf(
|
|
|
|
'Could not get config for "$wg%s". %s', $key,
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2021-03-19 20:00:11 +00:00
|
|
|
}
|