From cfead2e67e3e53befc361baac39d220b767b0d9e Mon Sep 17 00:00:00 2001 From: Chris Barth Date: Tue, 19 Sep 2023 00:25:06 -0500 Subject: [PATCH] Remove all the code associated with type checking --- test.js | 132 ----------------------------------------------------- xpath.d.ts | 13 ------ xpath.js | 36 --------------- 3 files changed, 181 deletions(-) diff --git a/test.js b/test.js index 962586d..42f2453 100644 --- a/test.js +++ b/test.js @@ -1051,136 +1051,4 @@ describe('xpath', () => { assert.strictEqual(xpath.select1('local-name(/book/characters)', doc), 'characters'); }); }); - - describe('Node type tests', () => { - it('should correctly identify a Node of type Element', () => { - var doc = parseXml(''); - var element = doc.createElement('characters'); - - assert.ok(xpath.isNodeLike(element)); - assert.ok(xpath.isElement(element)); - assert.ok(!xpath.isAttribute(doc)); - }); - - it('should correctly identify a Node of type Attribute', () => { - var doc = parseXml(''); - var attribute = doc.createAttribute('name'); - - assert.ok(xpath.isNodeLike(attribute)); - assert.ok(xpath.isAttribute(attribute)); - assert.ok(!xpath.isTextNode(attribute)); - }); - - it('should correctly identify a Node of type Text', () => { - var doc = parseXml(''); - var text = doc.createTextNode('Harry Potter'); - - assert.ok(xpath.isNodeLike(text)); - assert.ok(xpath.isTextNode(text)); - assert.ok(!xpath.isCDATASection(text)); - }); - - it('should correctly identify a Node of type CDATASection', () => { - var doc = parseXml(''); - var cdata = doc.createCDATASection('Harry Potter'); - - assert.ok(xpath.isNodeLike(cdata)); - assert.ok(xpath.isCDATASection(cdata)); - assert.ok(!xpath.isProcessingInstruction(cdata)); - }); - - it('should correctly identify a Node of type ProcessingInstruction', () => { - var doc = parseXml(''); - var pi = doc.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'); - - // This test fails due to a bug in @xmldom/xmldom@0.8.8 - // assert.ok(xpath.isNodeLike(pi)); - assert.ok(xpath.isProcessingInstruction(pi)); - assert.ok(!xpath.isComment(pi)); - }); - - it('should correctly identify a Node of type Comment', () => { - var doc = parseXml(''); - var comment = doc.createComment('Harry Potter'); - - assert.ok(xpath.isNodeLike(comment)); - assert.ok(xpath.isComment(comment)); - assert.ok(!xpath.isDocumentNode(comment)); - }); - - it('should correctly identify a Node of type Document', () => { - var doc = parseXml(''); - - assert.ok(xpath.isNodeLike(doc)); - assert.ok(xpath.isDocumentNode(doc)); - assert.ok(!xpath.isDocumentTypeNode(doc)); - }); - - it('should correctly identify a Node of type DocumentType', () => { - var doc = parseXml(''); - var doctype = doc.implementation.createDocumentType('book', null, null); - - assert.ok(xpath.isNodeLike(doctype)); - assert.ok(xpath.isDocumentTypeNode(doctype)); - assert.ok(!xpath.isDocumentFragment(doctype)); - }); - - it('should correctly identify a Node of type DocumentFragment', () => { - var doc = parseXml(''); - var fragment = doc.createDocumentFragment(); - - assert.ok(xpath.isNodeLike(fragment)); - assert.ok(xpath.isDocumentFragment(fragment)); - assert.ok(!xpath.isElement(fragment)); - }); - - it('should not identify a string as a Node', () => { - assert.ok(!xpath.isNodeLike('Harry Potter')); - }); - - it('should not identify a number as a Node', () => { - assert.ok(!xpath.isNodeLike(45)); - }); - - it('should not identify a boolean as a Node', () => { - assert.ok(!xpath.isNodeLike(true)); - }); - - it('should not identify null as a Node', () => { - assert.ok(!xpath.isNodeLike(null)); - }); - - it('should not identify undefined as a Node', () => { - assert.ok(!xpath.isNodeLike(undefined)); - }); - - it('should not identify an array as a Node', () => { - assert.ok(!xpath.isNodeLike([])); - }); - - it('should identify an array of Nodes as such', () => { - var doc = parseXml(''); - var fragment = doc.createDocumentFragment(); - var nodes = [doc, fragment]; - - assert.ok(xpath.isArrayOfNodes(nodes)); - assert.ok(!xpath.isNodeLike(nodes)); - }); - - it('should not identify an array of non-Nodes as an array of Nodes', () => { - var nodes = ['Harry Potter', 45]; - - assert.ok(!xpath.isArrayOfNodes(nodes)); - assert.ok(!xpath.isNodeLike(nodes)); - }); - - it('should not identify an array of mixed Nodes and non-Nodes as an array of Nodes', () => { - var doc = parseXml(''); - var fragment = doc.createDocumentFragment(); - var nodes = [doc, fragment, 'Harry Potter']; - - assert.ok(!xpath.isArrayOfNodes(nodes)); - assert.ok(!xpath.isNodeLike(nodes)); - }); - }); }); \ No newline at end of file diff --git a/xpath.d.ts b/xpath.d.ts index 9d1a734..4dd5139 100644 --- a/xpath.d.ts +++ b/xpath.d.ts @@ -36,16 +36,3 @@ export function selectWithResolver(expression: string, node: Node, resolver: XPa * @return a function with the same signature as `xpath.select` */ export function useNamespaces(namespaceMap: Record): XPathSelect; - -// Type guards to narrow down the type of the selected type of a returned Node object -export function isNodeLike(value: SelectedValue): value is Node; -export function isArrayOfNodes(value: SelectedValue): value is Node[]; -export function isElement(value: SelectedValue): value is Element; -export function isAttribute(value: SelectedValue): value is Attr; -export function isTextNode(value: SelectedValue): value is Text; -export function isCDATASection(value: SelectedValue): value is CDATASection; -export function isProcessingInstruction(value: SelectedValue): value is ProcessingInstruction; -export function isComment(value: SelectedValue): value is Comment; -export function isDocumentNode(value: SelectedValue): value is Document; -export function isDocumentTypeNode(value: SelectedValue): value is DocumentType; -export function isDocumentFragment(value: SelectedValue): value is DocumentFragment; diff --git a/xpath.js b/xpath.js index 1189c1a..e82896a 100644 --- a/xpath.js +++ b/xpath.js @@ -4898,41 +4898,5 @@ var xpath = (typeof exports === 'undefined') ? {} : exports; exports.select1 = function (e, doc) { return exports.select(e, doc, true); }; - - var isNodeLike = function (value) { - return value - && typeof value.nodeType === "number" - && Number.isInteger(value.nodeType) - && value.nodeType >= 1 - && value.nodeType <= 11 - && typeof value.nodeName === "string"; - }; - - var isArrayOfNodes = function (value) { - return Array.isArray(value) && value.every(isNodeLike); - }; - - var isNodeOfType = function (type) { - return function (value) { - return isNodeLike(value) && value.nodeType === type; - }; - }; - - assign( - exports, - { - isNodeLike: isNodeLike, - isArrayOfNodes: isArrayOfNodes, - isElement: isNodeOfType(NodeTypes.ELEMENT_NODE), - isAttribute: isNodeOfType(NodeTypes.ATTRIBUTE_NODE), - isTextNode: isNodeOfType(NodeTypes.TEXT_NODE), - isCDATASection: isNodeOfType(NodeTypes.CDATA_SECTION_NODE), - isProcessingInstruction: isNodeOfType(NodeTypes.PROCESSING_INSTRUCTION_NODE), - isComment: isNodeOfType(NodeTypes.COMMENT_NODE), - isDocumentNode: isNodeOfType(NodeTypes.DOCUMENT_NODE), - isDocumentTypeNode: isNodeOfType(NodeTypes.DOCUMENT_TYPE_NODE), - isDocumentFragment: isNodeOfType(NodeTypes.DOCUMENT_FRAGMENT_NODE), - } - ); // end non-node wrapper })(xpath);