Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/content/snippets.js

Issue 29835561: Issue 6803 - Implement readd snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Check for removedNodes first Created July 23, 2018, 7:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (element.style.getPropertyValue("display") != "none" || 113 if (element.style.getPropertyValue("display") != "none" ||
114 element.style.getPropertyPriority("display") != "important") 114 element.style.getPropertyPriority("display") != "important")
115 { 115 {
116 element.style.setProperty("display", "none", "important"); 116 element.style.setProperty("display", "none", "important");
117 } 117 }
118 }) 118 })
119 .observe(element, {attributes: true, attributeFilter: ["style"]}); 119 .observe(element, {attributes: true, attributeFilter: ["style"]});
120 } 120 }
121 121
122 /** 122 /**
123 * Observes changes to a DOM node using a <code>MutationObserver</code>.
124 *
125 * @param {Node} target The DOM node to observe for changes.
126 * @param {MutationObserverInit?} [options] Options that describe what DOM
127 * mutations should be reported to the callback.
128 * @param {function} callback A function that will be called on each DOM
129 * mutation, taking a <code>MutationRecord</code> as its parameter.
130 */
131 function observe(target, options, callback)
132 {
133 new MutationObserver(mutations =>
134 {
135 for (let mutation of mutations)
136 callback(mutation);
137 })
138 .observe(target, options);
139 }
140
141 /**
123 * Logs its arguments to the console. This may be used for testing and 142 * Logs its arguments to the console. This may be used for testing and
124 * debugging. 143 * debugging.
125 * 144 *
126 * @param {...*} [args] The arguments to log. 145 * @param {...*} [args] The arguments to log.
127 */ 146 */
128 function log(...args) 147 function log(...args)
129 { 148 {
130 console.log(...args); 149 console.log(...args);
131 } 150 }
132 151
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // will be removed. 239 // will be removed.
221 shadows.set(observer, {host: this, root}); 240 shadows.set(observer, {host: this, root});
222 241
223 return root; 242 return root;
224 } 243 }
225 }); 244 });
226 } 245 }
227 246
228 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains, 247 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
229 hideElement); 248 hideElement);
249
250 /**
251 * Untrashes a document by readding removed elements that match a CSS selector.
252 *
253 * @param {string} selector The CSS selector that a removed element should
254 * match for it to be added back.
255 * @param {string?} [parentSelector] The CSS selector that a removed element's
256 * former parent should match for it to be added back.
257 */
258 function untrash(selector, parentSelector = null)
259 {
260 observe(document, {childList: true, subtree: true}, mutation =>
261 {
262 if (mutation.removedNodes &&
263 (!parentSelector || mutation.target.matches(parentSelector)))
264 {
265 for (let node of mutation.removedNodes)
266 {
267 if (node instanceof HTMLElement && node.matches(selector))
268 {
269 // We don't have the location of the element in its former parent,
270 // but it's usually OK to just add it at the end.
271 mutation.target.appendChild(node);
272 }
273 }
274 }
275 });
276 }
277
278 exports.untrash = untrash;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld