mirror of https://github.com/python/cpython
Add support for the CharacterData methods, CDATASection.
This commit is contained in:
parent
c6a3cb4baf
commit
87432f42f9
|
@ -613,24 +613,76 @@ class ProcessingInstruction(Node):
|
||||||
def writexml(self, writer, indent="", addindent="", newl=""):
|
def writexml(self, writer, indent="", addindent="", newl=""):
|
||||||
writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl))
|
writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl))
|
||||||
|
|
||||||
class Text(Node):
|
class CharacterData(Node):
|
||||||
nodeType = Node.TEXT_NODE
|
|
||||||
nodeName = "#text"
|
|
||||||
attributes = None
|
|
||||||
childNodeTypes = ()
|
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
if type(data) not in _StringTypes:
|
if type(data) not in _StringTypes:
|
||||||
raise TypeError, "node contents must be a string"
|
raise TypeError, "node contents must be a string"
|
||||||
Node.__init__(self)
|
Node.__init__(self)
|
||||||
self.data = self.nodeValue = data
|
self.data = self.nodeValue = data
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
if name == "length":
|
||||||
|
return len(self.data)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if len(self.data) > 10:
|
if len(self.data) > 10:
|
||||||
dotdotdot = "..."
|
dotdotdot = "..."
|
||||||
else:
|
else:
|
||||||
dotdotdot = ""
|
dotdotdot = ""
|
||||||
return "<DOM Text node \"%s%s\">" % (self.data[0:10], dotdotdot)
|
return "<DOM %s node \"%s%s\">" % (
|
||||||
|
self.__class__.__name__, self.data[0:10], dotdotdot)
|
||||||
|
|
||||||
|
def substringData(self, offset, count):
|
||||||
|
if offset < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be negative")
|
||||||
|
if offset >= len(self.data):
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be beyond end of data")
|
||||||
|
if count < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("count cannot be negative")
|
||||||
|
return self.data[offset:offset+count]
|
||||||
|
|
||||||
|
def appendData(self, arg):
|
||||||
|
self.data = self.data + arg
|
||||||
|
self.nodeValue = self.data
|
||||||
|
|
||||||
|
def insertData(self, offset, arg):
|
||||||
|
if offset < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be negative")
|
||||||
|
if offset >= len(self.data):
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be beyond end of data")
|
||||||
|
if arg:
|
||||||
|
self.data = "%s%s%s" % (
|
||||||
|
self.data[:offset], arg, self.data[offset:])
|
||||||
|
self.nodeValue = self.data
|
||||||
|
|
||||||
|
def deleteData(self, offset, count):
|
||||||
|
if offset < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be negative")
|
||||||
|
if offset >= len(self.data):
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be beyond end of data")
|
||||||
|
if count < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("count cannot be negative")
|
||||||
|
if count:
|
||||||
|
self.data = self.data[:offset] + self.data[offset+count:]
|
||||||
|
self.nodeValue = self.data
|
||||||
|
|
||||||
|
def replaceData(self, offset, count, arg):
|
||||||
|
if offset < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be negative")
|
||||||
|
if offset >= len(self.data):
|
||||||
|
raise xml.dom.IndexSizeErr("offset cannot be beyond end of data")
|
||||||
|
if count < 0:
|
||||||
|
raise xml.dom.IndexSizeErr("count cannot be negative")
|
||||||
|
if count:
|
||||||
|
self.data = "%s%s%s" % (
|
||||||
|
self.data[:offset], arg, self.data[offset+count:])
|
||||||
|
self.nodeValue = self.data
|
||||||
|
|
||||||
|
class Text(CharacterData):
|
||||||
|
nodeType = Node.TEXT_NODE
|
||||||
|
nodeName = "#text"
|
||||||
|
attributes = None
|
||||||
|
childNodeTypes = ()
|
||||||
|
|
||||||
def splitText(self, offset):
|
def splitText(self, offset):
|
||||||
if offset < 0 or offset > len(self.data):
|
if offset < 0 or offset > len(self.data):
|
||||||
|
@ -648,6 +700,15 @@ class Text(Node):
|
||||||
def writexml(self, writer, indent="", addindent="", newl=""):
|
def writexml(self, writer, indent="", addindent="", newl=""):
|
||||||
_write_data(writer, "%s%s%s"%(indent, self.data, newl))
|
_write_data(writer, "%s%s%s"%(indent, self.data, newl))
|
||||||
|
|
||||||
|
|
||||||
|
class CDATASection(Text):
|
||||||
|
nodeType = Node.CDATA_SECTION_NODE
|
||||||
|
nodeName = "#cdata-section"
|
||||||
|
|
||||||
|
def writexml(self, writer, indent="", addindent="", newl=""):
|
||||||
|
_write_data(writer, "<![CDATA[%s]]>" % self.data)
|
||||||
|
|
||||||
|
|
||||||
def _nssplit(qualifiedName):
|
def _nssplit(qualifiedName):
|
||||||
fields = _string.split(qualifiedName, ':', 1)
|
fields = _string.split(qualifiedName, ':', 1)
|
||||||
if len(fields) == 2:
|
if len(fields) == 2:
|
||||||
|
@ -781,6 +842,11 @@ class Document(Node):
|
||||||
t.ownerDocument = self
|
t.ownerDocument = self
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
def createCDATASection(self, data):
|
||||||
|
c = CDATASection(data)
|
||||||
|
c.ownerDocument = self
|
||||||
|
return c
|
||||||
|
|
||||||
def createComment(self, data):
|
def createComment(self, data):
|
||||||
c = Comment(data)
|
c = Comment(data)
|
||||||
c.ownerDocument = self
|
c.ownerDocument = self
|
||||||
|
|
Loading…
Reference in New Issue