-
-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathstartIndex_spec.js
More file actions
117 lines (107 loc) · 4.1 KB
/
Copy pathstartIndex_spec.js
File metadata and controls
117 lines (107 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { XMLParser } from "../src/fxp.js";
describe("XMLParser", function () {
const xmlData = `<root><foo/><bar type="quux"/><bar type="bat"/><baz type="foo">FOO</baz></root>`;
const XML_METADATA = XMLParser.getMetaDataSymbol();
it("should support captureMetadata && !preserveOrder", function () {
const expected = {
root: {
[XML_METADATA]: { startIndex: 0 },
foo: '',
bar: [
{
[XML_METADATA]: { startIndex: 12 },
"@_type": 'quux'
},
{
[XML_METADATA]: { startIndex: 30 },
"@_type": 'bat'
},
],
baz: {
'@_type': 'foo',
'#text': 'FOO',
[XML_METADATA]: {startIndex: 47},
}
}
};
const parser = new XMLParser({ preserveOrder: false, ignoreAttributes: false, captureMetaData: true });
const result = parser.parse(xmlData);
// console.dir({ result, expected }, { depth: Infinity });
expect(result).toEqual(expected);
});
it("should support captureMetadata && preserveOrder", function () {
const expected = [
{
root: [
{ foo: [], [XML_METADATA]: { startIndex: 6 } },
{
bar: [],
':@': { "@_type": 'quux' },
[XML_METADATA]: { startIndex: 12 },
},
{
bar: [],
':@': { "@_type": 'bat' },
[XML_METADATA]: { startIndex: 30 },
},
{
baz: [{ '#text': 'FOO' }],
':@': { '@_type': 'foo' },
[XML_METADATA]: {startIndex: 47},
},
],
[XML_METADATA]: { startIndex: 0 },
}
];
const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, captureMetaData: true });
const result = parser.parse(xmlData);
// console.dir({ result, expected }, { depth: Infinity });
expect(result).toEqual(expected);
});
const xmlData2 = `<root><foo/><bar type="quux"/><bar type="bat"/><baz type="foo">FOO</baz><stop>This is a <b>stop</b> node.</stop><unpaired attr="1"></root>`;
it("should support captureMetadata && isArray && stopNodes && unpairedTags && updateTag", function () {
const expected = {
ROOT: {
[XML_METADATA]: { startIndex: 0 },
foo: [''],
bar: [
{
[XML_METADATA]: { startIndex: 12 },
"@_type": 'quux'
},
{
[XML_METADATA]: { startIndex: 30 },
"@_type": 'bat'
},
],
baz: {
'#text': 'FOO',
'@_type': 'foo',
[XML_METADATA]: { startIndex: 47 },
},
// no metadata on stop nodes.
stop: 'This is a <b>stop</b> node.',
unpaired: {
'@_attr': '1',
[XML_METADATA]: { startIndex: 112 },
}
}
};
const parser = new XMLParser({
preserveOrder: false, ignoreAttributes: false, captureMetaData: true,
isArray(tagName) {
return (tagName == 'foo');
},
stopNodes: [ 'root.stop' ], unpairedTags: ['unpaired'],
updateTag(tagName) {
if (tagName === 'root') {
tagName = 'ROOT';
}
return tagName;
},
});
const result = parser.parse(xmlData2);
// console.dir({ result, expected }, { depth: Infinity });
expect(result).toEqual(expected);
});
});