user-properties.js (2557B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /** 8 * Store of CSSStyleDeclarations mapped to properties that have been changed by 9 * the user. 10 */ 11 class UserProperties { 12 constructor() { 13 this.map = new Map(); 14 } 15 16 #propertyNames = new Set(); 17 18 /** 19 * Get a named property for a given CSSStyleDeclaration. 20 * 21 * @param {CSSStyleDeclaration} style 22 * The CSSStyleDeclaration against which the property is mapped. 23 * @param {string} name 24 * The name of the property to get. 25 * @param {string} value 26 * Default value. 27 * @return {string} 28 * The property value if it has previously been set by the user, null 29 * otherwise. 30 */ 31 getProperty(style, name, value) { 32 const key = this.getKey(style); 33 const entry = this.map.get(key, null); 34 35 if (entry && name in entry) { 36 return entry[name]; 37 } 38 return value; 39 } 40 41 /** 42 * Set a named property for a given CSSStyleDeclaration. 43 * 44 * @param {CSSStyleDeclaration} style 45 * The CSSStyleDeclaration against which the property is to be mapped. 46 * @param {string} name 47 * The name of the property to set. 48 * @param {string} userValue 49 * The value of the property to set. 50 */ 51 setProperty(style, name, userValue) { 52 const key = this.getKey(style, name); 53 const entry = this.map.get(key, null); 54 55 if (entry) { 56 entry[name] = userValue; 57 } else { 58 const props = {}; 59 props[name] = userValue; 60 this.map.set(key, props); 61 } 62 this.#propertyNames.add(name); 63 } 64 65 /** 66 * Check whether a named property for a given CSSStyleDeclaration is stored. 67 * 68 * @param {CSSStyleDeclaration} style 69 * The CSSStyleDeclaration against which the property would be mapped. 70 * @param {string} name 71 * The name of the property to check. 72 */ 73 contains(style, name) { 74 const key = this.getKey(style, name); 75 const entry = this.map.get(key, null); 76 return !!entry && name in entry; 77 } 78 79 /** 80 * Check whether a named property is stored. 81 * 82 * @param {string} name 83 * The name of the property to check. 84 */ 85 containsName(name) { 86 return this.#propertyNames.has(name); 87 } 88 89 getKey(style, name) { 90 return style.actorID + ":" + name; 91 } 92 93 clear() { 94 this.map.clear(); 95 this.#propertyNames.clear(); 96 } 97 } 98 99 module.exports = UserProperties;