-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
style_cascade.cc
1400 lines (1215 loc) · 50.9 KB
/
style_cascade.cc
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/css/resolver/style_cascade.h"
#include "third_party/blink/renderer/core/animation/css/css_animations.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_environment.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_types_map.h"
#include "third_party/blink/renderer/core/animation/invalidatable_interpolation.h"
#include "third_party/blink/renderer/core/animation/property_handle.h"
#include "third_party/blink/renderer/core/animation/transition_interpolation.h"
#include "third_party/blink/renderer/core/css/css_custom_property_declaration.h"
#include "third_party/blink/renderer/core/css/css_cyclic_variable_value.h"
#include "third_party/blink/renderer/core/css/css_font_selector.h"
#include "third_party/blink/renderer/core/css/css_invalid_variable_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_pending_substitution_value.h"
#include "third_party/blink/renderer/core/css/css_unset_value.h"
#include "third_party/blink/renderer/core/css/css_variable_data.h"
#include "third_party/blink/renderer/core/css/css_variable_reference_value.h"
#include "third_party/blink/renderer/core/css/document_style_environment_variables.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_local_context.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_token_stream.h"
#include "third_party/blink/renderer/core/css/parser/css_property_parser.h"
#include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
#include "third_party/blink/renderer/core/css/parser/css_variable_parser.h"
#include "third_party/blink/renderer/core/css/properties/css_parsing_utils.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/core/css/properties/css_property_ref.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/core/css/property_bitsets.h"
#include "third_party/blink/renderer/core/css/property_registry.h"
#include "third_party/blink/renderer/core/css/resolver/cascade_expansion-inl.h"
#include "third_party/blink/renderer/core/css/resolver/cascade_expansion.h"
#include "third_party/blink/renderer/core/css/resolver/cascade_interpolations.h"
#include "third_party/blink/renderer/core/css/resolver/cascade_resolver.h"
#include "third_party/blink/renderer/core/css/resolver/style_builder.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/style_property_shorthand.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
namespace {
template <class ParserTokenStream>
AtomicString ConsumeVariableName(ParserTokenStream& stream) {
stream.ConsumeWhitespace();
CSSParserToken ident_token = stream.ConsumeIncludingWhitespaceRaw();
DCHECK_EQ(ident_token.GetType(), kIdentToken);
return ident_token.Value().ToAtomicString();
}
template <class ParserTokenStream>
bool ConsumeComma(ParserTokenStream& stream) {
if (stream.Peek().GetType() == kCommaToken) {
stream.ConsumeRaw();
return true;
}
return false;
}
const CSSValue* Parse(const CSSProperty& property,
CSSParserTokenRange range,
const CSSParserContext* context) {
return CSSPropertyParser::ParseSingleValue(property.PropertyID(), range,
context);
}
const CSSValue* ValueAt(const MatchResult& result, uint32_t position) {
wtf_size_t matched_properties_index = DecodeMatchedPropertiesIndex(position);
wtf_size_t declaration_index = DecodeDeclarationIndex(position);
const MatchedPropertiesVector& vector = result.GetMatchedProperties();
const CSSPropertyValueSet* set = vector[matched_properties_index].properties;
return &set->PropertyAt(declaration_index).Value();
}
const TreeScope& TreeScopeAt(const MatchResult& result, uint32_t position) {
wtf_size_t matched_properties_index = DecodeMatchedPropertiesIndex(position);
const MatchedProperties& properties =
result.GetMatchedProperties()[matched_properties_index];
DCHECK_EQ(properties.types_.origin, CascadeOrigin::kAuthor);
return result.ScopeFromTreeOrder(properties.types_.tree_order);
}
PropertyHandle ToPropertyHandle(const CSSProperty& property,
CascadePriority priority) {
uint32_t position = priority.GetPosition();
CSSPropertyID id = DecodeInterpolationPropertyID(position);
if (id == CSSPropertyID::kVariable) {
DCHECK(IsA<CustomProperty>(property));
return PropertyHandle(property.GetPropertyNameAtomicString());
}
return PropertyHandle(CSSProperty::Get(id),
DecodeIsPresentationAttribute(position));
}
// https://drafts.csswg.org/css-cascade-4/#default
CascadeOrigin TargetOriginForRevert(CascadeOrigin origin) {
switch (origin) {
case CascadeOrigin::kNone:
case CascadeOrigin::kTransition:
NOTREACHED();
return CascadeOrigin::kNone;
case CascadeOrigin::kUserAgent:
return CascadeOrigin::kNone;
case CascadeOrigin::kUser:
return CascadeOrigin::kUserAgent;
case CascadeOrigin::kAuthorPresentationalHint:
case CascadeOrigin::kAuthor:
case CascadeOrigin::kAnimation:
return CascadeOrigin::kUser;
}
}
CSSPropertyID UnvisitedID(CSSPropertyID id) {
if (id == CSSPropertyID::kVariable) {
return id;
}
const CSSProperty& property = CSSProperty::Get(id);
if (!property.IsVisited()) {
return id;
}
return property.GetUnvisitedProperty()->PropertyID();
}
bool IsInterpolation(CascadePriority priority) {
switch (priority.GetOrigin()) {
case CascadeOrigin::kAnimation:
case CascadeOrigin::kTransition:
return true;
case CascadeOrigin::kNone:
case CascadeOrigin::kUserAgent:
case CascadeOrigin::kUser:
case CascadeOrigin::kAuthorPresentationalHint:
case CascadeOrigin::kAuthor:
return false;
}
}
#if DCHECK_IS_ON()
bool HasUnresolvedReferences(CSSParserTokenRange range) {
while (!range.AtEnd()) {
switch (range.Consume().FunctionId()) {
case CSSValueID::kVar:
case CSSValueID::kEnv:
return true;
default:
continue;
}
}
return false;
}
#endif // DCHECK_IS_ON()
} // namespace
MatchResult& StyleCascade::MutableMatchResult() {
DCHECK(!generation_) << "Apply has already been called";
needs_match_result_analyze_ = true;
return match_result_;
}
void StyleCascade::AddInterpolations(const ActiveInterpolationsMap* map,
CascadeOrigin origin) {
DCHECK(map);
needs_interpolations_analyze_ = true;
interpolations_.Add(map, origin);
}
void StyleCascade::Apply(CascadeFilter filter) {
AnalyzeIfNeeded();
state_.UpdateLengthConversionData();
CascadeResolver resolver(filter, ++generation_);
ApplyCascadeAffecting(resolver);
if (map_.NativeBitset().Has(CSSPropertyID::kColorScheme)) {
// Affects the computed value of 'color', hence needs to happen before
// high-priority properties.
LookupAndApply(GetCSSPropertyColorScheme(), resolver);
}
if (map_.NativeBitset().Has(CSSPropertyID::kMathDepth)) {
// Affects the computed value of 'font-size', hence needs to happen before
// high-priority properties.
LookupAndApply(GetCSSPropertyMathDepth(), resolver);
}
if (map_.NativeBitset().Has(CSSPropertyID::kWebkitMaskImage)) {
// -webkit-mask-image needs to be applied before -webkit-mask-composite,
// otherwise -webkit-mask-composite has no effect.
LookupAndApply(GetCSSPropertyWebkitMaskImage(), resolver);
}
if (map_.NativeBitset().Has(CSSPropertyID::kForcedColorAdjust)) {
// Affects the computed value of color when it is inherited and
// forced-color- adjust is set to preserve-parent-color.
LookupAndApply(GetCSSPropertyForcedColorAdjust(), resolver);
}
ApplyHighPriority(resolver);
if (map_.NativeBitset().Has(CSSPropertyID::kLineHeight)) {
LookupAndApply(GetCSSPropertyLineHeight(), resolver);
}
state_.UpdateLineHeight();
ApplyWideOverlapping(resolver);
ApplyMatchResult(resolver);
ApplyInterpolations(resolver);
// These three flags are only used if HasAppearance() is set
// (they are used for knowing whether appearance: auto is to be overridden),
// but we compute them nevertheless, to avoid suddenly having to compute them
// after-the-fact if inline style is updated incrementally.
if (resolver.AuthorFlags() & CSSProperty::kBackground) {
state_.StyleBuilder().SetHasAuthorBackground();
}
if (resolver.AuthorFlags() & CSSProperty::kBorder) {
state_.StyleBuilder().SetHasAuthorBorder();
}
if (resolver.AuthorFlags() & CSSProperty::kBorderRadius) {
state_.StyleBuilder().SetHasAuthorBorderRadius();
}
if ((state_.InsideLink() != EInsideLink::kInsideVisitedLink &&
(resolver.AuthorFlags() & CSSProperty::kHighlightColors)) ||
(state_.InsideLink() == EInsideLink::kInsideVisitedLink &&
(resolver.AuthorFlags() & CSSProperty::kVisitedHighlightColors))) {
state_.StyleBuilder().SetHasAuthorHighlightColors();
}
if (resolver.Flags() & CSSProperty::kAnimation) {
state_.StyleBuilder().SetCanAffectAnimations();
}
if (resolver.RejectedFlags() & CSSProperty::kLegacyOverlapping) {
state_.SetRejectedLegacyOverlapping();
}
// TOOD(crbug.com/1334570):
//
// Count applied H1 font-size from html.css UA stylesheet where H1 is inside
// a sectioning element matching selectors like:
//
// :-webkit-any(article,aside,nav,section) h1 { ... }
//
if (!state_.GetElement().HasTagName(html_names::kH1Tag)) {
return;
}
if (CascadePriority* priority =
map_.Find(GetCSSPropertyFontSize().GetCSSPropertyName())) {
if (priority->GetOrigin() != CascadeOrigin::kUserAgent) {
return;
}
const CSSValue* value = ValueAt(match_result_, priority->GetPosition());
if (const auto* numeric = DynamicTo<CSSNumericLiteralValue>(value)) {
DCHECK(numeric->GetType() == CSSNumericLiteralValue::UnitType::kEms);
if (numeric->DoubleValue() != 2.0) {
CountUse(WebFeature::kH1UserAgentFontSizeInSectionApplied);
}
}
}
}
std::unique_ptr<CSSBitset> StyleCascade::GetImportantSet() {
AnalyzeIfNeeded();
if (!map_.HasImportant()) {
return nullptr;
}
auto set = std::make_unique<CSSBitset>();
for (CSSPropertyID id : map_.NativeBitset()) {
// We use the unvisited ID because visited/unvisited colors are currently
// interpolated together.
// TODO(crbug.com/1062217): Interpolate visited colors separately
set->Or(UnvisitedID(id), map_.At(CSSPropertyName(id)).IsImportant());
}
return set;
}
void StyleCascade::Reset() {
map_.Reset();
match_result_.Reset();
interpolations_.Reset();
generation_ = 0;
depends_on_cascade_affecting_property_ = false;
}
const CSSValue* StyleCascade::Resolve(const CSSPropertyName& name,
const CSSValue& value,
CascadeOrigin origin,
CascadeResolver& resolver) {
CSSPropertyRef ref(name, state_.GetDocument());
const CSSValue* resolved = Resolve(ResolveSurrogate(ref.GetProperty()), value,
CascadePriority(origin), origin, resolver);
DCHECK(resolved);
// TODO(crbug.com/1185745): Cycles in animations get special handling by our
// implementation. This is not per spec, but the correct behavior is not
// defined at the moment.
if (resolved->IsCyclicVariableValue()) {
return nullptr;
}
// TODO(crbug.com/1185745): We should probably not return 'unset' for
// properties where CustomProperty::SupportsGuaranteedInvalid return true.
if (resolved->IsInvalidVariableValue()) {
return cssvalue::CSSUnsetValue::Create();
}
return resolved;
}
HeapHashMap<CSSPropertyName, Member<const CSSValue>>
StyleCascade::GetCascadedValues() const {
DCHECK(!needs_match_result_analyze_);
DCHECK(!needs_interpolations_analyze_);
DCHECK_GE(generation_, 0);
HeapHashMap<CSSPropertyName, Member<const CSSValue>> result;
for (CSSPropertyID id : map_.NativeBitset()) {
CSSPropertyName name(id);
CascadePriority priority = map_.At(name);
DCHECK(priority.HasOrigin());
if (IsInterpolation(priority)) {
continue;
}
const CSSValue* cascaded = ValueAt(match_result_, priority.GetPosition());
DCHECK(cascaded);
result.Set(name, cascaded);
}
for (const auto& name : map_.GetCustomMap().Keys()) {
CascadePriority priority = map_.At(CSSPropertyName(name));
DCHECK(priority.HasOrigin());
if (IsInterpolation(priority)) {
continue;
}
const CSSValue* cascaded = ValueAt(match_result_, priority.GetPosition());
DCHECK(cascaded);
result.Set(CSSPropertyName(name), cascaded);
}
return result;
}
void StyleCascade::AnalyzeIfNeeded() {
if (needs_match_result_analyze_) {
AnalyzeMatchResult();
needs_match_result_analyze_ = false;
}
if (needs_interpolations_analyze_) {
AnalyzeInterpolations();
needs_interpolations_analyze_ = false;
}
}
void StyleCascade::AnalyzeMatchResult() {
int index = 0;
for (const MatchedProperties& properties :
match_result_.GetMatchedProperties()) {
ExpandCascade(
properties, GetDocument(), index++,
[this](CascadePriority cascade_priority,
const AtomicString& custom_property_name) {
map_.Add(custom_property_name, cascade_priority);
},
[this](CascadePriority cascade_priority, CSSPropertyID property_id) {
if (kSurrogateProperties.Has(property_id)) {
const CSSProperty& property =
ResolveSurrogate(CSSProperty::Get(property_id));
map_.Add(property.PropertyID(), cascade_priority);
} else {
map_.Add(property_id, cascade_priority);
}
});
}
}
void StyleCascade::AnalyzeInterpolations() {
const auto& entries = interpolations_.GetEntries();
for (wtf_size_t i = 0; i < entries.size(); ++i) {
for (const auto& active_interpolation : *entries[i].map) {
auto name = active_interpolation.key.GetCSSPropertyName();
uint32_t position = EncodeInterpolationPosition(
name.Id(), i, active_interpolation.key.IsPresentationAttribute());
CascadePriority priority(entries[i].origin, false, 0, false, 0, position);
CSSPropertyRef ref(name, GetDocument());
DCHECK(ref.IsValid());
if (name.IsCustomProperty()) {
map_.Add(name.ToAtomicString(), priority);
} else {
const CSSProperty& property = ResolveSurrogate(ref.GetProperty());
map_.Add(property.PropertyID(), priority);
// Since an interpolation for an unvisited property also causes an
// interpolation of the visited property, add the visited property to
// the map as well.
// TODO(crbug.com/1062217): Interpolate visited colors separately
if (const CSSProperty* visited = property.GetVisitedProperty()) {
map_.Add(visited->PropertyID(), priority);
}
}
}
}
}
void StyleCascade::Reanalyze() {
map_.Reset();
generation_ = 0;
depends_on_cascade_affecting_property_ = false;
needs_match_result_analyze_ = true;
needs_interpolations_analyze_ = true;
AnalyzeIfNeeded();
}
void StyleCascade::ApplyCascadeAffecting(CascadeResolver& resolver) {
// During the initial call to Analyze, we speculatively assume that the
// direction/writing-mode inherited from the parent will be the final
// direction/writing-mode. If either property ends up with another value,
// our assumption was incorrect, and we have to Reanalyze with the correct
// values on ComputedStyle.
auto direction = state_.StyleBuilder().Direction();
auto writing_mode = state_.StyleBuilder().GetWritingMode();
if (map_.NativeBitset().Has(CSSPropertyID::kDirection)) {
LookupAndApply(GetCSSPropertyDirection(), resolver);
}
if (map_.NativeBitset().Has(CSSPropertyID::kWritingMode)) {
LookupAndApply(GetCSSPropertyWritingMode(), resolver);
}
if (depends_on_cascade_affecting_property_) {
if (direction != state_.StyleBuilder().Direction() ||
writing_mode != state_.StyleBuilder().GetWritingMode()) {
Reanalyze();
}
}
}
void StyleCascade::ApplyHighPriority(CascadeResolver& resolver) {
uint64_t bits = map_.HighPriorityBits();
while (bits) {
int i = base::bits::CountTrailingZeroBits(bits);
bits &= bits - 1; // Clear the lowest bit.
LookupAndApply(CSSProperty::Get(ConvertToCSSPropertyID(i)), resolver);
}
state_.UpdateFont();
}
void StyleCascade::ApplyWideOverlapping(CascadeResolver& resolver) {
// Overlapping properties are handled as follows:
//
// 1. Apply the "wide" longhand which represents the entire computed value
// first. This is not always the non-legacy property,
// e.g.-webkit-border-image is one such longhand.
// 2. For the other overlapping longhands (each of which represent a *part*
// of that computed value), *skip* applying that longhand if the wide
// longhand has a higher priority.
//
// This allows us to always apply the "wide" longhand in a fixed order versus
// the other overlapping longhands, but still produce the same result as if
// everything was applied in the order the properties were specified.
// Skip `property` if its priority is lower than the incoming priority.
// Skipping basically means pretending it's already applied by setting the
// generation.
auto maybe_skip = [this, &resolver](const CSSProperty& property,
CascadePriority priority) {
if (CascadePriority* p = map_.Find(property.GetCSSPropertyName())) {
if (*p < priority) {
*p = CascadePriority(*p, resolver.generation_);
}
}
};
const CSSProperty& webkit_border_image = GetCSSPropertyWebkitBorderImage();
if (!resolver.filter_.Rejects(webkit_border_image)) {
if (const CascadePriority* priority =
map_.Find(webkit_border_image.GetCSSPropertyName())) {
LookupAndApply(webkit_border_image, resolver);
const auto& shorthand = borderImageShorthand();
const CSSProperty** longhands = shorthand.properties();
for (unsigned i = 0; i < shorthand.length(); ++i) {
maybe_skip(*longhands[i], *priority);
}
}
}
const CSSProperty& perspective_origin = GetCSSPropertyPerspectiveOrigin();
if (!resolver.filter_.Rejects(perspective_origin)) {
if (const CascadePriority* priority =
map_.Find(perspective_origin.GetCSSPropertyName())) {
LookupAndApply(perspective_origin, resolver);
maybe_skip(GetCSSPropertyWebkitPerspectiveOriginX(), *priority);
maybe_skip(GetCSSPropertyWebkitPerspectiveOriginY(), *priority);
}
}
const CSSProperty& transform_origin = GetCSSPropertyTransformOrigin();
if (!resolver.filter_.Rejects(transform_origin)) {
if (const CascadePriority* priority =
map_.Find(transform_origin.GetCSSPropertyName())) {
LookupAndApply(transform_origin, resolver);
maybe_skip(GetCSSPropertyWebkitTransformOriginX(), *priority);
maybe_skip(GetCSSPropertyWebkitTransformOriginY(), *priority);
maybe_skip(GetCSSPropertyWebkitTransformOriginZ(), *priority);
}
}
// vertical-align will become a shorthand in the future - in order to
// mitigate the forward compat risk, skip the baseline-source longhand.
const CSSProperty& vertical_align = GetCSSPropertyVerticalAlign();
if (!resolver.filter_.Rejects(vertical_align)) {
if (const CascadePriority* priority =
map_.Find(vertical_align.GetCSSPropertyName())) {
LookupAndApply(vertical_align, resolver);
maybe_skip(GetCSSPropertyBaselineSource(), *priority);
}
}
}
// Go through all properties that were found during the analyze phase
// (e.g. in AnalyzeMatchResult()) and actually apply them. We need to do this
// in a second phase so that we know which ones actually won the cascade
// before we start applying, as some properties can affect others.
void StyleCascade::ApplyMatchResult(CascadeResolver& resolver) {
for (CSSPropertyID id : map_.NativeBitset()) {
CascadePriority* p = map_.FindKnownToExist(id);
const CascadePriority priority = *p;
if (priority.GetGeneration() >= resolver.generation_) {
// Already applied this generation.
// Also checked in LookupAndApplyDeclaration,
// but done here to get a fast exit.
continue;
}
if (IsInterpolation(priority)) {
continue;
}
const CSSProperty& property = CSSProperty::Get(id);
if (resolver.Rejects(property)) {
continue;
}
LookupAndApplyDeclaration(property, p, resolver);
}
for (auto& [name, priority_list] : map_.GetCustomMap()) {
CascadePriority* p = &map_.Top(priority_list);
CascadePriority priority = *p;
if (priority.GetGeneration() >= resolver.generation_) {
continue;
}
if (IsInterpolation(priority)) {
continue;
}
CustomProperty property(name, GetDocument());
if (resolver.Rejects(property)) {
continue;
}
LookupAndApplyDeclaration(property, p, resolver);
}
}
void StyleCascade::ApplyInterpolations(CascadeResolver& resolver) {
const auto& entries = interpolations_.GetEntries();
for (wtf_size_t i = 0; i < entries.size(); ++i) {
const auto& entry = entries[i];
ApplyInterpolationMap(*entry.map, entry.origin, i, resolver);
}
}
void StyleCascade::ApplyInterpolationMap(const ActiveInterpolationsMap& map,
CascadeOrigin origin,
size_t index,
CascadeResolver& resolver) {
for (const auto& entry : map) {
auto name = entry.key.GetCSSPropertyName();
uint32_t position = EncodeInterpolationPosition(
name.Id(), index, entry.key.IsPresentationAttribute());
CascadePriority priority(origin, false, 0, false, 0, position);
priority = CascadePriority(priority, resolver.generation_);
CSSPropertyRef ref(name, GetDocument());
if (resolver.Rejects(ref.GetProperty())) {
continue;
}
const CSSProperty& property = ResolveSurrogate(ref.GetProperty());
CascadePriority* p = map_.Find(property.GetCSSPropertyName());
if (!p || *p >= priority) {
continue;
}
*p = priority;
ApplyInterpolation(property, priority, *entry.value, resolver);
}
}
void StyleCascade::ApplyInterpolation(
const CSSProperty& property,
CascadePriority priority,
const ActiveInterpolations& interpolations,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
CSSInterpolationTypesMap map(state_.GetDocument().GetPropertyRegistry(),
state_.GetDocument());
CSSInterpolationEnvironment environment(map, state_, this, &resolver);
const Interpolation& interpolation = *interpolations.front();
if (IsA<InvalidatableInterpolation>(interpolation)) {
InvalidatableInterpolation::ApplyStack(interpolations, environment);
} else {
To<TransitionInterpolation>(interpolation).Apply(environment);
}
// Applying a color property interpolation will also unconditionally apply
// the -internal-visited- counterpart (see CSSColorInterpolationType::
// ApplyStandardPropertyValue). To make sure !important rules in :visited
// selectors win over animations, we re-apply the -internal-visited property
// if its priority is higher.
//
// TODO(crbug.com/1062217): Interpolate visited colors separately
if (const CSSProperty* visited = property.GetVisitedProperty()) {
CascadePriority* visited_priority =
map_.Find(visited->GetCSSPropertyName());
if (visited_priority && priority < *visited_priority) {
DCHECK(visited_priority->IsImportant());
// Resetting generation to zero makes it possible to apply the
// visited property again.
*visited_priority = CascadePriority(*visited_priority, 0);
LookupAndApply(*visited, resolver);
}
}
}
void StyleCascade::LookupAndApply(const CSSPropertyName& name,
CascadeResolver& resolver) {
CSSPropertyRef ref(name, state_.GetDocument());
DCHECK(ref.IsValid());
LookupAndApply(ref.GetProperty(), resolver);
}
void StyleCascade::LookupAndApply(const CSSProperty& property,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
CSSPropertyName name = property.GetCSSPropertyName();
DCHECK(!resolver.IsLocked(property));
CascadePriority* priority = map_.Find(name);
if (!priority) {
return;
}
if (resolver.Rejects(property)) {
return;
}
LookupAndApplyValue(property, priority, resolver);
}
void StyleCascade::LookupAndApplyValue(const CSSProperty& property,
CascadePriority* priority,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
if (priority->GetOrigin() < CascadeOrigin::kAnimation) {
LookupAndApplyDeclaration(property, priority, resolver);
} else if (priority->GetOrigin() >= CascadeOrigin::kAnimation) {
LookupAndApplyInterpolation(property, priority, resolver);
}
}
void StyleCascade::LookupAndApplyDeclaration(const CSSProperty& property,
CascadePriority* priority,
CascadeResolver& resolver) {
if (priority->GetGeneration() >= resolver.generation_) {
// Already applied this generation.
return;
}
*priority = CascadePriority(*priority, resolver.generation_);
DCHECK(!property.IsSurrogate());
DCHECK(priority->GetOrigin() < CascadeOrigin::kAnimation);
const CSSValue* value = ValueAt(match_result_, priority->GetPosition());
DCHECK(value);
CascadeOrigin origin = priority->GetOrigin();
value = Resolve(property, *value, *priority, origin, resolver);
DCHECK(!value->IsVariableReferenceValue());
DCHECK(!value->IsPendingSubstitutionValue());
const TreeScope* tree_scope{nullptr};
if (origin == CascadeOrigin::kAuthor) {
tree_scope = &TreeScopeAt(match_result_, priority->GetPosition());
} else if (origin == CascadeOrigin::kAuthorPresentationalHint) {
tree_scope = &GetDocument();
}
StyleBuilder::ApplyPhysicalProperty(property, state_,
value->EnsureScopedValue(tree_scope));
}
void StyleCascade::LookupAndApplyInterpolation(const CSSProperty& property,
CascadePriority* priority,
CascadeResolver& resolver) {
if (priority->GetGeneration() >= resolver.generation_) {
// Already applied this generation.
return;
}
*priority = CascadePriority(*priority, resolver.generation_);
DCHECK(!property.IsSurrogate());
// Interpolations for -internal-visited properties are applied via the
// interpolation for the main (unvisited) property, so we don't need to
// apply it twice.
// TODO(crbug.com/1062217): Interpolate visited colors separately
if (property.IsVisited()) {
return;
}
DCHECK(priority->GetOrigin() >= CascadeOrigin::kAnimation);
wtf_size_t index = DecodeInterpolationIndex(priority->GetPosition());
DCHECK_LE(index, interpolations_.GetEntries().size());
const ActiveInterpolationsMap& map = *interpolations_.GetEntries()[index].map;
PropertyHandle handle = ToPropertyHandle(property, *priority);
const auto& entry = map.find(handle);
DCHECK_NE(entry, map.end());
ApplyInterpolation(property, *priority, *entry->value, resolver);
}
bool StyleCascade::IsRootElement() const {
return &state_.GetElement() == state_.GetDocument().documentElement();
}
StyleCascade::TokenSequence::TokenSequence(const CSSVariableData* data)
: is_animation_tainted_(data->IsAnimationTainted()),
has_font_units_(data->HasFontUnits()),
has_root_font_units_(data->HasRootFontUnits()),
has_line_height_units_(data->HasLineHeightUnits()) {}
bool StyleCascade::TokenSequence::AppendFallback(const TokenSequence& sequence,
wtf_size_t byte_limit) {
// https://drafts.csswg.org/css-variables/#long-variables
if (original_text_.length() + sequence.original_text_.length() > byte_limit) {
return false;
}
base::span<const CSSParserToken> other_tokens(sequence.tokens_.begin(),
sequence.tokens_.end());
StringView other_text = sequence.original_text_;
other_text =
CSSVariableParser::StripTrailingWhitespaceAndComments(other_text);
while (!other_tokens.empty() &&
other_tokens.front().GetType() == kWhitespaceToken) {
other_tokens = other_tokens.subspan(1);
}
while (!other_tokens.empty() &&
other_tokens.back().GetType() == kWhitespaceToken) {
other_tokens = other_tokens.first(other_tokens.size() - 1);
}
if (!tokens_.empty() && !other_tokens.empty() &&
NeedsInsertedComment(tokens_.back(), other_tokens.front())) {
original_text_.Append("/**/");
}
tokens_.Append(other_tokens.data(),
static_cast<wtf_size_t>(other_tokens.size()));
original_text_.Append(other_text);
is_animation_tainted_ |= sequence.is_animation_tainted_;
has_font_units_ |= sequence.has_font_units_;
has_root_font_units_ |= sequence.has_root_font_units_;
has_line_height_units_ |= sequence.has_line_height_units_;
return true;
}
void StyleCascade::TokenSequence::StripCommentTokens() {
tokens_.erase(std::remove_if(tokens_.begin(), tokens_.end(),
[](const CSSParserToken& token) {
return token.GetType() == kCommentToken;
}),
tokens_.end());
}
bool StyleCascade::TokenSequence::Append(CSSVariableData* data,
CSSTokenizer* parent_tokenizer,
wtf_size_t byte_limit) {
// https://drafts.csswg.org/css-variables/#long-variables
if (original_text_.length() + data->OriginalText().length() > byte_limit) {
return false;
}
CSSTokenizer tokenizer(data->OriginalText());
const CSSParserToken first_token = tokenizer.TokenizeSingleWithComments();
if (first_token.GetType() != kEOFToken) {
if (!tokens_.empty() && NeedsInsertedComment(tokens_.back(), first_token)) {
original_text_.Append("/**/");
}
tokens_.push_back(first_token);
while (true) {
const CSSParserToken token = tokenizer.TokenizeSingleWithComments();
if (token.GetType() == kEOFToken) {
break;
} else {
tokens_.push_back(token);
}
}
}
original_text_.Append(data->OriginalText());
is_animation_tainted_ |= data->IsAnimationTainted();
has_font_units_ |= data->HasFontUnits();
has_root_font_units_ |= data->HasRootFontUnits();
has_line_height_units_ |= data->HasLineHeightUnits();
if (parent_tokenizer) {
tokenizer.PersistStrings(*parent_tokenizer);
}
return true;
}
void StyleCascade::TokenSequence::Append(const CSSParserToken& token,
StringView original_text) {
CSSVariableData::ExtractFeatures(token, has_font_units_, has_root_font_units_,
has_line_height_units_);
if (!tokens_.empty() && NeedsInsertedComment(tokens_.back(), token)) {
original_text_.Append("/**/");
}
tokens_.push_back(token);
original_text_.Append(original_text);
}
scoped_refptr<CSSVariableData>
StyleCascade::TokenSequence::BuildVariableData() {
int num_tokens_for_ablation =
RuntimeEnabledFeatures::CSSCustomPropertiesAblationEnabled()
? tokens_.size()
: -1;
return CSSVariableData::Create(
original_text_, num_tokens_for_ablation, is_animation_tainted_,
/*needs_variable_resolution=*/false, has_font_units_,
has_root_font_units_, has_line_height_units_);
}
const CSSValue* StyleCascade::Resolve(const CSSProperty& property,
const CSSValue& value,
CascadePriority priority,
CascadeOrigin& origin,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
const CSSValue* result = ResolveSubstitutions(property, value, resolver);
DCHECK(result);
if (result->IsRevertValue()) {
return ResolveRevert(property, *result, origin, resolver);
}
if (result->IsRevertLayerValue()) {
return ResolveRevertLayer(property, *result, priority, origin, resolver);
}
resolver.CollectFlags(property, origin);
return result;
}
const CSSValue* StyleCascade::ResolveSubstitutions(const CSSProperty& property,
const CSSValue& value,
CascadeResolver& resolver) {
if (const auto* v = DynamicTo<CSSCustomPropertyDeclaration>(value)) {
return ResolveCustomProperty(property, *v, resolver);
}
if (const auto* v = DynamicTo<CSSVariableReferenceValue>(value)) {
return ResolveVariableReference(property, *v, resolver);
}
if (const auto* v = DynamicTo<cssvalue::CSSPendingSubstitutionValue>(value)) {
return ResolvePendingSubstitution(property, *v, resolver);
}
return &value;
}
const CSSValue* StyleCascade::ResolveCustomProperty(
const CSSProperty& property,
const CSSCustomPropertyDeclaration& decl,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
DCHECK(!resolver.IsLocked(property));
CascadeResolver::AutoLock lock(property, resolver);
scoped_refptr<CSSVariableData> data = &decl.Value();
if (data->NeedsVariableResolution()) {
data = ResolveVariableData(data.get(), resolver);
}
if (HasFontSizeDependency(To<CustomProperty>(property), data.get())) {
resolver.DetectCycle(GetCSSPropertyFontSize());
}
if (HasLineHeightDependency(To<CustomProperty>(property), data.get())) {
resolver.DetectCycle(GetCSSPropertyLineHeight());
}
state_.StyleBuilder().SetHasVariableDeclaration();
if (resolver.InCycle()) {
return CSSCyclicVariableValue::Create();
}
if (!data) {
return CSSInvalidVariableValue::Create();
}
if (data == &decl.Value()) {
return &decl;
}
// If a declaration, once all var() functions are substituted in, contains
// only a CSS-wide keyword (and possibly whitespace), its value is determined
// as if that keyword were its specified value all along.
//
// https://drafts.csswg.org/css-variables/#substitute-a-var
{
CSSTokenizer tokenizer(data->OriginalText());
Vector<CSSParserToken, 32> tokens = tokenizer.TokenizeToEOF();
CSSParserTokenRange range(tokens);
range.ConsumeWhitespace();
CSSValue* value = css_parsing_utils::ConsumeCSSWideKeyword(range);
if (value && range.AtEnd()) {
return value;
}
}
return MakeGarbageCollected<CSSCustomPropertyDeclaration>(
data, decl.ParserContext());
}
const CSSValue* StyleCascade::ResolveVariableReference(
const CSSProperty& property,
const CSSVariableReferenceValue& value,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
DCHECK(!resolver.IsLocked(property));
CascadeResolver::AutoLock lock(property, resolver);
const CSSVariableData* data = value.VariableDataValue();
const CSSParserContext* context = GetParserContext(value);
MarkHasVariableReference(property);
DCHECK(data);
DCHECK(context);
TokenSequence sequence;
CSSTokenizer tokenizer(data->OriginalText());
CSSParserTokenStream stream(tokenizer);
if (ResolveTokensInto(stream, resolver, &tokenizer, sequence)) {
sequence.StripCommentTokens();
if (const auto* parsed = Parse(property, sequence.TokenRange(), context)) {
return parsed;
}
}
return cssvalue::CSSUnsetValue::Create();
}
const CSSValue* StyleCascade::ResolvePendingSubstitution(
const CSSProperty& property,
const cssvalue::CSSPendingSubstitutionValue& value,
CascadeResolver& resolver) {
DCHECK(!property.IsSurrogate());
DCHECK(!resolver.IsLocked(property));
CascadeResolver::AutoLock lock(property, resolver);
CascadePriority priority = map_.At(property.GetCSSPropertyName());
DCHECK_NE(property.PropertyID(), CSSPropertyID::kVariable);
DCHECK_NE(priority.GetOrigin(), CascadeOrigin::kNone);
MarkHasVariableReference(property);