Skip to content

Commit f04b866

Browse files
johannesggmta
authored andcommitted
LibWeb: Implement stubs for XPathEvaluator
1 parent 1ae7ecc commit f04b866

File tree

17 files changed

+441
-1
lines changed

17 files changed

+441
-1
lines changed

Libraries/LibWeb/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,10 @@ set(SOURCES
10661066
XHR/XMLHttpRequestUpload.cpp
10671067
XLink/AttributeNames.cpp
10681068
XML/XMLDocumentBuilder.cpp
1069+
XPath/XPathEvaluator.cpp
1070+
XPath/XPathExpression.cpp
1071+
XPath/XPathNSResolver.cpp
1072+
XPath/XPathResult.cpp
10691073
)
10701074

10711075
compile_ipc(Worker/WebWorkerClient.ipc Worker/WebWorkerClientEndpoint.h)

Libraries/LibWeb/Forward.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,3 +1292,12 @@ class TrustedTypePolicyFactory;
12921292
struct TrustedTypePolicyOptions;
12931293

12941294
}
1295+
1296+
namespace Web::XPath {
1297+
1298+
class XPathEvaluator;
1299+
class XPathExpression;
1300+
class XPathNSResolver;
1301+
class XPathResult;
1302+
1303+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/Realm.h>
8+
#include <LibWeb/Bindings/Intrinsics.h>
9+
#include <LibWeb/Bindings/PlatformObject.h>
10+
#include <LibWeb/Bindings/XPathEvaluatorPrototype.h>
11+
#include <LibWeb/WebIDL/ExceptionOr.h>
12+
13+
#include "XPathEvaluator.h"
14+
#include "XPathExpression.h"
15+
#include "XPathResult.h"
16+
17+
namespace Web::XPath {
18+
19+
GC_DEFINE_ALLOCATOR(XPathEvaluator);
20+
21+
XPathEvaluator::XPathEvaluator(JS::Realm& realm)
22+
: Web::Bindings::PlatformObject(realm)
23+
{
24+
}
25+
26+
XPathEvaluator::~XPathEvaluator() = default;
27+
28+
WebIDL::ExceptionOr<GC::Ref<XPathEvaluator>> XPathEvaluator::construct_impl(JS::Realm& realm)
29+
{
30+
return realm.create<XPathEvaluator>(realm);
31+
}
32+
33+
void XPathEvaluator::initialize(JS::Realm& realm)
34+
{
35+
WEB_SET_PROTOTYPE_FOR_INTERFACE(XPathEvaluator);
36+
Base::initialize(realm);
37+
}
38+
39+
WebIDL::ExceptionOr<GC::Ref<XPathExpression>> XPathEvaluator::create_expression(String const& expression, GC::Ptr<XPathNSResolver> resolver)
40+
{
41+
auto& realm = this->realm();
42+
return realm.create<XPathExpression>(realm, expression, resolver);
43+
}
44+
45+
WebIDL::ExceptionOr<GC::Ref<XPathResult>> XPathEvaluator::evaluate(String const&, DOM::Node const&, GC::Ptr<XPathNSResolver>, WebIDL::UnsignedShort, GC::Ptr<XPathResult>)
46+
{
47+
auto& realm = this->realm();
48+
return realm.create<XPathResult>(realm);
49+
}
50+
51+
GC::Ref<DOM::Node> XPathEvaluator::create_ns_resolver(GC::Ref<DOM::Node> node_resolver)
52+
{
53+
return node_resolver;
54+
}
55+
56+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/Bindings/PlatformObject.h>
10+
#include <LibWeb/Forward.h>
11+
#include <LibWeb/WebIDL/Types.h>
12+
13+
#include "XPathExpression.h"
14+
#include "XPathNSResolver.h"
15+
#include "XPathResult.h"
16+
17+
namespace Web::XPath {
18+
19+
class XPathEvaluator : public Bindings::PlatformObject {
20+
WEB_PLATFORM_OBJECT(XPathEvaluator, Bindings::PlatformObject);
21+
GC_DECLARE_ALLOCATOR(XPathEvaluator);
22+
23+
explicit XPathEvaluator(JS::Realm&);
24+
virtual ~XPathEvaluator() override;
25+
26+
public:
27+
static WebIDL::ExceptionOr<GC::Ref<XPathEvaluator>> construct_impl(JS::Realm&);
28+
virtual void initialize(JS::Realm&) override;
29+
30+
WebIDL::ExceptionOr<GC::Ref<XPathExpression>> create_expression(String const& expression, GC::Ptr<XPathNSResolver> resolver = nullptr);
31+
WebIDL::ExceptionOr<GC::Ref<XPathResult>> evaluate(String const& expression, DOM::Node const& context_node, GC::Ptr<XPathNSResolver> resolver = nullptr, WebIDL::UnsignedShort type = 0, GC::Ptr<XPathResult> result = nullptr);
32+
static GC::Ref<DOM::Node> create_ns_resolver(GC::Ref<DOM::Node> node_resolver); // legacy
33+
};
34+
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#import <DOM/Node.idl>
2+
3+
// FIXME: callback interfaces are not currently supported
4+
// callback interface XPathNSResolver {
5+
// DOMString? lookupNamespaceURI(DOMString? prefix);
6+
// };
7+
8+
// https://dom.spec.whatwg.org/#mixin-xpathevaluatorbase
9+
interface mixin XPathEvaluatorBase {
10+
[NewObject] XPathExpression createExpression(DOMString expression, optional XPathNSResolver? resolver = null);
11+
Node createNSResolver(Node nodeResolver); // legacy
12+
// XPathResult.ANY_TYPE = 0
13+
XPathResult evaluate(DOMString expression, Node contextNode, optional XPathNSResolver? resolver = null, optional unsigned short type = 0, optional XPathResult? result = null);
14+
};
15+
Document includes XPathEvaluatorBase;
16+
17+
// https://dom.spec.whatwg.org/#interface-xpathevaluator
18+
[Exposed=Window]
19+
interface XPathEvaluator {
20+
constructor();
21+
};
22+
23+
XPathEvaluator includes XPathEvaluatorBase;
24+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/Realm.h>
8+
#include <LibWeb/Bindings/Intrinsics.h>
9+
#include <LibWeb/Bindings/PlatformObject.h>
10+
#include <LibWeb/Bindings/XPathExpressionPrototype.h>
11+
#include <LibWeb/DOM/Node.h>
12+
#include <LibWeb/Forward.h>
13+
14+
#include "XPathEvaluator.h"
15+
#include "XPathExpression.h"
16+
#include "XPathResult.h"
17+
18+
namespace Web::XPath {
19+
20+
GC_DEFINE_ALLOCATOR(XPathExpression);
21+
22+
XPathExpression::XPathExpression(JS::Realm& realm, String const& expression, GC::Ptr<XPathNSResolver> resolver)
23+
: Web::Bindings::PlatformObject(realm)
24+
, m_expression(expression)
25+
, m_resolver(resolver)
26+
{
27+
}
28+
29+
void XPathExpression::initialize(JS::Realm& realm)
30+
{
31+
WEB_SET_PROTOTYPE_FOR_INTERFACE(XPathExpression);
32+
Base::initialize(realm);
33+
}
34+
35+
void XPathExpression::visit_edges(Cell::Visitor& visitor)
36+
{
37+
Base::visit_edges(visitor);
38+
visitor.visit(m_resolver);
39+
}
40+
41+
XPathExpression::~XPathExpression() = default;
42+
43+
WebIDL::ExceptionOr<GC::Ref<XPathResult>> XPathExpression::evaluate(DOM::Node const&, WebIDL::UnsignedShort, GC::Ptr<XPathResult>)
44+
{
45+
auto& realm = this->realm();
46+
return realm.create<XPathResult>(realm);
47+
}
48+
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/Bindings/PlatformObject.h>
10+
#include <LibWeb/Forward.h>
11+
#include <LibWeb/WebIDL/Types.h>
12+
13+
#include "XPathResult.h"
14+
15+
namespace Web::XPath {
16+
17+
class XPathExpression final : public Bindings::PlatformObject {
18+
WEB_PLATFORM_OBJECT(XPathExpression, Bindings::PlatformObject);
19+
GC_DECLARE_ALLOCATOR(XPathExpression);
20+
21+
public:
22+
explicit XPathExpression(JS::Realm&, String const& expression, GC::Ptr<XPathNSResolver> resolver);
23+
virtual ~XPathExpression() override;
24+
virtual void visit_edges(Cell::Visitor&) override;
25+
virtual void initialize(JS::Realm&) override;
26+
27+
WebIDL::ExceptionOr<GC::Ref<XPathResult>> evaluate(DOM::Node const& context_node, WebIDL::UnsignedShort type = 0, GC::Ptr<XPathResult> result = nullptr);
28+
29+
private:
30+
String m_expression;
31+
GC::Ptr<XPathNSResolver> m_resolver;
32+
};
33+
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <XPath/XPathResult.idl>
2+
3+
// https://dom.spec.whatwg.org/#interface-xpathexpression
4+
[Exposed=Window]
5+
interface XPathExpression {
6+
// XPathResult.ANY_TYPE = 0
7+
XPathResult evaluate(Node contextNode, optional unsigned short type = 0, optional XPathResult? result = null);
8+
};
9+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/Object.h>
8+
#include <LibJS/Runtime/Realm.h>
9+
#include <LibWeb/Bindings/Intrinsics.h>
10+
#include <LibWeb/WebIDL/CallbackType.h>
11+
12+
#include "XPathNSResolver.h"
13+
14+
namespace Web::XPath {
15+
16+
GC_DEFINE_ALLOCATOR(XPathNSResolver);
17+
18+
GC::Ref<XPathNSResolver> XPathNSResolver::create(JS::Realm& realm, GC::Ref<WebIDL::CallbackType> callback)
19+
{
20+
return realm.create<XPathNSResolver>(realm, callback);
21+
}
22+
23+
XPathNSResolver::XPathNSResolver(JS::Realm& realm, GC::Ref<WebIDL::CallbackType> callback)
24+
: JS::Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
25+
, m_callback(callback)
26+
{
27+
}
28+
29+
void XPathNSResolver::visit_edges(Cell::Visitor& visitor)
30+
{
31+
Base::visit_edges(visitor);
32+
visitor.visit(m_callback);
33+
}
34+
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025, Johannes Gustafsson <johannesgu@outlook.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/Bindings/PlatformObject.h>
10+
#include <LibWeb/Forward.h>
11+
12+
#include "XPathResult.h"
13+
14+
namespace Web::XPath {
15+
16+
class XPathNSResolver final : public JS::Object {
17+
JS_OBJECT(XPathNSResolver, JS::Object);
18+
GC_DECLARE_ALLOCATOR(XPathNSResolver);
19+
20+
public:
21+
[[nodiscard]] static GC::Ref<XPathNSResolver> create(JS::Realm&, GC::Ref<WebIDL::CallbackType>);
22+
XPathNSResolver(JS::Realm&, GC::Ref<WebIDL::CallbackType>);
23+
24+
virtual ~XPathNSResolver() = default;
25+
virtual void visit_edges(Cell::Visitor&) override;
26+
27+
private:
28+
GC::Ref<WebIDL::CallbackType> m_callback;
29+
};
30+
31+
}

0 commit comments

Comments
 (0)