Skip to content

Commit 7f609ab

Browse files
committed
start function analysis, only $.escape when necessary
1 parent 4f1f4cc commit 7f609ab

File tree

3 files changed

+413
-40
lines changed
  • packages/svelte

3 files changed

+413
-40
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/utils.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import * as b from '#compiler/builders';
1212
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
1313
import { regex_whitespaces_strict } from '../../../../patterns.js';
14+
import { NUMBER } from '../../../../scope.js';
1415

1516
/** Opens an if/each block, so that we can remove nodes in the case of a mismatch */
1617
export const block_open = b.literal(BLOCK_OPEN);
@@ -45,13 +46,19 @@ export function process_children(nodes, { visit, state }) {
4546
quasi.value.cooked +=
4647
node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
4748
} else {
48-
const evaluated = state.scope.evaluate(node.expression);
49-
49+
const expression = /** @type {Expression} */ (visit(node.expression));
50+
const evaluated = state.scope.evaluate(expression);
5051
if (evaluated.is_known) {
5152
quasi.value.cooked += escape_html((evaluated.value ?? '') + '');
5253
} else {
53-
expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));
54-
54+
if (
55+
(evaluated.values.size === 1 && [...evaluated.values][0] === NUMBER) ||
56+
[...evaluated.values].every((value) => typeof value === 'string' && !/[&<]/.test(value))
57+
) {
58+
expressions.push(expression);
59+
} else {
60+
expressions.push(b.call('$.escape', expression));
61+
}
5562
quasi = b.quasi('', i + 1 === sequence.length);
5663
quasis.push(quasi);
5764
}

0 commit comments

Comments
 (0)