1from __future__ import absolute_import, division, unicode_literals
2
3from six import PY2, text_type, unichr
4
5import io
6
7from . import support  # noqa
8
9from html5lib.constants import namespaces, tokenTypes
10from html5lib import parse, parseFragment, HTMLParser
11
12
13# tests that aren't autogenerated from text files
14def test_assertDoctypeCloneable():
15    doc = parse('<!DOCTYPE HTML>', treebuilder="dom")
16    assert doc.cloneNode(True) is not None
17
18
19def test_line_counter():
20    # http://groups.google.com/group/html5lib-discuss/browse_frm/thread/f4f00e4a2f26d5c0
21    assert parse("<pre>\nx\n&gt;\n</pre>") is not None
22
23
24def test_namespace_html_elements_0_dom():
25    doc = parse("<html></html>",
26                treebuilder="dom",
27                namespaceHTMLElements=True)
28    assert doc.childNodes[0].namespaceURI == namespaces["html"]
29
30
31def test_namespace_html_elements_1_dom():
32    doc = parse("<html></html>",
33                treebuilder="dom",
34                namespaceHTMLElements=False)
35    assert doc.childNodes[0].namespaceURI is None
36
37
38def test_namespace_html_elements_0_etree():
39    doc = parse("<html></html>",
40                treebuilder="etree",
41                namespaceHTMLElements=True)
42    assert doc.tag == "{%s}html" % (namespaces["html"],)
43
44
45def test_namespace_html_elements_1_etree():
46    doc = parse("<html></html>",
47                treebuilder="etree",
48                namespaceHTMLElements=False)
49    assert doc.tag == "html"
50
51
52def test_unicode_file():
53    assert parse(io.StringIO("a")) is not None
54
55
56def 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
71def test_duplicate_attribute():
72    # This is here because we impl it in parser and not tokenizer
73    doc = parse('<p class=a class=b>')
74    el = doc[1][0]
75    assert el.get("class") == "a"
76
77
78def 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
92def test_debug_log():
93    parser = HTMLParser(debug=True)
94    parser.parse("<!doctype html><title>a</title><p>b<script>c</script>d</p>e")
95
96    expected = [('dataState', 'InitialPhase', 'InitialPhase', 'processDoctype', {'type': 'Doctype'}),
97                ('dataState', 'BeforeHtmlPhase', 'BeforeHtmlPhase', 'processStartTag', {'name': 'title', 'type': 'StartTag'}),
98                ('dataState', 'BeforeHeadPhase', 'BeforeHeadPhase', 'processStartTag', {'name': 'title', 'type': 'StartTag'}),
99                ('dataState', 'InHeadPhase', 'InHeadPhase', 'processStartTag', {'name': 'title', 'type': 'StartTag'}),
100                ('rcdataState', 'TextPhase', 'TextPhase', 'processCharacters', {'type': 'Characters'}),
101                ('dataState', 'TextPhase', 'TextPhase', 'processEndTag', {'name': 'title', 'type': 'EndTag'}),
102                ('dataState', 'InHeadPhase', 'InHeadPhase', 'processStartTag', {'name': 'p', 'type': 'StartTag'}),
103                ('dataState', 'AfterHeadPhase', 'AfterHeadPhase', 'processStartTag', {'name': 'p', 'type': 'StartTag'}),
104                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processStartTag', {'name': 'p', 'type': 'StartTag'}),
105                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processCharacters', {'type': 'Characters'}),
106                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processStartTag', {'name': 'script', 'type': 'StartTag'}),
107                ('dataState', 'InBodyPhase', 'InHeadPhase', 'processStartTag', {'name': 'script', 'type': 'StartTag'}),
108                ('scriptDataState', 'TextPhase', 'TextPhase', 'processCharacters', {'type': 'Characters'}),
109                ('dataState', 'TextPhase', 'TextPhase', 'processEndTag', {'name': 'script', 'type': 'EndTag'}),
110                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processCharacters', {'type': 'Characters'}),
111                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processEndTag', {'name': 'p', 'type': 'EndTag'}),
112                ('dataState', 'InBodyPhase', 'InBodyPhase', 'processCharacters', {'type': 'Characters'})]
113
114    if PY2:
115        for i, log in enumerate(expected):
116            log = [x.encode("ascii") if isinstance(x, text_type) else x for x in log]
117            expected[i] = tuple(log)
118
119    assert parser.log == expected
120
121
122def test_no_duplicate_clone():
123    frag = parseFragment("<b><em><foo><foob><fooc><aside></b></em>")
124    assert len(frag) == 2
125
126
127def test_self_closing_col():
128    parser = HTMLParser()
129    parser.parseFragment('<table><colgroup><col /></colgroup></table>')
130    assert not parser.errors
131