shadow-dom-utils.js (3150B)
1 "use strict"; 2 3 function unit(f) { 4 return function () { 5 var ctx = newContext(); 6 try { 7 f(ctx); 8 } finally { 9 cleanContext(ctx); 10 } 11 } 12 } 13 14 function step_unit(f, ctx, t) { 15 return function () { 16 var done = false; 17 try { 18 f(); 19 done = true; 20 } finally { 21 if (done) { 22 t.done(); 23 } 24 cleanContext(ctx); 25 } 26 } 27 } 28 29 function assert_nodelist_contents_equal_noorder(actual, expected, message) { 30 assert_equals(actual.length, expected.length, message); 31 var used = []; 32 for (var i = 0; i < expected.length; i++) { 33 used.push(false); 34 } 35 for (i = 0; i < expected.length; i++) { 36 var found = false; 37 for (var j = 0; j < actual.length; j++) { 38 if (used[j] == false && expected[i] == actual[j]) { 39 used[j] = true; 40 found = true; 41 break; 42 } 43 } 44 if (!found) { 45 assert_unreached(message + ". Fail reason: element not found: " + expected[i]); 46 } 47 } 48 } 49 50 //Example taken from http://www.w3.org/TR/shadow-dom/#event-retargeting-example 51 function createTestMediaPlayer(d) { 52 d.body.innerHTML = '' + 53 '<div id="player">' + 54 '<input type="checkbox" id="outside-control">' + 55 '<div id="player-shadow-host">' + 56 '</div>' + 57 '</div>'; 58 59 var playerShadowRoot = d.querySelector('#player-shadow-host').attachShadow({mode: 'open'}); 60 playerShadowRoot.innerHTML = '' + 61 '<div id="controls">' + 62 '<button class="play-button">PLAY</button>' + 63 '<div tabindex="0" id="timeline">' + 64 '<div id="timeline-shadow-host">' + 65 '</div>' + 66 '</div>' + 67 '<div class="volume-slider-container" id="volume-slider-container">' + 68 '<div tabindex="0" class="volume-slider" id="volume-slider">' + 69 '<div id="volume-shadow-host">' + 70 '</div>' + 71 '</div>' + 72 '</div>' + 73 '</div>'; 74 75 var timeLineShadowRoot = playerShadowRoot.querySelector('#timeline-shadow-host').attachShadow({mode: 'open'}); 76 timeLineShadowRoot.innerHTML = '<div class="slider-thumb" id="timeline-slider-thumb"></div>'; 77 78 var volumeShadowRoot = playerShadowRoot.querySelector('#volume-shadow-host').attachShadow({mode: 'open'}); 79 volumeShadowRoot.innerHTML = '<div class="slider-thumb" id="volume-slider-thumb"></div>'; 80 81 return { 82 'playerShadowRoot': playerShadowRoot, 83 'timeLineShadowRoot': timeLineShadowRoot, 84 'volumeShadowRoot': volumeShadowRoot 85 }; 86 } 87 88 //FIXME This call of initKeyboardEvent works for WebKit-only. 89 //See https://bugs.webkit.org/show_bug.cgi?id=16735 90 // and https://bugs.webkit.org/show_bug.cgi?id=13368. Add check for browser here 91 function fireKeyboardEvent(doc, element, key) { 92 var event = doc.createEvent('KeyboardEvent'); 93 event.initKeyboardEvent("keydown", true, true, doc.defaultView, key, 0, false, false, false, false); 94 element.dispatchEvent(event); 95 }