@epa-wg/custom-element 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,107 @@
1
+ # Logs
2
+ logs
3
+ *.log
4
+ npm-debug.log*
5
+ yarn-debug.log*
6
+ yarn-error.log*
7
+ lerna-debug.log*
8
+
9
+ # Diagnostic reports (https://nodejs.org/api/report.html)
10
+ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11
+
12
+ # Runtime data
13
+ pids
14
+ *.pid
15
+ *.seed
16
+ *.pid.lock
17
+
18
+ # Directory for instrumented libs generated by jscoverage/JSCover
19
+ lib-cov
20
+
21
+ # Coverage directory used by tools like istanbul
22
+ coverage
23
+ *.lcov
24
+
25
+ # nyc test coverage
26
+ .nyc_output
27
+
28
+ # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29
+ .grunt
30
+
31
+ # Bower dependency directory (https://bower.io/)
32
+ bower_components
33
+
34
+ # node-waf configuration
35
+ .lock-wscript
36
+
37
+ # Compiled binary addons (https://nodejs.org/api/addons.html)
38
+ build/Release
39
+
40
+ # Dependency directories
41
+ node_modules/
42
+ jspm_packages/
43
+
44
+ # TypeScript v1 declaration files
45
+ typings/
46
+
47
+ # TypeScript cache
48
+ *.tsbuildinfo
49
+
50
+ # Optional npm cache directory
51
+ .npm
52
+
53
+ # Optional eslint cache
54
+ .eslintcache
55
+
56
+ # Microbundle cache
57
+ .rpt2_cache/
58
+ .rts2_cache_cjs/
59
+ .rts2_cache_es/
60
+ .rts2_cache_umd/
61
+
62
+ # Optional REPL history
63
+ .node_repl_history
64
+
65
+ # Output of 'npm pack'
66
+ *.tgz
67
+
68
+ # Yarn Integrity file
69
+ .yarn-integrity
70
+
71
+ # dotenv environment variables file
72
+ .env
73
+ .env.test
74
+
75
+ # parcel-bundler cache (https://parceljs.org/)
76
+ .cache
77
+
78
+ # Next.js build output
79
+ .next
80
+
81
+ # Nuxt.js build / generate output
82
+ .nuxt
83
+ dist
84
+
85
+ # Gatsby files
86
+ .cache/
87
+ # Comment in the public line in if your project uses Gatsby and *not* Next.js
88
+ # https://nextjs.org/blog/next-9-1#public-directory-support
89
+ # public
90
+
91
+ # vuepress build output
92
+ .vuepress/dist
93
+
94
+ # Serverless directories
95
+ .serverless/
96
+
97
+ # FuseBox cache
98
+ .fusebox/
99
+
100
+ # DynamoDB Local files
101
+ .dynamodb/
102
+
103
+ # TernJS port file
104
+ .tern-port
105
+
106
+ /.idea/
107
+ /0/
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
@@ -0,0 +1,28 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
5
+ <option name="myValues">
6
+ <value>
7
+ <list size="14">
8
+ <item index="0" class="java.lang.String" itemvalue="nobr" />
9
+ <item index="1" class="java.lang.String" itemvalue="noembed" />
10
+ <item index="2" class="java.lang.String" itemvalue="comment" />
11
+ <item index="3" class="java.lang.String" itemvalue="noscript" />
12
+ <item index="4" class="java.lang.String" itemvalue="embed" />
13
+ <item index="5" class="java.lang.String" itemvalue="script" />
14
+ <item index="6" class="java.lang.String" itemvalue="dce-link" />
15
+ <item index="7" class="java.lang.String" itemvalue="dce-1-slot" />
16
+ <item index="8" class="java.lang.String" itemvalue="dce-2-slots" />
17
+ <item index="9" class="java.lang.String" itemvalue="greet-element" />
18
+ <item index="10" class="java.lang.String" itemvalue="pokemon-tile" />
19
+ <item index="11" class="java.lang.String" itemvalue="html-demo-element" />
20
+ <item index="12" class="java.lang.String" itemvalue="custom-element" />
21
+ <item index="13" class="java.lang.String" itemvalue="slot" />
22
+ </list>
23
+ </value>
24
+ </option>
25
+ <option name="myCustomValuesEnabled" value="true" />
26
+ </inspection_tool>
27
+ </profile>
28
+ </component>
package/.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager">
4
+ <output url="file://$PROJECT_DIR$/out" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/custom-element.iml" filepath="$PROJECT_DIR$/.idea/custom-element.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
package/0/1.xml ADDED
@@ -0,0 +1,23 @@
1
+ <div>
2
+ <payload>
3
+ <span slot="">
4
+ <br>
5
+ <br>
6
+ </span>
7
+ <p slot="description">Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;\n the seed
8
+ grows in size as the PokΓ©mon does.
9
+ </p>
10
+ <span slot="">
11
+ <br>
12
+ <br>
13
+ </span>
14
+ </payload>
15
+ <attributes>
16
+ <title>bulbasaur</title>
17
+ <data-smile>πŸ‘Ό</data-smile>
18
+ <image-src>https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg</image-src>
19
+ </attributes>
20
+ <dataset>
21
+ <smile>πŸ‘Ό</smile>
22
+ </dataset>
23
+ </div>
package/0/1.xsl ADDED
@@ -0,0 +1,26 @@
1
+ <xsl:stylesheet version="1.0"
2
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
+ <xsl:output method="html" />
4
+
5
+ <xsl:template match="/">
6
+ <xsl:apply-templates select="//attributes"/>
7
+ </xsl:template>
8
+ <xsl:template match="attributes">
9
+
10
+ <h3><xsl:value-of select="title"></xsl:value-of></h3> <!-- title is an attribute in instance
11
+ mapped into /*/attributes/title -->
12
+ <xsl:if test="//smile"> <!-- data-smile DCE instance attribute,
13
+ mapped into /*/dataset/smile
14
+ used in condition -->
15
+ <!-- data-smile DCE instance attribute, used as HTML -->
16
+ <div>Smile as: <xsl:value-of select="//smile"></xsl:value-of></div>
17
+ </xsl:if>
18
+ <img src="{image-src}" alt="{title}" /> <!-- image-src and title are DCE instance attributes,
19
+ mapped into /*/attributes/
20
+ used within output attribute via curly brackets -->
21
+ <!-- `slot name=xxx` replaced with elements with `slot=xxx` attribute -->
22
+ <p><value-of xmlns="http://www.w3.org/1999/XSL/Transform" select="//*[@slot=&quot;description&quot;]"/></p>
23
+
24
+ </xsl:template>
25
+
26
+ </xsl:stylesheet>
package/0/a.html ADDED
@@ -0,0 +1,42 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
+ <title>custom-element Declarative Custom Element implementation demo</title>
6
+ <script type="module" src="../custom-element.js"></script>
7
+ <style>
8
+ body{ display: flex; flex-wrap: wrap; align-content: stretch; gap: 1rem; }
9
+ dce-link,dce-1-slot,dce-2-slots,greet-element,pokemon-tile
10
+ { box-shadow: 0 0 0.5rem lime; padding: 1rem; display: inline-block;}
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <custom-element tag="pokemon-tile" hidden>
15
+ <h3><xsl:value-of select="title"/></h3> <!-- title is an attribute in instance
16
+ mapped into /*/attributes/title -->
17
+ <xsl:if test="//smile"> <!-- data-smile DCE instance attribute,
18
+ mapped into /*/dataset/smile
19
+ used in condition -->
20
+ <!-- data-smile DCE instance attribute, used as HTML -->
21
+ <div>Smile as: <xsl:value-of select='//smile'/></div>
22
+ </xsl:if>
23
+ <img src="{image-src}" alt="{title}"/> <!-- image-src and title are DCE instance attributes,
24
+ mapped into /*/attributes/
25
+ used within output attribute via curly brackets -->
26
+ <!-- `slot name=xxx` replaced with elements with `slot=xxx` attribute -->
27
+ <p><slot name="description"><i>description is not available</i></slot></p>
28
+ </custom-element>
29
+ <pokemon-tile title="bulbasaur"
30
+ data-smile="πŸ‘Ό"
31
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg">
32
+
33
+ <p slot="description">Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;
34
+ the seed grows in size as the PokΓ©mon does.</p>
35
+
36
+ </pokemon-tile>
37
+
38
+ <pokemon-tile title="ninetales"
39
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/38.svg">
40
+ </pokemon-tile>
41
+ </body>
42
+ </html>
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # custom-element
2
+ `Declarative Custom Element` proof of concept as a part of [WCCG in Declarative custom elements](https://github.com/w3c/webcomponents-cg/issues/32#issuecomment-1321037301)
3
+ discussion.
4
+
5
+ It allows to define custom HTML tag with template filled from slots and attributes.
6
+
7
+ [![git][github-image] GitHub][git-url]
8
+ | Live demo: [custom-element][demo-url]
9
+ | [tests project][git-test-url]
10
+
11
+ [![NPM version][npm-image]][npm-url] [![coverage][coverage-image]][coverage-url]
12
+
13
+ # use
14
+ ## install
15
+ use via CDN
16
+ ```html
17
+ <script type="module" src="https://unpkg.com/epa-wg/custom-element/@0.0/custom-element.js"></script>
18
+ ```
19
+ NPM
20
+ ```shell
21
+ npm i -P epa-wg/custom-element
22
+ ```
23
+ yarn
24
+ ```shell
25
+ yarn add epa-wg/custom-element
26
+ ```
27
+
28
+ ## [Live demo][demo-url]
29
+ ```html
30
+ <custom-element tag="pokemon-tile" hidden>
31
+ <h3><xsl:value-of select="title"/></h3> <!-- title is an attribute in instance
32
+ mapped into /*/attributes/title -->
33
+ <xsl:if test="//smile"> <!-- data-smile DCE instance attribute,
34
+ mapped into /*/dataset/smile
35
+ used in condition -->
36
+ <!-- data-smile DCE instance attribute, used as HTML -->
37
+ <div>Smile as: <xsl:value-of select='//smile'/></div>
38
+ </xsl:if>
39
+ <img src="{image-src}" alt="{title}"/> <!-- image-src and title are DCE instance attributes,
40
+ mapped into /*/attributes/
41
+ used within output attribute via curly brackets -->
42
+ <!-- `slot name=xxx` replaced with elements with `slot=xxx` attribute -->
43
+ <p><slot name="description"><i>description is not available</i></slot></p>
44
+ </custom-element>
45
+
46
+ <pokemon-tile title="bulbasaur"
47
+ data-smile="πŸ‘Ό"
48
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg">
49
+ <p slot="description">Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;
50
+ the seed grows in size as the PokΓ©mon does.</p>
51
+ </pokemon-tile>
52
+
53
+ <pokemon-tile title="ninetales"
54
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/38.svg">
55
+ </pokemon-tile>
56
+ ```
57
+ generates HTML
58
+ ```html
59
+ <pokemon-tile title="bulbasaur" data-smile="πŸ‘Ό"
60
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg"
61
+ >
62
+ <h3>bulbasaur</h3>
63
+ <div>Smile as: πŸ‘Ό</div>
64
+ <img src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg" alt="bulbasaur">
65
+ <p>Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;
66
+ the seed grows in size as the PokΓ©mon does.</p>
67
+ </pokemon-tile>
68
+ <pokemon-tile title="ninetales"
69
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/38.svg"
70
+ >
71
+ <h3>ninetales</h3>
72
+ <img src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/38.svg" alt="ninetales">
73
+ <p></p>
74
+ </pokemon-tile>
75
+ ```
76
+
77
+ # Implementation notes
78
+ ## Life cycle
79
+ ### `custom-element` declaration
80
+ * constructor injects payload into XSL template
81
+ * creates a class for custom element extending HTMLElement
82
+ * registers element by `tag` attribute
83
+
84
+ NOTE: attempt to register custom element with already registered tag name would fail due to w3c standard limitations.
85
+ The scoped custom element registry is still a proposal.
86
+
87
+ ### custom element instance
88
+ constructor creates XML with
89
+ * root matching the tag
90
+ * payload
91
+ * dom nodes with `slot` attribute stay inside
92
+ * attributes
93
+ * ?dataset
94
+
95
+ DOM content is replaced with results of instance XML transformation by declaration XSLT.
96
+
97
+ # template syntax
98
+ ## Attributes
99
+ curly braces `{}` in attributes implemented as [attribute value template](https://www.w3.org/TR/xslt20/#attribute-value-templates)
100
+
101
+ The names in curly braces are matching the instance attributes. I.e. in XML node `/my-component/attributes/`.
102
+
103
+ To access payload XPath could start with `/*/payload/`. I.e. `{/*/payload//label}` refers to all `label` tags in payload.
104
+
105
+ ## Slots
106
+ `<slot name="xxx">` is replaced by payload top elements with `slot` attribute matching the name,
107
+ i.e. slot `xxx` is matching `<i slot="xxx">...</i>` in payload.
108
+ ```html
109
+ <custom-element tag="with-description" >
110
+ <slot name="description">description is not available</slot>
111
+ <!-- same as
112
+ <xsl:value-of select='/*/payload/*[@slot="description"]'/>
113
+ -->
114
+ </custom-element>
115
+ <with-description>
116
+ <p slot="description">Bulbasaur is a cute PokΓ©mon ...</p>
117
+ </with-description>
118
+ ```
119
+
120
+ ## loops, variables
121
+ Loop implemented via [xsl:for-each](https://developer.mozilla.org/en-US/docs/Web/XSLT/Element/for-each)
122
+
123
+ [Variables in XSLT](https://developer.mozilla.org/en-US/docs/Web/XSLT/Element/variable)
124
+
125
+ ## [XPath](https://developer.mozilla.org/en-US/docs/Web/XSLT/Transforming_XML_with_XSLT/The_Netscape_XSLT_XPath_Reference)
126
+ is available in `{}` in attributes, in `xsl:for-each`, `xsl:if`, `xsl:value-of`, and other XSL tags.
127
+
128
+ XPath is a selector language to navigate over custom element instance data, attributes, and payload.
129
+
130
+ # troubleshooting
131
+ ## `{}` does not give a value
132
+ * try to add as attribute you could observe and put the value of node name or text to identify the current location in data
133
+ within template
134
+ ```xml
135
+ <b title="{name(*)} : {text()}">xml tag name:<xsl:value-of select='name()'/></b>
136
+ ```
137
+
138
+ [git-url]: https://github.com/EPA-WG/custom-element
139
+ [git-test-url]: https://github.com/EPA-WG/custom-element-test
140
+ [demo-url]: https://unpkg.com/epa-wg/custom-element/@0.0/index.html
141
+ [github-image]: https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/mark-github.svg
142
+ [npm-image]: https://img.shields.io/npm/v/epa-wg/custom-element.svg
143
+ [npm-url]: https://npmjs.org/package/epa-wg/custom-element
144
+ [coverage-image]: https://unpkg.com/epa-wg/custom-element-test@0.0.2/coverage/coverage.svg
145
+ [coverage-url]: https://unpkg.com/epa-wg/custom-element-test@0.0.2/coverage/lcov-report/index.html
@@ -0,0 +1,4 @@
1
+ export function log(x: any): void;
2
+ export class CustomElement extends HTMLElement {
3
+ }
4
+ export default CustomElement;
@@ -0,0 +1,81 @@
1
+ const XML_DECLARATION = '<?xml version="1.0" encoding="UTF-8"?>'
2
+ , XSL_NS_URL = 'http://www.w3.org/1999/XSL/Transform';
3
+
4
+ // const log = x => console.debug( new XMLSerializer().serializeToString( x ) );
5
+
6
+ const create = ( tag, t='' ) =>
7
+ {
8
+ const e = document.createElement( tag );
9
+ if(t) e.innerText = t;
10
+ return e;
11
+ }
12
+ function xml2dom( xmlString )
13
+ {
14
+ return new DOMParser().parseFromString( XML_DECLARATION + xmlString, "application/xml" )
15
+ }
16
+ function bodyXml( dce )
17
+ { const s = new XMLSerializer().serializeToString( dce );
18
+ return s.substring( s.indexOf( '>' ) + 1, s.lastIndexOf( '<' ) );
19
+ }
20
+ function slot2xsl( s )
21
+ { const v = document.createElementNS( XSL_NS_URL, 'value-of' );
22
+ v.setAttribute( 'select', `//*[@slot="${ s.name }"]` );
23
+ s.parentNode.replaceChild( v, s );
24
+ }
25
+ function injectData( root, sectionName, arr, cb )
26
+ {
27
+ const inject = ( tag, parent, s ) =>
28
+ {
29
+ parent.append( s = create( tag ) );
30
+ return s;
31
+ };
32
+ const l = inject( sectionName, root );
33
+ [ ...arr ].forEach( e => l.append( cb(e) ) );
34
+ }
35
+ function assureSlot( e )
36
+ { if( !e.slot )
37
+ { if( !e.setAttribute )
38
+ e = create( 'span', e.textContent.replaceAll('\n','') );
39
+ e.setAttribute( 'slot', '' )
40
+ }
41
+ return e;
42
+ }
43
+ export class CustomElement extends HTMLElement
44
+ {
45
+ constructor()
46
+ { super();
47
+
48
+ [ ...this.getElementsByTagName( 'slot' ) ].forEach( slot2xsl );
49
+ const p = new XSLTProcessor();
50
+ p.importStylesheet( xml2dom( `<xsl:stylesheet version="1.0"
51
+ xmlns:xsl="${XSL_NS_URL}">
52
+ <xsl:output method="html" />
53
+
54
+ <xsl:template match="/">
55
+ <xsl:apply-templates select="//attributes"/>
56
+ </xsl:template>
57
+ <xsl:template match="attributes">
58
+ ${ bodyXml(this) }
59
+ </xsl:template>
60
+
61
+ </xsl:stylesheet>` ) );
62
+
63
+ window.customElements.define( this.getAttribute( 'tag' ), class extends HTMLElement
64
+ {
65
+ constructor()
66
+ {
67
+ super();
68
+ const x = create( 'div' );
69
+ injectData( x, 'payload' , this.childNodes, assureSlot );
70
+ injectData( x, 'attributes' , this.attributes, e=>create( e.nodeName, e.value ) );
71
+ injectData( x, 'dataset' , Object.keys( this.dataset ), k=>create( k, this.dataset[ k ] ) );
72
+ const f = p.transformToFragment( x, document );
73
+ this.innerHTML = '';
74
+ [...f.childNodes].forEach(e=>this.appendChild(e));
75
+ }
76
+ });
77
+ }
78
+ }
79
+
80
+ window.customElements.define( 'custom-element', CustomElement );
81
+ export default CustomElement;
package/index.html ADDED
@@ -0,0 +1,129 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
+ <title>custom-element Declarative Custom Element implementation demo</title>
6
+ <script type="module" src="custom-element.js"></script>
7
+ <style>
8
+ body,nav{ display: flex; flex-wrap: wrap; align-content: stretch; gap: 1rem; }
9
+ nav{ flex-direction: column;}
10
+ dce-link,dce-1-slot,dce-2-slot,dce-3-slot,dce-4-slot,dce-2-slots,greet-element,pokemon-tile
11
+ { box-shadow: 0 0 0.5rem lime; padding: 1rem; display: inline-block;}
12
+ dd{ padding: 1rem;}
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <nav>
17
+ <h3><code>custom-element</code> demo</h3>
18
+ <a href="https://github.com/EPA-WG/custom-element"
19
+ ><img src="https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/mark-github.svg">GIT</a>
20
+ <p>
21
+ This <em>Declarative Custom Element</em> allows to define<br/>
22
+ custom HTML tag with template filled from slots and attributes.
23
+ </p>
24
+ </nav>
25
+ <html-demo-element legend="1. simple payload"
26
+ description="payload is ignored as in DCE definition there is no default slot">
27
+ <template>
28
+ <custom-element tag="dce-link" hidden>
29
+ <a href="#">link πŸ˜ƒ</a>
30
+ </custom-element>
31
+ <dce-link><i>πŸ‹</i></dce-link>
32
+ </template>
33
+ </html-demo-element>
34
+
35
+ <html-demo-element legend="2. payload with slot definition and slot value"
36
+ description="slots are filled as in template+shadow root">
37
+ <template>
38
+ <custom-element tag="dce-1-slot" hidden>
39
+ <slot name="slot1"> πŸ˜ƒ</slot>
40
+ </custom-element>
41
+ <dce-1-slot><i slot="slot1">πŸ₯•</i></dce-1-slot>
42
+ </template>
43
+ </html-demo-element>
44
+
45
+ <html-demo-element legend="2a. payload with slot definition and slot value"
46
+ description="same slot can be used multiple times unlike in TEMPLATE">
47
+ <template>
48
+ <custom-element tag="dce-2-slots" hidden>
49
+ <slot name="slot2"> πŸ˜ƒ</slot> and again:
50
+ <slot name="slot2"> πŸ˜ƒ</slot>
51
+ </custom-element>
52
+ <dce-2-slots><i slot="slot2">πŸ₯•</i></dce-2-slots>
53
+ </template>
54
+ </html-demo-element>
55
+
56
+ <html-demo-element legend="2b. named default slot"
57
+ description="slot without `name` attribute or with blank value `name=''` use whole payload">
58
+ <template>
59
+ <custom-element tag="dce-3-slot" hidden>
60
+ #1
61
+ <slot name=""> πŸ˜ƒ</slot>
62
+ and
63
+ <slot> πŸ˜ƒ</slot>
64
+ </custom-element>
65
+ <dce-3-slot><i slot="">πŸ₯•</i></dce-3-slot>
66
+ </template>
67
+ </html-demo-element>
68
+
69
+ <html-demo-element legend="2c. named default slot"
70
+ description="slot without `name` attribute or with blank value `name=''` use whole payload">
71
+ <template>
72
+ <custom-element tag="dce-4-slot" hidden>
73
+ #2
74
+ <slot name=""> πŸ˜ƒ</slot>
75
+ and
76
+ <slot> πŸ˜ƒ</slot>
77
+ </custom-element>
78
+ <dce-4-slot>πŸ₯•</dce-4-slot>
79
+ </template>
80
+ </html-demo-element>
81
+ <html-demo-element legend="2d. default slot"
82
+ description="slot without `name` attribute use whole payload">
83
+ <template>
84
+
85
+ <custom-element tag="greet-element" hidden>
86
+ <slot> Hello </slot> World!
87
+ </custom-element>
88
+ <greet-element>πŸ‘‹</greet-element>
89
+ </template>
90
+ </html-demo-element>
91
+
92
+ <html-demo-element legend="3. πŸ’ͺ DCE template "
93
+ description="Complex case with slots, attributes, dataset, conditional render">
94
+ <template>
95
+
96
+ <custom-element tag="pokemon-tile" hidden>
97
+ <h3><xsl:value-of select="title"/></h3> <!-- title is an attribute in instance
98
+ mapped into /*/attributes/title -->
99
+ <xsl:if test="//smile"> <!-- data-smile DCE instance attribute,
100
+ mapped into /*/dataset/smile
101
+ used in condition -->
102
+ <!-- data-smile DCE instance attribute, used as HTML -->
103
+ <div>Smile as: <xsl:value-of select='//smile'/></div>
104
+ </xsl:if>
105
+ <img src="{image-src}" alt="{title}"/> <!-- image-src and title are DCE instance attributes,
106
+ mapped into /*/attributes/
107
+ used within output attribute via curly brackets -->
108
+ <!-- `slot name=xxx` replaced with elements with `slot=xxx` attribute -->
109
+ <p><slot name="description"><i>description is not available</i></slot></p>
110
+ </custom-element>
111
+ <pokemon-tile title="bulbasaur"
112
+ data-smile="πŸ‘Ό"
113
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg">
114
+
115
+ <p slot="description">Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;
116
+ the seed grows in size as the PokΓ©mon does.</p>
117
+
118
+ </pokemon-tile>
119
+
120
+ <pokemon-tile title="ninetales"
121
+ image-src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/38.svg">
122
+ </pokemon-tile>
123
+ </template>
124
+ </html-demo-element>
125
+
126
+ <script type="module" src="https://unpkg.com/html-demo-element@1.0/html-demo-element.js"></script>
127
+
128
+ </body>
129
+ </html>
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@epa-wg/custom-element",
3
+ "version": "0.0.1",
4
+ "description": "Declarative Custom Element as W3C proposal PoC with native(XSLT) based templating",
5
+ "browser": "custom-element.js",
6
+ "module": "custom-element.js",
7
+ "exports": {
8
+ ".": "./custom-element.js",
9
+ "./package.json": "./package.json",
10
+ "./CustomElement": "./custom-element.js"
11
+ },
12
+ "files": [
13
+ "*"
14
+ ],
15
+ "type": "module",
16
+ "types": "./custom-element.d.ts",
17
+ "scripts": {
18
+ "test": "echo \"test would reside in https://github.com/EPA-WG/custom-element-test\" && exit 0",
19
+ "typings": "npx -p typescript tsc custom-element.js --declaration --allowJs --emitDeclarationOnly "
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/EPA-WG/custom-element.git"
24
+ },
25
+ "keywords": [
26
+ "WebComponent",
27
+ "Declarative Custom Element",
28
+ "XSLT",
29
+ "JS",
30
+ "javascript"
31
+ ],
32
+ "author": {
33
+ "name": "Sasha Firsov",
34
+ "email": "suns@simulationworks.com",
35
+ "url": "https://blog.firsov.net/"
36
+ },
37
+ "license": "Apache-2.0",
38
+ "bugs": {
39
+ "url": "https://github.com/EPA-WG/custom-element/issues"
40
+ },
41
+ "homepage": "https://github.com/EPA-WG/custom-element#readme",
42
+ "funding": {
43
+ "type": "patreon",
44
+ "url": "https://www.patreon.com/sashafirsov"
45
+ }
46
+ }