Debian Codegraph @

tests/support/structural_diff.py

raw
1 import html.parser
2
3
4 class Parser(html.parser.HTMLParser):
5 def __init__(self):
6 super().__init__()
7 self._stream = []
8
9 def handle_starttag(self, tag, attrs):
10 attrs = sorted(attrs, key=lambda x: x[0])
11 attrs = '|'.join([k[0] + ':' + k[1] for k in attrs])
12 self._stream.append(('<', tag, attrs))
13
14 def handle_endtag(self, tag):
15 self._stream.append(('>', tag, ''))
16
17 def handle_data(self, data):
18 self._stream.append(('_', data, ''))
19
20 @property
21 def stream(self):
22 return self._stream
23
24
25 def _serialize(t):
26 parser = Parser()
27 parser.feed(t)
28 return parser.stream
29
30
31 def structural_diff(a, b):
32 """Check if there is a structural difference between two HTML files."""
33 a_s = _serialize(a)
34 b_s = _serialize(b)
35
36 for e, f in zip(a_s, b_s):
37 assert e == f, f'Expected: {e}, found: {f}'