openrosa-xpath-evaluator
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -6,2 +6,7 @@ Change Log | ||
[2.0.4] - 2021-04-02 | ||
------------------------ | ||
##### Fixed | ||
- Native XPath functions do not handle node-set arguments. | ||
[2.0.3] - 2021-03-18 | ||
@@ -8,0 +13,0 @@ ------------------------ |
{ | ||
"name": "openrosa-xpath-evaluator", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Wrapper for browsers' XPath evaluator with added support for OpenRosa extensions.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://enketo.org", |
@@ -83,13 +83,13 @@ const { handleOperation } = require('./utils/operation'); | ||
case XPathResult.ANY_TYPE: | ||
// don't convert | ||
// derive return type from the return value | ||
switch(r.t) { | ||
case 'num': return { resultType:XPathResult.NUMBER_TYPE, numberValue:r.v, stringValue:r.v.toString() }; | ||
case 'str': return { resultType:XPathResult.BOOLEAN_TYPE, stringValue:r.v }; | ||
case 'bool': return { resultType:XPathResult.BOOLEAN_TYPE, booleanValue:r.v, stringValue:r.v.toString() }; | ||
case 'arr': return toSnapshotResult(r, XPathResult.UNORDERED_NODE_ITERATOR_TYPE); | ||
case 'num': return toExternalResult(r, XPathResult.NUMBER_TYPE); | ||
case 'str': return toExternalResult(r, XPathResult.STRING_TYPE); | ||
case 'bool': return toExternalResult(r, XPathResult.BOOLEAN_TYPE); | ||
case 'arr': return toExternalResult(r, XPathResult.UNORDERED_NODE_ITERATOR_TYPE); | ||
default: throw new Error('unrecognised internal type: ' + r.t); | ||
} | ||
case XPathResult.NUMBER_TYPE: return { resultType:rt, numberValue: asNumber(r), stringValue:r.v.toString() }; | ||
case XPathResult.STRING_TYPE: return { resultType:rt, stringValue: asString(r) }; | ||
case XPathResult.BOOLEAN_TYPE: return { resultType:rt, booleanValue:asBoolean(r), stringValue:r.v.toString() }; | ||
case XPathResult.NUMBER_TYPE: return { resultType:rt, stringValue:asString(r), numberValue:asNumber(r) }; | ||
case XPathResult.STRING_TYPE: return { resultType:rt, stringValue:asString(r) }; | ||
case XPathResult.BOOLEAN_TYPE: return { resultType:rt, stringValue:asString(r), booleanValue:asBoolean(r) }; | ||
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: | ||
@@ -96,0 +96,0 @@ case XPathResult.ORDERED_NODE_ITERATOR_TYPE: |
@@ -6,13 +6,15 @@ const { asNumber, asString } = require('./xpath-cast'); | ||
const cast = { num:asNumber, str:asString }; | ||
const fns = { | ||
'ceiling': { min:1, max:1 }, | ||
'contains': { min:2, max:2 }, | ||
'floor': { min:1, max:1 }, | ||
'ceiling': { min:1, max:1, cast:['num'] }, | ||
'contains': { min:2, max:2, cast:['str', 'str'] }, | ||
'floor': { min:1, max:1, cast:['num'] }, | ||
'id': { min:1, max:1, conv:r => [ xpr.string(r.t === 'arr' ? r.v.map(asString).join(' ') : asString(r)) ] }, | ||
'lang': { min:1, max:1 }, | ||
'starts-with': { min:2, max:2 }, | ||
'lang': { min:1, max:1, cast:['str'] }, | ||
'starts-with': { min:2, max:2, cast:['str', 'str'] }, | ||
'substring': { min:2, max:3, conv:convertSubstringArgs }, | ||
'substring-after': { min:2, max:2 }, | ||
'substring-before': { min:2, max:2 }, | ||
'translate': { min:3, max:3 }, | ||
'substring-after': { min:2, max:2, cast:['str', 'str'] }, | ||
'substring-before': { min:2, max:2, cast:['str', 'str'] }, | ||
'translate': { min:3, max:3, cast:['str', 'str', 'str'] }, | ||
}; | ||
@@ -27,2 +29,8 @@ | ||
return def.conv(...args); | ||
} else if(def.cast) { | ||
return args | ||
.map((v, i) => { | ||
const t = def.cast[i]; | ||
return { t, v:cast[t](v) }; | ||
}); | ||
} | ||
@@ -29,0 +37,0 @@ return args; |
@@ -106,3 +106,3 @@ const { initDoc, nsResolver, filterAttributes, assert } = require('../helpers'); | ||
it('returns correct result type', () => { | ||
const res = doc.xEval(expr, doc, 3); | ||
const res = doc.xEval(expr, doc, XPathResult.BOOLEAN_TYPE); | ||
assert.equal(res.resultType, 3); | ||
@@ -137,3 +137,3 @@ assert.equal(res.booleanValue, expected); | ||
const expr = '/model/instance[1]/*//*[@template] | /model/instance[1]/*//*[@jr:template]'; | ||
const res = doc.xEval(expr, doc, 7); | ||
const res = doc.xEval(expr, doc, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); | ||
assert.equal(res.resultType, 7); | ||
@@ -140,0 +140,0 @@ assert.equal(res.snapshotLength, 0); |
@@ -295,2 +295,10 @@ const { initDoc, filterAttributes, assertThrow, assertTrue, | ||
it('translate() with a node parameter', () => { | ||
const doc = initDoc(` | ||
<div> | ||
<a id="A">TAXIcab</a> | ||
</div>`); | ||
assertString(doc.getElementById('A'), null, 'translate( ., "abc", "ABC")', 'TAXICAB'); | ||
}); | ||
it('translate() fails when too many arguments are provided', () => { | ||
@@ -297,0 +305,0 @@ assertThrow("translate(1, 2, 3, 4)"); |
@@ -70,3 +70,3 @@ const { assert } = require('chai'); | ||
// then | ||
assert.deepEqual(actualOutput, { resultType:3, stringValue:expectedOutput }); | ||
assert.deepEqual(actualOutput, { resultType:XPathResult.STRING_TYPE, stringValue:expectedOutput }); | ||
}); | ||
@@ -73,0 +73,0 @@ }); |
@@ -131,3 +131,3 @@ const { initDoc, assert, assertThrow, assertTrue, nsResolver } = require('../helpers'); | ||
const expr = 'randomize(/model/instance[@id="crop_list"]/root/item)'; | ||
const res = doc.xEval(expr, doc, 7); | ||
const res = doc.xEval(expr, doc, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); | ||
assert.equal(res.resultType, 7); | ||
@@ -134,0 +134,0 @@ assert.equal(res.snapshotLength, 6); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
363461
8757