All files node.ts

86.67% Statements 52/60
62.5% Branches 15/24
68% Functions 17/25
84.31% Lines 43/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161    1x                       1x   162x     162x     162x     162x     162x           162x     1x 20x       1x               1x 2x             1x 2x           1x   1x               92x   92x     1x 16x           1x   1x 6x 6x   1x   1x                       70x   70x       1x 4x     1x 4x     1x             1x   1x             70x 70x   70x               70x       1x 4x           1x  
import { ElementType } from "domelementtype";
 
const nodeTypes = new Map<ElementType, number>([
    [ElementType.Tag, 1],
    [ElementType.Script, 1],
    [ElementType.Style, 1],
    [ElementType.Directive, 1],
    [ElementType.Text, 3],
    [ElementType.CDATA, 4],
    [ElementType.Comment, 8]
]);
 
// This object will be used as the prototype for Nodes when creating a
// DOM-Level-1-compliant structure.
export class Node {
    /** Parent of the node */
    parent: NodeWithChildren | null = null;
 
    /** Previous sibling */
    prev: Node | null = null;
 
    /** Next sibling */
    next: Node | null = null;
 
    /** The start index of the node. Requires `withStartIndices` on the handler to be `true. */
    startIndex: number | null = null;
 
    /** The end index of the node. Requires `withEndIndices` on the handler to be `true. */
    endIndex: number | null = null;
 
    /**
     *
     * @param type The type of the node.
     */
    constructor(public type: ElementType) {}
 
    // Read-only aliases
    get nodeType(): number {
        return nodeTypes.get(this.type) || 1;
    }
 
    // Read-write aliases for properties
    get parentNode(): NodeWithChildren | null {
        return this.parent || null;
    }
 
    set parentNode(parent: NodeWithChildren | null) {
        this.parent = parent;
    }
 
    get previousSibling(): Node | null {
        return this.prev || null;
    }
 
    set previousSibling(prev: Node | null) {
        this.prev = prev;
    }
 
    get nextSibling(): Node | null {
        return this.next || null;
    }
 
    set nextSibling(next: Node | null) {
        this.next = next;
    }
}
 
export class DataNode extends Node {
    /**
     *
     * @param type The type of the node
     * @param data The content of the data node
     */
    constructor(
        type: ElementType.Comment | ElementType.Text | ElementType.Directive,
        public data: string
    ) {
        super(type);
    }
 
    get nodeValue(): string {
        return this.data;
    }
 
    set nodeValue(data: string) {
        this.data = data;
    }
}
 
export class ProcessingInstruction extends DataNode {
    constructor(public name: string, data: string) {
        super(ElementType.Directive, data);
    }
}
 
export class NodeWithChildren extends Node {
    /**
     *
     * @param type Type of the node.
     * @param children Children of the node. Only certain node types can have children.
     */
    constructor(
        type:
            | ElementType.CDATA
            | ElementType.Script
            | ElementType.Style
            | ElementType.Tag,
        public children: Node[]
    ) {
        super(type);
    }
 
    // Aliases
    get firstChild(): Node | null {
        return this.children[0] || null;
    }
 
    get lastChild(): Node | null {
        return this.children[this.children.length - 1] || null;
    }
 
    get childNodes(): Node[] {
        return this.children;
    }
 
    set childNodes(children: Node[]) {
        this.children = children;
    }
}
 
export class Element extends NodeWithChildren {
    /**
     *
     * @param name Name of the tag, eg. `div`, `span`
     * @param attribs Object mapping attribute names to attribute values
     */
    constructor(
        public name: string,
        public attribs: { [name: string]: string }
    ) {
        super(
            name === "script"
                ? ElementType.Script
                : name === "style"
                ? ElementType.Style
                : ElementType.Tag,
            []
        );
        this.attribs = attribs;
    }
 
    // DOM Level 1 aliases
    get tagName(): string {
        return this.name;
    }
 
    set tagName(name: string) {
        this.name = name;
    }
}