-
Notifications
You must be signed in to change notification settings - Fork 302
/
style_declaration.dart
506 lines (445 loc) · 16.4 KB
/
style_declaration.dart
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
/*
* Copyright (C) 2019-present Alibaba Inc. All rights reserved.
* Author: Kraken Team.
*/
import 'package:kraken/css.dart';
import 'package:kraken/dom.dart';
import 'package:kraken/rendering.dart';
typedef StyleChangeListener = void Function(String property, String? original, String present);
const Map<String, bool> _CSSShorthandProperty = {
MARGIN: true,
PADDING: true,
BACKGROUND: true,
BACKGROUND_POSITION: true,
BORDER_RADIUS: true,
BORDER: true,
BORDER_COLOR: true,
BORDER_WIDTH: true,
BORDER_STYLE: true,
BORDER_LEFT: true,
BORDER_RIGHT: true,
BORDER_TOP: true,
BORDER_BOTTOM: true,
FONT: true,
FLEX: true,
FLEX_FLOW: true,
OVERFLOW: true,
TRANSITION: true,
TEXT_DECORATION: true,
};
// Reorder the properties for control render style init order, the last is the largest.
List<String> _propertyOrders = [
LINE_CLAMP,
WHITE_SPACE,
FONT_SIZE,
COLOR,
TRANSITION_DURATION,
TRANSITION_PROPERTY,
OVERFLOW_X,
OVERFLOW_Y
];
RegExp _kebabCaseReg = RegExp(r'[A-Z]');
// CSS Object Model: https://drafts.csswg.org/cssom/#the-cssstyledeclaration-interface
/// The [CSSStyleDeclaration] interface represents an object that is a CSS
/// declaration block, and exposes style information and various style-related
/// methods and properties.
///
/// A [CSSStyleDeclaration] object can be exposed using three different APIs:
/// 1. Via [HTMLElement.style], which deals with the inline styles of a single
/// element (e.g., <div style="...">).
/// 2. Via the [CSSStyleSheet] API. For example,
/// document.styleSheets[0].cssRules[0].style returns a [CSSStyleDeclaration]
/// object on the first CSS rule in the document's first stylesheet.
/// 3. Via [Window.getComputedStyle()], which exposes the [CSSStyleDeclaration]
/// object as a read-only interface.
class CSSStyleDeclaration {
Element? target;
// TODO(yuanyan): defaultStyle should be longhand properties.
Map<String, dynamic>? defaultStyle;
StyleChangeListener? onStyleChanged;
CSSStyleDeclaration();
// ignore: prefer_initializing_formals
CSSStyleDeclaration.computedStyle(this.target, this.defaultStyle, this.onStyleChanged);
/// An empty style declaration.
static CSSStyleDeclaration empty = CSSStyleDeclaration();
/// When some property changed, corresponding [StyleChangeListener] will be
/// invoked in synchronous.
final List<StyleChangeListener> _styleChangeListeners = [];
final Map<String, String> _properties = {};
Map<String, String> _pendingProperties = {};
final Map<String, bool> _importants = {};
final Map<String, dynamic> _sheetStyle = {};
/// Textual representation of the declaration block.
/// Setting this attribute changes the style.
String get cssText {
String css = EMPTY_STRING;
_properties.forEach((property, value) {
if (css.isNotEmpty) css += ' ';
css += '${_kebabize(property)}: $value;';
});
return css;
}
// @TODO: Impl the cssText setter.
/// The number of properties.
int get length => _properties.length;
/// Returns a property name.
String item(int index) {
return _properties.keys.elementAt(index);
}
/// Returns the property value given a property name.
/// value is a String containing the value of the property.
/// If not set, returns the empty string.
String getPropertyValue(String propertyName) {
// Get the latest pending value first.
return _pendingProperties[propertyName] ?? _properties[propertyName] ?? EMPTY_STRING;
}
/// Removes a property from the CSS declaration.
void removeProperty(String propertyName, [bool? isImportant]) {
switch (propertyName) {
case PADDING:
return CSSStyleProperty.removeShorthandPadding(this, isImportant);
case MARGIN:
return CSSStyleProperty.removeShorthandMargin(this, isImportant);
case BACKGROUND:
return CSSStyleProperty.removeShorthandBackground(this, isImportant);
case BACKGROUND_POSITION:
return CSSStyleProperty.removeShorthandBackgroundPosition(this, isImportant);
case BORDER_RADIUS:
return CSSStyleProperty.removeShorthandBorderRadius(this, isImportant);
case OVERFLOW:
return CSSStyleProperty.removeShorthandOverflow(this, isImportant);
case FONT:
return CSSStyleProperty.removeShorthandFont(this, isImportant);
case FLEX:
return CSSStyleProperty.removeShorthandFlex(this, isImportant);
case FLEX_FLOW:
return CSSStyleProperty.removeShorthandFlexFlow(this, isImportant);
case BORDER:
case BORDER_TOP:
case BORDER_RIGHT:
case BORDER_BOTTOM:
case BORDER_LEFT:
case BORDER_COLOR:
case BORDER_STYLE:
case BORDER_WIDTH:
return CSSStyleProperty.removeShorthandBorder(this, propertyName, isImportant);
case TRANSITION:
return CSSStyleProperty.removeShorthandTransition(this, isImportant);
case TEXT_DECORATION:
return CSSStyleProperty.removeShorthandTextDecoration(this, isImportant);
}
String present = EMPTY_STRING;
if (isImportant == true) {
_importants.remove(propertyName);
// Fallback to css style.
String? value = _sheetStyle[propertyName];
if (!isNullOrEmptyValue(value)) {
present = value!;
}
} else if (isImportant == false) {
_sheetStyle.remove(propertyName);
}
// Fallback to default style.
if (isNullOrEmptyValue(present) && defaultStyle != null && defaultStyle!.containsKey(propertyName)) {
present = defaultStyle![propertyName];
}
// Update removed value by flush pending properties.
_pendingProperties[propertyName] = present;
}
void _expandShorthand(String propertyName, String normalizedValue, bool? isImportant) {
Map<String, String?> longhandProperties = {};
switch(propertyName) {
case PADDING:
CSSStyleProperty.setShorthandPadding(longhandProperties, normalizedValue);
break;
case MARGIN:
CSSStyleProperty.setShorthandMargin(longhandProperties, normalizedValue);
break;
case BACKGROUND:
CSSStyleProperty.setShorthandBackground(longhandProperties, normalizedValue);
break;
case BACKGROUND_POSITION:
CSSStyleProperty.setShorthandBackgroundPosition(longhandProperties, normalizedValue);
break;
case BORDER_RADIUS:
CSSStyleProperty.setShorthandBorderRadius(longhandProperties, normalizedValue);
break;
case OVERFLOW:
CSSStyleProperty.setShorthandOverflow(longhandProperties, normalizedValue);
break;
case FONT:
CSSStyleProperty.setShorthandFont(longhandProperties, normalizedValue);
break;
case FLEX:
CSSStyleProperty.setShorthandFlex(longhandProperties, normalizedValue);
break;
case FLEX_FLOW:
CSSStyleProperty.setShorthandFlexFlow(longhandProperties, normalizedValue);
break;
case BORDER:
case BORDER_TOP:
case BORDER_RIGHT:
case BORDER_BOTTOM:
case BORDER_LEFT:
case BORDER_COLOR:
case BORDER_STYLE:
case BORDER_WIDTH:
CSSStyleProperty.setShorthandBorder(longhandProperties, propertyName, normalizedValue);
break;
case TRANSITION:
CSSStyleProperty.setShorthandTransition(longhandProperties, normalizedValue);
break;
case TEXT_DECORATION:
CSSStyleProperty.setShorthandTextDecoration(longhandProperties, normalizedValue);
break;
}
if (longhandProperties.isNotEmpty) {
longhandProperties.forEach((String propertyName, String? value) {
setProperty(propertyName, value, isImportant);
});
}
}
String _replacePattern(String string, String lowerCase, String startString, String endString, [int start = 0]) {
int startIndex = lowerCase.indexOf(startString, start);
if (startIndex >= 0) {
int? endIndex;
int startStringLength = startString.length;
startIndex = startIndex + startStringLength;
for (int i = startIndex; i < string.length; i++) {
if (string[i] == endString) endIndex = i;
}
if (endIndex != null) {
var replacement = string.substring(startIndex, endIndex);
lowerCase = lowerCase.replaceRange(startIndex, endIndex, replacement);
if (endIndex < string.length - 1) {
lowerCase = _replacePattern(string, lowerCase, startString, endString, endIndex);
}
}
}
return lowerCase;
}
String _toLowerCase(String string) {
// Like url("http://path") declared with quotation marks and
// custom property names are case sensitive.
String lowerCase = string.toLowerCase();
lowerCase = _replacePattern(string, lowerCase, 'env(', ')');
lowerCase = _replacePattern(string, lowerCase, 'url(', ')');
// var(--my-color) will be treated as a separate custom property to var(--My-color).
lowerCase = _replacePattern(string, lowerCase, 'var(', ')');
return lowerCase;
}
bool _isValidValue(String propertyName, String normalizedValue) {
// Illegal value like ' ' after trim is '' should do nothing.
if (normalizedValue.isEmpty) return false;
// Validate value.
switch (propertyName) {
case WIDTH:
case HEIGHT:
case TOP:
case LEFT:
case RIGHT:
case BOTTOM:
case MARGIN_TOP:
case MARGIN_LEFT:
case MARGIN_RIGHT:
case MARGIN_BOTTOM:
// Validation length type
if (!CSSLength.isLength(normalizedValue) &&
!CSSLength.isAuto(normalizedValue) &&
!CSSPercentage.isPercentage(normalizedValue)
) {
return false;
}
break;
case MAX_WIDTH:
case MAX_HEIGHT:
if (normalizedValue != NONE &&
!CSSLength.isLength(normalizedValue) &&
!CSSPercentage.isPercentage(normalizedValue)
) {
return false;
}
break;
case MIN_WIDTH:
case MIN_HEIGHT:
case PADDING_TOP:
case PADDING_LEFT:
case PADDING_BOTTOM:
case PADDING_RIGHT:
if (!CSSLength.isLength(normalizedValue) &&
!CSSPercentage.isPercentage(normalizedValue)
) {
return false;
}
break;
case BORDER_BOTTOM_WIDTH:
case BORDER_TOP_WIDTH:
case BORDER_LEFT_WIDTH:
case BORDER_RIGHT_WIDTH:
if (!CSSLength.isLength(normalizedValue)) {
return false;
}
break;
case COLOR:
case BACKGROUND_COLOR:
case BORDER_BOTTOM_COLOR:
case BORDER_TOP_COLOR:
case BORDER_LEFT_COLOR:
case BORDER_RIGHT_COLOR:
case TEXT_DECORATION_COLOR:
// Validation color type
if (!CSSColor.isColor(normalizedValue)) return false;
break;
case BACKGROUND_IMAGE:
if (!CSSBackground.isValidBackgroundImageValue(normalizedValue)) return false;
break;
case BACKGROUND_REPEAT:
if (!CSSBackground.isValidBackgroundRepeatValue(normalizedValue)) return false;
break;
}
return true;
}
/// Modifies an existing CSS property or creates a new CSS property in
/// the declaration block.
void setProperty(String propertyName, value, [bool? isImportant]) {
// Null or empty value means should be removed.
if (isNullOrEmptyValue(value)) {
removeProperty(propertyName, isImportant);
return;
}
String normalizedValue = _toLowerCase(value.toString().trim());
if (!_isValidValue(propertyName, normalizedValue)) return;
if (_CSSShorthandProperty[propertyName] != null) {
return _expandShorthand(propertyName, normalizedValue, isImportant);
}
// From style sheet mark the property important as false.
if (isImportant == false) {
_sheetStyle[propertyName] = value;
}
// If the important property is already set, we should ignore it.
if (isImportant != true && _importants[propertyName] == true) {
return;
}
// Current only from inline style will mark the property as important.
if (isImportant == true) {
_importants[propertyName] = true;
}
String? prevValue = getPropertyValue(propertyName);
if (normalizedValue == prevValue) return;
_pendingProperties[propertyName] = normalizedValue;
}
void flushPendingProperties() {
Element? _target = target;
// If style target element not exists, no need to do flush operation.
if (_target == null) return;
// Display change from none to other value that the renderBoxModel is null.
if (_pendingProperties.containsKey(DISPLAY) && _target.isConnected &&
_target.parentElement?.renderStyle.display != CSSDisplay.sliver) {
String? prevValue = _properties[DISPLAY];
String currentValue = _pendingProperties[DISPLAY]!;
_properties[DISPLAY] = currentValue;
_pendingProperties.remove(DISPLAY);
_emitPropertyChanged(DISPLAY, prevValue, currentValue);
}
// If target has no renderer attached, no need to flush.
if (!_target.isRendererAttached) return;
RenderBoxModel? renderBoxModel = _target.renderBoxModel;
if (_pendingProperties.isEmpty || renderBoxModel == null) {
return;
}
Map<String, String> pendingProperties = _pendingProperties;
// Reset first avoid set property in flush stage.
_pendingProperties = {};
List<String> propertyNames = pendingProperties.keys.toList();
for (String propertyName in _propertyOrders) {
int index = propertyNames.indexOf(propertyName);
if (index > -1) {
propertyNames.removeAt(index);
propertyNames.insert(0, propertyName);
}
}
Map<String, String?> prevValues = {};
for (String propertyName in propertyNames) {
// Update the prevValue to currentValue.
prevValues[propertyName] = _properties[propertyName];
_properties[propertyName] = pendingProperties[propertyName]!;
}
for (String propertyName in propertyNames) {
String? prevValue = prevValues[propertyName];
String currentValue = pendingProperties[propertyName]!;
_emitPropertyChanged(propertyName, prevValue, currentValue);
}
}
Map<String, String?> diff(CSSStyleDeclaration other) {
Map<String, String?> diffs = {};
Map<String, String> properties = {}
..addAll(_properties)
..addAll(_pendingProperties);
for (String propertyName in properties.keys) {
String? prevValue = properties[propertyName];
String? currentValue = other._pendingProperties[propertyName];
if (isNullOrEmptyValue(prevValue) && isNullOrEmptyValue(currentValue)) {
continue;
} else if (!isNullOrEmptyValue(prevValue) && isNullOrEmptyValue(currentValue)) {
// Remove property.
diffs[propertyName] = null;
} else if (prevValue != currentValue) {
// Update property.
diffs[propertyName] = currentValue;
}
}
for (String propertyName in other._pendingProperties.keys) {
String? prevValue = properties[propertyName];
String? currentValue = other._pendingProperties[propertyName];
if (isNullOrEmptyValue(prevValue) && !isNullOrEmptyValue(currentValue)) {
// Add property.
diffs[propertyName] = currentValue;
}
}
return diffs;
}
/// Override [] and []= operator to get/set style properties.
operator [](String property) => getPropertyValue(property);
operator []=(String property, value) {
setProperty(property, value);
}
/// Check a css property is valid.
bool contains(String property) {
return getPropertyValue(property).isNotEmpty;
}
void addStyleChangeListener(StyleChangeListener listener) {
_styleChangeListeners.add(listener);
}
void removeStyleChangeListener(StyleChangeListener listener) {
_styleChangeListeners.remove(listener);
}
void _emitPropertyChanged(String property, String? original, String present) {
if (onStyleChanged != null) {
onStyleChanged!(property, original, present);
}
for (int i = 0; i < _styleChangeListeners.length; i++) {
StyleChangeListener listener = _styleChangeListeners[i];
listener(property, original, present);
}
}
void reset() {
_properties.clear();
_pendingProperties.clear();
_importants.clear();
_sheetStyle.clear();
}
void dispose() {
target = null;
_styleChangeListeners.clear();
reset();
}
static bool isNullOrEmptyValue(value) {
return value == null || value == EMPTY_STRING;
}
@override
String toString() => 'CSSStyleDeclaration($cssText)';
}
// aB to a-b
String _kebabize(String str) {
return str.replaceAllMapped(_kebabCaseReg, (match) => '-${match[0]!.toLowerCase()}');
}