Skip to content
Navigation Menu
{{ message }}
forked from AdguardTeam/Scriptlets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-node-text.test.js
More file actions
342 lines (269 loc) · 11.9 KB
/
Copy pathremove-node-text.test.js
File metadata and controls
342 lines (269 loc) · 11.9 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* eslint-disable no-underscore-dangle, max-len */
import { runScriptlet, clearGlobalProps } from '../helpers';
const { test, module } = QUnit;
const name = 'remove-node-text';
const elements = [];
const beforeEach = () => {
window.__debug = () => {
window.hit = 'FIRED';
};
};
const afterEach = () => {
elements.forEach((element) => element.remove());
clearGlobalProps('hit', '__debug');
};
module(name, { beforeEach, afterEach });
/**
* Adds a node to the document body with className if provided.
* @param {string} nodeName - the type of node to create
* @param {string} textContent - the text content of the node
* @param {string} className - the class name of the node, optional
* @returns {HTMLElement} - the created node
*/
const addNode = (nodeName, textContent, className) => {
const node = document.createElement(nodeName);
node.textContent = textContent;
if (className) {
node.className = className;
}
elements.push(node);
document.body.appendChild(node);
return node;
};
/**
* Appends multiple child elements to a parent element.
* @param {HTMLElement} parent - the parent element
* @param {HTMLElement} children - the child elements to append
*/
const appendChildren = (parent, ...children) => {
children.forEach((child) => parent.appendChild(child));
};
/**
* Creates a div element with the specified class name.
* @param {*} className - the class name of the div
* @returns {HTMLDivElement} - the created div element
*/
const createDiv = (className) => {
const div = document.createElement('div');
div.className = className;
return div;
};
/**
* Creates a text node with the specified content
* @param {string} text - the text content of the node
* @returns {HTMLDivElement} - the created text node
*/
const createTextNode = (text) => {
return document.createTextNode(text);
};
test('Checking if alias name works', (assert) => {
const adgParams = {
name,
engine: 'test',
verbose: true,
};
const uboParams = {
name: 'ubo-remove-node-text.js',
engine: 'test',
verbose: true,
};
const codeByAdgParams = window.scriptlets.invoke(adgParams);
const codeByUboParams = window.scriptlets.invoke(uboParams);
assert.strictEqual(codeByAdgParams, codeByUboParams, 'ubo name - ok');
});
test('removes matched #text in .content element after text node has been added', (assert) => {
const done = assert.async();
const targetText = 'Advert';
const safeText = 'Content';
runScriptlet(name, ['#text', targetText, '.content']);
const contentElement = createDiv('content');
const contentTextNode = createTextNode(safeText);
const advertTextNode = createTextNode(targetText);
appendChildren(document.body, contentElement);
setTimeout(() => {
appendChildren(contentElement, contentTextNode);
appendChildren(contentElement, advertTextNode);
}, 1);
setTimeout(() => {
assert.strictEqual(advertTextNode.nodeValue, '', 'Target text node should be removed');
assert.strictEqual(contentTextNode.nodeValue, safeText, 'Safe text node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 10);
});
test('case with text node with specified parent and similar text in other children.', (assert) => {
const done = assert.async();
const text = 'text';
const text1 = 'text1';
const text2 = 'text2';
runScriptlet(name, ['#text', text, '.container']);
const parentElement = createDiv('container');
const targetTextNode = createTextNode(text);
const child1 = createDiv('child1');
child1.textContent = text1;
const child2 = createDiv('child2');
child2.textContent = text2;
appendChildren(document.body, parentElement);
appendChildren(parentElement, targetTextNode, child1, child2);
setTimeout(() => {
assert.strictEqual(targetTextNode.nodeValue, '', 'text should be removed');
assert.strictEqual(child1.textContent, text1, 'non-matched node should not be affected');
assert.strictEqual(child2.textContent, text2, 'non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('removes matched #text in body after elements are added', (assert) => {
const done = assert.async();
const targetText = 'text1';
const safeText = 'text2';
// body > .safe-div > #node + a
const safeElement = createDiv('safe-div');
const safeTextNode = createTextNode(safeText);
const link = document.createElement('a');
appendChildren(safeElement, safeTextNode, link);
appendChildren(document.body, safeElement);
runScriptlet(name, ['#text', 'text', 'body']);
// body > #node
const targetTextNode = createTextNode(targetText);
appendChildren(document.body, targetTextNode);
assert.strictEqual(targetTextNode.nodeValue, targetText, 'Target text node should contain correct text');
assert.strictEqual(safeTextNode.nodeValue, safeText, 'Safe text node should contain correct text');
setTimeout(() => {
assert.strictEqual(targetTextNode.nodeValue, '', 'Target text node should be removed');
assert.strictEqual(safeTextNode.nodeValue, safeText, 'Safe text node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('case with parent selector option and #text node with rendering after scriptlet start', (assert) => {
const done = assert.async();
const text = 'content!1';
// Create text nodes
const textNode = createTextNode(text);
const secondTextNode = createTextNode(text);
// Create parent container with class 'container'
const parentElement = createDiv('container');
// Create a link element
const link = document.createElement('a');
link.href = 'link';
link.textContent = text;
runScriptlet(name, ['#text', 'content!', 'body']);
appendChildren(parentElement, secondTextNode, link);
appendChildren(document.body, parentElement, textNode);
assert.strictEqual(textNode.nodeValue, text, 'Text node should contain correct text');
assert.strictEqual(secondTextNode.nodeValue, text, 'Second text node should contain correct text');
setTimeout(() => {
assert.strictEqual(textNode.nodeValue, '', 'Text should be removed');
assert.strictEqual(secondTextNode.nodeValue, text, 'Non-matched node should not be affected');
assert.strictEqual(link.textContent, text, 'Non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('case when text node is changing after scriptlet is start', (assert) => {
const done = assert.async();
const initialText = 'test';
const matchingText = 'content!';
const updatedText = 'updated';
const targetParentElement = createDiv('container');
// body > .container
appendChildren(document.body, targetParentElement);
const targetElement = createDiv();
targetElement.textContent = initialText;
// body > .container > div (test)
appendChildren(targetParentElement, targetElement);
// start scriptlet
runScriptlet(name, ['div', matchingText, 'div.container']);
assert.strictEqual(targetElement.textContent, initialText, 'text node should not be removed as it does not match');
// .non-matching-container > div (content!)
const safeParentElement = createDiv('non-matching-container');
appendChildren(document.body, safeParentElement);
// change DOM, create div with other class
const safeElement = createDiv();
// body > .non-matching-container (content!)
appendChildren(safeParentElement, safeElement);
// change text to match in other div element
// .non-matching-container > div (content!)
safeElement.textContent = matchingText;
assert.strictEqual(safeElement.textContent, matchingText, 'text node should not be removed as parent does not match');
targetElement.textContent = updatedText;
assert.strictEqual(targetElement.textContent, updatedText, 'target element contains updated text, should not be removed');
// update targetTextNode node to match text
targetElement.textContent = matchingText;
setTimeout(() => {
// body > .container (content!)
// should be matched by scriptlet
assert.strictEqual(targetElement.textContent, '', 'text node should be removed as text now matches');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 100);
});
test('simple case', (assert) => {
const done = assert.async();
const text = 'content!1';
const nodeBefore = addNode('div', text);
const safeNodeBefore = addNode('a', text);
runScriptlet(name, ['div', 'content!']);
const nodeAfter = addNode('div', text);
const safeNodeAfter = addNode('span', text);
setTimeout(() => {
assert.strictEqual(nodeAfter.textContent, '', 'text should be removed');
assert.strictEqual(nodeBefore.textContent, '', 'text should be removed');
assert.strictEqual(safeNodeAfter.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(safeNodeBefore.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('nodeName matcher working as regexp', (assert) => {
const done = assert.async();
const text = 'content!2';
const nodeBefore = addNode('div', text);
const safeNodeBefore = addNode('a', text);
runScriptlet(name, ['/test/', 'content!']);
const nodeAfter = addNode('div', text);
const safeNodeAfter = addNode('span', text);
setTimeout(() => {
assert.strictEqual(nodeAfter.textContent, '', 'text should be removed');
assert.strictEqual(nodeBefore.textContent, '', 'text should be removed');
assert.strictEqual(safeNodeAfter.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(safeNodeBefore.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('textContent matcher working as regexp', (assert) => {
const done = assert.async();
const text = 'content!3';
const nodeBefore = addNode('div', text);
const safeNodeBefore = addNode('a', text);
runScriptlet(name, ['test', '/content!/']);
const nodeAfter = addNode('div', text);
const safeNodeAfter = addNode('span', text);
setTimeout(() => {
assert.strictEqual(nodeAfter.textContent, '', 'text should be removed');
assert.strictEqual(nodeBefore.textContent, '', 'text should be removed');
assert.strictEqual(safeNodeAfter.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(safeNodeBefore.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
test('both matchers working as regexes', (assert) => {
const done = assert.async();
const text = 'content!4';
const nodeBefore = addNode('div', text);
const safeNodeBefore = addNode('a', text);
runScriptlet(name, ['/test/', '/content!/']);
const nodeAfter = addNode('div', text);
const safeNodeAfter = addNode('span', text);
setTimeout(() => {
assert.strictEqual(nodeAfter.textContent, '', 'text should be removed');
assert.strictEqual(nodeBefore.textContent, '', 'text should be removed');
assert.strictEqual(safeNodeAfter.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(safeNodeBefore.textContent, text, 'non-matched node should not be affected');
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
done();
}, 1);
});
You can’t perform that action at this time.
