📄 ftnode.py
字号:
########################################################################
#
# File Name: Node.py
#
# Documentation: http://docs.4suite.com/4DOM/Node.py.html
#
"""
Implements the basic tree structure of DOM
WWW: http://4suite.com/4DOM e-mail: support@4suite.com
Copyright (c) 2000 Fourthought Inc, USA. All Rights Reserved.
See http://4suite.com/COPYRIGHT for license and copyright information
"""
from DOMImplementation import implementation
import Event
from xml.dom import Node
from xml.dom import NoModificationAllowedErr
from xml.dom import NamespaceErr
from xml.dom import NotFoundErr
from xml.dom import NotSupportedErr
from xml.dom import HierarchyRequestErr
from xml.dom import WrongDocumentErr
from xml.dom import InvalidCharacterErr
from xml.dom import UnspecifiedEventTypeErr
from xml.dom import XML_NAMESPACE
import re, copy
#FIXME: should allow combining characters: fix when Python gets Unicode
g_namePattern = re.compile(r'[a-zA-Z_:][\w\.\-_:]*\Z')
g_pattPrefix = re.compile(r'[a-zA-Z_][\w\.\-_]*\Z')
class FtNode(Event.EventTarget, Node):
"""
Encapsulates the pieces that DOM builds on the basic tree structure,
Which is implemented by composition of TreeNode
"""
nodeType = None
# Children that this node is allowed to have
_allowedChildren = []
def __init__(self,
ownerDocument,
namespaceURI=None,
prefix=None,
localName=None):
Event.EventTarget.__init__(self)
self.__dict__['__nodeName'] = None
self.__dict__['__nodeValue'] = None
self.__dict__['__parentNode'] = None
self.__dict__['__childNodes'] = None
self.__dict__['__previousSibling'] = None
self.__dict__['__nextSibling'] = None
self.__dict__['__attributes'] = None
self.__dict__['__ownerDocument'] = ownerDocument
self.__dict__['__namespaceURI'] = namespaceURI
self.__dict__['__prefix'] = prefix
self.__dict__['__localName'] = localName
self.__dict__['__childNodes'] = implementation._4dom_createNodeList([])
self.__dict__['__readOnly'] = 0
### Attribute Access Methods -- Node.attr ###
def __getattr__(self, name):
attrFunc = self._readComputedAttrs.get(name)
if attrFunc:
return attrFunc(self)
else:
return getattr(FtNode, name)
def __setattr__(self, name, value):
#Make sure attribute is not read-only
if name in self.__class__._readOnlyAttrs:
raise NoModificationAllowedErr()
#If it's computed execute that function
attrFunc = self.__class__._writeComputedAttrs.get(name)
if attrFunc:
attrFunc(self, value)
#Otherwise, just set the attribute
else:
self.__dict__[name] = value
### Attribute Methods -- Node._get_attr() ###
def _get_nodeName(self):
return self.__dict__['__nodeName']
def _get_nodeValue(self):
return self.__dict__['__nodeValue']
def _set_nodeValue(self,value):
self.__dict__['__nodeValue'] = value
def _get_nodeType(self):
return getattr(self.__class__, 'nodeType')
def _get_parentNode(self):
return self.__dict__['__parentNode']
def _get_childNodes(self):
return self.__dict__['__childNodes']
def _get_firstChild(self):
cn = self.__dict__['__childNodes']
return cn and cn[0] or None
def _get_lastChild(self):
cn = self.__dict__['__childNodes']
return cn and cn[-1] or None
def _get_previousSibling(self):
return self.__dict__['__previousSibling']
def _get_nextSibling(self):
return self.__dict__['__nextSibling']
def _get_ownerDocument(self):
return self.__dict__['__ownerDocument']
def _get_attributes(self):
return self.__dict__['__attributes']
def _get_namespaceURI(self):
return self.__dict__['__namespaceURI']
def _get_prefix(self):
return self.__dict__['__prefix']
def _set_prefix(self, value):
# Check for invalid characters
if not g_namePattern.match(value):
raise InvalidCharacterErr()
if (self.__dict__['__namespaceURI'] is None or
':' in value or
(value == 'xml' and
self.__dict__['__namespaceURI'] != XML_NAMESPACE)):
raise NamespaceErr()
self.__dict__['__prefix'] = value
self.__dict__['__nodeName'] = '%s:%s' % (
value,
self.__dict__['__localName'])
def _get_localName(self):
return self.__dict__['__localName']
### Methods ###
def insertBefore(self, newChild, refChild):
if refChild is None:
return self.appendChild(newChild)
elif newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE:
while newChild.firstChild:
self.insertBefore(newChild.firstChild, refChild)
else:
#Make sure the newChild is all it is cracked up to be
self._4dom_validateNode(newChild)
#Make sure the refChild is indeed our child
try:
index = self.__dict__['__childNodes'].index(refChild)
except:
raise NotFoundErr()
#Remove from old parent
if newChild.parentNode != None:
newChild.parentNode.removeChild(newChild);
#Insert it
self.__dict__['__childNodes'].insert(index, newChild)
#Update the child caches
newChild._4dom_setHierarchy(self, refChild.previousSibling, refChild)
newChild._4dom_fireMutationEvent('DOMNodeInserted',relatedNode=self)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return newChild
def replaceChild(self, newChild, oldChild):
if newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE:
refChild = oldChild.nextSibling
self.removeChild(oldChild)
self.insertBefore(newChild, refChild)
else:
self._4dom_validateNode(newChild)
#Make sure the oldChild is indeed our child
try:
index = self.__dict__['__childNodes'].index(oldChild)
except:
raise NotFoundErr()
self.__dict__['__childNodes'][index] = newChild
if newChild.parentNode is not None:
newChild.parentNode.removeChild(newChild)
newChild._4dom_setHierarchy(self,
oldChild.previousSibling,
oldChild.nextSibling)
oldChild._4dom_fireMutationEvent('DOMNodeRemoved',relatedNode=self)
oldChild._4dom_setHierarchy(None, None, None)
newChild._4dom_fireMutationEvent('DOMNodeInserted',relatedNode=self)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return oldChild
def removeChild(self, childNode):
#Make sure the childNode is indeed our child
#FIXME: more efficient using list.remove()
try:
self.__dict__['__childNodes'].remove(childNode)
except:
raise NotFoundErr()
childNode._4dom_fireMutationEvent('DOMNodeRemoved',relatedNode=self)
self._4dom_fireMutationEvent('DOMSubtreeModified')
# Adjust caches
prev = childNode.previousSibling
next = childNode.nextSibling
if prev:
prev.__dict__['__nextSibling'] = next
if next:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -