Bug 1174452: Part 2 -- scan `nsTextNode`'s parent element for preformat in nsDocumentEncoder r=masayuki,hsivonen
authorMirko Brodesser <mbrodesser@mozilla.com>
Thu, 09 May 2019 07:40:47 +0000
changeset 532581 66cff9aa39bcbf1b39aaf21c6759d1c97ad2fe1b
parent 532580 fe847bf289767657759062cf54d5a93a1f12c6fb
child 532582 6b61e5d68fc6495cf84517b4c10d21595634ab25
push id11270
push userrgurzau@mozilla.com
push dateWed, 15 May 2019 15:07:19 +0000
treeherdermozilla-beta@571bc76da583 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki, hsivonen
bugs1174452
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1174452: Part 2 -- scan `nsTextNode`'s parent element for preformat in nsDocumentEncoder r=masayuki,hsivonen This takes into account `white-space: pre` style of `nsTextNode`'s parent element when formatting the selected text of the `nsTextNode` node. Differential Revision: https://phabricator.services.mozilla.com/D29159
dom/base/nsDocumentEncoder.cpp
dom/base/test/test_user_select.html
--- a/dom/base/nsDocumentEncoder.cpp
+++ b/dom/base/nsDocumentEncoder.cpp
@@ -333,18 +333,25 @@ class FixupNodeDeterminer {
   nsINode& mOriginalNode;
 };
 
 nsresult nsDocumentEncoder::SerializeNodeStart(nsINode& aOriginalNode,
                                                int32_t aStartOffset,
                                                int32_t aEndOffset,
                                                nsAString& aStr,
                                                nsINode* aFixupNode) {
-  if (mNeedsPreformatScanning && aOriginalNode.IsElement()) {
-    mSerializer->ScanElementForPreformat(aOriginalNode.AsElement());
+  if (mNeedsPreformatScanning) {
+    if (aOriginalNode.IsElement()) {
+      mSerializer->ScanElementForPreformat(aOriginalNode.AsElement());
+    } else if (aOriginalNode.IsText()) {
+      const nsCOMPtr<nsINode> parent = aOriginalNode.GetParent();
+      if (parent && parent->IsElement()) {
+        mSerializer->ScanElementForPreformat(parent->AsElement());
+      }
+    }
   }
 
   if (!IsVisibleNode(&aOriginalNode)) {
     return NS_OK;
   }
 
   FixupNodeDeterminer fixupNodeDeterminer{mNodeFixup, aFixupNode,
                                           aOriginalNode};
@@ -388,18 +395,25 @@ nsresult nsDocumentEncoder::SerializeNod
       break;
     }
   }
 
   return NS_OK;
 }
 
 nsresult nsDocumentEncoder::SerializeNodeEnd(nsINode& aNode, nsAString& aStr) {
-  if (mNeedsPreformatScanning && aNode.IsElement()) {
-    mSerializer->ForgetElementForPreformat(aNode.AsElement());
+  if (mNeedsPreformatScanning) {
+    if (aNode.IsElement()) {
+      mSerializer->ForgetElementForPreformat(aNode.AsElement());
+    } else if (aNode.IsText()) {
+      const nsCOMPtr<nsINode> parent = aNode.GetParent();
+      if (parent && parent->IsElement()) {
+        mSerializer->ForgetElementForPreformat(parent->AsElement());
+      }
+    }
   }
 
   if (!IsVisibleNode(&aNode)) {
     return NS_OK;
   }
 
   if (aNode.IsElement()) {
     mSerializer->AppendElementEnd(aNode.AsElement(), aStr);
--- a/dom/base/test/test_user_select.html
+++ b/dom/base/test/test_user_select.html
@@ -288,27 +288,27 @@ function test()
   checkText("aaaa bbbb", e);
   checkRanges([[0,0,-1,1],[6,0,6,5]], e);
   doneTest(e);
 
   clear();
   e = document.getElementById('testG');
   synthesizeMouse(e, 1, 1, {});
   synthesizeMouse(e, 400, 180, { shiftKey: true });
-  checkText("aaaa bbbb", e); // XXX this doesn't seem right - bug 1247799
+  checkText("aaaa\n\n\n\nbbbb", e);
   checkRanges([[0,0,-1,1],[2,0,-1,3],[4,0,-1,5],[6,0,6,5]], e);
   doneTest(e);
 
   clear();
   e = document.getElementById('testH');
   synthesizeMouse(e, 1, 1, {});
   synthesizeMouse(e, 30, 90, { shiftKey: true });
   synthesizeMouse(e, 50, 90, { shiftKey: true });
   synthesizeMouse(e, 70, 90, { shiftKey: true });
-  checkText("aaaa bbb", e);
+  checkText("aaaa\n\nbbb", e);
   checkRanges([[0,0,-1,1],[-1,2,3,4]], e);
 
   doneTest(e);
   // ======================================================
   // ==================== Script tests ====================
   // ======================================================
 
   clear();