summaryrefslogtreecommitdiffstats
path: root/Lib/xml
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-04-04 14:09:46 (GMT)
committerFred Drake <fdrake@acm.org>2001-04-04 14:09:46 (GMT)
commit87432f42f953920912b9780f0ae0d7c41cb349f2 (patch)
tree9fbc3769d91bed2d3afb7703d7f8974c584c0b3f /Lib/xml
parentc6a3cb4baf70e6a36dba99c11c1084fd95d72779 (diff)
downloadcpython-87432f42f953920912b9780f0ae0d7c41cb349f2.zip
cpython-87432f42f953920912b9780f0ae0d7c41cb349f2.tar.gz
cpython-87432f42f953920912b9780f0ae0d7c41cb349f2.tar.bz2
Add support for the CharacterData methods, CDATASection.
Diffstat (limited to 'Lib/xml')
-rw-r--r--Lib/xml/dom/minidom.py80
1 files changed, 73 insertions, 7 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 81315e7..26923b5 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -613,24 +613,76 @@ class ProcessingInstruction(Node):
def writexml(self, writer, indent="", addindent="", newl=""):
writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl))
-class Text(Node):
- nodeType = Node.TEXT_NODE
- nodeName = "#text"
- attributes = None
- childNodeTypes = ()
-
+class CharacterData(Node):
def __init__(self, data):
if type(data) not in _StringTypes:
raise TypeError, "node contents must be a string"
Node.__init__(self)
self.data = self.nodeValue = data
+ def __getattr__(self, name):
+ if name == "length":
+ return len(self.data)
+
def __repr__(self):
if len(self.data) > 10:
dotdotdot = "..."
else:
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):
if offset < 0 or offset > len(self.data):
@@ -648,6 +700,15 @@ class Text(Node):
def writexml(self, writer, indent="", addindent="", 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):
fields = _string.split(qualifiedName, ':', 1)
if len(fields) == 2:
@@ -781,6 +842,11 @@ class Document(Node):
t.ownerDocument = self
return t
+ def createCDATASection(self, data):
+ c = CDATASection(data)
+ c.ownerDocument = self
+ return c
+
def createComment(self, data):
c = Comment(data)
c.ownerDocument = self