113
115
return ElementTree().parse(f)
116
# performance tuning for elementree's serialiser. This should be
117
# sent upstream - RBC 20060523.
118
# the functions here are patched into elementtree at runtime.
120
escape_re = re.compile("[&'\"<>]")
123
"'":"'", # FIXME: overkill
128
def _escape_replace(match, map=escape_map):
129
return map[match.group()]
131
def _escape_attrib(text, encoding=None, replace=None):
132
# escape attribute value
136
text = elementtree.ElementTree._encode(text, encoding)
138
return elementtree.ElementTree._encode_entity(text)
140
return escape_re.sub(_escape_replace, text)
142
text = replace(text, "&", "&")
143
text = replace(text, "'", "'") # FIXME: overkill
144
text = replace(text, "\"", """)
145
text = replace(text, "<", "<")
146
text = replace(text, ">", ">")
148
except (TypeError, AttributeError):
149
elementtree.ElementTree._raise_serialization_error(text)
151
elementtree.ElementTree._escape_attrib = _escape_attrib
153
escape_cdata_re = re.compile("[&<>]")
159
def _escape_cdata_replace(match, map=escape_cdata_map):
160
return map[match.group()]
162
def _escape_cdata(text, encoding=None, replace=None):
163
# escape character data
167
text = elementtree.ElementTree._encode(text, encoding)
169
return elementtree.ElementTree._encode_entity(text)
171
return escape_cdata_re.sub(_escape_cdata_replace, text)
173
text = replace(text, "&", "&")
174
text = replace(text, "<", "<")
175
text = replace(text, ">", ">")
177
except (TypeError, AttributeError):
178
elementtree.ElementTree._raise_serialization_error(text)
180
elementtree.ElementTree._escape_cdata = _escape_cdata
183
118
def escape_invalid_chars(message):
184
119
"""Escape the XML-invalid characters in a commit message.