Source/WebCore/editing/ReplaceSelectionCommand.cpp

760760 return node;
761761}
762762
 763static bool isNodeNeededToRetainStructure(const Node* node)
 764{
 765 if (node->hasTagName(aTag) || node->hasTagName(bodyTag))
 766 return true;
 767
 768 if (node->hasTagName(tbodyTag) || node->hasTagName(trTag))
 769 return enclosingNodeWithTag(firstPositionInNode(const_cast<Node*>(node)), tableTag);
 770
 771 if (node->renderer() && node->renderer()->isListItem())
 772 return true;
 773 if (!node->renderer() && node->hasTagName(liTag)) {
 774 for (Node* ancestor = node->parentNode(); ancestor; ancestor = ancestor->parentNode()) {
 775 if (node->hasTagName(ulTag) || node->hasTagName(olTag))
 776 return true;
 777 }
 778 return false;
 779 }
 780
 781 if (node->hasTagName(dtTag) || node->hasTagName(ddTag))
 782 return enclosingNodeWithTag(firstPositionInNode(const_cast<Node*>(node)), dlTag);
 783
 784 return false;
 785}
 786
763787static bool isInlineNodeWithStyle(const Node* node)
764788{
765  // We don't want to skip over any block elements.
766  if (!node->renderer() || !node->renderer()->isInline())
 789 if (isNodeNeededToRetainStructure(node))
767790 return false;
768791
769792 if (!node->isHTMLElement())
770793 return false;
771 
 794
772795 // We can skip over elements whose class attribute is
773796 // one of our internal classes.
774797 const HTMLElement* element = static_cast<const HTMLElement*>(node);

781804
782805 // We can skip inline elements that don't have attributes or whose only
783806 // attribute is the style attribute.
 807 // FIXME: we should make sure we only remove editing styles
784808 const NamedNodeMap* attributeMap = element->attributeMap();
785809 if (!attributeMap || attributeMap->isEmpty() || (attributeMap->length() == 1 && element->hasAttribute(styleAttr)))
786810 return true;

937961 // We can skip this optimization for fragments not wrapped in one of
938962 // our style spans and for positions inside list items
939963 // since insertAsListItems already does the right thing.
940  if (!m_matchStyle && !enclosingList(insertionPos.containerNode()) && isStyleSpan(fragment.firstChild())) {
 964 if (!m_matchStyle && !enclosingList(insertionPos.containerNode()) && isStyleSpan(fragment.firstChild())
 965 && VisiblePosition(firstPositionInNode(insertionPos.containerNode())) == VisiblePosition(lastPositionInNode(insertionPos.containerNode()))) {
941966 if (insertionPos.containerNode()->isTextNode() && insertionPos.offsetInContainerNode() && !insertionPos.atLastEditingPositionForNode()) {
942967 splitTextNodeContainingElement(insertionPos.containerText(), insertionPos.offsetInContainerNode());
943968 insertionPos = firstPositionInNode(insertionPos.containerNode());
944969 }
945970
946  // FIXME: isInlineNodeWithStyle does not check editability.
947971 if (RefPtr<Node> nodeToSplitTo = highestEnclosingNodeOfType(insertionPos, isInlineNodeWithStyle)) {
948  if (insertionPos.containerNode() != nodeToSplitTo) {
949  nodeToSplitTo = splitTreeToNode(insertionPos.anchorNode(), nodeToSplitTo.get(), true).get();
 972 if (insertionPos.containerNode() != nodeToSplitTo->parentNode()) {
 973 nodeToSplitTo = splitTreeToNode(insertionPos.anchorNode(), nodeToSplitTo->parentNode()).get();
950974 insertionPos = positionInParentBeforeNode(nodeToSplitTo.get());
951975 }
952976 }
92201

LayoutTests/editing/pasteboard/do-not-copy-unnecessary-styles-expected.txt

22<div>
33This test verifies that WebKit does not erroneously clone nodes hierarchy when copying text with background color.
44To manually this test, select the text inside the div with the border, cut and paste it back. You should not see an additional red border.
5 <div style="border: 2px solid red">
6 <div id="test"><span class="Apple-style-span" style="background-color: rgb(187, 187, 187); ">Select this text</span><br></div>
7 </div>
8 </div>
 5</div><span class="Apple-style-span" style="background-color: rgb(187, 187, 187); ">Select this text</span>
96
107
118
92201

LayoutTests/editing/pasteboard/paste-text-011-expected.txt

66EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
77EDITING DELEGATE: shouldInsertNode:#document-fragment replacingDOMRange:range from 0 of P > BODY > HTML > #document to 0 of P > BODY > HTML > #document givenAction:WebViewInsertActionPasted
88EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
9 EDITING DELEGATE: shouldChangeSelectedDOMRange:(null) toDOMRange:range from 5 of #text > B > FONT > P > SPAN > B > FONT > P > BODY > HTML > #document to 5 of #text > B > FONT > P > SPAN > B > FONT > P > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 9EDITING DELEGATE: shouldChangeSelectedDOMRange:(null) toDOMRange:range from 5 of #text > B > FONT > P > BODY > HTML > #document to 5 of #text > B > FONT > P > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
1010EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
1111EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
1212EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification

3030| <font>
3131| face="Monaco"
3232| <b>
33 | <span>
34 | class="Apple-style-span"
35 | style="font-family: Times; font-weight: normal; "
36 | <p>
37 | <font>
38 | face="Monaco"
39 | <b>
40 | "hello"
41 | <p>
42 | <font>
43 | face="Monaco"
44 | <b>
45 | "there<#selection-caret>"
 33| "hello"
 34| <p>
 35| <font>
 36| face="Monaco"
 37| <b>
 38| "there<#selection-caret>"
4639| "
4740
4841
92201

LayoutTests/editing/pasteboard/display-block-on-spans-expected.txt

2929| <span>
3030| style="display:block"
3131| <b>
32 | "This<#selection-caret>"
33 | <b>
 32| <span>
 33| class="Apple-style-span"
 34| style="font-weight: normal; "
 35| <b>
 36| "This<#selection-caret>"
3437| " is another paragraph."
3538| <br>
3639| "
92201

LayoutTests/editing/pasteboard/paste-pre-002-expected.txt

22foo
33bar
44execCopyCommand: <div id="test" class="editing"> <pre>foo bar</pre> </div>
5 execPasteCommand: <div id="test" class="editing"> <pre><span class="Apple-style-span" style="font-family: Times; white-space: normal; "><pre>foo bar</pre></span></pre> </div>
 5execPasteCommand: <div id="test" class="editing"> <pre>foo bar</pre> </div>
92201

LayoutTests/editing/pasteboard/paste-pre-001-expected.txt

77foo
88bar
99execCutCommand: <div id="test" class="editing"> <pre><br></pre> </div>
10 execPasteCommand: <div id="test" class="editing"> <pre><span class="Apple-style-span" style="font-family: Times; white-space: normal; "><pre>foo bar</pre></span></pre> </div>
 10execPasteCommand: <div id="test" class="editing"> <pre>foo bar</pre> </div>
92201

LayoutTests/editing/pasteboard/5065605-expected.txt

2121EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
2222EDITING DELEGATE: shouldInsertNode:#document-fragment replacingDOMRange:range from 0 of DIV > DIV > BODY > HTML > #document to 0 of DIV > DIV > BODY > HTML > #document givenAction:WebViewInsertActionPasted
2323EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
24 EDITING DELEGATE: shouldChangeSelectedDOMRange:(null) toDOMRange:range from 24 of #text > FONT > DIV > SPAN > FONT > DIV > DIV > BODY > HTML > #document to 24 of #text > FONT > DIV > SPAN > FONT > DIV > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 24EDITING DELEGATE: shouldChangeSelectedDOMRange:(null) toDOMRange:range from 24 of #text > FONT > DIV > DIV > BODY > HTML > #document to 24 of #text > FONT > DIV > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
2525EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
2626EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
2727EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification

3636| class="Apple-style-span"
3737| color="#ff0000"
3838| "This text should be red."
 39| <font>
 40| class="Apple-style-span"
 41| color="#ff0000"
 42| "This text should be red."
3943| <div>
4044| <font>
4145| class="Apple-style-span"
4246| color="#ff0000"
43 | <span>
44 | class="Apple-style-span"
45 | style="color: rgb(0, 0, 0); "
46 | <font>
47 | class="Apple-style-span"
48 | color="#ff0000"
49 | "This text should be red."
50 | <div>
51 | <font>
52 | class="Apple-style-span"
53 | color="#ff0000"
54 | "This text should be red.<#selection-caret>"
 47| "This text should be red.<#selection-caret>"
92201