Skip to content

Commit ef52831

Browse files
committed
Add XHTMLSerializer (same as HTMLSerializer, only w/different defaults)
--HG-- rename : python/src/serializer.py => python/src/serializer/htmlserializer.py extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40795
1 parent 160258e commit ef52831

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

src/serializer/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from htmlserializer import HTMLSerializer
2+
from xhtmlserializer import XHTMLSerializer

src/serializer/xhtmlserializer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from htmlserializer import HTMLSerializer
2+
3+
class XHTMLSerializer(HTMLSerializer):
4+
defaults = {
5+
'quote_attr_values': True,
6+
'minimize_boolean_attributes': False,
7+
'use_trailing_solidus': True,
8+
'escape_lt_in_attrs': True,
9+
'omit_optional_tags': False
10+
}
11+
12+
def __init__(self, **kwargs):
13+
options = self.defaults.copy()
14+
options.update(kwargs)
15+
HTMLSerializer.__init__(self, **options)

tests/test_serializer.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sys
1+
import os
22
import unittest
33
from support import simplejson, html5lib_test_files
44

@@ -14,9 +14,6 @@
1414
#from html5lib.treewalkers._base import TreeWalker
1515
#END RELEASE
1616

17-
#Run the serialize error checks
18-
checkSerializeErrors = False
19-
2017
class JsonWalker(TreeWalker):
2118
def __iter__(self):
2219
for token in self.tree:
@@ -39,47 +36,50 @@ def __iter__(self):
3936
raise ValueError("Unknown token type: " + type)
4037

4138
class TestCase(unittest.TestCase):
42-
def addTest(cls, name, expected, input, description, options):
43-
func = lambda self: self.mockTest(expected, input, options)
44-
func.__doc__ = "\t".join([description, str(input), str(options)])
39+
def addTest(cls, name, description, input, expected, xhtml, options):
40+
func = lambda self: self.mockTest(input, options, expected, xhtml)
41+
func.__doc__ = "\t".join([name, description, str(input), str(options)])
4542
setattr(cls, name, func)
4643
addTest = classmethod(addTest)
4744

48-
def mockTest(self, expected, input, options):
45+
def mockTest(self, input, options, expected, xhtml):
4946
result = self.serialize_html(input, options)
5047
if len(expected) == 1:
5148
self.assertEquals(expected[0], result)
5249
elif result not in expected:
5350
self.fail("Expected: %s, Received: %s" % (expected, result))
5451

52+
if not xhtml: return
53+
54+
result = self.serialize_xhtml(input, options)
55+
if len(xhtml) == 1:
56+
self.assertEquals(xhtml[0], result)
57+
elif result not in xhtml:
58+
self.fail("Expected: %s, Received: %s" % (xhtml, result))
59+
5560
def serialize_html(self, input, options):
5661
options = dict([(str(k),v) for k,v in options.iteritems()])
5762
return u''.join(serializer.HTMLSerializer(**options).
5863
serialize(JsonWalker(input),options.get("encoding",None)))
5964

60-
def test_serializer():
61-
for filename in html5lib_test_files('serializer', '*.test'):
62-
tests = simplejson.load(file(filename))
63-
for test in tests['tests']:
64-
yield test
65+
def serialize_xhtml(self, input, options):
66+
options = dict([(str(k),v) for k,v in options.iteritems()])
67+
return u''.join(serializer.XHTMLSerializer(**options).
68+
serialize(JsonWalker(input),options.get("encoding",None)))
6569

6670
def buildTestSuite():
67-
tests = 0
68-
for test in test_serializer():
69-
tests += 1
70-
testName = 'test%d' % tests
71-
TestCase.addTest(testName, test["expected"], test["input"], \
72-
test["description"], test.get("options", {}))
71+
for filename in html5lib_test_files('serializer', '*.test'):
72+
test_name = os.path.basename(filename).replace('.test','')
73+
tests = simplejson.load(file(filename))
74+
for index, test in enumerate(tests['tests']):
75+
xhtml = test.get("xhtml", test["expected"])
76+
if test_name == 'optionaltags': xhtml = None
77+
TestCase.addTest('test_%s_%d' % (test_name, index+1),
78+
test["description"], test["input"], test["expected"], xhtml,
79+
test.get("options", {}))
7380
return unittest.TestLoader().loadTestsFromTestCase(TestCase)
7481

7582
def main():
76-
# the following is temporary while the unit tests for parse errors are
77-
# still in flux
78-
if '-p' in sys.argv: # suppress check for serialize errors
79-
sys.argv.remove('-p')
80-
global checkSerializeErrors
81-
checkSerializeErrors = False
82-
8383
buildTestSuite()
8484
unittest.main()
8585

0 commit comments

Comments
 (0)