@webtides/element-js 1.0.0-alpha.5 → 1.0.0-alpha.6

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtides/element-js",
3
- "version": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.6",
4
4
  "module": "index.js",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -34,6 +34,8 @@ export class TemplatePart extends Part {
34
34
 
35
35
  let serverSideRendered = false;
36
36
  if (startNode) {
37
+ startNode.__part = this; // add Part to comment node for debugging in the browser
38
+
37
39
  const placeholder = startNode.data;
38
40
  const childNodes = [startNode];
39
41
  let childNode = startNode.nextSibling;
@@ -110,7 +112,6 @@ export class TemplatePart extends Part {
110
112
  .map((part, index) => {
111
113
  if (part.type === 'node') {
112
114
  return new ChildNodePart(part.node, templateResult.values[index]);
113
- // TODO: the nodes in the parts array also have nested parts information... could we start creating the nested parts here as well?
114
115
  }
115
116
  if (part.type === 'attribute') {
116
117
  // If we have multiple attribute parts with the same name, it means we have multiple
@@ -12,7 +12,7 @@ const partPositions = /[\x01\x02]/g;
12
12
  // \x03 COMMENT.ATTRIBUTE_TOKEN
13
13
  // \x04 Node.ATTRIBUTE_TOKEN
14
14
 
15
- const interpolation = new RegExp(`(<!--dom-part-(\\d+)--><!--/dom-part-(\\d+)-->|(\\S\\w+)="\x04(")?|\x04)`, 'g');
15
+ const interpolation = new RegExp(`(<!--dom-part-(\\d+)--><!--/dom-part-(\\d+)-->|(\\S[-\\w]+)="\x04(")?|\x04)`, 'g');
16
16
 
17
17
  /**
18
18
  * Given a template, find part positions as both nodes and attributes and
@@ -258,40 +258,39 @@ export class TemplateResult {
258
258
  const template = range.cloneContents();
259
259
 
260
260
  const treeWalker = globalThis.document?.createTreeWalker(template, 128);
261
- let node = treeWalker.currentNode;
261
+ let node = treeWalker.nextNode(); //to skip the root template-part
262
+ const parts = [];
262
263
 
263
- // this is kinda freaky because the tree walker is shared throughout the recursion - but YOLO
264
- const getParts = () => {
265
- const parts = [];
266
- let currentPart = undefined;
267
- // search for parts through numbered comment nodes with placeholders
268
- while ((node = treeWalker.nextNode())) {
269
- if (/^template-part$/.test(node.data) && currentPart) {
270
- // start recursion -> add nested parts to previous part
271
- currentPart.parts = getParts();
272
- }
273
- if (/^\/template-part$/.test(node.data)) {
274
- // end recursion
275
- return parts;
276
- }
277
- if (/^dom-part-\d+$/.test(node.data)) {
278
- parts.push((currentPart = { type: 'node', path: getNodePath(node), parts: [] }));
279
- }
280
- if (/^dom-part-\d+:/.test(node.data)) {
281
- const [_, attribute] = node.data.split(':');
282
- const [name, initialValue] = attribute.split('=');
283
- currentPart = undefined;
284
- parts.push({ type: 'attribute', path: getNodePath(node), name: name, initialValue });
285
- }
286
- if (/^dom-part-\d+\$/.test(node.data)) {
287
- currentPart = undefined;
288
- parts.push({ type: 'directive', path: getNodePath(node) });
264
+ let nestedLevel = 0;
265
+ // search for parts through numbered comment nodes with placeholders
266
+ while ((node = treeWalker.nextNode())) {
267
+ if (/^template-part$/.test(node.data)) {
268
+ nestedLevel++;
269
+ continue;
270
+ }
271
+ if (/^\/template-part$/.test(node.data)) {
272
+ if (nestedLevel > 0) {
273
+ nestedLevel--;
289
274
  }
275
+ continue;
290
276
  }
291
- return parts;
292
- };
293
-
294
- const parts = getParts();
277
+ if (nestedLevel > 0) {
278
+ continue;
279
+ }
280
+ if (/^dom-part-\d+$/.test(node.data)) {
281
+ parts.push({ type: 'node', path: getNodePath(node) });
282
+ continue;
283
+ }
284
+ if (/^dom-part-\d+:/.test(node.data)) {
285
+ const [_, ...attribute] = node.data.split(':');
286
+ const [name, initialValue] = attribute.join(':').split('=');
287
+ parts.push({ type: 'attribute', path: getNodePath(node), name: name, initialValue });
288
+ continue;
289
+ }
290
+ if (/^dom-part-\d+\$/.test(node.data)) {
291
+ parts.push({ type: 'directive', path: getNodePath(node) });
292
+ }
293
+ }
295
294
 
296
295
  // We couldn't correctly parse parts from the template
297
296
  if (parts.length !== this.strings.length - 1) {