Skip to content

Commit 8358352

Browse files
authored
Merge branch 'master' into fix-document-childNodes-checkup
2 parents 5a4490c + efacf2c commit 8358352

File tree

15 files changed

+524
-187
lines changed

15 files changed

+524
-187
lines changed

extension/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.17.0",
44
"manifest_version": 2,
55
"author": "Tulio Ornelas <[email protected]>",
6-
"description": "The most beautiful and customizable JSON/JSONP highlighter that your eyes have ever seen",
6+
"description": "The most beautiful and customizable JSON/JSONP highlighter that your eyes have ever seen. Open source at https://github.com/tulios/json-viewer",
77
"homepage_url": "https://github.com/tulios/json-viewer",
88
"minimum_chrome_version": "21",
99
"icons": {

extension/pages/options.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ <h2>Add-ons</h2>
4848
<li>
4949
<strong>"clickableUrls"</strong> - <span class="hint">Make URLs clickable (boolean). Default true</span>
5050
</li>
51+
<li>
52+
<strong>"wrapLinkWithAnchorTag"</strong> - <span class="hint">Add URLs as anchor tag (boolean). Default false</span>
53+
</li>
5154
<li>
5255
<strong>"openLinksInNewWindow"</strong> - <span class="hint">Open URLs in the same window/tab (boolean). Default true</span>
5356
</li>
@@ -72,6 +75,9 @@ <h2>Structure</h2>
7275
<li>
7376
<strong>"indentCStyle"</strong> - <span class="hint">Default false</span>
7477
</li>
78+
<li>
79+
<strong>"showArraySize"</strong> - <span class="hint">Default false</span>
80+
</li>
7581
</ul>
7682
</div>
7783
</header>

extension/src/json-viewer/content-extractor.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ var extractJSON = require('./extract-json');
55
var TOKEN = (Math.random() + 1).toString(36).slice(2, 7);
66
var WRAP_START = "<wrap_" + TOKEN + ">";
77
var WRAP_END = "</wrap_" + TOKEN +">";
8-
var NUM_REGEX = /^-?\d+\.?[\deE]*$/g;
8+
var NUM_REGEX = /^-?\d+\.?\d*([eE]\+)?\d*$/g;
9+
var ESCAPED_REGEX = "(-?\\d+\\.?\\d*([eE]\\+)?\\d*)"
910

1011
var WRAP_REGEX = new RegExp(
11-
"^" + WRAP_START + "(-?\\d+\\.?[\\deE]*)" + WRAP_END + "$", "g"
12+
"^" + WRAP_START + ESCAPED_REGEX + WRAP_END + "$", "g"
1213
);
1314

1415
var REPLACE_WRAP_REGEX = new RegExp(
15-
"\"" + WRAP_START + "(-?\\d+\\.?[\\deE]*)" + WRAP_END + "\"", "g"
16+
"\"" + WRAP_START + ESCAPED_REGEX + WRAP_END + "\"", "g"
1617
);
1718

1819
function contentExtractor(pre, options) {
@@ -119,6 +120,7 @@ function wrapNumbers(text) {
119120
function isCharInNumber(char, previous) {
120121
return ('0' <= char && char <= '9') ||
121122
('0' <= previous && previous <= '9' && (char == 'e' || char == 'E')) ||
123+
(('e' == previous || 'E' == previous) && char == '+') ||
122124
char == '.' ||
123125
char == '-';
124126
}
@@ -127,6 +129,7 @@ function isCharInString(char, previous) {
127129
return ('0' > char || char > '9') &&
128130
char != 'e' &&
129131
char != 'E' &&
132+
char != '+' &&
130133
char != '.' &&
131134
char != '-';
132135
}

extension/src/json-viewer/extract-json.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ function extractJSON(rawJson) {
22
return rawJson
33
.replace(/\s*while\((1|true)\)\s*;?/, '')
44
.replace(/\s*for\(;;\)\s*;?/, '')
5-
.replace(/^[^{\[].+\({/, '{')
6-
.replace(/}\);?\s*$/, '}');
5+
.replace(/^[^{\[].+\(\s*?{/, '{')
6+
.replace(/}\s*?\);?\s*$/, '}');
77
}
88

99
module.exports = extractJSON;

extension/src/json-viewer/highlight-content.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var renderAlert = require('./viewer/render-alert');
77
var getOptions = require('./viewer/get-options');
88
var loadRequiredCss = require('./viewer/load-required-css');
99

10-
function oversizedJSON(pre, options) {
10+
function oversizedJSON(pre, options, outsideViewer) {
1111
var jsonSize = pre.textContent.length;
1212
var accepted = options.addons.maxJsonSize;
1313

@@ -27,10 +27,26 @@ function oversizedJSON(pre, options) {
2727
"Accepted: " + accepted + " kbytes, received: " + loaded + " kbytes. " +
2828
"It's possible to change this value at options -> Add-ons -> maxJsonSize"
2929
);
30-
renderAlert(pre, options,
31-
"[JSONViewer] Content not highlighted due to oversize. " +
32-
"Take a look at the console log for more information."
33-
);
30+
31+
var container = document.createElement("div");
32+
33+
var message = document.createElement("div");
34+
message.innerHTML = "[JSONViewer] Content not highlighted due to oversize. " +
35+
"Take a look at the console log for more information.";
36+
container.appendChild(message);
37+
38+
var highlightAnyway = document.createElement("a");
39+
highlightAnyway.href = "#";
40+
highlightAnyway.title = "Highlight anyway!";
41+
highlightAnyway.innerHTML = "Highlight anyway!";
42+
highlightAnyway.onclick = function(e) {
43+
e.preventDefault();
44+
pre.hidden = true;
45+
highlightContent(pre, outsideViewer, true);
46+
}
47+
container.appendChild(highlightAnyway);
48+
49+
renderAlert(pre, options, container);
3450
}
3551

3652
return isOversizedJSON;
@@ -46,9 +62,9 @@ function prependHeader(options, outsideViewer, jsonText) {
4662
return jsonText;
4763
}
4864

49-
function highlightContent(pre, outsideViewer) {
65+
function highlightContent(pre, outsideViewer, ignoreLimit) {
5066
getOptions().then(function(options) {
51-
if (oversizedJSON(pre, options)) {
67+
if (!ignoreLimit && oversizedJSON(pre, options, outsideViewer)) {
5268
return pre.hidden = false;
5369
}
5470

extension/src/json-viewer/highlighter.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ Highlighter.prototype = {
9191
elements.forEach(function(node) {
9292
node.classList.add("cm-string-link");
9393
node.setAttribute("data-url", decodedText);
94+
if (self.wrapLinkWithAnchorTag()) {
95+
var linkTag = document.createElement("a");
96+
linkTag.href = decodedText;
97+
linkTag.setAttribute('target', '_blank')
98+
linkTag.innerHTML = decodedText;
99+
node.innerHTML = "";
100+
node.appendChild(linkTag);
101+
}
94102
});
95103
}
96104
});
@@ -200,6 +208,10 @@ Highlighter.prototype = {
200208
return this.options.addons.clickableUrls;
201209
},
202210

211+
wrapLinkWithAnchorTag: function() {
212+
return this.options.addons.wrapLinkWithAnchorTag;
213+
},
214+
203215
openLinksInNewWindow: function() {
204216
return this.options.addons.openLinksInNewWindow;
205217
},

extension/src/json-viewer/jsl-format.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ jsl.format = (function () {
1010
function repeat(s, count) {
1111
return new Array(count + 1).join(s);
1212
}
13-
13+
function getSizeOfArray(jsonString,startingPosition){
14+
var currentPosition = startingPosition + 1;
15+
var inString = false;
16+
var numOpened = 1;
17+
try{
18+
while (numOpened > 0 && currentPosition < jsonString.length) {
19+
var currentChar = jsonString.charAt(currentPosition)
20+
switch (currentChar) {
21+
case '[':
22+
if(!inString){
23+
numOpened++;
24+
}
25+
break;
26+
case ']':
27+
if(!inString){
28+
numOpened--;
29+
}
30+
break;
31+
case '"':
32+
inString = !inString;
33+
break;
34+
}
35+
currentPosition++;
36+
}
37+
return JSON.parse(jsonString.substring(startingPosition,currentPosition)).length;
38+
}
39+
catch(err){
40+
return null;
41+
}
42+
}
1443
function formatJson(json, options) {
1544
options = options || {};
1645
var tabSize = options.tabSize || 2;
1746
var indentCStyle = options.indentCStyle || false;
18-
47+
var showArraySize = (typeof options.showArraySize !== "undefined" ? Boolean(options.showArraySize) : true);
1948
var tab = "";
2049
for (var ts = 0; ts < tabSize; ts++) {
2150
tab += " ";
@@ -27,7 +56,6 @@ jsl.format = (function () {
2756
indentLevel = 0,
2857
inString = false,
2958
currentChar = null;
30-
3159
for (i = 0, il = json.length; i < il; i += 1) {
3260
currentChar = json.charAt(i);
3361

@@ -36,7 +64,17 @@ jsl.format = (function () {
3664
case '[':
3765
if (!inString) {
3866
if (indentCStyle) newJson += "\n" + repeat(tab, indentLevel);
39-
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
67+
if(currentChar === "["){
68+
if(showArraySize){
69+
var arraySize = getSizeOfArray(json,i);
70+
if(arraySize !== null){
71+
newJson += "Array[" + arraySize + "]";
72+
}
73+
}
74+
}
75+
newJson += currentChar;
76+
77+
newJson += "\n" + repeat(tab, indentLevel + 1);
4078
indentLevel += 1;
4179
} else {
4280
newJson += currentChar;
@@ -73,7 +111,10 @@ jsl.format = (function () {
73111
}
74112
break;
75113
case '"':
76-
if (i > 0 && (json.charAt(i - 1) !== '\\' || (json.charAt(i - 1) == '\\' && json.charAt(i - 2) == '\\'))) {
114+
if (i === 0) {
115+
inString = true;
116+
}
117+
else if (json.charAt(i - 1) !== '\\' || (json.charAt(i - 1) == '\\' && json.charAt(i - 2) == '\\')) {
77118
inString = !inString;
78119
}
79120
newJson += currentChar;

extension/src/json-viewer/options/defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
alwaysRenderAllContent: false,
88
sortKeys: false,
99
clickableUrls: true,
10+
wrapLinkWithAnchorTag: false,
1011
openLinksInNewWindow: true,
1112
autoHighlight: true
1213
},
@@ -16,7 +17,8 @@ module.exports = {
1617
lineWrapping: true,
1718
foldGutter: true,
1819
tabSize: 2,
19-
indentCStyle: false
20+
indentCStyle: false,
21+
showArraySize: false
2022
},
2123
style: [
2224
".CodeMirror {",

extension/src/json-viewer/viewer/render-alert.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
var loadCss = require('../load-css');
22

3-
function renderAlert(pre, options, message) {
3+
function renderAlert(pre, options, content) {
44
var alertContainer = document.createElement("div");
55
alertContainer.className = "json-viewer-alert";
6-
7-
var content = document.createElement("span");
8-
content.innerHTML = message;
96
alertContainer.appendChild(content);
107

118
var closeBtn = document.createElement("a");

extension/src/json-viewer/viewer/render-extras.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function renderExtras(pre, options, highlighter) {
4444
var unfoldLink = document.createElement("a");
4545
unfoldLink.className = "json_viewer icon unfold";
4646
unfoldLink.href = "#";
47-
unfoldLink.title = "Fold/Unfold all toogle";
47+
unfoldLink.title = "Fold/Unfold all toggle";
4848
unfoldLink.innerHTML = svgUnfold;
4949
unfoldLink.onclick = function(e) {
5050
e.preventDefault();

0 commit comments

Comments
 (0)