1/*
2 * Copyright (C) 2012 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "WKDOMRangePrivate.h"
28
29#if WK_API_ENABLED
30
31#import "InjectedBundleRangeHandle.h"
32#import "WKBundleAPICast.h"
33#import "WKDOMInternals.h"
34#import <WebCore/Document.h>
35
36@implementation WKDOMRange
37
38- (id)_initWithImpl:(WebCore::Range*)impl
39{
40    self = [super init];
41    if (!self)
42        return nil;
43
44    _impl = impl;
45    WebKit::WKDOMRangeCache().add(impl, self);
46
47    return self;
48}
49
50- (id)initWithDocument:(WKDOMDocument *)document
51{
52    RefPtr<WebCore::Range> range = WebCore::Range::create(*WebKit::toWebCoreDocument(document));
53    self = [self _initWithImpl:range.get()];
54    if (!self)
55        return nil;
56
57    return self;
58}
59
60- (void)dealloc
61{
62    WebKit::WKDOMRangeCache().remove(_impl.get());
63    [super dealloc];
64}
65
66- (void)setStart:(WKDOMNode *)node offset:(int)offset
67{
68    // FIXME: Do something about the exception.
69    WebCore::ExceptionCode ec = 0;
70    _impl->setStart(WebKit::toWebCoreNode(node), offset, ec);
71}
72
73- (void)setEnd:(WKDOMNode *)node offset:(int)offset
74{
75    // FIXME: Do something about the exception.
76    WebCore::ExceptionCode ec = 0;
77    _impl->setEnd(WebKit::toWebCoreNode(node), offset, ec);
78}
79
80- (void)collapse:(BOOL)toStart
81{
82    // FIXME: Do something about the exception.
83    WebCore::ExceptionCode ec = 0;
84    _impl->collapse(toStart, ec);
85}
86
87- (void)selectNode:(WKDOMNode *)node
88{
89    // FIXME: Do something about the exception.
90    WebCore::ExceptionCode ec = 0;
91    _impl->selectNode(WebKit::toWebCoreNode(node), ec);
92}
93
94- (void)selectNodeContents:(WKDOMNode *)node
95{
96    // FIXME: Do something about the exception.
97    WebCore::ExceptionCode ec = 0;
98    _impl->selectNodeContents(WebKit::toWebCoreNode(node), ec);
99}
100
101- (WKDOMNode *)startContainer
102{
103    // FIXME: Do something about the exception.
104    WebCore::ExceptionCode ec = 0;
105    return WebKit::toWKDOMNode(_impl->startContainer(ec));
106}
107
108- (NSInteger)startOffset
109{
110    // FIXME: Do something about the exception.
111    WebCore::ExceptionCode ec = 0;
112    return _impl->startOffset(ec);
113}
114
115- (WKDOMNode *)endContainer
116{
117    // FIXME: Do something about the exception.
118    WebCore::ExceptionCode ec = 0;
119    return WebKit::toWKDOMNode(_impl->endContainer(ec));
120}
121
122- (NSInteger)endOffset
123{
124    // FIXME: Do something about the exception.
125    WebCore::ExceptionCode ec = 0;
126    return _impl->endOffset(ec);
127}
128
129- (NSString *)text
130{
131    return _impl->text();
132}
133
134- (BOOL)isCollapsed
135{
136    // FIXME: Do something about the exception.
137    WebCore::ExceptionCode ec = 0;
138    return _impl->collapsed(ec);
139}
140
141- (NSArray *)textRects
142{
143    _impl->ownerDocument().updateLayoutIgnorePendingStylesheets();
144    Vector<WebCore::IntRect> rects;
145    _impl->textRects(rects);
146    return WebKit::toNSArray(rects);
147}
148
149@end
150
151@implementation WKDOMRange (WKPrivate)
152
153- (WKBundleRangeHandleRef)_copyBundleRangeHandleRef
154{
155    RefPtr<WebKit::InjectedBundleRangeHandle> rangeHandle = WebKit::InjectedBundleRangeHandle::getOrCreate(_impl.get());
156    return toAPI(rangeHandle.release().leakRef());
157}
158
159@end
160
161#endif // WK_API_ENABLED
162