Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show count for Object #241 #242

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ build/*
pkg/*
node_modules/*
*.log
.idea/*
yarn.lock
3 changes: 3 additions & 0 deletions extension/pages/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ <h2>Structure</h2>
<li>
<strong>"showArraySize"</strong> - <span class="hint">Default false</span>
</li>
<li>
<strong>"showObjectSize"</strong> - <span class="hint">Default false</span>
</li>
</ul>
</div>
</header>
Expand Down
11 changes: 6 additions & 5 deletions extension/src/json-viewer/check-if-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function getPreWithSource() {
}

var childNode = childNodes[0];
var nodeName = childNode.nodeName
var textContent = childNode.textContent
var nodeName = childNode.nodeName;
var textContent = childNode.textContent;

if (nodeName === "PRE") {
return childNode;
Expand Down Expand Up @@ -61,9 +61,10 @@ function isJSON(jsonStr) {
return false
}

str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '')
str = str
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
return (/^[\],:{}\s]*$/).test(str)
}

Expand Down
26 changes: 12 additions & 14 deletions extension/src/json-viewer/content-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var TOKEN = (Math.random() + 1).toString(36).slice(2, 7);
var WRAP_START = "<wrap_" + TOKEN + ">";
var WRAP_END = "</wrap_" + TOKEN +">";
var NUM_REGEX = /^-?\d+\.?\d*([eE]\+)?\d*$/g;
var ESCAPED_REGEX = "(-?\\d+\\.?\\d*([eE]\\+)?\\d*)"
var ESCAPED_REGEX = "(-?\\d+\\.?\\d*([eE]\\+)?\\d*)";

var WRAP_REGEX = new RegExp(
"^" + WRAP_START + ESCAPED_REGEX + WRAP_END + "$", "g"
Expand Down Expand Up @@ -80,7 +80,7 @@ function wrapNumbers(text) {
for (var i = 0, len = text.length; i < len; i++) {
var char = text[i];

if (char == '"' && !charIsEscaped) {
if (char === '"' && !charIsEscaped) {
isInString = !isInString;
}

Expand All @@ -102,11 +102,10 @@ function wrapNumbers(text) {
}

// this applies to the _next_ character - the one used in the next iteration
charIsEscaped = (char == '\\') ? !charIsEscaped : false
charIsEscaped = (char === '\\') ? !charIsEscaped : false;

if (isInNumber) {
numberBuffer += char;

} else {
buffer += char;
beforePrevious = previous;
Expand All @@ -119,19 +118,18 @@ function wrapNumbers(text) {

function isCharInNumber(char, previous) {
return ('0' <= char && char <= '9') ||
('0' <= previous && previous <= '9' && (char == 'e' || char == 'E')) ||
(('e' == previous || 'E' == previous) && char == '+') ||
char == '.' ||
char == '-';
('0' <= previous && previous <= '9' && char.toUpperCase() === 'E') ||
(char.toUpperCase() === 'E' && char === '+') ||
char === '.' ||
char === '-';
}

function isCharInString(char, previous) {
function isCharInString(char) {
return ('0' > char || char > '9') &&
char != 'e' &&
char != 'E' &&
char != '+' &&
char != '.' &&
char != '-';
char.toUpperCase() !== 'E' &&
char !== '+' &&
char !== '.' &&
char !== '-';
}

module.exports = contentExtractor;
20 changes: 10 additions & 10 deletions extension/src/json-viewer/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ Highlighter.prototype = {

var text = self.removeQuotes(textContent);

if (text.match(URL_PATTERN) && self.clickableUrls()) {
if (text.match(URL_PATTERN) && self.clickableUrls() && text !== "") {
var decodedText = self.decodeText(text);
elements.forEach(function(node) {
if (self.wrapLinkWithAnchorTag()) {
var linkTag = document.createElement("a");
linkTag.href = decodedText;
linkTag.setAttribute('target', '_blank')
linkTag.setAttribute('target', '_blank');
linkTag.classList.add("cm-string");

// reparent the child nodes to preserve the cursor when editing
Expand Down Expand Up @@ -121,7 +121,7 @@ Highlighter.prototype = {
this.editor.on("mousedown", function(cm, event) {
var element = event.target;
if (element.classList.contains("cm-string-link")) {
var url = element.getAttribute("data-url")
var url = element.getAttribute("data-url");
var target = "_self";
if (self.openLinksInNewWindow()) {
target = "_blank";
Expand All @@ -142,20 +142,20 @@ Highlighter.prototype = {
decodeText: function(text) {
var div = document.createElement("div");
div.innerHTML = text;
return div.firstChild.nodeValue;
return div.firstChild ? div.firstChild.nodeValue : "";
},

getEditorOptions: function() {
var obligatory = {
value: this.text,
theme: this.theme,
readOnly: this.isReadOny() ? true : false,
readOnly: this.isReadOny(),
mode: "application/ld+json",
indentUnit: 2,
tabSize: 2,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
extraKeys: this.getExtraKeysMap()
}
};

if (this.alwaysRenderAllContent()) {
obligatory.viewportMargin = Infinity;
Expand All @@ -174,16 +174,16 @@ Highlighter.prototype = {
cm.setSelection(cm.getCursor());
cm.focus();
}
}
};

if (this.options.structure.readOnly) {
extraKeyMap["Enter"] = function(cm) {
CodeMirror.commands.findNext(cm);
}
};

extraKeyMap["Shift-Enter"] = function(cm) {
CodeMirror.commands.findPrev(cm);
}
};

extraKeyMap["Ctrl-V"] = extraKeyMap["Cmd-V"] = function(cm) {};
}
Expand Down Expand Up @@ -230,6 +230,6 @@ Highlighter.prototype = {
isReadOny: function() {
return this.options.structure.readOnly;
}
}
};

module.exports = Highlighter;
62 changes: 42 additions & 20 deletions extension/src/json-viewer/jsl-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ var jsl = typeof jsl === 'undefined' ? {} : jsl;

/**
* jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).
*
**/
jsl.format = (function () {

function repeat(s, count) {
return new Array(count + 1).join(s);
}
function getSizeOfArray(jsonString,startingPosition){
function getSizeOfArray(jsonString, startingPosition){
return countSizeBy(jsonString, startingPosition, '[', ']');

}
function getSizeOfObject(jsonString, startingPosition){
return countSizeBy(jsonString, startingPosition, '{', '}');
}
function countSizeBy(jsonString, startingPosition, openChar, closeChar) {
var currentPosition = startingPosition + 1;
var inString = false;
var numOpened = 1;
try{
while (numOpened > 0 && currentPosition < jsonString.length) {
var currentChar = jsonString.charAt(currentPosition)
var currentChar = jsonString.charAt(currentPosition);
switch (currentChar) {
case '[':
case openChar:
if(!inString){
numOpened++;
}
break;
case ']':
case closeChar:
if(!inString){
numOpened--;
}
Expand All @@ -34,17 +39,35 @@ jsl.format = (function () {
}
currentPosition++;
}
return JSON.parse(jsonString.substring(startingPosition,currentPosition)).length;
return Object.keys(JSON.parse(jsonString.substring(startingPosition,currentPosition))).length;
}
catch(err){
return null;
}
}
function getSize(currentChar, json, i, options) {
var target;
var isShow;
var arraySize;

switch (currentChar) {
case '[': target = 'Array'; break;
case '{': target = 'Object'; break;
default: return null;
}

isShow = (typeof options['show'+target+'Size'] !== "undefined" ? Boolean(options['show'+target+'Size']) : false);
if(!isShow) {
return null;
}

arraySize = jsl.format['getSizeOf'+target](json, i);
return arraySize == null ? null : target + "[" + arraySize + "]";
}
function formatJson(json, options) {
options = options || {};
var tabSize = options.tabSize || 2;
var indentCStyle = options.indentCStyle || false;
var showArraySize = (typeof options.showArraySize !== "undefined" ? Boolean(options.showArraySize) : false);
var tab = "";
for (var ts = 0; ts < tabSize; ts++) {
tab += " ";
Expand All @@ -64,16 +87,11 @@ jsl.format = (function () {
case '[':
if (!inString) {
if (indentCStyle) newJson += "\n" + repeat(tab, indentLevel);
if(currentChar === "["){
if(showArraySize){
var arraySize = getSizeOfArray(json,i);
if(arraySize !== null){
newJson += "Array[" + arraySize + "]";
}
}
}
newJson += currentChar;

var size = getSize(currentChar, json, i, options);
if(size) newJson += size;

newJson += currentChar;
newJson += "\n" + repeat(tab, indentLevel + 1);
indentLevel += 1;
} else {
Expand Down Expand Up @@ -114,7 +132,8 @@ jsl.format = (function () {
if (i === 0) {
inString = true;
}
else if (json.charAt(i - 1) !== '\\' || (json.charAt(i - 1) == '\\' && json.charAt(i - 2) == '\\')) {
else if (json.charAt(i - 1) !== '\\' ||
(json.charAt(i - 1) === '\\' && json.charAt(i - 2) === '\\')) {
inString = !inString;
}
newJson += currentChar;
Expand All @@ -128,8 +147,11 @@ jsl.format = (function () {
return newJson;
}

return { "formatJson": formatJson };

return {
"formatJson": formatJson,
"getSizeOfArray": getSizeOfArray,
"getSizeOfObject": getSizeOfObject,
};
}());

module.exports = jsl.format.formatJson;
1 change: 0 additions & 1 deletion extension/src/json-viewer/options/bind-reset-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function bindResetButton() {

Storage.save(options);
document.location.reload();

});
}
}
Expand Down
3 changes: 1 addition & 2 deletions extension/src/json-viewer/options/bind-save-button.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function bindSaveButton(editors, onSaveClicked) {
var form = document.getElementById("options");
form.onsubmit = function() { return false; }
form.onsubmit = function() { return false; };

var saveButton = document.getElementById("save");
saveButton.onclick = function(e) {
Expand All @@ -19,7 +19,6 @@ function bindSaveButton(editors, onSaveClicked) {
}

onSaveClicked(output);

}
}

Expand Down
2 changes: 1 addition & 1 deletion extension/src/json-viewer/options/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ module.exports = {
" line-height: 1.5em;",
"}"
].join('\n')
}
};
6 changes: 3 additions & 3 deletions extension/src/json-viewer/options/render-theme-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var themeJSONExample = {
"and fake keys"
]
}
}
};

function onThemeChange(input, editor) {
var selectedTheme = input.options[input.selectedIndex].value;
Expand Down Expand Up @@ -60,7 +60,7 @@ function renderThemeList(CodeMirror, value) {

themes.onchange = function() {
onThemeChange(themesInput, themeEditor);
}
};

var optionSelected = value;
themesInput.appendChild(createOption(themeDefault, optionSelected));
Expand All @@ -74,7 +74,7 @@ function renderThemeList(CodeMirror, value) {

function createOption(theme, optionSelected) {
var option = document.createElement("option");
option.value = theme
option.value = theme;
option.text = theme;

if (theme === optionSelected) {
Expand Down
6 changes: 3 additions & 3 deletions extension/src/json-viewer/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
options = optionsStr ? JSON.parse(optionsStr) : {};
options.theme = options.theme || defaults.theme;
options.addons = options.addons ? JSON.parse(options.addons) : {};
options.addons = merge({}, defaults.addons, options.addons)
options.addons = merge({}, defaults.addons, options.addons);
options.structure = options.structure ? JSON.parse(options.structure) : defaults.structure;
options.style = options.style && options.style.length > 0 ? options.style : defaults.style;
return options;
Expand All @@ -36,7 +36,7 @@ module.exports = {
options.addons = {
prependHeader: JSON.parse(oldOptions.prependHeader || defaults.addons.prependHeader),
maxJsonSize: parseInt(oldOptions.maxJsonSize || defaults.addons.maxJsonSize, 10)
}
};

// Update to at least the new max value
if (options.addons.maxJsonSize < defaults.addons.maxJsonSize) {
Expand All @@ -60,4 +60,4 @@ module.exports = {

return optionsStr;
}
}
};
2 changes: 1 addition & 1 deletion extension/src/json-viewer/theme-darkness.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = function(name) {
if (themes.dark.indexOf(name) !== -1) darkness = "dark";

return darkness;
}
};
2 changes: 1 addition & 1 deletion extension/src/json-viewer/viewer/render-alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function renderAlert(pre, options, content) {
closeBtn.onclick = function(e) {
e.preventDefault();
alertContainer.parentNode.removeChild(alertContainer);
}
};

alertContainer.appendChild(closeBtn);

Expand Down
Loading