@webtides/element-js 1.0.0-alpha.10 → 1.0.0-alpha.12

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.10",
3
+ "version": "1.0.0-alpha.12",
4
4
  "module": "index.js",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,7 +24,12 @@ export { toString } from './util/toString.js';
24
24
 
25
25
  /**
26
26
  * Options object for the BaseElement propertyOptions option
27
- * @typedef {Object} PropertyOptions
27
+ * @typedef {Object<string, PropertyOption>} PropertyOptions
28
+ */
29
+
30
+ /**
31
+ * Option object for a property of the BaseElement propertyOptions option
32
+ * @typedef {Object} PropertyOption
28
33
  * @property {undefined | boolean | function} [reflect] - When set to true the element will reflect property changes back to attributes if the attribute was not present when connecting the element. By default, all attributes that are present when connecting the element will be reflected anyway. Default is `undefined`
29
34
  * @property {undefined | boolean | function} [parse] - When set to false the element will not automatically try to parse the attributes string value to a complex type (number, array, object). Default is `undefined`
30
35
  * @property {undefined | boolean | function} [notify] - When set to true the element will dispatch CustomEvents for every property/attribute change. The event name will be the property name all lowercase and camel to dashes with a postfix of -changed. Default is `undefined`
@@ -31,13 +31,13 @@ class TemplateElement extends StyledElement {
31
31
  * This will attach a shadow DOM if the element is supposed to render in shadow DOM.
32
32
  */
33
33
  connectedCallback() {
34
- if (this._options.shadowRender) this.attachShadow({ mode: 'open' });
34
+ if (this._options.shadowRender && !this.shadowRoot) this.attachShadow({ mode: 'open' });
35
35
  super.connectedCallback();
36
36
  }
37
37
 
38
38
  /**
39
39
  * The template method should be overridden in extending elements and return the template to be rendered to the root
40
- * @returns {TemplateResult}
40
+ * @returns {TemplateResult | string}
41
41
  */
42
42
  template() {
43
43
  return html``;
@@ -57,12 +57,7 @@ class TemplateElement extends StyledElement {
57
57
  */
58
58
  renderTemplate() {
59
59
  const template = this._template || this.template();
60
- if (typeof template === 'string') {
61
- // just a plain string literal. no lit-html required
62
- this.getRoot().innerHTML = `${template}`;
63
- } else {
64
- render(template, this.getRoot());
65
- }
60
+ render(template, this.getRoot());
66
61
  }
67
62
 
68
63
  /**
@@ -310,7 +310,7 @@ export class ChildNodePart extends Part {
310
310
  } else if (Array.isArray(value)) {
311
311
  let childNodePart = this.parts[index];
312
312
  if (!childNodePart) {
313
- // TODO: this seems not correct :(
313
+ // TODO: this seems not correct :( (maybe Template vs DOM Parts !?)
314
314
  const templatePartCommentNodes = this.childNodes?.filter(
315
315
  (node) => node && node.nodeType === COMMENT_NODE && node.data === 'template-part',
316
316
  );
@@ -29,6 +29,7 @@ export const createTemplateString = (templateStrings, attributePlaceholders = ''
29
29
  // find (match) all elements to identify their attributes
30
30
  template = template.replace(elements, (_, name, attributesString, trailingSlash) => {
31
31
  let elementTagWithAttributes = name + attributesString.replaceAll('\x01', attributePlaceholders).trimEnd();
32
+ // TODO the closing case is weird.
32
33
  if (trailingSlash.length) elementTagWithAttributes += voidElements.test(name) ? ' /' : '></' + name;
33
34
  // collect all attribute parts so that we can place matching comment nodes
34
35
  const attributeParts = attributesString.replace(attributes, (attribute, name, valueWithQuotes, directive) => {
@@ -48,26 +49,23 @@ export const createTemplateString = (templateStrings, attributePlaceholders = ''
48
49
  }
49
50
  return parts.join('');
50
51
  });
52
+ // TODO create test to check if lf are in place
51
53
  return `
52
- ${attributesString.includes('\x01') ? attributeParts : ''}
53
- <${elementTagWithAttributes}>
54
- `;
54
+ ${attributesString.includes('\x01') ? attributeParts : ''}\n
55
+ <${elementTagWithAttributes}>\n
56
+ `.trim();
55
57
  });
56
58
  // replace interpolation placeholders with our indexed markers
57
59
  template = template.replace(partPositions, (partPosition) => {
58
60
  if (partPosition === '\x01') {
59
61
  return `<!--dom-part-${partIndex}--><!--/dom-part-${partIndex++}-->`;
60
- }
61
- if (partPosition === '\x02') {
62
+ } else if (partPosition === '\x02') {
62
63
  return `dom-part-${partIndex++}`;
63
64
  }
64
65
  });
65
- return `
66
- <!--template-part-->
67
- ${template}
68
- <!--/template-part-->
69
- `;
70
- // TODO: ^ this is actually important because of the whitespace
66
+ // TODO create test to check if lf are in place
67
+ // /n is important in the returns as we expect a certain order of text / comments an nodes
68
+ return `<!--template-part-->\n${template}\n<!--/template-part-->`.trim();
71
69
  };
72
70
 
73
71
  /**
@@ -4,7 +4,12 @@
4
4
  * @param {Element} domNode
5
5
  */
6
6
  const render = (template, domNode) => {
7
- template.renderInto(domNode);
7
+ if (typeof template === 'string') {
8
+ // just a plain string (or literal)
9
+ domNode.innerHTML = template;
10
+ } else {
11
+ template.renderInto(domNode);
12
+ }
8
13
  };
9
14
 
10
15
  export { render };
@@ -153,8 +153,7 @@ export const convertStringToHTML = (template) => {
153
153
  */
154
154
  export const convertStringToTemplate = (string) => {
155
155
  const template = globalThis.document?.createElement('template');
156
- // TODO: ensure that SSRed and CSRed templates have the same amount of whitespace text nodes
157
- template.innerHTML = string.trimStart().trimEnd();
156
+ template.innerHTML = string;
158
157
  return template.content;
159
158
  };
160
159