event-handler-all-global-events.html (3708B)
1 <!DOCTYPE html> 2 <title>GlobalEventHandlers</title> 3 <link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me"> 4 <link rel="help" href="https://html.spec.whatwg.org/multipage/#globaleventhandlers"> 5 <link rel="help" href="https://html.spec.whatwg.org/multipage/#event-handler-idl-attributes"> 6 <link rel="help" href="https://html.spec.whatwg.org/multipage/#event-handler-content-attributes"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/resources/WebIDLParser.js"></script> 10 11 <script> 12 "use strict"; 13 14 // The prefixed animation events are special; their event types are 15 // camel-case. 16 const prefixedAnimationAttributeToEventType = new Map([ 17 ["webkitanimationend", "webkitAnimationEnd"], 18 ["webkitanimationiteration", "webkitAnimationIteration"], 19 ["webkitanimationstart", "webkitAnimationStart"], 20 ["webkittransitionend", "webkitTransitionEnd"], 21 ]); 22 23 setup({ explicit_done: true }); 24 25 fetch("/interfaces/html.idl").then(res => res.text()).then(htmlIDL => { 26 const parsedHTMLIDL = WebIDL2.parse(htmlIDL); 27 const globalEventHandlers = parsedHTMLIDL.find(idl => idl.name === "GlobalEventHandlers"); 28 29 // onerror is too special 30 const names = globalEventHandlers.members.map(member => member.name).filter(name => name !== "onerror"); 31 32 for (const name of names) { 33 let withoutOn = name.substring(2); 34 if (prefixedAnimationAttributeToEventType.has(withoutOn)) { 35 withoutOn = prefixedAnimationAttributeToEventType.get(withoutOn); 36 } 37 38 test(() => { 39 for (const location of [window, HTMLElement.prototype, SVGElement.prototype, Document.prototype]) { 40 assert_true(location.hasOwnProperty(name), 41 `${location.constructor.name} has an own property named "${name}"`); 42 } 43 assert_false(name in Element.prototype, `Element.prototype must not contain a "${name}" property`); 44 }, `${name}: must be on the appropriate locations for GlobalEventHandlers`); 45 46 test(() => { 47 const htmlElement = document.createElement("span"); 48 const svgElement = document.createElementNS("http://www.w3.org/2000/svg", "g"); 49 50 for (var location of [window, htmlElement, svgElement, document]) { 51 assert_equals(location[name], null, 52 `The default value of the property is null for a ${location.constructor.name} instance`); 53 } 54 }, `${name}: the default value must be null`); 55 56 test(() => { 57 const el = document.createElement("div"); 58 el.setAttribute(name, `window.${name}Happened = true;`); 59 const compiledHandler = el[name]; 60 61 assert_equals(typeof compiledHandler, "function", `The ${name} property must be a function`); 62 compiledHandler(); 63 assert_true(window[name + "Happened"], "Calling the handler must run the code"); 64 }, `${name}: the content attribute must be compiled into a function as the corresponding property`); 65 66 test(() => { 67 const el = document.createElement("div"); 68 el.setAttribute(name, `window.${name}Happened2 = true;`); 69 70 el.dispatchEvent(new Event(withoutOn)); 71 72 assert_true(window[name + "Happened2"], "Dispatching an event must run the code"); 73 }, `${name}: the content attribute must execute when an event is dispatched`); 74 75 test(() => { 76 const element = document.createElement("meta"); 77 let fired = false; 78 element[name] = e => { 79 assert_equals(e.currentTarget, element, "The event must be fired at the <meta> element"); 80 fired = true; 81 }; 82 83 element.dispatchEvent(new Event(withoutOn)); 84 assert_true(fired); 85 }, `${name}: dispatching an Event at a <meta> element must trigger element.${name}`); 86 } 87 88 done(); 89 }); 90 </script>