# HG changeset patch # User paulb # Date 1173745005 0 # Node ID 98c4b591d9b06d707bf22640226217e831a0175e # Parent 35414ebe6a437eed24a0b6d6616ee2f361e396b6 [project @ 2007-03-13 00:16:45 by paulb] Added an adoptDocument method to Implementation classes, along with usage of the method in the libxml2dom.parse* functions. Expanded the SVG support slightly. Added an events module which, when complete, will provide events support for the SVG elements. diff -r 35414ebe6a43 -r 98c4b591d9b0 libxml2dom/events.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libxml2dom/events.py Tue Mar 13 00:16:45 2007 +0000 @@ -0,0 +1,94 @@ +#!/usr/bin/env python + +""" +DOM events support. + +Copyright (C) 2007 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +""" + +class EventException(Exception): + + UNSPECIFIED_EVENT_TYPE_ERR = 0 + DISPATCH_REQUEST_ERR = 1 + +class Event: + + CAPTURING_PHASE = 1 + AT_TARGET = 2 + BUBBLING_PHASE = 3 + + def __init__(self): + type + target + currentTarget + eventPhase + bubbles + cancelable + timeStamp + namespaceURI + defaultPrevented + + def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): + pass + + def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): + pass + + def initEventNS(self, namespaceURIArg, eventTypeArg, canBubbleArg, cancelableArg): + pass + + def preventDefault(self): + pass + + def stopPropagation(self): + pass + + def stopImmediatePropagation(self): + pass + +class EventTarget: + + "An event target class." + + def __init__(self): + self.listeners = {} + + def addEventListener(self, type, listener, useCapture): + self.addEventListenerNS(None, type, listener, useCapture) + + def addEventListenerNS(self, namespaceURI, type, listener, useCapture): + if not self.listeners.has_key((namespaceURI, type)): + self.listeners[(namespaceURI, type)] = [] + self.listeners[(namespaceURI, type)].append((listener, useCapture)) + + def dispatchEvent(self, evt): + if not evt.type: + raise EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR) + # NOTE: Dispatch on namespaceURI, type... + + def removeEventListener(self, type, listener, useCapture): + self.removeEventListenerNS(None, type, listener, useCapture) + + def removeEventListenerNS(self, namespaceURI, type, listener, useCapture): + if self.listeners.has_key((namespaceURI, type)): + listeners = self.listeners[(namespaceURI, type)] + try: + listeners.remove((listener, useCapture)) + except ValueError: + pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 35414ebe6a43 -r 98c4b591d9b0 libxml2dom/svg.py --- a/libxml2dom/svg.py Tue Mar 13 00:15:13 2007 +0000 +++ b/libxml2dom/svg.py Tue Mar 13 00:16:45 2007 +0000 @@ -1,7 +1,8 @@ #!/usr/bin/env python """ -SVG-specific document support. +SVG-specific document support. See: +http://www.w3.org/TR/SVGMobile12/python-binding.html Copyright (C) 2007 Paul Boddie @@ -29,20 +30,51 @@ toString as Node_toString, toStream as Node_toStream, \ toFile as Node_toFile +SVG_NAMESPACE = "http://www.w3.org/2000/svg" + class SVGImplementation(libxml2dom.Implementation): "Contains an SVG-specific implementation." + # Wrapping of documents. + + def adoptDocument(self, node): + return SVGDocument(node, self) + + # Factory functions. + def get_node(self, _node, context_node): if Node_nodeType(_node) == context_node.ELEMENT_NODE and \ - Node_namespaceURI(_node) == "http://www.w3.org/2000/svg" and \ - Node_localName(_node) == "svg": + Node_namespaceURI(_node) == SVG_NAMESPACE: - return SVGElement(_node, self, context_node.ownerDocument) + if Node_localName(_node) == "svg": + return SVGSVGElement(_node, self, context_node.ownerDocument) + else: + return SVGElement(_node, self, context_node.ownerDocument) else: return libxml2dom.Implementation.get_node(self, _node, context_node) -class SVGElement(libxml2dom.Node): +class SVGElement(libxml2dom.Node): # Element, EventTarget, TraitAccess, ElementTraversal + + "An SVG-specific element." + + def _id(self): + return self.getAttribute("id") + + def _setId(self, value): + self.setAttribute("id", value) + + id = property(_id, _setId) + +class SVGLocatable: + + "A locatable interface." + +class SVGLocatableElement(SVGElement, SVGLocatable): + + "A locatable element." + +class SVGSVGElement(SVGLocatableElement): # SVGTimedElement "An SVG-specific top-level element." @@ -58,6 +90,14 @@ NAV_LEFT = 10 NAV_UP_LEFT = 11 +class SVGDocument(libxml2dom.Document): + + "An SVG-specific document node." + + # NOTE: Define global (SVGGlobal). + +# Convenience functions. + def parse(stream_or_string, html=0): return libxml2dom.parse(stream_or_string, html, default_impl)