Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: Source/core/editing/TypingCommand.cpp

Issue 23822003: Have EditCommand classes deal with Document references, not pointers (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/editing/TypingCommand.h ('k') | Source/core/editing/UnlinkCommand.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 m_typingCommand->insertParagraphSeparator(); 66 m_typingCommand->insertParagraphSeparator();
67 } 67 }
68 } 68 }
69 69
70 private: 70 private:
71 TypingCommand* m_typingCommand; 71 TypingCommand* m_typingCommand;
72 bool m_selectInsertedText; 72 bool m_selectInsertedText;
73 const String& m_text; 73 const String& m_text;
74 }; 74 };
75 75
76 TypingCommand::TypingCommand(Document *document, ETypingCommand commandType, con st String &textToInsert, Options options, TextGranularity granularity, TextCompo sitionType compositionType) 76 TypingCommand::TypingCommand(Document& document, ETypingCommand commandType, con st String &textToInsert, Options options, TextGranularity granularity, TextCompo sitionType compositionType)
77 : TextInsertionBaseCommand(document) 77 : TextInsertionBaseCommand(document)
78 , m_commandType(commandType) 78 , m_commandType(commandType)
79 , m_textToInsert(textToInsert) 79 , m_textToInsert(textToInsert)
80 , m_openForMoreTyping(true) 80 , m_openForMoreTyping(true)
81 , m_selectInsertedText(options & SelectInsertedText) 81 , m_selectInsertedText(options & SelectInsertedText)
82 , m_smartDelete(options & SmartDelete) 82 , m_smartDelete(options & SmartDelete)
83 , m_granularity(granularity) 83 , m_granularity(granularity)
84 , m_compositionType(compositionType) 84 , m_compositionType(compositionType)
85 , m_killRing(options & KillRing) 85 , m_killRing(options & KillRing)
86 , m_openedByBackwardDelete(false) 86 , m_openedByBackwardDelete(false)
87 , m_shouldRetainAutocorrectionIndicator(options & RetainAutocorrectionIndica tor) 87 , m_shouldRetainAutocorrectionIndicator(options & RetainAutocorrectionIndica tor)
88 , m_shouldPreventSpellChecking(options & PreventSpellChecking) 88 , m_shouldPreventSpellChecking(options & PreventSpellChecking)
89 { 89 {
90 updatePreservesTypingStyle(m_commandType); 90 updatePreservesTypingStyle(m_commandType);
91 } 91 }
92 92
93 void TypingCommand::deleteSelection(Document* document, Options options) 93 void TypingCommand::deleteSelection(Document& document, Options options)
94 { 94 {
95 ASSERT(document); 95 Frame* frame = document.frame();
96
97 Frame* frame = document->frame();
98 ASSERT(frame); 96 ASSERT(frame);
99 97
100 if (!frame->selection()->isRange()) 98 if (!frame->selection()->isRange())
101 return; 99 return;
102 100
103 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(frame)) { 101 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(frame)) {
104 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSpellC hecking); 102 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSpellC hecking);
105 lastTypingCommand->deleteSelection(options & SmartDelete); 103 lastTypingCommand->deleteSelection(options & SmartDelete);
106 return; 104 return;
107 } 105 }
108 106
109 TypingCommand::create(document, DeleteSelection, "", options)->apply(); 107 TypingCommand::create(document, DeleteSelection, "", options)->apply();
110 } 108 }
111 109
112 void TypingCommand::deleteKeyPressed(Document *document, Options options, TextGr anularity granularity) 110 void TypingCommand::deleteKeyPressed(Document& document, Options options, TextGr anularity granularity)
113 { 111 {
114 ASSERT(document);
115 if (granularity == CharacterGranularity) { 112 if (granularity == CharacterGranularity) {
116 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOp enForTyping(document->frame())) { 113 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOp enForTyping(document.frame())) {
117 updateSelectionIfDifferentFromCurrentSelection(lastTypingCommand.get (), document->frame()); 114 updateSelectionIfDifferentFromCurrentSelection(lastTypingCommand.get (), document.frame());
118 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSp ellChecking); 115 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSp ellChecking);
119 lastTypingCommand->deleteKeyPressed(granularity, options & KillRing) ; 116 lastTypingCommand->deleteKeyPressed(granularity, options & KillRing) ;
120 return; 117 return;
121 } 118 }
122 } 119 }
123 120
124 TypingCommand::create(document, DeleteKey, "", options, granularity)->apply( ); 121 TypingCommand::create(document, DeleteKey, "", options, granularity)->apply( );
125 } 122 }
126 123
127 void TypingCommand::forwardDeleteKeyPressed(Document *document, Options options, TextGranularity granularity) 124 void TypingCommand::forwardDeleteKeyPressed(Document& document, Options options, TextGranularity granularity)
128 { 125 {
129 // FIXME: Forward delete in TextEdit appears to open and close a new typing command. 126 // FIXME: Forward delete in TextEdit appears to open and close a new typing command.
130 ASSERT(document); 127 Frame* frame = document.frame();
131 Frame* frame = document->frame();
132 if (granularity == CharacterGranularity) { 128 if (granularity == CharacterGranularity) {
133 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOp enForTyping(frame)) { 129 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOp enForTyping(frame)) {
134 updateSelectionIfDifferentFromCurrentSelection(lastTypingCommand.get (), frame); 130 updateSelectionIfDifferentFromCurrentSelection(lastTypingCommand.get (), frame);
135 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSp ellChecking); 131 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSp ellChecking);
136 lastTypingCommand->forwardDeleteKeyPressed(granularity, options & Ki llRing); 132 lastTypingCommand->forwardDeleteKeyPressed(granularity, options & Ki llRing);
137 return; 133 return;
138 } 134 }
139 } 135 }
140 136
141 TypingCommand::create(document, ForwardDeleteKey, "", options, granularity)- >apply(); 137 TypingCommand::create(document, ForwardDeleteKey, "", options, granularity)- >apply();
142 } 138 }
143 139
144 void TypingCommand::updateSelectionIfDifferentFromCurrentSelection(TypingCommand * typingCommand, Frame* frame) 140 void TypingCommand::updateSelectionIfDifferentFromCurrentSelection(TypingCommand * typingCommand, Frame* frame)
145 { 141 {
146 ASSERT(frame); 142 ASSERT(frame);
147 VisibleSelection currentSelection = frame->selection()->selection(); 143 VisibleSelection currentSelection = frame->selection()->selection();
148 if (currentSelection == typingCommand->endingSelection()) 144 if (currentSelection == typingCommand->endingSelection())
149 return; 145 return;
150 146
151 typingCommand->setStartingSelection(currentSelection); 147 typingCommand->setStartingSelection(currentSelection);
152 typingCommand->setEndingSelection(currentSelection); 148 typingCommand->setEndingSelection(currentSelection);
153 } 149 }
154 150
155 void TypingCommand::insertText(Document* document, const String& text, Options o ptions, TextCompositionType composition) 151 void TypingCommand::insertText(Document& document, const String& text, Options o ptions, TextCompositionType composition)
156 { 152 {
157 ASSERT(document); 153 Frame* frame = document.frame();
158
159 Frame* frame = document->frame();
160 ASSERT(frame); 154 ASSERT(frame);
161 155
162 if (!text.isEmpty()) 156 if (!text.isEmpty())
163 document->frame()->editor().updateMarkersForWordsAffectedByEditing(isSpa ceOrNewline(text[0])); 157 document.frame()->editor().updateMarkersForWordsAffectedByEditing(isSpac eOrNewline(text[0]));
164 158
165 insertText(document, text, frame->selection()->selection(), options, composi tion); 159 insertText(document, text, frame->selection()->selection(), options, composi tion);
166 } 160 }
167 161
168 // FIXME: We shouldn't need to take selectionForInsertion. It should be identica l to FrameSelection's current selection. 162 // FIXME: We shouldn't need to take selectionForInsertion. It should be identica l to FrameSelection's current selection.
169 void TypingCommand::insertText(Document* document, const String& text, const Vis ibleSelection& selectionForInsertion, Options options, TextCompositionType compo sitionType) 163 void TypingCommand::insertText(Document& document, const String& text, const Vis ibleSelection& selectionForInsertion, Options options, TextCompositionType compo sitionType)
170 { 164 {
171 ASSERT(document); 165 RefPtr<Frame> frame = document.frame();
172
173 RefPtr<Frame> frame = document->frame();
174 ASSERT(frame); 166 ASSERT(frame);
175 167
176 VisibleSelection currentSelection = frame->selection()->selection(); 168 VisibleSelection currentSelection = frame->selection()->selection();
177 169
178 String newText = dispatchBeforeTextInsertedEvent(text, selectionForInsertion , compositionType == TextCompositionUpdate); 170 String newText = dispatchBeforeTextInsertedEvent(text, selectionForInsertion , compositionType == TextCompositionUpdate);
179 171
180 // Set the starting and ending selection appropriately if we are using a sel ection 172 // Set the starting and ending selection appropriately if we are using a sel ection
181 // that is different from the current selection. In the future, we should c hange EditCommand 173 // that is different from the current selection. In the future, we should c hange EditCommand
182 // to deal with custom selections in a general way that can be used by all o f the commands. 174 // to deal with custom selections in a general way that can be used by all o f the commands.
183 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(frame.get())) { 175 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(frame.get())) {
184 if (lastTypingCommand->endingSelection() != selectionForInsertion) { 176 if (lastTypingCommand->endingSelection() != selectionForInsertion) {
185 lastTypingCommand->setStartingSelection(selectionForInsertion); 177 lastTypingCommand->setStartingSelection(selectionForInsertion);
186 lastTypingCommand->setEndingSelection(selectionForInsertion); 178 lastTypingCommand->setEndingSelection(selectionForInsertion);
187 } 179 }
188 180
189 lastTypingCommand->setCompositionType(compositionType); 181 lastTypingCommand->setCompositionType(compositionType);
190 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator); 182 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator);
191 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSpellC hecking); 183 lastTypingCommand->setShouldPreventSpellChecking(options & PreventSpellC hecking);
192 lastTypingCommand->insertText(newText, options & SelectInsertedText); 184 lastTypingCommand->insertText(newText, options & SelectInsertedText);
193 return; 185 return;
194 } 186 }
195 187
196 RefPtr<TypingCommand> cmd = TypingCommand::create(document, InsertText, newT ext, options, compositionType); 188 RefPtr<TypingCommand> cmd = TypingCommand::create(document, InsertText, newT ext, options, compositionType);
197 applyTextInsertionCommand(frame.get(), cmd, selectionForInsertion, currentSe lection); 189 applyTextInsertionCommand(frame.get(), cmd, selectionForInsertion, currentSe lection);
198 } 190 }
199 191
200 void TypingCommand::insertLineBreak(Document *document, Options options) 192 void TypingCommand::insertLineBreak(Document& document, Options options)
201 { 193 {
202 ASSERT(document); 194 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document.frame())) {
203 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document->frame())) {
204 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator); 195 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator);
205 lastTypingCommand->insertLineBreak(); 196 lastTypingCommand->insertLineBreak();
206 return; 197 return;
207 } 198 }
208 199
209 applyCommand(TypingCommand::create(document, InsertLineBreak, "", options)); 200 applyCommand(TypingCommand::create(document, InsertLineBreak, "", options));
210 } 201 }
211 202
212 void TypingCommand::insertParagraphSeparatorInQuotedContent(Document *document) 203 void TypingCommand::insertParagraphSeparatorInQuotedContent(Document& document)
213 { 204 {
214 ASSERT(document); 205 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document.frame())) {
215 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document->frame())) {
216 lastTypingCommand->insertParagraphSeparatorInQuotedContent(); 206 lastTypingCommand->insertParagraphSeparatorInQuotedContent();
217 return; 207 return;
218 } 208 }
219 209
220 applyCommand(TypingCommand::create(document, InsertParagraphSeparatorInQuote dContent)); 210 applyCommand(TypingCommand::create(document, InsertParagraphSeparatorInQuote dContent));
221 } 211 }
222 212
223 void TypingCommand::insertParagraphSeparator(Document *document, Options options ) 213 void TypingCommand::insertParagraphSeparator(Document& document, Options options )
224 { 214 {
225 ASSERT(document); 215 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document.frame())) {
226 if (RefPtr<TypingCommand> lastTypingCommand = lastTypingCommandIfStillOpenFo rTyping(document->frame())) {
227 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator); 216 lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & Reta inAutocorrectionIndicator);
228 lastTypingCommand->insertParagraphSeparator(); 217 lastTypingCommand->insertParagraphSeparator();
229 return; 218 return;
230 } 219 }
231 220
232 applyCommand(TypingCommand::create(document, InsertParagraphSeparator, "", o ptions)); 221 applyCommand(TypingCommand::create(document, InsertParagraphSeparator, "", o ptions));
233 } 222 }
234 223
235 PassRefPtr<TypingCommand> TypingCommand::lastTypingCommandIfStillOpenForTyping(F rame* frame) 224 PassRefPtr<TypingCommand> TypingCommand::lastTypingCommandIfStillOpenForTyping(F rame* frame)
236 { 225 {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 ASSERT_NOT_REACHED(); 274 ASSERT_NOT_REACHED();
286 } 275 }
287 276
288 EditAction TypingCommand::editingAction() const 277 EditAction TypingCommand::editingAction() const
289 { 278 {
290 return EditActionTyping; 279 return EditActionTyping;
291 } 280 }
292 281
293 void TypingCommand::markMisspellingsAfterTyping(ETypingCommand commandType) 282 void TypingCommand::markMisspellingsAfterTyping(ETypingCommand commandType)
294 { 283 {
295 Frame* frame = document()->frame(); 284 Frame* frame = document().frame();
296 if (!frame) 285 if (!frame)
297 return; 286 return;
298 287
299 if (!frame->editor().isContinuousSpellCheckingEnabled()) 288 if (!frame->editor().isContinuousSpellCheckingEnabled())
300 return; 289 return;
301 290
302 frame->editor().spellCheckRequester().cancelCheck(); 291 frame->editor().spellCheckRequester().cancelCheck();
303 292
304 // Take a look at the selection that results after typing and determine whet her we need to spellcheck. 293 // Take a look at the selection that results after typing and determine whet her we need to spellcheck.
305 // Since the word containing the current selection is never marked, this doe s a check to 294 // Since the word containing the current selection is never marked, this doe s a check to
306 // see if typing made a new word that is not in the current selection. Basic ally, you 295 // see if typing made a new word that is not in the current selection. Basic ally, you
307 // get this by being at the end of a word and typing a space. 296 // get this by being at the end of a word and typing a space.
308 VisiblePosition start(endingSelection().start(), endingSelection().affinity( )); 297 VisiblePosition start(endingSelection().start(), endingSelection().affinity( ));
309 VisiblePosition previous = start.previous(); 298 VisiblePosition previous = start.previous();
310 if (previous.isNotNull()) { 299 if (previous.isNotNull()) {
311 VisiblePosition p1 = startOfWord(previous, LeftWordIfOnBoundary); 300 VisiblePosition p1 = startOfWord(previous, LeftWordIfOnBoundary);
312 VisiblePosition p2 = startOfWord(start, LeftWordIfOnBoundary); 301 VisiblePosition p2 = startOfWord(start, LeftWordIfOnBoundary);
313 if (p1 != p2) 302 if (p1 != p2)
314 frame->editor().markMisspellingsAfterTypingToWord(p1, endingSelectio n()); 303 frame->editor().markMisspellingsAfterTypingToWord(p1, endingSelectio n());
315 } 304 }
316 } 305 }
317 306
318 void TypingCommand::typingAddedToOpenCommand(ETypingCommand commandTypeForAddedT yping) 307 void TypingCommand::typingAddedToOpenCommand(ETypingCommand commandTypeForAddedT yping)
319 { 308 {
320 Frame* frame = document()->frame(); 309 Frame* frame = document().frame();
321 if (!frame) 310 if (!frame)
322 return; 311 return;
323 312
324 updatePreservesTypingStyle(commandTypeForAddedTyping); 313 updatePreservesTypingStyle(commandTypeForAddedTyping);
325 314
326 // The old spellchecking code requires that checking be done first, to preve nt issues like that in 6864072, where <doesn't> is marked as misspelled. 315 // The old spellchecking code requires that checking be done first, to preve nt issues like that in 6864072, where <doesn't> is marked as misspelled.
327 markMisspellingsAfterTyping(commandTypeForAddedTyping); 316 markMisspellingsAfterTyping(commandTypeForAddedTyping);
328 frame->editor().appliedEditing(this); 317 frame->editor().appliedEditing(this);
329 } 318 }
330 319
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 removeNode(child); 385 removeNode(child);
397 386
398 addBlockPlaceholderIfNeeded(root); 387 addBlockPlaceholderIfNeeded(root);
399 setEndingSelection(VisibleSelection(firstPositionInNode(root), DOWNSTREAM, e ndingSelection().isDirectional())); 388 setEndingSelection(VisibleSelection(firstPositionInNode(root), DOWNSTREAM, e ndingSelection().isDirectional()));
400 389
401 return true; 390 return true;
402 } 391 }
403 392
404 void TypingCommand::deleteKeyPressed(TextGranularity granularity, bool killRing) 393 void TypingCommand::deleteKeyPressed(TextGranularity granularity, bool killRing)
405 { 394 {
406 Frame* frame = document()->frame(); 395 Frame* frame = document().frame();
407 if (!frame) 396 if (!frame)
408 return; 397 return;
409 398
410 frame->editor().updateMarkersForWordsAffectedByEditing(false); 399 frame->editor().updateMarkersForWordsAffectedByEditing(false);
411 400
412 VisibleSelection selectionToDelete; 401 VisibleSelection selectionToDelete;
413 VisibleSelection selectionAfterUndo; 402 VisibleSelection selectionAfterUndo;
414 403
415 switch (endingSelection().selectionType()) { 404 switch (endingSelection().selectionType()) {
416 case VisibleSelection::RangeSelection: 405 case VisibleSelection::RangeSelection:
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 // more text than you insert. In that case all of the text that was around originally should be selected. 489 // more text than you insert. In that case all of the text that was around originally should be selected.
501 if (m_openedByBackwardDelete) 490 if (m_openedByBackwardDelete)
502 setStartingSelection(selectionAfterUndo); 491 setStartingSelection(selectionAfterUndo);
503 CompositeEditCommand::deleteSelection(selectionToDelete, m_smartDelete); 492 CompositeEditCommand::deleteSelection(selectionToDelete, m_smartDelete);
504 setSmartDelete(false); 493 setSmartDelete(false);
505 typingAddedToOpenCommand(DeleteKey); 494 typingAddedToOpenCommand(DeleteKey);
506 } 495 }
507 496
508 void TypingCommand::forwardDeleteKeyPressed(TextGranularity granularity, bool ki llRing) 497 void TypingCommand::forwardDeleteKeyPressed(TextGranularity granularity, bool ki llRing)
509 { 498 {
510 Frame* frame = document()->frame(); 499 Frame* frame = document().frame();
511 if (!frame) 500 if (!frame)
512 return; 501 return;
513 502
514 frame->editor().updateMarkersForWordsAffectedByEditing(false); 503 frame->editor().updateMarkersForWordsAffectedByEditing(false);
515 504
516 VisibleSelection selectionToDelete; 505 VisibleSelection selectionToDelete;
517 VisibleSelection selectionAfterUndo; 506 VisibleSelection selectionAfterUndo;
518 507
519 switch (endingSelection().selectionType()) { 508 switch (endingSelection().selectionType()) {
520 case VisibleSelection::RangeSelection: 509 case VisibleSelection::RangeSelection:
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 ASSERT_NOT_REACHED(); 608 ASSERT_NOT_REACHED();
620 m_preservesTypingStyle = false; 609 m_preservesTypingStyle = false;
621 } 610 }
622 611
623 bool TypingCommand::isTypingCommand() const 612 bool TypingCommand::isTypingCommand() const
624 { 613 {
625 return true; 614 return true;
626 } 615 }
627 616
628 } // namespace WebCore 617 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/editing/TypingCommand.h ('k') | Source/core/editing/UnlinkCommand.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698