OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Distributed under both the W3C Test Suite License [1] and the W3C |
| 3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the |
| 4 policies and contribution forms [3]. |
| 5 |
| 6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license |
| 7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license |
| 8 [3] http://www.w3.org/2004/10/27-testcases |
| 9 */ |
| 10 |
| 11 "use strict"; |
| 12 |
| 13 var HTML5_ELEMENTS = [ 'a', 'abbr', 'address', 'area', 'article', 'aside', |
| 14 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', |
| 15 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', |
| 16 'command', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', |
| 17 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', |
| 18 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', |
| 19 'hgroup', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', |
| 20 'keygen', 'label', 'legend', 'li', 'link', 'map', 'mark', 'menu', |
| 21 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', |
| 22 'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', |
| 23 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', |
| 24 'span', 'strong', 'style', 'sub', 'table', 'tbody', 'td', 'textarea', |
| 25 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', |
| 26 'var', 'video', 'wbr' ]; |
| 27 |
| 28 // only void (without end tag) HTML5 elements |
| 29 var HTML5_VOID_ELEMENTS = [ 'area', 'base', 'br', 'col', 'command', 'embed', |
| 30 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', |
| 31 'track', 'wbr' ]; |
| 32 |
| 33 // https://html.spec.whatwg.org/multipage/multipage/forms.html#form-associated-e
lement |
| 34 var HTML5_FORM_ASSOCIATED_ELEMENTS = [ 'button', 'fieldset', 'input', 'keygen', |
| 35 'label', 'object', 'output', 'select', 'textarea' ]; |
| 36 |
| 37 function newDocument() { |
| 38 var d = document.implementation.createDocument(); |
| 39 return d; |
| 40 } |
| 41 |
| 42 function newHTMLDocument() { |
| 43 var d = document.implementation.createHTMLDocument('Test Document'); |
| 44 return d; |
| 45 } |
| 46 |
| 47 function newXHTMLDocument() { |
| 48 var doctype = document.implementation.createDocumentType('html', |
| 49 '-//W3C//DTD XHTML 1.0 Transitional//EN', |
| 50 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'); |
| 51 |
| 52 var d = document.implementation.createDocument( |
| 53 'http://www.w3.org/1999/xhtml', 'html', doctype); |
| 54 return d; |
| 55 } |
| 56 |
| 57 function newIFrame(context, src) { |
| 58 if (typeof (context) === 'undefined' |
| 59 || typeof (context.iframes) !== 'object') { |
| 60 assert_unreached('Illegal context object in newIFrame'); |
| 61 } |
| 62 |
| 63 var iframe = document.createElement('iframe'); |
| 64 iframe.style.display = 'none'; |
| 65 |
| 66 if (typeof (src) != 'undefined') { |
| 67 iframe.src = src; |
| 68 } |
| 69 document.body.appendChild(iframe); |
| 70 context.iframes.push(iframe); |
| 71 |
| 72 assert_true(typeof (iframe.contentWindow) != 'undefined' |
| 73 && typeof (iframe.contentWindow.document) != 'undefined' |
| 74 && iframe.contentWindow.document != document, |
| 75 'Failed to create new rendered document'); |
| 76 return iframe; |
| 77 } |
| 78 |
| 79 function newRenderedHTMLDocument(context) { |
| 80 var frame = newIFrame(context); |
| 81 var d = frame.contentWindow.document; |
| 82 return d; |
| 83 } |
| 84 |
| 85 function newContext() { |
| 86 return { |
| 87 iframes : [] |
| 88 }; |
| 89 } |
| 90 |
| 91 function cleanContext(context) { |
| 92 context.iframes.forEach(function(e) { |
| 93 e.parentNode.removeChild(e); |
| 94 }); |
| 95 } |
| 96 |
| 97 // run given test function in context |
| 98 // the context is cleaned up after test completes. |
| 99 function inContext(f) { |
| 100 return function() { |
| 101 var context = newContext(); |
| 102 try { |
| 103 f(context); |
| 104 } finally { |
| 105 cleanContext(context); |
| 106 } |
| 107 }; |
| 108 } |
| 109 |
| 110 // new context and iframe are created and url (if supplied) is asigned to |
| 111 // iframe.src |
| 112 // function f is bound to the iframe onload event or executed directly after |
| 113 // iframe creation |
| 114 // the context is passed to function as argument |
| 115 function testInIFrame(url, f, testName, testProps) { |
| 116 if (url) { |
| 117 var t = async_test(testName, testProps); |
| 118 t.step(function() { |
| 119 var context = newContext(); |
| 120 var iframe = newIFrame(context, url); |
| 121 iframe.onload = t.step_func(function() { |
| 122 try { |
| 123 f(context); |
| 124 t.done(); |
| 125 } finally { |
| 126 cleanContext(context); |
| 127 } |
| 128 }); |
| 129 }); |
| 130 } else { |
| 131 test(inContext(function(context) { |
| 132 newRenderedHTMLDocument(context); |
| 133 f(context); |
| 134 }), testName, testProps); |
| 135 } |
| 136 } |
| 137 |
| 138 function assert_nodelist_contents_equal_noorder(actual, expected, message) { |
| 139 assert_equals(actual.length, expected.length, message); |
| 140 var used = []; |
| 141 for ( var i = 0; i < expected.length; i++) { |
| 142 used.push(false); |
| 143 } |
| 144 for (i = 0; i < expected.length; i++) { |
| 145 var found = false; |
| 146 for ( var j = 0; j < actual.length; j++) { |
| 147 if (used[j] == false && expected[i] == actual[j]) { |
| 148 used[j] = true; |
| 149 found = true; |
| 150 break; |
| 151 } |
| 152 } |
| 153 if (!found) { |
| 154 assert_unreached(message + ". Fail reason: element not found: " |
| 155 + expected[i]); |
| 156 } |
| 157 } |
| 158 } |
| 159 |
| 160 function isVisible(el) { |
| 161 return el.offsetTop != 0; |
| 162 } |
| 163 |
| 164 function isVoidElement(elementName) { |
| 165 return HTML5_VOID_ELEMENTS.indexOf(elementName) >= 0; |
| 166 } |
| 167 |
| 168 function checkTemplateContent(d, obj, html, id, nodeName) { |
| 169 |
| 170 obj.innerHTML = '<template id="tmpl">' + html + '</template>'; |
| 171 |
| 172 var t = d.querySelector('#tmpl'); |
| 173 |
| 174 if (id != null) { |
| 175 assert_equals(t.content.childNodes.length, 1, 'Element ' + nodeName |
| 176 + ' should present among template nodes'); |
| 177 assert_equals(t.content.firstChild.id, id, 'Wrong element ID'); |
| 178 } |
| 179 if (nodeName != null) { |
| 180 assert_equals(t.content.firstChild.nodeName, nodeName.toUpperCase(), |
| 181 'Wrong node name'); |
| 182 } |
| 183 } |
| 184 |
| 185 function checkBodyTemplateContent(d, html, id, nodeName) { |
| 186 checkTemplateContent(d, d.body, html, id, nodeName); |
| 187 } |
| 188 |
| 189 function checkHeadTemplateContent(d, html, id, nodeName) { |
| 190 checkTemplateContent(d, d.head, html, id, nodeName); |
| 191 } |
OLD | NEW |