Skip to content

Commit

Permalink
Merge pull request #150 from tulios/fix-document-childNodes-checkup
Browse files Browse the repository at this point in the history
Reorganize the document childNodes checks
  • Loading branch information
tulios committed Oct 23, 2017
2 parents efacf2c + 8358352 commit 6d5eebd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
47 changes: 25 additions & 22 deletions extension/src/json-viewer/check-if-json.js
@@ -1,14 +1,18 @@
var extractJSON = require('./extract-json');

function allTextNodes(nodes) {
return Object.keys(nodes).reduce(function(result, key) {
return result && nodes[key].nodeName === '#text'
}, true)
return !Object.keys(nodes).some(function(key) {
return nodes[key].nodeName !== '#text'
})
}

function getPreWithSource() {
var childNodes = document.body.childNodes;

if (childNodes.length === 0) {
return null
}

if (childNodes.length > 1 && allTextNodes(childNodes)) {
if (process.env.NODE_ENV === 'development') {
console.debug("[JSONViewer] Loaded from a multiple text nodes, normalizing");
Expand All @@ -17,26 +21,25 @@ function getPreWithSource() {
document.body.normalize() // concatenates adjacent text nodes
}

if (childNodes.length === 1) {
var childNode = childNodes[0];
var nodeName = childNode.nodeName
var textContent = childNode.textContent

if (nodeName === "PRE") {
return childNode;

// if Content-Type is text/html
} else if (nodeName === "#text" && textContent.trim().length > 0) {
if (process.env.NODE_ENV === 'development') {
console.debug("[JSONViewer] Loaded from a text node, this might have returned content-type: text/html");
}

var pre = document.createElement("pre");
pre.textContent = textContent;
document.body.removeChild(childNode);
document.body.appendChild(pre);
return pre;
var childNode = childNodes[0];
var nodeName = childNode.nodeName
var textContent = childNode.textContent

if (nodeName === "PRE") {
return childNode;
}

// if Content-Type is text/html
if (nodeName === "#text" && textContent.trim().length > 0) {
if (process.env.NODE_ENV === 'development') {
console.debug("[JSONViewer] Loaded from a text node, this might have returned content-type: text/html");
}

var pre = document.createElement("pre");
pre.textContent = textContent;
document.body.removeChild(childNode);
document.body.appendChild(pre);
return pre;
}

return null
Expand Down
1 change: 1 addition & 0 deletions tests/geojson.json

Large diffs are not rendered by default.

0 comments on commit 6d5eebd

Please sign in to comment.