[evolution/wip/mcrha/webkit-jsc-api: 221/292] Cover Undo/Redo when document body is completely empty, with no sub-elements



commit 1bcc8dd98758797e61a955daea81d81732a29c40
Author: Milan Crha <mcrha redhat com>
Date:   Tue Nov 5 17:20:41 2019 +0100

    Cover Undo/Redo when document body is completely empty, with no sub-elements

 data/webkit/e-editor.js    |  5 +++-
 data/webkit/e-undo-redo.js | 58 ++++++++++++++++++++++++++++------------------
 2 files changed, 39 insertions(+), 24 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 926d79ce49..ab523c89e0 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -518,7 +518,10 @@ EvoEditor.ClaimAffectedContent = function(startNode, endNode, flags)
        affected.restChildrenCount = commonParent.children.length - ii;
 
        if (withHtml) {
-               affected.html = html;
+               if (firstChildIndex == -1)
+                       affected.html = commonParent.innerHTML;
+               else
+                       affected.html = html;
        }
 
        return affected;
diff --git a/data/webkit/e-undo-redo.js b/data/webkit/e-undo-redo.js
index 701df0cb06..952004b342 100644
--- a/data/webkit/e-undo-redo.js
+++ b/data/webkit/e-undo-redo.js
@@ -463,7 +463,7 @@ EvoUndoRedo.applyRecord = function(record, isUndo, withSelection)
                        first = record.firstChildIndex;
 
                        // it can equal to the children.length, when the node had been removed
-                       if (first < 0 || first > commonParent.children.length) {
+                       if (first < -1 || first > commonParent.children.length) {
                                throw "EvoUndoRedo::applyRecord: firstChildIndex (" + first + ") out of 
bounds (" + commonParent.children.length + ")";
                        }
 
@@ -473,29 +473,37 @@ EvoUndoRedo.applyRecord = function(record, isUndo, withSelection)
                                        commonParent.children.length + " first:" + first + " last:" + last + 
")";
                        }
 
-                       for (ii = last - 1; ii >= first; ii--) {
-                               if (ii >= 0 && ii < commonParent.children.length) {
-                                       commonParent.removeChild(commonParent.children.item(ii));
+                       if (first == -1) {
+                               if (isUndo) {
+                                       commonParent.innerHTML = record.htmlBefore;
+                               } else {
+                                       commonParent.innerHTML = record.htmlAfter;
                                }
-                       }
-
-                       var tmpNode = document.createElement("evo-tmp");
-
-                       if (isUndo) {
-                               tmpNode.innerHTML = record.htmlBefore;
                        } else {
-                               tmpNode.innerHTML = record.htmlAfter;
-                       }
+                               for (ii = last - 1; ii >= first; ii--) {
+                                       if (ii >= 0 && ii < commonParent.children.length) {
+                                               commonParent.removeChild(commonParent.children.item(ii));
+                                       }
+                               }
 
-                       if (first < commonParent.children.length) {
-                               first = commonParent.children.item(first);
+                               var tmpNode = document.createElement("evo-tmp");
 
-                               while(tmpNode.firstElementChild) {
-                                       commonParent.insertBefore(tmpNode.firstElementChild, first);
+                               if (isUndo) {
+                                       tmpNode.innerHTML = record.htmlBefore;
+                               } else {
+                                       tmpNode.innerHTML = record.htmlAfter;
                                }
-                       } else {
-                               while(tmpNode.children.length) {
-                                       commonParent.appendChild(tmpNode.children.item(0));
+
+                               if (first < commonParent.children.length) {
+                                       first = commonParent.children.item(first);
+
+                                       while(tmpNode.firstElementChild) {
+                                               commonParent.insertBefore(tmpNode.firstElementChild, first);
+                                       }
+                               } else {
+                                       while(tmpNode.children.length) {
+                                               commonParent.appendChild(tmpNode.children.item(0));
+                                       }
                                }
                        }
                }
@@ -582,7 +590,7 @@ EvoUndoRedo.StopRecord = function(kind, opType)
                first = record.firstChildIndex;
 
                // it can equal to the children.length, when the node had been removed
-               if (first < 0 || first > commonParent.children.length) {
+               if (first < -1 || first > commonParent.children.length) {
                        throw "EvoUndoRedo::StopRecord: firstChildIndex (" + first + ") out of bounds (" + 
commonParent.children.length + ")";
                }
 
@@ -592,9 +600,13 @@ EvoUndoRedo.StopRecord = function(kind, opType)
                                commonParent.children.length + " first:" + first + " last:" + last + ")";
                }
 
-               for (ii = first; ii < last; ii++) {
-                       if (ii >= 0 && ii < commonParent.children.length) {
-                               html += commonParent.children.item(ii).outerHTML;
+               if (first == -1) {
+                       html = commonParent.innerHTML;
+               } else {
+                       for (ii = first; ii < last; ii++) {
+                               if (ii >= 0 && ii < commonParent.children.length) {
+                                       html += commonParent.children.item(ii).outerHTML;
+                               }
                        }
                }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]