dev@javaserverfaces.java.net

h:inputTextArea swallows one leading newline

From: Daniel Lichtenberger <daniel.lichtenberger_at_gmx.net>
Date: Mon, 25 Mar 2013 15:49:30 +0100 (CET)

Hi,

It seems that when the value displayed by h:inputTextArea has one or more leading newlines, the browser swallows (exactly) one of these newlines.

For example: my property has a value of "\r\n\r\nThis is the third line", but in the browser only one empty line is rendered before the text (and when the form is submitted, the property's value becomes "\r\nThis is the third line"). I verified this behaviour on both Linux and Windows with recent versions of Firefox, Chrome and IE. Here's a similar discussion around the same issue in Ruby on Rails:

https://github.com/rails/rails/issues/393

The cause of this somewhat surprising behaviour seems to be this bit of the HTML4/SGML spec:

http://www.w3.org/TR/html401/appendix/notes.html#notes-line-breaks

> SGML (see [ISO8879], section 7.6.1) specifies that a line break immediately
> following a start tag must be ignored, as must a line break immediately before
> an end tag. This applies to all HTML elements without exception.

So basically you'd need to emit a newline before rendering the value of the textarea, since it will always be ignored. This hack works for me (it's for the Primefaces textarea renderer, but Mojarra's renderer has the same writeText call):

if(valueToRender != null) {
  writer.writeText("\n", "value");
  writer.writeText(valueToRender, "value");
}

What's the appropriate way of doing this?

Daniel