| 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 } finally { | |
| 184 cleanContext(ctx); | |
| 185 } | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 function step_unit(f, ctx, t) { | |
| 190 return function () { | |
| 191 var done = false; | |
| 192 try { | |
| 193 f(); | |
| 194 done = true; | |
| 195 } finally { | |
| 196 if (done) { | |
| 197 t.done(); | |
| 198 } | |
| 199 cleanContext(ctx); | |
| 200 } | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 function assert_nodelist_contents_equal_noorder(actual, expected, message) { | |
| 205 assert_equals(actual.length, expected.length, message); | |
| 206 var used = []; | |
| 207 for (var i = 0; i < expected.length; i++) { | |
| 208 used.push(false); | |
| 209 } | |
| 210 for (i = 0; i < expected.length; i++) { | |
| 211 var found = false; | |
| 212 for (var j = 0; j < actual.length; j++) { | |
| 213 if (used[j] == false && expected[i] == actual[j]) { | |
| 214 used[j] = true; | |
| 215 found = true; | |
| 216 break; | |
| 217 } | |
| 218 } | |
| 219 if (!found) { | |
| 220 assert_unreached(message + ". Fail reason: element not found: " + e
xpected[i]); | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 | |
| 226 //Example taken from http://www.w3.org/TR/shadow-dom/#event-retargeting-example | |
| 227 function createTestMediaPlayer(d) { | |
| 228 d.body.innerHTML = '' + | |
| 229 '<div id="player">' + | |
| 230 '<input type="checkbox" id="outside-control">' + | |
| 231 '<div id="player-shadow-host">' + | |
| 232 '</div>' + | |
| 233 '</div>'; | |
| 234 | |
| 235 var playerShadowRoot = d.querySelector('#player-shadow-host').createShad
owRoot(); | |
| 236 playerShadowRoot.innerHTML = '' + | |
| 237 '<div id="controls">' + | |
| 238 '<button class="play-button">PLAY</button>' + | |
| 239 '<div tabindex="0" id="timeline">' + | |
| 240 '<div id="timeline-shadow-host">' + | |
| 241 '</div>' + | |
| 242 '</div>' + | |
| 243 '<div class="volume-slider-container" id="volume-slider-cont
ainer">' + | |
| 244 '<div tabindex="0" class="volume-slider" id="volume-slid
er">' + | |
| 245 '<div id="volume-shadow-host">' + | |
| 246 '</div>' + | |
| 247 '</div>' + | |
| 248 '</div>' + | |
| 249 '</div>'; | |
| 250 | |
| 251 var timeLineShadowRoot = playerShadowRoot.querySelector('#timeline-shado
w-host').createShadowRoot(); | |
| 252 timeLineShadowRoot.innerHTML = '<div class="slider-thumb" id="timeline-
slider-thumb"></div>'; | |
| 253 | |
| 254 var volumeShadowRoot = playerShadowRoot.querySelector('#volume-shadow-ho
st').createShadowRoot(); | |
| 255 volumeShadowRoot.innerHTML = '<div class="slider-thumb" id="volume-slide
r-thumb"></div>'; | |
| 256 | |
| 257 return { | |
| 258 'playerShadowRoot': playerShadowRoot, | |
| 259 'timeLineShadowRoot': timeLineShadowRoot, | |
| 260 'volumeShadowRoot': volumeShadowRoot | |
| 261 }; | |
| 262 } | |
| 263 | |
| 264 //FIXME This call of initKeyboardEvent works for WebKit-only. | |
| 265 //See https://bugs.webkit.org/show_bug.cgi?id=16735 | |
| 266 // and https://bugs.webkit.org/show_bug.cgi?id=13368. Add check for browser here | |
| 267 function fireKeyboardEvent(doc, element, key) { | |
| 268 var event = doc.createEvent('KeyboardEvent'); | |
| 269 event.initKeyboardEvent("keydown", true, true, doc.defaultView, key, 0, fals
e, false, false, false); | |
| 270 element.dispatchEvent(event); | |
| 271 } | |
| OLD | NEW |