1. Manfred Moitzi
  2. svgwrite

Commits

jmahmood  committed d236086

Added the <script> tag to the Drawing class.

Modified Drawing class so we can include scripts (Javascript, etc..). Also included appropriate changes to the testing functions.

  • Participants
  • Parent commits bec5a75
  • Branches default

Comments (0)

Files changed (2)

File svgwrite/drawing.py

View file
         super(Drawing, self).__init__(size=size, **extra)
         self.filename = filename
         self._stylesheets = [] # list of stylesheets appended
+        self._scriptlinks = []
+        self._scripts = []
 
     def get_xml(self):
         """ Get the XML representation as `ElementTree` object.
         """
         self._stylesheets.append( (href, title, alternate, media) )
 
+    def add_script(self, type, href=None, contents=None):
+        """ Add a script reference.  You should include a uri or the script contents. 
+
+        :param string type: type of script
+        :param string href: location of script <URI> (Optional)
+        :param string contents: script contents (Optional)
+
+        """
+        if href:
+            self._scriptlinks.append( (type, href) )
+            return
+
+        if contents:
+            self._scripts.append( (type, contents) )
+            return
+
+
     def write(self, fileobj):
         """ Write XML string to **fileobj**.
 
         # don't use DOCTYPE. It's useless. see also:
         # http://tech.groups.yahoo.com/group/svg-developers/message/48562
         # write stylesheets
-        for stylesheet in self._stylesheets:
-            stylestr = '<?xml-stylesheet href="%s" type="text/css" title="%s" ' \
-                     'alternate="%s" media="%s"?>\n' % stylesheet
-            fileobj.write(stylestr)
+        stylesheet_template = '<?xml-stylesheet href="%s" type="text/css" ' \
+                     'title="%s" alternate="%s" media="%s"?>\n'
+        map( lambda s: fileobj.write( stylesheet_template % s ), self._stylesheets)
+
+        script_template = '<script type="%s"><![CDATA[\n%s\n]]></script>\n'
+        map( lambda s: fileobj.write( script_template % s ), self._scripts)
+
+        scriptlink_template = '<script type="%s" xlink:href="%s"/>\n'
+        map( lambda s: fileobj.write( scriptlink_template % s ), self._scriptlinks)
+
         fileobj.write(self.tostring())
 
     def save(self):

File tests/test_drawing.py

View file
             'xmlns:ev="http://www.w3.org/2001/xml-events" '\
             'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
 
+    def test_script_link(self):
+        dwg = Drawing()
+        dwg.add_script(type="text/ecmascript", href='test.js')
+        f = StringIO()
+        dwg.write(f)
+        result = f.getvalue()
+        f.close()
+        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
+            '<script type="text/ecmascript" xlink:href="test.js"/>\n' \
+            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
+            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
+            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
+
+
+    def test_script_contents(self):
+        dwg = Drawing()
+        js = """        function stub() {
+            return {
+                of : function (name, callback, returnValue) {
+                    this[name] = function () {
+                      var args = Array.prototype.slice.call(arguments);
+                      this[name].calls.push(args);
+                      var ret = null;
+                      if(callback)
+                          ret = callback.apply(this, args);
+                      if(returnValue) return returnValue;
+                      return ret;
+                  };
+                  this[name].calls = [];
+         
+                  return this;
+                }
+            };
+        }"""
+        dwg.add_script(type="text/ecmascript", contents=js)
+        f = StringIO()
+        dwg.write(f)
+        result = f.getvalue()
+        f.close()
+        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
+            '<script type="text/ecmascript"><![CDATA[\n' \
+            + js + \
+            '\n]]></script>\n' \
+            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
+            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
+            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
+
+
     def test_stylesheet(self):
         dwg = Drawing()
         dwg.add_stylesheet('test.css', 'Test')
             'xmlns:ev="http://www.w3.org/2001/xml-events" '\
             'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
 
+
+    def test_script_link(self):
+        dwg = Drawing(profile="tiny")
+        dwg.add_script(type="text/ecmascript", href='test.js')
+        f = StringIO()
+        dwg.write(f)
+        result = f.getvalue()
+        f.close()
+        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
+            '<script type="text/ecmascript" xlink:href="test.js"/>\n'
+            '<svg baseProfile="tiny" height="100%" version="1.2" width="100%" '\
+            'xmlns="http://www.w3.org/2000/svg" '\
+            'xmlns:ev="http://www.w3.org/2001/xml-events" '\
+            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
+
+
     def test_stylesheet(self):
         dwg = Drawing(profile="tiny")
         dwg.add_stylesheet('test.css', 'Test')