tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_rules_out_of_sheets.html (3252B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=634373
      5 -->
      6 <head>
      7  <title>Test for Bug 634373</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=634373">Mozilla Bug 634373</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none">
     15 
     16 </div>
     17 <pre id="test">
     18 <script type="application/javascript">
     19 
     20 /** Test for Bug 634373 */
     21 
     22 function make_rule_and_remove_sheet(text, getter) {
     23  var style = document.createElement("style");
     24  style.setAttribute("type", "text/css");
     25  style.appendChild(document.createTextNode(text));
     26  document.head.appendChild(style);
     27  var result = style.sheet.cssRules[0];
     28  if (getter) {
     29    result = getter(result);
     30  }
     31  document.head.removeChild(style);
     32  style = null;
     33  SpecialPowers.DOMWindowUtils.garbageCollect();
     34  return result;
     35 }
     36 
     37 var gDisplayCS = getComputedStyle(document.getElementById("display"), "");
     38 
     39 function keep_rule_alive_by_matching(rule) {
     40  // It's the caller's job to guarantee that the rule matches a p.
     41  // This just causes a style flush, which in turn keeps the rule alive
     42  // until the next style flush.
     43  var color = gDisplayCS.color;
     44  return rule;
     45 }
     46 
     47 function get_rule_and_child(rule) {
     48  return [rule, rule.cssRules[0]];
     49 }
     50 
     51 function get_only_child(rule) {
     52  return rule.cssRules[0];
     53 }
     54 
     55 var rule;
     56 
     57 // In this case, the rule goes away immediately, so we're testing
     58 // the DOM wrapper's handling of a null rule, rather than the rule's
     59 // handling of a null sheet.
     60 rule = make_rule_and_remove_sheet("p { color: blue }");
     61 rule.style.color = "";
     62 try {
     63  rule.style.color = "fuchsia";
     64 } catch(ex) {}
     65 
     66 rule = make_rule_and_remove_sheet("p { color: blue }",
     67                                  keep_rule_alive_by_matching);
     68 try {
     69  rule.style.color = "";
     70 } catch(ex) {}
     71 try {
     72  rule.style.color = "fuchsia";
     73 } catch(ex) {}
     74 
     75 rule = make_rule_and_remove_sheet("@media screen { p { color: blue } }",
     76                                   get_rule_and_child);
     77 rule[1].style.color = "";
     78 try {
     79  rule[1].style.color = "fuchsia";
     80 } catch(ex) {}
     81 
     82 // In this case, the rule goes away immediately, so we're testing
     83 // the DOM wrapper's handling of a null rule, rather than the rule's
     84 // handling of a null sheet.
     85 rule = make_rule_and_remove_sheet("@media screen { p { color: blue } }",
     86                                   get_only_child);
     87 rule.style.color = "";
     88 try {
     89  rule.style.color = "fuchsia";
     90 } catch(ex) {}
     91 
     92 rule = make_rule_and_remove_sheet("@media screen { p { color: blue } }",
     93                                  function(ruleInner) {
     94                                    return keep_rule_alive_by_matching(
     95                                             get_only_child(ruleInner));
     96                                  });
     97 try {
     98  rule.style.color = "";
     99 } catch(ex) {}
    100 try {
    101  rule.style.color = "fuchsia";
    102 } catch(ex) {}
    103 
    104 rule = make_rule_and_remove_sheet("@keyframes a { from { color: blue } }");
    105 rule.appendRule("from { color: fuchsia}");
    106 rule.deleteRule("from");
    107 rule.name = "b";
    108 if (rule.cssRules[0]) {
    109  rule.cssRules[0].keyText = "50%";
    110 }
    111 
    112 ok(true, "didn't crash");
    113 
    114 </script>
    115 </pre>
    116 </body>
    117 </html>