-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjss.js
583 lines (497 loc) · 14.2 KB
/
jss.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.jss=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* StyleSheets written in javascript.
*
* @copyright Oleg Slobodskoi 2014
* @website https://github.com/jsstyles/jss
* @license MIT
*/
module.exports = require('./lib/index')
},{"./lib/index":4}],2:[function(require,module,exports){
'use strict'
var plugins = require('./plugins')
var uid = 0
var toString = Object.prototype.toString
/**
* Rule is selector + style hash.
*
* @param {String} [selector]
* @param {Object} [style] declarations block
* @param {Object} [options]
* @api public
*/
function Rule(selector, style, options) {
if (typeof selector == 'object') {
options = style
style = selector
selector = null
}
this.id = Rule.uid++
this.options = options || {}
if (this.options.named == null) this.options.named = true
if (selector) {
this.selector = selector
this.isAtRule = selector[0] == '@'
} else {
this.isAtRule = false
this.className = Rule.NAMESPACE_PREFIX + '-' + this.id
this.selector = '.' + this.className
}
this.style = style
// Will be set by StyleSheet#link if link option is true.
this.CSSRule = null
// When at-rule has sub rules.
this.rules = null
if (this.isAtRule && this.style) this.extractAtRules()
}
module.exports = Rule
/**
* Class name prefix when generated.
*
* @type {String}
* @api private
*/
Rule.NAMESPACE_PREFIX = 'jss'
/**
* Indentation string for formatting toString output.
*
* @type {String}
* @api private
*/
Rule.INDENTATION = ' '
/**
* Unique id, right now just a counter, because there is no need for better uid.
*
* @type {Number}
* @api private
*/
Rule.uid = 0
/**
* Get or set a style property.
*
* @param {String} name
* @param {String|Number} [value]
* @return {Rule|String|Number}
* @api public
*/
Rule.prototype.prop = function (name, value) {
// Its a setter.
if (value) {
if (!this.style) this.style = {}
this.style[name] = value
// If linked option in StyleSheet is not passed, CSSRule is not defined.
if (this.CSSRule) this.CSSRule.style[name] = value
return this
}
// Its a getter.
if (this.style) value = this.style[name]
// Read the value from the DOM if its not cached.
if (value == null && this.CSSRule) {
value = this.CSSRule.style[name]
// Cache the value after we have got it from the DOM once.
this.style[name] = value
}
return value
}
/**
* Add child rule. Required for plugins like "nested".
* StyleSheet will render them as a separate rule.
*
* @param {String} selector
* @param {Object} style
* @param {Object} [options] rule options
* @return {Rule}
* @api private
*/
Rule.prototype.addChild = function (selector, style, options) {
if (!this.children) this.children = {}
this.children[selector] = {
style: style,
options: options
}
return this
}
/**
* Add child rule. Required for plugins like "nested".
* StyleSheet will render them as a separate rule.
*
* @param {String} selector
* @param {Object} style
* @return {Rule}
* @api public
*/
Rule.prototype.extractAtRules = function () {
if (!this.rules) this.rules = {}
for (var name in this.style) {
var style = this.style[name]
// Not a nested rule.
if (typeof style == 'string') break
var selector = this.options.named ? undefined : name
var rule = this.rules[name] = new Rule(selector, style, this.options)
plugins.run(rule)
delete this.style[name]
}
return this
}
/**
* Apply rule to an element inline.
*
* @param {Element} element
* @return {Rule}
* @api public
*/
Rule.prototype.applyTo = function (element) {
for (var prop in this.style) {
var value = this.style[prop]
if (toString.call(value) == '[object Array]') {
for (var i = 0; i < value.length; i++) {
element.style[prop] = value[i]
}
} else {
element.style[prop] = value
}
}
return this
}
/**
* Converts the rule to css string.
*
* @return {String}
* @api public
*/
Rule.prototype.toString = function (options) {
var style = this.style
// At rules like @charset
if (this.isAtRule && !this.style && !this.rules) return this.selector + ';'
if (!options) options = {}
if (options.indentationLevel == null) options.indentationLevel = 0
var str = indent(options.indentationLevel, this.selector + ' {')
for (var prop in style) {
var value = style[prop]
// We want to generate multiple style with identical property names.
if (toString.call(value) == '[object Array]') {
for (var i = 0; i < value.length; i++) {
str += '\n' + indent(options.indentationLevel + 1, prop + ': ' + value[i] + ';')
}
} else {
str += '\n' + indent(options.indentationLevel + 1, prop + ': ' + value + ';')
}
}
// We are have an at-rule with nested statements.
// https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
for (var name in this.rules) {
var ruleStr = this.rules[name].toString({
indentationLevel: options.indentationLevel + 1
})
str += '\n' + indent(options.indentationLevel, ruleStr)
}
str += '\n' + indent(options.indentationLevel, '}')
return str
}
/**
* Indent a string.
*
* @param {Number} level
* @param {String} str
* @return {String}
*/
function indent(level, str) {
var indentStr = ''
for (var i = 0; i < level; i++) indentStr += Rule.INDENTATION
return indentStr + str
}
},{"./plugins":5}],3:[function(require,module,exports){
'use strict'
var Rule = require('./Rule')
var plugins = require('./plugins')
/**
* StyleSheet abstraction, contains rules, injects stylesheet into dom.
*
* Options:
*
* - `media` style element attribute
* - `title` style element attribute
* - `type` style element attribute
* - `named` true by default - keys are names, selectors will be generated,
* if false - keys are global selectors.
* - `link` link jss Rule instances with DOM CSSRule instances so that styles,
* can be modified dynamically, false by default because it has some performance cost.
*
* @param {Object} [rules] object with selectors and declarations
* @param {Object} [options]
* @api public
*/
function StyleSheet(rules, options) {
this.options = options || {}
if (this.options.named == null) this.options.named = true
this.element = null
this.attached = false
this.media = this.options.media
this.type = this.options.type
this.title = this.options.title
this.rules = {}
// Only when options.named: true.
this.classes = {}
this.deployed = false
this.linked = false
// Don't create element if we are not in a browser environment.
if (typeof document != 'undefined') {
this.element = this.createElement()
}
for (var key in rules) {
this.createRules(key, rules[key])
}
}
StyleSheet.ATTRIBUTES = ['title', 'type', 'media']
module.exports = StyleSheet
/**
* Insert stylesheet element to render tree.
*
* @api public
* @return {StyleSheet}
*/
StyleSheet.prototype.attach = function () {
if (this.attached) return this
if (!this.deployed) {
this.deploy()
this.deployed = true
}
document.head.appendChild(this.element)
// Before element is attached to the dom rules are not created.
if (!this.linked && this.options.link) {
this.link()
this.linked = true
}
this.attached = true
return this
}
/**
* Remove stylesheet element from render tree.
*
* @return {StyleSheet}
* @api public
*/
StyleSheet.prototype.detach = function () {
if (!this.attached) return this
this.element.parentNode.removeChild(this.element)
this.attached = false
return this
}
/**
* Deploy styles to the element.
*
* @return {StyleSheet}
* @api private
*/
StyleSheet.prototype.deploy = function () {
this.element.innerHTML = '\n' + this.toString() + '\n'
return this
}
/**
* Find CSSRule objects in the DOM and link them in the corresponding Rule instance.
*
* @return {StyleSheet}
* @api private
*/
StyleSheet.prototype.link = function () {
var CSSRuleList = this.element.sheet.cssRules
var rules = this.rules
for (var i = 0; i < CSSRuleList.length; i++) {
var CSSRule = CSSRuleList[i]
var rule = rules[CSSRule.selectorText]
if (rule) rule.CSSRule = CSSRule
}
return this
}
/**
* Add a rule to the current stylesheet. Will insert a rule also after the stylesheet
* has been rendered first time.
*
* @param {Object} [key] can be selector or name if `options.named` is true
* @param {Object} style property/value hash
* @return {Rule}
* @api public
*/
StyleSheet.prototype.addRule = function (key, style) {
var rules = this.createRules(key, style)
// Don't insert rule directly if there is no stringified version yet.
// It will be inserted all together when .attach is called.
if (this.deployed) {
var sheet = this.element.sheet
for (var i = 0; i < rules.length; i++) {
var nextIndex = sheet.cssRules.length
var rule = rules[i]
sheet.insertRule(rule.toString(), nextIndex)
if (this.options.link) rule.CSSRule = sheet.cssRules[nextIndex]
}
} else {
this.deploy()
}
return rules
}
/**
* Create rules, will render also after stylesheet was rendered the first time.
*
* @param {Object} rules key:style hash.
* @return {StyleSheet} this
* @api public
*/
StyleSheet.prototype.addRules = function (rules) {
for (var key in rules) {
this.addRule(key, rules[key])
}
return this
}
/**
* Get a rule.
*
* @param {String} key can be selector or name if `named` is true.
* @return {Rule}
* @api public
*/
StyleSheet.prototype.getRule = function (key) {
return this.rules[key]
}
/**
* Convert rules to a css string.
*
* @return {String}
* @api public
*/
StyleSheet.prototype.toString = function () {
var str = ''
var rules = this.rules
var stringified = {}
for (var key in rules) {
var rule = rules[key]
// We have the same rule referenced twice if using named urles.
// By name and by selector.
if (stringified[rule.id]) continue
if (str) str += '\n'
str += rules[key].toString()
stringified[rule.id] = true
}
return str
}
/**
* Create a rule, will not render after stylesheet was rendered the first time.
*
* @param {Object} [selector] if you don't pass selector - it will be generated
* @param {Object} [style] declarations block
* @param {Object} [options] rule options
* @return {Array} rule can contain child rules
* @api private
*/
StyleSheet.prototype.createRules = function (key, style, options) {
var rules = []
var selector, name
if (!options) options = {}
var named = this.options.named
// Scope options overwrite instance options.
if (options.named != null) named = options.named
if (named) name = key
else selector = key
var rule = new Rule(selector, style, {
sheet: this,
named: named,
name: name
})
rules.push(rule)
this.rules[rule.selector] = rule
if (name) {
this.rules[name] = rule
this.classes[name] = rule.className
}
plugins.run(rule)
for (key in rule.children) {
rules.push(this.createRules(
key,
rule.children[key].style,
rule.children[key].options
))
}
return rules
}
/**
* Create style sheet element.
*
* @api private
* @return {Element}
*/
StyleSheet.prototype.createElement = function () {
var element = document.createElement('style')
StyleSheet.ATTRIBUTES.forEach(function (name) {
if (this[name]) element.setAttribute(name, this[name])
}, this)
return element
}
},{"./Rule":2,"./plugins":5}],4:[function(require,module,exports){
'use strict'
var StyleSheet = require('./StyleSheet')
var Rule = require('./Rule')
exports.StyleSheet = StyleSheet
exports.Rule = Rule
exports.plugins = require('./plugins')
/**
* Create a stylesheet.
*
* @param {Object} rules is selector:style hash.
* @param {Object} [named] rules have names if true, class names will be generated.
* @param {Object} [attributes] stylesheet element attributes.
* @return {StyleSheet}
* @api public
*/
exports.createStyleSheet = function (rules, named, attributes) {
return new StyleSheet(rules, named, attributes)
}
/**
* Create a rule.
*
* @param {String} [selector]
* @param {Object} style is property:value hash.
* @return {Rule}
* @api public
*/
exports.createRule = function (selector, style) {
var rule = new Rule(selector, style)
exports.plugins.run(rule)
return rule
}
/**
* Register plugin. Passed function will be invoked with a rule instance.
*
* @param {Function} fn
* @api public
*/
exports.use = exports.plugins.use
},{"./Rule":2,"./StyleSheet":3,"./plugins":5}],5:[function(require,module,exports){
'use strict'
/**
* Registered plugins.
*
* @type {Array}
* @api public
*/
exports.registry = []
/**
* Register plugin. Passed function will be invoked with a rule instance.
*
* @param {Function} fn
* @api public
*/
exports.use = function (fn) {
exports.registry.push(fn)
}
/**
* Execute all registered plugins.
*
* @param {Rule} rule
* @api private
*/
exports.run = function (rule) {
for (var i = 0; i < exports.registry.length; i++) {
exports.registry[i](rule)
}
}
},{}]},{},[1])(1)
});