Skip to content

Commit

Permalink
fix: allow boolean value as autoescape type
Browse files Browse the repository at this point in the history
  • Loading branch information
zackad committed Oct 2, 2024
1 parent bd69277 commit e940f49
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Bugfixes
- Fix handling mapping that omit key part
- Fix documentation about `twigAlwaysBreakObjects` option to reflect actual default value
- Fix autoescape block which allow boolean value (`true` and `false`) as a valid escape type

### Internals
- Make `key` part of `ObjectProperty` type optional to support object declaration that omit key part
Expand Down
18 changes: 14 additions & 4 deletions src/print/AutoescapeBlock.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { doc } from "prettier";
import { isBoolean } from "lodash";
import { printChildBlock, quoteChar } from "../util/index.js";

const { hardline } = doc.builders;

const printEscapeType = (escapeType, options) => {
if (escapeType === null) {
return [];
}

if (isBoolean(escapeType)) {
return [escapeType ? "true" : "false", " "];
}

return [quoteChar(options), escapeType, quoteChar(options), " "];
};

const createOpener = (node, options) => {
return [
node.trimLeft ? "{%-" : "{%",
" autoescape ",
quoteChar(options),
node.escapeType || "html",
quoteChar(options),
" ",
...printEscapeType(node.escapeType, options),
node.trimRightAutoescape ? "-%}" : "%}"
];
};
Expand Down
26 changes: 26 additions & 0 deletions tests/Statements/__snapshots__/autoescape.snap.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,29 @@
{%- autoescape 'html' -%}
<button type="button">Click me</button>
{%- endautoescape -%}

{# Test case from https://twig.symfony.com/doc/3.x/tags/autoescape.html #}

{% autoescape %}
Everything will be automatically escaped in this block using the HTML
strategy
{% endautoescape %}

{% autoescape 'html' %}
Everything will be automatically escaped in this block using the HTML
strategy
{% endautoescape %}

{% autoescape 'js' %}
Everything will be automatically escaped in this block using the js escaping
strategy
{% endautoescape %}

{% autoescape false %}
Everything will be outputted as is in this block
{% endautoescape %}

{% autoescape true %}
Everything will be automatically escaped in this block using the HTML
strategy
{% endautoescape %}
26 changes: 26 additions & 0 deletions tests/Statements/autoescape.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,29 @@
{%- autoescape 'html' -%}
<button type="button">Click me</button>
{%- endautoescape -%}

{# Test case from https://twig.symfony.com/doc/3.x/tags/autoescape.html #}

{% autoescape %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}

{% autoescape 'html' %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}

{% autoescape 'js' %}
Everything will be automatically escaped in this block
using the js escaping strategy
{% endautoescape %}

{% autoescape false %}
Everything will be outputted as is in this block
{% endautoescape %}

{% autoescape true %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}

0 comments on commit e940f49

Please sign in to comment.