mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
LibWeb: Implement DOMStringList
This commit is contained in:
parent
aa08e52548
commit
75216182c9
Notes:
github-actions[bot]
2024-07-29 09:19:56 +00:00
Author: https://github.com/jamierocks Commit: https://github.com/LadybirdBrowser/ladybird/commit/75216182c9a Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/731 Reviewed-by: https://github.com/AtkinsSJ
@ -26,6 +26,7 @@ source_set("HTML") {
|
||||
"CloseWatcher.cpp",
|
||||
"CloseWatcherManager.cpp",
|
||||
"DOMParser.cpp",
|
||||
"DOMStringList.cpp",
|
||||
"DOMStringMap.cpp",
|
||||
"DataTransfer.cpp",
|
||||
"Dates.cpp",
|
||||
|
@ -119,6 +119,7 @@ standard_idl_files = [
|
||||
"//Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementRegistry.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/DataTransfer.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/DOMParser.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/DOMStringList.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/DOMStringMap.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/ElementInternals.idl",
|
||||
"//Userland/Libraries/LibWeb/HTML/ErrorEvent.idl",
|
||||
|
@ -70,6 +70,7 @@ DOMQuad
|
||||
DOMRect
|
||||
DOMRectList
|
||||
DOMRectReadOnly
|
||||
DOMStringList
|
||||
DOMStringMap
|
||||
DOMTokenList
|
||||
DataTransfer
|
||||
|
@ -280,6 +280,7 @@ set(SOURCES
|
||||
HTML/DedicatedWorkerGlobalScope.cpp
|
||||
HTML/DocumentState.cpp
|
||||
HTML/DOMParser.cpp
|
||||
HTML/DOMStringList.cpp
|
||||
HTML/DOMStringMap.cpp
|
||||
HTML/DragEvent.cpp
|
||||
HTML/ElementInternals.cpp
|
||||
|
@ -360,6 +360,7 @@ class CustomElementRegistry;
|
||||
class DecodedImageData;
|
||||
class DocumentState;
|
||||
class DOMParser;
|
||||
class DOMStringList;
|
||||
class DOMStringMap;
|
||||
class DragEvent;
|
||||
class ElementInternals;
|
||||
|
74
Userland/Libraries/LibWeb/HTML/DOMStringList.cpp
Normal file
74
Userland/Libraries/LibWeb/HTML/DOMStringList.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/DOMStringListPrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/HTML/DOMStringList.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(DOMStringList);
|
||||
|
||||
JS::NonnullGCPtr<DOMStringList> DOMStringList::create(JS::Realm& realm, Vector<String> list)
|
||||
{
|
||||
return realm.heap().allocate<DOMStringList>(realm, realm, list);
|
||||
}
|
||||
|
||||
DOMStringList::DOMStringList(JS::Realm& realm, Vector<String> list)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_list(move(list))
|
||||
{
|
||||
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true };
|
||||
}
|
||||
|
||||
void DOMStringList::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMStringList);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-length
|
||||
u32 DOMStringList::length() const
|
||||
{
|
||||
// The length getter steps are to return this's associated list's size.
|
||||
return m_list.size();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-item
|
||||
Optional<String> DOMStringList::item(u32 index) const
|
||||
{
|
||||
// The item(index) method steps are to return the indexth item in this's associated list, or null if index plus one
|
||||
// is greater than this's associated list's size.
|
||||
if (index + 1 > m_list.size())
|
||||
return {};
|
||||
|
||||
return m_list.at(index);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-contains
|
||||
bool DOMStringList::contains(StringView string)
|
||||
{
|
||||
// The contains(string) method steps are to return true if this's associated list contains string, and false otherwise.
|
||||
return m_list.contains_slow(string);
|
||||
}
|
||||
|
||||
bool DOMStringList::is_supported_property_index(u32 index) const
|
||||
{
|
||||
// The DOMStringList interface supports indexed properties. The supported property indices are the indices of this's
|
||||
// associated list.
|
||||
return index < m_list.size();
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<JS::Value> DOMStringList::item_value(size_t index) const
|
||||
{
|
||||
if (index + 1 > m_list.size())
|
||||
return JS::js_undefined();
|
||||
|
||||
return JS::PrimitiveString::create(vm(), m_list.at(index));
|
||||
}
|
||||
|
||||
}
|
36
Userland/Libraries/LibWeb/HTML/DOMStringList.h
Normal file
36
Userland/Libraries/LibWeb/HTML/DOMStringList.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
class DOMStringList final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(DOMStringList, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(DOMStringList);
|
||||
|
||||
public:
|
||||
static JS::NonnullGCPtr<DOMStringList> create(JS::Realm&, Vector<String>);
|
||||
|
||||
u32 length() const;
|
||||
Optional<String> item(u32 index) const;
|
||||
bool contains(StringView string);
|
||||
|
||||
virtual bool is_supported_property_index(u32) const override;
|
||||
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
|
||||
|
||||
private:
|
||||
explicit DOMStringList(JS::Realm&, Vector<String>);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
|
||||
Vector<String> m_list;
|
||||
};
|
||||
|
||||
}
|
7
Userland/Libraries/LibWeb/HTML/DOMStringList.idl
Normal file
7
Userland/Libraries/LibWeb/HTML/DOMStringList.idl
Normal file
@ -0,0 +1,7 @@
|
||||
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#domstringlist
|
||||
[Exposed=(Window,Worker)]
|
||||
interface DOMStringList {
|
||||
readonly attribute unsigned long length;
|
||||
getter DOMString? item(unsigned long index);
|
||||
boolean contains(DOMString string);
|
||||
};
|
@ -104,6 +104,7 @@ libweb_js_bindings(HTML/CustomElements/CustomElementRegistry)
|
||||
libweb_js_bindings(HTML/DataTransfer)
|
||||
libweb_js_bindings(HTML/DedicatedWorkerGlobalScope GLOBAL)
|
||||
libweb_js_bindings(HTML/DOMParser)
|
||||
libweb_js_bindings(HTML/DOMStringList)
|
||||
libweb_js_bindings(HTML/DOMStringMap)
|
||||
libweb_js_bindings(HTML/DragEvent)
|
||||
libweb_js_bindings(HTML/ElementInternals)
|
||||
|
Loading…
Reference in New Issue
Block a user