summaryrefslogtreecommitdiffstats
path: root/webkit/glue/editor_client_impl.cc
blob: a5ec8de58189f44638a6f4dd341fe2ad36eb3ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// The Mac interface forwards most of these commands to the application layer,
// and I'm not really sure what to do about most of them.

#include "config.h"
#pragma warning(push, 0)
#include "Document.h"
#include "EditCommand.h"
#include "Editor.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "HTMLInputElement.h"
#include "Frame.h"
#include "KeyboardEvent.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformString.h"
#pragma warning(pop)

#undef LOG

#include "base/string_util.h"
#include "webkit/glue/editor_client_impl.h"
#include "webkit/glue/glue_util.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webview.h"
#include "webkit/glue/webview_impl.h"

// The notImplemented() from NotImplemented.h is now being dragged in via
// webview_impl.h.  We want the one from LogWin.h instead.
#undef notImplemented
#include "LogWin.h"

// Arbitrary depth limit for the undo stack, to keep it from using
// unbounded memory.  This is the maximum number of distinct undoable
// actions -- unbroken stretches of typed characters are coalesced
// into a single action.
static const int kMaximumUndoStackDepth = 1000;

namespace {

// Record an editor command from the keyDownEntries[] below.  We ignore the
// Move* and Insert* commands because they're not that interesting.
void MaybeRecordCommand(WebViewDelegate* d, const char* command_name) {
  if (!d)
    return;

  const char* move_prefix = "Move";
  const char* insert_prefix = "Insert";
  const char* delete_prefix = "Delete";
  // Ignore all the Move*, Insert*, and Delete* commands.
  if (0 == strncmp(command_name, move_prefix, sizeof(move_prefix)) ||
      0 == strncmp(command_name, insert_prefix, sizeof(insert_prefix)) ||
      0 == strncmp(command_name, delete_prefix, sizeof(delete_prefix))) {
    return;
  }
  d->UserMetricsRecordComputedAction(UTF8ToWide(command_name));
}

}

EditorClientImpl::EditorClientImpl(WebView* web_view)
    : web_view_(static_cast<WebViewImpl*>(web_view)),
      use_editor_delegate_(false),
      in_redo_(false),
      preserve_(false),
      pending_inline_autocompleted_element_(NULL) {
}

EditorClientImpl::~EditorClientImpl() {
}

void EditorClientImpl::pageDestroyed() {
  // Called by the Page (which owns the editor client) when the page is going
  // away. This should cause us to delete ourselves, which is stupid. The page
  // should just delete us when it's going away. Oh well.
  delete this;
}

bool EditorClientImpl::shouldShowDeleteInterface(WebCore::HTMLElement* elem) {
  // Normally, we don't care to show WebCore's deletion UI, so we only enable
  // it if in testing mode and the test specifically requests it by using this
  // magic class name.
  return webkit_glue::IsLayoutTestMode() &&
         elem->className() == "needsDeletionUI";
}

bool EditorClientImpl::smartInsertDeleteEnabled() {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      return d->SmartInsertDeleteEnabled();
  }
  return true;
}

bool EditorClientImpl::isContinuousSpellCheckingEnabled() {
  // Spell check everything if possible.
  // FIXME(brettw) This should be modified to do reasonable defaults depending
  // on input type, and probably also allow the user to turn spellchecking on
  // for individual fields.
  return true;
}

void EditorClientImpl::toggleContinuousSpellChecking() {
  notImplemented();
}

bool EditorClientImpl::isGrammarCheckingEnabled() {
  notImplemented();
  return false;
}

void EditorClientImpl::toggleGrammarChecking() {
  notImplemented();
}

int EditorClientImpl::spellCheckerDocumentTag() {
  notImplemented();
  return 0;
}

bool EditorClientImpl::isEditable() {
  notImplemented();
  return false;
}

bool EditorClientImpl::shouldBeginEditing(WebCore::Range* range) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      return d->ShouldBeginEditing(web_view_, Describe(range));
  }
  return true;
}

bool EditorClientImpl::shouldEndEditing(WebCore::Range* range) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      return d->ShouldEndEditing(web_view_, Describe(range));
  }
  return true;
}

bool EditorClientImpl::shouldInsertNode(WebCore::Node* node,
                                        WebCore::Range* range,
                                        WebCore::EditorInsertAction action) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d) {
      return d->ShouldInsertNode(web_view_,
                                 Describe(node),
                                 Describe(range),
                                 Describe(action));
    }
  }
  return true;
}

bool EditorClientImpl::shouldInsertText(WebCore::String text,
                                        WebCore::Range* range,
                                        WebCore::EditorInsertAction action) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d) {
      std::wstring wstr = webkit_glue::StringToStdWString(text);
      return d->ShouldInsertText(web_view_, 
                                 wstr, 
                                 Describe(range), 
                                 Describe(action));
    }
  }
  return true;
}


bool EditorClientImpl::shouldDeleteRange(WebCore::Range* range) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      return d->ShouldDeleteRange(web_view_, Describe(range));
  }
  return true;
}

void EditorClientImpl::PreserveSelection() {
  preserve_ = true;
}

bool EditorClientImpl::shouldChangeSelectedRange(WebCore::Range* fromRange, 
                                                 WebCore::Range* toRange, 
                                                 WebCore::EAffinity affinity, 
                                                 bool stillSelecting) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d) {
      return d->ShouldChangeSelectedRange(web_view_, 
                                          Describe(fromRange), 
                                          Describe(toRange), 
                                          Describe(affinity), 
                                          stillSelecting);
    }
  }
  // Have we been told to preserve the selection? 
  // (See comments for PreserveSelection in header).
  if (preserve_) {
    preserve_ = false;
    return false;
  }
  return true;
}
                                                
bool EditorClientImpl::shouldApplyStyle(WebCore::CSSStyleDeclaration* style,
                                        WebCore::Range* range) {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      return d->ShouldApplyStyle(web_view_, Describe(style), Describe(range));
  }
  return true;
}

bool EditorClientImpl::shouldMoveRangeAfterDelete(
    WebCore::Range* /*range*/,
    WebCore::Range* /*rangeToBeReplaced*/) {
  notImplemented();
  return true;
}

void EditorClientImpl::didBeginEditing() {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      d->DidBeginEditing();
  }
}

void EditorClientImpl::respondToChangedSelection() {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      d->DidChangeSelection();
  }
}

void EditorClientImpl::respondToChangedContents() {
  // Ugly Hack. (See also webkit bug #16976).
  // Something is wrong with webcore's focusController in that when selection
  // is set to a region within a text element when handling an input event, if
  // you don't re-focus the node then it only _APPEARS_ to have successfully
  // changed the selection (the UI "looks" right) but in reality there is no
  // selection of text. And to make matters worse, you can't just re-focus it,
  // you have to re-focus it in code executed after the entire event listener 
  // loop has finished; and hence here we are. Oh, and to make matters worse, 
  // this sequence of events _doesn't_ happen when you debug through the code 
  // -- in that case it works perfectly fine -- because swapping to the debugger 
  // causes the refocusing we artificially reproduce here.
  // TODO (timsteele): Clean this up once root webkit problem is identified and
  // the bug is patched. 
  if (pending_inline_autocompleted_element_) {
    pending_inline_autocompleted_element_->blur();
    pending_inline_autocompleted_element_->focus();
    pending_inline_autocompleted_element_ = NULL; 
  }

  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      d->DidChangeContents();
  }
}

void EditorClientImpl::didEndEditing() {
  if (use_editor_delegate_) {
    WebViewDelegate* d = web_view_->delegate();
    if (d)
      d->DidEndEditing();
  }
}

void EditorClientImpl::didWriteSelectionToPasteboard() {
  notImplemented();
}

void EditorClientImpl::didSetSelectionTypesForPasteboard() {
  notImplemented();
}

void EditorClientImpl::registerCommandForUndo(
    PassRefPtr<WebCore::EditCommand> command) {
  if (undo_stack_.size() == kMaximumUndoStackDepth)
    undo_stack_.pop_front();  // drop oldest item off the far end
  if (!in_redo_)
    redo_stack_.clear();
  undo_stack_.push_back(command);
}

void EditorClientImpl::registerCommandForRedo(
    PassRefPtr<WebCore::EditCommand> command) {
  redo_stack_.push_back(command);
}

void EditorClientImpl::clearUndoRedoOperations() {
  undo_stack_.clear();
  redo_stack_.clear();
}

bool EditorClientImpl::canUndo() const {
  return !undo_stack_.empty();
}

bool EditorClientImpl::canRedo() const {
  return !redo_stack_.empty();
}

void EditorClientImpl::undo() {
  if (canUndo()) {
    RefPtr<WebCore::EditCommand> command(undo_stack_.back());
    undo_stack_.pop_back();
    command->unapply();
    // unapply will call us back to push this command onto the redo stack.
  }
}

void EditorClientImpl::redo() {
  if (canRedo()) {
    RefPtr<WebCore::EditCommand> command(redo_stack_.back());
    redo_stack_.pop_back();

    ASSERT(!in_redo_);
    in_redo_ = true;
    command->reapply();
    // reapply will call us back to push this command onto the undo stack.
    in_redo_ = false;
  }
}

// Get the distance we should go (in pixels) when doing a pageup/pagedown.
static int GetVerticalPageDistance(WebCore::KeyboardEvent* event) {
  if (event->target()) {
    WebCore::Node* node = event->target()->toNode();
    if (node && node->renderer())
      return node->renderer()->contentHeight();
  }

  return 0;
}

// The below code was adapted from the WebKit file webview.cpp
// provided by Apple, Inc, and is subject to the following copyright
// notice and disclaimer.

/*
 * Copyright (C) 2006, 2007 Apple, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

static const unsigned CtrlKey = 1 << 0;
static const unsigned AltKey = 1 << 1;
static const unsigned ShiftKey = 1 << 2;

// Keys with special meaning. These will be delegated to the editor using
// the execCommand() method
struct KeyDownEntry {
  unsigned virtualKey;
  unsigned modifiers;
  const char* name;
};

struct KeyPressEntry {
  unsigned charCode;
  unsigned modifiers;
  const char* name;
};

static const KeyDownEntry keyDownEntries[] = {
  { VK_LEFT,   0,                  "MoveLeft"                                    },
  { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection"                  },
  { VK_LEFT,   CtrlKey,            "MoveWordLeft"                                },
  { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"              },
  { VK_RIGHT,  0,                  "MoveRight"                                   },
  { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection"                 },
  { VK_RIGHT,  CtrlKey,            "MoveWordRight"                               },
  { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection"             },
  { VK_UP,     0,                  "MoveUp"                                      },
  { VK_UP,     ShiftKey,           "MoveUpAndModifySelection"                    },
  { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection"                },
  { VK_DOWN,   0,                  "MoveDown"                                    },
  { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection"                  },
  { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection"              },
  { VK_PRIOR,  0,                  "MovePageUp"                                  },
  { VK_NEXT,   0,                  "MovePageDown"                                },
  { VK_HOME,   0,                  "MoveToBeginningOfLine"                       },
  { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection"     },
  { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument"                   },
  { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
	
  { VK_END,    0,                  "MoveToEndOfLine"                             },
  { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection"           },
  { VK_END,    CtrlKey,            "MoveToEndOfDocument"                         },
  { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection"       },
	
  { VK_BACK,   0,                  "DeleteBackward"                              },
  { VK_BACK,   ShiftKey,           "DeleteBackward"                              },
  { VK_DELETE, 0,                  "DeleteForward"                               },
  { VK_BACK,   CtrlKey,            "DeleteWordBackward"                          },
  { VK_DELETE, CtrlKey,            "DeleteWordForward"                           },
	   
  { 'B',       CtrlKey,            "ToggleBold"                                  },
  { 'I',       CtrlKey,            "ToggleItalic"                                },
  { 'U',       CtrlKey,            "ToggleUnderline"                             },

  { VK_ESCAPE, 0,                  "Cancel"                                      },
  { VK_OEM_PERIOD, CtrlKey,        "Cancel"                                      },
  { VK_TAB,    0,                  "InsertTab"                                   },
  { VK_TAB,    ShiftKey,           "InsertBacktab"                               },
  { VK_RETURN, 0,                  "InsertNewline"                               },
  { VK_RETURN, CtrlKey,            "InsertNewline"                               },
  { VK_RETURN, AltKey,             "InsertNewline"                               },
  { VK_RETURN, AltKey | ShiftKey,  "InsertNewline"                               },
  { VK_RETURN, ShiftKey,           "InsertLineBreak"                             },	

  { 'C',       CtrlKey,            "Copy"                                        },
  { VK_INSERT, CtrlKey,            "Copy"                                        },	
  { 'V',       CtrlKey,            "Paste"                                       },
  { VK_INSERT, ShiftKey,           "Paste"                                       },	
  { 'V',       CtrlKey | ShiftKey, "PasteAndMatchStyle"                          },
  { 'X',       CtrlKey,            "Cut"                                         },
  { VK_DELETE, ShiftKey,           "Cut"                                         },	
  { 'A',       CtrlKey,            "SelectAll"                                   },
  { 'Z',       CtrlKey,            "Undo"                                        },
  { 'Z',       CtrlKey | ShiftKey, "Redo"                                        },
  { 'Y',       CtrlKey,            "Redo"                                        },
};

static const KeyPressEntry keyPressEntries[] = {
  { '\t',   0,                  "InsertTab"                                   },
  { '\t',   ShiftKey,           "InsertBacktab"                               },
  { '\r',   0,                  "InsertNewline"                               },
  { '\r',   CtrlKey,            "InsertNewline"                               },
  { '\r',   ShiftKey,           "InsertLineBreak"                             },
  { '\r',   AltKey,             "InsertNewline"                               },
  { '\r',   AltKey | ShiftKey,  "InsertNewline"                               },
};

const char* EditorClientImpl::interpretKeyEvent(
  const WebCore::KeyboardEvent* evt) {
  const WebCore::PlatformKeyboardEvent* keyEvent = evt->keyEvent();
  if (!keyEvent)
    return "";

  static HashMap<int, const char*>* keyDownCommandsMap = 0;
  static HashMap<int, const char*>* keyPressCommandsMap = 0;

  if (!keyDownCommandsMap) {
    keyDownCommandsMap = new HashMap<int, const char*>;
    keyPressCommandsMap = new HashMap<int, const char*>;

    for (unsigned i = 0; i < _countof(keyDownEntries); i++) {
      keyDownCommandsMap->set(
        keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey,
        keyDownEntries[i].name);
    }

    for (unsigned i = 0; i < _countof(keyPressEntries); i++) {
      keyPressCommandsMap->set(
        keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, 
        keyPressEntries[i].name);
    }
  }

  unsigned modifiers = 0;
  if (keyEvent->shiftKey())
    modifiers |= ShiftKey;
  if (keyEvent->altKey())
    modifiers |= AltKey;
  if (keyEvent->ctrlKey())
    modifiers |= CtrlKey;

  if (evt->type() == WebCore::EventNames::keydownEvent) {
    int mapKey = modifiers << 16 | evt->keyCode();
      return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
  }

  int mapKey = modifiers << 16 | evt->charCode();
  return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
}

bool EditorClientImpl::handleEditingKeyboardEvent(
  WebCore::KeyboardEvent* evt) {
  const WebCore::PlatformKeyboardEvent* keyEvent = evt->keyEvent();
  // do not treat this as text input if it's a system key event
  if (!keyEvent || keyEvent->isSystemKey())
      return false;

  WebCore::Frame* frame = evt->target()->toNode()->document()->frame();
  if (!frame)
    return false;

  const char* command_name = interpretKeyEvent(evt);
  WebCore::Editor::Command command = frame->editor()->command(command_name);

  if (keyEvent->type() == WebCore::PlatformKeyboardEvent::RawKeyDown) {
    // WebKit doesn't have enough information about mode to decide how
    // commands that just insert text if executed via Editor should be treated,
    // so we leave it upon WebCore to either handle them immediately
    // (e.g. Tab that changes focus) or let a keypress event be generated
    // (e.g. Tab that inserts a Tab character, or Enter).
    if (command.isTextInsertion() || !command_name)
      return false;
    if (command.execute(evt)) {
      WebViewDelegate* d = web_view_->delegate();
      MaybeRecordCommand(d, command_name);
      return true;
    }
    return false;
  }

  if (command.execute(evt)) {
    WebViewDelegate* d = web_view_->delegate();
    MaybeRecordCommand(d, command_name);
    return true;
  }

  if (evt->keyEvent()->text().length() == 1) {
    UChar ch = evt->keyEvent()->text()[0];

    // Don't insert null or control characters as they can result in
    // unexpected behaviour
    if (ch < ' ')
      return false;
  }

  return frame->editor()->insertText(evt->keyEvent()->text(), evt);
}

//
// End of code block subject to Apple, Inc. copyright
//

void EditorClientImpl::handleKeyboardEvent(WebCore::KeyboardEvent* evt) {
  if (handleEditingKeyboardEvent(evt))
    evt->setDefaultHandled();
}

void EditorClientImpl::handleInputMethodKeydown(WebCore::KeyboardEvent* keyEvent) {
  notImplemented();
}

void EditorClientImpl::textFieldDidBeginEditing(WebCore::Element*) {
  notImplemented();
}

void EditorClientImpl::textFieldDidEndEditing(WebCore::Element*) {
  // Notification that focus was lost.
  // Be careful with this, it's also sent when the page is being closed.
  notImplemented();
}

void EditorClientImpl::textDidChangeInTextField(WebCore::Element* element) {
  // Track the element so we can blur/focus it in respondToChangedContents
  // so that the selected range is properly set. (See respondToChangedContents).
  if (static_cast<WebCore::HTMLInputElement*>(element)->autofilled())
    pending_inline_autocompleted_element_ = element;
}

bool EditorClientImpl::doTextFieldCommandFromEvent(WebCore::Element*,
                                                   WebCore::KeyboardEvent*) {
  // The Mac code appears to use this method as a hook to implement special
  // keyboard commands specific to Safari's auto-fill implementation.  We
  // just return false to allow the default action.
  return false;
}

void EditorClientImpl::textWillBeDeletedInTextField(WebCore::Element*) {
  notImplemented();
}

void EditorClientImpl::textDidChangeInTextArea(WebCore::Element*) {
  notImplemented();
}

void EditorClientImpl::ignoreWordInSpellDocument(const WebCore::String&) {
  notImplemented();
}

void EditorClientImpl::learnWord(const WebCore::String&) {
  notImplemented();
}

void EditorClientImpl::checkSpellingOfString(const UChar* str, int length,
                                             int* misspellingLocation,
                                             int* misspellingLength) {
  // SpellCheckWord will write (0, 0) into the output vars, which is what our
  // caller expects if the word is spelled correctly.
  int spell_location = -1;
  int spell_length = 0;
  WebViewDelegate* d = web_view_->delegate();
  if (web_view_->FocusedFrameNeedsSpellchecking() && d) {
    std::wstring word(str, length);
    d->SpellCheck(word, spell_location, spell_length);
  } else {
    spell_location = 0;
    spell_length = 0;
  }

  // Note: the Mac code checks if the pointers are NULL before writing to them,
  // so we do too.
  if (misspellingLocation)
    *misspellingLocation = spell_location;
  if (misspellingLength)
    *misspellingLength = spell_length;
}

void EditorClientImpl::checkGrammarOfString(const UChar*, int length,
                                            WTF::Vector<WebCore::GrammarDetail>&,
                                            int* badGrammarLocation,
                                            int* badGrammarLength) {
  notImplemented();
  if (badGrammarLocation)
    *badGrammarLocation = 0;
  if (badGrammarLength)
    *badGrammarLength = 0;
}

void EditorClientImpl::updateSpellingUIWithGrammarString(const WebCore::String&,
                                                         const WebCore::GrammarDetail& detail) {
  notImplemented();
}

void EditorClientImpl::updateSpellingUIWithMisspelledWord(const WebCore::String&) {
  notImplemented();
}

void EditorClientImpl::showSpellingUI(bool show) {
  notImplemented();
}

bool EditorClientImpl::spellingUIIsShowing() {
  return false;
}

void EditorClientImpl::getGuessesForWord(const WebCore::String&,
                                         WTF::Vector<WebCore::String>& guesses) {
  notImplemented();
}

void EditorClientImpl::setInputMethodState(bool enabled) {
  WebViewDelegate* d = web_view_->delegate();
  if (d) {
    d->SetInputMethodState(enabled);
  }
}


std::wstring EditorClientImpl::DescribeOrError(int number, 
                                               WebCore::ExceptionCode ec) {
  if (ec)
    return L"ERROR";

  wchar_t buffer[128];
  _itow_s(number, buffer, arraysize(buffer), 10);
  return std::wstring(buffer);
}

std::wstring EditorClientImpl::DescribeOrError(WebCore::Node* node, 
                                               WebCore::ExceptionCode ec) {
  if (ec)
    return L"ERROR";

  return Describe(node);
}

// These Describe() functions match the output expected by the layout tests.
std::wstring EditorClientImpl::Describe(WebCore::Range* range) {
  if (range) {
    WebCore::ExceptionCode exception = 0;
    std::wstring str = L"range from ";
    int offset = range->startOffset(exception);
    str.append(DescribeOrError(offset, exception));
    str.append(L" of ");
    WebCore::Node* container = range->startContainer(exception);
    str.append(DescribeOrError(container, exception));
    str.append(L" to ");
    offset = range->endOffset(exception);
    str.append(DescribeOrError(offset, exception));
    str.append(L" of ");
    container = range->endContainer(exception);
    str.append(DescribeOrError(container, exception));
    return str;
  }
  return L"(null)";
}

// See comment for Describe(), above.
std::wstring EditorClientImpl::Describe(WebCore::Node* node) {
  if (node) {
    std::wstring str = webkit_glue::StringToStdWString(node->nodeName());
    WebCore::Node* parent = node->parentNode();
    if (parent) {
      str.append(L" > ");
      str.append(Describe(parent));
    }
    return str;
  }
  return L"(null)";
}

// See comment for Describe(), above.
std::wstring EditorClientImpl::Describe(WebCore::EditorInsertAction action) {
  switch (action) {
    case WebCore::EditorInsertActionTyped:
      return L"WebViewInsertActionTyped";
    case WebCore::EditorInsertActionPasted:
      return L"WebViewInsertActionPasted";
    case WebCore::EditorInsertActionDropped:
      return L"WebViewInsertActionDropped";
  }
  return L"(UNKNOWN ACTION)";
}

// See comment for Describe(), above.
std::wstring EditorClientImpl::Describe(WebCore::EAffinity affinity) {
  switch (affinity) {
    case WebCore::UPSTREAM:
      return L"NSSelectionAffinityUpstream";
    case WebCore::DOWNSTREAM:
      return L"NSSelectionAffinityDownstream";
  }
  return L"(UNKNOWN AFFINITY)";
}

std::wstring EditorClientImpl::Describe(WebCore::CSSStyleDeclaration* style) {
  // TODO(pamg): Implement me.  It's not clear what WebKit produces for this
  // (their [style description] method), and none of the layout tests provide
  // an example.  But because none of them use it, it's not yet important.
  return std::wstring();
}