Files
atom/vendor/specificity.js
Nathan Sobo 65605409b0 Favor key bindings with the most specific CSS selectors
Now if a keypress event bubbles to an element with bindings for
multiple matching selectors, the most specific selector is chosen. This
allows us to override a keybinding by using a more specific selector.
For example, say we have an editor instance inside of the file-finder
widget, so that the user can use all their normal bindings when they
type the name of the file. Except when they hit <esc> we want to close
the file finder, not do whatever they normally have it bound to. Now we
can just say:

```
.file-finder .editor {
  <esc>: close-file-finder
}
```

And we're assured that our binding will take precedence.
2012-01-11 13:16:10 -08:00

34 lines
1.1 KiB
JavaScript

// source: MooTools DOM branch -> https://raw.github.com/arian/DOM/matcher-specificity/Source/specificity.js
// changed to be compatible with our require system
var Slick = require('slick');
module.exports = function(selector){
var parsed = Slick.parse(selector);
var expressions = parsed.expressions;
var specificity = -1;
for (var j = 0; j < expressions.length; j++){
var b = 0, c = 0, d = 0, s = 0, nots = [];
for (var i = 0; i < expressions[j].length; i++){
var expression = expressions[j][i], pseudos = expression.pseudos;
if (expression.id) b++;
if (expression.attributes) c += expression.attributes.length;
if (expression.classes) c += expression.classes.length;
if (expression.tag && expression.tag != '*') d++;
if (pseudos){
d += pseudos.length;
for (var p = 0; p < pseudos.length; p++) if (pseudos[p].key == 'not'){
nots.push(pseudos[p].value);
d--;
}
}
}
s = b * 1e6 + c * 1e3 + d;
for (var ii = nots.length; ii--;) s += this.specificity(nots[ii]);
if (s > specificity) specificity = s;
}
return specificity;
};