[evolution/wip/mcrha/webkit-jsc-api] test-html-editor: Adapt the rest unit tests to changes from other unit tests



commit 0e72057a670ef641cf2974294538e86429891b29
Author: Milan Crha <mcrha redhat com>
Date:   Tue Apr 7 16:56:16 2020 +0200

    test-html-editor: Adapt the rest unit tests to changes from other unit tests
    
    Two tests are still failing, to be done next.

 data/webkit/e-editor.js                  | 263 +++++++++++++++++--------------
 data/webkit/e-undo-redo.js               |   5 +
 src/e-util/test-html-editor-units-bugs.c |  14 +-
 3 files changed, 157 insertions(+), 125 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index ec489fa0bf..26ae75b62f 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -1562,90 +1562,77 @@ EvoEditor.applyDivNormalize = function(record, isUndo)
        }
 }
 
-EvoEditor.InsertHTML = function(opType, html)
+EvoEditor.correctParagraphsAfterInsertContent = function(opType)
 {
-       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_GROUP, opType);
-       try {
-               document.execCommand("insertHTML", false, html);
+       var node, list, ii;
 
-               var node, list, ii;
-
-               list = document.body.querySelectorAll("SPAN[id=-x-evo-insert-content-wrapper]");
-
-               for (ii = list.length - 1; ii >= 0; ii--) {
-                       node = list[ii];
+       list = document.body.getElementsByTagName("DIV");
 
-                       var parentBlock = EvoEditor.GetParentBlockNode(node);
+       for (ii = 0; ii < list.length; ii++) {
+               node = list[ii];
 
-                       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::removeContentWrapper", parentBlock, parentBlock, EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML);
-                       try {
-                               var child = EvoEditor.GetDirectChild(parentBlock, node);
+               if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
+                       var beforeValue = node.style.width;
+                       EvoEditor.maybeUpdateParagraphWidth(node);
 
-                               while (node.firstChild) {
-                                       if (node.firstChild.tagName == "BLOCKQUOTE") {
-                                               if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
-                                                       node.firstChild.removeAttribute("class");
-                                                       node.firstChild.removeAttribute("style");
-                                               } else {
-                                                       node.firstChild.removeAttribute("class");
-                                                       node.firstChild.setAttribute("style", 
EvoEditor.BLOCKQUOTE_STYLE);
-                                               }
+                       if (node.style.width != beforeValue) {
+                               var record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::divWidths", node, node, EvoEditor.CLAIM_CONTENT_FLAG_NONE);
+                               try {
+                                       if (record) {
+                                               record.path = EvoSelection.GetChildPath(document.body, node);
+                                               record.beforeValue = beforeValue;
+                                               record.afterValue = node.style.width;
+                                               record.isWidthStyle = true;
+                                               record.apply = EvoEditor.applyDivNormalize;
                                        }
-
-                                       parentBlock.insertBefore(node.firstChild, child);
+                               } finally {
+                                       EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::divWidths");
                                }
+                       }
+               }
 
-                               node.parentElement.removeChild(node);
-
-                               if (!(child === node) && !child.firstChild)
-                                       parentBlock.removeChild(child);
+               if (!node.firstChild) {
+                       var record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::divBR", node, node, EvoEditor.CLAIM_CONTENT_FLAG_NONE);
+                       try {
+                               beforeValue = node.innerHTML;
+                               node.appendChild(document.createElement("BR"));
+
+                               if (record) {
+                                       record.path = EvoSelection.GetChildPath(document.body, node);
+                                       record.beforeValue = beforeValue;
+                                       record.afterValue = node.innerHTML;
+                                       record.apply = EvoEditor.applyDivNormalize;
+                               }
                        } finally {
-                               EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::removeContentWrapper");
+                               EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + "::divBR");
                        }
                }
+       }
+}
 
-               list = document.body.getElementsByTagName("DIV");
+EvoEditor.InsertHTML = function(opType, html)
+{
+       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_GROUP, opType);
+       try {
+               document.execCommand("insertHTML", false, html);
 
-               for (ii = 0; ii < list.length; ii++) {
-                       node = list[ii];
+               var node, list, ii;
 
-                       if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
-                               var beforeValue = node.style.width;
-                               EvoEditor.maybeUpdateParagraphWidth(node);
+               list = document.body.getElementsByTagName("BLOCKQUOTE");
 
-                               if (node.style.width != beforeValue) {
-                                       var record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, 
opType + "::divWidths", node, node, EvoEditor.CLAIM_CONTENT_FLAG_NONE);
-                                       try {
-                                               if (record) {
-                                                       record.path = 
EvoSelection.GetChildPath(document.body, node);
-                                                       record.beforeValue = beforeValue;
-                                                       record.afterValue = node.style.width;
-                                                       record.isWidthStyle = true;
-                                                       record.apply = EvoEditor.applyDivNormalize;
-                                               }
-                                       } finally {
-                                               EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType 
+ "::divWidths");
-                                       }
-                               }
-                       }
+               for (ii = 0; ii < list.length; ii++) {
+                       node = list[ii];
 
-                       if (!node.firstChild) {
-                               var record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::divBR", node, node, EvoEditor.CLAIM_CONTENT_FLAG_NONE);
-                               try {
-                                       beforeValue = node.innerHTML;
-                                       node.appendChild(document.createElement("BR"));
+                       EvoEditor.setAttributeWithUndoRedo("InsertHTML::fixBlockquote", node, "class", null);
 
-                                       if (record) {
-                                               record.path = EvoSelection.GetChildPath(document.body, node);
-                                               record.beforeValue = beforeValue;
-                                               record.afterValue = node.innerHTML;
-                                               record.apply = EvoEditor.applyDivNormalize;
-                                       }
-                               } finally {
-                                       EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opType + 
"::divBR");
-                               }
+                       if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
+                               EvoEditor.setAttributeWithUndoRedo("InsertHTML::fixBlockquote", node, 
"style", null);
+                       } else {
+                               EvoEditor.setAttributeWithUndoRedo("InsertHTML::fixBlockquote", node, 
"style", EvoEditor.BLOCKQUOTE_STYLE);
                        }
                }
+
+               EvoEditor.correctParagraphsAfterInsertContent(opType);
        } finally {
                EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_GROUP, opType);
                EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_MAYBE);
@@ -1665,44 +1652,9 @@ EvoEditor.InsertText = function(opType, text)
        }
 }
 
-EvoEditor.applySetBodyAttribute = function(record, isUndo)
-{
-       if (isUndo) {
-               if (record.beforeValue)
-                       document.body.setAttribute(record.attrName, record.beforeValue);
-               else
-                       document.body.removeAttribute(record.attrName);
-       } else {
-               if (record.attrValue)
-                       document.body.setAttribute(record.attrName, record.attrValue);
-               else
-                       document.body.removeAttribute(record.attrName);
-       }
-}
-
 EvoEditor.SetBodyAttribute = function(name, value)
 {
-       var record;
-
-       record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "setBodyAttribute::" + name, 
document.body, document.body, EvoEditor.CLAIM_CONTENT_FLAG_NONE);
-
-       try {
-               if (record) {
-                       record.attrName = name;
-                       record.attrValue = value;
-                       record.beforeValue = document.body.getAttribute(name);
-                       record.apply = EvoEditor.applySetBodyAttribute;
-               }
-
-               if (value)
-                       document.body.setAttribute(name, value);
-               else
-                       document.body.removeAttribute(name);
-       } finally {
-               EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "setBodyAttribute::" + name);
-               EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_MAYBE);
-               EvoEditor.EmitContentChanged();
-       }
+       EvoEditor.setAttributeWithUndoRedo("SetBodyAttribute", document.body, name, value);
 }
 
 EvoEditor.applySetBodyFontName = function(record, isUndo)
@@ -3775,8 +3727,11 @@ EvoEditor.setAttributeWithUndoRedo = function(opTypePrefix, element, name, value
                }
        } finally {
                EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, opTypePrefix + "::" + name);
-               EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_MAYBE);
-               EvoEditor.EmitContentChanged();
+
+               if (!EvoUndoRedo.IsRecording()) {
+                       EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_MAYBE);
+                       EvoEditor.EmitContentChanged();
+               }
        }
 
        return true;
@@ -4905,6 +4860,14 @@ EvoEditor.InsertSignature = function(content, isHTML, uid, fromMessage, checkCha
        return res;
 }
 
+EvoEditor.isEmptyParagraph = function(node)
+{
+       if (!node || !EvoEditor.IsBlockNode(node))
+               return false;
+
+       return !node.children.length || (node.children.length == 1 && node.children[0].tagName == "BR");
+}
+
 // the body contains data, which should be converted into editable content;
 // preferredPlainText can be empty, if not, then it replaces body content
 EvoEditor.ConvertContent = function(preferredPlainText, startBottom, topSignature)
@@ -4926,21 +4889,14 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                        }
                }
 
-               var wrapperNode, content;
-
-               wrapperNode = document.createElement("SPAN");
-               wrapperNode.id = "-x-evo-insert-content-wrapper";
+               var wasPlain = !isHTML;
+               var content = document.createElement(quote ? "BLOCKQUOTE" : "DIV");
 
                if (quote) {
-                       content = document.createElement("BLOCKQUOTE");
                        content.setAttribute("type", "cite");
 
-                       wrapperNode.appendChild(content);
-
                        if (EvoEditor.mode != EvoEditor.MODE_PLAIN_TEXT)
                                content.setAttribute("style", EvoEditor.BLOCKQUOTE_STYLE);
-               } else {
-                       content = wrapperNode;
                }
 
                if (isHTML) {
@@ -4991,13 +4947,14 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                } else {
                        var lines = text.split("\n");
 
-                       if (lines.length == 1) {
-                               content.innerText = text;
+                       if (lines.length == 1 || (lines.length == 2 && !lines[1])) {
+                               content.innerText = lines[0];
                        } else {
-                               var ii;
+                               var ii, line, divNode;
 
                                for (ii = 0; ii < lines.length; ii++) {
-                                       var line = lines[ii], divNode = document.createElement("DIV");
+                                       line = lines[ii];
+                                       divNode = document.createElement("DIV");
 
                                        content.appendChild(divNode);
 
@@ -5007,6 +4964,8 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                                                divNode.innerText = line;
                                        }
                                }
+
+                               isHTML = true;
                        }
                }
 
@@ -5125,15 +5084,13 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                                                        document.getSelection().setPosition(content, 0);
 
                                                if (anchorNode.nodeType == anchorNode.ELEMENT_NODE && 
anchorNode.parentElement &&
-                                                   (anchorNode.tagName == "DIV" || anchorNode.tagName == "P" 
|| anchorNode.tagName == "PRE") &&
-                                                   (!anchorNode.children.length || 
(anchorNode.children.length == 1 && anchorNode.children[0].tagName == "BR"))) {
+                                                   EvoEditor.isEmptyParagraph(anchorNode)) {
                                                        anchorNode.parentElement.removeChild(anchorNode);
                                                } else {
                                                        anchorNode = parentBlock.nextSibling.nextSibling;
 
                                                        if (anchorNode.nodeType == anchorNode.ELEMENT_NODE && 
anchorNode.parentElement &&
-                                                           (anchorNode.tagName == "DIV" || 
anchorNode.tagName == "P" || anchorNode.tagName == "PRE") &&
-                                                           (!anchorNode.children.length || 
(anchorNode.children.length == 1 && anchorNode.children[0].tagName == "BR"))) {
+                                                           EvoEditor.isEmptyParagraph(anchorNode)) {
                                                                
anchorNode.parentElement.removeChild(anchorNode);
                                                        }
                                                }
@@ -5155,8 +5112,72 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                                        EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, 
"InsertContent::text");
                                }
                        }
+
+                       if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
+                               var ii;
+
+                               for (ii = 0; ii < content.children.length; ii++) {
+                                       EvoEditor.requoteNodeParagraph(content.children[ii]);
+                               }
+                       }
                } else if (isHTML) {
-                       EvoEditor.InsertHTML("InsertContent::text", wrapperNode.outerHTML);
+                       var list, ii;
+
+                       list = content.getElementsByTagName("BLOCKQUOTE");
+
+                       for (ii = 0; ii < list.length; ii++) {
+                               var node = list[ii];
+
+                               if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
+                                       node.removeAttribute("class");
+                                       node.removeAttribute("style");
+                               } else {
+                                       node.removeAttribute("class");
+                                       node.setAttribute("style", EvoEditor.BLOCKQUOTE_STYLE);
+                               }
+                       }
+
+                       var selection = document.getSelection();
+
+                       var useOuterHTML = !list.length &&
+                               !content.getElementsByTagName("DIV").length &&
+                               !content.getElementsByTagName("PRE").length;
+
+                       if (!useOuterHTML && selection.isCollapsed && selection.focusNode && 
EvoEditor.isEmptyParagraph(selection.focusNode)) {
+                               var node = selection.focusNode, lastNode = null;
+
+                               EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, 
"InsertContent::replaceEmptyBlock", node, node,
+                                       EvoEditor.CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE | 
EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML);
+                               try {
+                                       if (useOuterHTML) {
+                                               lastNode = content;
+                                               node.parentElement.insertBefore(content, node);
+                                       } else {
+                                               while (content.firstChild) {
+                                                       lastNode = content.firstChild;
+                                                       node.parentElement.insertBefore(content.firstChild, 
node);
+                                               }
+                                       }
+
+                                       node.remove();
+
+                                       if (lastNode) {
+                                               while (lastNode.lastChild) {
+                                                       lastNode = lastNode.lastChild;
+                                               }
+
+                                               selection.setPosition(lastNode, lastNode.nodeType == 
lastNode.TEXT_NODE ? lastNode.nodeValue.length : 0);
+                                       }
+                               } finally {
+                                       EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, 
"InsertContent::replaceEmptyBlock");
+                               }
+
+                               EvoEditor.correctParagraphsAfterInsertContent("InsertContent::inEmptyBlock");
+                       } else {
+                               useOuterHTML = useOuterHTML && !wasPlain;
+
+                               EvoEditor.InsertHTML("InsertContent::text", useOuterHTML ? content.outerHTML 
: content.innerHTML);
+                       }
                } else {
                        EvoEditor.InsertText("InsertContent::text", content.innerText);
                }
diff --git a/data/webkit/e-undo-redo.js b/data/webkit/e-undo-redo.js
index 6c8cf41834..396ae63f5b 100644
--- a/data/webkit/e-undo-redo.js
+++ b/data/webkit/e-undo-redo.js
@@ -695,6 +695,11 @@ EvoUndoRedo.StopRecord = function(kind, opType)
        return true;
 }
 
+EvoUndoRedo.IsRecording = function()
+{
+       return !EvoUndoRedo.disabled && EvoUndoRedo.ongoingRecordings.length > 0;
+}
+
 EvoUndoRedo.Undo = function()
 {
        var record = EvoUndoRedo.stack.undo();
diff --git a/src/e-util/test-html-editor-units-bugs.c b/src/e-util/test-html-editor-units-bugs.c
index aff33738d1..6b496805d9 100644
--- a/src/e-util/test-html-editor-units-bugs.c
+++ b/src/e-util/test-html-editor-units-bugs.c
@@ -797,8 +797,8 @@ test_bug_773164 (TestFixture *fixture)
                "undo:undo\n"
                "undo:test\n"
                "undo:redo\n"
-               "seq:huue\n" /* Go to the end of the first line */
-               "seq:Sds\n"
+               "seq:huuue\n" /* Go to the end of the second line */
+               "seq:Sddes\n"
                "action:cut\n"
                "seq:dde\n" /* Go to the end of the last line */
                "action:paste\n"
@@ -806,12 +806,18 @@ test_bug_773164 (TestFixture *fixture)
                "undo:test\n"
                "undo:redo:3\n",
                HTML_PREFIX "<div style=\"width: 71ch;\">This is paragraph 1</div>"
+               "<div style=\"width: 71ch;\"><br></div>"
                "<div style=\"width: 71ch;\">This is a longer paragraph 3</div>"
+               "<div style=\"width: 71ch;\"><br></div>"
                "<div style=\"width: 71ch;\">This is paragraph 2</div>"
+               "<div style=\"width: 71ch;\"><br></div>"
                HTML_SUFFIX,
                "This is paragraph 1\n"
+               "\n"
                "This is a longer paragraph 3\n"
-               "This is paragraph 2\n"))
+               "\n"
+               "This is paragraph 2\n"
+               "\n"))
                g_test_fail ();
 }
 
@@ -1205,7 +1211,7 @@ test_bug_780088 (TestFixture *fixture)
                "&lt;<a 
href=\"https://example.subdomain.org/p/user/\";>https://example.subdomain.org/p/user/</a>&gt; , "
                "click fjwvne on the left, click skjd sjewncj on the right, and set wqje wjfdn Xs to 
something like "
                "wqjfnm <a href=\"https://www.example.com/~user\";>www.example.com/~user</a> wjfdncj or 
such.</div>"
-               "</div><div style=\"width: 71ch;\"><br></div>"
+               "<div style=\"width: 71ch;\"><br></div>"
                HTML_SUFFIX,
                "Seeing @blah instead of @foo XX'ed\n"
                "on" UNICODE_NBSP "https://example.sub"; UNICODE_NBSP "domain.org/page I'd recommend to XX 
YY\n"


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