Index: /trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEWebKit.m
===================================================================
--- /trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEWebKit.m	(revision 454)
+++ /trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEWebKit.m	(revision 455)
@@ -14,12 +14,9 @@
 #import <unistd.h>
 
-// XXX WebCoreFrameBridge is going away
-// (see <http://trac.webkit.org/projects/webkit/wiki/Maintenance%20and%20architecture%20list>)
+// Safari 3.1 and earlier (pre-r31014)
+// eliminated in http://bugs.webkit.org/show_bug.cgi?id=17640
 @interface WebCoreFrameBridge : NSObject
-
-// XXX move to DOM-based accessors
 - (DOMRange *)convertNSRangeToDOMRange:(NSRange)range;
 - (NSString *)stringForRange:(DOMRange *)range;
-
 @end
 
@@ -83,8 +80,9 @@
     
     WebView *webView = [(WebHTMLView *)self _webView];
-    BOOL isEditable = [webView isEditable];
+    BOOL webViewIsEditable = [webView isEditable];
+    BOOL elementIsEditable = YES; // don't restore
     BOOL isContinuousSpellCheckingEnabled = NO, isGrammarCheckingEnabled = NO;
 
-    if (isEditable) {
+    if (webViewIsEditable) {
 	[webView setPolicyDelegate: policyDelegate];
 	[policyDelegate release]; policyDelegate = nil;
@@ -97,5 +95,13 @@
 
     @try {
-	if (!isEditable) {
+	NSPoint viewClickPt = [webView convertPoint: downPt fromView: nil];
+	NSDictionary *elementDict = [webView elementAtPoint: viewClickPt];
+	ICLog(@"elementDict: %@", elementDict);
+
+	NSAssert([elementDict count] != 0, ICCF_LocalizedString(@"Sorry, ICeCoffEE was unable to find anything to select"));
+
+	elementIsEditable = [[elementDict objectForKey: @"WebElementIsContentEditableKey"] boolValue];
+	if (!elementIsEditable) {
+	    NSAssert(!webViewIsEditable, @"Internal error: uneditable element inside editable WebView");
 	    [webView setEditable: YES];
 	
@@ -118,14 +124,15 @@
 	}
 
-	NSPoint viewClickPt = [webView convertPoint: downPt fromView: nil];
-	NSDictionary *elementDict = [webView elementAtPoint: viewClickPt];
-	NSLog(@"elementDict: %@", elementDict);
-	NSAssert([elementDict count] != 0, ICCF_LocalizedString(@"Sorry, ICeCoffEE was unable to find anything to select"));
-        
-	WebCoreFrameBridge *bridge = [(WebHTMLView *)self _bridge];
+	DOMNode *clickedNode = [elementDict objectForKey: @"WebElementDOMNode"];
+	
+	if (!elementIsEditable && ([clickedNode isKindOfClass: NSClassFromString(@"DOMHTMLInputElement")] ||
+	    [clickedNode isKindOfClass: NSClassFromString(@"DOMHTMLFieldSetElement")])) {
+	    ICLog(@"got an uneditable form field (e.g., button)");
+	    return;
+	}
 
 	id link = [elementDict objectForKey: @"WebElementLinkURL"];
 	NSString *url = [link isKindOfClass: [NSURL class]] ? [link absoluteString] : nil;
-
+	
 	ICCF_StartIC();
 	
@@ -134,5 +141,5 @@
 	if (url != nil) {
 	    ICLog(@"got a link");
-	    if (!isEditable) {
+	    if (!elementIsEditable) {
 		ICCF_StopIC();
 		return;
@@ -140,18 +147,22 @@
 	    // XXX handle existing selection
 	    domRange = [webView selectedDOMRange];
-	    [domRange selectNode: [elementDict objectForKey:@"WebElementDOMNode"]];
+	    [domRange selectNode: clickedNode];
 	    [webView setSelectedDOMRange: domRange affinity: NSSelectionAffinityDownstream];
 	} else {
-	    // may have become deselected in mouseDown
-	    [webView setSelectedDOMRange: selectedRange affinity: NSSelectionAffinityDownstream];
+	    if (!elementIsEditable) // may have become deselected in mouseDown
+		[webView setSelectedDOMRange: selectedRange affinity: NSSelectionAffinityDownstream];
 	    
 	    NSRange range = [ICeCoffEETrigger rangeForEvent: downEvent onTarget: (NSView<NSTextInput> *)self];
-	    
 	    NSAssert(range.location != NSNotFound, ICCF_LocalizedString(@"Sorry, ICeCoffEE was unable to find anything to select"));
 
+	    WebCoreFrameBridge *bridge = [(WebHTMLView *)self _bridge];
+
 	    // XXX limit to a reasonable size
-	    // NSRange fetchRange = [bridge convertDOMRangeToNSRange: [(WebHTMLView *)self _documentRange]];
-	    NSString *s = [bridge stringForRange: [(WebHTMLView *)self _documentRange]];
-	
+	    NSString *s;
+	    if (elementIsEditable) // for form fields, range will be field-relative rather than document-relative
+		s = [[(NSView<NSTextInput> *)self attributedSubstringFromRange: NSMakeRange(0, UINT_MAX)] string];
+	    else // sometimes attributedSubstringFromRange: returns nil
+		s = [bridge stringForRange: [(WebHTMLView *)self _documentRange]];
+	    
 	    if (range.length == 0) {
 		range.length = 1;
@@ -184,5 +195,5 @@
 	[selectedRange release]; selectedRange = nil;
 	[downEvent release]; downEvent = nil;
-	if (!isEditable) {
+	if (!elementIsEditable && !webViewIsEditable) {
 	    [webView setEditable: NO];
 	    if (isContinuousSpellCheckingEnabled)
