| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,7 +184,17 @@ def goahead(self, end): | |||
| 184 | 184 | elif startswith("<?", i): | |
| 185 | 185 | k = self.parse_pi(i) | |
| 186 | 186 | elif startswith("<!", i): | |
| 187 | - k = self.parse_declaration(i) | ||
| 187 | + # this might fail with things like <! not a comment > or | ||
| 188 | + # <! -- space before '--' -->. When strict is True an | ||
| 189 | + # error is raised, when it's False they will be considered | ||
| 190 | + # as bogus comments and parsed (see parse_bogus_comment). | ||
| 191 | + if self.strict: | ||
| 192 | + k = self.parse_declaration(i) | ||
| 193 | + else: | ||
| 194 | + try: | ||
| 195 | + k = self.parse_declaration(i) | ||
| 196 | + except HTMLParseError: | ||
| 197 | + k = self.parse_bogus_comment(i) | ||
| 188 | 198 | elif (i + 1) < n: | |
| 189 | 199 | self.handle_data("<") | |
| 190 | 200 | k = i + 1 | |
@@ -256,6 +266,19 @@ def goahead(self, end): | |||
| 256 | 266 | i = self.updatepos(i, n) | |
| 257 | 267 | self.rawdata = rawdata[i:] | |
| 258 | 268 | ||
| 269 | + # Internal -- parse bogus comment, return length or -1 if not terminated | ||
| 270 | + # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state | ||
| 271 | + def parse_bogus_comment(self, i, report=1): | ||
| 272 | + rawdata = self.rawdata | ||
| 273 | + if rawdata[i:i+2] != '<!': | ||
| 274 | + self.error('unexpected call to parse_comment()') | ||
| 275 | + pos = rawdata.find('>', i+2) | ||
| 276 | + if pos == -1: | ||
| 277 | + return -1 | ||
| 278 | + if report: | ||
| 279 | + self.handle_comment(rawdata[i+2:pos]) | ||
| 280 | + return pos + 1 | ||
| 281 | + | ||
| 259 | 282 | # Internal -- parse processing instr, return end or -1 if not terminated | |
| 260 | 283 | def parse_pi(self, i): | |
| 261 | 284 | rawdata = self.rawdata | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -323,6 +323,23 @@ def get_events(self): | |||
| 323 | 323 | ("endtag", element_lower)], | |
| 324 | 324 | collector=Collector()) | |
| 325 | 325 | ||
| 326 | + def test_comments(self): | ||
| 327 | + html = ("<!-- I'm a valid comment -->" | ||
| 328 | + '<!--me too!-->' | ||
| 329 | + '<!------>' | ||
| 330 | + '<!---->' | ||
| 331 | + '<!----I have many hyphens---->' | ||
| 332 | + '<!-- I have a > in the middle -->' | ||
| 333 | + '<!-- and I have -- in the middle! -->') | ||
| 334 | + expected = [('comment', " I'm a valid comment "), | ||
| 335 | + ('comment', 'me too!'), | ||
| 336 | + ('comment', '--'), | ||
| 337 | + ('comment', ''), | ||
| 338 | + ('comment', '--I have many hyphens--'), | ||
| 339 | + ('comment', ' I have a > in the middle '), | ||
| 340 | + ('comment', ' and I have -- in the middle! ')] | ||
| 341 | + self._run_check(html, expected) | ||
| 342 | + | ||
| 326 | 343 | def test_condcoms(self): | |
| 327 | 344 | html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->' | |
| 328 | 345 | '<!--[if IE 8]>condcoms<![endif]-->' | |
@@ -426,6 +443,19 @@ def test_unescape_function(self): | |||
| 426 | 443 | # see #12888 | |
| 427 | 444 | self.assertEqual(p.unescape('{ ' * 1050), '{ ' * 1050) | |
| 428 | 445 | ||
| 446 | + def test_broken_comments(self): | ||
| 447 | + html = ('<! not really a comment >' | ||
| 448 | + '<! not a comment either -->' | ||
| 449 | + '<! -- close enough -->' | ||
| 450 | + '<!!! another bogus comment !!!>') | ||
| 451 | + expected = [ | ||
| 452 | + ('comment', ' not really a comment '), | ||
| 453 | + ('comment', ' not a comment either --'), | ||
| 454 | + ('comment', ' -- close enough --'), | ||
| 455 | + ('comment', '!! another bogus comment !!!'), | ||
| 456 | + ] | ||
| 457 | + self._run_check(html, expected) | ||
| 458 | + | ||
| 429 | 459 | def test_broken_condcoms(self): | |
| 430 | 460 | # these condcoms are missing the '--' after '<!' and before the '>' | |
| 431 | 461 | html = ('<![if !(IE)]>broken condcom<![endif]>' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -113,6 +113,9 @@ Core and Builtins | |||
| 113 | 113 | Library | |
| 114 | 114 | ------- | |
| 115 | 115 | ||
| 116 | + - Issue #13960: HTMLParser is now able to handle broken comments when | ||
| 117 | + strict=False. | ||
| 118 | + | ||
| 116 | 119 | - Issue #9021: Add an introduction to the copy module documentation. | |
| 117 | 120 | ||
| 118 | 121 | - Issue #6005: Examples in the socket library documentation use sendall, where | |
@@ -123,7 +126,7 @@ Library | |||
| 123 | 126 | ||
| 124 | 127 | - Issue #10881: Fix test_site failure with OS X framework builds. | |
| 125 | 128 | ||
| 126 | - - Issue #964437 Make IDLE help window non-modal. | ||
| 129 | + - Issue #964437: Make IDLE help window non-modal. | ||
| 127 | 130 | Patch by Guilherme Polo and Roger Serwy. | |
| 128 | 131 | ||
| 129 | 132 | - Issue #2945: Make the distutils upload command aware of bdist_rpm products. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments