简体   繁体   English

关于在 CSS url() 中使用数据 URI 的有效性的说明

[英]Clarification regarding validity of using data-URIs in CSS url()

I'm writing a pre-processing component (in PHP) which, in certain contexts, rewrites external image file requests in CSS such as:我正在编写一个预处理组件(在 PHP 中),它在某些情况下会重写 CSS 中的外部图像文件请求,例如:

background-image: url('/my-folder/my-image.png');

as CSS-inlined Data URIs , such as:作为 CSS 内联的Data URIs ,例如:

background-image: url('data:image/png;base64,[Base-64 Encoding Here]');

I've just read (with some surprise) over at MDN :我刚刚在MDN上阅读(有些惊讶):

In CSS Level 1 , the url() functional notation described only true URLs.CSS Level 1中, url()功能符号仅描述了真实的 URL。 In CSS Level 2 , the definition of url() was extended to describe any URI, such as a data-uri.CSS Level 2中, url()的定义被扩展为描述任何 URI,例如 data-uri。 CSS Values and Units Level 3 returned to the narrower, initial definition. CSS 值和单位 3 级返回到较窄的初始定义。 Now, url() denotes only true <url> s.现在, url()仅表示真正的<url> s。

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/url()来源: https://developer.mozilla.org/en-US/docs/Web/CSS/url()

Really?真的吗? This would seem to suggest that Data-URIs constitute an invalid value for url() in CSS Stylesheets (?)这似乎表明Data-URIs构成 CSS 样式表中url()的无效值(?)

But I can find nothing in:但我什么也找不到:

that backs this up.这支持了这一点。

I was under the impression that a Data-URI is an entirely valid value for url() in CSS Stylesheets.我的印象是Data-URI是 CSS 样式表中url()的完全有效值。

Can anyone clarify (ideally with an authoritative reference), please?任何人都可以澄清(最好是权威参考),好吗?


NB The tag below reads w3c-validation - I recognise it should probably read what-wg-validation .注意下面的标签读取w3c-validation - 我知道它可能应该读取what-wg-validation

data: URIs are actually valid URLs as per RFC 2397 , don't worry, they are still allowed. data:根据RFC 2397 ,URI 实际上是有效的 URL,别担心,它们仍然是允许的。

Not sure what this MDN article tried to imply when it says "such as a data-uri" , but I did edit it out to URN since it's actually what happened in CSS 2 :不确定这篇 MDN 文章在说“例如 data-uri”时试图暗示什么,但我确实将其编辑为URN ,因为它实际上 是 CSS 2 中发生的事情

The specs did indeed extend the <url> notation to all URIs, by allowing Uniform Resource Names to be part of it too... I can't tell why they did this change, but it seems very weird to say the least, as I can't see how an URN could be any useful in a stylesheet... According to the specs wording, it seems its authors didn't quite know yet what it would be.规范确实将 <url> 表示法扩展到所有 URI,通过允许统一资源名称也成为其中的一部分......我不知道他们为什么要这样做,但至少可以说似乎很奇怪,因为我看不出 URN 在样式表中有何用处……根据规范的措辞,它的作者似乎还不太清楚它会是什么。

URLs (Uniform Resource Locators, see [RFC1738] and [RFC1808]) provide the address of a resource on the Web. URL(统一资源定位器,参见 [RFC1738] 和 [RFC1808])提供 Web 上资源的地址。 An expected new way of identifying resources is called URN (Uniform Resource Name).一种预期的识别资源的新方法称为 URN(统一资源名称)。 Together they are called URIs (Uniform Resource Identifiers, see [URI]).它们一起被称为 URI(统一资源标识符,参见 [URI])。 This specification uses the term URI.本规范使用术语 URI。


Ps: Specs define it as " data: URLs" from the fetch API . Ps:规范将其定义为来自fetch API的“ data: URLs”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM