-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
554 lines (485 loc) · 12.1 KB
/
index.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
'use strict';
/**
* CSSOM LiveStyle patcher: maps incoming updates to browser’s
* CSS Object Model. This is a very fast method of applying
* incoming updates from LiveStyle which is also works in any
* modern browser environment.
*/
var pathfinder = require('livestyle-pathfinder');
var splitBy = require('./lib/split');
/**
* Returns hash with available stylesheets. The keys of hash
* are absolute urls and values are pointers to StyleSheet objects
* @return {Object}
*/
var stylesheets = module.exports.stylesheets = function() {
return findStyleSheets(document.styleSheets);
};
/**
* Updates given stylesheet with patches
* @param {CSSStyleSheet} stylesheet
* @param {Array} patches
* @returns {StyleSheet} List of `insertRule` and `deleteRule` commands
* that can be applied to stylesheet to receive the same result
* (used for Shadow CSS in Chrome extension)
*/
var patch = module.exports.patch = function(stylesheet, patches) {
var self = this;
if (typeof stylesheet === 'string') {
stylesheet = this.stylesheets()[stylesheet];
}
if (!stylesheet || !stylesheet.cssRules) {
return false;
}
if (!Array.isArray(patches)) {
patches = [patches];
}
var result = patches.map(function(patch) {
var path = new NodePath(patch.path);
var hints = patch.hints ? normalizeHints(patch.hints) : null;
var index = self.createIndex(stylesheet);
var location = pathfinder.find(index, path, hints);
if (location.partial && patch.action === 'remove') {
// node is absent, do nothing
return;
}
if (!location.partial) {
// exact match on node
if (patch.action === 'remove') {
var node = location.node;
deleteRuleFromMatch(location);
return resultOfDeletePatch(node);
}
return patchRule(location.node.ref, patch);
}
var out = [];
var rule = setupFromPartialMatch(location, out);
return out.concat(patchRule(rule, patch));
}).filter(Boolean);
return flatten(result);
};
var createIndex = module.exports.createIndex = function(ctx, parent) {
var indexOf = function(item) {
return this.children.indexOf(item);
};
if (!parent) {
parent = {
ix: -1,
name: '',
parent: null,
children: [],
ref: ctx,
indexOf: indexOf
};
}
var rules = ctx.cssRules;
if (!rules) {
return parent;
}
for (var i = 0, il = rules.length, rule, name, item; i < il; i++) {
rule = rules[i];
name = ruleName(rule);
if (name === '@charset' || name === '@import') {
continue;
}
item = {
ix: i,
name: normalizeSelector(name),
parent: parent,
children: [],
ref: rule,
indexOf: indexOf
};
parent.children.push(item);
this.createIndex(rule, item);
}
return parent;
};
function last(arr) {
return arr[arr.length - 1];
}
function flatten(input) {
var output = [];
for (var i = 0, il = input.length, value; i < il; i++) {
value = input[i];
if (Array.isArray(value)) {
output = output.concat(flatten(value));
} else {
output.push(value);
}
}
return output;
}
function isTopLevel(node) {
return node && node.parent && !node.parent.parent;
}
function resultOfDeletePatch(node) {
if (isTopLevel(node)) {
// matched top-level section, removed it
return {
action: 'delete',
index: node.ix
};
}
// matched inner node, mark top-level node as updated
var ctx = node;
while (ctx && !isTopLevel(ctx)) {
ctx = ctx.parent;
}
if (ctx) {
return {
action: 'update',
index: ctx.ix,
value: ctx.ref.cssText
};
}
}
/**
* Node path shim
*/
class NodePath {
constructor(path) {
this.components = [];
if (Array.isArray(path)) {
this.components = path.map(function(c) {
return new NodePathComponent(c);
});
}
}
toString() {
return this.components.map(function(c) {
return c.toString(true);
}).join('/');
}
}
class NodePathComponent {
constructor(name, pos) {
if (Array.isArray(name)) {
pos = name[1];
name = name[0]
}
this.name = normalizeSelector(name);
this.pos = pos || 1;
}
toString() {
return this.name + (this.pos > 1 ? '|' + this.pos : '');
}
}
function normalizeSelector(sel) {
return sel.trim().replace(/:+(before|after)$/, '::$1');
}
/**
* Findes all stylesheets in given context, including
* nested `@import`s
* @param {StyleSheetList} ctx List of stylesheets to scan
* @return {Object} Hash where key as a stylesheet URL and value
* is a stylesheet reference
*/
function findStyleSheets(ctx, out) {
out = out || {};
for (var i = 0, il = ctx.length, url, item; i < il; i++) {
item = ctx[i];
url = item.href;
if (url in out) {
// stylesheet already added
continue;
}
out[url] = item;
// find @import rules
// Firefox throws exception when accessing cssRules property
// of stylesheet from different origin
try {
if (item.cssRules) {
for (var j = 0, jl = item.cssRules.length; j < jl; j++) {
if (item.cssRules[j].type == 3) {
findStyleSheets([item.cssRules[j].styleSheet], out);
}
}
}
} catch(e) {}
}
return out;
}
function atRuleName(rule) {
/*
* Reference:
* UNKNOWN_RULE: 0
* STYLE_RULE: 1
* CHARSET_RULE: 2
* IMPORT_RULE: 3
* MEDIA_RULE: 4
* FONT_FACE_RULE: 5
* PAGE_RULE: 6
* KEYFRAMES_RULE: 7
* KEYFRAME_RULE: 8
* SUPPORTS_RULE: 12
* WEBKIT_FILTER_RULE: 17
* HOST_RULE: 1001
*/
switch (rule.type) {
case 2: return '@charset';
case 3: return '@import';
case 4: return '@media ' + rule.media.mediaText;
case 5: return '@font-face';
}
}
/**
* Returns name of given rule
* @param {CSSRule} rule
* @return {String}
*/
function ruleName(rule) {
var sel = rule.selectorText || atRuleName(rule);
if (sel) {
return sel;
}
var text = rule.cssText;
if (text) {
return (text.split('{', 2)[0] || '').trim();
}
}
/**
* Returns rule’s parent (stylesheet or rule)
* @param {CSSRule} rule
* @return {CSSStyleSheet}
*/
function parent(rule) {
return rule.parentRule || rule.parentStyleSheet;
}
/**
* Check if given @-rule equals to given patch property
* @param {CSSRule} rule
* @param {Object} prop
* @return {Boolean}
*/
function atRuleEquals(rule, prop) {
if (atRuleName(rule) !== prop.name) {
return false;
}
switch (prop.name) {
case '@charset':
return rule.encoding === prop.value.trim().replace(/^['"]|['"]$/g, '');
case '@import':
return rule.href === prop.value.trim().replace(/^url\(['"]?|['"]?\)$/g, '');
}
}
/**
* Updates properties in given CSS rule
* @param {CSSRule} rule
* @param {Array} properties
* @param {Patch} patch
*/
function updateProperties(rule, properties, patch) {
if (!rule || !rule.style) {
return;
}
if ('ownerNode' in rule) {
// A stylesheet (not CSS rule) cannot have properties;
// updating them in Chrome gives unpredictable result
return;
}
if (patch && patch.all) {
// there are few challenges when changing updated
// properies via CSSOM:
// 1. Updating a a short-hand property only (for example,
// `background` or `font`) will reset subsequent
// full properties (`background-size`, `line-height` etc.)
// that are not changed
// 2. Chrome has buggy implementation of `background` shorthand:
// at least it looses `background-size` property.
//
// So right now the only valid and simple solution is to
// re-apply all exising properties from source CSS even if they
// were not updated or they doesn’t exist in current CSSOM rule
properties = patch.all;
}
var style = rule.style;
properties.forEach(function(p) {
var important = null;
var value = p.value.replace(/\!important\s*$/, function() {
important = 'important';
return '';
})
nameVariations(p.name).forEach(function(name) {
style.setProperty(name, value, important);
});
});
}
function nameVariations(name) {
var out = [name];
if (name.indexOf('-') !== -1) {
var camelCased = name.replace(/\-([a-z])/g, function(str, l) {
return l.toUpperCase();
});
out.push(camelCased);
if (name[0] === '-') {
out.push(camelCased[0].toLowerCase() + camelCased.slice(1));
}
}
return out;
}
/**
* Updates given rule with data from patch
* @param {CSSRule} rule
* @param {Array} patch
*/
function patchRule(rule, patch) {
var result = [];
if (!rule) {
// not a CSSStyleRule, aborting
return result;
}
var reAt = /^@/, childRule;
// remove properties
patch.remove.forEach(function(prop) {
if (reAt.test(prop)) {
// @-properties are not properties but rules
if (!rule.cssRules || !rule.cssRules.length) {
return;
}
for (var i = 0, il = rule.cssRules.length; i < il; i++) {
if (atRuleEquals(rule.cssRules[i], prop)) {
return rule.deleteRule(i);
}
}
} else if (rule.style) {
rule.style.removeProperty(prop.name);
}
});
var updateRules = {
'@charset': [],
'@import': []
};
// update properties on current rule
var properties = patch.update.filter(function(prop) {
if (prop.name in updateRules) {
updateRules[prop.name].push(prop);
return false;
}
return true;
});
updateProperties(rule, properties, patch);
// insert @-properties as rules
while (childRule = updateRules['@charset'].pop()) {
rule.insertRule(childRule.name + ' ' + childRule.value, 0);
result.push({
action: 'insert',
index: 0,
value: childRule.name + ' ' + childRule.value
});
}
if (updateRules['@import'].length && rule.cssRules) {
// @import’s must be inserted right after existing imports
var childIx = 0, childName;
for (var i = rule.cssRules.length - 1; i >= 0; i--) {
childName = atRuleName(rule.cssRules[i]);
if (childName === '@charset' || childName === '@import') {
childIx = i;
break;
}
}
while (childRule = updateRules['@import'].pop()) {
rule.insertRule(childRule.name + ' ' + childRule.value, childIx);
result.push({
action: 'insert',
index: childIx,
value: childRule.name + ' ' + childRule.value
});
}
}
// find parent rule
var ctx = rule;
while (ctx.parentRule) {
ctx = ctx.parentRule;
}
var ruleIx = -1;
var allRules = ctx.parentStyleSheet && ctx.parentStyleSheet.cssRules;
if (allRules) {
for (var i = 0, il = allRules.length; i < il; i++) {
if (allRules[i] === ctx) {
ruleIx = i;
break;
}
}
}
if (ruleIx !== -1) {
result.push({
action: 'update',
index: ruleIx,
value: ctx.cssText
});
}
return result;
}
function setupFromPartialMatch(match, result) {
// The `rest` property means we didn’t found exact section
// where patch should be applied, but some of its parents.
// In this case we have to re-create the `rest` sections
// in best matching parent
var accumulated = match.rest.reduceRight(function(prev, cur) {
return cur.name + ' {' + prev + '}';
}, '');
var parent = match.parent;
var insertIndex = parent.ref.cssRules ? parent.ref.cssRules.length : 0;
if (match.node) {
insertIndex = match.node.ix;
}
// console.log('Insert rule at index', insertIndex, match);
try {
var ix = parent.ref.insertRule(accumulated, insertIndex);
if (!parent.ref.parentStyleSheet) {
// inserted a top-level rule
result.push({
action: 'insert',
index: ix,
value: parent.ref.cssRules[ix].cssText
});
}
} catch (e) {
console.warn('LiveStyle:', e.message);
return;
}
var ctx = parent.ref.cssRules[ix];
var indexed = exports.createIndex(ctx);
indexed.name = ruleName(ctx);
indexed.ix = ix;
parent.children.splice(match.index, 0, indexed);
for (var i = match.index + 1, il = parent.children.length; i < il; i++) {
parent.children[i].ix++;
}
while (ctx.cssRules && ctx.cssRules.length) {
ctx = ctx.cssRules[0];
}
return ctx;
}
function deleteRuleFromMatch(match) {
var result = null;
try {
parent(match.node.ref).deleteRule(match.node.ix);
} catch (e) {
console.warn('LiveStyle:', e);
console.warn(match);
}
// console.log('Removed rule at index', match.node.ix);
var ix = match.parent.children.indexOf(match.node);
if (~ix) {
match.parent.children.splice(ix, 1);
for (var i = ix, il = match.parent.children.length, child; i < il; i++) {
match.parent.children[i].ix--;
}
}
}
function normalizeHints(hints) {
var comp = function(c) {
return new NodePathComponent(c);
};
return hints.map(function(hint) {
if (hint.before) {
hint.before = hint.before.map(comp);
}
if (hint.after) {
hint.after = hint.after.map(comp);
}
return hint;
});
}