Merge branch 'sveltejs:master' into bugfix/destroy-empty-component

pull/7492/head
qinmu 4 years ago committed by GitHub
commit 8b6ffcea35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,9 @@
# Svelte changelog # Svelte changelog
## Unreleased
* Faster SSR ([#5701](https://github.com/sveltejs/svelte/pull/5701))
## 3.48.0 ## 3.48.0
* Allow creating cancelable custom events with `createEventDispatcher` ([#4623](https://github.com/sveltejs/svelte/issues/4623)) * Allow creating cancelable custom events with `createEventDispatcher` ([#4623](https://github.com/sveltejs/svelte/issues/4623))

@ -9,7 +9,7 @@ export function get_class_attribute_value(attribute: Attribute): ESTreeExpressio
// handle special case — `class={possiblyUndefined}` with scoped CSS // handle special case — `class={possiblyUndefined}` with scoped CSS
if (attribute.chunks.length === 2 && (attribute.chunks[1] as Text).synthetic) { if (attribute.chunks.length === 2 && (attribute.chunks[1] as Text).synthetic) {
const value = (attribute.chunks[0] as Expression).node; const value = (attribute.chunks[0] as Expression).node;
return x`@escape(@null_to_empty(${value})) + "${(attribute.chunks[1] as Text).data}"`; return x`@escape(@null_to_empty(${value}), true) + "${(attribute.chunks[1] as Text).data}"`;
} }
return get_attribute_value(attribute); return get_attribute_value(attribute);
@ -22,7 +22,7 @@ export function get_attribute_value(attribute: Attribute): ESTreeExpression {
.map((chunk) => { .map((chunk) => {
return chunk.type === 'Text' return chunk.type === 'Text'
? string_literal(chunk.data.replace(/"/g, '"')) as ESTreeExpression ? string_literal(chunk.data.replace(/"/g, '"')) as ESTreeExpression
: x`@escape(${chunk.node})`; : x`@escape(${chunk.node}, true)`;
}) })
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); .reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
} }

@ -69,20 +69,34 @@ export function merge_ssr_styles(style_attribute, style_directive) {
return style_object; return style_object;
} }
export const escaped = { const ATTR_REGEX = /[&"]/g;
'"': '&quot;', const CONTENT_REGEX = /[&<]/g;
"'": '&#39;',
'&': '&amp;', /**
'<': '&lt;', * Note: this method is performance sensitive and has been optimized
'>': '&gt;' * https://github.com/sveltejs/svelte/pull/5701
}; */
export function escape(value: unknown, is_attr = false) {
const str = String(value);
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
pattern.lastIndex = 0;
let escaped = '';
let last = 0;
while (pattern.test(str)) {
const i = pattern.lastIndex - 1;
const ch = str[i];
escaped += str.substring(last, i) + (ch === '&' ? '&amp;' : (ch === '"' ? '&quot;' : '&lt;'));
last = i + 1;
}
export function escape(html) { return escaped + str.substring(last);
return String(html).replace(/["'&<>]/g, match => escaped[match]);
} }
export function escape_attribute_value(value) { export function escape_attribute_value(value) {
return typeof value === 'string' ? escape(value) : value; return typeof value === 'string' ? escape(value, true) : value;
} }
export function escape_object(obj) { export function escape_object(obj) {

Loading…
Cancel
Save