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 <g xml:id="group1" fill="red"> 21 <rect xml:id="therect" x="0" y="0" width="100" height="100"/> 22 </g> 23 </svg> 24 """ 25 26 class Global(libxml2dom.svg.SVGGlobal): 27 28 "An event handler initialiser for the above document." 29 30 def initializeEventListeners(self, scriptElement): 31 document = scriptElement.ownerDocument 32 rect = document.getElementById("therect") 33 rect.addEventListenerNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", Handler(), 0, None) 34 g = document.getElementById("group1") 35 g.addEventListenerNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", Handler(), 0, None) 36 37 class Impl(libxml2dom.svg.SVGImplementation): 38 39 "A special implementation referring to the above global class." 40 41 def get_global(self, doc): 42 return Global(doc) 43 44 class Handler: 45 46 "An event handler." 47 48 def handleEvent(self, event): 49 print "Event handled in", event.currentTarget.localName, "in phase", event.eventPhase 50 51 d = libxml2dom.svg.parseString(s, impl=Impl()) 52 rect = d.getElementById("therect") 53 event = d.createEvent("MouseEvent") 54 event.initEventNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "click", 1, 1) 55 event.detail = "1" 56 d.sendEventToTarget(event, rect) 57 58 s2 = """\ 59 <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" 60 xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" 61 xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:exns="http://example.org/exNS"> 62 <script xml:id="init" type="application/x-python" xlink:href=""/> 63 <g xml:id="group1" fill="red"> 64 <handler type="application/x-python" ev:event="click" ev:phase="capture" xlink:href="#init" exns:listenerClass="Handler"/> 65 <rect xml:id="therect" x="0" y="0" width="100" height="100"> 66 <handler type="application/x-python" ev:event="click" xlink:href="#init" exns:listenerClass="Handler"/> 67 </rect> 68 </g> 69 </svg> 70 """ 71 72 class Global2(libxml2dom.svg.SVGGlobal): 73 74 "An event handler initialiser for the above document." 75 76 def createEventListener(self, handlerElement): 77 listenerInstance = None 78 try: 79 listenerClass = handlerElement.getAttributeNS("http://example.org/exNS", "listenerClass") 80 listenerInstance = globals()[listenerClass]() 81 except: 82 pass 83 return listenerInstance 84 85 class Impl2(libxml2dom.svg.SVGImplementation): 86 87 "A special implementation referring to the above global class." 88 89 def get_global(self, doc): 90 return Global2(doc) 91 92 d2 = libxml2dom.svg.parseString(s2, impl=Impl2()) 93 rect2 = d2.getElementById("therect") 94 event2 = d2.createEvent("MouseEvent") 95 event2.initEventNS(None, "click", 1, 1) 96 event2.detail = "1" 97 d2.sendEventToTarget(event2, rect2) 98 99 # vim: tabstop=4 expandtab shiftwidth=4