• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "CompositeEditCommand.h"
28 
29 #include "AppendNodeCommand.h"
30 #include "ApplyStyleCommand.h"
31 #include "CSSComputedStyleDeclaration.h"
32 #include "CSSMutableStyleDeclaration.h"
33 #include "CharacterNames.h"
34 #include "DeleteFromTextNodeCommand.h"
35 #include "DeleteSelectionCommand.h"
36 #include "Document.h"
37 #include "DocumentFragment.h"
38 #include "EditorInsertAction.h"
39 #include "Element.h"
40 #include "HTMLNames.h"
41 #include "InlineTextBox.h"
42 #include "InsertIntoTextNodeCommand.h"
43 #include "InsertLineBreakCommand.h"
44 #include "InsertNodeBeforeCommand.h"
45 #include "InsertParagraphSeparatorCommand.h"
46 #include "InsertTextCommand.h"
47 #include "JoinTextNodesCommand.h"
48 #include "MergeIdenticalElementsCommand.h"
49 #include "Range.h"
50 #include "RemoveCSSPropertyCommand.h"
51 #include "RemoveNodeCommand.h"
52 #include "RemoveNodePreservingChildrenCommand.h"
53 #include "ReplaceSelectionCommand.h"
54 #include "RenderBlock.h"
55 #include "RenderText.h"
56 #include "SetNodeAttributeCommand.h"
57 #include "SplitElementCommand.h"
58 #include "SplitTextNodeCommand.h"
59 #include "SplitTextNodeContainingElementCommand.h"
60 #include "Text.h"
61 #include "TextIterator.h"
62 #include "WrapContentsInDummySpanCommand.h"
63 #include "htmlediting.h"
64 #include "markup.h"
65 #include "visible_units.h"
66 
67 using namespace std;
68 
69 namespace WebCore {
70 
71 using namespace HTMLNames;
72 
CompositeEditCommand(Document * document)73 CompositeEditCommand::CompositeEditCommand(Document *document)
74     : EditCommand(document)
75 {
76 }
77 
doUnapply()78 void CompositeEditCommand::doUnapply()
79 {
80     size_t size = m_commands.size();
81     for (size_t i = size; i != 0; --i)
82         m_commands[i - 1]->unapply();
83 }
84 
doReapply()85 void CompositeEditCommand::doReapply()
86 {
87     size_t size = m_commands.size();
88     for (size_t i = 0; i != size; ++i)
89         m_commands[i]->reapply();
90 }
91 
92 //
93 // sugary-sweet convenience functions to help create and apply edit commands in composite commands
94 //
applyCommandToComposite(PassRefPtr<EditCommand> cmd)95 void CompositeEditCommand::applyCommandToComposite(PassRefPtr<EditCommand> cmd)
96 {
97     cmd->setParent(this);
98     cmd->apply();
99     m_commands.append(cmd);
100 }
101 
applyStyle(CSSStyleDeclaration * style,EditAction editingAction)102 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, EditAction editingAction)
103 {
104     applyCommandToComposite(ApplyStyleCommand::create(document(), style, editingAction));
105 }
106 
applyStyle(CSSStyleDeclaration * style,const Position & start,const Position & end,EditAction editingAction)107 void CompositeEditCommand::applyStyle(CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction editingAction)
108 {
109     applyCommandToComposite(ApplyStyleCommand::create(document(), style, start, end, editingAction));
110 }
111 
applyStyledElement(PassRefPtr<Element> element)112 void CompositeEditCommand::applyStyledElement(PassRefPtr<Element> element)
113 {
114     applyCommandToComposite(ApplyStyleCommand::create(element, false));
115 }
116 
removeStyledElement(PassRefPtr<Element> element)117 void CompositeEditCommand::removeStyledElement(PassRefPtr<Element> element)
118 {
119     applyCommandToComposite(ApplyStyleCommand::create(element, true));
120 }
121 
insertParagraphSeparator(bool useDefaultParagraphElement)122 void CompositeEditCommand::insertParagraphSeparator(bool useDefaultParagraphElement)
123 {
124     applyCommandToComposite(InsertParagraphSeparatorCommand::create(document(), useDefaultParagraphElement));
125 }
126 
insertLineBreak()127 void CompositeEditCommand::insertLineBreak()
128 {
129     applyCommandToComposite(InsertLineBreakCommand::create(document()));
130 }
131 
insertNodeBefore(PassRefPtr<Node> insertChild,PassRefPtr<Node> refChild)132 void CompositeEditCommand::insertNodeBefore(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
133 {
134     ASSERT(!refChild->hasTagName(bodyTag));
135     applyCommandToComposite(InsertNodeBeforeCommand::create(insertChild, refChild));
136 }
137 
insertNodeAfter(PassRefPtr<Node> insertChild,PassRefPtr<Node> refChild)138 void CompositeEditCommand::insertNodeAfter(PassRefPtr<Node> insertChild, PassRefPtr<Node> refChild)
139 {
140     ASSERT(insertChild);
141     ASSERT(refChild);
142     ASSERT(!refChild->hasTagName(bodyTag));
143     Element* parent = refChild->parentElement();
144     ASSERT(parent);
145     if (parent->lastChild() == refChild)
146         appendNode(insertChild, parent);
147     else {
148         ASSERT(refChild->nextSibling());
149         insertNodeBefore(insertChild, refChild->nextSibling());
150     }
151 }
152 
insertNodeAt(PassRefPtr<Node> insertChild,const Position & editingPosition)153 void CompositeEditCommand::insertNodeAt(PassRefPtr<Node> insertChild, const Position& editingPosition)
154 {
155     ASSERT(isEditablePosition(editingPosition));
156     // For editing positions like [table, 0], insert before the table,
157     // likewise for replaced elements, brs, etc.
158     Position p = rangeCompliantEquivalent(editingPosition);
159     Node* refChild = p.node();
160     int offset = p.offset();
161 
162     if (canHaveChildrenForEditing(refChild)) {
163         Node* child = refChild->firstChild();
164         for (int i = 0; child && i < offset; i++)
165             child = child->nextSibling();
166         if (child)
167             insertNodeBefore(insertChild, child);
168         else
169             appendNode(insertChild, static_cast<Element*>(refChild));
170     } else if (caretMinOffset(refChild) >= offset)
171         insertNodeBefore(insertChild, refChild);
172     else if (refChild->isTextNode() && caretMaxOffset(refChild) > offset) {
173         splitTextNode(static_cast<Text *>(refChild), offset);
174         insertNodeBefore(insertChild, refChild);
175     } else
176         insertNodeAfter(insertChild, refChild);
177 }
178 
appendNode(PassRefPtr<Node> node,PassRefPtr<Element> parent)179 void CompositeEditCommand::appendNode(PassRefPtr<Node> node, PassRefPtr<Element> parent)
180 {
181     ASSERT(canHaveChildrenForEditing(parent.get()));
182     applyCommandToComposite(AppendNodeCommand::create(parent, node));
183 }
184 
removeChildrenInRange(PassRefPtr<Node> node,unsigned from,unsigned to)185 void CompositeEditCommand::removeChildrenInRange(PassRefPtr<Node> node, unsigned from, unsigned to)
186 {
187     Vector<RefPtr<Node> > children;
188     Node* child = node->childNode(from);
189     for (unsigned i = from; child && i < to; i++, child = child->nextSibling())
190         children.append(child);
191 
192     size_t size = children.size();
193     for (size_t i = 0; i < size; ++i)
194         removeNode(children[i].release());
195 }
196 
removeNode(PassRefPtr<Node> node)197 void CompositeEditCommand::removeNode(PassRefPtr<Node> node)
198 {
199     applyCommandToComposite(RemoveNodeCommand::create(node));
200 }
201 
removeNodePreservingChildren(PassRefPtr<Node> node)202 void CompositeEditCommand::removeNodePreservingChildren(PassRefPtr<Node> node)
203 {
204     applyCommandToComposite(RemoveNodePreservingChildrenCommand::create(node));
205 }
206 
removeNodeAndPruneAncestors(PassRefPtr<Node> node)207 void CompositeEditCommand::removeNodeAndPruneAncestors(PassRefPtr<Node> node)
208 {
209     RefPtr<Node> parent = node->parentNode();
210     removeNode(node);
211     prune(parent.release());
212 }
213 
hasARenderedDescendant(Node * node)214 static bool hasARenderedDescendant(Node* node)
215 {
216     Node* n = node->firstChild();
217     while (n) {
218         if (n->renderer())
219             return true;
220         n = n->traverseNextNode(node);
221     }
222     return false;
223 }
224 
prune(PassRefPtr<Node> node)225 void CompositeEditCommand::prune(PassRefPtr<Node> node)
226 {
227     while (node) {
228         // If you change this rule you may have to add an updateLayout() here.
229         RenderObject* renderer = node->renderer();
230         if (renderer && (!renderer->canHaveChildren() || hasARenderedDescendant(node.get()) || node->rootEditableElement() == node))
231             return;
232 
233         RefPtr<Node> next = node->parentNode();
234         removeNode(node);
235         node = next;
236     }
237 }
238 
splitTextNode(PassRefPtr<Text> node,unsigned offset)239 void CompositeEditCommand::splitTextNode(PassRefPtr<Text> node, unsigned offset)
240 {
241     applyCommandToComposite(SplitTextNodeCommand::create(node, offset));
242 }
243 
splitElement(PassRefPtr<Element> element,PassRefPtr<Node> atChild)244 void CompositeEditCommand::splitElement(PassRefPtr<Element> element, PassRefPtr<Node> atChild)
245 {
246     applyCommandToComposite(SplitElementCommand::create(element, atChild));
247 }
248 
mergeIdenticalElements(PassRefPtr<Element> prpFirst,PassRefPtr<Element> prpSecond)249 void CompositeEditCommand::mergeIdenticalElements(PassRefPtr<Element> prpFirst, PassRefPtr<Element> prpSecond)
250 {
251     RefPtr<Element> first = prpFirst;
252     RefPtr<Element> second = prpSecond;
253     ASSERT(!first->isDescendantOf(second.get()) && second != first);
254     if (first->nextSibling() != second) {
255         removeNode(second);
256         insertNodeAfter(second, first);
257     }
258     applyCommandToComposite(MergeIdenticalElementsCommand::create(first, second));
259 }
260 
wrapContentsInDummySpan(PassRefPtr<Element> element)261 void CompositeEditCommand::wrapContentsInDummySpan(PassRefPtr<Element> element)
262 {
263     applyCommandToComposite(WrapContentsInDummySpanCommand::create(element));
264 }
265 
splitTextNodeContainingElement(PassRefPtr<Text> text,unsigned offset)266 void CompositeEditCommand::splitTextNodeContainingElement(PassRefPtr<Text> text, unsigned offset)
267 {
268     applyCommandToComposite(SplitTextNodeContainingElementCommand::create(text, offset));
269 }
270 
joinTextNodes(PassRefPtr<Text> text1,PassRefPtr<Text> text2)271 void CompositeEditCommand::joinTextNodes(PassRefPtr<Text> text1, PassRefPtr<Text> text2)
272 {
273     applyCommandToComposite(JoinTextNodesCommand::create(text1, text2));
274 }
275 
inputText(const String & text,bool selectInsertedText)276 void CompositeEditCommand::inputText(const String& text, bool selectInsertedText)
277 {
278     int offset = 0;
279     int length = text.length();
280     RefPtr<Range> startRange = Range::create(document(), Position(document()->documentElement(), 0), endingSelection().start());
281     int startIndex = TextIterator::rangeLength(startRange.get());
282     int newline;
283     do {
284         newline = text.find('\n', offset);
285         if (newline != offset) {
286             RefPtr<InsertTextCommand> command = InsertTextCommand::create(document());
287             applyCommandToComposite(command);
288             int substringLength = newline == -1 ? length - offset : newline - offset;
289             command->input(text.substring(offset, substringLength), false);
290         }
291         if (newline != -1)
292             insertLineBreak();
293 
294         offset = newline + 1;
295     } while (newline != -1 && offset != length);
296 
297     if (selectInsertedText) {
298         RefPtr<Range> selectedRange = TextIterator::rangeFromLocationAndLength(document()->documentElement(), startIndex, length);
299         setEndingSelection(Selection(selectedRange.get()));
300     }
301 }
302 
insertTextIntoNode(PassRefPtr<Text> node,unsigned offset,const String & text)303 void CompositeEditCommand::insertTextIntoNode(PassRefPtr<Text> node, unsigned offset, const String& text)
304 {
305     applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, text));
306 }
307 
deleteTextFromNode(PassRefPtr<Text> node,unsigned offset,unsigned count)308 void CompositeEditCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
309 {
310     applyCommandToComposite(DeleteFromTextNodeCommand::create(node, offset, count));
311 }
312 
replaceTextInNode(PassRefPtr<Text> node,unsigned offset,unsigned count,const String & replacementText)313 void CompositeEditCommand::replaceTextInNode(PassRefPtr<Text> node, unsigned offset, unsigned count, const String& replacementText)
314 {
315     applyCommandToComposite(DeleteFromTextNodeCommand::create(node.get(), offset, count));
316     applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, replacementText));
317 }
318 
positionOutsideTabSpan(const Position & pos)319 Position CompositeEditCommand::positionOutsideTabSpan(const Position& pos)
320 {
321     if (!isTabSpanTextNode(pos.node()))
322         return pos;
323 
324     Node* tabSpan = tabSpanNode(pos.node());
325 
326     if (pos.offset() <= caretMinOffset(pos.node()))
327         return positionBeforeNode(tabSpan);
328 
329     if (pos.offset() >= caretMaxOffset(pos.node()))
330         return positionAfterNode(tabSpan);
331 
332     splitTextNodeContainingElement(static_cast<Text *>(pos.node()), pos.offset());
333     return positionBeforeNode(tabSpan);
334 }
335 
insertNodeAtTabSpanPosition(PassRefPtr<Node> node,const Position & pos)336 void CompositeEditCommand::insertNodeAtTabSpanPosition(PassRefPtr<Node> node, const Position& pos)
337 {
338     // insert node before, after, or at split of tab span
339     insertNodeAt(node, positionOutsideTabSpan(pos));
340 }
341 
deleteSelection(bool smartDelete,bool mergeBlocksAfterDelete,bool replace,bool expandForSpecialElements)342 void CompositeEditCommand::deleteSelection(bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
343 {
344     if (endingSelection().isRange())
345         applyCommandToComposite(DeleteSelectionCommand::create(document(), smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
346 }
347 
deleteSelection(const Selection & selection,bool smartDelete,bool mergeBlocksAfterDelete,bool replace,bool expandForSpecialElements)348 void CompositeEditCommand::deleteSelection(const Selection &selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
349 {
350     if (selection.isRange())
351         applyCommandToComposite(DeleteSelectionCommand::create(selection, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements));
352 }
353 
removeCSSProperty(PassRefPtr<CSSMutableStyleDeclaration> style,CSSPropertyID property)354 void CompositeEditCommand::removeCSSProperty(PassRefPtr<CSSMutableStyleDeclaration> style, CSSPropertyID property)
355 {
356     applyCommandToComposite(RemoveCSSPropertyCommand::create(document(), style, property));
357 }
358 
removeNodeAttribute(PassRefPtr<Element> element,const QualifiedName & attribute)359 void CompositeEditCommand::removeNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute)
360 {
361     setNodeAttribute(element, attribute, AtomicString());
362 }
363 
setNodeAttribute(PassRefPtr<Element> element,const QualifiedName & attribute,const AtomicString & value)364 void CompositeEditCommand::setNodeAttribute(PassRefPtr<Element> element, const QualifiedName& attribute, const AtomicString& value)
365 {
366     applyCommandToComposite(SetNodeAttributeCommand::create(element, attribute, value));
367 }
368 
isWhitespace(UChar c)369 static inline bool isWhitespace(UChar c)
370 {
371     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
372 }
373 
374 // FIXME: Doesn't go into text nodes that contribute adjacent text (siblings, cousins, etc).
rebalanceWhitespaceAt(const Position & position)375 void CompositeEditCommand::rebalanceWhitespaceAt(const Position& position)
376 {
377     Node* node = position.node();
378     if (!node || !node->isTextNode())
379         return;
380     Text* textNode = static_cast<Text*>(node);
381 
382     if (textNode->length() == 0)
383         return;
384     RenderObject* renderer = textNode->renderer();
385     if (renderer && !renderer->style()->collapseWhiteSpace())
386         return;
387 
388     String text = textNode->data();
389     ASSERT(!text.isEmpty());
390 
391     int offset = position.offset();
392     // If neither text[offset] nor text[offset - 1] are some form of whitespace, do nothing.
393     if (!isWhitespace(text[offset])) {
394         offset--;
395         if (offset < 0 || !isWhitespace(text[offset]))
396             return;
397     }
398 
399     // Set upstream and downstream to define the extent of the whitespace surrounding text[offset].
400     int upstream = offset;
401     while (upstream > 0 && isWhitespace(text[upstream - 1]))
402         upstream--;
403 
404     int downstream = offset;
405     while ((unsigned)downstream + 1 < text.length() && isWhitespace(text[downstream + 1]))
406         downstream++;
407 
408     int length = downstream - upstream + 1;
409     ASSERT(length > 0);
410 
411     VisiblePosition visibleUpstreamPos(Position(position.node(), upstream));
412     VisiblePosition visibleDownstreamPos(Position(position.node(), downstream + 1));
413 
414     String string = text.substring(upstream, length);
415     String rebalancedString = stringWithRebalancedWhitespace(string,
416     // 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
417     // this function doesn't get all surrounding whitespace, just the whitespace in the current text node.
418                                                              isStartOfParagraph(visibleUpstreamPos) || upstream == 0,
419                                                              isEndOfParagraph(visibleDownstreamPos) || (unsigned)downstream == text.length() - 1);
420 
421     if (string != rebalancedString)
422         replaceTextInNode(textNode, upstream, length, rebalancedString);
423 }
424 
prepareWhitespaceAtPositionForSplit(Position & position)425 void CompositeEditCommand::prepareWhitespaceAtPositionForSplit(Position& position)
426 {
427     Node* node = position.node();
428     if (!node || !node->isTextNode())
429         return;
430     Text* textNode = static_cast<Text*>(node);
431 
432     if (textNode->length() == 0)
433         return;
434     RenderObject* renderer = textNode->renderer();
435     if (renderer && !renderer->style()->collapseWhiteSpace())
436         return;
437 
438     // Delete collapsed whitespace so that inserting nbsps doesn't uncollapse it.
439     Position upstreamPos = position.upstream();
440     deleteInsignificantText(position.upstream(), position.downstream());
441     position = upstreamPos.downstream();
442 
443     VisiblePosition visiblePos(position);
444     VisiblePosition previousVisiblePos(visiblePos.previous());
445     Position previous(previousVisiblePos.deepEquivalent());
446 
447     if (isCollapsibleWhitespace(previousVisiblePos.characterAfter()) && previous.node()->isTextNode() && !previous.node()->hasTagName(brTag))
448         replaceTextInNode(static_cast<Text*>(previous.node()), previous.offset(), 1, nonBreakingSpaceString());
449     if (isCollapsibleWhitespace(visiblePos.characterAfter()) && position.node()->isTextNode() && !position.node()->hasTagName(brTag))
450         replaceTextInNode(static_cast<Text*>(position.node()), position.offset(), 1, nonBreakingSpaceString());
451 }
452 
rebalanceWhitespace()453 void CompositeEditCommand::rebalanceWhitespace()
454 {
455     Selection selection = endingSelection();
456     if (selection.isNone())
457         return;
458 
459     rebalanceWhitespaceAt(selection.start());
460     if (selection.isRange())
461         rebalanceWhitespaceAt(selection.end());
462 }
463 
deleteInsignificantText(PassRefPtr<Text> textNode,unsigned start,unsigned end)464 void CompositeEditCommand::deleteInsignificantText(PassRefPtr<Text> textNode, unsigned start, unsigned end)
465 {
466     if (!textNode || start >= end)
467         return;
468 
469     RenderText* textRenderer = toRenderText(textNode->renderer());
470     if (!textRenderer)
471         return;
472 
473     InlineTextBox* box = textRenderer->firstTextBox();
474     if (!box) {
475         // whole text node is empty
476         removeNode(textNode);
477         return;
478     }
479 
480     unsigned length = textNode->length();
481     if (start >= length || end > length)
482         return;
483 
484     unsigned removed = 0;
485     InlineTextBox* prevBox = 0;
486     String str;
487 
488     // This loop structure works to process all gaps preceding a box,
489     // and also will look at the gap after the last box.
490     while (prevBox || box) {
491         unsigned gapStart = prevBox ? prevBox->start() + prevBox->len() : 0;
492         if (end < gapStart)
493             // No more chance for any intersections
494             break;
495 
496         unsigned gapEnd = box ? box->start() : length;
497         bool indicesIntersect = start <= gapEnd && end >= gapStart;
498         int gapLen = gapEnd - gapStart;
499         if (indicesIntersect && gapLen > 0) {
500             gapStart = max(gapStart, start);
501             gapEnd = min(gapEnd, end);
502             if (str.isNull())
503                 str = textNode->string()->substring(start, end - start);
504             // remove text in the gap
505             str.remove(gapStart - start - removed, gapLen);
506             removed += gapLen;
507         }
508 
509         prevBox = box;
510         if (box)
511             box = box->nextTextBox();
512     }
513 
514     if (!str.isNull()) {
515         // Replace the text between start and end with our pruned version.
516         if (!str.isEmpty())
517             replaceTextInNode(textNode, start, end - start, str);
518         else {
519             // Assert that we are not going to delete all of the text in the node.
520             // If we were, that should have been done above with the call to
521             // removeNode and return.
522             ASSERT(start > 0 || end - start < textNode->length());
523             deleteTextFromNode(textNode, start, end - start);
524         }
525     }
526 }
527 
deleteInsignificantText(const Position & start,const Position & end)528 void CompositeEditCommand::deleteInsignificantText(const Position& start, const Position& end)
529 {
530     if (start.isNull() || end.isNull())
531         return;
532 
533     if (Range::compareBoundaryPoints(start, end) >= 0)
534         return;
535 
536     Node* next;
537     for (Node* node = start.node(); node; node = next) {
538         next = node->traverseNextNode();
539         if (node->isTextNode()) {
540             Text* textNode = static_cast<Text*>(node);
541             int startOffset = node == start.node() ? start.offset() : 0;
542             int endOffset = node == end.node() ? end.offset() : textNode->length();
543             deleteInsignificantText(textNode, startOffset, endOffset);
544         }
545         if (node == end.node())
546             break;
547     }
548 }
549 
deleteInsignificantTextDownstream(const Position & pos)550 void CompositeEditCommand::deleteInsignificantTextDownstream(const Position& pos)
551 {
552     Position end = VisiblePosition(pos, VP_DEFAULT_AFFINITY).next().deepEquivalent().downstream();
553     deleteInsignificantText(pos, end);
554 }
555 
appendBlockPlaceholder(PassRefPtr<Element> container)556 PassRefPtr<Node> CompositeEditCommand::appendBlockPlaceholder(PassRefPtr<Element> container)
557 {
558     if (!container)
559         return 0;
560 
561     // Should assert isBlockFlow || isInlineFlow when deletion improves. See 4244964.
562     ASSERT(container->renderer());
563 
564     RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
565     appendNode(placeholder, container);
566     return placeholder.release();
567 }
568 
insertBlockPlaceholder(const Position & pos)569 PassRefPtr<Node> CompositeEditCommand::insertBlockPlaceholder(const Position& pos)
570 {
571     if (pos.isNull())
572         return 0;
573 
574     // Should assert isBlockFlow || isInlineFlow when deletion improves.  See 4244964.
575     ASSERT(pos.node()->renderer());
576 
577     RefPtr<Node> placeholder = createBlockPlaceholderElement(document());
578     insertNodeAt(placeholder, pos);
579     return placeholder.release();
580 }
581 
addBlockPlaceholderIfNeeded(Element * container)582 PassRefPtr<Node> CompositeEditCommand::addBlockPlaceholderIfNeeded(Element* container)
583 {
584     if (!container)
585         return 0;
586 
587     updateLayout();
588 
589     RenderObject* renderer = container->renderer();
590     if (!renderer || !renderer->isBlockFlow())
591         return 0;
592 
593     // append the placeholder to make sure it follows
594     // any unrendered blocks
595     RenderBlock* block = static_cast<RenderBlock*>(renderer);
596     if (block->height() == 0 || (block->isListItem() && block->isEmpty()))
597         return appendBlockPlaceholder(container);
598 
599     return 0;
600 }
601 
602 // Removes '\n's and brs that will collapse when content is inserted just before them.
603 // FIXME: We shouldn't really have to remove placeholders, but removing them is a workaround for 9661.
removePlaceholderAt(const VisiblePosition & visiblePosition)604 void CompositeEditCommand::removePlaceholderAt(const VisiblePosition& visiblePosition)
605 {
606     if (visiblePosition.isNull())
607         return;
608 
609     Position p = visiblePosition.deepEquivalent().downstream();
610     // If a br or '\n' is at the end of a block and not at the start of a paragraph,
611     // then it is superfluous, so adding content before a br or '\n' that is at
612     // the start of a paragraph will render it superfluous.
613     // FIXME: This doesn't remove placeholders at the end of anonymous blocks.
614     if (isEndOfBlock(visiblePosition) && isStartOfParagraph(visiblePosition)) {
615         if (p.node()->hasTagName(brTag) && p.offset() == 0)
616             removeNode(p.node());
617         else if (lineBreakExistsAtPosition(visiblePosition))
618             deleteTextFromNode(static_cast<Text*>(p.node()), p.offset(), 1);
619     }
620 }
621 
insertNewDefaultParagraphElementAt(const Position & position)622 PassRefPtr<Node> CompositeEditCommand::insertNewDefaultParagraphElementAt(const Position& position)
623 {
624     RefPtr<Element> paragraphElement = createDefaultParagraphElement(document());
625     ExceptionCode ec;
626     paragraphElement->appendChild(createBreakElement(document()), ec);
627     insertNodeAt(paragraphElement, position);
628     return paragraphElement.release();
629 }
630 
631 // If the paragraph is not entirely within it's own block, create one and move the paragraph into
632 // it, and return that block.  Otherwise return 0.
moveParagraphContentsToNewBlockIfNecessary(const Position & pos)633 PassRefPtr<Node> CompositeEditCommand::moveParagraphContentsToNewBlockIfNecessary(const Position& pos)
634 {
635     if (pos.isNull())
636         return 0;
637 
638     updateLayout();
639 
640     // It's strange that this function is responsible for verifying that pos has not been invalidated
641     // by an earlier call to this function.  The caller, applyBlockStyle, should do this.
642     VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
643     VisiblePosition visibleParagraphStart(startOfParagraph(visiblePos));
644     VisiblePosition visibleParagraphEnd = endOfParagraph(visiblePos);
645     VisiblePosition next = visibleParagraphEnd.next();
646     VisiblePosition visibleEnd = next.isNotNull() ? next : visibleParagraphEnd;
647 
648     Position upstreamStart = visibleParagraphStart.deepEquivalent().upstream();
649     Position upstreamEnd = visibleEnd.deepEquivalent().upstream();
650 
651     // If there are no VisiblePositions in the same block as pos then
652     // upstreamStart will be outside the paragraph
653     if (Range::compareBoundaryPoints(pos, upstreamStart) < 0)
654         return 0;
655 
656     // Perform some checks to see if we need to perform work in this function.
657     if (isBlock(upstreamStart.node())) {
658         // If the block is the body element, always move content to a new block, so that
659         // we avoid adding styles to the body element, since Mail's Make Plain Text feature
660         // can't handle those.
661         if (upstreamStart.node()->hasTagName(bodyTag)) {
662             // If the block is the body element and there is nothing insde of it, create a new
663             // block but don't try and move content into it, since there's nothing to move.
664             if (upstreamStart == upstreamEnd)
665                 return insertNewDefaultParagraphElementAt(upstreamStart);
666         } else if (isBlock(upstreamEnd.node())) {
667             if (!upstreamEnd.node()->isDescendantOf(upstreamStart.node())) {
668                 // If the paragraph end is a descendant of paragraph start, then we need to run
669                 // the rest of this function. If not, we can bail here.
670                 return 0;
671             }
672         }
673         else if (enclosingBlock(upstreamEnd.node()) != upstreamStart.node()) {
674             // The visibleEnd.  It must be an ancestor of the paragraph start.
675             // We can bail as we have a full block to work with.
676             ASSERT(upstreamStart.node()->isDescendantOf(enclosingBlock(upstreamEnd.node())));
677             return 0;
678         }
679         else if (isEndOfDocument(visibleEnd)) {
680             // At the end of the document. We can bail here as well.
681             return 0;
682         }
683     }
684 
685     RefPtr<Node> newBlock = insertNewDefaultParagraphElementAt(upstreamStart);
686 
687     moveParagraphs(visibleParagraphStart, visibleParagraphEnd, VisiblePosition(Position(newBlock.get(), 0)));
688 
689     return newBlock.release();
690 }
691 
pushAnchorElementDown(Node * anchorNode)692 void CompositeEditCommand::pushAnchorElementDown(Node* anchorNode)
693 {
694     if (!anchorNode)
695         return;
696 
697     ASSERT(anchorNode->isLink());
698 
699     setEndingSelection(Selection::selectionFromContentsOfNode(anchorNode));
700     applyStyledElement(static_cast<Element*>(anchorNode));
701     // Clones of anchorNode have been pushed down, now remove it.
702     if (anchorNode->inDocument())
703         removeNodePreservingChildren(anchorNode);
704 }
705 
706 // We must push partially selected anchors down before creating or removing
707 // links from a selection to create fully selected chunks that can be removed.
708 // ApplyStyleCommand doesn't do this for us because styles can be nested.
709 // Anchors cannot be nested.
pushPartiallySelectedAnchorElementsDown()710 void CompositeEditCommand::pushPartiallySelectedAnchorElementsDown()
711 {
712     Selection originalSelection = endingSelection();
713     VisiblePosition visibleStart(originalSelection.start());
714     VisiblePosition visibleEnd(originalSelection.end());
715 
716     Node* startAnchor = enclosingAnchorElement(originalSelection.start());
717     VisiblePosition startOfStartAnchor(Position(startAnchor, 0));
718     if (startAnchor && startOfStartAnchor != visibleStart)
719         pushAnchorElementDown(startAnchor);
720 
721     Node* endAnchor = enclosingAnchorElement(originalSelection.end());
722     VisiblePosition endOfEndAnchor(Position(endAnchor, 0));
723     if (endAnchor && endOfEndAnchor != visibleEnd)
724         pushAnchorElementDown(endAnchor);
725 
726     ASSERT(originalSelection.start().node()->inDocument() && originalSelection.end().node()->inDocument());
727     setEndingSelection(originalSelection);
728 }
729 
730 // This moves a paragraph preserving its style.
moveParagraph(const VisiblePosition & startOfParagraphToMove,const VisiblePosition & endOfParagraphToMove,const VisiblePosition & destination,bool preserveSelection,bool preserveStyle)731 void CompositeEditCommand::moveParagraph(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
732 {
733     ASSERT(isStartOfParagraph(startOfParagraphToMove));
734     ASSERT(isEndOfParagraph(endOfParagraphToMove));
735     moveParagraphs(startOfParagraphToMove, endOfParagraphToMove, destination, preserveSelection, preserveStyle);
736 }
737 
moveParagraphs(const VisiblePosition & startOfParagraphToMove,const VisiblePosition & endOfParagraphToMove,const VisiblePosition & destination,bool preserveSelection,bool preserveStyle)738 void CompositeEditCommand::moveParagraphs(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, const VisiblePosition& destination, bool preserveSelection, bool preserveStyle)
739 {
740     if (startOfParagraphToMove == destination)
741         return;
742 
743     int startIndex = -1;
744     int endIndex = -1;
745     int destinationIndex = -1;
746     if (preserveSelection && !endingSelection().isNone()) {
747         VisiblePosition visibleStart = endingSelection().visibleStart();
748         VisiblePosition visibleEnd = endingSelection().visibleEnd();
749 
750         bool startAfterParagraph = Range::compareBoundaryPoints(visibleStart.deepEquivalent(), endOfParagraphToMove.deepEquivalent()) > 0;
751         bool endBeforeParagraph = Range::compareBoundaryPoints(visibleEnd.deepEquivalent(), startOfParagraphToMove.deepEquivalent()) < 0;
752 
753         if (!startAfterParagraph && !endBeforeParagraph) {
754             bool startInParagraph = Range::compareBoundaryPoints(visibleStart.deepEquivalent(), startOfParagraphToMove.deepEquivalent()) >= 0;
755             bool endInParagraph = Range::compareBoundaryPoints(visibleEnd.deepEquivalent(), endOfParagraphToMove.deepEquivalent()) <= 0;
756 
757             startIndex = 0;
758             if (startInParagraph) {
759                 RefPtr<Range> startRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleStart.deepEquivalent()));
760                 startIndex = TextIterator::rangeLength(startRange.get(), true);
761             }
762 
763             endIndex = 0;
764             if (endInParagraph) {
765                 RefPtr<Range> endRange = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(visibleEnd.deepEquivalent()));
766                 endIndex = TextIterator::rangeLength(endRange.get(), true);
767             }
768         }
769     }
770 
771     VisiblePosition beforeParagraph = startOfParagraphToMove.previous();
772     VisiblePosition afterParagraph(endOfParagraphToMove.next());
773 
774     // We upstream() the end and downstream() the start so that we don't include collapsed whitespace in the move.
775     // When we paste a fragment, spaces after the end and before the start are treated as though they were rendered.
776     Position start = startOfParagraphToMove.deepEquivalent().downstream();
777     Position end = endOfParagraphToMove.deepEquivalent().upstream();
778 
779     // start and end can't be used directly to create a Range; they are "editing positions"
780     Position startRangeCompliant = rangeCompliantEquivalent(start);
781     Position endRangeCompliant = rangeCompliantEquivalent(end);
782     RefPtr<Range> range = Range::create(document(), startRangeCompliant.node(), startRangeCompliant.offset(), endRangeCompliant.node(), endRangeCompliant.offset());
783 
784     // FIXME: This is an inefficient way to preserve style on nodes in the paragraph to move.  It
785     // shouldn't matter though, since moved paragraphs will usually be quite small.
786     RefPtr<DocumentFragment> fragment = startOfParagraphToMove != endOfParagraphToMove ? createFragmentFromMarkup(document(), createMarkup(range.get(), 0, DoNotAnnotateForInterchange, true), "") : 0;
787 
788     // A non-empty paragraph's style is moved when we copy and move it.  We don't move
789     // anything if we're given an empty paragraph, but an empty paragraph can have style
790     // too, <div><b><br></b></div> for example.  Save it so that we can preserve it later.
791     RefPtr<CSSMutableStyleDeclaration> styleInEmptyParagraph;
792     if (startOfParagraphToMove == endOfParagraphToMove && preserveStyle) {
793         styleInEmptyParagraph = styleAtPosition(startOfParagraphToMove.deepEquivalent());
794         // The moved paragraph should assume the block style of the destination.
795         styleInEmptyParagraph->removeBlockProperties();
796     }
797 
798     // FIXME (5098931): We should add a new insert action "WebViewInsertActionMoved" and call shouldInsertFragment here.
799 
800     setEndingSelection(Selection(start, end, DOWNSTREAM));
801     deleteSelection(false, false, false, false);
802 
803     ASSERT(destination.deepEquivalent().node()->inDocument());
804 
805     // There are bugs in deletion when it removes a fully selected table/list.
806     // It expands and removes the entire table/list, but will let content
807     // before and after the table/list collapse onto one line.
808 
809     // Deleting a paragraph will leave a placeholder.  Remove it (and prune
810     // empty or unrendered parents).
811     VisiblePosition caretAfterDelete = endingSelection().visibleStart();
812     if (isStartOfParagraph(caretAfterDelete) && isEndOfParagraph(caretAfterDelete)) {
813         // Note: We want the rightmost candidate.
814         Position position = caretAfterDelete.deepEquivalent().downstream();
815         Node* node = position.node();
816         // Normally deletion will leave a br as a placeholder.
817         if (node->hasTagName(brTag))
818             removeNodeAndPruneAncestors(node);
819         // If the selection to move was empty and in an empty block that
820         // doesn't require a placeholder to prop itself open (like a bordered
821         // div or an li), remove it during the move (the list removal code
822         // expects this behavior).
823         else if (isBlock(node))
824             removeNodeAndPruneAncestors(node);
825         else if (lineBreakExistsAtPosition(caretAfterDelete)) {
826             // There is a preserved '\n' at caretAfterDelete.
827             Text* textNode = static_cast<Text*>(node);
828             if (textNode->length() == 1)
829                 removeNodeAndPruneAncestors(node);
830             else
831                 deleteTextFromNode(textNode, position.offset(), 1);
832         }
833     }
834 
835     // Add a br if pruning an empty block level element caused a collapse.  For example:
836     // foo^
837     // <div>bar</div>
838     // baz
839     // Imagine moving 'bar' to ^.  'bar' will be deleted and its div pruned.  That would
840     // cause 'baz' to collapse onto the line with 'foobar' unless we insert a br.
841     // Must recononicalize these two VisiblePositions after the pruning above.
842     beforeParagraph = VisiblePosition(beforeParagraph.deepEquivalent());
843     afterParagraph = VisiblePosition(afterParagraph.deepEquivalent());
844     if (beforeParagraph.isNotNull() && (!isEndOfParagraph(beforeParagraph) || beforeParagraph == afterParagraph)) {
845         // FIXME: Trim text between beforeParagraph and afterParagraph if they aren't equal.
846         insertNodeAt(createBreakElement(document()), beforeParagraph.deepEquivalent());
847         // Need an updateLayout here in case inserting the br has split a text node.
848         updateLayout();
849     }
850 
851     RefPtr<Range> startToDestinationRange(Range::create(document(), Position(document(), 0), rangeCompliantEquivalent(destination.deepEquivalent())));
852     destinationIndex = TextIterator::rangeLength(startToDestinationRange.get(), true);
853 
854     setEndingSelection(destination);
855     applyCommandToComposite(ReplaceSelectionCommand::create(document(), fragment, true, false, !preserveStyle, false, true));
856     // Restore styles from an empty paragraph to the new empty paragraph.
857     if (styleInEmptyParagraph)
858         applyStyle(styleInEmptyParagraph.get());
859 
860     if (preserveSelection && startIndex != -1) {
861         // Fragment creation (using createMarkup) incorrectly uses regular
862         // spaces instead of nbsps for some spaces that were rendered (11475), which
863         // causes spaces to be collapsed during the move operation.  This results
864         // in a call to rangeFromLocationAndLength with a location past the end
865         // of the document (which will return null).
866         RefPtr<Range> start = TextIterator::rangeFromLocationAndLength(document()->documentElement(), destinationIndex + startIndex, 0, true);
867         RefPtr<Range> end = TextIterator::rangeFromLocationAndLength(document()->documentElement(), destinationIndex + endIndex, 0, true);
868         if (start && end)
869             setEndingSelection(Selection(start->startPosition(), end->startPosition(), DOWNSTREAM));
870     }
871 }
872 
873 // FIXME: Send an appropriate shouldDeleteRange call.
breakOutOfEmptyListItem()874 bool CompositeEditCommand::breakOutOfEmptyListItem()
875 {
876     Node* emptyListItem = enclosingEmptyListItem(endingSelection().visibleStart());
877     if (!emptyListItem)
878         return false;
879 
880     RefPtr<CSSMutableStyleDeclaration> style = styleAtPosition(endingSelection().start());
881 
882     Node* listNode = emptyListItem->parentNode();
883 
884     if (!listNode->isContentEditable())
885         return false;
886 
887     RefPtr<Element> newBlock = isListElement(listNode->parentNode()) ? createListItemElement(document()) : createDefaultParagraphElement(document());
888 
889     if (emptyListItem->renderer()->nextSibling()) {
890         if (emptyListItem->renderer()->previousSibling())
891             splitElement(static_cast<Element*>(listNode), emptyListItem);
892         insertNodeBefore(newBlock, listNode);
893         removeNode(emptyListItem);
894     } else {
895         insertNodeAfter(newBlock, listNode);
896         removeNode(emptyListItem->renderer()->previousSibling() ? emptyListItem : listNode);
897     }
898 
899     appendBlockPlaceholder(newBlock);
900     setEndingSelection(Selection(Position(newBlock.get(), 0), DOWNSTREAM));
901 
902     computedStyle(endingSelection().start().node())->diff(style.get());
903     if (style->length() > 0)
904         applyStyle(style.get());
905 
906     return true;
907 }
908 
909 // If the caret is in an empty quoted paragraph, and either there is nothing before that
910 // paragraph, or what is before is unquoted, and the user presses delete, unquote that paragraph.
breakOutOfEmptyMailBlockquotedParagraph()911 bool CompositeEditCommand::breakOutOfEmptyMailBlockquotedParagraph()
912 {
913     if (!endingSelection().isCaret())
914         return false;
915 
916     VisiblePosition caret(endingSelection().visibleStart());
917     Node* highestBlockquote = highestEnclosingNodeOfType(caret.deepEquivalent(), &isMailBlockquote);
918     if (!highestBlockquote)
919         return false;
920 
921     if (!isStartOfParagraph(caret) || !isEndOfParagraph(caret))
922         return false;
923 
924     VisiblePosition previous(caret.previous(true));
925     // Only move forward if there's nothing before the caret, or if there's unquoted content before it.
926     if (enclosingNodeOfType(previous.deepEquivalent(), &isMailBlockquote))
927         return false;
928 
929     RefPtr<Node> br = createBreakElement(document());
930     // We want to replace this quoted paragraph with an unquoted one, so insert a br
931     // to hold the caret before the highest blockquote.
932     insertNodeBefore(br, highestBlockquote);
933     VisiblePosition atBR(Position(br.get(), 0));
934     // If the br we inserted collapsed, for example foo<br><blockquote>...</blockquote>, insert
935     // a second one.
936     if (!isStartOfParagraph(atBR))
937         insertNodeBefore(createBreakElement(document()), br);
938     setEndingSelection(Selection(atBR));
939 
940     // If this is an empty paragraph there must be a line break here.
941     if (!lineBreakExistsAtPosition(caret))
942         return false;
943 
944     Position caretPos(caret.deepEquivalent());
945     // A line break is either a br or a preserved newline.
946     ASSERT(caretPos.node()->hasTagName(brTag) || caretPos.node()->isTextNode() && caretPos.node()->renderer()->style()->preserveNewline());
947 
948     if (caretPos.node()->hasTagName(brTag)) {
949         Position beforeBR(positionBeforeNode(caretPos.node()));
950         removeNode(caretPos.node());
951         prune(beforeBR.node());
952     } else {
953         ASSERT(caretPos.offset() == 0);
954         Text* textNode = static_cast<Text*>(caretPos.node());
955         Node* parentNode = textNode->parentNode();
956         // The preserved newline must be the first thing in the node, since otherwise the previous
957         // paragraph would be quoted, and we verified that it wasn't above.
958         deleteTextFromNode(textNode, 0, 1);
959         prune(parentNode);
960     }
961 
962     return true;
963 }
964 
965 // Operations use this function to avoid inserting content into an anchor when at the start or the end of
966 // that anchor, as in NSTextView.
967 // FIXME: This is only an approximation of NSTextViews insertion behavior, which varies depending on how
968 // the caret was made.
positionAvoidingSpecialElementBoundary(const Position & original)969 Position CompositeEditCommand::positionAvoidingSpecialElementBoundary(const Position& original)
970 {
971     if (original.isNull())
972         return original;
973 
974     VisiblePosition visiblePos(original);
975     Node* enclosingAnchor = enclosingAnchorElement(original);
976     Position result = original;
977     // Don't avoid block level anchors, because that would insert content into the wrong paragraph.
978     if (enclosingAnchor && !isBlock(enclosingAnchor)) {
979         VisiblePosition firstInAnchor(Position(enclosingAnchor, 0));
980         VisiblePosition lastInAnchor(Position(enclosingAnchor, maxDeepOffset(enclosingAnchor)));
981         // If visually just after the anchor, insert *inside* the anchor unless it's the last
982         // VisiblePosition in the document, to match NSTextView.
983         if (visiblePos == lastInAnchor) {
984             // Make sure anchors are pushed down before avoiding them so that we don't
985             // also avoid structural elements like lists and blocks (5142012).
986             if (original.node() != enclosingAnchor && original.node()->parentNode() != enclosingAnchor) {
987                 pushAnchorElementDown(enclosingAnchor);
988                 enclosingAnchor = enclosingAnchorElement(original);
989                 if (!enclosingAnchor)
990                     return original;
991             }
992             // Don't insert outside an anchor if doing so would skip over a line break.  It would
993             // probably be safe to move the line break so that we could still avoid the anchor here.
994             Position downstream(visiblePos.deepEquivalent().downstream());
995             if (lineBreakExistsAtPosition(visiblePos) && downstream.node()->isDescendantOf(enclosingAnchor))
996                 return original;
997 
998             result = positionAfterNode(enclosingAnchor);
999         }
1000         // If visually just before an anchor, insert *outside* the anchor unless it's the first
1001         // VisiblePosition in a paragraph, to match NSTextView.
1002         if (visiblePos == firstInAnchor) {
1003             // Make sure anchors are pushed down before avoiding them so that we don't
1004             // also avoid structural elements like lists and blocks (5142012).
1005             if (original.node() != enclosingAnchor && original.node()->parentNode() != enclosingAnchor) {
1006                 pushAnchorElementDown(enclosingAnchor);
1007                 enclosingAnchor = enclosingAnchorElement(original);
1008             }
1009             result = positionBeforeNode(enclosingAnchor);
1010         }
1011     }
1012 
1013     if (result.isNull() || !editableRootForPosition(result))
1014         result = original;
1015 
1016     return result;
1017 }
1018 
1019 // Splits the tree parent by parent until we reach the specified ancestor. We use VisiblePositions
1020 // to determine if the split is necessary. Returns the last split node.
splitTreeToNode(Node * start,Node * end,bool splitAncestor)1021 PassRefPtr<Node> CompositeEditCommand::splitTreeToNode(Node* start, Node* end, bool splitAncestor)
1022 {
1023     RefPtr<Node> node;
1024     for (node = start; node && node->parent() != end; node = node->parent()) {
1025         VisiblePosition positionInParent(Position(node->parent(), 0), DOWNSTREAM);
1026         VisiblePosition positionInNode(Position(node, 0), DOWNSTREAM);
1027         if (positionInParent != positionInNode)
1028             applyCommandToComposite(SplitElementCommand::create(static_cast<Element*>(node->parent()), node));
1029     }
1030     if (splitAncestor)
1031         return splitTreeToNode(end, end->parent());
1032     return node.release();
1033 }
1034 
createBlockPlaceholderElement(Document * document)1035 PassRefPtr<Element> createBlockPlaceholderElement(Document* document)
1036 {
1037     ExceptionCode ec = 0;
1038     RefPtr<Element> breakNode = document->createElementNS(xhtmlNamespaceURI, "br", ec);
1039     ASSERT(ec == 0);
1040     return breakNode.release();
1041 }
1042 
1043 } // namespace WebCore
1044