MCPcopy Index your code
hub / github.com/angular/angular / triggerEventHandler

Method triggerEventHandler

packages/core/src/debug/debug_node.ts:355–389  ·  view source on GitHub ↗

* Triggers the event by its name if there is a corresponding listener in the element's * `listeners` collection. * * If the event lacks a listener or there's some other problem, consider * calling `nativeElement.dispatchEvent(eventObject)`. * * @param eventName The name of the even

(eventName: string, eventObj?: any)

Source from the content-addressed store, hash-verified

353 * @see [Testing components scenarios](guide/testing/components-scenarios#trigger-event-handler)
354 */
355 triggerEventHandler(eventName: string, eventObj?: any): void {
356 const node = this.nativeNode as any;
357 const invokedListeners: Function[] = [];
358
359 this.listeners.forEach((listener) => {
360 if (listener.name === eventName) {
361 const callback = listener.callback;
362 callback.call(node, eventObj);
363 invokedListeners.push(callback);
364 }
365 });
366
367 // We need to check whether `eventListeners` exists, because it's something
368 // that Zone.js only adds to `EventTarget` in browser environments.
369 if (typeof node.eventListeners === 'function') {
370 // Note that in Ivy we wrap event listeners with a call to `event.preventDefault` in some
371 // cases. We use '__ngUnwrap__' as a special token that gives us access to the actual event
372 // listener.
373 node.eventListeners(eventName).forEach((listener: Function) => {
374 // In order to ensure that we can detect the special __ngUnwrap__ token described above, we
375 // use `toString` on the listener and see if it contains the token. We use this approach to
376 // ensure that it still worked with compiled code since it cannot remove or rename string
377 // literals. We also considered using a special function name (i.e. if(listener.name ===
378 // special)) but that was more cumbersome and we were also concerned the compiled code could
379 // strip the name, turning the condition in to ("" === "") and always returning true.
380 if (listener.toString().indexOf('__ngUnwrap__') !== -1) {
381 const unwrappedListener = listener('__ngUnwrap__');
382 return (
383 invokedListeners.indexOf(unwrappedListener) === -1 &&
384 unwrappedListener.call(node, eventObj)
385 );
386 }
387 });
388 }
389 }
390}
391
392function copyDomProperties(element: Element | null, properties: {[name: string]: string}): void {

Calls 6

indexOfMethod · 0.80
eventListenersMethod · 0.65
toStringMethod · 0.65
listenerFunction · 0.50
forEachMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected