hax / hax/hax.github.com

预处理器支持CSS的@support的可能性

Open
#24 19 comments 0 reactions 0 assignees View on GitHub
笔记
Dominant language
HTML
Stars
464
Forks
27
PR merge metrics
No merged PRs in 30d

Description

``` css
@supports (text-shadow: 0 0 .3em gray) {
h1 {
color: transparent;
text-shadow: 0 0 .3em gray;
}
}
```

可编译为:

``` css
:root[data-cssrules-1] h1 {
...
}
```

``` js
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赋值实际会重新格式化,且需要考虑简写属性被展开为多个属性的情况。

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.