Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CSS specificity #7

Closed
ju1ius opened this issue Apr 4, 2011 · 2 comments
Closed

Support CSS specificity #7

ju1ius opened this issue Apr 4, 2011 · 2 comments

Comments

@ju1ius
Copy link

ju1ius commented Apr 4, 2011

The support for specificity would allow CSSParser to merge RuleSets according to CSS cascading rules (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order).

But I think it would need a bit of redesign.
The statement
#myid .someclass, foo.bar { mydeclaration: myvalue}
should be parsed as two CSSSelector objects having the same rules but a different specificity.

I invite you to have a look to http://github.com/alexdunae/css_parser, and in particular to https://github.com/alexdunae/css_parser/blob/master/lib/css_parser.rb for the css specificity thing.

@ju1ius
Copy link
Author

ju1ius commented Apr 4, 2011

here is a working code for computing specificity. It supports everything but the negation pseudo-class:

// in class CSSParser
const
  NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
    (\.[\w]+)                    # classes
    |
    \[(\w+)                      # attributes
    |
    (\:(                         # pseudo classes
      link|visited|active
      |hover|focus
      |lang
      |target
      |enabled|disabled|checked|indeterminate
      |root
      |nth-child|nth-last-child|nth-of-type|nth-last-of-type
      |first-child|last-child|first-of-type|last-of-type
      |only-child|only-of-type
      |empty|contains
    ))
  /ix',
  ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
    ((^|[\s\+\>\~]+)[\w]+ # elements
    |                   
    \:{1,2}(                 # pseudo-elements
      after|before
      |first-letter|first-line
      |selection
    )
  )/ix';

public static function computeSpecificity($selector)
{
  $a = 0;
  $b = substr_count($selector, '#');
  $c = preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $selector, $matches);
  $d = preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $selector, $matches);
  return ($a*1000) + ($b*100) + ($c*10) + $d;
}

@sabberworm
Copy link
Contributor

I’m closing this as I dont’t think there will ever be a merge method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants