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 "DeleteFromTextNodeCommand.h"
32 #include "DeleteSelectionCommand.h"
34 #include "DocumentFragment.h"
35 #include "DocumentMarkerController.h"
36 #include "EditorInsertAction.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"
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"
60 #include "TextIterator.h"
61 #include "WrapContentsInDummySpanCommand.h"
62 #include "htmlediting.h"
64 #include "visible_units.h"
65 #include <wtf/unicode/CharacterNames.h>
71 using namespace HTMLNames;
73 CompositeEditCommand::CompositeEditCommand(Document *document)
74 : EditCommand(document)
78 CompositeEditCommand::~CompositeEditCommand()
82 void CompositeEditCommand::doUnapply()
84 size_t size = m_commands.size();
85 for (size_t i = size; i != 0; --i)
86 m_commands[i - 1]->unapply();
89 void CompositeEditCommand::doReapply()
91 size_t size = m_commands.size();
92 for (size_t i = 0; i != size; ++i)
93 m_commands[i]->reapply();
97 // sugary-sweet convenience functions to help create and apply edit commands in composite commands
99 void CompositeEditCommand::applyCommandToComposite(PassRefPtr<EditCommand> cmd)
101 cmd->setParent(this);
103 m_commands.append(cmd);
106 void CompositeEditCommand::applyCommandToComposite(PassRefPtr<CompositeEditCommand> command, const VisibleSelection& selection)
108 command->setParent(this);
109 if (selection != command->endingSelection()) {
110 command->setStartingSelection(selection);
111 command->setEndingSelection(selection);
114 m_commands.append(command);
117 void CompositeEditCommand::applyStyle(const EditingStyle* style, EditAction editingAction)
119 applyCommandToComposite(ApplyStyleCommand::create(document(), style, editingAction));
122 void CompositeEditCommand::applyStyle(const EditingStyle* style, const Position& start, const Position& end, EditAction editingAction)
124 applyCommandToComposite(ApplyStyleCommand::create(document(), style, start, end, editingAction));
127 void CompositeEditCommand::applyStyledElement(PassRefPtr<Element> element)
129 applyCommandToComposite(ApplyStyleCommand::create(element, false));
132 void CompositeEditCommand::removeStyledElement(PassRefPtr<Element> element)
134 applyCommandToComposite(ApplyStyleCommand::create(element, true));
137 void CompositeEditCommand::insertParagraphSeparator(bool useDefaultParagraphElement)
139 applyCommandToComposite(InsertParagraphSeparatorCommand::create(document(), useDefaultParagraphElement));
142 void CompositeEditCommand::insertLineBreak()
144 applyCommandToComposite(InsertLineBreakCommand::create(document()));
147 void CompositeEditCommand::insertNodeBefore(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
149 ASSERT(!refChild->hasTagName(bodyTag));
150 applyCommandToComposite(InsertNodeBeforeCommand::create(insertChild, refChild));
153 void CompositeEditCommand::insertNodeAfter(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
157 ASSERT(!refChild->hasTagName(bodyTag));
158 ContainerNode* parent = refChild->parentNode();
160 ASSERT(!parent->isShadowRoot());
161 if (parent->lastChild() == refChild)
162 appendNode(insertChild, parent);
164 ASSERT(refChild->nextSibling());
165 insertNodeBefore(insertChild, refChild->nextSibling());
169 void CompositeEditCommand::insertNodeAt(PassRefPtr<Node> insertChild, const Position& editingPosition)
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();
178 if (canHaveChildrenForEditing(refChild)) {
179 Node* child = refChild->firstChild();
180 for (int i = 0; child && i < offset; i++)
181 child = child->nextSibling();
183 insertNodeBefore(insertChild, child);
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);
191 // Mutation events (bug 22634) from the text node insertion may have removed the refChild
192 if (!refChild->inDocument())
194 insertNodeBefore(insertChild, refChild);
196 insertNodeAfter(insertChild, refChild);
199 void CompositeEditCommand::appendNode(PassRefPtr<Node> node, PassRefPtr<ContainerNode> parent)
201 ASSERT(canHaveChildrenForEditing(parent.get()));
202 applyCommandToComposite(AppendNodeCommand::create(parent, node));
205 void CompositeEditCommand::removeChildrenInRange(PassRefPtr<Node> node, unsigned from, unsigned to)
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);
212 size_t size = children.size();
213 for (size_t i = 0; i < size; ++i)
214 removeNode(children[i].release());
217 void CompositeEditCommand::removeNode(PassRefPtr<Node> node)
219 if (!node || !node->nonShadowBoundaryParentNode())
221 applyCommandToComposite(RemoveNodeCommand::create(node));
224 void CompositeEditCommand::removeNodePreservingChildren(PassRefPtr<Node> node)
226 applyCommandToComposite(RemoveNodePreservingChildrenCommand::create(node));
229 void CompositeEditCommand::removeNodeAndPruneAncestors(PassRefPtr<Node> node)
231 RefPtr<ContainerNode> parent = node->parentNode();
233 prune(parent.release());
236 HTMLElement* CompositeEditCommand::replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement> node)
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();
250 static bool hasARenderedDescendant(Node* node)
252 Node* n = node->firstChild();
256 n = n->traverseNextNode(node);
261 void CompositeEditCommand::prune(PassRefPtr<Node> prpNode)
263 RefPtr<Node> node = prpNode;
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))
271 RefPtr<ContainerNode> next = node->parentNode();
277 void CompositeEditCommand::splitTextNode(PassRefPtr<Text> node, unsigned offset)
279 applyCommandToComposite(SplitTextNodeCommand::create(node, offset));
282 void CompositeEditCommand::splitElement(PassRefPtr<Element> element, PassRefPtr<Node> atChild)
284 applyCommandToComposite(SplitElementCommand::create(element, atChild));
287 void CompositeEditCommand::mergeIdenticalElements(PassRefPtr<Element> prpFirst, PassRefPtr<Element> prpSecond)
289 RefPtr<Element> first = prpFirst;
290 RefPtr<Element> second = prpSecond;
291 ASSERT(!first->isDescendantOf(second.get()) && second != first);
292 if (first->nextSibling() != second) {
294 insertNodeAfter(second, first);
296 applyCommandToComposite(MergeIdenticalElementsCommand::create(first, second));
299 void CompositeEditCommand::wrapContentsInDummySpan(PassRefPtr<Element> element)
301 applyCommandToComposite(WrapContentsInDummySpanCommand::create(element));
304 void CompositeEditCommand::splitTextNodeContainingElement(PassRefPtr<Text> text, unsigned offset)
306 applyCommandToComposite(SplitTextNodeContainingElementCommand::create(text, offset));
309 void CompositeEditCommand::insertTextIntoNode(PassRefPtr<Text> node, unsigned offset, const String& text)
312 applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, text));
315 void CompositeEditCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
317 applyCommandToComposite(DeleteFromTextNodeCommand::create(node, offset, count));
320 void CompositeEditCommand::replaceTextInNode(PassRefPtr<Text> node, unsigned offset, unsigned count, const String& replacementText)
322 applyCommandToComposite(DeleteFromTextNodeCommand::create(node.get(), offset, count));
323 if (!replacementText.isEmpty())
324 applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, replacementText));
327 Position CompositeEditCommand::replaceSelectedTextInNode(const String& text)
329 Position start = endingSelection().start();
330 Position end = endingSelection().end();
331 if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode() || isTabSpanTextNode(start.containerNode()))
334 RefPtr<Text> textNode = start.containerText();
335 replaceTextInNode(textNode, start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
337 return Position(textNode.release(), start.offsetInContainerNode() + text.length());
340 static void copyMarkers(const Vector<DocumentMarker*>& markerPointers, Vector<DocumentMarker>& markers)
342 size_t arraySize = markerPointers.size();
343 markers.reserveCapacity(arraySize);
344 for (size_t i = 0; i < arraySize; ++i)
345 markers.append(*markerPointers[i]);
348 void CompositeEditCommand::replaceTextInNodePreservingMarkers(PassRefPtr<Text> prpNode, unsigned offset, unsigned count, const String& replacementText)
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());
360 Position CompositeEditCommand::positionOutsideTabSpan(const Position& pos)
362 if (!isTabSpanTextNode(pos.anchorNode()))
365 switch (pos.anchorType()) {
366 case Position::PositionIsBeforeChildren:
367 case Position::PositionIsAfterChildren:
368 ASSERT_NOT_REACHED();
370 case Position::PositionIsOffsetInAnchor:
372 case Position::PositionIsBeforeAnchor:
373 return positionInParentBeforeNode(pos.anchorNode());
374 case Position::PositionIsAfterAnchor:
375 return positionInParentAfterNode(pos.anchorNode());
378 Node* tabSpan = tabSpanNode(pos.containerNode());
380 if (pos.offsetInContainerNode() <= caretMinOffset(pos.containerNode()))
381 return positionInParentBeforeNode(tabSpan);
383 if (pos.offsetInContainerNode() >= caretMaxOffset(pos.containerNode()))
384 return positionInParentAfterNode(tabSpan);
386 splitTextNodeContainingElement(static_cast<Text *>(pos.containerNode()), pos.offsetInContainerNode());
387 return positionInParentBeforeNode(tabSpan);
390 void CompositeEditCommand::insertNodeAtTabSpanPosition(PassRefPtr<Node> node, const Position& pos)
392 // insert node before, after, or at split of tab span
393 insertNodeAt(node, positionOutsideTabSpan(pos));
396 void CompositeEditCommand::deleteSelection(bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
398 if (endingSelection().isRange())
399 applyCommandToComposite(DeleteSelectionCommand::create(document(), smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
402 void CompositeEditCommand::deleteSelection(const VisibleSelection &selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
404 if (selection.isRange())
405 applyCommandToComposite(DeleteSelectionCommand::create(selection, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
408 void CompositeEditCommand::removeCSSProperty(PassRefPtr<StyledElement> element, CSSPropertyID property)
410 applyCommandToComposite(RemoveCSSPropertyCommand::create(document(), element, property));
413 void CompositeEditCommand::removeNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute)
415 setNodeAttribute(element, attribute, AtomicString());
418 void CompositeEditCommand::setNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute, const AtomicString& value)
420 applyCommandToComposite(SetNodeAttributeCommand::create(element, attribute, value));
423 static inline bool containsOnlyWhitespace(const String& text)
425 for (unsigned i = 0; i < text.length(); ++i) {
426 if (!isWhitespace(text.characters()[i]))
433 bool CompositeEditCommand::shouldRebalanceLeadingWhitespaceFor(const String& text) const
435 return containsOnlyWhitespace(text);
438 bool CompositeEditCommand::canRebalance(const Position& position) const
440 Node* node = position.containerNode();
441 if (position.anchorType() != Position::PositionIsOffsetInAnchor || !node || !node->isTextNode())
444 Text* textNode = static_cast<Text*>(node);
445 if (textNode->length() == 0)
448 RenderObject* renderer = textNode->renderer();
449 if (renderer && !renderer->style()->collapseWhiteSpace())
455 // FIXME: Doesn't go into text nodes that contribute adjacent text (siblings, cousins, etc).
456 void CompositeEditCommand::rebalanceWhitespaceAt(const Position& position)
458 Node* node = position.containerNode();
459 if (!canRebalance(position))
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])) {
467 if (offset < 0 || !isWhitespace(text[offset]))
471 rebalanceWhitespaceOnTextSubstring(static_cast<Text*>(node), position.offsetInContainerNode(), position.offsetInContainerNode());
474 void CompositeEditCommand::rebalanceWhitespaceOnTextSubstring(PassRefPtr<Text> prpTextNode, int startOffset, int endOffset)
476 RefPtr<Text> textNode = prpTextNode;
478 String text = textNode->data();
479 ASSERT(!text.isEmpty());
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]))
486 int downstream = endOffset;
487 while ((unsigned)downstream < text.length() && isWhitespace(text[downstream]))
490 int length = downstream - upstream;
494 VisiblePosition visibleUpstreamPos(Position(textNode, upstream));
495 VisiblePosition visibleDownstreamPos(Position(textNode, downstream));
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());
504 if (string != rebalancedString)
505 replaceTextInNodePreservingMarkers(textNode.release(), upstream, length, rebalancedString);
508 void CompositeEditCommand::prepareWhitespaceAtPositionForSplit(Position& position)
510 Node* node = position.deprecatedNode();
511 if (!node || !node->isTextNode())
513 Text* textNode = static_cast<Text*>(node);
515 if (textNode->length() == 0)
517 RenderObject* renderer = textNode->renderer();
518 if (renderer && !renderer->style()->collapseWhiteSpace())
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();
526 VisiblePosition visiblePos(position);
527 VisiblePosition previousVisiblePos(visiblePos.previous());
528 Position previous(previousVisiblePos.deepEquivalent());
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());
536 void CompositeEditCommand::rebalanceWhitespace()
538 VisibleSelection selection = endingSelection();
539 if (selection.isNone())
542 rebalanceWhitespaceAt(selection.start());
543 if (selection.isRange())
544 rebalanceWhitespaceAt(selection.end());
547 void CompositeEditCommand::deleteInsignificantText(PassRefPtr<Text> textNode, unsigned start, unsigned end)
549 if (!textNode || start >= end)
552 RenderText* textRenderer = toRenderText(textNode->renderer());
556 Vector<InlineTextBox*> sortedTextBoxes;
557 size_t sortedTextBoxesPosition = 0;
559 for (InlineTextBox* textBox = textRenderer->firstTextBox(); textBox; textBox = textBox->nextTextBox())
560 sortedTextBoxes.append(textBox);
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];
569 // whole text node is empty
570 removeNode(textNode);
574 unsigned length = textNode->length();
575 if (start >= length || end > length)
578 unsigned removed = 0;
579 InlineTextBox* prevBox = 0;
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;
587 // No more chance for any intersections
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);
597 str = textNode->data().substring(start, end - start);
598 // remove text in the gap
599 str.remove(gapStart - start - removed, gapLen);
605 if (++sortedTextBoxesPosition < sortedTextBoxes.size())
606 box = sortedTextBoxes[sortedTextBoxesPosition];
613 // Replace the text between start and end with our pruned version.
615 replaceTextInNode(textNode, start, end - start, str);
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);
626 void CompositeEditCommand::deleteInsignificantText(const Position& start, const Position& end)
628 if (start.isNull() || end.isNull())
631 if (comparePositions(start, end) >= 0)
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);
643 if (node == end.deprecatedNode())
648 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos)
650 Position end = VisiblePosition(pos, VP_DEFAULT_AFFINITY).next().deepEquivalent().downstream();
651 deleteInsignificantText(pos, end);
654 PassRefPtr<Node> CompositeEditCommand::appendBlockPlaceholder(PassRefPtr<Element> container)
659 // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
660 ASSERT(container->renderer());
662 RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
663 appendNode(placeholder, container);
664 return placeholder.release();
667 PassRefPtr<Node> CompositeEditCommand::insertBlockPlaceholder(const Position& pos)
672 // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
673 ASSERT(pos.deprecatedNode()->renderer());
675 RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
676 insertNodeAt(placeholder, pos);
677 return placeholder.release();
680 PassRefPtr<Node> CompositeEditCommand::addBlockPlaceholderIfNeeded(Element* container)
687 RenderObject* renderer = container->renderer();
688 if (!renderer || !renderer->isBlockFlow())
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);
700 // Assumes that the position is at a placeholder and does the removal without much checking.
701 void CompositeEditCommand::removePlaceholderAt(const Position& p)
703 ASSERT(lineBreakExistsAtPosition(p));
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());
711 deleteTextFromNode(static_cast<Text*>(p.anchorNode()), p.offsetInContainerNode(), 1);
714 PassRefPtr<Node> CompositeEditCommand::insertNewDefaultParagraphElementAt(const Position& position)
716 RefPtr<Element> paragraphElement = createDefaultParagraphElement(document());
718 paragraphElement->appendChild(createBreakElement(document()), ec);
719 insertNodeAt(paragraphElement, position);
720 return paragraphElement.release();
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)
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;
740 Position upstreamStart = visibleParagraphStart.deepEquivalent().upstream();
741 Position upstreamEnd = visibleEnd.deepEquivalent().upstream();
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)
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.
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())));
768 } else if (isEndOfDocument(visibleEnd)) {
769 // At the end of the document. We can bail here as well.
774 RefPtr<Node> newBlock = insertNewDefaultParagraphElementAt(upstreamStart);
776 bool endWasBr = visibleParagraphEnd.deepEquivalent().deprecatedNode()->hasTagName(brTag);
778 moveParagraphs(visibleParagraphStart, visibleParagraphEnd, VisiblePosition(firstPositionInNode(newBlock.get())));
780 if (newBlock->lastChild() && newBlock->lastChild()->hasTagName(brTag) && !endWasBr)
781 removeNode(newBlock->lastChild());
783 return newBlock.release();
786 void CompositeEditCommand::pushAnchorElementDown(Node* anchorNode)
791 ASSERT(anchorNode->isLink());
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);
800 // Clone the paragraph between start and end under blockElement,
801 // preserving the hierarchy up to outerNode.
803 void CompositeEditCommand::cloneParagraphUnderNewElement(Position& start, Position& end, Node* outerNode, Element* blockElement)
805 // First we clone the outerNode
807 RefPtr<Node> topNode = outerNode->cloneNode(isTableElement(outerNode));
808 appendNode(topNode, blockElement);
809 RefPtr<Node> lastNode = topNode;
811 if (start.deprecatedNode() != outerNode && lastNode->isElementNode()) {
812 Vector<RefPtr<Node> > ancestors;
814 // Insert each node from innerNode to outerNode (excluded) in a list.
815 for (Node* n = start.deprecatedNode(); n && n != outerNode; n = n->parentNode())
818 // Clone every node between start.deprecatedNode() and outerBlock.
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();
828 // Handle the case of paragraphs with more than one node,
829 // cloning all the siblings until end.deprecatedNode() is reached.
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();
840 for (Node* n = start.deprecatedNode()->traverseNextSibling(outerNode); n; n = n->traverseNextSibling(outerNode)) {
841 if (n->parentNode() != start.deprecatedNode()->parentNode())
842 lastNode = topNode->lastChild();
844 RefPtr<Node> clonedNode = n->cloneNode(true);
845 insertNodeAfter(clonedNode, lastNode);
846 lastNode = clonedNode.release();
847 if (n == end.deprecatedNode() || end.deprecatedNode()->isDescendantOf(n))
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).
860 void CompositeEditCommand::cleanupAfterDeletion(VisiblePosition destination)
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);
883 deleteTextFromNode(textNode, position.deprecatedEditingOffset(), 1);
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.
894 void CompositeEditCommand::moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode)
897 ASSERT(blockElement);
899 VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
900 VisiblePosition afterParagraph(endOfParagraphToMove.next());
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();
907 cloneParagraphUnderNewElement(start, end, outerNode, blockElement);
909 setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
910 deleteSelection(false, false, false, false);
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.
916 cleanupAfterDeletion();
918 // Add a br if pruning an empty block level element caused a collapse. For example:
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());
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());
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)
939 ASSERT(isStartOfParagraph(startOfParagraphToMove));
940 ASSERT(isEndOfParagraph(endOfParagraphToMove));
941 moveParagraphs(startOfParagraphToMove, endOfParagraphToMove, destination, preserveSelection, preserveStyle);
944 void CompositeEditCommand::moveParagraphs(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
946 if (startOfParagraphToMove == destination)
951 int destinationIndex = -1;
952 bool originalIsDirectional = endingSelection().isDirectional();
953 if (preserveSelection && !endingSelection().isNone()) {
954 VisiblePosition visibleStart = endingSelection().visibleStart();
955 VisiblePosition visibleEnd = endingSelection().visibleEnd();
957 bool startAfterParagraph = comparePositions(visibleStart, endOfParagraphToMove) > 0;
958 bool endBeforeParagraph = comparePositions(visibleEnd, startOfParagraphToMove) < 0;
960 if (!startAfterParagraph && !endBeforeParagraph) {
961 bool startInParagraph = comparePositions(visibleStart, startOfParagraphToMove) >= 0;
962 bool endInParagraph = comparePositions(visibleEnd, endOfParagraphToMove) <= 0;
965 if (startInParagraph) {
966 RefPtr<Range> startRange = Range::create(document(), startOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent(), visibleStart.deepEquivalent().parentAnchoredEquivalent());
967 startIndex = TextIterator::rangeLength(startRange.get(), true);
971 if (endInParagraph) {
972 RefPtr<Range> endRange = Range::create(document(), startOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent(), visibleEnd.deepEquivalent().parentAnchoredEquivalent());
973 endIndex = TextIterator::rangeLength(endRange.get(), true);
978 VisiblePosition beforeParagraph = startOfParagraphToMove.previous(CannotCrossEditingBoundary);
979 VisiblePosition afterParagraph(endOfParagraphToMove.next(CannotCrossEditingBoundary));
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();
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());
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), "");
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();
1009 // FIXME (5098931): We should add a new insert action "WebViewInsertActionMoved" and call shouldInsertFragment here.
1011 setEndingSelection(VisibleSelection(start, end, DOWNSTREAM));
1012 document()->frame()->editor()->clearMisspellingsAndBadGrammar(endingSelection());
1013 deleteSelection(false, false, false, false);
1015 ASSERT(destination.deepEquivalent().anchorNode()->inDocument());
1016 cleanupAfterDeletion(destination);
1017 ASSERT(destination.deepEquivalent().anchorNode()->inDocument());
1019 // Add a br if pruning an empty block level element caused a collapse. For example:
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.
1035 RefPtr<Range> startToDestinationRange(Range::create(document(), firstPositionInNode(document()->documentElement()), destination.deepEquivalent().parentAnchoredEquivalent()));
1036 destinationIndex = TextIterator::rangeLength(startToDestinationRange.get(), true);
1038 setEndingSelection(VisibleSelection(destination, originalIsDirectional));
1039 ASSERT(endingSelection().isCaretOrRange());
1040 ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::MovingParagraph;
1042 options |= ReplaceSelectionCommand::MatchStyle;
1043 applyCommandToComposite(ReplaceSelectionCommand::create(document(), fragment, options));
1045 document()->frame()->editor()->markMisspellingsAndBadGrammar(endingSelection());
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());
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);
1061 setEndingSelection(VisibleSelection(start->startPosition(), end->startPosition(), DOWNSTREAM, originalIsDirectional));
1065 // FIXME: Send an appropriate shouldDeleteRange call.
1066 bool CompositeEditCommand::breakOutOfEmptyListItem()
1068 Node* emptyListItem = enclosingEmptyListItem(endingSelection().visibleStart());
1072 RefPtr<EditingStyle> style = EditingStyle::create(endingSelection().start());
1073 style->mergeTypingStyle(document());
1075 ContainerNode* listNode = emptyListItem->parentNode();
1076 // FIXME: Can't we do something better when the immediate parent wasn't a list node?
1078 || (!listNode->hasTagName(ulTag) && !listNode->hasTagName(olTag))
1079 || !listNode->rendererIsEditable()
1080 || listNode == emptyListItem->rootEditableElement())
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());
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());
1100 newBlock = createDefaultParagraphElement(document());
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);
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);
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);
1119 appendBlockPlaceholder(newBlock);
1120 setEndingSelection(VisibleSelection(firstPositionInNode(newBlock.get()), DOWNSTREAM, endingSelection().isDirectional()));
1122 style->prepareToApplyAt(endingSelection().start());
1123 if (!style->isEmpty())
1124 applyStyle(style.get());
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()
1133 if (!endingSelection().isCaret())
1136 VisiblePosition caret(endingSelection().visibleStart());
1137 Node* highestBlockquote = highestEnclosingNodeOfType(caret.deepEquivalent(), &isMailBlockquote);
1138 if (!highestBlockquote)
1141 if (!isStartOfParagraph(caret) || !isEndOfParagraph(caret))
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))
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
1156 if (!isStartOfParagraph(atBR))
1157 insertNodeBefore(createBreakElement(document()), br);
1158 setEndingSelection(VisibleSelection(atBR, endingSelection().isDirectional()));
1160 // If this is an empty paragraph there must be a line break here.
1161 if (!lineBreakExistsAtVisiblePosition(caret))
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()));
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);
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)
1191 if (original.isNull())
1194 VisiblePosition visiblePos(original);
1195 Node* enclosingAnchor = enclosingAnchorElement(original);
1196 Position result = original;
1198 if (!enclosingAnchor)
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)
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))
1222 result = positionInParentAfterNode(enclosingAnchor);
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);
1233 if (!enclosingAnchor)
1236 result = positionInParentBeforeNode(enclosingAnchor);
1240 if (result.isNull() || !editableRootForPosition(result))
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)
1252 ASSERT(start != end);
1255 if (shouldSplitAncestor && end->parentNode())
1256 end = end->parentNode();
1258 RefPtr<Node> endNode = end;
1259 for (node = start; node && node->parentNode() != endNode; node = node->parentNode()) {
1260 if (!node->parentNode()->isElementNode())
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);
1269 return node.release();
1272 PassRefPtr<Element> createBlockPlaceholderElement(Document* document)
1274 RefPtr<Element> breakNode = document->createElement(brTag, false);
1275 return breakNode.release();
1278 } // namespace WebCore