From 4c20c9f5ade45cf79531230cb45cff530da1ae67 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Tue, 12 Jun 2018 14:59:15 -0700 Subject: [PATCH] Add ability to disable specific extensions fixes #6 by allowing disabling the tagfilter extension --- src/ParserContext.php | 6 ++++++ src/inlines/Context.php | 13 ++++++++++++- src/render/RenderContext.php | 8 ++++++++ src/unparsed-blocks/Context.php | 14 ++++++++++++++ tests/EdgeCaseTest.php | 12 ++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ParserContext.php b/src/ParserContext.php index a3770c2..c872c80 100644 --- a/src/ParserContext.php +++ b/src/ParserContext.php @@ -34,6 +34,12 @@ public function disableExtensions(): this { return $this; } + public function disableNamedExtension(string $name): this { + $this->blockContext->disableNamedExtension($name); + $this->inlineContext->disableNamedExtension($name); + return $this; + } + public function enableNamedExtension(string $name): this { $this->blockContext->enableNamedExtension($name); $this->inlineContext->enableNamedExtension($name); diff --git a/src/inlines/Context.php b/src/inlines/Context.php index fd0c955..2c1cd4f 100644 --- a/src/inlines/Context.php +++ b/src/inlines/Context.php @@ -74,12 +74,23 @@ public function disableExtensions(): this { return $this; } + public function disableNamedExtension(string $name): this { + $this->disabledInlineTypes = Keyset\union( + $this->disabledInlineTypes, + Keyset\filter( + self::ALL_INLINE_TYPES, + $class ==> Str\ends_with(Str\lowercase($class), "\\".$name.'extension'), + ), + ); + return $this; + } + public function enableNamedExtension(string $name): this { $this->disabledInlineTypes = Keyset\filter( $this->disabledInlineTypes, $class ==> !Str\ends_with( Str\lowercase($class), - "\\".$name.'extension', + "\\".Str\lowercase($name).'extension', ), ); return $this; diff --git a/src/render/RenderContext.php b/src/render/RenderContext.php index e367ec1..b284985 100644 --- a/src/render/RenderContext.php +++ b/src/render/RenderContext.php @@ -34,6 +34,14 @@ public function disableExtensions(): this { return $this; } + public function disableNamedExtension(string $extension): this { + $this->enabledExtensions = Vec\filter( + $this->enabledExtensions, + $obj ==> !Str\ends_with_ci(\get_class($obj), "\\".$extension.'Extension'), + ); + return $this; + } + public function enableNamedExtension(string $extension): this { $this->enabledExtensions = $this->extensions |> Vec\filter( diff --git a/src/unparsed-blocks/Context.php b/src/unparsed-blocks/Context.php index d675c07..5211b51 100644 --- a/src/unparsed-blocks/Context.php +++ b/src/unparsed-blocks/Context.php @@ -49,6 +49,20 @@ public function disableExtensions(): this { return $this; } + public function disableNamedExtension(string $name): this { + $this->disabledBlockTypes = Keyset\union( + $this->disabledBlockTypes, + Keyset\filter( + self::ALL_BLOCK_TYPES, + $class ==> Str\ends_with( + Str\lowercase($class), + "\\".Str\lowercase($name).'extension', + ), + ), + ); + return $this; + } + public function enableNamedExtension(string $name): this { $this->disabledBlockTypes = Keyset\filter( $this->disabledBlockTypes, diff --git a/tests/EdgeCaseTest.php b/tests/EdgeCaseTest.php index 5a86d4c..aa31d98 100644 --- a/tests/EdgeCaseTest.php +++ b/tests/EdgeCaseTest.php @@ -49,4 +49,16 @@ public function testManualExample( null, ); } + + public function testTagFilter(): void { + $ast = parse((new ParserContext())->enableHTML_UNSAFE(), '