/
free-style.ts
519 lines (422 loc) · 12.5 KB
/
free-style.ts
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
/**
* The unique id is used for unique hashes.
*/
let uniqueId = 0
/**
* Valid CSS property values.
*/
export type PropertyValue = number | boolean | string
/**
* Input styles object.
*/
export interface Styles {
[selector: string]: null | undefined | PropertyValue | PropertyValue[] | Styles
}
/**
* Hash algorithm interface.
*/
export type HashFunction = (str: string) => string
/**
* Tag styles with this string to get unique hashes.
*/
export const IS_UNIQUE = '__DO_NOT_DEDUPE_STYLE__'
const upperCasePattern = /[A-Z]/g
const msPattern = /^ms-/
const interpolatePattern = /&/g
const escapePattern = /[ !#$%&()*+,./;<=>?@[\]^`{|}~"'\\]/g
const propLower = (m: string) => `-${m.toLowerCase()}`
/**
* CSS properties that are valid unit-less numbers.
*/
const cssNumberProperties = [
'animation-iteration-count',
'box-flex',
'box-flex-group',
'column-count',
'counter-increment',
'counter-reset',
'flex',
'flex-grow',
'flex-positive',
'flex-shrink',
'flex-negative',
'font-weight',
'line-clamp',
'line-height',
'opacity',
'order',
'orphans',
'tab-size',
'widows',
'z-index',
'zoom',
// SVG properties.
'fill-opacity',
'stroke-dashoffset',
'stroke-opacity',
'stroke-width'
]
/**
* Map of css number properties.
*/
const CSS_NUMBER = Object.create(null)
// Add vendor prefixes to all unit-less properties.
for (const prefix of ['-webkit-', '-ms-', '-moz-', '-o-', '']) {
for (const property of cssNumberProperties) {
CSS_NUMBER[prefix + property] = true
}
}
/**
* Escape a CSS class name.
*/
export const escape = (str: string) => str.replace(escapePattern, '\\$&')
/**
* Transform a JavaScript property into a CSS property.
*/
export function hyphenate (propertyName: string): string {
return propertyName
.replace(upperCasePattern, propLower)
.replace(msPattern, '-ms-') // Internet Explorer vendor prefix.
}
/**
* Generate a hash value from a string.
*/
export function stringHash (str: string): string {
let value = 5381
let len = str.length
while (len--) value = (value * 33) ^ str.charCodeAt(len)
return (value >>> 0).toString(36)
}
/**
* Transform a style string to a CSS string.
*/
function styleToString (key: string, value: PropertyValue) {
if (typeof value === 'number' && value !== 0 && !CSS_NUMBER[key]) {
return `${key}:${value}px`
}
return `${key}:${value}`
}
/**
* Sort an array of tuples by first value.
*/
function sortTuples <T extends any[]> (value: T[]): T[] {
return value.sort((a, b) => a[0] > b[0] ? 1 : -1)
}
/**
* Categorize user styles.
*/
function parseStyles (styles: Styles, hasNestedStyles: boolean) {
const properties: Array<[string, PropertyValue | PropertyValue[]]> = []
const nestedStyles: Array<[string, Styles]> = []
let isUnique = false
// Sort keys before adding to styles.
for (const key of Object.keys(styles)) {
const value = styles[key]
if (value !== null && value !== undefined) {
if (key === IS_UNIQUE) {
isUnique = true
} else if (typeof value === 'object' && !Array.isArray(value)) {
nestedStyles.push([key.trim(), value])
} else {
properties.push([hyphenate(key.trim()), value])
}
}
}
return {
styleString: stringifyProperties(sortTuples(properties)),
nestedStyles: hasNestedStyles ? nestedStyles : sortTuples(nestedStyles),
isUnique
}
}
/**
* Stringify an array of property tuples.
*/
function stringifyProperties (properties: Array<[string, PropertyValue | PropertyValue[]]>) {
return properties.map(([name, value]) => {
if (!Array.isArray(value)) return styleToString(name, value)
return value.map(x => styleToString(name, x)).join(';')
}).join(';')
}
/**
* Interpolate CSS selectors.
*/
function interpolate (selector: string, parent: string) {
if (selector.indexOf('&') > -1) {
return selector.replace(interpolatePattern, parent)
}
return `${parent} ${selector}`
}
/**
* Recursive loop building styles with deferred selectors.
*/
function stylize (cache: Cache<any>, selector: string, styles: Styles, list: [string, Style][], parent?: string) {
const { styleString, nestedStyles, isUnique } = parseStyles(styles, !!selector)
let pid = styleString
if (selector.charCodeAt(0) === 64 /* @ */) {
const rule = cache.add(new Rule(selector, parent ? undefined : styleString, cache.hash))
// Nested styles support (e.g. `.foo > @media > .bar`).
if (styleString && parent) {
const style = rule.add(new Style(styleString, rule.hash, isUnique ? `u${(++uniqueId).toString(36)}` : undefined))
list.push([parent, style])
}
for (const [name, value] of nestedStyles) {
pid += name + stylize(rule, name, value, list, parent)
}
} else {
const key = parent ? interpolate(selector, parent) : selector
if (styleString) {
const style = cache.add(new Style(styleString, cache.hash, isUnique ? `u${(++uniqueId).toString(36)}` : undefined))
list.push([key, style])
}
for (const [name, value] of nestedStyles) {
pid += name + stylize(cache, name, value, list, key)
}
}
return pid
}
/**
* Register all styles, but collect for selector interpolation using the hash.
*/
function composeStyles (container: FreeStyle, selector: string, styles: Styles, isStyle: boolean, displayName?: string) {
const cache = new Cache<Rule | Style>(container.hash)
const list: [string, Style][] = []
const pid = stylize(cache, selector, styles, list)
const hash = `f${cache.hash(pid)}`
const id = displayName ? `${displayName}_${hash}` : hash
for (const [selector, style] of list) {
const key = isStyle ? interpolate(selector, `.${escape(id)}`) : selector
style.add(new Selector(key, style.hash, undefined, pid))
}
return { cache, pid, id }
}
/**
* Cache to list to styles.
*/
function join (arr: string[]): string {
let res = ''
for (let i = 0; i < arr.length; i++) res += arr[i]
return res
}
/**
* Propagate change events.
*/
export interface Changes {
add (style: Container<any>, index: number): void
change (style: Container<any>, oldIndex: number, newIndex: number): void
remove (style: Container<any>, index: number): void
}
/**
* Noop changes.
*/
const noopChanges: Changes = {
add: () => undefined,
change: () => undefined,
remove: () => undefined
}
/**
* Cacheable interface.
*/
export interface Container <T> {
id: string
clone (): T
getIdentifier (): string
getStyles (): string
}
/**
* Implement a cache/event emitter.
*/
export class Cache <T extends Container<any>> {
sheet: string[] = []
changeId = 0
private _keys: string[] = []
private _children: { [id: string]: T } = Object.create(null)
private _counters: { [id: string]: number } = Object.create(null)
constructor (public hash = stringHash, public changes: Changes = noopChanges) {}
add <U extends T> (style: U): U {
const count = this._counters[style.id] || 0
const item = this._children[style.id] || style.clone()
this._counters[style.id] = count + 1
if (count === 0) {
this._children[item.id] = item
this._keys.push(item.id)
this.sheet.push(item.getStyles())
this.changeId++
this.changes.add(item, this._keys.length - 1)
} else {
// Check if contents are different.
if (item.getIdentifier() !== style.getIdentifier()) {
throw new TypeError(`Hash collision: ${style.getStyles()} === ${item.getStyles()}`)
}
const oldIndex = this._keys.indexOf(style.id)
const newIndex = this._keys.length - 1
const prevChangeId = this.changeId
if (oldIndex !== newIndex) {
this._keys.splice(oldIndex, 1)
this._keys.push(style.id)
this.changeId++
}
if (item instanceof Cache && style instanceof Cache) {
const prevChangeId = item.changeId
item.merge(style)
if (item.changeId !== prevChangeId) {
this.changeId++
}
}
if (this.changeId !== prevChangeId) {
if (oldIndex === newIndex) {
this.sheet.splice(oldIndex, 1, item.getStyles())
} else {
this.sheet.splice(oldIndex, 1)
this.sheet.splice(newIndex, 0, item.getStyles())
}
this.changes.change(item, oldIndex, newIndex)
}
}
return item as U
}
remove (style: T): void {
const count = this._counters[style.id]
if (count > 0) {
this._counters[style.id] = count - 1
const item = this._children[style.id]
const index = this._keys.indexOf(item.id)
if (count === 1) {
delete this._counters[style.id]
delete this._children[style.id]
this._keys.splice(index, 1)
this.sheet.splice(index, 1)
this.changeId++
this.changes.remove(item, index)
} else if (item instanceof Cache && style instanceof Cache) {
const prevChangeId = item.changeId
item.unmerge(style)
if (item.changeId !== prevChangeId) {
this.sheet.splice(index, 1, item.getStyles())
this.changeId++
this.changes.change(item, index, index)
}
}
}
}
merge (cache: Cache<any>) {
for (const id of cache._keys) this.add(cache._children[id])
return this
}
unmerge (cache: Cache<any>) {
for (const id of cache._keys) this.remove(cache._children[id])
return this
}
clone () {
return new Cache(this.hash).merge(this)
}
}
/**
* Selector is a dumb class made to represent nested CSS selectors.
*/
export class Selector implements Container<Selector> {
constructor (
public selector: string,
public hash: HashFunction,
public id = `s${hash(selector)}`,
public pid = ''
) {}
getStyles () {
return this.selector
}
getIdentifier () {
return `${this.pid}.${this.selector}`
}
clone () {
return new Selector(this.selector, this.hash, this.id, this.pid)
}
}
/**
* The style container registers a style string with selectors.
*/
export class Style extends Cache<Selector> implements Container<Style> {
constructor (public style: string, public hash: HashFunction, public id = `c${hash(style)}`) {
super(hash)
}
getStyles (): string {
return `${this.sheet.join(',')}{${this.style}}`
}
getIdentifier () {
return this.style
}
clone (): Style {
return new Style(this.style, this.hash, this.id).merge(this)
}
}
/**
* Implement rule logic for style output.
*/
export class Rule extends Cache<Rule | Style> implements Container<Rule> {
constructor (
public rule: string,
public style = '',
public hash: HashFunction,
public id = `a${hash(`${rule}.${style}`)}`,
public pid = ''
) {
super(hash)
}
getStyles (): string {
return `${this.rule}{${this.style}${join(this.sheet)}}`
}
getIdentifier () {
return `${this.pid}.${this.rule}.${this.style}`
}
clone (): Rule {
return new Rule(this.rule, this.style, this.hash, this.id, this.pid).merge(this)
}
}
/**
* The FreeStyle class implements the API for everything else.
*/
export class FreeStyle extends Cache<Rule | Style> implements Container<FreeStyle> {
constructor (
public hash = stringHash,
public debug = typeof process !== 'undefined' && process.env['NODE_ENV'] !== 'production',
public id = `f${(++uniqueId).toString(36)}`,
changes?: Changes
) {
super(hash, changes)
}
registerStyle (styles: Styles, displayName?: string) {
const debugName = this.debug ? displayName : undefined
const { cache, id } = composeStyles(this, '&', styles, true, debugName)
this.merge(cache)
return id
}
registerKeyframes (keyframes: Styles, displayName?: string) {
return this.registerHashRule('@keyframes', keyframes, displayName)
}
registerHashRule (prefix: string, styles: Styles, displayName?: string) {
const debugName = this.debug ? displayName : undefined
const { cache, pid, id } = composeStyles(this, '', styles, false, debugName)
const rule = new Rule(`${prefix} ${escape(id)}`, undefined, this.hash, undefined, pid)
this.add(rule.merge(cache))
return id
}
registerRule (rule: string, styles: Styles) {
this.merge(composeStyles(this, rule, styles, false).cache)
}
registerCss (styles: Styles) {
this.merge(composeStyles(this, '', styles, false).cache)
}
getStyles (): string {
return join(this.sheet)
}
getIdentifier () {
return this.id
}
clone (): FreeStyle {
return new FreeStyle(this.hash, this.debug, this.id, this.changes).merge(this)
}
}
/**
* Exports a simple function to create a new instance.
*/
export function create (hash?: HashFunction, debug?: boolean, changes?: Changes) {
return new FreeStyle(hash, debug, undefined, changes)
}