1use crate::computed_value_flags::ComputedValueFlags;
9use crate::dom::TElement;
10use crate::logical_geometry::PhysicalSide;
11#[cfg(feature = "gecko")]
12use crate::properties::LonghandId;
13use crate::properties::longhands::display::computed_value::T as Display;
14use crate::properties::longhands::float::computed_value::T as Float;
15use crate::properties::longhands::position::computed_value::T as Position;
16#[cfg(feature = "gecko")]
17use crate::properties::longhands::{
18 contain::computed_value::T as Contain, container_type::computed_value::T as ContainerType,
19 content_visibility::computed_value::T as ContentVisibility,
20};
21use crate::properties::{ComputedValues, LonghandIdSet, StyleBuilder};
22use crate::values::computed::position::{
23 PositionTryFallbacksTryTactic, PositionTryFallbacksTryTacticKeyword, TryTacticAdjustment,
24};
25use crate::values::specified::align::AlignFlags;
26
27pub struct StyleAdjuster<'a, 'b: 'a> {
41 style: &'a mut StyleBuilder<'b>,
42}
43
44fn is_topmost_svg_svg_element<E>(e: E) -> bool
45where
46 E: TElement,
47{
48 debug_assert!(e.is_svg_element());
49 if e.local_name() != &*atom!("svg") {
50 return false;
51 }
52
53 let parent = match e.traversal_parent() {
54 Some(n) => n,
55 None => return true,
56 };
57
58 if !parent.is_svg_element() {
59 return true;
60 }
61
62 parent.local_name() == &*atom!("foreignObject")
63}
64
65fn is_effective_display_none_for_display_contents<E>(element: E) -> bool
67where
68 E: TElement,
69{
70 use crate::Atom;
71
72 const SPECIAL_HTML_ELEMENTS: [Atom; 16] = [
73 atom!("br"),
74 atom!("wbr"),
75 atom!("meter"),
76 atom!("progress"),
77 atom!("canvas"),
78 atom!("embed"),
79 atom!("object"),
80 atom!("audio"),
81 atom!("iframe"),
82 atom!("img"),
83 atom!("video"),
84 atom!("frame"),
85 atom!("frameset"),
86 atom!("input"),
87 atom!("textarea"),
88 atom!("select"),
89 ];
90
91 const SPECIAL_SVG_ELEMENTS: [Atom; 6] = [
97 atom!("svg"),
98 atom!("a"),
99 atom!("g"),
100 atom!("use"),
101 atom!("tspan"),
102 atom!("textPath"),
103 ];
104
105 if element.is_html_element() {
107 let local_name = element.local_name();
108 return SPECIAL_HTML_ELEMENTS
109 .iter()
110 .any(|name| &**name == local_name);
111 }
112
113 if element.is_svg_element() {
115 if is_topmost_svg_svg_element(element) {
116 return true;
117 }
118 let local_name = element.local_name();
119 return !SPECIAL_SVG_ELEMENTS
120 .iter()
121 .any(|name| &**name == local_name);
122 }
123
124 if element.is_mathml_element() {
126 return true;
127 }
128
129 false
130}
131
132impl<'a, 'b: 'a> StyleAdjuster<'a, 'b> {
133 #[inline]
135 pub fn new(style: &'a mut StyleBuilder<'b>) -> Self {
136 StyleAdjuster { style }
137 }
138
139 fn adjust_for_top_layer(&mut self) {
145 if !self.style.in_top_layer() {
146 return;
147 }
148 if !self.style.is_absolutely_positioned() {
149 self.style.mutate_box().set_position(Position::Absolute);
150 }
151 if self.style.get_box().get_display().is_contents() {
152 self.style.mutate_box().set_display(Display::Block);
153 }
154 }
155
156 #[cfg(feature = "gecko")]
163 fn adjust_for_webkit_line_clamp(&mut self) {
164 use crate::properties::longhands::_moz_box_orient::computed_value::T as BoxOrient;
165 use crate::values::specified::box_::{DisplayInside, DisplayOutside};
166 let box_style = self.style.get_box();
167 let line_clamp = box_style.get_line_clamp();
168 if line_clamp.is_none() {
169 return;
170 }
171
172 let disp = *box_style.get_display();
173 if disp.inside() != DisplayInside::WebkitBox
174 || *self.style.get_xul().get__moz_box_orient() != BoxOrient::Vertical
175 {
176 return;
177 }
178 let new_display = if disp.outside() == DisplayOutside::Block {
179 Display::FlowRoot
180 } else {
181 debug_assert_eq!(disp.outside(), DisplayOutside::Inline);
182 Display::InlineBlock
183 };
184 self.style
185 .mutate_box()
186 .set_adjusted_display(new_display, false);
187 }
188
189 fn adjust_for_position(&mut self) {
195 if self.style.is_absolutely_positioned() && self.style.is_floating() {
196 self.style.mutate_box().set_float(Float::None);
197 }
198 }
199
200 fn skip_item_display_fixup<E>(&self, element: Option<E>) -> bool
203 where
204 E: TElement,
205 {
206 if let Some(pseudo) = self.style.pseudo {
207 return pseudo.skip_item_display_fixup();
208 }
209
210 element.is_some_and(|e| e.skip_item_display_fixup())
211 }
212
213 fn blockify_if_necessary<E>(&mut self, layout_parent_style: &ComputedValues, element: Option<E>)
220 where
221 E: TElement,
222 {
223 let mut blockify = false;
224 macro_rules! blockify_if {
225 ($if_what:expr) => {
226 if !blockify {
227 blockify = $if_what;
228 }
229 };
230 }
231
232 blockify_if!(self.style.is_root_element);
233 if !self.skip_item_display_fixup(element) {
234 let parent_display = *layout_parent_style.get_box().get_display();
235 blockify_if!(parent_display.is_item_container());
236 }
237
238 let is_item_or_root = blockify;
239
240 blockify_if!(self.style.is_floating());
241 blockify_if!(self.style.is_absolutely_positioned());
242
243 if !blockify {
244 return;
245 }
246
247 let display = *self.style.get_box().get_display();
248 let blockified_display = display.equivalent_block_display(self.style.is_root_element);
249 if display != blockified_display {
250 self.style
251 .mutate_box()
252 .set_adjusted_display(blockified_display, is_item_or_root);
253 }
254 }
255
256 fn set_bits(&mut self) {
258 let box_style = self.style.get_box();
259 let display = *box_style.get_display();
260
261 if !display.is_contents() {
262 if !self.style.get_text().get_text_decoration_line().is_empty() {
263 self.style
264 .add_flags(ComputedValueFlags::HAS_TEXT_DECORATION_LINES);
265 }
266
267 if *self.style.get_effects().get_opacity() == 0. {
268 self.style
269 .add_flags(ComputedValueFlags::IS_IN_OPACITY_ZERO_SUBTREE);
270 }
271 } else if self
272 .style
273 .get_parent_box()
274 .get_display()
275 .is_item_container()
276 || self
277 .style
278 .get_parent_flags()
279 .contains(ComputedValueFlags::DISPLAY_CONTENTS_IN_ITEM_CONTAINER)
280 {
281 self.style
282 .add_flags(ComputedValueFlags::DISPLAY_CONTENTS_IN_ITEM_CONTAINER);
283 }
284
285 if self.style.pseudo.is_some_and(|p| p.is_first_line()) {
286 self.style
287 .add_flags(ComputedValueFlags::IS_IN_FIRST_LINE_SUBTREE);
288 }
289
290 if self.style.is_root_element {
291 self.style
292 .add_flags(ComputedValueFlags::IS_ROOT_ELEMENT_STYLE);
293 }
294
295 #[cfg(feature = "gecko")]
296 if box_style
297 .get_effective_containment()
298 .contains(Contain::STYLE)
299 {
300 self.style
301 .add_flags(ComputedValueFlags::SELF_OR_ANCESTOR_HAS_CONTAIN_STYLE);
302 }
303
304 if box_style.get_container_type().is_size_container_type() {
305 self.style
306 .add_flags(ComputedValueFlags::SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE);
307 }
308 }
309
310 #[cfg(feature = "gecko")]
317 pub fn adjust_for_text(&mut self) {
318 debug_assert!(!self.style.is_root_element);
319 self.adjust_for_text_combine_upright();
320 self.adjust_for_text_in_ruby();
321 self.set_bits();
322 }
323
324 #[cfg(feature = "gecko")]
335 fn adjust_for_text_combine_upright(&mut self) {
336 use crate::computed_values::text_combine_upright::T as TextCombineUpright;
337 use crate::computed_values::writing_mode::T as WritingMode;
338 use crate::logical_geometry;
339
340 let writing_mode = *self.style.get_inherited_box().get_writing_mode();
341 let text_combine_upright = *self.style.get_inherited_text().get_text_combine_upright();
342
343 if matches!(
344 writing_mode,
345 WritingMode::VerticalRl | WritingMode::VerticalLr
346 ) && text_combine_upright == TextCombineUpright::All
347 {
348 self.style.add_flags(ComputedValueFlags::IS_TEXT_COMBINED);
349 self.style
350 .mutate_inherited_box()
351 .set_writing_mode(WritingMode::HorizontalTb);
352 self.style.writing_mode =
353 logical_geometry::WritingMode::new(self.style.get_inherited_box());
354 }
355 }
356
357 #[cfg(feature = "gecko")]
364 fn adjust_for_text_in_ruby(&mut self) {
365 let parent_display = *self.style.get_parent_box().get_display();
366 if parent_display.is_ruby_type()
367 || self
368 .style
369 .get_parent_flags()
370 .contains(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK)
371 {
372 self.style
373 .add_flags(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK);
374 }
375 }
376
377 fn adjust_for_writing_mode(&mut self, layout_parent_style: &ComputedValues) {
391 let our_writing_mode = *self.style.get_inherited_box().get_writing_mode();
392 let parent_writing_mode = *layout_parent_style.get_inherited_box().get_writing_mode();
393
394 if our_writing_mode != parent_writing_mode
395 && *self.style.get_box().get_display() == Display::Inline
396 {
397 if cfg!(feature = "servo") {
400 self.style
401 .mutate_box()
402 .set_adjusted_display(Display::InlineBlock, false);
403 } else {
404 self.style.mutate_box().set_display(Display::InlineBlock);
405 }
406 }
407 }
408
409 fn adjust_for_overflow(&mut self) {
415 let overflow_x = *self.style.get_box().get_overflow_x();
416 let overflow_y = *self.style.get_box().get_overflow_y();
417 if overflow_x == overflow_y {
418 return; }
420
421 if overflow_x.is_scrollable() != overflow_y.is_scrollable() {
422 let box_style = self.style.mutate_box();
423 box_style.set_overflow_x(overflow_x.to_scrollable());
424 box_style.set_overflow_y(overflow_y.to_scrollable());
425 }
426 }
427
428 #[cfg(feature = "gecko")]
429 fn adjust_for_contain(&mut self) {
430 let box_style = self.style.get_box();
431 let container_type = *box_style.get_container_type();
432 let content_visibility = *box_style.get_content_visibility();
433 if !container_type.is_size_container_type()
434 && content_visibility == ContentVisibility::Visible
435 {
436 debug_assert_eq!(
437 box_style.get_contain(),
438 box_style.get_effective_containment()
439 );
440 return;
441 }
442 let old_contain = *box_style.get_contain();
443 let mut new_contain = old_contain;
444 match content_visibility {
445 ContentVisibility::Visible => {},
446 ContentVisibility::Auto => {
450 new_contain.insert(Contain::LAYOUT | Contain::PAINT | Contain::STYLE)
451 },
452 ContentVisibility::Hidden => new_contain
453 .insert(Contain::LAYOUT | Contain::PAINT | Contain::SIZE | Contain::STYLE),
454 }
455 if container_type.intersects(ContainerType::INLINE_SIZE) {
456 new_contain.insert(Contain::STYLE | Contain::INLINE_SIZE);
460 } else if container_type.intersects(ContainerType::SIZE) {
461 new_contain.insert(Contain::STYLE | Contain::SIZE);
465 }
466 if new_contain == old_contain {
467 debug_assert_eq!(
468 box_style.get_contain(),
469 box_style.get_effective_containment()
470 );
471 return;
472 }
473 self.style
474 .mutate_box()
475 .set_effective_containment(new_contain);
476 }
477
478 #[cfg(feature = "gecko")]
483 fn adjust_for_contain_intrinsic_size(&mut self) {
484 let content_visibility = *self.style.get_box().get_content_visibility();
485 if content_visibility != ContentVisibility::Auto {
486 return;
487 }
488
489 let pos = self.style.get_position();
490 let new_width = pos.get_contain_intrinsic_width().add_auto_if_needed();
491 let new_height = pos.get_contain_intrinsic_height().add_auto_if_needed();
492 if new_width.is_none() && new_height.is_none() {
493 return;
494 }
495
496 let pos = self.style.mutate_position();
497 if let Some(width) = new_width {
498 pos.set_contain_intrinsic_width(width);
499 }
500 if let Some(height) = new_height {
501 pos.set_contain_intrinsic_height(height);
502 }
503 }
504
505 fn adjust_for_prohibited_display_contents<E>(&mut self, element: Option<E>)
511 where
512 E: TElement,
513 {
514 if *self.style.get_box().get_display() != Display::Contents {
515 return;
516 }
517
518 #[cfg(feature = "gecko")]
520 if self.style.pseudo.is_some_and(|p| !p.is_element_backed()) {
521 self.style.mutate_box().set_display(Display::Inline);
522 return;
523 }
524
525 let element = match element {
526 Some(e) => e,
527 None => return,
528 };
529
530 if is_effective_display_none_for_display_contents(element) {
531 self.style.mutate_box().set_display(Display::None);
532 }
533 }
534
535 #[cfg(feature = "gecko")]
538 fn adjust_for_text_control_editing_root(&mut self) {
539 use crate::properties::longhands::white_space_collapse::computed_value::T as WhiteSpaceCollapse;
540 use crate::selector_parser::PseudoElement;
541
542 if self.style.pseudo != Some(&PseudoElement::MozTextControlEditingRoot) {
543 return;
544 }
545
546 let old_collapse = *self.style.get_inherited_text().get_white_space_collapse();
547 let new_collapse = match old_collapse {
548 WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces => old_collapse,
549 WhiteSpaceCollapse::Collapse
550 | WhiteSpaceCollapse::PreserveSpaces
551 | WhiteSpaceCollapse::PreserveBreaks => WhiteSpaceCollapse::Preserve,
552 };
553 if new_collapse != old_collapse {
554 self.style
555 .mutate_inherited_text()
556 .set_white_space_collapse(new_collapse);
557 }
558 }
559
560 #[cfg(feature = "gecko")]
563 fn adjust_for_fieldset_content(&mut self) {
564 use crate::selector_parser::PseudoElement;
565 if self.style.pseudo != Some(&PseudoElement::MozFieldsetContent) {
566 return;
567 }
568 let parent_display = *self.style.get_parent_box().get_display();
569 debug_assert!(
570 !parent_display.is_contents(),
571 "How did we create a fieldset-content box with display: contents?"
572 );
573 let new_display = match parent_display {
574 Display::Flex | Display::InlineFlex => Some(Display::Flex),
575 Display::Grid | Display::InlineGrid => Some(Display::Grid),
576 _ => None,
577 };
578 if let Some(new_display) = new_display {
579 self.style.mutate_box().set_display(new_display);
580 }
581 }
582
583 fn adjust_for_table_text_align(&mut self) {
590 use crate::properties::longhands::text_align::computed_value::T as TextAlign;
591 if *self.style.get_box().get_display() != Display::Table {
592 return;
593 }
594
595 match *self.style.get_inherited_text().get_text_align() {
596 TextAlign::MozLeft | TextAlign::MozCenter | TextAlign::MozRight => {},
597 _ => return,
598 }
599
600 self.style
601 .mutate_inherited_text()
602 .set_text_align(TextAlign::Start)
603 }
604
605 #[cfg(feature = "gecko")]
606 fn should_suppress_linebreak<E>(&self, element: Option<E>) -> bool
607 where
608 E: TElement,
609 {
610 let self_box = self.style.get_box();
611 if !self_box.get_display().is_contents()
613 && (self_box.get_float().is_floating()
614 || self_box.get_position().is_absolutely_positioned())
615 {
616 return false;
617 }
618 let parent_display = *self.style.get_parent_box().get_display();
619 if self
620 .style
621 .get_parent_flags()
622 .contains(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK)
623 {
624 if parent_display.is_line_participant() || parent_display.is_contents() {
627 return true;
628 }
629 }
630 match *self_box.get_display() {
631 Display::RubyBase | Display::RubyText => true,
633 Display::RubyBaseContainer | Display::RubyTextContainer
642 if element.is_none_or(|e| e.is_html_element()) =>
643 {
644 false
645 },
646 _ => parent_display.is_ruby_type(),
650 }
651 }
652
653 #[cfg(feature = "gecko")]
659 fn adjust_for_ruby<E>(&mut self, element: Option<E>)
660 where
661 E: TElement,
662 {
663 use crate::properties::longhands::unicode_bidi::computed_value::T as UnicodeBidi;
664
665 let self_display = *self.style.get_box().get_display();
666 if self.should_suppress_linebreak(element) {
668 self.style
669 .add_flags(ComputedValueFlags::SHOULD_SUPPRESS_LINEBREAK);
670 if !self.skip_item_display_fixup(element) {
672 let inline_display = self_display.inlinify();
673 if self_display != inline_display {
674 self.style
675 .mutate_box()
676 .set_adjusted_display(inline_display, false);
677 }
678 }
679 }
680 if self_display.is_ruby_level_container() {
685 self.style.reset_border_struct();
686 self.style.reset_padding_struct();
687 }
688
689 if self_display.is_ruby_type() {
692 let new_value = match *self.style.get_text().get_unicode_bidi() {
693 UnicodeBidi::Normal | UnicodeBidi::Embed => Some(UnicodeBidi::Isolate),
694 UnicodeBidi::BidiOverride => Some(UnicodeBidi::IsolateOverride),
695 _ => None,
696 };
697 if let Some(new_value) = new_value {
698 self.style.mutate_text().set_unicode_bidi(new_value);
699 }
700 }
701 }
702
703 fn enforce_link_underline(&mut self) {
704 use crate::values::computed::{Color, TextDecorationLine, TextDecorationStyle};
705
706 let text_style = self.style.get_text();
707 let old_line = *text_style.get_text_decoration_line();
708 let needs_adjustment = !old_line.intersects(TextDecorationLine::UNDERLINE)
709 || text_style.get_text_decoration_style() != &TextDecorationStyle::Solid
710 || text_style.get_text_decoration_color() != &Color::CurrentColor;
711 if !needs_adjustment {
712 return;
713 }
714
715 let text_style = self.style.mutate_text();
716 let new_line = (old_line & TextDecorationLine::MIXED_FLAGS) | TextDecorationLine::UNDERLINE;
718 text_style.set_text_decoration_line(new_line);
719 text_style.set_text_decoration_color(Color::CurrentColor);
720 text_style.set_text_decoration_style(TextDecorationStyle::Solid);
721 }
722
723 fn adjust_for_links<E>(&mut self, element: Option<E>)
726 where
727 E: TElement,
728 {
729 if !self.style.has_visited_style() {
730 return;
732 }
733
734 let is_link_element = self.style.pseudo.is_none() && element.is_some_and(|e| e.is_link());
735 if !is_link_element {
736 return;
737 }
738
739 if crate::pref!("layout.css.always_underline_links") {
740 self.enforce_link_underline();
741 }
742
743 if element.unwrap().is_visited_link() {
744 self.style
745 .add_flags(ComputedValueFlags::IS_RELEVANT_LINK_VISITED);
746 } else {
747 self.style
749 .remove_flags(ComputedValueFlags::IS_RELEVANT_LINK_VISITED);
750 }
751 }
752
753 fn adjust_for_justify_items(&mut self) {
758 use crate::values::specified::align;
759 let justify_items = *self.style.get_position().get_justify_items();
760 if justify_items.specified != align::JustifyItems::legacy() {
761 return;
762 }
763
764 let parent_justify_items = *self.style.get_parent_position().get_justify_items();
765
766 if !parent_justify_items.computed.contains(AlignFlags::LEGACY) {
767 return;
768 }
769
770 if parent_justify_items.computed == justify_items.computed {
771 return;
772 }
773
774 self.style
775 .mutate_position()
776 .set_computed_justify_items(parent_justify_items.computed);
777 }
778
779 fn adjust_for_appearance<E>(&mut self, element: Option<E>)
784 where
785 E: TElement,
786 {
787 use crate::properties::longhands::appearance::computed_value::T as Appearance;
788 use crate::properties::longhands::line_height::computed_value::T as LineHeight;
789
790 let box_ = self.style.get_box();
791 let appearance = match *box_.get_appearance() {
792 Appearance::Auto => *box_.get__moz_default_appearance(),
793 a => a,
794 };
795
796 if appearance == Appearance::Menulist {
797 if *self.style.get_font().get_line_height() == LineHeight::normal() {
798 return;
799 }
800 if self.style.pseudo.is_some() {
801 return;
802 }
803 let is_html_select_element =
804 element.is_some_and(|e| e.is_html_element() && e.local_name() == &*atom!("select"));
805 if !is_html_select_element {
806 return;
807 }
808 self.style
809 .mutate_font()
810 .set_line_height(LineHeight::normal());
811 }
812 }
813
814 #[cfg(feature = "gecko")]
825 fn adjust_for_marker_pseudo(&mut self, author_or_user_specified_properties: &LonghandIdSet) {
826 use crate::values::computed::counters::Content;
827 use crate::values::computed::font::{FontFamily, FontSynthesis, FontSynthesisStyle};
828 use crate::values::computed::text::{LetterSpacing, WordSpacing};
829
830 let is_legacy_marker = self.style.pseudo.is_some_and(|p| p.is_marker())
831 && self.style.get_list().get_list_style_type().is_bullet()
832 && matches!(self.style.get_counters().get_content(), Content::Normal);
833 if !is_legacy_marker {
834 return;
835 }
836 if !author_or_user_specified_properties.contains(LonghandId::FontFamily) {
837 self.style
838 .mutate_font()
839 .set_font_family(FontFamily::moz_bullet().clone());
840
841 if !author_or_user_specified_properties.contains(LonghandId::FontSynthesisWeight) {
845 self.style
846 .mutate_font()
847 .set_font_synthesis_weight(FontSynthesis::None);
848 }
849 if !author_or_user_specified_properties.contains(LonghandId::FontSynthesisStyle) {
850 self.style
851 .mutate_font()
852 .set_font_synthesis_style(FontSynthesisStyle::None);
853 }
854 }
855 if !author_or_user_specified_properties.contains(LonghandId::LetterSpacing) {
856 self.style
857 .mutate_inherited_text()
858 .set_letter_spacing(LetterSpacing::normal());
859 }
860 if !author_or_user_specified_properties.contains(LonghandId::WordSpacing) {
861 self.style
862 .mutate_inherited_text()
863 .set_word_spacing(WordSpacing::normal());
864 }
865 }
866
867 fn adjust_for_try_tactic(&mut self, tactic: &PositionTryFallbacksTryTactic) {
875 debug_assert!(!tactic.is_empty());
876 let wm = self.style.writing_mode;
878 for tactic in tactic.iter() {
880 use PositionTryFallbacksTryTacticKeyword::*;
881 match tactic {
882 FlipBlock => {
883 self.flip_self_alignment(true);
884 self.flip_insets_and_margins(wm.is_vertical());
885 },
886 FlipInline => {
887 self.flip_self_alignment(false);
888 self.flip_insets_and_margins(wm.is_horizontal());
889 },
890 FlipX => {
891 self.flip_self_alignment(wm.is_vertical());
892 self.flip_insets_and_margins(true);
893 },
894 FlipY => {
895 self.flip_self_alignment(wm.is_horizontal());
896 self.flip_insets_and_margins(false);
897 },
898 FlipStart => {
899 self.flip_start();
900 },
901 }
902 self.apply_position_area_tactic(*tactic);
903 }
904 }
905
906 fn apply_position_area_tactic(&mut self, tactic: PositionTryFallbacksTryTacticKeyword) {
907 let pos = self.style.get_position();
908 let old = *pos.get_position_area();
909 let wm = self.style.writing_mode;
910 let new = old.with_tactic(wm, tactic);
911 if new == old {
912 return;
913 }
914 let pos = self.style.mutate_position();
915 pos.set_position_area(new);
916 }
917
918 fn swap_insets(&mut self, a_side: PhysicalSide, b_side: PhysicalSide) {
920 debug_assert_ne!(a_side, b_side);
921 let pos = self.style.mutate_position();
922 let mut a = pos.get_inset(a_side).clone();
923 a.try_tactic_adjustment(a_side, b_side);
924 let mut b = pos.get_inset(b_side).clone();
925 b.try_tactic_adjustment(b_side, a_side);
926 pos.set_inset(a_side, b);
927 pos.set_inset(b_side, a);
928 }
929
930 fn swap_margins(&mut self, a_side: PhysicalSide, b_side: PhysicalSide) {
931 debug_assert_ne!(a_side, b_side);
932 let margin = self.style.get_margin();
933 let mut a = margin.get_margin(a_side).clone();
934 a.try_tactic_adjustment(a_side, b_side);
935 let mut b = margin.get_margin(b_side).clone();
936 b.try_tactic_adjustment(b_side, a_side);
937 let margin = self.style.mutate_margin();
938 margin.set_margin(a_side, b);
939 margin.set_margin(b_side, a);
940 }
941
942 fn swap_sizes(&mut self, block_start: PhysicalSide, inline_start: PhysicalSide) {
943 let pos = self.style.mutate_position();
944 let mut min_width = pos.slow_clone_min_width();
945 min_width.try_tactic_adjustment(inline_start, block_start);
946 let mut max_width = pos.slow_clone_max_width();
947 max_width.try_tactic_adjustment(inline_start, block_start);
948 let mut width = pos.slow_clone_width();
949 width.try_tactic_adjustment(inline_start, block_start);
950
951 let mut min_height = pos.slow_clone_min_height();
952 min_height.try_tactic_adjustment(block_start, inline_start);
953 let mut max_height = pos.slow_clone_max_height();
954 max_height.try_tactic_adjustment(block_start, inline_start);
955 let mut height = pos.slow_clone_height();
956 height.try_tactic_adjustment(block_start, inline_start);
957
958 let pos = self.style.mutate_position();
959 pos.set_width(height);
960 pos.set_height(width);
961 pos.set_max_width(max_height);
962 pos.set_max_height(max_width);
963 pos.set_min_width(min_height);
964 pos.set_min_height(min_width);
965 }
966
967 fn flip_start(&mut self) {
968 let wm = self.style.writing_mode;
969 let bs = wm.block_start_physical_side();
970 let is = wm.inline_start_physical_side();
971 let be = wm.block_end_physical_side();
972 let ie = wm.inline_end_physical_side();
973 self.swap_sizes(bs, is);
974 self.swap_insets(bs, is);
975 self.swap_insets(ie, be);
976 self.swap_margins(bs, is);
977 self.swap_margins(ie, be);
978 self.flip_alignment_start();
979 }
980
981 fn flip_insets_and_margins(&mut self, horizontal: bool) {
982 if horizontal {
983 self.swap_insets(PhysicalSide::Left, PhysicalSide::Right);
984 self.swap_margins(PhysicalSide::Left, PhysicalSide::Right);
985 } else {
986 self.swap_insets(PhysicalSide::Top, PhysicalSide::Bottom);
987 self.swap_margins(PhysicalSide::Top, PhysicalSide::Bottom);
988 }
989 }
990
991 fn flip_alignment_start(&mut self) {
992 let pos = self.style.get_position();
993 let align = *pos.get_align_self();
994 let mut justify = *pos.get_justify_self();
995 if align == justify {
996 return;
997 }
998
999 if matches!(justify.value(), AlignFlags::LEFT | AlignFlags::RIGHT) {
1002 let left = justify.value() == AlignFlags::LEFT;
1003 let ltr = self.style.writing_mode.is_bidi_ltr();
1004 justify = justify.with_value(if left == ltr {
1005 AlignFlags::SELF_START
1006 } else {
1007 AlignFlags::SELF_END
1008 });
1009 }
1010
1011 let pos = self.style.mutate_position();
1012 pos.set_align_self(justify);
1013 pos.set_justify_self(align);
1014 }
1015
1016 fn flip_self_alignment(&mut self, block: bool) {
1017 let pos = self.style.get_position();
1018 let cur = if block {
1019 *pos.get_align_self()
1020 } else {
1021 *pos.get_justify_self()
1022 };
1023 let flipped = cur.flip_position();
1024 if flipped == cur {
1025 return;
1026 }
1027 let pos = self.style.mutate_position();
1028 if block {
1029 pos.set_align_self(flipped);
1030 } else {
1031 pos.set_justify_self(flipped);
1032 }
1033 }
1034
1035 #[allow(unused_variables)]
1037 pub fn adjust<E>(
1038 &mut self,
1039 layout_parent_style: &ComputedValues,
1040 element: Option<E>,
1041 try_tactic: &PositionTryFallbacksTryTactic,
1042 author_or_user_specified_properties: &LonghandIdSet,
1043 ) where
1044 E: TElement,
1045 {
1046 if cfg!(debug_assertions)
1047 && let Some(e) = element
1048 && let Some(p) = e.implemented_pseudo_element()
1049 {
1050 debug_assert!(
1054 self.style.pseudo.is_some(),
1055 "Someone really messed up (no pseudo style for {e:?}, {p:?})"
1056 );
1057 }
1058 self.adjust_for_links(element);
1068 self.adjust_for_prohibited_display_contents(element);
1069 #[cfg(feature = "gecko")]
1070 {
1071 self.adjust_for_fieldset_content();
1072 self.adjust_for_text_control_editing_root();
1073 }
1074 self.adjust_for_top_layer();
1075 self.blockify_if_necessary(layout_parent_style, element);
1076 #[cfg(feature = "gecko")]
1077 self.adjust_for_webkit_line_clamp();
1078 self.adjust_for_position();
1079 self.adjust_for_overflow();
1080 #[cfg(feature = "gecko")]
1081 {
1082 self.adjust_for_contain();
1083 self.adjust_for_contain_intrinsic_size();
1084 }
1085 self.adjust_for_justify_items();
1086 self.adjust_for_table_text_align();
1087 self.adjust_for_writing_mode(layout_parent_style);
1088 #[cfg(feature = "gecko")]
1089 self.adjust_for_ruby(element);
1090 self.adjust_for_appearance(element);
1091 #[cfg(feature = "gecko")]
1092 self.adjust_for_marker_pseudo(author_or_user_specified_properties);
1093 if !try_tactic.is_empty() {
1094 self.adjust_for_try_tactic(try_tactic);
1095 }
1096 self.set_bits();
1097 }
1098}