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 "WKDOMNodePrivate.h"
28
29#if WK_API_ENABLED
30
31#import "InjectedBundleNodeHandle.h"
32#import "WKBundleAPICast.h"
33#import "WKDOMInternals.h"
34#import <WebCore/Document.h>
35
36@implementation WKDOMNode
37
38- (id)_initWithImpl:(WebCore::Node*)impl
39{
40    self = [super init];
41    if (!self)
42        return nil;
43
44    _impl = impl;
45    WebKit::WKDOMNodeCache().add(impl, self);
46
47    return self;
48}
49
50- (void)dealloc
51{
52    WebKit::WKDOMNodeCache().remove(_impl.get());
53    [super dealloc];
54}
55
56- (void)insertNode:(WKDOMNode *)node before:(WKDOMNode *)refNode
57{
58    // FIXME: Do something about the exception.
59    WebCore::ExceptionCode ec;
60    _impl->insertBefore(WebKit::toWebCoreNode(node), WebKit::toWebCoreNode(refNode), ec);
61}
62
63- (void)appendChild:(WKDOMNode *)node
64{
65    // FIXME: Do something about the exception.
66    WebCore::ExceptionCode ec;
67    _impl->appendChild(WebKit::toWebCoreNode(node), ec);
68}
69
70- (void)removeChild:(WKDOMNode *)node
71{
72    // FIXME: Do something about the exception.
73    WebCore::ExceptionCode ec;
74    _impl->removeChild(WebKit::toWebCoreNode(node), ec);
75}
76
77- (WKDOMDocument *)document
78{
79    return WebKit::toWKDOMDocument(&_impl->document());
80}
81
82- (WKDOMNode *)parentNode
83{
84    return WebKit::toWKDOMNode(_impl->parentNode());
85}
86
87- (WKDOMNode *)firstChild
88{
89    return WebKit::toWKDOMNode(_impl->firstChild());
90}
91
92- (WKDOMNode *)lastChild
93{
94    return WebKit::toWKDOMNode(_impl->lastChild());
95}
96
97- (WKDOMNode *)previousSibling
98{
99    return WebKit::toWKDOMNode(_impl->previousSibling());
100}
101
102- (WKDOMNode *)nextSibling
103{
104    return WebKit::toWKDOMNode(_impl->nextSibling());
105}
106
107- (NSArray *)textRects
108{
109    _impl->document().updateLayoutIgnorePendingStylesheets();
110    if (!_impl->renderer())
111        return nil;
112    Vector<WebCore::IntRect> rects;
113    _impl->textRects(rects);
114    return WebKit::toNSArray(rects);
115}
116
117@end
118
119@implementation WKDOMNode (WKPrivate)
120
121- (WKBundleNodeHandleRef)_copyBundleNodeHandleRef
122{
123    RefPtr<WebKit::InjectedBundleNodeHandle> nodeHandle = WebKit::InjectedBundleNodeHandle::getOrCreate(_impl.get());
124    return toAPI(nodeHandle.release().leakRef());
125}
126
127@end
128
129#endif // WK_API_ENABLED
130