def test_with(): with xsc.build(): with html.ul() as e: +html.li(1) +html.li(2) assert e == html.ul(html.li(1), html.li(2)) with xsc.build(): with html.p() as e: +html.span(1) with html.b(): +html.span(2) +html.span(3) assert e == html.p(html.span(1), html.b(html.span(2)), html.span(3)) with xsc.build(): with html.p() as e: +xsc.Text(1) assert e == html.p(1) with xsc.build(): with html.p() as e: +xml.Attrs(lang="de") assert e == html.p(xml.Attrs(lang="de")) assert e.bytes() == b'<p xml:lang="de"></p>' with xsc.build(): with xsc.Frag() as e: +xsc.Text(1) assert e == xsc.Frag(1) # Test add() with xsc.build(): with html.p() as e: xsc.add(1) assert e == html.p(1) with xsc.build(): with html.p() as e: xsc.add(class_="foo") assert e == html.p(class_="foo") with xsc.build(): with html.p() as e: xsc.add({"class": "foo"}) assert e == html.p(class_="foo") with xsc.build(): with html.p() as e: xsc.add(xml.Attrs(lang="en")) assert e == html.p(xml.Attrs(lang="en"))
def test_attribute_order(): node = html.div(xml.Attrs(lang="de"), id="id42", align="right", class_="foo") assert node.bytes( ) == b"""<div xml:lang="de" align="right" class="foo" id="id42"></div>"""
def test_publishxmlattr(): node = html.html(xml.Attrs(space="preserve")) assert node.bytes(prefixdefault=False) == b"""<html xml:space="preserve"></html>""" assert node.bytes(prefixdefault=True) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml" xml:space="preserve"></ns:html>""" assert node.bytes(prefixdefault=None) == b"""<html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve"></html>""" assert node.bytes(prefixes={html: "h"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xml:space="preserve"></h:html>""" # Prefix for XML namespace can't be overwritten assert node.bytes(prefixes={html: "h", xml: "spam"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xml:space="preserve"></h:html>"""
def test_attrupdate(): node = html.a(href="gurk", class_="hurz") node.attrs.update(xml.Attrs(lang="de"), { "href": "gurk2", html.a.Attrs.id: 42 }) assert str(node["href"]) == "gurk2" assert str(node["id"]) == "42" assert str(node[xml.Attrs.lang]) == "de" node = html.a({xml.Attrs.lang: "de"}, href="gurk", class_="hurz") assert str(node[xml.Attrs.lang]) == "de" node = html.a(xml.Attrs(lang="de"), href="gurk", class_="hurz") assert str(node[xml.Attrs.lang]) == "de" class Gurk(xsc.Element): model = False class Attrs(xsc.Element.Attrs): class gurk(xsc.TextAttr): pass class hurz(xsc.TextAttr): default = "hinz+kunz" node1 = Gurk() node2 = Gurk(hurz=None) node1.attrs.update(node2.attrs) assert "hurz" not in node1.attrs node1 = Gurk(hurz=None) node2 = Gurk() node1.attrs.update(node2.attrs) assert "hurz" in node1.attrs node = Gurk(Gurk(hurz=None).attrs) assert "hurz" not in node.attrs attrs = Gurk.Attrs(Gurk.Attrs(hurz=None)) assert "hurz" not in attrs
def test_pickle(): e = xsc.Frag( xml.XML(), html.DocTypeXHTML10transitional(), xsc.Comment("foo"), html.html(xml.Attrs(lang="de"), lang="de"), php.expression("$foo"), chars.nbsp(), abbr.xml(), ) e.append(e[3]) e2 = pickle.loads(pickle.dumps(e, 2)) assert e == e2 assert e2[3] is e2[-1]
def test_html(): # Without a conversion language ``htmlspecials.html`` will not touch the language attributes e = htmlspecials.html().conv() assert "lang" not in e.attrs assert xml.Attrs.lang not in e.attrs e = htmlspecials.html().conv(lang="de") assert str(e.attrs.lang) == "de" assert str(e.attrs[xml.Attrs.lang]) == "de" # If ``lang`` is given ``htmlspecials.html`` will not touch it e = htmlspecials.html(lang="en").conv(lang="de") assert str(e.attrs.lang) == "en" assert str(e.attrs[xml.Attrs.lang]) == "de" # If ``xml:lang`` is given ``htmlspecials.html`` will not touch it e = htmlspecials.html(xml.Attrs(lang="en")).conv(lang="de") assert str(e.attrs.lang) == "de" assert str(e.attrs[xml.Attrs.lang]) == "en"
def test_append(): for cls in (xsc.Frag, html.div): node = cls() node.append(1) check_lenstr(node, 1, "1") node.append(2) check_lenstr(node, 2, "12") node.append() check_lenstr(node, 2, "12") node.append(3, 4) check_lenstr(node, 4, "1234") node.append(None) check_lenstr(node, 4, "1234") node.append((5, 6)) check_lenstr(node, 6, "123456") node.append(html.p.Attrs.id(7)) check_lenstr(node, 7, "1234567") with pytest.raises(TypeError): node.append(xml.Attrs(lang=8))