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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 50x 1x 25x 25x 25x 25x 25x 1x 48x 48x 48x 48x 1x 1x 70x 70x 70x 70x 4x 70x 1x 70x 70x 70x 1x 453x 453x 453x 379x 25x 354x 74x 8x 74x 74x 74x 1x 12x 12x 12x 12x 1x 12x 1x 1x 1x 6x 6x 1x 48x 48x 1x 162x 162x 162x 162x 162x 28x 162x 14x 162x 162x 66x 66x 162x 74x 162x 1x 1x 1x | import { ElementType } from "domelementtype"; import { Node, Element, DataNode, NodeWithChildren, ProcessingInstruction } from "./node"; export { Node, NodeWithChildren, DataNode, Element }; const reWhitespace = /\s+/g; export interface DomHandlerOptions { /*** * Indicates whether the whitespace in text nodes should be normalized * (= all whitespace should be replaced with single spaces). The default value is "false". */ normalizeWhitespace?: boolean; /*** * Indicates whether a startIndex property will be added to nodes. * When the parser is used in a non-streaming fashion, startIndex is an integer * indicating the position of the start of the node in the document. * The default value is "false". */ withStartIndices?: boolean; /*** * Indicates whether a endIndex property will be added to nodes. * When the parser is used in a non-streaming fashion, endIndex is an integer * indicating the position of the end of the node in the document. * The default value is "false". */ withEndIndices?: boolean; } // Default options const defaultOpts: DomHandlerOptions = { normalizeWhitespace: false, withStartIndices: false, withEndIndices: false }; interface ParserInterface { startIndex: number | null; endIndex: number | null; } type Callback = (error: Error | null, dom: Node[]) => void; type ElementCallback = (element: Element) => void; export class DomHandler { /** The constructed DOM */ public dom: Node[] = []; /** Called once parsing has completed. */ private _callback: Callback | null; /** Settings for the handler. */ private _options: DomHandlerOptions; /** Callback whenever a tag is closed. */ private _elementCB: ElementCallback | null; /** Indicated whether parsing has been completed. */ private _done: boolean = false; /** Stack of open tags. */ private _tagStack: Element[] = []; /** A data node that is still being written to. */ private _lastNode: DataNode | null = null; /** Reference to the parser instance. Used for location information. */ private _parser: ParserInterface | null = null; /** * Initiate a new DomHandler. * * @param callback Called once parsing has completed. * @param options Settings for the handler. * @param elementCB Callback whenever a tag is closed. */ public constructor( callback?: Callback | null, options?: DomHandlerOptions | null, elementCB?: ElementCallback ) { // Make it possible to skip arguments, for backwards-compatibility Iif (typeof options === "function") { elementCB = options; options = defaultOpts; } Iif (typeof callback === "object") { options = callback; callback = undefined; } this._callback = callback || null; this._options = options || defaultOpts; this._elementCB = elementCB || null; } public onparserinit(parser: ParserInterface): void { this._parser = parser; } // Resets the handler back to starting state public onreset(): void { this.dom = []; this._done = false; this._tagStack = []; this._lastNode = null; this._parser = this._parser || null; } // Signals the handler that parsing is done public onend(): void { Iif (this._done) return; this._done = true; this._parser = null; this.handleCallback(null); } public onerror(error: Error): void { this.handleCallback(error); } public onclosetag(): void { this._lastNode = null; // If(this._tagStack.pop().name !== name) this.handleCallback(Error("Tagname didn't match!")); const elem = this._tagStack.pop(); Iif (!elem || !this._parser) { return; } if (this._options.withEndIndices) { elem.endIndex = this._parser.endIndex; } Iif (this._elementCB) this._elementCB(elem); } public onopentag(name: string, attribs: { [key: string]: string }): void { const element = new Element(name, attribs); this.addNode(element); this._tagStack.push(element); } public ontext(data: string): void { const normalize = this._options.normalizeWhitespace; const { _lastNode } = this; if (_lastNode && _lastNode.type === ElementType.Text) { if (normalize) { _lastNode.data = (_lastNode.data + data).replace( reWhitespace, " " ); } else { _lastNode.data += data; } } else { if (normalize) { data = data.replace(reWhitespace, " "); } const node = new DataNode(ElementType.Text, data); this.addNode(node); this._lastNode = node; } } public oncomment(data: string): void { Iif (this._lastNode && this._lastNode.type === ElementType.Comment) { this._lastNode.data += data; return; } const node = new DataNode(ElementType.Comment, data); this.addNode(node); this._lastNode = node; } public oncommentend(): void { this._lastNode = null; } public oncdatastart(): void { const text = new DataNode(ElementType.Text, ""); const node = new NodeWithChildren(ElementType.CDATA, [text]); this.addNode(node); text.parent = node; this._lastNode = text; } public oncdataend(): void { this._lastNode = null; } public onprocessinginstruction(name: string, data: string): void { const node = new ProcessingInstruction(name, data); this.addNode(node); } protected handleCallback(error: Error | null): void { Eif (typeof this._callback === "function") { this._callback(error, this.dom); } else if (error) { throw error; } } protected addNode(node: Node) { const parent = this._tagStack[this._tagStack.length - 1]; const siblings = parent ? parent.children : this.dom; const previousSibling = siblings[siblings.length - 1]; Eif (this._parser) { if (this._options.withStartIndices) { node.startIndex = this._parser.startIndex; } if (this._options.withEndIndices) { node.endIndex = this._parser.endIndex; } } siblings.push(node); if (previousSibling) { node.prev = previousSibling; previousSibling.next = node; } if (parent) { node.parent = parent; } this._lastNode = null; } protected addDataNode(node: DataNode): void { this.addNode(node); this._lastNode = node; } } export default DomHandler; |