-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathtext.js
More file actions
75 lines (71 loc) · 3.87 KB
/
text.js
File metadata and controls
75 lines (71 loc) · 3.87 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
const assert = require('../assert.js').for('Text');
const {parseHTML, DOMParser} = global[Symbol.for('linkedom')];
const {document} = parseHTML('<html><div></div></html>');
let node = document.querySelector('div');
let text = document.createTextNode('text');
node.innerHTML = '<p></p>';
document.documentElement.appendChild(node);
text.before(node.firstChild);
text.after(node.firstChild);
assert(node.toString(), '<div><p></p></div>', 'before after not affected');
node.firstChild.textContent = '<test>';
assert(node.toString(), '<div><p><test></p></div>', 'before after not affected');
node.firstChild.textContent = '';
assert(node.toString(), '<div><p></p></div>', 'before after not affected');
assert(text.isConnected, false, '!isConnected');
assert(text.parentElement, null, '!parentElement');
assert(node.contains(text), false, '!contains');
node.firstChild.appendChild(text);
assert(text.getRootNode(), document.documentElement, 'getRootNode as html');
assert(node.contains(text), true, 'contains');
assert(text.isConnected, true, 'isConnected');
assert(text.parentElement, node.firstChild, 'parentElement');
document.documentElement.cloneNode(true);
assert(node.toString(), '<div><p>text</p></div>', 'appended');
text.replaceWith(document.createComment('comment'));
assert(text.isConnected, false, '!isConnected');
assert(node.toString(), '<div><p><!--comment--></p></div>', 'replaceWith');
document.documentElement.cloneNode(true);
node.firstChild.firstChild.cloneNode(true);
node.firstChild.firstChild.cloneNode();
assert(node.firstChild.closest('p'), node.firstChild, 'closest(sameNode)');
assert(node.firstChild.closest('nope'), null, 'closest(nope)');
assert(!node.firstChild.firstChild.isEqualNode(text), true, 'isEqualNode');
assert(node.firstChild.firstChild.isEqualNode(node.firstChild.firstChild.cloneNode(true)), true, 'isEqualNode');
node.firstChild.removeChild(node.firstChild.firstChild);
assert(node.isEqualNode(node.cloneNode(true)), true, 'isEqualNode');
assert(node.toString(), '<div><p></p></div>', 'remove');
assert(text.isEqualNode(text.cloneNode(true)), true, 'isEqualNode');
assert(text.isSameNode(text), true, 'isSameNode');
assert(text.nodeValue, text.textContent, 'nodeValue');
assert(text.data, text.textContent, 'data');
assert(text.firstChild, null, 'firstChild');
assert(text.lastChild, null, 'lastChild');
assert(text.nextSibling, null, 'nextSibling');
assert(text.previousSibling, null, 'previousSibling');
assert(text.nextElementSibling, null, 'nextElementSibling');
assert(text.previousElementSibling, null, 'previousElementSibling');
text.normalize();
assert(node.hasChildNodes(), true, 'hasChildNodes');
assert(text.hasChildNodes(), false, '!hasChildNodes');
assert(text.getRootNode(), text, 'getRootNode as text');
assert(document.documentElement.parentElement, null, 'html.parentElement');
document.documentElement.insertBefore(text, null);
assert(document.documentElement.lastChild, text, 'insertBefore(node, null)');
node = document.createDocumentFragment();
node.prepend('a', document.createTextNode('b'), 'c');
assert(node.firstChild.nextElementSibling, null, 'nextElementSibling with text around');
assert(document.documentElement.insertBefore(node, node), node, 'insertBefore(node, node) works');
document.documentElement.insertBefore(node, text);
node.append('a', '');
node.normalize();
assert(node.childNodes.length, 1, 'normalize() empty text');
assert(text.nodeValue, 'text');
text.nodeValue = '';
assert(text.nodeValue, '');
const jsxDocument = (new DOMParser).parseFromString('<html><!-- <> --><div foo={bar}><></div><img className="foo" src={getSrc()} />{eles.map(ele => (<div foo={ele.id}></div>))}</html>', 'text/jsx+xml');
assert(
jsxDocument.getRootNode().toString(),
'<html><!-- <> --><div foo={bar}><></div><img className="foo" src={getSrc()} />{eles.map(ele => (<div foo={ele.id} />))}</html>',
'Must resemble JSX original form, no escaping, allow for JSON attributes'
);