1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/svg_events.py Fri Apr 06 18:38:22 2007 +0000
1.3 @@ -0,0 +1,92 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A test of SVG events using somewhat modified and fixed versions of various W3C
1.8 +examples and a tentative event handler initialisation mechanism.
1.9 +
1.10 +The specifications are explicit about things like .jar files and inline scripts,
1.11 +but remain vague about some of the mechanisms. Moreover, the initialiser
1.12 +interface appears to be part of the "global" object, yet treatment of that
1.13 +object is also vague, and the specifications focus on plugging in arbitrary
1.14 +initialisers via .jar files and their metadata.
1.15 +"""
1.16 +
1.17 +import libxml2dom.svg
1.18 +
1.19 +s = """\
1.20 +<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny"
1.21 + xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500">
1.22 + <script type="application/x-python" xlink:href=""/>
1.23 + <rect xml:id="therect" x="0" y="0" width="100" height="100"/>
1.24 +</svg>
1.25 +"""
1.26 +
1.27 +class Global(libxml2dom.svg.SVGGlobal):
1.28 +
1.29 + "An event handler initialiser for the above document."
1.30 +
1.31 + def initializeEventListeners(self, scriptElement):
1.32 + document = scriptElement.ownerDocument
1.33 + rect = document.getElementById("therect")
1.34 + rect.addEventListenerNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", Handler(), 0, None)
1.35 +
1.36 +class Impl(libxml2dom.svg.SVGImplementation):
1.37 +
1.38 + "A special implementation referring to the above global class."
1.39 +
1.40 + def get_global(self, doc):
1.41 + return Global(doc)
1.42 +
1.43 +class Handler:
1.44 +
1.45 + "An event handler."
1.46 +
1.47 + def handleEvent(self, event):
1.48 + print "Event handled with detail", event.detail
1.49 +
1.50 +d = libxml2dom.svg.parseString(s, impl=Impl())
1.51 +rect = d.getElementById("therect")
1.52 +event = d.createEvent("MouseEvent")
1.53 +event.initEventNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", 1, 1)
1.54 +event.detail = "1"
1.55 +rect.dispatchEvent(event)
1.56 +
1.57 +s2 = """\
1.58 +<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny"
1.59 + xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500"
1.60 + xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:exns="http://example.org/exNS">
1.61 + <script xml:id="init" type="application/x-python" xlink:href=""/>
1.62 + <rect xml:id="therect" x="0" y="0" width="100" height="100">
1.63 + <handler type="application/x-python" ev:event="click" xlink:href="#init" exns:listenerClass="Handler"/>
1.64 + </rect>
1.65 +</svg>
1.66 +"""
1.67 +
1.68 +class Global2(libxml2dom.svg.SVGGlobal):
1.69 +
1.70 + "An event handler initialiser for the above document."
1.71 +
1.72 + def createEventListener(self, handlerElement):
1.73 + listenerInstance = None
1.74 + try:
1.75 + listenerClass = handlerElement.getAttributeNS("http://example.org/exNS", "listenerClass")
1.76 + listenerInstance = globals()[listenerClass]()
1.77 + except:
1.78 + pass
1.79 + return listenerInstance
1.80 +
1.81 +class Impl2(libxml2dom.svg.SVGImplementation):
1.82 +
1.83 + "A special implementation referring to the above global class."
1.84 +
1.85 + def get_global(self, doc):
1.86 + return Global2(doc)
1.87 +
1.88 +d2 = libxml2dom.svg.parseString(s2, impl=Impl2())
1.89 +rect2 = d2.getElementById("therect")
1.90 +event2 = d2.createEvent("MouseEvent")
1.91 +event2.initEventNS(None, "click", 1, 1)
1.92 +event2.detail = "1"
1.93 +rect2.dispatchEvent(event2)
1.94 +
1.95 +# vim: tabstop=4 expandtab shiftwidth=4