comparison markup/input.py @ 209:fc6b2fb66518 trunk

* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC. * Enable the `XMLParser` to handle HTML entities without requiring the declaration of a HTML document type.
author cmlenz
date Tue, 29 Aug 2006 16:34:40 +0000
parents 28bfc6aafab7
children 13d2d4420628
comparison
equal deleted inserted replaced
208:bc146e63c159 209:fc6b2fb66518
78 parser.EndNamespaceDeclHandler = self._handle_end_ns 78 parser.EndNamespaceDeclHandler = self._handle_end_ns
79 parser.StartCdataSectionHandler = self._handle_start_cdata 79 parser.StartCdataSectionHandler = self._handle_start_cdata
80 parser.EndCdataSectionHandler = self._handle_end_cdata 80 parser.EndCdataSectionHandler = self._handle_end_cdata
81 parser.ProcessingInstructionHandler = self._handle_pi 81 parser.ProcessingInstructionHandler = self._handle_pi
82 parser.CommentHandler = self._handle_comment 82 parser.CommentHandler = self._handle_comment
83
84 # Tell Expat that we'll handle non-XML entities ourselves
85 # (in _handle_other)
83 parser.DefaultHandler = self._handle_other 86 parser.DefaultHandler = self._handle_other
87 parser.UseForeignDTD()
84 88
85 # Location reporting is only support in Python >= 2.4 89 # Location reporting is only support in Python >= 2.4
86 if not hasattr(parser, 'CurrentLineNumber'): 90 if not hasattr(parser, 'CurrentLineNumber'):
87 self._getpos = self._getpos_unknown 91 self._getpos = self._getpos_unknown
88 92
182 # deal with undefined entities 186 # deal with undefined entities
183 try: 187 try:
184 text = unichr(htmlentitydefs.name2codepoint[text[1:-1]]) 188 text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
185 self._enqueue(TEXT, text) 189 self._enqueue(TEXT, text)
186 except KeyError: 190 except KeyError:
187 lineno, offset = self._getpos() 191 filename, lineno, offset = self._getpos()
188 raise expat.error("undefined entity %s: line %d, column %d" % 192 error = expat.error('undefined entity "%s": line %d, column %d'
189 (text, lineno, offset)) 193 % (text, lineno, offset))
194 error.code = expat.errors.XML_ERROR_UNDEFINED_ENTITY
195 error.lineno = lineno
196 error.offset = offset
197 raise error
190 198
191 199
192 def XML(text): 200 def XML(text):
193 return Stream(list(XMLParser(StringIO(text)))) 201 return Stream(list(XMLParser(StringIO(text))))
194 202
Copyright (C) 2012-2017 Edgewall Software