From 7777fb5b6f5daa658db80ef30f98e463ead96f65 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Wed, 9 Feb 2022 20:07:25 +0100 Subject: [PATCH 1/3] Failing test: Indent changes meaning --- test/test_html.ml | 10 ++++++++++ test/tyxml_test.ml | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/test_html.ml b/test/test_html.ml index 40de98eb8..47be4a973 100644 --- a/test/test_html.ml +++ b/test/test_html.ml @@ -82,11 +82,21 @@ let escaping = "html escaping", tyxml_tests Html.[ ] +let indent = "html indent", tyxml_tests ~indent:true Html.[ + "no break between text nodes", + p [ + span ~a:[ a_class [ "some padding ............................" ] ] [ + txt "some text here"; txt ". and here" + ] + ], + "

\n some text here\n . and here\n \n

" +] let tests = [ html_elements ; html_attributes ; escaping ; + indent ; ] let () = Alcotest.run "tyxml" tests diff --git a/test/tyxml_test.ml b/test/tyxml_test.ml index 346ea0c6a..71bc2147f 100644 --- a/test/tyxml_test.ml +++ b/test/tyxml_test.ml @@ -2,11 +2,11 @@ open Tyxml (* Basic alcotest machinery *) -let to_string = Format.asprintf "%a" (Html.pp_elt ()) +let to_string ?indent = Format.asprintf "%a" (Html.pp_elt ?indent ()) -let tyxml_tests l = +let tyxml_tests ?indent l = let f (name, (ty : Html_types.body_content Html.elt), s) = - name, `Quick, fun () -> Alcotest.(check string) name (to_string ty) s + name, `Quick, fun () -> Alcotest.(check string) name (to_string ?indent ty) s in List.map f l From c33d58c1ef0035eae2c496d2a01a8adfaeea17a9 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Wed, 9 Feb 2022 20:10:26 +0100 Subject: [PATCH 2/3] Avoid breaks between text nodes when indent Breaking between two text nodes might result in different HTML whether indentation is enabled or not. --- lib/xml_print.ml | 24 ++++++++++++++++++------ test/test_html.ml | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/xml_print.ml b/lib/xml_print.ml index 4ca20aca0..b35203992 100644 --- a/lib/xml_print.ml +++ b/lib/xml_print.ml @@ -267,6 +267,10 @@ struct close_box indent fmt end + let elt_needs_cut = function + | Comment _ | Node _ | Leaf _ -> true + | Entity _ | PCDATA _ | EncodedPCDATA _ | Empty -> false + let rec pp_tag encode indent fmt tag attrs children = match children with | [] -> pp_closedtag encode indent fmt tag attrs @@ -276,13 +280,13 @@ struct (open_box indent) (pp_tag_and_attribs encode indent) (tag, attrs) (cut indent) - (pp_elts encode indent) children + (pp_elts true encode indent) children (close_box indent) (cut indent) tag ; close_box indent fmt - and pp_elt encode indent fmt elt = match content elt with + and pp_elt encode indent fmt = function | Comment texte -> Format.fprintf fmt "" (escape_comment texte) @@ -303,10 +307,18 @@ struct | Empty -> () - and pp_elts encode indent = - Format.pp_print_list - ~pp_sep:(fun fmt () -> cut indent fmt) - (pp_elt encode indent) + and pp_elts prev_did_cut encode indent fmt = function + | elt :: tl -> + let elt = content elt in + let need_cut = elt_needs_cut elt in + if not prev_did_cut && need_cut then cut indent fmt; + pp_elt encode indent fmt elt; + if need_cut then cut indent fmt; + pp_elts need_cut encode indent fmt tl + | [] -> () + + let pp_elt encode indent fmt elt = + pp_elt encode indent fmt (content elt) let pp ?(encode=encode_unsafe_char) ?(indent=false) () = pp_elt encode indent diff --git a/test/test_html.ml b/test/test_html.ml index 47be4a973..ff6acc460 100644 --- a/test/test_html.ml +++ b/test/test_html.ml @@ -89,7 +89,7 @@ let indent = "html indent", tyxml_tests ~indent:true Html.[ txt "some text here"; txt ". and here" ] ], - "

\n some text here\n . and here\n \n

" + "

\n \n some text here. and here\n \n

" ] let tests = [ From b6150a3be46f7ad7a111f2147c32e754bda8f4fa Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 10 Feb 2022 18:27:11 +0100 Subject: [PATCH 3/3] Failing test: Indent still changes meaning --- test/test_html.ml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test_html.ml b/test/test_html.ml index ff6acc460..d711efada 100644 --- a/test/test_html.ml +++ b/test/test_html.ml @@ -89,7 +89,14 @@ let indent = "html indent", tyxml_tests ~indent:true Html.[ txt "some text here"; txt ". and here" ] ], - "

\n \n some text here. and here\n \n

" + "

\n \n some text here. and here\n \n

"; + + "no break between inline nodes (failing)", + p ~a:[ a_class [ "some padding ............................" ] ] [ + txt "some text here"; span [ txt ". and here" ] + ], + "

some text here\n . and here\n

" + ] let tests = [