Skip to content

Commit 9b80eee

Browse files
committed
Prioritize named over spread attributes
1 parent b5102f4 commit 9b80eee

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/generators/nodes/Spread.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ export default class Spread {
6262

6363
const changes = block.getUniqueName(`${node.var}_spread_changes`);
6464

65+
const namedAttributes = block.getUniqueName(`${node.var}_attributes`);
66+
block.builders.init.addBlock(deindent`
67+
var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}];
68+
`)
69+
6570
block.builders.hydrate.addBlock(deindent`
6671
var ${changes} = ${init};
6772
for (var key in ${changes}) {
73+
if (${namedAttributes}.indexOf(key) !== -1) continue;
74+
6875
@setAttribute(${node.var}, key, ${changes}[key]);
6976
${activeKeys}[key] = true;
7077
}
@@ -90,9 +97,12 @@ export default class Spread {
9097
9198
var ${changes} = ${shouldCache ? last : value};
9299
for (var key in ${changes}) {
100+
if (${namedAttributes}.indexOf(key) !== -1) continue;
101+
102+
@setAttribute(${node.var}, key, ${changes}[key]);
103+
93104
${activeKeys}[key] = true;
94105
delete ${oldKeys}[key];
95-
@setAttribute(${node.var}, key, ${changes}[key]);
96106
}
97107
98108
for (var key in ${oldKeys}) {
@@ -135,6 +145,11 @@ export default class Spread {
135145

136146
const changes = block.getUniqueName(`${node.var}_spread_changes`);
137147

148+
const namedAttributes = block.getUniqueName(`${node.var}_attributes`);
149+
block.builders.init.addBlock(deindent`
150+
var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}];
151+
`)
152+
138153
if (dependencies.length || hasChangeableIndex) {
139154
const changedCheck = (
140155
( block.hasOutroMethod ? `#outroing || ` : '' ) +
@@ -156,9 +171,12 @@ export default class Spread {
156171
157172
var ${changes} = ${shouldCache ? last : value};
158173
for (var key in ${changes}) {
174+
if (${namedAttributes}.indexOf(key) !== -1) continue;
175+
176+
${node.var}_changes[key] = ${changes}[key];
177+
159178
${activeKeys}[key] = true;
160179
delete ${oldKeys}[key];
161-
${node.var}_changes[key] = ${changes}[key];
162180
}
163181
164182
for (var key in ${oldKeys}) {

test/runtime/samples/spread-component/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
}
1111
},
1212

13-
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>`,
13+
html: `<div><p>foo: lol</p>\n<p>baz: 42 (number)</p>\n<p>qux: named</p>\n<p>quux: core</p></div>`,
1414

1515
test ( assert, component, target ) {
1616
component.set({
@@ -22,6 +22,6 @@ export default {
2222
}
2323
});
2424

25-
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>` );
25+
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>` );
2626
}
2727
};

test/runtime/samples/spread-component/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
2-
<Widget {{...props}}/>
2+
<Widget {{...props}} qux="named"/>
33
</div>
44

55
<script>
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
export default {
22
solo: true,
33

4-
html: `<div data-foo="bar">red</div>`,
4+
html: `<div data-named="value" data-foo="bar">red</div>`,
55

66
test ( assert, component, target ) {
77
const div = target.querySelector( 'div' );
88

99
assert.equal( div.dataset.foo, 'bar' );
10+
assert.equal( div.dataset.named, 'value' );
1011

11-
component.set({ color: 'blue', props: { 'data-foo': 'baz' } });
12-
assert.equal( target.innerHTML, `<div data-foo="baz">blue</div>` );
12+
component.set({ color: 'blue', props: { 'data-foo': 'baz', 'data-named': 'qux' } });
13+
assert.equal( target.innerHTML, `<div data-named="value" data-foo="baz">blue</div>` );
1314
assert.equal( div.dataset.foo, 'baz' );
15+
assert.equal( div.dataset.named, 'value' );
1416

1517
component.set({ color: 'blue', props: {} });
16-
assert.equal( target.innerHTML, `<div>blue</div>` );
18+
assert.equal( target.innerHTML, `<div data-named="value">blue</div>` );
1719
assert.equal( div.dataset.foo, undefined );
1820
}
1921
};

test/runtime/samples/spread-element/main.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
<div {{...props}}>{{color}}</div>
1+
<div {{...props}} data-named="value">{{color}}</div>
22

33
<script>
44
export default {
55
data: () => ({
66
color: 'red',
77
props: {
88
'data-foo': 'bar',
9+
'data-named': 'qux'
910
}
1011
})
1112
};

0 commit comments

Comments
 (0)