forked from cujojs/wire
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sizzle.js
68 lines (56 loc) · 1.6 KB
/
sizzle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* wire/sizzle plugin
* Adds querySelectorAll functionality to wire using John Resig's Sizzle library.
* Sizzle must be wrapped in an AMD define(). Kris Zyp has a version of this at
* http://github.com/kriszyp/sizzle
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author John Hann (@unscriptable)
*/
define(['./lib/plugin-base/dom', 'sizzle'], function(createDomPlugin, sizzle) {
/**
* The usual addClass function
*
* @param node
* @param cls {String} space separated list of classes
*/
function addClass(node, cls) {
var className = node.className ? ' ' + node.className + ' ' : '';
cls = cls.split(/\s+/);
for (var i = 0, len = cls.length; i < len; i++) {
var c = ' ' + cls[i];
if(className.indexOf(c + ' ') < 0) {
className += c;
}
}
node.className = className.slice(1, className.length);
}
/**
* The usual removeClass function
*
* @param node
* @param cls {String} space separated list of classes
*/
function removeClass(node, cls) {
var className = ' ' + node.className + ' ';
cls = cls.split(/\s+/);
for (var i = 0, len = cls.length; i < len; i++) {
var c = ' ' + cls[i] + ' ';
className = className.replace(c, ' ');
}
node.className = className.replace(/(^\s+|\s+$)/g, '');
}
return createDomPlugin({
query: sizzle,
first: function (selector, root) {
return sizzle(selector, root)[0];
},
addClass: addClass,
removeClass: removeClass
});
});