From de04524c10619878aed1e7254761b956a34d8686 Mon Sep 17 00:00:00 2001 From: iteriani Date: Fri, 19 Apr 2024 07:42:33 -0700 Subject: [PATCH] feat(core): Remove enums from event-dispatch. These cause optimization issues in external. --- .../event-dispatch/src/accessibility.ts | 56 ++++---- .../event-dispatch/src/attribute.ts | 126 +++++++++--------- .../primitives/event-dispatch/src/cache.ts | 13 +- .../event-dispatch/src/dispatcher.ts | 8 +- .../event-dispatch/src/eventcontract.ts | 34 ++--- .../primitives/event-dispatch/src/key_code.ts | 24 ++-- .../primitives/event-dispatch/src/property.ts | 65 ++++----- .../event-dispatch/src/restriction.ts | 2 +- 8 files changed, 164 insertions(+), 164 deletions(-) diff --git a/packages/core/primitives/event-dispatch/src/accessibility.ts b/packages/core/primitives/event-dispatch/src/accessibility.ts index 6f88211dc2b4d8..a9dec26d0a7e44 100644 --- a/packages/core/primitives/event-dispatch/src/accessibility.ts +++ b/packages/core/primitives/event-dispatch/src/accessibility.ts @@ -6,39 +6,41 @@ * found in the LICENSE file at https://angular.io/license */ +/** + * An event-type set when the event contract detects a KEYDOWN event but + * doesn't know if the key press can be treated like a click. The dispatcher + * will use this event-type to parse the keypress and handle it accordingly. + */ +export const MAYBE_CLICK_EVENT_TYPE = 'maybe_click'; +/** + * A property added to a dispatched event that had the MAYBE_CLICK_EVENTTYPE + * event-type but could not be used as a click. The dispatcher sets this + * property for non-global dispatches before it retriggers the event and it + * signifies that the event contract should not dispatch this event globally. + */ +export const SKIP_GLOBAL_DISPATCH = 'a11ysgd'; +/** + * A property added to a dispatched event that had the MAYBE_CLICK_EVENTTYPE + * event-type but could not be used as a click. The dispatcher sets this + * property before it retriggers the event and it signifies that the event + * contract should not look at CLICK actions for KEYDOWN events. + */ +export const SKIP_A11Y_CHECK = 'a11ysc'; + /** * Defines special EventInfo and Event properties used when * A11Y_SUPPORT_IN_DISPATCHER is enabled. */ -export enum Attribute { - /** - * An event-type set when the event contract detects a KEYDOWN event but - * doesn't know if the key press can be treated like a click. The dispatcher - * will use this event-type to parse the keypress and handle it accordingly. - */ - MAYBE_CLICK_EVENT_TYPE = 'maybe_click', - - /** - * A property added to a dispatched event that had the MAYBE_CLICK_EVENTTYPE - * event-type but could not be used as a click. The dispatcher sets this - * property for non-global dispatches before it retriggers the event and it - * signifies that the event contract should not dispatch this event globally. - */ - SKIP_GLOBAL_DISPATCH = 'a11ysgd', - - /** - * A property added to a dispatched event that had the MAYBE_CLICK_EVENTTYPE - * event-type but could not be used as a click. The dispatcher sets this - * property before it retriggers the event and it signifies that the event - * contract should not look at CLICK actions for KEYDOWN events. - */ - SKIP_A11Y_CHECK = 'a11ysc', -} +export const Attribute = { + MAYBE_CLICK_EVENT_TYPE, + SKIP_GLOBAL_DISPATCH, + SKIP_A11Y_CHECK, +}; declare global { interface Event { - [Attribute.MAYBE_CLICK_EVENT_TYPE]?: boolean; - [Attribute.SKIP_GLOBAL_DISPATCH]?: boolean; - [Attribute.SKIP_A11Y_CHECK]?: boolean; + [MAYBE_CLICK_EVENT_TYPE]?: boolean; + [SKIP_GLOBAL_DISPATCH]?: boolean; + [SKIP_A11Y_CHECK]?: boolean; } } diff --git a/packages/core/primitives/event-dispatch/src/attribute.ts b/packages/core/primitives/event-dispatch/src/attribute.ts index 3675eca04fc4e4..7836d86143e464 100644 --- a/packages/core/primitives/event-dispatch/src/attribute.ts +++ b/packages/core/primitives/event-dispatch/src/attribute.ts @@ -6,72 +6,72 @@ * found in the LICENSE file at https://angular.io/license */ -export enum Attribute { - /** - * The jsaction attribute defines a mapping of a DOM event to a - * generic event (aka jsaction), to which the actual event handlers - * that implement the behavior of the application are bound. The - * value is a semicolon separated list of colon separated pairs of - * an optional DOM event name and a jsaction name. If the optional - * DOM event name is omitted, 'click' is assumed. The jsaction names - * are dot separated pairs of a namespace and a simple jsaction - * name. If the namespace is absent, it is taken from the closest - * ancestor element with a jsnamespace attribute, if there is - * any. If there is no ancestor with a jsnamespace attribute, the - * simple name is assumed to be the jsaction name. - * - * Used by EventContract. - */ - JSACTION = 'jsaction', +/** + * The jsaction attribute defines a mapping of a DOM event to a + * generic event (aka jsaction), to which the actual event handlers + * that implement the behavior of the application are bound. The + * value is a semicolon separated list of colon separated pairs of + * an optional DOM event name and a jsaction name. If the optional + * DOM event name is omitted, 'click' is assumed. The jsaction names + * are dot separated pairs of a namespace and a simple jsaction + * name. If the namespace is absent, it is taken from the closest + * ancestor element with a jsnamespace attribute, if there is + * any. If there is no ancestor with a jsnamespace attribute, the + * simple name is assumed to be the jsaction name. + * + * Used by EventContract. + */ +export const JSACTION = 'jsaction'; - /** - * The jsnamespace attribute provides the namespace part of the - * jaction names occurring in the jsaction attribute where it's - * missing. - * - * Used by EventContract. - */ - JSNAMESPACE = 'jsnamespace', +/** + * The jsnamespace attribute provides the namespace part of the + * jaction names occurring in the jsaction attribute where it's + * missing. + * + * Used by EventContract. + */ +export const JSNAMESPACE = 'jsnamespace'; - /** - * The oi attribute is a log impression tag for impression logging - * and action tracking. For an element that carries a jsaction - * attribute, the element is identified for the purpose of - * impression logging and click tracking by the dot separated path - * of all oi attributes in the chain of ancestors of the element. - * - * Used by ActionFlow. - */ - OI = 'oi', +/** + * The oi attribute is a log impression tag for impression logging + * and action tracking. For an element that carries a jsaction + * attribute, the element is identified for the purpose of + * impression logging and click tracking by the dot separated path + * of all oi attributes in the chain of ancestors of the element. + * + * Used by ActionFlow. + */ +export const OI = 'oi'; - /** - * The ved attribute is an encoded ClickTrackingCGI proto to track - * visual elements. - * - * Used by ActionFlow. - */ - VED = 'ved', +/** + * The ved attribute is an encoded ClickTrackingCGI proto to track + * visual elements. + * + * Used by ActionFlow. + */ +export const VED = 'ved'; - /** - * The vet attribute is the visual element type used to identify tracked - * visual elements. - */ - VET = 'vet', +/** + * The vet attribute is the visual element type used to identify tracked + * visual elements. + */ +export const VET = 'vet'; + +/** + * Support for iteration on reprocessing. + * + * Used by ActionFlow. + */ +export const JSINSTANCE = 'jsinstance'; - /** - * Support for iteration on reprocessing. - * - * Used by ActionFlow. - */ - JSINSTANCE = 'jsinstance', +/** + * All click jsactions that happen on the element that carries this + * attribute or its descendants are automatically logged. + * Impressions of jsactions on these elements are tracked too, if + * requested by the impression() method of ActionFlow. + * + * Used by ActionFlow. + */ +export const JSTRACK = 'jstrack'; - /** - * All click jsactions that happen on the element that carries this - * attribute or its descendants are automatically logged. - * Impressions of jsactions on these elements are tracked too, if - * requested by the impression() method of ActionFlow. - * - * Used by ActionFlow. - */ - JSTRACK = 'jstrack', -} +export const Attribute = {JSNAMESPACE, OI, VED, VET, JSINSTANCE, JSTRACK}; diff --git a/packages/core/primitives/event-dispatch/src/cache.ts b/packages/core/primitives/event-dispatch/src/cache.ts index 7fd144e9501bdc..e22d44f78468c1 100644 --- a/packages/core/primitives/event-dispatch/src/cache.ts +++ b/packages/core/primitives/event-dispatch/src/cache.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Property} from './property'; +import {JSACTION, JSNAMESPACE} from './property'; /** * Map from jsaction annotation to a parsed map from event name to action name. @@ -62,8 +62,8 @@ export function setParsed(text: string, parsed: {[key: string]: string}) { * @param element . */ export function clear(element: Element) { - if (Property.JSACTION in element) { - delete element[Property.JSACTION]; + if (JSACTION in element) { + delete element[JSACTION]; } } @@ -88,8 +88,7 @@ export function getNamespace(element: Element): string | null | undefined { * @param jsnamespace . */ export function setNamespace(element: Element, jsnamespace: string | null) { - // @ts-ignore - element[Property.JSNAMESPACE] = jsnamespace; + element[JSNAMESPACE] = jsnamespace!; } /** @@ -98,7 +97,7 @@ export function setNamespace(element: Element, jsnamespace: string | null) { * @param element . */ export function clearNamespace(element: Element) { - if (Property.JSNAMESPACE in element) { - delete element[Property.JSNAMESPACE]; + if (JSNAMESPACE in element) { + delete element[JSNAMESPACE]; } } diff --git a/packages/core/primitives/event-dispatch/src/dispatcher.ts b/packages/core/primitives/event-dispatch/src/dispatcher.ts index 253fad42a11775..1669f2256c9815 100644 --- a/packages/core/primitives/event-dispatch/src/dispatcher.ts +++ b/packages/core/primitives/event-dispatch/src/dispatcher.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Attribute as AccessibilityAttribute} from './accessibility'; +import {MAYBE_CLICK_EVENT_TYPE, SKIP_A11Y_CHECK, SKIP_GLOBAL_DISPATCH} from './accessibility'; import {Char} from './char'; import * as eventLib from './event'; import {EventInfo, EventInfoWrapper} from './event_info'; @@ -321,7 +321,7 @@ export class Dispatcher { * be re-dispatched. */ function resolveA11yEvent(eventInfoWrapper: EventInfoWrapper, isGlobalDispatch = false): boolean { - if (eventInfoWrapper.getEventType() !== AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE) { + if (eventInfoWrapper.getEventType() !== MAYBE_CLICK_EVENT_TYPE) { return true; } @@ -343,10 +343,10 @@ function resolveA11yEvent(eventInfoWrapper: EventInfoWrapper, isGlobalDispatch = // This prevents the event contract from setting the // AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE type for Keydown // events. - eventInfoWrapper.getEvent()[AccessibilityAttribute.SKIP_A11Y_CHECK] = true; + eventInfoWrapper.getEvent()[SKIP_A11Y_CHECK] = true; // Since globally dispatched events will get handled by the dispatcher, // don't have the event contract dispatch it again. - eventInfoWrapper.getEvent()[AccessibilityAttribute.SKIP_GLOBAL_DISPATCH] = true; + eventInfoWrapper.getEvent()[SKIP_GLOBAL_DISPATCH] = true; return false; } } diff --git a/packages/core/primitives/event-dispatch/src/eventcontract.ts b/packages/core/primitives/event-dispatch/src/eventcontract.ts index 0210c92bbd0b7c..9415bd899dbdf1 100644 --- a/packages/core/primitives/event-dispatch/src/eventcontract.ts +++ b/packages/core/primitives/event-dispatch/src/eventcontract.ts @@ -31,8 +31,8 @@ */ import * as a11yClickLib from './a11y_click'; -import {Attribute as AccessibilityAttribute} from './accessibility'; -import {Attribute} from './attribute'; +import {MAYBE_CLICK_EVENT_TYPE, SKIP_A11Y_CHECK, SKIP_GLOBAL_DISPATCH} from './accessibility'; +import {JSACTION, JSNAMESPACE} from './attribute'; import * as cache from './cache'; import {Char} from './char'; import {EarlyJsactionData} from './earlyeventcontract'; @@ -48,7 +48,7 @@ import { } from './event_contract_defines'; import * as eventInfoLib from './event_info'; import {EventType} from './event_type'; -import {Property} from './property'; +import {OWNER} from './property'; import {Restriction} from './restriction'; /** @@ -218,10 +218,7 @@ export class EventContract implements UnrenamedEventContract { this.populateAction(eventInfo); - if ( - this.dispatcher && - !eventInfoLib.getEvent(eventInfo)[AccessibilityAttribute.SKIP_GLOBAL_DISPATCH] - ) { + if (this.dispatcher && !eventInfoLib.getEvent(eventInfo)[SKIP_GLOBAL_DISPATCH]) { const globalEventInfo: eventInfoLib.EventInfo = eventInfoLib.cloneEventInfo(eventInfo); // In some cases, `populateAction` will rewrite `click` events to @@ -240,10 +237,7 @@ export class EventContract implements UnrenamedEventContract { } let stopPropagationAfterDispatch = false; - if ( - this.stopPropagation && - eventInfoLib.getEventType(eventInfo) !== AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE - ) { + if (this.stopPropagation && eventInfoLib.getEventType(eventInfo) !== MAYBE_CLICK_EVENT_TYPE) { if ( eventLib.isGecko && (eventInfoLib.getTargetElement(eventInfo).tagName === 'INPUT' || @@ -260,7 +254,7 @@ export class EventContract implements UnrenamedEventContract { } } else if ( this.stopPropagation && - eventInfoLib.getEventType(eventInfo) === AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE + eventInfoLib.getEventType(eventInfo) === MAYBE_CLICK_EVENT_TYPE ) { // We first need to let the dispatcher determine whether we can treat // this event as a click event. @@ -345,11 +339,11 @@ export class EventContract implements UnrenamedEventContract { } else if ( EventContract.A11Y_SUPPORT_IN_DISPATCHER && eventInfoLib.getEventType(eventInfo) === EventType.KEYDOWN && - !eventInfoLib.getEvent(eventInfo)[AccessibilityAttribute.SKIP_A11Y_CHECK] + !eventInfoLib.getEvent(eventInfo)[SKIP_A11Y_CHECK] ) { // We use a string literal as this value needs to be referenced in the // dispatcher's binary. - eventInfoLib.setEventType(eventInfo, AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE); + eventInfoLib.setEventType(eventInfo, MAYBE_CLICK_EVENT_TYPE); } // Walk to the parent node, unless the node has a different owner in @@ -365,8 +359,8 @@ export class EventContract implements UnrenamedEventContract { // ancestor chain of the event target node. break; } - if (actionElement[Property.OWNER]) { - actionElement = actionElement[Property.OWNER] as Element; + if (actionElement[OWNER]) { + actionElement = actionElement[OWNER] as Element; continue; } if (actionElement.parentNode?.nodeName !== '#document-fragment') { @@ -453,7 +447,7 @@ export class EventContract implements UnrenamedEventContract { } if (EventContract.A11Y_SUPPORT_IN_DISPATCHER) { if ( - eventInfoLib.getEventType(eventInfo) === AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE && + eventInfoLib.getEventType(eventInfo) === MAYBE_CLICK_EVENT_TYPE && actionMap[EventType.CLICK] !== undefined ) { // We'll take the first CLICK action we find and have the dispatcher @@ -688,7 +682,7 @@ export function addDeferredA11yClickSupport(eventContract: EventContract) { function checkDispatcherForA11yClick(eventInfo: eventInfoLib.EventInfo): boolean { return ( EventContract.A11Y_SUPPORT_IN_DISPATCHER && - eventInfoLib.getEventType(eventInfo) === AccessibilityAttribute.MAYBE_CLICK_EVENT_TYPE + eventInfoLib.getEventType(eventInfo) === MAYBE_CLICK_EVENT_TYPE ); } @@ -724,7 +718,7 @@ function shouldPreventDefaultBeforeDispatching( export function parseActions(actionElement: Element, container: Node): {[key: string]: string} { let actionMap: {[key: string]: string} | undefined = cache.get(actionElement); if (!actionMap) { - const jsactionAttribute = getAttr(actionElement, Attribute.JSACTION); + const jsactionAttribute = getAttr(actionElement, JSACTION); if (!jsactionAttribute) { actionMap = EMPTY_ACTION_MAP; cache.set(actionElement, actionMap); @@ -820,7 +814,7 @@ function getNamespaceFromElement(element: Element): string | null { // namespace is string|null if the query took place in the past, or // undefined if the query did not take place. if (namespace === undefined) { - namespace = getAttr(element, Attribute.JSNAMESPACE); + namespace = getAttr(element, JSNAMESPACE); cache.setNamespace(element, namespace); } return namespace; diff --git a/packages/core/primitives/event-dispatch/src/key_code.ts b/packages/core/primitives/event-dispatch/src/key_code.ts index 142a5eb4a02b39..5ed87f61493cd2 100644 --- a/packages/core/primitives/event-dispatch/src/key_code.ts +++ b/packages/core/primitives/event-dispatch/src/key_code.ts @@ -6,17 +6,17 @@ * found in the LICENSE file at https://angular.io/license */ -/** Special keycodes used by jsaction for the generic click action. */ -export enum KeyCode { - /** - * If on a Macintosh with an extended keyboard, the Enter key located in the - * numeric pad has a different ASCII code. - */ - MAC_ENTER = 3, +/** + * If on a Macintosh with an extended keyboard, the Enter key located in the + * numeric pad has a different ASCII code. + */ +export const MAC_ENTER = 3; - /** The Enter key. */ - ENTER = 13, +/** The Enter key. */ +export const ENTER = 13; - /** The Space key. */ - SPACE = 32, -} +/** The Space key. */ +export const SPACE = 32; + +/** Special keycodes used by jsaction for the generic click action. */ +export const KeyCode = {MAC_ENTER, ENTER, SPACE}; diff --git a/packages/core/primitives/event-dispatch/src/property.ts b/packages/core/primitives/event-dispatch/src/property.ts index e155ca84164e31..eaeddc0df051c6 100644 --- a/packages/core/primitives/event-dispatch/src/property.ts +++ b/packages/core/primitives/event-dispatch/src/property.ts @@ -6,41 +6,46 @@ * found in the LICENSE file at https://angular.io/license */ -/** All properties that are used by jsaction. */ -export enum Property { - /** - * The parsed value of the jsaction attribute is stored in this - * property on the DOM node. The parsed value is an Object. The - * property names of the object are the events; the values are the - * names of the actions. This property is attached even on nodes - * that don't have a jsaction attribute as an optimization, because - * property lookup is faster than attribute access. - */ - JSACTION = '__jsaction', +/** + * The parsed value of the jsaction attribute is stored in this + * property on the DOM node. The parsed value is an Object. The + * property names of the object are the events; the values are the + * names of the actions. This property is attached even on nodes + * that don't have a jsaction attribute as an optimization, because + * property lookup is faster than attribute access. + */ +export const JSACTION = '__jsaction'; + +/** + * The parsed value of the jsnamespace attribute is stored in this + * property on the DOM node. + */ +export const JSNAMESPACE = '__jsnamespace'; - /** - * The parsed value of the jsnamespace attribute is stored in this - * property on the DOM node. - */ - JSNAMESPACE = '__jsnamespace', +/** The value of the oi attribute as a property, for faster access. */ +export const OI = '__oi'; - /** The value of the oi attribute as a property, for faster access. */ - OI = '__oi', +/** + * The owner property references an a logical owner for a DOM node. JSAction + * will follow this reference instead of parentNode when traversing the DOM + * to find jsaction attributes. This allows overlaying a logical structure + * over a document where the DOM structure can't reflect that structure. + */ +export const OWNER = '__owner'; - /** - * The owner property references an a logical owner for a DOM node. JSAction - * will follow this reference instead of parentNode when traversing the DOM - * to find jsaction attributes. This allows overlaying a logical structure - * over a document where the DOM structure can't reflect that structure. - */ - OWNER = '__owner', -} +/** All properties that are used by jsaction. */ +export const Property = { + JSACTION, + JSNAMESPACE, + OI, + OWNER, +}; declare global { interface Node { - [Property.JSACTION]?: string; - [Property.JSNAMESPACE]?: string; - [Property.OI]?: string; - [Property.OWNER]?: ParentNode; + [JSACTION]?: string; + [JSNAMESPACE]?: string; + [OI]?: string; + [OWNER]?: ParentNode; } } diff --git a/packages/core/primitives/event-dispatch/src/restriction.ts b/packages/core/primitives/event-dispatch/src/restriction.ts index 2117b5c641c0d9..e6e0d69c9cb034 100644 --- a/packages/core/primitives/event-dispatch/src/restriction.ts +++ b/packages/core/primitives/event-dispatch/src/restriction.ts @@ -11,5 +11,5 @@ */ export enum Restriction { - I_AM_THE_JSACTION_FRAMEWORK = 1, + I_AM_THE_JSACTION_FRAMEWORK, }