[Zope] newline_to_br without breaking source code

Thomas B. Passin tpassin@mitretek.org
Fri, 15 Feb 2002 10:30:14 -0500


Depending on what you want to accomplish by using the text value with
javascript, this can be very tricky.  For example, if you want to write a
string that you later eval(), you have to handle line breaks.  If you don't
care how the string would be formatted on rendering, that's one thing, but
preserving the line breaks may be something else.

One approach to using multiline text is this: you can write multi-line
string assignments by using a continuation character:

var str='This is a \
multi-line string using \
continuation characters.'

You can get this by inserting a backslash character just before each \n.  If
you want each line to remain on a separate line for visual formatting
purposes, you can insert newlines :

var str='This is a \n\
multi-line string using \n\
continuation characters.'

Now all three lines will display as three lines in an alert or a text box,
for example, whereas in the first example, you will see just a single line.

This can be useful when you are creating javascript source code but probably
will not help here.  It seems to me that you ought be be able to manipulate
the string using javascript in the browser, using regular expressions that
are available in version 4 and later browsers.  Thus

OnMouseOver="line_fixing_javascript_function(useUserTextAreaInput('<dtml-var
textAreaInputWithLinebreak>'))"

The function line_fixing_javascript_function() would do something useful in
formatting the string.  Now if you know are going to do that in the browser,
you can go farther. You can use a string replacement in dtml to get rid of
any questionable characters, knowing that you can change them back at the
browser.  For example, change all "<br>" strings to "@@@@" or some such.

Like I said, it can get tricky depending on what you want to accomplish.

Tom P

[<webmaster@winkelmaenner.de>]

The rendered HTML sourcecode from zope always breaks the line, when
somebody wrote text in a textarea field and used several lines manually.

I can remove these "\n newlines" with javascript in the moment when
somebody sends his form but then i have no possibility to have them
back.
For example in a tag like <dtml-var textAreaInputWithLinebreak
newline_to_br>

But i want to use it also in a <a href> tag like this:
OnMouseOver="useUserTextAreaInput('<dtml-var
textAreaInputWithLinebreak>')"
and in 'normal' <dtml> tag like this:
<dtml-var textAreaInputWithLinebreak newline_to_br>

In the jacascript version the sourcecode will be broken without <br>
In the dtml version the sourcecode will also be broken but also with
<br>

The javascript version returns in every case an error.