[IMP] add support for top level comments

This commit is contained in:
Géry Debongnie
2021-11-29 13:22:04 +01:00
committed by Aaron Bohy
parent 779003e715
commit 8a1ac13975
53 changed files with 1086 additions and 1042 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ export { toggler } from "./toggler";
export { createBlock } from "./block_compiler";
export { list } from "./list";
export { multi } from "./multi";
export { text } from "./text";
export { text, comment } from "./text";
export { html } from "./html";
export interface VNode<T = any> {
+29 -12
View File
@@ -8,18 +8,17 @@ const nodeInsertBefore = nodeProto.insertBefore;
const characterDataSetData = getDescriptor(characterDataProto, "data").set!;
const nodeRemoveChild = nodeProto.removeChild;
class VText {
abstract class VSimpleNode {
text: string;
parentEl?: HTMLElement | undefined;
el?: Text;
el?: any;
constructor(text: string) {
this.text = text;
}
mount(parent: HTMLElement, afterNode: Node | null) {
mountNode(node: Node, parent: HTMLElement, afterNode: Node | null) {
this.parentEl = parent;
const node = document.createTextNode(toText(this.text));
nodeInsertBefore.call(parent, node, afterNode);
this.el = node;
}
@@ -29,14 +28,6 @@ class VText {
nodeInsertBefore.call(this.parentEl, this.el!, target);
}
patch(other: VText) {
const text2 = other.text;
if (this.text !== text2) {
characterDataSetData.call(this.el!, toText(text2));
this.text = text2;
}
}
beforeRemove() {}
remove() {
@@ -52,10 +43,36 @@ class VText {
}
}
class VText extends VSimpleNode {
mount(parent: HTMLElement, afterNode: Node | null) {
this.mountNode(document.createTextNode(toText(this.text)), parent, afterNode);
}
patch(other: VText) {
const text2 = other.text;
if (this.text !== text2) {
characterDataSetData.call(this.el!, toText(text2));
this.text = text2;
}
}
}
class VComment extends VSimpleNode {
mount(parent: HTMLElement, afterNode: Node | null) {
this.mountNode(document.createComment(toText(this.text)), parent, afterNode);
}
patch() {}
}
export function text(str: string): VNode<VText> {
return new VText(str);
}
export function comment(str: string): VNode<VComment> {
return new VComment(str);
}
export function toText(value: any): string {
switch (typeof value) {
case "string":