@silkui/core 0.1.3 → 0.1.4
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/README.md +11 -2
- package/package.json +1 -1
- package/src/index.d.ts +66 -1
- package/src/index.js +446 -5
package/README.md
CHANGED
|
@@ -61,11 +61,20 @@ mount(createBox({ position: "absolute", inset: 0, display: "flex", alignItems: "
|
|
|
61
61
|
| `createText(data?)` | A text node. It takes its look from its parent |
|
|
62
62
|
| `createInput(style?)` | An editable text field. `value`, `selectionStart`, `selectionEnd` and `setSelectionRange` work as on an `<input>` |
|
|
63
63
|
| `createRichInput(style?)` | A text field with marks and inline atoms (see below) |
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
64
|
+
| `createImage(src, style?)` | An image, as an `<img>`: `src` is a `data:` URL, a file or an `http(s):` address. An SVG file draws as an inline `<svg>` |
|
|
65
|
+
| `createIcon(svg, style?)` | SVG markup drawn at the node's size, with its `color` as `currentColor`. `icon.src` takes an SVG address instead |
|
|
66
|
+
| `createSvgElement(tag)` | `document.createElementNS` for SVG: an `Svg` root for `"svg"`, else an `SvgElement` (`path`, `g`, ...) to append inside one |
|
|
67
|
+
| `createNode(kind)` | A node of any kind: `"box"`, `"text"`, `"input"`, `"richinput"`, `"image"` (or `"img"`), `"icon"`, `"canvas"`, `"svg"` |
|
|
66
68
|
| `mount(node)` / `root()` | Makes a node the root of the window's tree, or returns it |
|
|
67
69
|
| `nodeById(id)` | The node with a tree id, for hosts and tools |
|
|
68
70
|
|
|
71
|
+
An `Svg` is one icon node whose markup its `SvgElement` and text children make, as an inline
|
|
72
|
+
`<svg>` is on a page. Class, look, events and ARIA attributes are the node's; `viewBox`,
|
|
73
|
+
`fill`, `width` and every other attribute are markup, and React's spellings (`strokeWidth`)
|
|
74
|
+
work too. Changes are drawn once, before the host applies the batch (`flushSvg()` draws them
|
|
75
|
+
now). Without a CSS size it takes the `<svg>`'s `width` and `height`, else a square of the
|
|
76
|
+
font size. Elements inside it hear no events: listen on the `Svg`.
|
|
77
|
+
|
|
69
78
|
A node has `append`, `appendChild`, `insertBefore`, `removeChild`, `remove`, `contains`,
|
|
70
79
|
`parentNode`, `childNodes`, `firstChild`, `lastChild` and the sibling getters, as in the DOM.
|
|
71
80
|
A removed node keeps its state and can be inserted again. `destroy()` frees a node and its
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -173,10 +173,66 @@ export class RichInput extends Input {
|
|
|
173
173
|
setSelectionRange(anchor: number, focus: number): void;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* An image, as an `<img>`: `src` is a `data:` URL, a file or `http(s):` address. An SVG
|
|
178
|
+
* file draws as an inline `<svg>` does. It hears `load` and `error`.
|
|
179
|
+
*/
|
|
180
|
+
export class Image extends Node {
|
|
181
|
+
constructor(src?: string);
|
|
182
|
+
src: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* An icon: SVG drawn at the node's CSS size, with its `color` as `currentColor`. Without a
|
|
187
|
+
* CSS size it takes the `<svg>`'s `width` and `height`, else a square of the font size.
|
|
188
|
+
* `svg` is markup and `src` the address of an SVG file; setting one clears the other.
|
|
189
|
+
*/
|
|
177
190
|
export class Icon extends Node {
|
|
178
191
|
constructor(svg?: string);
|
|
179
192
|
svg: string;
|
|
193
|
+
src: string;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Text inside an `<svg>`. */
|
|
197
|
+
export class SvgText {
|
|
198
|
+
constructor(data?: string);
|
|
199
|
+
data: string;
|
|
200
|
+
textContent: string;
|
|
201
|
+
readonly parentNode: SvgElement | Svg | null;
|
|
202
|
+
remove(): void;
|
|
203
|
+
destroy(): void;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** An element inside an `<svg>` (`path`, `g`, `circle`...): markup its root draws, no node. */
|
|
207
|
+
export class SvgElement {
|
|
208
|
+
constructor(tag: string);
|
|
209
|
+
readonly localName: string;
|
|
210
|
+
readonly tagName: string;
|
|
211
|
+
readonly namespaceURI: string;
|
|
212
|
+
readonly parentNode: SvgElement | Svg | null;
|
|
213
|
+
readonly childNodes: (SvgElement | SvgText | Text)[];
|
|
214
|
+
textContent: string;
|
|
215
|
+
getAttribute(name: string): string | null;
|
|
216
|
+
hasAttribute(name: string): boolean;
|
|
217
|
+
/** SVG's names, or React's spellings (`strokeWidth`, `xlinkHref`). */
|
|
218
|
+
setAttribute(name: string, value: unknown): void;
|
|
219
|
+
removeAttribute(name: string): void;
|
|
220
|
+
insertBefore<T extends SvgElement | SvgText | Text>(child: T, before?: SvgElement | SvgText | Text | null): T;
|
|
221
|
+
appendChild<T extends SvgElement | SvgText | Text>(child: T): T;
|
|
222
|
+
append(...children: (SvgElement | SvgText | Text | string)[]): void;
|
|
223
|
+
removeChild<T extends SvgElement | SvgText | Text>(child: T): T;
|
|
224
|
+
remove(): void;
|
|
225
|
+
destroy(): void;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* An inline `<svg>`: an icon node whose markup its `SvgElement` children make. Class, look,
|
|
230
|
+
* events and ARIA are the node's; `viewBox`, `fill`, `width` and the rest are markup.
|
|
231
|
+
*/
|
|
232
|
+
export class Svg extends Icon {
|
|
233
|
+
constructor();
|
|
234
|
+
readonly outerHTML: string;
|
|
235
|
+
textContent: string;
|
|
180
236
|
}
|
|
181
237
|
|
|
182
238
|
/** An event of the tree, with the members of the DOM event it corresponds to. */
|
|
@@ -308,6 +364,8 @@ export function createNode(kind: "text"): Text;
|
|
|
308
364
|
export function createNode(kind: "input"): Input;
|
|
309
365
|
export function createNode(kind: "richinput"): RichInput;
|
|
310
366
|
export function createNode(kind: "icon"): Icon;
|
|
367
|
+
export function createNode(kind: "image" | "img"): Image;
|
|
368
|
+
export function createNode(kind: "svg"): Svg;
|
|
311
369
|
export function createNode(kind: NodeKind): Node;
|
|
312
370
|
|
|
313
371
|
/** A box with `style` as its base look and `children` appended. */
|
|
@@ -317,6 +375,13 @@ export function createInput(style?: Style | null): Input;
|
|
|
317
375
|
export function createRichInput(style?: Style | null): RichInput;
|
|
318
376
|
/** An icon showing `svg` markup, with `style` as its base look. */
|
|
319
377
|
export function createIcon(svg: string, style?: Style | null): Icon;
|
|
378
|
+
/** An image showing `src`, with `style` as its base look. */
|
|
379
|
+
export function createImage(src: string, style?: Style | null): Image;
|
|
380
|
+
/** `document.createElementNS` in the SVG namespace: an `Svg` for `svg`, else an `SvgElement`. */
|
|
381
|
+
export function createSvgElement(tag: "svg"): Svg;
|
|
382
|
+
export function createSvgElement(tag: string): SvgElement;
|
|
383
|
+
/** Draws the `<svg>`s that changed now, rather than before the host applies the batch. */
|
|
384
|
+
export function flushSvg(): void;
|
|
320
385
|
|
|
321
386
|
/**
|
|
322
387
|
* Defines `@keyframes name`: `{ from: { opacity: 0 }, "50%": { ... }, to: { ... } }`.
|
package/src/index.js
CHANGED
|
@@ -523,6 +523,12 @@ export class Node {
|
|
|
523
523
|
this.#identify(text);
|
|
524
524
|
} else if (aria !== null) {
|
|
525
525
|
native.attribute(this.nodeId, name, text);
|
|
526
|
+
} else if (name === "src") {
|
|
527
|
+
// An image's or icon's address, as `<img src>`.
|
|
528
|
+
if ("src" in this) this.src = text ?? "";
|
|
529
|
+
else warnOnce("only an image or an icon has a src: it is ignored");
|
|
530
|
+
} else if (name === "alt") {
|
|
531
|
+
native.attribute(this.nodeId, "label", text ?? "");
|
|
526
532
|
} else {
|
|
527
533
|
if (name === "href") {
|
|
528
534
|
this._href = text || null;
|
|
@@ -630,6 +636,7 @@ export class Text extends Node {
|
|
|
630
636
|
if (value === this.#data && value !== "") return;
|
|
631
637
|
this.#data = value;
|
|
632
638
|
native.setText(this.nodeId, value);
|
|
639
|
+
if (this._svgText) svgChanged(this);
|
|
633
640
|
}
|
|
634
641
|
|
|
635
642
|
get textContent() {
|
|
@@ -701,16 +708,78 @@ function dropIcon(markup) {
|
|
|
701
708
|
}
|
|
702
709
|
}
|
|
703
710
|
|
|
711
|
+
// Addresses as image and icon resources, shared and freed as markup is. The tree keeps a load
|
|
712
|
+
// state per number, so these count from 1.
|
|
713
|
+
const ADDRESSES = new Map();
|
|
714
|
+
let nextAddress = 1;
|
|
715
|
+
|
|
716
|
+
function holdAddress(url, icon) {
|
|
717
|
+
const key = (icon ? "icon " : "image ") + url;
|
|
718
|
+
let entry = ADDRESSES.get(key);
|
|
719
|
+
if (!entry) {
|
|
720
|
+
entry = { id: nextAddress++, count: 0 };
|
|
721
|
+
ADDRESSES.set(key, entry);
|
|
722
|
+
native.load(entry.id, url, icon);
|
|
723
|
+
}
|
|
724
|
+
entry.count++;
|
|
725
|
+
return entry.id;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function dropAddress(url, icon) {
|
|
729
|
+
const key = (icon ? "icon " : "image ") + url;
|
|
730
|
+
const entry = ADDRESSES.get(key);
|
|
731
|
+
if (entry && --entry.count === 0) {
|
|
732
|
+
ADDRESSES.delete(key);
|
|
733
|
+
native.release(entry.id);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* An image, as an `<img>`: `src` is its address, a `data:` URL (base64 or percent-encoded),
|
|
739
|
+
* a file or `http(s):`. An SVG file draws as an inline `<svg>` does. It hears `load` and
|
|
740
|
+
* `error` when the address loads or fails.
|
|
741
|
+
*/
|
|
742
|
+
export class Image extends Node {
|
|
743
|
+
#src = "";
|
|
744
|
+
|
|
745
|
+
constructor(src = "") {
|
|
746
|
+
super("image");
|
|
747
|
+
this.src = src;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
get src() {
|
|
751
|
+
return this.#src;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
set src(url) {
|
|
755
|
+
url = url == null ? "" : String(url);
|
|
756
|
+
if (url === this.#src) return;
|
|
757
|
+
if (this.#src) dropAddress(this.#src, false);
|
|
758
|
+
this.#src = url;
|
|
759
|
+
native.source(this.nodeId, url ? holdAddress(url, false) : 0);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
_forget() {
|
|
763
|
+
if (this.#src) dropAddress(this.#src, false);
|
|
764
|
+
this.#src = "";
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
704
768
|
/**
|
|
705
769
|
* An icon: SVG drawn at the node's CSS size, as an inline `<svg>` is. Its `currentColor` is
|
|
706
770
|
* the node's `color`, so an icon that paints only with it (Lucide's, for example) takes the
|
|
707
|
-
* text color, and any other keeps its own colors.
|
|
771
|
+
* text color, and any other keeps its own colors. Without a CSS size it takes the `width`
|
|
772
|
+
* and `height` of its `<svg>`, else a square of the font size.
|
|
773
|
+
*
|
|
774
|
+
* `svg` is its markup; `src` is the address of an SVG file instead (a `data:image/svg+xml`
|
|
775
|
+
* URL too). Setting one clears the other.
|
|
708
776
|
*/
|
|
709
777
|
export class Icon extends Node {
|
|
710
778
|
#svg = "";
|
|
779
|
+
#src = "";
|
|
711
780
|
|
|
712
|
-
constructor(svg = "") {
|
|
713
|
-
super(
|
|
781
|
+
constructor(svg = "", kind = "icon") {
|
|
782
|
+
super(kind);
|
|
714
783
|
this.svg = svg;
|
|
715
784
|
}
|
|
716
785
|
|
|
@@ -722,15 +791,377 @@ export class Icon extends Node {
|
|
|
722
791
|
set svg(markup) {
|
|
723
792
|
markup = markup == null ? "" : String(markup);
|
|
724
793
|
if (markup === this.#svg) return;
|
|
725
|
-
|
|
794
|
+
this.#release();
|
|
726
795
|
this.#svg = markup;
|
|
727
796
|
native.source(this.nodeId, markup ? holdIcon(markup) : 0);
|
|
728
797
|
}
|
|
729
798
|
|
|
730
|
-
|
|
799
|
+
get src() {
|
|
800
|
+
return this.#src;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
set src(url) {
|
|
804
|
+
url = url == null ? "" : String(url);
|
|
805
|
+
if (url === this.#src) return;
|
|
806
|
+
this.#release();
|
|
807
|
+
this.#src = url;
|
|
808
|
+
native.source(this.nodeId, url ? holdAddress(url, true) : 0);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
#release() {
|
|
731
812
|
if (this.#svg) dropIcon(this.#svg);
|
|
813
|
+
if (this.#src) dropAddress(this.#src, true);
|
|
732
814
|
this.#svg = "";
|
|
815
|
+
this.#src = "";
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
_forget() {
|
|
819
|
+
this.#release();
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
824
|
+
|
|
825
|
+
// SVG attribute names that are camel case in SVG itself. Any other name with a capital letter
|
|
826
|
+
// is a React-style spelling of a hyphenated one (`strokeWidth` for `stroke-width`).
|
|
827
|
+
const SVG_CAMEL = new Set([
|
|
828
|
+
"viewBox", "preserveAspectRatio", "gradientUnits", "gradientTransform", "patternUnits",
|
|
829
|
+
"patternContentUnits", "patternTransform", "clipPathUnits", "maskUnits", "maskContentUnits",
|
|
830
|
+
"markerUnits", "markerWidth", "markerHeight", "refX", "refY", "spreadMethod", "stdDeviation",
|
|
831
|
+
"textLength", "lengthAdjust", "startOffset", "pathLength", "baseFrequency", "numOctaves",
|
|
832
|
+
"filterUnits", "primitiveUnits", "kernelMatrix", "kernelUnitLength", "tableValues",
|
|
833
|
+
"surfaceScale", "specularConstant", "specularExponent", "diffuseConstant", "limitingConeAngle",
|
|
834
|
+
"pointsAtX", "pointsAtY", "pointsAtZ", "edgeMode", "targetX", "targetY", "xChannelSelector",
|
|
835
|
+
"yChannelSelector", "attributeName", "attributeType", "calcMode", "keyTimes", "keySplines",
|
|
836
|
+
"keyPoints", "repeatCount", "repeatDur", "systemLanguage", "requiredFeatures", "zoomAndPan",
|
|
837
|
+
"glyphRef", "stitchTiles", "viewTarget", "contentScriptType", "contentStyleType",
|
|
838
|
+
]);
|
|
839
|
+
const SVG_NAMES = new Map([
|
|
840
|
+
["className", "class"], ["htmlFor", "for"], ["xlinkHref", "xlink:href"], ["xlinkTitle", "xlink:title"],
|
|
841
|
+
["xmlSpace", "xml:space"], ["xmlLang", "xml:lang"], ["xmlnsXlink", "xmlns:xlink"],
|
|
842
|
+
]);
|
|
843
|
+
|
|
844
|
+
function svgName(name) {
|
|
845
|
+
name = String(name);
|
|
846
|
+
const named = SVG_NAMES.get(name);
|
|
847
|
+
if (named) return named;
|
|
848
|
+
if (SVG_CAMEL.has(name) || !/[A-Z]/.test(name)) return name;
|
|
849
|
+
return name.replace(/[A-Z]/g, (letter) => "-" + letter.toLowerCase());
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function escaped(text, attribute) {
|
|
853
|
+
text = String(text).replace(/&/g, "&").replace(/</g, "<");
|
|
854
|
+
return attribute ? text.replace(/"/g, """) : text;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// The svg roots whose markup changed, drawn again once, before the host applies the mutations.
|
|
858
|
+
const changedSvgs = new Set();
|
|
859
|
+
|
|
860
|
+
function drawSvgs() {
|
|
861
|
+
for (const svg of changedSvgs) svg._draw();
|
|
862
|
+
changedSvgs.clear();
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** The element of `node` in an `<svg>` subtree: its text, or its element. */
|
|
866
|
+
function svgOwner(node) {
|
|
867
|
+
for (let at = node; at; at = at.parentNode) if (at instanceof Svg) return at;
|
|
868
|
+
return null;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function svgChanged(node) {
|
|
872
|
+
const svg = svgOwner(node);
|
|
873
|
+
if (!svg || changedSvgs.has(svg)) return;
|
|
874
|
+
if (changedSvgs.size === 0) queueMicrotask(drawSvgs);
|
|
875
|
+
changedSvgs.add(svg);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function svgStyle(value) {
|
|
879
|
+
if (typeof value !== "object" || value === null) return String(value);
|
|
880
|
+
let css = "";
|
|
881
|
+
for (const name of Object.keys(value)) {
|
|
882
|
+
const part = value[name];
|
|
883
|
+
if (part === null || part === undefined || part === "") continue;
|
|
884
|
+
css += cssName(name) + ":" + (typeof part === "number" && !UNITLESS.has(name) ? part + "px" : part) + ";";
|
|
885
|
+
}
|
|
886
|
+
return css;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// The children of an element of an `<svg>`: the markup they write, and how they move.
|
|
890
|
+
const svgChildren = {
|
|
891
|
+
insertBefore(child, before = null) {
|
|
892
|
+
if (before && before.parentNode !== this) throw new Error("the node to insert before is not a child of this node");
|
|
893
|
+
if (before === child) return child;
|
|
894
|
+
for (let at = this; at; at = at.parentNode) if (at === child) throw new Error("a node cannot be inserted into itself or its own subtree");
|
|
895
|
+
const old = child.parentNode;
|
|
896
|
+
if (old) {
|
|
897
|
+
svgChanged(old);
|
|
898
|
+
old.childNodes.splice(old.childNodes.indexOf(child), 1);
|
|
899
|
+
}
|
|
900
|
+
child.parentNode = this;
|
|
901
|
+
if (before) this.childNodes.splice(this.childNodes.indexOf(before), 0, child);
|
|
902
|
+
else this.childNodes.push(child);
|
|
903
|
+
if (child instanceof Text) child._svgText = true;
|
|
904
|
+
svgChanged(this);
|
|
905
|
+
return child;
|
|
906
|
+
},
|
|
907
|
+
appendChild(child) {
|
|
908
|
+
return this.insertBefore(child, null);
|
|
909
|
+
},
|
|
910
|
+
append(...children) {
|
|
911
|
+
for (const child of children) this.insertBefore(typeof child === "string" ? new SvgText(child) : child, null);
|
|
912
|
+
},
|
|
913
|
+
removeChild(child) {
|
|
914
|
+
if (child.parentNode !== this) throw new Error("the node to remove is not a child of this node");
|
|
915
|
+
svgChanged(this);
|
|
916
|
+
this.childNodes.splice(this.childNodes.indexOf(child), 1);
|
|
917
|
+
child.parentNode = null;
|
|
918
|
+
return child;
|
|
919
|
+
},
|
|
920
|
+
get textContent() {
|
|
921
|
+
let text = "";
|
|
922
|
+
for (const child of this.childNodes) text += child.textContent ?? child.data ?? "";
|
|
923
|
+
return text;
|
|
924
|
+
},
|
|
925
|
+
set textContent(value) {
|
|
926
|
+
for (const child of this.childNodes.slice()) this.removeChild(child);
|
|
927
|
+
if (value !== null && value !== undefined && value !== "") this.insertBefore(new SvgText(String(value)), null);
|
|
928
|
+
},
|
|
929
|
+
_markupOf(children) {
|
|
930
|
+
let markup = "";
|
|
931
|
+
for (const child of children) {
|
|
932
|
+
if (child instanceof SvgElement) markup += child._markup();
|
|
933
|
+
else markup += escaped(child.data ?? "", false);
|
|
934
|
+
}
|
|
935
|
+
return markup;
|
|
936
|
+
},
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
/** Text in an `<svg>`: its `<text>` and `<title>` contents. */
|
|
940
|
+
export class SvgText {
|
|
941
|
+
#data;
|
|
942
|
+
|
|
943
|
+
constructor(data = "") {
|
|
944
|
+
this.#data = String(data);
|
|
945
|
+
this.parentNode = null;
|
|
946
|
+
this.childNodes = NO_CHILDREN;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
get data() {
|
|
950
|
+
return this.#data;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
set data(value) {
|
|
954
|
+
value = String(value);
|
|
955
|
+
if (value === this.#data) return;
|
|
956
|
+
this.#data = value;
|
|
957
|
+
svgChanged(this);
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
get textContent() {
|
|
961
|
+
return this.#data;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
set textContent(value) {
|
|
965
|
+
this.data = value;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
get nextSibling() {
|
|
969
|
+
const siblings = this.parentNode?.childNodes;
|
|
970
|
+
return siblings ? siblings[siblings.indexOf(this) + 1] ?? null : null;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
get previousSibling() {
|
|
974
|
+
const siblings = this.parentNode?.childNodes;
|
|
975
|
+
return siblings ? siblings[siblings.indexOf(this) - 1] ?? null : null;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
contains(other) {
|
|
979
|
+
for (let node = other; node; node = node.parentNode) if (node === this) return true;
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
remove() {
|
|
984
|
+
this.parentNode?.removeChild(this);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
destroy() {
|
|
988
|
+
this.remove();
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* An element inside an `<svg>`: `path`, `circle`, `g`, `linearGradient` and the rest. It is
|
|
994
|
+
* no node of the tree: it is markup its `Svg` root draws. Its attributes take SVG's names or
|
|
995
|
+
* React's spellings (`strokeWidth`, `xlinkHref`).
|
|
996
|
+
*/
|
|
997
|
+
export class SvgElement {
|
|
998
|
+
#attributes = new Map();
|
|
999
|
+
|
|
1000
|
+
constructor(tag) {
|
|
1001
|
+
this.localName = String(tag);
|
|
1002
|
+
this.tagName = this.localName;
|
|
1003
|
+
this.namespaceURI = SVG_NAMESPACE;
|
|
1004
|
+
this.parentNode = null;
|
|
1005
|
+
this.childNodes = [];
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
getAttribute(name) {
|
|
1009
|
+
return this.#attributes.get(svgName(name)) ?? null;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
hasAttribute(name) {
|
|
1013
|
+
return this.#attributes.has(svgName(name));
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
setAttribute(name, value) {
|
|
1017
|
+
name = svgName(name);
|
|
1018
|
+
if (value === null || value === undefined || value === false) {
|
|
1019
|
+
this.removeAttribute(name);
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
const text = name === "style" ? svgStyle(value) : String(value);
|
|
1023
|
+
if (this.#attributes.get(name) === text) return;
|
|
1024
|
+
this.#attributes.set(name, text);
|
|
1025
|
+
svgChanged(this);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
removeAttribute(name) {
|
|
1029
|
+
if (this.#attributes.delete(svgName(name))) svgChanged(this);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
get firstChild() {
|
|
1033
|
+
return this.childNodes[0] ?? null;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
get lastChild() {
|
|
1037
|
+
return this.childNodes[this.childNodes.length - 1] ?? null;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
get nextSibling() {
|
|
1041
|
+
const siblings = this.parentNode?.childNodes;
|
|
1042
|
+
return siblings ? siblings[siblings.indexOf(this) + 1] ?? null : null;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
get previousSibling() {
|
|
1046
|
+
const siblings = this.parentNode?.childNodes;
|
|
1047
|
+
return siblings ? siblings[siblings.indexOf(this) - 1] ?? null : null;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
contains(other) {
|
|
1051
|
+
for (let node = other; node; node = node.parentNode) if (node === this) return true;
|
|
1052
|
+
return false;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
remove() {
|
|
1056
|
+
this.parentNode?.removeChild(this);
|
|
733
1057
|
}
|
|
1058
|
+
|
|
1059
|
+
/** Takes the element out of its `<svg>`. It is plain markup, so nothing else is freed. */
|
|
1060
|
+
destroy() {
|
|
1061
|
+
this.remove();
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
addEventListener(type) {
|
|
1065
|
+
warnOnce("an element inside an <svg> hears no events: listen on the <svg> ('" + type + "')");
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
removeEventListener() {}
|
|
1069
|
+
|
|
1070
|
+
_markup() {
|
|
1071
|
+
let markup = "<" + this.localName;
|
|
1072
|
+
for (const [name, value] of this.#attributes) markup += " " + name + '="' + escaped(value, true) + '"';
|
|
1073
|
+
if (this.childNodes.length === 0) return markup + "/>";
|
|
1074
|
+
return markup + ">" + svgChildren._markupOf(this.childNodes) + "</" + this.localName + ">";
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
for (const name of ["insertBefore", "appendChild", "append", "removeChild"]) SvgElement.prototype[name] = svgChildren[name];
|
|
1079
|
+
Object.defineProperty(SvgElement.prototype, "textContent", Object.getOwnPropertyDescriptor(svgChildren, "textContent"));
|
|
1080
|
+
|
|
1081
|
+
// Attributes of an `<svg>` root the tree has: they go to its node. Every other one is markup.
|
|
1082
|
+
function treeAttribute(name) {
|
|
1083
|
+
return name === "class" || name === "className" || name === "id" || name === "role" || name === "tabIndex"
|
|
1084
|
+
|| name === "tabindex" || name === "testId" || name === "label" || name === "cursor" || name === "transition"
|
|
1085
|
+
|| name === "inert" || name === "disabled" || name === "element" || name === "lang" || name === "dir"
|
|
1086
|
+
|| name.startsWith("data-") || ariaName(name) !== null;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* An inline `<svg>`, as the web has it: its children are `SvgElement`s and `SvgText`s, and
|
|
1091
|
+
* the tree draws the markup they make as an icon. Its look, class, events and accessibility
|
|
1092
|
+
* are the node's; `viewBox`, `fill`, `width` and the other SVG attributes are markup.
|
|
1093
|
+
* Changes are drawn once, before the host applies the batch.
|
|
1094
|
+
*/
|
|
1095
|
+
export class Svg extends Icon {
|
|
1096
|
+
#attributes = new Map();
|
|
1097
|
+
|
|
1098
|
+
constructor() {
|
|
1099
|
+
super("", "icon");
|
|
1100
|
+
this.localName = "svg";
|
|
1101
|
+
this.tagName = "svg";
|
|
1102
|
+
this.namespaceURI = SVG_NAMESPACE;
|
|
1103
|
+
svgChanged(this);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
getAttribute(name) {
|
|
1107
|
+
return treeAttribute(String(name)) ? super.getAttribute(name) : this.#attributes.get(svgName(name)) ?? null;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
setAttribute(name, value) {
|
|
1111
|
+
name = String(name);
|
|
1112
|
+
if (treeAttribute(name)) {
|
|
1113
|
+
super.setAttribute(name === "tabindex" ? "tabIndex" : name, value);
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
name = svgName(name);
|
|
1117
|
+
if (name === "xmlns") return;
|
|
1118
|
+
if (value === null || value === undefined || value === false || value === "") {
|
|
1119
|
+
this.removeAttribute(name);
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
const text = name === "style" ? svgStyle(value) : String(value);
|
|
1123
|
+
if (this.#attributes.get(name) === text) return;
|
|
1124
|
+
this.#attributes.set(name, text);
|
|
1125
|
+
svgChanged(this);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
removeAttribute(name) {
|
|
1129
|
+
name = String(name);
|
|
1130
|
+
if (treeAttribute(name)) super.removeAttribute(name);
|
|
1131
|
+
else if (this.#attributes.delete(svgName(name))) svgChanged(this);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/** The markup the tree draws, as `outerHTML` gives it. */
|
|
1135
|
+
get outerHTML() {
|
|
1136
|
+
let markup = '<svg xmlns="' + SVG_NAMESPACE + '"';
|
|
1137
|
+
const children = svgChildren._markupOf(this.childNodes);
|
|
1138
|
+
let xlink = this.#attributes.has("xmlns:xlink");
|
|
1139
|
+
for (const [name, value] of this.#attributes) {
|
|
1140
|
+
markup += " " + name + '="' + escaped(value, true) + '"';
|
|
1141
|
+
}
|
|
1142
|
+
if (!xlink && children.includes("xlink:")) markup += ' xmlns:xlink="http://www.w3.org/1999/xlink"';
|
|
1143
|
+
return markup + ">" + children + "</svg>";
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
_draw() {
|
|
1147
|
+
if (nodes.has(this.nodeId)) this.svg = this.outerHTML;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
for (const name of ["insertBefore", "appendChild", "append", "removeChild"]) Svg.prototype[name] = svgChildren[name];
|
|
1152
|
+
Object.defineProperty(Svg.prototype, "textContent", Object.getOwnPropertyDescriptor(svgChildren, "textContent"));
|
|
1153
|
+
|
|
1154
|
+
/**
|
|
1155
|
+
* An element of tag `tag` in the SVG namespace: an `Svg` root for `svg`, an `SvgElement`
|
|
1156
|
+
* inside one for anything else. `document.createElementNS` with the SVG namespace, as it were.
|
|
1157
|
+
*/
|
|
1158
|
+
export function createSvgElement(tag) {
|
|
1159
|
+
return tag === "svg" ? new Svg() : new SvgElement(tag);
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/** Draws the `<svg>` roots that changed now, instead of before the host applies the batch. */
|
|
1163
|
+
export function flushSvg() {
|
|
1164
|
+
drawSvgs();
|
|
734
1165
|
}
|
|
735
1166
|
|
|
736
1167
|
// Offsets cross the bridge as UTF-8 bytes; a rich input speaks JavaScript string indices.
|
|
@@ -931,10 +1362,13 @@ function forget(node) {
|
|
|
931
1362
|
|
|
932
1363
|
export function createNode(kind) {
|
|
933
1364
|
if (kind === "richinput") return new RichInput();
|
|
1365
|
+
if (kind === "svg") return new Svg();
|
|
1366
|
+
if (kind === "img") kind = "image";
|
|
934
1367
|
if (!(kind in KINDS)) throw new TypeError("the tree has no node kind '" + kind + "'");
|
|
935
1368
|
if (kind === "text") return new Text();
|
|
936
1369
|
if (kind === "input") return new Input();
|
|
937
1370
|
if (kind === "icon") return new Icon();
|
|
1371
|
+
if (kind === "image") return new Image();
|
|
938
1372
|
return new Node(kind);
|
|
939
1373
|
}
|
|
940
1374
|
|
|
@@ -957,6 +1391,13 @@ export function createIcon(svg, style) {
|
|
|
957
1391
|
return icon;
|
|
958
1392
|
}
|
|
959
1393
|
|
|
1394
|
+
/** An image showing `src` (see `Image`), with `style` as its base look. */
|
|
1395
|
+
export function createImage(src, style) {
|
|
1396
|
+
const image = new Image(src);
|
|
1397
|
+
if (style) image.style.set(style);
|
|
1398
|
+
return image;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
960
1401
|
export function createInput(style) {
|
|
961
1402
|
const input = new Input();
|
|
962
1403
|
if (style) input.style.set(style);
|