fortune_html_parser.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8
  2. from HTMLParser import HTMLParser
  3. class FortuneHTMLParser(HTMLParser):
  4. body = []
  5. 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>'
  6. # Is called when a doctype or other such tag is read in.
  7. # For our purposes, we assume this is only going to be
  8. # "DOCTYPE html", so we will surround it with "<!" and ">".
  9. def handle_decl(self, decl):
  10. # The spec says that for HTML this is case insensitive,
  11. # and since we did not specify xml compliance (where
  12. # incorrect casing would throw a syntax error), we must
  13. # allow all casings. We will lower for our normalization.
  14. self.body.append("<!{d}>".format(d=decl.lower()))
  15. # This is called when an HTML character is parsed (i.e.
  16. # &quot;). There are a number of issues to be resolved
  17. # here. For instance, some tests choose to leave the
  18. # "+" character as-is, which should be fine as far as
  19. # character escaping goes, but others choose to use the
  20. # character reference of "&#43;", which is also fine.
  21. # Therefore, this method looks for all possible character
  22. # references and normalizes them so that we can
  23. # validate the input against a single valid spec string.
  24. # Another example problem: "&quot;" is valid, but so is
  25. # "&#34;"
  26. def handle_charref(self, name):
  27. # "&#34;" is a valid escaping, but we are normalizing
  28. # it so that our final parse can just be checked for
  29. # equality.
  30. if name == "34" or name == "034" or name == "x22":
  31. # Append our normalized entity reference to our body.
  32. self.body.append("&quot;")
  33. # "&#39;" is a valid escaping of "-", but it is not
  34. # required, so we normalize for equality checking.
  35. if name == "39" or name == "039" or name == "x27":
  36. self.body.append("&apos;")
  37. # Again, "&#43;" is a valid escaping of the "+", but
  38. # it is not required, so we need to normalize for out
  39. # final parse and equality check.
  40. if name == "43" or name == "043" or name == "x2B":
  41. self.body.append("+")
  42. # Again, "&#62;" is a valid escaping of ">", but we
  43. # need to normalize to "&gt;" for equality checking.
  44. if name == "62" or name == "062" or name == "x3E":
  45. self.body.append("&gt;")
  46. # Again, "&#60;" is a valid escaping of "<", but we
  47. # need to normalize to "&lt;" for equality checking.
  48. if name == "60" or name == "060" or name == "x3C":
  49. self.body.append("&lt;")
  50. # Not sure why some are escaping '/'
  51. if name == "47" or name == "047" or name == "x2F":
  52. self.body.append("/")
  53. def handle_entityref(self, name):
  54. # Again, "&mdash;" is a valid escaping of "—", but we
  55. # need to normalize to "—" for equality checking.
  56. if name == "mdash":
  57. self.body.append("—")
  58. else:
  59. self.body.append("&{n};".format(n=name))
  60. # This is called every time a tag is opened. We append
  61. # each one wrapped in "<" and ">".
  62. def handle_starttag(self, tag, attrs):
  63. self.body.append("<{t}>".format(t=tag))
  64. # This is called whenever data is presented inside of a
  65. # start and end tag. Generally, this will only ever be
  66. # the contents inside of "<td>" and "</td>", but there
  67. # are also the "<title>" and "</title>" tags.
  68. def handle_data (self, data):
  69. if data.strip() != '':
  70. # After a LOT of debate, these are now considered
  71. # valid in data. The reason for this approach is
  72. # because a few tests use tools which determine
  73. # at compile time whether or not a string needs
  74. # a given type of html escaping, and our fortune
  75. # test has apostrophes and quotes in html data
  76. # rather than as an html attribute etc.
  77. # example:
  78. # <td>A computer scientist is someone who fixes things that aren't broken.</td>
  79. # Semanticly, that apostrophe does not NEED to
  80. # be escaped. The same is currently true for our
  81. # quotes.
  82. # In fact, in data (read: between two html tags)
  83. # even the '>' need not be replaced as long as
  84. # the '<' are all escaped.
  85. # We replace them with their escapings here in
  86. # order to have a noramlized string for equality
  87. # comparison at the end.
  88. data = data.replace('\'', '&apos;')
  89. data = data.replace('"', '&quot;')
  90. data = data.replace('>', '&gt;')
  91. self.body.append("{d}".format(d=data))
  92. # This is called every time a tag is closed. We append
  93. # each one wrapped in "</" and ">".
  94. def handle_endtag(self, tag):
  95. self.body.append("</{t}>".format(t=tag))
  96. # Returns whether the HTML input parsed by this parser
  97. # is valid against our known "fortune" spec.
  98. # The parsed data in 'body' is joined on empty strings
  99. # and checked for equality against our spec.
  100. def isValidFortune(self):
  101. return self.valid == ''.join(self.body)