diff options
author | Andreas Kling <kling@serenityos.org> | 2021-02-21 23:41:54 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-21 23:48:01 +0100 |
commit | 9194a97cbebc1d6e7f3af1ecbbfc1ca93ffd66a2 (patch) | |
tree | 81726c81f8235dc0c07f7aa14b39b7a633f6a083 /Userland/Libraries/LibWeb/DOM | |
parent | ff018607a1654158167ba4fea72c083714387dd0 (diff) | |
download | serenity-9194a97cbebc1d6e7f3af1ecbbfc1ca93ffd66a2.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.idl | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.h | 27 |
5 files changed, 40 insertions, 21 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 81564d29df..99e71a9af7 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -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; diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 63264eae05..eb6e9cfeb3 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -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; } diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl index a25d882c7a..824949e323 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -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; diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp index 377eb7f882..2196f76bd2 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.cpp +++ b/Userland/Libraries/LibWeb/DOM/Range.cpp @@ -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) { } diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h index 6be643b906..d9363d87e5 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.h +++ b/Userland/Libraries/LibWeb/DOM/Range.h @@ -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; |