当浏览器不支持 CSS 伪类时会发生什么? [英] What happens when the browser doesn't support a CSS pseudo-class?

查看:27
本文介绍了当浏览器不支持 CSS 伪类时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果浏览器不支持 CSS 伪类(如 :dir)会怎样?

What happens if a browser doesn't support a CSS pseudo-class, like :dir?

例如:

html:dir(rtl) {
    color: red;
}

如果浏览器不理解 :dir 伪类,他们会忽略这条规则吗?我对一般情况比对这个特定的伪类更感兴趣.我的直觉告诉我是的,但我还没有找到证实我的直觉的文档.

Would browsers just ignore this rule if they don't understand the :dir pseudo-class? I'm more interested in the general case then in this particular pseudo-class. My intuition tells me yes, but I haven't found documentation confirming my intuition.

这个问题与这个问题不同:无效的 CSS 选择器导致规则被丢弃:原因是什么?.它更窄,我问的是浏览器在看到它无法识别的伪类时会做什么,而不是它对一般无效的 CSS 选择器做什么.据我所知,例如,一个无法识别的伪类仍然可能被认为是一个有效的选择器.

This question is different from this one: Invalid CSS selector causes rule to be dropped: What is the rationale? . It is more narrow, I'm asking what the browser does when it sees a pseudo-class that it doesn't recognise, not what it does for invalid CSS selectors in general. For all I know, an unrecognised pseudo-class may still be considered a valid selector, for instance.

推荐答案

浏览器目前不区分 unrecognizedunsupported 选择器,以及 invalid 选择器.如果浏览器识别出一个选择器,通常它会尽其所能实现它(并且任何不符合规范的行为都可以在其错误跟踪器上归类为错误),即使它没有实现所有其他功能在同一级别的选择器中(例如,当前使用 :dir() 的情况,历史上 Internet Explorer 7 和 8 具有级别 3 属性选择器,以及 Internet Explorer 6 具有通用选择器).如果它无法识别选择器,则遵循 CSS2.1 §4.1.7 信守承诺,放弃整个规则集,不问任何问题.注意它说

Browsers currently make no distinction between unrecognized or unsupported selectors, and invalid selectors. If a browser recognizes a selector, generally it'll have implemented it to the best of its ability (and any behavior that's not to spec can be classified as a bug on its bug tracker), even if it doesn't implement all other features in the same level of Selectors (as is currently the case with :dir() for example, and historically Internet Explorer 7 and 8 with level 3 attribute selectors, and Internet Explorer 6 with the universal selector). If it doesn't recognize the selector, it follows CSS2.1 §4.1.7 to the letter and drops the entire ruleset, no questions asked. Notice that it says

当用户代理无法解析选择器(即,它不是有效的 CSS 2.1)时,它也必须忽略选择器和以下声明块(如果有).

When a user agent cannot parse the selector (i.e., it is not valid CSS 2.1), it must ignore the selector and the following declaration block (if any) as well.

这意味着如果用户代理无法解析选择器,则它必须是无效的 CSS2.1(或在某些其他级别的选择器中无效);反之,如果它可以解析一个选择器,那么它一定是有效的.但这假设用户代理完全符合标准;我们都知道,在现实中,不同的实现对每个标准的符合程度不同,某些实现甚至有自己的供应商特定的选择器,它们不属于任何标准.因此,我将其视为当用户代理无法解析选择器时"没有括号,我想浏览器供应商也会这样做.

which implies that if a user agent cannot parse a selector, it must therefore be invalid CSS2.1 (or invalid in some other level of Selectors); and inversely that if it can parse a selector, it must therefore be valid. But this assumes the user agent conforms fully to the standard; we all know that in reality, different implementations have different levels of conformance to each standard, and certain implementations even have their own vendor-specific selectors which are not part of any standard. So I treat this as "When a user agent cannot parse the selector" without the parenthetical, and I imagine browser vendors do the same.

事实上,选择器本身并没有区分具有不对应于有效伪类的标识或函数的伪类标记,以及一系列甚至不能被解析为伪类的字符——它们都同样无效 — 参见 css3-selectors 的第 12 节选择器-4 的第 3.9 节.从本质上讲,这意味着当前的浏览器行为完全符合标准,而不仅仅是浏览器供应商同意的任意决定.

In fact, Selectors itself makes no distinction between a pseudo-class token with an ident or function that doesn't correspond to a valid pseudo-class, and a series of characters that cannot even be parsed as a pseudo-class — they're both equally invalid — see section 12 of css3-selectors and section 3.9 of selectors-4. Essentially, this means that the current browser behavior is in full compliance with the standard, and not just an arbitrary decision agreed upon by browser vendors.

我还没有听说过任何浏览器将伪类识别为对错误处理有效,并继续忽略该伪类或整个复杂选择器(将其他复杂选择器留在选择器中 -列表不受影响).WebKit 确实有一个非常糟糕的习惯,即接受带有无法识别的伪元素的 CSS 规则,允许 ::selection、::-moz-selection 之类的东西,其中 被证明是无用的,因为所有其他布局引擎更严格地遵循规范.但是,我相信 WebKit 不再这样做了,但是您知道 WebKit 是如何处理这些事情的.但是 AFAIK 它从来没有用伪类做到这一点.

I've not heard of any browser that recognizes a pseudo-class as valid for the purposes of error handling, and proceeds to ignore either just that pseudo-class or the entire complex selector (leaving other complex selectors in the selector-list unaffected). WebKit did use to have a really bad habit of accepting CSS rules with unrecognized pseudo-elements, allowing things like ::selection, ::-moz-selection, which proved useless anyway because every other layout engine followed the spec more strictly. I believe WebKit no longer does this, however, but you know how WebKit is with these things. But AFAIK it has never done this with pseudo-classes.

在标准方面,选择器 4 似乎准备通过引入静态和动态配置文件来改变这一点.我关于该主题的电子邮件在 CSSWG 中处理电讯;您可以在这里找到会议记录(搜索不在快速配置文件中的选择器的行为").但是,已解决的问题是,不在动态(以前是快速)配置文件中的选择器应该被视为无效,并像往常一样导致整个 CSS 规则被删除.请参阅 第 2.1 节:

On the standards side, selectors-4 appears set to change this with the introduction of the static and dynamic profiles. My email on the subject was addressed in a CSSWG telecon; you can find the minutes here (search for "Behavior of Selectors not in Fast Profile"). However, it was resolved that selectors not in the dynamic (previously fast) profile should be treated as invalid and cause the entire CSS rule to be dropped, as usual. See section 2.1:

符合选择器级别 4 的 CSS 实现必须使用动态配置文件进行 CSS 选择.使用动态配置文件的实现必须将未包含在配置文件中的选择器视为未知和无效.

CSS implementations conformant to Selectors Level 4 must use the dynamic profile for CSS selection. Implementations using the dynamic profile must treat selectors that are not included in the profile as unknown and invalid.

这篇关于当浏览器不支持 CSS 伪类时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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