als-document 0.6.11 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <script src="/node_modules/he/he.js"></script>
8
+ <title>Document</title>
9
+ </head>
10
+ <body>
11
+ <script type="module">
12
+ import test from './test.js'
13
+ window.test = test
14
+ // test()
15
+ </script>
16
+ <iframe src="html.html" frameborder="0" width="600" height="800" id="iframe" onload="test()"></iframe>
17
+ </body>
18
+ </html>
@@ -0,0 +1,129 @@
1
+ import accessIFrameContent from '../utils/iframe.js'
2
+ import { Document } from '../../document.mjs'
3
+ import SimpleTest from '../utils/als-simple-test/test.mjs'
4
+ import html from './html.js'
5
+ let iframe = accessIFrameContent('iframe')
6
+ let {describe,it,beforeEach,runTests,expect,delay} = SimpleTest
7
+ // import he from 'he';
8
+
9
+ export default function() {
10
+ // let html = iframe.documentElement.outerHTML
11
+ let $ = new Document(html)
12
+ // console.log(iframe.documentElement.outerHTML)
13
+ describe('Comparing DOM from document and Document', () => {
14
+ describe('Simple tests', () => {
15
+ it('Parsing h1',() => {
16
+ let h1 = iframe.querySelector('h1')
17
+ let $h1 = $.$('h1')
18
+ let inner = $h1.innerHTML.replace(/&[#]?(?:[a-z0-9]+|x[a-f0-9]+);/g,str => {
19
+ return he.decode(str)
20
+ })
21
+ expect(h1.innerHTML).equalTo(inner)
22
+ })
23
+
24
+ it('Check number of elements',() => {
25
+ let as = iframe.querySelectorAll('a[href]')
26
+ let $as = $.$$('a[href]')
27
+ expect(as.length).equalTo($as.length)
28
+ })
29
+ it('Check attributes',() => {
30
+ let inputs = iframe.querySelectorAll('input[type=hidden]')
31
+ let $inputs = $.$$('input[type=hidden]')
32
+ expect(inputs.length).equalTo($inputs.length)
33
+ })
34
+ it('Check attributes of a specific element', () => {
35
+ let img = iframe.querySelector('img');
36
+ let $img = $.$('img');
37
+ let src = img.getAttribute('src');
38
+ let alt = img.getAttribute('alt');
39
+ expect($img.attr('src')).equalTo(src);
40
+ expect($img.attr('alt')).equalTo(alt);
41
+ });
42
+ it('Check presence and value of "rel" attribute in link elements', () => {
43
+ let links = Array.from(iframe.querySelectorAll('link[rel]'));
44
+ links = links.filter(link => link.getAttribute('rel') !== 'preload')
45
+ let $links = $.$$('link[rel]');
46
+ expect(links.length).equalTo($links.length);
47
+
48
+ links.forEach((link, index) => {
49
+ let rel = link.getAttribute('rel');
50
+ expect($links[index].attr('rel')).equalTo(rel);
51
+ });
52
+ });
53
+ it('Check presence and value of "rel" attribute in link elements', () => {
54
+ let links = Array.from(iframe.querySelectorAll('link[rel]'));
55
+ links = links.filter(link => link.getAttribute('rel') !== 'preload')
56
+
57
+ let $links = $.$$('link[rel]');
58
+ expect(links.length).equalTo($links.length);
59
+
60
+ links.forEach((link, index) => {
61
+ let rel = link.getAttribute('rel');
62
+ expect($links[index].attr('rel')).equalTo(rel);
63
+ });
64
+ });
65
+ })
66
+
67
+ describe('Complex tests', () => {
68
+ it('Check correct handling of sibling selectors', () => {
69
+ let siblingElements = Array.from(iframe.querySelectorAll('h2 + p'));
70
+ let $siblingElements = $.$$('h2 + p');
71
+ expect(siblingElements.length).equalTo($siblingElements.length);
72
+
73
+ siblingElements.forEach((siblingElement, index) => {
74
+ let prevSiblingId = siblingElement.previousElementSibling.getAttribute('id');
75
+ let $prevSiblingId = $siblingElements[index].prev().attr('id');
76
+ expect(prevSiblingId).equalTo($prevSiblingId);
77
+ });
78
+ });
79
+
80
+ it('Check correct handling of attribute selectors with different conditions', () => {
81
+ let complexAttributeSelectors = Array.from(iframe.querySelectorAll('a[href^="http"][target="_blank"]'));
82
+ let $complexAttributeSelectors = $.$$('a[href^="http"][target="_blank"]');
83
+ expect(complexAttributeSelectors.length).equalTo($complexAttributeSelectors.length);
84
+
85
+ complexAttributeSelectors.forEach((complexAttributeSelector, index) => {
86
+ let href = complexAttributeSelector.getAttribute('href');
87
+ let $href = $complexAttributeSelectors[index].attr('href');
88
+ expect(href).equalTo($href);
89
+ });
90
+ });
91
+
92
+ it('Check correct handling of nested elements', () => {
93
+ let nestedDivs = Array.from(iframe.querySelectorAll('div > div'));
94
+ let $nestedDivs = $.$$('div > div');
95
+ expect(nestedDivs.length).equalTo($nestedDivs.length-1);
96
+ nestedDivs.forEach((nestedDiv, index) => {
97
+ let $nestedDiv = $nestedDivs[index]
98
+ let parentId = nestedDiv.parentElement.getAttribute('id');
99
+ let $parentId = $nestedDiv.parent.id;
100
+ expect(nestedDiv.attributes.length).equalTo(Object.keys(($nestedDiv.attribs)).length);
101
+ expect(parentId).equalTo($parentId);
102
+ });
103
+ });
104
+
105
+ it('Check correct handling of :nth-child pseudo-class', () => {
106
+ let elements = Array.from(iframe.querySelectorAll('ul li'));
107
+ let $elements = $.$$('ul li');
108
+ expect(elements.length).equalTo($elements.length);
109
+ elements.forEach((evenLiElement, index) => {
110
+ let liText = evenLiElement.textContent.trim();
111
+ let $liText = $elements[index].innerText.trim();
112
+ while(/\s\s/.test(liText)) {
113
+ liText = liText.replace(/\n|\s\s/g,' ')
114
+ }
115
+ while(/\s\s/.test($liText)) {
116
+ $liText = $liText.replace(/\n|\s\s/g,' ')
117
+ }
118
+ $liText = $liText.replace(/&[#]?(?:[a-z0-9]+|x[a-f0-9]+);/g,str => {
119
+ return he.decode(str)
120
+ })
121
+
122
+ if(liText !== $liText) console.log({liText,$liText})
123
+ expect(liText).equalTo($liText);
124
+ });
125
+ });
126
+ })
127
+ })
128
+ runTests()
129
+ }
package/test/test.html ADDED
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <!-- <script src="/node_modules/als-document/document.js"></script> -->
8
+ <script type="module">
9
+ import test from './test.js'
10
+ test()
11
+ </script>
12
+ <title>Document</title>
13
+ </head>
14
+ <body>
15
+
16
+ </body>
17
+ </html>
package/test/test.js CHANGED
@@ -1,19 +1,15 @@
1
- let html1 = require('./html1')
2
- let html2 = require('./html2')
3
- let Test = require('als-test')
4
- let parser = require('../parser/test')
5
- let query = require('../query/test')
6
- let selector = require('../selector/test')
7
- let document = require('../document/test')
8
-
9
- async function run() {
10
- await Test.run('Document tests',[
11
- parser,
12
- query,
13
- selector,
14
- document
15
- ],{html1,html2})
16
-
17
- Test.summary()
18
- }
19
- run()
1
+ import SimpleTest from './utils/als-simple-test/test.mjs'
2
+ import documentTest from './tests/document-test.js'
3
+ import parserTest from './tests/parser-test.js'
4
+ import queryTest from './tests/query-test.js'
5
+ import selectorTest from './tests/selector-test.js'
6
+ SimpleTest.showFullError=true
7
+ export default function() {
8
+ documentTest()
9
+ parserTest()
10
+ queryTest()
11
+ selectorTest()
12
+ SimpleTest.runTests()
13
+ // console.log(SimpleTest.failed.length,SimpleTest.errors.length)
14
+ // console.log(SimpleTest.results)
15
+ }
@@ -0,0 +1,402 @@
1
+ import { Document } from '../../document.mjs'
2
+ import SimpleTest from '../utils/als-simple-test/test.mjs'
3
+ let {describe,it,beforeEach,runTests,expect,delay} = SimpleTest
4
+ import html1 from '../data/html1.js'
5
+ import html2 from '../data/html2.js'
6
+
7
+ export default function () {
8
+ describe('Document tests',() => {
9
+ let $ = new Document(html1)
10
+ let $2 = new Document(html2)
11
+ let code1 = /*html*/`<div id="test-id"><span>test</span>Hello world<span>test 2</span></div>`
12
+ describe('Insert tests',() => {
13
+
14
+ it('Check selector',() => {
15
+ expect($.$('div').index).equalTo(14)
16
+ })
17
+
18
+ it('insert before',() => {
19
+ let div = $.$('div')
20
+ div.insert(code1,0)
21
+ expect($.$('div').id).equalTo('test-id')
22
+ })
23
+
24
+ it('Check if new element, includes it\'s children',() => {
25
+ expect($.$('div').children[1].text).equalTo('Hello world')
26
+ })
27
+ it('Check if new element, includes it\'s children',() => {
28
+ expect($.$('div').children[0].innerHTML).equalTo('test')
29
+ })
30
+
31
+ it('Move existing element',() => {
32
+ let div = $.$('div')
33
+ let script = $.$('script')
34
+ script.insert(div,0)
35
+ expect(script.prev.id).equalTo('test-id')
36
+ })
37
+
38
+ it('Insert after',() => {
39
+ let div = $.$('div')
40
+ let script = $.$('script')
41
+ script.insert(div,3)
42
+ expect(script.next.id).equalTo('test-id')
43
+ })
44
+
45
+ it('Insert as first child',() => {
46
+ let script = $.$('script')
47
+ let div = $.$('div')
48
+ div.insert(script,1)
49
+ expect(div.children[0].tag).equalTo('script')
50
+ })
51
+
52
+ it('Insert as last child',() => {
53
+ let span = $.$('span')
54
+ let script = $.$('script')
55
+ span.insert(script,2)
56
+ expect(span.children[1].tag).equalTo('script')
57
+ })
58
+ it('setter before - number of parent children',() => {
59
+ let childrenLength = $.$('.headers').children.length
60
+ $.$('.headers>label').before = '<div id="test1">Test</div>'
61
+ expect($.$('.headers').children.length).above(childrenLength)
62
+ })
63
+
64
+ it('setter before - index of element',() => {
65
+ let el = $.$('.headers')
66
+ let index = el.index
67
+ let next = el.next
68
+ el.before = next
69
+ expect(el.prev.index).equalTo(index)
70
+ })
71
+
72
+ it('setter after - number of parent children',() => {
73
+ let parentsChildrenN = $.$('.headers').children.length
74
+ $.$('.headers>label').after = '<div id="test2">Test</div>'
75
+ expect($.$('.headers').children.length).above(parentsChildrenN)
76
+ })
77
+
78
+ it('setter after - index of element',() => {
79
+ let index = $.$('.headers').endIndex+1
80
+ let el = $.$('.headers')
81
+ let el2 = el.prev
82
+ el.after = el2
83
+ expect(el2.index + el2.elements.length).equalTo(index)
84
+ })
85
+
86
+ it('first - childIndex',() => {
87
+ let tabs = $.$('.tabs')
88
+ let child = tabs.children[1]
89
+ tabs.first = child
90
+ expect(child.childIndex).equalTo(0)
91
+ })
92
+
93
+ it('first - index',() => {
94
+ let index = $.$('.tabs').children[0].index
95
+ let tabs = $.$('.tabs')
96
+ let child = tabs.children[2]
97
+ tabs.first = child
98
+ expect(child.index).equalTo(index)
99
+ })
100
+
101
+ it('last - childIndex',() => {
102
+ let tabs = $.$('.tabs')
103
+ let last = tabs.children.length-1
104
+ let lastChildIndex = tabs.children[last].childIndex
105
+ let child = tabs.children[0]
106
+ tabs.last = child
107
+ expect(child.childIndex).equalTo(lastChildIndex)
108
+ })
109
+
110
+ it('last - index',() => {
111
+ let tabs = $.$('.tabs')
112
+ let last = tabs.children.length-1
113
+ let expected = tabs.children[last].index
114
+ let child = tabs.children[0]
115
+ let gap = child.elements.length
116
+ tabs.last = child
117
+ expect(child.index-gap).equalTo(expected)
118
+ })
119
+ })
120
+
121
+ describe('Attributes action tests',() => {
122
+ it('Get element\'s style property',() => {
123
+ expect($.$('[style*="display"]').style.display).equalTo('none')
124
+ })
125
+
126
+ it('Change element\'s style property',() => {
127
+ let element = $.$('[style*="display"]')
128
+ element.style.display = 'block'
129
+ expect(element.style.display).equalTo('block')
130
+ })
131
+
132
+ it('Changins element\'s style, change attribs.style too',() => {
133
+ let element = $.$('[style*="display"]')
134
+ element.style.display = 'flex'
135
+ expect(element.attribs.style).includes('flex')
136
+ })
137
+
138
+ it('Deleting element\'s style property',() => {
139
+ let element = $.$('[style*="display"]')
140
+ delete element.style.display
141
+ expect(element.style.display).isNot().defined()
142
+ })
143
+
144
+ it('Deleting element\'s style, change attribs.style too',() => {
145
+ let element = $.$('[style*="background-color"]')
146
+ delete element.style.backgroundColor
147
+ expect(element.attribs.style).isNot().includes('background-color')
148
+ })
149
+ })
150
+
151
+ describe('Classlist action tests',() => {
152
+ it('add class',() => {
153
+ let el = $.$('.tabs')
154
+ el.classList.add('super')
155
+ expect(el.classList).includes('super')
156
+ })
157
+
158
+ it('Adding class, changing attribs.class too',() => {
159
+ let el = $.$('.tabs')
160
+ el.classList.add('puper')
161
+ expect(el.attribs.class).includes('puper')
162
+ })
163
+
164
+ it('remove class',() => {
165
+ let el = $.$('.tabs')
166
+ el.classList.remove('super')
167
+ expect(el.classList).isNot().includes('super')
168
+
169
+ })
170
+ it('Removing class, removing class in attribs.class too',() => {
171
+ let el = $.$('.tabs')
172
+ el.classList.remove('puper')
173
+ expect(el.attribs.class).isNot().includes('puper')
174
+
175
+ })
176
+ it('Test id getter',() => {
177
+ expect($.$('[id]').id).equalTo('test-id')
178
+ })
179
+ it('Test id setter',() => {
180
+ $.$('[id]').id = 'another-one'
181
+ expect($.$('[id]').id).equalTo('another-one')
182
+ })
183
+ })
184
+
185
+ describe('Test attributes',() => {
186
+ it('Get attribute',() => {
187
+ expect($.$('input').attribs.type).equalTo($.$('input').attr('type'))
188
+ })
189
+
190
+ it('Set attribute',() => {
191
+ let before = $.$('input').attribs.name
192
+ let input = $.$('input')
193
+ input.attr('name','name-changed')
194
+ expect(input.attr('name')).isNot().equalTo(before)
195
+ })
196
+
197
+ it('Remove attribute',() => {
198
+ let before = Object.keys($.$('input').attribs).includes('checked')
199
+ let input = $.$('input')
200
+ input.attr('checked',null)
201
+ expect(Object.keys($.$('input').attribs).includes('checked')).isNot().equalTo(before)
202
+ })
203
+
204
+ it('Style not changable',() => {
205
+ let before = $.$('[style]').attr('style')
206
+ $.$('[style]').attr('style','style has changed')
207
+ expect($.$('[style]').attr('style')).equalTo(before)
208
+ })
209
+
210
+ it('Id not changable',() => {
211
+ let before = $.$('[id]').attr('id')
212
+ $.$('[id]').attr('id','id has changed')
213
+ expect($.$('[id]').attr('id')).equalTo(before)
214
+ })
215
+
216
+ it('Class not removable',() => {
217
+ let expected = $.$('[class]').attr('class')
218
+ $.$('[class]').attr('class',null)
219
+ expect($.$('[class]').attr('class')).equalTo(expected)
220
+ })
221
+
222
+ it('style not removable',() => {
223
+ let expected = $.$('[style]').attr('style')
224
+ $.$('[style]').attr('style',null)
225
+ expect($.$('[style]').attr('style')).equalTo(expected)
226
+ })
227
+
228
+ it('id not removable',() => {
229
+ let expected = $.$('[id]').attr('id')
230
+ $.$('[id]').attr('id',null)
231
+ expect($.$('[id]').attr('id')).equalTo(expected)
232
+ })
233
+
234
+ it('outerHTML changing then attribute changing',() => {
235
+ let expected = $.$('script').outerHTML.length
236
+ $.$('script').attr('src','some-test')
237
+ expect($.$('script').outerHTML.length).isNot().equalTo(expected)
238
+ })
239
+
240
+ it('remove element - children length',() => {
241
+ let expected = $.$('input').parent.children.length
242
+ let el = $.$('input')
243
+ let parent = el.parent
244
+ el.remove()
245
+ expect(parent.children.length).below(expected)
246
+ })
247
+
248
+ it('remove element - parent elements change',() => {
249
+ let expected = $.$('input').parent.elements.length
250
+ let el = $.$('input')
251
+ let count = el.elements.length
252
+ let parent = el.parent
253
+ el.remove()
254
+ expect(parent.elements.length+count).equalTo(expected)
255
+ })
256
+
257
+ it('remove element - innerHTML changed',() => {
258
+ let expected = $.$('input').parent.innerHTML.length
259
+ let el = $.$('input')
260
+ let parent = el.parent
261
+ el.remove()
262
+ expect(parent.innerHTML.length).below(expected)
263
+ })
264
+
265
+ it('Check selector',() => {
266
+ expect($2.$('div').index).equalTo(89)
267
+ })
268
+ })
269
+
270
+ describe('Insert tests',() => {
271
+ it('insert before',() => {
272
+ let div = $2.$('div')
273
+ div.insert(code1,0)
274
+ expect($2.$('div').id).equalTo('test-id')
275
+ })
276
+
277
+ it('Check if new element, includes it\'s children',() => {
278
+ expect($2.$('div').children[1].text).equalTo('Hello world')
279
+ })
280
+
281
+ it('Check if new element, includes it\'s children',() => {
282
+ expect($2.$('div').children[0].innerHTML).equalTo('test')
283
+ })
284
+
285
+ it('Move existing element',() => {
286
+ let div = $2.$('div')
287
+ let script = $2.$('script')
288
+ script.insert(div,0)
289
+ expect(script.prev.id).equalTo('test-id')
290
+ })
291
+
292
+ it('Insert after',() => {
293
+ let div = $2.$('div')
294
+ let script = $2.$('script')
295
+ script.insert(div,3)
296
+ expect(script.next.id).equalTo('test-id')
297
+ })
298
+
299
+ it('Insert as first child',() => {
300
+ let script = $2.$('script')
301
+ let div = $2.$('div')
302
+ div.insert(script,1)
303
+ expect(div.children[0].tag).equalTo('script')
304
+ })
305
+
306
+ it('Insert as last child',() => {
307
+ let span = $2.$('span')
308
+ let script = $2.$('script')
309
+ span.insert(script,2)
310
+ expect(span.children[1].tag).equalTo('script')
311
+ })
312
+
313
+ it('setter before - number of parent children',() => {
314
+ const expected = $2.$('#container').children.length
315
+ $2.$('#container>#navbarDropMenu').before = '<div id="test1">Test</div>'
316
+ expect($2.$('#container').children.length).above(expected)
317
+ })
318
+
319
+ it('setter before - index of element',() => {
320
+ const expected = $2.$('#container').index
321
+ let el = $2.$('#container')
322
+ let next = el.next
323
+ el.before = next
324
+ expect(el.prev.index).equalTo(expected)
325
+ })
326
+
327
+ it('setter after - number of parent children',() => {
328
+ const expected = $2.$('#container').children.length
329
+ $2.$('#container>#navbarDropMenu').after = '<div id="test2">Test</div>'
330
+ expect($2.$('#container').children.length).above(expected)
331
+ })
332
+
333
+ it('setter after - index of element',() => {
334
+ const expected = $2.$('#container').endIndex+1
335
+ let el = $2.$('#container')
336
+ let el2 = el.prev
337
+ el.after = el2
338
+ expect(el2.index + el2.elements.length).equalTo(expected)
339
+ })
340
+ it('first - childIndex',() => {
341
+ let tabs = $2.$('#container')
342
+ let child = tabs.children[1]
343
+ tabs.first = child
344
+ expect(child.childIndex).equalTo(0)
345
+ })
346
+
347
+ it('first - index',() => {
348
+ const expected = $2.$('#container').children[0].index
349
+ let tabs = $2.$('#container')
350
+ let child = tabs.children[2]
351
+ tabs.first = child
352
+ expect(child.index).equalTo(expected)
353
+ })
354
+
355
+ it('last - childIndex',() => {
356
+ let tabs = $2.$('#container')
357
+ let last = tabs.children.length-1
358
+ const expected = tabs.children[last].childIndex
359
+ let child = tabs.children[0]
360
+ tabs.last = child
361
+ expect(child.childIndex).equalTo(expected)
362
+ })
363
+ it('last - index',() => {
364
+ let tabs = $2.$('#container')
365
+ let last = tabs.children.length-1
366
+ const expected = tabs.children[last].index
367
+ last = tabs.children[tabs.children.length-1]
368
+ let child = tabs.children[0]
369
+ tabs.last = child
370
+ expect(child.index+child.elements.length-last.elements.length).equalTo(expected)
371
+ })
372
+
373
+
374
+ it('remove element - children length',() => {
375
+ const expected = $2.$('.w3-bar-item').parent.children.length
376
+ let el = $2.$('.w3-bar-item')
377
+ let parent = el.parent
378
+ el.remove()
379
+ expect(parent.children.length).below(expected)
380
+
381
+ })
382
+
383
+ it('remove element - parent elements change',() => {
384
+ const expected = $2.$('.w3-bar-item').parent.elements.length
385
+ let el = $2.$('.w3-bar-item')
386
+ let count = el.elements.length
387
+ let parent = el.parent
388
+ el.remove()
389
+ expect(parent.elements.length+count).equalTo(expected)
390
+
391
+ })
392
+
393
+ it('remove element - innerHTML changed',() => {
394
+ const expected = $2.$('.w3-bar-item').parent.innerHTML.length
395
+ let el = $2.$('.w3-bar-item')
396
+ let parent = el.parent
397
+ el.remove()
398
+ expect(parent.innerHTML.length).below(expected)
399
+ })
400
+ })
401
+ })
402
+ }