Range.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/Document.h>
  27. #include <LibWeb/DOM/Node.h>
  28. #include <LibWeb/DOM/Range.h>
  29. #include <LibWeb/DOM/Window.h>
  30. namespace Web::DOM {
  31. NonnullRefPtr<Range> Range::create(Window& window)
  32. {
  33. return Range::create(window.document());
  34. }
  35. NonnullRefPtr<Range> Range::create(Document& document)
  36. {
  37. return adopt(*new Range(document));
  38. }
  39. NonnullRefPtr<Range> Range::create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
  40. {
  41. return adopt(*new Range(start_container, start_offset, end_container, end_offset));
  42. }
  43. NonnullRefPtr<Range> Range::create_with_global_object(Bindings::WindowObject& window)
  44. {
  45. return Range::create(window.impl());
  46. }
  47. Range::Range(Document& document)
  48. : Range(document, 0, document, 0)
  49. {
  50. }
  51. Range::Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset)
  52. : m_start_container(start_container)
  53. , m_start_offset(start_offset)
  54. , m_end_container(end_container)
  55. , m_end_offset(end_offset)
  56. {
  57. }
  58. NonnullRefPtr<Range> Range::clone_range() const
  59. {
  60. return adopt(*new Range(const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset));
  61. }
  62. NonnullRefPtr<Range> Range::inverted() const
  63. {
  64. return adopt(*new Range(const_cast<Node&>(*m_end_container), m_end_offset, const_cast<Node&>(*m_start_container), m_start_offset));
  65. }
  66. NonnullRefPtr<Range> Range::normalized() const
  67. {
  68. if (m_start_container.ptr() == m_end_container.ptr()) {
  69. if (m_start_offset <= m_end_offset)
  70. return clone_range();
  71. return inverted();
  72. }
  73. if (m_start_container->is_before(m_end_container))
  74. return clone_range();
  75. return inverted();
  76. }
  77. }