Best JavaScript code snippet using playwright-internal
eventtargettester.js
Source:eventtargettester.js
...232function times(n) {233 return n;234}235function testNoListener() {236 dispatchEvent(eventTargets[0], EventType.A);237 assertNoOtherListenerIsCalled();238}239function testOneListener() {240 listen(eventTargets[0], EventType.A, listeners[0]);241 dispatchEvent(eventTargets[0], EventType.A);242 assertListenerIsCalled(listeners[0], times(1));243 assertNoOtherListenerIsCalled();244 resetListeners();245 dispatchEvent(eventTargets[0], EventType.B);246 dispatchEvent(eventTargets[0], EventType.C);247 assertNoOtherListenerIsCalled();248}249function testTwoListenersOfSameType() {250 var key1 = listen(eventTargets[0], EventType.A, listeners[0]);251 var key2 = listen(eventTargets[0], EventType.A, listeners[1]);252 if (keyType == goog.events.eventTargetTester.KeyType.NUMBER) {253 assertNotEquals(key1, key2);254 } else {255 assertUndefined(key1);256 assertUndefined(key2);257 }258 dispatchEvent(eventTargets[0], EventType.A);259 assertListenerIsCalled(listeners[0], times(1));260 assertListenerIsCalled(listeners[1], times(1));261 assertNoOtherListenerIsCalled();262}263function testInstallingSameListeners() {264 var key1 = listen(eventTargets[0], EventType.A, listeners[0]);265 var key2 = listen(eventTargets[0], EventType.A, listeners[0]);266 var key3 = listen(eventTargets[0], EventType.B, listeners[0]);267 if (keyType == goog.events.eventTargetTester.KeyType.NUMBER) {268 assertEquals(key1, key2);269 assertNotEquals(key1, key3);270 } else {271 assertUndefined(key1);272 assertUndefined(key2);273 assertUndefined(key3);274 }275 dispatchEvent(eventTargets[0], EventType.A);276 assertListenerIsCalled(listeners[0], times(1));277 dispatchEvent(eventTargets[0], EventType.B);278 assertListenerIsCalled(listeners[0], times(2));279 assertNoOtherListenerIsCalled();280}281function testScope() {282 listeners[0] = createListener(function(e) {283 assertEquals('Wrong scope with undefined scope', eventTargets[0], this);284 });285 listeners[1] = createListener(function(e) {286 assertEquals('Wrong scope with null scope', eventTargets[0], this);287 });288 var scope = {};289 listeners[2] = createListener(function(e) {290 assertEquals('Wrong scope with specific scope object', scope, this);291 });292 listen(eventTargets[0], EventType.A, listeners[0]);293 listen(eventTargets[0], EventType.A, listeners[1], false, null);294 listen(eventTargets[0], EventType.A, listeners[2], false, scope);295 dispatchEvent(eventTargets[0], EventType.A);296 assertListenerIsCalled(listeners[0], times(1));297 assertListenerIsCalled(listeners[1], times(1));298 assertListenerIsCalled(listeners[2], times(1));299}300function testDispatchEventDoesNotThrowWithDisposedEventTarget() {301 goog.dispose(eventTargets[0]);302 assertTrue(dispatchEvent(eventTargets[0], EventType.A));303}304function testDispatchEventWithObjectLiteral() {305 listen(eventTargets[0], EventType.A, listeners[0]);306 assertTrue(dispatchEvent(eventTargets[0], {type: EventType.A}));307 assertListenerIsCalled(listeners[0], times(1));308 assertNoOtherListenerIsCalled();309}310function testDispatchEventWithCustomEventObject() {311 listen(eventTargets[0], EventType.A, listeners[0]);312 var e = new TestEvent();313 assertTrue(dispatchEvent(eventTargets[0], e));314 assertListenerIsCalled(listeners[0], times(1));315 assertNoOtherListenerIsCalled();316 var actualEvent = listeners[0].getLastCall().getArgument(0);317 assertEquals(e, actualEvent);318 assertEquals(eventTargets[0], actualEvent.target);319}320function testDisposingEventTargetRemovesListeners() {321 if (!(listenableFactory() instanceof goog.events.EventTarget)) {322 return;323 }324 listen(eventTargets[0], EventType.A, listeners[0]);325 goog.dispose(eventTargets[0]);326 dispatchEvent(eventTargets[0], EventType.A);327 assertNoOtherListenerIsCalled();328}329/**330 * Unlisten/unlistenByKey should still work after disposal. There are331 * many circumstances when this is actually necessary. For example, a332 * user may have listened to an event target and stored the key333 * (e.g. in a goog.events.EventHandler) and only unlisten after the334 * target has been disposed.335 */336function testUnlistenWorksAfterDisposal() {337 var key = listen(eventTargets[0], EventType.A, listeners[0]);338 goog.dispose(eventTargets[0]);339 unlisten(eventTargets[0], EventType.A, listeners[1]);340 if (unlistenByKey) {341 unlistenByKey(eventTargets[0], key);342 }343}344function testRemovingListener() {345 var ret1 = unlisten(eventTargets[0], EventType.A, listeners[0]);346 listen(eventTargets[0], EventType.A, listeners[0]);347 var ret2 = unlisten(eventTargets[0], EventType.A, listeners[1]);348 var ret3 = unlisten(eventTargets[0], EventType.B, listeners[0]);349 var ret4 = unlisten(eventTargets[1], EventType.A, listeners[0]);350 dispatchEvent(eventTargets[0], EventType.A);351 assertListenerIsCalled(listeners[0], times(1));352 var ret5 = unlisten(eventTargets[0], EventType.A, listeners[0]);353 var ret6 = unlisten(eventTargets[0], EventType.A, listeners[0]);354 dispatchEvent(eventTargets[0], EventType.A);355 assertListenerIsCalled(listeners[0], times(1));356 assertNoOtherListenerIsCalled();357 if (unlistenReturnType ==358 goog.events.eventTargetTester.UnlistenReturnType.BOOLEAN) {359 assertFalse(ret1);360 assertFalse(ret2);361 assertFalse(ret3);362 assertFalse(ret4);363 assertTrue(ret5);364 assertFalse(ret6);365 } else {366 assertUndefined(ret1);367 assertUndefined(ret2);368 assertUndefined(ret3);369 assertUndefined(ret4);370 assertUndefined(ret5);371 assertUndefined(ret6);372 }373}374function testCapture() {375 eventTargets[0].setParentEventTarget(eventTargets[1]);376 eventTargets[1].setParentEventTarget(eventTargets[2]);377 eventTargets[9].setParentEventTarget(eventTargets[0]);378 var ordering = 0;379 listeners[0] = createListener(380 function(e) {381 assertEquals(eventTargets[2], e.currentTarget);382 assertEquals(eventTargets[0], e.target);383 assertEquals('First capture listener is not called first', 0, ordering);384 ordering++;385 });386 listeners[1] = createListener(387 function(e) {388 assertEquals(eventTargets[1], e.currentTarget);389 assertEquals(eventTargets[0], e.target);390 assertEquals('2nd capture listener is not called 2nd', 1, ordering);391 ordering++;392 });393 listeners[2] = createListener(394 function(e) {395 assertEquals(eventTargets[0], e.currentTarget);396 assertEquals(eventTargets[0], e.target);397 assertEquals('3rd capture listener is not called 3rd', 2, ordering);398 ordering++;399 });400 listen(eventTargets[2], EventType.A, listeners[0], true);401 listen(eventTargets[1], EventType.A, listeners[1], true);402 listen(eventTargets[0], EventType.A, listeners[2], true);403 // These should not be called.404 listen(eventTargets[3], EventType.A, listeners[3], true);405 listen(eventTargets[0], EventType.B, listeners[4], true);406 listen(eventTargets[0], EventType.C, listeners[5], true);407 listen(eventTargets[1], EventType.B, listeners[6], true);408 listen(eventTargets[1], EventType.C, listeners[7], true);409 listen(eventTargets[2], EventType.B, listeners[8], true);410 listen(eventTargets[2], EventType.C, listeners[9], true);411 dispatchEvent(eventTargets[0], EventType.A);412 assertListenerIsCalled(listeners[0], times(1));413 assertListenerIsCalled(listeners[1], times(1));414 assertListenerIsCalled(listeners[2], times(1));415 assertNoOtherListenerIsCalled();416}417function testBubble() {418 eventTargets[0].setParentEventTarget(eventTargets[1]);419 eventTargets[1].setParentEventTarget(eventTargets[2]);420 eventTargets[9].setParentEventTarget(eventTargets[0]);421 var ordering = 0;422 listeners[0] = createListener(423 function(e) {424 assertEquals(eventTargets[0], e.currentTarget);425 assertEquals(eventTargets[0], e.target);426 assertEquals('First bubble listener is not called first', 0, ordering);427 ordering++;428 });429 listeners[1] = createListener(430 function(e) {431 assertEquals(eventTargets[1], e.currentTarget);432 assertEquals(eventTargets[0], e.target);433 assertEquals('2nd bubble listener is not called 2nd', 1, ordering);434 ordering++;435 });436 listeners[2] = createListener(437 function(e) {438 assertEquals(eventTargets[2], e.currentTarget);439 assertEquals(eventTargets[0], e.target);440 assertEquals('3rd bubble listener is not called 3rd', 2, ordering);441 ordering++;442 });443 listen(eventTargets[0], EventType.A, listeners[0]);444 listen(eventTargets[1], EventType.A, listeners[1]);445 listen(eventTargets[2], EventType.A, listeners[2]);446 // These should not be called.447 listen(eventTargets[3], EventType.A, listeners[3]);448 listen(eventTargets[0], EventType.B, listeners[4]);449 listen(eventTargets[0], EventType.C, listeners[5]);450 listen(eventTargets[1], EventType.B, listeners[6]);451 listen(eventTargets[1], EventType.C, listeners[7]);452 listen(eventTargets[2], EventType.B, listeners[8]);453 listen(eventTargets[2], EventType.C, listeners[9]);454 dispatchEvent(eventTargets[0], EventType.A);455 assertListenerIsCalled(listeners[0], times(1));456 assertListenerIsCalled(listeners[1], times(1));457 assertListenerIsCalled(listeners[2], times(1));458 assertNoOtherListenerIsCalled();459}460function testCaptureAndBubble() {461 eventTargets[0].setParentEventTarget(eventTargets[1]);462 eventTargets[1].setParentEventTarget(eventTargets[2]);463 listen(eventTargets[0], EventType.A, listeners[0], true);464 listen(eventTargets[1], EventType.A, listeners[1], true);465 listen(eventTargets[2], EventType.A, listeners[2], true);466 listen(eventTargets[0], EventType.A, listeners[3]);467 listen(eventTargets[1], EventType.A, listeners[4]);468 listen(eventTargets[2], EventType.A, listeners[5]);469 dispatchEvent(eventTargets[0], EventType.A);470 assertListenerIsCalled(listeners[0], times(1));471 assertListenerIsCalled(listeners[1], times(1));472 assertListenerIsCalled(listeners[2], times(1));473 assertListenerIsCalled(listeners[3], times(1));474 assertListenerIsCalled(listeners[4], times(1));475 assertListenerIsCalled(listeners[5], times(1));476 assertNoOtherListenerIsCalled();477}478function testPreventDefaultByReturningFalse() {479 listeners[0] = createListener(function(e) { return false; });480 listeners[1] = createListener(function(e) { return true; });481 listen(eventTargets[0], EventType.A, listeners[0]);482 listen(eventTargets[0], EventType.A, listeners[1]);483 var result = dispatchEvent(eventTargets[0], EventType.A);484 assertFalse(result);485}486function testPreventDefault() {487 listeners[0] = createListener(function(e) { e.preventDefault(); });488 listeners[1] = createListener(function(e) { return true; });489 listen(eventTargets[0], EventType.A, listeners[0]);490 listen(eventTargets[0], EventType.A, listeners[1]);491 var result = dispatchEvent(eventTargets[0], EventType.A);492 assertFalse(result);493}494function testPreventDefaultAtCapture() {495 listeners[0] = createListener(function(e) { e.preventDefault(); });496 listeners[1] = createListener(function(e) { return true; });497 listen(eventTargets[0], EventType.A, listeners[0], true);498 listen(eventTargets[0], EventType.A, listeners[1], true);499 var result = dispatchEvent(eventTargets[0], EventType.A);500 assertFalse(result);501}502function testStopPropagation() {503 eventTargets[0].setParentEventTarget(eventTargets[1]);504 eventTargets[1].setParentEventTarget(eventTargets[2]);505 listeners[0] = createListener(function(e) { e.stopPropagation(); });506 listen(eventTargets[0], EventType.A, listeners[0]);507 listen(eventTargets[0], EventType.A, listeners[1]);508 listen(eventTargets[1], EventType.A, listeners[2]);509 listen(eventTargets[2], EventType.A, listeners[3]);510 dispatchEvent(eventTargets[0], EventType.A);511 assertListenerIsCalled(listeners[0], times(1));512 assertListenerIsCalled(listeners[1], times(1));513 assertNoOtherListenerIsCalled();514}515function testStopPropagation2() {516 eventTargets[0].setParentEventTarget(eventTargets[1]);517 eventTargets[1].setParentEventTarget(eventTargets[2]);518 listeners[1] = createListener(function(e) { e.stopPropagation(); });519 listen(eventTargets[0], EventType.A, listeners[0]);520 listen(eventTargets[0], EventType.A, listeners[1]);521 listen(eventTargets[1], EventType.A, listeners[2]);522 listen(eventTargets[2], EventType.A, listeners[3]);523 dispatchEvent(eventTargets[0], EventType.A);524 assertListenerIsCalled(listeners[0], times(1));525 assertListenerIsCalled(listeners[1], times(1));526 assertNoOtherListenerIsCalled();527}528function testStopPropagation3() {529 eventTargets[0].setParentEventTarget(eventTargets[1]);530 eventTargets[1].setParentEventTarget(eventTargets[2]);531 listeners[2] = createListener(function(e) { e.stopPropagation(); });532 listen(eventTargets[0], EventType.A, listeners[0]);533 listen(eventTargets[0], EventType.A, listeners[1]);534 listen(eventTargets[1], EventType.A, listeners[2]);535 listen(eventTargets[2], EventType.A, listeners[3]);536 dispatchEvent(eventTargets[0], EventType.A);537 assertListenerIsCalled(listeners[0], times(1));538 assertListenerIsCalled(listeners[1], times(1));539 assertListenerIsCalled(listeners[2], times(1));540 assertNoOtherListenerIsCalled();541}542function testStopPropagationAtCapture() {543 eventTargets[0].setParentEventTarget(eventTargets[1]);544 eventTargets[1].setParentEventTarget(eventTargets[2]);545 listeners[0] = createListener(function(e) { e.stopPropagation(); });546 listen(eventTargets[2], EventType.A, listeners[0], true);547 listen(eventTargets[1], EventType.A, listeners[1], true);548 listen(eventTargets[0], EventType.A, listeners[2], true);549 listen(eventTargets[0], EventType.A, listeners[3]);550 listen(eventTargets[1], EventType.A, listeners[4]);551 listen(eventTargets[2], EventType.A, listeners[5]);552 dispatchEvent(eventTargets[0], EventType.A);553 assertListenerIsCalled(listeners[0], times(1));554 assertNoOtherListenerIsCalled();555}556function testHandleEvent() {557 if (!objectTypeListenerSupported) {558 return;559 }560 var obj = {};561 obj.handleEvent = goog.testing.recordFunction();562 listen(eventTargets[0], EventType.A, obj);563 dispatchEvent(eventTargets[0], EventType.A);564 assertEquals(1, obj.handleEvent.getCallCount());565}566function testListenOnce() {567 if (!listenOnce) {568 return;569 }570 listenOnce(eventTargets[0], EventType.A, listeners[0], true);571 listenOnce(eventTargets[0], EventType.A, listeners[1]);572 listenOnce(eventTargets[0], EventType.B, listeners[2]);573 dispatchEvent(eventTargets[0], EventType.A);574 assertListenerIsCalled(listeners[0], times(1));575 assertListenerIsCalled(listeners[1], times(1));576 assertListenerIsCalled(listeners[2], times(0));577 assertNoOtherListenerIsCalled();578 resetListeners();579 dispatchEvent(eventTargets[0], EventType.A);580 assertListenerIsCalled(listeners[0], times(0));581 assertListenerIsCalled(listeners[1], times(0));582 assertListenerIsCalled(listeners[2], times(0));583 dispatchEvent(eventTargets[0], EventType.B);584 assertListenerIsCalled(listeners[2], times(1));585 assertNoOtherListenerIsCalled();586}587function testUnlistenInListen() {588 listeners[1] = createListener(589 function(e) {590 unlisten(eventTargets[0], EventType.A, listeners[1]);591 unlisten(eventTargets[0], EventType.A, listeners[2]);592 });593 listen(eventTargets[0], EventType.A, listeners[0]);594 listen(eventTargets[0], EventType.A, listeners[1]);595 listen(eventTargets[0], EventType.A, listeners[2]);596 listen(eventTargets[0], EventType.A, listeners[3]);597 dispatchEvent(eventTargets[0], EventType.A);598 assertListenerIsCalled(listeners[0], times(1));599 assertListenerIsCalled(listeners[1], times(1));600 assertListenerIsCalled(listeners[2], times(0));601 assertListenerIsCalled(listeners[3], times(1));602 assertNoOtherListenerIsCalled();603 resetListeners();604 dispatchEvent(eventTargets[0], EventType.A);605 assertListenerIsCalled(listeners[0], times(1));606 assertListenerIsCalled(listeners[1], times(0));607 assertListenerIsCalled(listeners[2], times(0));608 assertListenerIsCalled(listeners[3], times(1));609 assertNoOtherListenerIsCalled();610}611function testUnlistenByKeyInListen() {612 if (!unlistenByKey) {613 return;614 }615 var key1, key2;616 listeners[1] = createListener(617 function(e) {618 unlistenByKey(eventTargets[0], key1);619 unlistenByKey(eventTargets[0], key2);620 });621 listen(eventTargets[0], EventType.A, listeners[0]);622 key1 = listen(eventTargets[0], EventType.A, listeners[1]);623 key2 = listen(eventTargets[0], EventType.A, listeners[2]);624 listen(eventTargets[0], EventType.A, listeners[3]);625 dispatchEvent(eventTargets[0], EventType.A);626 assertListenerIsCalled(listeners[0], times(1));627 assertListenerIsCalled(listeners[1], times(1));628 assertListenerIsCalled(listeners[2], times(0));629 assertListenerIsCalled(listeners[3], times(1));630 assertNoOtherListenerIsCalled();631 resetListeners();632 dispatchEvent(eventTargets[0], EventType.A);633 assertListenerIsCalled(listeners[0], times(1));634 assertListenerIsCalled(listeners[1], times(0));635 assertListenerIsCalled(listeners[2], times(0));636 assertListenerIsCalled(listeners[3], times(1));637 assertNoOtherListenerIsCalled();638}639function testSetParentEventTarget() {640 assertNull(eventTargets[0].getParentEventTarget());641 eventTargets[0].setParentEventTarget(eventTargets[1]);642 assertEquals(eventTargets[1], eventTargets[0].getParentEventTarget());643 assertNull(eventTargets[1].getParentEventTarget());644 eventTargets[0].setParentEventTarget(null);645 assertNull(eventTargets[0].getParentEventTarget());646}647function testListenOnceAfterListenDoesNotChangeExistingListener() {648 if (!listenOnce) {649 return;650 }651 listen(eventTargets[0], EventType.A, listeners[0]);652 listenOnce(eventTargets[0], EventType.A, listeners[0]);653 dispatchEvent(eventTargets[0], EventType.A);654 dispatchEvent(eventTargets[0], EventType.A);655 dispatchEvent(eventTargets[0], EventType.A);656 assertListenerIsCalled(listeners[0], times(3));657 assertNoOtherListenerIsCalled();658}659function testListenOnceAfterListenOnceDoesNotChangeExistingListener() {660 if (!listenOnce) {661 return;662 }663 listenOnce(eventTargets[0], EventType.A, listeners[0]);664 listenOnce(eventTargets[0], EventType.A, listeners[0]);665 dispatchEvent(eventTargets[0], EventType.A);666 dispatchEvent(eventTargets[0], EventType.A);667 dispatchEvent(eventTargets[0], EventType.A);668 assertListenerIsCalled(listeners[0], times(1));669 assertNoOtherListenerIsCalled();670}671function testListenAfterListenOnceRemoveOnceness() {672 if (!listenOnce) {673 return;674 }675 listenOnce(eventTargets[0], EventType.A, listeners[0]);676 listen(eventTargets[0], EventType.A, listeners[0]);677 dispatchEvent(eventTargets[0], EventType.A);678 dispatchEvent(eventTargets[0], EventType.A);679 dispatchEvent(eventTargets[0], EventType.A);680 assertListenerIsCalled(listeners[0], times(3));681 assertNoOtherListenerIsCalled();682}683function testUnlistenAfterListenOnce() {684 if (!listenOnce) {685 return;686 }687 listenOnce(eventTargets[0], EventType.A, listeners[0]);688 unlisten(eventTargets[0], EventType.A, listeners[0]);689 dispatchEvent(eventTargets[0], EventType.A);690 listen(eventTargets[0], EventType.A, listeners[0]);691 listenOnce(eventTargets[0], EventType.A, listeners[0]);692 unlisten(eventTargets[0], EventType.A, listeners[0]);693 dispatchEvent(eventTargets[0], EventType.A);694 listenOnce(eventTargets[0], EventType.A, listeners[0]);695 listen(eventTargets[0], EventType.A, listeners[0]);696 unlisten(eventTargets[0], EventType.A, listeners[0]);697 dispatchEvent(eventTargets[0], EventType.A);698 listenOnce(eventTargets[0], EventType.A, listeners[0]);699 listenOnce(eventTargets[0], EventType.A, listeners[0]);700 unlisten(eventTargets[0], EventType.A, listeners[0]);701 dispatchEvent(eventTargets[0], EventType.A);702 assertNoOtherListenerIsCalled();703}704function testRemoveAllWithType() {705 if (!removeAll) {706 return;707 }708 listen(eventTargets[0], EventType.A, listeners[0], true);709 listen(eventTargets[0], EventType.A, listeners[1]);710 listen(eventTargets[0], EventType.C, listeners[2], true);711 listen(eventTargets[0], EventType.C, listeners[3]);712 listen(eventTargets[0], EventType.B, listeners[4], true);713 listen(eventTargets[0], EventType.B, listeners[5], true);714 listen(eventTargets[0], EventType.B, listeners[6]);715 listen(eventTargets[0], EventType.B, listeners[7]);716 assertEquals(4, removeAll(eventTargets[0], EventType.B));717 dispatchEvent(eventTargets[0], EventType.A);718 dispatchEvent(eventTargets[0], EventType.B);719 dispatchEvent(eventTargets[0], EventType.C);720 assertListenerIsCalled(listeners[0], times(1));721 assertListenerIsCalled(listeners[1], times(1));722 assertListenerIsCalled(listeners[2], times(1));723 assertListenerIsCalled(listeners[3], times(1));724 assertNoOtherListenerIsCalled();725}726function testRemoveAll() {727 if (!removeAll) {728 return;729 }730 listen(eventTargets[0], EventType.A, listeners[0], true);731 listen(eventTargets[0], EventType.A, listeners[1]);732 listen(eventTargets[0], EventType.C, listeners[2], true);733 listen(eventTargets[0], EventType.C, listeners[3]);734 listen(eventTargets[0], EventType.B, listeners[4], true);735 listen(eventTargets[0], EventType.B, listeners[5], true);736 listen(eventTargets[0], EventType.B, listeners[6]);737 listen(eventTargets[0], EventType.B, listeners[7]);738 assertEquals(8, removeAll(eventTargets[0]));739 dispatchEvent(eventTargets[0], EventType.A);740 dispatchEvent(eventTargets[0], EventType.B);741 dispatchEvent(eventTargets[0], EventType.C);742 assertNoOtherListenerIsCalled();743}744function testRemoveAllCallsMarkAsRemoved() {745 if (!removeAll) {746 return;747 }748 var key0 = listen(eventTargets[0], EventType.A, listeners[0]);749 var key1 = listen(eventTargets[1], EventType.A, listeners[1]);750 assertNotNullNorUndefined(key0.listener);751 assertFalse(key0.removed);752 assertNotNullNorUndefined(key1.listener);753 assertFalse(key1.removed);754 assertEquals(1, removeAll(eventTargets[0]));755 assertNull(key0.listener);756 assertTrue(key0.removed);757 assertNotNullNorUndefined(key1.listener);758 assertFalse(key1.removed);759 assertEquals(1, removeAll(eventTargets[1]));760 assertNull(key1.listener);761 assertTrue(key1.removed);762}763function testGetListeners() {764 if (!getListeners) {765 return;766 }767 listen(eventTargets[0], EventType.A, listeners[0], true);768 listen(eventTargets[0], EventType.A, listeners[1], true);769 listen(eventTargets[0], EventType.A, listeners[2]);770 listen(eventTargets[0], EventType.A, listeners[3]);771 var l = getListeners(eventTargets[0], EventType.A, true);772 assertEquals(2, l.length);773 assertEquals(listeners[0], l[0].listener);774 assertEquals(listeners[1], l[1].listener);775 l = getListeners(eventTargets[0], EventType.A, false);776 assertEquals(2, l.length);777 assertEquals(listeners[2], l[0].listener);778 assertEquals(listeners[3], l[1].listener);779 l = getListeners(eventTargets[0], EventType.B, true);780 assertEquals(0, l.length);781}782function testGetListener() {783 if (!getListener) {784 return;785 }786 listen(eventTargets[0], EventType.A, listeners[0], true);787 assertNotNull(getListener(eventTargets[0], EventType.A, listeners[0], true));788 assertNull(789 getListener(eventTargets[0], EventType.A, listeners[0], true, {}));790 assertNull(getListener(eventTargets[1], EventType.A, listeners[0], true));791 assertNull(getListener(eventTargets[0], EventType.B, listeners[0], true));792 assertNull(getListener(eventTargets[0], EventType.A, listeners[1], true));793}794function testHasListener() {795 if (!hasListener) {796 return;797 }798 assertFalse(hasListener(eventTargets[0]));799 listen(eventTargets[0], EventType.A, listeners[0], true);800 assertTrue(hasListener(eventTargets[0]));801 assertTrue(hasListener(eventTargets[0], EventType.A));802 assertTrue(hasListener(eventTargets[0], EventType.A, true));803 assertTrue(hasListener(eventTargets[0], undefined, true));804 assertFalse(hasListener(eventTargets[0], EventType.A, false));805 assertFalse(hasListener(eventTargets[0], undefined, false));806 assertFalse(hasListener(eventTargets[0], EventType.B));807 assertFalse(hasListener(eventTargets[0], EventType.B, true));808 assertFalse(hasListener(eventTargets[1]));809}810function testFiringEventBeforeDisposeInternalWorks() {811 /**812 * @extends {goog.events.EventTarget}813 * @constructor814 */815 var MockTarget = function() {816 goog.base(this);817 };818 goog.inherits(MockTarget, goog.events.EventTarget);819 MockTarget.prototype.disposeInternal = function() {820 dispatchEvent(this, EventType.A);821 goog.base(this, 'disposeInternal');822 };823 var t = new MockTarget();824 try {825 listen(t, EventType.A, listeners[0]);826 t.dispose();827 assertListenerIsCalled(listeners[0], times(1));828 } catch (e) {829 goog.dispose(t);830 }831}832function testLoopDetection() {833 var target = listenableFactory();834 target.setParentEventTarget(target);835 try {836 target.dispatchEvent('string');837 fail('expected error');838 } catch (e) {839 assertContains('infinite', e.message);840 }...
PseudoEvents.js
Source:PseudoEvents.js
...708 if(UCF_KeyboardHelper.bIsValidHotkey(iKey, bCtrl, bAlt, bShift)) {709 if (iKey == KeyCodes.F1 && bNoModifiers) {710 //special handling for FF - in IE the help is handeled by onHelp711 if (UCF_System.sDevice == "ff1") {712 this.fireSapEvent(this.E_SAP_EVENTS.hotkey, oEvent);713 }714 }715 else if (bCtrlOnly && iKey == KeyCodes.C && document.selection) {716 //handle ctrl+c centrally if text is selected to allow to copy it instead of firing the hotkey717 var oTextRange = document.selection.createRange();718 if (!oTextRange || oTextRange.text.length <= 0) {719 this.fireSapEvent(this.E_SAP_EVENTS.hotkey, oEvent);720 }721 }722 else {723 this.fireSapEvent(this.E_SAP_EVENTS.hotkey, oEvent);724 }725 }726 */727 /*728 * Other pseudo events729 * @public730 */731 /**732 * Pseudo event indicating delayed double click (e.g. for inline edit)733 * @public734 */735 sapdelayeddoubleclick: {736 sName: "sapdelayeddoubleclick",737 aTypes: ["click"],...
event-target-shim.mjs
Source:event-target-shim.mjs
...77 * @constructor78 * @param {EventTarget} eventTarget The event target of this dispatching.79 * @param {Event|{type:string}} event The original event to wrap.80 */81function Event(eventTarget, event) {82 privateData.set(this, {83 eventTarget,84 event,85 eventPhase: 2,86 currentTarget: eventTarget,87 canceled: false,88 stopped: false,89 immediateStopped: false,90 passiveListener: null,91 timeStamp: event.timeStamp || Date.now(),92 });93 // https://heycam.github.io/webidl/#Unforgeable94 Object.defineProperty(this, "isTrusted", { value: false, enumerable: true });95 // Define accessors96 const keys = Object.keys(event);97 for (let i = 0; i < keys.length; ++i) {98 const key = keys[i];99 if (!(key in this)) {100 Object.defineProperty(this, key, defineRedirectDescriptor(key));101 }102 }103}104// Should be enumerable, but class methods are not enumerable.105Event.prototype = {106 /**107 * The type of this event.108 * @type {string}109 */110 get type() {111 return pd(this).event.type112 },113 /**114 * The target of this event.115 * @type {EventTarget}116 */117 get target() {118 return pd(this).eventTarget119 },120 /**121 * The target of this event.122 * @type {EventTarget}123 */124 get currentTarget() {125 return pd(this).currentTarget126 },127 /**128 * @returns {EventTarget[]} The composed path of this event.129 */130 composedPath() {131 const currentTarget = pd(this).currentTarget;132 if (currentTarget == null) {133 return []134 }135 return [currentTarget]136 },137 /**138 * Constant of NONE.139 * @type {number}140 */141 get NONE() {142 return 0143 },144 /**145 * Constant of CAPTURING_PHASE.146 * @type {number}147 */148 get CAPTURING_PHASE() {149 return 1150 },151 /**152 * Constant of AT_TARGET.153 * @type {number}154 */155 get AT_TARGET() {156 return 2157 },158 /**159 * Constant of BUBBLING_PHASE.160 * @type {number}161 */162 get BUBBLING_PHASE() {163 return 3164 },165 /**166 * The target of this event.167 * @type {number}168 */169 get eventPhase() {170 return pd(this).eventPhase171 },172 /**173 * Stop event bubbling.174 * @returns {void}175 */176 stopPropagation() {177 const data = pd(this);178 data.stopped = true;179 if (typeof data.event.stopPropagation === "function") {180 data.event.stopPropagation();181 }182 },183 /**184 * Stop event bubbling.185 * @returns {void}186 */187 stopImmediatePropagation() {188 const data = pd(this);189 data.stopped = true;190 data.immediateStopped = true;191 if (typeof data.event.stopImmediatePropagation === "function") {192 data.event.stopImmediatePropagation();193 }194 },195 /**196 * The flag to be bubbling.197 * @type {boolean}198 */199 get bubbles() {200 return Boolean(pd(this).event.bubbles)201 },202 /**203 * The flag to be cancelable.204 * @type {boolean}205 */206 get cancelable() {207 return Boolean(pd(this).event.cancelable)208 },209 /**210 * Cancel this event.211 * @returns {void}212 */213 preventDefault() {214 setCancelFlag(pd(this));215 },216 /**217 * The flag to indicate cancellation state.218 * @type {boolean}219 */220 get defaultPrevented() {221 return pd(this).canceled222 },223 /**224 * The flag to be composed.225 * @type {boolean}226 */227 get composed() {228 return Boolean(pd(this).event.composed)229 },230 /**231 * The unix time of this event.232 * @type {number}233 */234 get timeStamp() {235 return pd(this).timeStamp236 },237 /**238 * The target of this event.239 * @type {EventTarget}240 * @deprecated241 */242 get srcElement() {243 return pd(this).eventTarget244 },245 /**246 * The flag to stop event bubbling.247 * @type {boolean}248 * @deprecated249 */250 get cancelBubble() {251 return pd(this).stopped252 },253 set cancelBubble(value) {254 if (!value) {255 return256 }257 const data = pd(this);258 data.stopped = true;259 if (typeof data.event.cancelBubble === "boolean") {260 data.event.cancelBubble = true;261 }262 },263 /**264 * The flag to indicate cancellation state.265 * @type {boolean}266 * @deprecated267 */268 get returnValue() {269 return !pd(this).canceled270 },271 set returnValue(value) {272 if (!value) {273 setCancelFlag(pd(this));274 }275 },276 /**277 * Initialize this event object. But do nothing under event dispatching.278 * @param {string} type The event type.279 * @param {boolean} [bubbles=false] The flag to be possible to bubble up.280 * @param {boolean} [cancelable=false] The flag to be possible to cancel.281 * @deprecated282 */283 initEvent() {284 // Do nothing.285 },286};287// `constructor` is not enumerable.288Object.defineProperty(Event.prototype, "constructor", {289 value: Event,290 configurable: true,291 writable: true,292});293// Ensure `event instanceof window.Event` is `true`.294if (typeof window !== "undefined" && typeof window.Event !== "undefined") {295 Object.setPrototypeOf(Event.prototype, window.Event.prototype);296 // Make association for wrappers.297 wrappers.set(window.Event.prototype, Event);298}299/**300 * Get the property descriptor to redirect a given property.301 * @param {string} key Property name to define property descriptor.302 * @returns {PropertyDescriptor} The property descriptor to redirect the property.303 * @private304 */305function defineRedirectDescriptor(key) {306 return {307 get() {308 return pd(this).event[key]309 },310 set(value) {311 pd(this).event[key] = value;312 },313 configurable: true,314 enumerable: true,315 }316}317/**318 * Get the property descriptor to call a given method property.319 * @param {string} key Property name to define property descriptor.320 * @returns {PropertyDescriptor} The property descriptor to call the method property.321 * @private322 */323function defineCallDescriptor(key) {324 return {325 value() {326 const event = pd(this).event;327 return event[key].apply(event, arguments)328 },329 configurable: true,330 enumerable: true,331 }332}333/**334 * Define new wrapper class.335 * @param {Function} BaseEvent The base wrapper class.336 * @param {Object} proto The prototype of the original event.337 * @returns {Function} The defined wrapper class.338 * @private339 */340function defineWrapper(BaseEvent, proto) {341 const keys = Object.keys(proto);342 if (keys.length === 0) {343 return BaseEvent344 }345 /** CustomEvent */346 function CustomEvent(eventTarget, event) {347 BaseEvent.call(this, eventTarget, event);348 }349 CustomEvent.prototype = Object.create(BaseEvent.prototype, {350 constructor: { value: CustomEvent, configurable: true, writable: true },351 });352 // Define accessors.353 for (let i = 0; i < keys.length; ++i) {354 const key = keys[i];355 if (!(key in BaseEvent.prototype)) {356 const descriptor = Object.getOwnPropertyDescriptor(proto, key);357 const isFunc = typeof descriptor.value === "function";358 Object.defineProperty(359 CustomEvent.prototype,360 key,361 isFunc362 ? defineCallDescriptor(key)363 : defineRedirectDescriptor(key)364 );365 }366 }367 return CustomEvent368}369/**370 * Get the wrapper class of a given prototype.371 * @param {Object} proto The prototype of the original event to get its wrapper.372 * @returns {Function} The wrapper class.373 * @private374 */375function getWrapper(proto) {376 if (proto == null || proto === Object.prototype) {377 return Event378 }379 let wrapper = wrappers.get(proto);380 if (wrapper == null) {381 wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto);382 wrappers.set(proto, wrapper);383 }384 return wrapper385}386/**387 * Wrap a given event to management a dispatching.388 * @param {EventTarget} eventTarget The event target of this dispatching.389 * @param {Object} event The event to wrap.390 * @returns {Event} The wrapper instance.391 * @private392 */393function wrapEvent(eventTarget, event) {394 const Wrapper = getWrapper(Object.getPrototypeOf(event));395 return new Wrapper(eventTarget, event)396}397/**398 * Get the immediateStopped flag of a given event.399 * @param {Event} event The event to get.400 * @returns {boolean} The flag to stop propagation immediately.401 * @private402 */403function isStopped(event) {404 return pd(event).immediateStopped405}406/**407 * Set the current event phase of a given event.408 * @param {Event} event The event to set current target.409 * @param {number} eventPhase New event phase.410 * @returns {void}411 * @private412 */413function setEventPhase(event, eventPhase) {414 pd(event).eventPhase = eventPhase;415}416/**417 * Set the current target of a given event.418 * @param {Event} event The event to set current target.419 * @param {EventTarget|null} currentTarget New current target.420 * @returns {void}421 * @private422 */423function setCurrentTarget(event, currentTarget) {424 pd(event).currentTarget = currentTarget;425}426/**427 * Set a passive listener of a given event.428 * @param {Event} event The event to set current target.429 * @param {Function|null} passiveListener New passive listener.430 * @returns {void}431 * @private432 */433function setPassiveListener(event, passiveListener) {434 pd(event).passiveListener = passiveListener;435}436/**437 * @typedef {object} ListenerNode438 * @property {Function} listener439 * @property {1|2|3} listenerType440 * @property {boolean} passive441 * @property {boolean} once442 * @property {ListenerNode|null} next443 * @private444 */445/**446 * @type {WeakMap<object, Map<string, ListenerNode>>}447 * @private448 */449const listenersMap = new WeakMap();450// Listener types451const CAPTURE = 1;452const BUBBLE = 2;453const ATTRIBUTE = 3;454/**455 * Check whether a given value is an object or not.456 * @param {any} x The value to check.457 * @returns {boolean} `true` if the value is an object.458 */459function isObject(x) {460 return x !== null && typeof x === "object" //eslint-disable-line no-restricted-syntax461}462/**463 * Get listeners.464 * @param {EventTarget} eventTarget The event target to get.465 * @returns {Map<string, ListenerNode>} The listeners.466 * @private467 */468function getListeners(eventTarget) {469 const listeners = listenersMap.get(eventTarget);470 if (listeners == null) {471 throw new TypeError(472 "'this' is expected an EventTarget object, but got another value."473 )474 }475 return listeners476}477/**478 * Get the property descriptor for the event attribute of a given event.479 * @param {string} eventName The event name to get property descriptor.480 * @returns {PropertyDescriptor} The property descriptor.481 * @private482 */483function defineEventAttributeDescriptor(eventName) {484 return {485 get() {486 const listeners = getListeners(this);487 let node = listeners.get(eventName);488 while (node != null) {489 if (node.listenerType === ATTRIBUTE) {490 return node.listener491 }492 node = node.next;493 }494 return null495 },496 set(listener) {497 if (typeof listener !== "function" && !isObject(listener)) {498 listener = null; // eslint-disable-line no-param-reassign499 }500 const listeners = getListeners(this);501 // Traverse to the tail while removing old value.502 let prev = null;503 let node = listeners.get(eventName);504 while (node != null) {505 if (node.listenerType === ATTRIBUTE) {506 // Remove old value.507 if (prev !== null) {508 prev.next = node.next;509 } else if (node.next !== null) {510 listeners.set(eventName, node.next);511 } else {512 listeners.delete(eventName);513 }514 } else {515 prev = node;516 }517 node = node.next;518 }519 // Add new value.520 if (listener !== null) {521 const newNode = {522 listener,523 listenerType: ATTRIBUTE,524 passive: false,525 once: false,526 next: null,527 };528 if (prev === null) {529 listeners.set(eventName, newNode);530 } else {531 prev.next = newNode;532 }533 }534 },535 configurable: true,536 enumerable: true,537 }538}539/**540 * Define an event attribute (e.g. `eventTarget.onclick`).541 * @param {Object} eventTargetPrototype The event target prototype to define an event attrbite.542 * @param {string} eventName The event name to define.543 * @returns {void}544 */545function defineEventAttribute(eventTargetPrototype, eventName) {546 Object.defineProperty(547 eventTargetPrototype,548 `on${eventName}`,549 defineEventAttributeDescriptor(eventName)550 );551}552/**553 * Define a custom EventTarget with event attributes.554 * @param {string[]} eventNames Event names for event attributes.555 * @returns {EventTarget} The custom EventTarget.556 * @private557 */558function defineCustomEventTarget(eventNames) {559 /** CustomEventTarget */560 function CustomEventTarget() {561 EventTarget.call(this);562 }563 CustomEventTarget.prototype = Object.create(EventTarget.prototype, {564 constructor: {565 value: CustomEventTarget,566 configurable: true,567 writable: true,568 },569 });570 for (let i = 0; i < eventNames.length; ++i) {571 defineEventAttribute(CustomEventTarget.prototype, eventNames[i]);572 }573 return CustomEventTarget574}575/**576 * EventTarget.577 *578 * - This is constructor if no arguments.579 * - This is a function which returns a CustomEventTarget constructor if there are arguments.580 *581 * For example:582 *583 * class A extends EventTarget {}584 * class B extends EventTarget("message") {}585 * class C extends EventTarget("message", "error") {}586 * class D extends EventTarget(["message", "error"]) {}587 */588function EventTarget() {589 /*eslint-disable consistent-return */590 if (this instanceof EventTarget) {591 listenersMap.set(this, new Map());592 return593 }594 if (arguments.length === 1 && Array.isArray(arguments[0])) {595 return defineCustomEventTarget(arguments[0])596 }597 if (arguments.length > 0) {598 const types = new Array(arguments.length);599 for (let i = 0; i < arguments.length; ++i) {600 types[i] = arguments[i];601 }602 return defineCustomEventTarget(types)603 }604 throw new TypeError("Cannot call a class as a function")605 /*eslint-enable consistent-return */606}607// Should be enumerable, but class methods are not enumerable.608EventTarget.prototype = {609 /**610 * Add a given listener to this event target.611 * @param {string} eventName The event name to add.612 * @param {Function} listener The listener to add.613 * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.614 * @returns {void}615 */616 addEventListener(eventName, listener, options) {617 if (listener == null) {618 return619 }620 if (typeof listener !== "function" && !isObject(listener)) {621 throw new TypeError("'listener' should be a function or an object.")622 }623 const listeners = getListeners(this);624 const optionsIsObj = isObject(options);625 const capture = optionsIsObj626 ? Boolean(options.capture)627 : Boolean(options);628 const listenerType = capture ? CAPTURE : BUBBLE;629 const newNode = {630 listener,631 listenerType,632 passive: optionsIsObj && Boolean(options.passive),633 once: optionsIsObj && Boolean(options.once),634 next: null,635 };636 // Set it as the first node if the first node is null.637 let node = listeners.get(eventName);638 if (node === undefined) {639 listeners.set(eventName, newNode);640 return641 }642 // Traverse to the tail while checking duplication..643 let prev = null;644 while (node != null) {645 if (646 node.listener === listener &&647 node.listenerType === listenerType648 ) {649 // Should ignore duplication.650 return651 }652 prev = node;653 node = node.next;654 }655 // Add it.656 prev.next = newNode;657 },658 /**659 * Remove a given listener from this event target.660 * @param {string} eventName The event name to remove.661 * @param {Function} listener The listener to remove.662 * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.663 * @returns {void}664 */665 removeEventListener(eventName, listener, options) {666 if (listener == null) {667 return668 }669 const listeners = getListeners(this);670 const capture = isObject(options)671 ? Boolean(options.capture)672 : Boolean(options);673 const listenerType = capture ? CAPTURE : BUBBLE;674 let prev = null;675 let node = listeners.get(eventName);676 while (node != null) {677 if (678 node.listener === listener &&679 node.listenerType === listenerType680 ) {681 if (prev !== null) {682 prev.next = node.next;683 } else if (node.next !== null) {684 listeners.set(eventName, node.next);685 } else {686 listeners.delete(eventName);687 }688 return689 }690 prev = node;691 node = node.next;692 }693 },694 /**695 * Dispatch a given event.696 * @param {Event|{type:string}} event The event to dispatch.697 * @returns {boolean} `false` if canceled.698 */699 dispatchEvent(event) {700 if (event == null || typeof event.type !== "string") {701 throw new TypeError('"event.type" should be a string.')702 }703 // If listeners aren't registered, terminate.704 const listeners = getListeners(this);705 const eventName = event.type;706 let node = listeners.get(eventName);707 if (node == null) {708 return true709 }710 // Since we cannot rewrite several properties, so wrap object.711 const wrappedEvent = wrapEvent(this, event);712 // This doesn't process capturing phase and bubbling phase.713 // This isn't participating in a tree.714 let prev = null;715 while (node != null) {716 // Remove this listener if it's once717 if (node.once) {718 if (prev !== null) {719 prev.next = node.next;720 } else if (node.next !== null) {721 listeners.set(eventName, node.next);722 } else {723 listeners.delete(eventName);724 }725 } else {726 prev = node;727 }728 // Call this listener729 setPassiveListener(730 wrappedEvent,731 node.passive ? node.listener : null732 );733 if (typeof node.listener === "function") {734 try {735 node.listener.call(this, wrappedEvent);736 } catch (err) {737 if (738 typeof console !== "undefined" &&739 typeof console.error === "function"740 ) {741 console.error(err);742 }743 }744 } else if (745 node.listenerType !== ATTRIBUTE &&746 typeof node.listener.handleEvent === "function"747 ) {748 node.listener.handleEvent(wrappedEvent);749 }750 // Break if `event.stopImmediatePropagation` was called.751 if (isStopped(wrappedEvent)) {752 break753 }754 node = node.next;755 }756 setPassiveListener(wrappedEvent, null);757 setEventPhase(wrappedEvent, 0);758 setCurrentTarget(wrappedEvent, null);759 return !wrappedEvent.defaultPrevented760 },761};762// `constructor` is not enumerable....
BMapLib.js
Source:BMapLib.js
...26 if (instance.addEventListener) {27 instance.addEventListener(eventName, handler, false);28 }29 else if (instance.attachEvent) {30 instance.attachEvent('on' + eventName, handler);31 }32 else {33 instance['on' + eventName] = handler;34 }35 return new MapsEventListener(instance, eventName, handler, MapsEventListener.DOM_EVENT);36};37/**38 * 娣诲å§DOMæµå¬©æ¬¢é©ææéè¥æéå±½å±éé¢ç²éµÑîæ¶â¬å¨ï¿½39 * @param {HTMLElement} DOMéåªç¤40 * @param {String} æµå¬©æ¬¢éå¶Ð41 * @param {Function} æµå¬©æ¬¢æ¾¶åæéè¥æ42 * @returns {MapsEventListener} æµå¬©æ¬¢é©ææçµç¡
è43 */44EventWrapper['addDomListenerOnce'] = function(instance, eventName, handler) {45 var eventListener = EventWrapper['addDomListener'](instance, eventName, function(){46 // ç»å©æ«47 EventWrapper['removeListener'](eventListener);48 return handler.apply(this, arguments);49 });50 return eventListener;51};52/**53 * 娣诲å§é¦æ¿æµæµå¬©æ¬¢é©ææéè¥æ54 * @param {Object} ç¹ç°ç·¥55 * @param {String} æµå¬©æ¬¢éå¶Ð56 * @param {Function} æµå¬©æ¬¢æ¾¶åæéè¥æ57 * @returns {MapsEventListener} æµå¬©æ¬¢é©ææçµç¡
è58 */59EventWrapper['addListener'] = function(instance, eventName, handler) {60 instance.addEventListener(eventName, handler);61 return new MapsEventListener(instance, eventName, handler, MapsEventListener.MAP_EVENT);62};63/**64 * 娣诲å§é¦æ¿æµæµå¬©æ¬¢é©ææéè¥æéå±½å±éé¢ç²éµÑîæ¶â¬å¨ï¿½65 * @param {Object} éâ¬ç佺æ´éîæ®ç¹ç°ç·¥66 * @param {String} æµå¬©æ¬¢éå¶Ð67 * @param {Function} æµå¬©æ¬¢æ¾¶åæéè¥æ68 * @returns {MapsEventListener} æµå¬©æ¬¢é©ææçµç¡
è69 */70EventWrapper['addListenerOnce'] = function(instance, eventName, handler){71 var eventListener = EventWrapper['addListener'](instance, eventName, function(){72 // ç»å©æ«73 EventWrapper['removeListener'](eventListener);74 return handler.apply(this, arguments);75 });76 return eventListener;77};78/**79 * ç»å©æ«éç°ç¾ç¹ç°ç·¥é¨å¬å¢éå¤ç°¨æµ åæ®éµâ¬éå¤æ´éîå±é�80 * @param {Object} éâ¬ç佺Щéãå¢éå¤ç°¨æµ åæ´éîæ®ç¹ç°ç·¥81 * @returns {None}82 */83EventWrapper['clearInstanceListeners'] = function(instance) {84 var listeners = instance._e_ || {};85 for (var i in listeners) {86 EventWrapper['removeListener'](listeners[i]);87 }88 instance._e_ = {};89};90/**91 * ç»å©æ«éç°ç¾ç¹ç°ç·¥éç°ç¾æµå¬©æ¬¢é¨å¬å¢éå¤æ´éîå±é�92 * @param {Object} éâ¬ç佺Щéãå£ç¹æ°«ç°¨æµ åæ´éîæ®ç¹ç°ç·¥93 * @param {String} éâ¬ç佺Щéãæ®æµå¬©æ¬¢é�94 * @returns {None}95 */96EventWrapper['clearListeners'] = function(instance, eventName) {97 var listeners = instance._e_ || {};98 for (var i in listeners) {99 if (listeners[i]._eventName == eventName) {100 EventWrapper['removeListener'](listeners[i]);101 }102 }103};104/**105 * ç»å©æ«éç°ç¾é¨åªç°¨æµ åæ´éîå±é�106 * @param {MapsEventListener} éâ¬ç佺Щéãæ®æµå¬©æ¬¢é©ææçµç¡
è107 * @returns {None}108 */109EventWrapper['removeListener'] = function(listener) {110 var instance = listener._instance;111 var eventName = listener._eventName;112 var handler = listener._handler;113 var listeners = instance._e_ || {};114 for (var i in listeners) {115 if (listeners[i]._guid == listener._guid) {116 if (listener._eventType == MapsEventListener.DOM_EVENT) {117 // DOMæµå¬©æ¬¢118 if (instance.removeEventListener) {119 instance.removeEventListener(eventName, handler, false);120 }121 else if (instance.detachEvent) {122 instance.detachEvent('on' + eventName, handler);123 }124 else {125 instance['on' + eventName] = null;126 }127 }128 else if (listener._eventType == MapsEventListener.MAP_EVENT) {129 // é¦æ¿æµæµå¬©æ¬¢130 instance.removeEventListener(eventName, handler);131 }132 delete listeners[i];133 }134 }135};136/**...
midifile.js
Source:midifile.js
...14 }15 16 var lastEventTypeByte;17 18 function readEvent(stream) {19 var event = {};20 event.deltaTime = stream.readVarInt();21 var eventTypeByte = stream.readInt8();22 if ((eventTypeByte & 0xf0) == 0xf0) {23 /* system / meta event */24 if (eventTypeByte == 0xff) {25 /* meta event */26 event.type = 'meta';27 var subtypeByte = stream.readInt8();28 var length = stream.readVarInt();29 switch(subtypeByte) {30 case 0x00:31 event.subtype = 'sequenceNumber';32 if (length != 2) throw "Expected length for sequenceNumber event is 2, got " + length;33 event.number = stream.readInt16();34 return event;35 case 0x01:36 event.subtype = 'text';37 event.text = stream.read(length);38 return event;39 case 0x02:40 event.subtype = 'copyrightNotice';41 event.text = stream.read(length);42 return event;43 case 0x03:44 event.subtype = 'trackName';45 event.text = stream.read(length);46 return event;47 case 0x04:48 event.subtype = 'instrumentName';49 event.text = stream.read(length);50 return event;51 case 0x05:52 event.subtype = 'lyrics';53 event.text = stream.read(length);54 return event;55 case 0x06:56 event.subtype = 'marker';57 event.text = stream.read(length);58 return event;59 case 0x07:60 event.subtype = 'cuePoint';61 event.text = stream.read(length);62 return event;63 case 0x20:64 event.subtype = 'midiChannelPrefix';65 if (length != 1) throw "Expected length for midiChannelPrefix event is 1, got " + length;66 event.channel = stream.readInt8();67 return event;68 case 0x2f:69 event.subtype = 'endOfTrack';70 if (length != 0) throw "Expected length for endOfTrack event is 0, got " + length;71 return event;72 case 0x51:73 event.subtype = 'setTempo';74 if (length != 3) throw "Expected length for setTempo event is 3, got " + length;75 event.microsecondsPerBeat = (76 (stream.readInt8() << 16)77 + (stream.readInt8() << 8)78 + stream.readInt8()79 )80 return event;81 case 0x54:82 event.subtype = 'smpteOffset';83 if (length != 5) throw "Expected length for smpteOffset event is 5, got " + length;84 var hourByte = stream.readInt8();85 event.frameRate = {86 0x00: 24, 0x20: 25, 0x40: 29, 0x60: 3087 }[hourByte & 0x60];88 event.hour = hourByte & 0x1f;89 event.min = stream.readInt8();90 event.sec = stream.readInt8();91 event.frame = stream.readInt8();92 event.subframe = stream.readInt8();93 return event;94 case 0x58:95 event.subtype = 'timeSignature';96 if (length != 4) throw "Expected length for timeSignature event is 4, got " + length;97 event.numerator = stream.readInt8();98 event.denominator = Math.pow(2, stream.readInt8());99 event.metronome = stream.readInt8();100 event.thirtyseconds = stream.readInt8();101 return event;102 case 0x59:103 event.subtype = 'keySignature';104 if (length != 2) throw "Expected length for keySignature event is 2, got " + length;105 event.key = stream.readInt8(true);106 event.scale = stream.readInt8();107 return event;108 case 0x7f:109 event.subtype = 'sequencerSpecific';110 event.data = stream.read(length);111 return event;112 default:113 // console.log("Unrecognised meta event subtype: " + subtypeByte);114 event.subtype = 'unknown'115 event.data = stream.read(length);116 return event;117 }118 event.data = stream.read(length);119 return event;120 } else if (eventTypeByte == 0xf0) {121 event.type = 'sysEx';122 var length = stream.readVarInt();123 event.data = stream.read(length);124 return event;125 } else if (eventTypeByte == 0xf7) {126 event.type = 'dividedSysEx';127 var length = stream.readVarInt();128 event.data = stream.read(length);129 return event;130 } else {131 throw "Unrecognised MIDI event type byte: " + eventTypeByte;132 }133 } else {134 /* channel event */135 var param1;136 if ((eventTypeByte & 0x80) == 0) {137 /* running status - reuse lastEventTypeByte as the event type.138 eventTypeByte is actually the first parameter139 */140 param1 = eventTypeByte;141 eventTypeByte = lastEventTypeByte;142 } else {143 param1 = stream.readInt8();144 lastEventTypeByte = eventTypeByte;145 }146 var eventType = eventTypeByte >> 4;147 event.channel = eventTypeByte & 0x0f;148 event.type = 'channel';149 switch (eventType) {150 case 0x08:151 event.subtype = 'noteOff';152 event.noteNumber = param1;153 event.velocity = stream.readInt8();154 return event;155 case 0x09:156 event.noteNumber = param1;157 event.velocity = stream.readInt8();158 if (event.velocity == 0) {159 event.subtype = 'noteOff';160 } else {161 event.subtype = 'noteOn';162 }163 return event;164 case 0x0a:165 event.subtype = 'noteAftertouch';166 event.noteNumber = param1;167 event.amount = stream.readInt8();168 return event;169 case 0x0b:170 event.subtype = 'controller';171 event.controllerType = param1;172 event.value = stream.readInt8();173 return event;174 case 0x0c:175 event.subtype = 'programChange';176 event.programNumber = param1;177 return event;178 case 0x0d:179 event.subtype = 'channelAftertouch';180 event.amount = param1;181 return event;182 case 0x0e:183 event.subtype = 'pitchBend';184 event.value = param1 + (stream.readInt8() << 7);185 return event;186 default:187 throw "Unrecognised MIDI event type: " + eventType188 /* 189 console.log("Unrecognised MIDI event type: " + eventType);190 stream.readInt8();191 event.subtype = 'unknown';192 return event;193 */194 }195 }196 }197 198 stream = Stream(data);199 var headerChunk = readChunk(stream);200 if (headerChunk.id != 'MThd' || headerChunk.length != 6) {201 throw "Bad .mid file - header not found";202 }203 var headerStream = Stream(headerChunk.data);204 var formatType = headerStream.readInt16();205 var trackCount = headerStream.readInt16();206 var timeDivision = headerStream.readInt16();207 208 if (timeDivision & 0x8000) {209 throw "Expressing time division in SMTPE frames is not supported yet"210 } else {211 ticksPerBeat = timeDivision;212 }213 214 var header = {215 'formatType': formatType,216 'trackCount': trackCount,217 'ticksPerBeat': ticksPerBeat218 }219 var tracks = [];220 for (var i = 0; i < header.trackCount; i++) {221 tracks[i] = [];222 var trackChunk = readChunk(stream);223 if (trackChunk.id != 'MTrk') {224 throw "Unexpected chunk - expected MTrk, got "+ trackChunk.id;225 }226 var trackStream = Stream(trackChunk.data);227 while (!trackStream.eof()) {228 var event = readEvent(trackStream);229 tracks[i].push(event);230 //console.log(event);231 }232 }233 234 return {235 'header': header,236 'tracks': tracks237 }...
DomEventMixin.js
Source:DomEventMixin.js
...59 // if such entry exist already - detach it first60 var oldRegister = this.GetHandlerRegister(element, eventName, eventHandlerName);61 if (oldRegister != null)62 {63 this.DetachDomEvent(oldRegister.Element, oldRegister.EventName, eventHandlerName);64 }65 66 // register the new entry67 var eventRegister = { 68 "Element" : element, 69 "EventName" : eventName, 70 "HandlerName" : eventHandlerName, 71 "Handler" : eventHandler 72 };73 this.AddHandlerRegister(eventRegister);74 75 // now do the "real" job76 if (element.addEventListener)77 {78 element.addEventListener(eventName, eventHandler, false);79 }80 else if (element.attachEvent)81 {82 element.attachEvent("on" + eventName, eventHandler);83 }84 },85 86 87 DetachDomEvent : function(element, eventName, eventHandler)88 {89 var eventRegister = null;90 var eventHandlerName = "";91 if (typeof eventHandler == "string") 92 {93 eventHandlerName = eventHandler;94 eventRegister = this.GetHandlerRegister(element, eventName, eventHandlerName);95 if(eventRegister == null)96 return;97 eventHandler = eventRegister.Handler;98 }99 100 if (!element)101 {102 return;103 }104 105 if (element.removeEventListener)106 {107 element.removeEventListener(eventName, eventHandler, false);108 }109 else if (element.detachEvent)110 {111 element.detachEvent("on" + eventName, eventHandler);112 }113 114 if (eventRegister != null && eventHandlerName != "")115 {116 this.RemoveHandlerRegister(eventRegister);117 eventRegister = null;118 }119 },120 121 DisposeDomEventHandlers : function()122 {123 for (var i=0; i < this.handlerRegisters.length; i ++)124 {125 var eventRegister = this.handlerRegisters[i];126 if (eventRegister != null)127 {128 this.DetachDomEvent(129 eventRegister.Element, 130 eventRegister.EventName, 131 eventRegister.Handler);132 }133 }134 135 this.handlerRegisters = [];136 },137 RemoveHandlerRegister : function(eventRegister)138 {139 try {140 var regIndex = eventRegister.index;141 for (var i in eventRegister)142 {...
translation_events.js
Source:translation_events.js
...12Blockly.TranslationEvents.haveEvent = function(eventName) {13 return Blockly.TranslationEvents.map[eventName] != undefined;14}15Blockly.TranslationEvents.addEvent = function(eventName, eventValue, eventDescription) {16 if (!Blockly.TranslationEvents.haveEvent(eventName)) {17 Blockly.TranslationEvents.map[eventName] = [eventValue, eventDescription];18 }19}20/**21 * eventList is an array list of event with format [eventValue, eventName, eventDescription]22 */23Blockly.TranslationEvents.addEvents = function(eventList) {24 for(var i = 0; i < eventList.length; i++) {25 var eventValue = eventList[i][0];26 var eventName = eventList[i][1];27 var eventDescription = eventList[i][2];28 Blockly.TranslationEvents.addEvent(eventName, eventValue, eventDescription);29 }30}31Blockly.TranslationEvents.updateEvent = function(eventName, eventValue, eventDescription) {32 if (Blockly.TranslationEvents.haveEvent(eventName)) {33 var decription = eventDescription;34 if (eventDescription == '') {35 decription = Blockly.TranslationEvents.map[eventName][1];36 }37 Blockly.TranslationEvents.map[eventName] = [eventValue, decription];38 } else {39 Blockly.TranslationEvents.addEvent(eventName, eventValue, eventDescription);40 }41}42/**43 * eventList is an array list of event with format [eventValue, eventName]44 */45Blockly.TranslationEvents.updateEvents = function(eventList) {46 for(var i = 0; i < eventList.length; i++) {47 var eventValue = eventList[i][0];48 var eventName = eventList[i][1];49 var eventDescription = eventList[i][2];50 Blockly.TranslationEvents.updateEvent(eventName, eventValue, eventDescription);51 }52}53Blockly.TranslationEvents.getEventValue = function(eventName) {54 if (!Blockly.TranslationEvents.haveEvent(eventName)) {55 return '';56 }57 return Blockly.TranslationEvents.map[eventName][0];58}59Blockly.TranslationEvents.getEventDescription = function(eventName) {60 if (!Blockly.TranslationEvents.haveEvent(eventName)) {61 return '';62 }63 return Blockly.TranslationEvents.map[eventName][1];64}65/**66 * Update event map 67 *68 * { "key": "EVENT-NAME",69 * "value": "EVENT-VALUE",70 * "decription": "EVENT-DESCRIPTION",71 * }72 *73 */74Blockly.TranslationEvents.updateEventMap = function(typeDescription) {75 var eventName = typeDescription.key;76 var eventValue = typeDescription.value;77 var eventDescription = typeDescription.description;78 Blockly.TranslationEvents.updateEvent(eventName, eventValue, eventDescription); 79}80/**81 * Update event map 82 *83 * typeJsonString has the following format (where upper-case strings are84 * non-terminals and lower-case strings are literals):85 * [86 * { "key": "EVENT-NAME",87 * "value": "EVENT-VALUE",88 * "decription": "EVENT-DESCRIPTION",89 * },+90 * ]91 */92Blockly.TranslationEvents.updateMap = function(jsonString) {...
jquery.tap.js
Source:jquery.tap.js
1(function($, specialEventName) {2 'use strict';3 /**4 * Native event names for creating custom one.5 *6 * @type {Object}7 */8 var nativeEvent = Object.create(null);9 /**10 * Get current time.11 *12 * @return {Number}13 */14 var getTime = function() {15 return new Date().getTime();16 };17 nativeEvent.original = 'click';18 if ('ontouchstart' in document) {19 nativeEvent.start = 'touchstart';20 nativeEvent.end = 'touchend';21 } else {22 nativeEvent.start = 'mousedown';23 nativeEvent.end = 'mouseup';24 }25 $.event.special[specialEventName] = {26 setup: function(data, namespaces, eventHandle) {27 var $element = $(this);28 var eventData = {};29 $element30 // Remove all handlers that were set for an original event.31 .off(nativeEvent.original)32 // Prevent default actions.33 .on(nativeEvent.original, false)34 // Split original event by two different and collect an information35 // on every phase.36 .on(nativeEvent.start + ' ' + nativeEvent.end, function(event) {37 // Handle the event system of touchscreen devices.38 eventData.event = event.originalEvent.changedTouches ? event.originalEvent.changedTouches[0] : event;39 })40 .on(nativeEvent.start, function(event) {41 // Stop execution if an event is simulated.42 if (event.which && event.which !== 1) {43 return;44 }45 eventData.target = event.target;46 eventData.pageX = eventData.event.pageX;47 eventData.pageY = eventData.event.pageY;48 eventData.time = getTime();49 })50 .on(nativeEvent.end, function(event) {51 // Compare properties from two phases.52 if (53 // The target should be the same.54 eventData.target === event.target &&55 // Time between first and last phases should be less than 750 ms.56 getTime() - eventData.time < 750 &&57 // Coordinates, when event ends, should be the same as they were58 // on start.59 (60 eventData.pageX === eventData.event.pageX &&61 eventData.pageY === eventData.event.pageY62 )63 ) {64 event.type = specialEventName;65 event.pageX = eventData.event.pageX;66 event.pageY = eventData.event.pageY;67 eventHandle.call(this, event);68 // If an event wasn't prevented then execute original actions.69 if (!event.isDefaultPrevented()) {70 $element71 // Remove prevention of default actions.72 .off(nativeEvent.original)73 // Bring the action.74 .trigger(nativeEvent.original);75 }76 }77 });78 },79 remove: function() {80 $(this).off(nativeEvent.start + ' ' + nativeEvent.end);81 }82 };83 $.fn[specialEventName] = function(fn) {84 return this[fn ? 'on' : 'trigger'](specialEventName, fn);85 };...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.waitForSelector('.navbar__inner');7 await page.click('text=Docs');8 await page.waitForSelector('text=API');
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 const [response] = await Promise.all([7 page.waitForResponse('**/api/v1/interactions'),8 page.evaluate(() => {9 window.dispatchEvent(new Event('click'));10 }),11 ]);12 console.log(response.url());13 await browser.close();14})();15const { chromium } = require('playwright');16(async () => {17 const browser = await chromium.launch();18 const context = await browser.newContext();19 const page = await context.newPage();20 const [response] = await Promise.all([21 page.waitForResponse('**/api/v1/interactions'),22 page.dispatchEvent('click'),23 ]);24 console.log(response.url());25 await browser.close();26})();27const { chromium } = require('playwright');28(async () => {29 const browser = await chromium.launch();30 const context = await browser.newContext();31 const page = await context.newPage();32 const [response] = await Promise.all([33 page.waitForResponse('**/api/v1/interactions'),34 page.dispatchEvent('click'),35 ]);36 console.log(response.url());37 await browser.close();38})();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.evaluate(() => {7 window.test = () => {8 console.log('test');9 };10 });11 await page.exposeFunction('test', () => {12 console.log('test');13 });14 await page.evaluate(() => {15 window.test();16 test();17 });18 await browser.close();19})();20const { chromium } = require('playwright');21(async () => {22 const browser = await chromium.launch();23 const context = await browser.newContext();24 const page = await context.newPage();25 await page.evaluate(() => {26 window.test = () => {27 console.log('test');28 };29 });30 await page.exposeFunction('test', () => {31 console.log('test');32 });33 await page.evaluate(() => {34 window.test();35 test();36 });37 await browser.close();38})();39Is it possible to use the page.on() method with the Page.evaluate() method?40Is it possible to use the page.on() method with the Page.evaluate() method?41Is it possible to use the page.on() method with the Page.evaluate() method?42Is it possible to use the page.on() method with the Page.evaluate() method?43Is it possible to use the page.on() method with the Page.evaluate() method?44I am trying to use the page.on() method with the Page
Using AI Code Generation
1const {EventEmitter} = require('events');2class MyEmitter extends EventEmitter {}3const myEmitter = new MyEmitter();4myEmitter.on('event', () => {5 console.log('an event occurred!');6});7myEmitter.emit('event');8const {EventEmitter} = require('playwright');9class MyEmitter extends EventEmitter {}10const myEmitter = new MyEmitter();11myEmitter.on('event', () => {12 console.log('an event occurred!');13});14myEmitter.emit('event');15const {EventEmitter} = require('events');16class MyEmitter extends EventEmitter {}17const myEmitter = new MyEmitter();18myEmitter.on('event', () => {19 console.log('an event occurred!');20});21myEmitter.emit('event');22 at Object.<anonymous> (C:\Users\test\test3.js:12:10)23 at Module._compile (internal/modules/cjs/loader.js:1063:30)24 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)25 at Module.load (internal/modules/cjs/loader.js:928:32)26 at Function.Module._load (internal/modules/cjs/loader.js:769:14)27 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Using AI Code Generation
1const {Event} = require('playwright');2const event = new Event('myEvent', {bubbles: true, cancelable: true});3console.log(event);4console.log(event.bubbles);5console.log(event.cancelable);6console.log(event.composed);7console.log(event.defaultPrevented);8console.log(event.eventPhase);9console.log(event.isTrusted);10console.log(event.returnValue);11console.log(event.srcElement);12console.log(event.target);13console.log(event.timeStamp);14console.log(event.type);15console.log(event.isDefaultPrevented());16console.log(event.isPropagationStopped());17console.log(event.preventDefault());18console.log(event.stopImmediatePropagation());19console.log(event.stopPropagation());20Event {21}22Event {23}24Event {25}
Using AI Code Generation
1const { Event } = require('@playwright/test');2const event = new Event();3event.on('myEvent', () => console.log('myEvent fired'));4event.emit('myEvent');5event.off('myEvent');6event.emit('myEvent');7const { test } = require('@playwright/test');8test('my test', async ({ event }) => {9 event.on('myEvent', () => console.log('myEvent fired'));10 event.emit('myEvent');11 event.off('myEvent');12 event.emit('myEvent');13});
Using AI Code Generation
1const { Event } = require('@playwright/test');2const { chromium } = require('playwright');3const browser = await chromium.launch();4const page = await browser.newPage();5page.on('click', (event, target) => {6 console.log('Click happened');7});8await page.click('text=Click me');9await browser.close();
Using AI Code Generation
1const { Event } = require('playwright/lib/server/events');2const { Connection } = require('playwright/lib/server/connection');3const { Events } = require('playwright/lib/server/events');4const connection = new Connection();5const events = new Events(connection);6const event = new Event(events, 'event', { value: 'value' });7const { Event } = require('playwright');8const { Connection } = require('playwright');9const { Events } = require('playwright');10const connection = new Connection();11const events = new Events(connection);12const event = new Event(events, 'event', { value: 'value' });
Using AI Code Generation
1const { Event } = require('../lib/internal/event');2const { EventEmitter } = require('events');3const emitter = new EventEmitter();4const e = new Event(emitter, 'test');5e.on(() => console.log('event fired!'));6emitter.emit('test');7const { Event } = require('../lib/internal/event');8const { EventEmitter } = require('events');9const emitter = new EventEmitter();10const e = new Event(emitter, 'test');11e.on(() => console.log('event fired!'));12emitter.emit('test');13const { Event } = require('../lib/internal/event');14const { EventEmitter } = require('events');15const emitter = new EventEmitter();16const e = new Event(emitter, 'test');17e.on(() => console.log('event fired!'));18emitter.emit('test');19const { Event } = require('../lib/internal/event');20const { EventEmitter } = require('events');21const emitter = new EventEmitter();22const e = new Event(emitter, 'test');23e.on(() => console.log('event fired!'));24emitter.emit('test');25const { Event } = require('../lib/internal/event');26const { EventEmitter } = require('events');27const emitter = new EventEmitter();28const e = new Event(emitter, 'test');29e.on(() => console.log('event fired!'));30emitter.emit('test');31const { Event } = require('../lib/internal/event');32const { EventEmitter } = require('events');33const emitter = new EventEmitter();34const e = new Event(emitter, 'test');35e.on(() => console.log('event fired!'));36emitter.emit('test');37const { Event } = require('../lib/internal/event');38const { EventEmitter } = require('events');39const emitter = new EventEmitter();40const e = new Event(emitter, 'test
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!