twing
Version:
First-class Twig engine for Node.js
42 lines (41 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAutoEscapeTagHandler = void 0;
const parsing_1 = require("../error/parsing");
const auto_escape_1 = require("../node/auto-escape");
/**
* Marks a section of a template to be escaped or not.
*/
const createAutoEscapeTagHandler = () => {
const tag = 'autoescape';
return {
tag,
initialize: (parser) => {
return (token, stream) => {
const { line, column } = token;
let strategy;
if (stream.test("TAG_END")) {
strategy = "html";
}
else {
const expression = parser.parseExpression(stream);
if (expression.type !== "constant" ||
(typeof expression.attributes.value !== "string" && expression.attributes.value !== false)) {
const { line, column } = expression;
throw (0, parsing_1.createParsingError)('An escaping strategy must be a string or false.', { line, column }, stream.source);
}
const { value } = expression.attributes;
strategy = value;
}
stream.expect("TAG_END");
let body = parser.subparse(stream, tag, (token) => {
return token.test("NAME", 'endautoescape');
});
stream.next();
stream.expect("TAG_END");
return (0, auto_escape_1.createAutoEscapeNode)(strategy, body, line, column, tag);
};
}
};
};
exports.createAutoEscapeTagHandler = createAutoEscapeTagHandler;