From 9c5dd6e78019bbb535968631ba03ff03555d587a Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 26 Feb 2016 17:34:40 -0800 Subject: [PATCH] Bug 1247483 - Only replace nodes in nsHTMLEditor::ReplaceOrphanedStructure if all nodes in node list are descendants of replacement node. r=ehsan --- editor/libeditor/nsHTMLDataTransfer.cpp | 26 +++++---- editor/libeditor/tests/mochitest.ini | 2 + editor/libeditor/tests/test_bug1247483.html | 61 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 editor/libeditor/tests/test_bug1247483.html diff --git a/editor/libeditor/nsHTMLDataTransfer.cpp b/editor/libeditor/nsHTMLDataTransfer.cpp index b062ca38601..3286a937394 100644 --- a/editor/libeditor/nsHTMLDataTransfer.cpp +++ b/editor/libeditor/nsHTMLDataTransfer.cpp @@ -2297,22 +2297,26 @@ nsHTMLEditor::ReplaceOrphanedStructure(StartOrEnd aStartOrEnd, } // If we found substructure, paste it instead of its descendants. - // Postprocess list to remove any descendants of this node so that we don't - // insert them twice. - while (aNodeArray.Length()) { - int32_t idx = aStartOrEnd == StartOrEnd::start ? 0 - : aNodeArray.Length() - 1; + // Only replace with the substructure if all the nodes in the list are + // descendants. + bool shouldReplaceNodes = true; + for (uint32_t i = 0; i < aNodeArray.Length(); i++) { + uint32_t idx = aStartOrEnd == StartOrEnd::start ? + i : (aNodeArray.Length() - i - 1); OwningNonNull endpoint = aNodeArray[idx]; if (!nsEditorUtils::IsDescendantOf(endpoint, replaceNode)) { + shouldReplaceNodes = false; break; } - aNodeArray.RemoveElementAt(idx); } - // Now replace the removed nodes with the structural parent - if (aStartOrEnd == StartOrEnd::end) { - aNodeArray.AppendElement(*replaceNode); - } else { - aNodeArray.InsertElementAt(0, *replaceNode); + if (shouldReplaceNodes) { + // Now replace the removed nodes with the structural parent + aNodeArray.Clear(); + if (aStartOrEnd == StartOrEnd::end) { + aNodeArray.AppendElement(*replaceNode); + } else { + aNodeArray.InsertElementAt(0, *replaceNode); + } } } diff --git a/editor/libeditor/tests/mochitest.ini b/editor/libeditor/tests/mochitest.ini index 5eab46b5feb..14828ef3cd4 100644 --- a/editor/libeditor/tests/mochitest.ini +++ b/editor/libeditor/tests/mochitest.ini @@ -170,3 +170,5 @@ skip-if = toolkit == 'android' [test_backspace_vs.html] [test_css_chrome_load_access.html] skip-if = toolkit == 'android' # chrome urls not available due to packaging +[test_bug1247483.html] +skip-if = toolkit == 'android' diff --git a/editor/libeditor/tests/test_bug1247483.html b/editor/libeditor/tests/test_bug1247483.html new file mode 100644 index 00000000000..40dbc36cec9 --- /dev/null +++ b/editor/libeditor/tests/test_bug1247483.html @@ -0,0 +1,61 @@ + + +Test for bug 1247483 + + + + + + + +Mozilla Bug 1247483 +

+ +
+
+ +
+ + + +
1 11 2
2 12 2
+
+ +
+
+ + +