@epa-wg/custom-element 0.0.25 → 0.0.26

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/README.md CHANGED
@@ -348,9 +348,9 @@ within template
348
348
  [github-image]: https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/mark-github.svg
349
349
  [npm-image]: https://img.shields.io/npm/v/@epa-wg/custom-element.svg
350
350
  [npm-url]: https://npmjs.org/package/@epa-wg/custom-element
351
- [coverage-image]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.25/coverage/src/custom-element/coverage.svg
352
- [coverage-url]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.25/coverage/src/custom-element/index.html
353
- [storybook-url]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.25/storybook-static/index.html?path=/story/welcome--introduction
351
+ [coverage-image]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.26/coverage/src/custom-element/coverage.svg
352
+ [coverage-url]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.26/coverage/src/custom-element/index.html
353
+ [storybook-url]: https://unpkg.com/@epa-wg/custom-element-dist@0.0.26/storybook-static/index.html?path=/story/welcome--introduction
354
354
  [sandbox-url]: https://stackblitz.com/github/EPA-WG/custom-element?file=index.html
355
355
  [webcomponents-url]: https://www.webcomponents.org/element/@epa-wg/custom-element
356
356
  [webcomponents-img]: https://img.shields.io/badge/webcomponents.org-published-blue.svg
@@ -116,7 +116,7 @@ writeFileSync( '.././ide/customData-xsl.json', JSON.stringify( vsCode, undefined
116
116
  const intelliJ = {
117
117
  "$schema": "http://json.schemastore.org/web-types",
118
118
  "name": "@epa-wg/custom-element",
119
- "version": "0.0.25",
119
+ "version": "0.0.26",
120
120
  "js-types-syntax": "typescript",
121
121
  "description-markup": "markdown",
122
122
  "contributions": {
package/custom-element.js CHANGED
@@ -259,7 +259,7 @@ createXsltFromDom( templateNode, S = 'xsl:stylesheet' )
259
259
  for( const c of fr.childNodes )
260
260
  payload.append(xslDom.importNode(c,true))
261
261
 
262
- const embeddedTemplates = [...payload.querySelectorAll('template')];
262
+ const embeddedTemplates = [...payload.getElementsByTagName('xsl:template')];
263
263
  embeddedTemplates.forEach(t=>payload.ownerDocument.documentElement.append(t));
264
264
 
265
265
  const slotCall = $(xslDom,'call-template[name="slot"]')
@@ -288,8 +288,9 @@ xhrTemplate(src)
288
288
  // xhr.overrideMimeType("text/xml");
289
289
  xhr.onload = () =>
290
290
  { if( xhr.readyState === xhr.DONE && xhr.status === 200 )
291
- resolve( xhr.responseXML || create('div', xhr.responseText ) )
292
- reject(xhr.statusText)
291
+ resolve( xhr.responseXML?.body || xhr.responseXML || create('div', xhr.responseText ) )
292
+ else
293
+ reject(`${xhr.statusText} - ${src}`)
293
294
  };
294
295
  xhr.addEventListener("error", ev=>reject(ev) );
295
296
 
@@ -372,7 +373,7 @@ event2slice( x, sliceNames, ev, dce )
372
373
  s.append( obj2node(new FormData(el),'value', s.ownerDocument) )
373
374
  return s
374
375
  }
375
- const v = el.value ?? attr( sel, 'value' ) ;
376
+ const v = el.value ?? attr( el, 'value' ) ;
376
377
  cleanSliceValue();
377
378
  if( v === null || v === undefined )
378
379
  [...s.childNodes].filter(n=>n.localName!=='event').map(n=>n.remove());
@@ -396,19 +397,27 @@ const loadTemplateRoots = async ( src, dce )=>
396
397
  return [dce]
397
398
  if( src.startsWith('#') )
398
399
  return ( n =>
399
- { if(!n) return []
400
- const a = n.querySelectorAll(src)
401
- if( a.length )
402
- return [...a]
403
- const r = n.getRootNode();
404
- return r===n ? []: getByHashId(r)
400
+ { const a = n.querySelectorAll(src)
401
+ return [...( a.length ? a : n.getRootNode().querySelectorAll(src) )]
405
402
  })(dce.parentElement)
406
403
  try
407
- { // todo cache
408
- const dom = await xhrTemplate(src)
409
- const hash = new URL(src, location).hash
404
+ { const [path, hash] = src.split('#');
405
+ if( '.' === src.charAt(0))
406
+ src = new URL(path, dce.closest('[base]')?.getAttribute('base') || location ).href;
407
+ else
408
+ try
409
+ { src = import.meta.resolve( path );
410
+ if(hash)
411
+ src +='#'+hash;
412
+ }
413
+ catch( e )
414
+ { console.error(e.message) }
415
+ // todo cache
416
+ const dom = await xhrTemplate(src);
417
+ dce.setAttributeNS('xml', 'base', src );
418
+
410
419
  if( hash )
411
- { const ret = dom.querySelectorAll(hash);
420
+ { const ret = dom.querySelectorAll('#'+hash);
412
421
  if( ret.length )
413
422
  return [...ret]
414
423
  return [dce]
@@ -454,8 +463,11 @@ export function appendByDceId(parent,e,k)
454
463
  }
455
464
  export function merge( parent, fromArr )
456
465
  {
457
- if(!fromArr.length)
458
- return removeChildren(parent);
466
+ if( 'dce-root' === parent.firstElementChild?.localName && 'dce-root' !== fromArr[0].localName)
467
+ return;
468
+ if( !fromArr.length )
469
+ return 'dce-root' !== parent.firstElementChild?.localName && removeChildren(parent);
470
+
459
471
  const id2old = {};
460
472
  for( let c of parent.childNodes)
461
473
  { ASSERT( !id2old[c.dceId] );
@@ -482,7 +494,8 @@ export function merge( parent, fromArr )
482
494
  appendByDceId(parent,e,k)
483
495
  }
484
496
  for( let v of Object.values(id2old) )
485
- v.remove();
497
+ if( v.localName !== 'dce-root')
498
+ v.remove();
486
499
  }
487
500
  export function assureUID(n,attr)
488
501
  { if( !n.hasAttribute(attr) )
@@ -523,7 +536,7 @@ export const xPath = (x,root)=>
523
536
  ret += n.textContent;
524
537
  return ret
525
538
  }
526
- export const xslTags = 'stylesheet,transform,import,include,strip-space,preserve-space,output,key,decimal-format,namespace-alias,template,value-of,copy-of,number,apply-templates,apply-imports,for-each,sort,if,choose,when,otherwise,attribute-set,call-template,with-param,variable,param,text,processing-instruction,element,attribute,comment,copy,message,fallback'.split(',');
539
+ export const xslTags = 'stylesheet,transform,import,include,strip-space,preserve-space,output,key,decimal-format,namespace-alias,value-of,copy-of,number,apply-templates,apply-imports,for-each,sort,if,choose,when,otherwise,attribute-set,call-template,with-param,variable,param,text,processing-instruction,element,attribute,comment,copy,message,fallback'.split(',');
527
540
  export const toXsl = (el, defParent) => {
528
541
  const x = create('xsl:'+el.localName);
529
542
  for( let a of el.attributes )
@@ -725,13 +738,17 @@ CustomElement extends HTMLElement
725
738
  applySlices();
726
739
  }
727
740
  #applyAttribute(name, newValue)
728
- { let a = this.xml.querySelector(`attributes>${name}`);
741
+ { if( 'value' === name )
742
+ this.value = newValue;
743
+ let a = this.xml.querySelector(`attributes>${name}`);
729
744
  if( a )
730
745
  emptyNode(a).append( createText(a,newValue) );
731
746
  else
732
747
  { a = create( name, newValue, this.xml );
733
748
  this.xml.querySelector('attributes').append( a );
734
749
  }
750
+
751
+ this.dispatchEvent(new CustomEvent('change', { bubbles: true,detail: { [name]: newValue }}))
735
752
  }
736
753
  attributeChangedCallback(name, oldValue, newValue)
737
754
  { if( !this.xml || this.#inTransform )
package/demo/confused.svg CHANGED
@@ -1,5 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
2
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48" version="1.0">
3
+ <title>Confused</title>
3
4
  <defs>
4
5
  <linearGradient id="d">
5
6
  <stop offset="0"/>
package/demo/demo.css CHANGED
@@ -11,9 +11,13 @@ custom-element:not([tag]),
11
11
  dce-link,dce-1-slot,dce-2-slot,dce-3-slot,dce-4-slot,dce-2-slots,greet-element,pokemon-tile,
12
12
  dce-1,dce-2,dce-3,dce-4,dce-internal,dce-hash
13
13
  { box-shadow: 0 0 0.5rem lime; padding: 1rem; display: inline-block; flex:1; }
14
+ script{ display:none !important; }
14
15
  dd{ padding: 1rem;}
15
16
  p{ margin: 0;}
16
-
17
+ code{ font-weight: bold; color: green; text-wrap: nowrap; }
18
+ *{
19
+ gap: 1rem;
20
+ }
17
21
 
18
22
  html-demo-element h3
19
23
  { text-shadow: 0 0 0.25em white;
package/demo/embed-1.html CHANGED
@@ -1,3 +1,2 @@
1
1
  <h4>embed-1.html</h4>
2
- <custom-element tag="dce-embed-1" hidden>🖖</custom-element>
3
- <dce-embed-1>?</dce-embed-1>
2
+ <custom-element><template>🖖</template></custom-element>
@@ -3,26 +3,61 @@
3
3
  <head>
4
4
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
5
  <title>http-request Declarative Custom Element implementation demo</title>
6
- <link rel="icon" href="./wc-square.svg" />
6
+ <link rel="icon" href="./wc-square.svg"/>
7
7
 
8
8
  <script type="module" src="../http-request.js"></script>
9
9
  <script type="module" src="../custom-element.js"></script>
10
10
  <style>
11
11
  @import "./demo.css";
12
12
 
13
- button
14
- { display: inline-flex; flex-direction: column; align-items: center; flex: auto;
15
- box-shadow: inset silver 0 0 1rem; min-width: 12rem; padding: 1rem;
16
- color: coral; text-shadow: 1px 1px silver; font-weight: bolder;
13
+ button {
14
+ display: inline-flex;
15
+ flex-direction: column;
16
+ align-items: center;
17
+ flex: auto;
18
+ box-shadow: inset silver 0 0 1rem;
19
+ min-width: 12rem;
20
+ padding: 1rem;
21
+ color: coral;
22
+ text-shadow: 1px 1px silver;
23
+ font-weight: bolder;
24
+ }
25
+
26
+ button img {
27
+ max-height: 10vw;
28
+ min-height: 4rem;
29
+ }
30
+
31
+ table {
32
+ min-width: 16rem;
33
+ }
34
+
35
+ td {
36
+ border-bottom: 1px solid silver;
37
+ }
38
+
39
+ tfoot td {
40
+ border-bottom: none;
41
+ }
42
+
43
+ td, th {
44
+ text-align: right;
45
+ }
46
+
47
+ caption {
48
+ padding: 1rem;
49
+ font-weight: bolder;
50
+ font-family: sans-serif;
51
+ }
52
+
53
+ code {
54
+ text-align: right;
55
+ min-width: 3rem;
56
+ }
57
+
58
+ svg {
59
+ max-height: 3rem;
17
60
  }
18
- button img{ max-height: 10vw; min-height: 4rem;}
19
- table{ min-width: 16rem; }
20
- td{ border-bottom: 1px solid silver; }
21
- tfoot td{ border-bottom: none; }
22
- td,th{text-align: right; }
23
- caption{ padding: 1rem; font-weight: bolder; font-family: sans-serif; }
24
- code{ text-align: right; min-width: 3rem;}
25
- svg{ max-height: 3rem;}
26
61
  </style>
27
62
  </head>
28
63
  <body>
@@ -35,15 +70,16 @@
35
70
  <html-demo-element legend="1. reference the template in page DOM"
36
71
  description="should render hand wave with '👋 World!' and 'Hello World!'">
37
72
  <template>
38
- <template id="template1">
39
- <slot> Hello </slot> World!
40
- </template>
73
+ <template id="template1">
74
+ <slot> Hello</slot>
75
+ World!
76
+ </template>
41
77
 
42
- <custom-element tag="dce-internal" src="#template1"></custom-element>
43
- <!-- no need for loading fallback as the template exists -->
78
+ <custom-element tag="dce-internal" src="#template1"></custom-element>
79
+ <!-- no need for loading fallback as the template exists -->
44
80
 
45
- <dce-internal>👋</dce-internal>
46
- <dce-internal></dce-internal>
81
+ <dce-internal>👋</dce-internal>
82
+ <dce-internal></dce-internal>
47
83
  </template>
48
84
  </html-demo-element>
49
85
 
@@ -51,67 +87,67 @@
51
87
  <html-demo-element legend="2. without TAG, inline instantiation"
52
88
  description="hash value in SRC references template by ID. Should output 'construction' 2 times">
53
89
  <template>
54
- <template id="template2">
55
- 🏗️ construction
56
- </template>
90
+ <template id="template2">
91
+ 🏗️ construction
92
+ </template>
57
93
 
58
- <custom-element src="#template2"></custom-element>
59
- <custom-element src="#template2"></custom-element>
94
+ <custom-element src="#template2"></custom-element>
95
+ <custom-element src="#template2"></custom-element>
60
96
  </template>
61
97
  </html-demo-element>
62
98
 
63
- <html-demo-element legend="3. external SVG file" >
99
+ <html-demo-element legend="3. external SVG file">
64
100
  <template>
65
- <custom-element tag="dce-external" src="confused.svg" >
66
- <template><i>loading from SVG ...</i></template>
67
- </custom-element>
68
- <dce-external></dce-external>
69
- <custom-element src="confused.svg" >
70
- <i>inline DCE loading from SVG ...</i>
71
- </custom-element>
72
- <custom-element src="no.svg" >
73
- <i>fallback for missing image</i>
74
- </custom-element>
101
+ <custom-element tag="dce-external" src="confused.svg">
102
+ <template><i>loading from SVG ...</i></template>
103
+ </custom-element>
104
+ <dce-external></dce-external>
105
+ <custom-element src="confused.svg">
106
+ <i>inline DCE loading from SVG ...</i>
107
+ </custom-element>
108
+ <custom-element src="no.svg">
109
+ <i>fallback for missing image</i>
110
+ </custom-element>
75
111
  </template>
76
112
  </html-demo-element>
77
113
 
78
114
  <html-demo-element legend="4. external XSLT file"
79
115
  description="This external templates generated the tree for DCE data set"
80
- >
116
+ >
81
117
  <a href="tree.xsl">tree.xsl</a>
82
118
  <template>
83
- <custom-element tag="dce-external-4" src="tree.xsl" >
84
- <template><i>loading from XSLT ...</i></template>
85
- </custom-element>
86
- <dce-external-4 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-4>
87
- <custom-element src="tree.xsl" data-smile="👼" data-basket="🍒">
88
- <i>inline DCE loading from XSLT ...</i>
89
- </custom-element>
119
+ <custom-element tag="dce-external-4" src="tree.xsl">
120
+ <template><i>loading from XSLT ...</i></template>
121
+ </custom-element>
122
+ <dce-external-4 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-4>
123
+ <custom-element src="tree.xsl" data-smile="👼" data-basket="🍒">
124
+ <i>inline DCE loading from XSLT ...</i>
125
+ </custom-element>
90
126
  </template>
91
127
  </html-demo-element>
92
128
 
93
129
  <html-demo-element legend="5. external HTML template" description="Should render 👋👌, svg, formula">
94
130
  <template>
95
- <custom-element tag="dce-external-5" src="html-template.html" >
96
- <template><i>loading from HTML file ...</i></template>
97
- </custom-element>
98
- <dce-external-5 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-5>
99
- <custom-element src="html-template.html" data-smile="👼" data-basket="🍒">
100
- <i>inline DCE loading from HTML file ...</i>
101
- </custom-element>
131
+ <custom-element tag="dce-external-5" src="html-template.html">
132
+ <template><i>loading from HTML file ...</i></template>
133
+ </custom-element>
134
+ <dce-external-5 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-5>
135
+ <custom-element src="html-template.html" data-smile="👼" data-basket="🍒">
136
+ <i>inline DCE loading from HTML file ...</i>
137
+ </custom-element>
102
138
  </template>
103
139
  </html-demo-element>
104
140
 
105
141
  <html-demo-element legend="6. HTML, SVG by ID within external file" description="Should render 👋, svg, formula">
106
142
  <a href="html-template.html">html-template.html</a>
107
143
  <template>
108
- <custom-element src="html-template.html#wave" >
144
+ <custom-element src="html-template.html#wave">
109
145
  <template><i>loading HTML from templates file ...</i></template>
110
146
  </custom-element>
111
- <custom-element src="html-template.html#dwc-logo" >
112
- <template><i>loading SVG from templates file ...</i></template>
147
+ <custom-element src="html-template.html#dwc-logo">
148
+ <template><i>loading SVG from templates file ...</i></template>
113
149
  </custom-element>
114
- <custom-element src="html-template.html#sophomores-dream" >
150
+ <custom-element src="html-template.html#sophomores-dream">
115
151
  <template><i>loading MathML from HTML file ...</i></template>
116
152
  </custom-element>
117
153
  </template>
@@ -119,10 +155,10 @@
119
155
  <html-demo-element legend="7. XSLT by ID within external file" description="Should render tree, and fallback">
120
156
  <a href="html-template.html">html-template.html</a>
121
157
  <template>
122
- <custom-element src="html-template.xhtml#embedded-xsl" >
123
- <template>whole XSLT is embedded into HTML body</template>
158
+ <custom-element src="html-template.xhtml#embedded-xsl">
159
+ <template>whole XSLT is embedded into HTML body</template>
124
160
  </custom-element>
125
- <custom-element src="html-template.html#none" >
161
+ <custom-element src="html-template.html#none">
126
162
  <template><i>element with id=none is missing in template</i></template>
127
163
  </custom-element>
128
164
  </template>
@@ -132,13 +168,37 @@
132
168
  description="Should render Vulcan Salute 🖖"
133
169
  >
134
170
  <template>
135
- <custom-element src="embed-1.html" >
136
- loading from embed-1.html ...
137
- </custom-element>
171
+ <custom-element src="embed-1.html">
172
+ loading from embed-1.html ...
173
+ </custom-element>
174
+ </template>
175
+ </html-demo-element>
176
+
177
+
178
+ <html-demo-element legend="9. external file with invoking of relative template as hash by enclosed custom-element"
179
+ description="Should render Vulcan Salute 🖖"
180
+ >
181
+ <template>
182
+ <custom-element src="./lib-dir/embed-lib.html#embed-relative-hash">
183
+ loading from embed-1.html ...
184
+ </custom-element>
138
185
  </template>
139
186
  </html-demo-element>
140
- <html-demo-element legend="embed-1.html external file " src="embed-1.html">
187
+
188
+ <html-demo-element
189
+ legend="10. external file with invoking of template in another relative path file by enclosed custom-element"
190
+ description="Should render Vulcan Salute 🖖"
191
+ >
192
+ <template>
193
+ <custom-element src="./lib-dir/embed-lib.html#embed-relative-file">
194
+ loading from ./lib-dir/embed-lib.html ...
195
+ </custom-element>
196
+ </template>
141
197
  </html-demo-element>
198
+
199
+
200
+ <html-demo-element legend="embed-1.html external file" src="embed-1.html"></html-demo-element>
201
+ <html-demo-element legend="embed-lib.html with multiple templates" src="embed-lib.html"></html-demo-element>
142
202
  <!--
143
203
  <html-demo-element legend="9. deep external files, complex rendering" >
144
204
  <template>
@@ -0,0 +1,24 @@
1
+ <?xml version="1.0" standalone="no"?>
2
+ <!DOCTYPE svg>
3
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32" baseProfile="full" viewBox="-21 -21 42 42">
4
+ <defs>
5
+ <radialGradient id="b" cx=".2" cy=".2" r=".5" fx=".2" fy=".2">
6
+ <stop offset="0" stop-color="#fff" stop-opacity=".7"/>
7
+ <stop offset="1" stop-color="#fff" stop-opacity="0"/>
8
+ </radialGradient>
9
+ <radialGradient id="a" cx=".5" cy=".5" r=".5">
10
+ <stop offset="0" stop-color="#ff0"/>
11
+ <stop offset=".75" stop-color="#ff0"/>
12
+ <stop offset=".95" stop-color="#ee0"/>
13
+ <stop offset="1" stop-color="#e8e800"/>
14
+ </radialGradient>
15
+ </defs>
16
+ <circle r="20" fill="url(#a)" stroke="#000" stroke-width=".15"/>
17
+ <circle r="20" fill="url(#b)"/>
18
+ <g id="c">
19
+ <ellipse cx="-6" cy="-7" rx="2.5" ry="4"/>
20
+ <path fill="none" stroke="#000" stroke-linecap="round" stroke-width=".5" d="M10.6 2.7a4 4 0 0 0 4 3"/>
21
+ </g>
22
+ <use xlink:href="#c" transform="scale(-1 1)"/>
23
+ <path fill="none" stroke="#000" stroke-width=".75" d="M-12 5a13.5 13.5 0 0 0 24 0 13 13 0 0 1-24 0"/>
24
+ </svg>
@@ -0,0 +1,34 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
+ <title>templates for embedded and external DCE lib dependencies</title>
6
+ </head>
7
+ <body>
8
+ <h1>templates for embedded and external DCE lib dependencies</h1>
9
+
10
+ <template id="embed-lib-component">
11
+ 👋 from embed-lib-component
12
+ </template>
13
+
14
+ <template id="embed-relative-hash">
15
+ 👌 from embed-relative-hash invoking
16
+ <module-url slice="full-url" src="./embed-lib.html"></module-url>
17
+ <a href="{//full-url}#embed-lib-component">
18
+ <img src="{//full-url}/../Smiley.svg"/>
19
+ #embed-lib-component
20
+ </a>:
21
+ <custom-element src="#embed-lib-component"></custom-element>
22
+ </template>
23
+
24
+ <template id="embed-relative-file">
25
+ 👍 from embed-relative-file invoking
26
+ <module-url slice="up-url" src="../embed-1.html"></module-url>
27
+ <a href="{//up-url}">../embed-1.html</a>:
28
+ <custom-element src="../embed-1.html">
29
+ loading from ../embed-1.html ...
30
+ </custom-element>
31
+ </template>
32
+
33
+ </body>
34
+ </html>