|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Ericsson AB. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 19 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +"use strict"; |
| 29 | + |
| 30 | +var domObject = (function () { |
| 31 | + |
| 32 | + function createAttributeDescriptor(name, attributes) { |
| 33 | + return { |
| 34 | + "get": function () { |
| 35 | + var attribute = attributes[name]; |
| 36 | + return typeof attribute == "function" ? attribute() : attribute; |
| 37 | + }, |
| 38 | + "enumerable": true |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + "addReadOnlyAttributes": function (target, attributes) { |
| 44 | + for (var name in attributes) |
| 45 | + Object.defineProperty(target, name, createAttributeDescriptor(name, attributes)); |
| 46 | + }, |
| 47 | + |
| 48 | + "addConstants": function (target, constants) { |
| 49 | + for (var name in constants) |
| 50 | + Object.defineProperty(target, name, { |
| 51 | + "value": constants[name], |
| 52 | + "enumerable": true |
| 53 | + }); |
| 54 | + } |
| 55 | + }; |
| 56 | +})(); |
| 57 | + |
| 58 | +function EventTarget(attributes) { |
| 59 | + var _this = this; |
| 60 | + var listenersMap = {}; |
| 61 | + |
| 62 | + if (attributes) |
| 63 | + addEventListenerAttributes(this, attributes); |
| 64 | + |
| 65 | + this.addEventListener = function (type, listener, useCapture) { |
| 66 | + if (typeof(listener) != "function") |
| 67 | + throw new TypeError("listener argument (" + listener + ") is not a function"); |
| 68 | + var listeners = listenersMap[type]; |
| 69 | + if (!listeners) |
| 70 | + listeners = listenersMap[type] = []; |
| 71 | + |
| 72 | + if (listeners.indexOf(listener) < 0) |
| 73 | + listeners.push(listener); |
| 74 | + }; |
| 75 | + |
| 76 | + this.removeEventListener = function (type, listener, useCapture) { |
| 77 | + var listeners = listenersMap[type]; |
| 78 | + if (!listeners) |
| 79 | + return; |
| 80 | + |
| 81 | + var i = listeners.indexOf(listener); |
| 82 | + if (i >= 0) |
| 83 | + listeners.splice(i, 1); |
| 84 | + }; |
| 85 | + |
| 86 | + this.dispatchEvent = function (evt) { |
| 87 | + var listeners = []; |
| 88 | + |
| 89 | + var attributeListener = _this["on" + evt.type]; |
| 90 | + if (attributeListener) |
| 91 | + listeners.push(attributeListener); |
| 92 | + |
| 93 | + if (listenersMap[evt.type]) |
| 94 | + Array.prototype.push.apply(listeners, listenersMap[evt.type]); |
| 95 | + |
| 96 | + var errors = []; |
| 97 | + var result = true; |
| 98 | + listeners.forEach(function (listener) { |
| 99 | + try { |
| 100 | + result = !(listener(evt) === false) && result; |
| 101 | + } catch (e) { |
| 102 | + errors.push(e); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + errors.forEach(function (e) { |
| 107 | + setTimeout(function () { |
| 108 | + throw e; |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + return result; |
| 113 | + }; |
| 114 | + |
| 115 | + function addEventListenerAttributes(target, attributes) { |
| 116 | + for (var name in attributes) |
| 117 | + Object.defineProperty(target, name, createEventListenerDescriptor(name, attributes)); |
| 118 | + } |
| 119 | + |
| 120 | + function createEventListenerDescriptor(name, attributes) { |
| 121 | + return { |
| 122 | + "get": function () { return attributes[name]; }, |
| 123 | + "set": function (cb) { attributes[name] = (typeof(cb) == "function") ? cb : null; }, |
| 124 | + "enumerable": true |
| 125 | + }; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function checkDictionary(name, dict, typeMap) { |
| 130 | + for (var memberName in dict) { |
| 131 | + if (!dict.hasOwnProperty(memberName) || !typeMap.hasOwnProperty(memberName)) |
| 132 | + continue; |
| 133 | + |
| 134 | + var message = name + ": Dictionary member " + memberName; |
| 135 | + checkType(message, dict[memberName], typeMap[memberName]); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function checkArguments(name, argsTypeTemplate, numRequired, args) { |
| 140 | + if (args.length < numRequired) { |
| 141 | + throw new TypeError(name + ": Too few arguments (got " + args.length + " expected " + |
| 142 | + numRequired + ")"); |
| 143 | + } |
| 144 | + |
| 145 | + var typeTemplates = argsTypeTemplate.split(/\s*,\s*/); |
| 146 | + |
| 147 | + for (var i = 0; i < args.length && i < typeTemplates.length; i++) { |
| 148 | + var message = name + ": Argument " + (i + 1); |
| 149 | + checkType(message, args[i], typeTemplates[i]); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function checkType(name, value, typeTemplate) { |
| 154 | + var expetedTypes = typeTemplate.split(/\s*\|\s*/); |
| 155 | + if (!canConvert(value, expetedTypes)) { |
| 156 | + throw new TypeError(name + " is of wrong type (expected " + |
| 157 | + expetedTypes.join(" or ") + ")"); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function canConvert(value, expetedTypes) { |
| 162 | + var type = typeof value; |
| 163 | + for (var i = 0; i < expetedTypes.length; i++) { |
| 164 | + var expetedType = expetedTypes[i]; |
| 165 | + if (expetedType == "string" || expetedType == "boolean") |
| 166 | + return true; // type conversion will never throw |
| 167 | + if (expetedType == "number") { |
| 168 | + var asNumber = +value; |
| 169 | + if (!isNaN(asNumber) && asNumber != -Infinity && asNumber != Infinity) |
| 170 | + return true; |
| 171 | + } |
| 172 | + if (type == "object") { |
| 173 | + if (expetedType == "object") |
| 174 | + return true; |
| 175 | + // could be a specific object type or host object (e.g. Array) |
| 176 | + var constructor = self[expetedType]; |
| 177 | + if (constructor && value instanceof constructor) |
| 178 | + return true; |
| 179 | + } |
| 180 | + if (type == expetedType && expetedType == "function") |
| 181 | + return true; |
| 182 | + } |
| 183 | + return false; |
| 184 | +} |
| 185 | + |
| 186 | +function randomString() { |
| 187 | + var randomValues = new Uint8Array(27); |
| 188 | + crypto.getRandomValues(randomValues); |
| 189 | + return btoa(String.fromCharCode.apply(null, randomValues)); |
| 190 | +} |
0 commit comments