Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2eecb5

Browse files
committed
Add tests that we throw when we should and escape when we should when serializing at ISO-8859-1
1 parent114ab64 commitc2eecb5

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

‎html5lib/tests/test_serializer.py‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ def _convertAttrib(self, attribs):
7575
returnattrs
7676

7777

78+
defserialize_html(input,options):
79+
options=dict([(str(k),v)fork,vinoptions.iteritems()])
80+
returnserializer.HTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
81+
82+
defserialize_xhtml(input,options):
83+
options=dict([(str(k),v)fork,vinoptions.iteritems()])
84+
returnserializer.XHTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
85+
86+
7887
classTestCase(unittest.TestCase):
7988
defaddTest(cls,name,description,input,expected,xhtml,options):
8089
func=lambdaself:self.mockTest(input,options,expected,xhtml)
@@ -83,29 +92,63 @@ def addTest(cls, name, description, input, expected, xhtml, options):
8392
addTest=classmethod(addTest)
8493

8594
defmockTest(self,input,options,expected,xhtml):
86-
result=self.serialize_html(input,options)
95+
result=serialize_html(input,options)
8796
iflen(expected)==1:
8897
self.assertEquals(expected[0],result,"Expected:\n%s\nActual:\n%s\nOptions\nxhtml:False\n%s"%(expected[0],result,str(options)))
8998
elifresultnotinexpected:
9099
self.fail("Expected: %s, Received: %s"% (expected,result))
91100

92101
ifnotxhtml:return
93102

94-
result=self.serialize_xhtml(input,options)
103+
result=serialize_xhtml(input,options)
95104
iflen(xhtml)==1:
96105
self.assertEquals(xhtml[0],result,"Expected:\n%s\nActual:\n%s\nOptions\nxhtml:True\n%s"%(xhtml[0],result,str(options)))
97106
elifresultnotinxhtml:
98107
self.fail("Expected: %s, Received: %s"% (xhtml,result))
99108

100-
defserialize_html(self,input,options):
101-
options=dict([(str(k),v)fork,vinoptions.iteritems()])
102-
returnu''.join(serializer.HTMLSerializer(**options).
103-
serialize(JsonWalker(input),options.get("encoding",None)))
104109

105-
defserialize_xhtml(self,input,options):
106-
options=dict([(str(k),v)fork,vinoptions.iteritems()])
107-
returnu''.join(serializer.XHTMLSerializer(**options).
108-
serialize(JsonWalker(input),options.get("encoding",None)))
110+
classEncodingTestCase(unittest.TestCase):
111+
defthrowsWithLatin1(self,input):
112+
self.assertRaises(UnicodeEncodeError,serialize_html,input, {"encoding":"iso-8859-1"})
113+
114+
deftestDoctypeName(self):
115+
self.throwsWithLatin1([["Doctype",u"\u0101"]])
116+
117+
deftestDoctypePublicId(self):
118+
self.throwsWithLatin1([["Doctype",u"potato",u"\u0101"]])
119+
120+
deftestDoctypeSystemId(self):
121+
self.throwsWithLatin1([["Doctype",u"potato",u"potato",u"\u0101"]])
122+
123+
deftestCdataCharacters(self):
124+
self.assertEquals("<style>&amacr;",serialize_html([["StartTag","http://www.w3.org/1999/xhtml","style", {}],
125+
["Characters",u"\u0101"]],
126+
{"encoding":"iso-8859-1"}))
127+
128+
deftestCharacters(self):
129+
self.assertEquals("&amacr;",serialize_html([["Characters",u"\u0101"]],
130+
{"encoding":"iso-8859-1"}))
131+
132+
deftestStartTagName(self):
133+
self.throwsWithLatin1([["StartTag",u"http://www.w3.org/1999/xhtml",u"\u0101", []]])
134+
135+
deftestEmptyTagName(self):
136+
self.throwsWithLatin1([["EmptyTag",u"http://www.w3.org/1999/xhtml",u"\u0101", []]])
137+
138+
deftestAttributeName(self):
139+
self.throwsWithLatin1([["StartTag",u"http://www.w3.org/1999/xhtml",u"span", [{"namespace":None,"name":u"\u0101","value":u"potato"}]]])
140+
141+
deftestAttributeValue(self):
142+
self.assertEquals("<span potato=&amacr;>",serialize_html([["StartTag",u"http://www.w3.org/1999/xhtml",u"span",
143+
[{"namespace":None,"name":u"potato","value":u"\u0101"}]]],
144+
{"encoding":"iso-8859-1"}))
145+
146+
deftestEndTagName(self):
147+
self.throwsWithLatin1([["EndTag",u"http://www.w3.org/1999/xhtml",u"\u0101"]])
148+
149+
deftestComment(self):
150+
self.throwsWithLatin1([["Comment",u"\u0101"]])
151+
109152

110153
classLxmlTestCase(unittest.TestCase):
111154
defsetUp(self):
@@ -146,6 +189,7 @@ def buildBasicTestSuite():
146189

147190
defbuildTestSuite():
148191
allTests= [buildBasicTestSuite()]
192+
allTests.append(unittest.TestLoader().loadTestsFromTestCase(EncodingTestCase))
149193
if"lxml"inoptionals_loaded:
150194
allTests.append(unittest.TestLoader().loadTestsFromTestCase(LxmlTestCase))
151195

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp