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

Unified Diff: include.preload.js

Issue 29348917: Issue 4191 - Restore rules for reinjected stylesheets (Closed)
Patch Set: Rebased. Created Aug. 11, 2016, 11:03 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
diff --git a/include.preload.js b/include.preload.js
index f4ab60be781c88c27079d7df6f1c4e77a430188d..0397c82c7a0036d8774807f2013f9b9dd04470be 100644
--- a/include.preload.js
+++ b/include.preload.js
@@ -334,32 +334,53 @@ ElementHidingTracer.prototype = {
}
};
+function runInPage(fn, arg)
+{
+ var script = document.createElement("script");
+ script.type = "application/javascript";
+ script.async = false;
+ script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
+ document.documentElement.appendChild(script);
+ document.documentElement.removeChild(script);
+}
+
function reinjectStyleSheetWhenRemoved(document, style)
{
if (!MutationObserver)
return null;
+ var rules = style.sheet.rules;
var parentNode = style.parentNode;
var observer = new MutationObserver(function()
{
if (style.parentNode != parentNode)
+ {
parentNode.appendChild(style);
+
+ if (style.sheet.rules.length == 0)
+ {
+ for (var i = 0; i < rules.length; i++)
+ style.sheet.addRule(rules[i].selectorText, "display: none !important;");
+
+ style.id = id;
+ runInPage(
+ function(id)
+ {
+ var style = document.getElementById(id) ||
+ document.documentElement.shadowRoot.getElementById(id);
+ style.removeAttribute("id");
+ Object.defineProperty(style.sheet, "disabled",
+ {value: false, enumerable: true});
+ }, id
+ );
+ }
+ }
});
observer.observe(parentNode, {childList: true});
return observer;
}
-function runInPage(fn, arg)
-{
- var script = document.createElement("script");
- script.type = "application/javascript";
- script.async = false;
- script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
- document.documentElement.appendChild(script);
- document.documentElement.removeChild(script);
-}
-
function protectStyleSheet(document, style)
{
style.id = id;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld