Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. type = (item) ->
  2. Object::toString.call(item)[8...-1].toLowerCase()
  3.  
  4. toDash = (string) ->
  5. string.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()
  6.  
  7. split = (string) ->
  8. string = string.trim()
  9. pattern = /('[^']+'|"[^"]+"|[^,]+)/g
  10. matches = string.match pattern
  11. match.trim() for match in matches
  12.  
  13. module.exports.css = css = (stylesheet, object=false) ->
  14. output = {} # keys: selectors, values: property-value object
  15. for selector, kvs of stylesheet
  16. output[selector] = {}
  17. for k, v of kvs
  18. if type(v) isnt "object"
  19. output[selector][toDash(k)] = v
  20. else
  21. rule = {}
  22. rule[k] = v
  23. for selector_, kvs_ of css(rule, true)
  24. selectors = split(selector)
  25. selector_s = split(selector_)
  26. combined_selectors = []
  27. for k1 in selectors
  28. for k2 in selector_s
  29. if "&" in k2
  30. key = k2.replace "&", k1
  31. else
  32. key = "#{k1} #{k2}"
  33. combined_selectors.push key
  34. output[combined_selectors.join " , "] = kvs_
  35. if object
  36. output
  37. else
  38. lines = JSON.stringify(output, undefined, 2).split "\n"
  39. (line[2..] for line in lines[1...-1]).join("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement