[evolution/wip/mcrha/webkit-jsc-api: 233/292] e-editor.js: Use the further node for style computation



commit cc6dfbb5c2af5be18407f0379ca76a53bf115393
Author: Milan Crha <mcrha redhat com>
Date:   Tue Nov 12 15:49:20 2019 +0100

    e-editor.js: Use the further node for style computation
    
    The starting node can be out of the actual format being used in
    the whole selection. The selection can start from left to right,
    but also from right to left, thus one needs to calculate, which
    node is the more further node.

 data/webkit/e-editor.js | 44 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 6 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 2d899094fe..6625a9fe2f 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -79,9 +79,26 @@ var EvoEditor = {
 
 EvoEditor.maybeUpdateFormattingState = function(force)
 {
-       var baseElem;
+       var baseElem = null;
 
-       baseElem = document.getSelection().baseNode;
+       if (!document.getSelection().isCollapsed) {
+               var commonParent;
+
+               commonParent = EvoEditor.GetCommonParent(document.getSelection().baseNode, 
document.getSelection().extentNode, true);
+               if (commonParent) {
+                       var child1, child2;
+
+                       child1 = EvoEditor.GetDirectChild(commonParent, document.getSelection().baseNode);
+                       child2 = EvoEditor.GetDirectChild(commonParent, document.getSelection().extentNode);
+
+                       if (child1 && (!child2 || (child2 && EvoEditor.GetChildIndex(commonParent, child1) <= 
EvoEditor.GetChildIndex(commonParent, child2)))) {
+                               baseElem = document.getSelection().extentNode;
+                       }
+               }
+       }
+
+       if (!baseElem)
+               baseElem = document.getSelection().baseNode;
        if (!baseElem)
                baseElem = document.body ? document.body.firstElementChild : null;
 
@@ -415,7 +432,7 @@ EvoEditor.ForeachChild = function(parent, firstChildIndex, lastChildIndex, trave
        return EvoEditor.foreachChildRecur(parent, parent, firstChildIndex, lastChildIndex, traversar);
 }
 
-EvoEditor.GetCommonParent = function(firstNode, secondNode)
+EvoEditor.GetCommonParent = function(firstNode, secondNode, longPath)
 {
        if (!firstNode || !secondNode) {
                return null;
@@ -439,12 +456,12 @@ EvoEditor.GetCommonParent = function(firstNode, secondNode)
 
        var commonParent, secondParent;
 
-       for (commonParent = firstNode.parentElement; commonParent; commonParent = commonParent.parentElement) 
{
+       for (commonParent = (longPath ? firstNode : firstNode.parentElement); commonParent; commonParent = 
commonParent.parentElement) {
                if (commonParent === document.body) {
                        break;
                }
 
-               for (secondParent = secondNode.parentElement; secondParent; secondParent = 
secondParent.parentElement) {
+               for (secondParent = (longPath ? secondNode : secondNode.parentElement); secondParent; 
secondParent = secondParent.parentElement) {
                        if (secondParent === document.body) {
                                break;
                        }
@@ -471,6 +488,21 @@ EvoEditor.GetDirectChild = function(parent, child)
        return child;
 }
 
+EvoEditor.GetChildIndex = function(parent, child)
+{
+       if (!parent || !child)
+               return -1;
+
+       var ii;
+
+       for (ii = 0; ii < parent.children.length; ii++) {
+               if (child === parent.children.item(ii))
+                       return ii;
+       }
+
+       return -1;
+}
+
 EvoEditor.ClaimAffectedContent = function(startNode, endNode, flags)
 {
        var commonParent, startChild, endChild;
@@ -503,7 +535,7 @@ EvoEditor.ClaimAffectedContent = function(startNode, endNode, flags)
                }
        }
 
-       commonParent = EvoEditor.GetCommonParent(startNode, endNode);
+       commonParent = EvoEditor.GetCommonParent(startNode, endNode, false);
        startChild = EvoEditor.GetDirectChild(commonParent, startNode);
        endChild = EvoEditor.GetDirectChild(commonParent, endNode);
 


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