diff --git a/dist/morphlex.js b/dist/morphlex.js index 92c70bf..2caa85b 100644 --- a/dist/morphlex.js +++ b/dist/morphlex.js @@ -114,9 +114,6 @@ function isEqualNode(node, ref) { const referenceNode = ref; return node.isEqualNode(referenceNode); } -// We cannot use `instanceof` when nodes might be from different documents, -// so we use type guards instead. This keeps TypeScript happy, while doing -// the necessary checks at runtime. function isText(node) { return node.nodeType === 3; } diff --git a/src/morphlex.ts b/src/morphlex.ts index 7f89266..c9bb01a 100644 --- a/src/morphlex.ts +++ b/src/morphlex.ts @@ -8,6 +8,13 @@ interface ReadOnlyNodeList { } interface ReadOnlyNodeInterface { + readonly nodeValue: Node["nodeValue"]; + readonly textContent: Node["textContent"]; + readonly indeterminate: HTMLInputElement["indeterminate"]; + readonly value: HTMLInputElement["value"]; + readonly checked: HTMLInputElement["checked"]; + readonly disabled: HTMLInputElement["disabled"]; + readonly selected: HTMLOptionElement["selected"]; readonly id: Element["id"]; readonly tagName: Element["tagName"]; readonly nodeType: Node["nodeType"]; @@ -18,6 +25,8 @@ interface ReadOnlyNodeInterface { readonly hasAttribute: Element["hasAttribute"]; readonly querySelectorAll: (query: string) => NodeListOf | ReadOnlyNodeList; readonly parentElement: ReadOnlyNodeInterface | Element["parentElement"]; + readonly hasAttributes: Element["hasAttributes"]; + readonly hasChildNodes: ParentNode["hasChildNodes"]; } type ReadOnlyNode = T | ReadOnlyNodeInterface; @@ -169,30 +178,44 @@ function isEqualNode(node: Node, ref: ReadOnlyNode): boolean { // so we use type guards instead. This keeps TypeScript happy, while doing // the necessary checks at runtime. -function isText(node: ReadOnlyNode): node is Text { +function isText(node: Node): node is Text; +function isText(node: ReadOnlyNode): node is ReadOnlyNode; +function isText(node: Node | ReadOnlyNode): boolean { return node.nodeType === 3; } -function isComment(node: ReadOnlyNode): node is Comment { +function isComment(node: Node): node is Comment; +function isComment(node: ReadOnlyNode): node is ReadOnlyNode; +function isComment(node: Node | ReadOnlyNode): boolean { return node.nodeType === 8; } -function isElement(node: ReadOnlyNode): node is Element { +function isElement(node: Node): node is Element; +function isElement(node: ReadOnlyNode): node is ReadOnlyNode; +function isElement(node: Node | ReadOnlyNode): boolean { return node.nodeType === 1; } -function isInput(element: ReadOnlyNode): element is HTMLInputElement { +function isInput(element: Element): element is HTMLInputElement; +function isInput(element: ReadOnlyNode): element is ReadOnlyNode; +function isInput(element: Element | ReadOnlyNode): boolean { return element.localName === "input"; } -function isOption(element: ReadOnlyNode): element is HTMLOptionElement { +function isOption(element: Element): element is HTMLOptionElement; +function isOption(element: ReadOnlyNode): element is ReadOnlyNode; +function isOption(element: Element | ReadOnlyNode): boolean { return element.localName === "option"; } -function isTextArea(element: ReadOnlyNode): element is HTMLTextAreaElement { +function isTextArea(element: Element): element is HTMLTextAreaElement; +function isTextArea(element: ReadOnlyNode): element is ReadOnlyNode; +function isTextArea(element: Element | ReadOnlyNode): boolean { return element.localName === "textarea"; } -function isParentNode(node: ReadOnlyNode): node is ParentNode { +function isParentNode(node: Node): node is ParentNode; +function isParentNode(node: ReadOnlyNode): node is ParentNode; +function isParentNode(node: Node | ReadOnlyNode): boolean { return node.nodeType === 1 || node.nodeType === 9 || node.nodeType === 11; }