mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
7ce6797e9c
* Implemented XPath for CDP driver * Added XPATH function * Added e2e tests for CDP * Fixed linting issues * Added support of XPath to HTTP driver * Fixed linting issues
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package templates
|
|
|
|
const xPathTemplate = `
|
|
(element, expression) => {
|
|
const out = document.evaluate(
|
|
expression,
|
|
element,
|
|
null,
|
|
XPathResult.ANY_TYPE
|
|
);
|
|
let result;
|
|
|
|
switch (out.resultType) {
|
|
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
|
|
case XPathResult.ORDERED_NODE_ITERATOR_TYPE: {
|
|
result = [];
|
|
let item;
|
|
|
|
while ((item = out.iterateNext())) {
|
|
result.push(item);
|
|
}
|
|
|
|
break;
|
|
}
|
|
case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
|
|
case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: {
|
|
result = [];
|
|
|
|
for (let i = 0; i < out.snapshotLength; i++) {
|
|
const item = out.snapshotItem(i);
|
|
|
|
if (item != null) {
|
|
result.push(item);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case XPathResult.NUMBER_TYPE: {
|
|
result = out.numberValue;
|
|
break;
|
|
}
|
|
case XPathResult.STRING_TYPE: {
|
|
result = out.stringValue;
|
|
break;
|
|
}
|
|
case XPathResult.BOOLEAN_TYPE: {
|
|
result = out.booleanValue;
|
|
break;
|
|
}
|
|
case XPathResult.ANY_UNORDERED_NODE_TYPE:
|
|
case XPathResult.FIRST_ORDERED_NODE_TYPE: {
|
|
result = out.singleNodeValue;
|
|
break;
|
|
}
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
`
|
|
|
|
func XPath() string {
|
|
return xPathTemplate
|
|
}
|