forked from xmldom/xmldom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-parser.test.js
84 lines (68 loc) · 2.64 KB
/
dom-parser.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
'use strict'
const { DOMParser } = require('../lib/dom-parser')
describe('DOMParser', () => {
describe('constructor', () => {
test('should store passed options.locator', () => {
const options = { locator: {} }
const it = new DOMParser(options)
// TODO: is there a simpler way to test this that doesn't involve invoking parseFromString?
it.parseFromString('<xml/>')
const expected = {
columnNumber: 1,
lineNumber: 1,
}
expect(options.locator).toStrictEqual(expected)
})
test('should store passed options.xmlns for default mime type', () => {
const options = { xmlns: { '': 'custom-default-ns' } }
const it = new DOMParser(options)
// TODO: is there a simpler way to test this that doesn't involve invoking parseFromString?
const actual = it.parseFromString('<xml/>')
expect(actual.toString()).toBe('<xml xmlns="custom-default-ns"/>')
})
test('should store and modify passed options.xmlns for html mime type', () => {
const options = { xmlns: { '': 'custom-default-ns' } }
const it = new DOMParser(options)
// TODO: is there a simpler way to test this that doesn't involve invoking parseFromString?
it.parseFromString('<xml/>', 'text/html')
expect(options.xmlns['']).toBe('http://www.w3.org/1999/xhtml')
})
})
describe('parseFromString', () => {
test('should use minimal entity map for default mime type', () => {
const XML = '<xml attr=""">< &</xml>'
const actual = new DOMParser().parseFromString(XML).toString()
expect(actual).toBe(XML)
})
test('should provide access to textContent and attribute values', () => {
// provides an executable example for https://github.com/xmldom/xmldom/issues/93
const XML = `
<pdf2xml producer="poppler" version="0.26.5">
<page number="1" position="absolute" top="0" left="0" height="1262" width="892">
<fontspec id="0" size="14" family="Times" color="#000000"/>
<text top="0" >first</text>
<text top="1" >second</text>
<text top="2" >last</text>
</page>
</pdf2xml>
`
/*
TODO: again this is the "simples and most readable way,
but it also means testing it over and over
*/
const document = new DOMParser().parseFromString(XML)
/*
FIXME: from here we are actually testing the Document/Element/Node API
maybe this should be split?
*/
const textTags = document.getElementsByTagName('text')
expect(textTags).toHaveLength(3)
const expectedText = ['first', 'second', 'last']
for (let i = 0; i < textTags.length; i++) {
const textTag = textTags[i]
expect(textTag.textContent).toBe(expectedText[i])
expect(textTag.getAttribute('top')).toBe(`${i}`)
}
})
})
})