Fix wrapper="…" failing for more complex selectors

For example, <templatestyles wrapper="a[href]" /> fails with an
InvalidArgumentException very late in the process, long after
the actual validation in TemplateStylesHooks::validateExtraWrapper()
was done. This happens because validateExtraWrapper() works on a
proper tree (where the [href] from the example is a nested object),
but flattens this tree in the end via toTokenArray(). This
flattened array ends in StyleRuleSanitizer::doSanitize() where it
is added to a ComponentValueList – which expects a tree.

The only change in this patch is that the object structure is not
flattened any more. That's all.

Bug: T258093
Change-Id: I5dc04611f91fe672da706f31fa8d0c254e3123d3
This commit is contained in:
Thiemo Kreuz 2020-09-15 19:34:01 +02:00 committed by Thiemo Kreuz (WMDE)
parent f67fbc1394
commit 1c0687c850
2 changed files with 8 additions and 1 deletions

View file

@ -84,7 +84,7 @@ class TemplateStylesHooks {
$match = self::getMatcherFactory()->cssSimpleSelectorSeq()
->match( $components, [ 'mark-significance' => true ] );
}
self::$wrappers[$wrapper] = $match ? $components->toTokenArray() : false;
self::$wrappers[$wrapper] = $match ? $components->toComponentValueArray() : false;
}
return self::$wrappers[$wrapper];
}

View file

@ -3,6 +3,7 @@
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\SlotRecord;
use Wikimedia\CSS\Parser\Parser as CSSParser;
/**
* @group TemplateStyles
@ -39,6 +40,12 @@ class TemplateStylesHooksTest extends MediaWikiLangTestCase {
TemplateStylesHooks::getSanitizer( 'foo', '.foo>.bar' );
}
public function testGetSanitizerNonLinearWrapper() {
$sanitizer = TemplateStylesHooks::getSanitizer( 'foo', 'div[data]' );
$sanitizer->sanitize( CSSParser::newFromString( '.not-empty { }' )->parseStylesheet() );
$this->assertSame( [], $sanitizer->getSanitizationErrors() );
}
/**
* @dataProvider provideOnRegistration
* @param array $textModelsToParse