LibWeb: Add Document.createRange()

Also tidy up DOM::Range a little bit while we're here, and unify the
way we create them to use a delegating constructors.
This commit is contained in:
Andreas Kling 2021-02-21 23:41:54 +01:00
parent ff018607a1
commit 9194a97cbe
6 changed files with 42 additions and 21 deletions

View file

@ -1093,10 +1093,12 @@ void generate_prototype_implementation(const IDL::Interface& interface)
#include <LibWeb/Bindings/ImageDataWrapper.h>
#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/Bindings/PerformanceTimingWrapper.h>
#include <LibWeb/Bindings/RangeWrapper.h>
#include <LibWeb/Bindings/TextWrapper.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/HTMLElement.h>

View file

@ -42,6 +42,7 @@
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/Dump.h>
@ -587,6 +588,11 @@ NonnullRefPtr<Comment> Document::create_comment(const String& data)
return adopt(*new Comment(*this, data));
}
NonnullRefPtr<Range> Document::create_range()
{
return Range::create(*this);
}
void Document::set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement* script)
{
m_pending_parsing_blocking_script = script;

View file

@ -162,6 +162,7 @@ public:
NonnullRefPtr<DocumentFragment> create_document_fragment();
NonnullRefPtr<Text> create_text_node(const String& data);
NonnullRefPtr<Comment> create_comment(const String& data);
NonnullRefPtr<Range> create_range();
void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }

View file

@ -27,6 +27,7 @@ interface Document : Node {
DocumentFragment createDocumentFragment();
Text createTextNode(DOMString data);
Comment createComment(DOMString data);
Range createRange();
readonly attribute DOMString compatMode;
readonly attribute DocumentType? doctype;

View file

@ -31,11 +31,27 @@
namespace Web::DOM {
Range::Range(Window& window)
: m_start_container(window.document())
, m_start_offset(0)
, m_end_container(window.document())
, m_end_offset(0)
NonnullRefPtr<Range> Range::create(Window& window)
{
return Range::create(window.document());
}
NonnullRefPtr<Range> Range::create(Document& document)
{
return adopt(*new Range(document));
}
NonnullRefPtr<Range> Range::create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
{
return adopt(*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());
}
Range::Range(Document& document)
: Range(document, 0, document, 0)
{
}

View file

@ -39,28 +39,22 @@ class Range final
public:
using WrapperType = Bindings::RangeWrapper;
static NonnullRefPtr<Range> create(Window& window)
{
return adopt(*new Range(window));
}
static NonnullRefPtr<Range> create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
{
return adopt(*new Range(start_container, start_offset, end_container, end_offset));
}
static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject& window)
{
return Range::create(window.impl());
}
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_with_global_object(Bindings::WindowObject&);
// FIXME: There are a ton of methods missing here.
Node* start_container() { return m_start_container; }
unsigned start_offset() { return m_start_offset; }
const Node* start_container() const { return m_start_container; }
unsigned start_offset() const { return m_start_offset; }
Node* end_container() { return m_end_container; }
unsigned end_offset() { return m_end_offset; }
const Node* end_container() const { return m_end_container; }
unsigned end_offset() const { return m_end_offset; }
bool collapsed()
bool collapsed() const
{
return start_container() == end_container() && start_offset() == end_offset();
}
@ -82,7 +76,8 @@ public:
NonnullRefPtr<Range> clone_range() const;
private:
explicit Range(Window&);
explicit Range(Document&);
Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
NonnullRefPtr<Node> m_start_container;