Format XML with xml-but-prettier
This commit is contained in:
@@ -84,6 +84,7 @@
|
|||||||
"whatwg-fetch": "0.11.1",
|
"whatwg-fetch": "0.11.1",
|
||||||
"worker-loader": "^0.7.1",
|
"worker-loader": "^0.7.1",
|
||||||
"xml": "1.0.1",
|
"xml": "1.0.1",
|
||||||
|
"xml-but-prettier": "^1.0.1",
|
||||||
"yaml-js": "0.2.0"
|
"yaml-js": "0.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import PropTypes from "prop-types"
|
import PropTypes from "prop-types"
|
||||||
import { formatXml } from "core/utils"
|
import formatXml from "xml-but-prettier"
|
||||||
import lowerCase from "lodash/lowerCase"
|
import lowerCase from "lodash/lowerCase"
|
||||||
|
|
||||||
export default class ResponseBody extends React.Component {
|
export default class ResponseBody extends React.Component {
|
||||||
@@ -31,7 +31,9 @@ export default class ResponseBody extends React.Component {
|
|||||||
|
|
||||||
// XML
|
// XML
|
||||||
} else if (/xml/i.test(contentType)) {
|
} else if (/xml/i.test(contentType)) {
|
||||||
body = formatXml(content)
|
body = formatXml(content, {
|
||||||
|
textNodesOnSameLine: true
|
||||||
|
})
|
||||||
bodyEl = <HighlightCode value={ body } />
|
bodyEl = <HighlightCode value={ body } />
|
||||||
|
|
||||||
// HTML or Plain Text
|
// HTML or Plain Text
|
||||||
|
|||||||
@@ -155,76 +155,6 @@ export function getList(iterable, keys) {
|
|||||||
return Im.List.isList(val) ? val : Im.List()
|
return Im.List.isList(val) ? val : Im.List()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapted from http://stackoverflow.com/a/2893259/454004
|
|
||||||
// Note: directly ported from CoffeeScript
|
|
||||||
export function formatXml (xml) {
|
|
||||||
var contexp, fn, formatted, indent, l, lastType, len, lines, ln, reg, transitions, wsexp
|
|
||||||
reg = /(>)(<)(\/*)/g
|
|
||||||
wsexp = /[ ]*(.*)[ ]+\n/g
|
|
||||||
contexp = /(<.+>)(.+\n)/g
|
|
||||||
xml = xml.replace(/\r\n/g, "\n").replace(reg, "$1\n$2$3").replace(wsexp, "$1\n").replace(contexp, "$1\n$2")
|
|
||||||
formatted = ""
|
|
||||||
lines = xml.split("\n")
|
|
||||||
indent = 0
|
|
||||||
lastType = "other"
|
|
||||||
transitions = {
|
|
||||||
"single->single": 0,
|
|
||||||
"single->closing": -1,
|
|
||||||
"single->opening": 0,
|
|
||||||
"single->other": 0,
|
|
||||||
"closing->single": 0,
|
|
||||||
"closing->closing": -1,
|
|
||||||
"closing->opening": 0,
|
|
||||||
"closing->other": 0,
|
|
||||||
"opening->single": 1,
|
|
||||||
"opening->closing": 0,
|
|
||||||
"opening->opening": 1,
|
|
||||||
"opening->other": 1,
|
|
||||||
"other->single": 0,
|
|
||||||
"other->closing": -1,
|
|
||||||
"other->opening": 0,
|
|
||||||
"other->other": 0
|
|
||||||
}
|
|
||||||
fn = function(ln) {
|
|
||||||
var fromTo, padding, type
|
|
||||||
|
|
||||||
if (ln.match(/<.+\/>/)) {
|
|
||||||
type = "single"
|
|
||||||
} else if (ln.match(/<\/.+>/)) {
|
|
||||||
type = "closing"
|
|
||||||
} else if (ln.match(/<[^!?].*>/)) {
|
|
||||||
type = "opening"
|
|
||||||
} else {
|
|
||||||
type = "other"
|
|
||||||
}
|
|
||||||
fromTo = lastType + "->" + type
|
|
||||||
lastType = type
|
|
||||||
padding = ""
|
|
||||||
indent += transitions[fromTo]
|
|
||||||
padding = ((function() {
|
|
||||||
/* eslint-disable no-unused-vars */
|
|
||||||
var m, ref1, results, j
|
|
||||||
results = []
|
|
||||||
for (j = m = 0, ref1 = indent; 0 <= ref1 ? m < ref1 : m > ref1; j = 0 <= ref1 ? ++m : --m) {
|
|
||||||
results.push(" ")
|
|
||||||
}
|
|
||||||
/* eslint-enable no-unused-vars */
|
|
||||||
return results
|
|
||||||
})()).join("")
|
|
||||||
if (fromTo === "opening->closing") {
|
|
||||||
formatted = formatted.substr(0, formatted.length - 1) + ln + "\n"
|
|
||||||
} else {
|
|
||||||
formatted += padding + ln + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (l = 0, len = lines.length; l < len; l++) {
|
|
||||||
ln = lines[l]
|
|
||||||
fn(ln)
|
|
||||||
}
|
|
||||||
return formatted
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapted from http://github.com/asvd/microlight
|
* Adapted from http://github.com/asvd/microlight
|
||||||
* @copyright 2016 asvd <heliosframework@gmail.com>
|
* @copyright 2016 asvd <heliosframework@gmail.com>
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import {
|
|||||||
getAcceptControllingResponse,
|
getAcceptControllingResponse,
|
||||||
createDeepLinkPath,
|
createDeepLinkPath,
|
||||||
escapeDeepLinkPath,
|
escapeDeepLinkPath,
|
||||||
formatXml,
|
|
||||||
sanitizeUrl
|
sanitizeUrl
|
||||||
} from "core/utils"
|
} from "core/utils"
|
||||||
import win from "core/window"
|
import win from "core/window"
|
||||||
@@ -888,16 +887,6 @@ describe("utils", function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("formatXml", function() {
|
|
||||||
it("simple xml formatting", function() {
|
|
||||||
const startTime = Date.now()
|
|
||||||
const result = formatXml("<xml><name>john doe</name></xml>")
|
|
||||||
let duration = Date.now() - startTime
|
|
||||||
expect(result).toEqual("<xml>\n <name>john doe</name>\n</xml>\n")
|
|
||||||
expect(duration).toBeLessThan(5)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("sanitizeUrl", function() {
|
describe("sanitizeUrl", function() {
|
||||||
it("should sanitize a `javascript:` url", function() {
|
it("should sanitize a `javascript:` url", function() {
|
||||||
const res = sanitizeUrl("javascript:alert('bam!')")
|
const res = sanitizeUrl("javascript:alert('bam!')")
|
||||||
|
|||||||
Reference in New Issue
Block a user