1/*
2 * Copyright (C) 2005, 2006 Apple Computer, 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 "config.h"
30#import "EditingDelegate.h"
31
32#import "DumpRenderTree.h"
33#import "LayoutTestController.h"
34#import <WebKit/WebKit.h>
35
36@interface DOMNode (dumpPath)
37- (NSString *)dumpPath;
38@end
39
40@implementation DOMNode (dumpPath)
41- (NSString *)dumpPath
42{
43    DOMNode *parent = [self parentNode];
44    NSString *str = [NSString stringWithFormat:@"%@", [self nodeName]];
45    if (parent != nil) {
46        str = [str stringByAppendingString:@" > "];
47        str = [str stringByAppendingString:[parent dumpPath]];
48    }
49    return str;
50}
51@end
52
53@interface DOMRange (dump)
54- (NSString *)dump;
55@end
56
57@implementation DOMRange (dump)
58- (NSString *)dump
59{
60    return [NSString stringWithFormat:@"range from %ld of %@ to %ld of %@", [self startOffset], [[self startContainer] dumpPath], [self endOffset], [[self endContainer] dumpPath]];
61}
62@end
63
64@implementation EditingDelegate
65
66- (id)init
67{
68    self = [super init];
69    if (!self)
70        return nil;
71    acceptsEditing = YES;
72    return self;
73}
74
75- (BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
76{
77    if (!done && gLayoutTestController->dumpEditingCallbacks())
78        printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", [[range dump] UTF8String]);
79    return acceptsEditing;
80}
81
82- (BOOL)webView:(WebView *)webView shouldEndEditingInDOMRange:(DOMRange *)range
83{
84    if (!done && gLayoutTestController->dumpEditingCallbacks())
85        printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", [[range dump] UTF8String]);
86    return acceptsEditing;
87}
88
89- (BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
90{
91    static const char *insertactionstring[] = {
92        "WebViewInsertActionTyped",
93        "WebViewInsertActionPasted",
94        "WebViewInsertActionDropped",
95    };
96
97    if (!done && gLayoutTestController->dumpEditingCallbacks())
98        printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", [[node dumpPath] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
99    return acceptsEditing;
100}
101
102- (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
103{
104    static const char *insertactionstring[] = {
105        "WebViewInsertActionTyped",
106        "WebViewInsertActionPasted",
107        "WebViewInsertActionDropped",
108    };
109
110    if (!done && gLayoutTestController->dumpEditingCallbacks())
111        printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n", [[text description] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
112    return acceptsEditing;
113}
114
115- (BOOL)webView:(WebView *)webView shouldDeleteDOMRange:(DOMRange *)range
116{
117    if (!done && gLayoutTestController->dumpEditingCallbacks())
118        printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", [[range dump] UTF8String]);
119    return acceptsEditing;
120}
121
122- (BOOL)webView:(WebView *)webView shouldShowDeleteInterfaceForElement:(DOMHTMLElement *)element
123{
124    return [[element className] isEqualToString:@"needsDeletionUI"];
125}
126
127- (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
128{
129    static const char *affinitystring[] = {
130        "NSSelectionAffinityUpstream",
131        "NSSelectionAffinityDownstream"
132    };
133    static const char *boolstring[] = {
134        "FALSE",
135        "TRUE"
136    };
137
138    if (!done && gLayoutTestController->dumpEditingCallbacks())
139        printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n", [[currentRange dump] UTF8String], [[proposedRange dump] UTF8String], affinitystring[selectionAffinity], boolstring[flag]);
140    return acceptsEditing;
141}
142
143- (BOOL)webView:(WebView *)webView shouldApplyStyle:(DOMCSSStyleDeclaration *)style toElementsInDOMRange:(DOMRange *)range
144{
145    if (!done && gLayoutTestController->dumpEditingCallbacks())
146        printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n", [[style description] UTF8String], [[range dump] UTF8String]);
147    return acceptsEditing;
148}
149
150- (BOOL)webView:(WebView *)webView shouldChangeTypingStyle:(DOMCSSStyleDeclaration *)currentStyle toStyle:(DOMCSSStyleDeclaration *)proposedStyle
151{
152    if (!done && gLayoutTestController->dumpEditingCallbacks())
153        printf("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n", [[currentStyle description] UTF8String], [[proposedStyle description] UTF8String]);
154    return acceptsEditing;
155}
156
157- (void)webViewDidBeginEditing:(NSNotification *)notification
158{
159    if (!done && gLayoutTestController->dumpEditingCallbacks())
160        printf("EDITING DELEGATE: webViewDidBeginEditing:%s\n", [[notification name] UTF8String]);
161}
162
163- (void)webViewDidChange:(NSNotification *)notification
164{
165    if (!done && gLayoutTestController->dumpEditingCallbacks())
166        printf("EDITING DELEGATE: webViewDidChange:%s\n", [[notification name] UTF8String]);
167}
168
169- (void)webViewDidEndEditing:(NSNotification *)notification
170{
171    if (!done && gLayoutTestController->dumpEditingCallbacks())
172        printf("EDITING DELEGATE: webViewDidEndEditing:%s\n", [[notification name] UTF8String]);
173}
174
175- (void)webViewDidChangeTypingStyle:(NSNotification *)notification
176{
177    if (!done && gLayoutTestController->dumpEditingCallbacks())
178        printf("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n", [[notification name] UTF8String]);
179}
180
181- (void)webViewDidChangeSelection:(NSNotification *)notification
182{
183    if (!done && gLayoutTestController->dumpEditingCallbacks())
184        printf("EDITING DELEGATE: webViewDidChangeSelection:%s\n", [[notification name] UTF8String]);
185}
186
187- (void)setAcceptsEditing:(BOOL)newAcceptsEditing
188{
189    acceptsEditing = newAcceptsEditing;
190}
191
192@end
193