@webtides/element-js 1.0.0-alpha.6 → 1.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -78,6 +78,9 @@ const processAttribute = (node, name) => {
|
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
/** @type {Map<Element, AttributePart>} */
|
|
82
|
+
const attributePartsCache = new WeakMap();
|
|
83
|
+
|
|
81
84
|
/**
|
|
82
85
|
* @param {Element} node
|
|
83
86
|
* @param {String} name
|
|
@@ -114,24 +117,27 @@ export class AttributePart extends Part {
|
|
|
114
117
|
initialValue = undefined;
|
|
115
118
|
|
|
116
119
|
/**
|
|
117
|
-
* @param {
|
|
120
|
+
* @param {Comment} node
|
|
118
121
|
* @param {String} name
|
|
119
122
|
* @param {String} initialValue
|
|
120
123
|
*/
|
|
121
124
|
constructor(node, name, initialValue) {
|
|
125
|
+
// If we have multiple attribute parts with the same name, it means we have multiple
|
|
126
|
+
// interpolations inside that attribute. Instead of creating a new part, we will return the same
|
|
127
|
+
// as before and let it defer the update until the last interpolation gets updated
|
|
128
|
+
const attributePart = attributePartsCache.get(node.nextElementSibling);
|
|
129
|
+
if (attributePart && attributePart.name === name) {
|
|
130
|
+
attributePart.interpolations++;
|
|
131
|
+
node.__part = attributePart; // add Part to comment node for debugging in the browser
|
|
132
|
+
return attributePart;
|
|
133
|
+
}
|
|
134
|
+
|
|
122
135
|
super();
|
|
123
|
-
node.__part = this; // add Part to comment node for debugging in the browser
|
|
124
136
|
this.name = name;
|
|
125
137
|
this.initialValue = initialValue;
|
|
126
138
|
this.processor = processAttributePart(node.nextElementSibling, this.name);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* @param {Node} node
|
|
131
|
-
*/
|
|
132
|
-
addNode(node) {
|
|
133
|
-
this.interpolations++;
|
|
134
|
-
// TODO: what about the comment node? It will always be the first for now...
|
|
139
|
+
node.__part = this; // add Part to comment node for debugging in the browser
|
|
140
|
+
attributePartsCache.set(node.nextElementSibling, this);
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
/**
|
|
@@ -12,13 +12,13 @@ const removeChildNodes = (node) => {
|
|
|
12
12
|
const firstChild = Array.isArray(node)
|
|
13
13
|
? node[0]
|
|
14
14
|
: node instanceof TemplatePart
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
? node.childNodes[0]
|
|
16
|
+
: node.firstChild;
|
|
17
17
|
const lastChild = Array.isArray(node)
|
|
18
18
|
? node[node.length - 1]
|
|
19
19
|
: node instanceof TemplatePart
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
? node.childNodes[node.childNodes.length - 1]
|
|
21
|
+
: node.lastChild;
|
|
22
22
|
range.setStartAfter(firstChild);
|
|
23
23
|
range.setEndAfter(lastChild);
|
|
24
24
|
range.deleteContents();
|
|
@@ -75,28 +75,12 @@ const diffChildNodes = function (parentNode, domChildNodes, templateChildNodes,
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
// If DOM node is equal to the template node, don't do anything
|
|
78
|
-
// if (domChildNode.isEqualNode(templateChildNode)) {
|
|
79
78
|
if (domChildNode === templateChildNode) {
|
|
80
79
|
continue;
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
parentNode.replaceChild(templateChildNode, domChildNode);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// If the element tag names are not the same, replace the DOM node with the template node
|
|
90
|
-
if (templateChildNode.nodeType === ELEMENT_NODE && templateChildNode.tagName !== domChildNode.tagName) {
|
|
91
|
-
parentNode.replaceChild(templateChildNode, domChildNode);
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// If the node is an SVG element, don't even think about diffing it, just replace it
|
|
96
|
-
if (templateChildNode.nodeType === ELEMENT_NODE && templateChildNode.tagName === 'SVG') {
|
|
97
|
-
parentNode.replaceChild(templateChildNode, domChildNode);
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
82
|
+
// Everything else is somehow different and can be replaced
|
|
83
|
+
parentNode.replaceChild(templateChildNode, domChildNode);
|
|
100
84
|
}
|
|
101
85
|
return templateChildNodes;
|
|
102
86
|
};
|
|
@@ -101,8 +101,6 @@ export class TemplatePart extends Part {
|
|
|
101
101
|
partsCache.set(templateResult.strings, parts);
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
/** @type AttributePart */
|
|
105
|
-
let previousAttributePart = undefined;
|
|
106
104
|
this.parts = parts
|
|
107
105
|
.map((part) => {
|
|
108
106
|
// We currently need the path because the fragment will be cloned via importNode and therefore the node will be a different one
|
|
@@ -114,19 +112,11 @@ export class TemplatePart extends Part {
|
|
|
114
112
|
return new ChildNodePart(part.node, templateResult.values[index]);
|
|
115
113
|
}
|
|
116
114
|
if (part.type === 'attribute') {
|
|
117
|
-
|
|
118
|
-
// interpolations inside that attribute. Instead of creating a new part, we will return the same
|
|
119
|
-
// as before and let it defer the update until the last interpolation gets updated
|
|
120
|
-
if (previousAttributePart && previousAttributePart.name === part.name) {
|
|
121
|
-
previousAttributePart.addNode(part.node);
|
|
122
|
-
return previousAttributePart;
|
|
123
|
-
}
|
|
124
|
-
return (previousAttributePart = new AttributePart(part.node, part.name, part.initialValue));
|
|
115
|
+
return new AttributePart(part.node, part.name, part.initialValue);
|
|
125
116
|
}
|
|
126
117
|
if (part.type === 'directive') {
|
|
127
118
|
return new NodePart(part.node, templateResult.values[index]);
|
|
128
119
|
}
|
|
129
|
-
|
|
130
120
|
throw `cannot map part: ${part}`;
|
|
131
121
|
});
|
|
132
122
|
this.strings = templateResult.strings;
|