Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Add ability to disable specific extensions
Browse files Browse the repository at this point in the history
fixes #6 by allowing disabling the tagfilter extension
  • Loading branch information
fredemmott committed Jun 12, 2018
1 parent e5cdc03 commit 4c20c9f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ParserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion src/inlines/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/render/RenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions src/unparsed-blocks/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/EdgeCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@ public function testManualExample(
null,
);
}

public function testTagFilter(): void {
$ast = parse((new ParserContext())->enableHTML_UNSAFE(), '<iframe />');
$html = (new HTMLRenderer(new RenderContext()))->render($ast);
expect($html)->toBeSame("&lt;iframe />\n");
$html = (
new HTMLRenderer(
(new RenderContext())->disableNamedExtension('TagFilter'),
)
)->render($ast);
expect($html)->toBeSame("<iframe />\n");
}
}

0 comments on commit 4c20c9f

Please sign in to comment.