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 "WebExceptionCode.h" 35 #include "WebFrameImpl.h" 36 #include "WebNode.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/TextIterator.h" 45 #include "core/page/Frame.h" 46 #include "core/page/FrameView.h" 47 #include "public/platform/WebFloatQuad.h" 48 #include "public/platform/WebString.h" 49 #include "wtf/PassRefPtr.h" 50 51 using namespace WebCore; 52 53 namespace WebKit { 54 55 class WebRangePrivate : public Range { 56 }; 57 58 void WebRange::reset() 59 { 60 assign(0); 61 } 62 63 void WebRange::assign(const WebRange& other) 64 { 65 WebRangePrivate* p = const_cast<WebRangePrivate*>(other.m_private); 66 if (p) 67 p->ref(); 68 assign(p); 69 } 70 71 int WebRange::startOffset() const 72 { 73 return m_private->startOffset(); 74 } 75 76 int WebRange::endOffset() const 77 { 78 return m_private->endOffset(); 79 } 80 81 WebNode WebRange::startContainer(WebExceptionCode& exceptionCode) const 82 { 83 // FIXME: Create a wrapper class that just sets the internal int. 84 TrackExceptionState es; 85 RefPtr<Node> node(m_private->startContainer(es)); 86 exceptionCode = es.code(); 87 return node.release(); 88 } 89 90 WebNode WebRange::endContainer(WebExceptionCode& exceptionCode) const 91 { 92 // FIXME: Create a wrapper class that just sets the internal int. 93 TrackExceptionState es; 94 RefPtr<Node> node(m_private->endContainer(es)); 95 exceptionCode = es.code(); 96 return node.release(); 97 } 98 99 WebString WebRange::toHTMLText() const 100 { 101 return m_private->toHTML(); 102 } 103 104 WebString WebRange::toPlainText() const 105 { 106 return m_private->text(); 107 } 108 109 WebRange WebRange::expandedToParagraph() const 110 { 111 WebRange copy(*this); 112 copy.m_private->expand("block", IGNORE_EXCEPTION); 113 return copy; 114 } 115 116 // static 117 WebRange WebRange::fromDocumentRange(WebFrame* frame, int start, int length) 118 { 119 WebCore::Frame* webFrame = static_cast<WebFrameImpl*>(frame)->frame(); 120 Element* selectionRoot = webFrame->selection()->rootEditableElement(); 121 ContainerNode* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement(); 122 return TextIterator::rangeFromLocationAndLength(scope, start, length); 123 } 124 125 WebVector<WebFloatQuad> WebRange::textQuads() const 126 { 127 if (isNull()) 128 return WebVector<WebFloatQuad>(); 129 130 Frame* frame = m_private->ownerDocument() ? m_private->ownerDocument()->frame() : 0; 131 if (!frame) 132 return WebVector<WebFloatQuad>(); 133 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()))); 141 } 142 143 return quads; 144 } 145 146 WebRange::WebRange(const WTF::PassRefPtr<WebCore::Range>& range) 147 : m_private(static_cast<WebRangePrivate*>(range.leakRef())) 148 { 149 } 150 151 WebRange& WebRange::operator=(const WTF::PassRefPtr<WebCore::Range>& range) 152 { 153 assign(static_cast<WebRangePrivate*>(range.leakRef())); 154 return *this; 155 } 156 157 WebRange::operator WTF::PassRefPtr<WebCore::Range>() const 158 { 159 return PassRefPtr<Range>(const_cast<WebRangePrivate*>(m_private)); 160 } 161 162 void WebRange::assign(WebRangePrivate* p) 163 { 164 // p is already ref'd for us by the caller 165 if (m_private) 166 m_private->deref(); 167 m_private = p; 168 } 169 170 } // namespace WebKit 171