Debian Codegraph @

closure-library/closure/goog/html/sanitizer/elementweakmap.js

raw
1 // Copyright 2017 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @package
17 * @supported IE 10+ and other browsers. IE 8 and IE 9 could be supported by
18 * by making anti-clobbering support optional.
19 */
20
21 goog.module('goog.html.sanitizer.ElementWeakMap');
22 goog.module.declareLegacyNamespace();
23
24 var noclobber = goog.require('goog.html.sanitizer.noclobber');
25 var userAgent = goog.require('goog.userAgent');
26
27 // We can't detect if WeakMap is defined by looking for the property on the
28 // global scope because a leaky polyfill might have been defined by the
29 // compiler, and we want to use this implementation instead of the polyfill.
30 /** @const {boolean} */
31 var WEAKMAP_SUPPORTED = !userAgent.IE || document.documentMode >= 11;
32
33 /** @const {string} */
34 var DATA_ATTRIBUTE_NAME_PREFIX = 'data-elementweakmap-index-';
35
36 // Increased every time a new ElementWeakMap is constructed, to guarantee
37 // that each weakmap uses a different attribute name.
38 var weakMapCount = 0;
39
40 /**
41 * A weakmap-like implementation for browsers that don't support native WeakMap.
42 * It uses a data attribute on the key element for O(1) lookups.
43 * @template T
44 * @constructor
45 */
46 var ElementWeakMap = function() {
47 /** @private {!Array<!Element>} */
48 this.keys_ = [];
49
50 /** @private {!Array<!T>} */
51 this.values_ = [];
52
53 /** @private @const {string} */
54 this.dataAttributeName_ = DATA_ATTRIBUTE_NAME_PREFIX + weakMapCount++;
55 };
56
57 /**
58 * Stores a {@code elementKey} -> {@code value} mapping.
59 * @param {!Element} elementKey
60 * @param {!T} value
61 * @return {!ElementWeakMap}
62 */
63 ElementWeakMap.prototype.set = function(elementKey, value) {
64 if (noclobber.hasElementAttribute(elementKey, this.dataAttributeName_)) {
65 var itemIndex = parseInt(
66 noclobber.getElementAttribute(elementKey, this.dataAttributeName_), 10);
67 this.values_[itemIndex] = value;
68 } else {
69 var itemIndex = this.values_.push(value) - 1;
70 noclobber.setElementAttribute(
71 elementKey, this.dataAttributeName_, itemIndex.toString());
72 this.keys_.push(elementKey);
73 }
74 return this;
75 };
76
77 /**
78 * Gets the value previously stored for {@code elementKey}, or undefined if no
79 * value was stored for such key.
80 * @param {!Element} elementKey
81 * @return {!Element|undefined}
82 */
83 ElementWeakMap.prototype.get = function(elementKey) {
84 if (!noclobber.hasElementAttribute(elementKey, this.dataAttributeName_)) {
85 return undefined;
86 }
87 var itemIndex = parseInt(
88 noclobber.getElementAttribute(elementKey, this.dataAttributeName_), 10);
89 return this.values_[itemIndex];
90 };
91
92 /** Clears the map. */
93 ElementWeakMap.prototype.clear = function() {
94 this.keys_.forEach(function(el) {
95 noclobber.removeElementAttribute(el, this.dataAttributeName_);
96 }, this);
97 this.keys_ = [];
98 this.values_ = [];
99 };
100
101 /**
102 * Returns either this weakmap adapter or the native weakmap implmentation, if
103 * available.
104 * @return {!ElementWeakMap|!WeakMap}
105 */
106 ElementWeakMap.newWeakMap = function() {
107 return WEAKMAP_SUPPORTED ? new WeakMap() : new ElementWeakMap();
108 };
109
110 exports = ElementWeakMap;