Requires

Provides

License:
MIT
Authors:
Thomas Aylott
  1. 14
;(function(exports){

  1. 18
  2. 19
var UNDEF = {undefined:1} if (!exports.SheetParser) exports.SheetParser = {}

  1. 22
  2. 23
  3. 24
  4. 25
var combineRegExp = UNDEF[typeof require] ? exports.combineRegExp : require('./sg-regex-tools').combineRegExp var SheetParser = exports.SheetParser

  1. 32
  2. 33
  3. 34
  4. 35
var CSS = SheetParser.CSS = {version: '1.0.2 dev'} CSS.trim = trim function trim(str){

http://blog.stevenlevithan.com/archives/faster-trim-javascript

  1. 37
  2. 38
  3. 39
  4. 40
  5. 41
  6. 42
  7. 43
  8. 44
  9. 45
  10. 46
  11. 47
  12. 48
  13. 49
  14. 50
  15. 51
  16. 52
  17. 53
  18. 54
  19. 55
  20. 56
  21. 57
  22. 58
  23. 59
  24. 60
  25. 61
  26. 62
var str = (''+str).replace(/^\s\s*/, ''), ws = /\s/, i = str.length; while (ws.test(str.charAt(--i))); return str.slice(0, i + 1); } CSS.camelCase = function(string){ return ('' + string).replace(camelCaseSearch, camelCaseReplace) } var camelCaseSearch = /-\D/g function camelCaseReplace(match){ return match.charAt(1).toUpperCase() } CSS.parse = function(cssText){ var found , rule , rules = {length:0} , keyIndex = -1 , regex = this.parser , names = CSS.parser.names , i,r,l , ruleCount rules.cssText = cssText = trim(cssText)

strip comments

  1. 65
  2. 66
  3. 67
  4. 68
cssText = cssText.replace(CSS.comment, ''); regex.lastIndex = 0 while ((found = regex.exec(cssText))){

avoid an infinite loop on zero-length keys

  1. 70
if (regex.lastIndex == found.index) ++ regex.lastIndex

key:value

  1. 73
  2. 74
  3. 75
  4. 76
  5. 77
  6. 78
  7. 79
  8. 80
  9. 81
  10. 82
  11. 83
  12. 84
  13. 85
  14. 86
  15. 87
  16. 88
  17. 89
  18. 90
  19. 91
  20. 92
  21. 93
  22. 94
if (found[names._key]){ rules[rules.length ++] = found[names._key] rules[found[names._key]] = found[names._value] rules[CSS.camelCase(found[names._key])] = found[names._value] continue } rules[rules.length++] = rule = {} for (i = 0, l = names.length; i < l; ++i){ if (!(names[i-1] && found[i])) continue rule[names[i-1]] = trim(found[i]) } } var atKey, atRule, atList, atI for (i = 0, l = rules.length; i < l; ++i){ if (!rules[i]) continue if (rules[i]._style_cssText){ rules[i].style = CSS.parse(rules[i]._style_cssText) delete rules[i]._style_cssText }

atKey/atValue

  1. 97
  2. 98
  3. 99
  4. 100
  5. 101
  6. 102
  7. 103
  8. 104
  9. 105
  10. 106
  11. 107
  12. 108
  13. 109
  14. 110
  15. 111
  16. 112
  17. 113
  18. 114
  19. 115
  20. 116
  21. 117
  22. 118
  23. 119
  24. 120
  25. 121
  26. 122
  27. 123
  28. 124
  29. 125
  30. 126
  31. 127
  32. 128
  33. 129
  34. 130
  35. 131
  36. 132
  37. 133
  38. 134
  39. 135
  40. 136
  41. 137
  42. 138
  43. 139
  44. 140
  45. 141
  46. 142
  47. 143
  48. 144
  49. 145
  50. 146
  51. 147
  52. 148
  53. 149
  54. 150
  55. 151
  56. 152
  57. 153
  58. 154
  59. 155
  60. 156
  61. 157
  62. 158
  63. 159
  64. 160
  65. 161
  66. 162
  67. 163
  68. 164
  69. 165
  70. 166
  71. 167
  72. 168
  73. 169
  74. 170
  75. 171
  76. 172
  77. 173
  78. 174
  79. 175
  80. 176
  81. 177
  82. 178
  83. 179
  84. 180
  85. 181
  86. 182
  87. 183
  88. 184
  89. 185
  90. 186
  91. 187
  92. 188
  93. 189
  94. 190
  95. 191
  96. 192
  97. 193
  98. 194
  99. 195
if (atKey = rules[i]._atKey){ atKey = CSS.camelCase(atKey) atRule = {length:0} rules[i][atKey] = atRule atRule["_source"] = atRule[atKey + "Text"] = rules[i]._atValue atList = ('' + rules[i]._atValue).split(/,\s*/) for (atI = 0; atI < atList.length; ++atI){ atRule[atRule.length ++] = atList[atI] } rules[i].length = 1 rules[i][0] = atKey delete rules[i]._atKey delete rules[i]._atValue } if (rules[i].style) for (ruleCount = -1, r = -1, rule; rule = rules[i].style[++r];){ if (typeof rule == 'string') continue rules[i][r] = (rules[i].cssRules || (rules[i].cssRules = {}))[++ ruleCount] = rule rules[i].cssRules.length = ruleCount + 1 rules[i].rules = rules[i].cssRules } } return rules } var x = combineRegExp var OR = '|' ;(CSS.at = x(/\s*@([-a-zA-Z0-9]+)\s+(([\w-]+)?[^;{]*)/)) .names=[ '_atKey', '_atValue', 'name'] CSS.atRule = x([CSS.at, ';']) ;(CSS.keyValue_key = x(/([-a-zA-Z0-9]+)/)) .names=[ '_key'] ;(CSS.keyValue_value_end = x(/(?:;|(?=\})|$)/)) ;(CSS.notString = x(/[^"']+/)) ;(CSS.stringSingle = x(/"(?:[^"]|\\")*"/)) ;(CSS.stringDouble = x(/'(?:[^']|\\')*'/)) ;(CSS.string = x(['(?:',CSS.stringSingle ,OR, CSS.stringDouble,')'])) ;(CSS.propertyValue = x([/[^;}]+/, CSS.keyValue_value_end])) var rRound = "(?:[^()]|\\((?:[^()]|\\((?:[^()]|\\((?:[^()]|\\([^()]*\\))*\\))*\\))*\\))" ;(CSS.keyValue_value = x( [ x(['((?:' , CSS.stringSingle , OR , CSS.stringDouble , OR , "\\("+rRound+"*\\)" , OR , /[^;}()]/ // not a keyValue_value terminator , ')*)' ]) , CSS.keyValue_value_end ])).names = ['_value'] ;(CSS.keyValue = x([CSS.keyValue_key ,/\s*:\s*/, CSS.keyValue_value])) ;(CSS.comment = x(/\/\*\s*((?:[^*]|\*(?!\/))*)\s*\*\//)) .names=[ 'comment'] ;(CSS.selector = x(['(',/\s*(\d+%)\s*/,OR,'(?:',/[^{}'"()]|\([^)]*\)|\[[^\]]*\]/,')+',')'])) .names=[ 'selectorText','keyText'] var rCurly = "(?:[^{}]|\\{(?:[^{}]|\\{(?:[^{}]|\\{(?:[^{}]|\\{[^{}]*\\})*\\})*\\})*\\})" var rCurlyRound = "(?:[^{}()]+|\\{(?:[^{}()]+|\\{(?:[^{}()]+|\\{(?:[^{}()]+|\\{[^{}()]*\\})*\\})*\\})*\\})" ;(CSS.block = x("\\{\\s*((?:"+"\\("+rRound+"*\\)|"+rCurly+")*)\\s*\\}")) .names=[ '_style_cssText'] CSS.selectorBlock = x([CSS.selector, CSS.block]) CSS.atBlock = x([CSS.at, CSS.block]) CSS.parser = x ( [ x(CSS.comment) , OR , x(CSS.atBlock) , OR , x(CSS.atRule) , OR , x(CSS.selectorBlock) , OR , x(CSS.keyValue) ] , 'cssText' ); })(typeof exports != 'undefined' ? exports : this);