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_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 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_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_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_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.p(self.content) return e.convert(converter)
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_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_clone_plain_attributes(): node = html.p({"data-id": 42}) assert node.clone().string() == '<p data-id="42"></p>'
return elements.richtext(boxes(element), style=style(element)) if __name__ == "__main__": import sys, os, os.path as op from t4.psg.drawing.engine_two.styles import lists from t4.psg.drawing.engine_two.styles.computer_modern \ import cmu_sans_serif as cmuss, style from t4.psg.util.colors import red from t4.psg.drawing.engine_two.processors import render_to_filename from ll.xist.ns import html, chars demo = html.html(html.p(u"Hallo ", html.strong("schöne "), u"Welt!"), html.p(u"DiesesWort", html.strong(u"Keine"), u"Leerzeichen."), html.p(u"Dieses", html.big(" Leerzeichen "), u"ist groß."), html.p(u"Dieses ", html.big("Leerzeichen"), u" ist klein.")) richtext = convert(demo, {"p": {"margin": (12, 0, 0, 0)}}) print richtext.__print__()
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]]
## Copyright 1999-2019 by Walter Dörwald ## ## All Rights Reserved ## ## See ll/xist/__init__.py for the license import pytest from ll import misc from ll.xist import xsc, xfind, parse from ll.xist.ns import html, php, abbr node = xsc.Frag( html.div( html.h1("The ", html.em("important"), " headline"), html.p("The ", html.em("first"), " paragraph."), html.p("The ", html.em("second"), " ", html.em("important"), " paragraph."), align="left", ), html.div( html.h1("The headline", html.img(src="root:gurk.gif")), html.p("The ", html.em("first"), " paragraph."), html.div( html.h2("The ", html.em("important"), " headline"), html.p("The ", html.em("second"), " ", html.em("important"), " paragraph."), id="id42", ), html.div(id="id23"), class_="foo",
def convert(self, converter): e = xsc.Frag( html.h1("Python code coverage"), html.p("Generated at ", ul4.printx("format(now, '%Y-%m-%d %H:%M:%S')"), class_="note"), html.p("Last commit at ", ul4.printx("format(timestamp, '%Y-%m-%d %H:%M:%S')"), " by ", ul4.printx("author"), class_="note"), html.p("Changeset identification hash ", ul4.printx("changesetid"), class_="note"), html.p("Local revision number ", ul4.printx("revision"), class_="note"), html.p(html.a("Build log", href="buildlog.txt"), " ",html.a("Test log", href="testlog.txt"), class_="note"), htmlspecials.plaintable( html.thead( html.tr( html.th("Filename", id="filename"), html.th("# lines", id="nroflines"), html.th("# coverable lines", id="coverablelines"), html.th("# covered lines", id="coveredlines"), html.th("coverage", id="coverage"), html.th("distribution", id="distibution"), ), ), html.tbody( ul4.for_("file in files"), html.tr( html.th( html.a( ul4.printx("file.name"), href=(ul4.printx("file.name"), ".html"), ), class_="filename", ), html.td( ul4.printx("file.lines"), class_="nroflines", ), html.td( ul4.printx("file.coverablelines"), class_="coverablelines", ), html.td( ul4.printx("file.coveredlines"), class_="coveredlines", ), html.td( ul4.if_("file.coverablelines"), ul4.printx("format((100.*file.coveredlines)/file.coverablelines, '.2f')"), "%", ul4.else_(), "n/a", ul4.end("if"), class_=( "coverage", ul4.if_("not file.coverablelines"), " disabled", ul4.end("if"), ), ), html.td( ul4.code("totalwidth = 100"), ul4.if_("file.coverablelines"), ul4.if_("file.coverablelines < file.lines"), ul4.code("width = int(1.*(file.lines-file.coverablelines)/file.lines*100)"), htmlspecials.pixel(src="/spc.gif", width=ul4.printx("width"), height=8, style="background-color: #ccc;"), ul4.code("totalwidth -= width"), ul4.end("if"), ul4.if_("file.coveredlines < file.coverablelines"), ul4.code("width = int(1.*(file.coverablelines-file.coveredlines)/file.lines*100)"), htmlspecials.pixel(src="/spc.gif", width=ul4.printx("width"), height=8, style="background-color: #f00;"), ul4.code("totalwidth -= width"), ul4.end("if"), ul4.if_("totalwidth"), htmlspecials.pixel(src="/spc.gif", width=ul4.printx("totalwidth"), height=8, style="background-color: #0c0;"), ul4.end("if"), ul4.else_(), htmlspecials.pixel(src="/spc.gif", width=ul4.printx("totalwidth"), height=8, style="background-color: #000;"), ul4.end("if"), class_="dist", ), class_="files", ), ul4.end("for"), id="files", ), class_="files", ) ) return e.convert(converter)