def test_replace_with(): document = Document("<root><a>b</a>c<d>e</d></root>") root = document.root b_text = root[0][0] b_text.replace_with(tag("b")) expected_new = root[0][0] assert b_text.parent is None assert expected_new is not b_text assert isinstance(expected_new, TagNode) assert expected_new.local_name == "b" assert str(document) == "<root><a><b/></a>c<d>e</d></root>" c_text = root[1] c_text.replace_with(tag("c")) expected_new = root[1] assert c_text.parent is None assert expected_new is not c_text assert isinstance(expected_new, TagNode) assert expected_new.local_name == "c" assert str(document) == "<root><a><b/></a><c/><d>e</d></root>" with pytest.raises(InvalidOperation): root.replace_with(tag("new"))
def generate_skeleton(context): header = context.result = new_tag_node("teiHeader", namespace=TEI_NAMESPACE) context.biblFull_titleStmt = header.new_tag_node("titleStmt") context.msDesc = header.new_tag_node("msDesc") context.publicationStmt = header.new_tag_node("publicationStmt") context.titleStmt = header.new_tag_node("titleStmt") header.append_child( tag( "fileDesc", ( tag( "sourceDesc", ( # TODO? tei.bibl(), tag( "biblFull", (context.publicationStmt, context.biblFull_titleStmt), ), context.msDesc, ), ), context.titleStmt, ), ), tag("encodingDesc"), tag("profileDesc"), )
def generate_skeleton(context): context.html = new_tag_node( "html", namespace="http://www.w3.org/1999/xhtml", children=( tag("head", tag("title", "Testing XML Example")), tag("body", (tag("h1", "Persons"), tag("ul"))), ), )
def get_publication(node: TagNode, publicationStmt: TagNode): date = first(node.css_select("mods|dateIssued")).full_text name = first(node.css_select("mods|publisher")).full_text place = first( node.xpath('./mods:place/mods:placeTerm[@type="text"]')).full_text publicationStmt.append_child( tag("publisher", tag("name", name)), tag("pubPlace", place), tag("date", {"type": "creation"}, date), )
def get_title(node: TagNode, titleStmt: TagNode, biblFull_titleStmt: TagNode): non_sort = first(node.css_select("mods|nonSort")) main_title = (non_sort.full_text + " ") if non_sort is not None else "" main_title += first(node.css_select("mods|title")).full_text sub_title = first(node.css_select("mods|subTitle")).full_text for target in (titleStmt, biblFull_titleStmt): target.append_child( tag("title", {"type": "main"}, main_title), tag("title", {"type": "sub"}, sub_title), )
def test_root_takes_no_siblings(): root = Document("<root/>").root with pytest.raises(InvalidOperation): root.add_next(tag("x")) with pytest.raises(InvalidOperation): root.add_next("x") with pytest.raises(InvalidOperation): root.add_previous(tag("x")) with pytest.raises(InvalidOperation): root.add_previous("x")
def test_add_tag_before_tail(): document = Document("<root><a/>b</root>") root = document.root root[1].add_previous(tag("c")) assert str(document) == "<root><a/><c/>b</root>"
def test_add_tag_between_two_appended(): document = Document("<root>data</root>") root = document.root root.append_child("appended") document.root[0].add_next(tag("node")) assert str(document) == "<root>data<node/>appended</root>" document = Document("<root>data</root>") root = document.root root.append_child("appended") root[1].add_previous(tag("node")) assert str(document) == "<root>data<node/>appended</root>"
def test_add_next_tag_before_tail(): document = Document("<root><a/>b</root>") root = document.root root[0].add_next(tag("c")) assert str(document) == "<root><a/><c/>b</root>"
def test_tag_definition_copies_attributes(): root = Document('<root foo="bar"/>').root definition = tag("test", root.attributes) root.attributes["bar"] = "foo" root.append_child(definition) assert root.attributes == {"bar": "foo", "foo": "bar"} assert root.first_child.attributes == {"foo": "bar"}
def test_detach_node_with_tail_1(): document = Document("<root><a>c<c/></a>b<d/></root>") root = document.root root[1].add_next("c") root[0].detach() assert str(document) == "<root>bc<d/></root>" root.append_child(tag("e"), "f") e = root[3] e.detach() assert str(document) == "<root>bc<d/>f</root>" root.append_child(tag("g"), "h") root[-2].detach() assert str(document) == "<root>bc<d/>fh</root>"
def test_add_previous_node(): document = Document("<root><a/></root>") root = document.root a = root[0] a.add_previous(tag("z")) assert str(document) == "<root><z/><a/></root>" z = root[0] z.add_previous("x") assert str(document) == "<root>x<z/><a/></root>" z.add_previous("y") assert str(document) == "<root>xy<z/><a/></root>" a.add_previous(tag("boundary")) assert str(document) == "<root>xy<z/><boundary/><a/></root>"
def test_add_tag_between_text_nodes_at_tail_position(): document = Document("<root><a/>tail</root>") root = document.root root[1].add_next("the") root[2].add_next(" end") root[1].add_next(tag("node")) assert len(document.root) == 5 assert str(document) == "<root><a/>tail<node/>the end</root>"
def test_none_content_wrapping(): document = Document("<root><e1/></root>") with pytest.raises(IndexError): document.root[1] e1 = document.root[0] assert e1.next_node() is None e1.add_next(tag("e2")) assert isinstance(e1.next_node(), TagNode)
def test_add_text_between_two_appended(): document = Document("<root>data</root>") root = document.root root[0].add_next(" appended_1") root[1].add_next(" appended_2") root[2].add_next(" appended_3") root[1].add_next(tag("tag")) assert len(document.root) == 5 assert str( document) == "<root>data appended_1<tag/> appended_2 appended_3</root>"
def test_wrapper_cache(): gc.collect() assert len(_wrapper_cache.wrappers) == 0 root = Document("<root/>").root assert len(_wrapper_cache.wrappers) == 1 root.append_child(tag("node")) assert len(_wrapper_cache.wrappers) == 2 root.first_child.detach() gc.collect() assert len(_wrapper_cache.wrappers) == 1
def test_insert(): document = Document("<root><a>c</a></root>") root = document.root a = root[0] a.insert_child(0, tag("b")) assert str(document) == "<root><a><b/>c</a></root>" a.insert_child(0, "|aaa|") assert str(document) == "<root><a>|aaa|<b/>c</a></root>" a.insert_child(0, TextNode("|aa|"), clone=True) assert str(document) == "<root><a>|aa||aaa|<b/>c</a></root>" a.insert_child(1, "-") assert str(document) == "<root><a>|aa|-|aaa|<b/>c</a></root>"
def test_fetch_or_create_by_xpath(): root = Document("<root><intermediate/></root>").root assert str(root.fetch_or_create_by_xpath("./test")) == "<test/>" assert str(root) == "<root><intermediate/><test/></root>" assert str(root.fetch_or_create_by_xpath("./intermediate/target")) == "<target/>" assert str(root) == "<root><intermediate><target/></intermediate><test/></root>" assert str(root.fetch_or_create_by_xpath("./intermediate/target")) == "<target/>" assert str(root) == "<root><intermediate><target/></intermediate><test/></root>" root.append_child(tag("intermediate")) with pytest.raises(InvalidOperation): root.fetch_or_create_by_xpath("./intermediate") with pytest.raises(InvalidOperation): root.fetch_or_create_by_xpath("./intermediate/test")
def test_detach_text_sandwiched_node(): document = Document("<root>data</root>") root = document.root data = root[0] data.add_next(" tailing") data.add_next(" more more more") more = document.root[1].detach() assert str(more) == " more more more" assert str(document) == "<root>data tailing</root>" data.add_next(more) data.detach() assert str(document) == "<root> more more more tailing</root>" root.insert_child(0, tag("tag")) more.detach() assert str(document) == "<root><tag/> tailing</root>"
def test_make_node_with_children(): result = new_tag_node("infocard", children=[tag("label")]) assert str(result) == "<infocard><label/></infocard>" result = new_tag_node("infocard", children=[tag("label", {"updated": "never"})]) assert str(result) == '<infocard><label updated="never"/></infocard>' result = new_tag_node("infocard", children=[tag("label", "Monty Python")]) assert str(result) == "<infocard><label>Monty Python</label></infocard>" result = new_tag_node( "infocard", children=[tag("label", ("Monty Python", tag("fullstop")))]) assert str( result ) == "<infocard><label>Monty Python<fullstop/></label></infocard>" result = new_tag_node( "infocard", children=[tag("label", {"updated": "never"}, "Monty Python")]) assert str(result) == ( '<infocard><label updated="never">Monty Python</label></infocard>') result = new_tag_node( "infocard", children=[tag("label", {"updated": "never"}, ("Monty ", "Python"))]) assert str(result) == ( '<infocard><label updated="never">Monty Python</label></infocard>') register_namespace("foo", "https://foo.org") result = new_tag_node("root", namespace="https://foo.org", children=[tag("node")]) assert str( result ) == '<foo:root xmlns:foo="https://foo.org"><foo:node/></foo:root>'
def test_significant_whitespace_is_saved(result_file): document = Document("<text/>") root = document.root hi = tag("hi") root.append_child(hi) root[0].append_child("Hello") root.append_child(" ") root.append_child(hi) root[2].append_child("world!") document.save(result_file) with result_file.open("rt") as result: assert result.readlines() == [ "<?xml version='1.0' encoding='UTF-8'?>\n", "<text><hi>Hello</hi> <hi>world!</hi></text>", ] document.save(result_file, pretty=True) with result_file.open("rt") as result: assert result.readlines() == [ "<?xml version='1.0' encoding='UTF-8'?>\n", "<text><hi>Hello</hi> <hi>world!</hi></text>\n", ]
def test_prepend_child(): document = Document("<root><b/></root>") document.root.prepend_child(tag("a")) assert str(document) == "<root><a/><b/></root>"
def test_add_next(): document = Document("<root><e1/></root>") document.root[0].add_next(tag("e2"), tag("e3")) assert str(document) == "<root><e1/><e2/><e3/></root>"
def test_replace_with_tag_definition(): root = Document('<root xmlns="https://name.space"><node/></root>').root root.first_child.replace_with(tag("vertex", {"type": "xml"})) assert root.first_child.namespace == "https://name.space" assert str( root) == '<root xmlns="https://name.space"><vertex type="xml"/></root>'
def test_detach_node_without_a_document(): root = new_tag_node("root", children=[tag("node")]) root.first_child.detach() assert str(root) == "<root/>"
def test_add_tag_after_tail_appended_text(): document = Document("<root><a/>b</root>") root = document.root root.append_child("c") root.append_child(tag("d")) assert str(document) == "<root><a/>bc<d/></root>"
def transform(self): ul = self.root.css_select("body > ul").first for node in self.origin_document.xpath("//name[text()]"): ul.append_child(tag("li")) cloned_node = node.clone(deep=True) node.replace_with(tag("item", cloned_node))
def test_add_tag_after_tail(): document = Document("<root><node/>tail</root>") tail = document.root[1] tail.add_next(tag("end")) assert str(document) == "<root><node/>tail<end/></root>"