From 1ac5c69420e94a6055a144fb03dc6c4a90453230 Mon Sep 17 00:00:00 2001 From: guido Date: Tue, 30 Jan 2007 16:42:19 +0100 Subject: [PATCH] [svn r37608] Added some code to make sure 'inline' elements aren't indented (gotta love 'ignorable' whitespace). --HG-- branch : trunk --- py/xmlobj/html.py | 7 +++++++ py/xmlobj/testing/test_html.py | 6 ++++++ py/xmlobj/testing/test_xml.py | 1 + py/xmlobj/visit.py | 6 +++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/py/xmlobj/html.py b/py/xmlobj/html.py index bf4410057..6e867b06a 100644 --- a/py/xmlobj/html.py +++ b/py/xmlobj/html.py @@ -10,6 +10,10 @@ class HtmlVisitor(SimpleUnicodeVisitor): single = dict([(x, 1) for x in ('br,img,area,param,col,hr,meta,link,base,' 'input,frame').split(',')]) + inline = dict([(x, 1) for x in + ('a abbr acronym b basefont bdo big br cite code dfn em font ' + 'i img input kbd label q s samp select small span strike ' + 'strong sub sup textarea tt u var'.split(' '))]) def repr_attribute(self, attrs, name): if name == 'class_': @@ -21,6 +25,9 @@ class HtmlVisitor(SimpleUnicodeVisitor): def _issingleton(self, tagname): return tagname in self.single + def _isinline(self, tagname): + return tagname in self.inline + class HtmlTag(Tag): def unicode(self, indent=2): l = [] diff --git a/py/xmlobj/testing/test_html.py b/py/xmlobj/testing/test_html.py index 8a6e8b2a6..2bf59c0f9 100644 --- a/py/xmlobj/testing/test_html.py +++ b/py/xmlobj/testing/test_html.py @@ -46,3 +46,9 @@ def test_singleton(): h = html.head(html.script(src="foo")) assert unicode(h) == '' + +def test_inline(): + h = html.div(html.span('foo'), html.span('bar')) + assert (h.unicode(indent=2) == + '
foobar
') + diff --git a/py/xmlobj/testing/test_xml.py b/py/xmlobj/testing/test_xml.py index a132eced4..031aa649a 100644 --- a/py/xmlobj/testing/test_xml.py +++ b/py/xmlobj/testing/test_xml.py @@ -55,3 +55,4 @@ def test_raw(): x = ns.some(py.xml.raw("

literal

")) u = unicode(x) assert u == "

literal

" + diff --git a/py/xmlobj/visit.py b/py/xmlobj/visit.py index f7a4164b0..2b48cae23 100644 --- a/py/xmlobj/visit.py +++ b/py/xmlobj/visit.py @@ -50,7 +50,7 @@ class SimpleUnicodeVisitor(object): tag.parent = None self.visited[id(tag)] = 1 tagname = getattr(tag, 'xmlname', tag.__class__.__name__) - if self.curindent: + if self.curindent and not self._isinline(tagname): self.write("\n" + u' ' * self.curindent) if tag: self.curindent += self.indent @@ -100,3 +100,7 @@ class SimpleUnicodeVisitor(object): """can (and will) be overridden in subclasses""" return self.shortempty + def _isinline(self, tagname): + """can (and will) be overridden in subclasses""" + return False +