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