a9af79c0d9d5d7d4486944c20204a3b5f959802d
[WebKit-https.git] / WebCore / editing / Editor.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Trolltech ASA
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "Editor.h"
29
30 #include "AXObjectCache.h"
31 #include "ApplyStyleCommand.h"
32 #include "CSSComputedStyleDeclaration.h"
33 #include "CSSProperty.h"
34 #include "CSSPropertyNames.h"
35 #include "ClipboardEvent.h"
36 #include "DeleteButtonController.h"
37 #include "DeleteSelectionCommand.h"
38 #include "DocLoader.h"
39 #include "DocumentFragment.h"
40 #include "EditorClient.h"
41 #include "EventHandler.h"
42 #include "EventNames.h"
43 #include "FocusController.h"
44 #include "Frame.h"
45 #include "FrameView.h"
46 #include "HTMLInputElement.h"
47 #include "HTMLTextAreaElement.h"
48 #include "HitTestResult.h"
49 #include "IndentOutdentCommand.h"
50 #include "InsertListCommand.h"
51 #include "KeyboardEvent.h"
52 #include "ModifySelectionListLevel.h"
53 #include "Page.h"
54 #include "Pasteboard.h"
55 #include "RemoveFormatCommand.h"
56 #include "ReplaceSelectionCommand.h"
57 #include "Sound.h"
58 #include "Text.h"
59 #include "TextIterator.h"
60 #include "TypingCommand.h"
61 #include "htmlediting.h"
62 #include "markup.h"
63 #include "visible_units.h"
64
65 namespace WebCore {
66
67 using namespace std;
68 using namespace EventNames;
69 using namespace HTMLNames;
70
71 // When an event handler has moved the selection outside of a text control
72 // we should use the target control's selection for this editing operation.
73 Selection Editor::selectionForCommand(Event* event)
74 {
75     Selection selection = m_frame->selectionController()->selection();
76     if (!event)
77         return selection;
78     // If the target is a text control, and the current selection is outside of its shadow tree,
79     // then use the saved selection for that text control.
80     Node* target = event->target()->toNode();
81     Node* selectionStart = selection.start().node();
82     if (target && (!selectionStart || target->shadowAncestorNode() != selectionStart->shadowAncestorNode())) {
83         if (target->hasTagName(inputTag) && static_cast<HTMLInputElement*>(target)->isTextField())
84             return static_cast<HTMLInputElement*>(target)->selection();
85         if (target->hasTagName(textareaTag))
86             return static_cast<HTMLTextAreaElement*>(target)->selection();
87     }
88     return selection;
89 }
90
91 EditorClient* Editor::client() const
92 {
93     if (Page* page = m_frame->page())
94         return page->editorClient();
95     return 0;
96 }
97
98 void Editor::handleKeyboardEvent(KeyboardEvent* event)
99 {
100     if (EditorClient* c = client())
101         if (selectionForCommand(event).isContentEditable())
102             c->handleKeyboardEvent(event);
103 }
104
105 void Editor::handleInputMethodKeydown(KeyboardEvent* event)
106 {
107     if (EditorClient* c = client())
108         if (selectionForCommand(event).isContentEditable())
109             c->handleInputMethodKeydown(event);
110 }
111
112 bool Editor::canEdit() const
113 {
114     return m_frame->selectionController()->isContentEditable();
115 }
116
117 bool Editor::canEditRichly() const
118 {
119     return m_frame->selectionController()->isContentRichlyEditable();
120 }
121
122 // WinIE uses onbeforecut and onbeforepaste to enables the cut and paste menu items.  They
123 // also send onbeforecopy, apparently for symmetry, but it doesn't affect the menu items.
124 // We need to use onbeforecopy as a real menu enabler because we allow elements that are not
125 // normally selectable to implement copy/paste (like divs, or a document body).
126
127 bool Editor::canDHTMLCut()
128 {
129     return !m_frame->selectionController()->isInPasswordField() && !dispatchCPPEvent(beforecutEvent, ClipboardNumb);
130 }
131
132 bool Editor::canDHTMLCopy()
133 {
134     return !m_frame->selectionController()->isInPasswordField() && !dispatchCPPEvent(beforecopyEvent, ClipboardNumb);
135 }
136
137 bool Editor::canDHTMLPaste()
138 {
139     return !dispatchCPPEvent(beforepasteEvent, ClipboardNumb);
140 }
141
142 bool Editor::canCut() const
143 {
144     return canCopy() && canDelete();
145 }
146
147 static HTMLImageElement* imageElementFromImageDocument(Document* document)
148 {
149     if (!document)
150         return 0;
151     if (!document->isImageDocument())
152         return 0;
153     
154     HTMLElement* body = document->body();
155     if (!body)
156         return 0;
157     
158     Node* node = body->firstChild();
159     if (!node)
160         return 0;    
161     if (!node->hasTagName(imgTag))
162         return 0;
163     return static_cast<HTMLImageElement*>(node);
164 }
165
166 bool Editor::canCopy() const
167 {
168     if (imageElementFromImageDocument(m_frame->document()))
169         return true;
170     SelectionController* selectionController = m_frame->selectionController();
171     return selectionController->isRange() && !selectionController->isInPasswordField();
172 }
173
174 bool Editor::canPaste() const
175 {
176     return canEdit();
177 }
178
179 bool Editor::canDelete() const
180 {
181     SelectionController* selectionController = m_frame->selectionController();
182     return selectionController->isRange() && selectionController->isContentEditable();
183 }
184
185 bool Editor::canDeleteRange(Range* range) const
186 {
187     ExceptionCode ec = 0;
188     Node* startContainer = range->startContainer(ec);
189     Node* endContainer = range->endContainer(ec);
190     if (!startContainer || !endContainer)
191         return false;
192     
193     if (!startContainer->isContentEditable() || !endContainer->isContentEditable())
194         return false;
195     
196     if (range->collapsed(ec)) {
197         VisiblePosition start(startContainer, range->startOffset(ec), DOWNSTREAM);
198         VisiblePosition previous = start.previous();
199         // FIXME: We sometimes allow deletions at the start of editable roots, like when the caret is in an empty list item.
200         if (previous.isNull() || previous.deepEquivalent().node()->rootEditableElement() != startContainer->rootEditableElement())
201             return false;
202     }
203     return true;
204 }
205
206 bool Editor::smartInsertDeleteEnabled()
207 {   
208     return client() && client()->smartInsertDeleteEnabled();
209 }
210     
211 bool Editor::canSmartCopyOrDelete()
212 {
213     return client() && client()->smartInsertDeleteEnabled() && m_frame->selectionGranularity() == WordGranularity;
214 }
215
216 bool Editor::deleteWithDirection(SelectionController::EDirection direction, TextGranularity granularity, bool killRing, bool isTypingAction)
217 {
218     // Delete the selection, if there is one.
219     // If not, make a selection using the passed-in direction and granularity.
220
221     if (!canEdit())
222         return false;
223
224     if (m_frame->selectionController()->isRange()) {
225         if (killRing)
226             addToKillRing(selectedRange().get(), false);
227         if (isTypingAction) {
228             if (m_frame->document()) {
229                 TypingCommand::deleteKeyPressed(m_frame->document(), canSmartCopyOrDelete(), granularity);
230                 revealSelectionAfterEditingOperation();
231             }
232         } else {
233             deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
234             // Implicitly calls revealSelectionAfterEditingOperation().
235         }
236     } else {
237         SelectionController selectionToDelete;
238         selectionToDelete.setSelection(m_frame->selectionController()->selection());
239         selectionToDelete.modify(SelectionController::EXTEND, direction, granularity);
240         if (killRing && selectionToDelete.isCaret() && granularity != CharacterGranularity)
241             selectionToDelete.modify(SelectionController::EXTEND, direction, CharacterGranularity);
242
243         RefPtr<Range> range = selectionToDelete.toRange();
244
245         if (killRing)
246             addToKillRing(range.get(), false);
247
248         if (!m_frame->selectionController()->setSelectedRange(range.get(), DOWNSTREAM, (granularity != CharacterGranularity)))
249             return true;
250
251         switch (direction) {
252             case SelectionController::FORWARD:
253             case SelectionController::RIGHT:
254                 if (m_frame->document())
255                     TypingCommand::forwardDeleteKeyPressed(m_frame->document(), false, granularity);
256                 break;
257             case SelectionController::BACKWARD:
258             case SelectionController::LEFT:
259                 if (m_frame->document())
260                     TypingCommand::deleteKeyPressed(m_frame->document(), false, granularity);
261                 break;
262         }
263         revealSelectionAfterEditingOperation();
264     }
265
266     // clear the "start new kill ring sequence" setting, because it was set to true
267     // when the selection was updated by deleting the range
268     if (killRing)
269         setStartNewKillRingSequence(false);
270
271     return true;
272 }
273
274 void Editor::deleteSelectionWithSmartDelete(bool smartDelete)
275 {
276     if (m_frame->selectionController()->isNone())
277         return;
278     
279     applyCommand(new DeleteSelectionCommand(m_frame->document(), smartDelete));
280 }
281
282 void Editor::pasteAsPlainTextWithPasteboard(Pasteboard* pasteboard)
283 {
284     String text = pasteboard->plainText(m_frame);
285     if (client() && client()->shouldInsertText(text, selectedRange().get(), EditorInsertActionPasted))
286         replaceSelectionWithText(text, false, canSmartReplaceWithPasteboard(pasteboard));
287 }
288
289 void Editor::pasteWithPasteboard(Pasteboard* pasteboard, bool allowPlainText)
290 {
291     RefPtr<Range> range = selectedRange();
292     bool chosePlainText;
293     RefPtr<DocumentFragment> fragment = pasteboard->documentFragment(m_frame, range, allowPlainText, chosePlainText);
294     if (fragment && shouldInsertFragment(fragment, range, EditorInsertActionPasted))
295         replaceSelectionWithFragment(fragment, false, canSmartReplaceWithPasteboard(pasteboard), chosePlainText);
296 }
297
298 bool Editor::canSmartReplaceWithPasteboard(Pasteboard* pasteboard)
299 {
300     return client() && client()->smartInsertDeleteEnabled() && pasteboard->canSmartReplace();
301 }
302
303 bool Editor::shouldInsertFragment(PassRefPtr<DocumentFragment> fragment, PassRefPtr<Range> replacingDOMRange, EditorInsertAction givenAction)
304 {
305     if (!client())
306         return false;
307         
308     Node* child = fragment->firstChild();
309     if (child && fragment->lastChild() == child && child->isCharacterDataNode())
310         return client()->shouldInsertText(static_cast<CharacterData*>(child)->data(), replacingDOMRange.get(), givenAction);
311
312     return client()->shouldInsertNode(fragment.get(), replacingDOMRange.get(), givenAction);
313 }
314
315 void Editor::replaceSelectionWithFragment(PassRefPtr<DocumentFragment> fragment, bool selectReplacement, bool smartReplace, bool matchStyle)
316 {
317     if (m_frame->selectionController()->isNone() || !fragment)
318         return;
319     
320     applyCommand(new ReplaceSelectionCommand(m_frame->document(), fragment, selectReplacement, smartReplace, matchStyle));
321     revealSelectionAfterEditingOperation();
322 }
323
324 void Editor::replaceSelectionWithText(const String& text, bool selectReplacement, bool smartReplace)
325 {
326     replaceSelectionWithFragment(createFragmentFromText(selectedRange().get(), text), selectReplacement, smartReplace, true); 
327 }
328
329 PassRefPtr<Range> Editor::selectedRange()
330 {
331     if (!m_frame)
332         return 0;
333     return m_frame->selectionController()->toRange();
334 }
335
336 bool Editor::shouldDeleteRange(Range* range) const
337 {
338     ExceptionCode ec;
339     if (!range || range->collapsed(ec))
340         return false;
341     
342     if (!canDeleteRange(range))
343         return false;
344
345     return client() && client()->shouldDeleteRange(range);
346 }
347
348 bool Editor::tryDHTMLCopy()
349 {   
350     if (m_frame->selectionController()->isInPasswordField())
351         return false;
352
353     // Must be done before oncopy adds types and data to the pboard,
354     // also done for security, as it erases data from the last copy/paste.
355     Pasteboard::generalPasteboard()->clear();
356
357     return !dispatchCPPEvent(copyEvent, ClipboardWritable);
358 }
359
360 bool Editor::tryDHTMLCut()
361 {
362     if (m_frame->selectionController()->isInPasswordField())
363         return false;
364
365     // Must be done before oncut adds types and data to the pboard,
366     // also done for security, as it erases data from the last copy/paste.
367     Pasteboard::generalPasteboard()->clear();
368
369     return !dispatchCPPEvent(cutEvent, ClipboardWritable);
370 }
371
372 bool Editor::tryDHTMLPaste()
373 {
374     return !dispatchCPPEvent(pasteEvent, ClipboardReadable);
375 }
376
377 void Editor::writeSelectionToPasteboard(Pasteboard* pasteboard)
378 {
379     pasteboard->writeSelection(selectedRange().get(), canSmartCopyOrDelete(), m_frame);
380 }
381
382 bool Editor::shouldInsertText(const String& text, Range* range, EditorInsertAction action) const
383 {
384     return client() && client()->shouldInsertText(text, range, action);
385 }
386
387 bool Editor::shouldShowDeleteInterface(HTMLElement* element) const
388 {
389     return client() && client()->shouldShowDeleteInterface(element);
390 }
391
392 void Editor::respondToChangedSelection(const Selection& oldSelection)
393 {
394     if (client())
395         client()->respondToChangedSelection();
396     m_deleteButtonController->respondToChangedSelection(oldSelection);
397 }
398
399 void Editor::respondToChangedContents(const Selection& endingSelection)
400 {
401     if (AXObjectCache::accessibilityEnabled()) {
402         Node* node = endingSelection.start().node();
403         if (node)
404             m_frame->renderer()->document()->axObjectCache()->postNotification(node->renderer(), "AXValueChanged");
405     }
406     
407     if (client())
408         client()->respondToChangedContents();  
409 }
410
411 const SimpleFontData* Editor::fontForSelection(bool& hasMultipleFonts) const
412 {
413 #if !PLATFORM(QT)
414     hasMultipleFonts = false;
415
416     if (!m_frame->selectionController()->isRange()) {
417         Node* nodeToRemove;
418         RenderStyle* style = m_frame->styleForSelectionStart(nodeToRemove); // sets nodeToRemove
419
420         const SimpleFontData* result = 0;
421         if (style)
422             result = style->font().primaryFont();
423         
424         if (nodeToRemove) {
425             ExceptionCode ec;
426             nodeToRemove->remove(ec);
427             ASSERT(ec == 0);
428         }
429
430         return result;
431     }
432
433     const SimpleFontData* font = 0;
434
435     RefPtr<Range> range = m_frame->selectionController()->toRange();
436     Node* startNode = range->editingStartPosition().node();
437     if (startNode) {
438         Node* pastEnd = range->pastEndNode();
439         // In the loop below, n should eventually match pastEnd and not become nil, but we've seen at least one
440         // unreproducible case where this didn't happen, so check for nil also.
441         for (Node* n = startNode; n && n != pastEnd; n = n->traverseNextNode()) {
442             RenderObject *renderer = n->renderer();
443             if (!renderer)
444                 continue;
445             // FIXME: Are there any node types that have renderers, but that we should be skipping?
446             const SimpleFontData* f = renderer->style()->font().primaryFont();
447             if (!font)
448                 font = f;
449             else if (font != f) {
450                 hasMultipleFonts = true;
451                 break;
452             }
453         }
454     }
455
456     return font;
457 #else
458     return 0;
459 #endif
460 }
461
462 TriState Editor::selectionUnorderedListState() const
463 {
464     if (m_frame->selectionController()->isCaret()) {
465         if (enclosingNodeWithTag(m_frame->selectionController()->selection().start(), ulTag))
466             return TrueTriState;
467     } else if (m_frame->selectionController()->isRange()) {
468         Node* startNode = enclosingNodeWithTag(m_frame->selectionController()->selection().start(), ulTag);
469         Node* endNode = enclosingNodeWithTag(m_frame->selectionController()->selection().end(), ulTag);
470         if (startNode && endNode && startNode == endNode)
471             return TrueTriState;
472     }
473
474     return FalseTriState;
475 }
476
477 TriState Editor::selectionOrderedListState() const
478 {
479     if (m_frame->selectionController()->isCaret()) {
480         if (enclosingNodeWithTag(m_frame->selectionController()->selection().start(), olTag))
481             return TrueTriState;
482     } else if (m_frame->selectionController()->isRange()) {
483         Node* startNode = enclosingNodeWithTag(m_frame->selectionController()->selection().start(), olTag);
484         Node* endNode = enclosingNodeWithTag(m_frame->selectionController()->selection().end(), olTag);
485         if (startNode && endNode && startNode == endNode)
486             return TrueTriState;
487     }
488
489     return FalseTriState;
490 }
491
492 PassRefPtr<Node> Editor::insertOrderedList()
493 {
494     if (!canEditRichly())
495         return 0;
496         
497     RefPtr<Node> newList = InsertListCommand::insertList(m_frame->document(), InsertListCommand::OrderedList);
498     revealSelectionAfterEditingOperation();
499     return newList;
500 }
501
502 PassRefPtr<Node> Editor::insertUnorderedList()
503 {
504     if (!canEditRichly())
505         return 0;
506         
507     RefPtr<Node> newList = InsertListCommand::insertList(m_frame->document(), InsertListCommand::UnorderedList);
508     revealSelectionAfterEditingOperation();
509     return newList;
510 }
511
512 bool Editor::canIncreaseSelectionListLevel()
513 {
514     return canEditRichly() && IncreaseSelectionListLevelCommand::canIncreaseSelectionListLevel(m_frame->document());
515 }
516
517 bool Editor::canDecreaseSelectionListLevel()
518 {
519     return canEditRichly() && DecreaseSelectionListLevelCommand::canDecreaseSelectionListLevel(m_frame->document());
520 }
521
522 PassRefPtr<Node> Editor::increaseSelectionListLevel()
523 {
524     if (!canEditRichly() || m_frame->selectionController()->isNone())
525         return 0;
526     
527     RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevel(m_frame->document());
528     revealSelectionAfterEditingOperation();
529     return newList;
530 }
531
532 PassRefPtr<Node> Editor::increaseSelectionListLevelOrdered()
533 {
534     if (!canEditRichly() || m_frame->selectionController()->isNone())
535         return 0;
536     
537     PassRefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelOrdered(m_frame->document());
538     revealSelectionAfterEditingOperation();
539     return newList;
540 }
541
542 PassRefPtr<Node> Editor::increaseSelectionListLevelUnordered()
543 {
544     if (!canEditRichly() || m_frame->selectionController()->isNone())
545         return 0;
546     
547     PassRefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelUnordered(m_frame->document());
548     revealSelectionAfterEditingOperation();
549     return newList;
550 }
551
552 void Editor::decreaseSelectionListLevel()
553 {
554     if (!canEditRichly() || m_frame->selectionController()->isNone())
555         return;
556     
557     DecreaseSelectionListLevelCommand::decreaseSelectionListLevel(m_frame->document());
558     revealSelectionAfterEditingOperation();
559 }
560
561 void Editor::removeFormattingAndStyle()
562 {
563     applyCommand(new RemoveFormatCommand(m_frame->document()));
564 }
565
566 void Editor::setLastEditCommand(PassRefPtr<EditCommand> lastEditCommand) 
567 {
568     m_lastEditCommand = lastEditCommand;
569 }
570
571 // Returns whether caller should continue with "the default processing", which is the same as 
572 // the event handler NOT setting the return value to false
573 bool Editor::dispatchCPPEvent(const AtomicString &eventType, ClipboardAccessPolicy policy)
574 {
575     Node* target = m_frame->selectionController()->start().element();
576     if (!target && m_frame->document())
577         target = m_frame->document()->body();
578     if (!target)
579         return true;
580     target = target->shadowAncestorNode();
581     
582     RefPtr<Clipboard> clipboard = newGeneralClipboard(policy);
583
584     ExceptionCode ec = 0;
585     RefPtr<Event> evt = new ClipboardEvent(eventType, true, true, clipboard.get());
586     EventTargetNodeCast(target)->dispatchEvent(evt, ec, true);
587     bool noDefaultProcessing = evt->defaultPrevented();
588
589     // invalidate clipboard here for security
590     clipboard->setAccessPolicy(ClipboardNumb);
591     
592     return !noDefaultProcessing;
593 }
594
595 void Editor::applyStyle(CSSStyleDeclaration* style, EditAction editingAction)
596 {
597     switch (m_frame->selectionController()->state()) {
598         case Selection::NONE:
599             // do nothing
600             break;
601         case Selection::CARET:
602             m_frame->computeAndSetTypingStyle(style, editingAction);
603             break;
604         case Selection::RANGE:
605             if (m_frame->document() && style)
606                 applyCommand(new ApplyStyleCommand(m_frame->document(), style, editingAction));
607             break;
608     }
609 }
610     
611 bool Editor::shouldApplyStyle(CSSStyleDeclaration* style, Range* range)
612 {   
613     return client()->shouldApplyStyle(style, range);
614 }
615     
616 void Editor::applyParagraphStyle(CSSStyleDeclaration* style, EditAction editingAction)
617 {
618     switch (m_frame->selectionController()->state()) {
619         case Selection::NONE:
620             // do nothing
621             break;
622         case Selection::CARET:
623         case Selection::RANGE:
624             if (m_frame->document() && style)
625                 applyCommand(new ApplyStyleCommand(m_frame->document(), style, editingAction, ApplyStyleCommand::ForceBlockProperties));
626             break;
627     }
628 }
629
630 void Editor::applyStyleToSelection(CSSStyleDeclaration* style, EditAction editingAction)
631 {
632     if (!style || style->length() == 0 || !canEditRichly())
633         return;
634
635     if (client() && client()->shouldApplyStyle(style, m_frame->selectionController()->toRange().get()))
636         applyStyle(style, editingAction);
637 }
638
639 void Editor::applyParagraphStyleToSelection(CSSStyleDeclaration* style, EditAction editingAction)
640 {
641     if (!style || style->length() == 0 || !canEditRichly())
642         return;
643     
644     if (client() && client()->shouldApplyStyle(style, m_frame->selectionController()->toRange().get()))
645         applyParagraphStyle(style, editingAction);
646 }
647
648 bool Editor::clientIsEditable() const
649 {
650     return client() && client()->isEditable();
651 }
652
653 bool Editor::selectionStartHasStyle(CSSStyleDeclaration* style) const
654 {
655     Node* nodeToRemove;
656     RefPtr<CSSComputedStyleDeclaration> selectionStyle = m_frame->selectionComputedStyle(nodeToRemove);
657     if (!selectionStyle)
658         return false;
659     
660     RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
661     
662     bool match = true;
663     DeprecatedValueListConstIterator<CSSProperty> end;
664     for (DeprecatedValueListConstIterator<CSSProperty> it = mutableStyle->valuesIterator(); it != end; ++it) {
665         int propertyID = (*it).id();
666         if (!equalIgnoringCase(mutableStyle->getPropertyValue(propertyID), selectionStyle->getPropertyValue(propertyID))) {
667             match = false;
668             break;
669         }
670     }
671     
672     if (nodeToRemove) {
673         ExceptionCode ec = 0;
674         nodeToRemove->remove(ec);
675         ASSERT(ec == 0);
676     }
677     
678     return match;
679 }
680
681 static void updateState(CSSMutableStyleDeclaration* desiredStyle, CSSComputedStyleDeclaration* computedStyle, bool& atStart, TriState& state)
682 {
683     DeprecatedValueListConstIterator<CSSProperty> end;
684     for (DeprecatedValueListConstIterator<CSSProperty> it = desiredStyle->valuesIterator(); it != end; ++it) {
685         int propertyID = (*it).id();
686         String desiredProperty = desiredStyle->getPropertyValue(propertyID);
687         String computedProperty = computedStyle->getPropertyValue(propertyID);
688         TriState propertyState = equalIgnoringCase(desiredProperty, computedProperty)
689             ? TrueTriState : FalseTriState;
690         if (atStart) {
691             state = propertyState;
692             atStart = false;
693         } else if (state != propertyState) {
694             state = MixedTriState;
695             break;
696         }
697     }
698 }
699
700 TriState Editor::selectionHasStyle(CSSStyleDeclaration* style) const
701 {
702     bool atStart = true;
703     TriState state = FalseTriState;
704
705     RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
706
707     if (!m_frame->selectionController()->isRange()) {
708         Node* nodeToRemove;
709         RefPtr<CSSComputedStyleDeclaration> selectionStyle = m_frame->selectionComputedStyle(nodeToRemove);
710         if (!selectionStyle)
711             return FalseTriState;
712         updateState(mutableStyle.get(), selectionStyle.get(), atStart, state);
713         if (nodeToRemove) {
714             ExceptionCode ec = 0;
715             nodeToRemove->remove(ec);
716             ASSERT(ec == 0);
717         }
718     } else {
719         for (Node* node = m_frame->selectionController()->start().node(); node; node = node->traverseNextNode()) {
720             RefPtr<CSSComputedStyleDeclaration> computedStyle = new CSSComputedStyleDeclaration(node);
721             if (computedStyle)
722                 updateState(mutableStyle.get(), computedStyle.get(), atStart, state);
723             if (state == MixedTriState)
724                 break;
725             if (node == m_frame->selectionController()->end().node())
726                 break;
727         }
728     }
729
730     return state;
731 }
732 void Editor::indent()
733 {
734     applyCommand(new IndentOutdentCommand(m_frame->document(), IndentOutdentCommand::Indent));
735 }
736
737 void Editor::outdent()
738 {
739     applyCommand(new IndentOutdentCommand(m_frame->document(), IndentOutdentCommand::Outdent));
740 }
741
742 static void dispatchEditableContentChangedEvents(const EditCommand& command)
743 {
744     Element* startRoot = command.startingRootEditableElement();
745     Element* endRoot = command.endingRootEditableElement();
746     ExceptionCode ec;
747     if (startRoot)
748         startRoot->dispatchEvent(new Event(webkitEditableContentChangedEvent, false, false), ec, true);
749     if (endRoot && endRoot != startRoot)
750         endRoot->dispatchEvent(new Event(webkitEditableContentChangedEvent, false, false), ec, true);
751 }
752
753 void Editor::appliedEditing(PassRefPtr<EditCommand> cmd)
754 {
755     dispatchEditableContentChangedEvents(*cmd);
756     
757     // FIXME: We shouldn't tell setSelection to clear the typing style or removed anchor here.
758     // If we didn't, we wouldn't have to save/restore the removedAnchor, and we wouldn't have to have
759     // the typing style stored in two places (the Frame and commands).
760     RefPtr<Node> anchor = removedAnchor();
761     
762     Selection newSelection(cmd->endingSelection());
763     // If there is no selection change, don't bother sending shouldChangeSelection, but still call setSelection,
764     // because there is work that it must do in this situation.
765     // The old selection can be invalid here and calling shouldChangeSelection can produce some strange calls.
766     // See <rdar://problem/5729315> Some shouldChangeSelectedDOMRange contain Ranges for selections that are no longer valid
767     if (newSelection == m_frame->selectionController()->selection() || m_frame->shouldChangeSelection(newSelection))
768         m_frame->selectionController()->setSelection(newSelection, false);
769     
770     setRemovedAnchor(anchor);
771     
772     // Now set the typing style from the command. Clear it when done.
773     // This helps make the case work where you completely delete a piece
774     // of styled text and then type a character immediately after.
775     // That new character needs to take on the style of the just-deleted text.
776     // FIXME: Improve typing style.
777     // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
778     if (cmd->typingStyle()) {
779         m_frame->setTypingStyle(cmd->typingStyle());
780         cmd->setTypingStyle(0);
781     }
782     
783     // Command will be equal to last edit command only in the case of typing
784     if (m_lastEditCommand.get() == cmd)
785         ASSERT(cmd->isTypingCommand());
786     else {
787         // Only register a new undo command if the command passed in is
788         // different from the last command
789         m_lastEditCommand = cmd;
790         if (client())
791             client()->registerCommandForUndo(m_lastEditCommand);
792     }
793     respondToChangedContents(newSelection);    
794 }
795
796 void Editor::unappliedEditing(PassRefPtr<EditCommand> cmd)
797 {
798     dispatchEditableContentChangedEvents(*cmd);
799     
800     Selection newSelection(cmd->startingSelection());
801     // If there is no selection change, don't bother sending shouldChangeSelection, but still call setSelection,
802     // because there is work that it must do in this situation.
803     // The old selection can be invalid here and calling shouldChangeSelection can produce some strange calls.
804     // See <rdar://problem/5729315> Some shouldChangeSelectedDOMRange contain Ranges for selections that are no longer valid
805     if (newSelection == m_frame->selectionController()->selection() || m_frame->shouldChangeSelection(newSelection))
806         m_frame->selectionController()->setSelection(newSelection, true);
807     
808     m_lastEditCommand = 0;
809     if (client())
810         client()->registerCommandForRedo(cmd);
811     respondToChangedContents(newSelection);    
812 }
813
814 void Editor::reappliedEditing(PassRefPtr<EditCommand> cmd)
815 {
816     dispatchEditableContentChangedEvents(*cmd);
817     
818     Selection newSelection(cmd->endingSelection());
819     // If there is no selection change, don't bother sending shouldChangeSelection, but still call setSelection,
820     // because there is work that it must do in this situation.
821     // The old selection can be invalid here and calling shouldChangeSelection can produce some strange calls.
822     // See <rdar://problem/5729315> Some shouldChangeSelectedDOMRange contain Ranges for selections that are no longer valid
823     if (newSelection == m_frame->selectionController()->selection() || m_frame->shouldChangeSelection(newSelection))
824         m_frame->selectionController()->setSelection(newSelection, true);
825     
826     m_lastEditCommand = 0;
827     if (client())
828         client()->registerCommandForUndo(cmd);
829     respondToChangedContents(newSelection);    
830 }
831
832 Editor::Editor(Frame* frame)
833     : m_frame(frame)
834     , m_deleteButtonController(new DeleteButtonController(frame))
835     , m_ignoreCompositionSelectionChange(false)
836     , m_shouldStartNewKillRingSequence(false)
837
838 }
839
840 Editor::~Editor()
841 {
842 }
843
844 void Editor::clear()
845 {
846     m_compositionNode = 0;
847     m_customCompositionUnderlines.clear();
848 }
849
850 bool Editor::insertText(const String& text, Event* triggeringEvent)
851 {
852     return m_frame->eventHandler()->handleTextInputEvent(text, triggeringEvent);
853 }
854
855 bool Editor::insertTextWithoutSendingTextEvent(const String& text, bool selectInsertedText, Event* triggeringEvent)
856 {
857     if (text.isEmpty())
858         return false;
859
860     Selection selection = selectionForCommand(triggeringEvent);
861     if (!selection.isContentEditable())
862         return false;
863     RefPtr<Range> range = selection.toRange();
864
865     if (!shouldInsertText(text, range.get(), EditorInsertActionTyped))
866         return true;
867
868     // Get the selection to use for the event that triggered this insertText.
869     // If the event handler changed the selection, we may want to use a different selection
870     // that is contained in the event target.
871     selection = selectionForCommand(triggeringEvent);
872     if (selection.isContentEditable()) {
873         if (Node* selectionStart = selection.start().node()) {
874             RefPtr<Document> document = selectionStart->document();
875             
876             // Insert the text
877             TypingCommand::insertText(document.get(), text, selection, selectInsertedText);
878
879             // Reveal the current selection 
880             if (Frame* editedFrame = document->frame())
881                 if (Page* page = editedFrame->page())
882                     page->focusController()->focusedOrMainFrame()->revealSelection(RenderLayer::gAlignToEdgeIfNeeded);
883         }
884     }
885
886     return true;
887 }
888
889 bool Editor::insertLineBreak()
890 {
891     if (!canEdit())
892         return false;
893
894     if (!shouldInsertText("\n", m_frame->selectionController()->toRange().get(), EditorInsertActionTyped))
895         return true;
896
897     TypingCommand::insertLineBreak(m_frame->document());
898     revealSelectionAfterEditingOperation();
899     return true;
900 }
901
902 bool Editor::insertParagraphSeparator()
903 {
904     if (!canEdit())
905         return false;
906
907     if (!canEditRichly())
908         return insertLineBreak();
909
910     if (!shouldInsertText("\n", m_frame->selectionController()->toRange().get(), EditorInsertActionTyped))
911         return true;
912
913     TypingCommand::insertParagraphSeparator(m_frame->document());
914     revealSelectionAfterEditingOperation();
915     return true;
916 }
917
918 void Editor::cut()
919 {
920     if (tryDHTMLCut())
921         return; // DHTML did the whole operation
922     if (!canCut()) {
923         systemBeep();
924         return;
925     }
926     RefPtr<Range> selection = selectedRange();
927     if (shouldDeleteRange(selection.get())) {
928         Pasteboard::generalPasteboard()->writeSelection(selection.get(), canSmartCopyOrDelete(), m_frame);
929         didWriteSelectionToPasteboard();
930         deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
931     }
932 }
933
934 void Editor::copy()
935 {
936     if (tryDHTMLCopy())
937         return; // DHTML did the whole operation
938     if (!canCopy()) {
939         systemBeep();
940         return;
941     }
942     
943     Document* document = m_frame->document();
944     if (HTMLImageElement* imageElement = imageElementFromImageDocument(document))
945         Pasteboard::generalPasteboard()->writeImage(imageElement, document->url(), document->title());
946     else
947         Pasteboard::generalPasteboard()->writeSelection(selectedRange().get(), canSmartCopyOrDelete(), m_frame);
948     
949     didWriteSelectionToPasteboard();
950 }
951
952 #if !PLATFORM(MAC)
953
954 void Editor::paste()
955 {
956     ASSERT(m_frame->document());
957     if (tryDHTMLPaste())
958         return;     // DHTML did the whole operation
959     if (!canPaste())
960         return;
961     DocLoader* loader = m_frame->document()->docLoader();
962     loader->setAllowStaleResources(true);
963     if (m_frame->selectionController()->isContentRichlyEditable())
964         pasteWithPasteboard(Pasteboard::generalPasteboard(), true);
965     else
966         pasteAsPlainTextWithPasteboard(Pasteboard::generalPasteboard());
967     loader->setAllowStaleResources(false);
968 }
969
970 #endif
971
972 void Editor::pasteAsPlainText()
973 {
974    if (!canPaste())
975         return;
976    pasteAsPlainTextWithPasteboard(Pasteboard::generalPasteboard());
977 }
978
979 void Editor::performDelete()
980 {
981     if (!canDelete()) {
982         systemBeep();
983         return;
984     }
985
986     addToKillRing(selectedRange().get(), false);
987     deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
988
989     // clear the "start new kill ring sequence" setting, because it was set to true
990     // when the selection was updated by deleting the range
991     setStartNewKillRingSequence(false);
992 }
993
994 void Editor::copyURL(const KURL& url, const String& title)
995 {
996     Pasteboard::generalPasteboard()->writeURL(url, title, m_frame);
997 }
998
999 void Editor::copyImage(const HitTestResult& result)
1000 {
1001     KURL url = result.absoluteLinkURL();
1002     if (url.isEmpty())
1003         url = result.absoluteImageURL();
1004
1005     Pasteboard::generalPasteboard()->writeImage(result.innerNonSharedNode(), url, result.altDisplayString());
1006 }
1007
1008 bool Editor::isContinuousSpellCheckingEnabled()
1009 {
1010     return client() && client()->isContinuousSpellCheckingEnabled();
1011 }
1012
1013 void Editor::toggleContinuousSpellChecking()
1014 {
1015     if (client())
1016         client()->toggleContinuousSpellChecking();
1017 }
1018
1019 bool Editor::isGrammarCheckingEnabled()
1020 {
1021     return client() && client()->isGrammarCheckingEnabled();
1022 }
1023
1024 void Editor::toggleGrammarChecking()
1025 {
1026     if (client())
1027         client()->toggleGrammarChecking();
1028 }
1029
1030 int Editor::spellCheckerDocumentTag()
1031 {
1032     return client() ? client()->spellCheckerDocumentTag() : 0;
1033 }
1034
1035 bool Editor::shouldEndEditing(Range* range)
1036 {
1037     return client() && client()->shouldEndEditing(range);
1038 }
1039
1040 bool Editor::shouldBeginEditing(Range* range)
1041 {
1042     return client() && client()->shouldBeginEditing(range);
1043 }
1044
1045 void Editor::clearUndoRedoOperations()
1046 {
1047     if (client())
1048         client()->clearUndoRedoOperations();
1049 }
1050
1051 bool Editor::canUndo()
1052 {
1053     return client() && client()->canUndo();
1054 }
1055
1056 void Editor::undo()
1057 {
1058     if (client())
1059         client()->undo();
1060 }
1061
1062 bool Editor::canRedo()
1063 {
1064     return client() && client()->canRedo();
1065 }
1066
1067 void Editor::redo()
1068 {
1069     if (client())
1070         client()->redo();
1071 }
1072
1073 void Editor::didBeginEditing()
1074 {
1075     if (client())
1076         client()->didBeginEditing();
1077 }
1078
1079 void Editor::didEndEditing()
1080 {
1081     if (client())
1082         client()->didEndEditing();
1083 }
1084
1085 void Editor::didWriteSelectionToPasteboard()
1086 {
1087     if (client())
1088         client()->didWriteSelectionToPasteboard();
1089 }
1090
1091 void Editor::toggleBold()
1092 {
1093     command("ToggleBold").execute();
1094 }
1095
1096 void Editor::toggleUnderline()
1097 {
1098     command("ToggleUnderline").execute();
1099 }
1100
1101 void Editor::setBaseWritingDirection(const String& direction)
1102 {
1103     ExceptionCode ec = 0;
1104
1105     RefPtr<CSSMutableStyleDeclaration> style = new CSSMutableStyleDeclaration;
1106     style->setProperty(CSS_PROP_DIRECTION, direction, false, ec);
1107     applyParagraphStyleToSelection(style.get(), EditActionSetWritingDirection);
1108 }
1109
1110 void Editor::selectComposition()
1111 {
1112     RefPtr<Range> range = compositionRange();
1113     if (!range)
1114         return;
1115     
1116     // The composition can start inside a composed character sequence, so we have to override checks.
1117     // See <http://bugs.webkit.org/show_bug.cgi?id=15781>
1118     Selection selection;
1119     selection.setWithoutValidation(range->startPosition(), range->endPosition());
1120     m_frame->selectionController()->setSelection(selection, false, false);
1121 }
1122
1123 void Editor::confirmComposition()
1124 {
1125     if (!m_compositionNode)
1126         return;
1127     confirmComposition(m_compositionNode->data().substring(m_compositionStart, m_compositionEnd - m_compositionStart), false);
1128 }
1129
1130 void Editor::confirmCompositionWithoutDisturbingSelection()
1131 {
1132     if (!m_compositionNode)
1133         return;
1134     confirmComposition(m_compositionNode->data().substring(m_compositionStart, m_compositionEnd - m_compositionStart), true);
1135 }
1136
1137 void Editor::confirmComposition(const String& text)
1138 {
1139     confirmComposition(text, false);
1140 }
1141
1142 void Editor::confirmComposition(const String& text, bool preserveSelection)
1143 {
1144     setIgnoreCompositionSelectionChange(true);
1145
1146     Selection oldSelection = m_frame->selectionController()->selection();
1147
1148     selectComposition();
1149
1150     if (m_frame->selectionController()->isNone()) {
1151         setIgnoreCompositionSelectionChange(false);
1152         return;
1153     }
1154     
1155     // If there is a composition to replace, remove it with a deletion that will be part of the
1156     // same Undo step as the next and previous insertions.
1157     TypingCommand::deleteSelection(m_frame->document(), false);
1158
1159     m_compositionNode = 0;
1160     m_customCompositionUnderlines.clear();
1161
1162     insertText(text, 0);
1163
1164     if (preserveSelection)
1165         m_frame->selectionController()->setSelection(oldSelection, false, false);
1166
1167     setIgnoreCompositionSelectionChange(false);
1168 }
1169
1170 void Editor::setComposition(const String& text, const Vector<CompositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd)
1171 {
1172     setIgnoreCompositionSelectionChange(true);
1173
1174     selectComposition();
1175
1176     if (m_frame->selectionController()->isNone()) {
1177         setIgnoreCompositionSelectionChange(false);
1178         return;
1179     }
1180     
1181     // If there is a composition to replace, remove it with a deletion that will be part of the
1182     // same Undo step as the next and previous insertions.
1183     TypingCommand::deleteSelection(m_frame->document(), false);
1184
1185     m_compositionNode = 0;
1186     m_customCompositionUnderlines.clear();
1187
1188     if (!text.isEmpty()) {
1189         TypingCommand::insertText(m_frame->document(), text, true, true);
1190
1191         Node* baseNode = m_frame->selectionController()->base().node();
1192         unsigned baseOffset = m_frame->selectionController()->base().offset();
1193         Node* extentNode = m_frame->selectionController()->extent().node();
1194         unsigned extentOffset = m_frame->selectionController()->extent().offset();
1195
1196         if (baseNode && baseNode == extentNode && baseNode->isTextNode() && baseOffset + text.length() == extentOffset) {
1197             m_compositionNode = static_cast<Text*>(baseNode);
1198             m_compositionStart = baseOffset;
1199             m_compositionEnd = extentOffset;
1200             m_customCompositionUnderlines = underlines;
1201             size_t numUnderlines = m_customCompositionUnderlines.size();
1202             for (size_t i = 0; i < numUnderlines; ++i) {
1203                 m_customCompositionUnderlines[i].startOffset += baseOffset;
1204                 m_customCompositionUnderlines[i].endOffset += baseOffset;
1205             }
1206             if (baseNode->renderer())
1207                 baseNode->renderer()->repaint();
1208
1209             unsigned start = min(baseOffset + selectionStart, extentOffset);
1210             unsigned end = min(max(start, baseOffset + selectionEnd), extentOffset);
1211             RefPtr<Range> selectedRange = new Range(baseNode->document(), baseNode, start, baseNode, end);                
1212             m_frame->selectionController()->setSelectedRange(selectedRange.get(), DOWNSTREAM, false);
1213         }
1214     }
1215
1216     setIgnoreCompositionSelectionChange(false);
1217 }
1218
1219 void Editor::ignoreSpelling()
1220 {
1221     if (!client())
1222         return;
1223
1224     String text = frame()->selectedText();
1225     ASSERT(text.length() != 0);
1226     client()->ignoreWordInSpellDocument(text);
1227 }
1228
1229 void Editor::learnSpelling()
1230 {
1231     if (!client())
1232         return;
1233
1234     String text = frame()->selectedText();
1235     ASSERT(text.length() != 0);
1236     client()->learnWord(text);
1237 }
1238
1239 static String findFirstMisspellingInRange(EditorClient* client, Range* searchRange, int& firstMisspellingOffset, bool markAll)
1240 {
1241     ASSERT_ARG(client, client);
1242     ASSERT_ARG(searchRange, searchRange);
1243     
1244     WordAwareIterator it(searchRange);
1245     firstMisspellingOffset = 0;
1246     
1247     String firstMisspelling;
1248     int currentChunkOffset = 0;
1249
1250     while (!it.atEnd()) {
1251         const UChar* chars = it.characters();
1252         int len = it.length();
1253         
1254         // Skip some work for one-space-char hunks
1255         if (!(len == 1 && chars[0] == ' ')) {
1256             
1257             int misspellingLocation = -1;
1258             int misspellingLength = 0;
1259             client->checkSpellingOfString(chars, len, &misspellingLocation, &misspellingLength);
1260
1261             // 5490627 shows that there was some code path here where the String constructor below crashes.
1262             // We don't know exactly what combination of bad input caused this, so we're making this much
1263             // more robust against bad input on release builds.
1264             ASSERT(misspellingLength >= 0);
1265             ASSERT(misspellingLocation >= -1);
1266             ASSERT(misspellingLength == 0 || misspellingLocation >= 0);
1267             ASSERT(misspellingLocation < len);
1268             ASSERT(misspellingLength <= len);
1269             ASSERT(misspellingLocation + misspellingLength <= len);
1270             
1271             if (misspellingLocation >= 0 && misspellingLength > 0 && misspellingLocation < len && misspellingLength <= len && misspellingLocation + misspellingLength <= len) {
1272                 
1273                 // Remember first-encountered misspelling and its offset
1274                 if (!firstMisspelling) {
1275                     firstMisspellingOffset = currentChunkOffset + misspellingLocation;
1276                     firstMisspelling = String(chars + misspellingLocation, misspellingLength);
1277                 }
1278                 
1279                 // Mark this instance if we're marking all instances. Otherwise bail out because we found the first one.
1280                 if (!markAll)
1281                     break;
1282                 
1283                 // Compute range of misspelled word
1284                 RefPtr<Range> misspellingRange = TextIterator::subrange(searchRange, currentChunkOffset + misspellingLocation, misspellingLength);
1285                 
1286                 // Store marker for misspelled word
1287                 ExceptionCode ec = 0;
1288                 misspellingRange->startContainer(ec)->document()->addMarker(misspellingRange.get(), DocumentMarker::Spelling);
1289                 ASSERT(ec == 0);
1290             }
1291         }
1292         
1293         currentChunkOffset += len;
1294         it.advance();
1295     }
1296     
1297     return firstMisspelling;
1298 }
1299
1300 #ifndef BUILDING_ON_TIGER
1301
1302 static PassRefPtr<Range> paragraphAlignedRangeForRange(Range* arbitraryRange, int& offsetIntoParagraphAlignedRange, String& paragraphString)
1303 {
1304     ASSERT_ARG(arbitraryRange, arbitraryRange);
1305     
1306     ExceptionCode ec = 0;
1307     
1308     // Expand range to paragraph boundaries
1309     RefPtr<Range> paragraphRange = arbitraryRange->cloneRange(ec);
1310     setStart(paragraphRange.get(), startOfParagraph(arbitraryRange->startPosition()));
1311     setEnd(paragraphRange.get(), endOfParagraph(arbitraryRange->endPosition()));
1312     
1313     // Compute offset from start of expanded range to start of original range
1314     RefPtr<Range> offsetAsRange = new Range(paragraphRange->startContainer(ec)->document(), paragraphRange->startPosition(), arbitraryRange->startPosition());
1315     offsetIntoParagraphAlignedRange = TextIterator::rangeLength(offsetAsRange.get());
1316     
1317     // Fill in out parameter with string representing entire paragraph range.
1318     // Someday we might have a caller that doesn't use this, but for now all callers do.
1319     paragraphString = plainText(paragraphRange.get());
1320
1321     return paragraphRange;
1322 }
1323
1324 static int findFirstGrammarDetailInRange(const Vector<GrammarDetail>& grammarDetails, int badGrammarPhraseLocation, int badGrammarPhraseLength, Range *searchRange, int startOffset, int endOffset, bool markAll)
1325 {
1326     // Found some bad grammar. Find the earliest detail range that starts in our search range (if any).
1327     // Optionally add a DocumentMarker for each detail in the range.
1328     int earliestDetailLocationSoFar = -1;
1329     int earliestDetailIndex = -1;
1330     for (unsigned i = 0; i < grammarDetails.size(); i++) {
1331         const GrammarDetail* detail = &grammarDetails[i];
1332         ASSERT(detail->length > 0 && detail->location >= 0);
1333         
1334         int detailStartOffsetInParagraph = badGrammarPhraseLocation + detail->location;
1335         
1336         // Skip this detail if it starts before the original search range
1337         if (detailStartOffsetInParagraph < startOffset)
1338             continue;
1339         
1340         // Skip this detail if it starts after the original search range
1341         if (detailStartOffsetInParagraph >= endOffset)
1342             continue;
1343         
1344         if (markAll) {
1345             RefPtr<Range> badGrammarRange = TextIterator::subrange(searchRange, badGrammarPhraseLocation - startOffset + detail->location, detail->length);
1346             ExceptionCode ec = 0;
1347             badGrammarRange->startContainer(ec)->document()->addMarker(badGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription);
1348             ASSERT(ec == 0);
1349         }
1350         
1351         // Remember this detail only if it's earlier than our current candidate (the details aren't in a guaranteed order)
1352         if (earliestDetailIndex < 0 || earliestDetailLocationSoFar > detail->location) {
1353             earliestDetailIndex = i;
1354             earliestDetailLocationSoFar = detail->location;
1355         }
1356     }
1357     
1358     return earliestDetailIndex;
1359 }
1360     
1361 static String findFirstBadGrammarInRange(EditorClient* client, Range* searchRange, GrammarDetail& outGrammarDetail, int& outGrammarPhraseOffset, bool markAll)
1362 {
1363     ASSERT_ARG(client, client);
1364     ASSERT_ARG(searchRange, searchRange);
1365     
1366     // Initialize out parameters; these will be updated if we find something to return.
1367     outGrammarDetail.location = -1;
1368     outGrammarDetail.length = 0;
1369     outGrammarDetail.guesses.clear();
1370     outGrammarDetail.userDescription = "";
1371     outGrammarPhraseOffset = 0;
1372     
1373     String firstBadGrammarPhrase;
1374
1375     // Expand the search range to encompass entire paragraphs, since grammar checking needs that much context.
1376     // Determine the character offset from the start of the paragraph to the start of the original search range,
1377     // since we will want to ignore results in this area.
1378     int searchRangeStartOffset;
1379     String paragraphString;
1380     RefPtr<Range> paragraphRange = paragraphAlignedRangeForRange(searchRange, searchRangeStartOffset, paragraphString);
1381         
1382     // Determine the character offset from the start of the paragraph to the end of the original search range, 
1383     // since we will want to ignore results in this area also.
1384     int searchRangeEndOffset = searchRangeStartOffset + TextIterator::rangeLength(searchRange);
1385         
1386     // Start checking from beginning of paragraph, but skip past results that occur before the start of the original search range.
1387     int startOffset = 0;
1388     while (startOffset < searchRangeEndOffset) {
1389         Vector<GrammarDetail> grammarDetails;
1390         int badGrammarPhraseLocation = -1;
1391         int badGrammarPhraseLength = 0;
1392         client->checkGrammarOfString(paragraphString.characters() + startOffset, paragraphString.length() - startOffset, grammarDetails, &badGrammarPhraseLocation, &badGrammarPhraseLength);
1393         
1394         if (badGrammarPhraseLength == 0) {
1395             ASSERT(badGrammarPhraseLocation == -1);
1396             return String();
1397         }
1398
1399         ASSERT(badGrammarPhraseLocation >= 0);
1400         badGrammarPhraseLocation += startOffset;
1401
1402         
1403         // Found some bad grammar. Find the earliest detail range that starts in our search range (if any).
1404         int badGrammarIndex = findFirstGrammarDetailInRange(grammarDetails, badGrammarPhraseLocation, badGrammarPhraseLength, searchRange, searchRangeStartOffset, searchRangeEndOffset, markAll);
1405         if (badGrammarIndex >= 0) {
1406             ASSERT(static_cast<unsigned>(badGrammarIndex) < grammarDetails.size());
1407             outGrammarDetail = grammarDetails[badGrammarIndex];
1408         }
1409
1410         // If we found a detail in range, then we have found the first bad phrase (unless we found one earlier but
1411         // kept going so we could mark all instances).
1412         if (badGrammarIndex >= 0 && firstBadGrammarPhrase.isEmpty()) {
1413             outGrammarPhraseOffset = badGrammarPhraseLocation - searchRangeStartOffset;
1414             firstBadGrammarPhrase = paragraphString.substring(badGrammarPhraseLocation, badGrammarPhraseLength);
1415             
1416             // Found one. We're done now, unless we're marking each instance.
1417             if (!markAll)
1418                 break;
1419         }
1420
1421         // These results were all between the start of the paragraph and the start of the search range; look
1422         // beyond this phrase.
1423         startOffset = badGrammarPhraseLocation + badGrammarPhraseLength;
1424     }
1425     
1426     return firstBadGrammarPhrase;
1427 }
1428     
1429 #endif /* not BUILDING_ON_TIGER */
1430
1431 void Editor::advanceToNextMisspelling(bool startBeforeSelection)
1432 {
1433     ExceptionCode ec = 0;
1434
1435     // The basic approach is to search in two phases - from the selection end to the end of the doc, and
1436     // then we wrap and search from the doc start to (approximately) where we started.
1437     
1438     // Start at the end of the selection, search to edge of document.  Starting at the selection end makes
1439     // repeated "check spelling" commands work.
1440     Selection selection(frame()->selectionController()->selection());
1441     RefPtr<Range> spellingSearchRange(rangeOfContents(frame()->document()));
1442     bool startedWithSelection = false;
1443     if (selection.start().node()) {
1444         startedWithSelection = true;
1445         if (startBeforeSelection) {
1446             VisiblePosition start(selection.visibleStart());
1447             // We match AppKit's rule: Start 1 character before the selection.
1448             VisiblePosition oneBeforeStart = start.previous();
1449             setStart(spellingSearchRange.get(), oneBeforeStart.isNotNull() ? oneBeforeStart : start);
1450         } else
1451             setStart(spellingSearchRange.get(), selection.visibleEnd());
1452     }
1453
1454     Position position = spellingSearchRange->startPosition();
1455     if (!isEditablePosition(position)) {
1456         // This shouldn't happen in very often because the Spelling menu items aren't enabled unless the
1457         // selection is editable.
1458         // This can happen in Mail for a mix of non-editable and editable content (like Stationary), 
1459         // when spell checking the whole document before sending the message.
1460         // In that case the document might not be editable, but there are editable pockets that need to be spell checked.
1461
1462         position = firstEditablePositionAfterPositionInRoot(position, frame()->document()->documentElement()).deepEquivalent();
1463         if (position.isNull())
1464             return;
1465         
1466         Position rangeCompliantPosition = rangeCompliantEquivalent(position);
1467         spellingSearchRange->setStart(rangeCompliantPosition.node(), rangeCompliantPosition.offset(), ec);
1468         startedWithSelection = false;   // won't need to wrap
1469     }
1470     
1471     // topNode defines the whole range we want to operate on 
1472     Node* topNode = highestEditableRoot(position);
1473     spellingSearchRange->setEnd(topNode, maxDeepOffset(topNode), ec);
1474
1475     // If spellingSearchRange starts in the middle of a word, advance to the next word so we start checking
1476     // at a word boundary. Going back by one char and then forward by a word does the trick.
1477     if (startedWithSelection) {
1478         VisiblePosition oneBeforeStart = startVisiblePosition(spellingSearchRange.get(), DOWNSTREAM).previous();
1479         if (oneBeforeStart.isNotNull()) {
1480             setStart(spellingSearchRange.get(), endOfWord(oneBeforeStart));
1481         } // else we were already at the start of the editable node
1482     }
1483     
1484     if (spellingSearchRange->collapsed(ec))
1485         return;       // nothing to search in
1486     
1487     // Get the spell checker if it is available
1488     if (!client())
1489         return;
1490         
1491     // We go to the end of our first range instead of the start of it, just to be sure
1492     // we don't get foiled by any word boundary problems at the start.  It means we might
1493     // do a tiny bit more searching.
1494     Node *searchEndNodeAfterWrap = spellingSearchRange->endContainer(ec);
1495     int searchEndOffsetAfterWrap = spellingSearchRange->endOffset(ec);
1496     
1497     int misspellingOffset;
1498     String misspelledWord = findFirstMisspellingInRange(client(), spellingSearchRange.get(), misspellingOffset, false);
1499     
1500     String badGrammarPhrase;
1501
1502 #ifndef BUILDING_ON_TIGER
1503     int grammarPhraseOffset = 0;
1504     GrammarDetail grammarDetail;
1505
1506     // Search for bad grammar that occurs prior to the next misspelled word (if any)
1507     RefPtr<Range> grammarSearchRange = spellingSearchRange->cloneRange(ec);
1508     if (!misspelledWord.isEmpty()) {
1509         // Stop looking at start of next misspelled word
1510         CharacterIterator chars(grammarSearchRange.get());
1511         chars.advance(misspellingOffset);
1512         grammarSearchRange->setEnd(chars.range()->startContainer(ec), chars.range()->startOffset(ec), ec);
1513     }
1514     
1515     if (isGrammarCheckingEnabled())
1516         badGrammarPhrase = findFirstBadGrammarInRange(client(), grammarSearchRange.get(), grammarDetail, grammarPhraseOffset, false);
1517 #endif
1518     
1519     // If we found neither bad grammar nor a misspelled word, wrap and try again (but don't bother if we started at the beginning of the
1520     // block rather than at a selection).
1521     if (startedWithSelection && !misspelledWord && !badGrammarPhrase) {
1522         spellingSearchRange->setStart(topNode, 0, ec);
1523         // going until the end of the very first chunk we tested is far enough
1524         spellingSearchRange->setEnd(searchEndNodeAfterWrap, searchEndOffsetAfterWrap, ec);
1525         
1526         misspelledWord = findFirstMisspellingInRange(client(), spellingSearchRange.get(), misspellingOffset, false);
1527
1528 #ifndef BUILDING_ON_TIGER
1529         grammarSearchRange = spellingSearchRange->cloneRange(ec);
1530         if (!misspelledWord.isEmpty()) {
1531             // Stop looking at start of next misspelled word
1532             CharacterIterator chars(grammarSearchRange.get());
1533             chars.advance(misspellingOffset);
1534             grammarSearchRange->setEnd(chars.range()->startContainer(ec), chars.range()->startOffset(ec), ec);
1535         }
1536         if (isGrammarCheckingEnabled())
1537             badGrammarPhrase = findFirstBadGrammarInRange(client(), grammarSearchRange.get(), grammarDetail, grammarPhraseOffset, false);
1538 #endif
1539     }
1540     
1541     if (!badGrammarPhrase.isEmpty()) {
1542 #ifdef BUILDING_ON_TIGER
1543         ASSERT_NOT_REACHED();
1544 #else
1545         // We found bad grammar. Since we only searched for bad grammar up to the first misspelled word, the bad grammar
1546         // takes precedence and we ignore any potential misspelled word. Select the grammar detail, update the spelling
1547         // panel, and store a marker so we draw the green squiggle later.
1548         
1549         ASSERT(badGrammarPhrase.length() > 0);
1550         ASSERT(grammarDetail.location != -1 && grammarDetail.length > 0);
1551         
1552         // FIXME 4859190: This gets confused with doubled punctuation at the end of a paragraph
1553         RefPtr<Range> badGrammarRange = TextIterator::subrange(grammarSearchRange.get(), grammarPhraseOffset + grammarDetail.location, grammarDetail.length);
1554         frame()->selectionController()->setSelection(Selection(badGrammarRange.get(), SEL_DEFAULT_AFFINITY));
1555         frame()->revealSelection();
1556         
1557         client()->updateSpellingUIWithGrammarString(badGrammarPhrase, grammarDetail);
1558         frame()->document()->addMarker(badGrammarRange.get(), DocumentMarker::Grammar, grammarDetail.userDescription);
1559 #endif        
1560     } else if (!misspelledWord.isEmpty()) {
1561         // We found a misspelling, but not any earlier bad grammar. Select the misspelling, update the spelling panel, and store
1562         // a marker so we draw the red squiggle later.
1563         
1564         RefPtr<Range> misspellingRange = TextIterator::subrange(spellingSearchRange.get(), misspellingOffset, misspelledWord.length());
1565         frame()->selectionController()->setSelection(Selection(misspellingRange.get(), DOWNSTREAM));
1566         frame()->revealSelection();
1567         
1568         client()->updateSpellingUIWithMisspelledWord(misspelledWord);
1569         frame()->document()->addMarker(misspellingRange.get(), DocumentMarker::Spelling);
1570     }
1571 }
1572
1573 bool Editor::isSelectionMisspelled()
1574 {
1575     String selectedString = frame()->selectedText();
1576     int length = selectedString.length();
1577     if (length == 0)
1578         return false;
1579
1580     if (!client())
1581         return false;
1582     
1583     int misspellingLocation = -1;
1584     int misspellingLength = 0;
1585     client()->checkSpellingOfString(selectedString.characters(), length, &misspellingLocation, &misspellingLength);
1586     
1587     // The selection only counts as misspelled if the selected text is exactly one misspelled word
1588     if (misspellingLength != length)
1589         return false;
1590     
1591     // Update the spelling panel to be displaying this error (whether or not the spelling panel is on screen).
1592     // This is necessary to make a subsequent call to [NSSpellChecker ignoreWord:inSpellDocumentWithTag:] work
1593     // correctly; that call behaves differently based on whether the spelling panel is displaying a misspelling
1594     // or a grammar error.
1595     client()->updateSpellingUIWithMisspelledWord(selectedString);
1596     
1597     return true;
1598 }
1599
1600 #ifndef BUILDING_ON_TIGER
1601 static bool isRangeUngrammatical(EditorClient* client, Range *range, Vector<String>& guessesVector)
1602 {
1603     if (!client)
1604         return false;
1605
1606     ExceptionCode ec;
1607     if (!range || range->collapsed(ec))
1608         return false;
1609     
1610     // Returns true only if the passed range exactly corresponds to a bad grammar detail range. This is analogous
1611     // to isSelectionMisspelled. It's not good enough for there to be some bad grammar somewhere in the range,
1612     // or overlapping the range; the ranges must exactly match.
1613     guessesVector.clear();
1614     int grammarPhraseOffset;
1615     
1616     GrammarDetail grammarDetail;
1617     String badGrammarPhrase = findFirstBadGrammarInRange(client, range, grammarDetail, grammarPhraseOffset, false);    
1618     
1619     // No bad grammar in these parts at all.
1620     if (badGrammarPhrase.isEmpty())
1621         return false;
1622     
1623     // Bad grammar, but phrase (e.g. sentence) starts beyond start of range.
1624     if (grammarPhraseOffset > 0)
1625         return false;
1626     
1627     ASSERT(grammarDetail.location >= 0 && grammarDetail.length > 0);
1628     
1629     // Bad grammar, but start of detail (e.g. ungrammatical word) doesn't match start of range
1630     if (grammarDetail.location + grammarPhraseOffset != 0)
1631         return false;
1632     
1633     // Bad grammar at start of range, but end of bad grammar is before or after end of range
1634     if (grammarDetail.length != TextIterator::rangeLength(range))
1635         return false;
1636     
1637     // Update the spelling panel to be displaying this error (whether or not the spelling panel is on screen).
1638     // This is necessary to make a subsequent call to [NSSpellChecker ignoreWord:inSpellDocumentWithTag:] work
1639     // correctly; that call behaves differently based on whether the spelling panel is displaying a misspelling
1640     // or a grammar error.
1641     client->updateSpellingUIWithGrammarString(badGrammarPhrase, grammarDetail);
1642     
1643     return true;
1644 }
1645 #endif
1646
1647 bool Editor::isSelectionUngrammatical()
1648 {
1649 #ifdef BUILDING_ON_TIGER
1650     return false;
1651 #else
1652     Vector<String> ignoredGuesses;
1653     return isRangeUngrammatical(client(), frame()->selectionController()->toRange().get(), ignoredGuesses);
1654 #endif
1655 }
1656
1657 Vector<String> Editor::guessesForUngrammaticalSelection()
1658 {
1659 #ifdef BUILDING_ON_TIGER
1660     return Vector<String>();
1661 #else
1662     Vector<String> guesses;
1663     // Ignore the result of isRangeUngrammatical; we just want the guesses, whether or not there are any
1664     isRangeUngrammatical(client(), frame()->selectionController()->toRange().get(), guesses);
1665     return guesses;
1666 #endif
1667 }
1668
1669 Vector<String> Editor::guessesForMisspelledSelection()
1670 {
1671     String selectedString = frame()->selectedText();
1672     ASSERT(selectedString.length() != 0);
1673
1674     Vector<String> guesses;
1675     if (client())
1676         client()->getGuessesForWord(selectedString, guesses);
1677     return guesses;
1678 }
1679
1680 void Editor::showSpellingGuessPanel()
1681 {
1682     if (!client()) {
1683         LOG_ERROR("No NSSpellChecker");
1684         return;
1685     }
1686
1687 #ifndef BUILDING_ON_TIGER
1688     // Post-Tiger, this menu item is a show/hide toggle, to match AppKit. Leave Tiger behavior alone
1689     // to match rest of OS X.
1690     if (client()->spellingUIIsShowing()) {
1691         client()->showSpellingUI(false);
1692         return;
1693     }
1694 #endif
1695     
1696     advanceToNextMisspelling(true);
1697     client()->showSpellingUI(true);
1698 }
1699
1700 bool Editor::spellingPanelIsShowing()
1701 {
1702     if (!client())
1703         return false;
1704     return client()->spellingUIIsShowing();
1705 }
1706
1707 void Editor::markMisspellingsAfterTypingToPosition(const VisiblePosition &p)
1708 {
1709     if (!isContinuousSpellCheckingEnabled())
1710         return;
1711     
1712     // Check spelling of one word
1713     markMisspellings(Selection(startOfWord(p, LeftWordIfOnBoundary), endOfWord(p, RightWordIfOnBoundary)));
1714     
1715     if (!isGrammarCheckingEnabled())
1716         return;
1717     
1718     // Check grammar of entire sentence
1719     markBadGrammar(Selection(startOfSentence(p), endOfSentence(p)));
1720 }
1721
1722 static void markAllMisspellingsInRange(EditorClient* client, Range* searchRange)
1723 {
1724     // Use the "markAll" feature of findFirstMisspellingInRange. Ignore the return value and the "out parameter";
1725     // all we need to do is mark every instance.
1726     int ignoredOffset;
1727     findFirstMisspellingInRange(client, searchRange, ignoredOffset, true);
1728 }
1729
1730 #ifndef BUILDING_ON_TIGER
1731 static void markAllBadGrammarInRange(EditorClient* client, Range* searchRange)
1732 {
1733     // Use the "markAll" feature of findFirstBadGrammarInRange. Ignore the return value and "out parameters"; all we need to
1734     // do is mark every instance.
1735     GrammarDetail ignoredGrammarDetail;
1736     int ignoredOffset;
1737     findFirstBadGrammarInRange(client, searchRange, ignoredGrammarDetail, ignoredOffset, true);
1738 }
1739 #endif
1740     
1741 static void markMisspellingsOrBadGrammar(Editor* editor, const Selection& selection, bool checkSpelling)
1742 {
1743     // This function is called with a selection already expanded to word boundaries.
1744     // Might be nice to assert that here.
1745     
1746     // This function is used only for as-you-type checking, so if that's off we do nothing. Note that
1747     // grammar checking can only be on if spell checking is also on.
1748     if (!editor->isContinuousSpellCheckingEnabled())
1749         return;
1750     
1751     RefPtr<Range> searchRange(selection.toRange());
1752     if (!searchRange || searchRange->isDetached())
1753         return;
1754     
1755     // If we're not in an editable node, bail.
1756     int exception = 0;
1757     Node *editableNode = searchRange->startContainer(exception);
1758     if (!editableNode->isContentEditable())
1759         return;
1760     
1761     // Get the spell checker if it is available
1762     if (!editor->client())
1763         return;
1764     
1765     if (checkSpelling)
1766         markAllMisspellingsInRange(editor->client(), searchRange.get());
1767     else {
1768 #ifdef BUILDING_ON_TIGER
1769         ASSERT_NOT_REACHED();
1770 #else
1771         if (editor->isGrammarCheckingEnabled())
1772             markAllBadGrammarInRange(editor->client(), searchRange.get());
1773 #endif
1774     }    
1775 }
1776
1777 void Editor::markMisspellings(const Selection& selection)
1778 {
1779     markMisspellingsOrBadGrammar(this, selection, true);
1780 }
1781     
1782 void Editor::markBadGrammar(const Selection& selection)
1783 {
1784 #ifndef BUILDING_ON_TIGER
1785     markMisspellingsOrBadGrammar(this, selection, false);
1786 #endif
1787 }
1788
1789 PassRefPtr<Range> Editor::rangeForPoint(const IntPoint& windowPoint)
1790 {
1791     Document* document = m_frame->documentAtPoint(windowPoint);
1792     if (!document)
1793         return 0;
1794     
1795     Frame* frame = document->frame();
1796     ASSERT(frame);
1797     FrameView* frameView = frame->view();
1798     if (!frameView)
1799         return 0;
1800     IntPoint framePoint = frameView->windowToContents(windowPoint);
1801     Selection selection(frame->visiblePositionForPoint(framePoint));
1802     return avoidIntersectionWithNode(selection.toRange().get(), deleteButtonController() ? deleteButtonController()->containerElement() : 0);
1803 }
1804
1805 void Editor::revealSelectionAfterEditingOperation()
1806 {
1807     if (m_ignoreCompositionSelectionChange)
1808         return;
1809
1810     m_frame->revealSelection(RenderLayer::gAlignToEdgeIfNeeded);
1811 }
1812
1813 void Editor::setIgnoreCompositionSelectionChange(bool ignore)
1814 {
1815     if (m_ignoreCompositionSelectionChange == ignore)
1816         return;
1817
1818     m_ignoreCompositionSelectionChange = ignore;
1819     if (!ignore)
1820         revealSelectionAfterEditingOperation();
1821 }
1822
1823 PassRefPtr<Range> Editor::compositionRange() const
1824 {
1825     if (!m_compositionNode)
1826         return 0;
1827     unsigned length = m_compositionNode->length();
1828     unsigned start = min(m_compositionStart, length);
1829     unsigned end = min(max(start, m_compositionEnd), length);
1830     if (start >= end)
1831         return 0;
1832     return new Range(m_compositionNode->document(), m_compositionNode.get(), start, m_compositionNode.get(), end);
1833 }
1834
1835 bool Editor::getCompositionSelection(unsigned& selectionStart, unsigned& selectionEnd) const
1836 {
1837     if (!m_compositionNode)
1838         return false;
1839     Position start = m_frame->selectionController()->start();
1840     if (start.node() != m_compositionNode)
1841         return false;
1842     Position end = m_frame->selectionController()->end();
1843     if (end.node() != m_compositionNode)
1844         return false;
1845
1846     if (static_cast<unsigned>(start.offset()) < m_compositionStart)
1847         return false;
1848     if (static_cast<unsigned>(end.offset()) > m_compositionEnd)
1849         return false;
1850
1851     selectionStart = start.offset() - m_compositionStart;
1852     selectionEnd = start.offset() - m_compositionEnd;
1853     return true;
1854 }
1855
1856 void Editor::transpose()
1857 {
1858     if (!canEdit())
1859         return;
1860
1861      Selection selection = m_frame->selectionController()->selection();
1862      if (!selection.isCaret())
1863          return;
1864
1865     // Make a selection that goes back one character and forward two characters.
1866     VisiblePosition caret = selection.visibleStart();
1867     VisiblePosition next = isEndOfParagraph(caret) ? caret : caret.next();
1868     VisiblePosition previous = next.previous();
1869     if (next == previous)
1870         return;
1871     previous = previous.previous();
1872     if (!inSameParagraph(next, previous))
1873         return;
1874     RefPtr<Range> range = makeRange(previous, next);
1875     if (!range)
1876         return;
1877     Selection newSelection(range.get(), DOWNSTREAM);
1878
1879     // Transpose the two characters.
1880     String text = plainText(range.get());
1881     if (text.length() != 2)
1882         return;
1883     String transposed = text.right(1) + text.left(1);
1884
1885     // Select the two characters.
1886     if (newSelection != m_frame->selectionController()->selection()) {
1887         if (!m_frame->shouldChangeSelection(newSelection))
1888             return;
1889         m_frame->selectionController()->setSelection(newSelection);
1890     }
1891
1892     // Insert the transposed characters.
1893     if (!shouldInsertText(transposed, range.get(), EditorInsertActionTyped))
1894         return;
1895     replaceSelectionWithText(transposed, false, false);
1896 }
1897
1898 void Editor::addToKillRing(Range* range, bool prepend)
1899 {
1900     if (m_shouldStartNewKillRingSequence)
1901         startNewKillRingSequence();
1902
1903     String text = plainText(range);
1904     text.replace('\\', m_frame->backslashAsCurrencySymbol());
1905     if (prepend)
1906         prependToKillRing(text);
1907     else
1908         appendToKillRing(text);
1909     m_shouldStartNewKillRingSequence = false;
1910 }
1911
1912 #if !PLATFORM(MAC)
1913
1914 void Editor::appendToKillRing(const String&)
1915 {
1916 }
1917
1918 void Editor::prependToKillRing(const String&)
1919 {
1920 }
1921
1922 String Editor::yankFromKillRing()
1923 {
1924     return String();
1925 }
1926
1927 void Editor::startNewKillRingSequence()
1928 {
1929 }
1930
1931 void Editor::setKillRingToYankedState()
1932 {
1933 }
1934
1935 #endif
1936
1937 } // namespace WebCore