def test_noelementsortext(): with xsc.Pool(): class el1(xsc.Element): xmlns = "ns1" model = sims.NoElementsOrText() class el2(xsc.Element): xmlns = "ns2" e = el1() e.bytes(validate=True) e = el1("foo") with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.IllegalTextWarning) e = el1(php.php("gurk")) e.bytes(validate=True) e = el1(xsc.Comment("gurk")) e.bytes(validate=True) e = el1(el1()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.ElementWarning) # Elements from a different namespace are OK e = el1(el2()) e.bytes(validate=True)
def comment(self, data): node = xsc.Comment(data) if self.loc: node.startloc = xsc.Location(self._url, *self._position) node.parsed(self, "comment") if self._inattr: self._stack[-1].append(node) elif not self._indoctype: return ("commentnode", node)
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_elements(): with xsc.Pool(): class el11(xsc.Element): xmlname = "el1" xmlns = "ns1" class el12(xsc.Element): xmlname = "el2" xmlns = "ns1" class el21(xsc.Element): xmlname = "el1" xmlns = "ns2" class el22(xsc.Element): xmlname = "el2" xmlns = "ns2" el11.model = sims.Elements(el11, el21) e = el11() e.bytes(validate=True) e = el11("foo") with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.IllegalTextWarning) e = el11(php.php("gurk")) e.bytes(validate=True) e = el11(xsc.Comment("gurk")) e.bytes(validate=True) e = el11(el11()) e.bytes(validate=True) e = el11(el21()) e.bytes(validate=True) e = el11(el12()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning) e = el11(el22()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning)
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 test_mixeq(): assert xsc.Comment(1) != xsc.Text(1) assert xsc.DocType(1) != xsc.Text(1)
def test_commenteq(): assert xsc.Comment() == xsc.Comment() assert xsc.Comment(1) == xsc.Comment(1) assert xsc.Comment("1") == xsc.Comment(1) assert xsc.Comment("1") == xsc.Comment(1) assert xsc.Comment("") != xsc.Comment(1)
def test_empty4(): e = el1(xsc.Comment("gurk")) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.EmptyElementWithContentWarning)
def test_textcomment(): pool = xsc.Pool() assert pool.text("foo") == xsc.Text("foo") assert pool.comment("foo") == xsc.Comment("foo")
def test_comment_in_attr(): node = html.div(class_=xsc.Comment("gurk")) assert node.bytes() == b"""<div class=""></div>"""