javascript - 根据特异性对一组 CSS 选择器进行排序

标签 javascript dom css-selectors css-specificity

如何根据 JS 函数中的 CSS 特异性对一组 CSS 选择器进行排序?

function SortByCssSpecificity(input_array_of_css_selectors) {
  ...
  return sorted_array_of_css_selectors;
}

最佳答案

来自 the Selectors level 3 spec :

A selector's specificity is calculated as follows:

  • count the number of ID selectors in the selector (= a)
  • count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
  • count the number of type selectors and pseudo-elements in the selector (= c)
  • ignore the universal selector

Selectors inside the negation pseudo-class [:not()] are counted like any other, but the negation itself does not count as a pseudo-class.

Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.

Examples:

*               /* a=0 b=0 c=0 -> specificity =   0 */
LI              /* a=0 b=0 c=1 -> specificity =   1 */
UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
#x34y           /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */

( Selectors level 4 在此答案之后发布,由于引入了一些当前超出此答案范围的新选择器,为特异性增加了另一层复杂性。)

这是一个让您入门的伪代码实现,它远非完美,但我希望它是一个合理的起点:

function SortByCssSpecificity(selectors, element) {
    simple_selectors = [][]
    for selector in selectors {
        // Optionally pass an element to only include selectors that match
        // The implementation of MatchSelector() is outside the scope
        // of this answer, but client-side JS can use Element#matches()
        // https://developer.mozilla.org/en-US/docs/Web/API/Element/matches
        if (element && !MatchSelector(selector, element)) {
            continue
        }

        simple_selectors[selector] = ParseSelector(selector)
        simple_selectors[selector] = simple_selectors[selector].filter(x | x != '*')

        // This assumes pseudo-elements are denoted with double colons per CSS3
        // A conforming implementation must interpret
        // :first-line, :first-letter, :before and :after as pseudo-elements
        a = simple_selectors[selector].filter(x | x ^= '#').length
        b = simple_selectors[selector].filter(x | x ^= '.' or x.match(/^:[^:]+/) or x.match(/^\[.+\]$/)).length
        c = simple_selectors[selector].length - (a + b)

        simple_selectors[selector][count] = parseInt('' + a + b + c)
    }

    return simple_selectors.sort(x, y | x[count] < y[count])
}

function ParseSelector(selector) {
    simple_selectors = []
    // Split by the group operator ','
    // Split each selector group by combinators ' ', '+', '~', '>'
    // :not() is a special case, do not include it as a pseudo-class

    // For the selector div > p:not(.foo) ~ span.bar,
    // sample output is ['div', 'p', '.foo', 'span', '.bar']
    return simple_selectors
}

关于javascript - 根据特异性对一组 CSS 选择器进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5158631/

相关文章:

javascript - 声明 jQuery 变量的官方正确方法是什么?

css - 选择所有 anchor 元素但不选择 :first-child anchor in a class group

css - 通用选择器 * 和伪元素

html - 在 HTML 中寻找显示包含特定属性的所有元素的引用

javascript - 使用原型(prototype)js选择元素

jquery - xml 标签内的 html 标签 - 如何在 javascript 中访问?

html - 此标记的偶数和奇数伪

javascript - Selectorator.js - 页面上所有隐藏元素的选择器

javascript - React 导入前的 '@' 是什么意思?

javascript - 停止在表单中发布隐藏的选择字段