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

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

Issue 1294543005: Move execCommand related files in core/editing/ related files into core/editing/commands/ (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 2015-08-18T14:20:58 Rebase for merging code style fixes Created 5 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
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "core/editing/InsertParagraphSeparatorCommand.h"
28
29 #include "core/HTMLNames.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/NodeTraversal.h"
32 #include "core/dom/Text.h"
33 #include "core/editing/EditingStyle.h"
34 #include "core/editing/EditingUtilities.h"
35 #include "core/editing/InsertLineBreakCommand.h"
36 #include "core/editing/VisibleUnits.h"
37 #include "core/html/HTMLBRElement.h"
38 #include "core/html/HTMLElement.h"
39 #include "core/html/HTMLQuoteElement.h"
40 #include "core/layout/LayoutObject.h"
41 #include "core/layout/LayoutText.h"
42
43 namespace blink {
44
45 using namespace HTMLNames;
46
47 // When inserting a new line, we want to avoid nesting empty divs if we can. Ot herwise, when
48 // pasting, it's easy to have each new line be a div deeper than the previous. E.g., in the case
49 // below, we want to insert at ^ instead of |.
50 // <div>foo<div>bar</div>|</div>^
51 static Element* highestVisuallyEquivalentDivBelowRoot(Element* startBlock)
52 {
53 Element* curBlock = startBlock;
54 // 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
55 // siblings for us to append to.
56 while (!curBlock->nextSibling() && isHTMLDivElement(*curBlock->parentElement ()) && curBlock->parentElement()->parentElement()) {
57 if (curBlock->parentElement()->hasAttributes())
58 break;
59 curBlock = curBlock->parentElement();
60 }
61 return curBlock;
62 }
63
64 InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document& docum ent, bool mustUseDefaultParagraphElement, bool pasteBlockquoteIntoUnquotedArea)
65 : CompositeEditCommand(document)
66 , m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement)
67 , m_pasteBlockquoteIntoUnquotedArea(pasteBlockquoteIntoUnquotedArea)
68 {
69 }
70
71 bool InsertParagraphSeparatorCommand::preservesTypingStyle() const
72 {
73 return true;
74 }
75
76 void InsertParagraphSeparatorCommand::calculateStyleBeforeInsertion(const Positi on &pos)
77 {
78 // It is only important to set a style to apply later if we're at the bounda ries of
79 // a paragraph. Otherwise, content that is moved as part of the work of the command
80 // will lend their styles to the new paragraph without any extra work needed .
81 VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
82 if (!isStartOfParagraph(visiblePos) && !isEndOfParagraph(visiblePos))
83 return;
84
85 ASSERT(pos.isNotNull());
86 m_style = EditingStyle::create(pos);
87 m_style->mergeTypingStyle(pos.document());
88 }
89
90 void InsertParagraphSeparatorCommand::applyStyleAfterInsertion(Element* original EnclosingBlock)
91 {
92 // Not only do we break out of header tags, but we also do not preserve the typing style,
93 // in order to match other browsers.
94 if (originalEnclosingBlock->hasTagName(h1Tag)
95 || originalEnclosingBlock->hasTagName(h2Tag)
96 || originalEnclosingBlock->hasTagName(h3Tag)
97 || originalEnclosingBlock->hasTagName(h4Tag)
98 || originalEnclosingBlock->hasTagName(h5Tag)) {
99 return;
100 }
101
102 if (!m_style)
103 return;
104
105 m_style->prepareToApplyAt(endingSelection().start());
106 if (!m_style->isEmpty())
107 applyStyle(m_style.get());
108 }
109
110 bool InsertParagraphSeparatorCommand::shouldUseDefaultParagraphElement(Element* enclosingBlock) const
111 {
112 if (m_mustUseDefaultParagraphElement)
113 return true;
114
115 // Assumes that if there was a range selection, it was already deleted.
116 if (!isEndOfBlock(endingSelection().visibleStart()))
117 return false;
118
119 return enclosingBlock->hasTagName(h1Tag)
120 || enclosingBlock->hasTagName(h2Tag)
121 || enclosingBlock->hasTagName(h3Tag)
122 || enclosingBlock->hasTagName(h4Tag)
123 || enclosingBlock->hasTagName(h5Tag);
124 }
125
126 void InsertParagraphSeparatorCommand::getAncestorsInsideBlock(const Node* insert ionNode, Element* outerBlock, WillBeHeapVector<RefPtrWillBeMember<Element>>& anc estors)
127 {
128 ancestors.clear();
129
130 // Build up list of ancestors elements between the insertion node and the ou ter block.
131 if (insertionNode != outerBlock) {
132 for (Element* n = insertionNode->parentElement(); n && n != outerBlock; n = n->parentElement())
133 ancestors.append(n);
134 }
135 }
136
137 PassRefPtrWillBeRawPtr<Element> InsertParagraphSeparatorCommand::cloneHierarchyU nderNewBlock(const WillBeHeapVector<RefPtrWillBeMember<Element>>& ancestors, Pas sRefPtrWillBeRawPtr<Element> blockToInsert)
138 {
139 // Make clones of ancestors in between the start node and the start block.
140 RefPtrWillBeRawPtr<Element> parent = blockToInsert;
141 for (size_t i = ancestors.size(); i != 0; --i) {
142 RefPtrWillBeRawPtr<Element> child = ancestors[i - 1]->cloneElementWithou tChildren();
143 // It should always be okay to remove id from the cloned elements, since the originals are not deleted.
144 child->removeAttribute(idAttr);
145 appendNode(child, parent);
146 parent = child.release();
147 }
148
149 return parent.release();
150 }
151
152 void InsertParagraphSeparatorCommand::doApply()
153 {
154 if (!endingSelection().isNonOrphanedCaretOrRange())
155 return;
156
157 Position insertionPosition = endingSelection().start();
158
159 EAffinity affinity = endingSelection().affinity();
160
161 // Delete the current selection.
162 if (endingSelection().isRange()) {
163 calculateStyleBeforeInsertion(insertionPosition);
164 deleteSelection(false, true);
165 insertionPosition = endingSelection().start();
166 affinity = endingSelection().affinity();
167 }
168
169 // FIXME: The parentAnchoredEquivalent conversion needs to be moved into enc losingBlock.
170 RefPtrWillBeRawPtr<Element> startBlock = enclosingBlock(insertionPosition.pa rentAnchoredEquivalent().computeContainerNode());
171 Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEqu ivalent().computeContainerNode());
172 RefPtrWillBeRawPtr<HTMLElement> listChild = listChildNode && listChildNode-> isHTMLElement() ? toHTMLElement(listChildNode) : 0;
173 Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent();
174 if (!startBlock
175 || !startBlock->nonShadowBoundaryParentNode()
176 || isTableCell(startBlock.get())
177 || isHTMLFormElement(*startBlock)
178 // 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
179 || (!canonicalPos.isNull() && isRenderedTableElement(canonicalPos.anchor Node()))
180 || (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.anchorNode() ))) {
181 applyCommandToComposite(InsertLineBreakCommand::create(document()));
182 return;
183 }
184
185 // Use the leftmost candidate.
186 insertionPosition = insertionPosition.upstream();
187 if (!insertionPosition.isCandidate())
188 insertionPosition = insertionPosition.downstream();
189
190 // Adjust the insertion position after the delete
191 insertionPosition = positionAvoidingSpecialElementBoundary(insertionPosition );
192 VisiblePosition visiblePos(insertionPosition, affinity);
193 calculateStyleBeforeInsertion(insertionPosition);
194
195 //---------------------------------------------------------------------
196 // Handle special case of typing return on an empty list item
197 if (breakOutOfEmptyListItem())
198 return;
199
200 //---------------------------------------------------------------------
201 // Prepare for more general cases.
202
203 bool isFirstInBlock = isStartOfBlock(visiblePos);
204 bool isLastInBlock = isEndOfBlock(visiblePos);
205 bool nestNewBlock = false;
206
207 // Create block to be inserted.
208 RefPtrWillBeRawPtr<Element> blockToInsert = nullptr;
209 if (startBlock->isRootEditableElement()) {
210 blockToInsert = createDefaultParagraphElement(document());
211 nestNewBlock = true;
212 } else if (shouldUseDefaultParagraphElement(startBlock.get())) {
213 blockToInsert = createDefaultParagraphElement(document());
214 } else {
215 blockToInsert = startBlock->cloneElementWithoutChildren();
216 }
217
218 //---------------------------------------------------------------------
219 // Handle case when position is in the last visible position in its block,
220 // including when the block is empty.
221 if (isLastInBlock) {
222 if (nestNewBlock) {
223 if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
224 // The block is empty. Create an empty block to
225 // represent the paragraph that we're leaving.
226 RefPtrWillBeRawPtr<HTMLElement> extraBlock = createDefaultParagr aphElement(document());
227 appendNode(extraBlock, startBlock);
228 appendBlockPlaceholder(extraBlock);
229 }
230 appendNode(blockToInsert, startBlock);
231 } else {
232 // 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
233 // into an unquoted area. We then don't want the newline within the blockquote or else it will also be quoted.
234 if (m_pasteBlockquoteIntoUnquotedArea) {
235 if (HTMLQuoteElement* highestBlockquote = toHTMLQuoteElement(hig hestEnclosingNodeOfType(canonicalPos, &isMailHTMLBlockquoteElement)))
236 startBlock = highestBlockquote;
237 }
238
239 if (listChild && listChild != startBlock) {
240 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren();
241 appendNode(blockToInsert, listChildToInsert.get());
242 insertNodeAfter(listChildToInsert.get(), listChild);
243 } else {
244 // Most of the time we want to stay at the nesting level of the startBlock (e.g., when nesting within lists). However,
245 // for div nodes, this can result in nested div tags that are ha rd to break out of.
246 Element* siblingElement = startBlock.get();
247 if (isHTMLDivElement(*blockToInsert))
248 siblingElement = highestVisuallyEquivalentDivBelowRoot(start Block.get());
249 insertNodeAfter(blockToInsert, siblingElement);
250 }
251 }
252
253 // Recreate the same structure in the new paragraph.
254
255 WillBeHeapVector<RefPtrWillBeMember<Element>> ancestors;
256 getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).anchor Node(), startBlock.get(), ancestors);
257 RefPtrWillBeRawPtr<Element> parent = cloneHierarchyUnderNewBlock(ancesto rs, blockToInsert);
258
259 appendBlockPlaceholder(parent);
260
261 setEndingSelection(VisibleSelection(firstPositionInNode(parent.get()), D OWNSTREAM, endingSelection().isDirectional()));
262 return;
263 }
264
265
266 //---------------------------------------------------------------------
267 // Handle case when position is in the first visible position in its block, and
268 // similar case where previous position is in another, presumeably nested, b lock.
269 if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) {
270 Node* refNode = nullptr;
271 insertionPosition = positionOutsideTabSpan(insertionPosition);
272
273 if (isFirstInBlock && !nestNewBlock) {
274 if (listChild && listChild != startBlock) {
275 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->clone ElementWithoutChildren();
276 appendNode(blockToInsert, listChildToInsert.get());
277 insertNodeBefore(listChildToInsert.get(), listChild);
278 } else {
279 refNode = startBlock.get();
280 }
281 } else if (isFirstInBlock && nestNewBlock) {
282 // startBlock should always have children, otherwise isLastInBlock w ould be true and it's handled above.
283 ASSERT(startBlock->hasChildren());
284 refNode = startBlock->firstChild();
285 } else if (insertionPosition.anchorNode() == startBlock && nestNewBlock) {
286 refNode = NodeTraversal::childAt(*startBlock, insertionPosition.comp uteEditingOffset());
287 ASSERT(refNode); // must be true or we'd be in the end of block case
288 } else {
289 refNode = insertionPosition.anchorNode();
290 }
291
292 // find ending selection position easily before inserting the paragraph
293 insertionPosition = insertionPosition.downstream();
294
295 if (refNode)
296 insertNodeBefore(blockToInsert, refNode);
297
298 // Recreate the same structure in the new paragraph.
299
300 WillBeHeapVector<RefPtrWillBeMember<Element>> ancestors;
301 getAncestorsInsideBlock(positionAvoidingSpecialElementBoundary(positionO utsideTabSpan(insertionPosition)).anchorNode(), startBlock.get(), ancestors);
302
303 appendBlockPlaceholder(cloneHierarchyUnderNewBlock(ancestors, blockToIns ert));
304
305 // In this case, we need to set the new ending selection.
306 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, endin gSelection().isDirectional()));
307 return;
308 }
309
310 //---------------------------------------------------------------------
311 // Handle the (more complicated) general case,
312
313 // All of the content in the current block after visiblePos is
314 // about to be wrapped in a new paragraph element. Add a br before
315 // it if visiblePos is at the start of a paragraph so that the
316 // content will move down a line.
317 if (isStartOfParagraph(visiblePos)) {
318 RefPtrWillBeRawPtr<HTMLBRElement> br = createBreakElement(document());
319 insertNodeAt(br.get(), insertionPosition);
320 insertionPosition = positionInParentAfterNode(*br);
321 // If the insertion point is a break element, there is nothing else
322 // we need to do.
323 if (visiblePos.deepEquivalent().anchorNode()->layoutObject()->isBR()) {
324 setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, e ndingSelection().isDirectional()));
325 return;
326 }
327 }
328
329 // Move downstream. Typing style code will take care of carrying along the
330 // style of the upstream position.
331 insertionPosition = insertionPosition.downstream();
332
333 // At this point, the insertionPosition's node could be a container, and we want to make sure we include
334 // all of the correct nodes when building the ancestor list. So this needs to be the deepest representation of the position
335 // before we walk the DOM tree.
336 insertionPosition = positionOutsideTabSpan(VisiblePosition(insertionPosition ).deepEquivalent());
337
338 // If the returned position lies either at the end or at the start of an ele ment that is ignored by editing
339 // we should move to its upstream or downstream position.
340 if (editingIgnoresContent(insertionPosition.anchorNode())) {
341 if (insertionPosition.atLastEditingPositionForNode())
342 insertionPosition = insertionPosition.downstream();
343 else if (insertionPosition.atFirstEditingPositionForNode())
344 insertionPosition = insertionPosition.upstream();
345 }
346
347 // Make sure we do not cause a rendered space to become unrendered.
348 // FIXME: We need the affinity for pos, but pos.downstream() does not give i t
349 Position leadingWhitespace = leadingWhitespacePosition(insertionPosition, VP _DEFAULT_AFFINITY);
350 // FIXME: leadingWhitespacePosition is returning the position before preserv ed newlines for positions
351 // after the preserved newline, causing the newline to be turned into a nbsp .
352 if (leadingWhitespace.isNotNull() && leadingWhitespace.anchorNode()->isTextN ode()) {
353 Text* textNode = toText(leadingWhitespace.anchorNode());
354 ASSERT(!textNode->layoutObject() || textNode->layoutObject()->style()->c ollapseWhiteSpace());
355 replaceTextInNodePreservingMarkers(textNode, leadingWhitespace.computeOf fsetInContainerNode(), 1, nonBreakingSpaceString());
356 }
357
358 // Split at pos if in the middle of a text node.
359 Position positionAfterSplit;
360 if (insertionPosition.isOffsetInAnchor() && insertionPosition.computeContain erNode()->isTextNode()) {
361 RefPtrWillBeRawPtr<Text> textNode = toText(insertionPosition.computeCont ainerNode());
362 int textOffset = insertionPosition.offsetInContainerNode();
363 bool atEnd = static_cast<unsigned>(textOffset) >= textNode->length();
364 if (textOffset > 0 && !atEnd) {
365 splitTextNode(textNode, textOffset);
366 positionAfterSplit = firstPositionInNode(textNode.get());
367 insertionPosition = Position(textNode->previousSibling(), textOffset );
368 visiblePos = VisiblePosition(insertionPosition);
369 }
370 }
371
372 // If we got detached due to mutation events, just bail out.
373 if (!startBlock->parentNode())
374 return;
375
376 // Put the added block in the tree.
377 if (nestNewBlock) {
378 appendNode(blockToInsert.get(), startBlock);
379 } else if (listChild && listChild != startBlock) {
380 RefPtrWillBeRawPtr<Element> listChildToInsert = listChild->cloneElementW ithoutChildren();
381 appendNode(blockToInsert.get(), listChildToInsert.get());
382 insertNodeAfter(listChildToInsert.get(), listChild);
383 } else {
384 insertNodeAfter(blockToInsert.get(), startBlock);
385 }
386
387 document().updateLayoutIgnorePendingStylesheets();
388
389 // If the paragraph separator was inserted at the end of a paragraph, an emp ty line must be
390 // created. All of the nodes, starting at visiblePos, are about to be added to the new paragraph
391 // element. If the first node to be inserted won't be one that will hold an empty line open, add a br.
392 if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtVisiblePosition(visibl ePos))
393 appendNode(createBreakElement(document()).get(), blockToInsert.get());
394
395 // Move the start node and the siblings of the start node.
396 if (VisiblePosition(insertionPosition).deepEquivalent() != VisiblePosition(p ositionBeforeNode(blockToInsert.get())).deepEquivalent()) {
397 Node* n;
398 if (insertionPosition.computeContainerNode() == startBlock) {
399 n = insertionPosition.computeNodeAfterPosition();
400 } else {
401 Node* splitTo = insertionPosition.computeContainerNode();
402 if (splitTo->isTextNode() && insertionPosition.offsetInContainerNode () >= caretMaxOffset(splitTo))
403 splitTo = NodeTraversal::next(*splitTo, startBlock.get());
404 ASSERT(splitTo);
405 splitTreeToNode(splitTo, startBlock.get());
406
407 for (n = startBlock->firstChild(); n; n = n->nextSibling()) {
408 VisiblePosition beforeNodePosition(positionBeforeNode(n));
409 if (!beforeNodePosition.isNull() && comparePositions(VisiblePosi tion(insertionPosition), beforeNodePosition) <= 0)
410 break;
411 }
412 }
413
414 moveRemainingSiblingsToNewParent(n, blockToInsert.get(), blockToInsert);
415 }
416
417 // Handle whitespace that occurs after the split
418 if (positionAfterSplit.isNotNull()) {
419 document().updateLayoutIgnorePendingStylesheets();
420 if (!positionAfterSplit.isRenderedCharacter()) {
421 // Clear out all whitespace and insert one non-breaking space
422 ASSERT(!positionAfterSplit.computeContainerNode()->layoutObject() || positionAfterSplit.computeContainerNode()->layoutObject()->style()->collapseWhi teSpace());
423 deleteInsignificantTextDownstream(positionAfterSplit);
424 if (positionAfterSplit.anchorNode()->isTextNode())
425 insertTextIntoNode(toText(positionAfterSplit.computeContainerNod e()), 0, nonBreakingSpaceString());
426 }
427 }
428
429 setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()) , DOWNSTREAM, endingSelection().isDirectional()));
430 applyStyleAfterInsertion(startBlock.get());
431 }
432
433 DEFINE_TRACE(InsertParagraphSeparatorCommand)
434 {
435 visitor->trace(m_style);
436 CompositeEditCommand::trace(visitor);
437 }
438
439
440 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698