test_initial_computation.html (6248B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 --> 5 <head> 6 <title>Test for computation of CSS 'initial' on all properties and 'unset' on reset properties</title> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <script type="text/javascript" src="property_database.js"></script> 9 <style type="text/css" id="stylesheet"></style> 10 <style type="text/css"> 11 /* For 'width', 'height', etc., need a constant size container. */ 12 #display { width: 500px; height: 200px } 13 </style> 14 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 15 <script type="text/javascript"> 16 SimpleTest.waitForExplicitFinish(); 17 18 var load_count = 0; 19 function load_done() { 20 if (++load_count == 3) 21 run_tests(); 22 } 23 </script> 24 </head> 25 <body> 26 <p id="display"><span><span id="elementf"></span></span> 27 <iframe id="unstyledn" src="unstyled.xml" height="10" width="10" onload="load_done()"></iframe> 28 <iframe id="unstyledf" src="unstyled-frame.xml" height="10" width="10" onload="load_done()"></iframe> 29 </p> 30 <div id="content" style="display: none"> 31 32 <div><span id="elementn"></span></div> 33 34 35 </div> 36 <pre id="test"> 37 <script class="testbody" type="text/javascript"> 38 39 /** 40 * Test for computation of CSS 'initial' on all properties and 'unset' on 41 * reset properties 42 */ 43 44 var gBrokenInitial = { 45 }; 46 47 function xfail_initial(property) { 48 return property in gBrokenInitial; 49 } 50 51 var gDisplayTree = document.getElementById("display"); 52 var gElementN = document.getElementById("elementn"); 53 var gElementF = document.getElementById("elementf"); 54 var gStyleSheet = document.getElementById("stylesheet").sheet; 55 var gRule1 = gStyleSheet.cssRules[gStyleSheet.insertRule("#elementn, #elementf {}", gStyleSheet.cssRules.length)]; 56 var gRule2 = gStyleSheet.cssRules[gStyleSheet.insertRule("#elementn, #elementf {}", gStyleSheet.cssRules.length)]; 57 58 var gInitialValuesN; 59 var gInitialValuesF; 60 var gInitialPrereqsRuleN; 61 var gInitialPrereqsRuleF; 62 63 function setup_initial_values(id, ivalprop, prereqprop) { 64 var iframe = document.getElementById(id); 65 window[ivalprop] = iframe.contentWindow.getComputedStyle( 66 iframe.contentDocument.documentElement.firstChild); 67 var sheet = iframe.contentDocument.styleSheets[0]; 68 // For 'width', 'height', etc., need a constant size container. 69 sheet.insertRule(":root { height: 200px; width: 500px }", sheet.cssRules.length); 70 71 window[prereqprop] = sheet.cssRules[sheet.insertRule(":root > * {}", sheet.cssRules.length)]; 72 } 73 74 function test_property(property) 75 { 76 var info = gCSSProperties[property]; 77 78 var keywords = ["initial"]; 79 if (!info.inherited) 80 keywords.push("unset"); 81 82 keywords.forEach(function(keyword) { 83 if ("prerequisites" in info) { 84 var prereqs = info.prerequisites; 85 for (var prereq in prereqs) { 86 gRule1.style.setProperty(prereq, prereqs[prereq], ""); 87 gInitialPrereqsRuleN.style.setProperty(prereq, prereqs[prereq], ""); 88 gInitialPrereqsRuleF.style.setProperty(prereq, prereqs[prereq], ""); 89 } 90 } 91 if (info.inherited) { 92 gElementN.parentNode.style.setProperty(property, info.other_values[0], ""); 93 gElementF.parentNode.style.setProperty(property, info.other_values[0], ""); 94 } 95 96 var initial_computed_n = get_computed_value(gInitialValuesN, property); 97 var initial_computed_f = get_computed_value(gInitialValuesF, property); 98 gRule1.style.setProperty(property, info.other_values[0], ""); 99 var other_computed_n = get_computed_value(getComputedStyle(gElementN, ""), property); 100 var other_computed_f = get_computed_value(getComputedStyle(gElementF, ""), property); 101 isnot(other_computed_n, initial_computed_n, 102 "should be testing with values that compute to different things " + 103 "for '" + property + "'"); 104 isnot(other_computed_f, initial_computed_f, 105 "should be testing with values that compute to different things " + 106 "for '" + property + "'"); 107 // It used to be important for values that are supposed to compute to the 108 // initial value (given the design of the old rule tree, nsRuleNode) that 109 // we're modifying the most specific rule that matches the element, and 110 // that we've already requested style while that rule was empty. This 111 // means we'd have a cached aStartStruct from the parent in the rule 112 // tree (caching the "other" value), so we'd make sure we don't get the 113 // initial value from the luck of default-initialization. This means 114 // that it would've been important that we set the prereqs on 115 // gRule1.style rather than on gElement.style. 116 // 117 // However, the rule tree no longer stores cached structs, and we only 118 // temporarily cache reset structs during a single restyle. So the 119 // particular failure mode this was designed to test for isn't as 120 // likely to eventuate. 121 gRule2.style.setProperty(property, keyword, ""); 122 var initial_val_computed_n = get_computed_value(getComputedStyle(gElementN, ""), property); 123 var initial_val_computed_f = get_computed_value(getComputedStyle(gElementF, ""), property); 124 (xfail_initial(property) ? todo_is : is)( 125 initial_val_computed_n, initial_computed_n, 126 keyword + " should cause initial value for '" + property + "'"); 127 (xfail_initial(property) ? todo_is : is)( 128 initial_val_computed_f, initial_computed_f, 129 keyword + " should cause initial value for '" + property + "'"); 130 gRule1.style.removeProperty(property); 131 gRule2.style.removeProperty(property); 132 133 if ("prerequisites" in info) { 134 var prereqs = info.prerequisites; 135 for (var prereq in prereqs) { 136 gRule1.style.removeProperty(prereq); 137 gInitialPrereqsRuleN.style.removeProperty(prereq); 138 gInitialPrereqsRuleF.style.removeProperty(prereq); 139 } 140 } 141 if (info.inherited) { 142 gElementN.parentNode.style.removeProperty(property); 143 gElementF.parentNode.style.removeProperty(property); 144 } 145 }); 146 } 147 148 function run_tests() { 149 setup_initial_values("unstyledn", "gInitialValuesN", "gInitialPrereqsRuleN"); 150 setup_initial_values("unstyledf", "gInitialValuesF", "gInitialPrereqsRuleF"); 151 for (var prop in gCSSProperties) 152 test_property(prop); 153 SimpleTest.finish(); 154 } 155 156 load_done(); 157 158 </script> 159 </pre> 160 </body> 161 </html>