Skip to content

Commit

Permalink
use dump safe filters instead of to_json function which should avoid …
Browse files Browse the repository at this point in the history
…escaping characters malforming the JSON
  • Loading branch information
Nerwyn committed Sep 21, 2024
1 parent 053e38d commit ef6f617
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
8 changes: 4 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ for (const f in FILTERS) {
}
for (const f in HASS_FILTERS) {
env.addFilter(f, function (...args) {
const hass = JSON.parse(decodeURIComponent(new nunjucks.Template('{{ to_json(hass) }}').render(
const hass = JSON.parse(new nunjucks.Template('{{ hass | dump | safe }}').render(
// @ts-ignore
this.getVariables())));
this.getVariables()));
return HASS_FILTERS[f](hass, ...args);
});
}
Expand All @@ -24,9 +24,9 @@ for (const t in TESTS) {
}
for (const t in HASS_TESTS) {
env.addTest(t, function (...args) {
const hass = JSON.parse(decodeURIComponent(new nunjucks.Template('{{ to_json(hass) }}').render(
const hass = JSON.parse(new nunjucks.Template('{{ hass | dump | safe }}').render(
// @ts-ignore
this.getVariables())));
this.getVariables()));
return HASS_TESTS[t](hass, ...args);
});
}
Expand Down
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ for (const f in FILTERS) {
for (const f in HASS_FILTERS) {
env.addFilter(f, function (...args) {
const hass = JSON.parse(
decodeURIComponent(
new nunjucks.Template('{{ to_json(hass) }}').render(
// @ts-ignore
this.getVariables(),
),
new nunjucks.Template('{{ hass | dump | safe }}').render(
// @ts-ignore
this.getVariables(),
),
);
return HASS_FILTERS[f](hass, ...args);
Expand All @@ -42,11 +40,9 @@ for (const t in HASS_TESTS) {
t,
function (...args: string[]) {
const hass = JSON.parse(
decodeURIComponent(
new nunjucks.Template('{{ to_json(hass) }}').render(
// @ts-ignore
this.getVariables(),
),
new nunjucks.Template('{{ hass | dump | safe }}').render(
// @ts-ignore
this.getVariables(),
),
);
return HASS_TESTS[t](hass, ...args);
Expand Down

0 comments on commit ef6f617

Please sign in to comment.