Open
Description
@supports (text-shadow: 0 0 .3em gray) {
h1 {
color: transparent;
text-shadow: 0 0 .3em gray;
}
}
可编译为:
:root[data-cssrules-1] h1 {
...
}
cssSupports('text-shadow: 0 0 .3em gray')
let id = 0
function cssSupports(cssDecl) {
let e = document.createElement('div')
e.style.cssText = cssDecl
if (e.style.cssText === cssDecl) {
document.documentElement.dataset[`cssrules-${++id}`] = true
}
}
注意:
- 此代码为简单想法,未经验证。
:root
应该可获得优化从而解决属性选择器慢的问题。:root
排除了所有不支持:root
的老浏览器如IE6,我们一般可假设所有需要@supports
的特性均是IE6不支持的。- 编译后specificity增加了2个类/伪类,可简单的在所有选择器前添加
:root:root
以保持一致。 - cssText赋值实际会重新格式化,且需要考虑简写属性被展开为多个属性的情况。