-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
invalid XML element names #49
Labels
Comments
Instead of fixing this on a case by case basis, I believe it will be better in the long-run if we switched to a DOM implementation like jsdom. This will require a major rewrite though, so expect this in a future major release. |
Merged
With const xmlbuilder = require("xmlbuilder")
const nameStartCharRegex = /^[:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF]$/
const nameCharRegex = /^[\x2D\.0-:A-Z_a-z\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF]$/
const sanitizeName = function(name) {
const oldName = String(name)
if (oldName.length == 0) {
return "__unnamed__"
}
const newName = []
if (nameStartCharRegex.test(name[0])) {
newName.push(name[0])
} else {
newName.push("_")
// might be a valid char except in first position
if (nameCharRegex.test(name[0])) {
newName.push(name[0])
}
}
for (i = 1; i < name.length; i++) {
if (nameCharRegex.test(name[i])) {
newName.push(name[i])
} else {
newName.push("_")
}
}
return newName.join("")
}
let xml = xmlbuilder.create("root", {
encoding: "UTF-8",
stringify: {
eleName: sanitizeName
}
}) Note: I only tested the regexes briefly, but can not verify their correctness. Create with regenerate: const regenerate = require("regenerate")
const nameStartChar = regenerate()
.add(":", "_")
.addRange("A", "Z")
.addRange("a", "z")
.addRange(0xC0, 0xD6)
.addRange(0xD8, 0xF6)
.addRange(0xF8, 0x2FF)
.addRange(0x370, 0x37D)
.addRange(0x37F, 0x1FFF)
.addRange(0x200C, 0x200D)
.addRange(0x2070, 0x218F)
.addRange(0x2C00, 0x2FEF)
.addRange(0x3001, 0xD7FF)
.addRange(0xF900, 0xFDCF)
.addRange(0xFDF0, 0xFFFD)
.addRange(0x10000, 0xEFFFF)
const nameChar = regenerate()
.add(nameStartChar)
.add("-", ".", 0xB7)
.addRange("0", "9")
.addRange(0x0300, 0x036F)
.addRange(0x203F, 0x2040) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Invalid XML is easily generated from unsanitized arbitrary objects:
This will produce bad XML
<my name>John</my name>
. Maybe some escaping or sanitizing scheme should be implemented? ("my name" -> "my_name"
or similar).The text was updated successfully, but these errors were encountered: