diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 8121b36cca4..5b257c1b0da 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -1093,10 +1093,12 @@ void generate_prototype_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 81564d29df0..99e71a9af76 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -587,6 +588,11 @@ NonnullRefPtr Document::create_comment(const String& data) return adopt(*new Comment(*this, data)); } +NonnullRefPtr Document::create_range() +{ + return Range::create(*this); +} + void Document::set_pending_parsing_blocking_script(Badge, 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 63264eae05d..eb6e9cfeb30 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -162,6 +162,7 @@ public: NonnullRefPtr create_document_fragment(); NonnullRefPtr create_text_node(const String& data); NonnullRefPtr create_comment(const String& data); + NonnullRefPtr create_range(); void set_pending_parsing_blocking_script(Badge, 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 a25d882c7af..824949e323f 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 377eb7f8822..2196f76bd2c 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::create(Window& window) +{ + return Range::create(window.document()); +} + +NonnullRefPtr Range::create(Document& document) +{ + return adopt(*new Range(document)); +} + +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)); +} +NonnullRefPtr 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 6be643b9061..d9363d87e53 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 create(Window& window) - { - return adopt(*new Range(window)); - } - static NonnullRefPtr 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 create_with_global_object(Bindings::WindowObject& window) - { - return Range::create(window.impl()); - } + static NonnullRefPtr create(Document&); + static NonnullRefPtr create(Window&); + static NonnullRefPtr create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset); + static NonnullRefPtr 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 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 m_start_container;