From e211f6c9254d6d95355ec05b0007dea02140ac96 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 20 Aug 2023 00:34:54 +0200 Subject: [PATCH] LibWeb: Implement getting "ancestor navigables" of a document --- Userland/Libraries/LibWeb/DOM/Document.cpp | 23 ++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Document.h | 1 + 2 files changed, 24 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index de9752e86af..2b0969ef524 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -2561,6 +2561,29 @@ Vector> Document::inclusive_descendant_navigables() return navigables; } +// https://html.spec.whatwg.org/multipage/document-sequences.html#ancestor-navigables +Vector> Document::ancestor_navigables() +{ + // 1. Let navigable be document's node navigable's parent. + VERIFY(navigable()); + auto navigable = this->navigable()->parent(); + + // 2. Let ancestors be an empty list. + Vector> ancestors; + + // 3. While navigable is not null: + while (navigable) { + // 1. Prepend navigable to ancestors. + ancestors.prepend(*navigable); + + // 2. Set navigable to navigable's parent. + navigable = navigable->parent(); + } + + // 4. Return ancestors. + return ancestors; +} + // https://html.spec.whatwg.org/multipage/browsers.html#list-of-the-descendant-browsing-contexts Vector> Document::list_of_descendant_browsing_contexts() const { diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 1872f5dfc66..59c9aa09ee2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -462,6 +462,7 @@ public: Vector> descendant_navigables(); Vector> inclusive_descendant_navigables(); + Vector> ancestor_navigables(); void destroy();