Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('skips the subtree for outermost() with tail', () => {
jsonMlMapper.parse(['root', ['foo', ['foo']]], documentNode);
const descendant = documentNode.documentElement.firstChild.firstChild;
// It is successful when the dom facade does not throw
const nodes = evaluateXPathToNodes(
'outermost(tail(descendant::node()))',
documentNode,
buildDomFacade(descendant),
null,
{ language: evaluateXPath.XQUERY_3_1_LANGUAGE }
);
chai.assert.deepEqual(nodes, [documentNode.documentElement.firstChild]);
});
it('skips the subtree for outermost() in debug mode', () => {
jsonMlMapper.parse(['root', ['foo', ['foo']]], documentNode);
const descendant = documentNode.documentElement.firstChild.firstChild;
// It is successful when the dom facade does not throw
const nodes = evaluateXPathToNodes(
'outermost(descendant::foo)',
documentNode,
buildDomFacade(descendant),
null,
{
debug: true
}
);
chai.assert.deepEqual(nodes, [documentNode.documentElement.firstChild]);
});
});
function createEnvironment(cwd, environmentNode) {
const fileName = evaluateXPathToString('source[@role="."]/@file', environmentNode);
const variables = evaluateXPathToNodes('source[@role!="."]', environmentNode).reduce(
(varsByName, variable) =>
Object.assign(varsByName, {
[evaluateXPathToString('@role', variable).substr(1)]: getFile(
(cwd ? cwd + '/' : '') + evaluateXPathToString('@file', variable)
)
}),
{}
);
// Params area also variables. But different
evaluateXPathToNodes('param', environmentNode).forEach(paramNode => {
variables[evaluateXPathToString('@name', paramNode)] = evaluateXPath(
evaluateXPathToString('@select', paramNode)
);
console.log(variables);
});
const namespaces = evaluateXPathToMap(
'(namespace!map:entry(@prefix/string(), @uri/string())) => map:merge()',
environmentNode
);
return {
contextNode: fileName ? getFile((cwd ? cwd + '/' : '') + fileName) : null,
variables,
namespaceResolver: Object.keys(namespaces).length ? prefix => namespaces[prefix] : null
};
[evaluateXPathToString('@role', variable).substr(1)]: getFile(
(cwd ? cwd + '/' : '') + evaluateXPathToString('@file', variable)
)
}),
{}
);
// Params area also variables. But different
evaluateXPathToNodes('param', environmentNode).forEach(paramNode => {
variables[evaluateXPathToString('@name', paramNode)] = evaluateXPath(
evaluateXPathToString('@select', paramNode)
);
console.log(variables);
});
const namespaces = evaluateXPathToMap(
'(namespace!map:entry(@prefix/string(), @uri/string())) => map:merge()',
environmentNode
);
return {
contextNode: fileName ? getFile((cwd ? cwd + '/' : '') + fileName) : null,
variables,
namespaceResolver: Object.keys(namespaces).length ? prefix => namespaces[prefix] : null
};
}
it('allows replacing something with something asynchronous', async () => {
const element = documentNode.appendChild(documentNode.createElement('element'));
// Duplicate all list items and set the @count attribute to the new count of items, in a very roundabout way
const result = await evaluateUpdatingExpression(
`
declare namespace fontoxpath="http://fontoxml.com/fontoxpath";
replace node fontoxpath:sleep(/element, 100) with fontoxpath:sleep(, 1)
`,
documentNode,
null,
{},
{}
);
chai.assert.deepEqual(result.xdmValue, []);
assertUpdateList(result.pendingUpdateList, [
{
replacementXML: [''],
target: element,
const actualOuterHTML =
actual.nodeType === actual.DOCUMENT_NODE
? actual.documentElement.outerHTML
: actual.outerHTML;
const expectedOuterHTML =
expected.nodeType === expected.DOCUMENT_NODE
? expected.documentElement.outerHTML
: expected.outerHTML;
// Try fast string compare
if (actualOuterHTML === expectedOuterHTML) {
return;
}
if (
evaluateXPathToBoolean('deep-equal($a, $b)', null, null, {
a: actual,
b: expected
})
) {
return;
}
// Do comparison on on outer HTML for clear fail message
chai.assert.equal(actualOuterHTML, expectedOuterHTML);
}
function assertXml (actual, expected) {
// actual.normalize();
expected.normalize();
const actualOuterHTML = actual.nodeType === actual.DOCUMENT_NODE ? actual.documentElement.outerHTML : actual.outerHTML;
const expectedOuterHTML = expected.nodeType === expected.DOCUMENT_NODE ? expected.documentElement.outerHTML : expected.outerHTML;
// Try fast string compare
if (actualOuterHTML === expectedOuterHTML) {
return;
}
if (evaluateXPathToBoolean('deep-equal($a, $b)', null, null, {
a: actual,
b: expected
})) {
return;
}
// Do comparison on on outer HTML for clear fail message
chai.assert.equal(actualOuterHTML, expectedOuterHTML);
}
createProcessingInstruction: assertNode.ownerDocument.createProcessingInstruction.bind(
assertNode.ownerDocument
),
createTextNode: assertNode.ownerDocument.createTextNode.bind(assertNode.ownerDocument)
};
switch (assertNode.localName) {
case 'all-of': {
const asserts = evaluateXPathToNodes('*', assertNode).map(innerAssertNode =>
createAsserter(baseUrl, innerAssertNode, language)
);
return (xpath, contextNode, variablesInScope, namespaceResolver) =>
asserts.forEach(a => a(xpath, contextNode, variablesInScope, namespaceResolver));
}
case 'any-of': {
const asserts = evaluateXPathToNodes('*', assertNode).map(innerAssertNode =>
createAsserter(baseUrl, innerAssertNode, language)
);
return (xpath, contextNode, variablesInScope, namespaceResolver) => {
const errors = [];
chai.assert(
asserts.some(a => {
try {
a(xpath, contextNode, variablesInScope, namespaceResolver);
} catch (error) {
if (error instanceof TypeError) {
// TypeErrors are always errors
throw error;
}
errors.push(error);
return false;
}
it('returns false if the first operand is equal to the second', () => {
chai.assert.isFalse(evaluateXPathToBoolean('1 lt 1'));
});
it('returns a float when given a float', () =>
chai.assert.isTrue(
evaluateXPathToBoolean('-(xs:float("1.5")) instance of xs:float', documentNode)
));