2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "CompositeEditCommand.h"
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"
37 #include "DocumentFragment.h"
38 #include "EditorInsertAction.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"
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"
63 #include "TextIterator.h"
64 #include "WrapContentsInDummySpanCommand.h"
65 #include "htmlediting.h"
67 #include "visible_units.h"
73 using namespace HTMLNames;
75 CompositeEditCommand::CompositeEditCommand(Document *document)
76 : EditCommand(document)
80 CompositeEditCommand::~CompositeEditCommand()
84 void CompositeEditCommand::doUnapply()
86 size_t size = m_commands.size();
87 for (size_t i = size; i != 0; --i)
88 m_commands[i - 1]->unapply();
91 void CompositeEditCommand::doReapply()
93 size_t size = m_commands.size();
94 for (size_t i = 0; i != size; ++i)
95 m_commands[i]->reapply();
99 // sugary-sweet convenience functions to help create and apply edit commands in composite commands
101 void CompositeEditCommand::applyCommandToComposite(PassRefPtr<EditCommand> cmd)
103 cmd->setParent(this);
105 m_commands.append(cmd);
108 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, EditAction editingAction)
110 applyCommandToComposite(ApplyStyleCommand::create(document(), style, editingAction));
113 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction editingAction)
115 applyCommandToComposite(ApplyStyleCommand::create(document(), style, start, end, editingAction));
118 void CompositeEditCommand::applyStyledElement(PassRefPtr<Element> element)
120 applyCommandToComposite(ApplyStyleCommand::create(element, false));
123 void CompositeEditCommand::removeStyledElement(PassRefPtr<Element> element)
125 applyCommandToComposite(ApplyStyleCommand::create(element, true));
128 void CompositeEditCommand::insertParagraphSeparator(bool useDefaultParagraphElement)
130 applyCommandToComposite(InsertParagraphSeparatorCommand::create(document(), useDefaultParagraphElement));
133 void CompositeEditCommand::insertLineBreak()
135 applyCommandToComposite(InsertLineBreakCommand::create(document()));
138 void CompositeEditCommand::insertNodeBefore(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
140 ASSERT(!refChild->hasTagName(bodyTag));
141 applyCommandToComposite(InsertNodeBeforeCommand::create(insertChild, refChild));
144 void CompositeEditCommand::insertNodeAfter(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
148 ASSERT(!refChild->hasTagName(bodyTag));
149 Element* parent = refChild->parentElement();
151 if (parent->lastChild() == refChild)
152 appendNode(insertChild, parent);
154 ASSERT(refChild->nextSibling());
155 insertNodeBefore(insertChild, refChild->nextSibling());
159 void CompositeEditCommand::insertNodeAt(PassRefPtr<Node> insertChild, const Position& editingPosition)
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();
168 if (canHaveChildrenForEditing(refChild)) {
169 Node* child = refChild->firstChild();
170 for (int i = 0; child && i < offset; i++)
171 child = child->nextSibling();
173 insertNodeBefore(insertChild, child);
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);
181 // Mutation events (bug 22634) from the text node insertion may have removed the refChild
182 if (!refChild->inDocument())
184 insertNodeBefore(insertChild, refChild);
186 insertNodeAfter(insertChild, refChild);
189 void CompositeEditCommand::appendNode(PassRefPtr<Node> node, PassRefPtr<Element> parent)
191 ASSERT(canHaveChildrenForEditing(parent.get()));
192 applyCommandToComposite(AppendNodeCommand::create(parent, node));
195 void CompositeEditCommand::removeChildrenInRange(PassRefPtr<Node> node, unsigned from, unsigned to)
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);
202 size_t size = children.size();
203 for (size_t i = 0; i < size; ++i)
204 removeNode(children[i].release());
207 void CompositeEditCommand::removeNode(PassRefPtr<Node> node)
209 if (!node || !node->parentNode())
211 applyCommandToComposite(RemoveNodeCommand::create(node));
214 void CompositeEditCommand::removeNodePreservingChildren(PassRefPtr<Node> node)
216 applyCommandToComposite(RemoveNodePreservingChildrenCommand::create(node));
219 void CompositeEditCommand::removeNodeAndPruneAncestors(PassRefPtr<Node> node)
221 RefPtr<ContainerNode> parent = node->parentNode();
223 prune(parent.release());
226 HTMLElement* CompositeEditCommand::replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement> node)
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();
240 static bool hasARenderedDescendant(Node* node)
242 Node* n = node->firstChild();
246 n = n->traverseNextNode(node);
251 void CompositeEditCommand::prune(PassRefPtr<Node> 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))
259 RefPtr<ContainerNode> next = node->parentNode();
265 void CompositeEditCommand::splitTextNode(PassRefPtr<Text> node, unsigned offset)
267 applyCommandToComposite(SplitTextNodeCommand::create(node, offset));
270 void CompositeEditCommand::splitElement(PassRefPtr<Element> element, PassRefPtr<Node> atChild)
272 applyCommandToComposite(SplitElementCommand::create(element, atChild));
275 void CompositeEditCommand::mergeIdenticalElements(PassRefPtr<Element> prpFirst, PassRefPtr<Element> prpSecond)
277 RefPtr<Element> first = prpFirst;
278 RefPtr<Element> second = prpSecond;
279 ASSERT(!first->isDescendantOf(second.get()) && second != first);
280 if (first->nextSibling() != second) {
282 insertNodeAfter(second, first);
284 applyCommandToComposite(MergeIdenticalElementsCommand::create(first, second));
287 void CompositeEditCommand::wrapContentsInDummySpan(PassRefPtr<Element> element)
289 applyCommandToComposite(WrapContentsInDummySpanCommand::create(element));
292 void CompositeEditCommand::splitTextNodeContainingElement(PassRefPtr<Text> text, unsigned offset)
294 applyCommandToComposite(SplitTextNodeContainingElementCommand::create(text, offset));
297 void CompositeEditCommand::joinTextNodes(PassRefPtr<Text> text1, PassRefPtr<Text> text2)
299 applyCommandToComposite(JoinTextNodesCommand::create(text1, text2));
302 void CompositeEditCommand::inputText(const String& text, bool selectInsertedText)
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());
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);
317 if (newline != notFound)
320 offset = newline + 1;
321 } while (newline != notFound && offset != length);
323 if (selectInsertedText) {
324 RefPtr<Range> selectedRange = TextIterator::rangeFromLocationAndLength(document()->documentElement(), startIndex, length);
325 setEndingSelection(VisibleSelection(selectedRange.get()));
329 void CompositeEditCommand::insertTextIntoNode(PassRefPtr<Text> node, unsigned offset, const String& text)
331 applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, text));
334 void CompositeEditCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
336 applyCommandToComposite(DeleteFromTextNodeCommand::create(node, offset, count));
339 void CompositeEditCommand::replaceTextInNode(PassRefPtr<Text> node, unsigned offset, unsigned count, const String& replacementText)
341 applyCommandToComposite(DeleteFromTextNodeCommand::create(node.get(), offset, count));
342 applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, replacementText));
345 Position CompositeEditCommand::positionOutsideTabSpan(const Position& pos)
347 if (!isTabSpanTextNode(pos.node()))
350 Node* tabSpan = tabSpanNode(pos.node());
352 if (pos.deprecatedEditingOffset() <= caretMinOffset(pos.node()))
353 return positionInParentBeforeNode(tabSpan);
355 if (pos.deprecatedEditingOffset() >= caretMaxOffset(pos.node()))
356 return positionInParentAfterNode(tabSpan);
358 splitTextNodeContainingElement(static_cast<Text *>(pos.node()), pos.deprecatedEditingOffset());
359 return positionInParentBeforeNode(tabSpan);
362 void CompositeEditCommand::insertNodeAtTabSpanPosition(PassRefPtr<Node> node, const Position& pos)
364 // insert node before, after, or at split of tab span
365 insertNodeAt(node, positionOutsideTabSpan(pos));
368 void CompositeEditCommand::deleteSelection(bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
370 if (endingSelection().isRange())
371 applyCommandToComposite(DeleteSelectionCommand::create(document(), smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
374 void CompositeEditCommand::deleteSelection(const VisibleSelection &selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
376 if (selection.isRange())
377 applyCommandToComposite(DeleteSelectionCommand::create(selection, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
380 void CompositeEditCommand::removeCSSProperty(PassRefPtr<StyledElement> element, CSSPropertyID property)
382 applyCommandToComposite(RemoveCSSPropertyCommand::create(document(), element, property));
385 void CompositeEditCommand::removeNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute)
387 setNodeAttribute(element, attribute, AtomicString());
390 void CompositeEditCommand::setNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute, const AtomicString& value)
392 applyCommandToComposite(SetNodeAttributeCommand::create(element, attribute, value));
395 static inline bool isWhitespace(UChar c)
397 return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
400 // FIXME: Doesn't go into text nodes that contribute adjacent text (siblings, cousins, etc).
401 void CompositeEditCommand::rebalanceWhitespaceAt(const Position& position)
403 Node* node = position.node();
404 if (!node || !node->isTextNode())
406 Text* textNode = static_cast<Text*>(node);
408 if (textNode->length() == 0)
410 RenderObject* renderer = textNode->renderer();
411 if (renderer && !renderer->style()->collapseWhiteSpace())
414 String text = textNode->data();
415 ASSERT(!text.isEmpty());
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])) {
421 if (offset < 0 || !isWhitespace(text[offset]))
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]))
430 int downstream = offset;
431 while ((unsigned)downstream + 1 < text.length() && isWhitespace(text[downstream + 1]))
434 int length = downstream - upstream + 1;
437 VisiblePosition visibleUpstreamPos(Position(position.node(), upstream));
438 VisiblePosition visibleDownstreamPos(Position(position.node(), downstream + 1));
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);
447 if (string != rebalancedString)
448 replaceTextInNode(textNode, upstream, length, rebalancedString);
451 void CompositeEditCommand::prepareWhitespaceAtPositionForSplit(Position& position)
453 Node* node = position.node();
454 if (!node || !node->isTextNode())
456 Text* textNode = static_cast<Text*>(node);
458 if (textNode->length() == 0)
460 RenderObject* renderer = textNode->renderer();
461 if (renderer && !renderer->style()->collapseWhiteSpace())
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();
469 VisiblePosition visiblePos(position);
470 VisiblePosition previousVisiblePos(visiblePos.previous());
471 Position previous(previousVisiblePos.deepEquivalent());
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());
479 void CompositeEditCommand::rebalanceWhitespace()
481 VisibleSelection selection = endingSelection();
482 if (selection.isNone())
485 rebalanceWhitespaceAt(selection.start());
486 if (selection.isRange())
487 rebalanceWhitespaceAt(selection.end());
490 void CompositeEditCommand::deleteInsignificantText(PassRefPtr<Text> textNode, unsigned start, unsigned end)
492 if (!textNode || start >= end)
495 RenderText* textRenderer = toRenderText(textNode->renderer());
499 Vector<InlineTextBox*> sortedTextBoxes;
500 size_t sortedTextBoxesPosition = 0;
502 for (InlineTextBox* textBox = textRenderer->firstTextBox(); textBox; textBox = textBox->nextTextBox())
503 sortedTextBoxes.append(textBox);
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];
512 // whole text node is empty
513 removeNode(textNode);
517 unsigned length = textNode->length();
518 if (start >= length || end > length)
521 unsigned removed = 0;
522 InlineTextBox* prevBox = 0;
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;
530 // No more chance for any intersections
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);
540 str = textNode->data().substring(start, end - start);
541 // remove text in the gap
542 str.remove(gapStart - start - removed, gapLen);
548 if (++sortedTextBoxesPosition < sortedTextBoxes.size())
549 box = sortedTextBoxes[sortedTextBoxesPosition];
556 // Replace the text between start and end with our pruned version.
558 replaceTextInNode(textNode, start, end - start, str);
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);
569 void CompositeEditCommand::deleteInsignificantText(const Position& start, const Position& end)
571 if (start.isNull() || end.isNull())
574 if (comparePositions(start, end) >= 0)
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);
586 if (node == end.node())
591 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos)
593 Position end = VisiblePosition(pos, VP_DEFAULT_AFFINITY).next().deepEquivalent().downstream();
594 deleteInsignificantText(pos, end);
597 PassRefPtr<Node> CompositeEditCommand::appendBlockPlaceholder(PassRefPtr<Element> container)
602 // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
603 ASSERT(container->renderer());
605 RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
606 appendNode(placeholder, container);
607 return placeholder.release();
610 PassRefPtr<Node> CompositeEditCommand::insertBlockPlaceholder(const Position& pos)
615 // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
616 ASSERT(pos.node()->renderer());
618 RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
619 insertNodeAt(placeholder, pos);
620 return placeholder.release();
623 PassRefPtr<Node> CompositeEditCommand::addBlockPlaceholderIfNeeded(Element* container)
630 RenderObject* renderer = container->renderer();
631 if (!renderer || !renderer->isBlockFlow())
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);
643 // Assumes that the position is at a placeholder and does the removal without much checking.
644 void CompositeEditCommand::removePlaceholderAt(const Position& p)
646 ASSERT(lineBreakExistsAtPosition(p));
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());
654 deleteTextFromNode(static_cast<Text*>(p.anchorNode()), p.offsetInContainerNode(), 1);
657 PassRefPtr<Node> CompositeEditCommand::insertNewDefaultParagraphElementAt(const Position& position)
659 RefPtr<Element> paragraphElement = createDefaultParagraphElement(document());
661 paragraphElement->appendChild(createBreakElement(document()), ec);
662 insertNodeAt(paragraphElement, position);
663 return paragraphElement.release();
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)
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;
683 Position upstreamStart = visibleParagraphStart.deepEquivalent().upstream();
684 Position upstreamEnd = visibleEnd.deepEquivalent().upstream();
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)
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.
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())));
713 else if (isEndOfDocument(visibleEnd)) {
714 // At the end of the document. We can bail here as well.
719 RefPtr<Node> newBlock = insertNewDefaultParagraphElementAt(upstreamStart);
721 moveParagraphs(visibleParagraphStart, visibleParagraphEnd, VisiblePosition(Position(newBlock.get(), 0)));
723 return newBlock.release();
726 void CompositeEditCommand::pushAnchorElementDown(Node* anchorNode)
731 ASSERT(anchorNode->isLink());
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);
740 // Clone the paragraph between start and end under blockElement,
741 // preserving the hierarchy up to outerNode.
743 void CompositeEditCommand::cloneParagraphUnderNewElement(Position& start, Position& end, Node* outerNode, Element* blockElement)
745 // First we clone the outerNode
747 RefPtr<Node> topNode = outerNode->cloneNode(isTableElement(outerNode));
748 appendNode(topNode, blockElement);
749 RefPtr<Node> lastNode = topNode;
751 if (start.node() != outerNode) {
752 Vector<RefPtr<Node> > ancestors;
754 // Insert each node from innerNode to outerNode (excluded) in a list.
755 for (Node* n = start.node(); n && n != outerNode; n = n->parentNode())
758 // Clone every node between start.node() and outerBlock.
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();
768 // Handle the case of paragraphs with more than one node,
769 // cloning all the siblings until end.node() is reached.
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();
780 for (Node* n = start.node()->traverseNextSibling(outerNode); n; n = n->traverseNextSibling(outerNode)) {
781 if (n->parentNode() != start.node()->parentNode())
782 lastNode = topNode->lastChild();
784 RefPtr<Node> clonedNode = n->cloneNode(true);
785 insertNodeAfter(clonedNode, lastNode);
786 lastNode = clonedNode.release();
787 if (n == end.node() || end.node()->isDescendantOf(n))
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).
800 void CompositeEditCommand::cleanupAfterDeletion()
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);
823 deleteTextFromNode(textNode, position.deprecatedEditingOffset(), 1);
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.
834 void CompositeEditCommand::moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode)
837 ASSERT(blockElement);
839 VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
840 VisiblePosition afterParagraph(endOfParagraphToMove.next());
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();
847 cloneParagraphUnderNewElement(start, end, outerNode, blockElement);
849 setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
850 deleteSelection(false, false, false, false);
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.
856 cleanupAfterDeletion();
858 // Add a br if pruning an empty block level element caused a collapse. For example:
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());
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());
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)
879 ASSERT(isStartOfParagraph(startOfParagraphToMove));
880 ASSERT(isEndOfParagraph(endOfParagraphToMove));
881 moveParagraphs(startOfParagraphToMove, endOfParagraphToMove, destination, preserveSelection, preserveStyle);
884 void CompositeEditCommand::moveParagraphs(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
886 if (startOfParagraphToMove == destination)
891 int destinationIndex = -1;
892 if (preserveSelection && !endingSelection().isNone()) {
893 VisiblePosition visibleStart = endingSelection().visibleStart();
894 VisiblePosition visibleEnd = endingSelection().visibleEnd();
896 bool startAfterParagraph = comparePositions(visibleStart, endOfParagraphToMove) > 0;
897 bool endBeforeParagraph = comparePositions(visibleEnd, startOfParagraphToMove) < 0;
899 if (!startAfterParagraph && !endBeforeParagraph) {
900 bool startInParagraph = comparePositions(visibleStart, startOfParagraphToMove) >= 0;
901 bool endInParagraph = comparePositions(visibleEnd, endOfParagraphToMove) <= 0;
904 if (startInParagraph) {
905 RefPtr<Range> startRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleStart.deepEquivalent()));
906 startIndex = TextIterator::rangeLength(startRange.get(), true);
910 if (endInParagraph) {
911 RefPtr<Range> endRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleEnd.deepEquivalent()));
912 endIndex = TextIterator::rangeLength(endRange.get(), true);
917 VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
918 VisiblePosition afterParagraph(endOfParagraphToMove.next());
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();
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());
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), "");
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();
947 // FIXME (5098931): We should add a new insert action "WebViewInsertActionMoved" and call shouldInsertFragment here.
949 setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
950 document()->frame()->editor()->clearMisspellingsAndBadGrammar(endingSelection());
951 deleteSelection(false, false, false, false);
953 ASSERT(destination.deepEquivalent().node()->inDocument());
955 cleanupAfterDeletion();
956 ASSERT(destination.deepEquivalent().node()->inDocument());
958 // Add a br if pruning an empty block level element caused a collapse. For example:
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.
974 RefPtr<Range> startToDestinationRange(Range::create(document(), Position(document(), 0), rangeCompliantEquivalent(destination.deepEquivalent())));
975 destinationIndex = TextIterator::rangeLength(startToDestinationRange.get(), true);
977 setEndingSelection(destination);
978 ASSERT(endingSelection().isCaretOrRange());
979 applyCommandToComposite(ReplaceSelectionCommand::create(document(), fragment, true, false, !preserveStyle, false, true));
981 document()->frame()->editor()->markMisspellingsAndBadGrammar(endingSelection());
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());
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);
997 setEndingSelection(VisibleSelection(start->startPosition(), end->startPosition(), DOWNSTREAM));
1001 // FIXME: Send an appropriate shouldDeleteRange call.
1002 bool CompositeEditCommand::breakOutOfEmptyListItem()
1004 Node* emptyListItem = enclosingEmptyListItem(endingSelection().visibleStart());
1008 RefPtr<EditingStyle> style = editingStyleIncludingTypingStyle(endingSelection().start());
1010 ContainerNode* listNode = emptyListItem->parentNode();
1011 // FIXME: Can't we do something better when the immediate parent wasn't a list node?
1013 || (!listNode->hasTagName(ulTag) && !listNode->hasTagName(olTag))
1014 || !listNode->isContentEditable()
1015 || listNode == emptyListItem->rootEditableElement())
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());
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());
1035 newBlock = createDefaultParagraphElement(document());
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);
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);
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);
1054 appendBlockPlaceholder(newBlock);
1055 setEndingSelection(VisibleSelection(Position(newBlock.get(), 0), DOWNSTREAM));
1057 style->prepareToApplyAt(endingSelection().start());
1058 if (!style->isEmpty())
1059 applyStyle(style->style());
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()
1068 if (!endingSelection().isCaret())
1071 VisiblePosition caret(endingSelection().visibleStart());
1072 Node* highestBlockquote = highestEnclosingNodeOfType(caret.deepEquivalent(), &isMailBlockquote);
1073 if (!highestBlockquote)
1076 if (!isStartOfParagraph(caret) || !isEndOfParagraph(caret))
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))
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
1091 if (!isStartOfParagraph(atBR))
1092 insertNodeBefore(createBreakElement(document()), br);
1093 setEndingSelection(VisibleSelection(atBR));
1095 // If this is an empty paragraph there must be a line break here.
1096 if (!lineBreakExistsAtVisiblePosition(caret))
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()));
1103 if (caretPos.node()->hasTagName(brTag)) {
1104 Position beforeBR(positionInParentBeforeNode(caretPos.node()));
1105 removeNode(caretPos.node());
1106 prune(beforeBR.node());
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);
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)
1126 if (original.isNull())
1129 VisiblePosition visiblePos(original);
1130 Node* enclosingAnchor = enclosingAnchorElement(original);
1131 Position result = original;
1133 if (!enclosingAnchor)
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)
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))
1157 result = positionInParentAfterNode(enclosingAnchor);
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);
1168 if (!enclosingAnchor)
1171 result = positionInParentBeforeNode(enclosingAnchor);
1175 if (result.isNull() || !editableRootForPosition(result))
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)
1185 ASSERT(start != end);
1188 for (node = start; node && node->parentNode() != end; node = node->parentNode()) {
1189 if (!node->parentNode()->isElementNode())
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));
1196 if (splitAncestor) {
1197 splitElement(static_cast<Element*>(end), node);
1198 return node->parentNode();
1200 return node.release();
1203 PassRefPtr<Element> createBlockPlaceholderElement(Document* document)
1205 RefPtr<Element> breakNode = document->createElement(brTag, false);
1206 return breakNode.release();
1209 } // namespace WebCore