OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / editing / CompositeEditCommand.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008 Apple 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 "CompositeEditCommand.h"
28
29 #include "AppendNodeCommand.h"
30 #include "ApplyStyleCommand.h"
31 #include "CSSComputedStyleDeclaration.h"
32 #include "CSSMutableStyleDeclaration.h"
33 #include "CharacterNames.h"
34 #include "DeleteFromTextNodeCommand.h"
35 #include "DeleteSelectionCommand.h"
36 #include "Document.h"
37 #include "DocumentFragment.h"
38 #include "EditorInsertAction.h"
39 #include "Frame.h"
40 #include "HTMLElement.h"
41 #include "HTMLNames.h"
42 #include "InlineTextBox.h"
43 #include "InsertIntoTextNodeCommand.h"
44 #include "InsertLineBreakCommand.h"
45 #include "InsertNodeBeforeCommand.h"
46 #include "InsertParagraphSeparatorCommand.h"
47 #include "InsertTextCommand.h"
48 #include "JoinTextNodesCommand.h"
49 #include "MergeIdenticalElementsCommand.h"
50 #include "Range.h"
51 #include "RemoveCSSPropertyCommand.h"
52 #include "RemoveNodeCommand.h"
53 #include "RemoveNodePreservingChildrenCommand.h"
54 #include "ReplaceNodeWithSpanCommand.h"
55 #include "ReplaceSelectionCommand.h"
56 #include "RenderBlock.h"
57 #include "RenderText.h"
58 #include "SetNodeAttributeCommand.h"
59 #include "SplitElementCommand.h"
60 #include "SplitTextNodeCommand.h"
61 #include "SplitTextNodeContainingElementCommand.h"
62 #include "Text.h"
63 #include "TextIterator.h"
64 #include "WrapContentsInDummySpanCommand.h"
65 #include "htmlediting.h"
66 #include "markup.h"
67 #include "visible_units.h"
68
69 using namespace std;
70
71 namespace WebCore {
72
73 using namespace HTMLNames;
74
75 CompositeEditCommand::CompositeEditCommand(Document *document)
76     : EditCommand(document)
77 {
78 }
79
80 CompositeEditCommand::~CompositeEditCommand()
81 {
82 }
83
84 void CompositeEditCommand::doUnapply()
85 {
86     size_t size = m_commands.size();
87     for (size_t i = size; i != 0; --i)
88         m_commands[i - 1]->unapply();
89 }
90
91 void CompositeEditCommand::doReapply()
92 {
93     size_t size = m_commands.size();
94     for (size_t i = 0; i != size; ++i)
95         m_commands[i]->reapply();
96 }
97
98 //
99 // sugary-sweet convenience functions to help create and apply edit commands in composite commands
100 //
101 void CompositeEditCommand::applyCommandToComposite(PassRefPtr<EditCommand> cmd)
102 {
103     cmd->setParent(this);
104     cmd->apply();
105     m_commands.append(cmd);
106 }
107
108 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, EditAction editingAction)
109 {
110     applyCommandToComposite(ApplyStyleCommand::create(document(), style, editingAction));
111 }
112
113 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction editingAction)
114 {
115     applyCommandToComposite(ApplyStyleCommand::create(document(), style, start, end, editingAction));
116 }
117
118 void CompositeEditCommand::applyStyledElement(PassRefPtr<Element> element)
119 {
120     applyCommandToComposite(ApplyStyleCommand::create(element, false));
121 }
122
123 void CompositeEditCommand::removeStyledElement(PassRefPtr<Element> element)
124 {
125     applyCommandToComposite(ApplyStyleCommand::create(element, true));
126 }
127
128 void CompositeEditCommand::insertParagraphSeparator(bool useDefaultParagraphElement)
129 {
130     applyCommandToComposite(InsertParagraphSeparatorCommand::create(document(), useDefaultParagraphElement));
131 }
132
133 void CompositeEditCommand::insertLineBreak()
134 {
135     applyCommandToComposite(InsertLineBreakCommand::create(document()));
136 }
137
138 void CompositeEditCommand::insertNodeBefore(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
139 {
140     ASSERT(!refChild->hasTagName(bodyTag));
141     applyCommandToComposite(InsertNodeBeforeCommand::create(insertChild, refChild));
142 }
143
144 void CompositeEditCommand::insertNodeAfter(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
145 {
146     ASSERT(insertChild);
147     ASSERT(refChild);
148     ASSERT(!refChild->hasTagName(bodyTag));
149     Element* parent = refChild->parentElement();
150     ASSERT(parent);
151     if (parent->lastChild() == refChild)
152         appendNode(insertChild, parent);
153     else {
154         ASSERT(refChild->nextSibling());
155         insertNodeBefore(insertChild, refChild->nextSibling());
156     }
157 }
158
159 void CompositeEditCommand::insertNodeAt(PassRefPtr<Node> insertChild, const Position& editingPosition)
160 {
161     ASSERT(isEditablePosition(editingPosition));
162     // For editing positions like [table, 0], insert before the table,
163     // likewise for replaced elements, brs, etc.
164     Position p = rangeCompliantEquivalent(editingPosition);
165     Node* refChild = p.node();
166     int offset = p.deprecatedEditingOffset();
167     
168     if (canHaveChildrenForEditing(refChild)) {
169         Node* child = refChild->firstChild();
170         for (int i = 0; child && i < offset; i++)
171             child = child->nextSibling();
172         if (child)
173             insertNodeBefore(insertChild, child);
174         else
175             appendNode(insertChild, static_cast<Element*>(refChild));
176     } else if (caretMinOffset(refChild) >= offset)
177         insertNodeBefore(insertChild, refChild);
178     else if (refChild->isTextNode() && caretMaxOffset(refChild) > offset) {
179         splitTextNode(static_cast<Text *>(refChild), offset);
180
181         // Mutation events (bug 22634) from the text node insertion may have removed the refChild
182         if (!refChild->inDocument())
183             return;
184         insertNodeBefore(insertChild, refChild);
185     } else
186         insertNodeAfter(insertChild, refChild);
187 }
188
189 void CompositeEditCommand::appendNode(PassRefPtr<Node> node, PassRefPtr<Element> parent)
190 {
191     ASSERT(canHaveChildrenForEditing(parent.get()));
192     applyCommandToComposite(AppendNodeCommand::create(parent, node));
193 }
194
195 void CompositeEditCommand::removeChildrenInRange(PassRefPtr<Node> node, unsigned from, unsigned to)
196 {
197     Vector<RefPtr<Node> > children;
198     Node* child = node->childNode(from);
199     for (unsigned i = from; child && i < to; i++, child = child->nextSibling())
200         children.append(child);
201
202     size_t size = children.size();
203     for (size_t i = 0; i < size; ++i)
204         removeNode(children[i].release());
205 }
206
207 void CompositeEditCommand::removeNode(PassRefPtr<Node> node)
208 {
209     if (!node || !node->parentNode())
210         return;
211     applyCommandToComposite(RemoveNodeCommand::create(node));
212 }
213
214 void CompositeEditCommand::removeNodePreservingChildren(PassRefPtr<Node> node)
215 {
216     applyCommandToComposite(RemoveNodePreservingChildrenCommand::create(node));
217 }
218
219 void CompositeEditCommand::removeNodeAndPruneAncestors(PassRefPtr<Node> node)
220 {
221     RefPtr<ContainerNode> parent = node->parentNode();
222     removeNode(node);
223     prune(parent.release());
224 }
225
226 HTMLElement* CompositeEditCommand::replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement> node)
227 {
228     // It would also be possible to implement all of ReplaceNodeWithSpanCommand
229     // as a series of existing smaller edit commands.  Someone who wanted to
230     // reduce the number of edit commands could do so here.
231     RefPtr<ReplaceNodeWithSpanCommand> command = ReplaceNodeWithSpanCommand::create(node);
232     applyCommandToComposite(command);
233     // Returning a raw pointer here is OK because the command is retained by
234     // applyCommandToComposite (thus retaining the span), and the span is also
235     // in the DOM tree, and thus alive whie it has a parent.
236     ASSERT(command->spanElement()->inDocument());
237     return command->spanElement();
238 }
239
240 static bool hasARenderedDescendant(Node* node)
241 {
242     Node* n = node->firstChild();
243     while (n) {
244         if (n->renderer())
245             return true;
246         n = n->traverseNextNode(node);
247     }
248     return false;
249 }
250
251 void CompositeEditCommand::prune(PassRefPtr<Node> node)
252 {
253     while (node) {
254         // If you change this rule you may have to add an updateLayout() here.
255         RenderObject* renderer = node->renderer();
256         if (renderer && (!renderer->canHaveChildren() || hasARenderedDescendant(node.get()) || node->rootEditableElement() == node))
257             return;
258             
259         RefPtr<ContainerNode> next = node->parentNode();
260         removeNode(node);
261         node = next;
262     }
263 }
264
265 void CompositeEditCommand::splitTextNode(PassRefPtr<Text> node, unsigned offset)
266 {
267     applyCommandToComposite(SplitTextNodeCommand::create(node, offset));
268 }
269
270 void CompositeEditCommand::splitElement(PassRefPtr<Element> element, PassRefPtr<Node> atChild)
271 {
272     applyCommandToComposite(SplitElementCommand::create(element, atChild));
273 }
274
275 void CompositeEditCommand::mergeIdenticalElements(PassRefPtr<Element> prpFirst, PassRefPtr<Element> prpSecond)
276 {
277     RefPtr<Element> first = prpFirst;
278     RefPtr<Element> second = prpSecond;
279     ASSERT(!first->isDescendantOf(second.get()) && second != first);
280     if (first->nextSibling() != second) {
281         removeNode(second);
282         insertNodeAfter(second, first);
283     }
284     applyCommandToComposite(MergeIdenticalElementsCommand::create(first, second));
285 }
286
287 void CompositeEditCommand::wrapContentsInDummySpan(PassRefPtr<Element> element)
288 {
289     applyCommandToComposite(WrapContentsInDummySpanCommand::create(element));
290 }
291
292 void CompositeEditCommand::splitTextNodeContainingElement(PassRefPtr<Text> text, unsigned offset)
293 {
294     applyCommandToComposite(SplitTextNodeContainingElementCommand::create(text, offset));
295 }
296
297 void CompositeEditCommand::joinTextNodes(PassRefPtr<Text> text1, PassRefPtr<Text> text2)
298 {
299     applyCommandToComposite(JoinTextNodesCommand::create(text1, text2));
300 }
301
302 void CompositeEditCommand::inputText(const String& text, bool selectInsertedText)
303 {
304     unsigned offset = 0;
305     unsigned length = text.length();
306     RefPtr<Range> startRange = Range::create(document(), Position(document()->documentElement(), 0), endingSelection().start());
307     unsigned startIndex = TextIterator::rangeLength(startRange.get());
308     size_t newline;
309     do {
310         newline = text.find('\n', offset);
311         if (newline != offset) {
312             RefPtr<InsertTextCommand> command = InsertTextCommand::create(document());
313             applyCommandToComposite(command);
314             int substringLength = newline == notFound ? length - offset : newline - offset;
315             command->input(text.substring(offset, substringLength), false);
316         }
317         if (newline != notFound)
318             insertLineBreak();
319             
320         offset = newline + 1;
321     } while (newline != notFound && offset != length);
322     
323     if (selectInsertedText) {
324         RefPtr<Range> selectedRange = TextIterator::rangeFromLocationAndLength(document()->documentElement(), startIndex, length);
325         setEndingSelection(VisibleSelection(selectedRange.get()));
326     }
327 }
328
329 void CompositeEditCommand::insertTextIntoNode(PassRefPtr<Text> node, unsigned offset, const String& text)
330 {
331     applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, text));
332 }
333
334 void CompositeEditCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
335 {
336     applyCommandToComposite(DeleteFromTextNodeCommand::create(node, offset, count));
337 }
338
339 void CompositeEditCommand::replaceTextInNode(PassRefPtr<Text> node, unsigned offset, unsigned count, const String& replacementText)
340 {
341     applyCommandToComposite(DeleteFromTextNodeCommand::create(node.get(), offset, count));
342     applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, replacementText));
343 }
344
345 Position CompositeEditCommand::positionOutsideTabSpan(const Position& pos)
346 {
347     if (!isTabSpanTextNode(pos.node()))
348         return pos;
349     
350     Node* tabSpan = tabSpanNode(pos.node());
351     
352     if (pos.deprecatedEditingOffset() <= caretMinOffset(pos.node()))
353         return positionInParentBeforeNode(tabSpan);
354         
355     if (pos.deprecatedEditingOffset() >= caretMaxOffset(pos.node()))
356         return positionInParentAfterNode(tabSpan);
357
358     splitTextNodeContainingElement(static_cast<Text *>(pos.node()), pos.deprecatedEditingOffset());
359     return positionInParentBeforeNode(tabSpan);
360 }
361
362 void CompositeEditCommand::insertNodeAtTabSpanPosition(PassRefPtr<Node> node, const Position& pos)
363 {
364     // insert node before, after, or at split of tab span
365     insertNodeAt(node, positionOutsideTabSpan(pos));
366 }
367
368 void CompositeEditCommand::deleteSelection(bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
369 {
370     if (endingSelection().isRange())
371         applyCommandToComposite(DeleteSelectionCommand::create(document(), smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
372 }
373
374 void CompositeEditCommand::deleteSelection(const VisibleSelection &selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
375 {
376     if (selection.isRange())
377         applyCommandToComposite(DeleteSelectionCommand::create(selection, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
378 }
379
380 void CompositeEditCommand::removeCSSProperty(PassRefPtr<StyledElement> element, CSSPropertyID property)
381 {
382     applyCommandToComposite(RemoveCSSPropertyCommand::create(document(), element, property));
383 }
384
385 void CompositeEditCommand::removeNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute)
386 {
387     setNodeAttribute(element, attribute, AtomicString());
388 }
389
390 void CompositeEditCommand::setNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute, const AtomicString& value)
391 {
392     applyCommandToComposite(SetNodeAttributeCommand::create(element, attribute, value));
393 }
394
395 static inline bool isWhitespace(UChar c)
396 {
397     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
398 }
399
400 // FIXME: Doesn't go into text nodes that contribute adjacent text (siblings, cousins, etc).
401 void CompositeEditCommand::rebalanceWhitespaceAt(const Position& position)
402 {
403     Node* node = position.node();
404     if (!node || !node->isTextNode())
405         return;
406     Text* textNode = static_cast<Text*>(node);
407     
408     if (textNode->length() == 0)
409         return;
410     RenderObject* renderer = textNode->renderer();
411     if (renderer && !renderer->style()->collapseWhiteSpace())
412         return;
413         
414     String text = textNode->data();
415     ASSERT(!text.isEmpty());
416
417     int offset = position.deprecatedEditingOffset();
418     // If neither text[offset] nor text[offset - 1] are some form of whitespace, do nothing.
419     if (!isWhitespace(text[offset])) {
420         offset--;
421         if (offset < 0 || !isWhitespace(text[offset]))
422             return;
423     }
424     
425     // Set upstream and downstream to define the extent of the whitespace surrounding text[offset].
426     int upstream = offset;
427     while (upstream > 0 && isWhitespace(text[upstream - 1]))
428         upstream--;
429     
430     int downstream = offset;
431     while ((unsigned)downstream + 1 < text.length() && isWhitespace(text[downstream + 1]))
432         downstream++;
433     
434     int length = downstream - upstream + 1;
435     ASSERT(length > 0);
436     
437     VisiblePosition visibleUpstreamPos(Position(position.node(), upstream));
438     VisiblePosition visibleDownstreamPos(Position(position.node(), downstream + 1));
439     
440     String string = text.substring(upstream, length);
441     String rebalancedString = stringWithRebalancedWhitespace(string,
442     // FIXME: Because of the problem mentioned at the top of this function, we must also use nbsps at the start/end of the string because
443     // this function doesn't get all surrounding whitespace, just the whitespace in the current text node.
444                                                              isStartOfParagraph(visibleUpstreamPos) || upstream == 0, 
445                                                              isEndOfParagraph(visibleDownstreamPos) || (unsigned)downstream == text.length() - 1);
446     
447     if (string != rebalancedString)
448         replaceTextInNode(textNode, upstream, length, rebalancedString);
449 }
450
451 void CompositeEditCommand::prepareWhitespaceAtPositionForSplit(Position& position)
452 {
453     Node* node = position.node();
454     if (!node || !node->isTextNode())
455         return;
456     Text* textNode = static_cast<Text*>(node);    
457     
458     if (textNode->length() == 0)
459         return;
460     RenderObject* renderer = textNode->renderer();
461     if (renderer && !renderer->style()->collapseWhiteSpace())
462         return;
463
464     // Delete collapsed whitespace so that inserting nbsps doesn't uncollapse it.
465     Position upstreamPos = position.upstream();
466     deleteInsignificantText(position.upstream(), position.downstream());
467     position = upstreamPos.downstream();
468
469     VisiblePosition visiblePos(position);
470     VisiblePosition previousVisiblePos(visiblePos.previous());
471     Position previous(previousVisiblePos.deepEquivalent());
472     
473     if (isCollapsibleWhitespace(previousVisiblePos.characterAfter()) && previous.node()->isTextNode() && !previous.node()->hasTagName(brTag))
474         replaceTextInNode(static_cast<Text*>(previous.node()), previous.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
475     if (isCollapsibleWhitespace(visiblePos.characterAfter()) && position.node()->isTextNode() && !position.node()->hasTagName(brTag))
476         replaceTextInNode(static_cast<Text*>(position.node()), position.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
477 }
478
479 void CompositeEditCommand::rebalanceWhitespace()
480 {
481     VisibleSelection selection = endingSelection();
482     if (selection.isNone())
483         return;
484         
485     rebalanceWhitespaceAt(selection.start());
486     if (selection.isRange())
487         rebalanceWhitespaceAt(selection.end());
488 }
489
490 void CompositeEditCommand::deleteInsignificantText(PassRefPtr<Text> textNode, unsigned start, unsigned end)
491 {
492     if (!textNode || start >= end)
493         return;
494
495     RenderText* textRenderer = toRenderText(textNode->renderer());
496     if (!textRenderer)
497         return;
498
499     Vector<InlineTextBox*> sortedTextBoxes;
500     size_t sortedTextBoxesPosition = 0;
501    
502     for (InlineTextBox* textBox = textRenderer->firstTextBox(); textBox; textBox = textBox->nextTextBox())
503         sortedTextBoxes.append(textBox);
504     
505     // If there is mixed directionality text, the boxes can be out of order,
506     // (like Arabic with embedded LTR), so sort them first. 
507     if (textRenderer->containsReversedText())    
508         std::sort(sortedTextBoxes.begin(), sortedTextBoxes.end(), InlineTextBox::compareByStart);
509     InlineTextBox* box = sortedTextBoxes.isEmpty() ? 0 : sortedTextBoxes[sortedTextBoxesPosition];
510
511     if (!box) {
512         // whole text node is empty
513         removeNode(textNode);
514         return;    
515     }
516     
517     unsigned length = textNode->length();
518     if (start >= length || end > length)
519         return;
520
521     unsigned removed = 0;
522     InlineTextBox* prevBox = 0;
523     String str;
524
525     // This loop structure works to process all gaps preceding a box,
526     // and also will look at the gap after the last box.
527     while (prevBox || box) {
528         unsigned gapStart = prevBox ? prevBox->start() + prevBox->len() : 0;
529         if (end < gapStart)
530             // No more chance for any intersections
531             break;
532
533         unsigned gapEnd = box ? box->start() : length;
534         bool indicesIntersect = start <= gapEnd && end >= gapStart;
535         int gapLen = gapEnd - gapStart;
536         if (indicesIntersect && gapLen > 0) {
537             gapStart = max(gapStart, start);
538             gapEnd = min(gapEnd, end);
539             if (str.isNull())
540                 str = textNode->data().substring(start, end - start);
541             // remove text in the gap
542             str.remove(gapStart - start - removed, gapLen);
543             removed += gapLen;
544         }
545         
546         prevBox = box;
547         if (box) {
548             if (++sortedTextBoxesPosition < sortedTextBoxes.size())
549                 box = sortedTextBoxes[sortedTextBoxesPosition];
550             else
551                 box = 0;
552         }
553     }
554
555     if (!str.isNull()) {
556         // Replace the text between start and end with our pruned version.
557         if (!str.isEmpty())
558             replaceTextInNode(textNode, start, end - start, str);
559         else {
560             // Assert that we are not going to delete all of the text in the node.
561             // If we were, that should have been done above with the call to 
562             // removeNode and return.
563             ASSERT(start > 0 || end - start < textNode->length());
564             deleteTextFromNode(textNode, start, end - start);
565         }
566     }
567 }
568
569 void CompositeEditCommand::deleteInsignificantText(const Position& start, const Position& end)
570 {
571     if (start.isNull() || end.isNull())
572         return;
573
574     if (comparePositions(start, end) >= 0)
575         return;
576
577     Node* next;
578     for (Node* node = start.node(); node; node = next) {
579         next = node->traverseNextNode();
580         if (node->isTextNode()) {
581             Text* textNode = static_cast<Text*>(node);
582             int startOffset = node == start.node() ? start.deprecatedEditingOffset() : 0;
583             int endOffset = node == end.node() ? end.deprecatedEditingOffset() : static_cast<int>(textNode->length());
584             deleteInsignificantText(textNode, startOffset, endOffset);
585         }
586         if (node == end.node())
587             break;
588     }
589 }
590
591 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos)
592 {
593     Position end = VisiblePosition(pos, VP_DEFAULT_AFFINITY).next().deepEquivalent().downstream();
594     deleteInsignificantText(pos, end);
595 }
596
597 PassRefPtr<Node> CompositeEditCommand::appendBlockPlaceholder(PassRefPtr<Element> container)
598 {
599     if (!container)
600         return 0;
601     
602     // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
603     ASSERT(container->renderer());
604
605     RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
606     appendNode(placeholder, container);
607     return placeholder.release();
608 }
609
610 PassRefPtr<Node> CompositeEditCommand::insertBlockPlaceholder(const Position& pos)
611 {
612     if (pos.isNull())
613         return 0;
614
615     // Should assert isBlockFlow || isInlineFlow when deletion improves.  See 4244964.
616     ASSERT(pos.node()->renderer());
617
618     RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
619     insertNodeAt(placeholder, pos);
620     return placeholder.release();
621 }
622
623 PassRefPtr<Node> CompositeEditCommand::addBlockPlaceholderIfNeeded(Element* container)
624 {
625     if (!container)
626         return 0;
627
628     updateLayout();
629
630     RenderObject* renderer = container->renderer();
631     if (!renderer || !renderer->isBlockFlow())
632         return 0;
633     
634     // append the placeholder to make sure it follows
635     // any unrendered blocks
636     RenderBlock* block = toRenderBlock(renderer);
637     if (block->height() == 0 || (block->isListItem() && block->isEmpty()))
638         return appendBlockPlaceholder(container);
639
640     return 0;
641 }
642
643 // Assumes that the position is at a placeholder and does the removal without much checking.
644 void CompositeEditCommand::removePlaceholderAt(const Position& p)
645 {
646     ASSERT(lineBreakExistsAtPosition(p));
647     
648     // We are certain that the position is at a line break, but it may be a br or a preserved newline.
649     if (p.anchorNode()->hasTagName(brTag)) {
650         removeNode(p.anchorNode());
651         return;
652     }
653     
654     deleteTextFromNode(static_cast<Text*>(p.anchorNode()), p.offsetInContainerNode(), 1);
655 }
656
657 PassRefPtr<Node> CompositeEditCommand::insertNewDefaultParagraphElementAt(const Position& position)
658 {
659     RefPtr<Element> paragraphElement = createDefaultParagraphElement(document());
660     ExceptionCode ec;
661     paragraphElement->appendChild(createBreakElement(document()), ec);
662     insertNodeAt(paragraphElement, position);
663     return paragraphElement.release();
664 }
665
666 // If the paragraph is not entirely within it's own block, create one and move the paragraph into 
667 // it, and return that block.  Otherwise return 0.
668 PassRefPtr<Node> CompositeEditCommand::moveParagraphContentsToNewBlockIfNecessary(const Position& pos)
669 {
670     if (pos.isNull())
671         return 0;
672     
673     updateLayout();
674     
675     // It's strange that this function is responsible for verifying that pos has not been invalidated
676     // by an earlier call to this function.  The caller, applyBlockStyle, should do this.
677     VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
678     VisiblePosition visibleParagraphStart(startOfParagraph(visiblePos));
679     VisiblePosition visibleParagraphEnd = endOfParagraph(visiblePos);
680     VisiblePosition next = visibleParagraphEnd.next();
681     VisiblePosition visibleEnd = next.isNotNull() ? next : visibleParagraphEnd;
682     
683     Position upstreamStart = visibleParagraphStart.deepEquivalent().upstream();
684     Position upstreamEnd = visibleEnd.deepEquivalent().upstream();
685
686     // If there are no VisiblePositions in the same block as pos then 
687     // upstreamStart will be outside the paragraph
688     if (comparePositions(pos, upstreamStart) < 0)
689         return 0;
690
691     // Perform some checks to see if we need to perform work in this function.
692     if (isBlock(upstreamStart.node())) {
693         // If the block is the root editable element, always move content to a new block,
694         // since it is illegal to modify attributes on the root editable element for editing.
695         if (upstreamStart.node() == editableRootForPosition(upstreamStart)) {
696             // If the block is the root editable element and it contains no visible content, create a new
697             // block but don't try and move content into it, since there's nothing for moveParagraphs to move.
698             if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(upstreamStart.node()->renderer()))
699                 return insertNewDefaultParagraphElementAt(upstreamStart);
700         } else if (isBlock(upstreamEnd.node())) {
701             if (!upstreamEnd.node()->isDescendantOf(upstreamStart.node())) {
702                 // If the paragraph end is a descendant of paragraph start, then we need to run
703                 // the rest of this function. If not, we can bail here.
704                 return 0;
705             }
706         }
707         else if (enclosingBlock(upstreamEnd.node()) != upstreamStart.node()) {
708             // The visibleEnd.  It must be an ancestor of the paragraph start.
709             // We can bail as we have a full block to work with.
710             ASSERT(upstreamStart.node()->isDescendantOf(enclosingBlock(upstreamEnd.node())));
711             return 0;
712         }
713         else if (isEndOfDocument(visibleEnd)) {
714             // At the end of the document. We can bail here as well.
715             return 0;
716         }
717     }
718
719     RefPtr<Node> newBlock = insertNewDefaultParagraphElementAt(upstreamStart);
720     
721     moveParagraphs(visibleParagraphStart, visibleParagraphEnd, VisiblePosition(Position(newBlock.get(), 0)));
722     
723     return newBlock.release();
724 }
725
726 void CompositeEditCommand::pushAnchorElementDown(Node* anchorNode)
727 {
728     if (!anchorNode)
729         return;
730     
731     ASSERT(anchorNode->isLink());
732     
733     setEndingSelection(VisibleSelection::selectionFromContentsOfNode(anchorNode));
734     applyStyledElement(static_cast<Element*>(anchorNode));
735     // Clones of anchorNode have been pushed down, now remove it.
736     if (anchorNode->inDocument())
737         removeNodePreservingChildren(anchorNode);
738 }
739
740 // Clone the paragraph between start and end under blockElement,
741 // preserving the hierarchy up to outerNode. 
742
743 void CompositeEditCommand::cloneParagraphUnderNewElement(Position& start, Position& end, Node* outerNode, Element* blockElement)
744 {
745     // First we clone the outerNode
746     
747     RefPtr<Node> topNode = outerNode->cloneNode(isTableElement(outerNode));
748     appendNode(topNode, blockElement);
749     RefPtr<Node> lastNode = topNode;
750
751     if (start.node() != outerNode) {
752         Vector<RefPtr<Node> > ancestors;
753         
754         // Insert each node from innerNode to outerNode (excluded) in a list.
755         for (Node* n = start.node(); n && n != outerNode; n = n->parentNode())
756             ancestors.append(n);
757
758         // Clone every node between start.node() and outerBlock.
759
760         for (size_t i = ancestors.size(); i != 0; --i) {
761             Node* item = ancestors[i - 1].get();
762             RefPtr<Node> child = item->cloneNode(isTableElement(item));
763             appendNode(child, static_cast<Element *>(lastNode.get()));
764             lastNode = child.release();
765         }
766     }
767
768     // Handle the case of paragraphs with more than one node,
769     // cloning all the siblings until end.node() is reached.
770     
771     if (start.node() != end.node() && !start.node()->isDescendantOf(end.node())) {
772         // If end is not a descendant of outerNode we need to
773         // find the first common ancestor and adjust the insertion
774         // point accordingly.
775         while (!end.node()->isDescendantOf(outerNode)) {
776             outerNode = outerNode->parentNode();
777             topNode = topNode->parentNode();
778         }
779
780         for (Node* n = start.node()->traverseNextSibling(outerNode); n; n = n->traverseNextSibling(outerNode)) {
781             if (n->parentNode() != start.node()->parentNode())
782                 lastNode = topNode->lastChild();
783
784             RefPtr<Node> clonedNode = n->cloneNode(true);
785             insertNodeAfter(clonedNode, lastNode);
786             lastNode = clonedNode.release();
787             if (n == end.node() || end.node()->isDescendantOf(n))
788                 break;
789         }
790     }
791 }
792
793     
794 // There are bugs in deletion when it removes a fully selected table/list.
795 // It expands and removes the entire table/list, but will let content
796 // before and after the table/list collapse onto one line.   
797 // Deleting a paragraph will leave a placeholder. Remove it (and prune
798 // empty or unrendered parents).
799
800 void CompositeEditCommand::cleanupAfterDeletion()
801 {
802     VisiblePosition caretAfterDelete = endingSelection().visibleStart();
803     if (isStartOfParagraph(caretAfterDelete) && isEndOfParagraph(caretAfterDelete)) {
804         // Note: We want the rightmost candidate.
805         Position position = caretAfterDelete.deepEquivalent().downstream();
806         Node* node = position.node();
807         // Normally deletion will leave a br as a placeholder.
808         if (node->hasTagName(brTag))
809             removeNodeAndPruneAncestors(node);
810         // If the selection to move was empty and in an empty block that 
811         // doesn't require a placeholder to prop itself open (like a bordered
812         // div or an li), remove it during the move (the list removal code
813         // expects this behavior).
814         else if (isBlock(node))
815             removeNodeAndPruneAncestors(node);
816         else if (lineBreakExistsAtPosition(position)) {
817             // There is a preserved '\n' at caretAfterDelete.
818             // We can safely assume this is a text node.
819             Text* textNode = static_cast<Text*>(node);
820             if (textNode->length() == 1)
821                 removeNodeAndPruneAncestors(node);
822             else
823                 deleteTextFromNode(textNode, position.deprecatedEditingOffset(), 1);
824         }
825     }
826 }
827     
828 // This is a version of moveParagraph that preserves style by keeping the original markup
829 // It is currently used only by IndentOutdentCommand but it is meant to be used in the
830 // future by several other commands such as InsertList and the align commands.
831 // The blockElement parameter is the element to move the paragraph to,
832 // outerNode is the top element of the paragraph hierarchy. 
833
834 void CompositeEditCommand::moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode)
835 {
836     ASSERT(outerNode);
837     ASSERT(blockElement);
838
839     VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
840     VisiblePosition afterParagraph(endOfParagraphToMove.next());
841     
842     // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move.
843     // When we paste a fragment, spaces after the end and before the start are treated as though they were rendered.
844     Position start = startOfParagraphToMove.deepEquivalent().downstream();
845     Position end = endOfParagraphToMove.deepEquivalent().upstream();
846
847     cloneParagraphUnderNewElement(start, end, outerNode, blockElement);
848       
849     setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
850     deleteSelection(false, false, false, false);
851     
852     // There are bugs in deletion when it removes a fully selected table/list.
853     // It expands and removes the entire table/list, but will let content
854     // before and after the table/list collapse onto one line.
855        
856     cleanupAfterDeletion();
857     
858     // Add a br if pruning an empty block level element caused a collapse.  For example:
859     // foo^
860     // <div>bar</div>
861     // baz
862     // Imagine moving 'bar' to ^.  'bar' will be deleted and its div pruned.  That would
863     // cause 'baz' to collapse onto the line with 'foobar' unless we insert a br.
864     // Must recononicalize these two VisiblePositions after the pruning above.
865     beforeParagraph = VisiblePosition(beforeParagraph.deepEquivalent());
866     afterParagraph = VisiblePosition(afterParagraph.deepEquivalent());
867
868     if (beforeParagraph.isNotNull() && !isTableElement(beforeParagraph.deepEquivalent().node())
869         && ((!isEndOfParagraph(beforeParagraph) && !isStartOfParagraph(beforeParagraph)) || beforeParagraph == afterParagraph)) {
870         // FIXME: Trim text between beforeParagraph and afterParagraph if they aren't equal.
871         insertNodeAt(createBreakElement(document()), beforeParagraph.deepEquivalent());
872     }
873 }
874     
875     
876 // This moves a paragraph preserving its style.
877 void CompositeEditCommand::moveParagraph(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
878 {
879     ASSERT(isStartOfParagraph(startOfParagraphToMove));
880     ASSERT(isEndOfParagraph(endOfParagraphToMove));
881     moveParagraphs(startOfParagraphToMove, endOfParagraphToMove, destination, preserveSelection, preserveStyle);
882 }
883
884 void CompositeEditCommand::moveParagraphs(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
885 {
886     if (startOfParagraphToMove == destination)
887         return;
888     
889     int startIndex = -1;
890     int endIndex = -1;
891     int destinationIndex = -1;
892     if (preserveSelection && !endingSelection().isNone()) {
893         VisiblePosition visibleStart = endingSelection().visibleStart();
894         VisiblePosition visibleEnd = endingSelection().visibleEnd();
895         
896         bool startAfterParagraph = comparePositions(visibleStart, endOfParagraphToMove) > 0;
897         bool endBeforeParagraph = comparePositions(visibleEnd, startOfParagraphToMove) < 0;
898         
899         if (!startAfterParagraph && !endBeforeParagraph) {
900             bool startInParagraph = comparePositions(visibleStart, startOfParagraphToMove) >= 0;
901             bool endInParagraph = comparePositions(visibleEnd, endOfParagraphToMove) <= 0;
902             
903             startIndex = 0;
904             if (startInParagraph) {
905                 RefPtr<Range> startRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleStart.deepEquivalent()));
906                 startIndex = TextIterator::rangeLength(startRange.get(), true);
907             }
908
909             endIndex = 0;
910             if (endInParagraph) {
911                 RefPtr<Range> endRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleEnd.deepEquivalent()));
912                 endIndex = TextIterator::rangeLength(endRange.get(), true);
913             }
914         }
915     }
916     
917     VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
918     VisiblePosition afterParagraph(endOfParagraphToMove.next());
919
920     // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move.
921     // When we paste a fragment, spaces after the end and before the start are treated as though they were rendered.
922     Position start = startOfParagraphToMove.deepEquivalent().downstream();
923     Position end = endOfParagraphToMove.deepEquivalent().upstream();
924     
925     // start and end can't be used directly to create a Range; they are "editing positions"
926     Position startRangeCompliant = rangeCompliantEquivalent(start);
927     Position endRangeCompliant = rangeCompliantEquivalent(end);
928     RefPtr<Range> range = Range::create(document(), startRangeCompliant.node(), startRangeCompliant.deprecatedEditingOffset(), endRangeCompliant.node(), endRangeCompliant.deprecatedEditingOffset());
929
930     // FIXME: This is an inefficient way to preserve style on nodes in the paragraph to move. It
931     // shouldn't matter though, since moved paragraphs will usually be quite small.
932     RefPtr<DocumentFragment> fragment;
933     // This used to use a ternary for initialization, but that confused some versions of GCC, see bug 37912
934     if (startOfParagraphToMove != endOfParagraphToMove)
935         fragment = createFragmentFromMarkup(document(), createMarkup(range.get(), 0, DoNotAnnotateForInterchange, true), "");
936
937     // A non-empty paragraph's style is moved when we copy and move it.  We don't move 
938     // anything if we're given an empty paragraph, but an empty paragraph can have style
939     // too, <div><b><br></b></div> for example.  Save it so that we can preserve it later.
940     RefPtr<EditingStyle> styleInEmptyParagraph;
941     if (startOfParagraphToMove == endOfParagraphToMove && preserveStyle) {
942         styleInEmptyParagraph = editingStyleIncludingTypingStyle(startOfParagraphToMove.deepEquivalent());
943         // The moved paragraph should assume the block style of the destination.
944         styleInEmptyParagraph->removeBlockProperties();
945     }
946     
947     // FIXME (5098931): We should add a new insert action "WebViewInsertActionMoved" and call shouldInsertFragment here.
948     
949     setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
950     document()->frame()->editor()->clearMisspellingsAndBadGrammar(endingSelection());
951     deleteSelection(false, false, false, false);
952
953     ASSERT(destination.deepEquivalent().node()->inDocument());
954
955     cleanupAfterDeletion();
956     ASSERT(destination.deepEquivalent().node()->inDocument());
957
958     // Add a br if pruning an empty block level element caused a collapse. For example:
959     // foo^
960     // <div>bar</div>
961     // baz
962     // Imagine moving 'bar' to ^. 'bar' will be deleted and its div pruned. That would
963     // cause 'baz' to collapse onto the line with 'foobar' unless we insert a br.
964     // Must recononicalize these two VisiblePositions after the pruning above.
965     beforeParagraph = VisiblePosition(beforeParagraph.deepEquivalent());
966     afterParagraph = VisiblePosition(afterParagraph.deepEquivalent());
967     if (beforeParagraph.isNotNull() && (!isEndOfParagraph(beforeParagraph) || beforeParagraph == afterParagraph)) {
968         // FIXME: Trim text between beforeParagraph and afterParagraph if they aren't equal.
969         insertNodeAt(createBreakElement(document()), beforeParagraph.deepEquivalent());
970         // Need an updateLayout here in case inserting the br has split a text node.
971         updateLayout();
972     }
973         
974     RefPtr<Range> startToDestinationRange(Range::create(document(), Position(document(), 0), rangeCompliantEquivalent(destination.deepEquivalent())));
975     destinationIndex = TextIterator::rangeLength(startToDestinationRange.get(), true);
976     
977     setEndingSelection(destination);
978     ASSERT(endingSelection().isCaretOrRange());
979     applyCommandToComposite(ReplaceSelectionCommand::create(document(), fragment, true, false, !preserveStyle, false, true));
980
981     document()->frame()->editor()->markMisspellingsAndBadGrammar(endingSelection());
982
983     // If the selection is in an empty paragraph, restore styles from the old empty paragraph to the new empty paragraph.
984     bool selectionIsEmptyParagraph = endingSelection().isCaret() && isStartOfParagraph(endingSelection().visibleStart()) && isEndOfParagraph(endingSelection().visibleStart());
985     if (styleInEmptyParagraph && selectionIsEmptyParagraph)
986         applyStyle(styleInEmptyParagraph->style());
987
988     if (preserveSelection && startIndex != -1) {
989         // Fragment creation (using createMarkup) incorrectly uses regular
990         // spaces instead of nbsps for some spaces that were rendered (11475), which
991         // causes spaces to be collapsed during the move operation.  This results
992         // in a call to rangeFromLocationAndLength with a location past the end
993         // of the document (which will return null).
994         RefPtr<Range> start = TextIterator::rangeFromLocationAndLength(document()->documentElement(), destinationIndex + startIndex, 0, true);
995         RefPtr<Range> end = TextIterator::rangeFromLocationAndLength(document()->documentElement(), destinationIndex + endIndex, 0, true);
996         if (start && end)
997             setEndingSelection(VisibleSelection(start->startPosition(), end->startPosition(), DOWNSTREAM));
998     }
999 }
1000
1001 // FIXME: Send an appropriate shouldDeleteRange call.
1002 bool CompositeEditCommand::breakOutOfEmptyListItem()
1003 {
1004     Node* emptyListItem = enclosingEmptyListItem(endingSelection().visibleStart());
1005     if (!emptyListItem)
1006         return false;
1007
1008     RefPtr<EditingStyle> style = editingStyleIncludingTypingStyle(endingSelection().start());
1009
1010     ContainerNode* listNode = emptyListItem->parentNode();
1011     // FIXME: Can't we do something better when the immediate parent wasn't a list node?
1012     if (!listNode
1013         || (!listNode->hasTagName(ulTag) && !listNode->hasTagName(olTag))
1014         || !listNode->isContentEditable()
1015         || listNode == emptyListItem->rootEditableElement())
1016         return false;
1017
1018     RefPtr<Element> newBlock = 0;
1019     if (ContainerNode* blockEnclosingList = listNode->parentNode()) {
1020         if (blockEnclosingList->hasTagName(liTag)) { // listNode is inside another list item
1021             if (visiblePositionAfterNode(blockEnclosingList) == visiblePositionAfterNode(listNode)) {
1022                 // If listNode appears at the end of the outer list item, then move listNode outside of this list item
1023                 // e.g. <ul><li>hello <ul><li><br></li></ul> </li></ul> should become <ul><li>hello</li> <ul><li><br></li></ul> </ul> after this section
1024                 // If listNode does NOT appear at the end, then we should consider it as a regular paragraph.
1025                 // e.g. <ul><li> <ul><li><br></li></ul> hello</li></ul> should become <ul><li> <div><br></div> hello</li></ul> at the end
1026                 splitElement(static_cast<Element*>(blockEnclosingList), listNode);
1027                 removeNodePreservingChildren(listNode->parentNode());
1028                 newBlock = createListItemElement(document());
1029             }
1030             // If listNode does NOT appear at the end of the outer list item, then behave as if in a regular paragraph.
1031         } else if (blockEnclosingList->hasTagName(olTag) || blockEnclosingList->hasTagName(ulTag))
1032             newBlock = createListItemElement(document());
1033     }
1034     if (!newBlock)
1035         newBlock = createDefaultParagraphElement(document());
1036
1037     if (emptyListItem->renderer()->nextSibling()) {
1038         // If emptyListItem follows another list item, split the list node.
1039         if (emptyListItem->renderer()->previousSibling())
1040             splitElement(static_cast<Element*>(listNode), emptyListItem);
1041
1042         // If emptyListItem is followed by other list item, then insert newBlock before the list node.
1043         // Because we have splitted the element, emptyListItem is the first element in the list node.
1044         // i.e. insert newBlock before ul or ol whose first element is emptyListItem
1045         insertNodeBefore(newBlock, listNode);
1046         removeNode(emptyListItem);
1047     } else {
1048         // When emptyListItem does not follow any list item, insert newBlock after the enclosing list node.
1049         // Remove the enclosing node if emptyListItem is the only child; otherwise just remove emptyListItem.
1050         insertNodeAfter(newBlock, listNode);
1051         removeNode(emptyListItem->renderer()->previousSibling() ? emptyListItem : listNode);
1052     }
1053
1054     appendBlockPlaceholder(newBlock);
1055     setEndingSelection(VisibleSelection(Position(newBlock.get(), 0), DOWNSTREAM));
1056
1057     style->prepareToApplyAt(endingSelection().start());
1058     if (!style->isEmpty())
1059         applyStyle(style->style());
1060
1061     return true;
1062 }
1063
1064 // If the caret is in an empty quoted paragraph, and either there is nothing before that
1065 // paragraph, or what is before is unquoted, and the user presses delete, unquote that paragraph.
1066 bool CompositeEditCommand::breakOutOfEmptyMailBlockquotedParagraph()
1067 {
1068     if (!endingSelection().isCaret())
1069         return false;
1070         
1071     VisiblePosition caret(endingSelection().visibleStart());
1072     Node* highestBlockquote = highestEnclosingNodeOfType(caret.deepEquivalent(), &isMailBlockquote);
1073     if (!highestBlockquote)
1074         return false;
1075         
1076     if (!isStartOfParagraph(caret) || !isEndOfParagraph(caret))
1077         return false;
1078     
1079     VisiblePosition previous(caret.previous(true));
1080     // Only move forward if there's nothing before the caret, or if there's unquoted content before it.
1081     if (enclosingNodeOfType(previous.deepEquivalent(), &isMailBlockquote))
1082         return false;
1083     
1084     RefPtr<Node> br = createBreakElement(document());
1085     // We want to replace this quoted paragraph with an unquoted one, so insert a br
1086     // to hold the caret before the highest blockquote.
1087     insertNodeBefore(br, highestBlockquote);
1088     VisiblePosition atBR(Position(br.get(), 0));
1089     // If the br we inserted collapsed, for example foo<br><blockquote>...</blockquote>, insert
1090     // a second one.
1091     if (!isStartOfParagraph(atBR))
1092         insertNodeBefore(createBreakElement(document()), br);
1093     setEndingSelection(VisibleSelection(atBR));
1094     
1095     // If this is an empty paragraph there must be a line break here.
1096     if (!lineBreakExistsAtVisiblePosition(caret))
1097         return false;
1098     
1099     Position caretPos(caret.deepEquivalent());
1100     // A line break is either a br or a preserved newline.
1101     ASSERT(caretPos.node()->hasTagName(brTag) || (caretPos.node()->isTextNode() && caretPos.node()->renderer()->style()->preserveNewline()));
1102     
1103     if (caretPos.node()->hasTagName(brTag)) {
1104         Position beforeBR(positionInParentBeforeNode(caretPos.node()));
1105         removeNode(caretPos.node());
1106         prune(beforeBR.node());
1107     } else {
1108         ASSERT(caretPos.deprecatedEditingOffset() == 0);
1109         Text* textNode = static_cast<Text*>(caretPos.node());
1110         ContainerNode* parentNode = textNode->parentNode();
1111         // The preserved newline must be the first thing in the node, since otherwise the previous
1112         // paragraph would be quoted, and we verified that it wasn't above.
1113         deleteTextFromNode(textNode, 0, 1);
1114         prune(parentNode);
1115     }
1116     
1117     return true;
1118 }
1119
1120 // Operations use this function to avoid inserting content into an anchor when at the start or the end of
1121 // that anchor, as in NSTextView.
1122 // FIXME: This is only an approximation of NSTextViews insertion behavior, which varies depending on how
1123 // the caret was made. 
1124 Position CompositeEditCommand::positionAvoidingSpecialElementBoundary(const Position& original)
1125 {
1126     if (original.isNull())
1127         return original;
1128         
1129     VisiblePosition visiblePos(original);
1130     Node* enclosingAnchor = enclosingAnchorElement(original);
1131     Position result = original;
1132
1133     if (!enclosingAnchor)
1134         return result;
1135
1136     // Don't avoid block level anchors, because that would insert content into the wrong paragraph.
1137     if (enclosingAnchor && !isBlock(enclosingAnchor)) {
1138         VisiblePosition firstInAnchor(firstDeepEditingPositionForNode(enclosingAnchor));
1139         VisiblePosition lastInAnchor(lastDeepEditingPositionForNode(enclosingAnchor));
1140         // If visually just after the anchor, insert *inside* the anchor unless it's the last
1141         // VisiblePosition in the document, to match NSTextView.
1142         if (visiblePos == lastInAnchor) {
1143             // Make sure anchors are pushed down before avoiding them so that we don't
1144             // also avoid structural elements like lists and blocks (5142012).
1145             if (original.node() != enclosingAnchor && original.node()->parentNode() != enclosingAnchor) {
1146                 pushAnchorElementDown(enclosingAnchor);
1147                 enclosingAnchor = enclosingAnchorElement(original);
1148                 if (!enclosingAnchor)
1149                     return original;
1150             }
1151             // Don't insert outside an anchor if doing so would skip over a line break.  It would
1152             // probably be safe to move the line break so that we could still avoid the anchor here.
1153             Position downstream(visiblePos.deepEquivalent().downstream());
1154             if (lineBreakExistsAtVisiblePosition(visiblePos) && downstream.node()->isDescendantOf(enclosingAnchor))
1155                 return original;
1156             
1157             result = positionInParentAfterNode(enclosingAnchor);
1158         }
1159         // If visually just before an anchor, insert *outside* the anchor unless it's the first
1160         // VisiblePosition in a paragraph, to match NSTextView.
1161         if (visiblePos == firstInAnchor) {
1162             // Make sure anchors are pushed down before avoiding them so that we don't
1163             // also avoid structural elements like lists and blocks (5142012).
1164             if (original.node() != enclosingAnchor && original.node()->parentNode() != enclosingAnchor) {
1165                 pushAnchorElementDown(enclosingAnchor);
1166                 enclosingAnchor = enclosingAnchorElement(original);
1167             }
1168             if (!enclosingAnchor)
1169                 return original;
1170
1171             result = positionInParentBeforeNode(enclosingAnchor);
1172         }
1173     }
1174         
1175     if (result.isNull() || !editableRootForPosition(result))
1176         result = original;
1177     
1178     return result;
1179 }
1180
1181 // Splits the tree parent by parent until we reach the specified ancestor. We use VisiblePositions
1182 // to determine if the split is necessary. Returns the last split node.
1183 PassRefPtr<Node> CompositeEditCommand::splitTreeToNode(Node* start, Node* end, bool splitAncestor)
1184 {
1185     ASSERT(start != end);
1186
1187     RefPtr<Node> node;
1188     for (node = start; node && node->parentNode() != end; node = node->parentNode()) {
1189         if (!node->parentNode()->isElementNode())
1190             break;
1191         VisiblePosition positionInParent(Position(node->parentNode(), 0), DOWNSTREAM);
1192         VisiblePosition positionInNode(Position(node, 0), DOWNSTREAM);
1193         if (positionInParent != positionInNode)
1194             applyCommandToComposite(SplitElementCommand::create(static_cast<Element*>(node->parentNode()), node));
1195     }
1196     if (splitAncestor) {
1197         splitElement(static_cast<Element*>(end), node);
1198         return node->parentNode();
1199     }
1200     return node.release();
1201 }
1202
1203 PassRefPtr<Element> createBlockPlaceholderElement(Document* document)
1204 {
1205     RefPtr<Element> breakNode = document->createElement(brTag, false);
1206     return breakNode.release();
1207 }
1208
1209 } // namespace WebCore