Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / 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 "public/web/WebRange.h"
33
34 #include "bindings/v8/ExceptionState.h"
35 #include "bindings/v8/ExceptionStatePlaceholder.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/Element.h"
38 #include "core/dom/Range.h"
39 #include "core/dom/shadow/ShadowRoot.h"
40 #include "core/editing/FrameSelection.h"
41 #include "core/editing/PlainTextRange.h"
42 #include "core/frame/FrameView.h"
43 #include "core/frame/LocalFrame.h"
44 #include "public/platform/WebFloatQuad.h"
45 #include "public/platform/WebString.h"
46 #include "public/web/WebExceptionCode.h"
47 #include "public/web/WebNode.h"
48 #include "web/WebLocalFrameImpl.h"
49 #include "wtf/PassRefPtr.h"
50
51 using namespace WebCore;
52
53 namespace blink {
54
55 void WebRange::reset()
56 {
57     m_private.reset();
58 }
59
60 void WebRange::assign(const WebRange& other)
61 {
62     m_private = other.m_private;
63 }
64
65 int WebRange::startOffset() const
66 {
67     return m_private->startOffset();
68 }
69
70 int WebRange::endOffset() const
71 {
72     return m_private->endOffset();
73 }
74
75 WebNode WebRange::startContainer(WebExceptionCode& exceptionCode) const
76 {
77     // FIXME: Create a wrapper class that just sets the internal int.
78     RefPtrWillBeRawPtr<Node> node(m_private->startContainer());
79     exceptionCode = 0;
80     return node.release();
81 }
82
83 WebNode WebRange::endContainer(WebExceptionCode& exceptionCode) const
84 {
85     // FIXME: Create a wrapper class that just sets the internal int.
86     RefPtrWillBeRawPtr<Node> node(m_private->endContainer());
87     exceptionCode = 0;
88     return node.release();
89 }
90
91 WebString WebRange::toHTMLText() const
92 {
93     return m_private->toHTML();
94 }
95
96 WebString WebRange::toPlainText() const
97 {
98     return m_private->text();
99 }
100
101 WebRange WebRange::expandedToParagraph() const
102 {
103     WebRange copy(*this);
104     copy.m_private->expand("block", IGNORE_EXCEPTION);
105     return copy;
106 }
107
108 // static
109 WebRange WebRange::fromDocumentRange(WebLocalFrame* frame, int start, int length)
110 {
111     WebCore::LocalFrame* webFrame = toWebLocalFrameImpl(frame)->frame();
112     Element* selectionRoot = webFrame->selection().rootEditableElement();
113     ContainerNode* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement();
114     return PlainTextRange(start, start + length).createRange(*scope);
115 }
116
117 WebVector<WebFloatQuad> WebRange::textQuads() const
118 {
119     if (isNull())
120         return WebVector<WebFloatQuad>();
121
122     LocalFrame* frame = m_private->ownerDocument().frame();
123     if (!frame)
124         return WebVector<WebFloatQuad>();
125
126     Vector<FloatQuad> quads;
127     m_private->textQuads(quads);
128     for (unsigned i = 0; i < quads.size(); ++i) {
129         quads[i].setP1(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p1())));
130         quads[i].setP2(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p2())));
131         quads[i].setP3(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p3())));
132         quads[i].setP4(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p4())));
133     }
134
135     return quads;
136 }
137
138 WebRange::WebRange(const PassRefPtrWillBeRawPtr<WebCore::Range>& range)
139     : m_private(range)
140 {
141 }
142
143 WebRange::operator PassRefPtrWillBeRawPtr<WebCore::Range>() const
144 {
145     return m_private.get();
146 }
147
148 } // namespace blink