dev@javaserverfaces.java.net

Re: h:inputTextArea swallows one leading newline

From: Edward Burns <edward.burns_at_oracle.com>
Date: Thu, 11 Apr 2013 08:30:43 -0700

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

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

Hello Daniel,

I hope things are going well at fliXive. I was hoping to see you at
CONFESS Vienna last week. Did you go?

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

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

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

DL> 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.

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

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

DL> What's the appropriate way of doing this?

How about this. We could add some additional text the javax.faces.Text
javax.faces.Input renderer to say, if the value to be rendered starts
with whitespace, determine the appropriate number of spaces by counting
the number of preceding \n chars and issue that many
writer.writeText("\n", "value") calls. Then render the value with the
leading whitespace removed.

I'd have to get permission from the EG for this kind of change, though.

Ed

--