fontoxpath
Version:
A minimalistic XPath 3.1 engine in JavaScript
89 lines (80 loc) • 2.25 kB
JavaScript
import * as slimdom from 'slimdom';
import jsonMlMapper from 'test-helpers/jsonMlMapper';
import {
evaluateXPathToNodes,
evaluateXPathToMap
} from 'fontoxpath';
let documentNode;
beforeEach(() => {
documentNode = new slimdom.Document();
});
describe('preceding', () => {
it('returns the preceding nodes', () => {
jsonMlMapper.parse([
'someParentElement',
['someOtherElement', ['someOtherElement']],
['someElement']
], documentNode);
chai.assert.deepEqual(evaluateXPathToNodes('preceding::someOtherElement', documentNode.documentElement.lastChild), [
documentNode.documentElement.firstChild,
documentNode.documentElement.firstChild.firstChild
]);
});
it('returns all the preceding nodes', () => {
const result = evaluateXPathToMap(
`
let $dom := <element>
<uncle expectedPreceding="true">
<nephew expectedPreceding="true">
<nephew expectedPreceding="true"/>
</nephew>
</uncle>
<parent>
<sibling expectedPreceding="true">
<nephew expectedPreceding="true"/>
</sibling>
<self>
<child/>
</self>
<sibling>
<nephew/>
</sibling>
</parent>
<uncle>
<nephew>
<nephew/>
</nephew>
</uncle>
</element>
return map{
"got": array{$dom!descendant::self[1]!preceding::*},
"expected": array{$dom/descendant-or-self::*[@expectedPreceding]}
}
`,
documentNode,
null,
null,
{ language: 'XQuery3.1' }
);
chai.assert.equal(result.got.length, 5);
chai.assert.deepEqual(result.got, result.expected);
});
it('does not return non-matching preceding nodes', () => {
jsonMlMapper.parse([
'someParentElement',
['someNonMatchingElement', ['someNonMatchingElement']],
['someElement']
], documentNode);
chai.assert.deepEqual(evaluateXPathToNodes('preceding::someSiblingElement', documentNode.documentElement.lastChild), []);
});
it('does nothing when there are no preceding siblings', () => {
jsonMlMapper.parse([
'someParentElement',
['someElement']
], documentNode);
chai.assert.deepEqual(evaluateXPathToNodes('preceding::someSiblingElement', documentNode.documentElement.firstChild), []);
});
it('throws the correct error if context is absent', () => {
chai.assert.throws(() => evaluateXPathToNodes('preceding::*', null), 'XPDY0002');
});
});