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 error_message(self, error): ''' Return a <div class="error"> with the formated error message in it. ''' if error is not None: if type(error) in ( ListType, TupleType, ): if len(error) > 1: ul = html.ul() for e in error: ul.append(html.li(translate(e, self))) error = ul else: error = translate(error[0], self) else: error = translate(error, self) error = html.div(error, class_="error") return error else: return Frag()
def convert(self, converter): e = html.li( html.span(self[name], class_="name") ) for e2 in self[duration]: e.append(" (", e2, ")") e.append(self[purchase]) return e.convert(converter)
def test_pretty(): tests = [ (html.p("apple", "tree"), b"<p>appletree</p>"), (html.p("apple", html.br(), "tree"), b"<p>apple<br />tree</p>"), (html.p(php.php("apple")), b"<p>\n\t<?php apple?>\n</p>"), (html.p(php.php("apple"), "tree"), b"<p><?php apple?>tree</p>"), ( html.div(2*html.p("apple", "tree"), html.br()), b"<div>\n\t<p>appletree</p>\n\t<p>appletree</p>\n\t<br />\n</div>" ), ( html.div( php.php("apple"), html.p("apple", "tree"), html.div( html.p("apple"), html.p("tree"), ), html.br() ), b"<div>\n\t<?php apple?>\n\t<p>appletree</p>\n\t<div>\n\t\t<p>apple</p>\n\t\t<p>tree</p>\n\t</div>\n\t<br />\n</div>" ), ( html.ul( ul4.for_("name in names"), html.li( ul4.printx("name"), ), ul4.end("for"), ), b"<ul>\n\t<?for name in names?>\n\t\t<li>\n\t\t\t<?printx name?>\n\t\t</li>\n\t<?end for?>\n</ul>" ), ( xsc.Frag( ul4.if_("n == 0"), html.span("zero"), ul4.elif_("n == 1"), html.span("one"), ul4.else_(), html.span("many"), ul4.end("if"), ), b"<?if n == 0?>\n\t<span>zero</span>\n<?elif n == 1?>\n\t<span>one</span>\n<?else ?>\n\t<span>many</span>\n<?end if?>" ), ( xsc.Frag( ul4.def_("spam"), ul4.printx("eggs"), ul4.end("def"), ), b"<?def spam?>\n\t<?printx eggs?>\n<?end def?>" ), ] for (got, exp) in tests: got.pretty().bytes() == exp
def convert(self, converter): e = html.li(html.span(self[name], class_="name")) durations = xsc.Frag(self[duration]) rcs = xsc.Frag(self[rc]) if len(durations) or len(rcs): e.append(" (") if len(durations): e.append(durations[0]) if len(rcs): e.append("; ") if len(rcs): e.append("RC ", rcs.withsep(", ")) e.append(")") e.append(self[purchase]) return e.convert(converter)
def convert(self, converter): e = html.li( html.span(self[name], class_="name") ) durations = xsc.Frag(self[duration]) rcs = xsc.Frag(self[rc]) if len(durations) or len(rcs): e.append(" (") if len(durations): e.append(durations[0]) if len(rcs): e.append("; ") if len(rcs): e.append("RC ", rcs.withsep(", ")) e.append(")") e.append(self[purchase]) return e.convert(converter)
def test_pretty(): tests = [ (html.p("apple", "tree"), b"<p>appletree</p>"), (html.p("apple", html.br(), "tree"), b"<p>apple<br />tree</p>"), (html.p(php.php("apple")), b"<p>\n\t<?php apple?>\n</p>"), (html.p(php.php("apple"), "tree"), b"<p><?php apple?>tree</p>"), (html.div(2 * html.p("apple", "tree"), html.br()), b"<div>\n\t<p>appletree</p>\n\t<p>appletree</p>\n\t<br />\n</div>"), (html.div(php.php("apple"), html.p("apple", "tree"), html.div( html.p("apple"), html.p("tree"), ), html.br()), b"<div>\n\t<?php apple?>\n\t<p>appletree</p>\n\t<div>\n\t\t<p>apple</p>\n\t\t<p>tree</p>\n\t</div>\n\t<br />\n</div>" ), (html.ul( ul4.for_("name in names"), html.li(ul4.printx("name"), ), ul4.end("for"), ), b"<ul>\n\t<?for name in names?>\n\t\t<li>\n\t\t\t<?printx name?>\n\t\t</li>\n\t<?end for?>\n</ul>" ), (xsc.Frag( ul4.if_("n == 0"), html.span("zero"), ul4.elif_("n == 1"), html.span("one"), ul4.else_(), html.span("many"), ul4.end("if"), ), b"<?if n == 0?>\n\t<span>zero</span>\n<?elif n == 1?>\n\t<span>one</span>\n<?else ?>\n\t<span>many</span>\n<?end if?>" ), (xsc.Frag( ul4.def_("spam"), ul4.printx("eggs"), ul4.end("def"), ), b"<?def spam?>\n\t<?printx eggs?>\n<?end def?>"), ] for (got, exp) in tests: got.pretty().bytes() == exp
def error_message(self, error): """ Return a <div class="error"> with the formated error message in it. """ if error is not None: if type(error) in (ListType, TupleType): if len(error) > 1: ul = html.ul() for e in error: ul.append(html.li(translate(e, self))) error = ul else: error = translate(error[0], self) else: error = translate(error, self) error = html.div(error, class_="error") return error else: return Frag()
def test_css(): with xsc.build(): with html.div(id=1) as e: with html.ul(id=2): +html.li("foo") +html.li() assert list(e.walknodes(css.selector("div"))) == [e] assert list(e.walknodes(css.selector("li"))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("div#1"))) == [e] assert list(e.walknodes(css.selector("#2"))) == [e[0]] assert list(e.walknodes(css.selector(":empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("li:empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("div :empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("div>*:empty"))) == [] assert list(e.walknodes(css.selector("div>:empty"))) == [] assert list(e.walknodes(css.selector("*|li"))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("h|li", prefixes={"h": html}))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("h|li", prefixes={"h": specials}))) == [] with xsc.build(): with xsc.Frag() as e: +html.div("foo") +xsc.Text("filler") +html.p("foo") +xsc.Text("filler") +html.ul(html.li("foo")) assert list(e.walknodes(css.selector("div + p"))) == [e[2]] assert list(e.walknodes(css.selector("div + ul"))) == [] assert list(e.walknodes(css.selector("ul + p"))) == [] assert list(e.walknodes(css.selector("div ~ p"))) == [e[2]] assert list(e.walknodes(css.selector("div ~ ul"))) == [e[4]] assert list(e.walknodes(css.selector("p ~ div"))) == [] assert list(e.walknodes(css.selector("div:first-child + p"))) == [e[2]] assert list(e.walknodes(css.selector("*:first-child + p"))) == [e[2]] with xsc.build(): with xsc.Frag() as e: +html.span(html.b("hurz"), "gurk", html.em("hinz"), html.em("kunz")) +html.em("hurz") +html.em("hinz") +xsc.Text("nix") +html.i("kunz") assert list(e.walknodes(css.selector("*:only-of-type"))) == [e[0], e[0][0], e[4]] assert list(e.walknodes(css.selector("*:nth-child(1)"))) == [e[0], e[0][0]] assert list(e.walknodes(css.selector("*:nth-child(2)"))) == [e[0][2], e[1]] assert list(e.walknodes(css.selector("*:nth-last-child(1)"))) == [e[0][3], e[4]] assert list(e.walknodes(css.selector("*:nth-last-child(2)"))) == [e[0][2], e[2]] assert list(e.walknodes(css.selector("*:nth-of-type(1)"))) == [e[0], e[0][0], e[0][2], e[1], e[4]] assert list(e.walknodes(css.selector("*:nth-of-type(2)"))) == [e[0][3], e[2]] assert list(e.walknodes(css.selector("*:nth-last-of-type(1)"))) == [e[0], e[0][0], e[0][3], e[2], e[4]] assert list(e.walknodes(css.selector("*:nth-last-of-type(2)"))) == [e[0][2], e[1]] e = xsc.Frag(html.span(html.b("hurz"), "gurk")) assert list(e.walknodes(css.selector("*:only-child"))) == [e[0], e[0][0]] with xsc.build(): with xsc.Frag() as e: +html.em(class_="gurk", lang="en") +html.em(class_="gurk hurz", lang="en-us") +html.em(class_="hurz", lang="de") assert list(e.walknodes(css.selector("em[class='gurk']"))) == [e[0]] assert list(e.walknodes(css.selector("em[class~='gurk']"))) == [e[0], e[1]] assert list(e.walknodes(css.selector("em[lang|='en']"))) == [e[0], e[1]]
def test_css(): with xsc.build(): with html.div(id=1) as e: with html.ul(id=2): +html.li("foo") +html.li() assert list(e.walknodes(css.selector("div"))) == [e] assert list(e.walknodes(css.selector("li"))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("div#1"))) == [e] assert list(e.walknodes(css.selector("#2"))) == [e[0]] assert list(e.walknodes(css.selector(":empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("li:empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("div :empty"))) == [e[0][1]] assert list(e.walknodes(css.selector("div>*:empty"))) == [] assert list(e.walknodes(css.selector("div>:empty"))) == [] assert list(e.walknodes(css.selector("*|li"))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("h|li", prefixes={"h": html }))) == [e[0][0], e[0][1]] assert list(e.walknodes(css.selector("h|li", prefixes={"h": specials}))) == [] with xsc.build(): with xsc.Frag() as e: +html.div("foo") +xsc.Text("filler") +html.p("foo") +xsc.Text("filler") +html.ul(html.li("foo")) assert list(e.walknodes(css.selector("div + p"))) == [e[2]] assert list(e.walknodes(css.selector("div + ul"))) == [] assert list(e.walknodes(css.selector("ul + p"))) == [] assert list(e.walknodes(css.selector("div ~ p"))) == [e[2]] assert list(e.walknodes(css.selector("div ~ ul"))) == [e[4]] assert list(e.walknodes(css.selector("p ~ div"))) == [] assert list(e.walknodes(css.selector("div:first-child + p"))) == [e[2]] assert list(e.walknodes(css.selector("*:first-child + p"))) == [e[2]] with xsc.build(): with xsc.Frag() as e: +html.span(html.b("hurz"), "gurk", html.em("hinz"), html.em("kunz")) +html.em("hurz") +html.em("hinz") +xsc.Text("nix") +html.i("kunz") assert list(e.walknodes( css.selector("*:only-of-type"))) == [e[0], e[0][0], e[4]] assert list(e.walknodes(css.selector("*:nth-child(1)"))) == [e[0], e[0][0]] assert list(e.walknodes(css.selector("*:nth-child(2)"))) == [e[0][2], e[1]] assert list(e.walknodes( css.selector("*:nth-last-child(1)"))) == [e[0][3], e[4]] assert list(e.walknodes( css.selector("*:nth-last-child(2)"))) == [e[0][2], e[2]] assert list(e.walknodes(css.selector("*:nth-of-type(1)"))) == [ e[0], e[0][0], e[0][2], e[1], e[4] ] assert list(e.walknodes( css.selector("*:nth-of-type(2)"))) == [e[0][3], e[2]] assert list(e.walknodes(css.selector("*:nth-last-of-type(1)"))) == [ e[0], e[0][0], e[0][3], e[2], e[4] ] assert list(e.walknodes( css.selector("*:nth-last-of-type(2)"))) == [e[0][2], e[1]] e = xsc.Frag(html.span(html.b("hurz"), "gurk")) assert list(e.walknodes(css.selector("*:only-child"))) == [e[0], e[0][0]] with xsc.build(): with xsc.Frag() as e: +html.em(class_="gurk", lang="en") +html.em(class_="gurk hurz", lang="en-us") +html.em(class_="hurz", lang="de") assert list(e.walknodes(css.selector("em[class='gurk']"))) == [e[0]] assert list(e.walknodes(css.selector("em[class~='gurk']"))) == [e[0], e[1]] assert list(e.walknodes(css.selector("em[lang|='en']"))) == [e[0], e[1]]