diff --git a/src/compiler/compile/render_dom/wrappers/Element/index.ts b/src/compiler/compile/render_dom/wrappers/Element/index.ts index 83bc8be94e..733125ae06 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/index.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/index.ts @@ -676,7 +676,7 @@ export default class ElementWrapper extends Wrapper { initial_props.push(snippet); - updates.push(condition ? x`${condition} && ${snippet}` : snippet); + updates.push(condition ? x`${condition} && ${snippet}` : x`false`); } else { const metadata = attr.get_metadata(); const name = attr.is_indirectly_bound_value() @@ -685,17 +685,14 @@ export default class ElementWrapper extends Wrapper { const snippet = x`{ ${name}: ${attr.get_value(block)} }`; initial_props.push(snippet); - updates.push(condition ? x`${condition} && ${snippet}` : snippet); + updates.push(condition ? x`${condition} && ${snippet}` : x`false`); } }); block.chunks.init.push(b` let ${levels} = [${initial_props}]; - let ${data} = {}; - for (let #i = 0; #i < ${levels}.length; #i += 1) { - ${data} = @assign(${data}, ${levels}[#i]); - } + let ${data} = @get_attributes_for_spread(${levels}); `); const fn = this.node.namespace === namespaces.svg ? x`@set_svg_attributes` : x`@set_attributes`; diff --git a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts index 00f803bbbd..b6bcafd8f5 100644 --- a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts +++ b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts @@ -252,7 +252,7 @@ export default class InlineComponentWrapper extends Wrapper { changes.push( unchanged - ? x`${levels}[${i}]` + ? x`false` : condition ? x`${condition} && ${change_object}` : change_object @@ -266,9 +266,7 @@ export default class InlineComponentWrapper extends Wrapper { `); statements.push(b` - for (let #i = 0; #i < ${levels}.length; #i += 1) { - ${props} = @assign(${props}, ${levels}[#i]); - } + ${props} = @assign(${props}, @get_attributes_for_spread(${levels})) `); if (all_dependencies.size) { diff --git a/src/runtime/internal/spread.ts b/src/runtime/internal/spread.ts index 203b0b11e9..be2fe8ad8f 100644 --- a/src/runtime/internal/spread.ts +++ b/src/runtime/internal/spread.ts @@ -1,3 +1,5 @@ +import { assign } from "./utils"; + export function get_spread_update(levels, updates) { const update = {}; @@ -11,7 +13,7 @@ export function get_spread_update(levels, updates) { if (n) { for (const key in o) { - if (!(key in n)) to_null_out[key] = 1; + if (!(key in n) && !accounted_for[key]) to_null_out[key] = 1; } for (const key in n) { @@ -24,6 +26,11 @@ export function get_spread_update(levels, updates) { levels[i] = n; } else { for (const key in o) { + // if spreads decides to null out the key + // should reset it back for static attribute + if (to_null_out[key]) { + update[key] = o[key]; + } accounted_for[key] = 1; } } @@ -36,6 +43,14 @@ export function get_spread_update(levels, updates) { return update; } +export function get_attributes_for_spread(levels) { + let attrs = {}; + for (let i = 0; i < levels.length; i += 1) { + attrs = assign(attrs, levels[i]); + } + return attrs; +} + export function get_spread_object(spread_props) { return typeof spread_props === 'object' && spread_props !== null ? spread_props : {}; } \ No newline at end of file