Bug 772506 - Make sure jsterm eval results are correctly formatted as strings; r=rcampbell a=lsblakk
authorMihai Sucan <mihai.sucan@gmail.com>
Fri, 27 Jul 2012 12:45:43 +0300
changeset 81922 bd76cc13e6a8bdc83d17fd85953ff44a784162b9
parent 81921 bb1d806b2efa69a73dda642bdbc387fbe21653be
child 81923 d07051929ca378d572e912298f90aa6ca44fc136
push id220
push usermihai.sucan@gmail.com
push dateFri, 27 Jul 2012 09:57:18 +0000
reviewersrcampbell, lsblakk
bugs772506
milestone10.0.7esrpre
Bug 772506 - Make sure jsterm eval results are correctly formatted as strings; r=rcampbell a=lsblakk
browser/devtools/webconsole/HUDService.jsm
browser/devtools/webconsole/test/browser/Makefile.in
browser/devtools/webconsole/test/browser/browser_result_format_as_string.js
browser/devtools/webconsole/test/browser/test-result-format-as-string.html
--- a/browser/devtools/webconsole/HUDService.jsm
+++ b/browser/devtools/webconsole/HUDService.jsm
@@ -5008,28 +5008,31 @@ JSTerm.prototype = {
       case "regexp":
         output = aResult.toString();
         break;
       case "null":
       case "undefined":
         output = type;
         break;
       default:
-        if (aResult.toSource) {
-          try {
+        try {
+          if (aResult.toSource) {
             output = aResult.toSource();
-          } catch (ex) { }
+          }
+          if (!output || output == "({})") {
+            output = aResult + "";
+          }
         }
-        if (!output || output == "({})") {
-          output = aResult.toString();
+        catch (ex) {
+          output = ex;
         }
         break;
     }
 
-    return output;
+    return output + "";
   },
 
   /**
    * Format a string for output.
    *
    * @param string aString
    *        The string you want to display.
    * @returns string
--- a/browser/devtools/webconsole/test/browser/Makefile.in
+++ b/browser/devtools/webconsole/test/browser/Makefile.in
@@ -146,16 +146,17 @@ include $(topsrcdir)/config/rules.mk
 	browser_webconsole_bug_678816.js \
 	browser_webconsole_bug_664131_console_group.js \
 	browser_gcli_inspect.js \
 	browser_gcli_integrate.js \
 	browser_gcli_require.js \
 	browser_gcli_web.js \
 	browser_webconsole_bug_658368_time_methods.js \
 	browser_webconsole_window_zombie.js \
+	browser_result_format_as_string.js \
 	head.js \
 	$(NULL)
 
 _BROWSER_TEST_PAGES = \
 	test-console.html \
 	test-network.html \
 	test-network-request.html \
 	test-mutation.html \
@@ -218,15 +219,16 @@ include $(topsrcdir)/config/rules.mk
 	test-bug-585956-console-trace.html \
 	test-bug-644419-log-limits.html \
 	test-bug-632275-getters.html \
 	test-bug-646025-console-file-location.html \
 	test-bug-678816-content.js \
 	test-file-location.js \
 	browser_gcli_inspect.html \
 	test-bug-658368-time-methods.html \
+	test-result-format-as-string.html \
 	$(NULL)
 
 libs:: $(_BROWSER_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
 
 libs:: $(_BROWSER_TEST_PAGES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/browser/browser_result_format_as_string.js
@@ -0,0 +1,37 @@
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Make sure that JS eval result are properly formatted as strings.
+
+const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/browser/test-result-format-as-string.html";
+
+function test()
+{
+  waitForExplicitFinish();
+
+  addTab(TEST_URI);
+
+  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+    performTest();
+  }, true);
+}
+
+function performTest()
+{
+  openConsole();
+  let hudId = HUDService.getHudIdByWindow(content);
+  let hud = HUDService.getHudReferenceById(hudId);
+  hud.jsterm.execute("document.querySelector('p')");
+  is(hud.outputNode.textContent.indexOf("bug772506_content"), -1,
+        "no content element found");
+  ok(!hud.outputNode.querySelector("div"), "no div element found");
+  let msg = hud.outputNode.querySelector(".webconsole-msg-output");
+  ok(msg, "eval output node found");
+  isnot(msg.textContent.indexOf("HTMLDivElement"), -1,
+        "HTMLDivElement string found");
+  EventUtils.synthesizeMouseAtCenter(msg, {type: "mousemove"});
+  ok(!gBrowser._bug772506, "no content variable");
+  finishTest();
+}
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/browser/test-result-format-as-string.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>Web Console test: jsterm eval format as a string</title>
+    <!-- Any copyright is dedicated to the Public Domain.
+         http://creativecommons.org/publicdomain/zero/1.0/ -->
+  </head>
+  <body>
+    <p>Make sure js eval results are formatted as strings.</p>
+    <script>
+      document.querySelector("p").toSource = function() {
+        var element = document.createElement("div");
+        element.textContent = "bug772506_content";
+        element.setAttribute("onmousemove",
+          "(function () {" +
+          "  gBrowser._bug772506 = 'foobar';" +
+          "})();"
+        );
+        return element;
+      };
+    </script>
+  </body>
+</html>