move some files, tidy up a bit

pull/456/head
Rich-Harris 8 years ago
parent 57f1b64ddb
commit e960e8b5c5

@ -1,6 +1,6 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../../utils/deindent.js';
import CodeBuilder from '../../../utils/CodeBuilder.js'; import CodeBuilder from '../../../../utils/CodeBuilder.js';
import visit from '../visit.js'; import visit from '../../visit.js';
import addComponentAttributes from './attributes/addComponentAttributes.js'; import addComponentAttributes from './attributes/addComponentAttributes.js';
function capDown ( name ) { function capDown ( name ) {

@ -1,5 +1,5 @@
import addComponentBinding from './addComponentBinding.js'; import addComponentBinding from './addComponentBinding.js';
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent.js';
export default function addComponentAttributes ( generator, block, node, local ) { export default function addComponentAttributes ( generator, block, node, local ) {
local.staticAttributes = []; local.staticAttributes = [];

@ -1,6 +1,6 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js'; import flattenReference from '../../../../../utils/flattenReference.js';
import getSetter from './binding/getSetter.js'; import getSetter from '../../shared/binding/getSetter.js';
export default function addComponentBinding ( generator, node, attribute, block, local ) { export default function addComponentBinding ( generator, node, attribute, block, local ) {
const { name, keypath } = flattenReference( attribute.value ); const { name, keypath } = flattenReference( attribute.value );

@ -1,8 +1,7 @@
import CodeBuilder from '../../../utils/CodeBuilder.js'; import deindent from '../../../../utils/deindent.js';
import deindent from '../../../utils/deindent.js'; import visit from '../../visit.js';
import visit from '../visit.js';
import addElementAttributes from './attributes/addElementAttributes.js'; import addElementAttributes from './attributes/addElementAttributes.js';
import visitComponent from './Component.js'; import visitComponent from '../Component/Component.js';
import visitWindow from './meta/Window.js'; import visitWindow from './meta/Window.js';
const meta = { const meta = {
@ -26,15 +25,20 @@ export default function visitElement ( generator, block, state, node ) {
namespace: node.name === 'svg' ? 'http://www.w3.org/2000/svg' : state.namespace namespace: node.name === 'svg' ? 'http://www.w3.org/2000/svg' : state.namespace
}); });
const local = { block.builders.create.addLine( `var ${name} = ${getRenderStatement( generator, childState.namespace, node.name )};` );
allUsedContexts: [],
create: new CodeBuilder(), if ( !state.parentNode ) {
update: new CodeBuilder(), block.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` );
destroy: new CodeBuilder() }
};
const isToplevel = !state.parentNode; // add CSS encapsulation attribute
if ( generator.cssId && state.isTopLevel ) {
block.builders.create.addLine( `${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );` );
}
const local = {
allUsedContexts: []
};
addElementAttributes( generator, block, childState, node, local ); addElementAttributes( generator, block, childState, node, local );
@ -57,47 +61,22 @@ export default function visitElement ( generator, block, state, node ) {
return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`; return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`;
}).join( '\n' ); }).join( '\n' );
local.create.addBlock( deindent` block.builders.create.addBlock( deindent`
${name}.__svelte = { ${name}.__svelte = {
${initialProps} ${initialProps}
}; };
` ); ` );
local.update.addBlock( updates ); block.builders.update.addBlock( updates );
}
let render;
if ( childState.namespace ) {
if ( childState.namespace === 'http://www.w3.org/2000/svg' ) {
render = `var ${name} = ${generator.helper( 'createSvgElement' )}( '${node.name}' )`;
} else {
render = `var ${name} = document.createElementNS( '${childState.namespace}', '${node.name}' );`;
}
} else {
render = `var ${name} = ${generator.helper( 'createElement' )}( '${node.name}' );`;
}
if ( generator.cssId && state.isTopLevel ) {
render += `\n${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );`;
}
local.create.addLineAtStart( render );
if ( isToplevel ) {
block.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` );
} }
// special case bound <option> without a value attribute // special case bound <option> without a value attribute
if ( node.name === 'option' && !node.attributes.find( attribute => attribute.type === 'Attribute' && attribute.name === 'value' ) ) { // TODO check it's bound if ( node.name === 'option' && !node.attributes.find( attribute => attribute.type === 'Attribute' && attribute.name === 'value' ) ) { // TODO check it's bound
const statement = `${name}.__value = ${name}.textContent;`; const statement = `${name}.__value = ${name}.textContent;`;
local.update.addLine( statement ); block.builders.update.addLine( statement );
node.initialUpdate = statement; node.initialUpdate = statement;
} }
block.builders.create.addBlock( local.create );
if ( !local.update.isEmpty() ) block.builders.update.addBlock( local.update );
if ( !local.destroy.isEmpty() ) block.builders.destroy.addBlock( local.destroy );
block.createMountStatement( name, state.parentNode ); block.createMountStatement( name, state.parentNode );
node.children.forEach( child => { node.children.forEach( child => {
@ -108,3 +87,15 @@ export default function visitElement ( generator, block, state, node ) {
block.builders.create.addBlock( node.initialUpdate ); block.builders.create.addBlock( node.initialUpdate );
} }
} }
function getRenderStatement ( generator, namespace, name ) {
if ( namespace ) {
if ( namespace === 'http://www.w3.org/2000/svg' ) {
return `${generator.helper( 'createSvgElement' )}( '${name}' )`;
}
return `document.createElementNS( '${namespace}', '${name}' )`;
}
return `${generator.helper( 'createElement' )}( '${name}' )`;
}

@ -1,8 +1,8 @@
import attributeLookup from './lookup.js'; import attributeLookup from './lookup.js';
import addElementBinding from './addElementBinding'; import addElementBinding from './addElementBinding';
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js'; import flattenReference from '../../../../../utils/flattenReference.js';
import getStaticAttributeValue from './binding/getStaticAttributeValue.js'; import getStaticAttributeValue from './getStaticAttributeValue.js';
export default function addElementAttributes ( generator, block, state, node, local ) { export default function addElementAttributes ( generator, block, state, node, local ) {
node.attributes.forEach( attribute => { node.attributes.forEach( attribute => {
@ -31,11 +31,11 @@ export default function addElementAttributes ( generator, block, state, node, lo
if ( attribute.value === true ) { if ( attribute.value === true ) {
// attributes without values, e.g. <textarea readonly> // attributes without values, e.g. <textarea readonly>
if ( propertyName ) { if ( propertyName ) {
local.create.addLine( block.builders.create.addLine(
`${state.parentNode}.${propertyName} = true;` `${state.parentNode}.${propertyName} = true;`
); );
} else { } else {
local.create.addLine( block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', true );` `${generator.helper( method )}( ${state.parentNode}, '${name}', true );`
); );
} }
@ -48,11 +48,11 @@ export default function addElementAttributes ( generator, block, state, node, lo
else if ( attribute.value.length === 0 ) { else if ( attribute.value.length === 0 ) {
if ( propertyName ) { if ( propertyName ) {
local.create.addLine( block.builders.create.addLine(
`${state.parentNode}.${propertyName} = '';` `${state.parentNode}.${propertyName} = '';`
); );
} else { } else {
local.create.addLine( block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', '' );` `${generator.helper( method )}( ${state.parentNode}, '${name}', '' );`
); );
} }
@ -74,7 +74,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
state.namespace = value.data; state.namespace = value.data;
addAttribute = true; addAttribute = true;
} else if ( propertyName ) { } else if ( propertyName ) {
local.create.addLine( block.builders.create.addLine(
`${state.parentNode}.${propertyName} = ${result};` `${state.parentNode}.${propertyName} = ${result};`
); );
} else { } else {
@ -82,7 +82,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
} }
if ( addAttribute ) { if ( addAttribute ) {
local.create.addLine( block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', ${result} );` `${generator.helper( method )}( ${state.parentNode}, '${name}', ${result} );`
); );
} }
@ -95,7 +95,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
const { snippet } = generator.contextualise( block, value.expression ); const { snippet } = generator.contextualise( block, value.expression );
const last = `last_${state.parentNode}_${name.replace( /-/g, '_')}`; const last = `last_${state.parentNode}_${name.replace( /-/g, '_')}`;
local.create.addLine( `var ${last} = ${snippet};` ); block.builders.create.addLine( `var ${last} = ${snippet};` );
let updater; let updater;
if ( propertyName ) { if ( propertyName ) {
@ -104,9 +104,9 @@ export default function addElementAttributes ( generator, block, state, node, lo
updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${last} );`; updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${last} );`;
} }
local.create.addLine( updater ); block.builders.create.addLine( updater );
local.update.addBlock( deindent` block.builders.update.addBlock( deindent`
if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) { if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) {
${last} = ${block.tmp()}; ${last} = ${block.tmp()};
${updater} ${updater}
@ -136,15 +136,15 @@ export default function addElementAttributes ( generator, block, state, node, lo
updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${value} );`; updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${value} );`;
} }
local.create.addLine( updater ); block.builders.create.addLine( updater );
local.update.addLine( updater ); block.builders.update.addLine( updater );
} }
if ( isIndirectlyBoundValue ) { if ( isIndirectlyBoundValue ) {
const updateValue = `${state.parentNode}.value = ${state.parentNode}.__value;`; const updateValue = `${state.parentNode}.value = ${state.parentNode}.__value;`;
local.create.addLine( updateValue ); block.builders.create.addLine( updateValue );
if ( dynamic ) local.update.addLine( updateValue ); if ( dynamic ) block.builders.update.addLine( updateValue );
} }
} }
@ -182,7 +182,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`; const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`;
if ( generator.events.has( name ) ) { if ( generator.events.has( name ) ) {
local.create.addBlock( deindent` block.builders.create.addBlock( deindent`
var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${block.component}, ${state.parentNode}, function ( event ) { var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${block.component}, ${state.parentNode}, function ( event ) {
${handlerBody} ${handlerBody}
}.bind( ${state.parentNode} ) ); }.bind( ${state.parentNode} ) );
@ -192,7 +192,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
${handlerName}.teardown(); ${handlerName}.teardown();
` ); ` );
} else { } else {
local.create.addBlock( deindent` block.builders.create.addBlock( deindent`
function ${handlerName} ( event ) { function ${handlerName} ( event ) {
${handlerBody} ${handlerBody}
} }
@ -213,7 +213,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
else if ( attribute.type === 'Ref' ) { else if ( attribute.type === 'Ref' ) {
generator.usesRefs = true; generator.usesRefs = true;
local.create.addLine( block.builders.create.addLine(
`${block.component}.refs.${name} = ${state.parentNode};` `${block.component}.refs.${name} = ${state.parentNode};`
); );

@ -1,7 +1,7 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js'; import flattenReference from '../../../../../utils/flattenReference.js';
import getSetter from './binding/getSetter.js'; import getSetter from '../../shared/binding/getSetter.js';
import getStaticAttributeValue from './binding/getStaticAttributeValue.js'; import getStaticAttributeValue from './getStaticAttributeValue.js';
export default function addElementBinding ( generator, node, block, state, attribute, local ) { export default function addElementBinding ( generator, node, block, state, attribute, local ) {
const { name, keypath } = flattenReference( attribute.value ); const { name, keypath } = flattenReference( attribute.value );
@ -66,11 +66,11 @@ export default function addElementBinding ( generator, node, block, state, attri
`~${snippet}.indexOf( ${state.parentNode}.__value )` : `~${snippet}.indexOf( ${state.parentNode}.__value )` :
`${state.parentNode}.__value === ${snippet}`; `${state.parentNode}.__value === ${snippet}`;
local.create.addLine( block.builders.create.addLine(
`${block.component}._bindingGroups[${bindingGroup}].push( ${state.parentNode} );` `${block.component}._bindingGroups[${bindingGroup}].push( ${state.parentNode} );`
); );
local.destroy.addBlock( block.builders.destroy.addBlock(
`${block.component}._bindingGroups[${bindingGroup}].splice( ${block.component}._bindingGroups[${bindingGroup}].indexOf( ${state.parentNode} ), 1 );` `${block.component}._bindingGroups[${bindingGroup}].splice( ${block.component}._bindingGroups[${bindingGroup}].indexOf( ${state.parentNode} ), 1 );`
); );
@ -84,7 +84,7 @@ export default function addElementBinding ( generator, node, block, state, attri
const updating = block.getUniqueName( `${state.parentNode}_updating` ); const updating = block.getUniqueName( `${state.parentNode}_updating` );
local.create.addBlock( deindent` block.builders.create.addBlock( deindent`
var ${updating} = false; var ${updating} = false;
function ${handler} () { function ${handler} () {
@ -98,7 +98,7 @@ export default function addElementBinding ( generator, node, block, state, attri
node.initialUpdate = updateElement; node.initialUpdate = updateElement;
local.update.addLine( deindent` block.builders.update.addLine( deindent`
if ( !${updating} ) { if ( !${updating} ) {
${updateElement} ${updateElement}
} }

@ -1,5 +1,5 @@
import flattenReference from '../../../../utils/flattenReference.js'; import flattenReference from '../../../../../utils/flattenReference.js';
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent.js';
const associatedEvents = { const associatedEvents = {
innerWidth: 'resize', innerWidth: 'resize',

@ -1,6 +1,6 @@
import Comment from './Comment.js'; import Comment from './Comment.js';
import EachBlock from './EachBlock.js'; import EachBlock from './EachBlock.js';
import Element from './Element.js'; import Element from './Element/Element.js';
import IfBlock from './IfBlock.js'; import IfBlock from './IfBlock.js';
import MustacheTag from './MustacheTag.js'; import MustacheTag from './MustacheTag.js';
import RawMustacheTag from './RawMustacheTag.js'; import RawMustacheTag from './RawMustacheTag.js';

Loading…
Cancel
Save