1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:08:11 +00:00

WebContent: Add $() and $$() functions to browser console

These are based on the ones in Firefox:

`$(selector, element = document)`:
    Equivalent to `element.querySelector(selector)`.

`$$(selector, element = document)`:
    Equivalent to `element.querySelectorAll(selector)`.
This commit is contained in:
Sam Atkins 2022-11-19 12:30:16 +00:00 committed by Linus Groh
parent c793beb0cc
commit 4ad1695c78
2 changed files with 63 additions and 0 deletions

View file

@ -5,8 +5,11 @@
*/
#include "ConsoleGlobalObject.h"
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/NodeList.h>
#include <LibWeb/HTML/Window.h>
namespace WebContent {
@ -23,6 +26,8 @@ void ConsoleGlobalObject::initialize(JS::Realm& realm)
define_native_accessor(realm, "$0", $0_getter, nullptr, 0);
define_native_accessor(realm, "$_", $__getter, nullptr, 0);
define_native_function(realm, "$", $_function, 2, JS::default_attributes);
define_native_function(realm, "$$", $$_function, 2, JS::default_attributes);
}
void ConsoleGlobalObject::visit_edges(Visitor& visitor)
@ -117,4 +122,58 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$__getter)
return console_global_object->m_most_recent_result;
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$_function)
{
auto* console_global_object = TRY(get_console(vm));
auto& window = *console_global_object->m_window_object;
auto selector = TRY(vm.argument(0).to_string(vm));
if (vm.argument_count() > 1) {
auto element_value = vm.argument(1);
if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
}
auto& element = static_cast<Web::DOM::ParentNode&>(element_value.as_object());
return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
return element.query_selector(selector);
}));
}
return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
return window.associated_document().query_selector(selector);
}));
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$$_function)
{
auto* console_global_object = TRY(get_console(vm));
auto& window = *console_global_object->m_window_object;
auto selector = TRY(vm.argument(0).to_string(vm));
Web::DOM::ParentNode* element = &window.associated_document();
if (vm.argument_count() > 1) {
auto element_value = vm.argument(1);
if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
}
element = static_cast<Web::DOM::ParentNode*>(&element_value.as_object());
}
auto node_list = TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
return element->query_selector_all(selector);
}));
auto* array = TRY(JS::Array::create(*vm.current_realm(), node_list->length()));
for (auto i = 0u; i < node_list->length(); ++i) {
TRY(array->create_data_property_or_throw(i, node_list->item_value(i)));
}
return array;
}
}