1/*
2 * Copyright (C) 2005, 2008 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import "WebDOMOperationsPrivate.h"
30
31#import "DOMDocumentInternal.h"
32#import "DOMElementInternal.h"
33#import "DOMNodeInternal.h"
34#import "DOMRangeInternal.h"
35#import "WebArchiveInternal.h"
36#import "WebDataSourcePrivate.h"
37#import "WebFrameInternal.h"
38#import "WebFramePrivate.h"
39#import "WebKitNSStringExtras.h"
40#import <JavaScriptCore/APICast.h>
41#import <WebCore/Document.h>
42#import <WebCore/Element.h>
43#import <WebCore/HTMLInputElement.h>
44#import <WebCore/HTMLParserIdioms.h>
45#import <WebCore/JSElement.h>
46#import <WebCore/LegacyWebArchive.h>
47#import <WebCore/markup.h>
48#import <WebCore/RenderTreeAsText.h>
49#import <WebKit/DOMExtensions.h>
50#import <WebKit/DOMHTML.h>
51#import <runtime/JSLock.h>
52#import <runtime/JSValue.h>
53#import <wtf/Assertions.h>
54
55using namespace WebCore;
56using namespace JSC;
57
58@implementation DOMElement (WebDOMElementOperationsPrivate)
59
60+ (DOMElement *)_DOMElementFromJSContext:(JSContextRef)context value:(JSValueRef)value
61{
62    if (!context)
63        return 0;
64
65    if (!value)
66        return 0;
67
68    JSLock lock(SilenceAssertionsOnly);
69    return kit(toElement(toJS(toJS(context), value)));
70}
71
72- (NSString *)_markerTextForListItem
73{
74    return WebCore::markerTextForListItem(core(self));
75}
76
77- (JSValueRef)_shadowRoot:(JSContextRef)context
78{
79    JSLock lock(SilenceAssertionsOnly);
80    ExecState* execState = toJS(context);
81    return toRef(execState, toJS(execState, core(self)->shadowRoot()));
82}
83
84@end
85
86@implementation DOMNode (WebDOMNodeOperations)
87
88- (WebArchive *)webArchive
89{
90    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
91}
92
93@end
94
95@implementation DOMNode (WebDOMNodeOperationsPendingPublic)
96
97- (NSString *)markupString
98{
99    return createFullMarkup(core(self));
100}
101
102- (NSRect)_renderRect:(bool *)isReplaced
103{
104    return NSRect(core(self)->renderRect(isReplaced));
105}
106
107@end
108
109/* This doesn't appear to be used by anyone.  We should consider removing this. */
110@implementation DOMNode (WebDOMNodeOperationsInternal)
111
112- (NSArray *)_subresourceURLs
113{
114    ListHashSet<KURL> urls;
115    core(self)->getSubresourceURLs(urls);
116    if (!urls.size())
117        return nil;
118
119    NSMutableArray *array = [NSMutableArray arrayWithCapacity:urls.size()];
120    ListHashSet<KURL>::iterator end = urls.end();
121    for (ListHashSet<KURL>::iterator it = urls.begin(); it != end; ++it)
122        [array addObject:(NSURL *)*it];
123
124    return array;
125}
126
127@end
128
129@implementation DOMDocument (WebDOMDocumentOperations)
130
131- (WebFrame *)webFrame
132{
133    Frame* frame = core(self)->frame();
134    if (!frame)
135        return nil;
136    return kit(frame);
137}
138
139- (NSURL *)URLWithAttributeString:(NSString *)string
140{
141    return core(self)->completeURL(stripLeadingAndTrailingHTMLSpaces(string));
142}
143
144@end
145
146@implementation DOMDocument (WebDOMDocumentOperationsInternal)
147
148- (DOMRange *)_documentRange
149{
150    DOMRange *range = [self createRange];
151
152    if (DOMNode* documentElement = [self documentElement])
153        [range selectNode:documentElement];
154
155    return range;
156}
157
158@end
159
160@implementation DOMDocument (WebDOMDocumentOperationsPrivate)
161
162- (NSArray *)_focusableNodes
163{
164    Vector<RefPtr<Node> > nodes;
165    core(self)->getFocusableNodes(nodes);
166    NSMutableArray *array = [NSMutableArray arrayWithCapacity:nodes.size()];
167    for (unsigned i = 0; i < nodes.size(); ++i)
168        [array addObject:kit(nodes[i].get())];
169    return array;
170}
171
172@end
173
174@implementation DOMRange (WebDOMRangeOperations)
175
176- (WebArchive *)webArchive
177{
178    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
179}
180
181- (NSString *)markupString
182{
183    return createFullMarkup(core(self));
184}
185
186@end
187
188@implementation DOMHTMLFrameElement (WebDOMHTMLFrameElementOperations)
189
190- (WebFrame *)contentFrame
191{
192    return [[self contentDocument] webFrame];
193}
194
195@end
196
197@implementation DOMHTMLIFrameElement (WebDOMHTMLIFrameElementOperations)
198
199- (WebFrame *)contentFrame
200{
201    return [[self contentDocument] webFrame];
202}
203
204@end
205
206@implementation DOMHTMLInputElement (WebDOMHTMLInputElementOperationsPrivate)
207
208- (void)_setAutofilled:(BOOL)autofilled
209{
210    static_cast<HTMLInputElement*>(core((DOMElement *)self))->setAutofilled(autofilled);
211}
212
213- (void)_setValueForUser:(NSString *)value
214{
215    static_cast<HTMLInputElement*>(core((DOMElement *)self))->setValueForUser(value);
216}
217
218@end
219
220@implementation DOMHTMLObjectElement (WebDOMHTMLObjectElementOperations)
221
222- (WebFrame *)contentFrame
223{
224    return [[self contentDocument] webFrame];
225}
226
227@end
228