mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:47:35 +00:00
LibWeb: Add document.querySelector()
This commit is contained in:
parent
6e505b853e
commit
67b742bf32
8 changed files with 46 additions and 5 deletions
|
@ -41,6 +41,7 @@ DocumentWrapper::DocumentWrapper(Document& document)
|
|||
: NodeWrapper(document)
|
||||
{
|
||||
put_native_function("getElementById", get_element_by_id, 1);
|
||||
put_native_function("querySelector", query_selector, 1);
|
||||
put_native_function("querySelectorAll", query_selector_all, 1);
|
||||
}
|
||||
|
||||
|
@ -86,6 +87,23 @@ JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter)
|
|||
return wrap(interpreter.heap(), const_cast<Element&>(*element));
|
||||
}
|
||||
|
||||
JS::Value DocumentWrapper::query_selector(JS::Interpreter& interpreter)
|
||||
{
|
||||
auto* document = document_from(interpreter);
|
||||
if (!document)
|
||||
return {};
|
||||
if (!interpreter.argument_count())
|
||||
return interpreter.throw_exception<JS::TypeError>("querySelector() needs one argument");
|
||||
auto selector = interpreter.argument(0).to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
// FIXME: Throw if selector is invalid
|
||||
auto element = document->query_selector(selector);
|
||||
if (!element)
|
||||
return JS::js_null();
|
||||
return wrap(interpreter.heap(), *element);
|
||||
}
|
||||
|
||||
JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
|
||||
{
|
||||
auto* document = document_from(interpreter);
|
||||
|
@ -96,6 +114,7 @@ JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
|
|||
auto selector = interpreter.argument(0).to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
// FIXME: Throw if selector is invalid
|
||||
auto elements = document->query_selector_all(selector);
|
||||
// FIXME: This should be a static NodeList, not a plain JS::Array.
|
||||
auto* node_list = JS::Array::create(interpreter.global_object());
|
||||
|
|
|
@ -43,6 +43,7 @@ private:
|
|||
virtual const char* class_name() const override { return "DocumentWrapper"; }
|
||||
|
||||
static JS::Value get_element_by_id(JS::Interpreter&);
|
||||
static JS::Value query_selector(JS::Interpreter&);
|
||||
static JS::Value query_selector_all(JS::Interpreter&);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue