0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-30 00:46:29 +01:00

add a createAnchor helper

This commit is contained in:
Arpad Borsos 2016-12-03 11:57:09 +01:00
parent c1ba15875e
commit 3f300c9b71
No known key found for this signature in database
GPG Key ID: 908EDF65263368B4
3 changed files with 9 additions and 4 deletions

View File

@ -45,6 +45,13 @@ export default function generate ( parsed, source, options ) {
} }
}, },
createAnchor ( _name, description = '' ) {
const name = `${_name}_anchor`;
const statement = `document.createComment( ${JSON.stringify( description )} )`;
generator.addElement( name, statement, true );
return name;
},
addRenderer ( fragment ) { addRenderer ( fragment ) {
if ( fragment.autofocus ) { if ( fragment.autofocus ) {
fragment.initStatements.push( `${fragment.autofocus}.focus();` ); fragment.initStatements.push( `${fragment.autofocus}.focus();` );

View File

@ -5,7 +5,6 @@ export default {
enter ( generator, node ) { enter ( generator, node ) {
const i = generator.counters.each++; const i = generator.counters.each++;
const name = `eachBlock_${i}`; const name = `eachBlock_${i}`;
const anchor = `${name}_anchor`;
const renderer = `renderEachBlock_${i}`; const renderer = `renderEachBlock_${i}`;
const listName = `${name}_value`; const listName = `${name}_value`;
@ -14,7 +13,7 @@ export default {
const { dependencies, snippet } = generator.contextualise( node.expression ); const { dependencies, snippet } = generator.contextualise( node.expression );
generator.addElement( anchor, `document.createComment( ${JSON.stringify( `#each ${generator.source.slice( node.expression.start, node.expression.end )}` )} )`, true ); const anchor = generator.createAnchor( name, `#each ${generator.source.slice( node.expression.start, node.expression.end )}` );
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
var ${name}_value = ${snippet}; var ${name}_value = ${snippet};

View File

@ -56,13 +56,12 @@ export default {
const { params, target } = generator.current; const { params, target } = generator.current;
const name = `ifBlock_${i}`; const name = `ifBlock_${i}`;
const anchor = `${name}_anchor`;
const getBlock = `getBlock_${i}`; const getBlock = `getBlock_${i}`;
const currentBlock = `currentBlock_${i}`; const currentBlock = `currentBlock_${i}`;
const conditionsAndBlocks = getConditionsAndBlocks( generator, node, `renderIfBlock_${i}` ); const conditionsAndBlocks = getConditionsAndBlocks( generator, node, `renderIfBlock_${i}` );
generator.addElement( anchor, `document.createComment( ${JSON.stringify( `#if ${generator.source.slice( node.expression.start, node.expression.end )}` )} )`, true ); const anchor = generator.createAnchor( name, `#if ${generator.source.slice( node.expression.start, node.expression.end )}` );
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
function ${getBlock} ( ${params} ) { function ${getBlock} ( ${params} ) {