robb / Swim

A DSL for writing HTML in Swift
312 stars 9 forks source link

`textarea` whitespace #33

Closed khanlou closed 3 years ago

khanlou commented 3 years ago

By default, the textarea tag uses all the whitespace inside of it and uses that for the value of the textarea. Since Swim's default approach for rendering an html tree to a string is to pretty print it with tons of whitespace (which in general is great!), this adds a bunch of whitespace in any textarea.

I'm not sure what the best way to fix this is:

  1. Special case textarea's printing?
  2. Have a printing mode that prints with no extra whitespace?
  3. Add another parameter to textarea for its value?
robb commented 3 years ago

(This issue got lost in my inbox somehow, apologies)

Just to double check, the whitespace you are talking about is the one highlighted with ¬ and · here:

textarea {
    """
    Hello
    World
    """
}
<textarea>¬
····Hello
World¬
<textarea>

if that's the case, it should be enough to give the textarea definition a trim whitespace attribute similar to textLevelSemantics.

In the short term, you can wrap your contents with a trim operator:

textarea {
    %"""
    Hello
    World
    """%
}

which should give you

<textarea>Hello
World<textarea>
khanlou commented 3 years ago

Oh, this is perfect. This unlocked it for me:

textarea(cols: "30", name: "notes", rows: "4") {
    Node.trim
    item.notes
    Node.trim
}
robb commented 3 years ago

Should be better in main.

khanlou commented 3 years ago

Looks wonderful, thank you!