1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var getBuiltIn = require('../internals/get-built-in');
|
---|
4 | var getBuiltInNodeModule = require('../internals/get-built-in-node-module');
|
---|
5 | var fails = require('../internals/fails');
|
---|
6 | var create = require('../internals/object-create');
|
---|
7 | var createPropertyDescriptor = require('../internals/create-property-descriptor');
|
---|
8 | var defineProperty = require('../internals/object-define-property').f;
|
---|
9 | var defineBuiltIn = require('../internals/define-built-in');
|
---|
10 | var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
|
---|
11 | var hasOwn = require('../internals/has-own-property');
|
---|
12 | var anInstance = require('../internals/an-instance');
|
---|
13 | var anObject = require('../internals/an-object');
|
---|
14 | var errorToString = require('../internals/error-to-string');
|
---|
15 | var normalizeStringArgument = require('../internals/normalize-string-argument');
|
---|
16 | var DOMExceptionConstants = require('../internals/dom-exception-constants');
|
---|
17 | var clearErrorStack = require('../internals/error-stack-clear');
|
---|
18 | var InternalStateModule = require('../internals/internal-state');
|
---|
19 | var DESCRIPTORS = require('../internals/descriptors');
|
---|
20 | var IS_PURE = require('../internals/is-pure');
|
---|
21 |
|
---|
22 | var DOM_EXCEPTION = 'DOMException';
|
---|
23 | var DATA_CLONE_ERR = 'DATA_CLONE_ERR';
|
---|
24 | var Error = getBuiltIn('Error');
|
---|
25 | // NodeJS < 17.0 does not expose `DOMException` to global
|
---|
26 | var NativeDOMException = getBuiltIn(DOM_EXCEPTION) || (function () {
|
---|
27 | try {
|
---|
28 | // NodeJS < 15.0 does not expose `MessageChannel` to global
|
---|
29 | var MessageChannel = getBuiltIn('MessageChannel') || getBuiltInNodeModule('worker_threads').MessageChannel;
|
---|
30 | // eslint-disable-next-line es/no-weak-map, unicorn/require-post-message-target-origin -- safe
|
---|
31 | new MessageChannel().port1.postMessage(new WeakMap());
|
---|
32 | } catch (error) {
|
---|
33 | if (error.name === DATA_CLONE_ERR && error.code === 25) return error.constructor;
|
---|
34 | }
|
---|
35 | })();
|
---|
36 | var NativeDOMExceptionPrototype = NativeDOMException && NativeDOMException.prototype;
|
---|
37 | var ErrorPrototype = Error.prototype;
|
---|
38 | var setInternalState = InternalStateModule.set;
|
---|
39 | var getInternalState = InternalStateModule.getterFor(DOM_EXCEPTION);
|
---|
40 | var HAS_STACK = 'stack' in new Error(DOM_EXCEPTION);
|
---|
41 |
|
---|
42 | var codeFor = function (name) {
|
---|
43 | return hasOwn(DOMExceptionConstants, name) && DOMExceptionConstants[name].m ? DOMExceptionConstants[name].c : 0;
|
---|
44 | };
|
---|
45 |
|
---|
46 | var $DOMException = function DOMException() {
|
---|
47 | anInstance(this, DOMExceptionPrototype);
|
---|
48 | var argumentsLength = arguments.length;
|
---|
49 | var message = normalizeStringArgument(argumentsLength < 1 ? undefined : arguments[0]);
|
---|
50 | var name = normalizeStringArgument(argumentsLength < 2 ? undefined : arguments[1], 'Error');
|
---|
51 | var code = codeFor(name);
|
---|
52 | setInternalState(this, {
|
---|
53 | type: DOM_EXCEPTION,
|
---|
54 | name: name,
|
---|
55 | message: message,
|
---|
56 | code: code
|
---|
57 | });
|
---|
58 | if (!DESCRIPTORS) {
|
---|
59 | this.name = name;
|
---|
60 | this.message = message;
|
---|
61 | this.code = code;
|
---|
62 | }
|
---|
63 | if (HAS_STACK) {
|
---|
64 | var error = new Error(message);
|
---|
65 | error.name = DOM_EXCEPTION;
|
---|
66 | defineProperty(this, 'stack', createPropertyDescriptor(1, clearErrorStack(error.stack, 1)));
|
---|
67 | }
|
---|
68 | };
|
---|
69 |
|
---|
70 | var DOMExceptionPrototype = $DOMException.prototype = create(ErrorPrototype);
|
---|
71 |
|
---|
72 | var createGetterDescriptor = function (get) {
|
---|
73 | return { enumerable: true, configurable: true, get: get };
|
---|
74 | };
|
---|
75 |
|
---|
76 | var getterFor = function (key) {
|
---|
77 | return createGetterDescriptor(function () {
|
---|
78 | return getInternalState(this)[key];
|
---|
79 | });
|
---|
80 | };
|
---|
81 |
|
---|
82 | if (DESCRIPTORS) {
|
---|
83 | // `DOMException.prototype.code` getter
|
---|
84 | defineBuiltInAccessor(DOMExceptionPrototype, 'code', getterFor('code'));
|
---|
85 | // `DOMException.prototype.message` getter
|
---|
86 | defineBuiltInAccessor(DOMExceptionPrototype, 'message', getterFor('message'));
|
---|
87 | // `DOMException.prototype.name` getter
|
---|
88 | defineBuiltInAccessor(DOMExceptionPrototype, 'name', getterFor('name'));
|
---|
89 | }
|
---|
90 |
|
---|
91 | defineProperty(DOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, $DOMException));
|
---|
92 |
|
---|
93 | // FF36- DOMException is a function, but can't be constructed
|
---|
94 | var INCORRECT_CONSTRUCTOR = fails(function () {
|
---|
95 | return !(new NativeDOMException() instanceof Error);
|
---|
96 | });
|
---|
97 |
|
---|
98 | // Safari 10.1 / Chrome 32- / IE8- DOMException.prototype.toString bugs
|
---|
99 | var INCORRECT_TO_STRING = INCORRECT_CONSTRUCTOR || fails(function () {
|
---|
100 | return ErrorPrototype.toString !== errorToString || String(new NativeDOMException(1, 2)) !== '2: 1';
|
---|
101 | });
|
---|
102 |
|
---|
103 | // Deno 1.6.3- DOMException.prototype.code just missed
|
---|
104 | var INCORRECT_CODE = INCORRECT_CONSTRUCTOR || fails(function () {
|
---|
105 | return new NativeDOMException(1, 'DataCloneError').code !== 25;
|
---|
106 | });
|
---|
107 |
|
---|
108 | // Deno 1.6.3- DOMException constants just missed
|
---|
109 | var MISSED_CONSTANTS = INCORRECT_CONSTRUCTOR
|
---|
110 | || NativeDOMException[DATA_CLONE_ERR] !== 25
|
---|
111 | || NativeDOMExceptionPrototype[DATA_CLONE_ERR] !== 25;
|
---|
112 |
|
---|
113 | var FORCED_CONSTRUCTOR = IS_PURE ? INCORRECT_TO_STRING || INCORRECT_CODE || MISSED_CONSTANTS : INCORRECT_CONSTRUCTOR;
|
---|
114 |
|
---|
115 | // `DOMException` constructor
|
---|
116 | // https://webidl.spec.whatwg.org/#idl-DOMException
|
---|
117 | $({ global: true, constructor: true, forced: FORCED_CONSTRUCTOR }, {
|
---|
118 | DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
|
---|
119 | });
|
---|
120 |
|
---|
121 | var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
|
---|
122 | var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
|
---|
123 |
|
---|
124 | if (INCORRECT_TO_STRING && (IS_PURE || NativeDOMException === PolyfilledDOMException)) {
|
---|
125 | defineBuiltIn(PolyfilledDOMExceptionPrototype, 'toString', errorToString);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (INCORRECT_CODE && DESCRIPTORS && NativeDOMException === PolyfilledDOMException) {
|
---|
129 | defineBuiltInAccessor(PolyfilledDOMExceptionPrototype, 'code', createGetterDescriptor(function () {
|
---|
130 | return codeFor(anObject(this).name);
|
---|
131 | }));
|
---|
132 | }
|
---|
133 |
|
---|
134 | // `DOMException` constants
|
---|
135 | for (var key in DOMExceptionConstants) if (hasOwn(DOMExceptionConstants, key)) {
|
---|
136 | var constant = DOMExceptionConstants[key];
|
---|
137 | var constantName = constant.s;
|
---|
138 | var descriptor = createPropertyDescriptor(6, constant.c);
|
---|
139 | if (!hasOwn(PolyfilledDOMException, constantName)) {
|
---|
140 | defineProperty(PolyfilledDOMException, constantName, descriptor);
|
---|
141 | }
|
---|
142 | if (!hasOwn(PolyfilledDOMExceptionPrototype, constantName)) {
|
---|
143 | defineProperty(PolyfilledDOMExceptionPrototype, constantName, descriptor);
|
---|
144 | }
|
---|
145 | }
|
---|