-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically convert character references in HTMLParser #57842
Comments
The doc for handle_charref and handle_entityref say: HTMLParser.handle_entityref(name)
This method is called to process a general entity reference of the form "&name;" where name is an general entity reference. It is intended to be overridden by a derived class; the base class implementation does nothing.
""" The doc doesn't mention hex references, like ">", and apparently they are passed to handle_charref without the '&#' but with the leading 'x': >>> from HTMLParser import HTMLParser
>>> class MyParser(HTMLParser):
... def handle_charref(self, data):
... print data
...
>>> MyParser().feed('> > >')
62
x3E I've seen code in the wild doing unichr(int(data)) in handle_charref (once they figured out that '62' is passed) and then fail when an hex entity is found. Passing 'x3E' doesn't seem too useful because the user has to first check if there's a leading 'x', if there is remove it, then convert the hex string to int, and finally use unichr() to get the char, otherwise just convert to int and use unichr(). There 3 different possible solutions:
The first solution alone doesn't solve much, but the doc should be clearer regardless of the decision we take. |
This behavior is now documented, but the situation could still be improved. Adding a new method that receives the converted entity seems a good way to handle this. The parser can call both, and users can pick either one. One problem with the current methods (handle_charref and handle_entityref) is that they don't do any processing on the entity and let invalid character references like � or &#iamnotanentity; go through. There are at least 3 changes that should be done in order to follow the HTML5 standard 0:
Now, 1) can be done for both the old and new method, but for 2) and 3) the situation is a bit more complicated. The best thing is probably to keep sending them unchanged to the old methods, and implement the correct behavior for the new method only. |
Another option is to add a new "convert_entities" option that, when True, automatically converts character references and doesn't call handle_charref and handle_entityref. (See also bpo-17802.) |
Here is a patch. |
I have added a couple of nitpicks on Rietveld. You can ignore most of them. ;) |
New patch attached. |
New changeset 1575f2dd08c4 by Ezio Melotti in branch 'default': |
Fixed, thanks for the reviews! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: