Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html.py 9.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. """Compare two HTML documents."""
  2. import html
  3. from html.parser import HTMLParser
  4. from django.utils.regex_helper import _lazy_re_compile
  5. # ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020
  6. # SPACE.
  7. # https://infra.spec.whatwg.org/#ascii-whitespace
  8. ASCII_WHITESPACE = _lazy_re_compile(r"[\t\n\f\r ]+")
  9. # https://html.spec.whatwg.org/#attributes-3
  10. BOOLEAN_ATTRIBUTES = {
  11. "allowfullscreen",
  12. "async",
  13. "autofocus",
  14. "autoplay",
  15. "checked",
  16. "controls",
  17. "default",
  18. "defer ",
  19. "disabled",
  20. "formnovalidate",
  21. "hidden",
  22. "ismap",
  23. "itemscope",
  24. "loop",
  25. "multiple",
  26. "muted",
  27. "nomodule",
  28. "novalidate",
  29. "open",
  30. "playsinline",
  31. "readonly",
  32. "required",
  33. "reversed",
  34. "selected",
  35. # Attributes for deprecated tags.
  36. "truespeed",
  37. }
  38. def normalize_whitespace(string):
  39. return ASCII_WHITESPACE.sub(" ", string)
  40. def normalize_attributes(attributes):
  41. normalized = []
  42. for name, value in attributes:
  43. if name == "class" and value:
  44. # Special case handling of 'class' attribute, so that comparisons
  45. # of DOM instances are not sensitive to ordering of classes.
  46. value = " ".join(
  47. sorted(value for value in ASCII_WHITESPACE.split(value) if value)
  48. )
  49. # Boolean attributes without a value is same as attribute with value
  50. # that equals the attributes name. For example:
  51. # <input checked> == <input checked="checked">
  52. if name in BOOLEAN_ATTRIBUTES:
  53. if not value or value == name:
  54. value = None
  55. elif value is None:
  56. value = ""
  57. normalized.append((name, value))
  58. return normalized
  59. class Element:
  60. def __init__(self, name, attributes):
  61. self.name = name
  62. self.attributes = sorted(attributes)
  63. self.children = []
  64. def append(self, element):
  65. if isinstance(element, str):
  66. element = normalize_whitespace(element)
  67. if self.children and isinstance(self.children[-1], str):
  68. self.children[-1] += element
  69. self.children[-1] = normalize_whitespace(self.children[-1])
  70. return
  71. elif self.children:
  72. # removing last children if it is only whitespace
  73. # this can result in incorrect dom representations since
  74. # whitespace between inline tags like <span> is significant
  75. if isinstance(self.children[-1], str) and self.children[-1].isspace():
  76. self.children.pop()
  77. if element:
  78. self.children.append(element)
  79. def finalize(self):
  80. def rstrip_last_element(children):
  81. if children and isinstance(children[-1], str):
  82. children[-1] = children[-1].rstrip()
  83. if not children[-1]:
  84. children.pop()
  85. children = rstrip_last_element(children)
  86. return children
  87. rstrip_last_element(self.children)
  88. for i, child in enumerate(self.children):
  89. if isinstance(child, str):
  90. self.children[i] = child.strip()
  91. elif hasattr(child, "finalize"):
  92. child.finalize()
  93. def __eq__(self, element):
  94. if not hasattr(element, "name") or self.name != element.name:
  95. return False
  96. if self.attributes != element.attributes:
  97. return False
  98. return self.children == element.children
  99. def __hash__(self):
  100. return hash((self.name, *self.attributes))
  101. def _count(self, element, count=True):
  102. if not isinstance(element, str) and self == element:
  103. return 1
  104. if isinstance(element, RootElement) and self.children == element.children:
  105. return 1
  106. i = 0
  107. elem_child_idx = 0
  108. for child in self.children:
  109. # child is text content and element is also text content, then
  110. # make a simple "text" in "text"
  111. if isinstance(child, str):
  112. if isinstance(element, str):
  113. if count:
  114. i += child.count(element)
  115. elif element in child:
  116. return 1
  117. else:
  118. # Look for element wholly within this child.
  119. i += child._count(element, count=count)
  120. if not count and i:
  121. return i
  122. # Also look for a sequence of element's children among self's
  123. # children. self.children == element.children is tested above,
  124. # but will fail if self has additional children. Ex: '<a/><b/>'
  125. # is contained in '<a/><b/><c/>'.
  126. if isinstance(element, RootElement) and element.children:
  127. elem_child = element.children[elem_child_idx]
  128. # Start or continue match, advance index.
  129. if elem_child == child:
  130. elem_child_idx += 1
  131. # Match found, reset index.
  132. if elem_child_idx == len(element.children):
  133. i += 1
  134. elem_child_idx = 0
  135. # No match, reset index.
  136. else:
  137. elem_child_idx = 0
  138. return i
  139. def __contains__(self, element):
  140. return self._count(element, count=False) > 0
  141. def count(self, element):
  142. return self._count(element, count=True)
  143. def __getitem__(self, key):
  144. return self.children[key]
  145. def __str__(self):
  146. output = "<%s" % self.name
  147. for key, value in self.attributes:
  148. if value is not None:
  149. output += ' %s="%s"' % (key, value)
  150. else:
  151. output += " %s" % key
  152. if self.children:
  153. output += ">\n"
  154. output += "".join(
  155. [
  156. html.escape(c) if isinstance(c, str) else str(c)
  157. for c in self.children
  158. ]
  159. )
  160. output += "\n</%s>" % self.name
  161. else:
  162. output += ">"
  163. return output
  164. def __repr__(self):
  165. return str(self)
  166. class RootElement(Element):
  167. def __init__(self):
  168. super().__init__(None, ())
  169. def __str__(self):
  170. return "".join(
  171. [html.escape(c) if isinstance(c, str) else str(c) for c in self.children]
  172. )
  173. class HTMLParseError(Exception):
  174. pass
  175. class Parser(HTMLParser):
  176. # https://html.spec.whatwg.org/#void-elements
  177. SELF_CLOSING_TAGS = {
  178. "area",
  179. "base",
  180. "br",
  181. "col",
  182. "embed",
  183. "hr",
  184. "img",
  185. "input",
  186. "link",
  187. "meta",
  188. "param",
  189. "source",
  190. "track",
  191. "wbr",
  192. # Deprecated tags
  193. "frame",
  194. "spacer",
  195. }
  196. def __init__(self):
  197. super().__init__()
  198. self.root = RootElement()
  199. self.open_tags = []
  200. self.element_positions = {}
  201. def error(self, msg):
  202. raise HTMLParseError(msg, self.getpos())
  203. def format_position(self, position=None, element=None):
  204. if not position and element:
  205. position = self.element_positions[element]
  206. if position is None:
  207. position = self.getpos()
  208. if hasattr(position, "lineno"):
  209. position = position.lineno, position.offset
  210. return "Line %d, Column %d" % position
  211. @property
  212. def current(self):
  213. if self.open_tags:
  214. return self.open_tags[-1]
  215. else:
  216. return self.root
  217. def handle_startendtag(self, tag, attrs):
  218. self.handle_starttag(tag, attrs)
  219. if tag not in self.SELF_CLOSING_TAGS:
  220. self.handle_endtag(tag)
  221. def handle_starttag(self, tag, attrs):
  222. attrs = normalize_attributes(attrs)
  223. element = Element(tag, attrs)
  224. self.current.append(element)
  225. if tag not in self.SELF_CLOSING_TAGS:
  226. self.open_tags.append(element)
  227. self.element_positions[element] = self.getpos()
  228. def handle_endtag(self, tag):
  229. if not self.open_tags:
  230. self.error("Unexpected end tag `%s` (%s)" % (tag, self.format_position()))
  231. element = self.open_tags.pop()
  232. while element.name != tag:
  233. if not self.open_tags:
  234. self.error(
  235. "Unexpected end tag `%s` (%s)" % (tag, self.format_position())
  236. )
  237. element = self.open_tags.pop()
  238. def handle_data(self, data):
  239. self.current.append(data)
  240. def parse_html(html):
  241. """
  242. Take a string that contains HTML and turn it into a Python object structure
  243. that can be easily compared against other HTML on semantic equivalence.
  244. Syntactical differences like which quotation is used on arguments will be
  245. ignored.
  246. """
  247. parser = Parser()
  248. parser.feed(html)
  249. parser.close()
  250. document = parser.root
  251. document.finalize()
  252. # Removing ROOT element if it's not necessary
  253. if len(document.children) == 1 and not isinstance(document.children[0], str):
  254. document = document.children[0]
  255. return document