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

Side by Side Diff: Source/core/editing/InsertParagraphSeparatorCommand.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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2005, 2006 Apple Computer, 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // We don't want to return a root node (if it happens to be a div, e.g., in a document fragment) because there are no 51 // We don't want to return a root node (if it happens to be a div, e.g., in a document fragment) because there are no
52 // siblings for us to append to. 52 // siblings for us to append to.
53 while (!curBlock->nextSibling() && curBlock->parentElement()->hasTagName(div Tag) && curBlock->parentElement()->parentElement()) { 53 while (!curBlock->nextSibling() && curBlock->parentElement()->hasTagName(div Tag) && curBlock->parentElement()->parentElement()) {
54 if (curBlock->parentElement()->hasAttributes()) 54 if (curBlock->parentElement()->hasAttributes())
55 break; 55 break;
56 curBlock = curBlock->parentElement(); 56 curBlock = curBlock->parentElement();
57 } 57 }
58 return curBlock; 58 return curBlock;
59 } 59 }
60 60
61 InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document *docum ent, bool mustUseDefaultParagraphElement, bool pasteBlockqutoeIntoUnquotedArea) 61 InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document& docum ent, bool mustUseDefaultParagraphElement, bool pasteBlockqutoeIntoUnquotedArea)
62 : CompositeEditCommand(document) 62 : CompositeEditCommand(document)
63 , m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement) 63 , m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement)
64 , m_pasteBlockqutoeIntoUnquotedArea(pasteBlockqutoeIntoUnquotedArea) 64 , m_pasteBlockqutoeIntoUnquotedArea(pasteBlockqutoeIntoUnquotedArea)
65 { 65 {
66 } 66 }
67 67
68 bool InsertParagraphSeparatorCommand::preservesTypingStyle() const 68 bool InsertParagraphSeparatorCommand::preservesTypingStyle() const
69 { 69 {
70 return true; 70 return true;
71 } 71 }
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 //--------------------------------------------------------------------- 194 //---------------------------------------------------------------------
195 // Prepare for more general cases. 195 // Prepare for more general cases.
196 196
197 bool isFirstInBlock = isStartOfBlock(visiblePos); 197 bool isFirstInBlock = isStartOfBlock(visiblePos);
198 bool isLastInBlock = isEndOfBlock(visiblePos); 198 bool isLastInBlock = isEndOfBlock(visiblePos);
199 bool nestNewBlock = false; 199 bool nestNewBlock = false;
200 200
201 // Create block to be inserted. 201 // Create block to be inserted.
202 RefPtr<Element> blockToInsert; 202 RefPtr<Element> blockToInsert;
203 if (startBlock->isRootEditableElement()) { 203 if (startBlock->isRootEditableElement()) {
204 blockToInsert = createDefaultParagraphElement(document()); 204 blockToInsert = createDefaultParagraphElement(&document());
205 nestNewBlock = true; 205 nestNewBlock = true;
206 } else if (shouldUseDefaultParagraphElement(startBlock.get())) 206 } else if (shouldUseDefaultParagraphElement(startBlock.get())) {
207 blockToInsert = createDefaultParagraphElement(document()); 207 blockToInsert = createDefaultParagraphElement(&document());
208 else 208 } else {
209 blockToInsert = startBlock->cloneElementWithoutChildren(); 209 blockToInsert = startBlock->cloneElementWithoutChildren();
210 }
210 211
211 //--------------------------------------------------------------------- 212 //---------------------------------------------------------------------
212 // Handle case when position is in the last visible position in its block, 213 // Handle case when position is in the last visible position in its block,
213 // including when the block is empty. 214 // including when the block is empty.
214 if (isLastInBlock) { 215 if (isLastInBlock) {
215 if (nestNewBlock) { 216 if (nestNewBlock) {
216 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) { 217 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
217 // The block is empty. Create an empty block to 218 // The block is empty. Create an empty block to
218 // represent the paragraph that we're leaving. 219 // represent the paragraph that we're leaving.
219 RefPtr<Element> extraBlock = createDefaultParagraphElement(docum ent()); 220 RefPtr<Element> extraBlock = createDefaultParagraphElement(&docu ment());
220 appendNode(extraBlock, startBlock); 221 appendNode(extraBlock, startBlock);
221 appendBlockPlaceholder(extraBlock); 222 appendBlockPlaceholder(extraBlock);
222 } 223 }
223 appendNode(blockToInsert, startBlock); 224 appendNode(blockToInsert, startBlock);
224 } else { 225 } else {
225 // We can get here if we pasted a copied portion of a blockquote wit h a newline at the end and are trying to paste it 226 // We can get here if we pasted a copied portion of a blockquote wit h a newline at the end and are trying to paste it
226 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted. 227 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted.
227 if (m_pasteBlockqutoeIntoUnquotedArea) { 228 if (m_pasteBlockqutoeIntoUnquotedArea) {
228 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote)) 229 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote))
229 startBlock = toElement(highestBlockquote); 230 startBlock = toElement(highestBlockquote);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 } 290 }
290 291
291 //--------------------------------------------------------------------- 292 //---------------------------------------------------------------------
292 // Handle the (more complicated) general case, 293 // Handle the (more complicated) general case,
293 294
294 // All of the content in the current block after visiblePos is 295 // All of the content in the current block after visiblePos is
295 // about to be wrapped in a new paragraph element. Add a br before 296 // about to be wrapped in a new paragraph element. Add a br before
296 // it if visiblePos is at the start of a paragraph so that the 297 // it if visiblePos is at the start of a paragraph so that the
297 // content will move down a line. 298 // content will move down a line.
298 if (isStartOfParagraph(visiblePos)) { 299 if (isStartOfParagraph(visiblePos)) {
299 RefPtr<Element> br = createBreakElement(document()); 300 RefPtr<Element> br = createBreakElement(&document());
300 insertNodeAt(br.get(), insertionPosition); 301 insertNodeAt(br.get(), insertionPosition);
301 insertionPosition = positionInParentAfterNode(br.get()); 302 insertionPosition = positionInParentAfterNode(br.get());
302 // If the insertion point is a break element, there is nothing else 303 // If the insertion point is a break element, there is nothing else
303 // we need to do. 304 // we need to do.
304 if (visiblePos.deepEquivalent().anchorNode()->renderer()->isBR()) { 305 if (visiblePos.deepEquivalent().anchorNode()->renderer()->isBR()) {
305 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, e ndingSelection().isDirectional())); 306 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, e ndingSelection().isDirectional()));
306 return; 307 return;
307 } 308 }
308 } 309 }
309 310
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 // If we got detached due to mutation events, just bail out. 353 // If we got detached due to mutation events, just bail out.
353 if (!startBlock->parentNode()) 354 if (!startBlock->parentNode())
354 return; 355 return;
355 356
356 // Put the added block in the tree. 357 // Put the added block in the tree.
357 if (nestNewBlock) 358 if (nestNewBlock)
358 appendNode(blockToInsert.get(), startBlock); 359 appendNode(blockToInsert.get(), startBlock);
359 else 360 else
360 insertNodeAfter(blockToInsert.get(), startBlock); 361 insertNodeAfter(blockToInsert.get(), startBlock);
361 362
362 document()->updateLayoutIgnorePendingStylesheets(); 363 document().updateLayoutIgnorePendingStylesheets();
363 364
364 // If the paragraph separator was inserted at the end of a paragraph, an emp ty line must be 365 // If the paragraph separator was inserted at the end of a paragraph, an emp ty line must be
365 // created. All of the nodes, starting at visiblePos, are about to be added to the new paragraph 366 // created. All of the nodes, starting at visiblePos, are about to be added to the new paragraph
366 // element. If the first node to be inserted won't be one that will hold an empty line open, add a br. 367 // element. If the first node to be inserted won't be one that will hold an empty line open, add a br.
367 if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtVisiblePosition(visibl ePos)) 368 if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtVisiblePosition(visibl ePos))
368 appendNode(createBreakElement(document()).get(), blockToInsert.get()); 369 appendNode(createBreakElement(&document()).get(), blockToInsert.get());
369 370
370 // Move the start node and the siblings of the start node. 371 // Move the start node and the siblings of the start node.
371 if (VisiblePosition(insertionPosition) != VisiblePosition(positionBeforeNode (blockToInsert.get()))) { 372 if (VisiblePosition(insertionPosition) != VisiblePosition(positionBeforeNode (blockToInsert.get()))) {
372 Node* n; 373 Node* n;
373 if (insertionPosition.containerNode() == startBlock) 374 if (insertionPosition.containerNode() == startBlock)
374 n = insertionPosition.computeNodeAfterPosition(); 375 n = insertionPosition.computeNodeAfterPosition();
375 else { 376 else {
376 Node* splitTo = insertionPosition.containerNode(); 377 Node* splitTo = insertionPosition.containerNode();
377 if (splitTo->isTextNode() && insertionPosition.offsetInContainerNode () >= caretMaxOffset(splitTo)) 378 if (splitTo->isTextNode() && insertionPosition.offsetInContainerNode () >= caretMaxOffset(splitTo))
378 splitTo = NodeTraversal::next(splitTo, startBlock.get()); 379 splitTo = NodeTraversal::next(splitTo, startBlock.get());
379 ASSERT(splitTo); 380 ASSERT(splitTo);
380 splitTreeToNode(splitTo, startBlock.get()); 381 splitTreeToNode(splitTo, startBlock.get());
381 382
382 for (n = startBlock->firstChild(); n; n = n->nextSibling()) { 383 for (n = startBlock->firstChild(); n; n = n->nextSibling()) {
383 VisiblePosition beforeNodePosition = positionBeforeNode(n); 384 VisiblePosition beforeNodePosition = positionBeforeNode(n);
384 if (!beforeNodePosition.isNull() && comparePositions(VisiblePosi tion(insertionPosition), beforeNodePosition) <= 0) 385 if (!beforeNodePosition.isNull() && comparePositions(VisiblePosi tion(insertionPosition), beforeNodePosition) <= 0)
385 break; 386 break;
386 } 387 }
387 } 388 }
388 389
389 moveRemainingSiblingsToNewParent(n, blockToInsert.get(), blockToInsert); 390 moveRemainingSiblingsToNewParent(n, blockToInsert.get(), blockToInsert);
390 } 391 }
391 392
392 // Handle whitespace that occurs after the split 393 // Handle whitespace that occurs after the split
393 if (positionAfterSplit.isNotNull()) { 394 if (positionAfterSplit.isNotNull()) {
394 document()->updateLayoutIgnorePendingStylesheets(); 395 document().updateLayoutIgnorePendingStylesheets();
395 if (!positionAfterSplit.isRenderedCharacter()) { 396 if (!positionAfterSplit.isRenderedCharacter()) {
396 // Clear out all whitespace and insert one non-breaking space 397 // Clear out all whitespace and insert one non-breaking space
397 ASSERT(!positionAfterSplit.containerNode()->renderer() || positionAf terSplit.containerNode()->renderer()->style()->collapseWhiteSpace()); 398 ASSERT(!positionAfterSplit.containerNode()->renderer() || positionAf terSplit.containerNode()->renderer()->style()->collapseWhiteSpace());
398 deleteInsignificantTextDownstream(positionAfterSplit); 399 deleteInsignificantTextDownstream(positionAfterSplit);
399 if (positionAfterSplit.deprecatedNode()->isTextNode()) 400 if (positionAfterSplit.deprecatedNode()->isTextNode())
400 insertTextIntoNode(toText(positionAfterSplit.containerNode()), 0 , nonBreakingSpaceString()); 401 insertTextIntoNode(toText(positionAfterSplit.containerNode()), 0 , nonBreakingSpaceString());
401 } 402 }
402 } 403 }
403 404
404 setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()) , DOWNSTREAM, endingSelection().isDirectional())); 405 setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()) , DOWNSTREAM, endingSelection().isDirectional()));
405 applyStyleAfterInsertion(startBlock.get()); 406 applyStyleAfterInsertion(startBlock.get());
406 } 407 }
407 408
408 } // namespace WebCore 409 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/editing/InsertParagraphSeparatorCommand.h ('k') | Source/core/editing/InsertTextCommand.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698