From e940f496ada660a221f90078a10e4b37f7a23c6f Mon Sep 17 00:00:00 2001 From: zackad Date: Wed, 2 Oct 2024 11:25:01 +0700 Subject: [PATCH] fix: allow boolean value as autoescape type Close GH-63 Ref: https://twig.symfony.com/doc/3.x/tags/autoescape.html --- CHANGELOG.md | 1 + src/print/AutoescapeBlock.js | 18 ++++++++++--- .../__snapshots__/autoescape.snap.twig | 26 +++++++++++++++++++ tests/Statements/autoescape.twig | 26 +++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dbc182f..a6affcf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/print/AutoescapeBlock.js b/src/print/AutoescapeBlock.js index e69f80da..ac15555a 100644 --- a/src/print/AutoescapeBlock.js +++ b/src/print/AutoescapeBlock.js @@ -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 ? "-%}" : "%}" ]; }; diff --git a/tests/Statements/__snapshots__/autoescape.snap.twig b/tests/Statements/__snapshots__/autoescape.snap.twig index 4586041b..9e8613d2 100644 --- a/tests/Statements/__snapshots__/autoescape.snap.twig +++ b/tests/Statements/__snapshots__/autoescape.snap.twig @@ -7,3 +7,29 @@ {%- autoescape 'html' -%} {%- 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 %} diff --git a/tests/Statements/autoescape.twig b/tests/Statements/autoescape.twig index 472b717d..0fdc8d35 100644 --- a/tests/Statements/autoescape.twig +++ b/tests/Statements/autoescape.twig @@ -9,3 +9,29 @@ {%- autoescape 'html' -%} {%- 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 %}