~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit/chromium/src/WebRange.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "Element.h"
 
36
#include "Frame.h"
 
37
#include "FrameView.h"
 
38
#include "Range.h"
 
39
#include "ShadowRoot.h"
 
40
#include "TextIterator.h"
 
41
#include "WebFrameImpl.h"
 
42
#include "WebNode.h"
 
43
#include <public/WebFloatQuad.h>
 
44
#include <public/WebString.h>
 
45
#include <wtf/PassRefPtr.h>
 
46
 
 
47
using namespace WebCore;
 
48
 
 
49
namespace WebKit {
 
50
 
 
51
class WebRangePrivate : public Range {
 
52
};
 
53
 
 
54
void WebRange::reset()
 
55
{
 
56
    assign(0);
 
57
}
 
58
 
 
59
void WebRange::assign(const WebRange& other)
 
60
{
 
61
    WebRangePrivate* p = const_cast<WebRangePrivate*>(other.m_private);
 
62
    if (p)
 
63
        p->ref();
 
64
    assign(p);
 
65
}
 
66
 
 
67
int WebRange::startOffset() const
 
68
{
 
69
    return m_private->startOffset();
 
70
}
 
71
 
 
72
int WebRange::endOffset() const
 
73
{
 
74
    return m_private->endOffset();
 
75
}
 
76
 
 
77
WebNode WebRange::startContainer(int& exceptionCode) const
 
78
{
 
79
    return PassRefPtr<Node>(m_private->startContainer(exceptionCode));
 
80
}
 
81
 
 
82
WebNode WebRange::endContainer(int& exceptionCode) const
 
83
{
 
84
    return PassRefPtr<Node>(m_private->endContainer(exceptionCode));
 
85
}
 
86
 
 
87
WebString WebRange::toHTMLText() const
 
88
{
 
89
    return m_private->toHTML();
 
90
}
 
91
 
 
92
WebString WebRange::toPlainText() const
 
93
{
 
94
    return m_private->text();
 
95
}
 
96
 
 
97
// static
 
98
WebRange WebRange::fromDocumentRange(WebFrame* frame, int start, int length)
 
99
{
 
100
    WebCore::Frame* webFrame = static_cast<WebFrameImpl*>(frame)->frame();
 
101
    Element* selectionRoot = webFrame->selection()->rootEditableElement();
 
102
    ContainerNode* scope = selectionRoot ? selectionRoot : webFrame->document()->documentElement();
 
103
    return TextIterator::rangeFromLocationAndLength(scope, start, length);
 
104
}
 
105
 
 
106
WebVector<WebFloatQuad> WebRange::textQuads() const
 
107
{
 
108
    if (isNull())
 
109
        return WebVector<WebFloatQuad>();
 
110
 
 
111
    Frame* frame = m_private->ownerDocument() ? m_private->ownerDocument()->frame() : 0;
 
112
    if (!frame)
 
113
        return WebVector<WebFloatQuad>();
 
114
 
 
115
    Vector<FloatQuad> quads;
 
116
    m_private->textQuads(quads);
 
117
    for (unsigned i = 0; i < quads.size(); ++i) {
 
118
        quads[i].setP1(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p1())));
 
119
        quads[i].setP2(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p2())));
 
120
        quads[i].setP3(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p3())));
 
121
        quads[i].setP4(frame->view()->contentsToWindow(roundedIntPoint(quads[i].p4())));
 
122
    }
 
123
 
 
124
    return quads;
 
125
}
 
126
 
 
127
WebRange::WebRange(const WTF::PassRefPtr<WebCore::Range>& range)
 
128
    : m_private(static_cast<WebRangePrivate*>(range.leakRef()))
 
129
{
 
130
}
 
131
 
 
132
WebRange& WebRange::operator=(const WTF::PassRefPtr<WebCore::Range>& range)
 
133
{
 
134
    assign(static_cast<WebRangePrivate*>(range.leakRef()));
 
135
    return *this;
 
136
}
 
137
 
 
138
WebRange::operator WTF::PassRefPtr<WebCore::Range>() const
 
139
{
 
140
    return PassRefPtr<Range>(const_cast<WebRangePrivate*>(m_private));
 
141
}
 
142
 
 
143
void WebRange::assign(WebRangePrivate* p)
 
144
{
 
145
    // p is already ref'd for us by the caller
 
146
    if (m_private)
 
147
        m_private->deref();
 
148
    m_private = p;
 
149
}
 
150
 
 
151
} // namespace WebKit