@parhelia/head 1.0.12951 → 1.0.13032
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/package.json +15 -1
- package/src/aem.d.ts +30 -0
- package/src/aem.js +80 -0
- package/src/bridge-core.d.ts +4 -0
- package/src/bridge-core.js +4 -0
- package/src/orchardcore.d.ts +41 -0
- package/src/orchardcore.js +145 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/head",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13032",
|
|
4
4
|
"description": "Make a rendering head editable in Parhelia, on any framework and without one: the editing bridge, slot markers, the in-place refresh (with a DOM-patching one for heads without a client framework), the rendered-content identity capture reads, the editor signal a head's server checks (/server), rich-text sanitising (/rich-text), per-CMS marker helpers (/contentful, /umbraco), a SitecoreAI editing host's canvas render (/sitecore), the build values and the Vite build adapter (/vite). No React: React heads add @parhelia/react, Next.js heads @parhelia/next, Astro heads @parhelia/astro.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"publishConfig": {
|
|
@@ -50,6 +50,14 @@
|
|
|
50
50
|
"types": "./src/typo3.d.ts",
|
|
51
51
|
"default": "./src/typo3.js"
|
|
52
52
|
},
|
|
53
|
+
"./aem": {
|
|
54
|
+
"types": "./src/aem.d.ts",
|
|
55
|
+
"default": "./src/aem.js"
|
|
56
|
+
},
|
|
57
|
+
"./orchardcore": {
|
|
58
|
+
"types": "./src/orchardcore.d.ts",
|
|
59
|
+
"default": "./src/orchardcore.js"
|
|
60
|
+
},
|
|
53
61
|
"./sitecore": {
|
|
54
62
|
"types": "./src/sitecore.d.ts",
|
|
55
63
|
"default": "./src/sitecore.js"
|
|
@@ -92,6 +100,12 @@
|
|
|
92
100
|
"typo3": [
|
|
93
101
|
"./src/typo3.d.ts"
|
|
94
102
|
],
|
|
103
|
+
"aem": [
|
|
104
|
+
"./src/aem.d.ts"
|
|
105
|
+
],
|
|
106
|
+
"orchardcore": [
|
|
107
|
+
"./src/orchardcore.d.ts"
|
|
108
|
+
],
|
|
95
109
|
"sitecore": [
|
|
96
110
|
"./src/sitecore.d.ts"
|
|
97
111
|
],
|
package/src/aem.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** One rendered field's identity, as an AEM render knows it. */
|
|
2
|
+
export type AemFieldRef = {
|
|
3
|
+
/** The item's connector id (a component's, or the page's for a field of its `jcr:content`). */
|
|
4
|
+
item: string;
|
|
5
|
+
/** The merged dialog's field name (`jcr:title`, `text`, a nested `a/b`). */
|
|
6
|
+
field: string;
|
|
7
|
+
/** The field's element holds rich text. */
|
|
8
|
+
richText?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** The vocabulary's wire name. */
|
|
12
|
+
export declare const AEM_MARKER_VOCABULARY: "aem";
|
|
13
|
+
/** The entity kind of a field marker: an item. */
|
|
14
|
+
export declare const AEM_ITEM_KIND: "item";
|
|
15
|
+
export declare const AEM_ITEM_ATTRIBUTE: string;
|
|
16
|
+
export declare const AEM_FIELD_ATTRIBUTE: string;
|
|
17
|
+
export declare const AEM_RICHTEXT_ATTRIBUTE: string;
|
|
18
|
+
/** The component sentinel's attribute carrying the connector's id. */
|
|
19
|
+
export declare const AEM_ID_ATTRIBUTE: string;
|
|
20
|
+
/** The component sentinel's attribute carrying the component's path below the page's `jcr:content`. */
|
|
21
|
+
export declare const AEM_PATH_ATTRIBUTE: string;
|
|
22
|
+
/** Parhelia addresses an AEM item as its current state. */
|
|
23
|
+
export declare const PARHELIA_AEM_VERSION: "0";
|
|
24
|
+
|
|
25
|
+
/** The key of an item marker: the connector's id (a lowercase UUID); undefined for anything else. */
|
|
26
|
+
export declare function aemItemKey(id: string): string | undefined;
|
|
27
|
+
/** The attributes of one rendered field; nothing when `edit` is false. */
|
|
28
|
+
export declare function aemFieldAttributes(ref: AemFieldRef, edit: boolean): Record<string, string>;
|
|
29
|
+
/** Whether `field` is a dialog field's name a marker may carry. */
|
|
30
|
+
export declare function isAemFieldName(field: string): boolean;
|
package/src/aem.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/head/aem` — **the AEM marker names, and the helpers a head renders them with** (AEM AEM7a, wire `aem-1` §7.3). This module
|
|
3
|
+
* is the one JavaScript source: the editor's `aem` vocabulary (`@parhelia/core`, `bridge/vocabularies/aem/`) reads them by importing
|
|
4
|
+
* this module, a headless AEM head (AEM7b) renders them, the Parhelia connector inside AEM writes them in its editing renders (the HTL
|
|
5
|
+
* helper `io.parhelia.aem.connector.markup.FieldMarker`, the component filter's sentinels) and the AEM store answers them
|
|
6
|
+
* (`Parhelia.Aem.Protocol.MarkerNames`). All of them are pinned to one test vector, `providers/aem/protocol-vectors/aem.markers.json`:
|
|
7
|
+
* a renamed attribute fails a test in each language.
|
|
8
|
+
*
|
|
9
|
+
* Fields carry the **AEM vocabulary** (store-resolved: a render knows the item and the dialog field's name, never Parhelia's field ids,
|
|
10
|
+
* so the page's store answers which item and field a marker names):
|
|
11
|
+
*
|
|
12
|
+
* data-aem-item="<connector id>" the item (entity kind `item`, key: the connector's id, a lowercase UUIDv7): a component's, or the
|
|
13
|
+
* page's for a field of its `jcr:content`
|
|
14
|
+
* data-aem-field="<field name>" the field (the merged dialog's property name: `jcr:title`, `text`, a nested `a/b`)
|
|
15
|
+
* data-aem-richtext="true" on a rich-text field's element
|
|
16
|
+
*
|
|
17
|
+
* An AEM item lies in one language root, so a marker names no locale: an edit writes in the locale of the page the canvas shows.
|
|
18
|
+
* Components use Parhelia's own sentinels: a `data-component-start` / `-end` script pair carrying the component's Guid (the store's
|
|
19
|
+
* name-based Guid over `{installation}:{id}`), `data-component-name` its `sling:resourceType`, `data-aem-id` the connector's id and
|
|
20
|
+
* `data-aem-path` its path below the page's `jcr:content`; the bridge reads them itself.
|
|
21
|
+
*
|
|
22
|
+
* Markers appear in Parhelia's editing renders only; every other render is AEM's own markup.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** The vocabulary's wire name: what the canvas sends with the markers it found, and what the AEM store states. */
|
|
26
|
+
export const AEM_MARKER_VOCABULARY = "aem";
|
|
27
|
+
|
|
28
|
+
/** The entity kind of a field marker: an item (a page or a component). */
|
|
29
|
+
export const AEM_ITEM_KIND = "item";
|
|
30
|
+
|
|
31
|
+
export const AEM_ITEM_ATTRIBUTE = "data-aem-item";
|
|
32
|
+
export const AEM_FIELD_ATTRIBUTE = "data-aem-field";
|
|
33
|
+
export const AEM_RICHTEXT_ATTRIBUTE = "data-aem-richtext";
|
|
34
|
+
|
|
35
|
+
/** The component sentinel's attribute carrying the connector's id. */
|
|
36
|
+
export const AEM_ID_ATTRIBUTE = "data-aem-id";
|
|
37
|
+
|
|
38
|
+
/** The component sentinel's attribute carrying the component's path below the page's `jcr:content`. */
|
|
39
|
+
export const AEM_PATH_ATTRIBUTE = "data-aem-path";
|
|
40
|
+
|
|
41
|
+
/** Parhelia addresses an AEM item as its current state (version 0). */
|
|
42
|
+
export const PARHELIA_AEM_VERSION = "0";
|
|
43
|
+
|
|
44
|
+
const ITEM_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
45
|
+
const FIELD = /^[^\s"'<>&=]{1,256}$/;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The key of an item marker: the connector's id as it spells it (a lowercase UUID); undefined for anything else (an uppercase or
|
|
49
|
+
* braced spelling is no connector id).
|
|
50
|
+
* @param {string} id
|
|
51
|
+
* @returns {string | undefined}
|
|
52
|
+
*/
|
|
53
|
+
export const aemItemKey = id => (typeof id === "string" && ITEM_ID.test(id) ? id : undefined);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The attributes of one rendered field: the item and the field, and the rich-text flag. Nothing when `edit` is false (a render outside
|
|
57
|
+
* Parhelia's editing render carries no marker).
|
|
58
|
+
* @param {{ item: string; field: string; richText?: boolean }} ref
|
|
59
|
+
* @param {boolean} edit
|
|
60
|
+
* @returns {Record<string, string>}
|
|
61
|
+
*/
|
|
62
|
+
export const aemFieldAttributes = (ref, edit) => {
|
|
63
|
+
if (!edit) return {};
|
|
64
|
+
const key = aemItemKey(ref.item);
|
|
65
|
+
if (!key) throw new TypeError(`An AEM field marker needs the item's connector id; got ${JSON.stringify(ref.item)}.`);
|
|
66
|
+
if (typeof ref.field !== "string" || !FIELD.test(ref.field))
|
|
67
|
+
throw new TypeError(`An AEM field marker needs the dialog field's name; got ${JSON.stringify(ref.field)}.`);
|
|
68
|
+
/** @type {Record<string, string>} */
|
|
69
|
+
const attributes = { [AEM_ITEM_ATTRIBUTE]: key, [AEM_FIELD_ATTRIBUTE]: ref.field };
|
|
70
|
+
if (ref.richText) attributes[AEM_RICHTEXT_ATTRIBUTE] = "true";
|
|
71
|
+
return attributes;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Whether `field` is a dialog field's name a marker may carry (no whitespace, quotes, angle brackets, `&` or `=`; at most 256
|
|
76
|
+
* characters).
|
|
77
|
+
* @param {string} field
|
|
78
|
+
* @returns {boolean}
|
|
79
|
+
*/
|
|
80
|
+
export const isAemFieldName = field => typeof field === "string" && FIELD.test(field);
|
package/src/bridge-core.d.ts
CHANGED
|
@@ -57,6 +57,8 @@ export interface ParheliaBridgeScriptOptions {
|
|
|
57
57
|
crossOrigin?: ParheliaBridgeCrossOrigin;
|
|
58
58
|
integrity?: string;
|
|
59
59
|
referrerPolicy?: ParheliaBridgeReferrerPolicy;
|
|
60
|
+
/** The host's label (`data-parhelia-source`), which the bridge's `ready` handshake carries, e.g. `sxa`. */
|
|
61
|
+
source?: string;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
export type ParheliaBridgeScriptProps = ParheliaBridgeActivationOptions & ParheliaBridgeScriptOptions;
|
|
@@ -74,6 +76,8 @@ export interface ParheliaBridgeScriptAttributes {
|
|
|
74
76
|
"data-parhelia-editor-bridge": "runtime";
|
|
75
77
|
/** The Parhelia origin when it serves the bridge; absent for a bridge origin. */
|
|
76
78
|
"data-parhelia-parent-origin"?: string;
|
|
79
|
+
/** The host's label when one is given (`source`). */
|
|
80
|
+
"data-parhelia-source"?: string;
|
|
77
81
|
}
|
|
78
82
|
|
|
79
83
|
/** The bridge's URL on `origin`; without one, on the Parhelia origin, then on the environment's bridge origin. */
|
package/src/bridge-core.js
CHANGED
|
@@ -115,6 +115,7 @@ export function parheliaBridgeScriptProps({
|
|
|
115
115
|
crossOrigin,
|
|
116
116
|
integrity,
|
|
117
117
|
referrerPolicy,
|
|
118
|
+
source,
|
|
118
119
|
} = {}) {
|
|
119
120
|
const editorOrigin = origin || originAlias || (bridgeOrigin ? "" : parheliaOrigin());
|
|
120
121
|
const src = resolveParheliaBridgeUrl(editorOrigin || bridgeOrigin);
|
|
@@ -130,6 +131,7 @@ export function parheliaBridgeScriptProps({
|
|
|
130
131
|
referrerPolicy,
|
|
131
132
|
"data-parhelia-editor-bridge": "runtime",
|
|
132
133
|
"data-parhelia-parent-origin": editorOrigin ? new URL(src).origin : undefined,
|
|
134
|
+
"data-parhelia-source": source || undefined,
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
137
|
|
|
@@ -168,6 +170,8 @@ export function loadParheliaBridge({ queryParam = "parhelia", ...options } = {})
|
|
|
168
170
|
script.setAttribute("data-parhelia-editor-bridge", props["data-parhelia-editor-bridge"]);
|
|
169
171
|
const parent = props["data-parhelia-parent-origin"];
|
|
170
172
|
if (parent) script.setAttribute("data-parhelia-parent-origin", parent);
|
|
173
|
+
const source = props["data-parhelia-source"];
|
|
174
|
+
if (source) script.setAttribute("data-parhelia-source", source);
|
|
171
175
|
document.body.appendChild(script);
|
|
172
176
|
return true;
|
|
173
177
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** An opening marker comment, its attributes decoded. */
|
|
2
|
+
export type OrchardOpenMarker = {
|
|
3
|
+
close: false;
|
|
4
|
+
kind: string;
|
|
5
|
+
/** The Parhelia key of a coordinate the render exposed. */
|
|
6
|
+
key?: string;
|
|
7
|
+
/** The content item's id: a coordinate's, or in the key's place for one the render did not expose. */
|
|
8
|
+
id?: string;
|
|
9
|
+
attributes: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
/** A closing marker comment. */
|
|
12
|
+
export type OrchardCloseMarker = { close: true; kind: string };
|
|
13
|
+
export type OrchardMarker = OrchardOpenMarker | OrchardCloseMarker;
|
|
14
|
+
/** One balanced marker pair: its two comments share a parent. */
|
|
15
|
+
export type OrchardMarkerPair = { marker: OrchardOpenMarker; open: Comment; close: Comment };
|
|
16
|
+
|
|
17
|
+
/** The vocabulary's wire name. */
|
|
18
|
+
export declare const ORCHARDCORE_MARKER_VOCABULARY: "orchardcore";
|
|
19
|
+
/** The marker kinds of an editing render. */
|
|
20
|
+
export declare const ORCHARDCORE_MARKER_KINDS: readonly string[];
|
|
21
|
+
/** The entity kind of a field marker naming an item. */
|
|
22
|
+
export declare const ORCHARDCORE_ITEM_KIND: "item";
|
|
23
|
+
/** The entity kind of a field marker naming a widget instance. */
|
|
24
|
+
export declare const ORCHARDCORE_WIDGET_KIND: "widget";
|
|
25
|
+
/** On the inert coordinate script of a session's page: the one element a selector finds there. */
|
|
26
|
+
export declare const ORCHARDCORE_RENDER_ATTRIBUTE: string;
|
|
27
|
+
export declare const ORCHARDCORE_KEY_ATTRIBUTE: string;
|
|
28
|
+
export declare const ORCHARDCORE_PATH_ATTRIBUTE: string;
|
|
29
|
+
export declare const ORCHARDCORE_TYPE_ATTRIBUTE: string;
|
|
30
|
+
export declare const ORCHARDCORE_KIND_ATTRIBUTE: string;
|
|
31
|
+
|
|
32
|
+
/** A value as the marker wrote it, decoded (.NET's `HtmlEncoder.Default`). */
|
|
33
|
+
export declare function decodeMarkerValue(value: string): string;
|
|
34
|
+
/** One comment's marker; undefined for a comment that is none of the grammar's. */
|
|
35
|
+
export declare function parseOrchardMarker(data: string): OrchardMarker | undefined;
|
|
36
|
+
/** The entity a coordinate's key names; undefined for anything that is no key. */
|
|
37
|
+
export declare function orchardEntityOf(key: string | undefined): { entityKind: string; entityKey: string } | undefined;
|
|
38
|
+
/** Every balanced marker pair below `root`, in document order of their opening comments. */
|
|
39
|
+
export declare function orchardMarkerPairs(root: Document | Element): OrchardMarkerPair[];
|
|
40
|
+
/** The nodes between a pair's comments that are content: elements and text with more than white space. */
|
|
41
|
+
export declare function orchardPairContent(pair: OrchardMarkerPair): ChildNode[];
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/head/orchardcore` — **the Orchard Core marker grammar, and the walk over a rendered page** (Orchard Core OC7a, wire `oc-1` §7). The
|
|
3
|
+
* module's editing render marks its shapes with HTML comment pairs (`<!--parhelia:{kind} {attributes}-->…<!--/parhelia:{kind}-->`), so no template
|
|
4
|
+
* of a theme is touched and Razor and Liquid themes alike are marked. This module is the one JavaScript source of that grammar: the editor's
|
|
5
|
+
* `orchardcore` vocabulary (`@parhelia/core`, `bridge/vocabularies/orchardcore/`) reads a page with it. The vector
|
|
6
|
+
* `providers/orchardcore/protocol-vectors/orchardcore.markers.json` pins it, the same file the module's rows (`MarkersVectorRows`) and the
|
|
7
|
+
* provider's name pins read: a renamed kind or attribute fails a test in each language.
|
|
8
|
+
*
|
|
9
|
+
* A marker's first attribute names its coordinate: `key` (the Parhelia key of a coordinate the render exposed, the rendered item, a displayed item, a
|
|
10
|
+
* widget instance) or `id` in its place (a content item's id, for a shape no display built: no identity was recorded, so a store answers nothing). A
|
|
11
|
+
* `field` marker adds `path` (the save's path, wire §5.1), `type` and `kind`, or `source` for the theme's own marker (the Liquid filter
|
|
12
|
+
* `parhelia_field`, the Razor tag helper `parhelia-field`). A `widget` pair of the rendered page's own composition adds `guid`, the Guid the store issues for the
|
|
13
|
+
* instance, and a `slot` pair of it adds `guid` and `slot`, the owner's Guid and the slot's key as the store's composition lists it (wire amendment OC5-1); an empty slot's
|
|
14
|
+
* pair holds a block with `data-parhelia-empty-slot`. A text field whose shape shows nothing holds a placeholder with `data-parhelia-blank-field` and its label as
|
|
15
|
+
* `data-empty-hint` (amendment OC5-3). Attribute values are HTML-encoded as .NET's `HtmlEncoder.Default` encodes them.
|
|
16
|
+
*
|
|
17
|
+
* Markers appear in Parhelia's editing renders only; every other render is the theme's markup, byte for byte.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** The vocabulary's wire name: what the canvas sends with the markers it found, and what the Orchard Core store states. */
|
|
21
|
+
export const ORCHARDCORE_MARKER_VOCABULARY = "orchardcore";
|
|
22
|
+
|
|
23
|
+
/** The marker kinds of an editing render. */
|
|
24
|
+
export const ORCHARDCORE_MARKER_KINDS = ["item", "widget", "slot", "part", "field"];
|
|
25
|
+
|
|
26
|
+
/** The entity kind of a field marker naming an item (`item:`, `set:`). */
|
|
27
|
+
export const ORCHARDCORE_ITEM_KIND = "item";
|
|
28
|
+
/** The entity kind of a field marker naming a widget instance (`widget:`). */
|
|
29
|
+
export const ORCHARDCORE_WIDGET_KIND = "widget";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* On the inert coordinate script of a session's page: the one element a selector finds on a page whose markers are comments. The editor's frame
|
|
33
|
+
* of the proxied canvas is told by it that the page is editable.
|
|
34
|
+
*/
|
|
35
|
+
export const ORCHARDCORE_RENDER_ATTRIBUTE = "data-orchardcore-markers";
|
|
36
|
+
|
|
37
|
+
/** The attributes the vocabulary writes on the element a field comment pair stands for: elements are what the bridge reads. */
|
|
38
|
+
export const ORCHARDCORE_KEY_ATTRIBUTE = "data-orchardcore-key";
|
|
39
|
+
export const ORCHARDCORE_PATH_ATTRIBUTE = "data-orchardcore-path";
|
|
40
|
+
export const ORCHARDCORE_TYPE_ATTRIBUTE = "data-orchardcore-type";
|
|
41
|
+
export const ORCHARDCORE_KIND_ATTRIBUTE = "data-orchardcore-kind";
|
|
42
|
+
|
|
43
|
+
const OPEN = /^parhelia:([a-z]+)((?:\s+[A-Za-z]+="[^"]*")*)\s*$/;
|
|
44
|
+
const CLOSE = /^\/parhelia:([a-z]+)$/;
|
|
45
|
+
const ATTRIBUTE = /\s+([A-Za-z]+)="([^"]*)"/g;
|
|
46
|
+
const ENTITY = /&(?:lt|gt|amp|quot|#x([0-9A-Fa-f]+));/g;
|
|
47
|
+
/** @type {Record<string, string>} */
|
|
48
|
+
const NAMED = { "<": "<", ">": ">", "&": "&", """: '"' };
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A value as the marker wrote it, decoded: `< > & "`, `&#x…;` (.NET's `HtmlEncoder.Default`, which encodes `'` and every non-ASCII
|
|
52
|
+
* character numerically).
|
|
53
|
+
* @param {string} value
|
|
54
|
+
* @returns {string}
|
|
55
|
+
*/
|
|
56
|
+
export const decodeMarkerValue = value =>
|
|
57
|
+
value.replace(ENTITY, (/** @type {string} */ entity, /** @type {string | undefined} */ hex) =>
|
|
58
|
+
hex ? String.fromCodePoint(parseInt(hex, 16)) : (NAMED[entity] ?? entity));
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* One comment's marker, or undefined for a comment that is none of the grammar's (the theme's own, the module's `parhelia-sentinel`, a kind it does
|
|
62
|
+
* not know): `{ close: true, kind }` for a closing comment; else `{ close: false, kind, key?, id?, attributes }` with `attributes` decoded, in order,
|
|
63
|
+
* `key` and `id` lifted from them too.
|
|
64
|
+
* @param {string} data the comment's text
|
|
65
|
+
* @returns {import("./orchardcore.js").OrchardMarker | undefined}
|
|
66
|
+
*/
|
|
67
|
+
export const parseOrchardMarker = data => {
|
|
68
|
+
const text = typeof data === "string" ? data.trim() : "";
|
|
69
|
+
const closing = CLOSE.exec(text);
|
|
70
|
+
if (closing) {
|
|
71
|
+
const kind = closing[1] ?? "";
|
|
72
|
+
return ORCHARDCORE_MARKER_KINDS.includes(kind) ? { close: true, kind } : undefined;
|
|
73
|
+
}
|
|
74
|
+
const opening = OPEN.exec(text);
|
|
75
|
+
const kind = opening?.[1] ?? "";
|
|
76
|
+
if (!opening || !ORCHARDCORE_MARKER_KINDS.includes(kind)) return undefined;
|
|
77
|
+
/** @type {Record<string, string>} */
|
|
78
|
+
const attributes = {};
|
|
79
|
+
for (const [, name, value] of (opening[2] ?? "").matchAll(ATTRIBUTE)) if (name) attributes[name] = decodeMarkerValue(value ?? "");
|
|
80
|
+
return {
|
|
81
|
+
close: false,
|
|
82
|
+
kind,
|
|
83
|
+
...(attributes.key !== undefined ? { key: attributes.key } : {}),
|
|
84
|
+
...(attributes.id !== undefined ? { id: attributes.id } : {}),
|
|
85
|
+
attributes,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The entity a coordinate's key names, as the store's marker extension reads it: kind `widget` for a `widget:` key, kind `item` for `item:` and
|
|
91
|
+
* `set:`; undefined for anything else (a marker naming only an `id` names no key, and no store answers it).
|
|
92
|
+
* @param {string | undefined} key
|
|
93
|
+
* @returns {{ entityKind: string; entityKey: string } | undefined}
|
|
94
|
+
*/
|
|
95
|
+
export const orchardEntityOf = key => {
|
|
96
|
+
if (typeof key !== "string") return undefined;
|
|
97
|
+
if (/^widget:\S+$/.test(key)) return { entityKind: ORCHARDCORE_WIDGET_KIND, entityKey: key };
|
|
98
|
+
if (/^(?:item|set):\S+$/.test(key)) return { entityKind: ORCHARDCORE_ITEM_KIND, entityKey: key };
|
|
99
|
+
return undefined;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Every balanced marker pair below `root`, in document order of their opening comments. A pair whose two comments do not share a parent (the
|
|
104
|
+
* parser moved one, a template broke the shape's nesting) stands for no run of nodes and is left out, as is a stray closing comment.
|
|
105
|
+
* @param {Document | Element} root
|
|
106
|
+
* @returns {import("./orchardcore.js").OrchardMarkerPair[]}
|
|
107
|
+
*/
|
|
108
|
+
export const orchardMarkerPairs = root => {
|
|
109
|
+
const doc = root.ownerDocument ?? root;
|
|
110
|
+
const walker = doc.createTreeWalker(root, 128 /* NodeFilter.SHOW_COMMENT */);
|
|
111
|
+
/** @type {{ open: Comment; marker: import("./orchardcore.js").OrchardOpenMarker; slot: number }[]} */
|
|
112
|
+
const open = [];
|
|
113
|
+
/** @type {(import("./orchardcore.js").OrchardMarkerPair | null)[]} */
|
|
114
|
+
const pairs = [];
|
|
115
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
116
|
+
const comment = /** @type {Comment} */ (node);
|
|
117
|
+
const marker = parseOrchardMarker(comment.data);
|
|
118
|
+
if (!marker) continue;
|
|
119
|
+
if (!marker.close) {
|
|
120
|
+
open.push({ open: comment, marker, slot: pairs.length });
|
|
121
|
+
pairs.push(null);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let index = open.length - 1;
|
|
125
|
+
while (index >= 0 && open[index]?.marker.kind !== marker.kind) index--;
|
|
126
|
+
const opened = index < 0 ? undefined : open.splice(index, 1)[0];
|
|
127
|
+
if (opened && opened.open.parentNode === comment.parentNode) pairs[opened.slot] = { marker: opened.marker, open: opened.open, close: comment };
|
|
128
|
+
}
|
|
129
|
+
return /** @type {import("./orchardcore.js").OrchardMarkerPair[]} */ (pairs.filter(pair => pair !== null));
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The nodes between a pair's two comments that are content: elements and text with more than white space. Comments (nested markers, the module's
|
|
134
|
+
* sentinel) and white space say nothing about the shape's output.
|
|
135
|
+
* @param {import("./orchardcore.js").OrchardMarkerPair} pair
|
|
136
|
+
* @returns {ChildNode[]}
|
|
137
|
+
*/
|
|
138
|
+
export const orchardPairContent = pair => {
|
|
139
|
+
/** @type {ChildNode[]} */
|
|
140
|
+
const content = [];
|
|
141
|
+
for (let node = pair.open.nextSibling; node && node !== pair.close; node = node.nextSibling) {
|
|
142
|
+
if (node.nodeType === 1 || (node.nodeType === 3 && (node.nodeValue ?? "").trim())) content.push(node);
|
|
143
|
+
}
|
|
144
|
+
return content;
|
|
145
|
+
};
|