replace all ssr __helpers

pull/1367/head
Rich Harris 7 years ago
parent ebf1fe3233
commit 6cf3f1121f

@ -272,6 +272,10 @@ export default class Generator {
const global = `_svelteTransitionManager`; const global = `_svelteTransitionManager`;
inlineHelpers += `\n\nvar ${this.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; inlineHelpers += `\n\nvar ${this.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`;
} else if (key === 'escaped' || key === 'missingComponent') {
// vars are an awkward special case... would be nice to avoid this
const alias = this.alias(key);
inlineHelpers += `\n\nconst ${alias} = ${code};`
} else { } else {
const alias = this.alias(expression.id.name); const alias = this.alias(expression.id.name);
if (alias !== expression.id.name) { if (alias !== expression.id.name) {

@ -340,7 +340,7 @@ export default class Attribute extends Node {
return escapeTemplate(escape(chunk.data).replace(/"/g, '"')); return escapeTemplate(escape(chunk.data).replace(/"/g, '"'));
} }
return '${__escape(' + chunk.snippet + ')}'; return '${@escape(' + chunk.snippet + ')}';
}) })
.join(''); .join('');
} }

@ -225,7 +225,7 @@ export default class AwaitBlock extends Node {
const childBlock = block.child({}); const childBlock = block.child({});
compiler.append('${(function(__value) { if(__isPromise(__value)) return `'); compiler.append('${(function(__value) { if(@isPromise(__value)) return `');
this.pending.children.forEach((child: Node) => { this.pending.children.forEach((child: Node) => {
child.ssr(compiler, block); child.ssr(compiler, block);

@ -489,7 +489,7 @@ export default class Component extends Node {
return escapeTemplate(escape(chunk.data)); return escapeTemplate(escape(chunk.data));
} }
return '${__escape( ' + chunk.snippet + ')}'; return '${@escape( ' + chunk.snippet + ')}';
} }
const bindingProps = this.bindings.map(binding => { const bindingProps = this.bindings.map(binding => {
@ -541,7 +541,7 @@ export default class Component extends Node {
const expression = ( const expression = (
this.name === 'svelte:self' ? compiler.name : this.name === 'svelte:self' ? compiler.name :
isDynamicComponent ? `((${this.expression.snippet}) || __missingComponent)` : isDynamicComponent ? `((${this.expression.snippet}) || @missingComponent)` :
`%components-${this.name}` `%components-${this.name}`
); );

@ -483,7 +483,7 @@ export default class EachBlock extends Node {
? `(item, i) => Object.assign({}, ctx, { ${props.join(', ')}, ${this.index}: i })` ? `(item, i) => Object.assign({}, ctx, { ${props.join(', ')}, ${this.index}: i })`
: `item => Object.assign({}, ctx, { ${props.join(', ')} })`; : `item => Object.assign({}, ctx, { ${props.join(', ')} })`;
const open = `\${ ${this.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``; const open = `\${ ${this.else ? `${snippet}.length ? ` : ''}@each(${snippet}, ${getContext}, ctx => \``;
compiler.append(open); compiler.append(open);
const childBlock = block.child({}); const childBlock = block.child({});

@ -31,7 +31,7 @@ export default class MustacheTag extends Tag {
this.parent.type === 'Element' && this.parent.type === 'Element' &&
this.parent.name === 'style' this.parent.name === 'style'
? '${' + this.expression.snippet + '}' ? '${' + this.expression.snippet + '}'
: '${__escape(' + this.expression.snippet + ')}' : '${@escape(' + this.expression.snippet + ')}'
); );
} }
} }

@ -163,51 +163,6 @@ export default function ssr(
var warned = false; var warned = false;
${templateProperties.preload && `${name}.preload = %preload;`} ${templateProperties.preload && `${name}.preload = %preload;`}
${
// TODO this is a bit hacky
/__escape/.test(generator.renderCode) && deindent`
var escaped = {
'"': '"',
"'": '&##39;',
'&': '&',
'<': '&lt;',
'>': '&gt;'
};
function __escape(html) {
return String(html).replace(/["'&<>]/g, match => escaped[match]);
}
`
}
${
/__each/.test(generator.renderCode) && deindent`
function __each(items, assign, fn) {
let str = '';
for (let i = 0; i < items.length; i += 1) {
str += fn(assign(items[i], i));
}
return str;
}
`
}
${
/__isPromise/.test(generator.renderCode) && deindent`
function __isPromise(value) {
return value && typeof value.then === 'function';
}
`
}
${
/__missingComponent/.test(generator.renderCode) && deindent`
var __missingComponent = {
_render: () => ''
};
`
}
`.replace(/(@+|#+|%+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => { `.replace(/(@+|#+|%+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
if (sigil === '@') { if (sigil === '@') {
helpers.add(name); helpers.add(name);

@ -3,6 +3,7 @@ import { noop } from './utils.js';
export * from './dom.js'; export * from './dom.js';
export * from './keyed-each.js'; export * from './keyed-each.js';
export * from './spread.js'; export * from './spread.js';
export * from './ssr.js';
export * from './transitions.js'; export * from './transitions.js';
export * from './utils.js'; export * from './utils.js';

@ -10,4 +10,28 @@ export function spread(args) {
}); });
return str; return str;
} }
export const escaped = {
'"': '&quot;',
"'": '&#39;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
};
export function escape(html) {
return String(html).replace(/["'&<>]/g, match => escaped[match]);
}
export function each(items, assign, fn) {
let str = '';
for (let i = 0; i < items.length; i += 1) {
str += fn(assign(items[i], i));
}
return str;
}
export const missingComponent = {
_render: () => ''
};
Loading…
Cancel
Save