EditingDelegate.mm 7.4 KB

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