|
1 | 1 | from __future__ import absolute_import, division, unicode_literals
|
2 | 2 |
|
3 |
| -from six import PY2, text_type |
| 3 | +from six import PY2, text_type, unichr |
4 | 4 |
|
5 | 5 | import io
|
6 | 6 |
|
7 | 7 | from . import support # noqa
|
8 | 8 |
|
9 |
| -from html5lib.constants import namespaces |
| 9 | +from html5lib.constants import namespaces, tokenTypes |
10 | 10 | from html5lib import parse, parseFragment, HTMLParser
|
11 | 11 |
|
12 | 12 |
|
@@ -53,13 +53,42 @@ def test_unicode_file():
|
53 | 53 | assert parse(io.StringIO("a")) is not None
|
54 | 54 |
|
55 | 55 |
|
| 56 | +def test_maintain_attribute_order(): |
| 57 | + # This is here because we impl it in parser and not tokenizer |
| 58 | + p = HTMLParser() |
| 59 | + # generate loads to maximize the chance a hash-based mutation will occur |
| 60 | + attrs = [(unichr(x), i) for i, x in enumerate(range(ord('a'), ord('z')))] |
| 61 | + token = {'name': 'html', |
| 62 | + 'selfClosing': False, |
| 63 | + 'selfClosingAcknowledged': False, |
| 64 | + 'type': tokenTypes["StartTag"], |
| 65 | + 'data': attrs} |
| 66 | + out = p.normalizeToken(token) |
| 67 | + attr_order = list(out["data"].keys()) |
| 68 | + assert attr_order == [x for x, i in attrs] |
| 69 | + |
| 70 | + |
56 | 71 | def test_duplicate_attribute():
|
57 | 72 | # This is here because we impl it in parser and not tokenizer
|
58 | 73 | doc = parse('<p class=a class=b>')
|
59 | 74 | el = doc[1][0]
|
60 | 75 | assert el.get("class") == "a"
|
61 | 76 |
|
62 | 77 |
|
| 78 | +def test_maintain_duplicate_attribute_order(): |
| 79 | + # This is here because we impl it in parser and not tokenizer |
| 80 | + p = HTMLParser() |
| 81 | + attrs = [(unichr(x), i) for i, x in enumerate(range(ord('a'), ord('z')))] |
| 82 | + token = {'name': 'html', |
| 83 | + 'selfClosing': False, |
| 84 | + 'selfClosingAcknowledged': False, |
| 85 | + 'type': tokenTypes["StartTag"], |
| 86 | + 'data': attrs + [('a', len(attrs))]} |
| 87 | + out = p.normalizeToken(token) |
| 88 | + attr_order = list(out["data"].keys()) |
| 89 | + assert attr_order == [x for x, i in attrs] |
| 90 | + |
| 91 | + |
63 | 92 | def test_debug_log():
|
64 | 93 | parser = HTMLParser(debug=True)
|
65 | 94 | parser.parse("<!doctype html><title>a</title><p>b<script>c</script>d</p>e")
|
|
0 commit comments