-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
style_adjuster.cc
1117 lines (999 loc) · 44.8 KB
/
style_adjuster.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 (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc.
* All rights reserved.
* Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
* Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
* (http://www.torchmobile.com/)
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/core/css/resolver/style_adjuster.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/container_node.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/pseudo_element.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/frame/event_handler_registry.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/fullscreen/fullscreen.h"
#include "third_party/blink/renderer/core/html/fenced_frame/html_fenced_frame_element.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/core/html/forms/html_text_area_element.h"
#include "third_party/blink/renderer/core/html/html_body_element.h"
#include "third_party/blink/renderer/core/html/html_dialog_element.h"
#include "third_party/blink/renderer/core/html/html_iframe_element.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/html/html_plugin_element.h"
#include "third_party/blink/renderer/core/html/html_table_cell_element.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/shadow/shadow_element_names.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/input_type_names.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/layout/list_marker.h"
#include "third_party/blink/renderer/core/layout/ng/inline/layout_ng_text_combine.h"
#include "third_party/blink/renderer/core/mathml/mathml_element.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/style/computed_style_constants.h"
#include "third_party/blink/renderer/core/style/style_intrinsic_length.h"
#include "third_party/blink/renderer/core/svg/svg_svg_element.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/platform/geometry/length.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/transforms/transform_operations.h"
#include "ui/base/ui_base_features.h"
namespace blink {
namespace {
bool IsOverflowClipOrVisible(EOverflow overflow) {
return overflow == EOverflow::kClip || overflow == EOverflow::kVisible;
}
TouchAction AdjustTouchActionForElement(TouchAction touch_action,
const ComputedStyleBuilder& builder,
const ComputedStyle& parent_style,
Element* element) {
Element* document_element = element->GetDocument().documentElement();
bool scrolls_overflow = builder.ScrollsOverflow();
if (element && element == element->GetDocument().FirstBodyElement()) {
// Body scrolls overflow if html root overflow is not visible or the
// propagation of overflow is stopped by containment.
if (parent_style.IsOverflowVisibleAlongBothAxes()) {
if (!parent_style.ShouldApplyAnyContainment(*document_element) &&
!builder.InternalStyle()->ShouldApplyAnyContainment(*element)) {
scrolls_overflow = false;
}
}
}
bool is_child_document = element && element == document_element &&
element->GetDocument().LocalOwner();
if (scrolls_overflow || is_child_document) {
return touch_action | TouchAction::kPan |
TouchAction::kInternalPanXScrolls |
TouchAction::kInternalNotWritable;
}
return touch_action;
}
bool HostIsInputFile(const Element* element) {
if (!element || !element->IsInUserAgentShadowRoot()) {
return false;
}
if (const Element* shadow_host = element->OwnerShadowHost()) {
if (const auto* input = DynamicTo<HTMLInputElement>(shadow_host)) {
return input->type() == input_type_names::kFile;
}
}
return false;
}
void AdjustStyleForSvgElement(const SVGElement& element,
ComputedStyleBuilder& builder) {
// Disable some of text decoration properties.
//
// Note that SetFooBar() is more efficient than ResetFooBar() if the current
// value is same as the reset value.
builder.SetTextDecorationSkipInk(ETextDecorationSkipInk::kAuto);
builder.SetTextDecorationStyle(
ETextDecorationStyle::kSolid); // crbug.com/1246719
builder.SetTextDecorationThickness(TextDecorationThickness(Length::Auto()));
builder.SetTextEmphasisMark(TextEmphasisMark::kNone);
builder.SetTextUnderlineOffset(Length()); // crbug.com/1247912
builder.SetTextUnderlinePosition(kTextUnderlinePositionAuto);
}
// Adjust style for anchor() and anchor-size() queries.
void AdjustAnchorQueryStyles(ComputedStyleBuilder& builder) {
if (!RuntimeEnabledFeatures::CSSAnchorPositioningEnabled()) {
return;
}
// anchor() and anchor-size() can only be used on absolutely positioned
// elements.
EPosition position = builder.GetPosition();
if (position != EPosition::kAbsolute && position != EPosition::kFixed) {
if (builder.Left().HasAnchorQueries()) {
builder.SetLeft(Length::Auto());
}
if (builder.Right().HasAnchorQueries()) {
builder.SetRight(Length::Auto());
}
if (builder.Top().HasAnchorQueries()) {
builder.SetTop(Length::Auto());
}
if (builder.Bottom().HasAnchorQueries()) {
builder.SetBottom(Length::Auto());
}
if (builder.Width().HasAnchorQueries()) {
builder.SetWidth(Length::Auto());
}
if (builder.MinWidth().HasAnchorQueries()) {
builder.SetMinWidth(Length::Auto());
}
if (builder.MaxWidth().HasAnchorQueries()) {
builder.SetMaxWidth(Length::Auto());
}
if (builder.Height().HasAnchorQueries()) {
builder.SetHeight(Length::Auto());
}
if (builder.MinHeight().HasAnchorQueries()) {
builder.SetMinHeight(Length::Auto());
}
if (builder.MaxHeight().HasAnchorQueries()) {
builder.SetMaxHeight(Length::Auto());
}
}
}
bool ElementForcesStackingContext(Element* element) {
if (!element) {
return false;
}
if (element == element->GetDocument().documentElement()) {
return true;
}
if (element->IsInTopLayer()) {
return true;
}
if (IsA<SVGForeignObjectElement>(*element)) {
return true;
}
return false;
}
} // namespace
static EDisplay EquivalentBlockDisplay(EDisplay display) {
switch (display) {
case EDisplay::kBlock:
case EDisplay::kTable:
case EDisplay::kWebkitBox:
case EDisplay::kFlex:
case EDisplay::kGrid:
case EDisplay::kBlockMath:
case EDisplay::kListItem:
case EDisplay::kFlowRoot:
case EDisplay::kLayoutCustom:
return display;
case EDisplay::kInlineTable:
return EDisplay::kTable;
case EDisplay::kWebkitInlineBox:
return EDisplay::kWebkitBox;
case EDisplay::kInlineFlex:
return EDisplay::kFlex;
case EDisplay::kInlineGrid:
return EDisplay::kGrid;
case EDisplay::kMath:
return EDisplay::kBlockMath;
case EDisplay::kInlineLayoutCustom:
return EDisplay::kLayoutCustom;
case EDisplay::kContents:
case EDisplay::kInline:
case EDisplay::kInlineBlock:
case EDisplay::kTableRowGroup:
case EDisplay::kTableHeaderGroup:
case EDisplay::kTableFooterGroup:
case EDisplay::kTableRow:
case EDisplay::kTableColumnGroup:
case EDisplay::kTableColumn:
case EDisplay::kTableCell:
case EDisplay::kTableCaption:
return EDisplay::kBlock;
case EDisplay::kNone:
NOTREACHED();
return display;
}
NOTREACHED();
return EDisplay::kBlock;
}
static bool IsOutermostSVGElement(const Element* element) {
auto* svg_element = DynamicTo<SVGElement>(element);
return svg_element && svg_element->IsOutermostSVGSVGElement();
}
static bool IsAtMediaUAShadowBoundary(const Element* element) {
if (!element) {
return false;
}
if (ContainerNode* parent = element->parentNode()) {
if (auto* shadow_root = DynamicTo<ShadowRoot>(parent)) {
return shadow_root->host().IsMediaElement();
}
}
return false;
}
// CSS requires text-decoration to be reset at each DOM element for inline
// blocks, inline tables, floating elements, and absolute or relatively
// positioned elements. Outermost <svg> roots are considered to be atomic
// inline-level. Media elements have a special rendering where the media
// controls do not use a proper containing block model which means we need
// to manually stop text-decorations to apply to text inside media controls.
static bool StopPropagateTextDecorations(const ComputedStyleBuilder& builder,
const Element* element) {
return builder.Display() == EDisplay::kInlineTable ||
builder.Display() == EDisplay::kInlineBlock ||
builder.Display() == EDisplay::kWebkitInlineBox ||
IsAtMediaUAShadowBoundary(element) || builder.IsFloating() ||
builder.HasOutOfFlowPosition() || IsOutermostSVGElement(element) ||
IsA<HTMLRTElement>(element);
}
// Certain elements (<a>, <font>) override text decoration colors. "The font
// element is expected to override the color of any text decoration that spans
// the text of the element to the used value of the element's 'color' property."
// (https://html.spec.whatwg.org/C/#phrasing-content-3)
// The <a> behavior is non-standard.
static bool OverridesTextDecorationColors(const Element* element) {
return !RuntimeEnabledFeatures::DisableTextDecorationColorOverrideEnabled() &&
element &&
(IsA<HTMLFontElement>(element) || IsA<HTMLAnchorElement>(element));
}
// FIXME: This helper is only needed because ResolveStyle passes a null
// element to AdjustComputedStyle for pseudo-element styles, so we can't just
// use element->isInTopLayer().
static bool IsInTopLayer(const Element* element,
const ComputedStyleBuilder& builder) {
return (element && element->IsInTopLayer()) ||
builder.StyleType() == kPseudoIdBackdrop;
}
static bool LayoutParentStyleForcesZIndexToCreateStackingContext(
const ComputedStyle& layout_parent_style) {
return layout_parent_style.IsDisplayFlexibleOrGridBox();
}
void StyleAdjuster::AdjustStyleForEditing(ComputedStyleBuilder& builder) {
if (builder.UserModify() != EUserModify::kReadWritePlaintextOnly) {
return;
}
// Collapsing whitespace is harmful in plain-text editing.
if (builder.WhiteSpace() == EWhiteSpace::kNormal) {
builder.SetWhiteSpace(EWhiteSpace::kPreWrap);
} else if (builder.WhiteSpace() == EWhiteSpace::kNowrap) {
builder.SetWhiteSpace(EWhiteSpace::kPre);
} else if (builder.WhiteSpace() == EWhiteSpace::kPreLine) {
builder.SetWhiteSpace(EWhiteSpace::kPreWrap);
}
}
void StyleAdjuster::AdjustStyleForTextCombine(ComputedStyleBuilder& builder) {
DCHECK_EQ(builder.Display(), EDisplay::kInlineBlock);
// Set box sizes
const Font& font = builder.GetFont();
DCHECK(font.GetFontDescription().IsVerticalBaseline());
const auto one_em = ComputedStyle::ComputedFontSizeAsFixed(builder.GetFont());
const auto line_height = builder.FontHeight();
const auto size =
LengthSize(Length::Fixed(line_height), Length::Fixed(one_em));
builder.SetContainIntrinsicWidth(StyleIntrinsicLength(false, size.Width()));
builder.SetContainIntrinsicHeight(StyleIntrinsicLength(false, size.Height()));
builder.SetHeight(size.Height());
builder.SetLineHeight(size.Height());
builder.SetMaxHeight(size.Height());
builder.SetMaxWidth(size.Width());
builder.SetMinHeight(size.Height());
builder.SetMinWidth(size.Width());
builder.SetWidth(size.Width());
AdjustStyleForCombinedText(builder);
}
void StyleAdjuster::AdjustStyleForCombinedText(ComputedStyleBuilder& builder) {
builder.ResetTextCombine();
builder.SetLetterSpacing(0.0f);
builder.SetTextAlign(ETextAlign::kCenter);
builder.SetTextDecorationLine(TextDecorationLine::kNone);
builder.SetTextEmphasisMark(TextEmphasisMark::kNone);
builder.SetVerticalAlign(EVerticalAlign::kMiddle);
builder.SetWordBreak(EWordBreak::kKeepAll);
builder.SetWordSpacing(0.0f);
builder.SetWritingMode(WritingMode::kHorizontalTb);
builder.ClearAppliedTextDecorations();
builder.ResetTextIndent();
builder.UpdateFontOrientation();
#if DCHECK_IS_ON()
DCHECK_EQ(builder.GetFont().GetFontDescription().Orientation(),
FontOrientation::kHorizontal);
scoped_refptr<const ComputedStyle> cloned_style = builder.CloneStyle();
LayoutNGTextCombine::AssertStyleIsValid(*cloned_style);
#endif
}
static void AdjustStyleForFirstLetter(ComputedStyleBuilder& builder) {
if (builder.StyleType() != kPseudoIdFirstLetter) {
return;
}
// Force inline display (except for floating first-letters).
builder.SetDisplay(builder.IsFloating() ? EDisplay::kBlock
: EDisplay::kInline);
}
static void AdjustStyleForMarker(ComputedStyleBuilder& builder,
const ComputedStyle& parent_style,
const Element& parent_element) {
if (builder.StyleType() != kPseudoIdMarker) {
return;
}
bool is_inside =
parent_style.ListStylePosition() == EListStylePosition::kInside ||
(IsA<HTMLLIElement>(parent_element) &&
!parent_style.IsInsideListElement());
if (is_inside) {
Document& document = parent_element.GetDocument();
auto margins = ListMarker::InlineMarginsForInside(
document, *builder.InternalStyle(), parent_style);
LogicalToPhysicalSetter setter(builder.GetWritingDirection(), builder,
&ComputedStyleBuilder::SetMarginTop,
&ComputedStyleBuilder::SetMarginRight,
&ComputedStyleBuilder::SetMarginBottom,
&ComputedStyleBuilder::SetMarginLeft);
setter.SetInlineStart(Length::Fixed(margins.first));
setter.SetInlineEnd(Length::Fixed(margins.second));
} else {
// Outside list markers should generate a block container.
builder.SetDisplay(EDisplay::kInlineBlock);
// Do not break inside the marker, and honor the trailing spaces.
builder.SetWhiteSpace(EWhiteSpace::kPre);
// Compute margins for 'outside' during layout, because it requires the
// layout size of the marker.
// TODO(kojii): absolute position looks more reasonable, and maybe required
// in some cases, but this is currently blocked by crbug.com/734554
// builder.SetPosition(EPosition::kAbsolute);
}
}
static void AdjustStyleForHTMLElement(ComputedStyleBuilder& builder,
HTMLElement& element) {
// <div> and <span> are the most common elements on the web, we skip all the
// work for them.
if (IsA<HTMLDivElement>(element) || IsA<HTMLSpanElement>(element)) {
return;
}
if (auto* image = DynamicTo<HTMLImageElement>(element)) {
if (image->IsCollapsed() || builder.Display() == EDisplay::kContents) {
builder.SetDisplay(EDisplay::kNone);
}
return;
}
if (IsA<HTMLTableElement>(element)) {
// Tables never support the -webkit-* values for text-align and will reset
// back to the default.
if (builder.GetTextAlign() == ETextAlign::kWebkitLeft ||
builder.GetTextAlign() == ETextAlign::kWebkitCenter ||
builder.GetTextAlign() == ETextAlign::kWebkitRight) {
builder.SetTextAlign(ETextAlign::kStart);
}
return;
}
if (IsA<HTMLFrameElement>(element) || IsA<HTMLFrameSetElement>(element)) {
// Frames and framesets never honor position:relative or position:absolute.
// This is necessary to fix a crash where a site tries to position these
// objects. They also never honor display nor floating.
builder.SetPosition(EPosition::kStatic);
builder.SetDisplay(EDisplay::kBlock);
builder.SetFloating(EFloat::kNone);
return;
}
if (IsA<HTMLFrameElementBase>(element)) {
if (builder.Display() == EDisplay::kContents) {
builder.SetDisplay(EDisplay::kNone);
return;
}
return;
}
if (IsA<HTMLFencedFrameElement>(element)) {
// Force the CSS style `zoom` property to 1 so that the embedder cannot
// communicate into the fenced frame by adjusting it, but still include
// the page zoom factor in the effective zoom, which is safe because it
// comes from user intervention. crbug.com/1285327
builder.SetEffectiveZoom(
element.GetDocument().GetStyleResolver().InitialZoom());
}
if (IsA<HTMLRTElement>(element)) {
// Ruby text does not support float or position. This might change with
// evolution of the specification.
builder.SetPosition(EPosition::kStatic);
builder.SetFloating(EFloat::kNone);
return;
}
if (IsA<HTMLLegendElement>(element) &&
builder.Display() != EDisplay::kContents) {
// Allow any blockified display value for legends. Note that according to
// the spec, this shouldn't affect computed style (like we do here).
// Instead, the display override should be determined during box creation,
// and even then only be applied to the rendered legend inside a
// fieldset. However, Blink determines the rendered legend during layout
// instead of during layout object creation, and also generally makes
// assumptions that the computed display value is the one to use.
builder.SetDisplay(EquivalentBlockDisplay(builder.Display()));
return;
}
if (IsA<HTMLMarqueeElement>(element)) {
// For now, <marquee> requires an overflow clip to work properly.
builder.SetOverflowX(EOverflow::kHidden);
builder.SetOverflowY(EOverflow::kHidden);
return;
}
if (IsA<HTMLTextAreaElement>(element)) {
// Textarea considers overflow visible as auto.
builder.SetOverflowX(builder.OverflowX() == EOverflow::kVisible
? EOverflow::kAuto
: builder.OverflowX());
builder.SetOverflowY(builder.OverflowY() == EOverflow::kVisible
? EOverflow::kAuto
: builder.OverflowY());
if (builder.Display() == EDisplay::kContents) {
builder.SetDisplay(EDisplay::kNone);
}
return;
}
if (auto* html_plugin_element = DynamicTo<HTMLPlugInElement>(element)) {
builder.SetRequiresAcceleratedCompositingForExternalReasons(
html_plugin_element->ShouldAccelerate());
if (builder.Display() == EDisplay::kContents) {
builder.SetDisplay(EDisplay::kNone);
}
return;
}
if (IsA<HTMLUListElement>(element) || IsA<HTMLOListElement>(element)) {
builder.SetIsInsideListElement();
return;
}
if (builder.Display() == EDisplay::kContents) {
// See https://drafts.csswg.org/css-display/#unbox-html
// Some of these elements are handled with other adjustments above.
if (IsA<HTMLBRElement>(element) || IsA<HTMLWBRElement>(element) ||
IsA<HTMLMeterElement>(element) || IsA<HTMLProgressElement>(element) ||
IsA<HTMLCanvasElement>(element) || IsA<HTMLMediaElement>(element) ||
IsA<HTMLInputElement>(element) || IsA<HTMLTextAreaElement>(element) ||
IsA<HTMLSelectElement>(element)) {
builder.SetDisplay(EDisplay::kNone);
}
}
if (IsA<HTMLBodyElement>(element) &&
element.GetDocument().FirstBodyElement() != element) {
builder.SetIsSecondaryBodyElement();
}
}
void StyleAdjuster::AdjustOverflow(ComputedStyleBuilder& builder,
Element* element) {
DCHECK(builder.OverflowX() != EOverflow::kVisible ||
builder.OverflowY() != EOverflow::kVisible);
bool overflow_is_clip_or_visible =
IsOverflowClipOrVisible(builder.OverflowY()) &&
IsOverflowClipOrVisible(builder.OverflowX());
if (!overflow_is_clip_or_visible && builder.IsDisplayTableBox()) {
// Tables only support overflow:hidden and overflow:visible and ignore
// anything else, see https://drafts.csswg.org/css2/visufx.html#overflow. As
// a table is not a block container box the rules for resolving conflicting
// x and y values in CSS Overflow Module Level 3 do not apply. Arguably
// overflow-x and overflow-y aren't allowed on tables but all UAs allow it.
if (builder.OverflowX() != EOverflow::kHidden) {
builder.SetOverflowX(EOverflow::kVisible);
}
if (builder.OverflowY() != EOverflow::kHidden) {
builder.SetOverflowY(EOverflow::kVisible);
}
// If we are left with conflicting overflow values for the x and y axes on a
// table then resolve both to OverflowVisible. This is interoperable
// behaviour but is not specced anywhere.
if (builder.OverflowX() == EOverflow::kVisible) {
builder.SetOverflowY(EOverflow::kVisible);
} else if (builder.OverflowY() == EOverflow::kVisible) {
builder.SetOverflowX(EOverflow::kVisible);
}
} else if (!IsOverflowClipOrVisible(builder.OverflowY())) {
// Values of 'clip' and 'visible' can only be used with 'clip' and
// 'visible.' If they aren't, 'clip' and 'visible' is reset.
if (builder.OverflowX() == EOverflow::kVisible) {
builder.SetOverflowX(EOverflow::kAuto);
} else if (builder.OverflowX() == EOverflow::kClip) {
builder.SetOverflowX(EOverflow::kHidden);
}
} else if (!IsOverflowClipOrVisible(builder.OverflowX())) {
// Values of 'clip' and 'visible' can only be used with 'clip' and
// 'visible.' If they aren't, 'clip' and 'visible' is reset.
if (builder.OverflowY() == EOverflow::kVisible) {
builder.SetOverflowY(EOverflow::kAuto);
} else if (builder.OverflowY() == EOverflow::kClip) {
builder.SetOverflowY(EOverflow::kHidden);
}
}
if (element && !element->IsPseudoElement() &&
(builder.OverflowX() == EOverflow::kClip ||
builder.OverflowY() == EOverflow::kClip)) {
UseCounter::Count(element->GetDocument(),
WebFeature::kOverflowClipAlongEitherAxis);
}
}
static void AdjustStyleForDisplay(ComputedStyleBuilder& builder,
const ComputedStyle& layout_parent_style,
const Element* element,
Document* document) {
// Blockify the children of flex, grid, math or LayoutCustom containers.
if (layout_parent_style.BlockifiesChildren() && !HostIsInputFile(element)) {
builder.SetIsInBlockifyingDisplay();
if (builder.Display() != EDisplay::kContents) {
builder.SetDisplay(EquivalentBlockDisplay(builder.Display()));
if (!builder.HasOutOfFlowPosition()) {
builder.SetIsFlexOrGridOrCustomItem();
}
}
if (layout_parent_style.IsDisplayFlexibleOrGridBox() ||
layout_parent_style.IsDisplayMathType()) {
builder.SetIsInsideDisplayIgnoringFloatingChildren();
}
}
if (builder.Display() == EDisplay::kBlock) {
return;
}
// FIXME: Don't support this mutation for pseudo styles like first-letter or
// first-line, since it's not completely clear how that should work.
if (builder.Display() == EDisplay::kInline &&
builder.StyleType() == kPseudoIdNone &&
builder.GetWritingMode() != layout_parent_style.GetWritingMode()) {
builder.SetDisplay(EDisplay::kInlineBlock);
}
// writing-mode does not apply to table row groups, table column groups, table
// rows, and table columns.
// TODO(crbug.com/736072): Borders specified with logical css properties will
// not change to reflect new writing mode. ex: border-block-start.
if (builder.Display() == EDisplay::kTableColumn ||
builder.Display() == EDisplay::kTableColumnGroup ||
builder.Display() == EDisplay::kTableFooterGroup ||
builder.Display() == EDisplay::kTableHeaderGroup ||
builder.Display() == EDisplay::kTableRow ||
builder.Display() == EDisplay::kTableRowGroup) {
builder.SetWritingMode(layout_parent_style.GetWritingMode());
builder.SetTextOrientation(layout_parent_style.GetTextOrientation());
builder.UpdateFontOrientation();
}
}
bool StyleAdjuster::IsEditableElement(Element* element,
const ComputedStyleBuilder& builder) {
if (builder.UserModify() != EUserModify::kReadOnly) {
return true;
}
if (!element) {
return false;
}
if (auto* textarea = DynamicTo<HTMLTextAreaElement>(*element)) {
return !textarea->IsDisabledOrReadOnly();
}
if (auto* input = DynamicTo<HTMLInputElement>(*element)) {
return !input->IsDisabledOrReadOnly() && input->IsTextField();
}
return false;
}
bool StyleAdjuster::IsPasswordFieldWithUnrevealedPassword(Element* element) {
if (!element) {
return false;
}
if (auto* input = DynamicTo<HTMLInputElement>(element)) {
return (input->type() == input_type_names::kPassword) &&
!input->ShouldRevealPassword();
}
return false;
}
void StyleAdjuster::AdjustEffectiveTouchAction(
ComputedStyleBuilder& builder,
const ComputedStyle& parent_style,
Element* element,
bool is_svg_root) {
TouchAction inherited_action = parent_style.EffectiveTouchAction();
bool is_replaced_canvas = element && IsA<HTMLCanvasElement>(element) &&
element->GetExecutionContext() &&
element->GetExecutionContext()->CanExecuteScripts(
kNotAboutToExecuteScript);
bool is_non_replaced_inline_elements =
builder.IsDisplayInlineType() &&
!(builder.IsDisplayReplacedType() || is_svg_root ||
IsA<HTMLImageElement>(element) || is_replaced_canvas);
bool is_table_row_or_column = builder.IsDisplayTableRowOrColumnType();
bool is_layout_object_needed =
element && element->LayoutObjectIsNeeded(builder.GetDisplayStyle());
TouchAction element_touch_action = TouchAction::kAuto;
// Touch actions are only supported by elements that support both the CSS
// width and height properties.
// See https://www.w3.org/TR/pointerevents/#the-touch-action-css-property.
if (!is_non_replaced_inline_elements && !is_table_row_or_column &&
is_layout_object_needed) {
element_touch_action = builder.GetTouchAction();
// kInternalPanXScrolls is only for internal usage, GetTouchAction()
// doesn't contain this bit. We set this bit when kPanX is set so it can be
// cleared for eligible editable areas later on.
if ((element_touch_action & TouchAction::kPanX) != TouchAction::kNone) {
element_touch_action |= TouchAction::kInternalPanXScrolls;
}
// kInternalNotWritable is only for internal usage, GetTouchAction()
// doesn't contain this bit. We set this bit when kPan is set so it can be
// cleared for eligible non-password editable areas later on.
if ((element_touch_action & TouchAction::kPan) != TouchAction::kNone) {
element_touch_action |= TouchAction::kInternalNotWritable;
}
}
if (!element) {
builder.SetEffectiveTouchAction(element_touch_action & inherited_action);
return;
}
bool is_child_document = element == element->GetDocument().documentElement();
// Apply touch action inherited from parent frame.
if (is_child_document && element->GetDocument().GetFrame()) {
inherited_action &=
TouchAction::kPan | TouchAction::kInternalPanXScrolls |
TouchAction::kInternalNotWritable |
element->GetDocument().GetFrame()->InheritedEffectiveTouchAction();
}
// The effective touch action is the intersection of the touch-action values
// of the current element and all of its ancestors up to the one that
// implements the gesture. Since panning is implemented by the scroller it is
// re-enabled for scrolling elements.
// The panning-restricted cancellation should also apply to iframes, so we
// allow (panning & local touch action) on the first descendant element of a
// iframe element.
inherited_action = AdjustTouchActionForElement(inherited_action, builder,
parent_style, element);
TouchAction enforced_by_policy = TouchAction::kNone;
if (element->GetDocument().IsVerticalScrollEnforced()) {
enforced_by_policy = TouchAction::kPanY;
}
if (::features::IsSwipeToMoveCursorEnabled() &&
IsEditableElement(element, builder)) {
element_touch_action &= ~TouchAction::kInternalPanXScrolls;
}
// TODO(crbug.com/1346169): Full style invalidation is needed when this
// feature status changes at runtime as it affects the computed style.
if (base::FeatureList::IsEnabled(blink::features::kStylusWritingToInput) &&
RuntimeEnabledFeatures::StylusHandwritingEnabled() &&
(element_touch_action & TouchAction::kPan) == TouchAction::kPan &&
IsEditableElement(element, builder) &&
!IsPasswordFieldWithUnrevealedPassword(element)) {
element_touch_action &= ~TouchAction::kInternalNotWritable;
}
// Apply the adjusted parent effective touch actions.
builder.SetEffectiveTouchAction((element_touch_action & inherited_action) |
enforced_by_policy);
// Propagate touch action to child frames.
if (auto* frame_owner = DynamicTo<HTMLFrameOwnerElement>(element)) {
Frame* content_frame = frame_owner->ContentFrame();
if (content_frame) {
content_frame->SetInheritedEffectiveTouchAction(
builder.EffectiveTouchAction());
}
}
}
static void AdjustStyleForInert(ComputedStyleBuilder& builder,
Element* element) {
if (!element) {
return;
}
if (element->IsInertRoot()) {
builder.SetIsInert(true);
builder.SetIsInertIsInherited(false);
return;
}
Document& document = element->GetDocument();
const Element* modal_element = document.ActiveModalDialog();
if (!modal_element) {
modal_element = Fullscreen::FullscreenElementFrom(document);
}
if (modal_element == element) {
builder.SetIsInert(false);
builder.SetIsInertIsInherited(false);
return;
}
if (modal_element && element == document.documentElement()) {
builder.SetIsInert(true);
builder.SetIsInertIsInherited(false);
return;
}
}
void StyleAdjuster::AdjustForForcedColorsMode(ComputedStyleBuilder& builder) {
if (!builder.InForcedColorsMode() ||
builder.ForcedColorAdjust() != EForcedColorAdjust::kAuto) {
return;
}
builder.SetTextShadow(ComputedStyleInitialValues::InitialTextShadow());
builder.SetBoxShadow(ComputedStyleInitialValues::InitialBoxShadow());
builder.SetColorScheme({"light", "dark"});
if (builder.ShouldForceColor(builder.AccentColor())) {
builder.SetAccentColor(ComputedStyleInitialValues::InitialAccentColor());
}
if (!builder.HasUrlBackgroundImage()) {
builder.ClearBackgroundImage();
}
}
void StyleAdjuster::AdjustForSVGTextElement(ComputedStyleBuilder& builder) {
builder.SetColumnGap(ComputedStyleInitialValues::InitialColumnGap());
builder.SetColumnWidthInternal(
ComputedStyleInitialValues::InitialColumnWidth());
builder.SetColumnRuleStyle(
ComputedStyleInitialValues::InitialColumnRuleStyle());
builder.SetColumnRuleWidthInternal(
LayoutUnit(ComputedStyleInitialValues::InitialColumnRuleWidth()));
builder.SetColumnRuleColor(
ComputedStyleInitialValues::InitialColumnRuleColor());
builder.SetInternalVisitedColumnRuleColor(
ComputedStyleInitialValues::InitialInternalVisitedColumnRuleColor());
builder.SetColumnCountInternal(
ComputedStyleInitialValues::InitialColumnCount());
builder.SetHasAutoColumnCountInternal(
ComputedStyleInitialValues::InitialHasAutoColumnCount());
builder.SetHasAutoColumnWidthInternal(
ComputedStyleInitialValues::InitialHasAutoColumnWidth());
builder.ResetColumnFill();
builder.ResetColumnSpan();
}
void StyleAdjuster::AdjustComputedStyle(StyleResolverState& state,
Element* element) {
DCHECK(state.LayoutParentStyle());
DCHECK(state.ParentStyle());
ComputedStyleBuilder& builder = state.StyleBuilder();
const ComputedStyle& parent_style = *state.ParentStyle();
const ComputedStyle& layout_parent_style = *state.LayoutParentStyle();
auto* html_element = DynamicTo<HTMLElement>(element);
if (html_element &&
(builder.Display() != EDisplay::kNone ||
element->LayoutObjectIsNeeded(builder.GetDisplayStyle()))) {
AdjustStyleForHTMLElement(builder, *html_element);
}
auto* svg_element = DynamicTo<SVGElement>(element);
bool is_mathml_element = RuntimeEnabledFeatures::MathMLCoreEnabled() &&
IsA<MathMLElement>(element);
if (builder.Display() != EDisplay::kNone) {
if (svg_element) {
AdjustStyleForSvgElement(*svg_element, builder);
}
bool is_document_element =
element && element->GetDocument().documentElement() == element;
// Per the spec, position 'static' and 'relative' in the top layer compute
// to 'absolute'. Root elements that are in the top layer should just
// be left alone because the fullscreen.css doesn't apply any style to
// them.
if (IsInTopLayer(element, builder) && !is_document_element) {
if (builder.GetPosition() == EPosition::kStatic ||
builder.GetPosition() == EPosition::kRelative) {
builder.SetPosition(EPosition::kAbsolute);
}
if (builder.Display() == EDisplay::kContents) {
// See crbug.com/1240701 for more details.
// https://fullscreen.spec.whatwg.org/#new-stacking-layer
// If its specified display property is contents, it computes to block.
builder.SetDisplay(EDisplay::kBlock);
}
}
// Absolute/fixed positioned elements, floating elements and the document
// element need block-like outside display.
if (builder.Display() != EDisplay::kContents &&
(builder.HasOutOfFlowPosition() || builder.IsFloating())) {
builder.SetDisplay(EquivalentBlockDisplay(builder.Display()));
}
if (is_document_element) {
builder.SetDisplay(EquivalentBlockDisplay(builder.Display()));
}
// math display values on non-MathML elements compute to flow display
// values.
if ((!element || !is_mathml_element) && builder.IsDisplayMathType()) {
DCHECK(RuntimeEnabledFeatures::MathMLCoreEnabled());
builder.SetDisplay(builder.Display() == EDisplay::kBlockMath
? EDisplay::kBlock
: EDisplay::kInline);
}
// We don't adjust the first letter style earlier because we may change the
// display setting in AdjustStyleForHTMLElement() above.
AdjustStyleForFirstLetter(builder);
AdjustStyleForMarker(builder, parent_style, state.GetElement());
AdjustStyleForDisplay(builder, layout_parent_style, element,
element ? &element->GetDocument() : nullptr);
// If this is a child of a LayoutNGCustom, we need the name of the parent
// layout function for invalidation purposes.
if (layout_parent_style.IsDisplayLayoutCustomBox()) {
builder.SetDisplayLayoutCustomParentName(
layout_parent_style.DisplayLayoutCustomName());
}
bool is_in_main_frame = element && element->GetDocument().IsInMainFrame();
// The root element of the main frame has no backdrop, so don't allow
// it to have a backdrop filter either.
if (is_document_element && is_in_main_frame &&
builder.HasBackdropFilter()) {
builder.MutableBackdropFilter().clear();
}
} else {
AdjustStyleForFirstLetter(builder);
}
builder.SetForcesStackingContext(false);
// Make sure our z-index value is only applied if the object is positioned.
if (!builder.HasAutoZIndex()) {
if (builder.GetPosition() == EPosition::kStatic &&
!LayoutParentStyleForcesZIndexToCreateStackingContext(
layout_parent_style)) {
builder.SetEffectiveZIndexZero(true);
} else {
builder.SetForcesStackingContext(true);
}
}
if (ElementForcesStackingContext(element)) {
builder.SetForcesStackingContext(true);
}
if (builder.OverflowX() != EOverflow::kVisible ||
builder.OverflowY() != EOverflow::kVisible) {
AdjustOverflow(builder, element ? element : state.GetPseudoElement());
}
// Highlight pseudos propagate decorations with inheritance only.
if (StopPropagateTextDecorations(builder, element) ||
state.IsForHighlight()) {
builder.ClearAppliedTextDecorations();
} else {
builder.RestoreParentTextDecorations(layout_parent_style);
}
// The computed value of currentColor for highlight pseudos is the
// color that would have been used if no highlights were applied,
// i.e. the originating element's color.
if (state.UsesHighlightPseudoInheritance() &&
state.OriginatingElementStyle()) {
const ComputedStyle* originating_style = state.OriginatingElementStyle();
if (builder.ColorIsCurrentColor()) {
builder.SetColor(originating_style->Color());
}
if (builder.InternalVisitedColorIsCurrentColor()) {
builder.SetInternalVisitedColor(
originating_style->InternalVisitedColor());
}
}
if (builder.Display() != EDisplay::kContents) {
builder.ApplyTextDecorations(parent_style.VisitedDependentColorFast(
GetCSSPropertyTextDecorationColor()),
OverridesTextDecorationColors(element));
}
// Cull out any useless layers and also repeat patterns into additional
// layers.
builder.AdjustBackgroundLayers();
builder.AdjustMaskLayers();
// A subset of CSS properties should be forced at computed value time:
// https://drafts.csswg.org/css-color-adjust-1/#forced-colors-properties.
AdjustForForcedColorsMode(builder);
// Let the theme also have a crack at adjusting the style.
LayoutTheme::GetTheme().AdjustStyle(element, builder);
AdjustStyleForInert(builder, element);
AdjustStyleForEditing(builder);
bool is_svg_root = false;
if (svg_element) {
is_svg_root = svg_element->IsOutermostSVGSVGElement();
if (!is_svg_root) {
// Only the root <svg> element in an SVG document fragment tree honors css