|
| 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 use either a |
| 42 | +[`DocumentPart`](./DOM-Parts-Imperative.md#option-2-documentpartgroup) on the |
| 43 | +`DocumentFragment` produced by the template. |
| 44 | + |
| 45 | +## Enablement in main `document` |
| 46 | + |
| 47 | +For the main document, a new `parseparts` attribute is introduced that indicates |
| 48 | +to the parser it should parse DOM part tags as DOM parts. |
| 49 | + |
| 50 | +```html |
| 51 | +<div parseparts></div> |
| 52 | +``` |
| 53 | + |
| 54 | +Even for `innerHTML` use cases, only DOM nodes that are wrapped DOM with the |
| 55 | +`parseparts` attribute will use declarative parts. |
| 56 | + |
| 57 | +## Node parts |
| 58 | + |
| 59 | +For node parts, a `{{}}` tag could be provided as an attribute. |
| 60 | + |
| 61 | +```html |
| 62 | +<template> |
| 63 | + <section {{}}></section> |
| 64 | +</template> |
| 65 | +``` |
| 66 | + |
| 67 | +Would create a `NodePart` for `<section>`. |
| 68 | + |
| 69 | +## Partial attributes |
| 70 | + |
| 71 | +Allowing `{{}}` inside an attribute works the same as a |
| 72 | +[partial attribute update](./DOM-Parts-Imperative.md#partial-attribute-updates), |
| 73 | +in that it will create an `AttributePart` for the entire attribute, but it will |
| 74 | +have multi-valued `value` property. |
| 75 | + |
| 76 | +```html |
| 77 | +<template> |
| 78 | + <section> |
| 79 | + <h1 id="name">{{}}</h1> |
| 80 | + Email: <a id="link" href="mailto:{{}}">{{}}</a> |
| 81 | + </section> |
| 82 | +</template> |
| 83 | +``` |
| 84 | + |
| 85 | +The `AttributePart` for `href` would have `value` equal to `['mailto:', '']`. |
| 86 | +Empty string values are provided for any markers without default content. |
| 87 | + |
| 88 | +## Default Values |
| 89 | + |
| 90 | +To provide default values for any part, a part can be split into a start `{{#}}` |
| 91 | +and finish `{{/}}` indicators. |
| 92 | + |
| 93 | +```html |
| 94 | +<template> |
| 95 | + <section> |
| 96 | + <h1 id="name">{{#}}Ryosuke Niwa{{/}}</h1> |
| 97 | + Email: |
| 98 | + < a id= "link" href= "mailto:{{#}}[email protected]{{/}}"> |
| 99 | + |
| 100 | + </a> |
| 101 | + </section> |
| 102 | +</template> |
| 103 | +``` |
| 104 | + |
| 105 | +## Names and Metadata |
| 106 | + |
| 107 | +Templating systems may need to serialize data about the nodes they are marking |
| 108 | +into the processing instructions. Or at the very least parts could be named so |
| 109 | +that they are easier to fetch. |
| 110 | + |
| 111 | +```html |
| 112 | +<div>{{email data="foo"}}</div> |
| 113 | +``` |
| 114 | + |
| 115 | +This could be exposed on the imperative API to be consumed in JavaScript by |
| 116 | +application logic. |
| 117 | + |
| 118 | +## Choice of marker |
| 119 | + |
| 120 | +The `{{}}` and `{{#}}{{/}}` are reasonable DOM part markers, but it could be |
| 121 | +something else, or it could even be something that is declared as the |
| 122 | +placeholder. |
| 123 | + |
| 124 | +## Compatability |
| 125 | + |
| 126 | +It may be challenging to implement `<template>` parsing of `{{}}` for |
| 127 | +compatability reasons, so there may need to be restrictions such as only |
| 128 | +allowing this template syntax in a new API like `createTemplateWithParts()`. |
0 commit comments