Skip to content

Commit

Permalink
Merge pull request #1130 from OpenTreeOfLife/fix-entities-in-svg-down…
Browse files Browse the repository at this point in the history
…load

Fix entities in SVG download
  • Loading branch information
jar398 authored Jun 1, 2017
2 parents 1897b23 + 4b2b7ce commit d6497ca
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions curator/static/js/study-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10499,8 +10499,31 @@ function updateSaveTreeViewLink() {
$treeSVG.prepend( $treeStylesheet[0].outerHTML );
}

// encode the current SVG
var base64src = b64EncodeUnicode( $treeSVG[0].outerHTML );
// Serialize the main SVG node (converting HTML entities to Unicode); first, we
// create a temporary XML document
var serializer = new XMLSerializer();
var tempXMLDoc = document.implementation.createDocument(
null, // desired namespace, or null
"svg", // name of top-level element
null // desired doctype, or null
)
// replace its boring top-level element with our SVG
// (cloned from the original, else it disappears!)
var htmlNode = $treeSVG.clone()[0];
tempXMLDoc.documentElement.replaceWith(htmlNode);
var xmlNode = tempXMLDoc.documentElement;
var svgString = serializer.serializeToString(xmlNode);

/* Alternate implmentation (fails in Safari)
var serializer = new XMLSerializer();
var node = $treeSVG[0];
var svgString = serializer.serializeToString(node);
// Safari may still fail to convert HTML entities :-/
svgString = svgString.replace(/ /gi,' ');
*/

// encode it for safe use in a data URI
var base64src = b64EncodeUnicode(svgString);

var $saveLink = $('#save-tree-view');
$saveLink.attr('href', 'data:image/svg+xml;base64,\n'+ base64src);
Expand Down

0 comments on commit d6497ca

Please sign in to comment.