Home | History | Annotate | Download | only in web
      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/core/v8/ExceptionState.h"
     35 #include "bindings/core/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/WebString.h"
     45 #include "public/web/WebExceptionCode.h"
     46 #include "public/web/WebNode.h"
     47 #include "web/WebLocalFrameImpl.h"
     48 #include "wtf/PassRefPtr.h"
     49 
     50 namespace blink {
     51 
     52 void WebRange::reset()
     53 {
     54     m_private.reset();
     55 }
     56 
     57 void WebRange::assign(const WebRange& other)
     58 {
     59     m_private = other.m_private;
     60 }
     61 
     62 int WebRange::startOffset() const
     63 {
     64     return m_private->startOffset();
     65 }
     66 
     67 int WebRange::endOffset() const
     68 {
     69     return m_private->endOffset();
     70 }
     71 
     72 WebNode WebRange::startContainer(WebExceptionCode& exceptionCode) const
     73 {
     74     // FIXME: Create a wrapper class that just sets the internal int.
     75     RefPtrWillBeRawPtr<Node> node(m_private->startContainer());
     76     exceptionCode = 0;
     77     return node.release();
     78 }
     79 
     80 WebNode WebRange::endContainer(WebExceptionCode& exceptionCode) const
     81 {
     82     // FIXME: Create a wrapper class that just sets the internal int.
     83     RefPtrWillBeRawPtr<Node> node(m_private->endContainer());
     84     exceptionCode = 0;
     85     return node.release();
     86 }
     87 
     88 WebString WebRange::toHTMLText() const
     89 {
     90     return m_private->toHTML();
     91 }
     92 
     93 WebString WebRange::toPlainText() const
     94 {
     95     return m_private->text();
     96 }
     97 
     98 WebRange WebRange::expandedToParagraph() const
     99 {
    100     WebRange copy(*this);
    101     copy.m_private->expand("block", IGNORE_EXCEPTION);
    102     return copy;
    103 }
    104 
    105 // static
    106 WebRange WebRange::fromDocumentRange(WebLocalFrame* frame, int start, int length)
    107 {
    108     LocalFrame* webFrame = toWebLocalFrameImpl(frame)->frame();
    109     Element* selectionRoot = webFrame->selection().rootEditableElement();
    110     ContainerNode* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement();
    111     return PlainTextRange(start, start + length).createRange(*scope);
    112 }
    113 
    114 WebRange::WebRange(const PassRefPtrWillBeRawPtr<Range>& range)
    115     : m_private(range)
    116 {
    117 }
    118 
    119 WebRange::operator PassRefPtrWillBeRawPtr<Range>() const
    120 {
    121     return m_private.get();
    122 }
    123 
    124 } // namespace blink
    125