• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const assert = require('assert');
4const HTML = require('../lib/common/html');
5const FormattingElementList = require('../lib/parser/formatting-element-list');
6const { generateTestsForEachTreeAdapter } = require('../../../test/utils/common');
7
8//Aliases
9const $ = HTML.TAG_NAMES;
10const NS = HTML.NAMESPACES;
11
12generateTestsForEachTreeAdapter(module.exports, (_test, treeAdapter) => {
13    _test['Insert marker'] = function() {
14        const list = new FormattingElementList(treeAdapter);
15
16        list.insertMarker();
17        assert.strictEqual(list.length, 1);
18        assert.strictEqual(list.entries[0].type, FormattingElementList.MARKER_ENTRY);
19
20        list.insertMarker();
21        assert.strictEqual(list.length, 2);
22        assert.strictEqual(list.entries[1].type, FormattingElementList.MARKER_ENTRY);
23    };
24
25    _test['Push element'] = function() {
26        const list = new FormattingElementList(treeAdapter);
27        const element1Token = 'token1';
28        const element2Token = 'token2';
29        const element1 = treeAdapter.createElement($.DIV, NS.HTML, []);
30        const element2 = treeAdapter.createElement($.P, NS.HTML, []);
31
32        list.pushElement(element1, element1Token);
33        assert.strictEqual(list.length, 1);
34        assert.strictEqual(list.entries[0].type, FormattingElementList.ELEMENT_ENTRY);
35        assert.strictEqual(list.entries[0].element, element1);
36        assert.strictEqual(list.entries[0].token, element1Token);
37
38        list.pushElement(element2, element2Token);
39        assert.strictEqual(list.length, 2);
40        assert.strictEqual(list.entries[1].type, FormattingElementList.ELEMENT_ENTRY);
41        assert.strictEqual(list.entries[1].element, element2);
42        assert.strictEqual(list.entries[1].token, element2Token);
43    };
44
45    _test['Insert element after bookmark'] = function() {
46        const list = new FormattingElementList(treeAdapter);
47        const token = 'token1';
48        const element1 = treeAdapter.createElement($.DIV, NS.HTML, []);
49        const element2 = treeAdapter.createElement($.P, NS.HTML, []);
50        const element3 = treeAdapter.createElement($.SPAN, NS.HTML, []);
51        const element4 = treeAdapter.createElement($.TITLE, NS.HTML, []);
52
53        list.pushElement(element1, token);
54        list.bookmark = list.entries[0];
55
56        list.pushElement(element2, token);
57        list.pushElement(element3, token);
58
59        list.insertElementAfterBookmark(element4, token);
60
61        assert.strictEqual(list.length, 4);
62        assert.strictEqual(list.entries[1].element, element4);
63    };
64
65    _test['Push element - Noah Ark condition'] = function() {
66        const list = new FormattingElementList(treeAdapter);
67        const token1 = 'token1';
68        const token2 = 'token2';
69        const token3 = 'token3';
70        const token4 = 'token4';
71        const token5 = 'token5';
72        const token6 = 'token6';
73
74        const element1 = treeAdapter.createElement($.DIV, NS.HTML, [
75            { name: 'attr1', value: 'val1' },
76            { name: 'attr2', value: 'val2' }
77        ]);
78
79        const element2 = treeAdapter.createElement($.DIV, NS.HTML, [
80            { name: 'attr1', value: 'val1' },
81            { name: 'attr2', value: 'someOtherValue' }
82        ]);
83
84        list.pushElement(element1, token1);
85        list.pushElement(element1, token2);
86        list.pushElement(element2, token3);
87        list.pushElement(element1, token4);
88
89        assert.strictEqual(list.length, 4);
90        assert.strictEqual(list.entries[0].token, token1);
91        assert.strictEqual(list.entries[1].token, token2);
92        assert.strictEqual(list.entries[2].token, token3);
93        assert.strictEqual(list.entries[3].token, token4);
94
95        list.pushElement(element1, token5);
96
97        assert.strictEqual(list.length, 4);
98        assert.strictEqual(list.entries[0].token, token2);
99        assert.strictEqual(list.entries[1].token, token3);
100        assert.strictEqual(list.entries[2].token, token4);
101        assert.strictEqual(list.entries[3].token, token5);
102
103        list.insertMarker();
104        list.pushElement(element1, token6);
105
106        assert.strictEqual(list.length, 6);
107        assert.strictEqual(list.entries[0].token, token2);
108        assert.strictEqual(list.entries[1].token, token3);
109        assert.strictEqual(list.entries[2].token, token4);
110        assert.strictEqual(list.entries[3].token, token5);
111        assert.strictEqual(list.entries[4].type, FormattingElementList.MARKER_ENTRY);
112        assert.strictEqual(list.entries[5].token, token6);
113    };
114
115    _test['Clear to the last marker'] = function() {
116        const list = new FormattingElementList(treeAdapter);
117        const token = 'token';
118
119        const element1 = treeAdapter.createElement($.DIV, NS.HTML, [
120            { name: 'attr1', value: 'val1' },
121            { name: 'attr2', value: 'val2' }
122        ]);
123
124        const element2 = treeAdapter.createElement($.DIV, NS.HTML, [
125            { name: 'attr1', value: 'val1' },
126            { name: 'attr2', value: 'someOtherValue' }
127        ]);
128
129        list.pushElement(element1, token);
130        list.pushElement(element2, token);
131        list.insertMarker();
132        list.pushElement(element1, token);
133        list.pushElement(element1, token);
134        list.pushElement(element2, token);
135
136        list.clearToLastMarker();
137
138        assert.strictEqual(list.length, 2);
139    };
140
141    _test['Remove entry'] = function() {
142        const list = new FormattingElementList(treeAdapter);
143        const token = 'token';
144
145        const element1 = treeAdapter.createElement($.DIV, NS.HTML, [
146            { name: 'attr1', value: 'val1' },
147            { name: 'attr2', value: 'val2' }
148        ]);
149
150        const element2 = treeAdapter.createElement($.DIV, NS.HTML, [
151            { name: 'attr1', value: 'val1' },
152            { name: 'attr2', value: 'someOtherValue' }
153        ]);
154
155        list.pushElement(element1, token);
156        list.pushElement(element2, token);
157        list.pushElement(element2, token);
158
159        list.removeEntry(list.entries[0]);
160
161        assert.strictEqual(list.length, 2);
162
163        for (let i = list.length - 1; i >= 0; i--) {
164            assert.notStrictEqual(list.entries[i].element, element1);
165        }
166    };
167
168    _test['Get entry in scope with given tag name'] = function() {
169        const list = new FormattingElementList(treeAdapter);
170        const token = 'token';
171        const element = treeAdapter.createElement($.DIV, NS.HTML, []);
172
173        assert.ok(!list.getElementEntryInScopeWithTagName($.DIV));
174
175        list.pushElement(element, token);
176        list.pushElement(element, token);
177        assert.strictEqual(list.getElementEntryInScopeWithTagName($.DIV), list.entries[1]);
178
179        list.insertMarker();
180        assert.ok(!list.getElementEntryInScopeWithTagName($.DIV));
181
182        list.pushElement(element, token);
183        assert.strictEqual(list.getElementEntryInScopeWithTagName($.DIV), list.entries[3]);
184    };
185
186    _test['Get element entry'] = function() {
187        const list = new FormattingElementList(treeAdapter);
188        const token = 'token';
189        const element1 = treeAdapter.createElement($.DIV, NS.HTML, []);
190        const element2 = treeAdapter.createElement($.A, NS.HTML, []);
191
192        list.pushElement(element2, token);
193        list.pushElement(element1, token);
194        list.pushElement(element2, token);
195        list.insertMarker();
196
197        const entry = list.getElementEntry(element1);
198
199        assert.strictEqual(entry.type, FormattingElementList.ELEMENT_ENTRY);
200        assert.strictEqual(entry.token, token);
201        assert.strictEqual(entry.element, element1);
202    };
203});
204