-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-137836: Support more RAWTEXT and PLAINTEXT elements in HTMLParser #137837
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
Changes from 8 commits
2153a4c
66827e0
c8429be
2219106
a46e28b
9971a24
69a2b33
08f4835
428abe1
a60ed6e
350ce25
03d3348
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,17 +127,25 @@ class HTMLParser(_markupbase.ParserBase): | |
| argument. | ||
| """ | ||
|
|
||
| CDATA_CONTENT_ELEMENTS = ("script", "style") | ||
| # See the HTML5 specs section "13.4 Parsing HTML fragments". | ||
| # https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments | ||
| # CDATA_CONTENT_ELEMENTS are parsed in RAWTEXT mode | ||
| CDATA_CONTENT_ELEMENTS = ("script", "style", "xmp", "iframe", "noembed", "noframes") | ||
| RCDATA_CONTENT_ELEMENTS = ("textarea", "title") | ||
|
|
||
| def __init__(self, *, convert_charrefs=True): | ||
| def __init__(self, *, convert_charrefs=True, scripting=False): | ||
| """Initialize and reset this instance. | ||
|
|
||
| If convert_charrefs is True (the default), all character references | ||
| If convert_charrefs is true (the default), all character references | ||
| are automatically converted to the corresponding Unicode characters. | ||
|
|
||
| If *scripting* is false (the default), the content of the | ||
| ``noscript`` element is parsed normally; if it's true, | ||
| it's parsed in RAWTEXT mode. | ||
serhiy-storchaka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| super().__init__() | ||
| self.convert_charrefs = convert_charrefs | ||
| self.scripting = scripting | ||
| self.reset() | ||
|
|
||
| def reset(self): | ||
|
|
@@ -172,7 +180,9 @@ def get_starttag_text(self): | |
| def set_cdata_mode(self, elem, *, escapable=False): | ||
| self.cdata_elem = elem.lower() | ||
| self._escapable = escapable | ||
| if escapable and not self.convert_charrefs: | ||
| if escapable is None: # PLAINTEXT mode | ||
| self.interesting = re.compile(r'\z') | ||
| elif escapable and not self.convert_charrefs: | ||
| self.interesting = re.compile(r'&|</%s(?=[\t\n\r\f />])' % self.cdata_elem, | ||
| re.IGNORECASE|re.ASCII) | ||
| else: | ||
|
|
@@ -448,6 +458,10 @@ def parse_starttag(self, i): | |
| self.set_cdata_mode(tag) | ||
| elif tag in self.RCDATA_CONTENT_ELEMENTS: | ||
| self.set_cdata_mode(tag, escapable=True) | ||
| elif self.scripting and tag == "noscript": | ||
| self.set_cdata_mode(tag) | ||
| elif tag == "plaintext": | ||
| self.set_cdata_mode(tag, escapable=None) | ||
|
||
| return endpos | ||
|
|
||
| # Internal -- check to see if we have a complete starttag; return end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.