You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: patching-explainer.md
+22-6Lines changed: 22 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -206,9 +206,16 @@ The patches uses a single marker node:
206
206
207
207
##### Markers with start/end attributes
208
208
209
-
This alternative adds `start` and `end` boolean attributes to the marker nodes, so that the start/end of a range are defined by the markers, not attributes on `<template>`.
209
+
This alternative leans into markers as the way to define content ranges, using optional `start` and `end` boolean attributes. The range to replace is defined by the `contentfor` attribute on `<template>`, and the range is determined as such:
210
210
211
-
This makes it possible to replace a `<title>` element.
211
+
- First find the first marker in the tree with the right name
212
+
- If there's no `start` or `end` attribute, or both are present, that marker itself is the whole range
213
+
- If there's a `start` attribute, traverse next siblings until a matching marker with `end` is found, or the end of the sibling list
214
+
- If there's an `end` attribute, traverse previous siblings until a matching marker with `start` is found, or the start of the sibling list
215
+
216
+
Everything in that range is removed (including the marker nodes) before adding new nodes from the `<template>` element.
217
+
218
+
This makes it possible to replace a `<title>` element:
212
219
213
220
<details>
214
221
<summary>Example</summary>
@@ -220,22 +227,26 @@ This makes it possible to replace a `<title>` element.
220
227
<?marker name="metadata" end?>
221
228
</head>
222
229
223
-
<templatecontentname="metadata">
230
+
<templatecontentfor="metadata">
224
231
<title>Page 2</title>
225
232
</template>
226
233
```
227
234
228
235
</details>
229
236
230
-
Details around `<title>` and the RCDATA tokenizer state are the main reason that the tag name needs to be repeated in other alterantives, but this isn't needed in this option, the children of `<template>` elements are just the new content. The `contentname` attribute can refer to either marker node(s) or an element.
237
+
Details around `<title>` and the RCDATA tokenizer state are the main reason that the tag name needs to be repeated in other alterantives, but this isn't needed in this option, the children of `<template>` elements are just the new content.
231
238
232
-
When `start` and `end` aren't used, the marker node is replaced. The interleaved patching example then becomes:
239
+
The interleaved patching example then becomes:
233
240
234
241
<details>
235
242
<summary>Example</summary>
236
243
237
244
```html
238
-
<templatecontentname=search-results>
245
+
<!-- explicit markers are needed *inside* the element being patched. end markers are omitted -->
@@ -258,6 +269,11 @@ When `start` and `end` aren't used, the marker node is replaced. The interleaved
258
269
259
270
</details>
260
271
272
+
Possible ergonomic additions this option:
273
+
274
+
- Targeting elements (not just markers) and replacing all child nodes by default
275
+
- A `keep` attribute on markers that would avoid removing them
276
+
261
277
## Script-initiated patching
262
278
263
279
`streamHTMLUnsafe()` is being pursued as a [separate proposal](https://github.com/whatwg/html/issues/2142), but will also work with patching. When `<template contentmethod>` appears in the streamed HTML, those patches can apply to descendants of element on which `streamHTMLUnsafe()` was called.
0 commit comments