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

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

Issue 299353004: Oilpan: move editing objects to the heap. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Make test wrapper class finalized Created 6 years, 6 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
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 if (!isEndOfBlock(endingSelection().visibleStart())) 112 if (!isEndOfBlock(endingSelection().visibleStart()))
113 return false; 113 return false;
114 114
115 return enclosingBlock->hasTagName(h1Tag) || 115 return enclosingBlock->hasTagName(h1Tag) ||
116 enclosingBlock->hasTagName(h2Tag) || 116 enclosingBlock->hasTagName(h2Tag) ||
117 enclosingBlock->hasTagName(h3Tag) || 117 enclosingBlock->hasTagName(h3Tag) ||
118 enclosingBlock->hasTagName(h4Tag) || 118 enclosingBlock->hasTagName(h4Tag) ||
119 enclosingBlock->hasTagName(h5Tag); 119 enclosingBlock->hasTagName(h5Tag);
120 } 120 }
121 121
122 void InsertParagraphSeparatorCommand::getAncestorsInsideBlock(const Node* insert ionNode, Element* outerBlock, Vector<RefPtr<Element> >& ancestors) 122 void InsertParagraphSeparatorCommand::getAncestorsInsideBlock(const Node* insert ionNode, Element* outerBlock, WillBeHeapVector<RefPtrWillBeMember<Element> >& an cestors)
123 { 123 {
124 ancestors.clear(); 124 ancestors.clear();
125 125
126 // Build up list of ancestors elements between the insertion node and the ou ter block. 126 // Build up list of ancestors elements between the insertion node and the ou ter block.
127 if (insertionNode != outerBlock) { 127 if (insertionNode != outerBlock) {
128 for (Element* n = insertionNode->parentElement(); n && n != outerBlock; n = n->parentElement()) 128 for (Element* n = insertionNode->parentElement(); n && n != outerBlock; n = n->parentElement())
129 ancestors.append(n); 129 ancestors.append(n);
130 } 130 }
131 } 131 }
132 132
133 PassRefPtr<Element> InsertParagraphSeparatorCommand::cloneHierarchyUnderNewBlock (const Vector<RefPtr<Element> >& ancestors, PassRefPtr<Element> blockToInsert) 133 PassRefPtrWillBeRawPtr<Element> InsertParagraphSeparatorCommand::cloneHierarchyU nderNewBlock(const WillBeHeapVector<RefPtrWillBeMember<Element> >& ancestors, Pa ssRefPtrWillBeRawPtr<Element> blockToInsert)
134 { 134 {
135 // Make clones of ancestors in between the start node and the start block. 135 // Make clones of ancestors in between the start node and the start block.
136 RefPtr<Element> parent = blockToInsert; 136 RefPtrWillBeRawPtr<Element> parent = blockToInsert;
137 for (size_t i = ancestors.size(); i != 0; --i) { 137 for (size_t i = ancestors.size(); i != 0; --i) {
138 RefPtr<Element> child = ancestors[i - 1]->cloneElementWithoutChildren(); 138 RefPtrWillBeRawPtr<Element> child = ancestors[i - 1]->cloneElementWithou tChildren();
139 // It should always be okay to remove id from the cloned elements, since the originals are not deleted. 139 // It should always be okay to remove id from the cloned elements, since the originals are not deleted.
140 child->removeAttribute(idAttr); 140 child->removeAttribute(idAttr);
141 appendNode(child, parent); 141 appendNode(child, parent);
142 parent = child.release(); 142 parent = child.release();
143 } 143 }
144 144
145 return parent.release(); 145 return parent.release();
146 } 146 }
147 147
148 void InsertParagraphSeparatorCommand::doApply() 148 void InsertParagraphSeparatorCommand::doApply()
149 { 149 {
150 if (!endingSelection().isNonOrphanedCaretOrRange()) 150 if (!endingSelection().isNonOrphanedCaretOrRange())
151 return; 151 return;
152 152
153 Position insertionPosition = endingSelection().start(); 153 Position insertionPosition = endingSelection().start();
154 154
155 EAffinity affinity = endingSelection().affinity(); 155 EAffinity affinity = endingSelection().affinity();
156 156
157 // Delete the current selection. 157 // Delete the current selection.
158 if (endingSelection().isRange()) { 158 if (endingSelection().isRange()) {
159 calculateStyleBeforeInsertion(insertionPosition); 159 calculateStyleBeforeInsertion(insertionPosition);
160 deleteSelection(false, true); 160 deleteSelection(false, true);
161 insertionPosition = endingSelection().start(); 161 insertionPosition = endingSelection().start();
162 affinity = endingSelection().affinity(); 162 affinity = endingSelection().affinity();
163 } 163 }
164 164
165 // FIXME: The parentAnchoredEquivalent conversion needs to be moved into enc losingBlock. 165 // FIXME: The parentAnchoredEquivalent conversion needs to be moved into enc losingBlock.
166 RefPtr<Element> startBlock = enclosingBlock(insertionPosition.parentAnchored Equivalent().containerNode()); 166 RefPtrWillBeRawPtr<Element> startBlock = enclosingBlock(insertionPosition.pa rentAnchoredEquivalent().containerNode());
167 Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEqu ivalent().containerNode()); 167 Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEqu ivalent().containerNode());
168 RefPtr<Element> listChild = listChildNode && listChildNode->isHTMLElement() ? toHTMLElement(listChildNode) : 0; 168 RefPtrWillBeRawPtr<Element> listChild = listChildNode && listChildNode->isHT MLElement() ? toHTMLElement(listChildNode) : 0;
169 Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent(); 169 Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent();
170 if (!startBlock 170 if (!startBlock
171 || !startBlock->nonShadowBoundaryParentNode() 171 || !startBlock->nonShadowBoundaryParentNode()
172 || isTableCell(startBlock.get()) 172 || isTableCell(startBlock.get())
173 || isHTMLFormElement(*startBlock) 173 || isHTMLFormElement(*startBlock)
174 // 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 174 // 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
175 || (!canonicalPos.isNull() && isRenderedTable(canonicalPos.deprecatedNod e())) 175 || (!canonicalPos.isNull() && isRenderedTable(canonicalPos.deprecatedNod e()))
176 || (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.deprecatedNo de()))) { 176 || (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.deprecatedNo de()))) {
177 applyCommandToComposite(InsertLineBreakCommand::create(document())); 177 applyCommandToComposite(InsertLineBreakCommand::create(document()));
178 return; 178 return;
(...skipping 15 matching lines...) Expand all
194 return; 194 return;
195 195
196 //--------------------------------------------------------------------- 196 //---------------------------------------------------------------------
197 // Prepare for more general cases. 197 // Prepare for more general cases.
198 198
199 bool isFirstInBlock = isStartOfBlock(visiblePos); 199 bool isFirstInBlock = isStartOfBlock(visiblePos);
200 bool isLastInBlock = isEndOfBlock(visiblePos); 200 bool isLastInBlock = isEndOfBlock(visiblePos);
201 bool nestNewBlock = false; 201 bool nestNewBlock = false;
202 202
203 // Create block to be inserted. 203 // Create block to be inserted.
204 RefPtr<Element> blockToInsert; 204 RefPtrWillBeRawPtr<Element> blockToInsert = nullptr;
205 if (startBlock->isRootEditableElement()) { 205 if (startBlock->isRootEditableElement()) {
206 blockToInsert = createDefaultParagraphElement(document()); 206 blockToInsert = createDefaultParagraphElement(document());
207 nestNewBlock = true; 207 nestNewBlock = true;
208 } else if (shouldUseDefaultParagraphElement(startBlock.get())) { 208 } else if (shouldUseDefaultParagraphElement(startBlock.get())) {
209 blockToInsert = createDefaultParagraphElement(document()); 209 blockToInsert = createDefaultParagraphElement(document());
210 } else { 210 } else {
211 blockToInsert = startBlock->cloneElementWithoutChildren(); 211 blockToInsert = startBlock->cloneElementWithoutChildren();
212 } 212 }
213 213
214 //--------------------------------------------------------------------- 214 //---------------------------------------------------------------------
215 // Handle case when position is in the last visible position in its block, 215 // Handle case when position is in the last visible position in its block,
216 // including when the block is empty. 216 // including when the block is empty.
217 if (isLastInBlock) { 217 if (isLastInBlock) {
218 if (nestNewBlock) { 218 if (nestNewBlock) {
219 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) { 219 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
220 // The block is empty. Create an empty block to 220 // The block is empty. Create an empty block to
221 // represent the paragraph that we're leaving. 221 // represent the paragraph that we're leaving.
222 RefPtr<Element> extraBlock = createDefaultParagraphElement(docum ent()); 222 RefPtrWillBeRawPtr<Element> extraBlock = createDefaultParagraphE lement(document());
223 appendNode(extraBlock, startBlock); 223 appendNode(extraBlock, startBlock);
224 appendBlockPlaceholder(extraBlock); 224 appendBlockPlaceholder(extraBlock);
225 } 225 }
226 appendNode(blockToInsert, startBlock); 226 appendNode(blockToInsert, startBlock);
227 } else { 227 } else {
228 // 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 228 // 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
229 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted. 229 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted.
230 if (m_pasteBlockqutoeIntoUnquotedArea) { 230 if (m_pasteBlockqutoeIntoUnquotedArea) {
231 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote)) 231 if (Node* highestBlockquote = highestEnclosingNodeOfType(canonic alPos, &isMailBlockquote))
232 startBlock = toElement(highestBlockquote); 232 startBlock = toElement(highestBlockquote);
233 } 233 }
234 234
235 if (listChild && listChild != startBlock) { 235 if (listChild && listChild != startBlock) {
236 RefPtr<Element> listChildToInsert = listChild->cloneElementWitho utChildren(); 236 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren();
237 appendNode(blockToInsert, listChildToInsert.get()); 237 appendNode(blockToInsert, listChildToInsert.get());
238 insertNodeAfter(listChildToInsert.get(), listChild); 238 insertNodeAfter(listChildToInsert.get(), listChild);
239 } else { 239 } else {
240 // Most of the time we want to stay at the nesting level of the startBlock (e.g., when nesting within lists). However, 240 // Most of the time we want to stay at the nesting level of the startBlock (e.g., when nesting within lists). However,
241 // for div nodes, this can result in nested div tags that are ha rd to break out of. 241 // for div nodes, this can result in nested div tags that are ha rd to break out of.
242 Element* siblingNode = startBlock.get(); 242 Element* siblingNode = startBlock.get();
243 if (isHTMLDivElement(*blockToInsert)) 243 if (isHTMLDivElement(*blockToInsert))
244 siblingNode = highestVisuallyEquivalentDivBelowRoot(startBlo ck.get()); 244 siblingNode = highestVisuallyEquivalentDivBelowRoot(startBlo ck.get());
245 insertNodeAfter(blockToInsert, siblingNode); 245 insertNodeAfter(blockToInsert, siblingNode);
246 } 246 }
247 } 247 }
248 248
249 // Recreate the same structure in the new paragraph. 249 // Recreate the same structure in the new paragraph.
250 250
251 Vector<RefPtr<Element> > ancestors; 251 WillBeHeapVector<RefPtrWillBeMember<Element> > ancestors;
252 getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).deprec atedNode(), startBlock.get(), ancestors); 252 getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).deprec atedNode(), startBlock.get(), ancestors);
253 RefPtr<Element> parent = cloneHierarchyUnderNewBlock(ancestors, blockToI nsert); 253 RefPtrWillBeRawPtr<Element> parent = cloneHierarchyUnderNewBlock(ancesto rs, blockToInsert);
254 254
255 appendBlockPlaceholder(parent); 255 appendBlockPlaceholder(parent);
256 256
257 setEndingSelection(VisibleSelection(firstPositionInNode(parent.get()), D OWNSTREAM, endingSelection().isDirectional())); 257 setEndingSelection(VisibleSelection(firstPositionInNode(parent.get()), D OWNSTREAM, endingSelection().isDirectional()));
258 return; 258 return;
259 } 259 }
260 260
261 261
262 //--------------------------------------------------------------------- 262 //---------------------------------------------------------------------
263 // Handle case when position is in the first visible position in its block, and 263 // Handle case when position is in the first visible position in its block, and
264 // similar case where previous position is in another, presumeably nested, b lock. 264 // similar case where previous position is in another, presumeably nested, b lock.
265 if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) { 265 if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) {
266 Node* refNode = 0; 266 Node* refNode = 0;
267 insertionPosition = positionOutsideTabSpan(insertionPosition); 267 insertionPosition = positionOutsideTabSpan(insertionPosition);
268 268
269 if (isFirstInBlock && !nestNewBlock) { 269 if (isFirstInBlock && !nestNewBlock) {
270 if (listChild && listChild != startBlock) { 270 if (listChild && listChild != startBlock) {
271 RefPtr<Element> listChildToInsert = listChild->cloneElementWitho utChildren(); 271 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren();
272 appendNode(blockToInsert, listChildToInsert.get()); 272 appendNode(blockToInsert, listChildToInsert.get());
273 insertNodeBefore(listChildToInsert.get(), listChild); 273 insertNodeBefore(listChildToInsert.get(), listChild);
274 } else { 274 } else {
275 refNode = startBlock.get(); 275 refNode = startBlock.get();
276 } 276 }
277 } else if (isFirstInBlock && nestNewBlock) { 277 } else if (isFirstInBlock && nestNewBlock) {
278 // startBlock should always have children, otherwise isLastInBlock w ould be true and it's handled above. 278 // startBlock should always have children, otherwise isLastInBlock w ould be true and it's handled above.
279 ASSERT(startBlock->firstChild()); 279 ASSERT(startBlock->firstChild());
280 refNode = startBlock->firstChild(); 280 refNode = startBlock->firstChild();
281 } 281 }
282 else if (insertionPosition.deprecatedNode() == startBlock && nestNewBloc k) { 282 else if (insertionPosition.deprecatedNode() == startBlock && nestNewBloc k) {
283 refNode = startBlock->traverseToChildAt(insertionPosition.deprecated EditingOffset()); 283 refNode = startBlock->traverseToChildAt(insertionPosition.deprecated EditingOffset());
284 ASSERT(refNode); // must be true or we'd be in the end of block case 284 ASSERT(refNode); // must be true or we'd be in the end of block case
285 } else 285 } else
286 refNode = insertionPosition.deprecatedNode(); 286 refNode = insertionPosition.deprecatedNode();
287 287
288 // find ending selection position easily before inserting the paragraph 288 // find ending selection position easily before inserting the paragraph
289 insertionPosition = insertionPosition.downstream(); 289 insertionPosition = insertionPosition.downstream();
290 290
291 if (refNode) 291 if (refNode)
292 insertNodeBefore(blockToInsert, refNode); 292 insertNodeBefore(blockToInsert, refNode);
293 293
294 // Recreate the same structure in the new paragraph. 294 // Recreate the same structure in the new paragraph.
295 295
296 Vector<RefPtr<Element> > ancestors; 296 WillBeHeapVector<RefPtrWillBeMember<Element> > ancestors;
297 getAncestorsInsideBlock(positionAvoidingSpecialElementBoundary(positionO utsideTabSpan(insertionPosition)).deprecatedNode(), startBlock.get(), ancestors) ; 297 getAncestorsInsideBlock(positionAvoidingSpecialElementBoundary(positionO utsideTabSpan(insertionPosition)).deprecatedNode(), startBlock.get(), ancestors) ;
298 298
299 appendBlockPlaceholder(cloneHierarchyUnderNewBlock(ancestors, blockToIns ert)); 299 appendBlockPlaceholder(cloneHierarchyUnderNewBlock(ancestors, blockToIns ert));
300 300
301 // In this case, we need to set the new ending selection. 301 // In this case, we need to set the new ending selection.
302 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, endin gSelection().isDirectional())); 302 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, endin gSelection().isDirectional()));
303 return; 303 return;
304 } 304 }
305 305
306 //--------------------------------------------------------------------- 306 //---------------------------------------------------------------------
307 // Handle the (more complicated) general case, 307 // Handle the (more complicated) general case,
308 308
309 // All of the content in the current block after visiblePos is 309 // All of the content in the current block after visiblePos is
310 // about to be wrapped in a new paragraph element. Add a br before 310 // about to be wrapped in a new paragraph element. Add a br before
311 // it if visiblePos is at the start of a paragraph so that the 311 // it if visiblePos is at the start of a paragraph so that the
312 // content will move down a line. 312 // content will move down a line.
313 if (isStartOfParagraph(visiblePos)) { 313 if (isStartOfParagraph(visiblePos)) {
314 RefPtr<Element> br = createBreakElement(document()); 314 RefPtrWillBeRawPtr<Element> br = createBreakElement(document());
315 insertNodeAt(br.get(), insertionPosition); 315 insertNodeAt(br.get(), insertionPosition);
316 insertionPosition = positionInParentAfterNode(*br); 316 insertionPosition = positionInParentAfterNode(*br);
317 // If the insertion point is a break element, there is nothing else 317 // If the insertion point is a break element, there is nothing else
318 // we need to do. 318 // we need to do.
319 if (visiblePos.deepEquivalent().anchorNode()->renderer()->isBR()) { 319 if (visiblePos.deepEquivalent().anchorNode()->renderer()->isBR()) {
320 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, e ndingSelection().isDirectional())); 320 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, e ndingSelection().isDirectional()));
321 return; 321 return;
322 } 322 }
323 } 323 }
324 324
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 } 365 }
366 366
367 // If we got detached due to mutation events, just bail out. 367 // If we got detached due to mutation events, just bail out.
368 if (!startBlock->parentNode()) 368 if (!startBlock->parentNode())
369 return; 369 return;
370 370
371 // Put the added block in the tree. 371 // Put the added block in the tree.
372 if (nestNewBlock) { 372 if (nestNewBlock) {
373 appendNode(blockToInsert.get(), startBlock); 373 appendNode(blockToInsert.get(), startBlock);
374 } else if (listChild && listChild != startBlock) { 374 } else if (listChild && listChild != startBlock) {
375 RefPtr<Element> listChildToInsert = listChild->cloneElementWithoutChildr en(); 375 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->cloneElementW ithoutChildren();
376 appendNode(blockToInsert.get(), listChildToInsert.get()); 376 appendNode(blockToInsert.get(), listChildToInsert.get());
377 insertNodeAfter(listChildToInsert.get(), listChild); 377 insertNodeAfter(listChildToInsert.get(), listChild);
378 } else { 378 } else {
379 insertNodeAfter(blockToInsert.get(), startBlock); 379 insertNodeAfter(blockToInsert.get(), startBlock);
380 } 380 }
381 381
382 document().updateLayoutIgnorePendingStylesheets(); 382 document().updateLayoutIgnorePendingStylesheets();
383 383
384 // If the paragraph separator was inserted at the end of a paragraph, an emp ty line must be 384 // If the paragraph separator was inserted at the end of a paragraph, an emp ty line must be
385 // created. All of the nodes, starting at visiblePos, are about to be added to the new paragraph 385 // created. All of the nodes, starting at visiblePos, are about to be added to the new paragraph
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 deleteInsignificantTextDownstream(positionAfterSplit); 418 deleteInsignificantTextDownstream(positionAfterSplit);
419 if (positionAfterSplit.deprecatedNode()->isTextNode()) 419 if (positionAfterSplit.deprecatedNode()->isTextNode())
420 insertTextIntoNode(toText(positionAfterSplit.containerNode()), 0 , nonBreakingSpaceString()); 420 insertTextIntoNode(toText(positionAfterSplit.containerNode()), 0 , nonBreakingSpaceString());
421 } 421 }
422 } 422 }
423 423
424 setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()) , DOWNSTREAM, endingSelection().isDirectional())); 424 setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()) , DOWNSTREAM, endingSelection().isDirectional()));
425 applyStyleAfterInsertion(startBlock.get()); 425 applyStyleAfterInsertion(startBlock.get());
426 } 426 }
427 427
428 void InsertParagraphSeparatorCommand::trace(Visitor *visitor)
429 {
430 visitor->trace(m_style);
431 CompositeEditCommand::trace(visitor);
432 }
433
434
428 } // namespace WebCore 435 } // 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