| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1f3b84f commit 7e82b27
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,7 +123,8 @@ An exception is defined as well: | |||
| 123 | 123 | ||
| 124 | 124 | .. method:: HTMLParser.handle_data(data) | |
| 125 | 125 | ||
| 126 | - This method is called to process arbitrary data. It is intended to be | ||
| 126 | + This method is called to process arbitrary data (e.g. the content of | ||
| 127 | + ``<script>...</script>`` and ``<style>...</style>``). It is intended to be | ||
| 127 | 128 | overridden by a derived class; the base class implementation does nothing. | |
| 128 | 129 | ||
| 129 | 130 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,6 +43,8 @@ | |||
| 43 | 43 | \s* # trailing whitespace | |
| 44 | 44 | """, re.VERBOSE) | |
| 45 | 45 | endendtag = re.compile('>') | |
| 46 | + # the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between | ||
| 47 | + # </ and the tag name, so maybe this should be fixed | ||
| 46 | 48 | endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>') | |
| 47 | 49 | ||
| 48 | 50 | ||
@@ -96,6 +98,7 @@ def reset(self): | |||
| 96 | 98 | self.rawdata = '' | |
| 97 | 99 | self.lasttag = '???' | |
| 98 | 100 | self.interesting = interesting_normal | |
| 101 | + self.cdata_elem = None | ||
| 99 | 102 | markupbase.ParserBase.reset(self) | |
| 100 | 103 | ||
| 101 | 104 | def feed(self, data): | |
@@ -120,11 +123,13 @@ def get_starttag_text(self): | |||
| 120 | 123 | """Return full source of start tag: '<...>'.""" | |
| 121 | 124 | return self.__starttag_text | |
| 122 | 125 | ||
| 123 | - def set_cdata_mode(self): | ||
| 126 | + def set_cdata_mode(self, elem): | ||
| 124 | 127 | self.interesting = interesting_cdata | |
| 128 | + self.cdata_elem = elem.lower() | ||
| 125 | 129 | ||
| 126 | 130 | def clear_cdata_mode(self): | |
| 127 | 131 | self.interesting = interesting_normal | |
| 132 | + self.cdata_elem = None | ||
| 128 | 133 | ||
| 129 | 134 | # Internal -- handle data as far as reasonable. May leave state | |
| 130 | 135 | # and data to be processed by a subsequent call. If 'end' is | |
@@ -270,7 +275,7 @@ def parse_starttag(self, i): | |||
| 270 | 275 | else: | |
| 271 | 276 | self.handle_starttag(tag, attrs) | |
| 272 | 277 | if tag in self.CDATA_CONTENT_ELEMENTS: | |
| 273 | - self.set_cdata_mode() | ||
| 278 | + self.set_cdata_mode(tag) | ||
| 274 | 279 | return endpos | |
| 275 | 280 | ||
| 276 | 281 | # Internal -- check to see if we have a complete starttag; return end | |
@@ -314,9 +319,18 @@ def parse_endtag(self, i): | |||
| 314 | 319 | j = match.end() | |
| 315 | 320 | match = endtagfind.match(rawdata, i) # </ + tag + > | |
| 316 | 321 | if not match: | |
| 322 | + if self.cdata_elem is not None: | ||
| 323 | + self.handle_data(rawdata[i:j]) | ||
| 324 | + return j | ||
| 317 | 325 | self.error("bad end tag: %r" % (rawdata[i:j],)) | |
| 318 | - tag = match.group(1) | ||
| 319 | - self.handle_endtag(tag.lower()) | ||
| 326 | + | ||
| 327 | + elem = match.group(1).lower() # script or style | ||
| 328 | + if self.cdata_elem is not None: | ||
| 329 | + if elem != self.cdata_elem: | ||
| 330 | + self.handle_data(rawdata[i:j]) | ||
| 331 | + return j | ||
| 332 | + | ||
| 333 | + self.handle_endtag(elem) | ||
| 320 | 334 | self.clear_cdata_mode() | |
| 321 | 335 | return j | |
| 322 | 336 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -312,18 +312,36 @@ def test_get_starttag_text(self): | |||
| 312 | 312 | ("starttag_text", s)]) | |
| 313 | 313 | ||
| 314 | 314 | def test_cdata_content(self): | |
| 315 | - s = """<script> <!-- not a comment --> ¬-an-entity-ref; </script>""" | ||
| 316 | - self._run_check(s, [ | ||
| 317 | - ("starttag", "script", []), | ||
| 318 | - ("data", " <!-- not a comment --> ¬-an-entity-ref; "), | ||
| 319 | - ("endtag", "script"), | ||
| 320 | - ]) | ||
| 321 | - s = """<script> <not a='start tag'> </script>""" | ||
| 322 | - self._run_check(s, [ | ||
| 323 | - ("starttag", "script", []), | ||
| 324 | - ("data", " <not a='start tag'> "), | ||
| 325 | - ("endtag", "script"), | ||
| 326 | - ]) | ||
| 315 | + contents = [ | ||
| 316 | + '<!-- not a comment --> ¬-an-entity-ref;', | ||
| 317 | + "<not a='start tag'>", | ||
| 318 | + '<a href="" /> <p> <span></span>', | ||
| 319 | + 'foo = "</scr" + "ipt>";', | ||
| 320 | + 'foo = "</SCRIPT" + ">";', | ||
| 321 | + 'foo = <\n/script> ', | ||
| 322 | + '<!-- document.write("</scr" + "ipt>"); -->', | ||
| 323 | + ('\n//<![CDATA[\n' | ||
| 324 | + 'document.write(\'<s\'+\'cript type="text/javascript" ' | ||
| 325 | + 'src="http://www.example.org/r=\'+new ' | ||
| 326 | + 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'), | ||
| 327 | + '\n<!-- //\nvar foo = 3.14;\n// -->\n', | ||
| 328 | + 'foo = "</sty" + "le>";', | ||
| 329 | + u'<!-- \u2603 -->', | ||
| 330 | + # these two should be invalid according to the HTML 5 spec, | ||
| 331 | + # section 8.1.2.2 | ||
| 332 | + #'foo = </\nscript>', | ||
| 333 | + #'foo = </ script>', | ||
| 334 | + ] | ||
| 335 | + elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style'] | ||
| 336 | + for content in contents: | ||
| 337 | + for element in elements: | ||
| 338 | + element_lower = element.lower() | ||
| 339 | + s = u'<{element}>{content}</{element}>'.format(element=element, | ||
| 340 | + content=content) | ||
| 341 | + self._run_check(s, [("starttag", element_lower, []), | ||
| 342 | + ("data", content), | ||
| 343 | + ("endtag", element_lower)]) | ||
| 344 | + | ||
| 327 | 345 | ||
| 328 | 346 | def test_entityrefs_in_attributes(self): | |
| 329 | 347 | self._run_check("<html foo='€&aa&unsupported;'>", [ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,10 +74,13 @@ Core and Builtins | |||
| 74 | 74 | Library | |
| 75 | 75 | ------- | |
| 76 | 76 | ||
| 77 | - - Issue 10817: Fix urlretrieve function to raise ContentTooShortError even | ||
| 77 | + - Issue #670664: Fix HTMLParser to correctly handle the content of | ||
| 78 | + ``<script>...</script>`` and ``<style>...</style>``. | ||
| 79 | + | ||
| 80 | + - Issue #10817: Fix urlretrieve function to raise ContentTooShortError even | ||
| 78 | 81 | when reporthook is None. Patch by Jyrki Pulliainen. | |
| 79 | 82 | ||
| 80 | - - Issue 13296: Fix IDLE to clear compile __future__ flags on shell restart. | ||
| 83 | + - Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart. | ||
| 81 | 84 | (Patch by Roger Serwy) | |
| 82 | 85 | ||
| 83 | 86 | - Issue #7334: close source files on ElementTree.parse and iterparse. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments