Bug 159511 - Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
Summary: Web Inspector, regression: JS/JSON pretty-printing sporadically broken in STP8
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: Safari Technology Preview
Hardware: All All
: P2 Normal
Assignee: Joseph Pecoraro
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2016-07-07 03:46 PDT by Xavier Morel
Modified: 2016-07-11 14:01 PDT (History)
8 users (show)

See Also:


Attachments
[PATCH] Proposed Fix (9.20 KB, patch)
2016-07-07 13:48 PDT, Joseph Pecoraro
no flags Details | Formatted Diff | Diff
[PATCH] Proposed Fix (10.10 KB, patch)
2016-07-07 13:51 PDT, Joseph Pecoraro
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Xavier Morel 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
Comment 1 Radar WebKit Bug Importer 2016-07-07 03:46:27 PDT
<rdar://problem/27218435>
Comment 2 Joseph Pecoraro 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.
Comment 3 Joseph Pecoraro 2016-07-07 13:48:25 PDT
Created attachment 283043 [details]
[PATCH] Proposed Fix
Comment 4 Joseph Pecoraro 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 =(
Comment 5 Joseph Pecoraro 2016-07-07 13:51:45 PDT
Created attachment 283044 [details]
[PATCH] Proposed Fix
Comment 6 WebKit Commit Bot 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>
Comment 7 WebKit Commit Bot 2016-07-07 14:28:51 PDT
All reviewed patches have been landed.  Closing bug.
Comment 8 Alexey Proskuryakov 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?
Comment 9 Timothy Hatcher 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.
Comment 10 Joseph Pecoraro 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
Comment 11 Joseph Pecoraro 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.
Comment 12 Joseph Pecoraro 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 '}'