Commit 19315f4b authored by S Anand's avatar S Anand

BUG: if XML contains invalid characters, strip them out before downloading

parent 8e9827f9
...@@ -68,6 +68,12 @@ function svgize(node) { ...@@ -68,6 +68,12 @@ function svgize(node) {
return node return node
} }
// XML 1.0 only allows certain characters. Ignore the rest
// http://stackoverflow.com/a/28152666/100904
function xml_escape(s) {
return s.replace(/[^\x09\x0a\x0d\u0020-\ud7ff\ue000-\ufffd\u10000-\u10ffff]/g, '')
}
let download = function(options) { let download = function(options) {
var mime = options.mime, var mime = options.mime,
source = options.source, source = options.source,
...@@ -80,7 +86,7 @@ let download = function(options) { ...@@ -80,7 +86,7 @@ let download = function(options) {
node = svgize($(options.svg).get(0)) node = svgize($(options.svg).get(0))
if (!source) { if (!source) {
source = (new XMLSerializer()).serializeToString(node) source = xml_escape((new XMLSerializer()).serializeToString(node))
if (!options.nostyle) { if (!options.nostyle) {
sheets = document.styleSheets || [] sheets = document.styleSheets || []
...@@ -106,7 +112,7 @@ let download = function(options) { ...@@ -106,7 +112,7 @@ let download = function(options) {
node.setAttribute('width', node.getBoundingClientRect().width) node.setAttribute('width', node.getBoundingClientRect().width)
img = new Image() img = new Image()
source = (new XMLSerializer()).serializeToString(node) source = xml_escape((new XMLSerializer()).serializeToString(node))
blob = new Blob([source], {type : 'image/svg+xml;charset=utf-8'}) blob = new Blob([source], {type : 'image/svg+xml;charset=utf-8'})
url = URL.createObjectURL(blob) url = URL.createObjectURL(blob)
img.onload = function() { img.onload = function() {
...@@ -149,4 +155,4 @@ let download = function(options) { ...@@ -149,4 +155,4 @@ let download = function(options) {
} }
} }
export {download} export {download}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment