diff --git a/src/markup.mli b/src/markup.mli index 150dab6..2cc67e4 100644 --- a/src/markup.mli +++ b/src/markup.mli @@ -408,9 +408,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) -> @@ -418,8 +416,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. *) @@ -707,7 +704,9 @@ val normalize_text : after parsing, or generating streams from scratch, and would like to clean up the [`Text] signals. *) -val pretty_print : ([> content_signal ] as 'a, 's) stream -> ('a, 's) stream +val pretty_print : + ([> content_signal | `Doctype of doctype ] as 'a, 's) stream -> + ('a, 's) stream (** Adjusts the whitespace in the [`Text] signals in the given stream so that the output appears nicely-indented when the stream is converted to bytes and written. diff --git a/src/utility.ml b/src/utility.ml index dfe3abe..81185ee 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] @@ -362,6 +367,8 @@ let pretty_print signals = [signal] (phrasing indentation phrasing_nesting_level) throw e k + (* | `Start_element _ | `End_element | `Doctype _ | `Xml _ | `PI _ + | `Comment _ -> *) | _ -> push signals signal; list 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;