Permalink
Browse files

Encode SVGs as base64 encoded data in an image tag to prevent cross-t…

…alk between styles.
  • Loading branch information...
1 parent 66a567b commit 5f698a30e291b608c59b349d00fe11d82cb2db1a @stefanv committed Jun 7, 2012
@@ -270,7 +270,7 @@ var IPython = (function (IPython) {
OutputArea.prototype.append_svg = function (svg, element) {
var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_svg");
- toinsert.append(svg);
+ toinsert.append($("<img/>").attr('src','data:image/svg+xml;base64,'+svg));
element.append(toinsert);
};
@@ -41,6 +41,8 @@ def restore_bytes(nb):
output.png = str_to_bytes(output.png, 'ascii')
if 'jpeg' in output:
output.jpeg = str_to_bytes(output.jpeg, 'ascii')
+ if 'svg' in output:
+ output.svg = str_to_bytes(output.svg, 'ascii')
return nb
# output keys that are likely to have multiline values
@@ -119,6 +121,11 @@ def base64_decode(nb):
if isinstance(output.jpeg, unicode):
output.jpeg = output.jpeg.encode('ascii')
output.jpeg = decodestring(output.jpeg)
+ if 'svg' in output:
+ if isinstance(output.svg, unicode):
+ output.svg = output.svg.encode('ascii')
+ output.svg = decodestring(output.svg)
+
return nb
@@ -137,6 +144,9 @@ def base64_encode(nb):
output.png = encodestring(output.png).decode('ascii')
if 'jpeg' in output:
output.jpeg = encodestring(output.jpeg).decode('ascii')
+ if 'svg' in output:
+ output.svg = encodestring(output.svg).decode('ascii')
+
return nb
@@ -38,7 +38,10 @@ def _encode_binary(format_dict):
jpegdata = format_dict.get('image/jpeg')
if isinstance(jpegdata, bytes):
encoded['image/jpeg'] = encodestring(jpegdata).decode('ascii')
-
+ svgdata = format_dict.get('image/svg+xml')
+ if isinstance(svgdata, bytes):
+ encoded['image/svg+xml'] = encodestring(svgdata).decode('ascii')
+
return encoded

0 comments on commit 5f698a3

Please sign in to comment.