more refactoring

pull/456/head
Rich-Harris 9 years ago
parent 4a782bc8f6
commit 9561a36eb2

@ -8,8 +8,6 @@ export default function visitAttribute ( generator, block, state, node, attribut
let metadata = state.namespace ? null : attributeLookup[ name ];
if ( metadata && metadata.appliesTo && !~metadata.appliesTo.indexOf( node.name ) ) metadata = null;
let dynamic = false;
const isIndirectlyBoundValue = name === 'value' && (
node.name === 'option' || // TODO check it's actually bound
node.name === 'input' && /^(checkbox|radio)$/.test( getStaticAttributeValue( node, 'type' ) )
@ -22,112 +20,73 @@ export default function visitAttribute ( generator, block, state, node, attribut
// HTML5?
const method = name.slice( 0, 6 ) === 'xlink:' ? 'setXlinkAttribute' : 'setAttribute';
// attributes without values, e.g. <textarea readonly>
if ( attribute.value === true ) {
if ( propertyName ) {
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = true;`
);
} else {
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', true );`
);
}
const isDynamic = attribute.value !== true && attribute.value.length > 1 || ( attribute.value.length === 1 && attribute.value[0].type !== 'Text' );
// special case autofocus. has to be handled in a bit of a weird way
if ( name === 'autofocus' ) {
block.autofocus = state.parentNode;
}
}
if ( isDynamic ) {
let value;
// empty string
else if ( attribute.value.length === 0 ) {
if ( propertyName ) {
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = '';`
);
if ( attribute.value.length === 1 ) {
// single {{tag}} — may be a non-string
const { snippet } = generator.contextualise( block, attribute.value[0].expression );
value = snippet;
} else {
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', '' );`
);
}
}
// static text or a single {{tag}}
else if ( attribute.value.length === 1 ) {
const value = attribute.value[0];
if ( value.type === 'Text' ) {
// static attributes
const result = JSON.stringify( value.data );
if ( propertyName ) {
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = ${result};`
);
// '{{foo}} {{bar}}' — treat as string concatenation
value = ( attribute.value[0].type === 'Text' ? '' : `"" + ` ) + (
attribute.value.map( chunk => {
if ( chunk.type === 'Text' ) {
return JSON.stringify( chunk.data );
} else {
if ( name === 'xmlns' ) {
// special case
// TODO this attribute must be static enforce at compile time
state.namespace = value.data;
const { snippet } = generator.contextualise( block, chunk.expression );
return `( ${snippet} )`;
}
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', ${result} );`
}).join( ' + ' )
);
}
}
else {
dynamic = true;
// dynamic but potentially non-string attributes
const { snippet } = generator.contextualise( block, value.expression );
const last = `last_${state.parentNode}_${name.replace( /-/g, '_')}`;
block.builders.create.addLine( `var ${last} = ${snippet};` );
block.builders.create.addLine( `var ${last} = ${value};` );
const updater = propertyName ?
`${state.parentNode}.${propertyName} = ${last};` :
`${generator.helper( method )}( ${state.parentNode}, '${name}', ${last} );`;
block.builders.create.addLine( updater );
block.builders.update.addBlock( deindent`
if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) {
if ( ( ${block.tmp()} = ${value} ) !== ${last} ) {
${last} = ${block.tmp()};
${updater}
}
` );
}
}
else {
dynamic = true;
const value = ( attribute.value[0].type === 'Text' ? '' : `"" + ` ) + (
attribute.value.map( chunk => {
if ( chunk.type === 'Text' ) {
return JSON.stringify( chunk.data );
} else {
const { snippet } = generator.contextualise( block, chunk.expression );
return `( ${snippet} )`;
}
}).join( ' + ' )
);
const value = attribute.value === true ? 'true' :
attribute.value.length === 0 ? `''` :
JSON.stringify( attribute.value[0].data );
const updater = propertyName ?
const statement = propertyName ?
`${state.parentNode}.${propertyName} = ${value};` :
`${generator.helper( method )}( ${state.parentNode}, '${name}', ${value} );`;
block.builders.create.addLine( updater );
block.builders.update.addLine( updater );
block.builders.create.addLine( statement );
// special case autofocus. has to be handled in a bit of a weird way
if ( attribute.value === true && name === 'autofocus' ) {
block.autofocus = state.parentNode;
}
// special case — xmlns
if ( name === 'xmlns' ) {
// TODO this attribute must be static enforce at compile time
state.namespace = attribute.value[0].data;
}
}
if ( isIndirectlyBoundValue ) {
const updateValue = `${state.parentNode}.value = ${state.parentNode}.__value;`;
block.builders.create.addLine( updateValue );
if ( dynamic ) block.builders.update.addLine( updateValue );
if ( isDynamic ) block.builders.update.addLine( updateValue );
}
}
Loading…
Cancel
Save