1#!/usr/bin/python -u
2import sys
3import libxml2
4
5# Memory debug specific
6libxml2.debugMemory(1)
7
8#
9# Testing XML document serialization
10#
11doc = libxml2.parseDoc("""<root><foo>hello</foo></root>""")
12str = doc.serialize()
13if str != """<?xml version="1.0"?>
14<root><foo>hello</foo></root>
15""":
16   print("error serializing XML document 1")
17   sys.exit(1)
18str = doc.serialize("iso-8859-1")
19if str != """<?xml version="1.0" encoding="iso-8859-1"?>
20<root><foo>hello</foo></root>
21""":
22   print("error serializing XML document 2")
23   sys.exit(1)
24str = doc.serialize(format=1)
25if str != """<?xml version="1.0"?>
26<root>
27  <foo>hello</foo>
28</root>
29""":
30   print("error serializing XML document 3")
31   sys.exit(1)
32str = doc.serialize("iso-8859-1", 1)
33if str != """<?xml version="1.0" encoding="iso-8859-1"?>
34<root>
35  <foo>hello</foo>
36</root>
37""":
38   print("error serializing XML document 4")
39   sys.exit(1)
40
41#
42# Test serializing a subnode
43#
44root = doc.getRootElement()
45str = root.serialize()
46if str != """<root><foo>hello</foo></root>""":
47   print("error serializing XML root 1")
48   sys.exit(1)
49str = root.serialize("iso-8859-1")
50if str != """<root><foo>hello</foo></root>""":
51   print("error serializing XML root 2")
52   sys.exit(1)
53str = root.serialize(format=1)
54if str != """<root>
55  <foo>hello</foo>
56</root>""":
57   print("error serializing XML root 3")
58   sys.exit(1)
59str = root.serialize("iso-8859-1", 1)
60if str != """<root>
61  <foo>hello</foo>
62</root>""":
63   print("error serializing XML root 4")
64   sys.exit(1)
65doc.freeDoc()
66
67#
68# Testing HTML document serialization
69#
70doc = libxml2.htmlParseDoc("""<html><head><title>Hello</title><body><p>hello</body></html>""", None)
71str = doc.serialize()
72if 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>
74""":
75   print("error serializing HTML document 1")
76   sys.exit(1)
77str = doc.serialize("ISO-8859-1")
78if 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>
80""":
81   print("error serializing HTML document 2")
82   sys.exit(1)
83str = doc.serialize(format=1)
84if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
85<html>
86<head>
87<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
88<title>Hello</title>
89</head>
90<body><p>hello</p></body>
91</html>
92""":
93   print("error serializing HTML document 3")
94   sys.exit(1)
95str = doc.serialize("iso-8859-1", 1)
96if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
97<html>
98<head>
99<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
100<title>Hello</title>
101</head>
102<body><p>hello</p></body>
103</html>
104""":
105   print("error serializing HTML document 4")
106   sys.exit(1)
107
108#
109# Test serializing a subnode
110#
111doc.htmlSetMetaEncoding(None)
112root = doc.getRootElement()
113str = root.serialize()
114if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
115   print("error serializing HTML root 1")
116   sys.exit(1)
117str = root.serialize("ISO-8859-1")
118if 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")
120   sys.exit(1)
121str = root.serialize(format=1)
122if str != """<html>
123<head>
124<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
125<title>Hello</title>
126</head>
127<body><p>hello</p></body>
128</html>""":
129   print("error serializing HTML root 3")
130   sys.exit(1)
131str = root.serialize("iso-8859-1", 1)
132if str != """<html>
133<head>
134<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
135<title>Hello</title>
136</head>
137<body><p>hello</p></body>
138</html>""":
139   print("error serializing HTML root 4")
140   sys.exit(1)
141
142doc.freeDoc()
143
144# Memory debug specific
145libxml2.cleanupParser()
146if libxml2.debugMemory(1) == 0:
147    print("OK")
148else:
149    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
150    libxml2.dumpMemory()
151