|
101 | 101 | from . import ElementPath |
102 | 102 |
|
103 | 103 |
|
| 104 | +# The white space characters of the XML specification (see XML 1.0, 2.3). |
| 105 | +_XML_WHITESPACE = " \t\r\n" |
| 106 | + |
104 | 107 | class ParseError(SyntaxError): |
105 | 108 | """An error when parsing an XML document. |
106 | 109 |
|
@@ -1197,17 +1200,17 @@ def _indent_children(elem, level): |
1197 | 1200 | child_indentation = indentations[level] + space |
1198 | 1201 | indentations.append(child_indentation) |
1199 | 1202 |
|
1200 | | - if not elem.text or not elem.text.strip(): |
| 1203 | + if not elem.text or not elem.text.strip(_XML_WHITESPACE): |
1201 | 1204 | elem.text = child_indentation |
1202 | 1205 |
|
1203 | 1206 | for child in elem: |
1204 | 1207 | if len(child): |
1205 | 1208 | _indent_children(child, child_level) |
1206 | | - if not child.tail or not child.tail.strip(): |
| 1209 | + if not child.tail or not child.tail.strip(_XML_WHITESPACE): |
1207 | 1210 | child.tail = child_indentation |
1208 | 1211 |
|
1209 | 1212 | # Dedent after the last child by overwriting the previous indentation. |
1210 | | - if not child.tail.strip(): |
| 1213 | + if not child.tail.strip(_XML_WHITESPACE): |
1211 | 1214 | child.tail = indentations[level] |
1212 | 1215 |
|
1213 | 1216 | _indent_children(tree, 0) |
@@ -1712,7 +1715,7 @@ def _default(self, text): |
1712 | 1715 | if prefix == ">": |
1713 | 1716 | self._doctype = None |
1714 | 1717 | return |
1715 | | - text = text.strip() |
| 1718 | + text = text.strip(_XML_WHITESPACE) |
1716 | 1719 | if not text: |
1717 | 1720 | return |
1718 | 1721 | self._doctype.append(text) |
@@ -1928,7 +1931,7 @@ def _flush(self, _join_text=''.join): |
1928 | 1931 | data = _join_text(self._data) |
1929 | 1932 | del self._data[:] |
1930 | 1933 | if self._strip_text and not self._preserve_space[-1]: |
1931 | | - data = data.strip() |
| 1934 | + data = data.strip(_XML_WHITESPACE) |
1932 | 1935 | if self._pending_start is not None: |
1933 | 1936 | args, self._pending_start = self._pending_start, None |
1934 | 1937 | qname_text = data if data and _looks_like_prefix_name(data) else None |
|
0 commit comments