Skip to content

Commit da053bb

Browse files
Add more syntax for declarative DOM
1 parent 0e7d430 commit da053bb

4 files changed

+296
-181
lines changed

proposals/DOM-Parts-Declarative-Template.md

-73
This file was deleted.

proposals/DOM-Parts-Declarative.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# DOM Parts Declarative API
2+
3+
This proposal covers the declarative API for creating a DOM part within a
4+
`<template>` element and main document HTML.
5+
6+
## Proposal
7+
8+
Double curly braces `{{}}` provide markers for DOM parts.
9+
10+
In the most basic level, this proposal can produce the three DOM parts:
11+
`NodePart`, `AttributePart`, `ChildNodePart`.
12+
13+
### Basic Examples
14+
15+
Suppose we had the following template in some HTML extension template languages
16+
where `{name}` and `{email}` indicated locations of dynamic data insertion:
17+
18+
```html
19+
<section>
20+
<h1 id="name">{name}</h1>
21+
Email: <a id="link" href="mailto:{email}">{email}</a>
22+
</section>
23+
```
24+
25+
And the application has produced an HTML `<template>` with the following
26+
content:
27+
28+
```html
29+
<template>
30+
<section>
31+
<h1 id="name">{{}}</h1>
32+
Email: <a id="link" href="{{}}">{{}}</a>
33+
</section>
34+
</template>
35+
```
36+
37+
This will create a `ChildNodePart` attached to `<h1>` with no content, an
38+
`AttributePart` connected to `href`, and a `ChildNodePart` connected to `<a>`
39+
with no content.
40+
41+
A framework could fetch these parts using the `getPartRoot()` on the
42+
[`DocumentFragment`](./DOM-Parts-Imperative.md#retrieving-a-documentpartroot)
43+
and then calling [`getParts()`](./DOM-Parts-Imperative.md#getparts).
44+
45+
## Enablement
46+
47+
For any DOM node, including `<template>`, a new `parseparts` attribute is
48+
introduced that indicates to the parser it should parse DOM part tags as DOM
49+
parts.
50+
51+
```html
52+
<div parseparts></div>
53+
```
54+
55+
Even for `innerHTML` use cases, only DOM nodes that are wrapped DOM with the
56+
`parseparts` attribute will use declarative parts.
57+
58+
## Node parts
59+
60+
For node parts, a `{{}}` tag could be provided as an attribute.
61+
62+
```html
63+
<template>
64+
<section {{}}></section>
65+
</template>
66+
```
67+
68+
Would create a `NodePart` for `<section>`.
69+
70+
## Partial attributes
71+
72+
Allowing `{{}}` inside an attribute works the same as a
73+
[partial attribute update](./DOM-Parts-Imperative.md#partial-attribute-updates),
74+
in that it will create an `AttributePart` for the entire attribute, but it will
75+
have multi-valued `value` property.
76+
77+
```html
78+
<template>
79+
<section>
80+
<h1 id="name">{{}}</h1>
81+
Email: <a id="link" href="mailto:{{}}">{{}}</a>
82+
</section>
83+
</template>
84+
```
85+
86+
The `AttributePart` for `href` would have `statics` equal to `['mailto:', '']`.
87+
Empty string values are provided for any markers without default content.
88+
89+
## Default Values
90+
91+
To provide default values for any part, a part can be split into a start `{{#}}`
92+
and finish `{{/}}` indicators.
93+
94+
```html
95+
<template>
96+
<section>
97+
<h1 id="name">{{#}}Ryosuke Niwa{{/}}</h1>
98+
Email:
99+
<a id="link" href="mailto:{{#}}[email protected]{{/}}">
100+
101+
</a>
102+
</section>
103+
</template>
104+
```
105+
106+
## Names and Metadata
107+
108+
Templating systems may need to serialize data about the nodes they are marking
109+
into the processing instructions. Or at the very least parts could be named so
110+
that they are easier to fetch.
111+
112+
```html
113+
<div>{{email data="foo"}}</div>
114+
```
115+
116+
This could be exposed on the imperative API to be consumed in JavaScript by
117+
application logic.
118+
119+
For parts that are split between opening and closing, it's possible to have
120+
multiple metadata values which are included as two elements in the `metadata`
121+
field.
122+
123+
```
124+
{{# metadata1}}default value{{/ metadata2}}
125+
```
126+
127+
## Choice of marker
128+
129+
The `{{}}` and `{{#}}{{/}}` are reasonable DOM part markers, but it could be
130+
something else, or it could even be something that is declared as the
131+
placeholder.
132+
133+
## Compatability
134+
135+
It may be challenging to implement `<template>` parsing of `{{}}` for
136+
compatability reasons, so there may need to be restrictions such as only
137+
allowing this template syntax in a new API like `createTemplateWithParts()`.

0 commit comments

Comments
 (0)