Skip to content

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

Open
@hax

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions