From e30dc8e3fa9fc5dca54b3aa692279fa70742c000 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 9 Dec 2017 16:04:51 -0500 Subject: [PATCH] put namespace on Element nodes --- src/generators/dom/State.ts | 2 -- src/generators/nodes/Attribute.ts | 8 +------- src/generators/nodes/Element.ts | 13 +++++-------- src/generators/nodes/Fragment.ts | 1 - 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/generators/dom/State.ts b/src/generators/dom/State.ts index de9814f7df..a60d83ab57 100644 --- a/src/generators/dom/State.ts +++ b/src/generators/dom/State.ts @@ -1,7 +1,6 @@ import { assign } from '../../shared/index.js'; interface StateData { - namespace?: string; parentNode?: string; parentNodes?: string; allUsedContexts?: string[]; @@ -9,7 +8,6 @@ interface StateData { } export default class State { - namespace?: string; parentNode?: string; parentNodes?: string; allUsedContexts?: string[]; diff --git a/src/generators/nodes/Attribute.ts b/src/generators/nodes/Attribute.ts index 98a63f8ff3..043694f083 100644 --- a/src/generators/nodes/Attribute.ts +++ b/src/generators/nodes/Attribute.ts @@ -57,7 +57,7 @@ export default class Attribute { } } - let metadata = state.namespace ? null : attributeLookup[name]; + let metadata = node.namespace ? null : attributeLookup[name]; if (metadata && metadata.appliesTo && !~metadata.appliesTo.indexOf(node.name)) metadata = null; @@ -252,12 +252,6 @@ export default class Attribute { if (this.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 = this.value[0].data; - } } if (isIndirectlyBoundValue) { diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 668b257b49..62e6262e8c 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -33,10 +33,10 @@ export default class Element extends Node { this.cannotUseInnerHTML(); } - const parentElement = this.findNearest('Element'); + const parentElement = this.parent && this.parent.findNearest('Element'); this.namespace = this.name === 'svg' ? namespaces.svg : - parentElement.namespace; + parentElement ? parentElement.namespace : this.generator.namespace; this.attributes.forEach(attribute => { if (attribute.type === 'Attribute' && attribute.value !== true) { @@ -179,9 +179,6 @@ export default class Element extends Node { const childState = state.child({ parentNode: this.var, parentNodes: block.getUniqueName(`${this.var}_nodes`), - namespace: this.name === 'svg' - ? 'http://www.w3.org/2000/svg' - : state.namespace, allUsedContexts: [], }); @@ -196,14 +193,14 @@ export default class Element extends Node { block.builders.create.addLine( `${name} = ${getRenderStatement( this.generator, - childState.namespace, + this.namespace, this.name )};` ); if (this.generator.hydratable) { block.builders.claim.addBlock(deindent` - ${name} = ${getClaimStatement(generator, childState.namespace, state.parentNodes, this)}; + ${name} = ${getClaimStatement(generator, this.namespace, state.parentNodes, this)}; var ${childState.parentNodes} = @children(${name}); `); } @@ -235,7 +232,7 @@ export default class Element extends Node { } // insert static children with textContent or innerHTML - if (!childState.namespace && this.canUseInnerHTML && this.children.length > 0) { + if (!this.namespace && this.canUseInnerHTML && this.children.length > 0) { if (this.children.length === 1 && this.children[0].type === 'Text') { block.builders.create.addLine( `${name}.textContent = ${stringify(this.children[0].data)};` diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index cb824825a1..bfdbac2a85 100644 --- a/src/generators/nodes/Fragment.ts +++ b/src/generators/nodes/Fragment.ts @@ -35,7 +35,6 @@ export default class Fragment extends Node { this.init(); const state = new State({ - namespace: this.generator.namespace, parentNode: null, parentNodes: 'nodes' });