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 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 "DOMWheelEventInternal.h"
36#import "WebArchiveInternal.h"
37#import "WebDataSourcePrivate.h"
38#import "WebFrameInternal.h"
39#import "WebFrameLoaderClient.h"
40#import "WebFramePrivate.h"
41#import "WebKitNSStringExtras.h"
42#import <JavaScriptCore/APICast.h>
43#import <WebCore/Document.h>
44#import <WebCore/Frame.h>
45#import <WebCore/FrameLoader.h>
46#import <WebCore/HTMLInputElement.h>
47#import <WebCore/HTMLParserIdioms.h>
48#import <WebCore/JSElement.h>
49#import <WebCore/LegacyWebArchive.h>
50#import <WebCore/PlatformWheelEvent.h>
51#import <WebCore/RenderElement.h>
52#import <WebCore/RenderTreeAsText.h>
53#import <WebCore/ShadowRoot.h>
54#import <WebCore/WheelEvent.h>
55#import <WebCore/markup.h>
56#import <WebKitLegacy/DOMExtensions.h>
57#import <WebKitLegacy/DOMHTML.h>
58#import <runtime/JSCJSValue.h>
59#import <runtime/JSLock.h>
60#import <wtf/Assertions.h>
61
62using namespace WebCore;
63using namespace JSC;
64
65@implementation DOMElement (WebDOMElementOperationsPrivate)
66
67+ (DOMElement *)_DOMElementFromJSContext:(JSContextRef)context value:(JSValueRef)value
68{
69    if (!context)
70        return 0;
71
72    if (!value)
73        return 0;
74
75    ExecState* exec = toJS(context);
76    JSLockHolder lock(exec);
77    return kit(toElement(toJS(exec, value)));
78}
79
80@end
81
82@implementation DOMNode (WebDOMNodeOperations)
83
84- (WebArchive *)webArchive
85{
86    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
87}
88
89- (WebArchive *)webArchiveByFilteringSubframes:(WebArchiveSubframeFilter)webArchiveSubframeFilter
90{
91    WebArchive *webArchive = [[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self), [webArchiveSubframeFilter](Frame& subframe) -> bool {
92        return webArchiveSubframeFilter(kit(&subframe));
93    })];
94
95    return [webArchive autorelease];
96}
97
98#if PLATFORM(IOS)
99- (BOOL)isHorizontalWritingMode
100{
101    Node* node = core(self);
102    if (!node)
103        return YES;
104
105    RenderObject* renderer = node->renderer();
106    if (!renderer)
107        return YES;
108
109    return renderer->style().isHorizontalWritingMode();
110}
111
112- (void)hidePlaceholder
113{
114    if (![self isKindOfClass:[DOMHTMLInputElement class]]
115        && ![self isKindOfClass:[DOMHTMLTextAreaElement class]])
116        return;
117
118    Node *node = core(self);
119    HTMLTextFormControlElement *formControl = static_cast<HTMLTextFormControlElement *>(node);
120    formControl->hidePlaceholder();
121}
122
123- (void)showPlaceholderIfNecessary
124{
125    if (![self isKindOfClass:[DOMHTMLInputElement class]]
126        && ![self isKindOfClass:[DOMHTMLTextAreaElement class]])
127        return;
128
129    HTMLTextFormControlElement *formControl = static_cast<HTMLTextFormControlElement *>(core(self));
130    formControl->showPlaceholderIfNecessary();
131}
132#endif
133
134@end
135
136@implementation DOMNode (WebDOMNodeOperationsPendingPublic)
137
138- (NSString *)markupString
139{
140    return createFullMarkup(*core(self));
141}
142
143- (NSRect)_renderRect:(bool *)isReplaced
144{
145    return NSRect(core(self)->pixelSnappedRenderRect(isReplaced));
146}
147
148@end
149
150@implementation DOMDocument (WebDOMDocumentOperations)
151
152- (WebFrame *)webFrame
153{
154    Frame* frame = core(self)->frame();
155    if (!frame)
156        return nil;
157    return kit(frame);
158}
159
160- (NSURL *)URLWithAttributeString:(NSString *)string
161{
162    return core(self)->completeURL(stripLeadingAndTrailingHTMLSpaces(string));
163}
164
165@end
166
167@implementation DOMDocument (WebDOMDocumentOperationsInternal)
168
169- (DOMRange *)_documentRange
170{
171    DOMRange *range = [self createRange];
172
173    if (DOMNode* documentElement = [self documentElement])
174        [range selectNode:documentElement];
175
176    return range;
177}
178
179@end
180
181@implementation DOMRange (WebDOMRangeOperations)
182
183- (WebArchive *)webArchive
184{
185    return [[[WebArchive alloc] _initWithCoreLegacyWebArchive:LegacyWebArchive::create(core(self))] autorelease];
186}
187
188- (NSString *)markupString
189{
190    return createFullMarkup(*core(self));
191}
192
193@end
194
195@implementation DOMHTMLFrameElement (WebDOMHTMLFrameElementOperations)
196
197- (WebFrame *)contentFrame
198{
199    return [[self contentDocument] webFrame];
200}
201
202@end
203
204@implementation DOMHTMLIFrameElement (WebDOMHTMLIFrameElementOperations)
205
206- (WebFrame *)contentFrame
207{
208    return [[self contentDocument] webFrame];
209}
210
211@end
212
213@implementation DOMHTMLInputElement (WebDOMHTMLInputElementOperationsPrivate)
214
215- (void)_setAutofilled:(BOOL)autofilled
216{
217    toHTMLInputElement(core((DOMElement *)self))->setAutofilled(autofilled);
218}
219
220@end
221
222@implementation DOMHTMLObjectElement (WebDOMHTMLObjectElementOperations)
223
224- (WebFrame *)contentFrame
225{
226    return [[self contentDocument] webFrame];
227}
228
229@end
230
231#if !PLATFORM(IOS)
232static NSEventPhase toNSEventPhase(PlatformWheelEventPhase platformPhase)
233{
234    uint32_t phase = PlatformWheelEventPhaseNone;
235    if (platformPhase & PlatformWheelEventPhaseBegan)
236        phase |= NSEventPhaseBegan;
237    if (platformPhase & PlatformWheelEventPhaseStationary)
238        phase |= NSEventPhaseStationary;
239    if (platformPhase & PlatformWheelEventPhaseChanged)
240        phase |= NSEventPhaseChanged;
241    if (platformPhase & PlatformWheelEventPhaseEnded)
242        phase |= NSEventPhaseEnded;
243    if (platformPhase & PlatformWheelEventPhaseCancelled)
244        phase |= NSEventPhaseCancelled;
245#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
246    if (platformPhase & PlatformWheelEventPhaseMayBegin)
247        phase |= NSEventPhaseMayBegin;
248#endif
249
250    return static_cast<NSEventPhase>(phase);
251}
252
253@implementation DOMWheelEvent (WebDOMWheelEventOperationsPrivate)
254
255- (NSEventPhase)_phase
256{
257    return toNSEventPhase(core(self)->phase());
258}
259
260- (NSEventPhase)_momentumPhase
261{
262    return toNSEventPhase(core(self)->momentumPhase());
263}
264
265@end
266#endif
267