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