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 40de98eb8..d711efada 100644 --- a/test/test_html.ml +++ b/test/test_html.ml @@ -82,11 +82,28 @@ 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 \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 = [ 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