SubsectionNode.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SubsectionNode.h"
  7. #include "PageNode.h"
  8. #include <AK/TypeCasts.h>
  9. namespace Manual {
  10. SubsectionNode::SubsectionNode(NonnullRefPtr<SectionNode const> parent, StringView name)
  11. : SectionNode(name, name)
  12. , m_parent(move(parent))
  13. {
  14. }
  15. Node const* SubsectionNode::parent() const { return m_parent; }
  16. PageNode const* SubsectionNode::document() const
  17. {
  18. auto maybe_siblings = parent()->children();
  19. if (maybe_siblings.is_error())
  20. return nullptr;
  21. auto siblings = maybe_siblings.release_value();
  22. for (auto const& sibling : siblings) {
  23. if (&*sibling == this)
  24. continue;
  25. auto sibling_name = sibling->name();
  26. if (sibling_name.is_error())
  27. continue;
  28. if (sibling_name.value() == m_name && is<PageNode>(*sibling))
  29. return static_cast<PageNode const*>(&*sibling);
  30. }
  31. return nullptr;
  32. }
  33. ErrorOr<String> SubsectionNode::name() const { return m_name; }
  34. ErrorOr<String> SubsectionNode::path() const
  35. {
  36. return String::formatted("{}/{}", TRY(m_parent->path()), m_section);
  37. }
  38. }