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