test_revert.html (4107B)
1 <!DOCTYPE HTML> 2 <title>Test for computation of CSS 'revert' on all properties</title> 3 <script src="/tests/SimpleTest/SimpleTest.js"></script> 4 <script src="property_database.js"></script> 5 <style id="stylesheet"></style> 6 <link rel="stylesheet" href="/tests/SimpleTest/test.css" /> 7 <div id="inheritanceParent"> 8 <div id="inherited"></div> 9 </div> 10 <div id="nonInherited"></div> 11 <div id="noAuthorStyleApplied"></div> 12 <pre id="test"> 13 <script class="testbody"> 14 15 const kSheet = document.getElementById("stylesheet").sheet; 16 const kAllDifferentFromInitialRule = kSheet.cssRules[kSheet.insertRule("#inheritanceParent {}", kSheet.cssRules.length)]; 17 const kPrerequisites = kSheet.cssRules[kSheet.insertRule("#inherited, #inheritanceParent, #nonInherited, #noAuthorStyleApplied {}", kSheet.cssRules.length)]; 18 const kResetPropRule = kSheet.cssRules[kSheet.insertRule("#nonInherited {}", kSheet.cssRules.length)]; 19 20 const kInheritedDiv = document.getElementById("inherited"); 21 const kResetDiv = document.getElementById("nonInherited"); 22 const kNoAuthorStylesDiv = document.getElementById("noAuthorStyleApplied"); 23 24 function computedValue(node, property) { 25 return get_computed_value(getComputedStyle(node), property); 26 } 27 28 function getInitialValue(node, property) { 29 node.style.setProperty(property, "initial"); 30 const initial = computedValue(node, property); 31 node.style.removeProperty(property); 32 return initial; 33 } 34 35 function testResetProperty(property, info) { 36 kResetPropRule.style.setProperty(property, info.other_values[0]); 37 38 const div = kResetDiv; 39 const initial = getInitialValue(div, property); 40 const computed = computedValue(div, property); 41 42 isnot(computed, initial, `${property}: Should get something non-initial to begin with`); 43 44 const defaultValue = computedValue(kNoAuthorStylesDiv, property); 45 46 div.style.setProperty(property, "revert"); 47 const reverted = computedValue(div, property); 48 is(reverted, defaultValue, `${property}: Should behave as if there was no author style`); 49 div.style.removeProperty(property); 50 kResetPropRule.style.removeProperty(property); 51 } 52 53 function testInheritedProperty(property, info) { 54 // Given how line-height works, and that it always returns the used value, we 55 // cannot test it. The prerequisites for line-height makes getComputedStyle 56 // and getDefaultComputedStyle return the same, even though the computed value 57 // is different (normal vs. 19px). 58 if (property == "line-height") 59 return; 60 61 const div = kInheritedDiv; 62 const initial = getInitialValue(div, property); 63 const parentValue = computedValue(div.parentNode, property); 64 65 isnot(parentValue, initial, `${property}: Should inherit something non-initial to begin with`); 66 67 const inheritedValue = computedValue(div, property); 68 const hasUARule = inheritedValue != parentValue; 69 70 const defaultValue = computedValue(kNoAuthorStylesDiv, property); 71 (hasUARule ? is : isnot)(defaultValue, inheritedValue, `${property}: Should get the non-inherited value from somewhere (expected ${hasUARule ? "UA Rule" : "inheritance"} - inherited: ${inheritedValue} - parent: ${parentValue} - default: ${defaultValue})`); 72 73 div.style.setProperty(property, "revert"); 74 const reverted = computedValue(div, property); 75 if (hasUARule) 76 is(reverted, defaultValue, `${property}: Should behave as if there was no author style`); 77 else 78 is(reverted, inheritedValue, `${property}: Should behave as if there was no author style`); 79 div.style.removeProperty(property); 80 } 81 82 function testProperty(property, info) { 83 if (info.prerequisites) 84 for (const prereq in info.prerequisites) 85 kPrerequisites.style.setProperty(prereq, info.prerequisites[prereq]); 86 if (info.inherited) 87 testInheritedProperty(property, info); 88 else 89 testResetProperty(property, info); 90 kPrerequisites.style = ""; 91 } 92 93 for (const prop in gCSSProperties) { 94 const info = gCSSProperties[prop]; 95 if (info.type != CSS_TYPE_LONGHAND) 96 continue; 97 kAllDifferentFromInitialRule.style.setProperty(prop, info.other_values[0]); 98 } 99 100 for (const prop in gCSSProperties) 101 testProperty(prop, gCSSProperties[prop]); 102 </script> 103 </pre>