OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 |
| 3 /* |
| 4 Distributed under both the W3C Test Suite License [1] and the W3C |
| 5 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the |
| 6 policies and contribution forms [3]. |
| 7 |
| 8 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license |
| 9 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license |
| 10 [3] http://www.w3.org/2004/10/27-testcases |
| 11 */ |
| 12 |
| 13 "use strict"; |
| 14 |
| 15 var HTML5_ELEMENT_NAMES = [ |
| 16 'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', |
| 17 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', |
| 18 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'command', |
| 19 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', |
| 20 'em', 'embed', |
| 21 'fieldset', 'figcaption', 'figure', 'footer', 'form', |
| 22 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', |
| 23 'html', |
| 24 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', |
| 25 'label', 'legend', 'li', 'link', |
| 26 'map', 'mark', 'menu', 'meta', 'meter', |
| 27 'nav', 'noscript', |
| 28 'object', 'ol', 'optgroup', 'option', 'output', |
| 29 'p', 'param', 'pre', 'progress', |
| 30 'q', |
| 31 'rp', 'rt', 'ruby', |
| 32 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', |
| 33 'strong', 'style', 'sub', |
| 34 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', |
| 35 'title', 'tr', 'track', |
| 36 'u', 'ul', |
| 37 'var', 'video', |
| 38 'wbr' |
| 39 ]; |
| 40 |
| 41 // http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#form-a
ssociated-element |
| 42 var HTML5_FORM_ASSOCIATED_ELEMENTS = ['button', 'fieldset', 'input', 'keygen', '
label', |
| 43 'object', 'output', 'select', 'textarea']; |
| 44 |
| 45 // Whether to work around vendor prefixes. |
| 46 var USE_VENDOR_SPECIFIC_WORKAROUND = true; |
| 47 |
| 48 function activateVendorSpecificWorkaround() { |
| 49 if (Element.prototype.webkitCreateShadowRoot && |
| 50 !Element.prototype.createShadowRoot) { |
| 51 Element.prototype.createShadowRoot = |
| 52 Element.prototype.webkitCreateShadowRoot; |
| 53 |
| 54 Object.defineProperty(Element.prototype, 'pseudo', { |
| 55 get: function () { return this.webkitPseudo; }, |
| 56 set: function (value) { return this.webkitPseudo = value; } |
| 57 }); |
| 58 |
| 59 Object.defineProperty(Element.prototype, 'shadowRoot', { |
| 60 get: function () { return this.webkitShadowRoot; } |
| 61 }); |
| 62 } |
| 63 } |
| 64 |
| 65 if (USE_VENDOR_SPECIFIC_WORKAROUND) |
| 66 activateVendorSpecificWorkaround(); |
| 67 |
| 68 // ---------------------------------------------------------------------------- |
| 69 // Deprecated: The code below is preserved only for the existing tests that are |
| 70 // using it. Now vendor prefixes are handled in a way that does not require |
| 71 // manual intervention. New tests should just use unprefixed APIs and you |
| 72 // are all set. |
| 73 // |
| 74 // These functions will eventually be removed when no tests use them. |
| 75 |
| 76 function ShadowDomNotSupportedError() { |
| 77 this.message = "Shadow DOM is not supported"; |
| 78 } |
| 79 |
| 80 // To allow using of both prefixed and non-prefixed API we do |
| 81 // the following hook |
| 82 function addPrefixed(element) { |
| 83 if (element && !element.pseudo) { |
| 84 Object.defineProperty(element, 'pseudo', { |
| 85 get: function () { return element.webkitPseudo; }, |
| 86 set: function (value) { return element.webkitPseudo =
value; } |
| 87 }); |
| 88 } |
| 89 } |
| 90 |
| 91 function addDocumentPrefixed(d) { |
| 92 if (d) { |
| 93 if (d.body) { |
| 94 addPrefixed(d.body); |
| 95 } |
| 96 if (d.head) { |
| 97 addPrefixed(d.head); |
| 98 } |
| 99 if (d.documentElement) { |
| 100 addPrefixed(d.documentElement); |
| 101 } |
| 102 d.oldCreate = d.createElement; |
| 103 d.createElement = function(tagName) { |
| 104 var el = d.oldCreate(tagName); |
| 105 addPrefixed(el); |
| 106 return el; |
| 107 }; |
| 108 } |
| 109 } |
| 110 |
| 111 |
| 112 function rethrowInternalErrors(e) { |
| 113 if (e instanceof ShadowDomNotSupportedError) { |
| 114 throw e; |
| 115 } |
| 116 |
| 117 } |
| 118 |
| 119 function newDocument() { |
| 120 var d = document.implementation.createDocument( |
| 121 'http://www.w3.org/1999/xhtml', 'html'); |
| 122 //FIXME remove the call below when non-prefixed API is used |
| 123 addDocumentPrefixed(d); |
| 124 return d; |
| 125 } |
| 126 |
| 127 function newHTMLDocument() { |
| 128 var d = document.implementation.createHTMLDocument('Test Document'); |
| 129 //FIXME remove the call below when non-prefixed API is used |
| 130 addDocumentPrefixed(d); |
| 131 return d; |
| 132 } |
| 133 |
| 134 function newIFrame(ctx, src) { |
| 135 if (typeof(ctx) == 'undefined' || typeof (ctx.iframes) != 'object') { |
| 136 assert_unreached('Illegal context object in newIFrame'); |
| 137 } |
| 138 |
| 139 var iframe = document.createElement('iframe'); |
| 140 if (!ctx.debug) { |
| 141 iframe.style.display = 'none'; |
| 142 } |
| 143 if (typeof(src) != 'undefined') { |
| 144 iframe.src = src; |
| 145 } |
| 146 document.body.appendChild(iframe); |
| 147 ctx.iframes.push(iframe); |
| 148 |
| 149 assert_true(typeof(iframe.contentWindow) != 'undefined' |
| 150 && typeof(iframe.contentWindow.document) != 'undefined' |
| 151 && iframe.contentWindow.document != document, 'Failed to create new rend
ered document' |
| 152 ); |
| 153 return iframe; |
| 154 } |
| 155 function newRenderedHTMLDocument(ctx) { |
| 156 var frame = newIFrame(ctx); |
| 157 var d = frame.contentWindow.document; |
| 158 //FIXME remove the call below when non-prefixed API is used |
| 159 addDocumentPrefixed(d); |
| 160 return d; |
| 161 } |
| 162 |
| 163 // End deprecated. |
| 164 // ---------------------------------------------------------------------------- |
| 165 |
| 166 function newContext() { |
| 167 return {iframes:[]}; |
| 168 } |
| 169 |
| 170 function cleanContext(ctx) { |
| 171 if (!ctx.debug) { |
| 172 ctx.iframes.forEach(function (e) { |
| 173 e.parentNode.removeChild(e); |
| 174 }); |
| 175 } |
| 176 } |
| 177 |
| 178 function unit(f) { |
| 179 return function () { |
| 180 var ctx = newContext(); |
| 181 try { |
| 182 f(ctx); |
| 183 } catch(e) { |
| 184 console.log(e.getMessage()); |
| 185 } finally { |
| 186 cleanContext(ctx); |
| 187 } |
| 188 } |
| 189 } |
| 190 |
| 191 function step_unit(f, ctx, t) { |
| 192 return function () { |
| 193 var done = false; |
| 194 try { |
| 195 f(); |
| 196 done = true; |
| 197 } finally { |
| 198 if (done) { |
| 199 t.done(); |
| 200 } |
| 201 cleanContext(ctx); |
| 202 } |
| 203 } |
| 204 } |
| 205 |
| 206 function assert_nodelist_contents_equal_noorder(actual, expected, message) { |
| 207 assert_equals(actual.length, expected.length, message); |
| 208 var used = []; |
| 209 for (var i = 0; i < expected.length; i++) { |
| 210 used.push(false); |
| 211 } |
| 212 for (i = 0; i < expected.length; i++) { |
| 213 var found = false; |
| 214 for (var j = 0; j < actual.length; j++) { |
| 215 if (used[j] == false && expected[i] == actual[j]) { |
| 216 used[j] = true; |
| 217 found = true; |
| 218 break; |
| 219 } |
| 220 } |
| 221 if (!found) { |
| 222 assert_unreached(message + ". Fail reason: element not found: " + e
xpected[i]); |
| 223 } |
| 224 } |
| 225 } |
| 226 |
| 227 |
| 228 //Example taken from http://www.w3.org/TR/shadow-dom/#event-retargeting-example |
| 229 function createTestMediaPlayer(d) { |
| 230 d.body.innerHTML = '' + |
| 231 '<div id="player">' + |
| 232 '<input type="checkbox" id="outside-control">' + |
| 233 '<div id="player-shadow-root">' + |
| 234 '</div>' + |
| 235 '</div>'; |
| 236 |
| 237 var playerShadowRoot = d.querySelector('#player-shadow-root').createShad
owRoot(); |
| 238 playerShadowRoot.innerHTML = '' + |
| 239 '<div id="controls">' + |
| 240 '<button class="play-button">PLAY</button>' + |
| 241 '<input type="range" id="timeline">' + |
| 242 '<div id="timeline-shadow-root">' + |
| 243 '</div>' + |
| 244 '</input>' + |
| 245 '<div class="volume-slider-container" id="volume-slider-cont
ainer">' + |
| 246 '<input type="range" class="volume-slider" id="volume-sl
ider">' + |
| 247 '<div id="volume-shadow-root">' + |
| 248 '</div>' + |
| 249 '</input>' + |
| 250 '</div>' + |
| 251 '</div>'; |
| 252 |
| 253 var timeLineShadowRoot = playerShadowRoot.querySelector('#timeline-shado
w-root').createShadowRoot(); |
| 254 timeLineShadowRoot.innerHTML = '<div class="slider-thumb" id="timeline-
slider-thumb"></div>'; |
| 255 |
| 256 var volumeShadowRoot = playerShadowRoot.querySelector('#volume-shadow-ro
ot').createShadowRoot(); |
| 257 volumeShadowRoot.innerHTML = '<div class="slider-thumb" id="volume-slide
r-thumb"></div>'; |
| 258 |
| 259 return { |
| 260 'playerShadowRoot': playerShadowRoot, |
| 261 'timeLineShadowRoot': timeLineShadowRoot, |
| 262 'volumeShadowRoot': volumeShadowRoot |
| 263 }; |
| 264 } |
| 265 |
| 266 //FIXME This call of initKeyboardEvent works for WebKit-only. |
| 267 //See https://bugs.webkit.org/show_bug.cgi?id=16735 |
| 268 // and https://bugs.webkit.org/show_bug.cgi?id=13368. Add check for browser here |
| 269 function fireKeyboardEvent(doc, element, key) { |
| 270 var event = doc.createEvent('KeyboardEvent'); |
| 271 event.initKeyboardEvent("keydown", true, true, doc.defaultView, key, 0, fals
e, false, false, false); |
| 272 element.dispatchEvent(event); |
| 273 } |
OLD | NEW |