Title: [202933] trunk
Revision
202933
Author
[email protected]
Date
2016-07-07 14:28:08 -0700 (Thu, 07 Jul 2016)

Log Message

Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
https://bugs.webkit.org/show_bug.cgi?id=159511
<rdar://problem/27218435>

Patch by Joseph Pecoraro <[email protected]> on 2016-07-07
Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

* UserInterface/Workers/Formatter/FormatterWorker.js:
(FormatterWorker.prototype.formatJavaScript):
Attempt to format invalid JSON that can be evaluated to an object.

LayoutTests:

* inspector/formatting/formatting-json-expected.txt: Added.
* inspector/formatting/formatting-json.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (202932 => 202933)


--- trunk/LayoutTests/ChangeLog	2016-07-07 21:26:33 UTC (rev 202932)
+++ trunk/LayoutTests/ChangeLog	2016-07-07 21:28:08 UTC (rev 202933)
@@ -1,3 +1,14 @@
+2016-07-07  Joseph Pecoraro  <[email protected]>
+
+        Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
+        https://bugs.webkit.org/show_bug.cgi?id=159511
+        <rdar://problem/27218435>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/formatting/formatting-json-expected.txt: Added.
+        * inspector/formatting/formatting-json.html: Added.
+
 2016-07-07  Antti Koivisto  <[email protected]>
 
         REGRESSION (r199054): CrashTracer: [USER] parseWebKit at WebCore: WebCore::RenderBlockFlow::checkFloatsInCleanLine + 107

Modified: trunk/LayoutTests/inspector/formatting/formatting-_javascript_-expected.txt (202932 => 202933)


--- trunk/LayoutTests/inspector/formatting/formatting-_javascript_-expected.txt	2016-07-07 21:26:33 UTC (rev 202932)
+++ trunk/LayoutTests/inspector/formatting/formatting-_javascript_-expected.txt	2016-07-07 21:28:08 UTC (rev 202933)
@@ -1,4 +1,4 @@
-Test _javascript_ formatting tests.
+Test _javascript_ formatting.
 
 
 == Running test suite: EsprimaFormatter._javascript_

Modified: trunk/LayoutTests/inspector/formatting/formatting-_javascript_.html (202932 => 202933)


--- trunk/LayoutTests/inspector/formatting/formatting-_javascript_.html	2016-07-07 21:26:33 UTC (rev 202932)
+++ trunk/LayoutTests/inspector/formatting/formatting-_javascript_.html	2016-07-07 21:28:08 UTC (rev 202933)
@@ -42,6 +42,6 @@
 </script>
 </head>
 <body _onload_="runTest()">
-<p>Test _javascript_ formatting tests.</p>
+<p>Test _javascript_ formatting.</p>
 </body>
 </html>

Added: trunk/LayoutTests/inspector/formatting/formatting-json-expected.txt (0 => 202933)


--- trunk/LayoutTests/inspector/formatting/formatting-json-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/formatting/formatting-json-expected.txt	2016-07-07 21:28:08 UTC (rev 202933)
@@ -0,0 +1,53 @@
+Test JSON formatting.
+
+
+== Running test suite: EsprimaFormatter.JSON
+-- Running test case: EsprimaFormatter.ValidJSON
+JSON: "{\"a\":123,\"b\":[1,2,3],\"c\":{\"d\":\"e\"}}"
+PASS: JSON should be valid.
+FORMATTED:
+{
+    "a": 123,
+    "b": [
+        1,
+        2,
+        3
+    ],
+    "c": {
+        "d": "e"
+    }
+}
+
+-- Running test case: EsprimaFormatter.InvalidButObject
+JSON: "{a:123,b:[1,2,3],c:{d:\"e\"}}"
+PASS: JSON should be invalid.
+PASS: Should be able to be evaluated to an object.
+FORMATTED:
+{
+    a: 123,
+    b: [1, 2, 3],
+    c: {
+        d: "e"
+    }
+}
+
+-- Running test case: EsprimaFormatter.InvalidWithComments
+JSON: "{\"a\":123,\"b\":[1,2,3],\"c\":{\"d\":\"e\"}}/*Comment*/"
+PASS: JSON should be invalid.
+PASS: Should be able to be evaluated to an object.
+FORMATTED:
+{
+    "a": 123,
+    "b": [1, 2, 3],
+    "c": {
+        "d": "e"
+    }
+} /*Comment*/
+
+
+-- Running test case: EsprimaFormatter.Invalid
+INPUT: "{a:1 b:2}"
+PASS: JSON should be invalid.
+PASS: Should not be able to be evaluated to an object.
+PASS: Response should be null.
+

Added: trunk/LayoutTests/inspector/formatting/formatting-json.html (0 => 202933)


--- trunk/LayoutTests/inspector/formatting/formatting-json.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/formatting/formatting-json.html	2016-07-07 21:28:08 UTC (rev 202933)
@@ -0,0 +1,109 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+<script>
+function test()
+{
+    function ensureJSON(string, expectation) {
+        let invalid = false;
+        try {
+            JSON.parse(string);
+        } catch (e) {
+            invalid = true;
+        }
+        if (expectation === "invalid")
+            InspectorTest.expectThat(invalid, "JSON should be invalid.");
+        else
+            InspectorTest.expectThat(!invalid, "JSON should be valid.");
+    }
+
+    function ensureEval(string, expectation) {
+        let isObject = false;
+        try {
+            let result = eval("(" + string + ")");
+            isObject = typeof result === "object";
+        } catch (e) {}
+
+        if (expectation === "object")
+            InspectorTest.expectThat(isObject, "Should be able to be evaluated to an object.");
+        else
+            InspectorTest.expectThat(!isObject, "Should not be able to be evaluated to an object.");
+    }
+
+
+    let suite = InspectorTest.createAsyncSuite("EsprimaFormatter.JSON");
+
+    let indentString = "    ";
+    let workerProxy = WebInspector.FormatterWorkerProxy.singleton();
+
+    suite.addTestCase({
+        name: "EsprimaFormatter.ValidJSON",
+        description: "Format valid JSON",
+        test: (resolve, reject) => {
+            let validJSON = JSON.stringify({"a":123,"b":[1,2,3],"c":{"d":"e"}});
+            InspectorTest.log("JSON: " + doubleQuotedString(validJSON));
+            ensureJSON(validJSON, "valid");
+            workerProxy.formatJavaScript(validJSON, indentString, ({formattedText, sourceMapData}) => {
+                InspectorTest.log("FORMATTED:");
+                InspectorTest.log(formattedText);
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: "EsprimaFormatter.InvalidButObject",
+        description: "Format invalid JSON due to literal keys, but it can be evaluated as a valid _javascript_ Object",
+        test: (resolve, reject) => {
+            let invalidJSON = `{a:123,b:[1,2,3],c:{d:"e"}}`;
+            InspectorTest.log("JSON: " + doubleQuotedString(invalidJSON));
+            ensureJSON(invalidJSON, "invalid");
+            ensureEval(invalidJSON, "object");
+            workerProxy.formatJavaScript(invalidJSON, indentString, ({formattedText, sourceMapData}) => {
+                InspectorTest.log("FORMATTED:");
+                InspectorTest.log(formattedText);
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: "EsprimaFormatter.InvalidWithComments",
+        description: "Format invalid JSON due to comments, but it can be evaluated as a valid _javascript_ Object",
+        test: (resolve, reject) => {
+            let invalidJSON = JSON.stringify({"a":123,"b":[1,2,3],"c":{"d":"e"}}) + "/*Comment*/";
+            InspectorTest.log("JSON: " + doubleQuotedString(invalidJSON));
+            ensureJSON(invalidJSON, "invalid");
+            ensureEval(invalidJSON, "object");
+            workerProxy.formatJavaScript(invalidJSON, indentString, ({formattedText, sourceMapData}) => {
+                InspectorTest.log("FORMATTED:");
+                InspectorTest.log(formattedText);
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: "EsprimaFormatter.Invalid",
+        description: "Format invalid JSON, that is not a valid _javascript_ Object",
+        test: (resolve, reject) => {
+            let invalid = `{a:1 b:2}`;
+            InspectorTest.log("INPUT: " + doubleQuotedString(invalid));
+            ensureJSON(invalid, "invalid");
+            ensureEval(invalid, "bad");
+            workerProxy.formatJavaScript(invalid, indentString, ({formattedText, sourceMapData}) => {
+                InspectorTest.expectThat(formattedText === null, "Response should be null.");
+                resolve();
+            });
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+</script>
+</head>
+<body _onload_="runTest()">
+<p>Test JSON formatting.</p>
+</body>
+</html>

Modified: trunk/Source/WebInspectorUI/ChangeLog (202932 => 202933)


--- trunk/Source/WebInspectorUI/ChangeLog	2016-07-07 21:26:33 UTC (rev 202932)
+++ trunk/Source/WebInspectorUI/ChangeLog	2016-07-07 21:28:08 UTC (rev 202933)
@@ -1,3 +1,15 @@
+2016-07-07  Joseph Pecoraro  <[email protected]>
+
+        Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
+        https://bugs.webkit.org/show_bug.cgi?id=159511
+        <rdar://problem/27218435>
+
+        Reviewed by Timothy Hatcher.
+
+        * UserInterface/Workers/Formatter/FormatterWorker.js:
+        (FormatterWorker.prototype.formatJavaScript):
+        Attempt to format invalid JSON that can be evaluated to an object.
+
 2016-07-07  Timothy Hatcher  <[email protected]>
 
         Web Inspector: scrolled Snapshot list is reset to top and drawn blank after switching back from Snapshot Comparison view

Modified: trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterWorker.js (202932 => 202933)


--- trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterWorker.js	2016-07-07 21:26:33 UTC (rev 202932)
+++ trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterWorker.js	2016-07-07 21:28:08 UTC (rev 202933)
@@ -42,6 +42,7 @@
 
     formatJavaScript(sourceText, indentString, includeSourceMapData)
     {
+        // Format a _javascript_ program.
         let formatter = new EsprimaFormatter(sourceText, indentString);
         if (formatter.success) {
             let result = {formattedText: formatter.formattedText};
@@ -56,6 +57,7 @@
             return result;
         }
 
+        // Format valid JSON.
         // The formatter could fail if this was just a JSON string. So try a JSON.parse and stringify.
         // This will produce empty source map data, but it is not code, so it is not as important.
         try {
@@ -66,6 +68,20 @@
             return result;
         } catch (e) {}
 
+        // Format invalid JSON.
+        // Some applications do not use JSON.parse but eval on JSON content. That is more permissive
+        // so try to format invalid JSON. Again no source map data since it is not code.
+        if (/^\s*\{/.test(sourceText)) {
+            let invalidJSONFormatter = new EsprimaFormatter("(" + sourceText + ")", indentString);
+            if (invalidJSONFormatter.success) {
+                let formattedTextWithParens = invalidJSONFormatter.formattedText;
+                let result = {formattedText: formattedTextWithParens.substring(1, formattedTextWithParens.length - 2)}; // Remove "(" and ")\n".
+                if (includeSourceMapData)
+                    result.sourceMapData = {mapping: {original: [], formatted: []}, originalLineEndings:[], formattedLineEndings: []};
+                return result;
+            }
+        }
+
         return {formattedText: null};
     }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to