Skip to content

Commit ada71dc

Browse files
linusgawesomekling
authored andcommitted
LibWeb: Add DOM::DOMException class and bindings
1 parent cc0f591 commit ada71dc

File tree

6 files changed

+207
-1
lines changed

6 files changed

+207
-1
lines changed

Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <LibWeb/Bindings/CharacterDataPrototype.h>
3535
#include <LibWeb/Bindings/CommentConstructor.h>
3636
#include <LibWeb/Bindings/CommentPrototype.h>
37+
#include <LibWeb/Bindings/DOMExceptionConstructor.h>
38+
#include <LibWeb/Bindings/DOMExceptionPrototype.h>
3739
#include <LibWeb/Bindings/DOMImplementationConstructor.h>
3840
#include <LibWeb/Bindings/DOMImplementationPrototype.h>
3941
#include <LibWeb/Bindings/DocumentConstructor.h>
@@ -237,6 +239,7 @@
237239
ADD_WINDOW_OBJECT_INTERFACE(DocumentFragment) \
238240
ADD_WINDOW_OBJECT_INTERFACE(Document) \
239241
ADD_WINDOW_OBJECT_INTERFACE(DocumentType) \
242+
ADD_WINDOW_OBJECT_INTERFACE(DOMException) \
240243
ADD_WINDOW_OBJECT_INTERFACE(DOMImplementation) \
241244
ADD_WINDOW_OBJECT_INTERFACE(Element) \
242245
ADD_WINDOW_OBJECT_INTERFACE(Event) \

Userland/Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ libweb_js_wrapper(DOM/Comment)
281281
libweb_js_wrapper(DOM/Document)
282282
libweb_js_wrapper(DOM/DocumentFragment)
283283
libweb_js_wrapper(DOM/DocumentType)
284+
libweb_js_wrapper(DOM/DOMException)
284285
libweb_js_wrapper(DOM/DOMImplementation)
285286
libweb_js_wrapper(DOM/Element)
286287
libweb_js_wrapper(DOM/Event)

Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,13 @@ namespace Web::Bindings {
11531153
{
11541154
)~~~");
11551155

1156-
if (!interface.parent_name.is_empty()) {
1156+
if (interface.name == "DOMException") {
1157+
// https://heycam.github.io/webidl/#es-DOMException-specialness
1158+
// Object.getPrototypeOf(DOMException.prototype) === Error.prototype
1159+
generator.append(R"~~~(
1160+
set_prototype(global_object.error_prototype());
1161+
)~~~");
1162+
} else if (!interface.parent_name.is_empty()) {
11571163
generator.append(R"~~~(
11581164
set_prototype(&static_cast<WindowObject&>(global_object).ensure_web_prototype<@prototype_base_class@>("@parent_name@"));
11591165
)~~~");
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
interface DOMException {
2+
3+
// FIXME: Support parameter default values in WrapperGenerator
4+
// constructor(optional DOMString message = "", optional DOMString name = "Error");
5+
constructor(optional DOMString message, optional DOMString name);
6+
7+
readonly attribute DOMString name;
8+
readonly attribute DOMString message;
9+
readonly attribute unsigned short code;
10+
11+
const unsigned short INDEX_SIZE_ERR = 1;
12+
const unsigned short DOMSTRING_SIZE_ERR = 2;
13+
const unsigned short HIERARCHY_REQUEST_ERR = 3;
14+
const unsigned short WRONG_DOCUMENT_ERR = 4;
15+
const unsigned short INVALID_CHARACTER_ERR = 5;
16+
const unsigned short NO_DATA_ALLOWED_ERR = 6;
17+
const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7;
18+
const unsigned short NOT_FOUND_ERR = 8;
19+
const unsigned short NOT_SUPPORTED_ERR = 9;
20+
const unsigned short INUSE_ATTRIBUTE_ERR = 10;
21+
const unsigned short INVALID_STATE_ERR = 11;
22+
const unsigned short SYNTAX_ERR = 12;
23+
const unsigned short INVALID_MODIFICATION_ERR = 13;
24+
const unsigned short NAMESPACE_ERR = 14;
25+
const unsigned short INVALID_ACCESS_ERR = 15;
26+
const unsigned short VALIDATION_ERR = 16;
27+
const unsigned short TYPE_MISMATCH_ERR = 17;
28+
const unsigned short SECURITY_ERR = 18;
29+
const unsigned short NETWORK_ERR = 19;
30+
const unsigned short ABORT_ERR = 20;
31+
const unsigned short URL_MISMATCH_ERR = 21;
32+
const unsigned short QUOTA_EXCEEDED_ERR = 22;
33+
const unsigned short TIMEOUT_ERR = 23;
34+
const unsigned short INVALID_NODE_TYPE_ERR = 24;
35+
const unsigned short DATA_CLONE_ERR = 25;
36+
37+
};

Userland/Libraries/LibWeb/Forward.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Comment;
4343
class Document;
4444
class DocumentFragment;
4545
class DocumentType;
46+
class DOMException;
4647
class DOMImplementation;
4748
class Element;
4849
class Event;
@@ -205,6 +206,7 @@ class CommentWrapper;
205206
class DocumentFragmentWrapper;
206207
class DocumentTypeWrapper;
207208
class DocumentWrapper;
209+
class DOMExceptionWrapper;
208210
class DOMImplementationWrapper;
209211
class ElementWrapper;
210212
class EventListenerWrapper;

0 commit comments

Comments
 (0)