MCPcopy Create free account
hub / github.com/ampproject/amphtml / installActionHandler

Method installActionHandler

src/service/action-impl.js:450–483  ·  view source on GitHub ↗

* Installs action handler for the specified element. The action handler is * responsible for checking invocation trust. * * For AMP elements, use base-element.registerAction() instead. * * @param {!Element} target * @param {ActionHandlerDef} handler

(target, handler)

Source from the content-addressed store, hash-verified

448 * @param {ActionHandlerDef} handler
449 */
450 installActionHandler(target, handler) {
451 // TODO(dvoytenko, #7063): switch back to `target.id` with form proxy.
452 const targetId = target.getAttribute('id') || '';
453
454 devAssert(
455 isAmpTagName(targetId) ||
456 target.tagName.toLowerCase() in NON_AMP_ELEMENTS_ACTIONS_,
457 'AMP or special element expected: %s',
458 target.tagName + '#' + targetId
459 );
460
461 if (target[ACTION_HANDLER_]) {
462 dev().error(TAG_, `Action handler already installed for ${target}`);
463 return;
464 }
465 target[ACTION_HANDLER_] = handler;
466
467 /** @const {Array<!ActionInvocation>} */
468 const queuedInvocations = target[ACTION_QUEUE_];
469 if (isArray(queuedInvocations)) {
470 // Invoke and clear all queued invocations now handler is installed.
471 Services.timerFor(getWin(target)).delay(() => {
472 // TODO(dvoytenko, #1260): dedupe actions.
473 queuedInvocations.forEach((invocation) => {
474 try {
475 handler(invocation);
476 } catch (e) {
477 dev().error(TAG_, 'Action execution failed:', invocation, e);
478 }
479 });
480 target[ACTION_QUEUE_].length = 0;
481 }, 1);
482 }
483 }
484
485 /**
486 * Checks if the given element has registered a particular action type.

Callers 3

test-action.jsFile · 0.80
startInternal_Method · 0.80
constructorMethod · 0.80

Calls 10

devAssertFunction · 0.90
devFunction · 0.90
getWinFunction · 0.90
isAmpTagNameFunction · 0.85
isArrayFunction · 0.85
getAttributeMethod · 0.80
delayMethod · 0.80
handlerFunction · 0.50
errorMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected