Skip to content
Merged
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
47 changes: 25 additions & 22 deletions extension/src/json-viewer/check-if-json.js
Original file line number Diff line number Diff line change
@@ -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.