Skip to content

Commit a64592d

Browse files
author
Waylan Limberg
committed
Serializers now preserve case of tags.
It is up to the markdown code (and extension authors to make sure tags are of the correct case (there may be cases were an extension might need to mix cases - which should be preserved). Fixes #237. Thanks for the report @eichin.
1 parent a4ceb0b commit a64592d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

markdown/serializers.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,18 @@ def _serialize_html(write, elem, qnames, namespaces, format):
172172
if k:
173173
k = ":" + k
174174
write(" xmlns%s=\"%s\"" % (k, _escape_attrib(v)))
175-
if format == "xhtml" and tag in HTML_EMPTY:
175+
if format == "xhtml" and tag.lower() in HTML_EMPTY:
176176
write(" />")
177177
else:
178178
write(">")
179-
tag = tag.lower()
180179
if text:
181-
if tag == "script" or tag == "style":
180+
if tag.lower() in ["script", "style"]:
182181
write(text)
183182
else:
184183
write(_escape_cdata(text))
185184
for e in elem:
186185
_serialize_html(write, e, qnames, None, format)
187-
if tag not in HTML_EMPTY:
186+
if tag.lower() not in HTML_EMPTY:
188187
write("</" + tag + ">")
189188
if elem.tail:
190189
write(_escape_cdata(elem.tail))

tests/test_apis.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,38 @@ def testCommentPrettify(self):
326326
'<!--foo-->\n')
327327

328328

329+
class testSerializers(unittest.TestCase):
330+
""" Test the html and xhtml serializers. """
331+
332+
def testHtml(self):
333+
""" Test HTML serialization. """
334+
el = markdown.util.etree.Element('div')
335+
p = markdown.util.etree.SubElement(el, 'p')
336+
p.text = 'foo'
337+
hr = markdown.util.etree.SubElement(el, 'hr')
338+
self.assertEqual(markdown.serializers.to_html_string(el),
339+
'<div><p>foo</p><hr></div>')
340+
341+
def testXhtml(self):
342+
"""" Test XHTML serialization. """
343+
el = markdown.util.etree.Element('div')
344+
p = markdown.util.etree.SubElement(el, 'p')
345+
p.text = 'foo'
346+
hr = markdown.util.etree.SubElement(el, 'hr')
347+
self.assertEqual(markdown.serializers.to_xhtml_string(el),
348+
'<div><p>foo</p><hr /></div>')
349+
350+
def testMixedCaseTags(self):
351+
"""" Test preservation of tag case. """
352+
el = markdown.util.etree.Element('MixedCase')
353+
el.text = 'not valid '
354+
em = markdown.util.etree.SubElement(el, 'EMPHASIS')
355+
em.text = 'html'
356+
hr = markdown.util.etree.SubElement(el, 'HR')
357+
self.assertEqual(markdown.serializers.to_xhtml_string(el),
358+
'<MixedCase>not valid <EMPHASIS>html</EMPHASIS><HR /></MixedCase>')
359+
360+
329361
class testAtomicString(unittest.TestCase):
330362
""" Test that AtomicStrings are honored (not parsed). """
331363

0 commit comments

Comments
 (0)