Precise DOM morphing
morphing typescript dom
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

91 cloc

+7 -8
+2 -3
dist/morphlite.js
··· 23 23 else 24 24 from.replaceWith(to.cloneNode(true)); 25 25 } 26 - else { 27 - throw new Error(`Cannot morph nodes of different types: from is ${from.constructor.name}, to is ${to.constructor.name}`); 28 - } 26 + else 27 + throw new Error(`Cannot morph from ${from.constructor.name}, to ${to.constructor.name}`); 29 28 } 30 29 function morphAttributes(from, to) { 31 30 for (const { name } of from.attributes)
+5 -5
src/morphlite.ts
··· 22 22 if (from.attributes.length > 0 || to.attributes.length > 0) morphAttributes(from, to); 23 23 if (from.childNodes.length > 0 || to.childNodes.length > 0) morphChildNodes(from, to, idMap); 24 24 } else from.replaceWith(to.cloneNode(true)); 25 - } else { 26 - throw new Error( 27 - `Cannot morph nodes of different types: from is ${from.constructor.name}, to is ${to.constructor.name}`, 28 - ); 29 - } 25 + } else throw new Error(`Cannot morph from ${from.constructor.name}, to ${to.constructor.name}`); 30 26 } 31 27 32 28 function morphAttributes(from: Element, to: Element): void { ··· 97 93 function numberOfItemsInCommon<T>(a: Set<T>, b: Set<T>): number { 98 94 return [...a].filter((item) => b.has(item)).length; 99 95 } 96 + 97 + // We cannot use `instanceof` when nodes might be from different documents, 98 + // so we use type guards instead. This keeps TypeScript happy, while doing 99 + // the necessary checks at runtime. 100 100 101 101 function isElement(node: Node): node is Element { 102 102 return node.nodeType === 1;