1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:37:45 +00:00

LibWeb: Abstract Range's members into AbstractRange

Range's member variables are stored in AbstractRange as per the spec,
as they are also shared with StaticRange.
This commit is contained in:
Luke Wilde 2022-01-30 23:35:51 +00:00 committed by Andreas Kling
parent ed76b4238c
commit a2acda5669
8 changed files with 86 additions and 42 deletions

View file

@ -12,6 +12,8 @@
#include <LibWeb/Bindings/AbortControllerPrototype.h>
#include <LibWeb/Bindings/AbortSignalConstructor.h>
#include <LibWeb/Bindings/AbortSignalPrototype.h>
#include <LibWeb/Bindings/AbstractRangeConstructor.h>
#include <LibWeb/Bindings/AbstractRangePrototype.h>
#include <LibWeb/Bindings/AudioConstructor.h>
#include <LibWeb/Bindings/CSSRuleConstructor.h>
#include <LibWeb/Bindings/CSSRuleListConstructor.h>
@ -326,6 +328,7 @@
auto& vm = this->vm(); \
ADD_WINDOW_OBJECT_INTERFACE(AbortController) \
ADD_WINDOW_OBJECT_INTERFACE(AbortSignal) \
ADD_WINDOW_OBJECT_INTERFACE(AbstractRange) \
ADD_WINDOW_OBJECT_INTERFACE(Crypto) \
ADD_WINDOW_OBJECT_INTERFACE(CSSRule) \
ADD_WINDOW_OBJECT_INTERFACE(CSSRuleList) \

View file

@ -411,6 +411,7 @@ libweb_js_wrapper(CSS/MediaQueryListEvent)
libweb_js_wrapper(CSS/Screen)
libweb_js_wrapper(CSS/StyleSheet)
libweb_js_wrapper(CSS/StyleSheetList)
libweb_js_wrapper(DOM/AbstractRange)
libweb_js_wrapper(DOM/Attribute)
libweb_js_wrapper(DOM/AbortController)
libweb_js_wrapper(DOM/AbortSignal)

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <LibWeb/Bindings/Wrappable.h>
namespace Web::DOM {
class AbstractRange
: public RefCounted<AbstractRange>
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::AbstractRangeWrapper;
virtual ~AbstractRange() override = default;
Node* start_container() { return m_start_container; }
const Node* start_container() const { return m_start_container; }
unsigned start_offset() const { return m_start_offset; }
Node* end_container() { return m_end_container; }
const Node* end_container() const { return m_end_container; }
unsigned end_offset() const { return m_end_offset; }
// https://dom.spec.whatwg.org/#range-collapsed
bool collapsed() const
{
// A range is collapsed if its start node is its end node and its start offset is its end offset.
return start_container() == end_container() && start_offset() == end_offset();
}
protected:
AbstractRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
: m_start_container(start_container)
, m_start_offset(start_offset)
, m_end_container(end_container)
, m_end_offset(end_offset)
{
}
NonnullRefPtr<Node> m_start_container;
u32 m_start_offset;
NonnullRefPtr<Node> m_end_container;
u32 m_end_offset;
};
}

View file

@ -0,0 +1,8 @@
[Exposed=Window]
interface AbstractRange {
readonly attribute Node startContainer;
readonly attribute unsigned long startOffset;
readonly attribute Node endContainer;
readonly attribute unsigned long endOffset;
readonly attribute boolean collapsed;
};

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -21,10 +22,11 @@ NonnullRefPtr<Range> Range::create(Document& document)
return adopt_ref(*new Range(document));
}
NonnullRefPtr<Range> Range::create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
NonnullRefPtr<Range> Range::create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
{
return adopt_ref(*new Range(start_container, start_offset, end_container, end_offset));
}
NonnullRefPtr<Range> Range::create_with_global_object(Bindings::WindowObject& window)
{
return Range::create(window.impl());
@ -35,11 +37,12 @@ Range::Range(Document& document)
{
}
Range::Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
: m_start_container(start_container)
, m_start_offset(start_offset)
, m_end_container(end_container)
, m_end_offset(end_offset)
Range::Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
: AbstractRange(start_container, start_offset, end_container, end_offset)
{
}
Range::~Range()
{
}

View file

@ -1,44 +1,29 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/AbstractRange.h>
namespace Web::DOM {
class Range final
: public RefCounted<Range>
, public Bindings::Wrappable {
class Range final : public AbstractRange {
public:
using WrapperType = Bindings::RangeWrapper;
virtual ~Range() override;
static NonnullRefPtr<Range> create(Document&);
static NonnullRefPtr<Range> create(Window&);
static NonnullRefPtr<Range> create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
static NonnullRefPtr<Range> create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject&);
// FIXME: There are a ton of methods missing here.
Node* start_container() { return m_start_container; }
const Node* start_container() const { return m_start_container; }
unsigned start_offset() const { return m_start_offset; }
Node* end_container() { return m_end_container; }
const Node* end_container() const { return m_end_container; }
unsigned end_offset() const { return m_end_offset; }
bool collapsed() const
{
return start_container() == end_container() && start_offset() == end_offset();
}
void set_start(Node& container, unsigned offset)
{
m_start_container = container;
@ -60,13 +45,7 @@ public:
private:
explicit Range(Document&);
Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
NonnullRefPtr<Node> m_start_container;
unsigned m_start_offset;
NonnullRefPtr<Node> m_end_container;
unsigned m_end_offset;
Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
};
}

View file

@ -1,18 +1,13 @@
#import <DOM/Node.idl>
#import <DOM/AbstractRange.idl>
interface Range {
[Exposed=Window]
interface Range : AbstractRange {
constructor();
readonly attribute boolean collapsed;
readonly attribute Node commonAncestorContainer;
readonly attribute Node startContainer;
readonly attribute unsigned long startOffset;
readonly attribute Node endContainer;
readonly attribute unsigned long endOffset;
undefined setStart(Node node, unsigned long offset);
undefined setEnd(Node node, unsigned long offset);

View file

@ -86,6 +86,7 @@ class UnsetStyleValue;
}
namespace Web::DOM {
class AbstractRange;
class AbortController;
class AbortSignal;
class Attribute;
@ -347,6 +348,7 @@ class URLSearchParamsIterator;
}
namespace Web::Bindings {
class AbstractRangeWrapper;
class AbortControllerWrapper;
class AbortSignalWrapper;
class AttributeWrapper;