From 42ea442577c1b7969af99c3013c8e9c04003ca5e Mon Sep 17 00:00:00 2001 From: Harry Wilkinson Date: Wed, 6 Jul 2022 10:13:19 +0100 Subject: [PATCH 1/2] feat(multiline): option to use HTML linebreaks for multiline text --- README.rst | 2 ++ json2html/jsonconv.py | 11 +++++++---- test/run_tests.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index aa7bb04..d5c4422 100644 --- a/README.rst +++ b/README.rst @@ -56,6 +56,8 @@ Argument Description `encode` turn on/off[default] encoding of result to escaped html, compatible with any browser --------------------- ---------------- `escape` turn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html) +--------------------- ---------------- +`multiline` turn on/off[default] using HTML line breaks in multiline text nodes ===================== ================ Installation diff --git a/json2html/jsonconv.py b/json2html/jsonconv.py index 9ef8c73..f8c8292 100644 --- a/json2html/jsonconv.py +++ b/json2html/jsonconv.py @@ -33,7 +33,7 @@ class Json2Html: - def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False, escape=True): + def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False, escape=True, multiline=False): """ Convert JSON to HTML Table format """ @@ -42,6 +42,7 @@ def convert(self, json="", table_attributes='border="1"', clubbing=True, encode= self.table_init_markup = "" % table_attributes self.clubbing = clubbing self.escape = escape + self.multiline = multiline json_input = None if not json: json_input = {} @@ -91,10 +92,12 @@ def convert_json_node(self, json_input): basic JSON types. """ if type(json_input) in text_types: + html_output = text(json_input) if self.escape: - return html_escape(text(json_input)) - else: - return text(json_input) + html_output = html_escape(html_output) + if self.multiline: + html_output = html_output.replace("\n", "
") + return html_output if hasattr(json_input, 'items'): return self.convert_object(json_input) if hasattr(json_input, '__iter__') and hasattr(json_input, '__getitem__'): diff --git a/test/run_tests.py b/test/run_tests.py index e12c2f1..135c2e3 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -197,6 +197,16 @@ def test_xss(self): u"" ) + def test_multiline(self): + self.assertEqual( + json2html.convert("a\nb"), + u"a\nb" + ) + self.assertEqual( + json2html.convert("a\nb", multiline=True), + u"a
b" + ) + def test_all(self): for test_definition in self.test_json: _json = test_definition['json'] From 4194eb9616d8160bb35e25b784f4f64a963e72eb Mon Sep 17 00:00:00 2001 From: Harry Wilkinson Date: Wed, 6 Jul 2022 10:13:52 +0100 Subject: [PATCH 2/2] test(): update Python versions used for CI --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 00cb1c7..abb11bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,9 @@ python: - "3.5" - "3.6" - "3.7" - - "3.8-dev" + - "3.8" + - "3.9" + - "3.10-dev" # command to run tests script: cd test/ && python run_tests.py