From 687207f0bf1a8d2bc74da28c78901485fbef492d Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sun, 4 Jun 2023 16:54:19 +0200 Subject: [PATCH] Create HookRunner class and the hook handler interfaces Bug: T338435 Change-Id: I34726d15dad5045f5c1b00ec7796b7b5f1516d12 --- hooks.txt | 24 --------- includes/Hooks.php | 9 ++-- includes/Hooks/HookRunner.php | 50 +++++++++++++++++++ .../TemplateStylesPropertySanitizerHook.php | 29 +++++++++++ .../TemplateStylesStylesheetSanitizerHook.php | 34 +++++++++++++ tests/phpunit/unit/HookRunnerTest.php | 16 ++++++ 6 files changed, 134 insertions(+), 28 deletions(-) delete mode 100644 hooks.txt create mode 100644 includes/Hooks/HookRunner.php create mode 100644 includes/Hooks/TemplateStylesPropertySanitizerHook.php create mode 100644 includes/Hooks/TemplateStylesStylesheetSanitizerHook.php create mode 100644 tests/phpunit/unit/HookRunnerTest.php diff --git a/hooks.txt b/hooks.txt deleted file mode 100644 index b688341..0000000 --- a/hooks.txt +++ /dev/null @@ -1,24 +0,0 @@ -This document describes MediaWiki hooks added by the TemplateStyles extension. -See MediaWiki core's docs/hooks.txt for details on how hooks work. - -==Events and parameters== - -'TemplateStylesPropertySanitizer': Allows for adjusting or replacing the -StylePropertySanitizer used when sanitizing style rules. For example, you might -add, remove, or redefine known properties. -&$propertySanitizer: Wikimedia\CSS\Sanitizer\StylePropertySanitizer to be used - for sanitization. -$matcherFactory: Wikimedia\CSS\Grammar\MatcherFactory being used, for use in - adding or redefining known properties or replacing the entire sanitizer. - -'TemplateStylesStylesheetSanitizer': Allows for adjusting or replacing the -StylesheetSanitizer. For example, you might add, remove, or redefine at-rule -sanitizers. -&$sanitizer: Wikimedia\CSS\Sanitizer\StylesheetSanitizer to be used for - sanitization. The array returned by `$sanitizer->getRuleSanitizers()` will use - the at-rule names (including the '@') as keys. The style rule sanitizer has - key 'styles'. -$propertySanitizer: Wikimedia\CSS\Sanitizer\StylePropertySanitizer being used - for sanitization, for use in adding or redefining rule sanitizers. -$matcherFactory: Wikimedia\CSS\Grammar\MatcherFactory being used, for use in - adding or redefining rule sanitizers. diff --git a/includes/Hooks.php b/includes/Hooks.php index f4cc9b2..b843831 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -13,6 +13,7 @@ use ExtensionRegistry; use Html; use InvalidArgumentException; use MapCacheLRU; +use MediaWiki\Extension\TemplateStyles\Hooks\HookRunner; use MediaWiki\MediaWikiServices; use MediaWiki\Revision\SlotRecord; use Parser; @@ -107,8 +108,8 @@ class Hooks { $propertySanitizer->getKnownProperties(), array_flip( $config->get( 'TemplateStylesDisallowedProperties' ) ) ) ); - $hookContainer = MediaWikiServices::getInstance()->getHookContainer(); - $hookContainer->run( 'TemplateStylesPropertySanitizer', [ &$propertySanitizer, $matcherFactory ] ); + $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); + $hookRunner->onTemplateStylesPropertySanitizer( $propertySanitizer, $matcherFactory ); $htmlOrBodySimpleSelectorSeqMatcher = new CheckedMatcher( $matcherFactory->cssSimpleSelectorSeq(), @@ -175,8 +176,8 @@ class Hooks { ]; $allRuleSanitizers = array_diff_key( $allRuleSanitizers, $disallowedAtRules ); $sanitizer = new StylesheetSanitizer( $allRuleSanitizers ); - $hookContainer->run( 'TemplateStylesStylesheetSanitizer', - [ &$sanitizer, $propertySanitizer, $matcherFactory ] + $hookRunner->onTemplateStylesStylesheetSanitizer( + $sanitizer, $propertySanitizer, $matcherFactory ); self::$sanitizers[$key] = $sanitizer; } diff --git a/includes/Hooks/HookRunner.php b/includes/Hooks/HookRunner.php new file mode 100644 index 0000000..5cac225 --- /dev/null +++ b/includes/Hooks/HookRunner.php @@ -0,0 +1,50 @@ +hookContainer = $hookContainer; + } + + /** + * @inheritDoc + */ + public function onTemplateStylesPropertySanitizer( + StylePropertySanitizer &$propertySanitizer, + MatcherFactory $matcherFactory + ) { + return $this->hookContainer->run( + 'TemplateStylesPropertySanitizer', + [ &$propertySanitizer, $matcherFactory ] + ); + } + + /** + * @inheritDoc + */ + public function onTemplateStylesStylesheetSanitizer( + StylesheetSanitizer &$sanitizer, + StylePropertySanitizer $propertySanitizer, + MatcherFactory $matcherFactory + ) { + return $this->hookContainer->run( + 'TemplateStylesStylesheetSanitizer', + [ &$sanitizer, $propertySanitizer, $matcherFactory ] + ); + } +} diff --git a/includes/Hooks/TemplateStylesPropertySanitizerHook.php b/includes/Hooks/TemplateStylesPropertySanitizerHook.php new file mode 100644 index 0000000..c6420f7 --- /dev/null +++ b/includes/Hooks/TemplateStylesPropertySanitizerHook.php @@ -0,0 +1,29 @@ +getRuleSanitizers()` will use the at-rule names (including the '@') as keys. + * The style rule sanitizer has key 'styles' + * @param StylePropertySanitizer $propertySanitizer StylePropertySanitizer being used for sanitization, + * for use in adding or redefining rule sanitizers + * @param MatcherFactory $matcherFactory MatcherFactory being used, for use in adding or redefining rule sanitizers + * @return bool|void True or no return value to continue or false to abort + */ + public function onTemplateStylesStylesheetSanitizer( + StylesheetSanitizer &$sanitizer, + StylePropertySanitizer $propertySanitizer, + MatcherFactory $matcherFactory + ); +} diff --git a/tests/phpunit/unit/HookRunnerTest.php b/tests/phpunit/unit/HookRunnerTest.php new file mode 100644 index 0000000..f280de4 --- /dev/null +++ b/tests/phpunit/unit/HookRunnerTest.php @@ -0,0 +1,16 @@ + [ HookRunner::class ]; + } +}