1// Flags: --expose-internals 2'use strict'; 3 4const common = require('../common'); 5 6const { 7 Event, 8 EventTarget, 9} = require('internal/event_target'); 10 11const { 12 fail, 13 ok, 14 strictEqual 15} = require('assert'); 16 17// Manually ported from WPT AddEventListenerOptions-passive.html 18{ 19 const document = new EventTarget(); 20 let supportsPassive = false; 21 const query_options = { 22 get passive() { 23 supportsPassive = true; 24 return false; 25 }, 26 get dummy() { 27 fail('dummy value getter invoked'); 28 return false; 29 } 30 }; 31 32 document.addEventListener('test_event', null, query_options); 33 ok(supportsPassive); 34 35 supportsPassive = false; 36 document.removeEventListener('test_event', null, query_options); 37 strictEqual(supportsPassive, false); 38} 39{ 40 function testPassiveValue(optionsValue, expectedDefaultPrevented) { 41 const document = new EventTarget(); 42 let defaultPrevented; 43 function handler(e) { 44 if (e.defaultPrevented) { 45 fail('Event prematurely marked defaultPrevented'); 46 } 47 e.preventDefault(); 48 defaultPrevented = e.defaultPrevented; 49 } 50 document.addEventListener('test', handler, optionsValue); 51 // TODO the WHATWG test is more extensive here and tests dispatching on 52 // document.body, if we ever support getParent we should amend this 53 const ev = new Event('test', { bubbles: true, cancelable: true }); 54 const uncanceled = document.dispatchEvent(ev); 55 56 strictEqual(defaultPrevented, expectedDefaultPrevented); 57 strictEqual(uncanceled, !expectedDefaultPrevented); 58 59 document.removeEventListener('test', handler, optionsValue); 60 } 61 testPassiveValue(undefined, true); 62 testPassiveValue({}, true); 63 testPassiveValue({ passive: false }, true); 64 65 common.skip('TODO: passive listeners is still broken'); 66 testPassiveValue({ passive: 1 }, false); 67 testPassiveValue({ passive: true }, false); 68 testPassiveValue({ passive: 0 }, true); 69} 70