OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 196 |
197 // If there's no Element.attachShadow API present then we don't care, it must | 197 // If there's no Element.attachShadow API present then we don't care, it must |
198 // be Firefox or an older version of Chrome. | 198 // be Firefox or an older version of Chrome. |
199 if (!originalAttachShadow) | 199 if (!originalAttachShadow) |
200 return; | 200 return; |
201 | 201 |
202 // Mutation observers mapped to their corresponding shadow roots and their | 202 // Mutation observers mapped to their corresponding shadow roots and their |
203 // hosts. | 203 // hosts. |
204 let shadows = new WeakMap(); | 204 let shadows = new WeakMap(); |
205 | 205 |
206 function observe(mutations, observer) | 206 function observeShadow(mutations, observer) |
207 { | 207 { |
208 let {host, root} = shadows.get(observer) || {}; | 208 let {host, root} = shadows.get(observer) || {}; |
209 | 209 |
210 // Since it's a weak map, it's possible that either the element or its | 210 // Since it's a weak map, it's possible that either the element or its |
211 // shadow has been removed. | 211 // shadow has been removed. |
212 if (!host || !root) | 212 if (!host || !root) |
213 return; | 213 return; |
214 | 214 |
215 // If the shadow contains the given text, check if the host or one of its | 215 // If the shadow contains the given text, check if the host or one of its |
216 // ancestors matches the selector; if a matching element is found, hide | 216 // ancestors matches the selector; if a matching element is found, hide |
(...skipping 15 matching lines...) Expand all Loading... |
232 } | 232 } |
233 | 233 |
234 Object.defineProperty(Element.prototype, "attachShadow", { | 234 Object.defineProperty(Element.prototype, "attachShadow", { |
235 value(...args) | 235 value(...args) |
236 { | 236 { |
237 // Create the shadow root first. It doesn't matter if it's a closed | 237 // Create the shadow root first. It doesn't matter if it's a closed |
238 // shadow root, we keep the reference in a weak map. | 238 // shadow root, we keep the reference in a weak map. |
239 let root = originalAttachShadow.apply(this, args); | 239 let root = originalAttachShadow.apply(this, args); |
240 | 240 |
241 // Listen for relevant DOM mutations in the shadow. | 241 // Listen for relevant DOM mutations in the shadow. |
242 let observer = new MutationObserver(observe); | 242 let observer = new MutationObserver(observeShadow); |
243 observer.observe(root, { | 243 observer.observe(root, { |
244 childList: true, | 244 childList: true, |
245 characterData: true, | 245 characterData: true, |
246 subtree: true | 246 subtree: true |
247 }); | 247 }); |
248 | 248 |
249 // Keep references to the shadow root and its host in a weak map. If | 249 // Keep references to the shadow root and its host in a weak map. If |
250 // either the shadow is detached or the host itself is removed from the | 250 // either the shadow is detached or the host itself is removed from the |
251 // DOM, the mutation observer too will be freed eventually and the entry | 251 // DOM, the mutation observer too will be freed eventually and the entry |
252 // will be removed. | 252 // will be removed. |
(...skipping 30 matching lines...) Expand all Loading... |
283 // We don't have the location of the element in its former parent, | 283 // We don't have the location of the element in its former parent, |
284 // but it's usually OK to just add it at the end. | 284 // but it's usually OK to just add it at the end. |
285 mutation.target.appendChild(node); | 285 mutation.target.appendChild(node); |
286 } | 286 } |
287 } | 287 } |
288 } | 288 } |
289 }); | 289 }); |
290 } | 290 } |
291 | 291 |
292 exports.readd = readd; | 292 exports.readd = readd; |
OLD | NEW |