more simplification

pull/992/head
Rich Harris 8 years ago
parent 05d63e6ba7
commit 16bac3bbca

@ -64,11 +64,12 @@ export default class AwaitBlock extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const name = this.var;
const needsAnchor = this.next ? !this.next.isDomNode() : !state.parentNode || !this.parent.isDomNode();
const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode();
const anchor = needsAnchor
? block.getUniqueName(`${name}_anchor`)
: (this.next && this.next.var) || 'null';
@ -83,7 +84,7 @@ export default class AwaitBlock extends Node {
anchor,
`@createComment()`,
`@createComment()`,
state.parentNode
parentNode
);
}
@ -119,7 +120,7 @@ export default class AwaitBlock extends Node {
${old_block}.u();
${old_block}.d();
${await_block}.c();
${await_block}.m(${state.parentNode || `${anchor}.parentNode`}, ${anchor});
${await_block}.m(${parentNode || `${anchor}.parentNode`}, ${anchor});
}
}
@ -155,11 +156,11 @@ export default class AwaitBlock extends Node {
`);
block.builders.claim.addBlock(deindent`
${await_block}.l(${state.parentNodes});
${await_block}.l(${parentNodes});
`);
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addBlock(deindent`
${await_block}.m(${targetNode}, ${anchorNode});
@ -205,10 +206,7 @@ export default class AwaitBlock extends Node {
[this.pending, this.then, this.catch].forEach(status => {
status.children.forEach(child => {
child.build(status._block, {
parentNode: null,
parentNodes: 'nodes'
});
child.build(status._block, null,'nodes');
});
});
}

@ -62,7 +62,8 @@ export default class Component extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { generator } = this;
generator.hasComponents = true;
@ -76,10 +77,7 @@ export default class Component extends Node {
componentInitProperties.push(`slots: { ${slots.join(', ')} }`);
this.children.forEach((child: Node) => {
child.build(block, {
parentNode: `${this.var}._slotted.default`,
parentNodes: 'nodes'
});
child.build(block, `${this.var}._slotted.default`, 'nodes');
});
}
@ -252,7 +250,7 @@ export default class Component extends Node {
block.contextualise(this.expression);
const { dependencies, snippet } = this.metadata;
const needsAnchor = this.next ? !this.next.isDomNode() : !state.parentNode || !this.parent.isDomNode();
const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode();
const anchor = needsAnchor
? block.getUniqueName(`${name}_anchor`)
: (this.next && this.next.var) || 'null';
@ -262,7 +260,7 @@ export default class Component extends Node {
anchor,
`@createComment()`,
`@createComment()`,
state.parentNode
parentNode
);
}
@ -298,11 +296,11 @@ export default class Component extends Node {
);
block.builders.claim.addLine(
`if (${name}) ${name}._fragment.l(${state.parentNodes});`
`if (${name}) ${name}._fragment.l(${parentNodes});`
);
block.builders.mount.addLine(
`if (${name}) ${name}._mount(${state.parentNode || '#target'}, ${state.parentNode ? 'null' : 'anchor'});`
`if (${name}) ${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
);
block.builders.update.addBlock(deindent`
@ -341,7 +339,7 @@ export default class Component extends Node {
`);
}
if (!state.parentNode) block.builders.unmount.addLine(`if (${name}) ${name}._unmount();`);
if (!parentNode) block.builders.unmount.addLine(`if (${name}) ${name}._unmount();`);
block.builders.destroy.addLine(`if (${name}) ${name}.destroy(false);`);
} else {
@ -365,11 +363,11 @@ export default class Component extends Node {
block.builders.create.addLine(`${name}._fragment.c();`);
block.builders.claim.addLine(
`${name}._fragment.l(${state.parentNodes});`
`${name}._fragment.l(${parentNodes});`
);
block.builders.mount.addLine(
`${name}._mount(${state.parentNode || '#target'}, ${state.parentNode ? 'null' : 'anchor'});`
`${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
);
if (updates.length) {
@ -381,7 +379,7 @@ export default class Component extends Node {
`);
}
if (!state.parentNode) block.builders.unmount.addLine(`${name}._unmount();`);
if (!parentNode) block.builders.unmount.addLine(`${name}._unmount();`);
block.builders.destroy.addLine(deindent`
${name}.destroy(false);

@ -3,7 +3,6 @@ import Node from './shared/Node';
import ElseBlock from './ElseBlock';
import { DomGenerator } from '../dom/index';
import Block from '../dom/Block';
import State from '../dom/State';
import createDebuggingComment from '../../utils/createDebuggingComment';
export default class EachBlock extends Node {
@ -104,7 +103,8 @@ export default class EachBlock extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { generator } = this;
@ -115,7 +115,7 @@ export default class EachBlock extends Node {
const iterations = this.iterations;
const params = block.params.join(', ');
const needsAnchor = this.next ? !this.next.isDomNode() : !state.parentNode || !this.parent.isDomNode();
const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode();
const anchor = needsAnchor
? block.getUniqueName(`${each}_anchor`)
: (this.next && this.next.var) || 'null';
@ -145,19 +145,19 @@ export default class EachBlock extends Node {
block.builders.init.addLine(`var ${each_block_value} = ${snippet};`);
if (this.key) {
keyed(generator, block, state, this, snippet, vars);
keyed(generator, block, parentNode, parentNodes, this, snippet, vars);
} else {
unkeyed(generator, block, state, this, snippet, vars);
unkeyed(generator, block, parentNode, parentNodes, this, snippet, vars);
}
const isToplevel = !state.parentNode;
const isToplevel = !parentNode;
if (needsAnchor) {
block.addElement(
anchor,
`@createComment()`,
`@createComment()`,
state.parentNode
parentNode
);
}
@ -176,11 +176,11 @@ export default class EachBlock extends Node {
block.builders.mount.addBlock(deindent`
if (${each_block_else}) {
${each_block_else}.${mountOrIntro}(${state.parentNode || '#target'}, null);
${each_block_else}.${mountOrIntro}(${parentNode || '#target'}, null);
}
`);
const parentNode = state.parentNode || `${anchor}.parentNode`;
const targetNode = parentNode || `${anchor}.parentNode`;
if (this.else._block.hasUpdateMethod) {
block.builders.update.addBlock(deindent`
@ -189,7 +189,7 @@ export default class EachBlock extends Node {
} else if (!${each_block_value}.${length}) {
${each_block_else} = ${this.else._block.name}(${params}, #component);
${each_block_else}.c();
${each_block_else}.${mountOrIntro}(${parentNode}, ${anchor});
${each_block_else}.${mountOrIntro}(${targetNode}, ${anchor});
} else if (${each_block_else}) {
${each_block_else}.u();
${each_block_else}.d();
@ -207,7 +207,7 @@ export default class EachBlock extends Node {
} else if (!${each_block_else}) {
${each_block_else} = ${this.else._block.name}(${params}, #component);
${each_block_else}.c();
${each_block_else}.${mountOrIntro}(${parentNode}, ${anchor});
${each_block_else}.${mountOrIntro}(${targetNode}, ${anchor});
}
`);
}
@ -222,18 +222,12 @@ export default class EachBlock extends Node {
}
this.children.forEach((child: Node) => {
child.build(this._block, {
parentNode: null,
parentNodes: 'nodes'
});
child.build(this._block, null, 'nodes');
});
if (this.else) {
this.else.children.forEach((child: Node) => {
child.build(this.else._block, {
parentNode: null,
parentNodes: 'nodes'
});
child.build(this.else._block, null, 'nodes');
});
}
}
@ -242,7 +236,8 @@ export default class EachBlock extends Node {
function keyed(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: EachBlock,
snippet: string,
{
@ -292,8 +287,8 @@ function keyed(
}
`);
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.create.addBlock(deindent`
var ${iteration} = ${head};
@ -306,7 +301,7 @@ function keyed(
block.builders.claim.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.l(${state.parentNodes});
${iteration}.l(${parentNodes});
${iteration} = ${iteration}.next;
}
`);
@ -320,7 +315,7 @@ function keyed(
`);
const dynamic = node._block.hasUpdateMethod;
const parentNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const mountNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
let destroy;
if (node._block.hasOutroMethod) {
@ -403,12 +398,12 @@ function keyed(
${iteration}.discard = false;
${iteration}.last = ${last};
if (!${expected}) ${iteration}.m(${parentNode}, ${anchor});
if (!${expected}) ${iteration}.m(${mountNode}, ${anchor});
} else {
// key is being inserted
${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key});
${iteration}.c();
${iteration}.${mountOrIntro}(${parentNode}, ${expected}.first);
${iteration}.${mountOrIntro}(${mountNode}, ${expected}.first);
${expected}.last = ${iteration};
${iteration}.next = ${expected};
@ -419,17 +414,17 @@ function keyed(
if (${iteration}) {
${iteration}.discard = false;
${iteration}.next = null;
${iteration}.m(${parentNode}, ${anchor});
${iteration}.m(${mountNode}, ${anchor});
} else {
${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key});
${iteration}.c();
${iteration}.${mountOrIntro}(${parentNode}, ${anchor});
${iteration}.${mountOrIntro}(${mountNode}, ${anchor});
}
}
if (${last}) ${last}.next = ${iteration};
${iteration}.last = ${last};
${node._block.hasIntroMethod && `${iteration}.i(${parentNode}, ${anchor});`}
${node._block.hasIntroMethod && `${iteration}.i(${mountNode}, ${anchor});`}
${last} = ${iteration};
}
@ -440,7 +435,7 @@ function keyed(
${head} = ${lookup}[${each_block_value}[0] && ${each_block_value}[0].${node.key}];
`);
if (!state.parentNode) {
if (!parentNode) {
block.builders.unmount.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
@ -462,7 +457,8 @@ function keyed(
function unkeyed(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: EachBlock,
snippet: string,
{
@ -483,8 +479,8 @@ function unkeyed(
}
`);
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.create.addBlock(deindent`
for (var #i = 0; #i < ${iterations}.length; #i += 1) {
@ -494,7 +490,7 @@ function unkeyed(
block.builders.claim.addBlock(deindent`
for (var #i = 0; #i < ${iterations}.length; #i += 1) {
${iterations}[#i].l(${state.parentNodes});
${iterations}[#i].l(${parentNodes});
}
`);
@ -515,7 +511,7 @@ function unkeyed(
.map(dependency => `changed.${dependency}`)
.join(' || ');
const parentNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const mountNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
if (condition !== '') {
const forLoopBody = node._block.hasUpdateMethod
@ -527,7 +523,7 @@ function unkeyed(
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
${iterations}[#i].c();
}
${iterations}[#i].i(${parentNode}, ${anchor});
${iterations}[#i].i(${mountNode}, ${anchor});
`
: deindent`
if (${iterations}[#i]) {
@ -535,13 +531,13 @@ function unkeyed(
} else {
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
${iterations}[#i].c();
${iterations}[#i].m(${parentNode}, ${anchor});
${iterations}[#i].m(${mountNode}, ${anchor});
}
`
: deindent`
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
${iterations}[#i].c();
${iterations}[#i].${mountOrIntro}(${parentNode}, ${anchor});
${iterations}[#i].${mountOrIntro}(${mountNode}, ${anchor});
`;
const start = node._block.hasUpdateMethod ? '0' : `${iterations}.length`;

@ -164,7 +164,8 @@ export default class Element extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { generator } = this;
@ -182,9 +183,9 @@ export default class Element extends Node {
const allUsedContexts: Set<string> = new Set();
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
const parentNode = this.slotted ?
const targetNode = this.slotted ?
`${this.nearestComponent().var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
state.parentNode;
parentNode;
block.addVariable(name);
block.builders.create.addLine(
@ -197,14 +198,14 @@ export default class Element extends Node {
if (this.generator.hydratable) {
block.builders.claim.addBlock(deindent`
${name} = ${getClaimStatement(generator, this.namespace, state.parentNodes, this)};
${name} = ${getClaimStatement(generator, this.namespace, parentNodes, this)};
var ${childState.parentNodes} = @children(${name});
`);
}
if (parentNode) {
if (targetNode) {
block.builders.mount.addLine(
`@appendNode(${name}, ${parentNode});`
`@appendNode(${name}, ${targetNode});`
);
} else {
block.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`);
@ -241,7 +242,7 @@ export default class Element extends Node {
}
} else {
this.children.forEach((child: Node) => {
child.build(block, childState);
child.build(block, childState.parentNode, childState.parentNodes);
});
}

@ -34,10 +34,7 @@ export default class Fragment extends Node {
this.init();
this.children.forEach(child => {
child.build(this.block, {
parentNode: null,
parentNodes: 'nodes'
});
child.build(this.block, null, 'nodes');
});
}
}

@ -3,7 +3,6 @@ import Node from './shared/Node';
import ElseBlock from './ElseBlock';
import { DomGenerator } from '../dom/index';
import Block from '../dom/Block';
import State from '../dom/State';
import createDebuggingComment from '../../utils/createDebuggingComment';
function isElseIf(node: ElseBlock) {
@ -92,17 +91,18 @@ export default class IfBlock extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const name = this.var;
const needsAnchor = this.next ? !this.next.isDomNode() : !state.parentNode || !this.parent.isDomNode();
const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode();
const anchor = needsAnchor
? block.getUniqueName(`${name}_anchor`)
: (this.next && this.next.var) || 'null';
const params = block.params.join(', ');
const branches = getBranches(this.generator, block, state, this);
const branches = getBranches(this.generator, block, parentNode, parentNodes, this);
const hasElse = isElseBranch(branches[branches.length - 1]);
const if_name = hasElse ? '' : `if (${name}) `;
@ -117,23 +117,24 @@ export default class IfBlock extends Node {
compoundWithOutros(
this.generator,
block,
state,
parentNode,
parentNodes,
this,
branches,
dynamic,
vars
);
} else {
compound(this.generator, block, state, this, branches, dynamic, vars);
compound(this.generator, block, parentNode, parentNodes, this, branches, dynamic, vars);
}
} else {
simple(this.generator, block, state, this, branches[0], dynamic, vars);
simple(this.generator, block, parentNode, parentNodes, this, branches[0], dynamic, vars);
}
block.builders.create.addLine(`${if_name}${name}.c();`);
block.builders.claim.addLine(
`${if_name}${name}.l(${state.parentNodes});`
`${if_name}${name}.l(${parentNodes});`
);
if (needsAnchor) {
@ -141,7 +142,7 @@ export default class IfBlock extends Node {
anchor,
`@createComment()`,
`@createComment()`,
state.parentNode
parentNode
);
}
}
@ -156,7 +157,8 @@ export default class IfBlock extends Node {
function getBranches(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: Node
) {
block.contextualise(node.expression); // TODO remove
@ -175,7 +177,7 @@ function getBranches(
if (isElseIf(node.else)) {
branches.push(
...getBranches(generator, block, state, node.else.children[0])
...getBranches(generator, block, parentNode, parentNodes, node.else.children[0])
);
} else {
branches.push({
@ -200,10 +202,7 @@ function visitChildren(
node: Node
) {
node.children.forEach((child: Node) => {
child.build(node._block, {
parentNode: null,
parentNodes: 'nodes'
});
child.build(node._block, null, 'nodes');
});
}
@ -212,7 +211,8 @@ function visitChildren(
function simple(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: Node,
branch,
dynamic,
@ -223,14 +223,14 @@ function simple(
`);
const mountOrIntro = branch.hasIntroMethod ? 'i' : 'm';
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine(
`if (${name}) ${name}.${mountOrIntro}(${targetNode}, ${anchorNode});`
);
const parentNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const mountNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const enter = dynamic
? branch.hasIntroMethod
@ -242,7 +242,7 @@ function simple(
if (${name}) ${name}.c();
}
${name}.i(${parentNode}, ${anchor});
${name}.i(${mountNode}, ${anchor});
`
: deindent`
if (${name}) {
@ -250,7 +250,7 @@ function simple(
} else {
${name} = ${branch.block}(${params}, #component);
${name}.c();
${name}.m(${parentNode}, ${anchor});
${name}.m(${mountNode}, ${anchor});
}
`
: branch.hasIntroMethod
@ -259,13 +259,13 @@ function simple(
${name} = ${branch.block}(${params}, #component);
${name}.c();
}
${name}.i(${parentNode}, ${anchor});
${name}.i(${mountNode}, ${anchor});
`
: deindent`
if (!${name}) {
${name} = ${branch.block}(${params}, #component);
${name}.c();
${name}.m(${parentNode}, ${anchor});
${name}.m(${mountNode}, ${anchor});
}
`;
@ -301,7 +301,8 @@ function simple(
function compound(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: Node,
branches,
dynamic,
@ -326,13 +327,13 @@ function compound(
const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine(
`${if_name}${name}.${mountOrIntro}(${targetNode}, ${anchorNode});`
);
const parentNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const mountNode = node.parent.isDomNode() ? node.parent.var : `${anchor}.parentNode`;
const changeBlock = deindent`
${hasElse
@ -347,7 +348,7 @@ function compound(
}`}
${name} = ${current_block_type_and}${current_block_type}(${params}, #component);
${if_name}${name}.c();
${if_name}${name}.${mountOrIntro}(${parentNode}, ${anchor});
${if_name}${name}.${mountOrIntro}(${mountNode}, ${anchor});
`;
if (dynamic) {
@ -376,7 +377,8 @@ function compound(
function compoundWithOutros(
generator: DomGenerator,
block: Block,
state: { parentNode: string, parentNodes: string },
parentNode: string,
parentNodes: string,
node: Node,
branches,
dynamic,
@ -423,14 +425,14 @@ function compoundWithOutros(
}
const mountOrIntro = branches[0].hasIntroMethod ? 'i' : 'm';
const targetNode = state.parentNode || '#target';
const anchorNode = state.parentNode ? 'null' : 'anchor';
const targetNode = parentNode || '#target';
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.mount.addLine(
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].${mountOrIntro}(${targetNode}, ${anchorNode});`
);
const parentNode = (state.parentNode && !needsAnchor) ? state.parentNode : `${anchor}.parentNode`;
const mountNode = (parentNode && !needsAnchor) ? parentNode : `${anchor}.parentNode`;
const destroyOldBlock = deindent`
${name}.o(function() {
@ -446,7 +448,7 @@ function compoundWithOutros(
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](${params}, #component);
${name}.c();
}
${name}.${mountOrIntro}(${parentNode}, ${anchor});
${name}.${mountOrIntro}(${mountNode}, ${anchor});
`;
const changeBlock = hasElse

@ -12,7 +12,8 @@ export default class MustacheTag extends Tag {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { init } = this.renameThisMethod(
block,
@ -22,8 +23,8 @@ export default class MustacheTag extends Tag {
block.addElement(
this.var,
`@createText(${init})`,
`@claimText(${state.parentNodes}, ${init})`,
state.parentNode
`@claimText(${parentNodes}, ${init})`,
parentNode
);
}
}

@ -2,7 +2,6 @@ import deindent from '../../utils/deindent';
import Node from './shared/Node';
import Tag from './shared/Tag';
import Block from '../dom/Block';
import State from '../dom/State';
export default class RawMustacheTag extends Tag {
init(block: Block) {
@ -13,12 +12,13 @@ export default class RawMustacheTag extends Tag {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const name = this.var;
const needsAnchorBefore = this.prev ? this.prev.type !== 'Element' : !state.parentNode;
const needsAnchorAfter = this.next ? this.next.type !== 'Element' : !state.parentNode;
const needsAnchorBefore = this.prev ? this.prev.type !== 'Element' : !parentNode;
const needsAnchorAfter = this.next ? this.next.type !== 'Element' : !parentNode;
const anchorBefore = needsAnchorBefore
? block.getUniqueName(`${name}_before`)
@ -34,8 +34,8 @@ export default class RawMustacheTag extends Tag {
if (anchorBefore === 'null' && anchorAfter === 'null') {
useInnerHTML = true;
detach = `${state.parentNode}.innerHTML = '';`;
insert = content => `${state.parentNode}.innerHTML = ${content};`;
detach = `${parentNode}.innerHTML = '';`;
insert = content => `${parentNode}.innerHTML = ${content};`;
} else if (anchorBefore === 'null') {
detach = `@detachBefore(${anchorAfter});`;
insert = content => `${anchorAfter}.insertAdjacentHTML("beforebegin", ${content});`;
@ -62,7 +62,7 @@ export default class RawMustacheTag extends Tag {
anchorBefore,
`@createElement('noscript')`,
`@createElement('noscript')`,
state.parentNode
parentNode
);
}
@ -71,7 +71,7 @@ export default class RawMustacheTag extends Tag {
anchorAfter,
`@createElement('noscript')`,
`@createElement('noscript')`,
state.parentNode
parentNode
);
}

@ -27,7 +27,8 @@ export default class Slot extends Element {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { generator } = this;
@ -37,8 +38,8 @@ export default class Slot extends Element {
const content_name = block.getUniqueName(`slot_content_${slotName}`);
block.addVariable(content_name, `#component._slotted.${slotName}`);
const needsAnchorBefore = this.prev ? this.prev.type !== 'Element' : !state.parentNode;
const needsAnchorAfter = this.next ? this.next.type !== 'Element' : !state.parentNode;
const needsAnchorBefore = this.prev ? this.prev.type !== 'Element' : !parentNode;
const needsAnchorAfter = this.next ? this.next.type !== 'Element' : !parentNode;
const anchorBefore = needsAnchorBefore
? block.getUniqueName(`${content_name}_before`)
@ -58,7 +59,7 @@ export default class Slot extends Element {
block.builders.destroy.pushCondition(`!${content_name}`);
this.children.forEach((child: Node) => {
child.build(block, state);
child.build(block, parentNode, parentNodes);
});
block.builders.create.popCondition();
@ -68,12 +69,12 @@ export default class Slot extends Element {
block.builders.destroy.popCondition();
// TODO can we use an else here?
if (state.parentNode) {
if (parentNode) {
block.builders.mount.addBlock(deindent`
if (${content_name}) {
${needsAnchorBefore && `@appendNode(${anchorBefore} || (${anchorBefore} = @createComment()), ${state.parentNode});`}
@appendNode(${content_name}, ${state.parentNode});
${needsAnchorAfter && `@appendNode(${anchorAfter} || (${anchorAfter} = @createComment()), ${state.parentNode});`}
${needsAnchorBefore && `@appendNode(${anchorBefore} || (${anchorBefore} = @createComment()), ${parentNode});`}
@appendNode(${content_name}, ${parentNode});
${needsAnchorAfter && `@appendNode(${anchorAfter} || (${anchorAfter} = @createComment()), ${parentNode});`}
}
`);
} else {
@ -94,7 +95,7 @@ export default class Slot extends Element {
if (anchorBefore === 'null' && anchorAfter === 'null') {
block.builders.unmount.addBlock(deindent`
if (${content_name}) {
@reinsertChildren(${state.parentNode}, ${content_name});
@reinsertChildren(${parentNode}, ${content_name});
}
`);
} else if (anchorBefore === 'null') {

@ -35,15 +35,16 @@ export default class Text extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
if (this.shouldSkip) return;
block.addElement(
this.var,
`@createText(${stringify(this.data)})`,
`@claimText(${state.parentNodes}, ${stringify(this.data)})`,
state.parentNode
`@claimText(${parentNodes}, ${stringify(this.data)})`,
parentNode
);
}
}

@ -41,7 +41,8 @@ export default class Window extends Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
const { generator } = this;

@ -118,7 +118,8 @@ export default class Node {
build(
block: Block,
state: { parentNode: string, parentNodes: string }
parentNode: string,
parentNodes: string
) {
// implemented by subclasses
}

Loading…
Cancel
Save