diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp index 40af4a1444..015fa3adde 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.cpp +++ b/Userland/Libraries/LibWeb/DOM/Range.cpp @@ -68,4 +68,20 @@ NonnullRefPtr Range::normalized() const return inverted(); } +// https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer +NonnullRefPtr Range::common_ancestor_container() const +{ + // 1. Let container be start node. + auto container = m_start_container; + + // 2. While container is not an inclusive ancestor of end node, let container be container’s parent. + while (!container->is_inclusive_ancestor_of(m_end_container)) { + VERIFY(container->parent()); + container = *container->parent(); + } + + // 3. Return container. + return container; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h index 84cf00937c..4d5873ffc4 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.h +++ b/Userland/Libraries/LibWeb/DOM/Range.h @@ -55,6 +55,8 @@ public: NonnullRefPtr normalized() const; NonnullRefPtr clone_range() const; + NonnullRefPtr common_ancestor_container() const; + private: explicit Range(Document&); diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl index 76a10dfd5b..f84e225f8c 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.idl +++ b/Userland/Libraries/LibWeb/DOM/Range.idl @@ -6,6 +6,8 @@ interface Range { readonly attribute boolean collapsed; + readonly attribute Node commonAncestorContainer; + readonly attribute Node startContainer; readonly attribute unsigned long startOffset; readonly attribute Node endContainer;