Skip to content

Commit 46da741

Browse files
committed
Change wrap property to namespace enum
Replace the `wrap` property with a `namespace` property that accepts a type-safe enum ('html' | 'svg' | 'mathml') instead of an arbitrary tag name. The namespace defaults to 'html', matching the current default behavior. tidy Tweak the description in the ADR omit word remove unneeded `as const` ADR: for clarity, use backticks rather than quotes when enumerating possible values for the namespace option update the namespace validity check to a form that typescript can use for narrowing remove link to namespaceURI docs since the namespace option does not directly allow specifying namespaces (the connection can be elucidated in the user-facing docs)
1 parent 623da45 commit 46da741

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

library/src/plugins/watchers/patchElements.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,27 @@ type PatchElementsMode =
1818
| 'before'
1919
| 'after'
2020

21+
const namespaceToTag = {
22+
'html': '',
23+
'svg': 'svg',
24+
'mathml': 'math'
25+
}
26+
27+
type Namespace = keyof typeof namespaceToTag
28+
2129
type PatchElementsArgs = {
2230
elements: string
2331
mode: PatchElementsMode
2432
selector: string
2533
useViewTransition: boolean
26-
wrap: string
34+
namespace: Namespace
2735
}
2836

2937
watcher({
3038
name: 'datastar-patch-elements',
3139
apply(
3240
ctx,
33-
{ elements = '', selector = '', mode = 'outer', useViewTransition = '', wrap = '' },
41+
{ elements = '', selector = '', mode = 'outer', useViewTransition = '', namespace = 'html' },
3442
) {
3543
switch (mode) {
3644
case 'remove':
@@ -50,12 +58,16 @@ watcher({
5058
throw ctx.error('PatchElementsExpectedSelector')
5159
}
5260

61+
if (!(namespace === 'html' || namespace === 'svg' || namespace === 'mathml')) {
62+
throw ctx.error('PatchElementsInvalidNamespace', { namespace })
63+
}
64+
5365
const args2: PatchElementsArgs = {
5466
mode,
5567
selector,
5668
elements,
5769
useViewTransition: useViewTransition.trim() === 'true',
58-
wrap,
70+
namespace
5971
}
6072

6173
if (supportsViewTransitions && useViewTransition) {
@@ -68,8 +80,10 @@ watcher({
6880

6981
const onPatchElements = (
7082
{ error }: WatcherContext,
71-
{ elements, selector, mode, wrap }: PatchElementsArgs,
83+
{ elements, selector, mode, namespace }: PatchElementsArgs,
7284
) => {
85+
const wrap = namespaceToTag[namespace]
86+
7387
const elementsWithSvgsRemoved = elements.replace(
7488
/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim,
7589
'',

sdk/ADR.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ServerSentEventGenerator.PatchElements(
109109
selector?: string,
110110
mode?: ElementPatchMode,
111111
useViewTransition?: boolean,
112-
wrap?: string,
112+
namespace?: 'html' | 'svg' | 'mathml',
113113
eventId?: string,
114114
retryDuration?: durationInMilliseconds
115115
}
@@ -138,7 +138,7 @@ ServerSentEventGenerator.PatchElements(
138138
data: mode inner
139139
data: selector #feed
140140
data: useViewTransition true
141-
data: wrap div
141+
data: namespace html
142142
data: elements <div id="feed">
143143
data: elements <span>1</span>
144144
data: elements </div>
@@ -184,7 +184,7 @@ ServerSentEventGenerator.PatchElements(
184184
event: datastar-patch-elements
185185
data: mode append
186186
data: selector #vis
187-
data: wrap svg
187+
data: namespace svg
188188
data: elements <circle id="c1" cx="10" r="5" fill="red"/>
189189
data: elements <circle id="c2" cx="20" r="5" fill="green"/>
190190
data: elements <circle id="c3" cx="30" r="5" fill="blue"/>
@@ -234,7 +234,7 @@ String enum defining how elements are patched into the DOM.
234234
| `selector` | string | Element ID | CSS selector for target element. If a selector is not specified, each element must have an ID specified. |
235235
| `mode` | ElementPatchMode | `outer` | How to patch the element |
236236
| `useViewTransition` | boolean | `false` | Enable view transitions API |
237-
| `wrap` | string | Tag name | Tag name used to control the [namespace](https://developer.mozilla.org/en-US/docs/Web/API/Element/namespaceURI) of any new elements. If a tag name is not specified, elements will be created in the HTML namespace. |
237+
| `namespace` | `html` \| `svg` \| `mathml` | `html` | Namespace in which to create new elements |
238238

239239
### Implementation
240240

@@ -244,7 +244,7 @@ String enum defining how elements are patched into the DOM.
244244
- `selector SELECTOR\n` (if provided)
245245
- `mode PATCH_MODE\n` (if not `outer`)
246246
- `useViewTransition true\n` (if `true`)
247-
- `wrap TAG_NAME\n` (if provided)
247+
- `namespace NAMESPACE\n` (if not `html`)
248248
- `elements HTML_LINE\n` (for each line of HTML)
249249

250250
---

0 commit comments

Comments
 (0)