Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation changes rendering of <pre> #287

Open
Julow opened this issue Jul 13, 2021 · 6 comments
Open

Indentation changes rendering of <pre> #287

Julow opened this issue Jul 13, 2021 · 6 comments

Comments

@Julow
Copy link
Contributor

Julow commented Jul 13, 2021

The following code with indentation enabled renders a line break before the closing </pre>:

    <div><p>some doc ..............................</p>
     <pre><code><span>some code of just the right length ....</span></code>
     </pre><p>some more doc</p>

This adds an extra blank line to the pre element, which renders differently whether indentation is enabled or not.

open Tyxml.Html

let content =
  html
    (head (title (txt "example")) [])
    (body
       [
         div
           [
             div
               [
                 div
                   [
                     p [ txt "some doc .............................." ];
                     pre
                       [
                         code
                           [
                             span
                               [ txt "some code of just the right length ...." ];
                           ];
                       ];
                     p [ txt "some more doc" ];
                   ];
               ];
           ];
       ])

let () =
  Format.printf "%a" (pp ~indent:true ()) content
@Julow
Copy link
Contributor Author

Julow commented Jul 13, 2021

I have an other example where the indent option adds a blank line before and after the code block but also adds visible indentation to every lines:

open Tyxml.Html

let content =
  html
    (head (title (txt "example")) [])
    (body
       [
         div
           [
             h2 [ txt "example" ];
             pre
               [
                 code
                   [
                     txt
                       (List.init 20 (fun _ -> "a lot of code")
                       |> String.concat "\n");
                   ];
               ];
           ];
       ])

let () =
  Format.printf "%a" (pp ~indent:true ()) content

@Drup
Copy link
Member

Drup commented Jul 13, 2021

Yes, it's sort-of-known that indent:true is not very good with pre elements (see #285). In the meantime, disable indentation should work well. It's not clear to me how to solve the problem reliably within tyxml ....

@Julow
Copy link
Contributor Author

Julow commented Jul 13, 2021

I've found a similar bug that doesn't involve the pre element: #288
It's less of an edge case but perhaps it can be fixed easily, which would help the pre issue ?

Would this work (to fix #288) ?

  • Never insert a line break before or after a text element
  • Never indent text (if a txt element contains a newline, it is not indented)

@smondet
Copy link

smondet commented Sep 5, 2021

@Drup I just had a similar problem with ~indent:true adding unwanted white space.

Would it work to just add some List.fold instead of pp_list here:

tyxml/lib/xml_print.ml

Lines 273 to 279 in 157c795

| _ ->
open_box indent fmt ;
Format.fprintf fmt "<%t%a>%t%a%t%t</%s>"
(open_box indent)
(pp_tag_and_attribs encode indent) (tag, attrs)
(cut indent)
(pp_elts encode indent) children
?

So that children of the node that are PCDATA would not have cuts before and after them?

@Drup
Copy link
Member

Drup commented Sep 8, 2021

That might work, could you try it ?

@favonia
Copy link

favonia commented Jun 8, 2022

It seems the only safe way to add whitespace is to check whether we are in a context where inter-element whitespace does not matter. (Which was called the "box model" in HTML4). Only in such a context we cut; otherwise we should proceed as if indent is false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants