/* * Copyright (c) 2020, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Web::DOM { NonnullRefPtr Range::create(Window& window) { return Range::create(window.associated_document()); } NonnullRefPtr Range::create(Document& document) { return adopt_ref(*new Range(document)); } NonnullRefPtr Range::create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset) { return adopt_ref(*new Range(start_container, start_offset, end_container, end_offset)); } NonnullRefPtr Range::create_with_global_object(Bindings::WindowObject& window) { return Range::create(window.impl()); } Range::Range(Document& document) : Range(document, 0, document, 0) { } Range::Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset) : m_start_container(start_container) , m_start_offset(start_offset) , m_end_container(end_container) , m_end_offset(end_offset) { } NonnullRefPtr Range::clone_range() const { return adopt_ref(*new Range(const_cast(*m_start_container), m_start_offset, const_cast(*m_end_container), m_end_offset)); } NonnullRefPtr Range::inverted() const { return adopt_ref(*new Range(const_cast(*m_end_container), m_end_offset, const_cast(*m_start_container), m_start_offset)); } NonnullRefPtr Range::normalized() const { if (m_start_container.ptr() == m_end_container.ptr()) { if (m_start_offset <= m_end_offset) return clone_range(); return inverted(); } if (m_start_container->is_before(m_end_container)) return clone_range(); return inverted(); } }