5 # Memory debug specific
9 # Testing XML document serialization
11 doc = libxml2.parseDoc("""<root><foo>hello</foo></root>""")
13 if str != """<?xml version="1.0"?>
14 <root><foo>hello</foo></root>
16 print("error serializing XML document 1")
18 str = doc.serialize("iso-8859-1")
19 if str != """<?xml version="1.0" encoding="iso-8859-1"?>
20 <root><foo>hello</foo></root>
22 print("error serializing XML document 2")
24 str = doc.serialize(format=1)
25 if str != """<?xml version="1.0"?>
30 print("error serializing XML document 3")
32 str = doc.serialize("iso-8859-1", 1)
33 if str != """<?xml version="1.0" encoding="iso-8859-1"?>
38 print("error serializing XML document 4")
42 # Test serializing a subnode
44 root = doc.getRootElement()
45 str = root.serialize()
46 if str != """<root><foo>hello</foo></root>""":
47 print("error serializing XML root 1")
49 str = root.serialize("iso-8859-1")
50 if str != """<root><foo>hello</foo></root>""":
51 print("error serializing XML root 2")
53 str = root.serialize(format=1)
57 print("error serializing XML root 3")
59 str = root.serialize("iso-8859-1", 1)
63 print("error serializing XML root 4")
68 # Testing HTML document serialization
70 doc = libxml2.htmlParseDoc("""<html><head><title>Hello</title><body><p>hello</body></html>""", None)
72 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
73 <html><head><title>Hello</title></head><body><p>hello</p></body></html>
75 print("error serializing HTML document 1")
77 str = doc.serialize("ISO-8859-1")
78 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
79 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>
81 print("error serializing HTML document 2")
83 str = doc.serialize(format=1)
84 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
87 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
90 <body><p>hello</p></body>
93 print("error serializing HTML document 3")
95 str = doc.serialize("iso-8859-1", 1)
96 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
99 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
102 <body><p>hello</p></body>
105 print("error serializing HTML document 4")
109 # Test serializing a subnode
111 doc.htmlSetMetaEncoding(None)
112 root = doc.getRootElement()
113 str = root.serialize()
114 if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
115 print("error serializing HTML root 1")
117 str = root.serialize("ISO-8859-1")
118 if str != """<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>""":
119 print("error serializing HTML root 2")
121 str = root.serialize(format=1)
124 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
127 <body><p>hello</p></body>
129 print("error serializing HTML root 3")
131 str = root.serialize("iso-8859-1", 1)
134 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
137 <body><p>hello</p></body>
139 print("error serializing HTML root 4")
144 # Memory debug specific
145 libxml2.cleanupParser()
146 if libxml2.debugMemory(1) == 0:
149 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))