Skip to content

Commit

Permalink
Fixed build - sveltejs#588
Browse files Browse the repository at this point in the history
* Introduced jsdom.VirtualConsole
  • Loading branch information
btakita committed Mar 16, 2018
1 parent fd14ecb commit 8a7c30f
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 84 deletions.
155 changes: 81 additions & 74 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import deindent from '../../utils/deindent';
import Node from './shared/Node';
import ElseBlock from './ElseBlock';
import { DomGenerator } from '../dom/index';
import Block from '../dom/Block';
import createDebuggingComment from '../../utils/createDebuggingComment';

Expand Down Expand Up @@ -254,6 +253,11 @@ export default class EachBlock extends Node {
const head = block.getUniqueName(`${each}_head`);
const last = block.getUniqueName(`${each}_last`);
const expected = block.getUniqueName(`${each}_expected`);
const keep = block.getUniqueName(`${each}_keep`);
const all = block.getUniqueName(`${each}_all`);
const inserts = block.getUniqueName(`${each}_inserts`);
const moves = block.getUniqueName(`${each}_moves`);
const next_iteration = block.getUniqueName(`${each}_next_iteration`);

block.addVariable(lookup, `@blankObject()`);
block.addVariable(head);
Expand Down Expand Up @@ -318,7 +322,7 @@ export default class EachBlock extends Node {

const dynamic = this.block.hasUpdateMethod;

let destroy;
const fn_destroy = block.getUniqueName('destroy');
if (this.block.hasOutroMethod) {
const fn = block.getUniqueName(`${each}_outro`);
block.builders.init.addBlock(deindent`
Expand All @@ -329,130 +333,133 @@ export default class EachBlock extends Node {
${lookup}[iteration.key] = null;
});
}
`);

destroy = deindent`
while (${expected}) {
${fn}(${expected});
${expected} = ${expected}.next;
}
for (#i = 0; #i < discard_pile.length; #i += 1) {
if (discard_pile[#i].discard) {
${fn}(discard_pile[#i]);
function ${fn_destroy}(${all}, ${keep}) {
for (var key_all in ${all}) {
if (!${keep}[key_all]) {
${fn}(${all}[key_all]);
}
}
}
`;
`);
} else {
const fn = block.getUniqueName(`${each}_destroy`);
block.builders.init.addBlock(deindent`
function ${fn}(iteration) {
iteration.u();
var first = iteration.first
if (first && first.parentNode) {
iteration.u();
}
iteration.d();
${lookup}[iteration.key] = null;
}
`);

destroy = deindent`
while (${expected}) {
${fn}(${expected});
${expected} = ${expected}.next;
}
for (#i = 0; #i < discard_pile.length; #i += 1) {
var ${iteration} = discard_pile[#i];
if (${iteration}.discard) {
${fn}(${iteration});
function ${fn_destroy}(${all}, ${keep}) {
for (var key_all in ${all}) {
if (!${keep}[key_all]) {
${fn}(${all}[key_all]);
}
}
}
`;
`);
}

block.builders.update.addBlock(deindent`
var ${each_block_value} = ${snippet};
var ${expected} = ${head};
var ${last} = null;
var rendered = {};
var all = {};
var ${keep} = {};
var ${all} = {};
var ${inserts} = {};
var ${moves} = {};
var ${next_iteration} = null;
var each_all = ${head};
while(each_all) {
all[each_all.key] = each_all;
each_all = each_all.next;
}
while(each_all) {
${all}[each_all.key] = each_all;
each_all = each_all.next;
}
for (#i = 0; #i < ${each_block_value}.${length}; #i += 1) {
var ${key} = ${each_block_value}[#i].${this.key};
var ${iteration} = ${lookup}[${key}];
var next_data = ${each_block_value}[#i+1];
var next = next_data && ${lookup}[next_data.${this.key}];
var ${this.each_context} = @assign({}, state, {
${this.contextProps.join(',\n')}
});
${dynamic &&
`if (${iteration}) ${iteration}.p(changed, ${this.each_context});`}
if (${expected}) {
if (${key} === ${expected}.key) {
${expected} = ${expected}.next;
var first = ${iteration} && ${iteration}.first;
var parentNode = first && first.parentNode
if (!parentNode) ${inserts}[${key}] = ${iteration};
else if ((${iteration} && ${iteration}.next) != next) ${moves}[${key}] = ${iteration};
${expected} = ${iteration}.next;
} else {
if (${iteration}) {
var next_data = ${each_block_value}[#i+1];
var next = next_data && ${lookup}[next_data.id];
var first = ${iteration}.first;
var first_next = next && next.first;
///insertNode(first, tbody, first_next);
${updateMountNode}.insertBefore(first, first_next);
${expected} = next;
${iteration}.next = ${expected};
var prev_data = ${each_block_value}[#i-1];
var prev = prev_data && ${lookup}[prev_data.id];
if (prev) {
prev.next = ${iteration};
}
${moves}[${key}] = ${iteration};
${expected} = ${iteration}.next;
} else {
// key is being inserted
${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context});
${iteration}.c();
${iteration}.${mountOrIntro}(${updateMountNode}, ${expected}.first);
${expected}.last = ${iteration};
${iteration}.next = ${expected};
${inserts}[${key}] = ${iteration};
${expected} = null;
}
}
} else {
// we're appending from this point forward
if (${iteration}) {
${iteration}.next = null;
${iteration}.m(${updateMountNode}, ${anchor});
${moves}[${key}] = ${iteration};
${expected} = ${iteration}.next;
} else {
${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context});
${iteration}.c();
${iteration}.${mountOrIntro}(${updateMountNode}, ${anchor});
${inserts}[${key}] = ${iteration};
${expected} = null;
}
}
if (${iteration}) {
rendered[${iteration}.key] = ${iteration};
}
${lookup}[${key}] = ${iteration};
if (${last}) ${last}.next = ${iteration};
${iteration}.last = ${last};
${this.block.hasIntroMethod && `${iteration}.i(${updateMountNode}, ${anchor});`}
if (${iteration}) {
${keep}[${iteration}.key] = ${iteration};
}
${last} = ${iteration};
}
${fn_destroy}(${all}, ${keep})
if (${last}) ${last}.next = null;
for (var key_all in all) {
if (!rendered[key_all]) all[key_all].d();
}
// Work backwards due to DOM api having insertBefore
for (#i = ${each_block_value}.${length} - 1; #i >= 0; #i -= 1) {
var data = ${each_block_value}[#i];
var ${key} = data.${this.key};
${iteration} = ${lookup}[${key}];
var anchor;
${this.block.hasOutroMethod
? deindent`
var key_next_iteration = ${next_iteration} && ${next_iteration}.key;
var iteration_anchor = ${iteration}.next;
var key_anchor = iteration_anchor && iteration_anchor.key;
anchor = ${next_iteration} && ${next_iteration}.first;
while(iteration_anchor && key_anchor != key_next_iteration && !${keep}[key_anchor]) {
anchor = iteration_anchor && iteration_anchor.first;
iteration_anchor = iteration_anchor && iteration_anchor.next;
key_anchor = iteration_anchor && iteration_anchor.key;
}`
: deindent`
anchor = ${next_iteration} && ${next_iteration}.first;
` }
if (${inserts}[${key}]) {
${inserts}[${key}].${mountOrIntro}(${updateMountNode}, anchor);
} else if (${moves}[${key}]) {
${this.block.hasIntroMethod && `${iteration}.i(${updateMountNode}, ${anchor});`}
${moves}[${key}].m(${updateMountNode}, anchor);
}
${iteration}.next = ${next_iteration};
if (${next_iteration}) ${next_iteration}.prev = ${iteration};
${next_iteration} = ${iteration};
}
${head} = ${lookup}[${each_block_value}[0] && ${each_block_value}[0].${this.key}];
`);

Expand Down
2 changes: 1 addition & 1 deletion src/shared/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ export function selectMultipleValue(select) {
return [].map.call(select.querySelectorAll(':checked'), function(option) {
return option.__value;
});
}
}
1 change: 0 additions & 1 deletion src/shared/transitions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assign, noop } from './utils.js';
import { createElement } from './dom.js';

export function linear(t) {
Expand Down
4 changes: 3 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jsdom from 'jsdom';
import { JSDOM } from 'jsdom';
import assert from 'assert';
import glob from 'glob';
Expand Down Expand Up @@ -45,7 +46,8 @@ export function tryToReadFile(file) {
}
}

const { window } = new JSDOM('<main></main>');
export const virtualConsole = new jsdom.VirtualConsole();
const { window } = new JSDOM('<main></main>', {virtualConsole});
global.document = window.document;

export function env() {
Expand Down
5 changes: 3 additions & 2 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
loadSvelte,
env,
setupHtmlEqual,
spaces
spaces,
virtualConsole
} from "../helpers.js";

let svelte;
Expand Down Expand Up @@ -183,7 +184,7 @@ describe("runtime", () => {
if (config.html) {
assert.htmlEqual(target.innerHTML, config.html);
}

virtualConsole.on('debug', () => console.debug)
if (config.test) {
return Promise.resolve(config.test(assert, component, target, window, raf)).then(() => {
component.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default {
test( 'g' );
test( '' );
test( 'abc' );
test( 'duqbmineapjhtlofrskcg' );
test( 'hdnkjougmrvftewsqpailcb' );

// then, we party
for ( let i = 0; i < 100; i += 1 ) test( permute() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ export default {
assert.equal( divs[1].foo, 1 );
assert.equal( divs[2].foo, 1 );
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
}
}
};
</script>
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export default {
assert.equal( divs[1].foo, 0.5 );
assert.equal( divs[2].foo, undefined );
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
}
}
};
</script>
</script>
2 changes: 1 addition & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ require.extensions['.js'] = function(module, filename) {
console.log(code); // eslint-disable-line no-console
throw err;
}
};
};

0 comments on commit 8a7c30f

Please sign in to comment.