Prioritize named over spread attributes

pull/1248/head
mrkishi 7 years ago
parent b5102f4f1b
commit 9b80eee51a

@ -62,9 +62,16 @@ export default class Spread {
const changes = block.getUniqueName(`${node.var}_spread_changes`); const changes = block.getUniqueName(`${node.var}_spread_changes`);
const namedAttributes = block.getUniqueName(`${node.var}_attributes`);
block.builders.init.addBlock(deindent`
var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}];
`)
block.builders.hydrate.addBlock(deindent` block.builders.hydrate.addBlock(deindent`
var ${changes} = ${init}; var ${changes} = ${init};
for (var key in ${changes}) { for (var key in ${changes}) {
if (${namedAttributes}.indexOf(key) !== -1) continue;
@setAttribute(${node.var}, key, ${changes}[key]); @setAttribute(${node.var}, key, ${changes}[key]);
${activeKeys}[key] = true; ${activeKeys}[key] = true;
} }
@ -90,9 +97,12 @@ export default class Spread {
var ${changes} = ${shouldCache ? last : value}; var ${changes} = ${shouldCache ? last : value};
for (var key in ${changes}) { for (var key in ${changes}) {
if (${namedAttributes}.indexOf(key) !== -1) continue;
@setAttribute(${node.var}, key, ${changes}[key]);
${activeKeys}[key] = true; ${activeKeys}[key] = true;
delete ${oldKeys}[key]; delete ${oldKeys}[key];
@setAttribute(${node.var}, key, ${changes}[key]);
} }
for (var key in ${oldKeys}) { for (var key in ${oldKeys}) {
@ -135,6 +145,11 @@ export default class Spread {
const changes = block.getUniqueName(`${node.var}_spread_changes`); const changes = block.getUniqueName(`${node.var}_spread_changes`);
const namedAttributes = block.getUniqueName(`${node.var}_attributes`);
block.builders.init.addBlock(deindent`
var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}];
`)
if (dependencies.length || hasChangeableIndex) { if (dependencies.length || hasChangeableIndex) {
const changedCheck = ( const changedCheck = (
( block.hasOutroMethod ? `#outroing || ` : '' ) + ( block.hasOutroMethod ? `#outroing || ` : '' ) +
@ -156,9 +171,12 @@ export default class Spread {
var ${changes} = ${shouldCache ? last : value}; var ${changes} = ${shouldCache ? last : value};
for (var key in ${changes}) { for (var key in ${changes}) {
if (${namedAttributes}.indexOf(key) !== -1) continue;
${node.var}_changes[key] = ${changes}[key];
${activeKeys}[key] = true; ${activeKeys}[key] = true;
delete ${oldKeys}[key]; delete ${oldKeys}[key];
${node.var}_changes[key] = ${changes}[key];
} }
for (var key in ${oldKeys}) { for (var key in ${oldKeys}) {

@ -10,7 +10,7 @@ export default {
} }
}, },
html: `<div><p>foo: lol</p>\n<p>baz: 42 (number)</p>\n<p>qux: this is a piece of string</p>\n<p>quux: core</p></div>`, html: `<div><p>foo: lol</p>\n<p>baz: 42 (number)</p>\n<p>qux: named</p>\n<p>quux: core</p></div>`,
test ( assert, component, target ) { test ( assert, component, target ) {
component.set({ component.set({
@ -22,6 +22,6 @@ export default {
} }
}); });
assert.equal( target.innerHTML, `<div><p>foo: wut</p>\n<p>baz: 43 (number)</p>\n<p>qux: this is a rather boring string</p>\n<p>quux: heart</p></div>` ); assert.equal( target.innerHTML, `<div><p>foo: wut</p>\n<p>baz: 43 (number)</p>\n<p>qux: named</p>\n<p>quux: heart</p></div>` );
} }
}; };

@ -1,5 +1,5 @@
<div> <div>
<Widget {{...props}}/> <Widget {{...props}} qux="named"/>
</div> </div>
<script> <script>

@ -1,19 +1,21 @@
export default { export default {
solo: true, solo: true,
html: `<div data-foo="bar">red</div>`, html: `<div data-named="value" data-foo="bar">red</div>`,
test ( assert, component, target ) { test ( assert, component, target ) {
const div = target.querySelector( 'div' ); const div = target.querySelector( 'div' );
assert.equal( div.dataset.foo, 'bar' ); assert.equal( div.dataset.foo, 'bar' );
assert.equal( div.dataset.named, 'value' );
component.set({ color: 'blue', props: { 'data-foo': 'baz' } }); component.set({ color: 'blue', props: { 'data-foo': 'baz', 'data-named': 'qux' } });
assert.equal( target.innerHTML, `<div data-foo="baz">blue</div>` ); assert.equal( target.innerHTML, `<div data-named="value" data-foo="baz">blue</div>` );
assert.equal( div.dataset.foo, 'baz' ); assert.equal( div.dataset.foo, 'baz' );
assert.equal( div.dataset.named, 'value' );
component.set({ color: 'blue', props: {} }); component.set({ color: 'blue', props: {} });
assert.equal( target.innerHTML, `<div>blue</div>` ); assert.equal( target.innerHTML, `<div data-named="value">blue</div>` );
assert.equal( div.dataset.foo, undefined ); assert.equal( div.dataset.foo, undefined );
} }
}; };

@ -1,4 +1,4 @@
<div {{...props}}>{{color}}</div> <div {{...props}} data-named="value">{{color}}</div>
<script> <script>
export default { export default {
@ -6,6 +6,7 @@
color: 'red', color: 'red',
props: { props: {
'data-foo': 'bar', 'data-foo': 'bar',
'data-named': 'qux'
} }
}) })
}; };

Loading…
Cancel
Save