From a4f7728fadd85ba1da92e6c920256b2b4ba8cf0d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 7 Apr 2012 22:15:31 +1200 Subject: [PATCH] XML/HTML pretty view tweaks. --- libmproxy/console/contentview.py | 18 +++++++----------- test/test_console_contentview.py | 15 +++++++-------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/libmproxy/console/contentview.py b/libmproxy/console/contentview.py index 02394c6f5..97d217f28 100644 --- a/libmproxy/console/contentview.py +++ b/libmproxy/console/contentview.py @@ -7,7 +7,7 @@ import common from .. import utils, encoding, flow from ..contrib import jsbeautifier -VIEW_CUTOFF = 1024*200 +VIEW_CUTOFF = 1024*100 VIEW_AUTO = 0 VIEW_JSON = 1 @@ -126,7 +126,6 @@ def view_xml(hdrs, content): try: document = lxml.etree.fromstring(content, parser) except lxml.etree.XMLSyntaxError, v: - print v return None docinfo = document.getroottree().docinfo @@ -138,12 +137,16 @@ def view_xml(hdrs, content): lxml.etree.tostring(p) ) p = p.getprevious() + doctype=docinfo.doctype + if prev: + doctype += "\n".join(prev).strip() + doctype = doctype.strip() s = lxml.etree.tostring( document, pretty_print=True, xml_declaration=True, - doctype=docinfo.doctype + "\n".join(prev), + doctype=doctype or None, encoding = docinfo.encoding ) @@ -162,14 +165,7 @@ def view_html(hdrs, content): d = lxml.html.fromstring(content, parser=parser) docinfo = d.getroottree().docinfo s = lxml.etree.tostring(d, pretty_print=True, doctype=docinfo.doctype) - - txt = [] - for i in s[:VIEW_CUTOFF].strip().split("\n"): - txt.append( - urwid.Text(("text", i)), - ) - trailer(len(content), txt) - return "HTML", txt + return "HTML", _view_text(s[:VIEW_CUTOFF], len(s)) def view_json(hdrs, content): diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index cf2ab1e57..e0dbd577c 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -32,7 +32,7 @@ class uContentView(libpry.AutoTree): ), "foo" ) - assert f is cv.view_xmlish + assert f is cv.view_html f = cv.get_view_func( cv.VIEW_AUTO, @@ -50,7 +50,7 @@ class uContentView(libpry.AutoTree): ), "" ) - assert f is cv.view_xmlish + assert f is cv.view_xml def test_view_urlencoded(self): d = utils.urlencode([("one", "two"), ("three", "four")]) @@ -71,18 +71,17 @@ class uContentView(libpry.AutoTree): assert cv.view_json([], "[" + ",".join(["0"]*cv.VIEW_CUTOFF) + "]") def test_view_xml(self): - #assert cv.view_xml([], "") - #assert not cv.view_xml([], "") - + assert cv.view_xml([], "") + assert not cv.view_xml([], "") s = """ - """ - print cv.view_xml([], s) + assert cv.view_xml([], s) def test_view_raw(self): assert cv.view_raw([], "foo") @@ -170,7 +169,7 @@ Larry encoding.encode('gzip', "[1, 2, 3]") ) assert "decoded gzip" in r[0] - assert "XML" in r[0] + assert "Raw" in r[0] tests = [