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 "WebFrameLoaderClient.h"
39#import "WebFramePrivate.h"
40#import "WebKitNSStringExtras.h"
41#import <JavaScriptCore/APICast.h>
42#import <WebCore/Document.h>
43#import <WebCore/Frame.h>
44#import <WebCore/FrameLoader.h>
45#import <WebCore/HTMLInputElement.h>
46#import <WebCore/HTMLParserIdioms.h>
47#import <WebCore/JSElement.h>
48#import <WebCore/LegacyWebArchive.h>
49#import <WebCore/markup.h>
50#import <WebCore/RenderTreeAsText.h>
51#import <WebCore/ShadowRoot.h>
52#import <WebKit/DOMExtensions.h>
53#import <WebKit/DOMHTML.h>
54#import <runtime/JSLock.h>
55#import <runtime/JSCJSValue.h>
56#import <wtf/Assertions.h>
57
58using namespace WebCore;
59using namespace JSC;
60
61@implementation DOMElement (WebDOMElementOperationsPrivate)
62
63+ (DOMElement *)_DOMElementFromJSContext:(JSContextRef)context value:(JSValueRef)value
64{
65    if (!context)
66        return 0;
67
68    if (!value)
69        return 0;
70
71    ExecState* exec = toJS(context);
72    JSLockHolder lock(exec);
73    return kit(toElement(toJS(exec, value)));
74}
75
76@end
77
78class WebFrameFilter : public WebCore::FrameFilter {
79public:
80    WebFrameFilter(WebArchiveSubframeFilter filterBlock);
81    ~WebFrameFilter();
82private:
83    virtual bool shouldIncludeSubframe(Frame*) const OVERRIDE;
84
85    WebArchiveSubframeFilter m_filterBlock;
86};
87
88WebFrameFilter::WebFrameFilter(WebArchiveSubframeFilter filterBlock)
89    : m_filterBlock(Block_copy(filterBlock))
90{
91}
92
93WebFrameFilter::~WebFrameFilter()
94{
95    Block_release(m_filterBlock);
96}
97
98bool WebFrameFilter::shouldIncludeSubframe(Frame* frame) const
99{
100    if (!m_filterBlock)
101        return true;
102
103    return m_filterBlock(kit(frame));
104}
105
106@implementation DOMNode (WebDOMNodeOperations)
107
108- (WebArchive *)webArchive
109{
110    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
111}
112
113- (WebArchive *)webArchiveByFilteringSubframes:(WebArchiveSubframeFilter)webArchiveSubframeFilter
114{
115    WebFrameFilter filter(webArchiveSubframeFilter);
116    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self), &filter)] autorelease];
117}
118
119@end
120
121@implementation DOMNode (WebDOMNodeOperationsPendingPublic)
122
123- (NSString *)markupString
124{
125    return createFullMarkup(core(self));
126}
127
128- (NSRect)_renderRect:(bool *)isReplaced
129{
130    return NSRect(core(self)->pixelSnappedRenderRect(isReplaced));
131}
132
133@end
134
135@implementation DOMDocument (WebDOMDocumentOperations)
136
137- (WebFrame *)webFrame
138{
139    Frame* frame = core(self)->frame();
140    if (!frame)
141        return nil;
142    return kit(frame);
143}
144
145- (NSURL *)URLWithAttributeString:(NSString *)string
146{
147    return core(self)->completeURL(stripLeadingAndTrailingHTMLSpaces(string));
148}
149
150@end
151
152@implementation DOMDocument (WebDOMDocumentOperationsInternal)
153
154- (DOMRange *)_documentRange
155{
156    DOMRange *range = [self createRange];
157
158    if (DOMNode* documentElement = [self documentElement])
159        [range selectNode:documentElement];
160
161    return range;
162}
163
164@end
165
166@implementation DOMRange (WebDOMRangeOperations)
167
168- (WebArchive *)webArchive
169{
170    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
171}
172
173- (NSString *)markupString
174{
175    return createFullMarkup(core(self));
176}
177
178@end
179
180@implementation DOMHTMLFrameElement (WebDOMHTMLFrameElementOperations)
181
182- (WebFrame *)contentFrame
183{
184    return [[self contentDocument] webFrame];
185}
186
187@end
188
189@implementation DOMHTMLIFrameElement (WebDOMHTMLIFrameElementOperations)
190
191- (WebFrame *)contentFrame
192{
193    return [[self contentDocument] webFrame];
194}
195
196@end
197
198@implementation DOMHTMLInputElement (WebDOMHTMLInputElementOperationsPrivate)
199
200- (void)_setAutofilled:(BOOL)autofilled
201{
202    static_cast<HTMLInputElement*>(core((DOMElement *)self))->setAutofilled(autofilled);
203}
204
205@end
206
207@implementation DOMHTMLObjectElement (WebDOMHTMLObjectElementOperations)
208
209- (WebFrame *)contentFrame
210{
211    return [[self contentDocument] webFrame];
212}
213
214@end
215