1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/editing/EphemeralRange.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h"
10 #include "core/dom/Range.h"
11 #include "core/dom/Text.h"
15 template <typename Strategy
>
16 EphemeralRangeTemplate
<Strategy
>::EphemeralRangeTemplate(const PositionAlgorithm
<Strategy
>& start
, const PositionAlgorithm
<Strategy
>& end
)
17 : m_startPosition(start
)
20 , m_domTreeVersion(start
.isNull() ? 0 : start
.document()->domTreeVersion())
23 if (m_startPosition
.isNull()) {
24 ASSERT(m_endPosition
.isNull());
27 ASSERT(m_endPosition
.isNotNull());
28 ASSERT(m_startPosition
.document() == m_endPosition
.document());
29 ASSERT(m_startPosition
.inDocument());
30 ASSERT(m_endPosition
.inDocument());
33 template <typename Strategy
>
34 EphemeralRangeTemplate
<Strategy
>::EphemeralRangeTemplate(const EphemeralRangeTemplate
<Strategy
>& other
)
35 : EphemeralRangeTemplate(other
.m_startPosition
, other
.m_endPosition
)
37 ASSERT(other
.isValid());
40 template <typename Strategy
>
41 EphemeralRangeTemplate
<Strategy
>::EphemeralRangeTemplate(const PositionAlgorithm
<Strategy
>& position
)
42 : EphemeralRangeTemplate(position
, position
)
46 template <typename Strategy
>
47 EphemeralRangeTemplate
<Strategy
>::EphemeralRangeTemplate(const Range
* range
)
51 m_startPosition
= fromPositionInDOMTree
<Strategy
>(range
->startPosition());
52 m_endPosition
= fromPositionInDOMTree
<Strategy
>(range
->endPosition());
54 m_domTreeVersion
= range
->ownerDocument().domTreeVersion();
58 template <typename Strategy
>
59 EphemeralRangeTemplate
<Strategy
>::EphemeralRangeTemplate()
63 template <typename Strategy
>
64 EphemeralRangeTemplate
<Strategy
>::~EphemeralRangeTemplate()
68 template <typename Strategy
>
69 EphemeralRangeTemplate
<Strategy
>& EphemeralRangeTemplate
<Strategy
>::operator=(const EphemeralRangeTemplate
<Strategy
>& other
)
71 ASSERT(other
.isValid());
72 m_startPosition
= other
.m_startPosition
;
73 m_endPosition
= other
.m_endPosition
;
75 m_domTreeVersion
= other
.m_domTreeVersion
;
80 template <typename Strategy
>
81 bool EphemeralRangeTemplate
<Strategy
>::operator==(const EphemeralRangeTemplate
<Strategy
>& other
) const
83 return startPosition() == other
.startPosition() && endPosition() == other
.endPosition();
86 template <typename Strategy
>
87 bool EphemeralRangeTemplate
<Strategy
>::operator!=(const EphemeralRangeTemplate
<Strategy
>& other
) const
89 return !operator==(other
);
92 template <typename Strategy
>
93 Document
& EphemeralRangeTemplate
<Strategy
>::document() const
96 return *m_startPosition
.document();
99 template <typename Strategy
>
100 PositionAlgorithm
<Strategy
> EphemeralRangeTemplate
<Strategy
>::startPosition() const
103 return m_startPosition
;
106 template <typename Strategy
>
107 PositionAlgorithm
<Strategy
> EphemeralRangeTemplate
<Strategy
>::endPosition() const
110 return m_endPosition
;
113 template <typename Strategy
>
114 bool EphemeralRangeTemplate
<Strategy
>::isCollapsed() const
117 return m_startPosition
== m_endPosition
;
120 template <typename Strategy
>
121 EphemeralRangeTemplate
<Strategy
> EphemeralRangeTemplate
<Strategy
>::rangeOfContents(const Node
& node
)
123 return EphemeralRangeTemplate
<Strategy
>(PositionAlgorithm
<Strategy
>::firstPositionInNode(&const_cast<Node
&>(node
)), PositionAlgorithm
<Strategy
>::lastPositionInNode(&const_cast<Node
&>(node
)));
127 template <typename Strategy
>
128 bool EphemeralRangeTemplate
<Strategy
>::isValid() const
130 return m_startPosition
.isNull() || m_domTreeVersion
== m_startPosition
.document()->domTreeVersion();
133 template <typename Strategy
>
134 bool EphemeralRangeTemplate
<Strategy
>::isValid() const
140 PassRefPtrWillBeRawPtr
<Range
> createRange(const EphemeralRange
& range
)
144 return Range::create(range
.document(), range
.startPosition(), range
.endPosition());
147 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate
<EditingStrategy
>;
148 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate
<EditingInComposedTreeStrategy
>;