def test_with_addattr(): with xsc.build(): with html.ul() as e: with xsc.addattr("id"): +xsc.Text("gurk") assert e == html.ul(id="gurk") with xsc.build(): with html.ul() as e: with xsc.addattr(html.ul.Attrs.id): +xsc.Text("gurk") assert e == html.ul(id="gurk")
def test_with_addattr(): with xsc.build(): with html.ul() as e: with xsc.addattr("id"): +xsc.Text("gurk") assert e == html.ul(id="gurk") with xsc.build(): with html.ul() as e: with xsc.addattr(html.ul.Attrs.id): +xsc.Text("gurk") assert e == html.ul(id="gurk")
def test_applystylesheets_media(): # Check that media="screen" picks up the media stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" # Check that media="screen" doesn't pick up the print stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == "" # Check that @media rules are treated properly with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == ""
def test_text(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("foo") +detox.end("def") assert makeoutput(e, "gurk") == "foo"
def convert(self, converter): def namekey(node): return str(node[name][0].content) dvds = xsc.Frag(self[dvd]).sorted(key=namekey) lds = xsc.Frag(self[ld]).sorted(key=namekey) with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("Media") +meta.stylesheet(href="Media.css") with htmlspecials.plainbody(): +html.h1("Media") if lds: +html.h2(len(lds), " LDs") +html.ol(lds) if dvds: +html.h2(len(dvds), " DVDs") +html.ol(dvds) return e.convert(converter)
def test_expr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.expr("arg") assert makeoutput(e, "gurk", "hurz") == "hurz"
def test_expr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.expr("arg") assert makeoutput(e, "gurk", "hurz") == "hurz"
def convert(self, converter): def namekey(node): return str(node[name][0].content) dvds = xsc.Frag(self[dvd]).sorted(key=namekey) lds = xsc.Frag(self[ld]).sorted(key=namekey) with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("Media") +meta.stylesheet(href="Media.css") with htmlspecials.plainbody(): +html.h1("Media") if lds: +html.h2(len(lds), " LDs") +html.ol(lds) if dvds: +html.h2(len(dvds), " DVDs") +html.ol(dvds) return e.convert(converter)
def index(self): def isimg(name): if name.endswith(".gif") or name.endswith(".jpg") or name.endswith(".png"): return os.path.isfile(os.path.join(self.directory, name)) return False names = [name for name in os.listdir(self.directory) if isimg(name)] names.sort() collect = xsc.Frag() i = 0 with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("All images from ", doc.z(self.directory)) +html.link(rel="stylesheet", type="text/css", href="images.css") with html.body(): with htmlspecials.plaintable(): for name in names: collect.append(html.td(htmlspecials.autoimg(src=("/images/", name)), html.br(), name, align="center")) i += 1 if i == cols: +html.tr(collect) collect = xsc.Frag() i = 0 if collect: +html.tr(collect) return e.conv().bytes()
def test_text(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("foo") +detox.end("def") assert makeoutput(e, "gurk") == "foo"
def test_applystylesheets_media(): # Check that media="screen" picks up the media stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" # Check that media="screen" doesn't pick up the print stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == "" # Check that @media rules are treated properly with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == ""
def test_scopecheck(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end() assert makeoutput(e, "gurk") == "hurz" with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end("for") with pytest.raises(SyntaxError): makeoutput(e, "gurk")
def test_textexpr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk()"): +detox.code("""s = '"a" < "b" & "b" > "a"'""") +detox.textexpr("s") assert makeoutput(e, "gurk") == '"a" < "b" & "b" > "a"'
def test_for(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): with forblock(loop="i in range(arg)"): +detox.expr("str(i)") assert makeoutput(e, "gurk", 3) == "012"
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_textexpr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk()"): +detox.code("""s = '"a" < "b" & "b" > "a"'""") +detox.textexpr("s") assert makeoutput(e, "gurk") == '"a" < "b" & "b" > "a"'
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_scopecheck(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end() assert makeoutput(e, "gurk") == "hurz" with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end("for") with pytest.raises(SyntaxError): makeoutput(e, "gurk")
def test_for(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): with forblock(loop="i in range(arg)"): +detox.expr("str(i)") assert makeoutput(e, "gurk", 3) == "012"
def makenode(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") +html.style("p {color: blue;}", type="text/css", title="blue") with html.body(): +html.p("gurk") return e
def makenode(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") +html.style( "p {color: blue;}", type="text/css", title="blue") with html.body(): +html.p("gurk") return e
def test_while(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.code("i = 0") with whileblock(loop="i < arg"): +detox.expr("str(i)") +detox.code("i += 1") assert makeoutput(e, "gurk", 3) == "012"
def test_while(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.code("i = 0") with whileblock(loop="i < arg"): +detox.expr("str(i)") +detox.code("i += 1") assert makeoutput(e, "gurk", 3) == "012"
def convert(self, converter): with xsc.build(): with xsc.Frag() as e: if self[term] or self[classifier]: with doc.dt(): +xsc.Frag(self[term]) if self[classifier]: +xsc.Text(" (") +xsc.Frag(self[classifier]).withsep(", ") +xsc.Text(")") +xsc.Frag(self[definition]) return e.convert(converter)
def test_applystylesheets1(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e) assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets1(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e) assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets5(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # stylesheet always wins (at least in CSS 2.1 and 3) assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets5(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # stylesheet always wins (at least in CSS 2.1 and 3) assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets4(): with xsc.build(): with html.html() as e: with html.head(): +html.style("#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # style attribute wins assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets4(): with xsc.build(): with html.html() as e: with html.head(): +html.style("#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # style attribute wins assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def test_if(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.if_("arg>2") +detox.expr("str(2*arg)") +detox.else_() +detox.expr("str(3*arg)") +detox.end("if") assert makeoutput(e, "gurk", 0) == "0" assert makeoutput(e, "gurk", 1) == "3" assert makeoutput(e, "gurk", 2) == "6" assert makeoutput(e, "gurk", 3) == "6" assert makeoutput(e, "gurk", 4) == "8"
def test_if(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.if_("arg>2") +detox.expr("str(2*arg)") +detox.else_() +detox.expr("str(3*arg)") +detox.end("if") assert makeoutput(e, "gurk", 0) == "0" assert makeoutput(e, "gurk", 1) == "3" assert makeoutput(e, "gurk", 2) == "6" assert makeoutput(e, "gurk", 3) == "6" assert makeoutput(e, "gurk", 4) == "8"
def convert(self, converter): with xsc.build(): with xsc.Frag() as e: +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title(self[title][0].content) +meta.stylesheet(href="root:python-quotes.css") +html.body( self[title], self[editor], self[description], self[quotation] ) return e.convert(converter)
def index(self): def isimg(name): if name.endswith(".gif") or name.endswith(".jpg") or name.endswith( ".png"): return os.path.isfile(os.path.join(self.directory, name)) return False names = [name for name in os.listdir(self.directory) if isimg(name)] names.sort() collect = xsc.Frag() i = 0 with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("All images from ", doc.z(self.directory)) +html.link(rel="stylesheet", type="text/css", href="images.css") with html.body(): with htmlspecials.plaintable(): for name in names: collect.append( html.td( htmlspecials.autoimg(src=("/images/", name)), html.br(), name, align="center")) i += 1 if i == cols: +html.tr(collect) collect = xsc.Frag() i = 0 if collect: +html.tr(collect) return e.conv().bytes()
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]]