From 7600cd859014bac31200d52b1c4f6e88136b3c97 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 20 Nov 2014 08:57:20 -0800 Subject: runtests.py: Fixed normalization of declarations and CDATA. If the input contains CDATA, we break it out and pass it through verbatim, without sending it through HTMLParser, which breaks on CDATA. Improves on #161. --- runtests.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'runtests.py') diff --git a/runtests.py b/runtests.py index 83c331d..8a37f6d 100755 --- a/runtests.py +++ b/runtests.py @@ -115,7 +115,7 @@ class MyHTMLParser(HTMLParser): def handle_decl(self, data): self.output += '' self.last = "decl" - def handle_unknown_decl(self, data): + def unknown_decl(self, data): self.output += '' self.last = "decl" def handle_pi(self,data): @@ -174,15 +174,18 @@ def normalize_html(html): * Attributes are sorted and lowercased. * References are converted to unicode, except that '<', '>', '&', and '&' are rendered using entities. - - Known limitations: - - * HTMLParser just swallows CDATA. - * HTMLParser seems to treat unknown declarations as comments. """ + html_chunk_re = re.compile("(\|\<[^>]*\>|[^<]+)") try: parser = MyHTMLParser() - parser.feed(html.decode(encoding='UTF-8')) + # We work around HTMLParser's limitations parsing CDATA + # by breaking the input into chunks and passing CDATA chunks + # through verbatim. + for chunk in re.finditer(html_chunk_re, html): + if chunk.group(0)[:8] == "