1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-02-03 13:11:45 +02:00
Tim Voronov 145a16f97d
Bugfix/#421 xpath (#435)
* Fixed attr retrieval using XPATH in CDP

* Updated single node value in CDP

* Added e2e test

* Fixed attr retrieval with XPATH for HTTP driver

* Update Makefile

* Update attr.fql
2019-12-29 12:46:46 -05:00

70 lines
1.4 KiB
Go

package templates
const xPathTemplate = `
(element, expression) => {
const unwrap = (item) => {
return item.nodeType != 2 ? item : item.nodeValue;
};
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(unwrap(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(unwrap(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 = unwrap(out.singleNodeValue);
break;
}
default: {
break;
}
}
return result;
}
`
func XPath() string {
return xPathTemplate
}