Migrate from ints to unsigneds when referring to indices into strings
[WebKit-https.git] / Source / WebCore / rendering / SelectionSubtreeRoot.h
1  /*
2  * Copyright (C) 2014 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef SelectionSubtreeRoot_h
31 #define SelectionSubtreeRoot_h
32
33 #include "RenderObject.h"
34 #include "RenderSelectionInfo.h"
35
36 namespace WebCore {
37
38 class Document;
39
40 class SelectionSubtreeRoot {
41 public:
42     
43     typedef HashMap<RenderObject*, std::unique_ptr<RenderSelectionInfo>> SelectedObjectMap;
44     typedef HashMap<const RenderBlock*, std::unique_ptr<RenderBlockSelectionInfo>> SelectedBlockMap;
45
46     struct OldSelectionData {
47         OldSelectionData()
48         {
49         }
50
51         Optional<unsigned> selectionStartPos;
52         Optional<unsigned> selectionEndPos;
53         SelectedObjectMap selectedObjects;
54         SelectedBlockMap selectedBlocks;
55     };
56
57     class SelectionSubtreeData {
58     public:
59         SelectionSubtreeData() = default;
60
61         SelectionSubtreeData(RenderObject* selectionStart, Optional<unsigned> selectionStartPos, RenderObject* selectionEnd, Optional<unsigned> selectionEndPos)
62             : m_selectionStart(selectionStart)
63             , m_selectionStartPos(selectionStartPos)
64             , m_selectionEnd(selectionEnd)
65             , m_selectionEndPos(selectionEndPos)
66         {
67         }
68
69         RenderObject* selectionStart() const { return m_selectionStart; }
70         Optional<unsigned> selectionStartPos() const { return m_selectionStartPos; }
71         RenderObject* selectionEnd() const { return m_selectionEnd; }
72         Optional<unsigned> selectionEndPos() const { return m_selectionEndPos; }
73         bool selectionClear() const
74         {
75             return !m_selectionStart
76             && (!m_selectionStartPos)
77             && !m_selectionEnd
78             && (!m_selectionEndPos);
79         }
80
81         void selectionStartEndPositions(unsigned& startPos, unsigned& endPos) const
82         {
83             startPos = m_selectionStartPos.value();
84             endPos = m_selectionEndPos.value();
85         }
86         void setSelectionStart(RenderObject* selectionStart) { m_selectionStart = selectionStart; }
87         void setSelectionStartPos(Optional<unsigned> selectionStartPos) { m_selectionStartPos = selectionStartPos;}
88         void setSelectionEnd(RenderObject* selectionEnd) { m_selectionEnd = selectionEnd; }
89         void setSelectionEndPos(Optional<unsigned> selectionEndPos) { m_selectionEndPos = selectionEndPos;}
90         void clearSelection()
91         {
92             m_selectionStart = nullptr;
93             m_selectionStartPos = Nullopt;
94             m_selectionEnd = nullptr;
95             m_selectionEndPos = Nullopt;
96         }
97
98     private:
99         RenderObject* m_selectionStart { nullptr };
100         Optional<unsigned> m_selectionStartPos;
101         RenderObject* m_selectionEnd { nullptr };
102         Optional<unsigned> m_selectionEndPos;
103     };
104
105     typedef HashMap<SelectionSubtreeRoot*, SelectionSubtreeData> RenderSubtreesMap;
106     typedef HashMap<const SelectionSubtreeRoot*, std::unique_ptr<OldSelectionData>> SubtreeOldSelectionDataMap;
107
108     SelectionSubtreeRoot();
109
110     SelectionSubtreeData& selectionData() { return m_selectionSubtreeData; }
111     const SelectionSubtreeData& selectionData() const { return m_selectionSubtreeData; }
112
113     void setSelectionData(const SelectionSubtreeData& selectionSubtreeData) { m_selectionSubtreeData = selectionSubtreeData; }
114     void adjustForVisibleSelection(Document&);
115
116 private:
117     SelectionSubtreeData m_selectionSubtreeData;
118 };
119
120 } // namespace WebCore
121
122 #endif // SelectionSubtreeRoot_h