Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
it('allows rename', async () => {
const element = documentNode.appendChild(documentNode.createElement('element'));
const result = await evaluateUpdatingExpression(
`rename node /element as "elem"`,
documentNode,
null,
{},
{});
chai.assert.deepEqual(result.xdmValue, []);
assertUpdateList(result.pendingUpdateList, [
{
type: 'rename',
target: element,
newName: { prefix: '', namespaceURI: null, localPart: 'elem' }
}
]);
});
it('merges pul from target expressions', async () => {
const element = documentNode.appendChild(documentNode.createElement('element'));
const a = element.appendChild(documentNode.createElement('a'));
const result = await evaluateUpdatingExpression(
`delete node (/element, delete node /element/a)`,
documentNode,
null,
{},
{}
);
chai.assert.deepEqual(result.xdmValue, []);
assertUpdateList(result.pendingUpdateList, [
{
type: 'delete',
target: element
},
{
type: 'delete',
target: a
it('disallows replacing attributes with elements', async () => {
documentNode
.appendChild(documentNode.createElement('element'))
.setAttribute('attr', 'value');
let error;
try {
await evaluateUpdatingExpression(
'replace node /element/@attr with ',
documentNode,
null,
{},
{}
);
} catch (err) {
error = err;
}
chai.assert.throws(() => {
if (error) {
throw error;
} else {
return null;
}
it('allows rename', async () => {
const element = documentNode.appendChild(documentNode.createElement('element'));
const result = await evaluateUpdatingExpression(
`rename node /element as "elem"`,
documentNode,
null,
{},
{}
);
chai.assert.deepEqual(result.xdmValue, []);
assertUpdateList(result.pendingUpdateList, [
{
type: 'rename',
target: element,
newName: { prefix: '', namespaceURI: null, localName: 'elem' }
}
]);
});
it('disallows replacing multiple nodes at once', async () => {
let error;
try {
await evaluateUpdatingExpression(
'replace node (/, /) with ',
documentNode,
null,
{},
{}
);
} catch (err) {
error = err;
}
chai.assert.throws(() => {
if (error) {
throw error;
} else {
return null;
}
it('merges puls from copy clauses', async () => {
const element = documentNode.appendChild(documentNode.createElement('element'));
const result = await evaluateUpdatingExpression(
`
copy $a := (element, replace node element with ),
$b := (element, rename node element as "renamed")
modify replace value of node $a with "content"
return $a
`,
documentNode,
null,
{},
{ returnType: evaluateXPath.NODES_TYPE }
);
chai.assert.equal(result.xdmValue.length, 1);
const actualXml = new slimdom.XMLSerializer().serializeToString(result.xdmValue[0]);
chai.assert.equal(actualXml, '<element>content</element>');
assertUpdateList(result.pendingUpdateList, [
{
it('can replace a node and generate the correct update list', async () => {
const ele = documentNode.appendChild(documentNode.createElement('ele'));
const result = await evaluateUpdatingExpression(
'replace node ele with ',
documentNode,
null,
{},
{}
);
chai.assert.deepEqual(result.xdmValue, []);
assertUpdateList(result.pendingUpdateList, [
{
replacementXML: [''],
target: ele,
type: 'replaceNode'
}
]);
});
it('disallows replacing the value of a document node', async () => {
let error;
try {
await evaluateUpdatingExpression(
'replace value of node . with ',
documentNode,
null,
{},
{}
);
} catch (err) {
error = err;
}
chai.assert.throws(() => {
if (error) {
throw error;
} else {
return null;
}
const getChildNode = (node: slimdom.Node) =>
node === documentNode ? xml : node === xml ? a : null;
const myDomFacade: IDomFacade = {
getAllAttributes: () => [],
getAttribute: () => null,
getChildNodes: (node: slimdom.Node) => (getChildNode(node) ? [getChildNode(node)] : []),
getData: () => '',
getFirstChild: getChildNode,
getLastChild: getChildNode,
getNextSibling: () => null,
getParentNode: (node: slimdom.Node) =>
node === a ? xml : node === xml ? documentNode : null,
getPreviousSibling: () => null
};
const result = await evaluateUpdatingExpression(
`copy $a := xml modify () return $a`,
documentNode,
myDomFacade,
{},
{}
);
const actualXml = new slimdom.XMLSerializer().serializeToString(result.xdmValue[0]);
const expectedXml = new slimdom.XMLSerializer().serializeToString(sync('<a></a><a>'));
chai.assert.equal(actualXml, expectedXml);
assertUpdateList(result.pendingUpdateList, []);
});
</a>