[evolution/wip/mcrha/webkit-jsc-api] Introduce Evo.foreachIFrameDocument helper function



commit 6923271ae29dabc94e566abd768ae0850b9bd861
Author: Milan Crha <mcrha redhat com>
Date:   Wed Oct 2 18:03:33 2019 +0200

    Introduce Evo.foreachIFrameDocument helper function

 src/web-extensions/ext-utils.js | 257 ++++++++++++++++++++--------------------
 1 file changed, 126 insertions(+), 131 deletions(-)
---
diff --git a/src/web-extensions/ext-utils.js b/src/web-extensions/ext-utils.js
index de226c4d8d..ce44f9c02a 100644
--- a/src/web-extensions/ext-utils.js
+++ b/src/web-extensions/ext-utils.js
@@ -4,26 +4,52 @@ var Evo = {
        hasSelection : false
 };
 
-Evo.findIFrameInDocument = function(doc, iframe_id)
+/* The 'traversar_obj' is an object, which implements a callback function:
+   boolean exec(doc, iframe_id);
+   and it returns whether continue the traversar */
+Evo.foreachIFrameDocument = function(doc, traversar_obj, call_also_for_doc)
 {
        if (!doc)
-               return null;
+               return false;
 
-       var iframes, ii, res;
+       if (call_also_for_doc && !traversar_obj.exec(doc, doc.defaultView.frameElement ? 
doc.defaultView.frameElement.id : ""))
+               return false;
+
+       var iframes, ii;
 
        iframes = doc.getElementsByTagName("iframe");
 
        for (ii = 0; ii < iframes.length; ii++) {
-               if (iframes[ii].id == iframe_id)
-                       return iframes[ii];
-
-               res = Evo.findIFrameInDocument(iframes[ii].contentDocument, iframe_id);
+               if (!traversar_obj.exec(iframes[ii].contentDocument, iframes[ii].id))
+                       return false;
 
-               if (res)
-                       return res;
+               if (!Evo.foreachIFrameDocument(iframes[ii].contentDocument, traversar_obj, false))
+                       return false;
        }
 
-       return null;
+       return true;
+}
+
+Evo.findIFrameInDocument = function(doc, iframe_id)
+{
+       if (!doc)
+               return null;
+
+       var traversar = {
+               iframe_id : iframe_id,
+               iframe : null,
+               exec : function(doc, iframe_id) {
+                       if (iframe_id == this.iframe_id) {
+                               this.iframe = doc.defaultView.frameElement;
+                               return false;
+                       }
+                       return true;
+               }
+       };
+
+       Evo.foreachIFrameDocument(doc, traversar, true);
+
+       return traversar.iframe;
 }
 
 Evo.findIFrame = function(iframe_id)
@@ -61,28 +87,6 @@ Evo.findElementInDocumentById = function(doc, element_id)
        return doc.getElementById(element_id);
 }
 
-Evo.findElementInDocument = function(doc, element_id)
-{
-       var elem, iframes, ii;
-
-       if (!doc)
-               return null;
-
-       elem = Evo.findElementInDocumentById(doc, element_id);
-       if (elem)
-               return elem;
-
-       iframes = doc.getElementsByTagName("iframe");
-
-       for (ii = 0; ii < iframes.length; ii++) {
-               elem = Evo.findElementInDocument(iframes[ii].contentDocument, element_id);
-               if (elem)
-                       return elem;
-       }
-
-       return null;
-}
-
 Evo.findElement = function(iframe_id, element_id)
 {
        var iframe;
@@ -91,7 +95,20 @@ Evo.findElement = function(iframe_id, element_id)
                return Evo.findElementInDocumentById(document, element_id);
 
        if (iframe_id == "*") {
-               return Evo.findElementInDocument(document, element_id);
+               var traversar = {
+                       element_id : element_id,
+                       res : null,
+                       exec : function(doc, iframe_id) {
+                               this.res = Evo.findElementInDocumentById(doc, element_id);
+                               if (this.res)
+                                       return false;
+                               return true;
+                       }
+               };
+
+               Evo.foreachIFrameDocument(document, traversar, true);
+
+               return traversar.res;
        }
 
        iframe = Evo.findIFrame(iframe_id);
@@ -163,42 +180,29 @@ Evo.CreateStyleSheet = function(iframe_id, style_sheet_id, content)
        Evo.createStyleSheet(doc, style_sheet_id, content);
 }
 
-Evo.removeStyleSheetInDocument = function(doc, style_sheet_id)
+Evo.RemoveStyleSheet = function(iframe_id, style_sheet_id)
 {
-       if (!doc || !doc.head)
-               return;
-
-       var styles = doc.head.getElementsByTagName("style"), ii;
+       var traversar = {
+               style_sheet_id : style_sheet_id,
+               exec : function(doc, iframe_id) {
+                       if (doc && doc.head) {
+                               var ii, styles = doc.head.getElementsByTagName("style");
+
+                               for (ii = styles.length - 1; ii >= 0; ii--) {
+                                       if (this.style_sheet_id == "*" || styles[ii].id == 
this.style_sheet_id) {
+                                               doc.head.removeChild(styles[ii]);
+                                       }
+                               }
+                       }
 
-       for (ii = styles.length - 1; ii >= 0; ii--) {
-               if (style_sheet_id == "*" || styles[ii].id == style_sheet_id) {
-                       doc.head.removeChild(styles[ii]);
+                       return true;
                }
-       }
-}
+       };
 
-Evo.removeStyleSheetInDocumentRecursive = function(doc, style_sheet_id)
-{
-       if (!doc)
-               return;
-
-       Evo.removeStyleSheetInDocument(doc, style_sheet_id);
-
-       var iframes, ii;
-
-       iframes = doc.getElementsByTagName("iframe");
-
-       for (ii = 0; ii < iframes.length; ii++) {
-               Evo.removeStyleSheetInDocumentRecursive(iframes[ii].contentDocument, style_sheet_id);
-       }
-}
-
-Evo.RemoveStyleSheet = function(iframe_id, style_sheet_id)
-{
        if (iframe_id == "*") {
-               Evo.removeStyleSheetInDocumentRecursive(document, style_sheet_id);
+               Evo.foreachIFrameDocument(document, traversar, true);
        } else {
-               Evo.removeStyleSheetInDocument(Evo.findIFrameDocument(iframe_id), style_sheet_id);
+               traversar.exec(Evo.findIFrameDocument(iframe_id), iframe_id);
        }
 }
 
@@ -228,27 +232,25 @@ Evo.addRuleIntoStyleSheetDocument = function(doc, style_sheet_id, selector, styl
        styleobj.sheet.addRule(selector, style);
 }
 
-Evo.addRuleIntoStyleSheetDocumentRecursive = function(doc, style_sheet_id, selector, style)
+Evo.AddRuleIntoStyleSheet = function(iframe_id, style_sheet_id, selector, style)
 {
-       Evo.addRuleIntoStyleSheetDocument(doc, style_sheet_id, selector, style);
-
-       var iframes, ii;
-
-       iframes = doc.getElementsByTagName("iframe");
+       var traversar = {
+               style_sheet_id : style_sheet_id,
+               selector : selector,
+               style : style,
+               exec : function(doc, iframe_id) {
+                       if (doc && doc.head) {
+                               Evo.addRuleIntoStyleSheetDocument(doc, this.style_sheet_id, this.selector, 
this.style);
+                       }
 
-       for (ii = 0; ii < iframes.length; ii++) {
-               Evo.addRuleIntoStyleSheetDocumentRecursive(iframes[ii].contentDocument, style_sheet_id, 
selector, style);
-       }
-}
+                       return true;
+               }
+       };
 
-Evo.AddRuleIntoStyleSheet = function(iframe_id, style_sheet_id, selector, style)
-{
        if (iframe_id == "*") {
-               Evo.addRuleIntoStyleSheetDocumentRecursive(document, style_sheet_id, selector, style);
+               Evo.foreachIFrameDocument(document, traversar, true);
        } else {
-               var doc = Evo.findIFrameDocument(iframe_id);
-
-               Evo.addRuleIntoStyleSheetDocument(doc, style_sheet_id, selector, style);
+               traversar.exec(Evo.findIFrameDocument(iframe_id), iframe_id);
        }
 }
 
@@ -334,47 +336,35 @@ Evo.ElemClicked = function(elem)
        window.webkit.messageHandlers.elementClicked.postMessage(res);
 }
 
-Evo.registerElementClickedInDocument = function(doc, elem_classes)
+Evo.RegisterElementClicked = function(iframe_id, elem_classes_str)
 {
-       var ii;
+       var traversar = {
+               elem_classes : elem_classes_str.split("\n"),
+               exec : function(doc, iframe_id) {
+                       if (doc && this.elem_classes) {
+                               var ii;
 
-       for (ii = 0; ii < elem_classes.length; ii++) {
-               if (elem_classes[ii] != "") {
-                       var jj, elems;
+                               for (ii = 0; ii < this.elem_classes.length; ii++) {
+                                       if (this.elem_classes[ii] != "") {
+                                               var jj, elems;
 
-                       elems = doc.getElementsByClassName(elem_classes[ii]);
+                                               elems = doc.getElementsByClassName(this.elem_classes[ii]);
 
-                       for (jj = 0; jj < elems.length; jj++) {
-                               elems[jj].onclick = function() { Evo.ElemClicked(this); };
+                                               for (jj = 0; jj < elems.length; jj++) {
+                                                       elems[jj].onclick = function() { 
Evo.ElemClicked(this); };
+                                               }
+                                       }
+                               }
                        }
-               }
-       }
-}
-
-Evo.registerElementClickedInDocumentRecursive = function(doc, elem_classes)
-{
-       Evo.registerElementClickedInDocument(doc, elem_classes);
 
-       var iframes, ii;
-
-       iframes = doc.getElementsByTagName("iframe");
-
-       for (ii = 0; ii < iframes.length; ii++) {
-               Evo.registerElementClickedInDocumentRecursive(iframes[ii].contentDocument, elem_classes);
-       }
-}
-
-Evo.RegisterElementClicked = function(iframe_id, elem_classes_str)
-{
-       var elem_classes = elem_classes_str.split("\n");
+                       return true;
+               }
+       };
 
        if (iframe_id == "*") {
-               Evo.registerElementClickedInDocumentRecursive(document, elem_classes);
+               Evo.foreachIFrameDocument(document, traversar, true);
        } else {
-               var doc;
-
-               doc = Evo.findIFrameDocument(iframe_id);
-               Evo.registerElementClickedInDocument(doc, elem_classes);
+               traversar.exec(Evo.findIFrameDocument(iframe_id), iframe_id);
        }
 }
 
@@ -419,39 +409,44 @@ Evo.getElementContent = function(node, format, useOuterHTML)
        return data;
 }
 
-Evo.checkHasSelectionRecursive = function(doc, content)
+Evo.checkHasSelection = function(content)
 {
-       var has = false, iframes, ii;
+       var traversar = {
+               content : content,
+               has : false,
+               exec : function(doc, iframe_id) {
+                       if (doc && !doc.defaultView.getSelection().isCollapsed) {
+                               if (content) {
+                                       var fragment, node, inpre;
 
-       if (!doc.defaultView.getSelection().isCollapsed) {
-               if (content) {
-                       var fragment, node, inpre;
+                                       fragment = 
doc.defaultView.getSelection().getRangeAt(0).cloneContents();
+                                       inpre = 
Evo.checkAnyParentIsPre(doc.defaultView.getSelection().getRangeAt(0).startContainer);
+                                       node = doc.createElement(inpre ? "PRE" : "DIV");
+                                       node.appendChild(fragment);
 
-                       fragment = doc.defaultView.getSelection().getRangeAt(0).cloneContents();
-                       inpre = 
Evo.checkAnyParentIsPre(doc.defaultView.getSelection().getRangeAt(0).startContainer);
-                       node = doc.createElement(inpre ? "PRE" : "DIV");
-                       node.appendChild(fragment);
+                                       content.data = Evo.getElementContent(node, content.format, inpre);
+                               }
 
-                       content.data = Evo.getElementContent(node, content.format, inpre);
-               }
+                               this.has = true;
 
-               return true;
-       }
+                               return false;
+                       }
 
-       iframes = doc.getElementsByTagName("iframe");
+                       return true;
+               }
+       };
 
-       for (ii = 0; !has && ii < iframes.length; ii++) {
-               has = Evo.checkHasSelectionRecursive(iframes[ii].contentDocument, content);
-       }
+       Evo.foreachIFrameDocument(document, traversar, true);
 
-       return has;
+       return traversar.has;
 }
 
 Evo.selectionChanged = function()
 {
+
        var has;
 
-       has = Evo.checkHasSelectionRecursive(document, null);
+       has = Evo.checkHasSelection(null);
 
        if (has != Evo.hasSelection) {
                Evo.hasSelection = has;
@@ -465,7 +460,7 @@ Evo.GetSelection = function(format)
 
        content.format = format;
 
-       if (!Evo.checkHasSelectionRecursive(document, content))
+       if (!Evo.checkHasSelection(content))
                return null;
 
        return content.data;


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