diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 409ccd43de..34a964d9bd 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -952,10 +952,15 @@ NonnullRefPtr Document::create_range() } // https://dom.spec.whatwg.org/#dom-document-createevent -NonnullRefPtr Document::create_event(String const& interface) +DOM::ExceptionOr> Document::create_event(String const& interface) { - auto interface_lowercase = interface.to_lowercase(); + // NOTE: This is named event here, since we do step 5 and 6 as soon as possible for each case. + // 1. Let constructor be null. RefPtr event; + + // 2. If interface is an ASCII case-insensitive match for any of the strings in the first column in the following table, + // then set constructor to the interface in the second column on the same row as the matching string: + auto interface_lowercase = interface.to_lowercase(); if (interface_lowercase == "beforeunloadevent") { event = Event::create(""); // FIXME: Create BeforeUnloadEvent } else if (interface_lowercase == "compositionevent") { @@ -992,17 +997,29 @@ NonnullRefPtr Document::create_event(String const& interface) event = Event::create(""); // FIXME: Create TouchEvent } else if (interface_lowercase.is_one_of("uievent", "uievents")) { event = UIEvents::UIEvent::create(""); - } else { - // FIXME: - // 3. If constructor is null, then throw a "NotSupportedError" DOMException. - // 4. If the interface indicated by constructor is not exposed on the relevant global object of this, then throw a "NotSupportedError" DOMException. - TODO(); } - // Setting type to empty string is handled by each constructor. - // FIXME: - // 7. Initialize event’s timeStamp attribute to a DOMHighResTimeStamp representing the high resolution time from the time origin to now. + + // 3. If constructor is null, then throw a "NotSupportedError" DOMException. + if (!event) { + return DOM::NotSupportedError::create("No constructor for interface found"); + } + + // FIXME: 4. If the interface indicated by constructor is not exposed on the relevant global object of this, then throw a "NotSupportedError" DOMException. + + // NOTE: These are done in the if-chain above + // 5. Let event be the result of creating an event given constructor. + // 6. Initialize event’s type attribute to the empty string. + // NOTE: This is handled by each constructor. + + // FIXME: 7. Initialize event’s timeStamp attribute to the result of calling current high resolution time with this’s relevant global object. + + // 8. Initialize event’s isTrusted attribute to false. event->set_is_trusted(false); + + // 9. Unset event’s initialized flag. event->set_initialized(false); + + // 10. Return event. return event.release_nonnull(); } diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 1fe315a9dd..ac646979f1 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -186,7 +186,7 @@ public: NonnullRefPtr create_text_node(String const& data); NonnullRefPtr create_comment(String const& data); NonnullRefPtr create_range(); - NonnullRefPtr create_event(String const& interface); + ExceptionOr> create_event(String const& interface); void set_pending_parsing_blocking_script(Badge, HTML::HTMLScriptElement*); HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }