做浏览器发送" \\ r \\ n"或QUOT; \\ n"或者它依赖于浏览器? [英] Do browsers send "\r\n" or "\n" or does it depend on the browser?

查看:225
本文介绍了做浏览器发送" \\ r \\ n"或QUOT; \\ n"或者它依赖于浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已困扰了我一万年......每当我创建一个文本区域,允许多行(如生物为用户配置文件)一个网站我总是最后写以下偏执code:

This question has bothered me for a million years... whenever I create a website with a textarea that allows multi-line (such as a "Bio" for a user's profile) I always end up writing the following paranoid code:

// C# code sample...
bio = bio.Replace("\r\n", "\n").Replace("\r", "\n");
bio = Regex.Replace(@"\n{2,}", "\n\n");

那么,是什么做的浏览器发送了一个< textarea的名字=生物>< / textarea的> 如果有多个行

推荐答案

MIME 规范指定头行必须以\\ r \\ n结束,但他们并不清楚(有人会说这是不明确的,如果他们都清楚)关于如何处理一个textarea的内容做。 (参见,例如,<一href=\"https://listserv.heanet.ie/cgi-bin/wa?A2=ind9501&L=html-wg&T=0&F=&S=&P=3550\">this从HTML工作组对这个问题线程。)

The HTTP and MIME specs specify that header lines must end with \r\n, but they aren't clear (some would argue that it isn't clear if they are clear) about what to do with the contents of a TEXTAREA. (See, for instance, this thread from an HTML working group about the issue.)

下面是一个从HTTP / 1.1规范有关消息头报价:

Here's a quote from the HTTP/1.1 spec about message headers:

有关消息的头字段行结束符是序列CRLF。但是,我们建议应用程序,例如解析头时,识别单个LF作为行终止,而忽略了领先的CR。

The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR.

我认为这是一般的好策略:严格对你产生什么样的,但在自由派你接受什么。你应该假设,您将收到各种行结​​束的。 (请注意,除了CRLF和LF的Mac OS-9单独使用CR及仍有几个那些周围的 UNI code标准(第5.8节)指定一个范围广泛的应认定为行结束字符序列;有它们的列表的这里。)

I think that is a good strategy in general: be strict about what you produce but liberal in what you accept. You should assume that you will receive all sorts of line terminators. (Note that in addition to CRLF and LF, Mac OS-9 used CR alone, and there are still a few of those around. The Unicode standard (section 5.8) specifies a wide range of character sequences that should be recognized as line terminators; there's a list of them here.)

这篇关于做浏览器发送&QUOT; \\ r \\ n&QUOT;或QUOT; \\ n&QUOT;或者它依赖于浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆