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

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

Issue 423003003: Use tighter typing in editing: InsertParagraphSeparatorCommand / InsertTextCommand (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 4 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // 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 53 // 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
54 // siblings for us to append to. 54 // siblings for us to append to.
55 while (!curBlock->nextSibling() && isHTMLDivElement(*curBlock->parentElement ()) && curBlock->parentElement()->parentElement()) { 55 while (!curBlock->nextSibling() && isHTMLDivElement(*curBlock->parentElement ()) && curBlock->parentElement()->parentElement()) {
56 if (curBlock->parentElement()->hasAttributes()) 56 if (curBlock->parentElement()->hasAttributes())
57 break; 57 break;
58 curBlock = curBlock->parentElement(); 58 curBlock = curBlock->parentElement();
59 } 59 }
60 return curBlock; 60 return curBlock;
61 } 61 }
62 62
63 InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document& docum ent, bool mustUseDefaultParagraphElement, bool pasteBlockqutoeIntoUnquotedArea) 63 InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document& docum ent, bool mustUseDefaultParagraphElement, bool pasteBlockquoteIntoUnquotedArea)
64 : CompositeEditCommand(document) 64 : CompositeEditCommand(document)
65 , m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement) 65 , m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement)
66 , m_pasteBlockqutoeIntoUnquotedArea(pasteBlockqutoeIntoUnquotedArea) 66 , m_pasteBlockquoteIntoUnquotedArea(pasteBlockquoteIntoUnquotedArea)
67 { 67 {
68 } 68 }
69 69
70 bool InsertParagraphSeparatorCommand::preservesTypingStyle() const 70 bool InsertParagraphSeparatorCommand::preservesTypingStyle() const
71 { 71 {
72 return true; 72 return true;
73 } 73 }
74 74
75 void InsertParagraphSeparatorCommand::calculateStyleBeforeInsertion(const Positi on &pos) 75 void InsertParagraphSeparatorCommand::calculateStyleBeforeInsertion(const Positi on &pos)
76 { 76 {
77 // It is only important to set a style to apply later if we're at the bounda ries of 77 // It is only important to set a style to apply later if we're at the bounda ries of
78 // a paragraph. Otherwise, content that is moved as part of the work of the command 78 // a paragraph. Otherwise, content that is moved as part of the work of the command
79 // will lend their styles to the new paragraph without any extra work needed . 79 // will lend their styles to the new paragraph without any extra work needed .
80 VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY); 80 VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
81 if (!isStartOfParagraph(visiblePos) && !isEndOfParagraph(visiblePos)) 81 if (!isStartOfParagraph(visiblePos) && !isEndOfParagraph(visiblePos))
82 return; 82 return;
83 83
84 ASSERT(pos.isNotNull()); 84 ASSERT(pos.isNotNull());
85 m_style = EditingStyle::create(pos); 85 m_style = EditingStyle::create(pos);
86 m_style->mergeTypingStyle(pos.document()); 86 m_style->mergeTypingStyle(pos.document());
87 } 87 }
88 88
89 void InsertParagraphSeparatorCommand::applyStyleAfterInsertion(Node* originalEnc losingBlock) 89 void InsertParagraphSeparatorCommand::applyStyleAfterInsertion(Element* original EnclosingBlock)
90 { 90 {
91 // Not only do we break out of header tags, but we also do not preserve the typing style, 91 // Not only do we break out of header tags, but we also do not preserve the typing style,
92 // in order to match other browsers. 92 // in order to match other browsers.
93 if (originalEnclosingBlock->hasTagName(h1Tag) || 93 if (originalEnclosingBlock->hasTagName(h1Tag) ||
94 originalEnclosingBlock->hasTagName(h2Tag) || 94 originalEnclosingBlock->hasTagName(h2Tag) ||
95 originalEnclosingBlock->hasTagName(h3Tag) || 95 originalEnclosingBlock->hasTagName(h3Tag) ||
96 originalEnclosingBlock->hasTagName(h4Tag) || 96 originalEnclosingBlock->hasTagName(h4Tag) ||
97 originalEnclosingBlock->hasTagName(h5Tag)) 97 originalEnclosingBlock->hasTagName(h5Tag))
98 return; 98 return;
99 99
100 if (!m_style) 100 if (!m_style)
101 return; 101 return;
102 102
103 m_style->prepareToApplyAt(endingSelection().start()); 103 m_style->prepareToApplyAt(endingSelection().start());
104 if (!m_style->isEmpty()) 104 if (!m_style->isEmpty())
105 applyStyle(m_style.get()); 105 applyStyle(m_style.get());
106 } 106 }
107 107
108 bool InsertParagraphSeparatorCommand::shouldUseDefaultParagraphElement(Node* enc losingBlock) const 108 bool InsertParagraphSeparatorCommand::shouldUseDefaultParagraphElement(Element* enclosingBlock) const
109 { 109 {
110 if (m_mustUseDefaultParagraphElement) 110 if (m_mustUseDefaultParagraphElement)
111 return true; 111 return true;
112 112
113 // Assumes that if there was a range selection, it was already deleted. 113 // Assumes that if there was a range selection, it was already deleted.
114 if (!isEndOfBlock(endingSelection().visibleStart())) 114 if (!isEndOfBlock(endingSelection().visibleStart()))
115 return false; 115 return false;
116 116
117 return enclosingBlock->hasTagName(h1Tag) || 117 return enclosingBlock->hasTagName(h1Tag) ||
118 enclosingBlock->hasTagName(h2Tag) || 118 enclosingBlock->hasTagName(h2Tag) ||
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 if (endingSelection().isRange()) { 160 if (endingSelection().isRange()) {
161 calculateStyleBeforeInsertion(insertionPosition); 161 calculateStyleBeforeInsertion(insertionPosition);
162 deleteSelection(false, true); 162 deleteSelection(false, true);
163 insertionPosition = endingSelection().start(); 163 insertionPosition = endingSelection().start();
164 affinity = endingSelection().affinity(); 164 affinity = endingSelection().affinity();
165 } 165 }
166 166
167 // FIXME: The parentAnchoredEquivalent conversion needs to be moved into enc losingBlock. 167 // FIXME: The parentAnchoredEquivalent conversion needs to be moved into enc losingBlock.
168 RefPtrWillBeRawPtr<Element> startBlock = enclosingBlock(insertionPosition.pa rentAnchoredEquivalent().containerNode()); 168 RefPtrWillBeRawPtr<Element> startBlock = enclosingBlock(insertionPosition.pa rentAnchoredEquivalent().containerNode());
169 Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEqu ivalent().containerNode()); 169 Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEqu ivalent().containerNode());
170 RefPtrWillBeRawPtr<Element> listChild = listChildNode && listChildNode->isHT MLElement() ? toHTMLElement(listChildNode) : 0; 170 RefPtrWillBeRawPtr<HTMLElement> listChild = listChildNode && listChildNode-> isHTMLElement() ? toHTMLElement(listChildNode) : 0;
171 Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent(); 171 Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent();
172 if (!startBlock 172 if (!startBlock
173 || !startBlock->nonShadowBoundaryParentNode() 173 || !startBlock->nonShadowBoundaryParentNode()
174 || isTableCell(startBlock.get()) 174 || isTableCell(startBlock.get())
175 || isHTMLFormElement(*startBlock) 175 || isHTMLFormElement(*startBlock)
176 // FIXME: If the node is hidden, we don't have a canonical position so w e will do the wrong thing for tables and <hr>. https://bugs.webkit.org/show_bug. cgi?id=40342 176 // FIXME: If the node is hidden, we don't have a canonical position so w e will do the wrong thing for tables and <hr>. https://bugs.webkit.org/show_bug. cgi?id=40342
177 || (!canonicalPos.isNull() && isRenderedTable(canonicalPos.deprecatedNod e())) 177 || (!canonicalPos.isNull() && isRenderedTable(canonicalPos.deprecatedNod e()))
178 || (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.deprecatedNo de()))) { 178 || (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.deprecatedNo de()))) {
179 applyCommandToComposite(InsertLineBreakCommand::create(document())); 179 applyCommandToComposite(InsertLineBreakCommand::create(document()));
180 return; 180 return;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 214 }
215 215
216 //--------------------------------------------------------------------- 216 //---------------------------------------------------------------------
217 // Handle case when position is in the last visible position in its block, 217 // Handle case when position is in the last visible position in its block,
218 // including when the block is empty. 218 // including when the block is empty.
219 if (isLastInBlock) { 219 if (isLastInBlock) {
220 if (nestNewBlock) { 220 if (nestNewBlock) {
221 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) { 221 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
222 // The block is empty. Create an empty block to 222 // The block is empty. Create an empty block to
223 // represent the paragraph that we're leaving. 223 // represent the paragraph that we're leaving.
224 RefPtrWillBeRawPtr<Element> extraBlock = createDefaultParagraphE lement(document()); 224 RefPtrWillBeRawPtr<HTMLElement> extraBlock = createDefaultParagr aphElement(document());
225 appendNode(extraBlock, startBlock); 225 appendNode(extraBlock, startBlock);
226 appendBlockPlaceholder(extraBlock); 226 appendBlockPlaceholder(extraBlock);
227 } 227 }
228 appendNode(blockToInsert, startBlock); 228 appendNode(blockToInsert, startBlock);
229 } else { 229 } else {
230 // 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 230 // 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
231 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted. 231 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted.
232 if (m_pasteBlockqutoeIntoUnquotedArea) { 232 if (m_pasteBlockquoteIntoUnquotedArea) {
233 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote)) 233 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote))
234 startBlock = toElement(highestBlockquote); 234 startBlock = toElement(highestBlockquote);
235 } 235 }
236 236
237 if (listChild && listChild != startBlock) { 237 if (listChild && listChild != startBlock) {
238 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren(); 238 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren();
239 appendNode(blockToInsert, listChildToInsert.get()); 239 appendNode(blockToInsert, listChildToInsert.get());
240 insertNodeAfter(listChildToInsert.get(), listChild); 240 insertNodeAfter(listChildToInsert.get(), listChild);
241 } else { 241 } else {
242 // Most of the time we want to stay at the nesting level of the startBlock (e.g., when nesting within lists). However, 242 // Most of the time we want to stay at the nesting level of the startBlock (e.g., when nesting within lists). However,
243 // for div nodes, this can result in nested div tags that are ha rd to break out of. 243 // for div nodes, this can result in nested div tags that are ha rd to break out of.
244 Element* siblingNode = startBlock.get(); 244 Element* siblingElement = startBlock.get();
245 if (isHTMLDivElement(*blockToInsert)) 245 if (isHTMLDivElement(*blockToInsert))
246 siblingNode = highestVisuallyEquivalentDivBelowRoot(startBlo ck.get()); 246 siblingElement = highestVisuallyEquivalentDivBelowRoot(start Block.get());
247 insertNodeAfter(blockToInsert, siblingNode); 247 insertNodeAfter(blockToInsert, siblingElement);
248 } 248 }
249 } 249 }
250 250
251 // Recreate the same structure in the new paragraph. 251 // Recreate the same structure in the new paragraph.
252 252
253 WillBeHeapVector<RefPtrWillBeMember<Element> > ancestors; 253 WillBeHeapVector<RefPtrWillBeMember<Element> > ancestors;
254 getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).deprec atedNode(), startBlock.get(), ancestors); 254 getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).deprec atedNode(), startBlock.get(), ancestors);
255 RefPtrWillBeRawPtr<Element> parent = cloneHierarchyUnderNewBlock(ancesto rs, blockToInsert); 255 RefPtrWillBeRawPtr<Element> parent = cloneHierarchyUnderNewBlock(ancesto rs, blockToInsert);
256 256
257 appendBlockPlaceholder(parent); 257 appendBlockPlaceholder(parent);
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 } 428 }
429 429
430 void InsertParagraphSeparatorCommand::trace(Visitor *visitor) 430 void InsertParagraphSeparatorCommand::trace(Visitor *visitor)
431 { 431 {
432 visitor->trace(m_style); 432 visitor->trace(m_style);
433 CompositeEditCommand::trace(visitor); 433 CompositeEditCommand::trace(visitor);
434 } 434 }
435 435
436 436
437 } // namespace blink 437 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698