Range.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <LibWeb/Bindings/WindowObject.h>
  29. #include <LibWeb/Bindings/Wrappable.h>
  30. #include <LibWeb/DOM/Node.h>
  31. namespace Web::DOM {
  32. class Range final
  33. : public RefCounted<Range>
  34. , public Bindings::Wrappable {
  35. public:
  36. using WrapperType = Bindings::RangeWrapper;
  37. static NonnullRefPtr<Range> create(Document&);
  38. static NonnullRefPtr<Range> create(Window&);
  39. static NonnullRefPtr<Range> create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
  40. static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject&);
  41. // FIXME: There are a ton of methods missing here.
  42. Node* start_container() { return m_start_container; }
  43. const Node* start_container() const { return m_start_container; }
  44. unsigned start_offset() const { return m_start_offset; }
  45. Node* end_container() { return m_end_container; }
  46. const Node* end_container() const { return m_end_container; }
  47. unsigned end_offset() const { return m_end_offset; }
  48. bool collapsed() const
  49. {
  50. return start_container() == end_container() && start_offset() == end_offset();
  51. }
  52. void set_start(Node& container, unsigned offset)
  53. {
  54. m_start_container = container;
  55. m_start_offset = offset;
  56. }
  57. void set_end(Node& container, unsigned offset)
  58. {
  59. m_end_container = container;
  60. m_end_offset = offset;
  61. }
  62. NonnullRefPtr<Range> inverted() const;
  63. NonnullRefPtr<Range> normalized() const;
  64. NonnullRefPtr<Range> clone_range() const;
  65. private:
  66. explicit Range(Document&);
  67. Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
  68. NonnullRefPtr<Node> m_start_container;
  69. unsigned m_start_offset;
  70. NonnullRefPtr<Node> m_end_container;
  71. unsigned m_end_offset;
  72. };
  73. }