Bug 772506 - Make sure jsterm eval results are correctly formatted as strings; r=rcampbell
authorMihai Sucan <mihai.sucan@gmail.com>
Tue, 10 Jul 2012 19:29:58 +0300
changeset 98898 6c31b06b0c8f5e6b4cb1893332a8f8782032d634
parent 98897 c9c8ecd605b5eec52e3ffdb0249983c0c76a239b
child 98899 395e4eaaf47b39b2678f500c0925237cbfdb22c7
push id23089
push usertim.taubert@gmx.de
push dateWed, 11 Jul 2012 06:44:16 +0000
treeherdermozilla-central@53860f11100c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrcampbell
bugs772506
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 772506 - Make sure jsterm eval results are correctly formatted as strings; r=rcampbell
browser/devtools/webconsole/WebConsoleUtils.jsm
browser/devtools/webconsole/test/Makefile.in
browser/devtools/webconsole/test/browser_result_format_as_string.js
browser/devtools/webconsole/test/test-result-format-as-string.html
--- a/browser/devtools/webconsole/WebConsoleUtils.jsm
+++ b/browser/devtools/webconsole/WebConsoleUtils.jsm
@@ -241,28 +241,31 @@ var WebConsoleUtils = {
       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.
    * @return string
--- a/browser/devtools/webconsole/test/Makefile.in
+++ b/browser/devtools/webconsole/test/Makefile.in
@@ -104,16 +104,17 @@ MOCHITEST_BROWSER_FILES = \
 	browser_webconsole_bug_664131_console_group.js \
 	browser_webconsole_bug_704295.js \
 	browser_webconsole_bug_658368_time_methods.js \
 	browser_webconsole_bug_622303_persistent_filters.js \
 	browser_webconsole_window_zombie.js \
 	browser_cached_messages.js \
 	browser_bug664688_sandbox_update_after_navigation.js \
 	browser_webconsole_menustatus.js \
+	browser_result_format_as_string.js \
 	head.js \
 	$(NULL)
 
 MOCHITEST_BROWSER_FILES += \
 	test-console.html \
 	test-network.html \
 	test-network-request.html \
 	test-mutation.html \
@@ -176,11 +177,12 @@ MOCHITEST_BROWSER_FILES += \
 	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-file-location.js \
 	test-bug-658368-time-methods.html \
 	test-webconsole-error-observer.html \
 	test-for-of.html \
+	test-result-format-as-string.html \
 	$(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/browser_result_format_as_string.js
@@ -0,0 +1,49 @@
+/* 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/test-result-format-as-string.html";
+
+function test()
+{
+  waitForExplicitFinish();
+
+  addTab(TEST_URI);
+
+  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+    openConsole(null, performTest);
+  }, true);
+}
+
+function performTest(hud)
+{
+  hud.jsterm.clearOutput(true);
+
+  hud.jsterm.execute("document.querySelector('p')");
+  waitForSuccess({
+    name: "eval result shown",
+    validatorFn: function()
+    {
+      return hud.outputNode.querySelector(".webconsole-msg-output");
+    },
+    successFn: function()
+    {
+      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();
+    },
+    failureFn: finishTest,
+  });
+}
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/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>