• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005, 2006, 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 "ReplaceSelectionCommand.h"
28 
29 #include "ApplyStyleCommand.h"
30 #include "BeforeTextInsertedEvent.h"
31 #include "BreakBlockquoteCommand.h"
32 #include "CSSComputedStyleDeclaration.h"
33 #include "CSSMutableStyleDeclaration.h"
34 #include "CSSProperty.h"
35 #include "CSSPropertyNames.h"
36 #include "CSSValueKeywords.h"
37 #include "Document.h"
38 #include "DocumentFragment.h"
39 #include "EditingText.h"
40 #include "Element.h"
41 #include "EventNames.h"
42 #include "Frame.h"
43 #include "HTMLElement.h"
44 #include "HTMLInputElement.h"
45 #include "HTMLInterchange.h"
46 #include "HTMLNames.h"
47 #include "SelectionController.h"
48 #include "SmartReplace.h"
49 #include "TextIterator.h"
50 #include "htmlediting.h"
51 #include "markup.h"
52 #include "visible_units.h"
53 #include <wtf/StdLibExtras.h>
54 
55 namespace WebCore {
56 
57 using namespace HTMLNames;
58 
59 enum EFragmentType { EmptyFragment, SingleTextNodeFragment, TreeFragment };
60 
61 // --- ReplacementFragment helper class
62 
63 class ReplacementFragment : public Noncopyable {
64 public:
65     ReplacementFragment(Document*, DocumentFragment*, bool matchStyle, const VisibleSelection&);
66 
67     Node* firstChild() const;
68     Node* lastChild() const;
69 
70     bool isEmpty() const;
71 
hasInterchangeNewlineAtStart() const72     bool hasInterchangeNewlineAtStart() const { return m_hasInterchangeNewlineAtStart; }
hasInterchangeNewlineAtEnd() const73     bool hasInterchangeNewlineAtEnd() const { return m_hasInterchangeNewlineAtEnd; }
74 
75     void removeNode(PassRefPtr<Node>);
76     void removeNodePreservingChildren(Node*);
77 
78 private:
79     PassRefPtr<Node> insertFragmentForTestRendering(Node* context);
80     void removeUnrenderedNodes(Node*);
81     void restoreTestRenderingNodesToFragment(Node*);
82     void removeInterchangeNodes(Node*);
83 
84     void insertNodeBefore(PassRefPtr<Node> node, Node* refNode);
85 
86     RefPtr<Document> m_document;
87     RefPtr<DocumentFragment> m_fragment;
88     bool m_matchStyle;
89     bool m_hasInterchangeNewlineAtStart;
90     bool m_hasInterchangeNewlineAtEnd;
91 };
92 
isInterchangeNewlineNode(const Node * node)93 static bool isInterchangeNewlineNode(const Node *node)
94 {
95     DEFINE_STATIC_LOCAL(String, interchangeNewlineClassString, (AppleInterchangeNewline));
96     return node && node->hasTagName(brTag) &&
97            static_cast<const Element *>(node)->getAttribute(classAttr) == interchangeNewlineClassString;
98 }
99 
isInterchangeConvertedSpaceSpan(const Node * node)100 static bool isInterchangeConvertedSpaceSpan(const Node *node)
101 {
102     DEFINE_STATIC_LOCAL(String, convertedSpaceSpanClassString, (AppleConvertedSpace));
103     return node->isHTMLElement() &&
104            static_cast<const HTMLElement *>(node)->getAttribute(classAttr) == convertedSpaceSpanClassString;
105 }
106 
ReplacementFragment(Document * document,DocumentFragment * fragment,bool matchStyle,const VisibleSelection & selection)107 ReplacementFragment::ReplacementFragment(Document* document, DocumentFragment* fragment, bool matchStyle, const VisibleSelection& selection)
108     : m_document(document),
109       m_fragment(fragment),
110       m_matchStyle(matchStyle),
111       m_hasInterchangeNewlineAtStart(false),
112       m_hasInterchangeNewlineAtEnd(false)
113 {
114     if (!m_document)
115         return;
116     if (!m_fragment)
117         return;
118     if (!m_fragment->firstChild())
119         return;
120 
121     Element* editableRoot = selection.rootEditableElement();
122     ASSERT(editableRoot);
123     if (!editableRoot)
124         return;
125 
126     Node* shadowAncestorNode = editableRoot->shadowAncestorNode();
127 
128     if (!editableRoot->getAttributeEventListener(eventNames().webkitBeforeTextInsertedEvent) &&
129         // FIXME: Remove these checks once textareas and textfields actually register an event handler.
130         !(shadowAncestorNode && shadowAncestorNode->renderer() && shadowAncestorNode->renderer()->isTextControl()) &&
131         editableRoot->isContentRichlyEditable()) {
132         removeInterchangeNodes(m_fragment.get());
133         return;
134     }
135 
136     Node* styleNode = selection.base().node();
137     RefPtr<Node> holder = insertFragmentForTestRendering(styleNode);
138 
139     RefPtr<Range> range = VisibleSelection::selectionFromContentsOfNode(holder.get()).toNormalizedRange();
140     String text = plainText(range.get());
141     // Give the root a chance to change the text.
142     RefPtr<BeforeTextInsertedEvent> evt = BeforeTextInsertedEvent::create(text);
143     ExceptionCode ec = 0;
144     editableRoot->dispatchEvent(evt, ec);
145     ASSERT(ec == 0);
146     if (text != evt->text() || !editableRoot->isContentRichlyEditable()) {
147         restoreTestRenderingNodesToFragment(holder.get());
148         removeNode(holder);
149 
150         m_fragment = createFragmentFromText(selection.toNormalizedRange().get(), evt->text());
151         if (!m_fragment->firstChild())
152             return;
153         holder = insertFragmentForTestRendering(styleNode);
154     }
155 
156     removeInterchangeNodes(holder.get());
157 
158     removeUnrenderedNodes(holder.get());
159     restoreTestRenderingNodesToFragment(holder.get());
160     removeNode(holder);
161 }
162 
isEmpty() const163 bool ReplacementFragment::isEmpty() const
164 {
165     return (!m_fragment || !m_fragment->firstChild()) && !m_hasInterchangeNewlineAtStart && !m_hasInterchangeNewlineAtEnd;
166 }
167 
firstChild() const168 Node *ReplacementFragment::firstChild() const
169 {
170     return m_fragment ? m_fragment->firstChild() : 0;
171 }
172 
lastChild() const173 Node *ReplacementFragment::lastChild() const
174 {
175     return m_fragment ? m_fragment->lastChild() : 0;
176 }
177 
removeNodePreservingChildren(Node * node)178 void ReplacementFragment::removeNodePreservingChildren(Node *node)
179 {
180     if (!node)
181         return;
182 
183     while (RefPtr<Node> n = node->firstChild()) {
184         removeNode(n);
185         insertNodeBefore(n.release(), node);
186     }
187     removeNode(node);
188 }
189 
removeNode(PassRefPtr<Node> node)190 void ReplacementFragment::removeNode(PassRefPtr<Node> node)
191 {
192     if (!node)
193         return;
194 
195     Node *parent = node->parentNode();
196     if (!parent)
197         return;
198 
199     ExceptionCode ec = 0;
200     parent->removeChild(node.get(), ec);
201     ASSERT(ec == 0);
202 }
203 
insertNodeBefore(PassRefPtr<Node> node,Node * refNode)204 void ReplacementFragment::insertNodeBefore(PassRefPtr<Node> node, Node* refNode)
205 {
206     if (!node || !refNode)
207         return;
208 
209     Node* parent = refNode->parentNode();
210     if (!parent)
211         return;
212 
213     ExceptionCode ec = 0;
214     parent->insertBefore(node, refNode, ec);
215     ASSERT(ec == 0);
216 }
217 
insertFragmentForTestRendering(Node * context)218 PassRefPtr<Node> ReplacementFragment::insertFragmentForTestRendering(Node* context)
219 {
220     Node* body = m_document->body();
221     if (!body)
222         return 0;
223 
224     RefPtr<StyledElement> holder = createDefaultParagraphElement(m_document.get());
225 
226     ExceptionCode ec = 0;
227 
228     // Copy the whitespace and user-select style from the context onto this element.
229     // FIXME: We should examine other style properties to see if they would be appropriate to consider during the test rendering.
230     Node* n = context;
231     while (n && !n->isElementNode())
232         n = n->parentNode();
233     if (n) {
234         RefPtr<CSSComputedStyleDeclaration> conFontStyle = computedStyle(n);
235         CSSStyleDeclaration* style = holder->style();
236         style->setProperty(CSSPropertyWhiteSpace, conFontStyle->getPropertyValue(CSSPropertyWhiteSpace), false, ec);
237         ASSERT(ec == 0);
238         style->setProperty(CSSPropertyWebkitUserSelect, conFontStyle->getPropertyValue(CSSPropertyWebkitUserSelect), false, ec);
239         ASSERT(ec == 0);
240     }
241 
242     holder->appendChild(m_fragment, ec);
243     ASSERT(ec == 0);
244 
245     body->appendChild(holder.get(), ec);
246     ASSERT(ec == 0);
247 
248     m_document->updateLayoutIgnorePendingStylesheets();
249 
250     return holder.release();
251 }
252 
restoreTestRenderingNodesToFragment(Node * holder)253 void ReplacementFragment::restoreTestRenderingNodesToFragment(Node *holder)
254 {
255     if (!holder)
256         return;
257 
258     ExceptionCode ec = 0;
259     while (RefPtr<Node> node = holder->firstChild()) {
260         holder->removeChild(node.get(), ec);
261         ASSERT(ec == 0);
262         m_fragment->appendChild(node.get(), ec);
263         ASSERT(ec == 0);
264     }
265 }
266 
removeUnrenderedNodes(Node * holder)267 void ReplacementFragment::removeUnrenderedNodes(Node* holder)
268 {
269     Vector<Node*> unrendered;
270 
271     for (Node* node = holder->firstChild(); node; node = node->traverseNextNode(holder))
272         if (!isNodeRendered(node) && !isTableStructureNode(node))
273             unrendered.append(node);
274 
275     size_t n = unrendered.size();
276     for (size_t i = 0; i < n; ++i)
277         removeNode(unrendered[i]);
278 }
279 
removeInterchangeNodes(Node * container)280 void ReplacementFragment::removeInterchangeNodes(Node* container)
281 {
282     // Interchange newlines at the "start" of the incoming fragment must be
283     // either the first node in the fragment or the first leaf in the fragment.
284     Node* node = container->firstChild();
285     while (node) {
286         if (isInterchangeNewlineNode(node)) {
287             m_hasInterchangeNewlineAtStart = true;
288             removeNode(node);
289             break;
290         }
291         node = node->firstChild();
292     }
293     if (!container->hasChildNodes())
294         return;
295     // Interchange newlines at the "end" of the incoming fragment must be
296     // either the last node in the fragment or the last leaf in the fragment.
297     node = container->lastChild();
298     while (node) {
299         if (isInterchangeNewlineNode(node)) {
300             m_hasInterchangeNewlineAtEnd = true;
301             removeNode(node);
302             break;
303         }
304         node = node->lastChild();
305     }
306 
307     node = container->firstChild();
308     while (node) {
309         Node *next = node->traverseNextNode();
310         if (isInterchangeConvertedSpaceSpan(node)) {
311             RefPtr<Node> n = 0;
312             while ((n = node->firstChild())) {
313                 removeNode(n);
314                 insertNodeBefore(n, node);
315             }
316             removeNode(node);
317             if (n)
318                 next = n->traverseNextNode();
319         }
320         node = next;
321     }
322 }
323 
ReplaceSelectionCommand(Document * document,PassRefPtr<DocumentFragment> fragment,bool selectReplacement,bool smartReplace,bool matchStyle,bool preventNesting,bool movingParagraph,EditAction editAction)324 ReplaceSelectionCommand::ReplaceSelectionCommand(Document* document, PassRefPtr<DocumentFragment> fragment,
325         bool selectReplacement, bool smartReplace, bool matchStyle, bool preventNesting, bool movingParagraph,
326         EditAction editAction)
327     : CompositeEditCommand(document),
328       m_selectReplacement(selectReplacement),
329       m_smartReplace(smartReplace),
330       m_matchStyle(matchStyle),
331       m_documentFragment(fragment),
332       m_preventNesting(preventNesting),
333       m_movingParagraph(movingParagraph),
334       m_editAction(editAction),
335       m_shouldMergeEnd(false)
336 {
337 }
338 
hasMatchingQuoteLevel(VisiblePosition endOfExistingContent,VisiblePosition endOfInsertedContent)339 static bool hasMatchingQuoteLevel(VisiblePosition endOfExistingContent, VisiblePosition endOfInsertedContent)
340 {
341     Position existing = endOfExistingContent.deepEquivalent();
342     Position inserted = endOfInsertedContent.deepEquivalent();
343     bool isInsideMailBlockquote = nearestMailBlockquote(inserted.node());
344     return isInsideMailBlockquote && (numEnclosingMailBlockquotes(existing) == numEnclosingMailBlockquotes(inserted));
345 }
346 
shouldMergeStart(bool selectionStartWasStartOfParagraph,bool fragmentHasInterchangeNewlineAtStart,bool selectionStartWasInsideMailBlockquote)347 bool ReplaceSelectionCommand::shouldMergeStart(bool selectionStartWasStartOfParagraph, bool fragmentHasInterchangeNewlineAtStart, bool selectionStartWasInsideMailBlockquote)
348 {
349     if (m_movingParagraph)
350         return false;
351 
352     VisiblePosition startOfInsertedContent(positionAtStartOfInsertedContent());
353     VisiblePosition prev = startOfInsertedContent.previous(true);
354     if (prev.isNull())
355         return false;
356 
357     // When we have matching quote levels, its ok to merge more frequently.
358     // For a successful merge, we still need to make sure that the inserted content starts with the beginning of a paragraph.
359     // And we should only merge here if the selection start was inside a mail blockquote.  This prevents against removing a
360     // blockquote from newly pasted quoted content that was pasted into an unquoted position.  If that unquoted position happens
361     // to be right after another blockquote, we don't want to merge and risk stripping a valid block (and newline) from the pasted content.
362     if (isStartOfParagraph(startOfInsertedContent) && selectionStartWasInsideMailBlockquote && hasMatchingQuoteLevel(prev, positionAtEndOfInsertedContent()))
363         return true;
364 
365     return !selectionStartWasStartOfParagraph &&
366            !fragmentHasInterchangeNewlineAtStart &&
367            isStartOfParagraph(startOfInsertedContent) &&
368            !startOfInsertedContent.deepEquivalent().node()->hasTagName(brTag) &&
369            shouldMerge(startOfInsertedContent, prev);
370 }
371 
shouldMergeEnd(bool selectionEndWasEndOfParagraph)372 bool ReplaceSelectionCommand::shouldMergeEnd(bool selectionEndWasEndOfParagraph)
373 {
374     VisiblePosition endOfInsertedContent(positionAtEndOfInsertedContent());
375     VisiblePosition next = endOfInsertedContent.next(true);
376     if (next.isNull())
377         return false;
378 
379     return !selectionEndWasEndOfParagraph &&
380            isEndOfParagraph(endOfInsertedContent) &&
381            !endOfInsertedContent.deepEquivalent().node()->hasTagName(brTag) &&
382            shouldMerge(endOfInsertedContent, next);
383 }
384 
isMailPasteAsQuotationNode(const Node * node)385 static bool isMailPasteAsQuotationNode(const Node* node)
386 {
387     return node && node->hasTagName(blockquoteTag) && node->isElementNode() && static_cast<const Element*>(node)->getAttribute(classAttr) == ApplePasteAsQuotation;
388 }
389 
390 // Wrap CompositeEditCommand::removeNodePreservingChildren() so we can update the nodes we track
removeNodePreservingChildren(Node * node)391 void ReplaceSelectionCommand::removeNodePreservingChildren(Node* node)
392 {
393     if (m_firstNodeInserted == node)
394         m_firstNodeInserted = node->traverseNextNode();
395     if (m_lastLeafInserted == node)
396         m_lastLeafInserted = node->lastChild() ? node->lastChild() : node->traverseNextSibling();
397     CompositeEditCommand::removeNodePreservingChildren(node);
398 }
399 
400 // Wrap CompositeEditCommand::removeNodeAndPruneAncestors() so we can update the nodes we track
removeNodeAndPruneAncestors(Node * node)401 void ReplaceSelectionCommand::removeNodeAndPruneAncestors(Node* node)
402 {
403     // prepare in case m_firstNodeInserted and/or m_lastLeafInserted get removed
404     // FIXME: shouldn't m_lastLeafInserted be adjusted using traversePreviousNode()?
405     Node* afterFirst = m_firstNodeInserted ? m_firstNodeInserted->traverseNextSibling() : 0;
406     Node* afterLast = m_lastLeafInserted ? m_lastLeafInserted->traverseNextSibling() : 0;
407 
408     CompositeEditCommand::removeNodeAndPruneAncestors(node);
409 
410     // adjust m_firstNodeInserted and m_lastLeafInserted since either or both may have been removed
411     if (m_lastLeafInserted && !m_lastLeafInserted->inDocument())
412         m_lastLeafInserted = afterLast;
413     if (m_firstNodeInserted && !m_firstNodeInserted->inDocument())
414         m_firstNodeInserted = m_lastLeafInserted && m_lastLeafInserted->inDocument() ? afterFirst : 0;
415 }
416 
isHeaderElement(Node * a)417 static bool isHeaderElement(Node* a)
418 {
419     if (!a)
420         return false;
421 
422     return a->hasTagName(h1Tag) ||
423            a->hasTagName(h2Tag) ||
424            a->hasTagName(h3Tag) ||
425            a->hasTagName(h4Tag) ||
426            a->hasTagName(h5Tag);
427 }
428 
haveSameTagName(Node * a,Node * b)429 static bool haveSameTagName(Node* a, Node* b)
430 {
431     return a && b && a->isElementNode() && b->isElementNode() && static_cast<Element*>(a)->tagName() == static_cast<Element*>(b)->tagName();
432 }
433 
shouldMerge(const VisiblePosition & source,const VisiblePosition & destination)434 bool ReplaceSelectionCommand::shouldMerge(const VisiblePosition& source, const VisiblePosition& destination)
435 {
436     if (source.isNull() || destination.isNull())
437         return false;
438 
439     Node* sourceNode = source.deepEquivalent().node();
440     Node* destinationNode = destination.deepEquivalent().node();
441     Node* sourceBlock = enclosingBlock(sourceNode);
442     Node* destinationBlock = enclosingBlock(destinationNode);
443     return !enclosingNodeOfType(source.deepEquivalent(), &isMailPasteAsQuotationNode) &&
444            sourceBlock && (!sourceBlock->hasTagName(blockquoteTag) || isMailBlockquote(sourceBlock))  &&
445            enclosingListChild(sourceBlock) == enclosingListChild(destinationNode) &&
446            enclosingTableCell(source.deepEquivalent()) == enclosingTableCell(destination.deepEquivalent()) &&
447            (!isHeaderElement(sourceBlock) || haveSameTagName(sourceBlock, destinationBlock)) &&
448            // Don't merge to or from a position before or after a block because it would
449            // be a no-op and cause infinite recursion.
450            !isBlock(sourceNode) && !isBlock(destinationNode);
451 }
452 
453 // Style rules that match just inserted elements could change their appearance, like
454 // a div inserted into a document with div { display:inline; }.
negateStyleRulesThatAffectAppearance()455 void ReplaceSelectionCommand::negateStyleRulesThatAffectAppearance()
456 {
457     for (RefPtr<Node> node = m_firstNodeInserted.get(); node; node = node->traverseNextNode()) {
458         // FIXME: <rdar://problem/5371536> Style rules that match pasted content can change it's appearance
459         if (isStyleSpan(node.get())) {
460             HTMLElement* e = static_cast<HTMLElement*>(node.get());
461             // There are other styles that style rules can give to style spans,
462             // but these are the two important ones because they'll prevent
463             // inserted content from appearing in the right paragraph.
464             // FIXME: Hyatt is concerned that selectively using display:inline will give inconsistent
465             // results. We already know one issue because td elements ignore their display property
466             // in quirks mode (which Mail.app is always in). We should look for an alternative.
467             if (isBlock(e))
468                 e->getInlineStyleDecl()->setProperty(CSSPropertyDisplay, CSSValueInline);
469             if (e->renderer() && e->renderer()->style()->floating() != FNONE)
470                 e->getInlineStyleDecl()->setProperty(CSSPropertyFloat, CSSValueNone);
471         }
472         if (node == m_lastLeafInserted)
473             break;
474     }
475 }
476 
removeUnrenderedTextNodesAtEnds()477 void ReplaceSelectionCommand::removeUnrenderedTextNodesAtEnds()
478 {
479     document()->updateLayoutIgnorePendingStylesheets();
480     if (!m_lastLeafInserted->renderer() &&
481         m_lastLeafInserted->isTextNode() &&
482         !enclosingNodeWithTag(Position(m_lastLeafInserted.get(), 0), selectTag) &&
483         !enclosingNodeWithTag(Position(m_lastLeafInserted.get(), 0), scriptTag)) {
484         if (m_firstNodeInserted == m_lastLeafInserted) {
485             removeNode(m_lastLeafInserted.get());
486             m_lastLeafInserted = 0;
487             m_firstNodeInserted = 0;
488             return;
489         }
490         RefPtr<Node> previous = m_lastLeafInserted->traversePreviousNode();
491         removeNode(m_lastLeafInserted.get());
492         m_lastLeafInserted = previous;
493     }
494 
495     // We don't have to make sure that m_firstNodeInserted isn't inside a select or script element, because
496     // it is a top level node in the fragment and the user can't insert into those elements.
497     if (!m_firstNodeInserted->renderer() &&
498         m_firstNodeInserted->isTextNode()) {
499         if (m_firstNodeInserted == m_lastLeafInserted) {
500             removeNode(m_firstNodeInserted.get());
501             m_firstNodeInserted = 0;
502             m_lastLeafInserted = 0;
503             return;
504         }
505         RefPtr<Node> next = m_firstNodeInserted->traverseNextSibling();
506         removeNode(m_firstNodeInserted.get());
507         m_firstNodeInserted = next;
508     }
509 }
510 
handlePasteAsQuotationNode()511 void ReplaceSelectionCommand::handlePasteAsQuotationNode()
512 {
513     Node* node = m_firstNodeInserted.get();
514     if (isMailPasteAsQuotationNode(node))
515         removeNodeAttribute(static_cast<Element*>(node), classAttr);
516 }
517 
positionAtEndOfInsertedContent()518 VisiblePosition ReplaceSelectionCommand::positionAtEndOfInsertedContent()
519 {
520     Node* lastNode = m_lastLeafInserted.get();
521     // FIXME: Why is this hack here?  What's special about <select> tags?
522     Node* enclosingSelect = enclosingNodeWithTag(firstDeepEditingPositionForNode(lastNode), selectTag);
523     if (enclosingSelect)
524         lastNode = enclosingSelect;
525     return lastDeepEditingPositionForNode(lastNode);
526 }
527 
positionAtStartOfInsertedContent()528 VisiblePosition ReplaceSelectionCommand::positionAtStartOfInsertedContent()
529 {
530     // Return the inserted content's first VisiblePosition.
531     return VisiblePosition(nextCandidate(positionBeforeNode(m_firstNodeInserted.get())));
532 }
533 
534 // Remove style spans before insertion if they are unnecessary.  It's faster because we'll
535 // avoid doing a layout.
handleStyleSpansBeforeInsertion(ReplacementFragment & fragment,const Position & insertionPos)536 static bool handleStyleSpansBeforeInsertion(ReplacementFragment& fragment, const Position& insertionPos)
537 {
538     Node* topNode = fragment.firstChild();
539 
540     // Handling the case where we are doing Paste as Quotation or pasting into quoted content is more complicated (see handleStyleSpans)
541     // and doesn't receive the optimization.
542     if (isMailPasteAsQuotationNode(topNode) || nearestMailBlockquote(topNode))
543         return false;
544 
545     // Either there are no style spans in the fragment or a WebKit client has added content to the fragment
546     // before inserting it.  Look for and handle style spans after insertion.
547     if (!isStyleSpan(topNode))
548         return false;
549 
550     Node* sourceDocumentStyleSpan = topNode;
551     RefPtr<Node> copiedRangeStyleSpan = sourceDocumentStyleSpan->firstChild();
552 
553     RefPtr<CSSMutableStyleDeclaration> styleAtInsertionPos = editingStyleAtPosition(rangeCompliantEquivalent(insertionPos));
554 
555     String styleText = styleAtInsertionPos->cssText();
556 
557     if (styleText == static_cast<Element*>(sourceDocumentStyleSpan)->getAttribute(styleAttr)) {
558         fragment.removeNodePreservingChildren(sourceDocumentStyleSpan);
559         if (!isStyleSpan(copiedRangeStyleSpan.get()))
560             return true;
561     }
562 
563     if (isStyleSpan(copiedRangeStyleSpan.get()) && styleText == static_cast<Element*>(copiedRangeStyleSpan.get())->getAttribute(styleAttr)) {
564         fragment.removeNodePreservingChildren(copiedRangeStyleSpan.get());
565         return true;
566     }
567 
568     return false;
569 }
570 
571 // At copy time, WebKit wraps copied content in a span that contains the source document's
572 // default styles.  If the copied Range inherits any other styles from its ancestors, we put
573 // those styles on a second span.
574 // This function removes redundant styles from those spans, and removes the spans if all their
575 // styles are redundant.
576 // We should remove the Apple-style-span class when we're done, see <rdar://problem/5685600>.
577 // We should remove styles from spans that are overridden by all of their children, either here
578 // or at copy time.
handleStyleSpans()579 void ReplaceSelectionCommand::handleStyleSpans()
580 {
581     Node* sourceDocumentStyleSpan = 0;
582     Node* copiedRangeStyleSpan = 0;
583     // The style span that contains the source document's default style should be at
584     // the top of the fragment, but Mail sometimes adds a wrapper (for Paste As Quotation),
585     // so search for the top level style span instead of assuming it's at the top.
586     for (Node* node = m_firstNodeInserted.get(); node; node = node->traverseNextNode()) {
587         if (isStyleSpan(node)) {
588             sourceDocumentStyleSpan = node;
589             // If the copied Range's common ancestor had user applied inheritable styles
590             // on it, they'll be on a second style span, just below the one that holds the
591             // document defaults.
592             if (isStyleSpan(node->firstChild()))
593                 copiedRangeStyleSpan = node->firstChild();
594             break;
595         }
596     }
597 
598     // There might not be any style spans if we're pasting from another application or if
599     // we are here because of a document.execCommand("InsertHTML", ...) call.
600     if (!sourceDocumentStyleSpan)
601         return;
602 
603     RefPtr<CSSMutableStyleDeclaration> sourceDocumentStyle = static_cast<HTMLElement*>(sourceDocumentStyleSpan)->getInlineStyleDecl()->copy();
604     Node* context = sourceDocumentStyleSpan->parentNode();
605 
606     // If Mail wraps the fragment with a Paste as Quotation blockquote, or if you're pasting into a quoted region,
607     // styles from blockquoteNode are allowed to override those from the source document, see <rdar://problem/4930986> and <rdar://problem/5089327>.
608     Node* blockquoteNode = isMailPasteAsQuotationNode(context) ? context : nearestMailBlockquote(context);
609     if (blockquoteNode) {
610         RefPtr<CSSMutableStyleDeclaration> blockquoteStyle = editingStyleAtPosition(Position(blockquoteNode, 0));
611         RefPtr<CSSMutableStyleDeclaration> parentStyle = editingStyleAtPosition(Position(blockquoteNode->parentNode(), 0));
612         parentStyle->diff(blockquoteStyle.get());
613 
614         CSSMutableStyleDeclaration::const_iterator end = blockquoteStyle->end();
615         for (CSSMutableStyleDeclaration::const_iterator it = blockquoteStyle->begin(); it != end; ++it) {
616             const CSSProperty& property = *it;
617             sourceDocumentStyle->removeProperty(property.id());
618         }
619 
620         context = blockquoteNode->parentNode();
621     }
622 
623     // This operation requires that only editing styles to be removed from sourceDocumentStyle.
624     prepareEditingStyleToApplyAt(sourceDocumentStyle.get(), Position(context, 0));
625 
626     // Remove block properties in the span's style. This prevents properties that probably have no effect
627     // currently from affecting blocks later if the style is cloned for a new block element during a future
628     // editing operation.
629     // FIXME: They *can* have an effect currently if blocks beneath the style span aren't individually marked
630     // with block styles by the editing engine used to style them.  WebKit doesn't do this, but others might.
631     sourceDocumentStyle->removeBlockProperties();
632 
633     // The styles on sourceDocumentStyleSpan are all redundant, and there is no copiedRangeStyleSpan
634     // to consider.  We're finished.
635     if (sourceDocumentStyle->length() == 0 && !copiedRangeStyleSpan) {
636         removeNodePreservingChildren(sourceDocumentStyleSpan);
637         return;
638     }
639 
640     // There are non-redundant styles on sourceDocumentStyleSpan, but there is no
641     // copiedRangeStyleSpan.  Clear the redundant styles from sourceDocumentStyleSpan
642     // and return.
643     if (sourceDocumentStyle->length() > 0 && !copiedRangeStyleSpan) {
644         setNodeAttribute(static_cast<Element*>(sourceDocumentStyleSpan), styleAttr, sourceDocumentStyle->cssText());
645         return;
646     }
647 
648     RefPtr<CSSMutableStyleDeclaration> copiedRangeStyle = static_cast<HTMLElement*>(copiedRangeStyleSpan)->getInlineStyleDecl()->copy();
649 
650     // We're going to put sourceDocumentStyleSpan's non-redundant styles onto copiedRangeStyleSpan,
651     // as long as they aren't overridden by ones on copiedRangeStyleSpan.
652     sourceDocumentStyle->merge(copiedRangeStyle.get(), true);
653     copiedRangeStyle = sourceDocumentStyle;
654 
655     removeNodePreservingChildren(sourceDocumentStyleSpan);
656 
657     // Remove redundant styles.
658     context = copiedRangeStyleSpan->parentNode();
659     prepareEditingStyleToApplyAt(copiedRangeStyle.get(), Position(context, 0));
660 
661     // See the comments above about removing block properties.
662     copiedRangeStyle->removeBlockProperties();
663 
664     // All the styles on copiedRangeStyleSpan are redundant, remove it.
665     if (copiedRangeStyle->length() == 0) {
666         removeNodePreservingChildren(copiedRangeStyleSpan);
667         return;
668     }
669 
670     // Clear the redundant styles from the span's style attribute.
671     // FIXME: If font-family:-webkit-monospace is non-redundant, then the font-size should stay, even if it
672     // appears redundant.
673     setNodeAttribute(static_cast<Element*>(copiedRangeStyleSpan), styleAttr, copiedRangeStyle->cssText());
674 }
675 
mergeEndIfNeeded()676 void ReplaceSelectionCommand::mergeEndIfNeeded()
677 {
678     if (!m_shouldMergeEnd)
679         return;
680 
681     VisiblePosition startOfInsertedContent(positionAtStartOfInsertedContent());
682     VisiblePosition endOfInsertedContent(positionAtEndOfInsertedContent());
683 
684     // Bail to avoid infinite recursion.
685     if (m_movingParagraph) {
686         ASSERT_NOT_REACHED();
687         return;
688     }
689 
690     // Merging two paragraphs will destroy the moved one's block styles.  Always move the end of inserted forward
691     // to preserve the block style of the paragraph already in the document, unless the paragraph to move would
692     // include the what was the start of the selection that was pasted into, so that we preserve that paragraph's
693     // block styles.
694     bool mergeForward = !(inSameParagraph(startOfInsertedContent, endOfInsertedContent) && !isStartOfParagraph(startOfInsertedContent));
695 
696     VisiblePosition destination = mergeForward ? endOfInsertedContent.next() : endOfInsertedContent;
697     VisiblePosition startOfParagraphToMove = mergeForward ? startOfParagraph(endOfInsertedContent) : endOfInsertedContent.next();
698 
699     moveParagraph(startOfParagraphToMove, endOfParagraph(startOfParagraphToMove), destination);
700     // Merging forward will remove m_lastLeafInserted from the document.
701     // FIXME: Maintain positions for the start and end of inserted content instead of keeping nodes.  The nodes are
702     // only ever used to create positions where inserted content starts/ends.  Also, we sometimes insert content
703     // directly into text nodes already in the document, in which case tracking inserted nodes is inadequate.
704     if (mergeForward) {
705         m_lastLeafInserted = destination.previous().deepEquivalent().node();
706         if (!m_firstNodeInserted->inDocument())
707             m_firstNodeInserted = endingSelection().visibleStart().deepEquivalent().node();
708     }
709 }
710 
doApply()711 void ReplaceSelectionCommand::doApply()
712 {
713     VisibleSelection selection = endingSelection();
714     ASSERT(selection.isCaretOrRange());
715     ASSERT(selection.start().node());
716     if (selection.isNone() || !selection.start().node())
717         return;
718 
719     bool selectionIsPlainText = !selection.isContentRichlyEditable();
720 
721     Element* currentRoot = selection.rootEditableElement();
722     ReplacementFragment fragment(document(), m_documentFragment.get(), m_matchStyle, selection);
723 
724     if (performTrivialReplace(fragment))
725         return;
726 
727     if (m_matchStyle)
728         m_insertionStyle = editingStyleAtPosition(selection.start(), IncludeTypingStyle);
729 
730     VisiblePosition visibleStart = selection.visibleStart();
731     VisiblePosition visibleEnd = selection.visibleEnd();
732 
733     bool selectionEndWasEndOfParagraph = isEndOfParagraph(visibleEnd);
734     bool selectionStartWasStartOfParagraph = isStartOfParagraph(visibleStart);
735 
736     Node* startBlock = enclosingBlock(visibleStart.deepEquivalent().node());
737 
738     Position insertionPos = selection.start();
739     bool startIsInsideMailBlockquote = nearestMailBlockquote(insertionPos.node());
740 
741     if ((selectionStartWasStartOfParagraph && selectionEndWasEndOfParagraph && !startIsInsideMailBlockquote) ||
742         startBlock == currentRoot ||
743         (startBlock && startBlock->renderer() && startBlock->renderer()->isListItem()) ||
744         selectionIsPlainText)
745         m_preventNesting = false;
746 
747     if (selection.isRange()) {
748         // When the end of the selection being pasted into is at the end of a paragraph, and that selection
749         // spans multiple blocks, not merging may leave an empty line.
750         // When the start of the selection being pasted into is at the start of a block, not merging
751         // will leave hanging block(s).
752         // Merge blocks if the start of the selection was in a Mail blockquote, since we handle
753         // that case specially to prevent nesting.
754         bool mergeBlocksAfterDelete = startIsInsideMailBlockquote || isEndOfParagraph(visibleEnd) || isStartOfBlock(visibleStart);
755         // FIXME: We should only expand to include fully selected special elements if we are copying a
756         // selection and pasting it on top of itself.
757         deleteSelection(false, mergeBlocksAfterDelete, true, false);
758         visibleStart = endingSelection().visibleStart();
759         if (fragment.hasInterchangeNewlineAtStart()) {
760             if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleStart)) {
761                 if (!isEndOfDocument(visibleStart))
762                     setEndingSelection(visibleStart.next());
763             } else
764                 insertParagraphSeparator();
765         }
766         insertionPos = endingSelection().start();
767     } else {
768         ASSERT(selection.isCaret());
769         if (fragment.hasInterchangeNewlineAtStart()) {
770             VisiblePosition next = visibleStart.next(true);
771             if (isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleStart) && next.isNotNull())
772                 setEndingSelection(next);
773             else
774                 insertParagraphSeparator();
775         }
776         // We split the current paragraph in two to avoid nesting the blocks from the fragment inside the current block.
777         // For example paste <div>foo</div><div>bar</div><div>baz</div> into <div>x^x</div>, where ^ is the caret.
778         // As long as the  div styles are the same, visually you'd expect: <div>xbar</div><div>bar</div><div>bazx</div>,
779         // not <div>xbar<div>bar</div><div>bazx</div></div>.
780         // Don't do this if the selection started in a Mail blockquote.
781         if (m_preventNesting && !startIsInsideMailBlockquote && !isEndOfParagraph(visibleStart) && !isStartOfParagraph(visibleStart)) {
782             insertParagraphSeparator();
783             setEndingSelection(endingSelection().visibleStart().previous());
784         }
785         insertionPos = endingSelection().start();
786     }
787 
788     // We don't want any of the pasted content to end up nested in a Mail blockquote, so first break
789     // out of any surrounding Mail blockquotes. Unless we're inserting in a table, in which case
790     // breaking the blockquote will prevent the content from actually being inserted in the table.
791     if (startIsInsideMailBlockquote && m_preventNesting && !(enclosingNodeOfType(insertionPos, &isTableStructureNode))) {
792         applyCommandToComposite(BreakBlockquoteCommand::create(document()));
793         // This will leave a br between the split.
794         Node* br = endingSelection().start().node();
795         ASSERT(br->hasTagName(brTag));
796         // Insert content between the two blockquotes, but remove the br (since it was just a placeholder).
797         insertionPos = positionBeforeNode(br);
798         removeNode(br);
799     }
800 
801     // Inserting content could cause whitespace to collapse, e.g. inserting <div>foo</div> into hello^ world.
802     prepareWhitespaceAtPositionForSplit(insertionPos);
803 
804     // NOTE: This would be an incorrect usage of downstream() if downstream() were changed to mean the last position after
805     // p that maps to the same visible position as p (since in the case where a br is at the end of a block and collapsed
806     // away, there are positions after the br which map to the same visible position as [br, 0]).
807     Node* endBR = insertionPos.downstream().node()->hasTagName(brTag) ? insertionPos.downstream().node() : 0;
808     VisiblePosition originalVisPosBeforeEndBR;
809     if (endBR)
810         originalVisPosBeforeEndBR = VisiblePosition(endBR, 0, DOWNSTREAM).previous();
811 
812     startBlock = enclosingBlock(insertionPos.node());
813 
814     // Adjust insertionPos to prevent nesting.
815     // If the start was in a Mail blockquote, we will have already handled adjusting insertionPos above.
816     if (m_preventNesting && startBlock && !startIsInsideMailBlockquote) {
817         ASSERT(startBlock != currentRoot);
818         VisiblePosition visibleInsertionPos(insertionPos);
819         if (isEndOfBlock(visibleInsertionPos) && !(isStartOfBlock(visibleInsertionPos) && fragment.hasInterchangeNewlineAtEnd()))
820             insertionPos = positionAfterNode(startBlock);
821         else if (isStartOfBlock(visibleInsertionPos))
822             insertionPos = positionBeforeNode(startBlock);
823     }
824 
825     // Paste into run of tabs splits the tab span.
826     insertionPos = positionOutsideTabSpan(insertionPos);
827 
828     // Paste at start or end of link goes outside of link.
829     insertionPos = positionAvoidingSpecialElementBoundary(insertionPos);
830 
831     // FIXME: Can this wait until after the operation has been performed?  There doesn't seem to be
832     // any work performed after this that queries or uses the typing style.
833     if (Frame* frame = document()->frame())
834         frame->clearTypingStyle();
835 
836     bool handledStyleSpans = handleStyleSpansBeforeInsertion(fragment, insertionPos);
837 
838     // FIXME: When pasting rich content we're often prevented from heading down the fast path by style spans.  Try
839     // again here if they've been removed.
840 
841     // We're finished if there is nothing to add.
842     if (fragment.isEmpty() || !fragment.firstChild())
843         return;
844 
845     // 1) Insert the content.
846     // 2) Remove redundant styles and style tags, this inner <b> for example: <b>foo <b>bar</b> baz</b>.
847     // 3) Merge the start of the added content with the content before the position being pasted into.
848     // 4) Do one of the following: a) expand the last br if the fragment ends with one and it collapsed,
849     // b) merge the last paragraph of the incoming fragment with the paragraph that contained the
850     // end of the selection that was pasted into, or c) handle an interchange newline at the end of the
851     // incoming fragment.
852     // 5) Add spaces for smart replace.
853     // 6) Select the replacement if requested, and match style if requested.
854 
855     VisiblePosition startOfInsertedContent, endOfInsertedContent;
856 
857     RefPtr<Node> refNode = fragment.firstChild();
858     RefPtr<Node> node = refNode->nextSibling();
859 
860     fragment.removeNode(refNode);
861     insertNodeAtAndUpdateNodesInserted(refNode, insertionPos);
862 
863     // Mutation events (bug 22634) may have already removed the inserted content
864     if (!refNode->inDocument())
865         return;
866 
867     while (node) {
868         Node* next = node->nextSibling();
869         fragment.removeNode(node);
870         insertNodeAfterAndUpdateNodesInserted(node, refNode.get());
871 
872         // Mutation events (bug 22634) may have already removed the inserted content
873         if (!node->inDocument())
874             return;
875 
876         refNode = node;
877         node = next;
878     }
879 
880     removeUnrenderedTextNodesAtEnds();
881 
882     negateStyleRulesThatAffectAppearance();
883 
884     if (!handledStyleSpans)
885         handleStyleSpans();
886 
887     // Mutation events (bug 20161) may have already removed the inserted content
888     if (!m_firstNodeInserted || !m_firstNodeInserted->inDocument())
889         return;
890 
891     endOfInsertedContent = positionAtEndOfInsertedContent();
892     startOfInsertedContent = positionAtStartOfInsertedContent();
893 
894     // We inserted before the startBlock to prevent nesting, and the content before the startBlock wasn't in its own block and
895     // didn't have a br after it, so the inserted content ended up in the same paragraph.
896     if (startBlock && insertionPos.node() == startBlock->parentNode() && (unsigned)insertionPos.deprecatedEditingOffset() < startBlock->nodeIndex() && !isStartOfParagraph(startOfInsertedContent))
897         insertNodeAt(createBreakElement(document()).get(), startOfInsertedContent.deepEquivalent());
898 
899     Position lastPositionToSelect;
900 
901     bool interchangeNewlineAtEnd = fragment.hasInterchangeNewlineAtEnd();
902 
903     if (shouldRemoveEndBR(endBR, originalVisPosBeforeEndBR))
904         removeNodeAndPruneAncestors(endBR);
905 
906     // Determine whether or not we should merge the end of inserted content with what's after it before we do
907     // the start merge so that the start merge doesn't effect our decision.
908     m_shouldMergeEnd = shouldMergeEnd(selectionEndWasEndOfParagraph);
909 
910     if (shouldMergeStart(selectionStartWasStartOfParagraph, fragment.hasInterchangeNewlineAtStart(), startIsInsideMailBlockquote)) {
911         VisiblePosition destination = startOfInsertedContent.previous();
912         VisiblePosition startOfParagraphToMove = startOfInsertedContent;
913 
914         // Merging the the first paragraph of inserted content with the content that came
915         // before the selection that was pasted into would also move content after
916         // the selection that was pasted into if: only one paragraph was being pasted,
917         // and it was not wrapped in a block, the selection that was pasted into ended
918         // at the end of a block and the next paragraph didn't start at the start of a block.
919         // Insert a line break just after the inserted content to separate it from what
920         // comes after and prevent that from happening.
921         VisiblePosition endOfInsertedContent = positionAtEndOfInsertedContent();
922         if (startOfParagraph(endOfInsertedContent) == startOfParagraphToMove) {
923             insertNodeAt(createBreakElement(document()).get(), endOfInsertedContent.deepEquivalent());
924             // Mutation events (bug 22634) triggered by inserting the <br> might have removed the content we're about to move
925             if (!startOfParagraphToMove.deepEquivalent().node()->inDocument())
926                 return;
927         }
928 
929         // FIXME: Maintain positions for the start and end of inserted content instead of keeping nodes.  The nodes are
930         // only ever used to create positions where inserted content starts/ends.
931         moveParagraph(startOfParagraphToMove, endOfParagraph(startOfParagraphToMove), destination);
932         m_firstNodeInserted = endingSelection().visibleStart().deepEquivalent().downstream().node();
933         if (!m_lastLeafInserted->inDocument())
934             m_lastLeafInserted = endingSelection().visibleEnd().deepEquivalent().upstream().node();
935     }
936 
937     endOfInsertedContent = positionAtEndOfInsertedContent();
938     startOfInsertedContent = positionAtStartOfInsertedContent();
939 
940     if (interchangeNewlineAtEnd) {
941         VisiblePosition next = endOfInsertedContent.next(true);
942 
943         if (selectionEndWasEndOfParagraph || !isEndOfParagraph(endOfInsertedContent) || next.isNull()) {
944             if (!isStartOfParagraph(endOfInsertedContent)) {
945                 setEndingSelection(endOfInsertedContent);
946                 // Use a default paragraph element (a plain div) for the empty paragraph, using the last paragraph
947                 // block's style seems to annoy users.
948                 insertParagraphSeparator(true);
949 
950                 // Select up to the paragraph separator that was added.
951                 lastPositionToSelect = endingSelection().visibleStart().deepEquivalent();
952                 updateNodesInserted(lastPositionToSelect.node());
953             }
954         } else {
955             // Select up to the beginning of the next paragraph.
956             lastPositionToSelect = next.deepEquivalent().downstream();
957         }
958 
959     } else
960         mergeEndIfNeeded();
961 
962     handlePasteAsQuotationNode();
963 
964     endOfInsertedContent = positionAtEndOfInsertedContent();
965     startOfInsertedContent = positionAtStartOfInsertedContent();
966 
967     // Add spaces for smart replace.
968     if (m_smartReplace && currentRoot) {
969         // Disable smart replace for password fields.
970         Node* start = currentRoot->shadowAncestorNode();
971         if (start->hasTagName(inputTag) && static_cast<HTMLInputElement*>(start)->inputType() == HTMLInputElement::PASSWORD)
972             m_smartReplace = false;
973     }
974     if (m_smartReplace) {
975         bool needsTrailingSpace = !isEndOfParagraph(endOfInsertedContent) &&
976                                   !isCharacterSmartReplaceExempt(endOfInsertedContent.characterAfter(), false);
977         if (needsTrailingSpace) {
978             RenderObject* renderer = m_lastLeafInserted->renderer();
979             bool collapseWhiteSpace = !renderer || renderer->style()->collapseWhiteSpace();
980             Node* endNode = positionAtEndOfInsertedContent().deepEquivalent().upstream().node();
981             if (endNode->isTextNode()) {
982                 Text* text = static_cast<Text*>(endNode);
983                 insertTextIntoNode(text, text->length(), collapseWhiteSpace ? nonBreakingSpaceString() : " ");
984             } else {
985                 RefPtr<Node> node = document()->createEditingTextNode(collapseWhiteSpace ? nonBreakingSpaceString() : " ");
986                 insertNodeAfterAndUpdateNodesInserted(node, endNode);
987             }
988         }
989 
990         bool needsLeadingSpace = !isStartOfParagraph(startOfInsertedContent) &&
991                                  !isCharacterSmartReplaceExempt(startOfInsertedContent.previous().characterAfter(), true);
992         if (needsLeadingSpace) {
993             RenderObject* renderer = m_lastLeafInserted->renderer();
994             bool collapseWhiteSpace = !renderer || renderer->style()->collapseWhiteSpace();
995             Node* startNode = positionAtStartOfInsertedContent().deepEquivalent().downstream().node();
996             if (startNode->isTextNode()) {
997                 Text* text = static_cast<Text*>(startNode);
998                 insertTextIntoNode(text, 0, collapseWhiteSpace ? nonBreakingSpaceString() : " ");
999             } else {
1000                 RefPtr<Node> node = document()->createEditingTextNode(collapseWhiteSpace ? nonBreakingSpaceString() : " ");
1001                 // Don't updateNodesInserted.  Doing so would set m_lastLeafInserted to be the node containing the
1002                 // leading space, but m_lastLeafInserted is supposed to mark the end of pasted content.
1003                 insertNodeBefore(node, startNode);
1004                 // FIXME: Use positions to track the start/end of inserted content.
1005                 m_firstNodeInserted = node;
1006             }
1007         }
1008     }
1009 
1010     completeHTMLReplacement(lastPositionToSelect);
1011 }
1012 
shouldRemoveEndBR(Node * endBR,const VisiblePosition & originalVisPosBeforeEndBR)1013 bool ReplaceSelectionCommand::shouldRemoveEndBR(Node* endBR, const VisiblePosition& originalVisPosBeforeEndBR)
1014 {
1015     if (!endBR || !endBR->inDocument())
1016         return false;
1017 
1018     VisiblePosition visiblePos(Position(endBR, 0));
1019 
1020     // Don't remove the br if nothing was inserted.
1021     if (visiblePos.previous() == originalVisPosBeforeEndBR)
1022         return false;
1023 
1024     // Remove the br if it is collapsed away and so is unnecessary.
1025     if (!document()->inStrictMode() && isEndOfBlock(visiblePos) && !isStartOfParagraph(visiblePos))
1026         return true;
1027 
1028     // A br that was originally holding a line open should be displaced by inserted content or turned into a line break.
1029     // A br that was originally acting as a line break should still be acting as a line break, not as a placeholder.
1030     return isStartOfParagraph(visiblePos) && isEndOfParagraph(visiblePos);
1031 }
1032 
completeHTMLReplacement(const Position & lastPositionToSelect)1033 void ReplaceSelectionCommand::completeHTMLReplacement(const Position &lastPositionToSelect)
1034 {
1035     Position start;
1036     Position end;
1037 
1038     // FIXME: This should never not be the case.
1039     if (m_firstNodeInserted && m_firstNodeInserted->inDocument() && m_lastLeafInserted && m_lastLeafInserted->inDocument()) {
1040 
1041         start = positionAtStartOfInsertedContent().deepEquivalent();
1042         end = positionAtEndOfInsertedContent().deepEquivalent();
1043 
1044         // FIXME (11475): Remove this and require that the creator of the fragment to use nbsps.
1045         rebalanceWhitespaceAt(start);
1046         rebalanceWhitespaceAt(end);
1047 
1048         if (m_matchStyle) {
1049             ASSERT(m_insertionStyle);
1050             applyStyle(m_insertionStyle.get(), start, end);
1051         }
1052 
1053         if (lastPositionToSelect.isNotNull())
1054             end = lastPositionToSelect;
1055     } else if (lastPositionToSelect.isNotNull())
1056         start = end = lastPositionToSelect;
1057     else
1058         return;
1059 
1060     if (m_selectReplacement)
1061         setEndingSelection(VisibleSelection(start, end, SEL_DEFAULT_AFFINITY));
1062     else
1063         setEndingSelection(VisibleSelection(end, SEL_DEFAULT_AFFINITY));
1064 }
1065 
editingAction() const1066 EditAction ReplaceSelectionCommand::editingAction() const
1067 {
1068     return m_editAction;
1069 }
1070 
insertNodeAfterAndUpdateNodesInserted(PassRefPtr<Node> insertChild,Node * refChild)1071 void ReplaceSelectionCommand::insertNodeAfterAndUpdateNodesInserted(PassRefPtr<Node> insertChild, Node* refChild)
1072 {
1073     Node* nodeToUpdate = insertChild.get(); // insertChild will be cleared when passed
1074     insertNodeAfter(insertChild, refChild);
1075     updateNodesInserted(nodeToUpdate);
1076 }
1077 
insertNodeAtAndUpdateNodesInserted(PassRefPtr<Node> insertChild,const Position & p)1078 void ReplaceSelectionCommand::insertNodeAtAndUpdateNodesInserted(PassRefPtr<Node> insertChild, const Position& p)
1079 {
1080     Node* nodeToUpdate = insertChild.get(); // insertChild will be cleared when passed
1081     insertNodeAt(insertChild, p);
1082     updateNodesInserted(nodeToUpdate);
1083 }
1084 
insertNodeBeforeAndUpdateNodesInserted(PassRefPtr<Node> insertChild,Node * refChild)1085 void ReplaceSelectionCommand::insertNodeBeforeAndUpdateNodesInserted(PassRefPtr<Node> insertChild, Node* refChild)
1086 {
1087     Node* nodeToUpdate = insertChild.get(); // insertChild will be cleared when passed
1088     insertNodeBefore(insertChild, refChild);
1089     updateNodesInserted(nodeToUpdate);
1090 }
1091 
updateNodesInserted(Node * node)1092 void ReplaceSelectionCommand::updateNodesInserted(Node *node)
1093 {
1094     if (!node)
1095         return;
1096 
1097     if (!m_firstNodeInserted)
1098         m_firstNodeInserted = node;
1099 
1100     if (node == m_lastLeafInserted)
1101         return;
1102 
1103     m_lastLeafInserted = node->lastDescendant();
1104 }
1105 
1106 // During simple pastes, where we're just pasting a text node into a run of text, we insert the text node
1107 // directly into the text node that holds the selection.  This is much faster than the generalized code in
1108 // ReplaceSelectionCommand, and works around <https://bugs.webkit.org/show_bug.cgi?id=6148> since we don't
1109 // split text nodes.
performTrivialReplace(const ReplacementFragment & fragment)1110 bool ReplaceSelectionCommand::performTrivialReplace(const ReplacementFragment& fragment)
1111 {
1112     if (!fragment.firstChild() || fragment.firstChild() != fragment.lastChild() || !fragment.firstChild()->isTextNode())
1113         return false;
1114 
1115     // FIXME: Would be nice to handle smart replace in the fast path.
1116     if (m_smartReplace || fragment.hasInterchangeNewlineAtStart() || fragment.hasInterchangeNewlineAtEnd())
1117         return false;
1118 
1119     Text* textNode = static_cast<Text*>(fragment.firstChild());
1120     // Our fragment creation code handles tabs, spaces, and newlines, so we don't have to worry about those here.
1121     String text(textNode->data());
1122 
1123     Position start = endingSelection().start();
1124     Position end = endingSelection().end();
1125 
1126     if (start.anchorNode() != end.anchorNode() || !start.anchorNode()->isTextNode())
1127         return false;
1128 
1129     replaceTextInNode(static_cast<Text*>(start.anchorNode()), start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
1130 
1131     end = Position(start.anchorNode(), start.offsetInContainerNode() + text.length());
1132 
1133     VisibleSelection selectionAfterReplace(m_selectReplacement ? start : end, end);
1134 
1135     setEndingSelection(selectionAfterReplace);
1136 
1137     return true;
1138 }
1139 
1140 } // namespace WebCore
1141