-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathattributesSpec.test.ts
More file actions
150 lines (126 loc) · 4.12 KB
/
attributesSpec.test.ts
File metadata and controls
150 lines (126 loc) · 4.12 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { describe, expect, it } from 'vitest';
// import he from 'he';
import { parse } from '../parse.ts';
const encoder = new TextEncoder();
describe('XMLParser', () => {
it('should parse attributes with valid names', () => {
const xmlData = encoder.encode(
`<issue _ent-ity.23="Mjg2MzY2OTkyNA==" state="partial" version="1"></issue>`,
);
const expected = {
issue: {
'_ent-ity.23': 'Mjg2MzY2OTkyNA==',
state: 'partial',
version: 1,
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
});
expect(result).toStrictEqual(expected);
//result = validator.validate(xmlData);
//expect(result).toBe(true);
});
it('should parse attributes with newline char', () => {
const xmlData = encoder.encode(
`<element id="7" data="foo\nbar" bug="true"/>`,
);
const expected = {
element: {
id: 7,
data: 'foo bar',
bug: true,
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
});
expect(result).toStrictEqual(expected);
//result = validator.validate(xmlData);
//expect(result).toBe(true);
});
it('should not decode HTML entities / char by default', () => {
const xmlData = encoder.encode(
`<element id="7" data="foo\nbar" bug="foo&bar'"/>`,
);
const expected = {
element: {
id: 7,
data: 'foo bar',
bug: 'foo&bar'',
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
});
expect(result).toStrictEqual(expected);
});
it('should parse Boolean Attributes', () => {
const xmlData = encoder.encode(
`<element id="7" str="" data><selfclosing/><selfclosing /><selfclosingwith attr/></element>`,
);
const expected = {
element: {
id: 7,
str: '',
data: true,
selfclosing: ['', ''],
selfclosingwith: {
attr: true,
},
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
allowBooleanAttributes: true,
});
expect(result).toStrictEqual(expected);
// result = validator.validate(xmlData, {
// allowBooleanAttributes: true,
// });
});
it('should not remove xmlns when namespaces are not set to be ignored', () => {
const xmlData = encoder.encode(
`<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"></project>`,
);
const expected = {
project: {
xmlns: 'http://maven.apache.org/POM/4.0.0',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation':
'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd',
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
});
expect(result).toStrictEqual(expected);
// result = validator.validate(xmlData, {
// allowBooleanAttributes: true,
// });
});
it('should remove xmlns when namespaces are set to be ignored', () => {
const xmlData = encoder.encode(
`<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi-ns="http://www.w3.org/2001/XMLSchema-instance" xsi-ns:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"></project>`,
);
const expected = {
project: {
//"xmlns": "http://maven.apache.org/POM/4.0.0",
//"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
schemaLocation:
'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd',
},
};
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
ignoreAttributes: false,
ignoreNameSpace: true,
});
expect(result).toStrictEqual(expected);
});
});