-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssc.coffee
55 lines (50 loc) · 1.49 KB
/
cssc.coffee
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
###
CSSC: CoffeeScript's StyleSheet.
@example
stylesheet = new CSSC
CSSC.add 'html',
backgroundColor: 'red'
###
class @CSSC
###
Static method for creating percentage strings.
@param {Number} val Value set in the string.
@return {String} Stringified value with percentage.
###
@pc: (val) -> "#{val}%"
###
Static method for creating pixel strings.
@param {Number} val Value set in the string.
@return {string} Stringified value with pixel.
###
@px: (val) -> "#{val}px"
###
Static method for creating em strings.
@param {Number} val Value set in the string.
@return {string} Stringified value with em.
###
@em: (val) -> "#{val}em"
###
C-tor creating a StyleSheet.
@return {CSSC} The GhostTag's StyleSheet.
###
constructor: ->
style = document.createElement 'style'
style.setAttribute 'media', 'screen'
# Hack for WebKit
style.appendChild document.createTextNode ''
document.head.appendChild style
@sheet = style.sheet
###
Add a CSSRule to the current StyleSheet.
@param {String} or {Array} tags A single tag / class or and Array of srings.
@param {Object} properties A dictionnay of CSS's properties.
@return {CSSC} The GhostTag's StyleSheet, allowing chaining.
###
add: (tags, properties) ->
tags = [tags] if typeof tags is 'string'
for tag in tags
idx = @sheet.insertRule "#{tag} {}", @sheet.cssRules.length
rule = @sheet.cssRules[idx]
rule.style[key] = val for key, val of properties
@