-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrespec.js
57 lines (34 loc) · 1.05 KB
/
respec.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
/*
# respec
## version 0.0.1
Respec allows CSS authors to assign a specificity level to the rules they write separate from the specificity of the selector
### Syntax
respec(selector, specificity, rule)
- `selector` is a CSS selector
- `specificity` is a number 0 or greater
- `rule` is one or more CSS declarations separated by semicolons
### Example
respec('div', 3, 'background: lime;')
### Info
- website: https://github.com/tomhodgins/respec
- author: Tommy Hodgins
- license: MIT
*/
function respec(selector, specificity, rule) {
var tag = document.querySelectorAll(selector)
var style = ''
var count = 0
for (var i=0; i<tag.length; i++) {
var attr = (selector+specificity).replace(/\W+/g, '')
var specificSelector = ''
tag[i].setAttribute('data-respec-' + attr, count)
for (var j=0; j<=specificity; j++) {
specificSelector += '[data-respec-' + attr + '="' + count + '"]'
}
style += '\n' + specificSelector + ' {\n'
+ ' ' + rule + '\n'
+ '}\n'
count++
}
return style
}