Commits

jmahmood committed c33cfb4

Moved <script> to container file, created proper class file.

The script tag can't be used in the same way as the stylesheet is used. To that end, I've moved it into its own class and put it in the container file, with appropriate modifications ot the other files (drawing / element factory). I've tested and it seems to work fine.

I also added an appropriate test file to show the expected output.

I don't suppose anyone will actually want to put their content directly into the SVG file so I haven't added that functionality.

  • Participants
  • Parent commits d236086

Comments (0)

Files changed (5)

File svgwrite/container.py

         :param 2-tuple insert: reference point (**refX**, **refY**)
         :param 2-tuple size: (**markerWidth**, **markerHeight**)
         :param orient: ``'auto'`` | `angle`
-        :param extra: additional SVG attributs as keyword-arguments
+        :param extra: additional SVG attributes as keyword-arguments
         """
         super(Marker, self).__init__(**extra)
         if insert:
         """
         :param 2-tuple insert: insert position (**x**, **y**)
         :param 2-tuple size: (**width**, **height**)
-        :param extra: additional SVG attributs as keyword-arguments
+        :param extra: additional SVG attributes as keyword-arguments
         """
         super(SVG, self).__init__(**extra)
         if insert:
         :param string href: object link (id-string) or an object with an id-attribute
         :param 2-tuple insert: insert point (**x**, **y**)
         :param 2-tuple size: (**width**, **height**)
-        :param extra: additional SVG attributs as keyword-arguments
+        :param extra: additional SVG attributes as keyword-arguments
         """
         super(Use, self).__init__(**extra)
         self.set_href(href)
 class Hyperlink(BaseElement, Transform, Presentation):
     """ The **a** element indicate links (also known as Hyperlinks or Web links).
 
-    The remote resource (the destination for the link) is defined by a `<IRI>`
+    The remote resource (the destination for the link) is defined by a `<URI>`
     specified by the XLink **xlink:href** attribute. The remote resource may be
     any Web resource (e.g., an image, a video clip, a sound bite, a program,
     another SVG document, an HTML document, an element within the current
         """
         :param string href: hyperlink to the target resource
         :param string target: ``'_blank|_replace|_self|_parent|_top|<XML-name>'``
-        :param extra: additional SVG attributs as keyword-arguments
+        :param extra: additional SVG attributes as keyword-arguments
         """
         super(Hyperlink, self).__init__(**extra)
         self['xlink:href'] = href
         self['target'] = target
+
+
+class Script(BaseElement, Presentation):
+    """ The **script** element indicate links to a client-side language.  This
+    is normally a  (also known as Hyperlinks or Web links).
+
+    The remote resource (the source of the script) is defined by a `<URI>`
+    specified by the XLink **xlink:href** attribute. The remote resource must
+    be a text-file that contains the script contents.  This script can be used
+    within the SVG file by catching events or adding the mouseover/mousedown/
+    mouseup elements to the markup.
+
+    """
+
+    elementname = 'script'
+    def __init__(self, href, type='text/ecmascript', **extra):
+        """
+        :param string href: hyperlink to the target resource
+        :param string type: This is the type of script being used.
+        :param extra: additional attributes as keyword-arguments
+        """
+        super(Script, self).__init__(**extra)
+        self['xlink:href'] = href
+        self['type'] = type

File svgwrite/drawing.py

         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**.
 
         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 svgwrite/elementfactory.py

     'marker': container.Marker,
     'use': container.Use,
     'a': container.Hyperlink,
+    'script': container.Script,
     'line': shapes.Line,
     'rect': shapes.Rect,
     'circle': shapes.Circle,

File tests/test_drawing.py

             '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')

File tests/test_script.py

+#!/usr/bin/env python
+#coding:utf-8
+# Author:  mozman --<mozman@gmx.at>
+# Purpose: test svg element
+# Created: 25.09.2010
+# Copyright (C) 2010, Manfred Moitzi
+# License: GPLv3
+
+import sys
+import unittest
+
+from svgwrite.container import Script
+
+class TestUse(unittest.TestCase):
+    def test_constructor(self):
+        script = Script('test.js', "text/ecmascript")
+        self.assertEqual(script.tostring(), '<script type="text/ecmascript" xlink:href="test.js" />')
+
+if __name__=='__main__':
+    unittest.main()