/
section-test.js
163 lines (134 loc) · 5.1 KB
/
section-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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import PostNodeBuilder from 'mobiledoc-kit/models/post-node-builder';
import SectionParser from 'mobiledoc-kit/parsers/section';
import Helpers from '../../test-helpers';
const {module, test} = Helpers;
let builder, parser;
let buildDOM = Helpers.dom.fromHTML;
module('Unit: Parser: SectionParser', {
beforeEach() {
builder = new PostNodeBuilder();
parser = new SectionParser(builder);
},
afterEach() {
builder = null;
parser = null;
}
});
test('#parse parses simple dom', (assert) => {
let container = buildDOM('<p>hello there<b>i am bold</b><p>');
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 2, 'has 2 markers');
const [m1, m2] = section.markers.toArray();
assert.equal(m1.value, 'hello there');
assert.equal(m2.value, 'i am bold');
assert.ok(m2.hasMarkup('b'), 'm2 is bold');
});
test('#parse parses nested markups', (assert) => {
let container = buildDOM(`
<p><b>i am bold<i>i am bold and italic</i>i am bold again</b></p>
`);
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.markers.length, 3, 'has 3 markers');
const [m1, m2, m3] = section.markers.toArray();
assert.equal(m1.value, 'i am bold');
assert.equal(m2.value, 'i am bold and italic');
assert.equal(m3.value, 'i am bold again');
assert.ok(m1.hasMarkup('b'), 'm1 is bold');
assert.ok(m2.hasMarkup('b') && m2.hasMarkup('i'), 'm2 is bold and i');
assert.ok(m3.hasMarkup('b'), 'm3 is bold');
assert.ok(!m1.hasMarkup('i') && !m3.hasMarkup('i'), 'm1 and m3 are not i');
});
test('#parse ignores non-markup elements like spans', (assert) => {
let container = buildDOM(`
<p><span>i was in span</span></p>
`);
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 markers');
const [m1] = section.markers.toArray();
assert.equal(m1.value, 'i was in span');
});
test('#parse reads attributes', (assert) => {
let container = buildDOM(`
<p><a href="google.com">i am a link</a></p>
`);
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.markers.length, 1, 'has 1 markers');
const [m1] = section.markers.toArray();
assert.equal(m1.value, 'i am a link');
assert.ok(m1.hasMarkup('a'), 'has "a" markup');
assert.equal(m1.getMarkup('a').attributes.href, 'google.com');
});
test('#parse joins contiguous text nodes separated by non-markup elements', (assert) => {
let container = buildDOM(`
<p><span>span 1</span><span>span 2</span></p>
`);
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 marker');
const [m1] = section.markers.toArray();
assert.equal(m1.value, 'span 1span 2');
});
test('#parse turns a textNode into a section', (assert) => {
let container = buildDOM(`I am a text node`);
let element = container.firstChild;
const [section] = parser.parse(element);
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 marker');
const [m1] = section.markers.toArray();
assert.equal(m1.value, 'I am a text node');
});
test('#parse allows passing in parserPlugins that can override element parsing', (assert) => {
let container = buildDOM(`
<p>text 1<img src="http://placehold.it/100x100">text 2</p>
`);
let element = container.firstChild;
let plugins = [function(element, builder, {addSection}) {
if (element.tagName !== 'IMG') {
return;
}
let payload = {url: element.src};
let cardSection = builder.createCardSection('test-image', payload);
addSection(cardSection);
}];
parser = new SectionParser(builder, {plugins});
const sections = parser.parse(element);
assert.equal(sections.length, 3, '3 sections');
assert.equal(sections[0].text, 'text 1');
assert.equal(sections[2].text, 'text 2');
let cardSection = sections[1];
assert.equal(cardSection.name, 'test-image');
assert.deepEqual(cardSection.payload, {url: 'http://placehold.it/100x100'});
});
test('#parse allows passing in parserPlugins that can override text parsing', (assert) => {
let container = buildDOM(`
<p>text 1<img src="http://placehold.it/100x100">text 2</p>
`);
let element = container.firstChild;
let plugins = [function(element, builder, {addMarkerable, nodeFinished}) {
if (element.nodeType === 3) {
if (element.textContent === 'text 1') {
addMarkerable(builder.createMarker('oh my'));
}
nodeFinished();
}
}];
parser = new SectionParser(builder, {plugins});
const sections = parser.parse(element);
assert.equal(sections.length, 1, '1 section');
assert.equal(sections[0].text, 'oh my');
});
test('#parse skips STYLE nodes', (assert) => {
let element = buildDOM(`
<style>.rule { font-color: red; }</style>
`).firstChild;
parser = new SectionParser(builder);
let sections = parser.parse(element);
assert.equal(sections.length, 0, 'does not parse style');
});