Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/django/utils/xmlutils.py: 42%
19 statements
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
1"""
2Utilities for XML generation/parsing.
3"""
5import re
6from xml.sax.saxutils import XMLGenerator
9class UnserializableContentError(ValueError):
10 pass
13class SimplerXMLGenerator(XMLGenerator):
14 def addQuickElement(self, name, contents=None, attrs=None):
15 "Convenience method for adding an element with no children"
16 if attrs is None:
17 attrs = {}
18 self.startElement(name, attrs)
19 if contents is not None:
20 self.characters(contents)
21 self.endElement(name)
23 def characters(self, content):
24 if content and re.search(r"[\x00-\x08\x0B-\x0C\x0E-\x1F]", content):
25 # Fail loudly when content has control chars (unsupported in XML 1.0)
26 # See https://www.w3.org/International/questions/qa-controls
27 raise UnserializableContentError(
28 "Control characters are not supported in XML 1.0"
29 )
30 XMLGenerator.characters(self, content)
32 def startElement(self, name, attrs):
33 # Sort attrs for a deterministic output.
34 sorted_attrs = dict(sorted(attrs.items())) if attrs else attrs
35 super().startElement(name, sorted_attrs)