@@ -19,14 +19,53 @@ const afterEach = () => {
19
19
20
20
module ( name , { beforeEach, afterEach } ) ;
21
21
22
- const addNode = ( nodeName , textContent ) => {
22
+ /**
23
+ * Adds a node to the document body with className if provided.
24
+ * @param {string } nodeName - the type of node to create
25
+ * @param {string } textContent - the text content of the node
26
+ * @param {string } className - the class name of the node, optional
27
+ * @returns {HTMLElement } - the created node
28
+ */
29
+ const addNode = ( nodeName , textContent , className ) => {
23
30
const node = document . createElement ( nodeName ) ;
24
31
node . textContent = textContent ;
32
+ if ( className ) {
33
+ node . className = className ;
34
+ }
25
35
elements . push ( node ) ;
26
36
document . body . appendChild ( node ) ;
27
37
return node ;
28
38
} ;
29
39
40
+ /**
41
+ * Appends multiple child elements to a parent element.
42
+ * @param {HTMLElement } parent - the parent element
43
+ * @param {HTMLElement } children - the child elements to append
44
+ */
45
+ const appendChildren = ( parent , ...children ) => {
46
+ children . forEach ( ( child ) => parent . appendChild ( child ) ) ;
47
+ } ;
48
+
49
+ /**
50
+ * Creates a div element with the specified class name.
51
+ * @param {* } className - the class name of the div
52
+ * @returns {HTMLDivElement } - the created div element
53
+ */
54
+ const createDiv = ( className ) => {
55
+ const div = document . createElement ( 'div' ) ;
56
+ div . className = className ;
57
+ return div ;
58
+ } ;
59
+
60
+ /**
61
+ * Creates a text node with the specified content
62
+ * @param {string } text - the text content of the node
63
+ * @returns {HTMLDivElement } - the created text node
64
+ */
65
+ const createTextNode = ( text ) => {
66
+ return document . createTextNode ( text ) ;
67
+ } ;
68
+
30
69
test ( 'Checking if alias name works' , ( assert ) => {
31
70
const adgParams = {
32
71
name,
@@ -45,6 +84,171 @@ test('Checking if alias name works', (assert) => {
45
84
assert . strictEqual ( codeByAdgParams , codeByUboParams , 'ubo name - ok' ) ;
46
85
} ) ;
47
86
87
+ test ( 'removes matched #text in .content element after text node has been added' , ( assert ) => {
88
+ const done = assert . async ( ) ;
89
+ const targetText = 'Advert' ;
90
+ const safeText = 'Content' ;
91
+
92
+ runScriptlet ( name , [ '#text' , targetText , '.content' ] ) ;
93
+
94
+ const contentElement = createDiv ( 'content' ) ;
95
+ const contentTextNode = createTextNode ( safeText ) ;
96
+ const advertTextNode = createTextNode ( targetText ) ;
97
+ appendChildren ( document . body , contentElement ) ;
98
+
99
+ setTimeout ( ( ) => {
100
+ appendChildren ( contentElement , contentTextNode ) ;
101
+ appendChildren ( contentElement , advertTextNode ) ;
102
+ } , 1 ) ;
103
+
104
+ setTimeout ( ( ) => {
105
+ assert . strictEqual ( advertTextNode . nodeValue , '' , 'Target text node should be removed' ) ;
106
+ assert . strictEqual ( contentTextNode . nodeValue , safeText , 'Safe text node should not be affected' ) ;
107
+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function should fire' ) ;
108
+ done ( ) ;
109
+ } , 10 ) ;
110
+ } ) ;
111
+
112
+ test ( 'case with text node with specified parent and similar text in other children.' , ( assert ) => {
113
+ const done = assert . async ( ) ;
114
+ const text = 'text' ;
115
+ const text1 = 'text1' ;
116
+ const text2 = 'text2' ;
117
+
118
+ runScriptlet ( name , [ '#text' , text , '.container' ] ) ;
119
+
120
+ const parentElement = createDiv ( 'container' ) ;
121
+
122
+ const targetTextNode = createTextNode ( text ) ;
123
+
124
+ const child1 = createDiv ( 'child1' ) ;
125
+ child1 . textContent = text1 ;
126
+
127
+ const child2 = createDiv ( 'child2' ) ;
128
+ child2 . textContent = text2 ;
129
+
130
+ appendChildren ( document . body , parentElement ) ;
131
+ appendChildren ( parentElement , targetTextNode , child1 , child2 ) ;
132
+
133
+ setTimeout ( ( ) => {
134
+ assert . strictEqual ( targetTextNode . nodeValue , '' , 'text should be removed' ) ;
135
+ assert . strictEqual ( child1 . textContent , text1 , 'non-matched node should not be affected' ) ;
136
+ assert . strictEqual ( child2 . textContent , text2 , 'non-matched node should not be affected' ) ;
137
+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function should fire' ) ;
138
+ done ( ) ;
139
+ } , 1 ) ;
140
+ } ) ;
141
+
142
+ test ( 'removes matched #text in body after elements are added' , ( assert ) => {
143
+ const done = assert . async ( ) ;
144
+ const targetText = 'text1' ;
145
+ const safeText = 'text2' ;
146
+ // body > .safe-div > #node + a
147
+ const safeElement = createDiv ( 'safe-div' ) ;
148
+ const safeTextNode = createTextNode ( safeText ) ;
149
+ const link = document . createElement ( 'a' ) ;
150
+ appendChildren ( safeElement , safeTextNode , link ) ;
151
+ appendChildren ( document . body , safeElement ) ;
152
+
153
+ runScriptlet ( name , [ '#text' , 'text' , 'body' ] ) ;
154
+
155
+ // body > #node
156
+ const targetTextNode = createTextNode ( targetText ) ;
157
+ appendChildren ( document . body , targetTextNode ) ;
158
+
159
+ assert . strictEqual ( targetTextNode . nodeValue , targetText , 'Target text node should contain correct text' ) ;
160
+ assert . strictEqual ( safeTextNode . nodeValue , safeText , 'Safe text node should contain correct text' ) ;
161
+
162
+ setTimeout ( ( ) => {
163
+ assert . strictEqual ( targetTextNode . nodeValue , '' , 'Target text node should be removed' ) ;
164
+ assert . strictEqual ( safeTextNode . nodeValue , safeText , 'Safe text node should not be affected' ) ;
165
+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function should fire' ) ;
166
+ done ( ) ;
167
+ } , 1 ) ;
168
+ } ) ;
169
+
170
+ test ( 'case with parent selector option and #text node with rendering after scriptlet start' , ( assert ) => {
171
+ const done = assert . async ( ) ;
172
+ const text = 'content!1' ;
173
+
174
+ // Create text nodes
175
+ const textNode = createTextNode ( text ) ;
176
+ const secondTextNode = createTextNode ( text ) ;
177
+
178
+ // Create parent container with class 'container'
179
+ const parentElement = createDiv ( 'container' ) ;
180
+
181
+ // Create a link element
182
+ const link = document . createElement ( 'a' ) ;
183
+ link . href = 'link' ;
184
+ link . textContent = text ;
185
+
186
+ runScriptlet ( name , [ '#text' , 'content!' , 'body' ] ) ;
187
+
188
+ appendChildren ( parentElement , secondTextNode , link ) ;
189
+
190
+ appendChildren ( document . body , parentElement , textNode ) ;
191
+
192
+ assert . strictEqual ( textNode . nodeValue , text , 'Text node should contain correct text' ) ;
193
+ assert . strictEqual ( secondTextNode . nodeValue , text , 'Second text node should contain correct text' ) ;
194
+
195
+ setTimeout ( ( ) => {
196
+ assert . strictEqual ( textNode . nodeValue , '' , 'Text should be removed' ) ;
197
+ assert . strictEqual ( secondTextNode . nodeValue , text , 'Non-matched node should not be affected' ) ;
198
+ assert . strictEqual ( link . textContent , text , 'Non-matched node should not be affected' ) ;
199
+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function should fire' ) ;
200
+ done ( ) ;
201
+ } , 1 ) ;
202
+ } ) ;
203
+
204
+ test ( 'case when text node is changing after scriptlet is start' , ( assert ) => {
205
+ const done = assert . async ( ) ;
206
+ const initialText = 'test' ;
207
+ const matchingText = 'content!' ;
208
+ const updatedText = 'updated' ;
209
+
210
+ const targetParentElement = createDiv ( 'container' ) ;
211
+ // body > .container
212
+ appendChildren ( document . body , targetParentElement ) ;
213
+
214
+ const targetElement = createDiv ( ) ;
215
+ targetElement . textContent = initialText ;
216
+
217
+ // body > .container > div (test)
218
+ appendChildren ( targetParentElement , targetElement ) ;
219
+
220
+ // start scriptlet
221
+ runScriptlet ( name , [ 'div' , matchingText , 'div.container' ] ) ;
222
+
223
+ assert . strictEqual ( targetElement . textContent , initialText , 'text node should not be removed as it does not match' ) ;
224
+
225
+ // .non-matching-container > div (content!)
226
+ const safeParentElement = createDiv ( 'non-matching-container' ) ;
227
+ appendChildren ( document . body , safeParentElement ) ;
228
+
229
+ // change DOM, create div with other class
230
+ const safeElement = createDiv ( ) ;
231
+ // body > .non-matching-container (content!)
232
+ appendChildren ( safeParentElement , safeElement ) ;
233
+
234
+ // change text to match in other div element
235
+ // .non-matching-container > div (content!)
236
+ safeElement . textContent = matchingText ;
237
+ assert . strictEqual ( safeElement . textContent , matchingText , 'text node should not be removed as parent does not match' ) ;
238
+ targetElement . textContent = updatedText ;
239
+ assert . strictEqual ( targetElement . textContent , updatedText , 'target element contains updated text, should not be removed' ) ;
240
+ // update targetTextNode node to match text
241
+ targetElement . textContent = matchingText ;
242
+
243
+ setTimeout ( ( ) => {
244
+ // body > .container (content!)
245
+ // should be matched by scriptlet
246
+ assert . strictEqual ( targetElement . textContent , '' , 'text node should be removed as text now matches' ) ;
247
+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function should fire' ) ;
248
+ done ( ) ;
249
+ } , 100 ) ;
250
+ } ) ;
251
+
48
252
test ( 'simple case' , ( assert ) => {
49
253
const done = assert . async ( ) ;
50
254
const text = 'content!1' ;
0 commit comments