test-ua-stylesheet.js (2400B)
1 function runUAStyleTests(props) { 2 const refs = document.getElementById('refs'); 3 for (const el of document.querySelectorAll('#tests > *')) { 4 const clone = fakeClone(el); 5 refs.append(clone); 6 } 7 const testsContainer = document.getElementById('tests'); 8 const testEls = document.querySelectorAll('#tests *'); 9 const refEls = document.querySelectorAll('#refs *'); 10 for (let i = 0; i < testEls.length; ++i) { 11 const testEl = testEls[i]; 12 if (testEl.hasAttribute('data-skip')) { 13 continue; 14 } 15 const refEl = refEls[i]; 16 const testStyle = getComputedStyle(testEl); 17 const refStyle = getComputedStyle(refEl); 18 for (const prop of props) { 19 // Don't test display for some elements. 20 // TODO(zcorpan): https://github.com/whatwg/html/issues/4093 21 // TODO(zcorpan): https://github.com/whatwg/html/issues/5063 22 if (prop === 'display' && 23 (testEl.localName === 'optgroup' || 24 testEl.localName === 'option' || 25 testEl.localName === 'marquee') 26 ) { 27 continue; 28 } 29 if (prop === 'overflow' && testEl.localName === 'select') { 30 // TODO: https://github.com/whatwg/html/issues/10031 31 continue; 32 } 33 test(() => { 34 assert_equals(testStyle.getPropertyValue(prop), refStyle.getPropertyValue(prop)); 35 }, `${testNameContext(testEl)} - ${prop}`); 36 } 37 } 38 39 function fakeClone(el) { 40 const clone = document.createElementNS('urn:not-html', el.localName); 41 for (const att of el.attributes) { 42 clone.setAttributeNS(att.namespaceURI, att.name, att.value); 43 } 44 // deep clone 45 for (const child of el.children) { 46 clone.append(fakeClone(child)); 47 } 48 return clone; 49 } 50 51 function testNameContext(el) { 52 const outerHTML = el.outerHTML; 53 const startTags = outerHTML.substring(0, outerHTML.indexOf('</')) || outerHTML; 54 55 let ancestors = []; 56 let current = el.parentNode; 57 while (current != testsContainer) { 58 ancestors.unshift(`<${current.localName}${contextAttrs(current.attributes)}>`); 59 current = current.parentNode; 60 } 61 return startTags + (ancestors.length ? ` (in ${ancestors.join('')})` : ''); 62 } 63 64 function contextAttrs(attributes) { 65 let rv = ""; 66 for (let i = 0; i < attributes.length; ++i) { 67 if (attributes[i].name === 'data-skip') { 68 continue; 69 } 70 rv += ` ${attributes[i].name}="${attributes[i].value}"`; 71 } 72 return rv; 73 } 74 }