tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / WebRange.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "WebRange.h"
33
34 #include "Document.h"
35 #include "Frame.h"
36 #include "Range.h"
37 #include "TextIterator.h"
38 #include "WebFrameImpl.h"
39 #include "WebNode.h"
40 #include "WebString.h"
41 #include <wtf/PassRefPtr.h>
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 class WebRangePrivate : public Range {
48 };
49
50 void WebRange::reset()
51 {
52     assign(0);
53 }
54
55 void WebRange::assign(const WebRange& other)
56 {
57     WebRangePrivate* p = const_cast<WebRangePrivate*>(other.m_private);
58     if (p)
59         p->ref();
60     assign(p);
61 }
62
63 int WebRange::startOffset() const
64 {
65     return m_private->startOffset();
66 }
67
68 int WebRange::endOffset() const
69 {
70     return m_private->endOffset();
71 }
72
73 WebNode WebRange::startContainer(int& exceptionCode) const
74 {
75     return PassRefPtr<Node>(m_private->startContainer(exceptionCode));
76 }
77
78 WebNode WebRange::endContainer(int& exceptionCode) const
79 {
80     return PassRefPtr<Node>(m_private->endContainer(exceptionCode));
81 }
82
83 WebString WebRange::toHTMLText() const
84 {
85     return m_private->toHTML();
86 }
87
88 WebString WebRange::toPlainText() const
89 {
90     return m_private->text();
91 }
92
93 // static
94 WebRange WebRange::fromDocumentRange(WebFrame* frame, int start, int length)
95 {
96     WebCore::Frame* webFrame = static_cast<WebFrameImpl*>(frame)->frame();
97     Element* selectionRoot = webFrame->selection()->rootEditableElement();
98     Element* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement();
99     return TextIterator::rangeFromLocationAndLength(scope, start, length);
100 }
101
102 WebRange::WebRange(const WTF::PassRefPtr<WebCore::Range>& range)
103     : m_private(static_cast<WebRangePrivate*>(range.leakRef()))
104 {
105 }
106
107 WebRange& WebRange::operator=(const WTF::PassRefPtr<WebCore::Range>& range)
108 {
109     assign(static_cast<WebRangePrivate*>(range.leakRef()));
110     return *this;
111 }
112
113 WebRange::operator WTF::PassRefPtr<WebCore::Range>() const
114 {
115     return PassRefPtr<Range>(const_cast<WebRangePrivate*>(m_private));
116 }
117
118 void WebRange::assign(WebRangePrivate* p)
119 {
120     // p is already ref'd for us by the caller
121     if (m_private)
122         m_private->deref();
123     m_private = p;
124 }
125
126 } // namespace WebKit