123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219(** Declaration deduplication and shorthand composition. *)openShorthand_commonopenShorthand_boxopenShorthand_pairopenShorthand_heldopenShorthand_dedupopenShorthand_borderopenShorthand_textopenShorthand_layoutopenShorthand_timingopenShorthand_layerletis_intentionally_duplicated=Shorthand_coverage.is_intentionally_duplicatedletdeclaration_covers=Shorthand_coverage.declaration_coversletdeclarations_overlap=Shorthand_coverage.declarations_overlaptypeoverlap_key=Shorthand_coverage.overlap_keyletoverlap_key_equal=Shorthand_coverage.overlap_key_equalletoverlap_key_hash=Shorthand_coverage.overlap_key_hashletoverlap_key_compare=Shorthand_coverage.overlap_key_compareletbroad_overlap_key=Shorthand_coverage.broad_overlap_keyletoverlap_keys_broad=Shorthand_coverage.overlap_keys_broadletoverlap_keys_intersect=Shorthand_coverage.overlap_keys_intersectletdeclaration_overlap_keys=Shorthand_coverage.declaration_overlap_keysletdeclaration_slot_keys=Shorthand_coverage.declaration_slot_keysletfootprint_family=Shorthand_coverage.footprint_familyletdeclarations_overlap_with_keys=Shorthand_coverage.declarations_overlap_with_keysletdeclarations_conflict_with_keys=Shorthand_dedup.declarations_conflict_with_keysletdeclaration_is_broad=Shorthand_coverage.declaration_is_broadletcustom_property_name=Shorthand_coverage.custom_property_nameletsame_value=Shorthand_dedup.same_valueletdeclarations_commute=Shorthand_dedup.declarations_commuteletimplied_longhands=Shorthand_dedup.implied_longhandsletdrop_redundant_decoration_color_aliases=Shorthand_dedup.drop_redundant_decoration_color_aliasesletas_aliased_property=Shorthand_dedup.as_aliased_propertyletread_by_no_target=Shorthand_dedup.read_by_no_targetletis_all_declaration=Shorthand_dedup.is_all_declarationletmerge_box_shorthand_longhands=Shorthand_box.merge_box_shorthand_longhandsletmerge_overflow_longhands=Shorthand_box.merge_overflow_longhandstypeheld=Shorthand_held.heldletheld_none=Shorthand_held.held_noneletheld_add=Shorthand_held.held_add(* Run every index-based composer against the same Rule_index so we pay one
[build] + [to_list] per rule for the whole group. *)letcompose_index_group_a~ctxkept=letidx=Rule_index.build~floors:(Ctx.floorsctx)(List.mapsndkept)incompose_box_via_index~ctxidx;compose_pair_via_index~ctxidx;compose_outline_via_indexidx;compose_view_timeline_via_indexidx;compose_font_synthesis_via_indexidx;List.mapi(funid->(i,d))(Rule_index.to_listidx)(* CSS Backgrounds 3 sec. 4.4: [border] resets [border-image], which no run of
its other longhands writes. In [`Fragment] scope a sheet cascade does not see
may hold it, so the reset is a hazard there unless the rule writes every
[border-image] slot again after the run; under [`Stylesheet]
{!Shorthand_common.resets_held} asks the sheet. *)letborder_image_hazard~ctx~heldidxi~important=(matchscopectxwith|`Stylesheet->false|`Fragment->not(bi_rewritten_afteridxi~missing:border_image_bit~important))||bi_reset_hazardidxi~held~missing:border_image_bit~important(* Second index group runs after the font-reset reorder. Font + list-style +
flex + text-decoration + border all share the same index. *)letcompose_index_group_b~ctx~heldkept=letidx=Rule_index.build~floors:(Ctx.floorsctx)(List.mapsndkept)incompose_font_via_index~ctxidx;compose_list_style_via_indexidx;compose_flex_via_indexidx;compose_text_decoration_via_index~heldidx;compose_offset_via_indexidx;compose_columns_via_indexidx;compose_font_variant_via_indexidx;compose_grid_via_indexidx;compose_grid_template_via_indexidx;compose_border_via_index~ctx~bi_hazard:(border_image_hazard~ctx~heldidx)idx;compose_line_via_indexidx;compose_border_sides_via_index~ctx~bi_hazard:(border_image_hazard~ctx~heldidx)idx;List.mapi(funid->(i,d))(Rule_index.to_listidx)(* Third index group runs at the very end: mask + transition + animation share
one index. *)letcompose_index_group_c~ctx~heldkept=letidx=Rule_index.build~floors:(Ctx.floorsctx)(List.mapsndkept)incompose_mask_via_index~ctxidx;compose_transition_via_index~ctx~heldidx;compose_animation_via_index~ctx~heldidx;List.mapi(funid->(i,d))(Rule_index.to_listidx)letcompose_border_whole_step~held~ctxkept=letidx=Rule_index.build~floors:(Ctx.floorsctx)(List.mapsndkept)incompose_border_whole_via_index~bi_hazard:(border_image_hazard~ctx~heldidx)~ctxidx;List.mapi(funid->(i,d))(Rule_index.to_listidx)letcompose_shorthands?(held=held_none)~ctxkept=kept|>compose_index_group_a~ctx|>reorder_font_resets_before_font|>compose_index_group_b~ctx~held|>reorder_border_image_before_border|>compose_border_whole_step~held~ctx|>drop_bimg_shadowed_by_border|>compose_border_image_shorthand~ctx|>compose_background_shorthand~ctx|>reorder_mask_border_before_mask|>compose_index_group_c~ctx~held|>funkept->letfloors=Ctx.floorsctxinmerge_box_shorthand_longhands~floorskeptkept|>merge_overflow_longhands~floorsletreccomposition_family=function|Declaration.Declaration{property;_}->Properties.composition_familyproperty|Theme_guarded{decl;_}->composition_familydecl(* Whether two declarations of [kept] are of one composition family, [seen]
holding the families of those before. *)letrecshares_familyseen=function|[]->false|(_,decl)::rest->(matchcomposition_familydeclwith|None->shares_familyseenrest|Somefamily->letbit=1lslProperties.family_rankfamilyinseenlandbit<>0||shares_family(seenlorbit)rest)(* What follows the reduction of [kept]: composition, when [merge_box], then the
vendor aliases and the longhands the shorthands now restate. *)letcompose_reduced~held~ctx~merge_boxpropskept=letkept=(* Composition reads a family's declarations together, so a body with at
most one declaration per family composes to itself in either scope, as
one declaration on its own does; the tests of that ask every value the
grammar inventory models, and every pair of families. *)(* A shorthand composition writes can override an earlier declaration of
the same property whole, which only the deduplication sees; what that
drops can let another composition through. Each round drops a
declaration, so this ends. *)letreccomposekept=matchkeptwith|_::_::_whenmerge_box&&shares_family0kept->letcomposed=compose_shorthands~held~ctxkeptinifList.equal(fun(_,a)(_,b)->a==b)composedkeptthenkeptelseletdeduplicated=List.rev(List.fold_left(deduplicate_step~enforce_spec:(Ctx.enforce_specctx)~floors:(Ctx.floorsctx)~judged:(Option.is_some(Ctx.judgectx)))[]composed)inifList.lengthdeduplicated<List.lengthcomposedthencomposededuplicatedelsecomposed|_->keptinletkept=composekeptinletkept=drop_vendor_aliases~ctxkeptinList.map(fun(_,decl)->decl)keptin(* Composition writes shorthands the input did not carry, so the longhands one
of them now restates are only visible on a second pass. *)letkept=drop_longhands_after_covering_shorthand~enforce_spec:(Ctx.enforce_specctx)keptin(* Each pipeline step keeps untouched declarations, so [preserve_list]
restores the input list when every declaration is physically unchanged -
callers detect a no-op by identity (the factoring fixpoint relies on
it). *)preserve_listpropskeptletdeduplicate_declarations_with?(held=held_none)~ctx?(merge_box=true)props=letheld=held_outsideheldpropsinletprops=drop_longhands_after_covering_shorthand~enforce_spec:(Ctx.enforce_specctx)propsinletindexed_props=List.mapi(funidecl->(i,decl))propsinletkept=List.rev(List.fold_left(deduplicate_step~enforce_spec:(Ctx.enforce_specctx)~floors:(Ctx.floorsctx)~judged:(Option.is_some(Ctx.judgectx)))[]indexed_props)incompose_reduced~held~ctx~merge_boxpropskeptletcompose_deduplicated?(held=held_none)~ctxprops=letheld=held_outsideheldpropsincompose_reduced~held~ctx~merge_box:trueprops(List.mapi(funidecl->(i,decl))props)letdeduplicate_declarations?scopeprops=deduplicate_declarations_with~ctx:(Ctx.of_scopescope)props