LayoutPosition.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Position.h>
  7. #include <LibWeb/DOM/Range.h>
  8. #include <LibWeb/Layout/LayoutPosition.h>
  9. namespace Web::Layout {
  10. DOM::Position LayoutPosition::to_dom_position() const
  11. {
  12. if (!layout_node)
  13. return {};
  14. // FIXME: Verify that there are no shenanigans going on.
  15. return { const_cast<DOM::Node&>(*layout_node->dom_node()), (unsigned)index_in_node };
  16. }
  17. LayoutRange LayoutRange::normalized() const
  18. {
  19. if (!is_valid())
  20. return {};
  21. if (m_start.layout_node == m_end.layout_node) {
  22. if (m_start.index_in_node < m_end.index_in_node)
  23. return *this;
  24. return { m_end, m_start };
  25. }
  26. if (m_start.layout_node->is_before(*m_end.layout_node))
  27. return *this;
  28. return { m_end, m_start };
  29. }
  30. NonnullRefPtr<DOM::Range> LayoutRange::to_dom_range() const
  31. {
  32. VERIFY(is_valid());
  33. auto start = m_start.to_dom_position();
  34. auto end = m_end.to_dom_position();
  35. return DOM::Range::create(*start.node(), start.offset(), *end.node(), end.offset());
  36. }
  37. }