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