|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Property} from './property'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Map from jsaction annotation to a parsed map from event name to action name. |
| 13 | + */ |
| 14 | +const parseCache: {[key: string]: {[key: string]: string}} = {}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Reads the jsaction parser cache from the given DOM Element. |
| 18 | + * |
| 19 | + * @param element . |
| 20 | + * @return Map from event to qualified name of the jsaction bound to it. |
| 21 | + */ |
| 22 | +export function get(element: Element): {[key: string]: string} { |
| 23 | + // @ts-ignore |
| 24 | + return element[Property.JSACTION]; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Writes the jsaction parser cache to the given DOM Element. |
| 29 | + * |
| 30 | + * @param element . |
| 31 | + * @param actionMap Map from event to qualified name of the jsaction bound to |
| 32 | + * it. |
| 33 | + */ |
| 34 | +export function set(element: Element, actionMap: {[key: string]: string}) { |
| 35 | + // @ts-ignore |
| 36 | + element[Property.JSACTION] = actionMap; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Looks up the parsed action map from the source jsaction attribute value. |
| 41 | + * |
| 42 | + * @param text Unparsed jsaction attribute value. |
| 43 | + * @return Parsed jsaction attribute value, if already present in the cache. |
| 44 | + */ |
| 45 | +export function getParsed(text: string): {[key: string]: string}|undefined { |
| 46 | + return parseCache[text]; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Inserts the parse result for the given source jsaction value into the cache. |
| 51 | + * |
| 52 | + * @param text Unparsed jsaction attribute value. |
| 53 | + * @param parsed Attribute value parsed into the action map. |
| 54 | + */ |
| 55 | +export function setParsed(text: string, parsed: {[key: string]: string}) { |
| 56 | + parseCache[text] = parsed; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Clears the jsaction parser cache from the given DOM Element. |
| 61 | + * |
| 62 | + * @param element . |
| 63 | + */ |
| 64 | +export function clear(element: Element) { |
| 65 | + if (Property.JSACTION in element) { |
| 66 | + delete element[Property.JSACTION]; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Reads the cached jsaction namespace from the given DOM |
| 72 | + * Element. Undefined means there is no cached value; null is a cached |
| 73 | + * jsnamespace attribute that's absent. |
| 74 | + * |
| 75 | + * @param element . |
| 76 | + * @return . |
| 77 | + */ |
| 78 | +export function getNamespace(element: Element): string|null|undefined { |
| 79 | + // @ts-ignore |
| 80 | + return element[Property.JSNAMESPACE]; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Writes the cached jsaction namespace to the given DOM Element. Null |
| 85 | + * represents a jsnamespace attribute that's absent. |
| 86 | + * |
| 87 | + * @param element . |
| 88 | + * @param jsnamespace . |
| 89 | + */ |
| 90 | +export function setNamespace(element: Element, jsnamespace: string|null) { |
| 91 | + // @ts-ignore |
| 92 | + element[Property.JSNAMESPACE] = jsnamespace; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Clears the cached jsaction namespace from the given DOM Element. |
| 97 | + * |
| 98 | + * @param element . |
| 99 | + */ |
| 100 | +export function clearNamespace(element: Element) { |
| 101 | + if (Property.JSNAMESPACE in element) { |
| 102 | + delete element[Property.JSNAMESPACE]; |
| 103 | + } |
| 104 | +} |
0 commit comments