What does Linq replace a new line with when updating? - linq-to-sql

I have always used Replace(myString, vbCrLf, "<br/>") to show line breaks when outting something to a page from the database (retaining line breaks). I am now using a DetailsView that has a textarea as one of the fields and uses a LinqDataSource as its datasource. I want to allow users to type line breaks in the textarea and display them on a page (replaced with <br/>'s to show breaks in the HTML). Linq seems to be replacing the line breaks with something else that is now causing the Replace statement to not find the breaks, therefor not inserting the html <br/>. When loading the value from the database to a textarea the line breaks are still there though. I have tried replacing the following with <br> but none of it works.
vbCrLf
vbNewLine
Environment.NewLine
...none of those work... what do I need to find/replace with <br> to show breaks?

TextArea uses different newline characters depending on the browser:
Internet Explorer: \r\n
FireFox: \n
It has also been suggested that \r is used in some cases, although, I haven't come across those cases.
Carriage return is encoded as %0D and Line feed as %0A. So if your text is HTML encoded (as it should be), then you need to replace %0D and/or %0A [depending on your environment] with your <br />
Here is a full discussion on the topic http://www.highdots.com/forums/html/standard-newline-character-264611.html.

Look at the string as a byte array, what values are the line breaks? There are only so many options here, 10, 13, both, none?

This works great for me:
string Output = HttpUtility.HtmlEncode(DirtyText); // HTML Encode it first for safety..
return Output.Replace("\n", "<br />"); // Now replace New Lines with HTML BRs
You end up with a safe encoded output, but also nicely formatted line spacing exactly as entered by the user into a standard textarea.

Related

web2py: textareas lose initial newline

I'm not sure if this is a web2py problem or a general html problem, but when I create a form in web2py that contains an editable string in a textarea, and the string contains an initial newline, like "\nsecond_line", the textarea does not display or save the newline - it is cut out. It works fine if there is a character before the newline: "firstline\nsecond_line" shows as on two lines. It is also only relevant for the first newline. If I have a string like "\n\nthird_line", then the textarea shows a single newline at the start.
This is with the most recent (non beta) version of web2py, on safari 9.1.3 and chrome 56.0.2924.87.
Ah. "By HTML 4.0 appendix B chapter 3.1, ā€œ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.ā€"

How to style contents of textarea field with CSS

Is it possible to style the contents of a textarea with CSS? I need to at the very least, insert more space after line breaks.
I want to basically have a new paragraph of text start within the textarea after every carriage return or line break.
You can't style the content of a textarea. All you can do is insert more whitespace manually.
One approach is to replace single carriage returns with double ones
This can be done with either Javascript or PHP...
JS: str.replace("\r\n", "<br />");
http://www.w3schools.com/jsref/jsref_replace.asp
PHP: str_replace("\r\n", "<br />", "whatyouaresearching", "number of replacements you want to make")
http://php.net/manual/en/function.str-replace.php
These are "String Replace" functions. So essentially what we are doing is searching a given string for a certain value and replacing it with another value.
Edit: Looks like I may have incorrectly assumed that the text area would interpret correctly. OP, have you tried yet? If that doesn't work, then try using the HTML Entity Code as the replace value:
http://www.w3schools.com/tags/ref_ascii.asp

How can i convert/replace every newline to '<br/>'?

set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
I tried this, content.gsub("\r\n","<br/>") but when I click the view/show button to see the contents of these line, I get the output/result=>
set tabstop=4<br/> set shiftwidth=4<br/> set nu<br/> set ai<br/> syntax on<br/> filetype plugin indent on
But I tried to get those lines as a seperate lines. But all become as a single line. Why?
How can I make all those lines with a html break (<br/>) ?
I tried this, that didn't work.
#addpost = Post.new params[:data]
#temptest = #addpost.content.html_safe
#addpost.content = #temptest
#logger.debug(#addpost)
#addpost.save
Also tried without saving into database. Tried only in view layer,<%= t.content.html_safe %> That didn't work too.
Got this from page source
vimrc file <br/>
2011-12-06<br/><br/>
set tabstop=4<br/><br/>set shiftwidth=4<br/><br/>set nu<br/><br/>set ai<br/><br/>syntax on<br/><br/>filetype plugin indent on<br/>
Edit
Delete
<br/><br/>
An alternative to convert every new lines to html tags <br> would be to use css to display the content as it was given :
.wrapped-text {
white-space: pre-wrap;
}
This will wrap the content on a new line, without altering its current form.
You need to use html_safe if you want to render embedded HTML:
<%= #the_string.html_safe %>
If it might be nil, raw(#the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.
With Ruby On Rails 4.0.1 comes the simple_format from TextHelper. It will handle more tags than the OP requested, but will filter malicious tags from the content (sanitize).
simple_format(t.content)
Reference : http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
http://www.ruby-doc.org/core-1.9.3/String.html
as it says there gsub expects regex and replacement
since "\n\r" is a string you can see in the docs:
if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ā€˜dā€™, instead of a digit.
so you are trying to match "\n\r", you probably want a character class containing \n or \r -[\n\r]
a = <<-EOL
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
EOL
print a.gsub(/[\n\r]/,"<br/>\n");
I'm not sure I exactly follow the question - are you seeing the output as e.g. preformatted text, or does the source HTML have those tags? If the source HTML has those tags, they should appear on new lines, even if they aren't on line breaks in the source, right?
Anyway, I'm guessing you're dealing with automatic string escaping. Check out this other Stack Overflow question
Also, this: Katz talking about this feature

New Line \n or line break in authnet AIM x_description field for generated email

Is this possible?
I've tried newline, carriage return, and also just straight putting the < br \> into this field, and the email is just showing it all on one line none the less.
Their documentation doesn't say anything about valid characters or HTML being valid or invalid for that field. But if you cannot get it to work then that pretty much shows that you cannot do it.

Add a linebreak in an HTML text area

How can i add a line break to the text area in a html page?
i use VB.net for server side coding.
If it's not vb you can use
(ascii codes for cr,lf)
Add a linefeed ("\n") to the output:
<textarea>Hello
Bybye</textarea>
Will have a newline in it.
You could use \r\n, or System.Environment.NewLine.
If you're inserting text from a database or such (which one usually do), convert all "<br />"'s to &vbCrLf. Works great for me :)
In a text area, as in the form input, then just a normal line break will work:
<textarea>
This is a text area
line breaks are automatic
</textarea>
If you're talking about normal text on the page, the <br /> (or just <br> if using plain 'ole HTML4) is a line break.
However, I'd say that you often don't actually want a line break. Usually, your text is seperated into paragraphs:
<p>
This is some text
</p>
<p>
This is some more
</p>
Which is much better because it gives a clue as to how your text is structured to machines that read it. Machines that read it include screen readers for the partially sighted or blind, seperating text into paragraphs gives it a chance of being presented correctly to these users.
I believe this will work:
TextArea.Text = "Line 1" & vbCrLf & "Line 2"
System.Environment.NewLine could be used in place of vbCrLf if you wanted to be a little less VB6 about it.
Escape sequences like "\n" work fine ! even with text area! I passed a java string with the "\n" to a html textarea and it worked fine as it works on consoles for java!
Here is my method made with pure PHP and CSS :
/** PHP code */
<?php
$string = "the string with linebreaks";
$string = strtr($string,array("."=>".\r\r",":"=>" : \r","-"=>"\r - "));
?>
And the CSS :
.your_textarea_class {
style='white-space:pre-wrap';
}
You can do the same with regex (I'm learning how to build regex with pregreplace using an associative array, seems to be better for adding the \n\r which makes the breaks display).