Bug 1851372 [wpt PR 41785] - HTML: search setter always uses UTF-8, a=testonly
[gecko.git] / testing / web-platform / tests / html / resources / common.js
blob4f9ec54dde884bacec55da02a8507676d1ca5081
1 "use strict";
3 const HTML5_ELEMENTS = [
4   'a',        'abbr',     'address',  'area',     'article',    'aside',
5   'audio',    'b',        'base',     'bdi',      'bdo',        'blockquote',
6   'body',     'br',       'button',   'canvas',   'caption',    'cite',
7   'code',     'col',      'colgroup', 'data',     'datalist',   'dd',
8   'del',      'details',  'dfn',      'dialog',   'div',        'dl',
9   'dt',       'em',       'embed',    'fieldset', 'figcaption', 'figure',
10   'footer',   'form',     'h1',       'h2',       'h3',         'h4',
11   'h5',       'h6',       'head',     'header',   'hr',         'html',
12   'i',        'iframe',   'img',      'input',    'ins',        'kbd',
13   'label',    'legend',   'li',       'link',     'main',       'map',
14   'mark',     'menu',     'meta',     'meter',    'nav',        'noscript',
15   'object',   'ol',       'optgroup', 'option',   'output',     'p',
16   'param',    'pre',      'progress', 'q',        'rp',         'rt',
17   'ruby',     's',        'samp',     'script',   'section',    'select',
18   'slot',     'small',    'source',   'span',     'strong',     'style',
19   'sub',      'sup',      'summary',  'table',    'tbody',      'td',
20   'template', 'textarea', 'tfoot',    'th',       'thead',      'time',
21   'title',    'tr',       'track',    'u',        'ul',         'var',
22   'video',    'wbr'
25 // only void (without end tag) HTML5 elements
26 var HTML5_VOID_ELEMENTS = [
27   'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta',
28   'param', 'source', 'track', 'wbr'
31 // https://html.spec.whatwg.org/multipage/multipage/forms.html#form-associated-element
32 var HTML5_FORM_ASSOCIATED_ELEMENTS = [ 'button', 'fieldset', 'input',
33         'object', 'output', 'select', 'textarea' ];
35 const HTML5_SHADOW_ALLOWED_ELEMENTS = [
36   'article', 'aside', 'blockquote', 'body', 'div', 'footer', 'h1', 'h2', 'h3',
37   'h4', 'h5', 'h6', 'header', 'main', 'nav', 'p', 'section', 'span'
40 const HTML5_SHADOW_DISALLOWED_ELEMENTS =
41     HTML5_ELEMENTS.filter(el => !HTML5_SHADOW_ALLOWED_ELEMENTS.includes(el));
43 // These are *deprecated/removed* HTML5 element names.
44 const HTML5_DEPRECATED_ELEMENTS = [
45   'acronym',  'applet',  'basefont', 'bgsound',  'big',       'blink',
46   'center',   'command', 'content',  'dir',      'font',      'frame',
47   'frameset', 'hgroup',  'image',    'isindex',  'keygen',    'marquee',
48   'menuitem', 'nobr',    'noembed',  'noframes', 'plaintext', 'rb',
49   'rtc',      'shadow',  'spacer',   'strike',   'tt',        'xmp'
52 function newDocument() {
53     var d = document.implementation.createDocument();
54     return d;
57 function newHTMLDocument() {
58     var d = document.implementation.createHTMLDocument('Test Document');
59     return d;
62 function newXHTMLDocument() {
63     var doctype = document.implementation.createDocumentType('html',
64             '-//W3C//DTD XHTML 1.0 Transitional//EN',
65             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd');
67     var d = document.implementation.createDocument(
68             'http://www.w3.org/1999/xhtml', 'html', doctype);
69     return d;
72 function newIFrame(context, src) {
73     if (typeof (context) === 'undefined'
74             || typeof (context.iframes) !== 'object') {
75         assert_unreached('Illegal context object in newIFrame');
76     }
78     var iframe = document.createElement('iframe');
80     if (typeof (src) != 'undefined') {
81         iframe.src = src;
82     }
83     document.body.appendChild(iframe);
84     context.iframes.push(iframe);
86     assert_true(typeof (iframe.contentWindow) != 'undefined'
87             && typeof (iframe.contentWindow.document) != 'undefined'
88             && iframe.contentWindow.document != document,
89             'Failed to create new rendered document');
90     return iframe;
93 function newRenderedHTMLDocument(context) {
94     var frame = newIFrame(context);
95     var d = frame.contentWindow.document;
96     return d;
99 function newContext() {
100     return {
101         iframes : []
102     };
105 function cleanContext(context) {
106     context.iframes.forEach(function(e) {
107         e.parentNode.removeChild(e);
108     });
111 // run given test function in context
112 // the context is cleaned up after test completes.
113 function inContext(f) {
114     return function() {
115         var context = newContext();
116         try {
117             f(context);
118         } finally {
119             cleanContext(context);
120         }
121     };
124 // new context and iframe are created and url (if supplied) is asigned to
125 // iframe.src
126 // function f is bound to the iframe onload event or executed directly after
127 // iframe creation
128 // the context is passed to function as argument
129 function testInIFrame(url, f, testName, testProps) {
130     if (url) {
131         var t = async_test(testName);
132         t.step(function() {
133             var context = newContext();
134             var iframe = newIFrame(context, url);
135             iframe.onload = t.step_func(function() {
136                 try {
137                     f(context);
138                     t.done();
139                 } finally {
140                     cleanContext(context);
141                 }
142             });
143         });
144     } else {
145         test(inContext(function(context) {
146             newRenderedHTMLDocument(context);
147             f(context);
148         }), testName);
149     }
152 function assert_nodelist_contents_equal_noorder(actual, expected, message) {
153     assert_equals(actual.length, expected.length, message);
154     var used = [];
155     for ( var i = 0; i < expected.length; i++) {
156         used.push(false);
157     }
158     for (i = 0; i < expected.length; i++) {
159         var found = false;
160         for ( var j = 0; j < actual.length; j++) {
161             if (used[j] == false && expected[i] == actual[j]) {
162                 used[j] = true;
163                 found = true;
164                 break;
165             }
166         }
167         if (!found) {
168             assert_unreached(message + ". Fail reason:  element not found: "
169                     + expected[i]);
170         }
171     }
174 function isVoidElement(elementName) {
175     return HTML5_VOID_ELEMENTS.indexOf(elementName) >= 0;
178 function checkTemplateContent(d, obj, html, id, nodeName) {
180     obj.innerHTML = '<template id="tmpl">' + html + '</template>';
182     var t = d.querySelector('#tmpl');
184     if (id != null) {
185         assert_equals(t.content.childNodes.length, 1, 'Element ' + nodeName
186                 + ' should present among template nodes');
187         assert_equals(t.content.firstChild.id, id, 'Wrong element ID');
188     }
189     if (nodeName != null) {
190         assert_equals(t.content.firstChild.nodeName, nodeName.toUpperCase(),
191                 'Wrong node name');
192     }
195 function checkBodyTemplateContent(d, html, id, nodeName) {
196     checkTemplateContent(d, d.body, html, id, nodeName);
199 function checkHeadTemplateContent(d, html, id, nodeName) {
200     checkTemplateContent(d, d.head, html, id, nodeName);