LibWeb: Implement Document.evaluate and related XPath methods

This commit is contained in:
Johannes Gustafsson 2025-09-29 22:16:30 +02:00 committed by Jelle Raaijmakers
commit d2030a5377
Notes: github-actions[bot] 2025-10-03 11:17:31 +00:00
3 changed files with 29 additions and 0 deletions

View file

@ -6802,6 +6802,23 @@ HashMap<FlyString, GC::Ref<Web::CSS::CSSPropertyRule>>& Document::registered_cus
return m_registered_custom_properties; return m_registered_custom_properties;
} }
WebIDL::ExceptionOr<GC::Ref<XPath::XPathExpression>> Document::create_expression(String const& expression, GC::Ptr<XPath::XPathNSResolver> resolver)
{
auto& realm = this->realm();
return XPath::create_expression(realm, expression, resolver);
}
WebIDL::ExceptionOr<GC::Ref<XPath::XPathResult>> Document::evaluate(String const& expression, DOM::Node const& context_node, GC::Ptr<XPath::XPathNSResolver> resolver, WebIDL::UnsignedShort type, GC::Ptr<XPath::XPathResult> result)
{
auto& realm = this->realm();
return XPath::evaluate(realm, expression, context_node, resolver, type, result);
}
GC::Ref<DOM::Node> Document::create_ns_resolver(GC::Ref<DOM::Node> node_resolver)
{
return node_resolver;
}
NonnullRefPtr<CSS::StyleValue const> Document::custom_property_initial_value(FlyString const& name) const NonnullRefPtr<CSS::StyleValue const> Document::custom_property_initial_value(FlyString const& name) const
{ {
auto maybe_custom_property = m_registered_custom_properties.get(name); auto maybe_custom_property = m_registered_custom_properties.get(name);

View file

@ -45,6 +45,7 @@
#include <LibWeb/ViewTransition/ViewTransition.h> #include <LibWeb/ViewTransition/ViewTransition.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebIDL/ObservableArray.h> #include <LibWeb/WebIDL/ObservableArray.h>
#include <LibWeb/XPath/XPath.h>
namespace Web::DOM { namespace Web::DOM {
@ -682,6 +683,10 @@ public:
WebIDL::ExceptionOr<bool> query_command_supported(FlyString const& command); WebIDL::ExceptionOr<bool> query_command_supported(FlyString const& command);
WebIDL::ExceptionOr<String> query_command_value(FlyString const& command); WebIDL::ExceptionOr<String> query_command_value(FlyString const& command);
WebIDL::ExceptionOr<GC::Ref<XPath::XPathExpression>> create_expression(String const& expression, GC::Ptr<XPath::XPathNSResolver> resolver = nullptr);
WebIDL::ExceptionOr<GC::Ref<XPath::XPathResult>> evaluate(String const& expression, const DOM::Node& context_node, GC::Ptr<XPath::XPathNSResolver> resolver = nullptr, WebIDL::UnsignedShort type = 0, GC::Ptr<XPath::XPathResult> result = nullptr);
GC::Ref<DOM::Node> create_ns_resolver(GC::Ref<DOM::Node> node_resolver); // legacy
// https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event // https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
bool has_scheduled_selectionchange_event() const { return m_has_scheduled_selectionchange_event; } bool has_scheduled_selectionchange_event() const { return m_has_scheduled_selectionchange_event; }
void set_scheduled_selectionchange_event(bool value) { m_has_scheduled_selectionchange_event = value; } void set_scheduled_selectionchange_event(bool value) { m_has_scheduled_selectionchange_event = value; }

View file

@ -27,6 +27,8 @@
#import <HTML/Location.idl> #import <HTML/Location.idl>
#import <Selection/Selection.idl> #import <Selection/Selection.idl>
#import <ViewTransition/ViewTransition.idl> #import <ViewTransition/ViewTransition.idl>
#import <XPath/XPathResult.idl>
#import <XPath/XPathExpression.idl>
// https://dom.spec.whatwg.org/#document // https://dom.spec.whatwg.org/#document
// https://html.spec.whatwg.org/multipage/dom.html#the-document-object // https://html.spec.whatwg.org/multipage/dom.html#the-document-object
@ -153,6 +155,11 @@ interface Document : Node {
boolean queryCommandSupported(DOMString commandId); boolean queryCommandSupported(DOMString commandId);
DOMString queryCommandValue(DOMString commandId); DOMString queryCommandValue(DOMString commandId);
// XPath
[NewObject] XPathExpression createExpression(DOMString expression, optional XPathNSResolver? resolver = null);
XPathResult evaluate(DOMString expression, Node contextNode, optional XPathNSResolver? resolver = null, optional unsigned short type = 0, optional XPathResult? result = null);
Node createNSResolver(Node nodeResolver); // legacy
// special event handler IDL attributes that only apply to Document objects // special event handler IDL attributes that only apply to Document objects
[LegacyLenientThis] attribute EventHandler onreadystatechange; [LegacyLenientThis] attribute EventHandler onreadystatechange;
attribute EventHandler onvisibilitychange; attribute EventHandler onvisibilitychange;