|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Linus Groh <mail@linusgroh.de> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <AK/FlyString.h> |
| 30 | +#include <AK/NonnullRefPtr.h> |
| 31 | +#include <AK/RefCounted.h> |
| 32 | +#include <LibWeb/Bindings/WindowObject.h> |
| 33 | +#include <LibWeb/Bindings/Wrappable.h> |
| 34 | + |
| 35 | +namespace Web::DOM { |
| 36 | + |
| 37 | +// The following have a legacy code value but *don't* produce it as |
| 38 | +// DOMException.code value when used as name (and are therefore omitted here): |
| 39 | +// - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2) |
| 40 | +// - NoDataAllowedError (NO_DATA_ALLOWED_ERR = 6) |
| 41 | +// - ValidationError (VALIDATION_ERR = 16) |
| 42 | +#define ENUMERATE_DOM_EXCEPTION_LEGACY_CODES \ |
| 43 | + __ENUMERATE(IndexSizeError, 1) \ |
| 44 | + __ENUMERATE(HierarchyRequestError, 3) \ |
| 45 | + __ENUMERATE(WrongDocumentError, 4) \ |
| 46 | + __ENUMERATE(InvalidCharacterError, 5) \ |
| 47 | + __ENUMERATE(NoModificationAllowedError, 7) \ |
| 48 | + __ENUMERATE(NotFoundError, 8) \ |
| 49 | + __ENUMERATE(NotSupportedError, 9) \ |
| 50 | + __ENUMERATE(InUseAttributeError, 10) \ |
| 51 | + __ENUMERATE(InvalidStateError, 11) \ |
| 52 | + __ENUMERATE(SyntaxError, 12) \ |
| 53 | + __ENUMERATE(InvalidModificationError, 13) \ |
| 54 | + __ENUMERATE(NamespaceError, 14) \ |
| 55 | + __ENUMERATE(InvalidAccessError, 15) \ |
| 56 | + __ENUMERATE(TypeMismatchError, 17) \ |
| 57 | + __ENUMERATE(SecurityError, 18) \ |
| 58 | + __ENUMERATE(NetworkError, 19) \ |
| 59 | + __ENUMERATE(AbortError, 20) \ |
| 60 | + __ENUMERATE(URLMismatchError, 21) \ |
| 61 | + __ENUMERATE(QuotaExceededError, 22) \ |
| 62 | + __ENUMERATE(TimeoutError, 23) \ |
| 63 | + __ENUMERATE(InvalidNodeTypeError, 24) \ |
| 64 | + __ENUMERATE(DataCloneError, 25) |
| 65 | + |
| 66 | +// https://heycam.github.io/webidl/#idl-DOMException-error-names |
| 67 | +// Same order as in the spec document, also matches the legacy codes order above. |
| 68 | +#define ENUMERATE_DOM_EXCEPTION_ERROR_NAMES \ |
| 69 | + __ENUMERATE(IndexSizeError) /* Deprecated */ \ |
| 70 | + __ENUMERATE(HierarchyRequestError) \ |
| 71 | + __ENUMERATE(WrongDocumentError) \ |
| 72 | + __ENUMERATE(InvalidCharacterError) \ |
| 73 | + __ENUMERATE(NoModificationAllowedError) \ |
| 74 | + __ENUMERATE(NotFoundError) \ |
| 75 | + __ENUMERATE(NotSupportedError) \ |
| 76 | + __ENUMERATE(InUseAttributeError) \ |
| 77 | + __ENUMERATE(InvalidStateError) \ |
| 78 | + __ENUMERATE(SyntaxError) \ |
| 79 | + __ENUMERATE(InvalidModificationError) \ |
| 80 | + __ENUMERATE(NamespaceError) \ |
| 81 | + __ENUMERATE(InvalidAccessError) /* Deprecated */ \ |
| 82 | + __ENUMERATE(TypeMismatchError) /* Deprecated */ \ |
| 83 | + __ENUMERATE(SecurityError) \ |
| 84 | + __ENUMERATE(NetworkError) \ |
| 85 | + __ENUMERATE(AbortError) \ |
| 86 | + __ENUMERATE(URLMismatchError) \ |
| 87 | + __ENUMERATE(QuotaExceededError) \ |
| 88 | + __ENUMERATE(TimeoutError) \ |
| 89 | + __ENUMERATE(InvalidNodeTypeError) \ |
| 90 | + __ENUMERATE(DataCloneError) \ |
| 91 | + __ENUMERATE(EncodingError) \ |
| 92 | + __ENUMERATE(NotReadableError) \ |
| 93 | + __ENUMERATE(UnknownError) \ |
| 94 | + __ENUMERATE(ConstraintError) \ |
| 95 | + __ENUMERATE(DataError) \ |
| 96 | + __ENUMERATE(TransactionInactiveError) \ |
| 97 | + __ENUMERATE(ReadOnlyError) \ |
| 98 | + __ENUMERATE(VersionError) \ |
| 99 | + __ENUMERATE(OperationError) \ |
| 100 | + __ENUMERATE(NotAllowedError) |
| 101 | + |
| 102 | +static u16 get_legacy_code_for_name(const FlyString& name) |
| 103 | +{ |
| 104 | +#define __ENUMERATE(ErrorName, code) \ |
| 105 | + if (name == #ErrorName) \ |
| 106 | + return code; |
| 107 | + ENUMERATE_DOM_EXCEPTION_LEGACY_CODES |
| 108 | +#undef __ENUMERATE |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +// https://heycam.github.io/webidl/#idl-DOMException |
| 113 | +class DOMException final |
| 114 | + : public RefCounted<DOMException> |
| 115 | + , public Bindings::Wrappable { |
| 116 | +public: |
| 117 | + using WrapperType = Bindings::DOMExceptionWrapper; |
| 118 | + |
| 119 | + static NonnullRefPtr<DOMException> create(const FlyString& name, const FlyString& message) |
| 120 | + { |
| 121 | + return adopt(*new DOMException(name, message)); |
| 122 | + } |
| 123 | + |
| 124 | + // JS constructor has message first, name second |
| 125 | + static NonnullRefPtr<DOMException> create_with_global_object(Bindings::WindowObject&, const FlyString& message, const FlyString& name) |
| 126 | + { |
| 127 | + return adopt(*new DOMException(name, message)); |
| 128 | + } |
| 129 | + |
| 130 | + const FlyString& name() const { return m_name; } |
| 131 | + const FlyString& message() const { return m_message; } |
| 132 | + u16 code() const { return get_legacy_code_for_name(m_name); } |
| 133 | + |
| 134 | +protected: |
| 135 | + DOMException(const FlyString& name, const FlyString& message) |
| 136 | + : m_name(name) |
| 137 | + , m_message(message) |
| 138 | + { |
| 139 | + } |
| 140 | + |
| 141 | +private: |
| 142 | + FlyString m_name; |
| 143 | + FlyString m_message; |
| 144 | +}; |
| 145 | + |
| 146 | +#define __ENUMERATE(ErrorName) \ |
| 147 | + class ErrorName final { \ |
| 148 | + public: \ |
| 149 | + static NonnullRefPtr<DOMException> create(const FlyString& message) \ |
| 150 | + { \ |
| 151 | + return DOMException::create(#ErrorName, message); \ |
| 152 | + } \ |
| 153 | + }; |
| 154 | +ENUMERATE_DOM_EXCEPTION_ERROR_NAMES |
| 155 | +#undef __ENUMERATE |
| 156 | + |
| 157 | +} |
0 commit comments