From 5d961317750c6eef4a3551199efa2e15c6865fad 2011-12-11 00:59:04 From: MinRK Date: 2011-12-11 00:59:04 Subject: [PATCH] extract svg tag from svg files We expect svg *tags*, not svg *documents*, which have headers, etc. This lets the SVG DisplayObject extract the svg tag from a document, so it works with regular svgs from a URL or file. closes #700 --- diff --git a/IPython/core/display.py b/IPython/core/display.py index 40f4a57..7f5cc6d 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -17,6 +17,8 @@ Authors: # Imports #----------------------------------------------------------------------------- +from xml.dom import minidom + from .displaypub import ( publish_pretty, publish_html, publish_latex, publish_svg, @@ -300,7 +302,32 @@ class Math(DisplayObject): class SVG(DisplayObject): - + + # wrap data in a property, which extracts the tag, discarding + # document headers + _data = None + + @property + def data(self): + return self._data + + @data.setter + def data(self, svg): + if svg is None: + self._data = None + return + # parse into dom object + x = minidom.parseString(svg) + # get svg tag (should be 1) + found_svg = x.getElementsByTagName('svg') + if found_svg: + svg = found_svg[0].toxml() + else: + # fallback on the input, trust the user + # but this is probably an error. + pass + self._data = svg + def _repr_svg_(self): return self.data