2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "WebExceptionCode.h"
35 #include "WebFrameImpl.h"
37 #include "bindings/v8/ExceptionState.h"
38 #include "bindings/v8/ExceptionStatePlaceholder.h"
39 #include "core/dom/Document.h"
40 #include "core/dom/Element.h"
41 #include "core/dom/Range.h"
42 #include "core/dom/shadow/ShadowRoot.h"
43 #include "core/editing/FrameSelection.h"
44 #include "core/editing/PlainTextRange.h"
45 #include "core/frame/Frame.h"
46 #include "core/frame/FrameView.h"
47 #include "public/platform/WebFloatQuad.h"
48 #include "public/platform/WebString.h"
49 #include "wtf/PassRefPtr.h"
51 using namespace WebCore;
55 class WebRangePrivate : public Range {
58 void WebRange::reset()
63 void WebRange::assign(const WebRange& other)
65 WebRangePrivate* p = const_cast<WebRangePrivate*>(other.m_private);
71 int WebRange::startOffset() const
73 return m_private->startOffset();
76 int WebRange::endOffset() const
78 return m_private->endOffset();
81 WebNode WebRange::startContainer(WebExceptionCode& exceptionCode) const
83 // FIXME: Create a wrapper class that just sets the internal int.
84 TrackExceptionState exceptionState;
85 RefPtr<Node> node(m_private->startContainer(exceptionState));
86 exceptionCode = exceptionState.code();
87 return node.release();
90 WebNode WebRange::endContainer(WebExceptionCode& exceptionCode) const
92 // FIXME: Create a wrapper class that just sets the internal int.
93 TrackExceptionState exceptionState;
94 RefPtr<Node> node(m_private->endContainer(exceptionState));
95 exceptionCode = exceptionState.code();
96 return node.release();
99 WebString WebRange::toHTMLText() const
101 return m_private->toHTML();
104 WebString WebRange::toPlainText() const
106 return m_private->text();
109 WebRange WebRange::expandedToParagraph() const
111 WebRange copy(*this);
112 copy.m_private->expand("block", IGNORE_EXCEPTION);
117 WebRange WebRange::fromDocumentRange(WebFrame* frame, int start, int length)
119 WebCore::Frame* webFrame = toWebFrameImpl(frame)->frame();
120 Element* selectionRoot = webFrame->selection().rootEditableElement();
121 ContainerNode* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement();
122 return PlainTextRange(start, start + length).createRange(*scope);
125 WebVector<WebFloatQuad> WebRange::textQuads() const
128 return WebVector<WebFloatQuad>();
130 Frame* frame = m_private->ownerDocument().frame();
132 return WebVector<WebFloatQuad>();
134 Vector<FloatQuad> quads;
135 m_private->textQuads(quads);
136 for (unsigned i = 0; i < quads.size(); ++i) {
137 quads[i].setP1(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p1())));
138 quads[i].setP2(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p2())));
139 quads[i].setP3(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p3())));
140 quads[i].setP4(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p4())));
146 WebRange::WebRange(const WTF::PassRefPtr<WebCore::Range>& range)
147 : m_private(static_cast<WebRangePrivate*>(range.leakRef()))
151 WebRange& WebRange::operator=(const WTF::PassRefPtr<WebCore::Range>& range)
153 assign(static_cast<WebRangePrivate*>(range.leakRef()));
157 WebRange::operator WTF::PassRefPtr<WebCore::Range>() const
159 return PassRefPtr<Range>(const_cast<WebRangePrivate*>(m_private));
162 void WebRange::assign(WebRangePrivate* p)
164 // p is already ref'd for us by the caller