問題描述
如果瀏覽器不支持 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.
推薦答案
瀏覽器目前不區分 unrecognized 或 unsupported 選擇器,以及 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 偽類時會發生什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!