LibWeb: Add fieldset elements property

This commit is contained in:
Bastiaan van der Plaat 2023-12-10 10:42:41 +01:00 committed by Tim Flynn
parent 0fe192dfd9
commit ea04a86715
Notes: sideshowbarker 2024-07-17 01:21:02 +09:00
6 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,2 @@
1. 3
2. "PASS"

View file

@ -0,0 +1,24 @@
<script src="./include.js"></script>
<script>
test(() => {
let testCounter = 1;
function testPart(part) {
println(`${testCounter++}. ${JSON.stringify(part())}`);
}
// 1. Get fieldset elements length
testPart(() => {
const fieldset = document.createElement('fieldset');
fieldset.innerHTML = `<input><input><button></button>`;
return fieldset.elements.length;
});
// 2. Get fieldset elements set stuff
testPart(() => {
const fieldset = document.createElement('fieldset');
fieldset.innerHTML = `<input><input value="FAIL"><button></button>`;
fieldset.elements[0].value = 'PASS';
return fieldset.children[0].value;
});
});
</script>

View file

@ -5,8 +5,14 @@
*/ */
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/HTMLButtonElement.h>
#include <LibWeb/HTML/HTMLFieldSetElement.h> #include <LibWeb/HTML/HTMLFieldSetElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLLegendElement.h> #include <LibWeb/HTML/HTMLLegendElement.h>
#include <LibWeb/HTML/HTMLObjectElement.h>
#include <LibWeb/HTML/HTMLOutputElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
namespace Web::HTML { namespace Web::HTML {
@ -25,6 +31,12 @@ void HTMLFieldSetElement::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLFieldSetElementPrototype>(realm, "HTMLFieldSetElement"_fly_string)); set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLFieldSetElementPrototype>(realm, "HTMLFieldSetElement"_fly_string));
} }
void HTMLFieldSetElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_elements);
}
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled // https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
bool HTMLFieldSetElement::is_disabled() const bool HTMLFieldSetElement::is_disabled() const
{ {
@ -45,4 +57,23 @@ bool HTMLFieldSetElement::is_disabled() const
return false; return false;
} }
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-fieldset-elements
JS::GCPtr<DOM::HTMLCollection> const& HTMLFieldSetElement::elements()
{
// The elements IDL attribute must return an HTMLCollection rooted at the fieldset element, whose filter matches listed elements.
if (!m_elements) {
m_elements = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Descendants, [](DOM::Element const& element) {
// FIXME: Form-associated custom elements return also true
return is<HTMLButtonElement>(element)
|| is<HTMLFieldSetElement>(element)
|| is<HTMLInputElement>(element)
|| is<HTMLObjectElement>(element)
|| is<HTMLOutputElement>(element)
|| is<HTMLSelectElement>(element)
|| is<HTMLTextAreaElement>(element);
});
}
return m_elements;
}
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <LibWeb/ARIA/Roles.h> #include <LibWeb/ARIA/Roles.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/HTML/FormAssociatedElement.h> #include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLElement.h> #include <LibWeb/HTML/HTMLElement.h>
@ -30,6 +31,8 @@ public:
bool is_disabled() const; bool is_disabled() const;
JS::GCPtr<DOM::HTMLCollection> const& elements();
// ^FormAssociatedElement // ^FormAssociatedElement
// https://html.spec.whatwg.org/multipage/forms.html#category-listed // https://html.spec.whatwg.org/multipage/forms.html#category-listed
virtual bool is_listed() const override { return true; } virtual bool is_listed() const override { return true; }
@ -43,6 +46,9 @@ private:
HTMLFieldSetElement(DOM::Document&, DOM::QualifiedName); HTMLFieldSetElement(DOM::Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
JS::GCPtr<DOM::HTMLCollection> m_elements;
}; };
} }

View file

@ -12,7 +12,7 @@ interface HTMLFieldSetElement : HTMLElement {
readonly attribute DOMString type; readonly attribute DOMString type;
// FIXME: [SameObject] readonly attribute HTMLCollection elements; [SameObject] readonly attribute HTMLCollection elements;
// FIXME: readonly attribute boolean willValidate; // FIXME: readonly attribute boolean willValidate;
// FIXME: [SameObject] readonly attribute ValidityState validity; // FIXME: [SameObject] readonly attribute ValidityState validity;

View file

@ -396,6 +396,8 @@ static bool is_form_control(DOM::Element const& element)
return true; return true;
} }
// FIXME: Form-associated custom elements return also true
return false; return false;
} }