1 #!/usr/bin/env python 2 3 """ 4 A test of SVG events using somewhat modified and fixed versions of various W3C 5 examples and a tentative event handler initialisation mechanism. 6 7 The specifications are explicit about things like .jar files and inline scripts, 8 but remain vague about some of the mechanisms. Moreover, the initialiser 9 interface appears to be part of the "global" object, yet treatment of that 10 object is also vague, and the specifications focus on plugging in arbitrary 11 initialisers via .jar files and their metadata. 12 """ 13 14 import libxml2dom.svg 15 16 s = """\ 17 <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" 18 xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500"> 19 <script type="application/x-python" xlink:href=""/> 20 <rect xml:id="therect" x="0" y="0" width="100" height="100"/> 21 </svg> 22 """ 23 24 class Global(libxml2dom.svg.SVGGlobal): 25 26 "An event handler initialiser for the above document." 27 28 def initializeEventListeners(self, scriptElement): 29 document = scriptElement.ownerDocument 30 rect = document.getElementById("therect") 31 rect.addEventListenerNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", Handler(), 0, None) 32 33 class Impl(libxml2dom.svg.SVGImplementation): 34 35 "A special implementation referring to the above global class." 36 37 def get_global(self, doc): 38 return Global(doc) 39 40 class Handler: 41 42 "An event handler." 43 44 def handleEvent(self, event): 45 print "Event handled with detail", event.detail 46 47 d = libxml2dom.svg.parseString(s, impl=Impl()) 48 rect = d.getElementById("therect") 49 event = d.createEvent("MouseEvent") 50 event.initEventNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", 1, 1) 51 event.detail = "1" 52 rect.dispatchEvent(event) 53 54 s2 = """\ 55 <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" 56 xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" 57 xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:exns="http://example.org/exNS"> 58 <script xml:id="init" type="application/x-python" xlink:href=""/> 59 <rect xml:id="therect" x="0" y="0" width="100" height="100"> 60 <handler type="application/x-python" ev:event="click" xlink:href="#init" exns:listenerClass="Handler"/> 61 </rect> 62 </svg> 63 """ 64 65 class Global2(libxml2dom.svg.SVGGlobal): 66 67 "An event handler initialiser for the above document." 68 69 def createEventListener(self, handlerElement): 70 listenerInstance = None 71 try: 72 listenerClass = handlerElement.getAttributeNS("http://example.org/exNS", "listenerClass") 73 listenerInstance = globals()[listenerClass]() 74 except: 75 pass 76 return listenerInstance 77 78 class Impl2(libxml2dom.svg.SVGImplementation): 79 80 "A special implementation referring to the above global class." 81 82 def get_global(self, doc): 83 return Global2(doc) 84 85 d2 = libxml2dom.svg.parseString(s2, impl=Impl2()) 86 rect2 = d2.getElementById("therect") 87 event2 = d2.createEvent("MouseEvent") 88 event2.initEventNS(None, "click", 1, 1) 89 event2.detail = "1" 90 rect2.dispatchEvent(event2) 91 92 # vim: tabstop=4 expandtab shiftwidth=4