This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathattributes.spec.js
129 lines (112 loc) · 5.51 KB
/
attributes.spec.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
import {parses} from '../helpers/testParse';
import {peeks} from '../helpers/testPeek';
describe('HtmlParser', () => {
describe('#readToken', () => {
it('parses double quoted attributes', parses('<img src="http://localhost/">', (tok, str) => {
expect(tok).to.have.property('tagName', 'img');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 29);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<img src="http://localhost/">');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('src', 'http://localhost/');
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.be.empty();
expect(str).to.equal('<img src="http://localhost/">');
}));
it('parses single quoted attributes', parses('<img src=\'http://localhost/\'>', (tok, str) => {
expect(tok).to.have.property('tagName', 'img');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 29);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<img src=\'http://localhost/\'>');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('src', 'http://localhost/');
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.be.empty();
expect(str).to.equal('<img src="http://localhost/">');
}));
it('parses unquoted attributes', parses('<img src=http://localhost/>', (tok, str) => {
expect(tok).to.have.property('tagName', 'img');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 27);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<img src=http://localhost/>');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('src', 'http://localhost/');
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.be.empty();
expect(str).to.equal('<img src="http://localhost/">');
}));
it('parses empty attributes', parses('<img src="">', (tok, str) => {
expect(tok).to.have.property('tagName', 'img');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 12);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<img src="">');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('src', '');
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.be.empty();
expect(str).to.equal('<img src="">');
}));
it('parses missing equal attributes', parses('<img src"">', (tok, str) => {
expect(tok).to.have.property('tagName', 'img');
expect(tok).to.have.property('rest', 'src""');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 11);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<img src"">');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.be.empty();
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.be.empty();
expect(str).to.equal('<img src"">');
}));
it('parses boolean attributes', parses('<input type="checkbox" checked>', (tok, str) => {
expect(tok).to.have.property('tagName', 'input');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', false);
expect(tok).to.have.property('length', 31);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<input type="checkbox" checked>');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('type', 'checkbox');
expect(tok).to.have.property('booleanAttrs');
expect(tok.booleanAttrs).to.have.property('checked', true);
expect(str).to.equal('<input type="checkbox" checked>');
}));
it('parses self closing tags', parses('<div class="foo"/>', (tok, str) => {
expect(tok).to.have.property('tagName', 'div');
expect(tok).to.have.property('rest', '');
expect(tok).to.have.property('unary', true);
expect(tok).to.have.property('length', 18);
expect(tok).to.have.property('type', 'startTag');
expect(tok).to.have.property('text', '<div class="foo"/>');
expect(tok).to.have.property('attrs');
expect(tok.attrs).to.have.property('class', 'foo');
expect(str).to.equal('<div class="foo"/>');
}));
it('parses escaped quotes', parses("<iframe onload='var s=\"\";'></iframe>", (tok, str) => {
expect(tok).to.have.property('tagName', 'iframe');
expect(tok.attrs).to.have.property('onload', 'var s="";');
expect(str).to.equal('<iframe onload="var s=\\"\\";"></iframe>');
}));
});
describe('#peek', () => {
const TAG = '<img/>';
it('does not mutate the stream', peeks(TAG, (token, parser) => {
expect(parser.rest()).to.equal(token.toString());
}));
it('shows the next token', peeks(TAG, (token) => {
expect(token.toString()).to.equal(TAG);
}));
it('returns undefined when there are no more tokens', peeks('', (token) => {
expect(token).to.be(undefined);
}));
});
});