inheritance-testcommon.js (4100B)
1 'use strict'; 2 3 (function() { 4 5 function assert_initial(property, initial) { 6 let initialDesc = initial; 7 if (Array.isArray(initial)) 8 initialDesc = '[' + initial.map(e => "'" + e + "'").join(' or ') + ']'; 9 10 test(() => { 11 const target = document.getElementById('target'); 12 assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style"); 13 target.style[property] = 'initial'; 14 if (Array.isArray(initial)) { 15 assert_in_array(getComputedStyle(target)[property], initial); 16 } else { 17 assert_equals(getComputedStyle(target)[property], initial); 18 } 19 target.style[property] = ''; 20 }, 'Property ' + property + ' has initial value ' + initialDesc); 21 } 22 23 /** 24 * Create tests that a CSS property inherits and has the given initial value. 25 * 26 * The current document must have an element #target within element #container. 27 * 28 * @param {string} property The name of the CSS property being tested. 29 * @param {string|array} initial The computed value for 'initial' or a list 30 * of acceptable computed value serializations. 31 * @param {string} other An arbitrary value for the property that 32 * round trips and is distinct from the initial 33 * value. 34 */ 35 function assert_inherited(property, initial, other) { 36 if (initial) 37 assert_initial(property, initial); 38 39 test(() => { 40 const container = document.getElementById('container'); 41 const target = document.getElementById('target'); 42 assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style"); 43 container.style[property] = 'initial'; 44 target.style[property] = 'unset'; 45 assert_not_equals(getComputedStyle(container)[property], other); 46 assert_not_equals(getComputedStyle(target)[property], other); 47 container.style[property] = other; 48 assert_equals(getComputedStyle(container)[property], other); 49 assert_equals(getComputedStyle(target)[property], other); 50 target.style[property] = 'initial'; 51 assert_equals(getComputedStyle(container)[property], other); 52 assert_not_equals(getComputedStyle(target)[property], other); 53 target.style[property] = 'inherit'; 54 assert_equals(getComputedStyle(target)[property], other); 55 container.style[property] = ''; 56 target.style[property] = ''; 57 }, 'Property ' + property + ' inherits'); 58 } 59 60 /** 61 * Create tests that a CSS property does not inherit, and that it has the 62 * given initial value. 63 * 64 * The current document must have an element #target within element #container. 65 * 66 * @param {string} property The name of the CSS property being tested. 67 * @param {string|array} initial The computed value for 'initial' or a list 68 * of acceptable computed value serializations. 69 * @param {string} other An arbitrary value for the property that 70 * round trips and is distinct from the initial 71 * value. 72 */ 73 function assert_not_inherited(property, initial, other) { 74 assert_initial(property, initial); 75 76 test(() => { 77 const container = document.getElementById('container'); 78 const target = document.getElementById('target'); 79 assert_true(property in getComputedStyle(target)); 80 container.style[property] = 'initial'; 81 target.style[property] = 'unset'; 82 assert_not_equals(getComputedStyle(container)[property], other); 83 assert_not_equals(getComputedStyle(target)[property], other); 84 container.style[property] = other; 85 assert_equals(getComputedStyle(container)[property], other); 86 assert_not_equals(getComputedStyle(target)[property], other); 87 target.style[property] = 'inherit'; 88 assert_equals(getComputedStyle(target)[property], other); 89 container.style[property] = ''; 90 target.style[property] = ''; 91 }, 'Property ' + property + ' does not inherit'); 92 } 93 94 window.assert_inherited = assert_inherited; 95 window.assert_not_inherited = assert_not_inherited; 96 })();