RESOLVED FIXED 159511
Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
https://bugs.webkit.org/show_bug.cgi?id=159511
Summary Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
Xavier Morel
Reported 2016-07-07 03:46:09 PDT
[In Safari 9.1.1 on El Capitan] * Open web inspector * Switch to network tab * Navigate to google.com * Type some stuff to trigger FAYT * Select "search" network resource * Click on "Pretty print" button Expected: the resource result gets pretty-printed, the "Pretty print" button gets highlighted (blue font) Result: expectations fulfilled [Repeat same steps in STP8] Expected: same as above Result: the resource does not get pretty-printed, the "Pretty print" button remains inactive
Attachments
[PATCH] Proposed Fix (9.20 KB, patch)
2016-07-07 13:48 PDT, Joseph Pecoraro
no flags
[PATCH] Proposed Fix (10.10 KB, patch)
2016-07-07 13:51 PDT, Joseph Pecoraro
no flags
Radar WebKit Bug Importer
Comment 1 2016-07-07 03:46:27 PDT
Joseph Pecoraro
Comment 2 2016-07-07 12:58:48 PDT
The "application/json" content looks like: {"e":"...","c":-1,...}/*""*/ Pretty Printing fails because: • It is an invalid JavaScript program. (Esprima Parse error) • JSON.parse fails. (The comment at the end makes it invalid) I suppose instead of JSON.parse we could try '(' + str + ')' and chopping off some pieces. That is rather unfortunate.
Joseph Pecoraro
Comment 3 2016-07-07 13:48:25 PDT
Created attachment 283043 [details] [PATCH] Proposed Fix
Joseph Pecoraro
Comment 4 2016-07-07 13:50:03 PDT
Comment on attachment 283043 [details] [PATCH] Proposed Fix View in context: https://bugs.webkit.org/attachment.cgi?id=283043&action=review > LayoutTests/inspector/formatting/formatting-json-expected.txt:51 > +PASS: Should not be able to not be evaluated to an object. Typo =(
Joseph Pecoraro
Comment 5 2016-07-07 13:51:45 PDT
Created attachment 283044 [details] [PATCH] Proposed Fix
WebKit Commit Bot
Comment 6 2016-07-07 14:28:46 PDT
Comment on attachment 283044 [details] [PATCH] Proposed Fix Clearing flags on attachment: 283044 Committed r202933: <http://trac.webkit.org/changeset/202933>
WebKit Commit Bot
Comment 7 2016-07-07 14:28:51 PDT
All reviewed patches have been landed. Closing bug.
Alexey Proskuryakov
Comment 8 2016-07-09 20:50:58 PDT
> • It is an invalid JavaScript program. (Esprima Parse error) > • JSON.parse fails. (The comment at the end makes it invalid) If this is neither valid JS not valid JSON, isn't misleading to pretty print it?
Timothy Hatcher
Comment 9 2016-07-11 08:38:57 PDT
(In reply to comment #8) > > • It is an invalid JavaScript program. (Esprima Parse error) > > • JSON.parse fails. (The comment at the end makes it invalid) > > If this is neither valid JS not valid JSON, isn't misleading to pretty print > it? It is not directly valid JS, but sites often take it and add parens to make it valid. That is what Joe did here.
Joseph Pecoraro
Comment 10 2016-07-11 13:48:37 PDT
(In reply to comment #9) > (In reply to comment #8) > > > • It is an invalid JavaScript program. (Esprima Parse error) > > > • JSON.parse fails. (The comment at the end makes it invalid) > > > > If this is neither valid JS not valid JSON, isn't misleading to pretty print > > it? > > It is not directly valid JS, but sites often take it and add parens to make > it valid. That is what Joe did here. Correct. It is not a valid JavaScript program, but it is a valid JavaScript object. For example: "{a:1, b:2}" Is not a valid JavaScript program: >>> {a:1, b:2} Unexpected token ':'. Parse error.:1 But it is a valid JavaScript object: >>> o = {a:1, b:2} [object Object] A common practice among web pages is to wrap the input in parenthesis and eval it, or use it in a function call: var json; /* load "{a:1, b:2}" from network */ var object = eval( '(' + json + ')' ); Which would produce the expected object: >>> o = eval('(' + "{a:1, b:2}" + ')') [object Object] Because
Joseph Pecoraro
Comment 11 2016-07-11 13:52:09 PDT
Finishing the comment: --- A common practice among web pages is to wrap the input in parenthesis and eval it, or use it in a function call: var json; /* load "{a:1, b:2}" from network */ // Eval with parenthesis to produce an object: var object = eval( '(' + json + ')' ); // Eval with a function call providing the parens: eval( 'parseResponse(' + json + ')' ); Which would produce the expected object: >>> o = eval('(' + "{a:1, b:2}" + ')') [object Object] This was a common practice before JSON.parse, and may still be in use to some degree. In the case on google.com the content can not be JSON.parse'd directly, so I assume some approach like this, or knowing to strip off some characters, must be at play.
Joseph Pecoraro
Comment 12 2016-07-11 14:01:07 PDT
I should also point out that object properties need to be double quoted in JSON, so the example given before would not satisfy JSON.parse: >>> JSON.parse("{a:1, b:2}") Exception: SyntaxError: JSON Parse error: Expected '}'
Note You need to log in before you can comment on or make changes to this bug.