1use crate::computed_value_flags::ComputedValueFlags;
9use crate::dom::TElement;
10use crate::logical_geometry::PhysicalSide;
11use crate::properties::longhands::display::computed_value::T as Display;
12use crate::properties::longhands::float::computed_value::T as Float;
13use crate::properties::longhands::position::computed_value::T as Position;
14#[cfg(feature = "gecko")]
15use crate::properties::longhands::{
16 contain::computed_value::T as Contain, container_type::computed_value::T as ContainerType,
17 content_visibility::computed_value::T as ContentVisibility,
18 overflow_x::computed_value::T as Overflow,
19};
20use crate::properties::{ComputedValues, StyleBuilder};
21use crate::values::computed::position::{
22 PositionTryFallbacksTryTactic, PositionTryFallbacksTryTacticKeyword, TryTacticAdjustment,
23};
24use crate::values::specified::align::AlignFlags;
25
26#[cfg(feature = "gecko")]
27use selectors::parser::PseudoElement;
28
29pub struct StyleAdjuster<'a, 'b: 'a> {
43 style: &'a mut StyleBuilder<'b>,
44}
45
46#[cfg(feature = "gecko")]
47fn is_topmost_svg_svg_element<E>(e: E) -> bool
48where
49 E: TElement,
50{
51 debug_assert!(e.is_svg_element());
52 if e.local_name() != &*atom!("svg") {
53 return false;
54 }
55
56 let parent = match e.traversal_parent() {
57 Some(n) => n,
58 None => return true,
59 };
60
61 if !parent.is_svg_element() {
62 return true;
63 }
64
65 parent.local_name() == &*atom!("foreignObject")
66}
67
68#[cfg(feature = "gecko")]
70fn is_effective_display_none_for_display_contents<E>(element: E) -> bool
71where
72 E: TElement,
73{
74 use crate::Atom;
75
76 const SPECIAL_HTML_ELEMENTS: [Atom; 16] = [
77 atom!("br"),
78 atom!("wbr"),
79 atom!("meter"),
80 atom!("progress"),
81 atom!("canvas"),
82 atom!("embed"),
83 atom!("object"),
84 atom!("audio"),
85 atom!("iframe"),
86 atom!("img"),
87 atom!("video"),
88 atom!("frame"),
89 atom!("frameset"),
90 atom!("input"),
91 atom!("textarea"),
92 atom!("select"),
93 ];
94
95 const SPECIAL_SVG_ELEMENTS: [Atom; 6] = [
101 atom!("svg"),
102 atom!("a"),
103 atom!("g"),
104 atom!("use"),
105 atom!("tspan"),
106 atom!("textPath"),
107 ];
108
109 if element.is_html_element() {
111 let local_name = element.local_name();
112 return SPECIAL_HTML_ELEMENTS
113 .iter()
114 .any(|name| &**name == local_name);
115 }
116
117 if element.is_svg_element() {
119 if is_topmost_svg_svg_element(element) {
120 return true;
121 }
122 let local_name = element.local_name();
123 return !SPECIAL_SVG_ELEMENTS
124 .iter()
125 .any(|name| &**name == local_name);
126 }
127
128 if element.is_mathml_element() {
130 return true;
131 }
132
133 false
134}
135
136impl<'a, 'b: 'a> StyleAdjuster<'a, 'b> {
137 #[inline]
139 pub fn new(style: &'a mut StyleBuilder<'b>) -> Self {
140 StyleAdjuster { style }
141 }
142
143 fn adjust_for_top_layer(&mut self) {
149 if !self.style.in_top_layer() {
150 return;
151 }
152 if !self.style.is_absolutely_positioned() {
153 self.style.mutate_box().set_position(Position::Absolute);
154 }
155 if self.style.get_box().clone_display().is_contents() {
156 self.style.mutate_box().set_display(Display::Block);
157 }
158 }
159
160 #[cfg(feature = "gecko")]
167 fn adjust_for_webkit_line_clamp(&mut self) {
168 use crate::properties::longhands::_moz_box_orient::computed_value::T as BoxOrient;
169 use crate::values::specified::box_::{DisplayInside, DisplayOutside};
170 let box_style = self.style.get_box();
171 if box_style.clone__webkit_line_clamp().is_none() {
172 return;
173 }
174 let disp = box_style.clone_display();
175 if disp.inside() != DisplayInside::WebkitBox {
176 return;
177 }
178 if self.style.get_xul().clone__moz_box_orient() != BoxOrient::Vertical {
179 return;
180 }
181 let new_display = if disp.outside() == DisplayOutside::Block {
182 Display::FlowRoot
183 } else {
184 debug_assert_eq!(disp.outside(), DisplayOutside::Inline);
185 Display::InlineBlock
186 };
187 self.style
188 .mutate_box()
189 .set_adjusted_display(new_display, false);
190 }
191
192 fn adjust_for_position(&mut self) {
198 if self.style.is_absolutely_positioned() && self.style.is_floating() {
199 self.style.mutate_box().set_float(Float::None);
200 }
201 }
202
203 fn skip_item_display_fixup<E>(&self, element: Option<E>) -> bool
206 where
207 E: TElement,
208 {
209 if let Some(pseudo) = self.style.pseudo {
210 return pseudo.skip_item_display_fixup();
211 }
212
213 element.is_some_and(|e| e.skip_item_display_fixup())
214 }
215
216 fn blockify_if_necessary<E>(&mut self, layout_parent_style: &ComputedValues, element: Option<E>)
223 where
224 E: TElement,
225 {
226 let mut blockify = false;
227 macro_rules! blockify_if {
228 ($if_what:expr) => {
229 if !blockify {
230 blockify = $if_what;
231 }
232 };
233 }
234
235 blockify_if!(self.style.is_root_element);
236 if !self.skip_item_display_fixup(element) {
237 let parent_display = layout_parent_style.get_box().clone_display();
238 blockify_if!(parent_display.is_item_container());
239 }
240
241 let is_item_or_root = blockify;
242
243 blockify_if!(self.style.is_floating());
244 blockify_if!(self.style.is_absolutely_positioned());
245
246 if !blockify {
247 return;
248 }
249
250 let display = self.style.get_box().clone_display();
251 let blockified_display = display.equivalent_block_display(self.style.is_root_element);
252 if display != blockified_display {
253 self.style
254 .mutate_box()
255 .set_adjusted_display(blockified_display, is_item_or_root);
256 }
257 }
258
259 fn set_bits(&mut self) {
261 let box_style = self.style.get_box();
262 let display = box_style.clone_display();
263
264 if !display.is_contents() {
265 if !self
266 .style
267 .get_text()
268 .clone_text_decoration_line()
269 .is_empty()
270 {
271 self.style
272 .add_flags(ComputedValueFlags::HAS_TEXT_DECORATION_LINES);
273 }
274
275 if self.style.get_effects().clone_opacity() == 0. {
276 self.style
277 .add_flags(ComputedValueFlags::IS_IN_OPACITY_ZERO_SUBTREE);
278 }
279 }
280
281 if self.style.pseudo.is_some_and(|p| p.is_first_line()) {
282 self.style
283 .add_flags(ComputedValueFlags::IS_IN_FIRST_LINE_SUBTREE);
284 }
285
286 if self.style.is_root_element {
287 self.style
288 .add_flags(ComputedValueFlags::IS_ROOT_ELEMENT_STYLE);
289 }
290
291 #[cfg(feature = "gecko")]
292 if box_style
293 .clone_effective_containment()
294 .contains(Contain::STYLE)
295 {
296 self.style
297 .add_flags(ComputedValueFlags::SELF_OR_ANCESTOR_HAS_CONTAIN_STYLE);
298 }
299
300 if box_style.clone_container_type().is_size_container_type() {
301 self.style
302 .add_flags(ComputedValueFlags::SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE);
303 }
304
305 #[cfg(feature = "servo")]
306 if self.style.get_parent_column().is_multicol() {
307 self.style.add_flags(ComputedValueFlags::CAN_BE_FRAGMENTED);
308 }
309 }
310
311 #[cfg(feature = "gecko")]
318 pub fn adjust_for_text(&mut self) {
319 debug_assert!(!self.style.is_root_element);
320 self.adjust_for_text_combine_upright();
321 self.adjust_for_text_in_ruby();
322 self.set_bits();
323 }
324
325 #[cfg(feature = "gecko")]
336 fn adjust_for_text_combine_upright(&mut self) {
337 use crate::computed_values::text_combine_upright::T as TextCombineUpright;
338 use crate::computed_values::writing_mode::T as WritingMode;
339 use crate::logical_geometry;
340
341 let writing_mode = self.style.get_inherited_box().clone_writing_mode();
342 let text_combine_upright = self.style.get_inherited_text().clone_text_combine_upright();
343
344 if matches!(
345 writing_mode,
346 WritingMode::VerticalRl | WritingMode::VerticalLr
347 ) && text_combine_upright == TextCombineUpright::All
348 {
349 self.style.add_flags(ComputedValueFlags::IS_TEXT_COMBINED);
350 self.style
351 .mutate_inherited_box()
352 .set_writing_mode(WritingMode::HorizontalTb);
353 self.style.writing_mode =
354 logical_geometry::WritingMode::new(self.style.get_inherited_box());
355 }
356 }
357
358 #[cfg(feature = "gecko")]
365 fn adjust_for_text_in_ruby(&mut self) {
366 let parent_display = self.style.get_parent_box().clone_display();
367 if parent_display.is_ruby_type()
368 || self
369 .style
370 .get_parent_flags()
371 .contains(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK)
372 {
373 self.style
374 .add_flags(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK);
375 }
376 }
377
378 fn adjust_for_writing_mode(&mut self, layout_parent_style: &ComputedValues) {
392 let our_writing_mode = self.style.get_inherited_box().clone_writing_mode();
393 let parent_writing_mode = layout_parent_style.get_inherited_box().clone_writing_mode();
394
395 if our_writing_mode != parent_writing_mode
396 && self.style.get_box().clone_display() == Display::Inline
397 {
398 if cfg!(feature = "servo") {
401 self.style
402 .mutate_box()
403 .set_adjusted_display(Display::InlineBlock, false);
404 } else {
405 self.style.mutate_box().set_display(Display::InlineBlock);
406 }
407 }
408 }
409
410 fn adjust_for_overflow(&mut self) {
416 let overflow_x = self.style.get_box().clone_overflow_x();
417 let overflow_y = self.style.get_box().clone_overflow_y();
418 if overflow_x == overflow_y {
419 return; }
421
422 if overflow_x.is_scrollable() != overflow_y.is_scrollable() {
423 let box_style = self.style.mutate_box();
424 box_style.set_overflow_x(overflow_x.to_scrollable());
425 box_style.set_overflow_y(overflow_y.to_scrollable());
426 }
427 }
428
429 #[cfg(feature = "gecko")]
430 fn adjust_for_contain(&mut self) {
431 let box_style = self.style.get_box();
432 let container_type = box_style.clone_container_type();
433 let content_visibility = box_style.clone_content_visibility();
434 if !container_type.is_size_container_type()
435 && content_visibility == ContentVisibility::Visible
436 {
437 debug_assert_eq!(
438 box_style.clone_contain(),
439 box_style.clone_effective_containment()
440 );
441 return;
442 }
443 let old_contain = box_style.clone_contain();
444 let mut new_contain = old_contain;
445 match content_visibility {
446 ContentVisibility::Visible => {},
447 ContentVisibility::Auto => {
451 new_contain.insert(Contain::LAYOUT | Contain::PAINT | Contain::STYLE)
452 },
453 ContentVisibility::Hidden => new_contain
454 .insert(Contain::LAYOUT | Contain::PAINT | Contain::SIZE | Contain::STYLE),
455 }
456 if container_type.intersects(ContainerType::INLINE_SIZE) {
457 new_contain.insert(Contain::STYLE | Contain::INLINE_SIZE);
461 } else if container_type.intersects(ContainerType::SIZE) {
462 new_contain.insert(Contain::STYLE | Contain::SIZE);
466 }
467 if new_contain == old_contain {
468 debug_assert_eq!(
469 box_style.clone_contain(),
470 box_style.clone_effective_containment()
471 );
472 return;
473 }
474 self.style
475 .mutate_box()
476 .set_effective_containment(new_contain);
477 }
478
479 #[cfg(feature = "gecko")]
484 fn adjust_for_contain_intrinsic_size(&mut self) {
485 let content_visibility = self.style.get_box().clone_content_visibility();
486 if content_visibility != ContentVisibility::Auto {
487 return;
488 }
489
490 let pos = self.style.get_position();
491 let new_width = pos.clone_contain_intrinsic_width().add_auto_if_needed();
492 let new_height = pos.clone_contain_intrinsic_height().add_auto_if_needed();
493 if new_width.is_none() && new_height.is_none() {
494 return;
495 }
496
497 let pos = self.style.mutate_position();
498 if let Some(width) = new_width {
499 pos.set_contain_intrinsic_width(width);
500 }
501 if let Some(height) = new_height {
502 pos.set_contain_intrinsic_height(height);
503 }
504 }
505
506 #[cfg(feature = "gecko")]
512 fn adjust_for_prohibited_display_contents<E>(&mut self, element: Option<E>)
513 where
514 E: TElement,
515 {
516 if self.style.get_box().clone_display() != Display::Contents {
517 return;
518 }
519
520 if self.style.pseudo.is_some_and(|p| !p.is_element_backed()) {
522 self.style.mutate_box().set_display(Display::Inline);
523 return;
524 }
525
526 let element = match element {
527 Some(e) => e,
528 None => return,
529 };
530
531 if is_effective_display_none_for_display_contents(element) {
532 self.style.mutate_box().set_display(Display::None);
533 }
534 }
535
536 #[cfg(feature = "gecko")]
539 fn adjust_for_text_control_editing_root(&mut self) {
540 use crate::properties::longhands::white_space_collapse::computed_value::T as WhiteSpaceCollapse;
541 use crate::selector_parser::PseudoElement;
542
543 if self.style.pseudo != Some(&PseudoElement::MozTextControlEditingRoot) {
544 return;
545 }
546
547 let old_collapse = self.style.get_inherited_text().clone_white_space_collapse();
548 let new_collapse = match old_collapse {
549 WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces => old_collapse,
550 WhiteSpaceCollapse::Collapse
551 | WhiteSpaceCollapse::PreserveSpaces
552 | WhiteSpaceCollapse::PreserveBreaks => WhiteSpaceCollapse::Preserve,
553 };
554 if new_collapse != old_collapse {
555 self.style
556 .mutate_inherited_text()
557 .set_white_space_collapse(new_collapse);
558 }
559
560 let box_style = self.style.get_box();
561 let overflow_x = box_style.clone_overflow_x();
562 let overflow_y = box_style.clone_overflow_y();
563
564 if overflow_x.is_scrollable() || overflow_y.is_scrollable() {
567 return;
568 }
569
570 let box_style = self.style.mutate_box();
571 box_style.set_overflow_x(Overflow::Auto);
572 box_style.set_overflow_y(Overflow::Auto);
573 }
574
575 #[cfg(feature = "gecko")]
582 fn adjust_for_fieldset_content(&mut self, layout_parent_style: &ComputedValues) {
583 use crate::selector_parser::PseudoElement;
584
585 if self.style.pseudo != Some(&PseudoElement::FieldsetContent) {
586 return;
587 }
588
589 let parent_display = layout_parent_style.get_box().clone_display();
593 let new_display = match parent_display {
594 Display::Flex | Display::InlineFlex => Some(Display::Flex),
595 Display::Grid | Display::InlineGrid => Some(Display::Grid),
596 _ => None,
597 };
598 if let Some(new_display) = new_display {
599 self.style.mutate_box().set_display(new_display);
600 }
601 }
602
603 fn adjust_for_table_text_align(&mut self) {
610 use crate::properties::longhands::text_align::computed_value::T as TextAlign;
611 if self.style.get_box().clone_display() != Display::Table {
612 return;
613 }
614
615 match self.style.get_inherited_text().clone_text_align() {
616 TextAlign::MozLeft | TextAlign::MozCenter | TextAlign::MozRight => {},
617 _ => return,
618 }
619
620 self.style
621 .mutate_inherited_text()
622 .set_text_align(TextAlign::Start)
623 }
624
625 #[cfg(feature = "gecko")]
626 fn should_suppress_linebreak<E>(
627 &self,
628 layout_parent_style: &ComputedValues,
629 element: Option<E>,
630 ) -> bool
631 where
632 E: TElement,
633 {
634 if self.style.is_floating() || self.style.is_absolutely_positioned() {
636 return false;
637 }
638 let parent_display = layout_parent_style.get_box().clone_display();
639 if layout_parent_style
640 .flags
641 .contains(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK)
642 {
643 if parent_display.is_line_participant() {
646 return true;
647 }
648 }
649 match self.style.get_box().clone_display() {
650 Display::RubyBase | Display::RubyText => true,
652 Display::RubyBaseContainer | Display::RubyTextContainer
661 if element.map_or(true, |e| e.is_html_element()) =>
662 {
663 false
664 },
665 _ => parent_display.is_ruby_type(),
669 }
670 }
671
672 #[cfg(feature = "gecko")]
678 fn adjust_for_ruby<E>(&mut self, layout_parent_style: &ComputedValues, element: Option<E>)
679 where
680 E: TElement,
681 {
682 use crate::properties::longhands::unicode_bidi::computed_value::T as UnicodeBidi;
683
684 let self_display = self.style.get_box().clone_display();
685 if self.should_suppress_linebreak(layout_parent_style, element) {
687 self.style
688 .add_flags(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK);
689 if !self.skip_item_display_fixup(element) {
691 let inline_display = self_display.inlinify();
692 if self_display != inline_display {
693 self.style
694 .mutate_box()
695 .set_adjusted_display(inline_display, false);
696 }
697 }
698 }
699 if self_display.is_ruby_level_container() {
704 self.style.reset_border_struct();
705 self.style.reset_padding_struct();
706 }
707
708 if self_display.is_ruby_type() {
711 let new_value = match self.style.get_text().clone_unicode_bidi() {
712 UnicodeBidi::Normal | UnicodeBidi::Embed => Some(UnicodeBidi::Isolate),
713 UnicodeBidi::BidiOverride => Some(UnicodeBidi::IsolateOverride),
714 _ => None,
715 };
716 if let Some(new_value) = new_value {
717 self.style.mutate_text().set_unicode_bidi(new_value);
718 }
719 }
720 }
721
722 fn adjust_for_visited<E>(&mut self, element: Option<E>)
732 where
733 E: TElement,
734 {
735 if !self.style.has_visited_style() {
736 return;
737 }
738
739 let is_link_element = self.style.pseudo.is_none() && element.map_or(false, |e| e.is_link());
740
741 if !is_link_element {
742 return;
743 }
744
745 if element.unwrap().is_visited_link() {
746 self.style
747 .add_flags(ComputedValueFlags::IS_RELEVANT_LINK_VISITED);
748 } else {
749 self.style
751 .remove_flags(ComputedValueFlags::IS_RELEVANT_LINK_VISITED);
752 }
753 }
754
755 #[cfg(feature = "gecko")]
760 fn adjust_for_justify_items(&mut self) {
761 use crate::values::specified::align;
762 let justify_items = self.style.get_position().clone_justify_items();
763 if justify_items.specified != align::JustifyItems::legacy() {
764 return;
765 }
766
767 let parent_justify_items = self.style.get_parent_position().clone_justify_items();
768
769 if !parent_justify_items.computed.contains(AlignFlags::LEGACY) {
770 return;
771 }
772
773 if parent_justify_items.computed == justify_items.computed {
774 return;
775 }
776
777 self.style
778 .mutate_position()
779 .set_computed_justify_items(parent_justify_items.computed);
780 }
781
782 #[cfg(feature = "gecko")]
787 fn adjust_for_appearance<E>(&mut self, element: Option<E>)
788 where
789 E: TElement,
790 {
791 use crate::properties::longhands::appearance::computed_value::T as Appearance;
792 use crate::properties::longhands::line_height::computed_value::T as LineHeight;
793
794 let box_ = self.style.get_box();
795 let appearance = match box_.clone_appearance() {
796 Appearance::Auto => box_.clone__moz_default_appearance(),
797 a => a,
798 };
799
800 if appearance == Appearance::Menulist {
801 if self.style.get_font().clone_line_height() == LineHeight::normal() {
802 return;
803 }
804 if self.style.pseudo.is_some() {
805 return;
806 }
807 let is_html_select_element = element.map_or(false, |e| {
808 e.is_html_element() && e.local_name() == &*atom!("select")
809 });
810 if !is_html_select_element {
811 return;
812 }
813 self.style
814 .mutate_font()
815 .set_line_height(LineHeight::normal());
816 }
817 }
818
819 #[cfg(feature = "gecko")]
830 fn adjust_for_marker_pseudo(&mut self) {
831 use crate::values::computed::counters::Content;
832 use crate::values::computed::font::{FontFamily, FontSynthesis, FontSynthesisStyle};
833 use crate::values::computed::text::{LetterSpacing, WordSpacing};
834
835 let is_legacy_marker = self.style.pseudo.map_or(false, |p| p.is_marker())
836 && self.style.get_list().clone_list_style_type().is_bullet()
837 && self.style.get_counters().clone_content() == Content::Normal;
838 if !is_legacy_marker {
839 return;
840 }
841 let flags = self.style.flags.get();
842 if !flags.contains(ComputedValueFlags::HAS_AUTHOR_SPECIFIED_FONT_FAMILY) {
843 self.style
844 .mutate_font()
845 .set_font_family(FontFamily::moz_bullet().clone());
846
847 if !flags.contains(ComputedValueFlags::HAS_AUTHOR_SPECIFIED_FONT_SYNTHESIS_WEIGHT) {
851 self.style
852 .mutate_font()
853 .set_font_synthesis_weight(FontSynthesis::None);
854 }
855 if !flags.contains(ComputedValueFlags::HAS_AUTHOR_SPECIFIED_FONT_SYNTHESIS_STYLE) {
856 self.style
857 .mutate_font()
858 .set_font_synthesis_style(FontSynthesisStyle::None);
859 }
860 }
861 if !flags.contains(ComputedValueFlags::HAS_AUTHOR_SPECIFIED_LETTER_SPACING) {
862 self.style
863 .mutate_inherited_text()
864 .set_letter_spacing(LetterSpacing::normal());
865 }
866 if !flags.contains(ComputedValueFlags::HAS_AUTHOR_SPECIFIED_WORD_SPACING) {
867 self.style
868 .mutate_inherited_text()
869 .set_word_spacing(WordSpacing::normal());
870 }
871 }
872
873 fn adjust_for_try_tactic(&mut self, tactic: &PositionTryFallbacksTryTactic) {
881 debug_assert!(!tactic.is_empty());
882 let wm = self.style.writing_mode;
884 for tactic in tactic.iter() {
886 use PositionTryFallbacksTryTacticKeyword::*;
887 match tactic {
888 FlipBlock => {
889 self.flip_self_alignment(true);
890 self.flip_insets_and_margins(wm.is_vertical());
891 },
892 FlipInline => {
893 self.flip_self_alignment(false);
894 self.flip_insets_and_margins(wm.is_horizontal());
895 },
896 FlipX => {
897 self.flip_self_alignment(wm.is_vertical());
898 self.flip_insets_and_margins(true);
899 },
900 FlipY => {
901 self.flip_self_alignment(wm.is_horizontal());
902 self.flip_insets_and_margins(false);
903 },
904 FlipStart => {
905 self.flip_start();
906 },
907 }
908 self.apply_position_area_tactic(*tactic);
909 }
910 }
911
912 fn apply_position_area_tactic(&mut self, tactic: PositionTryFallbacksTryTacticKeyword) {
913 let pos = self.style.get_position();
914 let old = pos.clone_position_area();
915 let wm = self.style.writing_mode;
916 let new = old.with_tactic(wm, tactic);
917 if new == old {
918 return;
919 }
920 let pos = self.style.mutate_position();
921 pos.set_position_area(new);
922 }
923
924 fn swap_insets(&mut self, a_side: PhysicalSide, b_side: PhysicalSide) {
926 debug_assert_ne!(a_side, b_side);
927 let pos = self.style.mutate_position();
928 let mut a = pos.get_inset(a_side).clone();
929 a.try_tactic_adjustment(a_side, b_side);
930 let mut b = pos.get_inset(b_side).clone();
931 b.try_tactic_adjustment(b_side, a_side);
932 pos.set_inset(a_side, b);
933 pos.set_inset(b_side, a);
934 }
935
936 fn swap_margins(&mut self, a_side: PhysicalSide, b_side: PhysicalSide) {
937 debug_assert_ne!(a_side, b_side);
938 let margin = self.style.get_margin();
939 let mut a = margin.get_margin(a_side).clone();
940 a.try_tactic_adjustment(a_side, b_side);
941 let mut b = margin.get_margin(b_side).clone();
942 b.try_tactic_adjustment(b_side, a_side);
943 let margin = self.style.mutate_margin();
944 margin.set_margin(a_side, b);
945 margin.set_margin(b_side, a);
946 }
947
948 fn swap_sizes(&mut self, block_start: PhysicalSide, inline_start: PhysicalSide) {
949 let pos = self.style.mutate_position();
950 let mut min_width = pos.clone_min_width();
951 min_width.try_tactic_adjustment(inline_start, block_start);
952 let mut max_width = pos.clone_max_width();
953 max_width.try_tactic_adjustment(inline_start, block_start);
954 let mut width = pos.clone_width();
955 width.try_tactic_adjustment(inline_start, block_start);
956
957 let mut min_height = pos.clone_min_height();
958 min_height.try_tactic_adjustment(block_start, inline_start);
959 let mut max_height = pos.clone_max_height();
960 max_height.try_tactic_adjustment(block_start, inline_start);
961 let mut height = pos.clone_height();
962 height.try_tactic_adjustment(block_start, inline_start);
963
964 let pos = self.style.mutate_position();
965 pos.set_width(height);
966 pos.set_height(width);
967 pos.set_max_width(max_height);
968 pos.set_max_height(max_width);
969 pos.set_min_width(min_height);
970 pos.set_min_height(min_width);
971 }
972
973 fn flip_start(&mut self) {
974 let wm = self.style.writing_mode;
975 let bs = wm.block_start_physical_side();
976 let is = wm.inline_start_physical_side();
977 let be = wm.block_end_physical_side();
978 let ie = wm.inline_end_physical_side();
979 self.swap_sizes(bs, is);
980 self.swap_insets(bs, is);
981 self.swap_insets(ie, be);
982 self.swap_margins(bs, is);
983 self.swap_margins(ie, be);
984 self.flip_alignment_start();
985 }
986
987 fn flip_insets_and_margins(&mut self, horizontal: bool) {
988 if horizontal {
989 self.swap_insets(PhysicalSide::Left, PhysicalSide::Right);
990 self.swap_margins(PhysicalSide::Left, PhysicalSide::Right);
991 } else {
992 self.swap_insets(PhysicalSide::Top, PhysicalSide::Bottom);
993 self.swap_margins(PhysicalSide::Top, PhysicalSide::Bottom);
994 }
995 }
996
997 fn flip_alignment_start(&mut self) {
998 let pos = self.style.get_position();
999 let align = pos.clone_align_self();
1000 let mut justify = pos.clone_justify_self();
1001 if align == justify {
1002 return;
1003 }
1004
1005 if matches!(justify.value(), AlignFlags::LEFT | AlignFlags::RIGHT) {
1008 let left = justify.value() == AlignFlags::LEFT;
1009 let ltr = self.style.writing_mode.is_bidi_ltr();
1010 justify = justify.with_value(if left == ltr {
1011 AlignFlags::SELF_START
1012 } else {
1013 AlignFlags::SELF_END
1014 });
1015 }
1016
1017 let pos = self.style.mutate_position();
1018 pos.set_align_self(justify);
1019 pos.set_justify_self(align);
1020 }
1021
1022 fn flip_self_alignment(&mut self, block: bool) {
1023 let pos = self.style.get_position();
1024 let cur = if block {
1025 pos.clone_align_self()
1026 } else {
1027 pos.clone_justify_self()
1028 };
1029 let flipped = cur.flip_position();
1030 if flipped == cur {
1031 return;
1032 }
1033 let pos = self.style.mutate_position();
1034 if block {
1035 pos.set_align_self(flipped);
1036 } else {
1037 pos.set_justify_self(flipped);
1038 }
1039 }
1040
1041 pub fn adjust<E>(
1043 &mut self,
1044 layout_parent_style: &ComputedValues,
1045 element: Option<E>,
1046 try_tactic: &PositionTryFallbacksTryTactic,
1047 ) where
1048 E: TElement,
1049 {
1050 if cfg!(debug_assertions) {
1051 if let Some(e) = element {
1052 if let Some(p) = e.implemented_pseudo_element() {
1053 debug_assert!(
1057 self.style.pseudo.is_some(),
1058 "Someone really messed up (no pseudo style for {e:?}, {p:?})"
1059 );
1060 }
1061 }
1062 }
1063 self.adjust_for_visited(element);
1073 #[cfg(feature = "gecko")]
1074 {
1075 self.adjust_for_prohibited_display_contents(element);
1076 self.adjust_for_fieldset_content(layout_parent_style);
1077 self.adjust_for_text_control_editing_root();
1080 }
1081 self.adjust_for_top_layer();
1082 self.blockify_if_necessary(layout_parent_style, element);
1083 #[cfg(feature = "gecko")]
1084 self.adjust_for_webkit_line_clamp();
1085 self.adjust_for_position();
1086 self.adjust_for_overflow();
1087 #[cfg(feature = "gecko")]
1088 {
1089 self.adjust_for_contain();
1090 self.adjust_for_contain_intrinsic_size();
1091 self.adjust_for_justify_items();
1092 }
1093 self.adjust_for_table_text_align();
1094 self.adjust_for_writing_mode(layout_parent_style);
1095 #[cfg(feature = "gecko")]
1096 {
1097 self.adjust_for_ruby(layout_parent_style, element);
1098 self.adjust_for_appearance(element);
1099 self.adjust_for_marker_pseudo();
1100 }
1101 if !try_tactic.is_empty() {
1102 self.adjust_for_try_tactic(try_tactic);
1103 }
1104 self.set_bits();
1105 }
1106}