xpath
Advanced tools
Comparing version 0.0.32 to 0.0.33
@@ -28,10 +28,10 @@ # Variable Resolvers | ||
```` | ||
````javascript | ||
var evaluator = xpath.parse('concat($character1, ", ", $character2, ", and ", $character3)'); | ||
var mainCharacters = evaluator.evaluateString({ | ||
variables: { | ||
character1: 'Harry', | ||
variables: { | ||
character1: 'Harry', | ||
character2: 'Ron', | ||
character3: 'Hermione' | ||
} | ||
} | ||
}); | ||
@@ -47,6 +47,6 @@ ```` | ||
```` | ||
````javascript | ||
var evaluator = xpath.parse('concat($hp:character1, ", ", $hp:character2, ", and ", $hp:character3)'); | ||
var mainCharacters = evaluator.evaluateString({ | ||
variables: function (name, namespace) { | ||
variables: function (name, namespace) { | ||
if (namespace === 'http://sample.org/harrypotter/') { | ||
@@ -59,3 +59,3 @@ switch (name) { | ||
} | ||
}, | ||
}, | ||
namespaces: { | ||
@@ -74,6 +74,6 @@ hp: 'http://sample.org/harrypotter/' | ||
```` | ||
````javascript | ||
var evaluator = xpath.parse('concat($hp:character1, ", ", $hp:character2, ", and ", $hp:character3)'); | ||
var mainCharacters = evaluator.evaluateString({ | ||
variables: { | ||
variables: { | ||
getVariable: function (name, namespace) { | ||
@@ -87,3 +87,3 @@ if (namespace === 'http://sample.org/harrypotter/') { | ||
} | ||
} | ||
} | ||
}, | ||
@@ -90,0 +90,0 @@ namespaces: { |
{ | ||
"name": "xpath", | ||
"version": "0.0.32", | ||
"description": "DOM 3 XPath implemention and helper for node.js.", | ||
"version": "0.0.33", | ||
"description": "DOM 3 XPath implemention and helper for node.js and the web", | ||
"engines": { | ||
@@ -19,9 +19,11 @@ "node": ">=0.6.0" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"xmldom": "^0.1.19" | ||
"@xmldom/xmldom": "^0.8.9", | ||
"es-check": "^7.1.1", | ||
"mocha": "^9.0.2" | ||
}, | ||
"typings": "./xpath.d.ts", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"validate": "es-check es5 xpath.js" | ||
}, | ||
@@ -28,0 +30,0 @@ "repository": { |
@@ -18,5 +18,5 @@ ## xpath | ||
xpath is xml engine agnostic but I recommend to use [xmldom](https://github.com/jindw/xmldom): | ||
xpath is xml engine agnostic but we recommend [xmldom](https://github.com/xmldom/xmldom): | ||
npm install xmldom | ||
npm install @xmldom/xmldom | ||
@@ -29,11 +29,11 @@ ## API Documentation | ||
`````javascript | ||
var xpath = require('xpath') | ||
, dom = require('xmldom').DOMParser | ||
var xpath = require('xpath'); | ||
var dom = require('@xmldom/xmldom').DOMParser; | ||
var xml = "<book><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var nodes = xpath.select("//title", doc) | ||
var xml = "<book><title>Harry Potter</title></book>"; | ||
var doc = new dom().parseFromString(xml, 'text/xml'); | ||
var nodes = xpath.select("//title", doc); | ||
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data) | ||
console.log("Node: " + nodes[0].toString()) | ||
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data); | ||
console.log("Node: " + nodes[0].toString()); | ||
````` | ||
@@ -51,4 +51,4 @@ ➡ | ||
var node = null; | ||
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"; | ||
var doc = new dom().parseFromString(xml, 'text/xml'); | ||
var result = xpath.evaluate( | ||
@@ -60,3 +60,3 @@ "/book/title", // xpathExpression | ||
null // result | ||
) | ||
); | ||
@@ -79,3 +79,3 @@ node = result.iterateNext(); | ||
var xml = "<book><title>Harry Potter</title></book>"; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = new dom().parseFromString(xml, 'text/xml'); | ||
var title = xpath.select("string(//title)", doc); | ||
@@ -91,7 +91,7 @@ | ||
`````javascript | ||
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0] | ||
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>"; | ||
var doc = new dom().parseFromString(xml, 'text/xml'); | ||
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0]; | ||
console.log(node.namespaceURI) | ||
console.log(node.namespaceURI); | ||
````` | ||
@@ -126,7 +126,7 @@ ➡ | ||
`````javascript | ||
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var author = xpath.select1("/book/@author", doc).value | ||
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"; | ||
var doc = new dom().parseFromString(xml, 'text/xml'); | ||
var author = xpath.select1("/book/@author", doc).value; | ||
console.log(author) | ||
console.log(author); | ||
````` | ||
@@ -140,2 +140,2 @@ ➡ | ||
## License | ||
MIT | ||
MIT |
218
test.js
const xpath = require('./xpath.js'); | ||
const dom = require('xmldom').DOMParser; | ||
const dom = require('@xmldom/xmldom').DOMParser; | ||
const assert = require('assert'); | ||
@@ -7,2 +7,6 @@ | ||
const parser = new dom(); | ||
const parseXml = (xml, mimeType = 'text/xml') => parser.parseFromString(xml, mimeType); | ||
describe('xpath', () => { | ||
@@ -18,3 +22,3 @@ describe('api', () => { | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var nodes = xpath.evaluate('//title', doc, null, xpath.XPathResult.ANY_TYPE, null).nodes; | ||
@@ -29,3 +33,3 @@ | ||
var xml = '<?book title="Harry Potter"?><?series title="Harry Potter"?><?series books="7"?><book><!-- This is a great book --><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var nodes = xpath.select('//title', doc); | ||
@@ -65,3 +69,3 @@ assert.strictEqual('title', nodes[0].localName); | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -73,3 +77,3 @@ assert.strictEqual('title', xpath.select('//title[1]', doc)[0].localName); | ||
var xml = '<book><title>Harry</title><title>Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -82,3 +86,3 @@ assert.deepEqual('book', xpath.select('local-name(/book)', doc)); | ||
var xml = '<book><title>Harry</title><title>Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -90,3 +94,3 @@ assert.deepEqual(2, xpath.select('count(//title)', doc)); | ||
var xml = '<book><title xmlns="myns">Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -104,3 +108,3 @@ var nodes = xpath.select('//*[local-name(.)="title" and namespace-uri(.)="myns"]', doc); | ||
var xml = '<book xmlns:testns="http://example.com/test" xmlns:otherns="http://example.com/other"><otherns:title>Narnia</otherns:title><testns:title>Harry Potter</testns:title><testns:field testns:type="author">JKR</testns:field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -128,3 +132,3 @@ var resolver = { | ||
var xml = '<book xmlns="http://example.com/test"><title>Harry Potter</title><field type="author">JKR</field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -147,3 +151,3 @@ var resolver = { | ||
var xml = '<book xmlns:testns="http://example.com/test"><testns:title>Harry Potter</testns:title><testns:field testns:type="author">JKR</testns:field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -167,3 +171,3 @@ var resolver = { | ||
var xml = '<book xmlns:testns="http://example.com/test"><testns:title>Harry Potter</testns:title><testns:field testns:type="author">JKR</testns:field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var select = xpath.useNamespaces({ 'testns': 'http://example.com/test' }); | ||
@@ -177,3 +181,3 @@ | ||
var xml = '<author name="J. K. Rowling"></author>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -188,3 +192,3 @@ var author = xpath.select1('/author/@name', doc).value; | ||
var xml = '<characters><character name="Snape" sex="M" age="50" /><character name="McGonnagal" sex="F" age="65" /><character name="Harry" sex="M" age="14" /></characters>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -200,3 +204,3 @@ var characters = xpath.select('/*/character[@sex = "M"][@age > 40]/@name', doc); | ||
var xml = '<authors><author name="J. K. Rowling" /><author name="Saeed Akl" /></authors>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -208,3 +212,3 @@ var authors = xpath.select('/authors/author/@name', doc); | ||
// https://github.com/goto100/xpath/issues/41 | ||
doc = new dom().parseFromString('<chapters><chapter v="1"/><chapter v="2"/><chapter v="3"/></chapters>'); | ||
doc = parseXml('<chapters><chapter v="1"/><chapter v="2"/><chapter v="3"/></chapters>'); | ||
var nodes = xpath.select("/chapters/chapter/@v", doc); | ||
@@ -233,3 +237,3 @@ var values = nodes.map(function (n) { return n.value; }); | ||
var xml = '<?book-record added="2015-01-16" author="J.K. Rowling" ?><book>Harry Potter</book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var expectedName = 'book-record'; | ||
@@ -245,3 +249,3 @@ var localName = xpath.select('local-name(/processing-instruction())', doc); | ||
var xml = '<classmate>Hermione</classmate>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -253,3 +257,3 @@ var part = xpath.select('substring-after(/classmate, "Her")', doc); | ||
it('should support preceding:: on document fragments', () => { | ||
var doc = new dom().parseFromString('<n />'), | ||
var doc = parseXml('<n />'), | ||
df = doc.createDocumentFragment(), | ||
@@ -272,3 +276,3 @@ root = doc.createElement('book'); | ||
it('should allow getting sorted and unsorted arrays from nodesets', () => { | ||
const doc = new dom().parseFromString('<book><character>Harry</character><character>Ron</character><character>Hermione</character></book>'); | ||
const doc = parseXml('<book><character>Harry</character><character>Ron</character><character>Hermione</character></book>'); | ||
const path = xpath.parse("/*/*[3] | /*/*[2] | /*/*[1]"); | ||
@@ -297,3 +301,3 @@ const nset = path.evaluateNodeSet({ node: doc }); | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var houses = xpath.parse('/school/houses/house[student = /school/honorStudents/student]').select({ node: doc }); | ||
@@ -318,3 +322,3 @@ | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var houses = xpath.parse('/school/houses/house[student/@level >= /school/courses/course/@minLevel]').select({ node: doc }); | ||
@@ -332,3 +336,3 @@ | ||
var xml = "<books><book num='1' title='PS' /><book num='2' title='CoS' /><book num='3' title='PoA' /><book num='4' title='GoF' /><book num='5' title='OotP' /><book num='6' title='HBP' /><book num='7' title='DH' /></books>"; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -377,3 +381,3 @@ var options = { node: doc, variables: { theNumber: 3, theString: '3', theBoolean: true } }; | ||
it('should correctly evaluate context position', () => { | ||
var doc = new dom().parseFromString("<books><book><chapter>The boy who lived</chapter><chapter>The vanishing glass</chapter></book><book><chapter>The worst birthday</chapter><chapter>Dobby's warning</chapter><chapter>The burrow</chapter></book></books>"); | ||
var doc = parseXml("<books><book><chapter>The boy who lived</chapter><chapter>The vanishing glass</chapter></book><book><chapter>The worst birthday</chapter><chapter>Dobby's warning</chapter><chapter>The burrow</chapter></book></books>"); | ||
@@ -406,3 +410,3 @@ var chapters = xpath.parse('/books/book/chapter[2]').select({ node: doc }); | ||
it('should work with no arguments', () => { | ||
var doc = new dom().parseFromString('<book>Harry Potter</book>'); | ||
var doc = parseXml('<book>Harry Potter</book>'); | ||
@@ -416,3 +420,3 @@ var rootElement = xpath.select1('/book', doc); | ||
it('should work on document fragments', () => { | ||
var doc = new dom().parseFromString('<n />'); | ||
var doc = parseXml('<n />'); | ||
var docFragment = doc.createDocumentFragment(); | ||
@@ -467,3 +471,3 @@ | ||
const xml = "<people><person><![CDATA[Harry Potter]]></person><person>Ron <![CDATA[Weasley]]></person></people>"; | ||
const doc = new dom().parseFromString(xml); | ||
const doc = parseXml(xml); | ||
@@ -483,3 +487,3 @@ const person1 = xpath.parse("/people/person").evaluateString({ node: doc }); | ||
var xml = "<book xmlns:hp='http://harry'><!-- This describes the Harry Potter Book --><?author name='J.K. Rowling' ?><title lang='en'><![CDATA[Harry Potter & the Philosopher's Stone]]></title><character>Harry Potter</character></book>", | ||
doc = new dom().parseFromString(xml), | ||
doc = parseXml(xml), | ||
allText = xpath.parse('.').evaluateString({ node: doc }), | ||
@@ -529,3 +533,3 @@ ns = xpath.parse('*/namespace::*[name() = "hp"]').evaluateString({ node: doc }), | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var parsed = xpath.parse('/*/title'); | ||
@@ -546,3 +550,3 @@ | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var parsed = xpath.parse('/*/title'); | ||
@@ -565,3 +569,3 @@ | ||
var xml = '<book><title>Harry Potter</title><numVolumes>7</numVolumes></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var parsed = xpath.parse('/*/numVolumes'); | ||
@@ -583,3 +587,3 @@ | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var context = { node: doc }; | ||
@@ -609,3 +613,3 @@ | ||
'</characters>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -657,3 +661,3 @@ var expr = xpath.parse('/characters/c:character'); | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -708,3 +712,3 @@ var parsed = xpath.parse('concat(double(/*/title), " is cool")'); | ||
var xml = '<book><title>Harry Potter</title><friend>Ron</friend><friend>Hermione</friend><friend>Neville</friend></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -772,3 +776,3 @@ var parsed = xpath.parse('concat(hp:double(/*/title), " is 2 cool ", hp:square(2), " school")'); | ||
var xml = '<book><title>Harry Potter</title><volumes>7</volumes></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
@@ -817,3 +821,3 @@ var variables = { | ||
var xml = '<book><title>Harry Potter</title><volumes>7</volumes></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var doc = parseXml(xml); | ||
var hpns = 'http://harry-potter.com'; | ||
@@ -892,3 +896,3 @@ | ||
var docHtml = new dom().parseFromString(markup, 'text/html'); | ||
var docHtml = parseXml(markup, 'text/html'); | ||
@@ -932,3 +936,3 @@ var noPrefixPath = xpath.parse('/html/body/p[2]'); | ||
var markup = '<html><head></head><body><p>Hi Ron!</p><my:p xmlns:my="http://www.example.com/my">Hi Draco!</p><p>Hi Hermione!</p></body></html>'; | ||
var docHtml = new dom().parseFromString(markup, 'text/html'); | ||
var docHtml = parseXml(markup, 'text/html'); | ||
@@ -989,3 +993,3 @@ var ns = { h: xhtmlNs }; | ||
it('should support the contains() function on attributes', () => { | ||
var doc = new dom().parseFromString("<books><book title='Harry Potter and the Philosopher\"s Stone' /><book title='Harry Potter and the Chamber of Secrets' /></books>"), | ||
var doc = parseXml("<books><book title='Harry Potter and the Philosopher\"s Stone' /><book title='Harry Potter and the Chamber of Secrets' /></books>"), | ||
andTheBooks = xpath.select("/books/book[contains(@title, ' ')]", doc), | ||
@@ -1003,3 +1007,3 @@ secretBooks = xpath.select("/books/book[contains(@title, 'Secrets')]", doc); | ||
var characters = new dom().parseFromString('<characters><character>Harry</character><character>Ron</character><character>Hermione</character></characters>'); | ||
var characters = parseXml('<characters><character>Harry</character><character>Ron</character><character>Hermione</character></characters>'); | ||
@@ -1081,3 +1085,3 @@ var firstTwo = xpath.parse('/characters/character[position() <= 2]').select({ node: characters }); | ||
it('should work with nodes created using DOM1 createElement()', () => { | ||
var doc = new dom().parseFromString('<book />'); | ||
var doc = parseXml('<book />'); | ||
@@ -1091,2 +1095,134 @@ doc.documentElement.appendChild(doc.createElement('characters')); | ||
}); | ||
describe('Node type tests', () => { | ||
it('should correctly identify a Node of type Element', () => { | ||
var doc = parseXml('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
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('<book />'); | ||
var fragment = doc.createDocumentFragment(); | ||
var nodes = [doc, fragment, 'Harry Potter']; | ||
assert.ok(!xpath.isArrayOfNodes(nodes)); | ||
assert.ok(!xpath.isNodeLike(nodes)); | ||
}); | ||
}); | ||
}); |
/// <reference lib="dom" /> | ||
type SelectedValue = Node | Attr | string | number | boolean; | ||
interface XPathSelect { | ||
(expression: string, node?: Node): Array<SelectedValue>; | ||
(expression: string, node: Node, single: true): SelectedValue | undefined; | ||
export type SelectedValue = Node | string | number | boolean | null; | ||
export type SelectReturnType = Array<Node> | SelectedValue; | ||
export type SelectSingleReturnType = SelectedValue; | ||
export interface XPathSelect { | ||
(expression: string, node: Node): SelectReturnType; | ||
(expression: string, node: Node, single: false): SelectReturnType; | ||
(expression: string, node: Node, single: true): SelectSingleReturnType; | ||
} | ||
export var select: XPathSelect; | ||
export function select1(expression: string, node?: Node): SelectedValue | undefined; | ||
export function evaluate(expression: string, contextNode: Node, resolver: XPathNSResolver | null, type: number, result: XPathResult | null): XPathResult; | ||
export function useNamespaces(namespaceMap: { [name: string]: string }): XPathSelect; | ||
/** | ||
* Evaluate an XPath expression against a DOM node. | ||
*/ | ||
export function select(expression: string, node: Node): SelectReturnType; | ||
export function select(expression: string, node: Node, single: false): SelectReturnType; | ||
export function select(expression: string, node: Node, single: true): SelectSingleReturnType; | ||
/** | ||
* Evaluate an xpath expression against a DOM node, returning the first result only. | ||
*/ | ||
export function select1(expression: string, node: Node): SelectSingleReturnType; | ||
/** | ||
* Evaluate an XPath expression against a DOM node using a given namespace resolver. | ||
*/ | ||
export function selectWithResolver(expression: string, node: Node, resolver?: XPathNSResolver | null): SelectReturnType; | ||
export function selectWithResolver(expression: string, node: Node, resolver: XPathNSResolver | null, single: false): SelectReturnType; | ||
export function selectWithResolver(expression: string, node: Node, resolver: XPathNSResolver | null, single: true): SelectSingleReturnType; | ||
/** | ||
* Creates a `select` function that uses the given namespace prefix to URI mappings when evaluating queries. | ||
* @param namespaceMap an object mapping namespace prefixes to namespace URIs. Each key is a prefix; each value is a URI. | ||
* @return a function with the same signature as `xpath.select` | ||
*/ | ||
export function useNamespaces(namespaceMap: Record<string, string>): 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; |
Sorry, the diff of this file is too big to display
245949
5281
134
3