Skip to content

Commit

Permalink
Merge pull request #499 from sveltejs/gh-495
Browse files Browse the repository at this point in the history
Deconflict variable names used in binding event handlers
  • Loading branch information
Rich-Harris authored Apr 19, 2017
2 parents 1dfd72c + b0095bd commit 0ed26a8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/generators/dom/visitors/Component/Binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenReference from '../../../../utils/flattenReference.js';
import getSetter from '../shared/binding/getSetter.js';

export default function visitBinding ( generator, block, state, node, attribute, local ) {
const { name, keypath } = flattenReference( attribute.value );
const { name } = flattenReference( attribute.value );
const { snippet, contexts, dependencies } = block.contextualise( attribute.value );

if ( dependencies.length > 1 ) throw new Error( 'An unexpected situation arose. Please raise an issue at https://github.com/sveltejs/svelte/issues — thanks!' );
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function visitBinding ( generator, block, state, node, attribute,
prop
});

const setter = getSetter({ block, name, keypath, context: '_context', attribute, dependencies, value: 'value' });
const setter = getSetter({ block, name, context: '_context', attribute, dependencies, value: 'value' });

generator.hasComplexBindings = true;

Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/Element/Binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function visitBinding ( generator, block, state, node, attribute
const bindingGroup = attribute.name === 'group' ? getBindingGroup( generator, keypath ) : null;
const value = getBindingValue( generator, block, state, node, attribute, isMultipleSelect, bindingGroup, type );

let setter = getSetter({ block, name, keypath, context: '_svelte', attribute, dependencies, value });
let setter = getSetter({ block, name, context: '_svelte', attribute, dependencies, value });
let updateElement = `${state.parentNode}.${attribute.name} = ${snippet};`;
const lock = block.getUniqueName( `${state.parentNode}_${attribute.name}_updating` );
let updateCondition = `!${lock}`;
Expand Down
13 changes: 8 additions & 5 deletions src/generators/dom/visitors/shared/binding/getSetter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import deindent from '../../../../../utils/deindent.js';

export default function getSetter ({ block, name, keypath, context, attribute, dependencies, value }) {
export default function getSetter ({ block, name, context, attribute, dependencies, value }) {
const tail = attribute.value.type === 'MemberExpression' ? getTailSnippet( attribute.value ) : '';

if ( block.contexts.has( name ) ) {
const prop = dependencies[0];
const tail = attribute.value.type === 'MemberExpression' ? getTailSnippet( attribute.value ) : '';

return deindent`
var list = this.${context}.${block.listNames.get( name )};
Expand All @@ -15,10 +16,12 @@ export default function getSetter ({ block, name, keypath, context, attribute, d
}

if ( attribute.value.type === 'MemberExpression' ) {
const alias = block.alias( name );

return deindent`
var ${name} = ${block.component}.get( '${name}' );
${keypath} = ${value};
${block.component}._set({ ${name}: ${name} });
var ${alias} = ${block.component}.get( '${name}' );
${alias}${tail} = ${value};
${block.component}._set({ ${name}: ${alias} });
`;
}

Expand Down
35 changes: 35 additions & 0 deletions test/runtime/samples/binding-input-text-deconflicted/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export default {
data: {
component: {
name: 'world'
}
},

html: `
<h1>Hello world!</h1>
<input>
`,

test ( assert, component, target, window ) {
const input = target.querySelector( 'input' );
assert.equal( input.value, 'world' );

const event = new window.Event( 'input' );

input.value = 'everybody';
input.dispatchEvent( event );

assert.equal( input.value, 'everybody' );
assert.htmlEqual( target.innerHTML, `
<h1>Hello everybody!</h1>
<input>
` );

component.set({ component: { name: 'goodbye' } });
assert.equal( input.value, 'goodbye' );
assert.htmlEqual( target.innerHTML, `
<h1>Hello goodbye!</h1>
<input>
` );
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Hello {{component.name}}!</h1>
<input bind:value="component.name"/>

0 comments on commit 0ed26a8

Please sign in to comment.