2022-04-06 20:57:30 +03:00
#!/usr/bin/env python
2002-03-07 00:39:42 +03:00
import sys
import libxml2
# Memory debug specific
libxml2 . debugMemory ( 1 )
#
# Testing XML document serialization
#
doc = libxml2 . parseDoc ( """ <root><foo>hello</foo></root> """ )
str = doc . serialize ( )
if str != """ <?xml version= " 1.0 " ?>
< root > < foo > hello < / foo > < / root >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML document 1 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( " iso-8859-1 " )
if str != """ <?xml version= " 1.0 " encoding= " iso-8859-1 " ?>
< root > < foo > hello < / foo > < / root >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML document 2 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( format = 1 )
if str != """ <?xml version= " 1.0 " ?>
< root >
2002-08-12 16:13:01 +04:00
< foo > hello < / foo >
2002-03-07 00:39:42 +03:00
< / root >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML document 3 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( " iso-8859-1 " , 1 )
if str != """ <?xml version= " 1.0 " encoding= " iso-8859-1 " ?>
< root >
2002-08-12 16:13:01 +04:00
< foo > hello < / foo >
2002-03-07 00:39:42 +03:00
< / root >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML document 4 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
#
# Test serializing a subnode
#
root = doc . getRootElement ( )
str = root . serialize ( )
if str != """ <root><foo>hello</foo></root> """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML root 1 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( " iso-8859-1 " )
if str != """ <root><foo>hello</foo></root> """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML root 2 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( format = 1 )
if str != """ <root>
2002-08-12 16:13:01 +04:00
< foo > hello < / foo >
2002-03-07 00:39:42 +03:00
< / root > """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML root 3 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( " iso-8859-1 " , 1 )
if str != """ <root>
2002-08-12 16:13:01 +04:00
< foo > hello < / foo >
2002-03-07 00:39:42 +03:00
< / root > """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing XML root 4 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
doc . freeDoc ( )
#
# Testing HTML document serialization
#
doc = libxml2 . htmlParseDoc ( """ <html><head><title>Hello</title><body><p>hello</body></html> """ , None )
str = doc . serialize ( )
2003-12-04 15:31:49 +03:00
if str != """ <!DOCTYPE html PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " " http://www.w3.org/TR/REC-html40/loose.dtd " >
2002-03-07 00:39:42 +03:00
< html > < head > < title > Hello < / title > < / head > < body > < p > hello < / p > < / body > < / html >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML document 1 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( " ISO-8859-1 " )
2003-12-04 15:31:49 +03:00
if str != """ <!DOCTYPE html PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " " http://www.w3.org/TR/REC-html40/loose.dtd " >
2002-09-12 19:00:57 +04:00
< html > < head > < meta http - equiv = " Content-Type " content = " text/html; charset=ISO-8859-1 " > < title > Hello < / title > < / head > < body > < p > hello < / p > < / body > < / html >
2002-03-07 00:39:42 +03:00
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML document 2 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( format = 1 )
2003-12-04 15:31:49 +03:00
if str != """ <!DOCTYPE html PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " " http://www.w3.org/TR/REC-html40/loose.dtd " >
2002-03-07 00:39:42 +03:00
< html >
< head >
2002-09-12 19:00:57 +04:00
< meta http - equiv = " Content-Type " content = " text/html; charset=ISO-8859-1 " >
2002-03-07 00:39:42 +03:00
< title > Hello < / title >
< / head >
< body > < p > hello < / p > < / body >
< / html >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML document 3 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = doc . serialize ( " iso-8859-1 " , 1 )
2003-12-04 15:31:49 +03:00
if str != """ <!DOCTYPE html PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " " http://www.w3.org/TR/REC-html40/loose.dtd " >
2002-03-07 00:39:42 +03:00
< html >
< head >
2012-05-11 08:38:23 +04:00
< meta http - equiv = " Content-Type " content = " text/html; charset=ISO-8859-1 " >
2002-03-07 00:39:42 +03:00
< title > Hello < / title >
< / head >
< body > < p > hello < / p > < / body >
< / html >
""" :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML document 4 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
#
# Test serializing a subnode
#
doc . htmlSetMetaEncoding ( None )
root = doc . getRootElement ( )
str = root . serialize ( )
if str != """ <html><head><title>Hello</title></head><body><p>hello</p></body></html> """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML root 1 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( " ISO-8859-1 " )
2002-09-12 19:00:57 +04:00
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> """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML root 2 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( format = 1 )
if str != """ <html>
< head >
2002-09-12 19:00:57 +04:00
< meta http - equiv = " Content-Type " content = " text/html; charset=ISO-8859-1 " >
2002-03-07 00:39:42 +03:00
< title > Hello < / title >
< / head >
< body > < p > hello < / p > < / body >
< / html > """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML root 3 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
str = root . serialize ( " iso-8859-1 " , 1 )
if str != """ <html>
< head >
2012-05-11 08:38:23 +04:00
< meta http - equiv = " Content-Type " content = " text/html; charset=ISO-8859-1 " >
2002-03-07 00:39:42 +03:00
< title > Hello < / title >
< / head >
< body > < p > hello < / p > < / body >
< / html > """ :
2013-03-30 17:38:20 +04:00
print ( " error serializing HTML root 4 " )
2002-03-07 00:39:42 +03:00
sys . exit ( 1 )
doc . freeDoc ( )
# Memory debug specific
libxml2 . cleanupParser ( )
if libxml2 . debugMemory ( 1 ) == 0 :
2013-03-30 17:38:20 +04:00
print ( " OK " )
2002-03-07 00:39:42 +03:00
else :
2013-03-30 17:38:20 +04:00
print ( " Memory leak %d bytes " % ( libxml2 . debugMemory ( 1 ) ) )
2002-03-07 00:39:42 +03:00
libxml2 . dumpMemory ( )