fortune_html_parser.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8
  2. import re
  3. from HTMLParser import HTMLParser
  4. from difflib import unified_diff
  5. class FortuneHTMLParser(HTMLParser):
  6. body = []
  7. valid = '<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr><tr><td>11</td><td>&lt;script&gt;alert(&quot;This should not be displayed in a browser alert box.&quot;);&lt;/script&gt;</td></tr><tr><td>4</td><td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td></tr><tr><td>5</td><td>A computer program does what you tell it to do, not what you want it to do.</td></tr><tr><td>2</td><td>A computer scientist is someone who fixes things that aren&apos;t broken.</td></tr><tr><td>8</td><td>A list is only as strong as its weakest link. — Donald Knuth</td></tr><tr><td>0</td><td>Additional fortune added at request time.</td></tr><tr><td>3</td><td>After enough decimal places, nobody gives a damn.</td></tr><tr><td>7</td><td>Any program that runs right is obsolete.</td></tr><tr><td>10</td><td>Computers make very fast, very accurate mistakes.</td></tr><tr><td>6</td><td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td></tr><tr><td>9</td><td>Feature: A bug with seniority.</td></tr><tr><td>1</td><td>fortune: No such file or directory</td></tr><tr><td>12</td><td>フレームワークのベンチマーク</td></tr></table></body></html>'
  8. # Is called when a doctype or other such tag is read in.
  9. # For our purposes, we assume this is only going to be
  10. # "DOCTYPE html", so we will surround it with "<!" and ">".
  11. def handle_decl(self, decl):
  12. # The spec says that for HTML this is case insensitive,
  13. # and since we did not specify xml compliance (where
  14. # incorrect casing would throw a syntax error), we must
  15. # allow all casings. We will lower for our normalization.
  16. self.body.append("<!{d}>".format(d=decl.lower()))
  17. # This is called when an HTML character is parsed (i.e.
  18. # &quot;). There are a number of issues to be resolved
  19. # here. For instance, some tests choose to leave the
  20. # "+" character as-is, which should be fine as far as
  21. # character escaping goes, but others choose to use the
  22. # character reference of "&#43;", which is also fine.
  23. # Therefore, this method looks for all possible character
  24. # references and normalizes them so that we can
  25. # validate the input against a single valid spec string.
  26. # Another example problem: "&quot;" is valid, but so is
  27. # "&#34;"
  28. def handle_charref(self, name):
  29. val = name.lower()
  30. # "&#34;" is a valid escaping, but we are normalizing
  31. # it so that our final parse can just be checked for
  32. # equality.
  33. if val == "34" or val == "034" or val == "x22":
  34. # Append our normalized entity reference to our body.
  35. self.body.append("&quot;")
  36. # "&#39;" is a valid escaping of "-", but it is not
  37. # required, so we normalize for equality checking.
  38. if val == "39" or val == "039" or val == "x27":
  39. self.body.append("&apos;")
  40. # Again, "&#43;" is a valid escaping of the "+", but
  41. # it is not required, so we need to normalize for out
  42. # final parse and equality check.
  43. if val == "43" or val == "043" or val == "x2b":
  44. self.body.append("+")
  45. # Again, "&#62;" is a valid escaping of ">", but we
  46. # need to normalize to "&gt;" for equality checking.
  47. if val == "62" or val == "062" or val == "x3e":
  48. self.body.append("&gt;")
  49. # Again, "&#60;" is a valid escaping of "<", but we
  50. # need to normalize to "&lt;" for equality checking.
  51. if val == "60" or val == "060" or val == "x3c":
  52. self.body.append("&lt;")
  53. # Not sure why some are escaping '/'
  54. if val == "47" or val == "047" or val == "x2f":
  55. self.body.append("/")
  56. def handle_entityref(self, name):
  57. # Again, "&mdash;" is a valid escaping of "—", but we
  58. # need to normalize to "—" for equality checking.
  59. if name == "mdash":
  60. self.body.append("—")
  61. else:
  62. self.body.append("&{n};".format(n=name))
  63. # This is called every time a tag is opened. We append
  64. # each one wrapped in "<" and ">".
  65. def handle_starttag(self, tag, attrs):
  66. self.body.append("<{t}>".format(t=tag))
  67. # This is called whenever data is presented inside of a
  68. # start and end tag. Generally, this will only ever be
  69. # the contents inside of "<td>" and "</td>", but there
  70. # are also the "<title>" and "</title>" tags.
  71. def handle_data (self, data):
  72. if data.strip() != '':
  73. # After a LOT of debate, these are now considered
  74. # valid in data. The reason for this approach is
  75. # because a few tests use tools which determine
  76. # at compile time whether or not a string needs
  77. # a given type of html escaping, and our fortune
  78. # test has apostrophes and quotes in html data
  79. # rather than as an html attribute etc.
  80. # example:
  81. # <td>A computer scientist is someone who fixes things that aren't broken.</td>
  82. # Semanticly, that apostrophe does not NEED to
  83. # be escaped. The same is currently true for our
  84. # quotes.
  85. # In fact, in data (read: between two html tags)
  86. # even the '>' need not be replaced as long as
  87. # the '<' are all escaped.
  88. # We replace them with their escapings here in
  89. # order to have a noramlized string for equality
  90. # comparison at the end.
  91. data = data.replace('\'', '&apos;')
  92. data = data.replace('"', '&quot;')
  93. data = data.replace('>', '&gt;')
  94. self.body.append("{d}".format(d=data))
  95. # This is called every time a tag is closed. We append
  96. # each one wrapped in "</" and ">".
  97. def handle_endtag(self, tag):
  98. self.body.append("</{t}>".format(t=tag))
  99. # Returns whether the HTML input parsed by this parser
  100. # is valid against our known "fortune" spec.
  101. # The parsed data in 'body' is joined on empty strings
  102. # and checked for equality against our spec.
  103. def isValidFortune(self, out):
  104. body = ''.join(self.body)
  105. diff = self.valid == body
  106. if not diff:
  107. out.write("Fortune invalid. Diff following:\n")
  108. diff_str = ''.join(unified_diff(self.valid.split(' '), body.split(' '), fromfile='Valid', tofile='Response', n=5))
  109. out.write(diff_str)
  110. return diff