Permalink
Browse files

better matchesCss() function

supports any "tagname#myid.class1.class2" combination
(as long as it's in that order)
  • Loading branch information...
1 parent 3c99175 commit 7e5c7e0c15d16b8784f6603302af2d15623660e0 @mislav committed Jun 20, 2009
Showing with 12 additions and 7 deletions.
  1. +12 −7 toolkit/toolkit.js
View
@@ -14,15 +14,20 @@ function up(node, selector, stopNode) {
}
function matchesCss(node, selector) {
- var firstChar = selector.charAt(0)
+ var tests = selector.match(/^(\w*)(#\w+)?((?:\.\w+)*)$/),
+ tag = tests[1],
+ id = tests[2],
+ classes = tests[3]
- if (firstChar == '.') {
- return hasClassName(node, selector.slice(1, selector.length))
- } else if (firstChar == '#') {
- return node.id == selector.slice(1, selector.length)
- } else {
- return node.nodeName.toLowerCase() == selector
+ if (classes) {
+ var classmatch = true
+ forEach(classes.split('.'), function(klass) {
+ if (klass && !hasClassName(node, klass)) classmatch = false
+ })
}
+
+ return (!tag || node.nodeName.toLowerCase() == tag.toLowerCase()) &&
+ (!id || node.id == id) && (!classes || classmatch)
}
function hasClassName(element, className) {

0 comments on commit 7e5c7e0

Please sign in to comment.