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