diff options
-rw-r--r-- | markdown/serializers.py | 7 | ||||
-rw-r--r-- | tests/test_apis.py | 32 |
2 files changed, 35 insertions, 4 deletions
diff --git a/markdown/serializers.py b/markdown/serializers.py index b19d61c..0c4ebcb 100644 --- a/markdown/serializers.py +++ b/markdown/serializers.py @@ -172,19 +172,18 @@ def _serialize_html(write, elem, qnames, namespaces, format): if k: k = ":" + k write(" xmlns%s=\"%s\"" % (k, _escape_attrib(v))) - if format == "xhtml" and tag in HTML_EMPTY: + if format == "xhtml" and tag.lower() in HTML_EMPTY: write(" />") else: write(">") - tag = tag.lower() if text: - if tag == "script" or tag == "style": + if tag.lower() in ["script", "style"]: write(text) else: write(_escape_cdata(text)) for e in elem: _serialize_html(write, e, qnames, None, format) - if tag not in HTML_EMPTY: + if tag.lower() not in HTML_EMPTY: write("</" + tag + ">") if elem.tail: write(_escape_cdata(elem.tail)) diff --git a/tests/test_apis.py b/tests/test_apis.py index dd232b3..4a7c7c7 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -326,6 +326,38 @@ class testETreeComments(unittest.TestCase): '<!--foo-->\n') +class testSerializers(unittest.TestCase): + """ Test the html and xhtml serializers. """ + + def testHtml(self): + """ Test HTML serialization. """ + el = markdown.util.etree.Element('div') + p = markdown.util.etree.SubElement(el, 'p') + p.text = 'foo' + hr = markdown.util.etree.SubElement(el, 'hr') + self.assertEqual(markdown.serializers.to_html_string(el), + '<div><p>foo</p><hr></div>') + + def testXhtml(self): + """" Test XHTML serialization. """ + el = markdown.util.etree.Element('div') + p = markdown.util.etree.SubElement(el, 'p') + p.text = 'foo' + hr = markdown.util.etree.SubElement(el, 'hr') + self.assertEqual(markdown.serializers.to_xhtml_string(el), + '<div><p>foo</p><hr /></div>') + + def testMixedCaseTags(self): + """" Test preservation of tag case. """ + el = markdown.util.etree.Element('MixedCase') + el.text = 'not valid ' + em = markdown.util.etree.SubElement(el, 'EMPHASIS') + em.text = 'html' + hr = markdown.util.etree.SubElement(el, 'HR') + self.assertEqual(markdown.serializers.to_xhtml_string(el), + '<MixedCase>not valid <EMPHASIS>html</EMPHASIS><HR /></MixedCase>') + + class testAtomicString(unittest.TestCase): """ Test that AtomicStrings are honored (not parsed). """ |