Skip to content

Commit

Permalink
LibWeb: Implement Range.commonAncestorContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Feb 25, 2022
1 parent aec0e54 commit c25d653
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,20 @@ NonnullRefPtr<Range> Range::normalized() const
return inverted();
}

// https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer
NonnullRefPtr<Node> 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;
}

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Range final
NonnullRefPtr<Range> normalized() const;
NonnullRefPtr<Range> clone_range() const;

NonnullRefPtr<Node> common_ancestor_container() const;

private:
explicit Range(Document&);

Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Range.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c25d653

Please sign in to comment.