Requires

Provides

Sheet.js

Code to extract style rule definitions from the stylesheet

License:
Public domain (http://unlicense.org).
Authors:
Yaroslaff Fedin
  1. 28
  2. 29
  3. 30
  4. 31
  5. 32
  6. 33
  7. 34
  8. 35
  9. 36
  10. 37
  11. 38
  12. 39
  13. 40
  14. 41
  15. 42
  16. 43
  17. 44
  18. 45
  19. 46
  20. 47
  21. 48
  22. 49
  23. 50
  24. 51
  25. 52
  26. 53
  27. 54
  28. 55
  29. 56
  30. 57
  31. 58
  32. 59
  33. 60
  34. 61
  35. 62
  36. 63
  37. 64
  38. 65
  39. 66
  40. 67
  41. 68
  42. 69
  43. 70
  44. 71
  45. 72
  46. 73
  47. 74
  48. 75
  49. 76
  50. 77
  51. 78
  52. 79
  53. 80
  54. 81
  55. 82
  56. 83
  57. 84
  58. 85
  59. 86
  60. 87
  61. 88
  62. 89
  63. 90
  64. 91
  65. 92
  66. 93
  67. 94
  68. 95
  69. 96
  70. 97
  71. 98
  72. 99
  73. 100
  74. 101
  75. 102
  76. 103
  77. 104
  78. 105
  79. 106
  80. 107
  81. 108
  82. 109
  83. 110
  84. 111
  85. 112
!function() { LSD.Sheet = new Class({ Extends: LSD.Node, options: { compile: false, combine: true //combine rules }, initialize: function(element, callback) { this.parent.apply(this, arguments); this.rules = [] this.callback = callback; if (this.element) this.fetch(); else if (callback) callback(this); if (!LSD.Sheet.stylesheets) LSD.Sheet.stylesheets = []; LSD.Sheet.stylesheets.push(this); }, define: function(selectors, style) { LSD.Sheet.Rule.fromSelectors(selectors, style).each(this.addRule.bind(this)) }, addRule: function(rule) { this.rules.push(rule) }, fetch: function(href) { if (!href && this.element) href = this.element.get('href'); if (!href) return; new Request({ url: href, onSuccess: this.apply.bind(this) }).get(); }, apply: function(sheet) { if (typeof sheet == 'string') sheet = this.parse(sheet); if (this.options.compile) this.compile(sheet); for (var selector in sheet) this.define(selector, sheet[selector]); if (this.callback) this.callback(this) }, parse: function(text) { var sheet = new Sheet(text); var rules = sheet.cssRules; var CSS = SheetParser.Styles, Paint = LSD.Styles; var parsed = {}; for (var i = 0, rule; rule = rules[i++];) { var selector = LSD.Sheet.convertSelector(rule.selectorText) if (!selector.length || LSD.Sheet.isElementSelector(selector)) continue; if (!parsed[selector]) parsed[selector] = {}; var styles = parsed[selector]; for (var style = rule.style, j = 0, name; name = style[j++];) { var property = name.replace('-lsd-', '').camelCase(); var value = SheetParser.Value.translate(style[name]); var definition = Paint[property] || CSS[property]; if (!definition) continue; if (definition.type != 'simple') { var result = definition[value.push ? 'apply' : 'call'](this, value); if (result === false) value = false; else if (result !== true) { for (prop in result) styles[prop] = Value.compile(result[prop]); continue } } styles[property] = Value.compile(value); } }; return parsed; }, attach: function(node) { this.rules.each(function(rule) { rule.attach(node) }); LSD.start(); }, detach: function(node) { this.rules.each(function(rule) { rule.detach(node) }); },

Compile LSD stylesheet to CSS when possible to speed up setting of regular properties

Will create stylesheet node and apply the css unless lightly parameter was given.

Unused now, because we decompile css instead

  1. 121
  2. 122
  3. 123
  4. 124
  5. 125
  6. 126
  7. 127
  8. 128
  9. 129
  10. 130
  11. 131
  12. 132
  13. 133
  14. 134
  15. 135
  16. 136
  17. 137
  18. 138
  19. 139
  20. 140
  21. 141
  22. 142
  23. 143
  24. 144
  25. 145
  26. 146
  27. 147
  28. 148
  29. 149
  30. 150
  31. 151
  32. 152
  33. 153
  34. 154
  35. 155
  36. 156
  37. 157
  38. 158
  39. 159
  40. 160
  41. 161
  42. 162
  43. 163
  44. 164
  45. 165
  46. 166
  47. 167
  48. 168
  49. 169
  50. 170
  51. 171
  52. 172
  53. 173
  54. 174
  55. 175
  56. 176
  57. 177
  58. 178
  59. 179
  60. 180
  61. 181
  62. 182
  63. 183
  64. 184
  65. 185
  66. 186
  67. 187
  68. 188
  69. 189
  70. 190
  71. 191
  72. 192
compile: function(lightly) { var bits = []; this.rules.each(function(rule) { if (!rule.implied) return; bits.push(rule.getCSSSelector() + " {") for (var property in rule.implied) { var value = rule.implied[property]; if (typeof value == 'number') { if (property != 'zIndex') value += 'px'; } else if (value.map) { value = value.map(function(bit) { return bit + 'px'}).join(' '); } bits.push(property.hyphenate() + ': ' + value + ';') } bits.push("}") }) var text = bits.join("\n"); if (lightly) return text; if (window.createStyleSheet) { var style = window.createStyleSheet(""); style.cssText = text; } else { var style = new Element('style', {type: 'text/css', media: 'screen'}).adopt(document.newTextNode(text)).inject(document.body); } } }); Object.append(LSD.Sheet, { isElementSelector: function(selector) { return selector.match(/svg|v\\?:|:(?:before|after)|\.container/); }, convertSelector: function(selector) { return selector.replace(/\.id-/g , '#').replace(/\.is-/g, ':').replace(/\.lsd#/g, '#'). replace(/\.lsd\./g, '').replace(/html\sbody\s/g, ''); }, isElementStyle: function(cc) { return SheetParser.Styles[cc] && !LSD.Styles[cc] && (cc != 'height' && cc != 'width') }, isRawValue: function(value) { return (value.indexOf('hsb') > -1) || (value.indexOf('ART') > -1) || (value.indexOf('LSD') > -1) || (value.charAt(0) == '"') || (value == 'false') || (value == parseInt(value)) || (value == parseFloat(value)) } }); LSD.Sheet.Rule = function(selector, style) { this.selector = selector; this.index = LSD.Sheet.Rule.index ++; this.expressions = selector.expressions[0]; this.specificity = this.getSpecificity(); for (property in style) { var cc = property.camelCase(); var type = (LSD.Sheet.Rule.separate && LSD.Sheet.isElementStyle(cc)) ? 'implied' : 'style'; if (!this[type]) this[type] = {}; this[type][cc] = style[property]; } } LSD.Sheet.Rule.index = 0; LSD.Sheet.Rule.separate = true; Object.append(LSD.Sheet.Rule.prototype, { attach: function(node) { if (!this.watcher) this.watcher = this.watch.bind(this); node.watch(this.selector, this.watcher) }, detach: function(node) { node.unwatch(this.selector, this.watcher); }, watch: function(node, state) {

console.log(node, state, this.selector.raw, this.style)

  1. 194
  2. 195
  3. 196
  4. 197
  5. 198
  6. 199
  7. 200
  8. 201
  9. 202
  10. 203
  11. 204
  12. 205
  13. 206
  14. 207
  15. 208
  16. 209
  17. 210
  18. 211
  19. 212
  20. 213
  21. 214
  22. 215
  23. 216
  24. 217
  25. 218
  26. 219
  27. 220
  28. 221
  29. 222
  30. 223
  31. 224
  32. 225
  33. 226
  34. 227
  35. 228
  36. 229
  37. 230
  38. 231
  39. 232
  40. 233
  41. 234
  42. 235
  43. 236
  44. 237
  45. 238
  46. 239
  47. 240
  48. 241
  49. 242
  50. 243
  51. 244
  52. 245
  53. 246
  54. 247
  55. 248
  56. 249
  57. 250
  58. 251
  59. 252
  60. 253
  61. 254
  62. 255
  63. 256
  64. 257
  65. 258
  66. 259
  67. 260
  68. 261
  69. 262
  70. 263
  71. 264
  72. 265
  73. 266
  74. 267
  75. 268
  76. 269
  77. 270
  78. 271
  79. 272
  80. 273
  81. 274
  82. 275
  83. 276
  84. 277
  85. 278
  86. 279
  87. 280
  88. 281
  89. 282
  90. 283
  91. 284
  92. 285
  93. 286
  94. 287
  95. 288
  96. 289
  97. 290
  98. 291
  99. 292
  100. 293
  101. 294
  102. 295
  103. 296
  104. 297
  105. 298
  106. 299
  107. 300
  108. 301
  109. 302
  110. 303
  111. 304
  112. 305
  113. 306
  114. 307
  115. 308
  116. 309
  117. 310
  118. 311
  119. 312
  120. 313
  121. 314
  122. 315
  123. 316
  124. 317
  125. 318
  126. 319
  127. 320
  128. 321
  129. 322
  130. 323
  131. 324
  132. 325
node[state ? 'addRule' : 'removeRule'](this) }, getCSSSelector: function() { return this.expressions.map(function(parsed){ var classes = ['', 'lsd']; if (parsed.tag) classes.push(parsed.tag); if (parsed.id) classes.push('id-' + parsed.id); if (parsed.pseudos) { parsed.pseudos.each(function(pseudo) { classes.push(pseudo.key); }); }; return classes.join('.'); }).join(' '); }, getSpecificity: function(selector) { specificity = 0; this.expressions.each(function(chunk){ if (chunk.tag && chunk.tag != '*') specificity++; if (chunk.id) specificity += 100; for (var i in chunk.attributes) specificity++; specificity += (chunk.pseudos || []).length; specificity += (chunk.classes || []).length * 10; }); return specificity; } }); var Value = LSD.Sheet.Value = { px: function(value) { return value; }, deg: function(value) { return value; }, em: function(value) { return function() { return value * (this.baseline || 16) } }, "%": function(value) { return function(property) { var resolved = Value['%'].resolve(property); if (resolved.call) resolved = resolved.call(this); return resolved / 100 * value; } }, url: function(value) { return value }, src: function(value) { return value }, rgb: function() { return window.rgb.apply(window, arguments) }, rgba: function(value) { return window.rgb.apply(window, arguments) }, hsb: function() { return window.hsb.apply(window, arguments) }, hex: function(value) { return new Color(value) }, calc: function(value) { var bits = value.map(function(bit, i) { if (bit.call) { return "value[" + i + "].call(this, property)" } else { return bit; } }) eval("var compiled = function(property) { return " + bits.join(" ") + "}"); return compiled }, min: function() { return Math.min.apply(Math, arguments) }, max: function() { return Math.max.apply(Math, arguments) } }; var resolved = {}; var dimensions = { height: /[hH]eight|[tT]op|[bB]ottom|[a-z]Y/, width: /[wW]idth|[lL]eft|[rR]ight|[a-z]X/ } Value['%'].resolve = function(property) { var result = resolved[property]; if (result != null) return result; for (var side in dimensions) if (property.match(dimensions[side])) { result = function() { if (this.parentNode) return this.parentNode.getStyle(side); return 0;} break; } return (resolved[property] = (result || 1)); }; Value.compiled = {}; Value.compile = function(value, context) { if (!value || value.call || typeof value == 'number') return value; if (!context) context = Value; if (value.push) { for (var i = 0, j = value.length, result = [], bit; i < j; bit = value[i++]) result[i] = Value.compile(value[i], context); return result; } if (value.unit) return Object.append(context[value.unit](value.number), value); if (value.charAt) { if (context.hex && value.charAt(0) == "#" && value.match(/#[a-z0-9]{3,6}/)) return context.hex(value); } else for (var name in value) { if (context[name]) { return context[name](Value.compile(value[name]), context); } else { value[name] = Value.compile(value[name]); } break; } return value; } LSD.Sheet.Rule.fromSelectors = function(selectors, style) { //temp solution, split by comma return selectors.split(/\s*,\s*/).map(function(selector){ return new LSD.Sheet.Rule(Slick.parse(selector), style); }); }; }();