OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // https://discourse.wicg.io/t/proposal-staticrange-to-be-used-instead-of-range-
for-new-apis/1472 | 5 // https://discourse.wicg.io/t/proposal-staticrange-to-be-used-instead-of-range-
for-new-apis/1472 |
6 | 6 |
7 #ifndef StaticRange_h | 7 #ifndef StaticRange_h |
8 #define StaticRange_h | 8 #define StaticRange_h |
9 | 9 |
10 #include "bindings/core/v8/ScriptWrappable.h" | 10 #include "bindings/core/v8/ScriptWrappable.h" |
11 #include "core/CoreExport.h" | 11 #include "core/CoreExport.h" |
| 12 #include "core/dom/Range.h" |
12 #include "platform/heap/Handle.h" | 13 #include "platform/heap/Handle.h" |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 class Document; | 17 class Document; |
17 class ExceptionState; | 18 class ExceptionState; |
18 class Range; | |
19 | 19 |
20 class CORE_EXPORT StaticRange final : public GarbageCollected<StaticRange>, | 20 class CORE_EXPORT StaticRange final : public GarbageCollected<StaticRange>, |
21 public ScriptWrappable { | 21 public ScriptWrappable { |
22 DEFINE_WRAPPERTYPEINFO(); | 22 DEFINE_WRAPPERTYPEINFO(); |
23 | 23 |
24 public: | 24 public: |
25 static StaticRange* create(Document& document) { | 25 static StaticRange* create(Document& document) { |
26 return new StaticRange(document); | 26 return new StaticRange(document); |
27 } | 27 } |
28 static StaticRange* create(Document& document, | 28 static StaticRange* create(Document& document, |
29 Node* startContainer, | 29 Node* startContainer, |
30 int startOffset, | 30 int startOffset, |
31 Node* endContainer, | 31 Node* endContainer, |
32 int endOffset) { | 32 int endOffset) { |
33 return new StaticRange(document, startContainer, startOffset, endContainer, | 33 return new StaticRange(document, startContainer, startOffset, endContainer, |
34 endOffset); | 34 endOffset); |
35 } | 35 } |
| 36 static StaticRange* create(const Range* range) { |
| 37 return new StaticRange(range->ownerDocument(), range->startContainer(), |
| 38 range->startOffset(), range->endContainer(), |
| 39 range->endOffset()); |
| 40 } |
36 | 41 |
37 Node* startContainer() const { return m_startContainer.get(); } | 42 Node* startContainer() const { return m_startContainer.get(); } |
38 void setStartContainer(Node* startContainer) { | 43 void setStartContainer(Node* startContainer) { |
39 m_startContainer = startContainer; | 44 m_startContainer = startContainer; |
40 } | 45 } |
41 | 46 |
42 int startOffset() const { return m_startOffset; } | 47 int startOffset() const { return m_startOffset; } |
43 void setStartOffset(int startOffset) { m_startOffset = startOffset; } | 48 void setStartOffset(int startOffset) { m_startOffset = startOffset; } |
44 | 49 |
45 Node* endContainer() const { return m_endContainer.get(); } | 50 Node* endContainer() const { return m_endContainer.get(); } |
46 void setEndContainer(Node* endContainer) { m_endContainer = endContainer; } | 51 void setEndContainer(Node* endContainer) { m_endContainer = endContainer; } |
47 | 52 |
48 int endOffset() const { return m_endOffset; } | 53 int endOffset() const { return m_endOffset; } |
49 void setEndOffset(int endOffset) { m_endOffset = endOffset; } | 54 void setEndOffset(int endOffset) { m_endOffset = endOffset; } |
50 | 55 |
51 bool collapsed() const { | 56 bool collapsed() const { |
52 return m_startContainer == m_endContainer && m_startOffset == m_endOffset; | 57 return m_startContainer == m_endContainer && m_startOffset == m_endOffset; |
53 } | 58 } |
54 | 59 |
55 void setStart(Node* container, int offset); | 60 void setStart(Node* container, int offset); |
56 void setEnd(Node* container, int offset); | 61 void setEnd(Node* container, int offset); |
57 | 62 |
58 Range* toRange(ExceptionState&) const; | 63 Range* toRange(ExceptionState& = ASSERT_NO_EXCEPTION) const; |
59 | 64 |
60 DECLARE_TRACE(); | 65 DECLARE_TRACE(); |
61 | 66 |
62 private: | 67 private: |
63 explicit StaticRange(Document&); | 68 explicit StaticRange(Document&); |
64 StaticRange(Document&, | 69 StaticRange(Document&, |
65 Node* startContainer, | 70 Node* startContainer, |
66 int startOffset, | 71 int startOffset, |
67 Node* endContainer, | 72 Node* endContainer, |
68 int endOffset); | 73 int endOffset); |
69 | 74 |
70 Member<Document> m_ownerDocument; // Required by |toRange()|. | 75 Member<Document> m_ownerDocument; // Required by |toRange()|. |
71 Member<Node> m_startContainer; | 76 Member<Node> m_startContainer; |
72 int m_startOffset; | 77 int m_startOffset; |
73 Member<Node> m_endContainer; | 78 Member<Node> m_endContainer; |
74 int m_endOffset; | 79 int m_endOffset; |
75 }; | 80 }; |
76 | 81 |
77 using StaticRangeVector = HeapVector<Member<StaticRange>>; | 82 using StaticRangeVector = HeapVector<Member<StaticRange>>; |
78 | 83 |
79 } // namespace blink | 84 } // namespace blink |
80 | 85 |
81 #endif // StaticRange_h | 86 #endif // StaticRange_h |
OLD | NEW |