From 4033776fb5ee9fecb02c61826fe3402192671ad2 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 9 Jun 2024 15:13:38 +1200 Subject: [PATCH] LibWeb: Add stub for ValidityState This fixes https://html5test.com/ as previously an exception was being thrown after trying to access this attribute which would then result in a popup about the test failing (and none of the test results being shown). (cherry picked from commit e0bbbc729b6aad04ceff5f67c3e2868b65970047, manually amended with the output of `git clang-format master`) --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/Forward.h | 1 + .../LibWeb/HTML/HTMLInputElement.cpp | 12 +++++++++ .../Libraries/LibWeb/HTML/HTMLInputElement.h | 2 ++ .../LibWeb/HTML/HTMLInputElement.idl | 3 ++- .../Libraries/LibWeb/HTML/ValidityState.cpp | 26 ++++++++++++++++++ .../Libraries/LibWeb/HTML/ValidityState.h | 27 +++++++++++++++++++ .../Libraries/LibWeb/HTML/ValidityState.idl | 15 +++++++++++ Userland/Libraries/LibWeb/idl_files.cmake | 1 + 9 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Userland/Libraries/LibWeb/HTML/ValidityState.cpp create mode 100644 Userland/Libraries/LibWeb/HTML/ValidityState.h create mode 100644 Userland/Libraries/LibWeb/HTML/ValidityState.idl diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index ce5d8b535a..124a1b9ee4 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -448,6 +448,7 @@ set(SOURCES HTML/WorkerGlobalScope.cpp HTML/WorkerLocation.cpp HTML/WorkerNavigator.cpp + HTML/ValidityState.cpp HighResolutionTime/Performance.cpp HighResolutionTime/TimeOrigin.cpp Infra/Base64.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 5f08b56a6b..32c40f769b 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -468,6 +468,7 @@ class TrackEvent; struct TransferDataHolder; class TraversableNavigable; class UserActivation; +class ValidityState; class VideoTrack; class VideoTrackList; class Window; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index be3a54bbd7..53fbebb3f6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,17 @@ void HTMLInputElement::visit_edges(Cell::Visitor& visitor) visitor.visit(m_image_request); } +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-validity +JS::NonnullGCPtr HTMLInputElement::validity() const +{ + auto& vm = this->vm(); + auto& realm = this->realm(); + + dbgln("FIXME: Implement validity attribute getter"); + + return vm.heap().allocate(realm, realm); +} + JS::GCPtr HTMLInputElement::create_layout_node(NonnullRefPtr style) { if (type_state() == TypeAttributeState::Hidden) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index a9dca6c56c..96bd516ca0 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -174,6 +174,8 @@ public: virtual void form_associated_element_was_removed(DOM::Node*) override; virtual void form_associated_element_attribute_changed(FlyString const&, Optional const&) override; + JS::NonnullGCPtr validity() const; + // ^HTMLElement // https://html.spec.whatwg.org/multipage/forms.html#category-label virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl index 654c218cd5..f50216a72f 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl @@ -1,5 +1,6 @@ #import #import +#import #import // https://html.spec.whatwg.org/multipage/input.html#htmlinputelement @@ -48,7 +49,7 @@ interface HTMLInputElement : HTMLElement { undefined stepDown(optional long n = 1); [FIXME] readonly attribute boolean willValidate; - [FIXME] readonly attribute ValidityState validity; + readonly attribute ValidityState validity; [FIXME] readonly attribute DOMString validationMessage; boolean checkValidity(); boolean reportValidity(); diff --git a/Userland/Libraries/LibWeb/HTML/ValidityState.cpp b/Userland/Libraries/LibWeb/HTML/ValidityState.cpp new file mode 100644 index 0000000000..00a80c3a6a --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/ValidityState.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::HTML { + +JS_DEFINE_ALLOCATOR(ValidityState); + +ValidityState::ValidityState(JS::Realm& realm) + : PlatformObject(realm) +{ +} + +void ValidityState::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(ValidityState); +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/ValidityState.h b/Userland/Libraries/LibWeb/HTML/ValidityState.h new file mode 100644 index 0000000000..8914630e64 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/ValidityState.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#validitystate +class ValidityState final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(ValidityState, Bindings::PlatformObject); + JS_DECLARE_ALLOCATOR(ValidityState); + +public: + virtual ~ValidityState() override = default; + +private: + ValidityState(JS::Realm&); + + virtual void initialize(JS::Realm&) override; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/ValidityState.idl b/Userland/Libraries/LibWeb/HTML/ValidityState.idl new file mode 100644 index 0000000000..24e15368c6 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/ValidityState.idl @@ -0,0 +1,15 @@ +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#validitystate +[Exposed=Window] +interface ValidityState { + [FIXME] readonly attribute boolean valueMissing; + [FIXME] readonly attribute boolean typeMismatch; + [FIXME] readonly attribute boolean patternMismatch; + [FIXME] readonly attribute boolean tooLong; + [FIXME] readonly attribute boolean tooShort; + [FIXME] readonly attribute boolean rangeUnderflow; + [FIXME] readonly attribute boolean rangeOverflow; + [FIXME] readonly attribute boolean stepMismatch; + [FIXME] readonly attribute boolean badInput; + [FIXME] readonly attribute boolean customError; + [FIXME] readonly attribute boolean valid; +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 96845cbb76..947e1dac05 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -211,6 +211,7 @@ libweb_js_bindings(HTML/TimeRanges) libweb_js_bindings(HTML/ToggleEvent) libweb_js_bindings(HTML/TrackEvent) libweb_js_bindings(HTML/UserActivation) +libweb_js_bindings(HTML/ValidityState) libweb_js_bindings(HTML/VideoTrack) libweb_js_bindings(HTML/VideoTrackList) libweb_js_bindings(HTML/Window GLOBAL)