# HG changeset patch # User paulb # Date 1175884702 0 # Node ID 84ee9a8a8d288921aab6654d3ce0511137fe5e08 # Parent 5d8c0e00789ea3b27d8b69ae5765017438d9cadc [project @ 2007-04-06 18:38:22 by paulb] Added a test of SVG event handling. diff -r 5d8c0e00789e -r 84ee9a8a8d28 tests/svg_events.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/svg_events.py Fri Apr 06 18:38:22 2007 +0000 @@ -0,0 +1,92 @@ +#!/usr/bin/env python + +""" +A test of SVG events using somewhat modified and fixed versions of various W3C +examples and a tentative event handler initialisation mechanism. + +The specifications are explicit about things like .jar files and inline scripts, +but remain vague about some of the mechanisms. Moreover, the initialiser +interface appears to be part of the "global" object, yet treatment of that +object is also vague, and the specifications focus on plugging in arbitrary +initialisers via .jar files and their metadata. +""" + +import libxml2dom.svg + +s = """\ + + + + +""" + +class Global(libxml2dom.svg.SVGGlobal): + + "An event handler initialiser for the above document." + + def initializeEventListeners(self, scriptElement): + document = scriptElement.ownerDocument + rect = document.getElementById("therect") + rect.addEventListenerNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", Handler(), 0, None) + +class Impl(libxml2dom.svg.SVGImplementation): + + "A special implementation referring to the above global class." + + def get_global(self, doc): + return Global(doc) + +class Handler: + + "An event handler." + + def handleEvent(self, event): + print "Event handled with detail", event.detail + +d = libxml2dom.svg.parseString(s, impl=Impl()) +rect = d.getElementById("therect") +event = d.createEvent("MouseEvent") +event.initEventNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", 1, 1) +event.detail = "1" +rect.dispatchEvent(event) + +s2 = """\ + + + + + + +""" + +class Global2(libxml2dom.svg.SVGGlobal): + + "An event handler initialiser for the above document." + + def createEventListener(self, handlerElement): + listenerInstance = None + try: + listenerClass = handlerElement.getAttributeNS("http://example.org/exNS", "listenerClass") + listenerInstance = globals()[listenerClass]() + except: + pass + return listenerInstance + +class Impl2(libxml2dom.svg.SVGImplementation): + + "A special implementation referring to the above global class." + + def get_global(self, doc): + return Global2(doc) + +d2 = libxml2dom.svg.parseString(s2, impl=Impl2()) +rect2 = d2.getElementById("therect") +event2 = d2.createEvent("MouseEvent") +event2.initEventNS(None, "click", 1, 1) +event2.detail = "1" +rect2.dispatchEvent(event2) + +# vim: tabstop=4 expandtab shiftwidth=4