@silkui/core 0.1.3 → 0.1.5

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  The node and event API of a [Silk UI](https://github.com/silk-lang/SilkUI) tree, for
4
4
  JavaScript apps that run on the SilkJS engine embedded in a Silk UI program. Its nodes are
5
- the tree's own (box, text, input, rich input, image, icon, canvas). Its methods and its
5
+ the tree's own (box, text, input, rich input, image, canvas) and inline svg. Its methods and its
6
6
  event flow are the DOM's: `appendChild`, `addEventListener`, capture and bubble,
7
7
  `preventDefault`.
8
8
 
@@ -61,11 +61,19 @@ 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
- | `createIcon(svg, style?)` | SVG markup drawn at the node's size, with its `color` as `currentColor` |
65
- | `createNode(kind)` | A node of any kind: `"box"`, `"text"`, `"input"`, `"richinput"`, `"image"`, `"icon"`, `"canvas"` |
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
+ | `createSvgElement(tag)` | `document.createElementNS` for SVG: an `Svg` root for `"svg"`, else an `SvgElement` (`path`, `g`, ...) to append inside one |
66
+ | `createNode(kind)` | A node of any kind: `"box"`, `"text"`, `"input"`, `"richinput"`, `"image"` (or `"img"`), `"canvas"`, `"svg"` |
66
67
  | `mount(node)` / `root()` | Makes a node the root of the window's tree, or returns it |
67
68
  | `nodeById(id)` | The node with a tree id, for hosts and tools |
68
69
 
70
+ An `Svg` is one node whose markup its `SvgElement` and text children make, as an inline
71
+ `<svg>` is on a page. Class, look, events and ARIA attributes are the node's; `viewBox`,
72
+ `fill`, `width` and every other attribute are markup, and React's spellings (`strokeWidth`)
73
+ work too. Changes are drawn once, before the host applies the batch (`flushSvg()` draws them
74
+ now). Without a CSS size it takes the `<svg>`'s `width` and `height`, else a square of the
75
+ font size. Elements inside it hear no events: listen on the `Svg`.
76
+
69
77
  A node has `append`, `appendChild`, `insertBefore`, `removeChild`, `remove`, `contains`,
70
78
  `parentNode`, `childNodes`, `firstChild`, `lastChild` and the sibling getters, as in the DOM.
71
79
  A removed node keeps its state and can be inserted again. `destroy()` frees a node and its
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silkui/core",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "The node and event API of a Silk UI tree for JavaScript applications that run on the embedded SilkJS engine",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.d.ts CHANGED
@@ -11,7 +11,7 @@ export function updateStyleSheet(id: string, compiled: Uint8Array | ArrayBuffer
11
11
  import type * as CSS from "csstype";
12
12
 
13
13
  /** The node kinds of the tree. `richinput` is an `input` with marks and atoms. */
14
- export type NodeKind = "box" | "text" | "input" | "richinput" | "image" | "icon" | "canvas";
14
+ export type NodeKind = "box" | "text" | "input" | "richinput" | "image" | "canvas" | "svg";
15
15
 
16
16
  /** The looks a state lays over the base look: `:hover`, `:active`, `:focus`, `:focus-visible`, `@starting-style`. */
17
17
  export type LookState = "base" | "hover" | "active" | "focus" | "focusVisible" | "starting";
@@ -173,10 +173,57 @@ export class RichInput extends Input {
173
173
  setSelectionRange(anchor: number, focus: number): void;
174
174
  }
175
175
 
176
- /** An icon: SVG markup drawn at the node's CSS size, with its `color` as `currentColor`. */
177
- export class Icon extends Node {
178
- constructor(svg?: string);
179
- svg: string;
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
+ /** Text inside an `<svg>`. */
186
+ export class SvgText {
187
+ constructor(data?: string);
188
+ data: string;
189
+ textContent: string;
190
+ readonly parentNode: SvgElement | Svg | null;
191
+ remove(): void;
192
+ destroy(): void;
193
+ }
194
+
195
+ /** An element inside an `<svg>` (`path`, `g`, `circle`...): markup its root draws, no node. */
196
+ export class SvgElement {
197
+ constructor(tag: string);
198
+ readonly localName: string;
199
+ readonly tagName: string;
200
+ readonly namespaceURI: string;
201
+ readonly parentNode: SvgElement | Svg | null;
202
+ readonly childNodes: (SvgElement | SvgText | Text)[];
203
+ textContent: string;
204
+ getAttribute(name: string): string | null;
205
+ hasAttribute(name: string): boolean;
206
+ /** SVG's names, or React's spellings (`strokeWidth`, `xlinkHref`). */
207
+ setAttribute(name: string, value: unknown): void;
208
+ removeAttribute(name: string): void;
209
+ insertBefore<T extends SvgElement | SvgText | Text>(child: T, before?: SvgElement | SvgText | Text | null): T;
210
+ appendChild<T extends SvgElement | SvgText | Text>(child: T): T;
211
+ append(...children: (SvgElement | SvgText | Text | string)[]): void;
212
+ removeChild<T extends SvgElement | SvgText | Text>(child: T): T;
213
+ remove(): void;
214
+ destroy(): void;
215
+ }
216
+
217
+ /**
218
+ * An inline `<svg>`: one node drawing the markup its `SvgElement` children make, at its CSS
219
+ * size (else its `width` and `height`, else a square of the font size), with its `color` as
220
+ * `currentColor`. Class, look, events and ARIA are the node's; `viewBox`, `fill`, `width`
221
+ * and the rest are markup.
222
+ */
223
+ export class Svg extends Node {
224
+ constructor();
225
+ readonly outerHTML: string;
226
+ textContent: string;
180
227
  }
181
228
 
182
229
  /** An event of the tree, with the members of the DOM event it corresponds to. */
@@ -307,7 +354,8 @@ export interface EventMap {
307
354
  export function createNode(kind: "text"): Text;
308
355
  export function createNode(kind: "input"): Input;
309
356
  export function createNode(kind: "richinput"): RichInput;
310
- export function createNode(kind: "icon"): Icon;
357
+ export function createNode(kind: "image" | "img"): Image;
358
+ export function createNode(kind: "svg"): Svg;
311
359
  export function createNode(kind: NodeKind): Node;
312
360
 
313
361
  /** A box with `style` as its base look and `children` appended. */
@@ -315,8 +363,13 @@ export function createBox(style?: Style | null, ...children: (Node | string)[]):
315
363
  export function createText(data?: string): Text;
316
364
  export function createInput(style?: Style | null): Input;
317
365
  export function createRichInput(style?: Style | null): RichInput;
318
- /** An icon showing `svg` markup, with `style` as its base look. */
319
- export function createIcon(svg: string, style?: Style | null): Icon;
366
+ /** An image showing `src`, with `style` as its base look. */
367
+ export function createImage(src: string, style?: Style | null): Image;
368
+ /** `document.createElementNS` in the SVG namespace: an `Svg` for `svg`, else an `SvgElement`. */
369
+ export function createSvgElement(tag: "svg"): Svg;
370
+ export function createSvgElement(tag: string): SvgElement;
371
+ /** Draws the `<svg>`s that changed now, rather than before the host applies the batch. */
372
+ export function flushSvg(): void;
320
373
 
321
374
  /**
322
375
  * Defines `@keyframes name`: `{ from: { opacity: 0 }, "50%": { ... }, to: { ... } }`.
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // @silkui/core: the nodes of a Silk UI tree and their events, for JavaScript that runs on the
2
- // embedded SilkJS engine. The primitives are the tree's own (box, text, input, image, icon,
3
- // canvas); the methods and the event flow are the DOM's, so code written for the web reads
2
+ // embedded SilkJS engine. The primitives are the tree's own (box, text, input, image, canvas)
3
+ // and inline svg; the methods and the event flow are the DOM's, so code written for the web reads
4
4
  // the same here.
5
5
  //
6
6
  // Every call queues a mutation through the native `__ui` functions. The host applies the
@@ -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 address, as `<img src>`.
528
+ if ("src" in this) this.src = text ?? "";
529
+ else warnOnce("only an image 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() {
@@ -677,8 +684,8 @@ export class Input extends Node {
677
684
  }
678
685
  }
679
686
 
680
- // Inline SVG markup as icon resources: identical markup shares one, freed when no icon shows
681
- // it. The ids start high, clear of resources a host or a URL table numbers from 1.
687
+ // Inline SVG markup as icon resources: identical markup shares one, freed when no `<svg>`
688
+ // shows it. The ids start high, clear of resources a host or a URL table numbers from 1.
682
689
  const ICONS = new Map();
683
690
  let nextIcon = 0x40000000;
684
691
 
@@ -701,38 +708,418 @@ function dropIcon(markup) {
701
708
  }
702
709
  }
703
710
 
711
+ // Addresses as image resources, shared and freed as markup is. The tree keeps a load state per
712
+ // number, so these count from 1. The loader tells an SVG file from a bitmap by itself.
713
+ const ADDRESSES = new Map();
714
+ let nextAddress = 1;
715
+
716
+ function holdAddress(url) {
717
+ let entry = ADDRESSES.get(url);
718
+ if (!entry) {
719
+ entry = { id: nextAddress++, count: 0 };
720
+ ADDRESSES.set(url, entry);
721
+ native.load(entry.id, url, false);
722
+ }
723
+ entry.count++;
724
+ return entry.id;
725
+ }
726
+
727
+ function dropAddress(url) {
728
+ const entry = ADDRESSES.get(url);
729
+ if (entry && --entry.count === 0) {
730
+ ADDRESSES.delete(url);
731
+ native.release(entry.id);
732
+ }
733
+ }
734
+
735
+ /**
736
+ * An image, as an `<img>`: `src` is its address, a `data:` URL (base64 or percent-encoded),
737
+ * a file or `http(s):`. An SVG file draws as an inline `<svg>` does. It hears `load` and
738
+ * `error` when the address loads or fails.
739
+ */
740
+ export class Image extends Node {
741
+ #src = "";
742
+
743
+ constructor(src = "") {
744
+ super("image");
745
+ this.src = src;
746
+ }
747
+
748
+ get src() {
749
+ return this.#src;
750
+ }
751
+
752
+ set src(url) {
753
+ url = url == null ? "" : String(url);
754
+ if (url === this.#src) return;
755
+ if (this.#src) dropAddress(this.#src);
756
+ this.#src = url;
757
+ native.source(this.nodeId, url ? holdAddress(url) : 0);
758
+ }
759
+
760
+ _forget() {
761
+ if (this.#src) dropAddress(this.#src);
762
+ this.#src = "";
763
+ }
764
+ }
765
+
766
+ const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
767
+
768
+ // SVG attribute names that are camel case in SVG itself. Any other name with a capital letter
769
+ // is a React-style spelling of a hyphenated one (`strokeWidth` for `stroke-width`).
770
+ const SVG_CAMEL = new Set([
771
+ "viewBox", "preserveAspectRatio", "gradientUnits", "gradientTransform", "patternUnits",
772
+ "patternContentUnits", "patternTransform", "clipPathUnits", "maskUnits", "maskContentUnits",
773
+ "markerUnits", "markerWidth", "markerHeight", "refX", "refY", "spreadMethod", "stdDeviation",
774
+ "textLength", "lengthAdjust", "startOffset", "pathLength", "baseFrequency", "numOctaves",
775
+ "filterUnits", "primitiveUnits", "kernelMatrix", "kernelUnitLength", "tableValues",
776
+ "surfaceScale", "specularConstant", "specularExponent", "diffuseConstant", "limitingConeAngle",
777
+ "pointsAtX", "pointsAtY", "pointsAtZ", "edgeMode", "targetX", "targetY", "xChannelSelector",
778
+ "yChannelSelector", "attributeName", "attributeType", "calcMode", "keyTimes", "keySplines",
779
+ "keyPoints", "repeatCount", "repeatDur", "systemLanguage", "requiredFeatures", "zoomAndPan",
780
+ "glyphRef", "stitchTiles", "viewTarget", "contentScriptType", "contentStyleType",
781
+ ]);
782
+ const SVG_NAMES = new Map([
783
+ ["className", "class"], ["htmlFor", "for"], ["xlinkHref", "xlink:href"], ["xlinkTitle", "xlink:title"],
784
+ ["xmlSpace", "xml:space"], ["xmlLang", "xml:lang"], ["xmlnsXlink", "xmlns:xlink"],
785
+ ]);
786
+
787
+ function svgName(name) {
788
+ name = String(name);
789
+ const named = SVG_NAMES.get(name);
790
+ if (named) return named;
791
+ if (SVG_CAMEL.has(name) || !/[A-Z]/.test(name)) return name;
792
+ return name.replace(/[A-Z]/g, (letter) => "-" + letter.toLowerCase());
793
+ }
794
+
795
+ function escaped(text, attribute) {
796
+ text = String(text).replace(/&/g, "&amp;").replace(/</g, "&lt;");
797
+ return attribute ? text.replace(/"/g, "&quot;") : text;
798
+ }
799
+
800
+ // The svg roots whose markup changed, drawn again once, before the host applies the mutations.
801
+ const changedSvgs = new Set();
802
+
803
+ function drawSvgs() {
804
+ for (const svg of changedSvgs) svg._draw();
805
+ changedSvgs.clear();
806
+ }
807
+
808
+ /** The element of `node` in an `<svg>` subtree: its text, or its element. */
809
+ function svgOwner(node) {
810
+ for (let at = node; at; at = at.parentNode) if (at instanceof Svg) return at;
811
+ return null;
812
+ }
813
+
814
+ function svgChanged(node) {
815
+ const svg = svgOwner(node);
816
+ if (!svg || changedSvgs.has(svg)) return;
817
+ if (changedSvgs.size === 0) queueMicrotask(drawSvgs);
818
+ changedSvgs.add(svg);
819
+ }
820
+
821
+ function svgStyle(value) {
822
+ if (typeof value !== "object" || value === null) return String(value);
823
+ let css = "";
824
+ for (const name of Object.keys(value)) {
825
+ const part = value[name];
826
+ if (part === null || part === undefined || part === "") continue;
827
+ css += cssName(name) + ":" + (typeof part === "number" && !UNITLESS.has(name) ? part + "px" : part) + ";";
828
+ }
829
+ return css;
830
+ }
831
+
832
+ // The children of an element of an `<svg>`: the markup they write, and how they move.
833
+ const svgChildren = {
834
+ insertBefore(child, before = null) {
835
+ if (before && before.parentNode !== this) throw new Error("the node to insert before is not a child of this node");
836
+ if (before === child) return child;
837
+ 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");
838
+ const old = child.parentNode;
839
+ if (old) {
840
+ svgChanged(old);
841
+ old.childNodes.splice(old.childNodes.indexOf(child), 1);
842
+ }
843
+ child.parentNode = this;
844
+ if (before) this.childNodes.splice(this.childNodes.indexOf(before), 0, child);
845
+ else this.childNodes.push(child);
846
+ if (child instanceof Text) child._svgText = true;
847
+ svgChanged(this);
848
+ return child;
849
+ },
850
+ appendChild(child) {
851
+ return this.insertBefore(child, null);
852
+ },
853
+ append(...children) {
854
+ for (const child of children) this.insertBefore(typeof child === "string" ? new SvgText(child) : child, null);
855
+ },
856
+ removeChild(child) {
857
+ if (child.parentNode !== this) throw new Error("the node to remove is not a child of this node");
858
+ svgChanged(this);
859
+ this.childNodes.splice(this.childNodes.indexOf(child), 1);
860
+ child.parentNode = null;
861
+ return child;
862
+ },
863
+ get textContent() {
864
+ let text = "";
865
+ for (const child of this.childNodes) text += child.textContent ?? child.data ?? "";
866
+ return text;
867
+ },
868
+ set textContent(value) {
869
+ for (const child of this.childNodes.slice()) this.removeChild(child);
870
+ if (value !== null && value !== undefined && value !== "") this.insertBefore(new SvgText(String(value)), null);
871
+ },
872
+ _markupOf(children) {
873
+ let markup = "";
874
+ for (const child of children) {
875
+ if (child instanceof SvgElement) markup += child._markup();
876
+ else markup += escaped(child.data ?? "", false);
877
+ }
878
+ return markup;
879
+ },
880
+ };
881
+
882
+ /** Text in an `<svg>`: its `<text>` and `<title>` contents. */
883
+ export class SvgText {
884
+ #data;
885
+
886
+ constructor(data = "") {
887
+ this.#data = String(data);
888
+ this.parentNode = null;
889
+ this.childNodes = NO_CHILDREN;
890
+ }
891
+
892
+ get data() {
893
+ return this.#data;
894
+ }
895
+
896
+ set data(value) {
897
+ value = String(value);
898
+ if (value === this.#data) return;
899
+ this.#data = value;
900
+ svgChanged(this);
901
+ }
902
+
903
+ get textContent() {
904
+ return this.#data;
905
+ }
906
+
907
+ set textContent(value) {
908
+ this.data = value;
909
+ }
910
+
911
+ get nextSibling() {
912
+ const siblings = this.parentNode?.childNodes;
913
+ return siblings ? siblings[siblings.indexOf(this) + 1] ?? null : null;
914
+ }
915
+
916
+ get previousSibling() {
917
+ const siblings = this.parentNode?.childNodes;
918
+ return siblings ? siblings[siblings.indexOf(this) - 1] ?? null : null;
919
+ }
920
+
921
+ contains(other) {
922
+ for (let node = other; node; node = node.parentNode) if (node === this) return true;
923
+ return false;
924
+ }
925
+
926
+ remove() {
927
+ this.parentNode?.removeChild(this);
928
+ }
929
+
930
+ destroy() {
931
+ this.remove();
932
+ }
933
+ }
934
+
704
935
  /**
705
- * An icon: SVG drawn at the node's CSS size, as an inline `<svg>` is. Its `currentColor` is
706
- * 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.
936
+ * An element inside an `<svg>`: `path`, `circle`, `g`, `linearGradient` and the rest. It is
937
+ * no node of the tree: it is markup its `Svg` root draws. Its attributes take SVG's names or
938
+ * React's spellings (`strokeWidth`, `xlinkHref`).
708
939
  */
709
- export class Icon extends Node {
710
- #svg = "";
940
+ export class SvgElement {
941
+ #attributes = new Map();
942
+
943
+ constructor(tag) {
944
+ this.localName = String(tag);
945
+ this.tagName = this.localName;
946
+ this.namespaceURI = SVG_NAMESPACE;
947
+ this.parentNode = null;
948
+ this.childNodes = [];
949
+ }
950
+
951
+ getAttribute(name) {
952
+ return this.#attributes.get(svgName(name)) ?? null;
953
+ }
954
+
955
+ hasAttribute(name) {
956
+ return this.#attributes.has(svgName(name));
957
+ }
958
+
959
+ setAttribute(name, value) {
960
+ name = svgName(name);
961
+ if (value === null || value === undefined || value === false) {
962
+ this.removeAttribute(name);
963
+ return;
964
+ }
965
+ const text = name === "style" ? svgStyle(value) : String(value);
966
+ if (this.#attributes.get(name) === text) return;
967
+ this.#attributes.set(name, text);
968
+ svgChanged(this);
969
+ }
970
+
971
+ removeAttribute(name) {
972
+ if (this.#attributes.delete(svgName(name))) svgChanged(this);
973
+ }
974
+
975
+ get firstChild() {
976
+ return this.childNodes[0] ?? null;
977
+ }
978
+
979
+ get lastChild() {
980
+ return this.childNodes[this.childNodes.length - 1] ?? null;
981
+ }
982
+
983
+ get nextSibling() {
984
+ const siblings = this.parentNode?.childNodes;
985
+ return siblings ? siblings[siblings.indexOf(this) + 1] ?? null : null;
986
+ }
987
+
988
+ get previousSibling() {
989
+ const siblings = this.parentNode?.childNodes;
990
+ return siblings ? siblings[siblings.indexOf(this) - 1] ?? null : null;
991
+ }
711
992
 
712
- constructor(svg = "") {
993
+ contains(other) {
994
+ for (let node = other; node; node = node.parentNode) if (node === this) return true;
995
+ return false;
996
+ }
997
+
998
+ remove() {
999
+ this.parentNode?.removeChild(this);
1000
+ }
1001
+
1002
+ /** Takes the element out of its `<svg>`. It is plain markup, so nothing else is freed. */
1003
+ destroy() {
1004
+ this.remove();
1005
+ }
1006
+
1007
+ addEventListener(type) {
1008
+ warnOnce("an element inside an <svg> hears no events: listen on the <svg> ('" + type + "')");
1009
+ }
1010
+
1011
+ removeEventListener() {}
1012
+
1013
+ _markup() {
1014
+ let markup = "<" + this.localName;
1015
+ for (const [name, value] of this.#attributes) markup += " " + name + '="' + escaped(value, true) + '"';
1016
+ if (this.childNodes.length === 0) return markup + "/>";
1017
+ return markup + ">" + svgChildren._markupOf(this.childNodes) + "</" + this.localName + ">";
1018
+ }
1019
+ }
1020
+
1021
+ for (const name of ["insertBefore", "appendChild", "append", "removeChild"]) SvgElement.prototype[name] = svgChildren[name];
1022
+ Object.defineProperty(SvgElement.prototype, "textContent", Object.getOwnPropertyDescriptor(svgChildren, "textContent"));
1023
+
1024
+ // Attributes of an `<svg>` root the tree has: they go to its node. Every other one is markup.
1025
+ function treeAttribute(name) {
1026
+ return name === "class" || name === "className" || name === "id" || name === "role" || name === "tabIndex"
1027
+ || name === "tabindex" || name === "testId" || name === "label" || name === "cursor" || name === "transition"
1028
+ || name === "inert" || name === "disabled" || name === "element" || name === "lang" || name === "dir"
1029
+ || name.startsWith("data-") || ariaName(name) !== null;
1030
+ }
1031
+
1032
+ /**
1033
+ * An inline `<svg>`, as the web has it: its children are `SvgElement`s and `SvgText`s, and
1034
+ * the tree draws the markup they make as an icon. Its look, class, events and accessibility
1035
+ * are the node's; `viewBox`, `fill`, `width` and the other SVG attributes are markup.
1036
+ * Changes are drawn once, before the host applies the batch.
1037
+ */
1038
+ export class Svg extends Node {
1039
+ #attributes = new Map();
1040
+ #markup = "";
1041
+
1042
+ constructor() {
1043
+ // The tree's node for drawn SVG markup is an icon; to code it is an `svg`.
713
1044
  super("icon");
714
- this.svg = svg;
1045
+ this.kind = "svg";
1046
+ this.localName = "svg";
1047
+ this.tagName = "svg";
1048
+ this.namespaceURI = SVG_NAMESPACE;
1049
+ svgChanged(this);
1050
+ }
1051
+
1052
+ getAttribute(name) {
1053
+ return treeAttribute(String(name)) ? super.getAttribute(name) : this.#attributes.get(svgName(name)) ?? null;
715
1054
  }
716
1055
 
717
- /** The SVG markup, as `outerHTML` of an `<svg>` element would give it. */
718
- get svg() {
719
- return this.#svg;
1056
+ setAttribute(name, value) {
1057
+ name = String(name);
1058
+ if (treeAttribute(name)) {
1059
+ super.setAttribute(name === "tabindex" ? "tabIndex" : name, value);
1060
+ return;
1061
+ }
1062
+ name = svgName(name);
1063
+ if (name === "xmlns") return;
1064
+ if (value === null || value === undefined || value === false || value === "") {
1065
+ this.removeAttribute(name);
1066
+ return;
1067
+ }
1068
+ const text = name === "style" ? svgStyle(value) : String(value);
1069
+ if (this.#attributes.get(name) === text) return;
1070
+ this.#attributes.set(name, text);
1071
+ svgChanged(this);
720
1072
  }
721
1073
 
722
- set svg(markup) {
723
- markup = markup == null ? "" : String(markup);
724
- if (markup === this.#svg) return;
725
- if (this.#svg) dropIcon(this.#svg);
726
- this.#svg = markup;
727
- native.source(this.nodeId, markup ? holdIcon(markup) : 0);
1074
+ removeAttribute(name) {
1075
+ name = String(name);
1076
+ if (treeAttribute(name)) super.removeAttribute(name);
1077
+ else if (this.#attributes.delete(svgName(name))) svgChanged(this);
1078
+ }
1079
+
1080
+ /** The markup the tree draws, as `outerHTML` gives it. */
1081
+ get outerHTML() {
1082
+ let markup = '<svg xmlns="' + SVG_NAMESPACE + '"';
1083
+ const children = svgChildren._markupOf(this.childNodes);
1084
+ let xlink = this.#attributes.has("xmlns:xlink");
1085
+ for (const [name, value] of this.#attributes) {
1086
+ markup += " " + name + '="' + escaped(value, true) + '"';
1087
+ }
1088
+ if (!xlink && children.includes("xlink:")) markup += ' xmlns:xlink="http://www.w3.org/1999/xlink"';
1089
+ return markup + ">" + children + "</svg>";
1090
+ }
1091
+
1092
+ _draw() {
1093
+ if (!nodes.has(this.nodeId)) return;
1094
+ const markup = this.outerHTML;
1095
+ if (markup === this.#markup) return;
1096
+ this._forget();
1097
+ this.#markup = markup;
1098
+ native.source(this.nodeId, holdIcon(markup));
728
1099
  }
729
1100
 
730
1101
  _forget() {
731
- if (this.#svg) dropIcon(this.#svg);
732
- this.#svg = "";
1102
+ if (this.#markup) dropIcon(this.#markup);
1103
+ this.#markup = "";
733
1104
  }
734
1105
  }
735
1106
 
1107
+ for (const name of ["insertBefore", "appendChild", "append", "removeChild"]) Svg.prototype[name] = svgChildren[name];
1108
+ Object.defineProperty(Svg.prototype, "textContent", Object.getOwnPropertyDescriptor(svgChildren, "textContent"));
1109
+
1110
+ /**
1111
+ * An element of tag `tag` in the SVG namespace: an `Svg` root for `svg`, an `SvgElement`
1112
+ * inside one for anything else. `document.createElementNS` with the SVG namespace, as it were.
1113
+ */
1114
+ export function createSvgElement(tag) {
1115
+ return tag === "svg" ? new Svg() : new SvgElement(tag);
1116
+ }
1117
+
1118
+ /** Draws the `<svg>` roots that changed now, instead of before the host applies the batch. */
1119
+ export function flushSvg() {
1120
+ drawSvgs();
1121
+ }
1122
+
736
1123
  // Offsets cross the bridge as UTF-8 bytes; a rich input speaks JavaScript string indices.
737
1124
  const ASCII = /^[\x00-\x7f]*$/;
738
1125
 
@@ -931,10 +1318,12 @@ function forget(node) {
931
1318
 
932
1319
  export function createNode(kind) {
933
1320
  if (kind === "richinput") return new RichInput();
934
- if (!(kind in KINDS)) throw new TypeError("the tree has no node kind '" + kind + "'");
1321
+ if (kind === "svg") return new Svg();
1322
+ if (kind === "img") kind = "image";
1323
+ if (!(kind in KINDS) || kind === "icon") throw new TypeError("the tree has no node kind '" + kind + "'");
935
1324
  if (kind === "text") return new Text();
936
1325
  if (kind === "input") return new Input();
937
- if (kind === "icon") return new Icon();
1326
+ if (kind === "image") return new Image();
938
1327
  return new Node(kind);
939
1328
  }
940
1329
 
@@ -950,11 +1339,11 @@ export function createText(data = "") {
950
1339
  return new Text(data);
951
1340
  }
952
1341
 
953
- /** An icon showing `svg` markup, with `style` as its base look. */
954
- export function createIcon(svg, style) {
955
- const icon = new Icon(svg);
956
- if (style) icon.style.set(style);
957
- return icon;
1342
+ /** An image showing `src` (see `Image`), with `style` as its base look. */
1343
+ export function createImage(src, style) {
1344
+ const image = new Image(src);
1345
+ if (style) image.style.set(style);
1346
+ return image;
958
1347
  }
959
1348
 
960
1349
  export function createInput(style) {