为什么Twitter和Google API文档不会在网址中编码&符号?

时间:2021-10-14 17:33:26

I have read I should encode my ampersands as & in HTML.
However numerous code samples from respected companies somehow forget to do this.

我已经读过我应该将我的&符编码为&在HTML中。然而,来自受尊敬公司的大量代码样本却忘了这样做。

Just a few examples off the top of my head:

我只想了几个例子:

Google Web Fonts sample code:

Google Web Fonts示例代码:

<link href='http://fonts.googleapis.com/css?family=PT+Sans&subset=latin,cyrillic' rel='stylesheet' type='text/css'>

Google Maps documentation:

Google地图文档:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=ja">

Twitter Anywhere official tutorial:

Twitter Anywhere官方教程:

<script src="http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1" type="text/javascript"></script>

Is there any real benefit from not escaping ampersand in links?
Is this related to browser quirks? Is this just a mistake in documentation?

在链接中没有转义&符是否有任何实际好处?这与浏览器的怪癖有关吗?这只是文档中的错误吗?

Dear answerers, please make sure you're answering the right question.

亲爱的回答者,请确保您正在回答正确的问题。

I know I should escape ampersands per spec. I also know why the mechanism was invented in the first place. I'm not asking about this. My question is:

我知道我应该按规格逃避&符号。我也知道为什么这个机制首先被发明了。我不是在问这个问题。我的问题是:

Is there a reason API documentation by respectable companies often violates this rule?

有尊严的公司的API文档是否经常违反此规则?

2 个解决方案

#1


4  

Is there any real benefit from not escaping ampersand in links?

在链接中没有转义&符是否有任何实际好处?

It saves a few keystrokes.

它节省了一些按键。

Is this related to browser quirks?

这与浏览器的怪癖有关吗?

No

Is this just a mistake in documentation?

这只是文档中的错误吗?

Yes

Is there a reason API documentation by respectable companies often violates this rule?

有尊严的公司的API文档是否经常违反此规则?

Ignorance and/or laziness. Browsers perform error recovery so they either don't notice the errors or they don't care. The documentation probably isn't written by their best experts.

无知和/或懒惰。浏览器执行错误恢复,因此他们要么没有注意到错误,要么他们不关心。文档可能不是由他们最好的专家编写的。

#2


5  

Two different contexts here.

这里有两种不同的背景。

  1. Within the context of a javascript href, the & is just fine and should not be encoded.
  2. 在javascript href的上下文中,&很好,不应该编码。

  3. In an HTML link the & is forbidden and should be escaped.
  4. 在HTML链接中禁止使用&,应该对其进行转义。

In the HTML link context an HTML character entity will be decoded before the address is passed to the HTTP process; a URL-encoded character will not, as the server can read it directly.

在HTML链接上下文中,在将地址传递给HTTP进程之前,将解码HTML字符实体; URL编码的字符不会,因为服务器可以直接读取它。

#1


4  

Is there any real benefit from not escaping ampersand in links?

在链接中没有转义&符是否有任何实际好处?

It saves a few keystrokes.

它节省了一些按键。

Is this related to browser quirks?

这与浏览器的怪癖有关吗?

No

Is this just a mistake in documentation?

这只是文档中的错误吗?

Yes

Is there a reason API documentation by respectable companies often violates this rule?

有尊严的公司的API文档是否经常违反此规则?

Ignorance and/or laziness. Browsers perform error recovery so they either don't notice the errors or they don't care. The documentation probably isn't written by their best experts.

无知和/或懒惰。浏览器执行错误恢复,因此他们要么没有注意到错误,要么他们不关心。文档可能不是由他们最好的专家编写的。

#2


5  

Two different contexts here.

这里有两种不同的背景。

  1. Within the context of a javascript href, the & is just fine and should not be encoded.
  2. 在javascript href的上下文中,&很好,不应该编码。

  3. In an HTML link the & is forbidden and should be escaped.
  4. 在HTML链接中禁止使用&,应该对其进行转义。

In the HTML link context an HTML character entity will be decoded before the address is passed to the HTTP process; a URL-encoded character will not, as the server can read it directly.

在HTML链接上下文中,在将地址传递给HTTP进程之前,将解码HTML字符实体; URL编码的字符不会,因为服务器可以直接读取它。