UNPKG

6.07 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var tryNodeRequire = require('../internals/try-node-require');
4var getBuiltIn = require('../internals/get-built-in');
5var fails = require('../internals/fails');
6var create = require('../internals/object-create');
7var createPropertyDescriptor = require('../internals/create-property-descriptor');
8var defineProperty = require('../internals/object-define-property').f;
9var defineBuiltIn = require('../internals/define-built-in');
10var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
11var hasOwn = require('../internals/has-own-property');
12var anInstance = require('../internals/an-instance');
13var anObject = require('../internals/an-object');
14var errorToString = require('../internals/error-to-string');
15var normalizeStringArgument = require('../internals/normalize-string-argument');
16var DOMExceptionConstants = require('../internals/dom-exception-constants');
17var clearErrorStack = require('../internals/error-stack-clear');
18var InternalStateModule = require('../internals/internal-state');
19var DESCRIPTORS = require('../internals/descriptors');
20var IS_PURE = require('../internals/is-pure');
21
22var DOM_EXCEPTION = 'DOMException';
23var DATA_CLONE_ERR = 'DATA_CLONE_ERR';
24var Error = getBuiltIn('Error');
25// NodeJS < 17.0 does not expose `DOMException` to global
26var NativeDOMException = getBuiltIn(DOM_EXCEPTION) || (function () {
27 try {
28 // NodeJS < 15.0 does not expose `MessageChannel` to global
29 var MessageChannel = getBuiltIn('MessageChannel') || tryNodeRequire('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})();
36var NativeDOMExceptionPrototype = NativeDOMException && NativeDOMException.prototype;
37var ErrorPrototype = Error.prototype;
38var setInternalState = InternalStateModule.set;
39var getInternalState = InternalStateModule.getterFor(DOM_EXCEPTION);
40var HAS_STACK = 'stack' in new Error(DOM_EXCEPTION);
41
42var codeFor = function (name) {
43 return hasOwn(DOMExceptionConstants, name) && DOMExceptionConstants[name].m ? DOMExceptionConstants[name].c : 0;
44};
45
46var $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
70var DOMExceptionPrototype = $DOMException.prototype = create(ErrorPrototype);
71
72var createGetterDescriptor = function (get) {
73 return { enumerable: true, configurable: true, get: get };
74};
75
76var getterFor = function (key) {
77 return createGetterDescriptor(function () {
78 return getInternalState(this)[key];
79 });
80};
81
82if (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
91defineProperty(DOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, $DOMException));
92
93// FF36- DOMException is a function, but can't be constructed
94var INCORRECT_CONSTRUCTOR = fails(function () {
95 return !(new NativeDOMException() instanceof Error);
96});
97
98// Safari 10.1 / Chrome 32- / IE8- DOMException.prototype.toString bugs
99var 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
104var 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
109var MISSED_CONSTANTS = INCORRECT_CONSTRUCTOR
110 || NativeDOMException[DATA_CLONE_ERR] !== 25
111 || NativeDOMExceptionPrototype[DATA_CLONE_ERR] !== 25;
112
113var 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
121var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
122var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
123
124if (INCORRECT_TO_STRING && (IS_PURE || NativeDOMException === PolyfilledDOMException)) {
125 defineBuiltIn(PolyfilledDOMExceptionPrototype, 'toString', errorToString);
126}
127
128if (INCORRECT_CODE && DESCRIPTORS && NativeDOMException === PolyfilledDOMException) {
129 defineBuiltInAccessor(PolyfilledDOMExceptionPrototype, 'code', createGetterDescriptor(function () {
130 return codeFor(anObject(this).name);
131 }));
132}
133
134// `DOMException` constants
135for (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}