From 038924c35d9c1d1095d3765566c0c68dd815d3a5 Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Thu, 22 Oct 2020 07:10:38 +0300 Subject: [PATCH] Markup.pretty_print: add newline after doctype Fixes #58. --- src/markup.mli | 7 ++----- src/utility.ml | 5 +++++ test/test_integration.ml | 16 ++++++++++++++-- test/test_utility.ml | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/markup.mli b/src/markup.mli index bb1466a..0f75879 100644 --- a/src/markup.mli +++ b/src/markup.mli @@ -399,9 +399,7 @@ foo example, if the first signal would be [`Doctype], the context is set to [`Document], but if the first signal would be [`Start_element "td"], the context is set to [`Fragment "tr"]. If the first signal would be - [`Start_element "g"], the context is set to [`Fragment "svg"]. - - *) + [`Start_element "g"], the context is set to [`Fragment "svg"]. *) val write_html : ?escape_attribute:(string -> string) -> @@ -409,8 +407,7 @@ val write_html : ([< signal ], 's) stream -> (char, 's) stream (** Similar to {!write_xml}, but emits HTML5 instead of XML. If [~escape_attribute] and/or [~escape_text] are provided, - they are used instead of default escaping functions. -*) + they are used instead of default escaping functions. *) diff --git a/src/utility.ml b/src/utility.ml index db3914d..2350ba6 100644 --- a/src/utility.ml +++ b/src/utility.ml @@ -338,6 +338,11 @@ let pretty_print signals = [`Text [indent indentation]] (phrasing indentation 0) throw e k + | `Doctype _ -> + list + [signal; `Text ["\n"]] + (flow indentation) throw e k + | _ -> list [signal] diff --git a/test/test_integration.ml b/test/test_integration.ml index 3eef24d..d9c12cb 100644 --- a/test/test_integration.ml +++ b/test/test_integration.ml @@ -92,7 +92,7 @@ let tests = [ assert_equal ~msg:"fi" (assemble ()) (Some ()); assert_equal ~msg:"fi" (assemble ()) None); - ("integration.doctype.roundtrip" >:: fun _ -> + ("integration.doctype.round-trip" >:: fun _ -> ({||}) |> string @@ -103,5 +103,17 @@ let tests = [ |> assert_equal ({||} ^ - {||})) + {||})); + + ("integration.doctype.pretty_print" >:: fun _ -> + "
" + |> string + |> parse_html + |> signals + |> pretty_print + |> write_html + |> to_string + |> assert_equal + ("\n\n \n" ^ + " \n
\n \n\n")); ] diff --git a/test/test_utility.ml b/test/test_utility.ml index 0c82efd..06198ae 100644 --- a/test/test_utility.ml +++ b/test/test_utility.ml @@ -216,6 +216,7 @@ let tests = [ |> to_list |> assert_equal [ doctype; + `Text ["\n"]; start_element "div"; `Text ["\n"; " "]; start_element "p"; @@ -224,6 +225,21 @@ let tests = [ `End_element; `Text ["\n"]]); + ("utility.pretty_print.doctype.existing-newline" >:: fun _ -> + [doctype; + `Text ["\n"]; + start_element "div"; + `End_element] + |> of_list + |> pretty_print + |> to_list + |> assert_equal [ + doctype; + `Text ["\n"]; + start_element "div"; + `End_element; + `Text ["\n"]]); + ("utility.html5" >:: fun _ -> [doctype; doctype;