def widget(self, **kw): extra_args = self.extra_args.copy() extra_args.update(kw) ret = html.div(**extra_args) current = self.value() for name, title in self.mapping(): radio_button_id = "radio-button-%i" % id(name) if name == current: checked = "checked" else: checked = None button = Frag( html.input(type="radio", name=self.name, value=name, checked=checked, id=radio_button_id), html.label(title, for_=radio_button_id), ) if self.div: ret.append(html.div(button, class_="radio-button-row")) else: ret.append(button, " ") return ret
def row(self, plone=False, error=None, **kw): """ Return a row for an HTML form <div class="row"> <div class="label">self.title</div> <div class="help">self._help if non-null</div> <div class="error">error if non-null</div> <div class="field"> self.widget() </div> </div> @param plone: Set attributes so that this form may be used with CMFPlone`s JavaScript (unimplemented) @param error: Error message to be displayed along with the widget. """ return html.div( self.label(), self.help(), self.error_message(error), html.div(self.widget(**kw), class_="field"), class_="row", )
def test_mapped(): def maplang(node, converter): if isinstance(node, xsc.Text): node = node.replace("lang", converter.lang) return node node = xsc.Frag( "lang", html.div( "lang", class_="lang", ) ) node2 = node.mapped(maplang, lang="en") assert node == xsc.Frag( "lang", html.div( "lang", class_="lang", ) ) assert node2 == xsc.Frag( "en", html.div( "en", class_="lang", # No replacement in attributes ) )
def convert(self, converter): e = xsc.Frag( xml.XML(), "\n", html.DocTypeXHTML10transitional(), "\n", html.html( html.head( meta.contenttype(), html.title( "Python code coverage", ul4.if_("filename"), ": ", ul4.printx("filename"), ul4.else_(), " (", ul4.print_("format(timestamp, '%Y-%m-%d')"), ")", ul4.end("if"), ), meta.stylesheet(href="/coverage.css"), meta.stylesheet(href="/coverage_sortfilelist.css"), htmlspecials.javascript(src="/coverage.js"), ), html.body( html.div( html.div( html.a( htmlspecials.autoimg(src="http://www.python.org/images/python-logo.gif", alt="Python", border=0), href="http://www.python.org/", ), class_="logo", ), html.div( ul4.for_("(i, item) in enumerate(crumbs)"), html.span( html.span( ul4.if_("i"), ">", ul4.else_(), "\xbb", ul4.end("if"), class_="bullet", ), ul4.if_("item.href"), html.a(ul4.printx("item.title"), href=ul4.printx("item.href")), ul4.else_(), html.span(ul4.printx("item.title"), class_="here"), ul4.end("if"), ul4.end("for"), class_="crumb", ), class_="crumbs", ), class_="header", ), html.div( self.content, class_="content", ), onload=ul4.attr_if(ul4.printx("onload"), cond="onload"), ), ), ) return e.convert(converter)
def widget(self, **kw): extra_args = self.extra_args.copy() extra_args.update(kw) ret = html.div(**extra_args) current = self.value() for name, title in self.mapping(): radio_button_id = "radio-button-%i" % id(name) if name == current: checked = 'checked' else: checked = None button = Frag( html.input(type="radio", name=self.name, value=name, checked=checked, id=radio_button_id), html.label(title, for_=radio_button_id)) if self.div: ret.append(html.div(button, class_="radio-button-row")) else: ret.append(button, " ") return ret
def test_number(): node = html.div(class_=1234) assert int(node["class"]) == 1234 assert abs(float(node["class"]) - 1234.) < 1e-2 node = html.div(class_="1+1j") compl = complex(node["class"]) assert abs(compl.real - 1.) < 1e-2 assert abs(compl.imag - 1.) < 1e-2
def test_publishstyleattr(): node = html.div(style=None) assert node.bytes(xhtml=1) == b"""<div></div>""" node = html.div(style="background-image: url(root:gurk.html)") assert node.bytes(xhtml=1) == b"""<div style="background-image: url(root:gurk.html)"></div>""" assert node.bytes(xhtml=1, base="root:gurk.html") == b"""<div style="background-image: url()"></div>""" assert node.bytes(xhtml=1, base="root:hurz.html") == b"""<div style="background-image: url(gurk.html)"></div>"""
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 check(encoding): node = xsc.Frag( html.div( php.php("echo $foo"), abbr.html(), html.div("gurk", class_="hurz"), "\u3042", ) ) s = node.bytes(encoding=encoding) node2 = parse.tree(s, parse.Expat(), parse.NS(html), xsc.Pool(html, php, abbr)) assert node == node2
def check(encoding): node = xsc.Frag( html.div( php.php("echo $foo"), abbr.html(), html.div("gurk", class_="hurz"), "\u3042", )) s = node.bytes(encoding=encoding) node2 = parse.tree(s, parse.Expat(), parse.NS(html), xsc.Pool(html, php, abbr)) assert node == node2
def test_walkgetitem(): e = html.div(1, html.div(2, html.div(3))) isdiv = xfind.selector(html.div) # Test ``walknodes`` assert str(misc.first(e.walknodes(isdiv))) == "123" assert str(misc.last(e.walknodes(isdiv))) == "3" misc.item(e.walknodes(isdiv), 3) is None misc.item(e.walknodes(isdiv), -4) is None # Test ``walkpaths`` assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123" assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3" misc.item(e.walkpaths(isdiv), 3) is None misc.item(e.walkpaths(isdiv), -4) is None
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_html(self, converter): target = converter.target context = converter[self] context.sections[-1] += 1 level = len(context.sections) context.sections.append(0) # for numbering the subsections ts = xsc.Frag() cs = html.div(class_="content") for child in self: if isinstance(child, h): ts.append(child) else: cs.append(child) e = target.div(class_=("section level", level), id=self.attrs.id) if "role" in self.attrs: e.attrs.class_.append(" ", self.attrs.role) #if "id" in self.attrs: # e.append(target.a(name=self.attrs.id, id=self.attrs.id)) hclass = getattr(target, f"h{level}", target.h6) for t in ts: e.append(hclass(t.content)) e.append(cs) with _stack(context, self): # make sure to call the inner convert() before popping the number off of the stack e = e.convert(converter) del context.sections[-1] return e
def test_itemsslices(): def check(node, expr, ids): assert "".join(str(e.attrs.id) for e in node.walknodes(expr)) == ids # ____0____ # / | \ # _1_ _2_ _3_ # / \ / \ / \ # 4 5 6 7 ds = [html.div(id=id) for id in range(8)] ds[0].append(ds[1], ds[2], ds[3]) ds[1].append(ds[4], ds[5]) ds[2].append(ds[5], ds[6]) ds[3].append(ds[6], ds[7]) tests = [ (ds[0] / html.div[0] / html.div[-1], "5"), (ds[0] / html.div / html.div[-1], "567"), (ds[0] / html.div[-1] / html.div, "67"), (ds[0] / html.div / html.div, "455667"), # we get 5 and 6 twice #(ds[0]/(html.div/html.div) & xfind.nthchild(2), u"5"), # we get 5 and 6 twice #(ds[0]/html.div[:]/html.div[:], u"455667"), (ds[0] / html.div / html.p[0], ""), (ds[0] / html.p[0] / html.p[0], ""), (ds[0] // html.div, "145256367"), ] for (got, exp) in tests: check(ds[0], got, exp)
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_clone(): for cls in (xsc.Frag, html.div): e = html.div(1) src = cls(1, e, e) dst = src.clone() assert src is not dst assert src[0] is dst[0] # Text nodes are immutable and shared assert src[1] is not dst[1] assert dst[1] is not dst[2] e.append(e) # create a cycle dst = src.copy() assert src is not dst assert src[0] is dst[0] assert src[1] is dst[1] assert dst[1] is dst[2] dst = src.deepcopy() assert src is not dst assert src[0] is dst[0] assert src[1] is not dst[1] assert dst[1] is dst[2] e = html.div(id=(17, html.div(23), 42)) for src in (e, e.attrs): dst = src.clone() assert src["id"] is not dst["id"] assert src["id"][0] is dst["id"][0] assert src["id"][1] is not dst["id"][1] e["id"][1] = e # create a cycle e["id"][2] = e # create a cycle for src in (e, e.attrs): dst = src.copy() assert src["id"] is dst["id"] assert src["id"][0] is dst["id"][0] assert src["id"][1] is dst["id"][1] assert dst["id"][1] is dst["id"][2] dst = src.deepcopy() assert src["id"] is not dst["id"] assert src["id"][0] is dst["id"][0] assert src["id"][1] is not dst["id"][1] assert dst["id"][1] is dst["id"][2]
def convert(self, converter): e = html.div(self[place], class_="purchase") for e2 in self[price]: e.append(": ", e2) e.append(" ") for e2 in self[date]: e.append("(", e2, ")") return e.convert(converter)
def test_xmlheader(): assert xml.XML().bytes( encoding="utf-8") == b'<?xml version="1.0" encoding="utf-8"?>' assert xml.XML().bytes( encoding="latin-1") == b'<?xml version="1.0" encoding="latin-1"?>' assert html.div(xml.XML()).bytes( encoding="latin-1" ) == b'<div><?xml version="1.0" encoding="latin-1"?></div>'
def test_getitem(): for cls in (xsc.Frag, html.div): e = cls(list(range(6))) # int assert e[2] == xsc.Text(2) assert e[-1] == xsc.Text(5) # slice assert e[:] == cls(list(range(6))) assert e[:2] == cls(0, 1) assert e[-2:] == cls(4, 5) assert e[::2] == cls(0, 2, 4) assert e[1::2] == cls(1, 3, 5) assert e[::-1] == cls(list(range(5, -1, -1))) # selector e = cls((html.dt(i), html.dd(2 * i)) for i in range(3)) assert xsc.Frag(e[html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[html.dt[1]]) == xsc.Frag(html.dt(1)) assert e[e[0]][0] is e[ 0] # selector for a single node (returns an iterator nevertheless) def isgt1(p): return int(str(p[-1])) > 1 assert xsc.Frag(e[html.dt & isgt1]) == xsc.Frag(html.dt(2)) assert xsc.Frag(e[e / html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[e.__class__ / html.dt]) == xsc.Frag( html.dt(0), html.dt(1), html.dt(2)) for attr in ("class_", xml.Attrs.lang): e = cls("foo", html.div("bar", {attr: "gurk"}), "baz") i = e[xsc.Text] assert str(next(i)) == "foo" assert str(next(i)) == "baz" with pytest.raises(StopIteration): next(i) # list for attr in ("class", xml.Attrs.lang): node = cls(html.div("foo", html.div("bar", {attr: "gurk"}), "baz")) assert node[[]] is node assert str(node[[0, 1]]) == "bar" assert str(node[[0, 1, attr]]) == "gurk"
def test_mul(): node = xsc.Frag("a") assert 3*node == xsc.Frag(list("aaa")) assert node*3 == xsc.Frag(list("aaa")) node = html.div() assert 3*node == xsc.Frag(html.div(), html.div(), html.div()) assert node*3 == xsc.Frag(html.div(), html.div(), html.div())
def test_mul(): node = xsc.Frag("a") assert 3 * node == xsc.Frag(list("aaa")) assert node * 3 == xsc.Frag(list("aaa")) node = html.div() assert 3 * node == xsc.Frag(html.div(), html.div(), html.div()) assert node * 3 == xsc.Frag(html.div(), html.div(), html.div())
def test_item(): e = html.div(range(10)) assert str(misc.item(e[xsc.Text], 0)) == "0" assert str(misc.item(e[xsc.Text], 9)) == "9" assert str(misc.item(e[xsc.Text], -1)) == "9" assert str(misc.item(e[xsc.Text], -10)) == "0" misc.item(e[xsc.Text], 10) is None misc.item(e[xsc.Text], -11) is None assert str(misc.item(e[xsc.Text], 10, "x")) == "x" assert str(misc.item(e[xsc.Text], -11, "x")) == "x"
def test_setitem(): for cls in (xsc.Frag, html.div): e = cls(range(6)) e[1] = 10 assert e == cls(0, 10, 2, 3, 4, 5) e[1] = None assert e == cls(0, 2, 3, 4, 5) e[1] = () assert e == cls(0, 3, 4, 5) e = cls(range(6)) e[-1] = None assert e == cls(0, 1, 2, 3, 4) e = cls(range(6)) e[1:5] = (100, 200) assert e == cls(0, 100, 200, 5) e = cls(range(6)) e[:] = (100, 200) assert e == cls(100, 200) e = cls(range(6)) e[::2] = (100, 120, 140) assert e == cls(100, 1, 120, 3, 140, 5) e = cls(range(6)) e[1::2] = (110, 130, 150) assert e == cls(0, 110, 2, 130, 4, 150) e = cls(range(6)) e[::-1] = range(6) assert e == cls(range(5, -1, -1)) for attr in ("class_", xml.Attrs.lang): node = cls(html.div("foo", html.div({attr: "gurk"}), "bar")) node[[0, 1, attr]] = "hurz" assert str(node[[0, 1, attr]]) == "hurz" with pytest.raises(ValueError): node[[]] = None with pytest.raises(ValueError): del node[[]]
def test_setitem(): for cls in (xsc.Frag, html.div): e = cls(list(range(6))) e[1] = 10 assert e == cls(0, 10, 2, 3, 4, 5) e[1] = None assert e == cls(0, 2, 3, 4, 5) e[1] = () assert e == cls(0, 3, 4, 5) e = cls(list(range(6))) e[-1] = None assert e == cls(0, 1, 2, 3, 4) e = cls(list(range(6))) e[1:5] = (100, 200) assert e == cls(0, 100, 200, 5) e = cls(list(range(6))) e[:] = (100, 200) assert e == cls(100, 200) e = cls(list(range(6))) e[::2] = (100, 120, 140) assert e == cls(100, 1, 120, 3, 140, 5) e = cls(list(range(6))) e[1::2] = (110, 130, 150) assert e == cls(0, 110, 2, 130, 4, 150) e = cls(list(range(6))) e[::-1] = list(range(6)) assert e == cls(list(range(5, -1, -1))) for attr in ("class_", xml.Attrs.lang): node = cls(html.div("foo", html.div({attr: "gurk"}), "bar")) node[[0, 1, attr]] = "hurz" assert str(node[[0, 1, attr]]) == "hurz" with pytest.raises(ValueError): node[[]] = None with pytest.raises(ValueError): del node[[]]
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 test_getitem(): for cls in (xsc.Frag, html.div): e = cls(range(6)) # int assert e[2] == xsc.Text(2) assert e[-1] == xsc.Text(5) # slice assert e[:] == cls(range(6)) assert e[:2] == cls(0, 1) assert e[-2:] == cls(4, 5) assert e[::2] == cls(0, 2, 4) assert e[1::2] == cls(1, 3, 5) assert e[::-1] == cls(range(5, -1, -1)) # selector e = cls((html.dt(i), html.dd(2*i)) for i in range(3)) assert xsc.Frag(e[html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[html.dt[1]]) == xsc.Frag(html.dt(1)) assert e[e[0]][0] is e[0] # selector for a single node (returns an iterator nevertheless) def isgt1(p): return int(str(p[-1]))>1 assert xsc.Frag(e[html.dt & isgt1]) == xsc.Frag(html.dt(2)) assert xsc.Frag(e[e/html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[e.__class__/html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) for attr in ("class_", xml.Attrs.lang): e = cls("foo", html.div("bar", {attr: "gurk"}), "baz") i = e[xsc.Text] assert str(next(i)) == "foo" assert str(next(i)) == "baz" with pytest.raises(StopIteration): next(i) # list for attr in ("class", xml.Attrs.lang): node = cls(html.div("foo", html.div("bar", {attr: "gurk"}), "baz")) assert node[[]] is node assert str(node[[0, 1]]) == "bar" assert str(node[[0, 1, attr]]) == "gurk"
def help(self, **kw): """ @returns: A html.div instance containing the help message. The key word arguments will be added to the <div> as arguments. The css class defaults to 'help' """ if not kw.has_key("class_"): kw["class_"] = "help" if self._help: return html.div(self._help, **kw) else: return Frag()
def row(self, plone=False, error=None, **kw): ''' Return a row for an HTML form <div class="row"> <div class="label">self.title</div> <div class="help">self._help if non-null</div> <div class="error">error if non-null</div> <div class="field"> self.widget() </div> </div> @param plone: Set attributes so that this form may be used with CMFPlone`s JavaScript (unimplemented) @param error: Error message to be displayed along with the widget. ''' return html.div(self.label(), self.help(), self.error_message(error), html.div(self.widget(**kw), class_="field"), class_="row")
def test_walkgetitem(): e = html.div( 1, html.div( 2, html.div( 3 ) ) ) isdiv = xfind.selector(html.div) # Test ``walknodes`` assert str(misc.first(e.walknodes(isdiv))) == "123" assert str(misc.last(e.walknodes(isdiv))) == "3" misc.item(e.walknodes(isdiv), 3) is None misc.item(e.walknodes(isdiv), -4) is None # Test ``walkpaths`` assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123" assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3" misc.item(e.walkpaths(isdiv), 3) is None misc.item(e.walkpaths(isdiv), -4) is None
def test_multiall(): def check(node, expr, ids): assert "".join(str(e.attrs.id) for e in node.walknodes(expr)) == ids # ____0____ # / \ # _1_ _2_ # / \ / \ # 3 4 5 6 # / \ / \ / \ / \ # 7 8 9 a b c d e ds = [html.div(id=hex(id).lower()[2:]) for id in range(15)] for i in range(7): ds[i].append(ds[2 * i + 1:2 * i + 3]) check(ds[0], ds[0] // html.div // html.div, "37849a5bc6de")
def convert(self, converter): content = xsc.Frag() authors = xsc.Frag() sources = xsc.Frag() for child in self: if isinstance(child, author): authors.append(child) elif isinstance(child, source): sources.append(child) else: content.append(child) if authors: if sources: footer = html.div(authors, " ", chars.mdash(), " ", sources, class_="source") else: footer = html.div(authors, class_="source") else: if sources: footer = html.div(sources, class_="source") else: footer = None e = html.div(content, footer, class_="quotation") return e.convert(converter)
def createfrag(): return xsc.Frag( xml.XML(), html.DocTypeHTML401transitional(), xsc.Comment("gurk"), "hurz", specials.tab(), abbr.xist(), None, True, False, 1, 2.0, "3", "4", (5, 6), [7, 8], html.div( align="left" ), url.URL("http://www.python.org/"), html.span( 1, 2, class_="gurk", id=(1, 2, (3, 4)), lang=( True, False, url.URL("http://www.python.org/"), html.abbr( xml.XML(), "hurz", specials.tab(), abbr.xist(), None, 1, 2.0, "3", "4", (5, 6), [7, 8], html.span("gurk"), title="hurz" ) ) ) )
def widget(self, **kw): extra_args = self.extra_args.copy() extra_args.update(kw) value = self.value() if value: checked = "checked" else: checked = None cbid = "check-box-%i" % id(self) return html.div( html.input(type="checkbox", checked=checked, name=self.name, id=cbid), html.label(self.title, for_=cbid), class_="checkbox", **extra_args )
def label(self, **kw): """ @returns: A html.div instance containing the widget's label (title attribute). The key word arguments will be added to the <div> as arguments. The css class defaults to 'label' """ if not kw.has_key("class_"): kw["class_"] = "label" required = False for validator in self.dbproperty.validators: if isinstance(validator, not_null_validator) or isinstance(validator, not_empty_validator): required = True break if required: kw["class_"] += " required" return html.div(self.title, **kw)
def widget(self, **kw): extra_args = self.extra_args.copy() extra_args.update(kw) value = self.value() if value: checked = "checked" else: checked = None cbid = "check-box-%i" % id(self) return html.div(html.input(type="checkbox", checked=checked, name=self.name, id=cbid), html.label(self.title, for_=cbid), class_="checkbox", **extra_args)
def label(self, **kw): """ @returns: A html.div instance containing the widget's label (title attribute). The key word arguments will be added to the <div> as arguments. The css class defaults to 'label' """ if not kw.has_key("class_"): kw["class_"] = "label" required = False for validator in self.dbproperty.validators: if isinstance(validator, not_null_validator) or \ isinstance(validator, not_empty_validator): required = True break if required: kw["class_"] += " required" return html.div(self.title, **kw)
def test_levels(): def check(node, expr, ids): assert "".join(str(e.attrs.id) for e in node.walknodes(expr)) == ids ds = [html.div(id=id) for id in range(8)] ds[1].append(ds[4:7]) ds[2].append(ds[7]) ds[0].append(ds[1:4]) # ____0____ # / | \ # _1_ 2 3 # / | \ | # 4 5 6 7 tests = [ (ds[0] // html.div, "1456273"), (ds[0] / html.div, "123"), (ds[0] / html.div / html.div, "4567"), (ds[0] / html.div / html.div / html.div, ""), ] for (got, exp) in tests: check(ds[0], got, 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 createfrag(): return xsc.Frag( xml.XML(), html.DocTypeHTML401transitional(), xsc.Comment("gurk"), "hurz", specials.tab(), abbr.xist(), None, True, False, 1, 2.0, "3", "4", (5, 6), [7, 8], html.div(align="left"), url.URL("http://www.python.org/"), html.span(1, 2, class_="gurk", id=(1, 2, (3, 4)), lang=(True, False, url.URL("http://www.python.org/"), html.abbr(xml.XML(), "hurz", specials.tab(), abbr.xist(), None, 1, 2.0, "3", "4", (5, 6), [7, 8], html.span("gurk"), title="hurz"))))
def convert(self, converter): e = html.div(self.content, class_="description") 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_write(): node = html.div() s = io.BytesIO() node.write(s, xhtml=2) assert s.getvalue() == b"<div/>"
def test_comment_in_attr(): node = html.div(class_=xsc.Comment("gurk")) assert node.bytes() == b"""<div class=""></div>"""
def test_replaceurls(): node = html.div("gurk", style="background-image: url(gurk.gif);") node.attrs.style.replaceurls(lambda u: url.URL("http://www.example.org")/u) assert str(node.attrs.style) == "background-image: url(http://www.example.org/gurk.gif)"
def test_doctype_in_attr(): node = html.div(class_=html.DocTypeXHTML11()) assert node.bytes() == b"""<div class=""></div>"""
## ## 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 test_elementeq(): assert html.div() == html.div() assert html.div(1) == html.div(1) assert html.div(1, 2) == html.div(1, 2, None) assert html.div(1, 2) != html.div(12) assert html.div() != html.div("") assert html.div("") != html.div("", "") assert html.div(1, html.div(2, html.div(3))) == html.div(1, html.div(2, html.div(3))) # Test plain element instances plainelement = xsc.element(html, "div") assert plainelement.__class__ is xsc.Element assert plainelement.xmlns == html.div.xmlns assert plainelement.xmlname == html.div.xmlname assert html.div() == plainelement
def convert(self, converter): e = html.div(self.content, class_="note") return e.convert(converter)
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" id="id42" align="right" class="foo"></div>"""