diff --git a/openerp/tests/test_html_sanitize.py b/openerp/tests/test_html_sanitize.py new file mode 100755 index 00000000000..906c78d1dcf --- /dev/null +++ b/openerp/tests/test_html_sanitize.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +import unittest +from openerp.tools.html_sanitize import html_sanitize + +test_case = """ +test1 +
+test2
+test3
+test4
+test5
+test6
  1. test9 +
  2. test10
+
+test11
+
+test12

+google +""" + +class TestSanitizer(unittest.TestCase): + + def test_simple(self): + x = "yop" + self.assertEqual(x, html_sanitize(x)) + + def test_test_case(self): + res = html_sanitize(test_case) + print res + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/openerp/tools/__init__.py b/openerp/tools/__init__.py index a6eeb14ba96..a0ca411a9df 100644 --- a/openerp/tools/__init__.py +++ b/openerp/tools/__init__.py @@ -33,6 +33,7 @@ from pdf_utils import * from yaml_import import * from sql import * from float_utils import * +from html_sanitize import * #.apidoc title: Tools diff --git a/openerp/tools/html_sanitize.py b/openerp/tools/html_sanitize.py index 4e6fb1540b1..d25d4466bb9 100644 --- a/openerp/tools/html_sanitize.py +++ b/openerp/tools/html_sanitize.py @@ -1,4 +1,39 @@ +from pyquery import PyQuery as pq def html_sanitize(x): - return x + root = pq("
") + root.html(x) + result = handle_element(root[0]) + new = pq(result) + return new.html() + +def handle_element(el): + if type(el) == str or type(el) == unicode: + return [el] + else: + new = pq("<%s />" % el.tag)[0] + for i in children(el): + append_to(handle_element(i), new) + return [new] + +def children(el): + res = [] + if el.text is not None: + res.append(el.text) + for i in el.getchildren(): + res.append(i) + if i.tail is not None: + res.append(i.tail) + return res + +def append_to(new_ones, el): + for i in new_ones: + if type(i) == str or type(i) == unicode: + children = el.getchildren() + if len(children) == 0: + el.text = i + else: + children[-1].tail = i + else: + el.append(i) \ No newline at end of file