-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhtml.js
148 lines (123 loc) · 5.24 KB
/
html.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
import inspect from '../lib/index.js'
import { expect } from 'chai'
const h = (name, attributes, ...children) => {
const container = document.createElement(name)
for (const key in attributes) {
container.setAttribute(key, attributes[key])
}
for (const i in children) {
container.appendChild(children[i])
}
return container
}
describe('HTMLElement', () => {
beforeEach(() => {
if (typeof HTMLElement !== 'function') {
class HTMLElement {
constructor(tagName) {
this.tagName = tagName.toUpperCase()
this.attributes = {}
this.children = []
}
appendChild(element) {
this.children.push(element)
}
getAttribute(name) {
return this.attributes[name]
}
setAttribute(name, value) {
this.attributes[name] = value
}
getAttributeNames() {
return Object.keys(this.attributes)
}
}
if (typeof global === 'undefined') {
window.document.createElement = tagName => new HTMLElement(tagName)
window.HTMLElement = HTMLElement
} else {
global.document = {}
global.document.createElement = tagName => new HTMLElement(tagName)
global.HTMLElement = HTMLElement
}
}
})
it('returns `<div></div>` for an empty div', () => {
expect(inspect(h('div'))).to.equal('<div></div>')
})
it('returns `<div id="foo"></div>` for a div with an id', () => {
expect(inspect(h('div', { id: 'foo' }))).to.equal('<div id="foo"></div>')
})
it('returns `<div id="foo" aria-live="foo" hidden></div>` for a div with an id', () => {
expect(inspect(h('div', { id: 'foo', 'aria-live': 'bar', hidden: '' }))).to.equal(
'<div id="foo" aria-live="bar" hidden></div>'
)
})
it('returns output including children', () => {
expect(
inspect(h('div', { id: 'foo', hidden: '' }, h('pre', {}, h('code', {}, h('span', { style: 'color:red' })))))
).to.equal('<div id="foo" hidden><pre><code><span style="color:red"></span></code></pre></div>')
})
describe('truncate', () => {
let template = null
beforeEach(() => {
template = h('div', { id: 'foo', hidden: '' }, h('pre', {}, h('code', {}, h('span', { style: 'color:red' }))))
})
it('returns the full representation when truncate is over string length', () => {
expect(inspect(template, { truncate: 100 })).to.equal(
'<div id="foo" hidden><pre><code><span style="color:red"></span></code></pre></div>'
)
})
it('truncates arguments values longer than truncate (81)', () => {
expect(inspect(template, { truncate: 81 })).to.equal(
'<div id="foo" hidden><pre><code><span …(1)></span></code></pre></div>'
)
})
it('truncates arguments values longer than truncate (78)', () => {
expect(inspect(template, { truncate: 78 })).to.equal(
'<div id="foo" hidden><pre><code><span …(1)></span></code></pre></div>'
)
})
it('truncates arguments values longer than truncate (77)', () => {
expect(inspect(template, { truncate: 77 })).to.equal('<div id="foo" hidden><pre><code>…(1)</code></pre></div>')
})
it('truncates arguments values longer than truncate (64)', () => {
expect(inspect(template, { truncate: 64 })).to.equal('<div id="foo" hidden><pre><code>…(1)</code></pre></div>')
})
it('truncates arguments values longer than truncate (63)', () => {
expect(inspect(template, { truncate: 63 })).to.equal('<div id="foo" hidden><pre>…(1)</pre></div>')
})
it('truncates arguments values longer than truncate (51)', () => {
expect(inspect(template, { truncate: 51 })).to.equal('<div id="foo" hidden><pre>…(1)</pre></div>')
})
it('truncates arguments values longer than truncate (49)', () => {
expect(inspect(template, { truncate: 49 })).to.equal('<div id="foo" hidden>…(1)</div>')
})
it('truncates arguments values longer than truncate (26)', () => {
expect(inspect(template, { truncate: 26 })).to.equal('<div id="foo" hidden>…(1)</div>')
})
it('truncates arguments values longer than truncate (25)', () => {
expect(inspect(template, { truncate: 25 })).to.equal('<div id="foo" …(1)>…(1)</div>')
})
it('truncates arguments values longer than truncate (24)', () => {
expect(inspect(template, { truncate: 24 })).to.equal('<div id="foo" …(1)>…(1)</div>')
})
it('truncates arguments values longer than truncate (23)', () => {
expect(inspect(template, { truncate: 23 })).to.equal('<div …(2)>…(1)</div>')
})
it('disregards truncate when it cannot truncate further (18)', () => {
expect(inspect(template, { truncate: 18 })).to.equal('<div …(2)>…(1)</div>')
})
it('disregards truncate when it cannot truncate further (1)', () => {
expect(inspect(template, { truncate: 1 })).to.equal('<div …(2)>…(1)</div>')
})
})
describe('colors', () => {
it('returns element as cyan, with attribute names in yellow and values as string colour', () => {
expect(inspect(h('div', { id: 'foo' }), { colors: true })).to.equal(
'\u001b[36m<div\u001b[39m \u001b[33mid\u001b[39m=\u001b[32m' +
'"foo"\u001b[39m\u001b[36m>\u001b[39m\u001b[36m</div>\u001b[39m'
)
})
})
})