@@ -10,12 +10,12 @@ export function jsonToHTML(json: any, uri: string) {
1010
1111/** Convert a whole JSON value / JSONP response into an HTML body, without title and scripts */
1212function jsonToHTMLBody ( json : any ) {
13- return `<div id="json">${ valueToHTML ( json , ' <root>' ) } </div>` ;
13+ return `<div id="json">${ valueToHTML ( json , " <root>" ) } </div>` ;
1414}
1515
1616/** Produce an error document for when parsing fails. */
1717export function errorPage ( error : Error , data : string , uri : string ) {
18- return toHTML ( errorPageBody ( error , data ) , uri + ' - Error' ) ;
18+ return toHTML ( errorPageBody ( error , data ) , uri + " - Error" ) ;
1919}
2020
2121/** Produce an error content for when parsing fails. */
@@ -25,7 +25,7 @@ function errorPageBody(error: Error, data: string) {
2525
2626 const errorInfo = massageError ( error ) ;
2727
28- let output = `<div id="error">${ chrome . i18n . getMessage ( ' errorParsing' ) } ` ;
28+ let output = `<div id="error">${ chrome . i18n . getMessage ( " errorParsing" ) } ` ;
2929 if ( errorInfo . message ) {
3030 output += `<div class="errormessage">${ errorInfo . message } </div>` ;
3131 }
@@ -37,13 +37,15 @@ function errorPageBody(error: Error, data: string) {
3737 * Encode a string to be used in HTML
3838 */
3939function htmlEncode ( t : any ) : string {
40- return ( typeof t !== "undefined" && t !== null ) ? t . toString ( )
41- . replace ( / & / g, "&" )
42- . replace ( / " / g, """ )
43- . replace ( / ' / g, "'" )
44- . replace ( / < / g, "<" )
45- . replace ( / > / g, ">" )
46- : '' ;
40+ return typeof t !== "undefined" && t !== null
41+ ? t
42+ . toString ( )
43+ . replace ( / & / g, "&" )
44+ . replace ( / " / g, """ )
45+ . replace ( / ' / g, "'" )
46+ . replace ( / < / g, "<" )
47+ . replace ( / > / g, ">" )
48+ : "" ;
4749}
4850
4951/**
@@ -74,71 +76,77 @@ function valueToHTML(value: any, path: string) {
7476 const valueType = typeof value ;
7577
7678 if ( value === null ) {
77- return decorateWithSpan ( ' null' , ' null' ) ;
79+ return decorateWithSpan ( " null" , " null" ) ;
7880 } else if ( Array . isArray ( value ) ) {
7981 return arrayToHTML ( value , path ) ;
80- } else if ( valueType === ' object' ) {
82+ } else if ( valueType === " object" ) {
8183 return objectToHTML ( value , path ) ;
82- } else if ( valueType === 'number' ) {
83- return decorateWithSpan ( value , 'num' ) ;
84- } else if ( valueType === 'string' &&
85- value . charCodeAt ( 0 ) === 8203 &&
86- ! isNaN ( value . slice ( 1 ) ) ) {
87- return decorateWithSpan ( value . slice ( 1 ) , 'num' ) ;
88- } else if ( valueType === 'string' ) {
84+ } else if ( valueType === "number" ) {
85+ return decorateWithSpan ( value , "num" ) ;
86+ } else if ( valueType === "string" && value . charCodeAt ( 0 ) === 8203 && ! isNaN ( value . slice ( 1 ) ) ) {
87+ return decorateWithSpan ( value . slice ( 1 ) , "num" ) ;
88+ } else if ( valueType === "string" ) {
8989 if ( / ^ ( h t t p | h t t p s | f i l e ) : \/ \/ [ ^ \s ] + $ / i. test ( value ) ) {
90- return `<a href="${ htmlEncode ( value ) } "><span class="q">"</span>${ jsString ( value ) } <span class="q">"</span></a>` ;
90+ return `<a href="${ htmlEncode ( value ) } "><span class="q">"</span>${ jsString (
91+ value
92+ ) } <span class="q">"</span></a>`;
9193 } else {
9294 return `<span class="string">"${ jsString ( value ) } "</span>` ;
9395 }
94- } else if ( valueType === ' boolean' ) {
95- return decorateWithSpan ( value , ' bool' ) ;
96+ } else if ( valueType === " boolean" ) {
97+ return decorateWithSpan ( value , " bool" ) ;
9698 }
9799
98- return '' ;
100+ return "" ;
99101}
100102
101103// Convert an array into an HTML fragment
102104function arrayToHTML ( json : any , path : string ) {
103105 if ( json . length === 0 ) {
104- return ' [ ]' ;
106+ return " [ ]" ;
105107 }
106108
107- let output = '' ;
109+ let output = "" ;
108110 for ( let i = 0 ; i < json . length ; i ++ ) {
109111 const subPath = `${ path } [${ i } ]` ;
110- output += ' <li>' + valueToHTML ( json [ i ] , subPath ) ;
112+ output += " <li>" + valueToHTML ( json [ i ] , subPath ) ;
111113 if ( i < json . length - 1 ) {
112- output += ',' ;
114+ output += "," ;
113115 }
114- output += ' </li>' ;
116+ output += " </li>" ;
115117 }
116- return ( json . length === 0 ? '' : '<span class="collapser"></span>' ) +
117- `[<ul class="array collapsible">${ output } </ul>]` ;
118+ return (
119+ ( json . length === 0 ? "" : '<span class="collapser"></span>' ) +
120+ `[<ul class="array collapsible">${ output } </ul>]`
121+ ) ;
118122}
119123
120124// Convert a JSON object to an HTML fragment
121125function objectToHTML ( json : any , path : string ) {
122126 let numProps = Object . keys ( json ) . length ;
123127 if ( numProps === 0 ) {
124- return ' { }' ;
128+ return " { }" ;
125129 }
126130
127- let output = '' ;
131+ let output = "" ;
128132 for ( const prop in json ) {
129- let subPath = '' ;
133+ let subPath = "" ;
130134 let escapedProp = JSON . stringify ( prop ) . slice ( 1 , - 1 ) ;
131135 const bare = isBareProp ( prop ) ;
132136 if ( bare ) {
133137 subPath = `${ path } .${ escapedProp } ` ;
134138 } else {
135139 escapedProp = `"${ escapedProp } "` ;
136140 }
137- output += `<li><span class="prop${ ( bare ? '' : ' quoted' ) } " title="${ htmlEncode ( subPath ) } "><span class="q">"</span>${ jsString ( prop ) } <span class="q">"</span></span>: ${ valueToHTML ( json [ prop ] , subPath ) } ` ;
141+ output += `<li><span class="prop${ bare ? "" : " quoted" } " title="${ htmlEncode (
142+ subPath
143+ ) } "><span class="q">"</span>${ jsString (
144+ prop
145+ ) } <span class="q">"</span></span>: ${ valueToHTML ( json [ prop ] , subPath ) } `;
138146 if ( numProps > 1 ) {
139- output += ',' ;
147+ output += "," ;
140148 }
141- output += ' </li>' ;
149+ output += " </li>" ;
142150 numProps -- ;
143151 }
144152
@@ -155,7 +163,7 @@ function massageError(error: Error): {
155163 return error ;
156164 }
157165
158- const message = error . message . replace ( / ^ J S O N .p a r s e : / , '' ) . replace ( / o f t h e J S O N d a t a / , '' ) ;
166+ const message = error . message . replace ( / ^ J S O N .p a r s e : / , "" ) . replace ( / o f t h e J S O N d a t a / , "" ) ;
159167 const parts = / l i n e ( \d + ) c o l u m n ( \d + ) / . exec ( message ) ;
160168 if ( ! parts || parts . length !== 3 ) {
161169 return error ;
@@ -164,7 +172,7 @@ function massageError(error: Error): {
164172 return {
165173 message : htmlEncode ( message ) ,
166174 line : Number ( parts [ 1 ] ) ,
167- column : Number ( parts [ 2 ] )
175+ column : Number ( parts [ 2 ] ) ,
168176 } ;
169177}
170178
@@ -175,14 +183,18 @@ function highlightError(data: string, lineNum?: number, columnNum?: number) {
175183
176184 const lines = data . match ( / ^ .* ( ( \r \n | \n | \r ) | $ ) / gm) ! ;
177185
178- let output = '' ;
186+ let output = "" ;
179187 for ( let i = 0 ; i < lines . length ; i ++ ) {
180188 const line = lines [ i ] ;
181189
182190 if ( i === lineNum - 1 ) {
183191 output += '<span class="errorline">' ;
184- output += `${ htmlEncode ( line . substring ( 0 , columnNum - 1 ) ) } <span class="errorcolumn">${ htmlEncode ( line [ columnNum - 1 ] ) } </span>${ htmlEncode ( line . substring ( columnNum ) ) } ` ;
185- output += '</span>' ;
192+ output += `${ htmlEncode (
193+ line . substring ( 0 , columnNum - 1 )
194+ ) } <span class="errorcolumn">${ htmlEncode ( line [ columnNum - 1 ] ) } </span>${ htmlEncode (
195+ line . substring ( columnNum )
196+ ) } `;
197+ output += "</span>" ;
186198 } else {
187199 output += htmlEncode ( line ) ;
188200 }
0 commit comments