From cead5f417bb61e7da360dff6e83cbcaf79e5224b Mon Sep 17 00:00:00 2001 From: seunggabi Date: Sun, 3 Nov 2019 09:08:56 +0900 Subject: [PATCH 1/5] refact(-): syntax --- extension/src/json-viewer/check-if-json.js | 11 ++++---- .../src/json-viewer/content-extractor.js | 26 +++++++++---------- extension/src/json-viewer/highlighter.js | 6 ++--- extension/src/json-viewer/jsl-format.js | 5 ++-- .../json-viewer/options/bind-reset-button.js | 1 - .../json-viewer/options/bind-save-button.js | 1 - extension/src/json-viewer/options/defaults.js | 2 +- .../json-viewer/options/render-theme-list.js | 6 ++--- extension/src/json-viewer/storage.js | 6 ++--- extension/src/json-viewer/theme-darkness.js | 2 +- .../src/json-viewer/viewer/render-alert.js | 2 +- .../src/json-viewer/viewer/render-extras.js | 8 +++--- 12 files changed, 37 insertions(+), 39 deletions(-) diff --git a/extension/src/json-viewer/check-if-json.js b/extension/src/json-viewer/check-if-json.js index 5160fde..2ddd7aa 100644 --- a/extension/src/json-viewer/check-if-json.js +++ b/extension/src/json-viewer/check-if-json.js @@ -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; @@ -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) } diff --git a/extension/src/json-viewer/content-extractor.js b/extension/src/json-viewer/content-extractor.js index 59a6f8f..063192d 100644 --- a/extension/src/json-viewer/content-extractor.js +++ b/extension/src/json-viewer/content-extractor.js @@ -6,7 +6,7 @@ var TOKEN = (Math.random() + 1).toString(36).slice(2, 7); var WRAP_START = ""; var WRAP_END = ""; 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" @@ -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; } @@ -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; @@ -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; diff --git a/extension/src/json-viewer/highlighter.js b/extension/src/json-viewer/highlighter.js index 77f60b5..7a6cec0 100644 --- a/extension/src/json-viewer/highlighter.js +++ b/extension/src/json-viewer/highlighter.js @@ -92,7 +92,7 @@ Highlighter.prototype = { 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 @@ -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"; @@ -230,6 +230,6 @@ Highlighter.prototype = { isReadOny: function() { return this.options.structure.readOnly; } -} +}; module.exports = Highlighter; diff --git a/extension/src/json-viewer/jsl-format.js b/extension/src/json-viewer/jsl-format.js index ac38ab5..033b078 100644 --- a/extension/src/json-viewer/jsl-format.js +++ b/extension/src/json-viewer/jsl-format.js @@ -16,7 +16,7 @@ jsl.format = (function () { var numOpened = 1; try{ while (numOpened > 0 && currentPosition < jsonString.length) { - var currentChar = jsonString.charAt(currentPosition) + var currentChar = jsonString.charAt(currentPosition); switch (currentChar) { case '[': if(!inString){ @@ -114,7 +114,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; diff --git a/extension/src/json-viewer/options/bind-reset-button.js b/extension/src/json-viewer/options/bind-reset-button.js index fc353eb..d94ce0b 100644 --- a/extension/src/json-viewer/options/bind-reset-button.js +++ b/extension/src/json-viewer/options/bind-reset-button.js @@ -25,7 +25,6 @@ function bindResetButton() { Storage.save(options); document.location.reload(); - }); } } diff --git a/extension/src/json-viewer/options/bind-save-button.js b/extension/src/json-viewer/options/bind-save-button.js index 5563134..82c5620 100644 --- a/extension/src/json-viewer/options/bind-save-button.js +++ b/extension/src/json-viewer/options/bind-save-button.js @@ -19,7 +19,6 @@ function bindSaveButton(editors, onSaveClicked) { } onSaveClicked(output); - } } diff --git a/extension/src/json-viewer/options/defaults.js b/extension/src/json-viewer/options/defaults.js index 88c3e29..82afaf3 100644 --- a/extension/src/json-viewer/options/defaults.js +++ b/extension/src/json-viewer/options/defaults.js @@ -27,4 +27,4 @@ module.exports = { " line-height: 1.5em;", "}" ].join('\n') -} +}; diff --git a/extension/src/json-viewer/options/render-theme-list.js b/extension/src/json-viewer/options/render-theme-list.js index 9ef9706..ad4207b 100644 --- a/extension/src/json-viewer/options/render-theme-list.js +++ b/extension/src/json-viewer/options/render-theme-list.js @@ -15,7 +15,7 @@ var themeJSONExample = { "and fake keys" ] } -} +}; function onThemeChange(input, editor) { var selectedTheme = input.options[input.selectedIndex].value; @@ -60,7 +60,7 @@ function renderThemeList(CodeMirror, value) { themes.onchange = function() { onThemeChange(themesInput, themeEditor); - } + }; var optionSelected = value; themesInput.appendChild(createOption(themeDefault, optionSelected)); @@ -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) { diff --git a/extension/src/json-viewer/storage.js b/extension/src/json-viewer/storage.js index 6f3382f..a07775c 100644 --- a/extension/src/json-viewer/storage.js +++ b/extension/src/json-viewer/storage.js @@ -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; @@ -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) { @@ -60,4 +60,4 @@ module.exports = { return optionsStr; } -} +}; diff --git a/extension/src/json-viewer/theme-darkness.js b/extension/src/json-viewer/theme-darkness.js index 974deb4..4a03090 100644 --- a/extension/src/json-viewer/theme-darkness.js +++ b/extension/src/json-viewer/theme-darkness.js @@ -4,4 +4,4 @@ module.exports = function(name) { if (themes.dark.indexOf(name) !== -1) darkness = "dark"; return darkness; -} +}; diff --git a/extension/src/json-viewer/viewer/render-alert.js b/extension/src/json-viewer/viewer/render-alert.js index 4009e62..99e53fc 100644 --- a/extension/src/json-viewer/viewer/render-alert.js +++ b/extension/src/json-viewer/viewer/render-alert.js @@ -13,7 +13,7 @@ function renderAlert(pre, options, content) { closeBtn.onclick = function(e) { e.preventDefault(); alertContainer.parentNode.removeChild(alertContainer); - } + }; alertContainer.appendChild(closeBtn); diff --git a/extension/src/json-viewer/viewer/render-extras.js b/extension/src/json-viewer/viewer/render-extras.js index 14c173f..d266343 100644 --- a/extension/src/json-viewer/viewer/render-extras.js +++ b/extension/src/json-viewer/viewer/render-extras.js @@ -39,7 +39,7 @@ function renderExtras(pre, options, highlighter) { pre.hidden = true; extras.className = extras.className.replace(/\s+auto-highlight-off/, ''); } - } + }; var unfoldLink = document.createElement("a"); unfoldLink.className = "json_viewer icon unfold"; @@ -48,7 +48,7 @@ function renderExtras(pre, options, highlighter) { unfoldLink.innerHTML = svgUnfold; unfoldLink.onclick = function(e) { e.preventDefault(); - var value = pre.getAttribute('data-folded') + var value = pre.getAttribute('data-folded'); if (value === 'true' || value === true) { highlighter.unfoldAll(); @@ -58,14 +58,14 @@ function renderExtras(pre, options, highlighter) { highlighter.fold(); pre.setAttribute('data-folded', true) } - } + }; extras.appendChild(optionsLink); extras.appendChild(rawLink); // "awaysFold" was a typo but to avoid any problems I'll keep it // a while - pre.setAttribute('data-folded', options.addons.alwaysFold || options.addons.awaysFold) + pre.setAttribute('data-folded', options.addons.alwaysFold || options.addons.awaysFold); extras.appendChild(unfoldLink); document.body.appendChild(extras); From 1b7eee28e84f5a9288348dafb701948e344d3af9 Mon Sep 17 00:00:00 2001 From: seunggabi Date: Sun, 3 Nov 2019 18:17:33 +0900 Subject: [PATCH 2/5] feat(-): gitignore modified --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 297ae2a..13f2b7f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ build/* pkg/* node_modules/* *.log +.idea/* +yarn.lock From c664d26574757b322a8b461a28fdc64b7bdb8373 Mon Sep 17 00:00:00 2001 From: seunggabi Date: Sun, 3 Nov 2019 18:55:54 +0900 Subject: [PATCH 3/5] feat(#241): count object field size --- extension/pages/options.html | 3 + extension/src/json-viewer/jsl-format.js | 57 +++++++++++++------ .../json-viewer/options/bind-save-button.js | 2 +- extension/src/options.js | 7 +++ 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/extension/pages/options.html b/extension/pages/options.html index 39da0ff..3d13269 100644 --- a/extension/pages/options.html +++ b/extension/pages/options.html @@ -78,6 +78,9 @@

Structure

  • "showArraySize" - Default false
  • +
  • + "showObjectSize" - Default false +
  • diff --git a/extension/src/json-viewer/jsl-format.js b/extension/src/json-viewer/jsl-format.js index 033b078..f4eb9b3 100644 --- a/extension/src/json-viewer/jsl-format.js +++ b/extension/src/json-viewer/jsl-format.js @@ -3,14 +3,19 @@ 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; @@ -18,12 +23,12 @@ jsl.format = (function () { while (numOpened > 0 && currentPosition < jsonString.length) { var currentChar = jsonString.charAt(currentPosition); switch (currentChar) { - case '[': + case openChar: if(!inString){ numOpened++; } break; - case ']': + case closeChar: if(!inString){ numOpened--; } @@ -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 += " "; @@ -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 { @@ -129,8 +147,11 @@ jsl.format = (function () { return newJson; } - return { "formatJson": formatJson }; - + return { + "formatJson": formatJson, + "getSizeOfArray": getSizeOfArray, + "getSizeOfObject": getSizeOfObject, + }; }()); module.exports = jsl.format.formatJson; diff --git a/extension/src/json-viewer/options/bind-save-button.js b/extension/src/json-viewer/options/bind-save-button.js index 82c5620..44245c7 100644 --- a/extension/src/json-viewer/options/bind-save-button.js +++ b/extension/src/json-viewer/options/bind-save-button.js @@ -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) { diff --git a/extension/src/options.js b/extension/src/options.js index 6b0170a..e94ddd4 100644 --- a/extension/src/options.js +++ b/extension/src/options.js @@ -27,6 +27,10 @@ function isValidJSON(pseudoJSON) { } } +function clearShowSize(json) { + return json.replace(/(Array|Object)\[\d+\]/gi, ''); +} + function renderVersion() { var version = process.env.VERSION; var versionLink = document.getElementsByClassName('version')[0]; @@ -45,6 +49,9 @@ function onLoaded() { bindResetButton(); bindSaveButton([addonsEditor, structureEditor, styleEditor], function(options) { + options.addons = clearShowSize(options.addons); + options.structure = clearShowSize(options.structure); + if (!isValidJSON(options.addons)) { sweetAlert("Ops!", "\"Add-ons\" isn't a valid JSON", "error"); From 7b02e69e553c297a9a73b0e1f3b1910787c57861 Mon Sep 17 00:00:00 2001 From: seunggabi Date: Mon, 4 Nov 2019 16:55:36 +0900 Subject: [PATCH 4/5] fix(#241): firstChild is null --- extension/src/json-viewer/highlighter.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extension/src/json-viewer/highlighter.js b/extension/src/json-viewer/highlighter.js index 7a6cec0..98445db 100644 --- a/extension/src/json-viewer/highlighter.js +++ b/extension/src/json-viewer/highlighter.js @@ -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; @@ -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) {}; } From 25c5b1658bd193e84c65f85e4da296efc2187a33 Mon Sep 17 00:00:00 2001 From: seunggabi Date: Mon, 4 Nov 2019 16:59:55 +0900 Subject: [PATCH 5/5] fix(#241): '' not url --- extension/src/json-viewer/highlighter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/src/json-viewer/highlighter.js b/extension/src/json-viewer/highlighter.js index 98445db..3e4bf55 100644 --- a/extension/src/json-viewer/highlighter.js +++ b/extension/src/json-viewer/highlighter.js @@ -86,7 +86,7 @@ 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()) {