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

Simplify formatXml #3858

Closed
wants to merge 4 commits into from
Closed
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
29 changes: 11 additions & 18 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,17 @@ export function formatXml (xml) {
"other->other": 0
}
fn = function(ln) {
var fromTo, key, padding, type, types, value
types = {
single: Boolean(ln.match(/<.+\/>/)),
closing: Boolean(ln.match(/<\/.+>/)),
opening: Boolean(ln.match(/<[^!?].*>/))
}
type = ((function() {
var results
results = []
for (key in types) {
value = types[key]
if (value) {
results.push(key)
}
}
return results
})())[0]
type = type === void 0 ? "other" : type
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 = ""
Expand Down
12 changes: 12 additions & 0 deletions test/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getAcceptControllingResponse,
createDeepLinkPath,
escapeDeepLinkPath,
formatXml,
sanitizeUrl
} from "core/utils"
import win from "core/window"
Expand Down Expand Up @@ -887,6 +888,16 @@ 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() {
it("should sanitize a `javascript:` url", function() {
const res = sanitizeUrl("javascript:alert('bam!')")
Expand Down Expand Up @@ -925,4 +936,5 @@ sbG8iKTs8L3NjcmlwdD4=`)
expect(sanitizeUrl({})).toEqual("")
})
})

})