summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Layout/LayoutPosition.h
blob: 7f5d221139c64d80edbf0ec3a3c918e12dacda75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/RefPtr.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Layout/Node.h>

namespace Web::Layout {

class Node;

struct LayoutPosition {
    RefPtr<Node> layout_node;
    int index_in_node { 0 };

    DOM::Position to_dom_position() const;
};

class LayoutRange {
public:
    LayoutRange() = default;
    LayoutRange(const LayoutPosition& start, const LayoutPosition& end)
        : m_start(start)
        , m_end(end)
    {
    }

    bool is_valid() const { return m_start.layout_node && m_end.layout_node; }

    void set(const LayoutPosition& start, const LayoutPosition& end)
    {
        m_start = start;
        m_end = end;
    }

    void set_start(const LayoutPosition& start) { m_start = start; }
    void set_end(const LayoutPosition& end) { m_end = end; }

    const LayoutPosition& start() const { return m_start; }
    LayoutPosition& start() { return m_start; }
    const LayoutPosition& end() const { return m_end; }
    LayoutPosition& end() { return m_end; }

    LayoutRange normalized() const;

    NonnullRefPtr<DOM::Range> to_dom_range() const;

private:
    LayoutPosition m_start;
    LayoutPosition m_end;
};

}