OLD | NEW |
| (Empty) |
1 <html> | |
2 <link rel="import" href="../resources/chai.html" /> | |
3 <link rel="import" href="../resources/mocha.html" /> | |
4 <body id="body"> | |
5 <script> | |
6 document.body = document.getElementById("body"); | |
7 | |
8 describe('MutationObserver on character data', function() { | |
9 it('should handle basic aspects of characterData observation', function(done
) { | |
10 var observer; | |
11 var mutations; | |
12 var charDataNode; | |
13 | |
14 function start() { | |
15 var div = document.createElement('div'); | |
16 div.textContent = 'foo'; | |
17 charDataNode = div.firstChild; | |
18 observer = new MutationObserver(function(records) { | |
19 mutations = records; | |
20 }); | |
21 | |
22 observer.observe(charDataNode, {characterData: true}); | |
23 charDataNode.textContent = 'bar'; | |
24 setTimeout(checkDisconnectAndMutate, 0); | |
25 } | |
26 | |
27 function checkDisconnectAndMutate() { | |
28 // ...can characterData changes be observed at all | |
29 | |
30 assert.equal(mutations.length, 1); | |
31 assert.equal(mutations[0].type, "characterData"); | |
32 assert.equal(mutations[0].target, charDataNode); | |
33 | |
34 mutations = null; | |
35 observer.disconnect(); | |
36 charDataNode.textContent = 'baz'; | |
37 setTimeout(checkNotDeliveredAndMutateMultiple, 0); | |
38 } | |
39 | |
40 function checkNotDeliveredAndMutateMultiple() { | |
41 // ...observer.disconnect() should prevent further delivery of mutat
ions. | |
42 | |
43 assert.equal(mutations, null); | |
44 charDataNode = document.createTextNode(''); | |
45 observer.observe(charDataNode, { characterData: true }); | |
46 charDataNode.textContent = 'foo'; | |
47 charDataNode.textContent = 'bar'; | |
48 setTimeout(finish); | |
49 } | |
50 | |
51 function finish() { | |
52 // ...re-observing after disconnect works with the same observer. | |
53 | |
54 assert.equal(mutations.length, 2); | |
55 assert.equal(mutations[0].type, "characterData"); | |
56 assert.equal(mutations[0].target, charDataNode); | |
57 assert.equal(mutations[1].type, "characterData"); | |
58 assert.equal(mutations[1].target, charDataNode); | |
59 observer.disconnect(); | |
60 done(); | |
61 } | |
62 | |
63 start(); | |
64 }); | |
65 | |
66 it('should only notify of characterData changes when requested', function(do
ne) { | |
67 var observer; | |
68 var mutations; | |
69 | |
70 function start() { | |
71 var div = document.createElement('div'); | |
72 div.textContent = 'hello'; | |
73 var charDataNode = div.firstChild; | |
74 observer = new MutationObserver(function(records) { | |
75 mutations = records; | |
76 }); | |
77 | |
78 observer.observe(charDataNode, {childList: true, attributes: true}); | |
79 charDataNode = 'goodbye'; | |
80 setTimeout(finish, 0); | |
81 } | |
82 | |
83 function finish() { | |
84 assert.equal(mutations, null); | |
85 observer.disconnect(); | |
86 done(); | |
87 } | |
88 | |
89 start(); | |
90 }); | |
91 | |
92 it('should allow multiple observers can be registered to a given node and bo
th receive mutations', function(done) { | |
93 var observer; | |
94 var observer2; | |
95 var charDataNode; | |
96 var mutations; | |
97 var mutations2; | |
98 | |
99 function start() { | |
100 var div = document.createElement('div'); | |
101 div.textContent = 'foo'; | |
102 charDataNode = div.firstChild; | |
103 observer = new MutationObserver(function(records) { | |
104 mutations = records; | |
105 }); | |
106 observer2 = new MutationObserver(function(records) { | |
107 mutations2 = records; | |
108 }); | |
109 observer.observe(charDataNode, {characterData: true}); | |
110 observer2.observe(charDataNode, {characterData: true}); | |
111 charDataNode.textContent = 'bar'; | |
112 setTimeout(finish, 0); | |
113 } | |
114 | |
115 function finish() { | |
116 assert.equal(mutations.length, 1); | |
117 assert.equal(mutations[0].type, "characterData"); | |
118 assert.equal(mutations[0].target, charDataNode); | |
119 assert.equal(mutations2.length, 1); | |
120 assert.equal(mutations2[0].type, "characterData"); | |
121 assert.equal(mutations2[0].target, charDataNode); | |
122 observer.disconnect(); | |
123 observer2.disconnect(); | |
124 done(); | |
125 } | |
126 | |
127 start(); | |
128 }); | |
129 | |
130 it('should provide oldValue is returned when requested', function(done) { | |
131 var observer; | |
132 var mutations; | |
133 var charDataNode; | |
134 | |
135 function start() { | |
136 var div = document.createElement('div'); | |
137 div.textContent = 'foo'; | |
138 charDataNode = div.firstChild; | |
139 observer = new MutationObserver(function(records) { | |
140 mutations = records; | |
141 }); | |
142 observer.observe(charDataNode, {characterData: true, characterDataOl
dValue: true}); | |
143 charDataNode.textContent = 'bar'; | |
144 charDataNode.textContent = 'baz'; | |
145 setTimeout(finish, 0); | |
146 } | |
147 | |
148 function finish() { | |
149 assert.equal(mutations.length, 2); | |
150 assert.equal(mutations[0].type, "characterData"); | |
151 assert.equal(mutations[0].target, charDataNode); | |
152 assert.equal(mutations[0].oldValue, "foo"); | |
153 assert.equal(mutations[1].type, "characterData"); | |
154 assert.equal(mutations[1].target, charDataNode); | |
155 assert.equal(mutations[1].oldValue, "bar"); | |
156 observer.disconnect(); | |
157 done(); | |
158 } | |
159 | |
160 start(); | |
161 }); | |
162 | |
163 it('should allow observing both with and without oldValue', function(done) { | |
164 var observerWithOldValue; | |
165 var observer; | |
166 var mutations; | |
167 var mutationsWithOldValue; | |
168 | |
169 function start() { | |
170 var div = document.createElement('div'); | |
171 div.textContent = 'foo'; | |
172 var charDataNode = div.firstChild; | |
173 observerWithOldValue = new MutationObserver(function(records) { | |
174 mutationsWithOldValue = records; | |
175 }); | |
176 observer = new MutationObserver(function(records) { | |
177 mutations = records; | |
178 }); | |
179 observerWithOldValue.observe(charDataNode, {characterData: true, cha
racterDataOldValue: true}); | |
180 observer.observe(charDataNode, {characterData: true}); | |
181 charDataNode.textContent = 'bar'; | |
182 setTimeout(finish, 0); | |
183 } | |
184 | |
185 function finish() { | |
186 assert.equal(mutationsWithOldValue.length, 1); | |
187 assert.equal(mutationsWithOldValue[0].type, "characterData"); | |
188 assert.equal(mutationsWithOldValue[0].oldValue, "foo"); | |
189 assert.equal(mutations.length, 1); | |
190 assert.equal(mutations[0].type, "characterData"); | |
191 assert.equal(mutations[0].oldValue, null); | |
192 observerWithOldValue.disconnect(); | |
193 observer.disconnect(); | |
194 done(); | |
195 } | |
196 | |
197 start(); | |
198 }); | |
199 | |
200 it('should provide oldValue if any observation requests it', function(done)
{ | |
201 var observer; | |
202 var mutations; | |
203 | |
204 function start() { | |
205 var div = document.createElement('div'); | |
206 div.textContent = 'foo'; | |
207 var charDataNode = div.firstChild; | |
208 observer = new MutationObserver(function(records) { | |
209 mutations = records; | |
210 }); | |
211 observer.observe(div, {characterData: true, characterDataOldValue: t
rue, subtree: true}); | |
212 observer.observe(charDataNode, {characterData: true}); | |
213 charDataNode.textContent = 'bar'; | |
214 setTimeout(finish, 0); | |
215 } | |
216 | |
217 function finish() { | |
218 assert.equal(mutations.length, 1); | |
219 assert.equal(mutations[0].type, "characterData"); | |
220 assert.equal(mutations[0].oldValue, "foo"); | |
221 observer.disconnect(); | |
222 done(); | |
223 } | |
224 | |
225 start(); | |
226 }); | |
227 }); | |
228 </script> | |
229 </html> | |
OLD | NEW |