Skip to content

Commit 78e262e

Browse files
committed
Fixed nasty #fragment bug
1 parent b0e39be commit 78e262e

16 files changed

Lines changed: 459 additions & 139 deletions

cjs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22
const {cache, cacheInfo, setCache} = require('./cache.js');
3-
const {create, defineProperties} = require('./object.js');
43
const {Hole, retrieve} = require('./rabbit.js');
54

5+
const {create, defineProperties} = Object;
6+
67
const util = type => {
78
const cache = new WeakMap;
89
const fixed = info => (template, ...values) => retrieve(

cjs/node.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const createContent = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('@ungap/create-content'));
33

4-
const {defineProperties} = require('./object.js');
54
const {indexOf, slice} = require('./array.js');
65

76
const getNode = (node, i) => node.childNodes[i];
@@ -19,36 +18,36 @@ const getPath = node => {
1918
};
2019
exports.getPath = getPath;
2120

22-
const getWire = fragment => {
23-
const {childNodes} = fragment;
21+
const getWire = content => {
22+
const {childNodes} = content;
2423
const {length} = childNodes;
2524
if (length === 1)
2625
return childNodes[0];
27-
const nodes = slice.call(childNodes, 0);
28-
return defineProperties(fragment, {
29-
firstChild: {value: nodes[0]},
30-
lastChild: {value: nodes[length - 1]},
31-
remove: {
32-
value() {
33-
const range = document.createRange();
34-
range.setStartBefore(nodes[1]);
35-
range.setEndAfter(nodes[length - 1]);
36-
range.deleteContents();
37-
return nodes[0];
38-
}
26+
const firstChild = childNodes[0];
27+
const lastChild = childNodes[length - 1];
28+
return {
29+
ELEMENT_NODE: 1,
30+
nodeType: 11,
31+
childNodes: slice.call(childNodes, 0),
32+
firstChild,
33+
lastChild,
34+
remove() {
35+
const range = document.createRange();
36+
range.setStartAfter(firstChild);
37+
range.setEndAfter(lastChild);
38+
range.deleteContents();
39+
return firstChild;
3940
},
40-
valueOf: {
41-
value() {
42-
if (childNodes.length !== length) {
43-
const range = document.createRange();
44-
range.setStartBefore(nodes[0]);
45-
range.setEndAfter(nodes[length - 1]);
46-
fragment.appendChild(range.extractContents());
47-
}
48-
return fragment;
41+
valueOf() {
42+
if (childNodes.length !== length) {
43+
const range = document.createRange();
44+
range.setStartBefore(firstChild);
45+
range.setEndAfter(lastChild);
46+
content.appendChild(range.extractContents());
4947
}
48+
return content;
5049
}
51-
});
50+
};
5251
};
5352
exports.getWire = getWire;
5453

cjs/object.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

cjs/rabbit.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const prefix = 'isµ';
1111
const templates = new WeakMap;
1212

1313
const createEntry = (type, template) => {
14-
const {wire, updates} = mapUpdates(type, template);
15-
return {type, template, wire, updates};
14+
const {content, updates} = mapUpdates(type, template);
15+
return {type, template, content, updates, wire: null};
1616
};
1717

1818
const mapTemplate = (type, template) => {
@@ -60,7 +60,7 @@ const mapUpdates = (type, template) => {
6060
const {content, nodes} = templates.get(template) || setTemplate(type, template);
6161
const fragment = importNode.call(document, content, true);
6262
const updates = nodes.map(handlers, fragment);
63-
return {wire: getWire(fragment), updates};
63+
return {content: fragment, updates};
6464
};
6565

6666
const retrieve = (info, hole) => {
@@ -97,10 +97,10 @@ const unroll = (info, hole, counter) => {
9797
let entry = stack[i];
9898
if (!unknown && (entry.template !== template || entry.type !== type))
9999
stack[i] = (entry = createEntry(type, template));
100-
const {wire, updates} = entry;
100+
const {content, updates, wire} = entry;
101101
for (let i = 0, {length} = updates; i < length; i++)
102102
updates[i](values[i]);
103-
return wire;
103+
return wire || (entry.wire = getWire(content));
104104
};
105105

106106
const unrollArray = (info, values, counter) => {

esm/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {cache, cacheInfo, setCache} from './cache.js';
2-
import {create, defineProperties} from './object.js';
32
import {Hole, retrieve} from './rabbit.js';
43

4+
const {create, defineProperties} = Object;
5+
56
const util = type => {
67
const cache = new WeakMap;
78
const fixed = info => (template, ...values) => retrieve(

esm/node.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import createContent from '@ungap/create-content';
22

3-
import {defineProperties} from './object.js';
43
import {indexOf, slice} from './array.js';
54

65
export const getNode = (node, i) => node.childNodes[i];
@@ -16,36 +15,36 @@ export const getPath = node => {
1615
return path;
1716
};
1817

19-
export const getWire = fragment => {
20-
const {childNodes} = fragment;
18+
export const getWire = content => {
19+
const {childNodes} = content;
2120
const {length} = childNodes;
2221
if (length === 1)
2322
return childNodes[0];
24-
const nodes = slice.call(childNodes, 0);
25-
return defineProperties(fragment, {
26-
firstChild: {value: nodes[0]},
27-
lastChild: {value: nodes[length - 1]},
28-
remove: {
29-
value() {
30-
const range = document.createRange();
31-
range.setStartBefore(nodes[1]);
32-
range.setEndAfter(nodes[length - 1]);
33-
range.deleteContents();
34-
return nodes[0];
35-
}
23+
const firstChild = childNodes[0];
24+
const lastChild = childNodes[length - 1];
25+
return {
26+
ELEMENT_NODE: 1,
27+
nodeType: 11,
28+
childNodes: slice.call(childNodes, 0),
29+
firstChild,
30+
lastChild,
31+
remove() {
32+
const range = document.createRange();
33+
range.setStartAfter(firstChild);
34+
range.setEndAfter(lastChild);
35+
range.deleteContents();
36+
return firstChild;
3637
},
37-
valueOf: {
38-
value() {
39-
if (childNodes.length !== length) {
40-
const range = document.createRange();
41-
range.setStartBefore(nodes[0]);
42-
range.setEndAfter(nodes[length - 1]);
43-
fragment.appendChild(range.extractContents());
44-
}
45-
return fragment;
38+
valueOf() {
39+
if (childNodes.length !== length) {
40+
const range = document.createRange();
41+
range.setStartBefore(firstChild);
42+
range.setEndAfter(lastChild);
43+
content.appendChild(range.extractContents());
4644
}
45+
return content;
4746
}
48-
});
47+
};
4948
};
5049

5150
const {createTreeWalker, importNode} = document;

esm/object.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

esm/rabbit.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const prefix = 'isµ';
1212
const templates = new WeakMap;
1313

1414
const createEntry = (type, template) => {
15-
const {wire, updates} = mapUpdates(type, template);
16-
return {type, template, wire, updates};
15+
const {content, updates} = mapUpdates(type, template);
16+
return {type, template, content, updates, wire: null};
1717
};
1818

1919
const mapTemplate = (type, template) => {
@@ -61,7 +61,7 @@ const mapUpdates = (type, template) => {
6161
const {content, nodes} = templates.get(template) || setTemplate(type, template);
6262
const fragment = importNode.call(document, content, true);
6363
const updates = nodes.map(handlers, fragment);
64-
return {wire: getWire(fragment), updates};
64+
return {content: fragment, updates};
6565
};
6666

6767
export const retrieve = (info, hole) => {
@@ -97,10 +97,10 @@ const unroll = (info, hole, counter) => {
9797
let entry = stack[i];
9898
if (!unknown && (entry.template !== template || entry.type !== type))
9999
stack[i] = (entry = createEntry(type, template));
100-
const {wire, updates} = entry;
100+
const {content, updates, wire} = entry;
101101
for (let i = 0, {length} = updates; i < length; i++)
102102
updates[i](values[i]);
103-
return wire;
103+
return wire || (entry.wire = getWire(content));
104104
};
105105

106106
const unrollArray = (info, values, counter) => {

0 commit comments

Comments
 (0)