Save free form description along with spaces and line breaks - html

I'm sure there must be a lot of posts answering my query, but I am just not able to find the correct post.
In my application user is entering free from description in the text area. but when data is saved and displayed on the next page, entire text is wrapped in to single para. I want whole text to be saved as user enters it along with line spaces and newlines, tabs etc.
please redirect me to correct post. Do i have to save textarea as blob?
Thanks

Your problem is that the text is entered in plain text, however, in HTML, extra spaces are removed, and all sorts of space are just displayed as a single space character. You have to either display the text in a <pre></pre> tag (ugly way) or reformat it using regular expression or other string processing methods to make an actual HTML.

Related

Line ending charactor LFs are automatically changed to CRLFs in HTML textarea

I noticed that all LFs are automatically changed to CRLFs if I put them into a HTML textarea.
■ Questions:
where and what causes this behavior?
is this because of Windows Operation system, i.e. it will not happen if using a different Operating system such as MacOS? (I just experienced this on a windows machine, not yet tested on a Mac though...)
or is this something which depends on Browser? (I have seen this behavior on Chrome, IE, and Firefox. Not yet tested on Safari...)
or is this something only happens on my editor? (i.e I am using sakura editor)
If possible, how to preserve the LF so that it does not get changed into CRLF?
■ Steps to reproduce this:
find a textarea where you can input, for example the following w3school website.
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_textarea
prepare a text that at least 2 lines with some LFs using an editor which can detect the line ending charactors (so that you can make sure you have some LFs).
※ I am using Sakura editor as an example.
copy and paste the text prepared in step 2 to the textarea.
once text is copied into the textarea, this time, copy the entire content of the textarea.
paste the content of the textarea back to your editor.
the line ending characters all become CRLFs.
■ P.S.
Please see the screenshots for details
left side is original text with 3 LFs
right side is the content copied back from the textarea and all LFs becomes CRLFs)
「↓」indicated LF
「⏎」indicated CRLF
Thanks
I think I find myself the answer at least some helpful information, i will just leave a record in case there are people seeking for the answer for similar questions.
where and what causes this behavior?
For historical reasons, the element’s value is normalized in three different ways for three different purposes. The raw value is the value as it was originally set. It is not normalized. The API value is the value used in the value IDL attribute. It is normalized so that line breaks use U+000A LINE FEED (LF) characters. Finally, there is the value, as used in form submission and other processing models in this specification. It is normalized so that line breaks use U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pairs, and in addition, if necessary given the element’s wrap attribute, additional line breaks are inserted to wrap the text at the given width.
for more information please read:
https://www.w3.org/TR/html5/forms.html#the-textarea-element
If possible, how to preserve the LF so that it does not get changed into CRLF?
I guess there are a lot of ways. Using javascript to replace all /r/n to /n before submit a form will likely be a client side solution. or if it doesn't have the necessity to be handled on client side which is exactly my case, I do the replacement process on the server side to force convert all line ending characters to LF.

Display the string with new line with out using \n in jsp

I have a form with a text area as one of the input fields. I want to store the user entered data. In the process I need to store the \n (enter key). But here the problem is in the DataBase (MySql) data is stored, but \n is not stored; i.e. data store as:
.
When I display the same data in the browser it shows in single line.
I need to split the data with \n and it should look like:
When I display the same data in the browser it shows in single line.
Yes, it would - because a newline in HTML doesn't get rendered as a newline. If you look at the raw HTML, I suspect you'll still see the line breaks... it's just that's not how it's rendered.
In order to represent line breaks in HTML, you need to use <br /> between lines, or something similar - or display it with a <pre> tag.
Basically, you need to format your raw text as HTML, however you decide to do that. If you're just dumping the HTML straight into your page, you may well find you already have issues if the text contains HTML tags - they should be escaped.
If you are seeing \n being stored in database, then you can write logic to include
<br/> tag while printing database values on your jsp.

Can I add text to HTML that isn't copied when the user copies text on the page?

I'm writing a Javascript program that pretty prints some input as HTML. As part of that pretty printing, I add some text that isn't part of the original input (such as annotations, line numbers, etc.) But when the user copies from the page, it's a bunch of garbled text that isn't valid in the original format. Is there a way to prevent the text I added from getting copied?
You can "steal" a solution from pastebin. They have two versions of code: 1 with pretty printing, and another below the pretty printed text in a scrollable textbox, code without pretty printing and other edits.

How to preserve the layout of textarea in the database?

I have a text area in html and a form to store the information in the database. When I click on submit, it supposed to take this text and save it in the database. For example:
"This is a text and this is a list:
1. number 1
2. number 2"
However, when I load the information from the database it looks like:
"This is a text and this is a list: 1. number 1 2. number 2"
How do I keep the layout of the textarea not changed (keep the spaces, lists, etc) without the need for the user to enter any tags.
It's being stored just fine in the database. You're outputting what was entered as plain text as HTML, and HTML ignores line breaks. You need to convert your \n characters to <br /> tags. PHP has a nl2br() function for this.
You could have them enter the data in a WSYWIG and do the work on your side to make sure it's always formatted properly - client-side users still won't have to see any tags, especially if you limit their editing options...
tinyMCE, nicEdit are two good editors

Can I word wrap a URL inside a HTML hyperlink

Is there a way to separate the URL name in a hyperlink so it continues onto the next line? In the application I am using I need to use a long URL and the space is limited so it wraps around to the next line. However, a space is automatically added to it and the link is then broken. Is there a continuation character to tell the application to keep link together or a way to use a shorter variable name that contains the longer actual URL?
No, HTML treats any and all whitespace characters (including multiple series of them) as a single space. You cannot disable this.
Is it absolutely necessary to put the URL as the hyperlink text?
In fact, showing a shorter meaningful description about the URL would be better for the hyperlink text.
meaning descr
I don't the actual requirement, but thought to leave this comment for you.