'))
def test_shortrepr2(self):
from . import parse_xmlstring
from ._compat import _b
root = parse_xmlstring(_COMPLEX_XHTML)
r = root.shortrepr()
self.assertEqual(r,
_b('\n'
' \n'
' \n'
' [...]\n\n'
' \n'
' [...]\n'
''))
def test_diffmeld1(self):
from . import parse_xmlstring
from . import _MELD_ID
root = parse_xmlstring(_COMPLEX_XHTML)
clone = root.clone()
div = self._makeOne('div', {_MELD_ID:'newdiv'})
clone.append(div)
tr = clone.findmeld('tr')
tr.deparent()
title = clone.findmeld('title')
title.deparent()
clone.append(title)
# unreduced
diff = root.diffmeld(clone)
changes = diff['unreduced']
addedtags = [ x.attrib[_MELD_ID] for x in changes['added'] ]
removedtags = [x.attrib[_MELD_ID] for x in changes['removed'] ]
movedtags = [ x.attrib[_MELD_ID] for x in changes['moved'] ]
addedtags.sort()
removedtags.sort()
movedtags.sort()
self.assertEqual(addedtags,['newdiv'])
self.assertEqual(removedtags,['td1', 'td2', 'tr'])
self.assertEqual(movedtags, ['title'])
# reduced
changes = diff['reduced']
addedtags = [ x.attrib[_MELD_ID] for x in changes['added'] ]
removedtags = [x.attrib[_MELD_ID] for x in changes['removed'] ]
movedtags = [ x.attrib[_MELD_ID] for x in changes['moved'] ]
addedtags.sort()
removedtags.sort()
movedtags.sort()
self.assertEqual(addedtags,['newdiv'])
self.assertEqual(removedtags,['tr'])
self.assertEqual(movedtags, ['title'])
def test_diffmeld2(self):
source = """
"""
target = """
"""
from . import parse_htmlstring
source_root = parse_htmlstring(source)
target_root = parse_htmlstring(target)
changes = source_root.diffmeld(target_root)
# unreduced
actual = [x.meldid() for x in changes['unreduced']['moved']]
expected = ['b']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['added']]
expected = []
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['removed']]
expected = []
self.assertEqual(expected, actual)
# reduced
actual = [x.meldid() for x in changes['reduced']['moved']]
expected = ['b']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['added']]
expected = []
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['removed']]
expected = []
self.assertEqual(expected, actual)
def test_diffmeld3(self):
source = """
"""
target = """
"""
from . import parse_htmlstring
source_root = parse_htmlstring(source)
target_root = parse_htmlstring(target)
changes = source_root.diffmeld(target_root)
# unreduced
actual = [x.meldid() for x in changes['unreduced']['moved']]
expected = ['b', 'c']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['added']]
expected = ['d', 'e']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['removed']]
expected = ['z', 'y']
self.assertEqual(expected, actual)
# reduced
actual = [x.meldid() for x in changes['reduced']['moved']]
expected = ['b']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['added']]
expected = ['d']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['removed']]
expected = ['z']
self.assertEqual(expected, actual)
def test_diffmeld4(self):
source = """
"""
target = """
"""
from . import parse_htmlstring
source_root = parse_htmlstring(source)
target_root = parse_htmlstring(target)
changes = source_root.diffmeld(target_root)
# unreduced
actual = [x.meldid() for x in changes['unreduced']['moved']]
expected = ['a', 'b']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['added']]
expected = ['m', 'n']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['unreduced']['removed']]
expected = ['c', 'd', 'z', 'y']
self.assertEqual(expected, actual)
# reduced
actual = [x.meldid() for x in changes['reduced']['moved']]
expected = ['a']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['added']]
expected = ['m']
self.assertEqual(expected, actual)
actual = [x.meldid() for x in changes['reduced']['removed']]
expected = ['c', 'z']
self.assertEqual(expected, actual)
def test_diffmeld5(self):
source = """
"""
target = """
"""
self.assertNormalizedHTMLEqual(actual, expected)
def test_write_complex_xhtml_as_xhtml(self):
# I'm not entirely sure if the cdata "script" quoting in this
# test is entirely correct for XHTML. Ryan Tomayko suggests
# that escaped entities are handled properly in script tags by
# XML-aware browsers at
# http://sourceforge.net/mailarchive/message.php?msg_id=10835582
# but I haven't tested it at all. ZPT does not seem to do
# this; it outputs unescaped data.
root = self._parse(_COMPLEX_XHTML)
actual = self._write_xhtml(root)
expected = """
This will be escaped in html output: &
"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_emptytags_html(self):
from ._compat import _u
root = self._parse(_EMPTYTAGS_HTML)
actual = self._write_html(root)
expected = """
"""
self.assertEqual(_u(actual), expected)
def test_write_booleanattrs_xhtml_as_html(self):
root = self._parse(_BOOLEANATTRS_XHTML)
actual = self._write_html(root)
expected = """
"""
self.assertNormalizedHTMLEqual(actual, expected)
def test_write_simple_xhtml_pipeline(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, pipeline=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_pipeline(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, pipeline=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_override_encoding(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, encoding="latin-1")
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_as_fragment(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, fragment=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_with_doctype(self):
root = self._parse(_SIMPLE_XML)
from . import doctype
actual = self._write_xml(root, doctype=doctype.xhtml)
expected = """
NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_doctype_nodeclaration(self):
root = self._parse(_SIMPLE_XML)
from . import doctype
actual = self._write_xml(root, declaration=False,
doctype=doctype.xhtml)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_fragment_kills_doctype_and_declaration(self):
root = self._parse(_SIMPLE_XML)
from . import doctype
actual = self._write_xml(root, declaration=True,
doctype=doctype.xhtml, fragment=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_override_encoding(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, encoding="latin-1", declaration=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_fragment(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_with_doctype(self):
root = self._parse(_SIMPLE_XHTML)
from . import doctype
actual = self._write_xhtml(root, doctype=doctype.xhtml)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_doctype_nodeclaration(self):
root = self._parse(_SIMPLE_XHTML)
from . import doctype
actual = self._write_xhtml(root, declaration=False,
doctype=doctype.xhtml)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_fragment_kills_doctype_and_declaration(self):
root = self._parse(_SIMPLE_XHTML)
from . import doctype
actual = self._write_xhtml(root, declaration=True,
doctype=doctype.xhtml, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_html_fragment(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_html(root, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_with_doctype_as_html(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_html(root)
expected = """
Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_html_new_doctype(self):
root = self._parse(_SIMPLE_XHTML)
from . import doctype
actual = self._write_html(root, doctype=doctype.html_strict)
expected = """
Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_entities_xhtml_no_doctype(self):
root = self._parse_html(_ENTITIES_XHTML)
# this will be considered an XHTML document by default; we needn't
# declare a doctype
actual = self._write_xhtml(root)
expected =r"""