Handle entity references as data, possibly converting known HTML and/or XML entity references to the corresponding Unicode characters.
(self, ref)
| 1401 | self.handle_data(data) |
| 1402 | |
| 1403 | def handle_entityref(self, ref): |
| 1404 | """Handle entity references as data, possibly converting known |
| 1405 | HTML and/or XML entity references to the corresponding Unicode |
| 1406 | characters.""" |
| 1407 | data = None |
| 1408 | if self.convertHTMLEntities: |
| 1409 | try: |
| 1410 | data = unichr(name2codepoint[ref]) |
| 1411 | except KeyError: |
| 1412 | pass |
| 1413 | |
| 1414 | if not data and self.convertXMLEntities: |
| 1415 | data = self.XML_ENTITIES_TO_SPECIAL_CHARS.get(ref) |
| 1416 | |
| 1417 | if not data and self.convertHTMLEntities and \ |
| 1418 | not self.XML_ENTITIES_TO_SPECIAL_CHARS.get(ref): |
| 1419 | # TODO: We've got a problem here. We're told this is |
| 1420 | # an entity reference, but it's not an XML entity |
| 1421 | # reference or an HTML entity reference. Nonetheless, |
| 1422 | # the logical thing to do is to pass it through as an |
| 1423 | # unrecognized entity reference. |
| 1424 | # |
| 1425 | # Except: when the input is "&carol;" this function |
| 1426 | # will be called with input "carol". When the input is |
| 1427 | # "AT&T", this function will be called with input |
| 1428 | # "T". We have no way of knowing whether a semicolon |
| 1429 | # was present originally, so we don't know whether |
| 1430 | # this is an unknown entity or just a misplaced |
| 1431 | # ampersand. |
| 1432 | # |
| 1433 | # The more common case is a misplaced ampersand, so I |
| 1434 | # escape the ampersand and omit the trailing semicolon. |
| 1435 | data = "&%s" % ref |
| 1436 | if not data: |
| 1437 | # This case is different from the one above, because we |
| 1438 | # haven't already gone through a supposedly comprehensive |
| 1439 | # mapping of entities to Unicode characters. We might not |
| 1440 | # have gone through any mapping at all. So the chances are |
| 1441 | # very high that this is a real entity, and not a |
| 1442 | # misplaced ampersand. |
| 1443 | data = "&%s;" % ref |
| 1444 | self.handle_data(data) |
| 1445 | |
| 1446 | def handle_decl(self, data): |
| 1447 | "Handle DOCTYPEs and the like as Declaration objects." |
nothing calls this directly
no test coverage detected