From 44d65952e09be9b4954a724e3d52ef7e78e41c39 Mon Sep 17 00:00:00 2001 From: dominikg Date: Wed, 9 Feb 2022 22:29:26 +0100 Subject: [PATCH 1/6] refactor: combine JSON.stringify and character escapes in one function that takes a JSONValue --- .../kit/src/runtime/server/page/load_node.js | 4 +- .../kit/src/runtime/server/page/render.js | 2 +- packages/kit/src/utils/escape.js | 46 ++++--------------- 3 files changed, 11 insertions(+), 41 deletions(-) diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 714a3e2ba462..014e743423e5 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -1,7 +1,7 @@ import { normalize } from '../../load.js'; import { respond } from '../index.js'; import { s } from '../../../utils/misc.js'; -import { escape_json_value_in_html } from '../../../utils/escape.js'; +import {escape_json_in_html} from '../../../utils/escape.js'; import { is_root_relative, resolve } from '../../../utils/url.js'; import { create_prerendering_url_proxy } from './utils.js'; import { is_pojo } from '../utils.js'; @@ -257,7 +257,7 @@ export async function load_node({ fetched.push({ url: requested, body: /** @type {string} */ (opts.body), - json: `{"status":${response.status},"statusText":${s(response.statusText)},"headers":${s(headers)},"body":"${escape_json_value_in_html(body)}"}` + json: `{"status":${response.status},"statusText":${s(response.statusText)},"headers":${s(headers)},"body":${escape_json_in_html(body)}}` }); } diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index 67891e389f6c..361510bafeb7 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -261,7 +261,7 @@ export async function render_response({ if (shadow_props) { // prettier-ignore - body += ``; + body += ``; } } diff --git a/packages/kit/src/utils/escape.js b/packages/kit/src/utils/escape.js index 895c45570d26..8682969ba179 100644 --- a/packages/kit/src/utils/escape.js +++ b/packages/kit/src/utils/escape.js @@ -1,50 +1,20 @@ +// dict from https://github.com/yahoo/serialize-javascript/blob/183c18a776e4635a379fdc620f81771f219832bb/index.js#L25 +// code licensed under the New BSD License. /** @type {Record} */ const escape_json_in_html_dict = { - '&': '\\u0026', - '>': '\\u003e', - '<': '\\u003c', - '\u2028': '\\u2028', - '\u2029': '\\u2029' -}; - -/** @type {Record} */ -const escape_json_value_in_html_dict = { - '"': '\\"', - '<': '\\u003C', - '>': '\\u003E', - '/': '\\u002F', - '\\': '\\\\', - '\b': '\\b', - '\f': '\\f', - '\n': '\\n', - '\r': '\\r', - '\t': '\\t', - '\0': '\\0', + '<' : '\\u003C', + '>' : '\\u003E', + '/' : '\\u002F', '\u2028': '\\u2028', '\u2029': '\\u2029' }; /** * Escape a stringified JSON object that's going to be embedded in a `