[Pkg-mozext-commits] [adblock-plus-element-hiding-helper] 18/28: Issue 2879 - Make "view source" command work again

David Prévot taffit at moszumanska.debian.org
Fri Aug 4 21:15:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus-element-hiding-helper.

commit f50b59ebbe33051edc980abdf6549961076f1e82
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Thu Dec 8 14:10:15 2016 +0100

    Issue 2879 - Make "view source" command work again
---
 lib/aardvark.js       | 170 ++++++++++++++++++++++++++------------------------
 lib/child/commands.js |  44 +++++++++++++
 2 files changed, 133 insertions(+), 81 deletions(-)

diff --git a/lib/aardvark.js b/lib/aardvark.js
index 0a24177..85bc6d9 100644
--- a/lib/aardvark.js
+++ b/lib/aardvark.js
@@ -15,6 +15,8 @@ let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"]
 // To be replaced when selection starts
 function E(id) {return null;}
 
+messageManager.addMessageListener("ElemHideHelper:Response",
+                                  messageResponse);
 messageManager.addMessageListener("ElemHideHelper:SelectionStarted",
                                   selectionStarted);
 messageManager.addMessageListener("ElemHideHelper:SelectionSucceeded",
@@ -23,6 +25,8 @@ messageManager.addMessageListener("ElemHideHelper:SelectionStopped",
                                   selectionStopped);
 onShutdown.add(() =>
 {
+  messageManager.removeMessageListener("ElemHideHelper:Response",
+                                       messageResponse);
   messageManager.removeMessageListener("ElemHideHelper:SelectionStarted",
                                        selectionStarted);
   messageManager.removeMessageListener("ElemHideHelper:SelectionSucceeded",
@@ -33,6 +37,28 @@ onShutdown.add(() =>
   selectionStopped();
 });
 
+let maxMessageId = 0;
+let messageCallbacks = new Map();
+
+function sendMessageWithResponse(messageName, data, callback)
+{
+  if (!data)
+    data = {};
+  data.messageId = ++maxMessageId;
+  messageCallbacks.set(data.messageId, callback);
+  messageManager.broadcastAsyncMessage(messageName, data);
+}
+
+function messageResponse(message)
+{
+  let callback = messageCallbacks.get(message.data.messageId);
+  if (callback)
+  {
+    messageCallbacks.delete(message.data.messageId);
+    callback(message.data);
+  }
+}
+
 function selectionStarted(message)
 {
   Aardvark.selectionStarted();
@@ -273,32 +299,33 @@ let Aardvark = exports.Aardvark =
     "showMenu"
   ],
 
-  viewSource: function(elem)
+  viewSource: function()
   {
-    if (!elem)
-      return false;
-
-    var sourceBox = E("ehh-viewsource");
+    let sourceBox = E("ehh-viewsource");
     if (sourceBox.state == "open")
     {
       sourceBox.hidePopup();
       return true;
     }
-    sourceBox.hidePopup();
-
-    while (sourceBox.firstElementChild)
-      sourceBox.removeChild(sourceBox.firstElementChild);
-    this.getOuterHtmlFormatted(elem, sourceBox);
 
-    let anchor = this.window.document.documentElement;
-    let x = this.mouseX;
-    let y = this.mouseY;
-    this.viewSourceTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-    this.viewSourceTimer.initWithCallback(function()
+    sendMessageWithResponse("ElemHideHelper:SerializeSelected", null, data =>
     {
-      sourceBox.showPopup(anchor, x, y, "tooltip", "topleft", "topleft");
-      Aardvark.viewSourceTimer = null;
-    }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
+      sourceBox.hidePopup();
+
+      while (sourceBox.firstElementChild)
+        sourceBox.removeChild(sourceBox.firstElementChild);
+      this.getOuterHtmlFormatted(data.serialized, sourceBox);
+
+      let anchor = this.window.document.documentElement;
+      let x = this.mouseX;
+      let y = this.mouseY;
+      this.viewSourceTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+      this.viewSourceTimer.initWithCallback(function()
+      {
+        sourceBox.showPopup(anchor, x, y, "tooltip", "topleft", "topleft");
+        Aardvark.viewSourceTimer = null;
+      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
+    });
     return true;
   },
 
@@ -338,82 +365,63 @@ let Aardvark = exports.Aardvark =
 
   getOuterHtmlFormatted: function(node, container)
   {
-    var type = null;
-    switch (node.nodeType)
+    let type = node.type;
+    if (type == "element")
     {
-      case node.ELEMENT_NODE:
-        var box = this.window.document.createElement("vbox");
-        box.className = "elementBox";
+      let box = this.window.document.createElement("vbox");
+      box.className = "elementBox";
 
-        var startTag = this.window.document.createElement("hbox");
-        startTag.className = "elementStartTag";
-        if (!node.firstChild)
-          startTag.className += " elementEndTag";
+      let startTag = this.window.document.createElement("hbox");
+      startTag.className = "elementStartTag";
+      if (!node.children.length)
+        startTag.className += " elementEndTag";
 
-        this.appendDescription(startTag, "<", null);
-        this.appendDescription(startTag, node.tagName, "tagName");
+      this.appendDescription(startTag, "<", null);
+      this.appendDescription(startTag, node.tagName, "tagName");
 
-        for (var i = 0; i < node.attributes.length; i++)
+      for (let {name, value} of node.attributes)
+      {
+        this.appendDescription(startTag, name, "attrName");
+        if (value != "")
         {
-          var attr = node.attributes[i];
-          this.appendDescription(startTag, attr.name, "attrName");
-          if (attr.value != "")
-          {
-            this.appendDescription(startTag, "=", null);
-            this.appendDescription(startTag, '"' + attr.value.replace(/"/, """) + '"', "attrValue");
-          }
+          this.appendDescription(startTag, "=", null);
+          this.appendDescription(startTag, `"${value.replace(/"/, """)}"`,
+                                 "attrValue");
         }
-
-        this.appendDescription(startTag, node.firstChild ? ">" : " />", null);
-        box.appendChild(startTag);
-
-        if (node.firstChild)
-        {
-          for (var child = node.firstChild; child; child = child.nextSibling)
-            this.getOuterHtmlFormatted(child, box);
-
-          var endTag = this.window.document.createElement("hbox");
-          endTag.className = "elementEndTag";
-          this.appendDescription(endTag, "<", null);
-          this.appendDescription(endTag, "/" + node.tagName, "tagName");
-          this.appendDescription(endTag, ">", null);
-          box.appendChild(endTag);
-        }
-        container.appendChild(box);
-        return;
-
-      case node.TEXT_NODE:
-        type = "text";
-        break;
-      case node.CDATA_SECTION_NODE:
-        type = "cdata";
-        break;
-      case node.COMMENT_NODE:
-        type = "comment";
-        break;
-      default:
-        return;
+      }
+
+      this.appendDescription(startTag, node.children.length ? ">" : " />", null);
+      box.appendChild(startTag);
+
+      if (node.children.length)
+      {
+        for (let child of node.children)
+          this.getOuterHtmlFormatted(child, box);
+
+        let endTag = this.window.document.createElement("hbox");
+        endTag.className = "elementEndTag";
+        this.appendDescription(endTag, "<", null);
+        this.appendDescription(endTag, "/" + node.tagName, "tagName");
+        this.appendDescription(endTag, ">", null);
+        box.appendChild(endTag);
+      }
+      container.appendChild(box);
+      return;
     }
 
-    var text = node.nodeValue.replace(/\r/g, '').replace(/^\s+/, '').replace(/\s+$/, '');
+    let text = node.text.replace(/\r/g, "").trim();
     if (text == "")
       return;
 
-    if (type != "cdata")
-    {
-      text = text.replace(/&/g, "&")
-                 .replace(/</g, "<")
-                 .replace(/>/g, ">");
-    }
-    text = text.replace(/\t/g, "  ");
-    if (type == "cdata")
-      text = "<![CDATA[" + text + "]]>";
-    else if (type == "comment")
+    text = text.replace(/&/g, "&")
+               .replace(/</g, "<")
+               .replace(/>/g, ">")
+               .replace(/\t/g, "  ");
+    if (type == "comment")
       text = "<!--" + text + "-->";
 
-    var lines = text.split("\n");
-    for (var i = 0; i < lines.length; i++)
-      this.appendDescription(container, lines[i].replace(/^\s+/, '').replace(/\s+$/, ''), type);
+    for (let line of text.split("\n"))
+      this.appendDescription(container, line.trim(), type);
   },
 
   showMenu: function()
diff --git a/lib/child/commands.js b/lib/child/commands.js
index 067ce37..90f7b2c 100644
--- a/lib/child/commands.js
+++ b/lib/child/commands.js
@@ -14,10 +14,14 @@ let {
 let {getParentElement} = require("./utils");
 
 messageManager.addMessageListener("ElemHideHelper:Command", onCommand);
+messageManager.addMessageListener("ElemHideHelper:SerializeSelected",
+                                  serializeSelected);
 
 onShutdown.add(() =>
 {
   messageManager.removeMessageListener("ElemHideHelper:Command", onCommand);
+  messageManager.removeMessageListener("ElemHideHelper:SerializeSelected",
+                                       serializeSelected);
 });
 
 function onCommand(message)
@@ -27,6 +31,46 @@ function onCommand(message)
     exports[command]();
 }
 
+function serializeNode(node)
+{
+  let result = null;
+  switch (node.nodeType)
+  {
+    case node.ELEMENT_NODE:
+      result = {
+        type: "element",
+        tagName: node.localName,
+        attributes: [],
+        children: []
+      };
+      for (let {name, value} of node.attributes)
+        result.attributes.push({name, value});
+      for (let child = node.firstChild; child; child = child.nextSibling)
+      {
+        let serialized = serializeNode(child);
+        if (serialized)
+          result.children.push(serialized);
+      }
+      break;
+    case node.TEXT_NODE:
+    case node.COMMENT_NODE:
+      result= {
+        type: node.nodeType == node.TEXT_NODE ? "text" : "comment",
+        text: node.textContent
+      };
+      break;
+  }
+  return result;
+}
+
+function serializeSelected(message)
+{
+  messageManager.sendAsyncMessage("ElemHideHelper:Response", {
+    messageId: message.data.messageId,
+    serialized: serializeNode(state.selectedElement)
+  });
+}
+
 function quit()
 {
   stopSelection();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus-element-hiding-helper.git



More information about the Pkg-mozext-commits mailing list