forked from xmldom/xmldom
/
node.test.js
222 lines (182 loc) · 5.81 KB
/
node.test.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict'
const { Node } = require('../../lib/dom')
const { DOMParser } = require('../../lib/dom-parser')
const expectNeighbours = (first, second, ...nodes) => {
expect(first.nextSibling).toStrictEqual(second)
expect(second.previousSibling).toStrictEqual(first)
expect(first.parentNode).toStrictEqual(second.parentNode)
if (nodes.length > 0) {
expectNeighbours(second, ...nodes)
}
}
describe('XML Node Parse', () => {
it('element', () => {
const dom = new DOMParser().parseFromString('<xml><child/></xml>')
expect(dom.documentElement.nodeType).toStrictEqual(Node.ELEMENT_NODE)
expect(dom.documentElement.firstChild.nodeType).toStrictEqual(
Node.ELEMENT_NODE
)
expect(dom).toMatchObject({
childNodes: {
length: 1,
},
documentElement: {
childNodes: {
length: 1,
},
firstChild: {
nodeName: 'child',
tagName: 'child',
},
nodeName: 'xml',
tagName: 'xml',
},
})
})
it('text', () => {
const { firstChild } = new DOMParser().parseFromString(
'<xml>start center end</xml>'
).documentElement
expect(firstChild.nodeType).toStrictEqual(Node.TEXT_NODE)
expect(firstChild).toMatchObject({
data: 'start center end',
nextSibling: null,
})
})
it('cdata', () => {
const { documentElement } = new DOMParser().parseFromString(
'<xml>start <![CDATA[<encoded>]]> end<![CDATA[[[[[[[[[]]]]]]]]]]></xml>'
)
expect(documentElement.firstChild).toMatchObject({
data: 'start ',
nextSibling: {
data: '<encoded>',
nextSibling: {
nextSibling: {
data: '[[[[[[[[]]]]]]]]',
},
},
},
})
})
it('cdata empty', () => {
const { documentElement } = new DOMParser().parseFromString(
'<xml><![CDATA[]]>start <![CDATA[]]> end</xml>'
)
expect(documentElement).toMatchObject({
textContent: 'start end',
})
})
it('comment', () => {
const { documentElement } = new DOMParser().parseFromString(
'<xml><!-- comment&>< --></xml>'
)
expect(documentElement.firstChild).toMatchObject({
nodeValue: ' comment&>< ',
})
})
it('prefixed without empty namespace', () => {
const source = '<w:p><w:r>test1</w:r><w:r>test2</w:r></w:p>'
const { documentElement } = new DOMParser().parseFromString(source)
expect(documentElement.firstChild.firstChild).toMatchObject({
nodeValue: 'test1',
})
expect(documentElement.lastChild.firstChild).toMatchObject({
nodeValue: 'test2',
})
expect(documentElement.toString()).toStrictEqual(source)
})
it('cdata comment', () => {
const { documentElement } = new DOMParser().parseFromString(
'<xml>start <![CDATA[<encoded>]]> <!-- comment -->end</xml>'
)
expect(documentElement.firstChild).toMatchObject({
nodeName: '#text', // 0
nodeValue: 'start ',
nextSibling: {
nodeName: '#cdata-section', // 1
nodeValue: '<encoded>',
nextSibling: {
nodeName: '#text', // 2
nodeValue: ' ',
nextSibling: {
nodeName: '#comment', // 3
nodeValue: ' comment ',
nextSibling: {
nodeName: '#text', // 4
nodeValue: 'end',
},
},
},
},
})
})
describe('appendChild', () => {
it('returns the argument', () => {
const dom = new DOMParser().parseFromString('<xml/>')
const child = dom.createElement('child')
expect(dom.documentElement.appendChild(child)).toStrictEqual(child)
})
it('appends as firstChild', () => {
const dom = new DOMParser().parseFromString('<xml/>')
const child = dom.createElement('child')
dom.documentElement.appendChild(child)
expect(dom.documentElement.firstChild).toStrictEqual(child)
})
it('subsequent calls append in order', () => {
const dom = new DOMParser().parseFromString('<xml />')
const fragment = dom.createDocumentFragment()
expect(fragment.nodeType).toStrictEqual(Node.DOCUMENT_FRAGMENT_NODE)
const first = fragment.appendChild(dom.createElement('first'))
const last = fragment.appendChild(dom.createElement('last'))
expectNeighbours(first, last)
expect(fragment.firstChild).toStrictEqual(first)
expect(fragment.lastChild).toStrictEqual(last)
})
})
describe('insertBefore', () => {
it('places created element before existing element', () => {
const dom = new DOMParser().parseFromString('<xml><child/></xml>')
const inserted = dom.createElement('sibling')
const child = dom.documentElement.firstChild
child.parentNode.insertBefore(inserted, child)
expectNeighbours(inserted, child)
})
it('inserts all elements in DocumentFragment', () => {
const dom = new DOMParser().parseFromString('<xml><child/></xml>')
const child = dom.documentElement.firstChild
const fragment = dom.createDocumentFragment()
const first = fragment.appendChild(dom.createElement('first'))
const second = fragment.appendChild(dom.createElement('second'))
child.parentNode.insertBefore(fragment, child)
expectNeighbours(first, second, child)
expect(child.parentNode.firstChild).toStrictEqual(first)
})
})
it('preserves instruction', () => {
const source =
'<?xml version="1.0"?><root><child>&<!-- & --></child></root>'
const actual = new DOMParser()
.parseFromString(source, 'text/xml')
.toString()
expect(actual).toStrictEqual(source)
})
it('preserves doctype with public id and sysid', () => {
const DOCTYPE =
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' +
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
const actual = new DOMParser()
.parseFromString(`${DOCTYPE}<html/>`, 'text/html')
.toString()
expect(actual).toStrictEqual(
`${DOCTYPE}<html xmlns="http://www.w3.org/1999/xhtml"></html>`
)
})
it('preserves doctype with sysid', () => {
const DOCTYPE = '<!DOCTYPE custom SYSTEM "custom.dtd">'
const actual = new DOMParser()
.parseFromString(`${DOCTYPE}<custom/>`, 'text/xml')
.toString()
expect(actual).toStrictEqual(`${DOCTYPE}<custom/>`)
})
})