Something went wrong. Try again.
we (web engine): Experimental web browser project to understand the limits of Claude
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475//! Block layout engine: box generation, block/inline layout, and text wrapping.//!//! Builds a layout tree from a styled tree (DOM + computed styles) and positions//! block-level elements vertically with proper inline formatting context.
use std::collections::HashMap;
use we_css::values::Color;use we_dom::{Document, NodeData, NodeId};use we_style::computed::{ AlignContent, AlignItems, AlignSelf, BorderStyle, BoxSizing, Clear, ComputedStyle, Display, FlexDirection, FlexWrap, Float, JustifyContent, LengthOrAuto, Overflow, Position, StyledNode, TextAlign, TextDecoration, Visibility,};use we_text::font::Font;
/// Width of scroll bars in pixels.pub const SCROLLBAR_WIDTH: f32 = 15.0;
/// Edge sizes for box model (margin, padding, border).#[derive(Debug, Clone, Copy, Default, PartialEq)]pub struct EdgeSizes { pub top: f32, pub right: f32, pub bottom: f32, pub left: f32,}
/// A positioned rectangle with content area dimensions.#[derive(Debug, Clone, Copy, Default, PartialEq)]pub struct Rect { pub x: f32, pub y: f32, pub width: f32, pub height: f32,}
/// The type of layout box.#[derive(Debug)]pub enum BoxType { /// Block-level box from an element. Block(NodeId), /// Inline-level box from an element. Inline(NodeId), /// A run of text from a text node. TextRun { node: NodeId, text: String }, /// Anonymous block wrapping inline content within a block container. Anonymous,}
/// A single positioned text fragment with its own styling.////// Multiple fragments can share the same y-coordinate when they are/// on the same visual line (e.g. `<p>Hello <em>world</em></p>` produces/// two fragments at the same y).#[derive(Debug, Clone, PartialEq)]pub struct TextLine { pub text: String, pub x: f32, pub y: f32, pub width: f32, pub font_size: f32, pub color: Color, pub text_decoration: TextDecoration, pub background_color: Color,}
/// A box in the layout tree with dimensions and child boxes.#[derive(Debug)]pub struct LayoutBox { pub box_type: BoxType, pub display: Display, pub rect: Rect, pub margin: EdgeSizes, pub padding: EdgeSizes, pub border: EdgeSizes, pub children: Vec<LayoutBox>, pub font_size: f32, /// Positioned text fragments (populated for boxes with inline content). pub lines: Vec<TextLine>, /// Text color. pub color: Color, /// Background color. pub background_color: Color, /// Text decoration (underline, etc.). pub text_decoration: TextDecoration, /// Border styles (top, right, bottom, left). pub border_styles: [BorderStyle; 4], /// Border colors (top, right, bottom, left). pub border_colors: [Color; 4], /// Text alignment for this box's inline content. pub text_align: TextAlign, /// Computed line height in px. pub line_height: f32, /// For replaced elements (e.g., `<img>`): content dimensions (width, height). pub replaced_size: Option<(f32, f32)>, /// CSS `position` property. pub position: Position, /// CSS `z-index` property (None = auto). pub z_index: Option<i32>, /// Relative position offset (dx, dy) applied after normal flow layout. pub relative_offset: (f32, f32), /// CSS `overflow` property. pub overflow: Overflow, /// CSS `box-sizing` property. pub box_sizing: BoxSizing, /// CSS `width` property (explicit or auto, may contain percentage). pub css_width: LengthOrAuto, /// CSS `height` property (explicit or auto, may contain percentage). pub css_height: LengthOrAuto, /// CSS margin values (may contain percentages for layout resolution). pub css_margin: [LengthOrAuto; 4], /// CSS padding values (may contain percentages for layout resolution). pub css_padding: [LengthOrAuto; 4], /// CSS position offset values (top, right, bottom, left) for relative/sticky positioning. pub css_offsets: [LengthOrAuto; 4], /// For `position: sticky`: the containing block's content rect in document /// coordinates. Paint-time logic clamps the element within this rectangle. pub sticky_constraint: Option<Rect>, /// CSS `visibility` property. pub visibility: Visibility, /// CSS `float` property. pub float: Float, /// CSS `clear` property. pub clear: Clear, /// Natural content height before CSS height override. /// Used to determine overflow for scroll containers. pub content_height: f32, // Flex container properties pub flex_direction: FlexDirection, pub flex_wrap: FlexWrap, pub justify_content: JustifyContent, pub align_items: AlignItems, pub align_content: AlignContent, pub row_gap: f32, pub column_gap: f32, // Flex item properties pub flex_grow: f32, pub flex_shrink: f32, pub flex_basis: LengthOrAuto, pub align_self: AlignSelf, pub order: i32,}
impl LayoutBox { fn new(box_type: BoxType, style: &ComputedStyle) -> Self { LayoutBox { box_type, display: style.display, rect: Rect::default(), margin: EdgeSizes::default(), padding: EdgeSizes::default(), border: EdgeSizes::default(), children: Vec::new(), font_size: style.font_size, lines: Vec::new(), color: style.color, background_color: style.background_color, text_decoration: style.text_decoration, border_styles: [ style.border_top_style, style.border_right_style, style.border_bottom_style, style.border_left_style, ], border_colors: [ style.border_top_color, style.border_right_color, style.border_bottom_color, style.border_left_color, ], text_align: style.text_align, line_height: style.line_height, replaced_size: None, position: style.position, z_index: style.z_index, relative_offset: (0.0, 0.0), overflow: style.overflow, box_sizing: style.box_sizing, css_width: style.width, css_height: style.height, css_margin: [ style.margin_top, style.margin_right, style.margin_bottom, style.margin_left, ], css_padding: [ style.padding_top, style.padding_right, style.padding_bottom, style.padding_left, ], css_offsets: [style.top, style.right, style.bottom, style.left], sticky_constraint: None, visibility: style.visibility, float: style.float, clear: style.clear, content_height: 0.0, flex_direction: style.flex_direction, flex_wrap: style.flex_wrap, justify_content: style.justify_content, align_items: style.align_items, align_content: style.align_content, row_gap: style.row_gap, column_gap: style.column_gap, flex_grow: style.flex_grow, flex_shrink: style.flex_shrink, flex_basis: style.flex_basis, align_self: style.align_self, order: style.order, } }
/// Total height including margin, border, and padding. pub fn margin_box_height(&self) -> f32 { self.margin.top + self.border.top + self.padding.top + self.rect.height + self.padding.bottom + self.border.bottom + self.margin.bottom }
/// Iterate over all boxes in depth-first pre-order. pub fn iter(&self) -> LayoutBoxIter<'_> { LayoutBoxIter { stack: vec![self] } }}
/// Depth-first pre-order iterator over layout boxes.pub struct LayoutBoxIter<'a> { stack: Vec<&'a LayoutBox>,}
impl<'a> Iterator for LayoutBoxIter<'a> { type Item = &'a LayoutBox;
fn next(&mut self) -> Option<&'a LayoutBox> { let node = self.stack.pop()?; for child in node.children.iter().rev() { self.stack.push(child); } Some(node) }}
/// The result of laying out a document.#[derive(Debug)]pub struct LayoutTree { pub root: LayoutBox, pub width: f32, pub height: f32,}
impl LayoutTree { /// Iterate over all layout boxes in depth-first pre-order. pub fn iter(&self) -> LayoutBoxIter<'_> { self.root.iter() }}
// ---------------------------------------------------------------------------// Resolve LengthOrAuto to f32// ---------------------------------------------------------------------------
/// Resolve a `LengthOrAuto` to px. Percentages are resolved against/// `reference` (typically the containing block width). Auto resolves to 0.fn resolve_length_against(value: LengthOrAuto, reference: f32) -> f32 { match value { LengthOrAuto::Length(px) => px, LengthOrAuto::Percentage(p) => p / 100.0 * reference, LengthOrAuto::Auto => 0.0, }}
/// Resolve horizontal offset for `position: relative`./// If both `left` and `right` are specified, `left` wins (CSS2 §9.4.3, ltr).fn resolve_relative_horizontal(left: LengthOrAuto, right: LengthOrAuto, cb_width: f32) -> f32 { match left { LengthOrAuto::Length(px) => px, LengthOrAuto::Percentage(p) => p / 100.0 * cb_width, LengthOrAuto::Auto => match right { LengthOrAuto::Length(px) => -px, LengthOrAuto::Percentage(p) => -(p / 100.0 * cb_width), LengthOrAuto::Auto => 0.0, }, }}
/// Resolve vertical offset for `position: relative`./// If both `top` and `bottom` are specified, `top` wins (CSS2 §9.4.3).fn resolve_relative_vertical(top: LengthOrAuto, bottom: LengthOrAuto, cb_height: f32) -> f32 { match top { LengthOrAuto::Length(px) => px, LengthOrAuto::Percentage(p) => p / 100.0 * cb_height, LengthOrAuto::Auto => match bottom { LengthOrAuto::Length(px) => -px, LengthOrAuto::Percentage(p) => -(p / 100.0 * cb_height), LengthOrAuto::Auto => 0.0, }, }}
// ---------------------------------------------------------------------------// Build layout tree from styled tree// ---------------------------------------------------------------------------
fn build_box( styled: &StyledNode, doc: &Document, image_sizes: &HashMap<NodeId, (f32, f32)>,) -> Option<LayoutBox> { let node = styled.node; let style = &styled.style;
match doc.node_data(node) { NodeData::Document => { let mut children = Vec::new(); for child in &styled.children { if let Some(child_box) = build_box(child, doc, image_sizes) { children.push(child_box); } } if children.len() == 1 { children.into_iter().next() } else if children.is_empty() { None } else { let mut b = LayoutBox::new(BoxType::Anonymous, style); b.children = children; Some(b) } } NodeData::Element { .. } => { if style.display == Display::None { return None; }
// Margin and padding: resolve absolute lengths now; percentages // will be re-resolved in compute_layout against containing block. // Use 0.0 as a placeholder reference for percentages — they'll be // resolved properly in compute_layout. let margin = EdgeSizes { top: resolve_length_against(style.margin_top, 0.0), right: resolve_length_against(style.margin_right, 0.0), bottom: resolve_length_against(style.margin_bottom, 0.0), left: resolve_length_against(style.margin_left, 0.0), }; let padding = EdgeSizes { top: resolve_length_against(style.padding_top, 0.0), right: resolve_length_against(style.padding_right, 0.0), bottom: resolve_length_against(style.padding_bottom, 0.0), left: resolve_length_against(style.padding_left, 0.0), }; let border = EdgeSizes { top: if style.border_top_style != BorderStyle::None { style.border_top_width } else { 0.0 }, right: if style.border_right_style != BorderStyle::None { style.border_right_width } else { 0.0 }, bottom: if style.border_bottom_style != BorderStyle::None { style.border_bottom_width } else { 0.0 }, left: if style.border_left_style != BorderStyle::None { style.border_left_width } else { 0.0 }, };
let mut children = Vec::new(); for child in &styled.children { if let Some(child_box) = build_box(child, doc, image_sizes) { children.push(child_box); } }
// Per CSS2 §9.7: float forces display to block. let effective_display = if style.float != Float::None && style.display == Display::Inline { Display::Block } else { style.display };
let box_type = match effective_display { Display::Block | Display::Flex | Display::InlineFlex => BoxType::Block(node), Display::Inline => BoxType::Inline(node), Display::None => unreachable!(), };
if effective_display == Display::Block { children = normalize_children(children, style); }
let mut b = LayoutBox::new(box_type, style); b.margin = margin; b.padding = padding; b.border = border; b.children = children;
// Check for replaced element (e.g., <img>). if let Some(&(w, h)) = image_sizes.get(&node) { b.replaced_size = Some((w, h)); }
// Relative position offsets are resolved in compute_layout // where the containing block dimensions are known.
Some(b) } NodeData::Text { data } => { let collapsed = collapse_whitespace(data); if collapsed.is_empty() { return None; } Some(LayoutBox::new( BoxType::TextRun { node, text: collapsed, }, style, )) } NodeData::Comment { .. } => None, }}
/// Collapse runs of whitespace to a single space.fn collapse_whitespace(s: &str) -> String { let mut result = String::new(); let mut in_ws = false; for ch in s.chars() { if ch.is_whitespace() { if !in_ws { result.push(' '); } in_ws = true; } else { in_ws = false; result.push(ch); } } result}
/// If a block container has a mix of block-level and inline-level children,/// wrap consecutive inline runs in anonymous block boxes./// Out-of-flow elements (absolute/fixed) are excluded from the determination/// and placed at the top level without affecting normalization.fn normalize_children(children: Vec<LayoutBox>, parent_style: &ComputedStyle) -> Vec<LayoutBox> { if children.is_empty() { return children; }
// Consider in-flow block children and floated children as "block-level" // for normalization purposes. When floats are present alongside inline // content, the inline content must be wrapped in anonymous blocks so that // `layout_block_children` can lay it out via its inline formatting context. let has_block = children .iter() .any(|c| (is_in_flow(c) && is_block_level(c)) || is_floated(c)); if !has_block { return children; }
let has_inline = children.iter().any(|c| is_in_flow(c) && !is_block_level(c)); if !has_inline { return children; }
let mut result = Vec::new(); let mut inline_group: Vec<LayoutBox> = Vec::new();
for child in children { if !is_in_flow(&child) || is_block_level(&child) { // Out-of-flow or block-level: flush any pending inline group first. if !inline_group.is_empty() { let mut anon = LayoutBox::new(BoxType::Anonymous, parent_style); anon.children = std::mem::take(&mut inline_group); result.push(anon); } result.push(child); } else { inline_group.push(child); } }
if !inline_group.is_empty() { let mut anon = LayoutBox::new(BoxType::Anonymous, parent_style); anon.children = inline_group; result.push(anon); }
result}
fn is_block_level(b: &LayoutBox) -> bool { matches!(b.box_type, BoxType::Block(_) | BoxType::Anonymous)}
/// Returns `true` if this box is in normal flow (not absolutely/fixed positioned, not floated).fn is_in_flow(b: &LayoutBox) -> bool { b.position != Position::Absolute && b.position != Position::Fixed && b.float == Float::None}
/// Returns `true` if this box is floated.fn is_floated(b: &LayoutBox) -> bool { b.float != Float::None}
// ---------------------------------------------------------------------------// Float tracking// ---------------------------------------------------------------------------
/// A positioned float rectangle used for tracking placed floats.#[derive(Debug, Clone, Copy)]struct PlacedFloat { /// Left edge of the float's margin box. x: f32, /// Top edge of the float's margin box. y: f32, /// Width of the float's margin box. width: f32, /// Height of the float's margin box. height: f32, /// Which side this float is on. side: Float,}
/// Tracks active floats within a block formatting context.#[derive(Debug, Default)]struct FloatContext { floats: Vec<PlacedFloat>,}
impl FloatContext { /// Get the available x-range at a given y position, narrowed by active floats. /// Returns (left_edge, right_edge) within the containing block. fn available_range( &self, y: f32, line_height: f32, container_x: f32, container_width: f32, ) -> (f32, f32) { let mut left = container_x; let mut right = container_x + container_width;
for f in &self.floats { let float_top = f.y; let float_bottom = f.y + f.height;
// Check if this float overlaps vertically with the line. if y + line_height > float_top && y < float_bottom { match f.side { Float::Left => { let float_right_edge = f.x + f.width; if float_right_edge > left { left = float_right_edge; } } Float::Right => { let float_left_edge = f.x; if float_left_edge < right { right = float_left_edge; } } Float::None => {} } } }
(left, right) }
/// Find the y position below all floats that match the given clear side. fn clear_y(&self, clear: Clear) -> f32 { let mut y = 0.0f32; for f in &self.floats { let dominated = match clear { Clear::Left => f.side == Float::Left, Clear::Right => f.side == Float::Right, Clear::Both => true, Clear::None => false, }; if dominated { let bottom = f.y + f.height; if bottom > y { y = bottom; } } } y }
/// Find the bottom edge of all placed floats. fn max_float_bottom(&self) -> f32 { let mut bottom = 0.0f32; for f in &self.floats { let fb = f.y + f.height; if fb > bottom { bottom = fb; } } bottom }
/// Place a float and return its position. fn place_float( &mut self, float_side: Float, float_width: f32, float_height: f32, cursor_y: f32, container_x: f32, container_width: f32, ) -> (f32, f32) { // Start at cursor_y and find a position where the float fits. let mut y = cursor_y;
loop { let (left, right) = self.available_range(y, float_height, container_x, container_width); let available = right - left;
if available >= float_width || available >= container_width { let x = match float_side { Float::Left => left, Float::Right => right - float_width, Float::None => unreachable!(), }; self.floats.push(PlacedFloat { x, y, width: float_width, height: float_height, side: float_side, }); return (x, y); }
// Move down below the topmost interfering float and try again. let mut next_y = f32::MAX; for f in &self.floats { let fb = f.y + f.height; if fb > y && fb < next_y { next_y = fb; } } if next_y == f32::MAX { // No more floats to clear, place at current position. let x = match float_side { Float::Left => container_x, Float::Right => container_x + container_width - float_width, Float::None => unreachable!(), }; self.floats.push(PlacedFloat { x, y, width: float_width, height: float_height, side: float_side, }); return (x, y); } y = next_y; } }}
// ---------------------------------------------------------------------------// Layout algorithm// ---------------------------------------------------------------------------
/// Position and size a layout box within `available_width` at position (`x`, `y`).////// `available_width` is the containing block width — used as the reference for/// percentage widths, margins, and paddings (per CSS spec, even vertical margins//// padding resolve against the containing block width).////// `abs_cb` is the padding box of the nearest positioned ancestor, used as the/// containing block for absolutely positioned descendants.#[allow(clippy::too_many_arguments)]fn compute_layout( b: &mut LayoutBox, x: f32, y: f32, available_width: f32, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document, abs_cb: Rect, float_ctx: Option<&FloatContext>,) { // Resolve percentage margins against containing block width. // Only re-resolve percentages — absolute margins may have been modified // by margin collapsing and must not be overwritten. if matches!(b.css_margin[0], LengthOrAuto::Percentage(_)) { b.margin.top = resolve_length_against(b.css_margin[0], available_width); } if matches!(b.css_margin[1], LengthOrAuto::Percentage(_)) { b.margin.right = resolve_length_against(b.css_margin[1], available_width); } if matches!(b.css_margin[2], LengthOrAuto::Percentage(_)) { b.margin.bottom = resolve_length_against(b.css_margin[2], available_width); } if matches!(b.css_margin[3], LengthOrAuto::Percentage(_)) { b.margin.left = resolve_length_against(b.css_margin[3], available_width); }
// Resolve percentage padding against containing block width. if matches!(b.css_padding[0], LengthOrAuto::Percentage(_)) { b.padding.top = resolve_length_against(b.css_padding[0], available_width); } if matches!(b.css_padding[1], LengthOrAuto::Percentage(_)) { b.padding.right = resolve_length_against(b.css_padding[1], available_width); } if matches!(b.css_padding[2], LengthOrAuto::Percentage(_)) { b.padding.bottom = resolve_length_against(b.css_padding[2], available_width); } if matches!(b.css_padding[3], LengthOrAuto::Percentage(_)) { b.padding.left = resolve_length_against(b.css_padding[3], available_width); }
let content_x = x + b.margin.left + b.border.left + b.padding.left; let content_y = y + b.margin.top + b.border.top + b.padding.top;
let horizontal_extra = b.border.left + b.border.right + b.padding.left + b.padding.right;
// Resolve content width: explicit CSS width (adjusted for box-sizing) or auto. let content_width = match b.css_width { LengthOrAuto::Length(w) => match b.box_sizing { BoxSizing::ContentBox => w.max(0.0), BoxSizing::BorderBox => (w - horizontal_extra).max(0.0), }, LengthOrAuto::Percentage(p) => { let resolved = p / 100.0 * available_width; match b.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - horizontal_extra).max(0.0), } } LengthOrAuto::Auto => { (available_width - b.margin.left - b.margin.right - horizontal_extra).max(0.0) } };
b.rect.x = content_x; b.rect.y = content_y; b.rect.width = content_width;
// For overflow:scroll, reserve space for vertical scrollbar. if b.overflow == Overflow::Scroll { b.rect.width = (b.rect.width - SCROLLBAR_WIDTH).max(0.0); }
// Replaced elements (e.g., <img>) have intrinsic dimensions. if let Some((rw, rh)) = b.replaced_size { b.rect.width = rw.min(b.rect.width); b.rect.height = rh; set_sticky_constraints(b); layout_abspos_children(b, abs_cb, viewport_width, viewport_height, font, doc); apply_relative_offset(b, available_width, viewport_height); return; }
match &b.box_type { BoxType::Block(_) | BoxType::Anonymous => { if matches!(b.display, Display::Flex | Display::InlineFlex) { layout_flex_children(b, viewport_width, viewport_height, font, doc, abs_cb); } else if has_block_children(b) || has_float_children(b) { layout_block_children(b, viewport_width, viewport_height, font, doc, abs_cb); } else { layout_inline_children(b, font, doc, float_ctx); } } BoxType::TextRun { .. } | BoxType::Inline(_) => { // Handled by the parent's inline layout. } }
// Save the natural content height before CSS height override. b.content_height = b.rect.height;
// Apply explicit CSS height (adjusted for box-sizing), overriding auto height. match b.css_height { LengthOrAuto::Length(h) => { let vertical_extra = b.border.top + b.border.bottom + b.padding.top + b.padding.bottom; b.rect.height = match b.box_sizing { BoxSizing::ContentBox => h.max(0.0), BoxSizing::BorderBox => (h - vertical_extra).max(0.0), }; } LengthOrAuto::Percentage(p) => { // Height percentage resolves against containing block height. // For the root element, use viewport height. let cb_height = viewport_height; let resolved = p / 100.0 * cb_height; let vertical_extra = b.border.top + b.border.bottom + b.padding.top + b.padding.bottom; b.rect.height = match b.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - vertical_extra).max(0.0), }; } LengthOrAuto::Auto => {} }
// Set sticky constraint rects now that this box's dimensions are final. set_sticky_constraints(b);
// Layout absolutely and fixed positioned children after this box's // dimensions are fully resolved. layout_abspos_children(b, abs_cb, viewport_width, viewport_height, font, doc);
apply_relative_offset(b, available_width, viewport_height);}
/// For each direct child with `position: sticky`, record the parent's content/// rect as the sticky constraint rectangle. This is called after the parent's/// dimensions are fully resolved so that the rect is accurate.fn set_sticky_constraints(parent: &mut LayoutBox) { let content_rect = parent.rect; for child in &mut parent.children { if child.position == Position::Sticky { child.sticky_constraint = Some(content_rect); } }}
/// Apply `position: relative` offset to a box and all its descendants.////// Resolves the CSS position offsets (which may contain percentages) and/// shifts the visual position without affecting the normal-flow layout.fn apply_relative_offset(b: &mut LayoutBox, cb_width: f32, cb_height: f32) { if b.position != Position::Relative { return; } let [top, right, bottom, left] = b.css_offsets; let dx = resolve_relative_horizontal(left, right, cb_width); let dy = resolve_relative_vertical(top, bottom, cb_height); b.relative_offset = (dx, dy); if dx == 0.0 && dy == 0.0 { return; } shift_box(b, dx, dy);}
/// Recursively shift a box and all its descendants by (dx, dy).fn shift_box(b: &mut LayoutBox, dx: f32, dy: f32) { b.rect.x += dx; b.rect.y += dy; for line in &mut b.lines { line.x += dx; line.y += dy; } for child in &mut b.children { shift_box(child, dx, dy); }}
// ---------------------------------------------------------------------------// Absolute / fixed positioning// ---------------------------------------------------------------------------
/// Resolve a `LengthOrAuto` offset to an optional pixel value./// Returns `None` for `Auto` (offset not specified).fn resolve_offset(value: LengthOrAuto, reference: f32) -> Option<f32> { match value { LengthOrAuto::Length(px) => Some(px), LengthOrAuto::Percentage(p) => Some(p / 100.0 * reference), LengthOrAuto::Auto => None, }}
/// Compute the padding box rectangle for a layout box.fn padding_box_rect(b: &LayoutBox) -> Rect { Rect { x: b.rect.x - b.padding.left, y: b.rect.y - b.padding.top, width: b.rect.width + b.padding.left + b.padding.right, height: b.rect.height + b.padding.top + b.padding.bottom, }}
/// Lay out all absolutely and fixed positioned children of `parent`.////// `abs_cb` is the padding box of the nearest positioned ancestor passed from/// further up the tree. If `parent` itself is positioned (not `static`), its/// padding box becomes the new containing block for absolute descendants.fn layout_abspos_children( parent: &mut LayoutBox, abs_cb: Rect, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document,) { let viewport_cb = Rect { x: 0.0, y: 0.0, width: viewport_width, height: viewport_height, };
// If this box is positioned, it becomes the containing block for absolute // descendants. let new_abs_cb = if parent.position != Position::Static { padding_box_rect(parent) } else { abs_cb };
for i in 0..parent.children.len() { if parent.children[i].position == Position::Absolute { layout_absolute_child( &mut parent.children[i], new_abs_cb, viewport_width, viewport_height, font, doc, ); } else if parent.children[i].position == Position::Fixed { layout_absolute_child( &mut parent.children[i], viewport_cb, viewport_width, viewport_height, font, doc, ); } }}
/// Lay out a single absolutely or fixed positioned element.////// `cb` is the containing block (padding box of nearest positioned ancestor/// for absolute, or the viewport for fixed).fn layout_absolute_child( child: &mut LayoutBox, cb: Rect, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document,) { let [css_top, css_right, css_bottom, css_left] = child.css_offsets;
// Resolve margins against containing block width. child.margin = EdgeSizes { top: resolve_length_against(child.css_margin[0], cb.width), right: resolve_length_against(child.css_margin[1], cb.width), bottom: resolve_length_against(child.css_margin[2], cb.width), left: resolve_length_against(child.css_margin[3], cb.width), };
// Resolve padding against containing block width. child.padding = EdgeSizes { top: resolve_length_against(child.css_padding[0], cb.width), right: resolve_length_against(child.css_padding[1], cb.width), bottom: resolve_length_against(child.css_padding[2], cb.width), left: resolve_length_against(child.css_padding[3], cb.width), };
let horiz_extra = child.border.left + child.border.right + child.padding.left + child.padding.right; let vert_extra = child.border.top + child.border.bottom + child.padding.top + child.padding.bottom;
// Resolve offsets. let left_offset = resolve_offset(css_left, cb.width); let right_offset = resolve_offset(css_right, cb.width); let top_offset = resolve_offset(css_top, cb.height); let bottom_offset = resolve_offset(css_bottom, cb.height);
// --- Resolve content width --- let content_width = match child.css_width { LengthOrAuto::Length(w) => match child.box_sizing { BoxSizing::ContentBox => w.max(0.0), BoxSizing::BorderBox => (w - horiz_extra).max(0.0), }, LengthOrAuto::Percentage(p) => { let resolved = p / 100.0 * cb.width; match child.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - horiz_extra).max(0.0), } } LengthOrAuto::Auto => { // If both left and right are specified, stretch to fill. if let (Some(l), Some(r)) = (left_offset, right_offset) { (cb.width - l - r - child.margin.left - child.margin.right - horiz_extra).max(0.0) } else { // Use available width minus margins. (cb.width - child.margin.left - child.margin.right - horiz_extra).max(0.0) } } };
child.rect.width = content_width;
// --- Resolve horizontal position --- child.rect.x = if let Some(l) = left_offset { cb.x + l + child.margin.left + child.border.left + child.padding.left } else if let Some(r) = right_offset { cb.x + cb.width - r - child.margin.right - child.border.right - child.padding.right - content_width } else { // Static position: containing block origin. cb.x + child.margin.left + child.border.left + child.padding.left };
// Set a temporary y for child content layout. child.rect.y = cb.y;
// For overflow:scroll, reserve scrollbar width. if child.overflow == Overflow::Scroll { child.rect.width = (child.rect.width - SCROLLBAR_WIDTH).max(0.0); }
// --- Layout child's own content --- if let Some((rw, rh)) = child.replaced_size { child.rect.width = rw.min(child.rect.width); child.rect.height = rh; } else { let child_abs_cb = if child.position != Position::Static { padding_box_rect(child) } else { cb }; match &child.box_type { BoxType::Block(_) | BoxType::Anonymous => { if matches!(child.display, Display::Flex | Display::InlineFlex) { layout_flex_children( child, viewport_width, viewport_height, font, doc, child_abs_cb, ); } else if has_block_children(child) || has_float_children(child) { layout_block_children( child, viewport_width, viewport_height, font, doc, child_abs_cb, ); } else { layout_inline_children(child, font, doc, None); } } _ => {} } }
// Save natural content height. child.content_height = child.rect.height;
// --- Resolve content height --- match child.css_height { LengthOrAuto::Length(h) => { child.rect.height = match child.box_sizing { BoxSizing::ContentBox => h.max(0.0), BoxSizing::BorderBox => (h - vert_extra).max(0.0), }; } LengthOrAuto::Percentage(p) => { let resolved = p / 100.0 * cb.height; child.rect.height = match child.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - vert_extra).max(0.0), }; } LengthOrAuto::Auto => { // If both top and bottom are specified, stretch. if let (Some(t), Some(b_val)) = (top_offset, bottom_offset) { child.rect.height = (cb.height - t - b_val - child.margin.top - child.margin.bottom - vert_extra) .max(0.0); } // Otherwise keep content height. } }
// --- Resolve vertical position --- let final_y = if let Some(t) = top_offset { cb.y + t + child.margin.top + child.border.top + child.padding.top } else if let Some(b_val) = bottom_offset { cb.y + cb.height - b_val - child.margin.bottom - child.border.bottom - child.padding.bottom - child.rect.height } else { // Static position: containing block origin. cb.y + child.margin.top + child.border.top + child.padding.top };
// Shift from temporary y to final y. let dy = final_y - child.rect.y; if dy != 0.0 { shift_box(child, 0.0, dy); }
// Set sticky constraints now that this child's dimensions are final. set_sticky_constraints(child);
// Recursively lay out any absolutely positioned grandchildren. let new_abs_cb = if child.position != Position::Static { padding_box_rect(child) } else { cb }; layout_abspos_children( child, new_abs_cb, viewport_width, viewport_height, font, doc, );}
fn has_block_children(b: &LayoutBox) -> bool { b.children .iter() .any(|c| is_in_flow(c) && is_block_level(c))}
fn has_float_children(b: &LayoutBox) -> bool { b.children.iter().any(is_floated)}
/// Collapse two adjoining margins per CSS2 §8.3.1.////// Both non-negative → use the larger./// Both negative → use the more negative./// Mixed → sum the largest positive and most negative.fn collapse_margins(a: f32, b: f32) -> f32 { if a >= 0.0 && b >= 0.0 { a.max(b) } else if a < 0.0 && b < 0.0 { a.min(b) } else { a + b }}
/// Returns `true` if this box establishes a new block formatting context,/// which prevents its margins from collapsing with children.fn establishes_bfc(b: &LayoutBox) -> bool { b.overflow != Overflow::Visible || matches!(b.display, Display::Flex | Display::InlineFlex) || b.float != Float::None}
/// Returns `true` if a block box has no in-flow content (empty block).fn is_empty_block(b: &LayoutBox) -> bool { b.children.is_empty() && b.lines.is_empty() && b.replaced_size.is_none() && matches!(b.css_height, LengthOrAuto::Auto)}
/// Pre-collapse parent-child margins (CSS2 §8.3.1).////// When a parent has no border/padding/BFC separating it from its first/last/// child, the child's margin collapses into the parent's margin. This must/// happen *before* positioning so the parent is placed using the collapsed/// value. The function walks bottom-up: children are pre-collapsed first, then/// their (possibly enlarged) margins are folded into the parent.fn pre_collapse_margins(b: &mut LayoutBox) { // Recurse into in-flow block children first (bottom-up). for child in &mut b.children { if is_in_flow(child) && is_block_level(child) { pre_collapse_margins(child); } }
if !matches!(b.box_type, BoxType::Block(_) | BoxType::Anonymous) { return; } if establishes_bfc(b) { return; } if !has_block_children(b) { return; }
// --- Top: collapse with first non-empty child --- if b.border.top == 0.0 && b.padding.top == 0.0 { if let Some(child_top) = first_block_top_margin(&b.children) { b.margin.top = collapse_margins(b.margin.top, child_top); } }
// --- Bottom: collapse with last non-empty child --- if b.border.bottom == 0.0 && b.padding.bottom == 0.0 { if let Some(child_bottom) = last_block_bottom_margin(&b.children) { b.margin.bottom = collapse_margins(b.margin.bottom, child_bottom); } }}
/// Top margin of the first non-empty in-flow block child (already pre-collapsed).fn first_block_top_margin(children: &[LayoutBox]) -> Option<f32> { for child in children { if is_in_flow(child) && is_block_level(child) { if is_empty_block(child) { continue; } return Some(child.margin.top); } } // All in-flow block children empty — fold all their collapsed margins. let mut m = 0.0f32; for child in children .iter() .filter(|c| is_in_flow(c) && is_block_level(c)) { m = collapse_margins(m, collapse_margins(child.margin.top, child.margin.bottom)); } if m != 0.0 { Some(m) } else { None }}
/// Bottom margin of the last non-empty in-flow block child (already pre-collapsed).fn last_block_bottom_margin(children: &[LayoutBox]) -> Option<f32> { for child in children.iter().rev() { if is_in_flow(child) && is_block_level(child) { if is_empty_block(child) { continue; } return Some(child.margin.bottom); } } let mut m = 0.0f32; for child in children .iter() .filter(|c| is_in_flow(c) && is_block_level(c)) { m = collapse_margins(m, collapse_margins(child.margin.top, child.margin.bottom)); } if m != 0.0 { Some(m) } else { None }}
/// Lay out block-level children with vertical margin collapsing (CSS2 §8.3.1)/// and float support.////// Handles adjacent-sibling collapsing, empty-block collapsing,/// parent-child internal spacing (the parent's external margins were already/// updated by `pre_collapse_margins`), and float placement.fn layout_block_children( parent: &mut LayoutBox, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document, abs_cb: Rect,) { let content_x = parent.rect.x; let content_width = parent.rect.width; let mut cursor_y = parent.rect.y;
let parent_top_open = parent.border.top == 0.0 && parent.padding.top == 0.0 && !establishes_bfc(parent); let parent_bottom_open = parent.border.bottom == 0.0 && parent.padding.bottom == 0.0 && !establishes_bfc(parent);
// Pending bottom margin from the previous sibling. let mut pending_margin: Option<f32> = None; let child_count = parent.children.len(); // Track whether we've seen any in-flow children (for parent_top_open). let mut first_in_flow = true; let mut float_ctx = FloatContext::default();
for i in 0..child_count { // Skip out-of-flow children (absolute/fixed) — they are laid out // separately in layout_abspos_children. if parent.children[i].position == Position::Absolute || parent.children[i].position == Position::Fixed { continue; }
// --- Handle floated children --- if is_floated(&parent.children[i]) { layout_float_child( &mut parent.children[i], &mut float_ctx, cursor_y, content_x, content_width, viewport_width, viewport_height, font, doc, abs_cb, ); continue; }
let child_top_margin = parent.children[i].margin.top; let child_bottom_margin = parent.children[i].margin.bottom;
// --- Handle clear property --- if parent.children[i].clear != Clear::None { let clear_y = float_ctx.clear_y(parent.children[i].clear); if clear_y > cursor_y { cursor_y = clear_y; // Clear resets pending margin. pending_margin = None; } }
// --- Empty block: top+bottom margins self-collapse --- if is_empty_block(&parent.children[i]) { let self_collapsed = collapse_margins(child_top_margin, child_bottom_margin); pending_margin = Some(match pending_margin { Some(prev) => collapse_margins(prev, self_collapsed), None => self_collapsed, }); // Position at cursor_y with zero height. let child = &mut parent.children[i]; child.rect.x = content_x + child.border.left + child.padding.left; child.rect.y = cursor_y + child.border.top + child.padding.top; child.rect.width = (content_width - child.border.left - child.border.right - child.padding.left - child.padding.right) .max(0.0); child.rect.height = 0.0; first_in_flow = false; continue; }
// --- Compute effective top spacing --- let collapsed_top = if let Some(prev_bottom) = pending_margin.take() { // Sibling collapsing: previous bottom vs this top. collapse_margins(prev_bottom, child_top_margin) } else if first_in_flow && parent_top_open { // First child, parent top open: margin was already pulled into // parent by pre_collapse_margins — no internal spacing. 0.0 } else { child_top_margin };
// `compute_layout` adds `child.margin.top` internally, so compensate. let y_for_child = cursor_y + collapsed_top - child_top_margin; compute_layout( &mut parent.children[i], content_x, y_for_child, content_width, viewport_width, viewport_height, font, doc, abs_cb, Some(&float_ctx), );
let child = &parent.children[i]; // Use the normal-flow position (before relative offset) so that // `position: relative` does not affect sibling placement. let (_, rel_dy) = child.relative_offset; cursor_y = (child.rect.y - rel_dy) + child.rect.height + child.padding.bottom + child.border.bottom; pending_margin = Some(child_bottom_margin); first_in_flow = false; }
// Trailing margin. if let Some(trailing) = pending_margin { if !parent_bottom_open { // Parent has border/padding at bottom — margin stays inside. cursor_y += trailing; } // If parent_bottom_open, the margin was already pulled into the // parent by pre_collapse_margins. }
parent.rect.height = cursor_y - parent.rect.y;
// BFC containment: if this box establishes a BFC, it must expand to // contain all of its floated children. if establishes_bfc(parent) { let float_bottom = float_ctx.max_float_bottom(); let needed = float_bottom - parent.rect.y; if needed > parent.rect.height { parent.rect.height = needed; } }}
/// Lay out a single floated child element.#[allow(clippy::too_many_arguments)]fn layout_float_child( child: &mut LayoutBox, float_ctx: &mut FloatContext, cursor_y: f32, container_x: f32, container_width: f32, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document, abs_cb: Rect,) { let float_side = child.float;
// Resolve margins against containing block width. child.margin = EdgeSizes { top: resolve_length_against(child.css_margin[0], container_width), right: resolve_length_against(child.css_margin[1], container_width), bottom: resolve_length_against(child.css_margin[2], container_width), left: resolve_length_against(child.css_margin[3], container_width), };
// Resolve padding against containing block width. child.padding = EdgeSizes { top: resolve_length_against(child.css_padding[0], container_width), right: resolve_length_against(child.css_padding[1], container_width), bottom: resolve_length_against(child.css_padding[2], container_width), left: resolve_length_against(child.css_padding[3], container_width), };
let horiz_extra = child.border.left + child.border.right + child.padding.left + child.padding.right;
// Resolve content width. let content_width = match child.css_width { LengthOrAuto::Length(w) => match child.box_sizing { BoxSizing::ContentBox => w.max(0.0), BoxSizing::BorderBox => (w - horiz_extra).max(0.0), }, LengthOrAuto::Percentage(p) => { let resolved = p / 100.0 * container_width; match child.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - horiz_extra).max(0.0), } } LengthOrAuto::Auto => { // Shrink-to-fit: measure content. let max_content = measure_float_content_width(child, font); let available = (container_width - child.margin.left - child.margin.right - horiz_extra).max(0.0); max_content.min(available) } };
child.rect.width = content_width;
// Temporary position for layout. child.rect.x = container_x + child.margin.left + child.border.left + child.padding.left; child.rect.y = cursor_y + child.margin.top + child.border.top + child.padding.top;
// Layout child content. if let Some((rw, rh)) = child.replaced_size { child.rect.width = rw.min(child.rect.width); child.rect.height = rh; } else { match &child.box_type { BoxType::Block(_) | BoxType::Anonymous => { if matches!(child.display, Display::Flex | Display::InlineFlex) { layout_flex_children(child, viewport_width, viewport_height, font, doc, abs_cb); } else if has_block_children(child) || has_float_children(child) { layout_block_children( child, viewport_width, viewport_height, font, doc, abs_cb, ); } else { layout_inline_children(child, font, doc, None); } } _ => {} } }
// Resolve explicit CSS height. child.content_height = child.rect.height; match child.css_height { LengthOrAuto::Length(h) => { let vert_extra = child.border.top + child.border.bottom + child.padding.top + child.padding.bottom; child.rect.height = match child.box_sizing { BoxSizing::ContentBox => h.max(0.0), BoxSizing::BorderBox => (h - vert_extra).max(0.0), }; } LengthOrAuto::Percentage(p) => { let resolved = p / 100.0 * viewport_height; let vert_extra = child.border.top + child.border.bottom + child.padding.top + child.padding.bottom; child.rect.height = match child.box_sizing { BoxSizing::ContentBox => resolved.max(0.0), BoxSizing::BorderBox => (resolved - vert_extra).max(0.0), }; } LengthOrAuto::Auto => {} }
// Compute the float's margin box dimensions. let margin_box_width = child.margin.left + child.border.left + child.padding.left + child.rect.width + child.padding.right + child.border.right + child.margin.right; let margin_box_height = child.margin.top + child.border.top + child.padding.top + child.rect.height + child.padding.bottom + child.border.bottom + child.margin.bottom;
// Place the float. let (fx, fy) = float_ctx.place_float( float_side, margin_box_width, margin_box_height, cursor_y, container_x, container_width, );
// Position the child's content box relative to the placed margin box. let final_x = fx + child.margin.left + child.border.left + child.padding.left; let final_y = fy + child.margin.top + child.border.top + child.padding.top;
// Shift the entire box tree from its temporary position to the final one. let dx = final_x - child.rect.x; let dy = final_y - child.rect.y; if dx != 0.0 || dy != 0.0 { shift_box(child, dx, dy); }
// Set sticky constraints and handle abspos children. set_sticky_constraints(child); layout_abspos_children(child, abs_cb, viewport_width, viewport_height, font, doc); apply_relative_offset(child, container_width, viewport_height);}
/// Measure the max-content width of a float's content (shrink-to-fit).fn measure_float_content_width(b: &LayoutBox, font: &Font) -> f32 { let mut max_width = 0.0f32; measure_box_content_width(b, font, &mut max_width); max_width}
// ---------------------------------------------------------------------------// Flex layout// ---------------------------------------------------------------------------
/// Measure the intrinsic max-content width of a flex item's content./// Returns the width needed to lay out content without wrapping.fn measure_flex_item_max_content_width(child: &LayoutBox, font: &Font, _doc: &Document) -> f32 { // Measure the widest text line from all inline content. let mut max_width = 0.0f32; measure_box_content_width(child, font, &mut max_width); max_width}
/// Recursively measure max content width of a layout box tree.fn measure_box_content_width(b: &LayoutBox, font: &Font, max_w: &mut f32) { match &b.box_type { BoxType::TextRun { text, .. } => { let w = measure_text_width(font, text, b.font_size); if w > *max_w { *max_w = w; } } _ => { let horiz = b.border.left + b.border.right + b.padding.left + b.padding.right; let mut child_max = 0.0f32; for child in &b.children { measure_box_content_width(child, font, &mut child_max); } let total = child_max + horiz; if total > *max_w { *max_w = total; } } }}
/// Lay out children according to the CSS Flexbox algorithm (Level 1 §9).fn layout_flex_children( parent: &mut LayoutBox, viewport_width: f32, viewport_height: f32, font: &Font, doc: &Document, abs_cb: Rect,) { let flex_direction = parent.flex_direction; let flex_wrap = parent.flex_wrap; let justify_content = parent.justify_content; let align_items = parent.align_items; let align_content = parent.align_content; let container_main_size = match flex_direction { FlexDirection::Row | FlexDirection::RowReverse => parent.rect.width, FlexDirection::Column | FlexDirection::ColumnReverse => { match parent.css_height { LengthOrAuto::Length(h) => h, LengthOrAuto::Percentage(p) => p / 100.0 * viewport_height, LengthOrAuto::Auto => viewport_height, // fallback } } }; let container_cross_size = match flex_direction { FlexDirection::Row | FlexDirection::RowReverse => match parent.css_height { LengthOrAuto::Length(h) => Some(h), LengthOrAuto::Percentage(p) => Some(p / 100.0 * viewport_height), LengthOrAuto::Auto => None, }, FlexDirection::Column | FlexDirection::ColumnReverse => Some(parent.rect.width), };
let is_row = matches!( flex_direction, FlexDirection::Row | FlexDirection::RowReverse ); let is_reverse = matches!( flex_direction, FlexDirection::RowReverse | FlexDirection::ColumnReverse );
// Per CSS Box Alignment §8, row-gap applies between rows and column-gap // between columns. In a row flex container the main axis is horizontal // (column-gap between items, row-gap between lines). In a column flex // container the axes are swapped. let (main_gap, cross_gap) = if is_row { (parent.column_gap, parent.row_gap) } else { (parent.row_gap, parent.column_gap) };
if parent.children.is_empty() { parent.rect.height = 0.0; return; }
// Sort children by `order` (stable sort preserves DOM order for equal values). let child_count = parent.children.len(); let mut order_indices: Vec<usize> = (0..child_count).collect(); order_indices.sort_by_key(|&i| parent.children[i].order);
// Step 1: Determine flex base sizes and hypothetical main sizes. struct FlexItemInfo { index: usize, base_size: f32, hypothetical_main: f32, flex_grow: f32, flex_shrink: f32, frozen: bool, target_main: f32, outer_main: f32, // margin+border+padding on main axis outer_cross: f32, // margin+border+padding on cross axis }
let mut items: Vec<FlexItemInfo> = Vec::with_capacity(child_count);
for &idx in &order_indices { // Skip out-of-flow children (absolute/fixed). if !is_in_flow(&parent.children[idx]) { continue; } let child = &mut parent.children[idx];
// Resolve margin/padding percentages for the child. let cb_width = if is_row { container_main_size } else { container_cross_size.unwrap_or(parent.rect.width) };
// Resolve percentage margins and padding against containing block width. for i in 0..4 { if matches!(child.css_margin[i], LengthOrAuto::Percentage(_)) { let resolved = resolve_length_against(child.css_margin[i], cb_width); match i { 0 => child.margin.top = resolved, 1 => child.margin.right = resolved, 2 => child.margin.bottom = resolved, 3 => child.margin.left = resolved, _ => {} } } if matches!(child.css_padding[i], LengthOrAuto::Percentage(_)) { let resolved = resolve_length_against(child.css_padding[i], cb_width); match i { 0 => child.padding.top = resolved, 1 => child.padding.right = resolved, 2 => child.padding.bottom = resolved, 3 => child.padding.left = resolved, _ => {} } } }
let margin_main = if is_row { child.margin.left + child.margin.right } else { child.margin.top + child.margin.bottom }; let border_padding_main = if is_row { child.border.left + child.border.right + child.padding.left + child.padding.right } else { child.border.top + child.border.bottom + child.padding.top + child.padding.bottom }; let margin_cross = if is_row { child.margin.top + child.margin.bottom } else { child.margin.left + child.margin.right }; let border_padding_cross = if is_row { child.border.top + child.border.bottom + child.padding.top + child.padding.bottom } else { child.border.left + child.border.right + child.padding.left + child.padding.right };
// Determine flex-basis. let flex_basis = child.flex_basis; let specified_main = if is_row { child.css_width } else { child.css_height };
let base_size = match flex_basis { LengthOrAuto::Length(px) => px, LengthOrAuto::Percentage(p) => p / 100.0 * container_main_size, LengthOrAuto::Auto => { // Use specified main size if set, otherwise content size. match specified_main { LengthOrAuto::Length(px) => px, LengthOrAuto::Percentage(p) => p / 100.0 * container_main_size, LengthOrAuto::Auto => { // Content-based sizing: use max-content size. if is_row { measure_flex_item_max_content_width(child, font, doc) } else { // For column direction, measure content height. let avail = container_cross_size.unwrap_or(parent.rect.width); compute_layout( child, 0.0, 0.0, avail, viewport_width, viewport_height, font, doc, abs_cb, None, ); child.rect.height } } } } };
let hypothetical_main = base_size.max(0.0); let outer = margin_main + border_padding_main;
items.push(FlexItemInfo { index: idx, base_size, hypothetical_main, flex_grow: child.flex_grow, flex_shrink: child.flex_shrink, frozen: false, target_main: hypothetical_main, outer_main: outer, outer_cross: margin_cross + border_padding_cross, }); }
// Step 2: Collect items into flex lines. let mut lines: Vec<Vec<usize>> = Vec::new(); // indices into `items`
if flex_wrap == FlexWrap::Nowrap { // All items on one line. lines.push((0..items.len()).collect()); } else { let mut current_line: Vec<usize> = Vec::new(); let mut line_main_size = 0.0f32;
for (i, item) in items.iter().enumerate() { let item_outer = item.hypothetical_main + item.outer_main; let gap = if current_line.is_empty() { 0.0 } else { main_gap };
if !current_line.is_empty() && line_main_size + gap + item_outer > container_main_size { lines.push(std::mem::take(&mut current_line)); line_main_size = 0.0; }
if !current_line.is_empty() { line_main_size += main_gap; } line_main_size += item_outer; current_line.push(i); }
if !current_line.is_empty() { lines.push(current_line); } }
if flex_wrap == FlexWrap::WrapReverse { lines.reverse(); }
// Step 3: Resolve flexible lengths for each line. for line in &lines { // Total hypothetical main sizes + gaps. let total_gaps = if line.len() > 1 { (line.len() - 1) as f32 * main_gap } else { 0.0 }; let total_outer_hypo: f32 = line .iter() .map(|&i| items[i].hypothetical_main + items[i].outer_main) .sum(); let used_space = total_outer_hypo + total_gaps; let free_space = container_main_size - used_space;
// Reset frozen state. for &i in line { items[i].frozen = false; items[i].target_main = items[i].hypothetical_main; }
if free_space > 0.0 { // Grow items. let total_grow: f32 = line.iter().map(|&i| items[i].flex_grow).sum(); if total_grow > 0.0 { for &i in line { if items[i].flex_grow > 0.0 { items[i].target_main += free_space * (items[i].flex_grow / total_grow); } } } } else if free_space < 0.0 { // Shrink items. let total_shrink_scaled: f32 = line .iter() .map(|&i| items[i].flex_shrink * items[i].base_size) .sum(); if total_shrink_scaled > 0.0 { for &i in line { let scaled = items[i].flex_shrink * items[i].base_size; let ratio = scaled / total_shrink_scaled; items[i].target_main = (items[i].hypothetical_main + free_space * ratio).max(0.0); } } } }
// Step 4: Determine cross sizes of items by laying them out at their target main size. struct LineCrossInfo { max_cross: f32, }
let mut line_infos: Vec<LineCrossInfo> = Vec::new();
for line in &lines { let mut max_cross = 0.0f32;
for &i in line { let idx = items[i].index; let child = &mut parent.children[idx]; let target_main = items[i].target_main;
// Set up the child for layout at the resolved main size. if is_row { child.css_width = LengthOrAuto::Length(target_main); compute_layout( child, 0.0, 0.0, target_main, viewport_width, viewport_height, font, doc, abs_cb, None, ); let cross = child.rect.height + items[i].outer_cross; if cross > max_cross { max_cross = cross; } } else { let avail = container_cross_size.unwrap_or(parent.rect.width); compute_layout( child, 0.0, 0.0, avail, viewport_width, viewport_height, font, doc, abs_cb, None, ); child.rect.height = target_main; let cross = child.rect.width + child.border.left + child.border.right + child.padding.left + child.padding.right + child.margin.left + child.margin.right; if cross > max_cross { max_cross = cross; } } }
line_infos.push(LineCrossInfo { max_cross }); }
// Per CSS Flexbox §9.4: If the flex container is single-line and has a // definite cross size, the cross size of the flex line is the container's // cross size (clamped to min/max). This ensures alignment works relative // to the full container. if lines.len() == 1 { if let Some(cs) = container_cross_size { if line_infos[0].max_cross < cs { line_infos[0].max_cross = cs; } } }
// Step 5: Handle align-items: stretch — stretch items to fill the line cross size. for (line_idx, line) in lines.iter().enumerate() { let line_cross = line_infos[line_idx].max_cross;
for &i in line { let idx = items[i].index; let child = &mut parent.children[idx];
let effective_align = match child.align_self { AlignSelf::Auto => align_items, AlignSelf::FlexStart => AlignItems::FlexStart, AlignSelf::FlexEnd => AlignItems::FlexEnd, AlignSelf::Center => AlignItems::Center, AlignSelf::Baseline => AlignItems::Baseline, AlignSelf::Stretch => AlignItems::Stretch, };
if effective_align == AlignItems::Stretch { let item_cross_space = line_cross - items[i].outer_cross; if is_row { if matches!(child.css_height, LengthOrAuto::Auto) { child.rect.height = item_cross_space.max(0.0); } } else if matches!(child.css_width, LengthOrAuto::Auto) { child.rect.width = item_cross_space.max(0.0); } } } }
// Step 6: Position items on main and cross axes. let total_cross_gaps = if lines.len() > 1 { (lines.len() - 1) as f32 * cross_gap } else { 0.0 }; let total_line_cross: f32 = line_infos.iter().map(|li| li.max_cross).sum(); let total_cross_used = total_line_cross + total_cross_gaps;
// For definite cross size containers, compute align-content offsets. let cross_free_space = container_cross_size .map(|cs| (cs - total_cross_used).max(0.0)) .unwrap_or(0.0);
let (ac_initial_offset, ac_between_offset) = compute_content_distribution( align_content_to_justify(align_content), cross_free_space, lines.len(), );
let mut cross_cursor = if is_row { parent.rect.y } else { parent.rect.x } + ac_initial_offset;
for (line_idx, line) in lines.iter().enumerate() { let line_cross = line_infos[line_idx].max_cross;
// Main-axis justification. let total_main_gaps = if line.len() > 1 { (line.len() - 1) as f32 * main_gap } else { 0.0 }; let items_main: f32 = line .iter() .map(|&i| items[i].target_main + items[i].outer_main) .sum(); let main_free = (container_main_size - items_main - total_main_gaps).max(0.0);
let (jc_initial, jc_between) = compute_content_distribution(justify_content, main_free, line.len());
// Determine the starting main position. let mut main_cursor = if is_row { parent.rect.x } else { parent.rect.y };
if is_reverse { // For reverse directions, start from the end. main_cursor += container_main_size; }
main_cursor += if is_reverse { -jc_initial } else { jc_initial };
let line_items: Vec<usize> = if is_reverse { line.iter().rev().copied().collect() } else { line.to_vec() };
for (item_pos, &i) in line_items.iter().enumerate() { let idx = items[i].index; let child = &mut parent.children[idx]; let target_main = items[i].target_main;
// Main-axis position. let child_main_margin_start = if is_row { child.margin.left } else { child.margin.top }; let child_main_margin_end = if is_row { child.margin.right } else { child.margin.bottom }; let child_main_bp_start = if is_row { child.border.left + child.padding.left } else { child.border.top + child.padding.top }; let child_main_bp_end = if is_row { child.border.right + child.padding.right } else { child.border.bottom + child.padding.bottom };
let outer_size = child_main_margin_start + child_main_bp_start + target_main + child_main_bp_end + child_main_margin_end;
if is_reverse { main_cursor -= outer_size; }
let content_main = main_cursor + child_main_margin_start + child_main_bp_start;
// Cross-axis position. let child_cross_margin_start = if is_row { child.margin.top } else { child.margin.left }; let child_cross_bp_start = if is_row { child.border.top + child.padding.top } else { child.border.left + child.padding.left }; let child_cross_total = if is_row { child.rect.height + items[i].outer_cross } else { child.rect.width + items[i].outer_cross };
let effective_align = match child.align_self { AlignSelf::Auto => align_items, AlignSelf::FlexStart => AlignItems::FlexStart, AlignSelf::FlexEnd => AlignItems::FlexEnd, AlignSelf::Center => AlignItems::Center, AlignSelf::Baseline => AlignItems::Baseline, AlignSelf::Stretch => AlignItems::Stretch, };
let cross_offset = match effective_align { AlignItems::FlexStart | AlignItems::Stretch | AlignItems::Baseline => 0.0, AlignItems::FlexEnd => line_cross - child_cross_total, AlignItems::Center => (line_cross - child_cross_total) / 2.0, };
let content_cross = cross_cursor + cross_offset + child_cross_margin_start + child_cross_bp_start;
// Set child position. if is_row { child.rect.x = content_main; child.rect.y = content_cross; child.rect.width = target_main; } else { child.rect.x = content_cross; child.rect.y = content_main; child.rect.height = target_main; }
// Shift any text lines to match new position. reposition_lines(child, font, doc);
if !is_reverse { main_cursor += outer_size; }
// Add gap between items (not after last). if item_pos < line_items.len() - 1 { if is_reverse { main_cursor -= main_gap; } else { main_cursor += main_gap; } }
// Also add justify-content between spacing. if item_pos < line_items.len() - 1 { if is_reverse { main_cursor -= jc_between; } else { main_cursor += jc_between; } } }
cross_cursor += line_cross + cross_gap + ac_between_offset; }
// Set parent height based on children. if is_row { if matches!(parent.css_height, LengthOrAuto::Auto) { parent.rect.height = total_cross_used; } } else if matches!(parent.css_height, LengthOrAuto::Auto) { // For column flex, height is the main axis. let total_main: f32 = lines .iter() .map(|line| { let items_main: f32 = line .iter() .map(|&i| items[i].target_main + items[i].outer_main) .sum(); let gaps = if line.len() > 1 { (line.len() - 1) as f32 * main_gap } else { 0.0 }; items_main + gaps }) .fold(0.0f32, f32::max); parent.rect.height = total_main; }}
/// Reposition inline text lines inside a layout box after moving it./// Re-runs inline layout if the box has inline children.fn reposition_lines(b: &mut LayoutBox, font: &Font, doc: &Document) { if !b.lines.is_empty() { // Re-run inline layout at the new position. b.lines.clear(); layout_inline_children(b, font, doc, None); } // Recursively reposition children that have their own inline content. for child in &mut b.children { if !child.lines.is_empty() { child.lines.clear(); layout_inline_children(child, font, doc, None); } }}
/// Convert AlignContent to JustifyContent for reuse of distribution logic.fn align_content_to_justify(ac: AlignContent) -> JustifyContent { match ac { AlignContent::FlexStart | AlignContent::Stretch => JustifyContent::FlexStart, AlignContent::FlexEnd => JustifyContent::FlexEnd, AlignContent::Center => JustifyContent::Center, AlignContent::SpaceBetween => JustifyContent::SpaceBetween, AlignContent::SpaceAround => JustifyContent::SpaceAround, }}
/// Compute the initial offset and between-item spacing for content distribution./// Returns (initial_offset, between_spacing).fn compute_content_distribution( mode: JustifyContent, free_space: f32, item_count: usize,) -> (f32, f32) { if item_count == 0 { return (0.0, 0.0); } match mode { JustifyContent::FlexStart => (0.0, 0.0), JustifyContent::FlexEnd => (free_space, 0.0), JustifyContent::Center => (free_space / 2.0, 0.0), JustifyContent::SpaceBetween => { if item_count <= 1 { (0.0, 0.0) } else { (0.0, free_space / (item_count - 1) as f32) } } JustifyContent::SpaceAround => { let per_item = free_space / item_count as f32; (per_item / 2.0, per_item) } JustifyContent::SpaceEvenly => { let spacing = free_space / (item_count + 1) as f32; (spacing, spacing) } }}
// ---------------------------------------------------------------------------// Inline formatting context// ---------------------------------------------------------------------------
/// An inline item produced by flattening the inline tree.enum InlineItemKind { /// A word of text with associated styling. Word { text: String, font_size: f32, color: Color, text_decoration: TextDecoration, background_color: Color, }, /// Whitespace between words. Space { font_size: f32 }, /// Forced line break (`<br>`). ForcedBreak, /// Start of an inline box (for margin/padding/border tracking). InlineStart { margin_left: f32, padding_left: f32, border_left: f32, }, /// End of an inline box. InlineEnd { margin_right: f32, padding_right: f32, border_right: f32, },}
/// A pending fragment on the current line.struct PendingFragment { text: String, x: f32, width: f32, font_size: f32, color: Color, text_decoration: TextDecoration, background_color: Color,}
/// Flatten the inline children tree into a sequence of items.fn flatten_inline_tree(children: &[LayoutBox], doc: &Document, items: &mut Vec<InlineItemKind>) { for child in children { // Skip out-of-flow children (absolute/fixed positioned). if !is_in_flow(child) { continue; } match &child.box_type { BoxType::TextRun { text, .. } => { let words = split_into_words(text); for segment in words { match segment { WordSegment::Word(w) => { items.push(InlineItemKind::Word { text: w, font_size: child.font_size, color: child.color, text_decoration: child.text_decoration, background_color: child.background_color, }); } WordSegment::Space => { items.push(InlineItemKind::Space { font_size: child.font_size, }); } } } } BoxType::Inline(node_id) => { if let NodeData::Element { tag_name, .. } = doc.node_data(*node_id) { if tag_name == "br" { items.push(InlineItemKind::ForcedBreak); continue; } }
items.push(InlineItemKind::InlineStart { margin_left: child.margin.left, padding_left: child.padding.left, border_left: child.border.left, });
flatten_inline_tree(&child.children, doc, items);
items.push(InlineItemKind::InlineEnd { margin_right: child.margin.right, padding_right: child.padding.right, border_right: child.border.right, }); } _ => {} } }}
enum WordSegment { Word(String), Space,}
/// Split text into alternating words and spaces.fn split_into_words(text: &str) -> Vec<WordSegment> { let mut segments = Vec::new(); let mut current_word = String::new();
for ch in text.chars() { if ch == ' ' { if !current_word.is_empty() { segments.push(WordSegment::Word(std::mem::take(&mut current_word))); } segments.push(WordSegment::Space); } else { current_word.push(ch); } }
if !current_word.is_empty() { segments.push(WordSegment::Word(current_word)); }
segments}
/// Lay out inline children using a proper inline formatting context.////// If `float_ctx` is provided, line boxes are shortened to avoid overlapping/// with active floats from the parent block formatting context.fn layout_inline_children( parent: &mut LayoutBox, font: &Font, doc: &Document, float_ctx: Option<&FloatContext>,) { let available_width = parent.rect.width; let text_align = parent.text_align; let line_height = parent.line_height;
let mut items = Vec::new(); flatten_inline_tree(&parent.children, doc, &mut items);
if items.is_empty() { parent.rect.height = 0.0; return; }
// Build line boxes, respecting float-narrowed available widths. let mut all_lines: Vec<(Vec<PendingFragment>, f32, f32)> = Vec::new(); // (fragments, line_left_offset, line_available_width) let mut current_line: Vec<PendingFragment> = Vec::new(); let mut cursor_x: f32 = 0.0; let mut line_y = parent.rect.y;
// Compute the available width for the current line, narrowed by floats. let line_avail = |y: f32| -> (f32, f32) { if let Some(fctx) = float_ctx { let (left, right) = fctx.available_range(y, line_height, parent.rect.x, available_width); let offset = (left - parent.rect.x).max(0.0); let width = (right - left).max(0.0); (offset, width) } else { (0.0, available_width) } };
let (mut current_line_offset, mut current_line_width) = line_avail(line_y);
for item in &items { match item { InlineItemKind::Word { text, font_size, color, text_decoration, background_color, } => { let word_width = measure_text_width(font, text, *font_size);
// If this word doesn't fit and the line isn't empty, break. if cursor_x > 0.0 && cursor_x + word_width > current_line_width { all_lines.push(( std::mem::take(&mut current_line), current_line_offset, current_line_width, )); line_y += line_height; let (off, w) = line_avail(line_y); current_line_offset = off; current_line_width = w; cursor_x = 0.0; }
current_line.push(PendingFragment { text: text.clone(), x: cursor_x, width: word_width, font_size: *font_size, color: *color, text_decoration: *text_decoration, background_color: *background_color, }); cursor_x += word_width; } InlineItemKind::Space { font_size } => { // Only add space if we have content on the line. if !current_line.is_empty() { let space_width = measure_text_width(font, " ", *font_size); if cursor_x + space_width <= current_line_width { cursor_x += space_width; } } } InlineItemKind::ForcedBreak => { all_lines.push(( std::mem::take(&mut current_line), current_line_offset, current_line_width, )); line_y += line_height; let (off, w) = line_avail(line_y); current_line_offset = off; current_line_width = w; cursor_x = 0.0; } InlineItemKind::InlineStart { margin_left, padding_left, border_left, } => { cursor_x += margin_left + padding_left + border_left; } InlineItemKind::InlineEnd { margin_right, padding_right, border_right, } => { cursor_x += margin_right + padding_right + border_right; } } }
// Flush the last line. if !current_line.is_empty() { all_lines.push((current_line, current_line_offset, current_line_width)); }
if all_lines.is_empty() { parent.rect.height = 0.0; return; }
// Position lines vertically and apply text-align. let mut text_lines = Vec::new(); let mut y = parent.rect.y; let num_lines = all_lines.len();
for (line_idx, (line_fragments, line_offset, line_avail_w)) in all_lines.iter().enumerate() { if line_fragments.is_empty() { y += line_height; continue; }
// Compute line width from last fragment. let line_width = match line_fragments.last() { Some(last) => last.x + last.width, None => 0.0, };
// Compute text-align offset. let is_last_line = line_idx == num_lines - 1; let align_offset = compute_align_offset(text_align, *line_avail_w, line_width, is_last_line);
for frag in line_fragments { text_lines.push(TextLine { text: frag.text.clone(), x: parent.rect.x + line_offset + frag.x + align_offset, y, width: frag.width, font_size: frag.font_size, color: frag.color, text_decoration: frag.text_decoration, background_color: frag.background_color, }); }
y += line_height; }
parent.rect.height = num_lines as f32 * line_height; parent.lines = text_lines;}
/// Compute the horizontal offset for text alignment.fn compute_align_offset( align: TextAlign, available_width: f32, line_width: f32, is_last_line: bool,) -> f32 { let extra_space = (available_width - line_width).max(0.0); match align { TextAlign::Left => 0.0, TextAlign::Center => extra_space / 2.0, TextAlign::Right => extra_space, TextAlign::Justify => { // Don't justify the last line (CSS spec behavior). if is_last_line { 0.0 } else { // For justify, we shift the whole line by 0 — the actual distribution // of space between words would need per-word spacing. For now, treat // as left-aligned; full justify support is a future enhancement. 0.0 } } }}
// ---------------------------------------------------------------------------// Text measurement// ---------------------------------------------------------------------------
/// Measure the total advance width of a text string at the given font size.fn measure_text_width(font: &Font, text: &str, font_size: f32) -> f32 { let shaped = font.shape_text(text, font_size); match shaped.last() { Some(last) => last.x_offset + last.x_advance, None => 0.0, }}
// ---------------------------------------------------------------------------// Public API// ---------------------------------------------------------------------------
/// Build and lay out from a styled tree (produced by `resolve_styles`).////// Returns a `LayoutTree` with positioned boxes ready for rendering.pub fn layout( styled_root: &StyledNode, doc: &Document, viewport_width: f32, viewport_height: f32, font: &Font, image_sizes: &HashMap<NodeId, (f32, f32)>,) -> LayoutTree { let mut root = match build_box(styled_root, doc, image_sizes) { Some(b) => b, None => { return LayoutTree { root: LayoutBox::new(BoxType::Anonymous, &ComputedStyle::default()), width: viewport_width, height: 0.0, }; } };
// Pre-collapse parent-child margins before positioning. pre_collapse_margins(&mut root);
// The initial containing block for absolutely positioned elements is the viewport. let viewport_cb = Rect { x: 0.0, y: 0.0, width: viewport_width, height: viewport_height, }; compute_layout( &mut root, 0.0, 0.0, viewport_width, viewport_width, viewport_height, font, doc, viewport_cb, None, );
let height = root.margin_box_height(); LayoutTree { root, width: viewport_width, height, }}
#[cfg(test)]mod tests { use super::*; use we_dom::Document; use we_style::computed::{extract_stylesheets, resolve_styles};
fn test_font() -> Font { let paths = [ "/System/Library/Fonts/Geneva.ttf", "/System/Library/Fonts/Monaco.ttf", ]; for path in &paths { let p = std::path::Path::new(path); if p.exists() { return Font::from_file(p).expect("failed to parse font"); } } panic!("no test font found"); }
fn layout_doc(doc: &Document) -> LayoutTree { let font = test_font(); let sheets = extract_stylesheets(doc); let styled = resolve_styles(doc, &sheets, (800.0, 600.0)).unwrap(); layout(&styled, doc, 800.0, 600.0, &font, &HashMap::new()) }
#[test] fn empty_document() { let doc = Document::new(); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)); if let Some(styled) = styled { let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new()); assert_eq!(tree.width, 800.0); } }
#[test] fn single_paragraph() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p = doc.create_element("p"); let text = doc.create_text("Hello world"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, text);
let tree = layout_doc(&doc);
assert!(matches!(tree.root.box_type, BoxType::Block(_)));
let body_box = &tree.root.children[0]; assert!(matches!(body_box.box_type, BoxType::Block(_)));
let p_box = &body_box.children[0]; assert!(matches!(p_box.box_type, BoxType::Block(_)));
assert!(!p_box.lines.is_empty(), "p should have text fragments");
// Collect all text on the first visual line. let first_y = p_box.lines[0].y; let line_text: String = p_box .lines .iter() .filter(|l| (l.y - first_y).abs() < 0.01) .map(|l| l.text.as_str()) .collect::<Vec<_>>() .join(" "); assert!( line_text.contains("Hello") && line_text.contains("world"), "line should contain Hello and world, got: {line_text}" );
assert_eq!(p_box.margin.top, 16.0); assert_eq!(p_box.margin.bottom, 16.0); }
#[test] fn blocks_stack_vertically() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p1 = doc.create_element("p"); let t1 = doc.create_text("First"); let p2 = doc.create_element("p"); let t2 = doc.create_text("Second"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p1); doc.append_child(p1, t1); doc.append_child(body, p2); doc.append_child(p2, t2);
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
assert!( second.rect.y > first.rect.y, "second p (y={}) should be below first p (y={})", second.rect.y, first.rect.y ); }
#[test] fn heading_larger_than_body() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let h1 = doc.create_element("h1"); let h1_text = doc.create_text("Title"); let p = doc.create_element("p"); let p_text = doc.create_text("Text"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, h1); doc.append_child(h1, h1_text); doc.append_child(body, p); doc.append_child(p, p_text);
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let h1_box = &body_box.children[0]; let p_box = &body_box.children[1];
assert!( h1_box.font_size > p_box.font_size, "h1 font_size ({}) should be > p font_size ({})", h1_box.font_size, p_box.font_size ); assert_eq!(h1_box.font_size, 32.0);
assert!( h1_box.rect.height > p_box.rect.height, "h1 height ({}) should be > p height ({})", h1_box.rect.height, p_box.rect.height ); }
#[test] fn body_has_default_margin() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p = doc.create_element("p"); let text = doc.create_text("Test"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, text);
let tree = layout_doc(&doc); let body_box = &tree.root.children[0];
// body default margin is 8px, but it collapses with p's 16px margin // (parent-child collapsing: no border/padding on body). assert_eq!(body_box.margin.top, 16.0); assert_eq!(body_box.margin.right, 8.0); assert_eq!(body_box.margin.bottom, 16.0); assert_eq!(body_box.margin.left, 8.0);
assert_eq!(body_box.rect.x, 8.0); // body.rect.y = collapsed margin (16) from viewport top. assert_eq!(body_box.rect.y, 16.0); }
#[test] fn text_wraps_at_container_width() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p = doc.create_element("p"); let text = doc.create_text("The quick brown fox jumps over the lazy dog and more words to wrap"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, text);
let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 100.0, 600.0, &font, &HashMap::new()); let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
// Count distinct y-positions to count visual lines. let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect(); ys.sort_by(|a, b| a.partial_cmp(b).unwrap()); ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
assert!( ys.len() > 1, "text should wrap to multiple lines, got {} visual lines", ys.len() ); }
#[test] fn layout_produces_positive_dimensions() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let div = doc.create_element("div"); let text = doc.create_text("Content"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, div); doc.append_child(div, text);
let tree = layout_doc(&doc);
for b in tree.iter() { assert!(b.rect.width >= 0.0, "width should be >= 0"); assert!(b.rect.height >= 0.0, "height should be >= 0"); }
assert!(tree.height > 0.0, "layout height should be > 0"); }
#[test] fn head_is_hidden() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let head = doc.create_element("head"); let title = doc.create_element("title"); let title_text = doc.create_text("Page Title"); let body = doc.create_element("body"); let p = doc.create_element("p"); let p_text = doc.create_text("Visible"); doc.append_child(root, html); doc.append_child(html, head); doc.append_child(head, title); doc.append_child(title, title_text); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, p_text);
let tree = layout_doc(&doc);
assert_eq!( tree.root.children.len(), 1, "html should have 1 child (body), head should be hidden" ); }
#[test] fn mixed_block_and_inline() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let div = doc.create_element("div"); let text1 = doc.create_text("Text"); let p = doc.create_element("p"); let p_text = doc.create_text("Block"); let text2 = doc.create_text("More"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, div); doc.append_child(div, text1); doc.append_child(div, p); doc.append_child(p, p_text); doc.append_child(div, text2);
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!( div_box.children.len(), 3, "div should have 3 children (anon, block, anon), got {}", div_box.children.len() );
assert!(matches!(div_box.children[0].box_type, BoxType::Anonymous)); assert!(matches!(div_box.children[1].box_type, BoxType::Block(_))); assert!(matches!(div_box.children[2].box_type, BoxType::Anonymous)); }
#[test] fn inline_elements_contribute_text() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p = doc.create_element("p"); let t1 = doc.create_text("Hello "); let em = doc.create_element("em"); let t2 = doc.create_text("world"); let t3 = doc.create_text("!"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, t1); doc.append_child(p, em); doc.append_child(em, t2); doc.append_child(p, t3);
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
assert!(!p_box.lines.is_empty());
let first_y = p_box.lines[0].y; let line_texts: Vec<&str> = p_box .lines .iter() .filter(|l| (l.y - first_y).abs() < 0.01) .map(|l| l.text.as_str()) .collect(); let combined = line_texts.join(""); assert!( combined.contains("Hello") && combined.contains("world") && combined.contains("!"), "line should contain all text, got: {combined}" ); }
#[test] fn collapse_whitespace_works() { assert_eq!(collapse_whitespace("hello world"), "hello world"); assert_eq!(collapse_whitespace(" spaces "), " spaces "); assert_eq!(collapse_whitespace("\n\ttabs\n"), " tabs "); assert_eq!(collapse_whitespace("no-extra"), "no-extra"); assert_eq!(collapse_whitespace(" "), " "); }
#[test] fn content_width_respects_body_margin() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let div = doc.create_element("div"); let text = doc.create_text("Content"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, div); doc.append_child(div, text);
let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new()); let body_box = &tree.root.children[0];
assert_eq!(body_box.rect.width, 784.0);
let div_box = &body_box.children[0]; assert_eq!(div_box.rect.width, 784.0); }
#[test] fn multiple_heading_levels() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); doc.append_child(root, html); doc.append_child(html, body);
let tags = ["h1", "h2", "h3"]; for tag in &tags { let h = doc.create_element(tag); let t = doc.create_text(tag); doc.append_child(body, h); doc.append_child(h, t); }
let tree = layout_doc(&doc); let body_box = &tree.root.children[0];
let h1 = &body_box.children[0]; let h2 = &body_box.children[1]; let h3 = &body_box.children[2]; assert!(h1.font_size > h2.font_size); assert!(h2.font_size > h3.font_size);
assert!(h2.rect.y > h1.rect.y); assert!(h3.rect.y > h2.rect.y); }
#[test] fn layout_tree_iteration() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let p = doc.create_element("p"); let text = doc.create_text("Test"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, p); doc.append_child(p, text);
let tree = layout_doc(&doc); let count = tree.iter().count(); assert!(count >= 3, "should have at least html, body, p boxes"); }
#[test] fn css_style_affects_layout() { let html_str = r#"<!DOCTYPE html><html><head><style>p { margin-top: 50px; margin-bottom: 50px; }</style></head><body><p>First</p><p>Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
assert_eq!(first.margin.top, 50.0); assert_eq!(first.margin.bottom, 50.0);
// Adjacent sibling margins collapse: gap = max(50, 50) = 50, not 100. let gap = second.rect.y - (first.rect.y + first.rect.height); assert!( (gap - 50.0).abs() < 1.0, "collapsed margin gap should be ~50px, got {gap}" ); }
#[test] fn inline_style_affects_layout() { let html_str = r#"<!DOCTYPE html><html><body><div style="padding-top: 20px; padding-bottom: 20px;"><p>Content</p></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!(div_box.padding.top, 20.0); assert_eq!(div_box.padding.bottom, 20.0); }
#[test] fn css_color_propagates_to_layout() { let html_str = r#"<!DOCTYPE html><html><head><style>p { color: red; background-color: blue; }</style></head><body><p>Colored</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
assert_eq!(p_box.color, Color::rgb(255, 0, 0)); assert_eq!(p_box.background_color, Color::rgb(0, 0, 255)); }
// --- New inline layout tests ---
#[test] fn inline_elements_have_per_fragment_styling() { let html_str = r#"<!DOCTYPE html><html><head><style>em { color: red; }</style></head><body><p>Hello <em>world</em></p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
let colors: Vec<Color> = p_box.lines.iter().map(|l| l.color).collect(); assert!( colors.iter().any(|c| *c == Color::rgb(0, 0, 0)), "should have black text" ); assert!( colors.iter().any(|c| *c == Color::rgb(255, 0, 0)), "should have red text from <em>" ); }
#[test] fn br_element_forces_line_break() { let html_str = r#"<!DOCTYPE html><html><body><p>Line one<br>Line two</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect(); ys.sort_by(|a, b| a.partial_cmp(b).unwrap()); ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
assert!( ys.len() >= 2, "<br> should produce 2 visual lines, got {}", ys.len() ); }
#[test] fn text_align_center() { let html_str = r#"<!DOCTYPE html><html><head><style>p { text-align: center; }</style></head><body><p>Hi</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
assert!(!p_box.lines.is_empty()); let first = &p_box.lines[0]; // Center-aligned: text should be noticeably offset from content x. assert!( first.x > p_box.rect.x + 10.0, "center-aligned text x ({}) should be offset from content x ({})", first.x, p_box.rect.x ); }
#[test] fn text_align_right() { let html_str = r#"<!DOCTYPE html><html><head><style>p { text-align: right; }</style></head><body><p>Hi</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
assert!(!p_box.lines.is_empty()); let first = &p_box.lines[0]; let right_edge = p_box.rect.x + p_box.rect.width; assert!( (first.x + first.width - right_edge).abs() < 1.0, "right-aligned text end ({}) should be near right edge ({})", first.x + first.width, right_edge ); }
#[test] fn inline_padding_offsets_text() { let html_str = r#"<!DOCTYPE html><html><head><style>span { padding-left: 20px; padding-right: 20px; }</style></head><body><p>A<span>B</span>C</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
// Should have at least 3 fragments: A, B, C assert!( p_box.lines.len() >= 3, "should have fragments for A, B, C, got {}", p_box.lines.len() );
// B should be offset by the span's padding. let a_frag = &p_box.lines[0]; let b_frag = &p_box.lines[1]; let gap = b_frag.x - (a_frag.x + a_frag.width); // Gap should include the 20px padding-left from the span. assert!( gap >= 19.0, "gap between A and B ({gap}) should include span padding-left (20px)" ); }
#[test] fn text_fragments_have_correct_font_size() { let html_str = r#"<!DOCTYPE html><html><body><h1>Big</h1><p>Small</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let h1_box = &body_box.children[0]; let p_box = &body_box.children[1];
assert!(!h1_box.lines.is_empty()); assert!(!p_box.lines.is_empty()); assert_eq!(h1_box.lines[0].font_size, 32.0); assert_eq!(p_box.lines[0].font_size, 16.0); }
#[test] fn line_height_from_computed_style() { let html_str = r#"<!DOCTYPE html><html><head><style>p { line-height: 30px; }</style></head><body><p>Line one Line two Line three</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); // Narrow viewport to force wrapping. let tree = layout(&styled, &doc, 100.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
let mut ys: Vec<f32> = p_box.lines.iter().map(|l| l.y).collect(); ys.sort_by(|a, b| a.partial_cmp(b).unwrap()); ys.dedup_by(|a, b| (*a - *b).abs() < 0.01);
if ys.len() >= 2 { let gap = ys[1] - ys[0]; assert!( (gap - 30.0).abs() < 1.0, "line spacing ({gap}) should be ~30px from line-height" ); } }
// --- Relative positioning tests ---
#[test] fn relative_position_top_left() { let html_str = r#"<!DOCTYPE html><html><body><div style="position: relative; top: 10px; left: 20px;">Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!(div_box.position, Position::Relative); assert_eq!(div_box.relative_offset, (20.0, 10.0));
// The div should be shifted from where it would be in normal flow. // Normal flow position: body.rect.x + margin, body.rect.y + margin. // With relative offset: shifted by (20, 10). // Body has 8px margin by default, so content starts at x=8, y=8. assert!( (div_box.rect.x - (8.0 + 20.0)).abs() < 0.01, "div x ({}) should be 28.0 (8 + 20)", div_box.rect.x ); assert!( (div_box.rect.y - (8.0 + 10.0)).abs() < 0.01, "div y ({}) should be 18.0 (8 + 10)", div_box.rect.y ); }
#[test] fn relative_position_does_not_affect_siblings() { let html_str = r#"<!DOCTYPE html><html><head><style>p { margin: 0; }</style></head><body><p id="first" style="position: relative; top: 50px;">First</p><p id="second">Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
// The first paragraph is shifted down by 50px visually. assert_eq!(first.relative_offset, (0.0, 50.0));
// But the second paragraph should be at its normal-flow position, // as if the first paragraph were NOT shifted. The second paragraph // should come right after the first's normal-flow height. // Body content starts at y=8 (default body margin). First p has 0 margin. // Second p should start right after first p's height (without offset). let first_normal_y = 8.0; // body margin let first_height = first.rect.height; let expected_second_y = first_normal_y + first_height; assert!( (second.rect.y - expected_second_y).abs() < 1.0, "second y ({}) should be at normal-flow position ({expected_second_y}), not affected by first's relative offset", second.rect.y ); }
#[test] fn relative_position_conflicting_offsets() { // When both top and bottom are specified, top wins. // When both left and right are specified, left wins. let html_str = r#"<!DOCTYPE html><html><body><div style="position: relative; top: 10px; bottom: 20px; left: 30px; right: 40px;">Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// top wins over bottom: dy = 10 (not -20) // left wins over right: dx = 30 (not -40) assert_eq!(div_box.relative_offset, (30.0, 10.0)); }
#[test] fn relative_position_auto_offsets() { // auto offsets should resolve to 0 (no movement). let html_str = r#"<!DOCTYPE html><html><body><div style="position: relative;">Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!(div_box.position, Position::Relative); assert_eq!(div_box.relative_offset, (0.0, 0.0)); }
#[test] fn relative_position_bottom_right() { // bottom: 15px should shift up by 15px (negative direction). // right: 25px should shift left by 25px (negative direction). let html_str = r#"<!DOCTYPE html><html><body><div style="position: relative; bottom: 15px; right: 25px;">Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!(div_box.relative_offset, (-25.0, -15.0)); }
#[test] fn relative_position_shifts_text_lines() { let html_str = r#"<!DOCTYPE html><html><head><style>p { margin: 0; }</style></head><body><p style="position: relative; top: 30px; left: 40px;">Hello</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0];
assert!(!p_box.lines.is_empty(), "p should have text lines"); let first_line = &p_box.lines[0];
// Text should be shifted by the relative offset. // Body content starts at x=8, y=8. With offset: x=48, y=38. assert!( first_line.x >= 8.0 + 40.0 - 1.0, "text x ({}) should be shifted by left offset", first_line.x ); assert!( first_line.y >= 8.0 + 30.0 - 1.0, "text y ({}) should be shifted by top offset", first_line.y ); }
#[test] fn static_position_has_no_offset() { let html_str = r#"<!DOCTYPE html><html><body><div>Normal flow</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
assert_eq!(div_box.position, Position::Static); assert_eq!(div_box.relative_offset, (0.0, 0.0)); }
// --- Margin collapsing tests ---
#[test] fn adjacent_sibling_margins_collapse() { // Two <p> elements each with margin 16px: gap should be 16px (max), not 32px (sum). let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }p { margin-top: 16px; margin-bottom: 16px; }</style></head><body><p>First</p><p>Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
// Gap between first's bottom border-box and second's top border-box // should be the collapsed margin: max(16, 16) = 16. let first_bottom = first.rect.y + first.rect.height + first.padding.bottom + first.border.bottom; let gap = second.rect.y - second.border.top - second.padding.top - first_bottom; assert!( (gap - 16.0).abs() < 1.0, "collapsed sibling margin should be ~16px, got {gap}" ); }
#[test] fn sibling_margins_collapse_unequal() { // p1 bottom-margin 20, p2 top-margin 30: gap should be 30 (max). let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.first { margin-top: 0; margin-bottom: 20px; }.second { margin-top: 30px; margin-bottom: 0; }</style></head><body><p class="first">First</p><p class="second">Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
let first_bottom = first.rect.y + first.rect.height + first.padding.bottom + first.border.bottom; let gap = second.rect.y - second.border.top - second.padding.top - first_bottom; assert!( (gap - 30.0).abs() < 1.0, "collapsed margin should be max(20, 30) = 30, got {gap}" ); }
#[test] fn parent_first_child_margin_collapsing() { // Parent with no padding/border: first child's top margin collapses. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.parent { margin-top: 10px; }.child { margin-top: 20px; }</style></head><body><div class="parent"><p class="child">Child</p></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let parent_box = &body_box.children[0];
// Parent margin collapses with child's: max(10, 20) = 20. assert_eq!(parent_box.margin.top, 20.0); }
#[test] fn negative_margin_collapsing() { // One positive (20) and one negative (-10): collapsed = 20 + (-10) = 10. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.first { margin-top: 0; margin-bottom: 20px; }.second { margin-top: -10px; margin-bottom: 0; }</style></head><body><p class="first">First</p><p class="second">Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
let first_bottom = first.rect.y + first.rect.height + first.padding.bottom + first.border.bottom; let gap = second.rect.y - second.border.top - second.padding.top - first_bottom; // 20 + (-10) = 10 assert!( (gap - 10.0).abs() < 1.0, "positive + negative margin collapse should be 10, got {gap}" ); }
#[test] fn both_negative_margins_collapse() { // Both negative: use the more negative value. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.first { margin-top: 0; margin-bottom: -10px; }.second { margin-top: -20px; margin-bottom: 0; }</style></head><body><p class="first">First</p><p class="second">Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
let first_bottom = first.rect.y + first.rect.height + first.padding.bottom + first.border.bottom; let gap = second.rect.y - second.border.top - second.padding.top - first_bottom; // Both negative: min(-10, -20) = -20 assert!( (gap - (-20.0)).abs() < 1.0, "both-negative margin collapse should be -20, got {gap}" ); }
#[test] fn border_blocks_margin_collapsing() { // When border separates margins, they don't collapse. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.first { margin-top: 0; margin-bottom: 20px; border-bottom: 1px solid black; }.second { margin-top: 20px; border-top: 1px solid black; }</style></head><body><p class="first">First</p><p class="second">Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let first = &body_box.children[0]; let second = &body_box.children[1];
// Borders are on the elements themselves, but the MARGINS are still // between the border boxes — sibling margins still collapse regardless // of borders on the elements. The margin gap = max(20, 20) = 20. let first_bottom = first.rect.y + first.rect.height + first.padding.bottom + first.border.bottom; let gap = second.rect.y - second.border.top - second.padding.top - first_bottom; assert!( (gap - 20.0).abs() < 1.0, "sibling margins collapse even with borders on elements, gap should be 20, got {gap}" ); }
#[test] fn padding_blocks_parent_child_collapsing() { // Parent with padding-top prevents margin collapsing with first child. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.parent { margin-top: 10px; padding-top: 5px; }.child { margin-top: 20px; }</style></head><body><div class="parent"><p class="child">Child</p></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let parent_box = &body_box.children[0];
// Parent has padding-top, so no collapsing: margin stays at 10. assert_eq!(parent_box.margin.top, 10.0); }
#[test] fn empty_block_margins_collapse() { // An empty div's top and bottom margins collapse with adjacent margins. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; border-top: 1px solid black; }.spacer { margin-top: 10px; margin-bottom: 10px; }p { margin-top: 5px; margin-bottom: 5px; }</style></head><body><p>Before</p><div class="spacer"></div><p>After</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let before = &body_box.children[0]; let after = &body_box.children[2]; // [0]=p, [1]=empty div, [2]=p
// Empty div's margins (10+10) self-collapse to max(10,10)=10. // Then collapse with before's bottom (5) and after's top (5): // collapse(5, collapse(10, 10)) = collapse(5, 10) = 10 // Then collapse(10, 5) = 10. // So total gap between before and after = 10. let before_bottom = before.rect.y + before.rect.height + before.padding.bottom + before.border.bottom; let gap = after.rect.y - after.border.top - after.padding.top - before_bottom; assert!( (gap - 10.0).abs() < 1.0, "empty block margin collapse gap should be ~10px, got {gap}" ); }
#[test] fn collapse_margins_unit() { // Unit tests for the collapse_margins helper. assert_eq!(collapse_margins(10.0, 20.0), 20.0); assert_eq!(collapse_margins(20.0, 10.0), 20.0); assert_eq!(collapse_margins(0.0, 15.0), 15.0); assert_eq!(collapse_margins(-5.0, -10.0), -10.0); assert_eq!(collapse_margins(20.0, -5.0), 15.0); assert_eq!(collapse_margins(-5.0, 20.0), 15.0); assert_eq!(collapse_margins(0.0, 0.0), 0.0); }
// --- Box-sizing tests ---
#[test] fn content_box_default_width_applies_to_content() { // Default box-sizing (content-box): width = content width only. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { width: 200px; padding: 10px; border: 5px solid black; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// content-box: rect.width = 200 (the specified width IS the content) assert_eq!(div_box.rect.width, 200.0); assert_eq!(div_box.padding.left, 10.0); assert_eq!(div_box.padding.right, 10.0); assert_eq!(div_box.border.left, 5.0); assert_eq!(div_box.border.right, 5.0); }
#[test] fn border_box_width_includes_padding_and_border() { // box-sizing: border-box: width includes padding and border. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { box-sizing: border-box; width: 200px; padding: 10px; border: 5px solid black; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// border-box: content width = 200 - 10*2 (padding) - 5*2 (border) = 170 assert_eq!(div_box.rect.width, 170.0); assert_eq!(div_box.padding.left, 10.0); assert_eq!(div_box.padding.right, 10.0); assert_eq!(div_box.border.left, 5.0); assert_eq!(div_box.border.right, 5.0); }
#[test] fn border_box_padding_exceeds_width_clamps_to_zero() { // border-box with padding+border > specified width: content clamps to 0. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { box-sizing: border-box; width: 20px; padding: 15px; border: 5px solid black; }</style></head><body><div>X</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// border-box: content = 20 - 15*2 - 5*2 = 20 - 40 = -20 → clamped to 0 assert_eq!(div_box.rect.width, 0.0); }
#[test] fn box_sizing_is_not_inherited() { // box-sizing is not inherited: child should use default content-box. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.parent { box-sizing: border-box; width: 300px; padding: 10px; border: 5px solid black; }.child { width: 100px; padding: 10px; border: 5px solid black; }</style></head><body><div class="parent"><div class="child">Inner</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let parent_box = &body_box.children[0]; let child_box = &parent_box.children[0];
// Parent: border-box → content = 300 - 20 - 10 = 270 assert_eq!(parent_box.rect.width, 270.0); // Child: default content-box → content = 100 (not reduced by padding/border) assert_eq!(child_box.rect.width, 100.0); }
#[test] fn border_box_height() { // box-sizing: border-box also applies to height. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { box-sizing: border-box; height: 100px; padding: 10px; border: 5px solid black; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// border-box: content height = 100 - 10*2 (padding) - 5*2 (border) = 70 assert_eq!(div_box.rect.height, 70.0); }
#[test] fn content_box_explicit_height() { // content-box: height applies to content only. let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { height: 100px; padding: 10px; border: 5px solid black; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0];
// content-box: rect.height = 100 (specified height IS content height) assert_eq!(div_box.rect.height, 100.0); }
// --- Visibility / display:none tests ---
#[test] fn display_none_excludes_from_layout_tree() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.hidden { display: none; }p { margin: 0; }</style></head><body><p>First</p><div class="hidden">Hidden content</div><p>Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; // display:none element is excluded — body should have only 2 children. assert_eq!(body_box.children.len(), 2);
let first = &body_box.children[0]; let second = &body_box.children[1]; // Second paragraph should be directly below first (no gap for hidden). assert!( second.rect.y == first.rect.y + first.rect.height, "display:none should not occupy space" ); }
#[test] fn visibility_hidden_preserves_layout_space() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.hidden { visibility: hidden; height: 50px; margin: 0; }p { margin: 0; }</style></head><body><p>First</p><div class="hidden">Hidden</div><p>Second</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; // visibility:hidden still in layout tree — body has 3 children. assert_eq!(body_box.children.len(), 3);
let hidden_box = &body_box.children[1]; assert_eq!(hidden_box.visibility, Visibility::Hidden); assert_eq!(hidden_box.rect.height, 50.0);
let second = &body_box.children[2]; // Second paragraph should be below hidden div (it occupies 50px). assert!( second.rect.y >= hidden_box.rect.y + 50.0, "visibility:hidden should preserve layout space" ); }
#[test] fn visibility_inherited_by_children() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.parent { visibility: hidden; }</style></head><body><div class="parent"><p>Child</p></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let parent_box = &body_box.children[0]; let child_box = &parent_box.children[0]; assert_eq!(parent_box.visibility, Visibility::Hidden); assert_eq!(child_box.visibility, Visibility::Hidden); }
#[test] fn visibility_visible_overrides_hidden_parent() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.parent { visibility: hidden; }.child { visibility: visible; }</style></head><body><div class="parent"><p class="child">Visible child</p></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let parent_box = &body_box.children[0]; let child_box = &parent_box.children[0]; assert_eq!(parent_box.visibility, Visibility::Hidden); assert_eq!(child_box.visibility, Visibility::Visible); }
#[test] fn visibility_collapse_on_non_table_treated_as_hidden() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { visibility: collapse; height: 50px; }</style></head><body><div>Collapsed</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; assert_eq!(div_box.visibility, Visibility::Collapse); // Still occupies space (non-table collapse = hidden behavior). assert_eq!(div_box.rect.height, 50.0); }
// --- Viewport units and percentage resolution tests ---
#[test] fn width_50_percent_resolves_to_half_containing_block() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { width: 50%; }</style></head><body><div>Half width</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; assert!( (div_box.rect.width - 400.0).abs() < 0.01, "width: 50% should be 400px on 800px viewport, got {}", div_box.rect.width ); }
#[test] fn margin_10_percent_resolves_against_containing_block_width() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { margin: 10%; width: 100px; height: 50px; }</style></head><body><div>Box</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; // All margins (including top/bottom) resolve against containing block WIDTH. assert!( (div_box.margin.left - 80.0).abs() < 0.01, "margin-left: 10% should be 80px on 800px viewport, got {}", div_box.margin.left ); assert!( (div_box.margin.top - 80.0).abs() < 0.01, "margin-top: 10% should be 80px (against width, not height), got {}", div_box.margin.top ); }
#[test] fn width_50vw_resolves_to_half_viewport() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { width: 50vw; }</style></head><body><div>Half viewport</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; assert!( (div_box.rect.width - 400.0).abs() < 0.01, "width: 50vw should be 400px on 800px viewport, got {}", div_box.rect.width ); }
#[test] fn height_100vh_resolves_to_full_viewport() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { height: 100vh; }</style></head><body><div>Full height</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; assert!( (div_box.rect.height - 600.0).abs() < 0.01, "height: 100vh should be 600px on 600px viewport, got {}", div_box.rect.height ); }
#[test] fn font_size_5vmin_resolves_to_smaller_dimension() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }p { font-size: 5vmin; }</style></head><body><p>Text</p></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); // viewport 800x600 → vmin = 600 let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let p_box = &body_box.children[0]; // 5vmin = 5% of min(800, 600) = 5% of 600 = 30px assert!( (p_box.font_size - 30.0).abs() < 0.01, "font-size: 5vmin should be 30px, got {}", p_box.font_size ); }
#[test] fn nested_percentage_widths_compound() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.outer { width: 50%; }.inner { width: 50%; }</style></head><body><div class="outer"><div class="inner">Nested</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let outer_box = &body_box.children[0]; let inner_box = &outer_box.children[0]; // outer = 50% of 800 = 400 assert!( (outer_box.rect.width - 400.0).abs() < 0.01, "outer width should be 400px, got {}", outer_box.rect.width ); // inner = 50% of 400 = 200 assert!( (inner_box.rect.width - 200.0).abs() < 0.01, "inner width should be 200px (50% of 400), got {}", inner_box.rect.width ); }
#[test] fn padding_percent_resolves_against_width() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { padding: 5%; width: 400px; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; // padding: 5% resolves against containing block width (800px) assert!( (div_box.padding.top - 40.0).abs() < 0.01, "padding-top: 5% should be 40px (5% of 800), got {}", div_box.padding.top ); assert!( (div_box.padding.left - 40.0).abs() < 0.01, "padding-left: 5% should be 40px (5% of 800), got {}", div_box.padding.left ); }
#[test] fn vmax_uses_larger_dimension() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { width: 10vmax; }</style></head><body><div>Content</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); // viewport 800x600 → vmax = 800 let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; // 10vmax = 10% of max(800, 600) = 10% of 800 = 80px assert!( (div_box.rect.width - 80.0).abs() < 0.01, "width: 10vmax should be 80px, got {}", div_box.rect.width ); }
#[test] fn height_50_percent_resolves_against_viewport() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }div { height: 50%; }</style></head><body><div>Half height</div></body></html>"#; let doc = we_html::parse_html(html_str); let font = test_font(); let sheets = extract_stylesheets(&doc); let styled = resolve_styles(&doc, &sheets, (800.0, 600.0)).unwrap(); let tree = layout(&styled, &doc, 800.0, 600.0, &font, &HashMap::new());
let body_box = &tree.root.children[0]; let div_box = &body_box.children[0]; // height: 50% resolves against viewport height (600) assert!( (div_box.rect.height - 300.0).abs() < 0.01, "height: 50% should be 300px on 600px viewport, got {}", div_box.rect.height ); }
// ----------------------------------------------------------------------- // Flexbox tests // -----------------------------------------------------------------------
#[test] fn flex_row_default_items_laid_out_horizontally() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0]; assert_eq!(container.children.len(), 3);
// Items should be at x=0, 100, 200 assert!( (container.children[0].rect.x - 0.0).abs() < 1.0, "first item x should be 0, got {}", container.children[0].rect.x ); assert!( (container.children[1].rect.x - 100.0).abs() < 1.0, "second item x should be 100, got {}", container.children[1].rect.x ); assert!( (container.children[2].rect.x - 200.0).abs() < 1.0, "third item x should be 200, got {}", container.children[2].rect.x );
// All items should have the same y. let y0 = container.children[0].rect.y; assert!((container.children[1].rect.y - y0).abs() < 1.0); assert!((container.children[2].rect.y - y0).abs() < 1.0); }
#[test] fn flex_direction_column() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; flex-direction: column; width: 600px; }.item { height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0]; assert_eq!(container.children.len(), 3);
// Items should be stacked vertically at y offsets: 0, 50, 100 (relative to container). let cy = container.rect.y; assert!((container.children[0].rect.y - cy).abs() < 1.0); assert!( (container.children[1].rect.y - cy - 50.0).abs() < 1.0, "second item y should be {}, got {}", cy + 50.0, container.children[1].rect.y ); assert!( (container.children[2].rect.y - cy - 100.0).abs() < 1.0, "third item y should be {}, got {}", cy + 100.0, container.children[2].rect.y ); }
#[test] fn flex_grow_distributes_space() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; }.a { flex-grow: 1; flex-basis: 0; height: 50px; }.b { flex-grow: 2; flex-basis: 0; height: 50px; }.c { flex-grow: 1; flex-basis: 0; height: 50px; }</style></head><body><div class="container"> <div class="a">A</div> <div class="b">B</div> <div class="c">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// flex-grow 1:2:1 should split 600px as 150:300:150 let a = &container.children[0]; let b = &container.children[1]; let c = &container.children[2];
assert!( (a.rect.width - 150.0).abs() < 1.0, "item A width should be 150, got {}", a.rect.width ); assert!( (b.rect.width - 300.0).abs() < 1.0, "item B width should be 300, got {}", b.rect.width ); assert!( (c.rect.width - 150.0).abs() < 1.0, "item C width should be 150, got {}", c.rect.width ); }
#[test] fn flex_justify_content_center() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; justify-content: center; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// 600px container, 200px of items, 400px free space, offset = 200. let a = &container.children[0]; let b = &container.children[1];
assert!( (a.rect.x - 200.0).abs() < 1.0, "first item x should be 200, got {}", a.rect.x ); assert!( (b.rect.x - 300.0).abs() < 1.0, "second item x should be 300, got {}", b.rect.x ); }
#[test] fn flex_justify_content_space_between() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; justify-content: space-between; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// 3 items of 100px each = 300px, 300px free, between = 150 // Positions: 0, 250, 500 let a = &container.children[0]; let b = &container.children[1]; let c = &container.children[2];
assert!( (a.rect.x - 0.0).abs() < 1.0, "first item x should be 0, got {}", a.rect.x ); assert!( (b.rect.x - 250.0).abs() < 1.0, "second item x should be 250, got {}", b.rect.x ); assert!( (c.rect.x - 500.0).abs() < 1.0, "third item x should be 500, got {}", c.rect.x ); }
#[test] fn flex_align_items_center() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; height: 200px; align-items: center; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0]; let a = &container.children[0];
// Container height=200, item height=50, centered at y = container.y + 75 let expected_y = container.rect.y + 75.0; assert!( (a.rect.y - expected_y).abs() < 1.0, "item y should be {}, got {}", expected_y, a.rect.y ); }
#[test] fn flex_wrap_wraps_to_new_line() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; flex-wrap: wrap; width: 250px; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// 250px container, 100px items: A and B fit on line 1, C wraps to line 2 let a = &container.children[0]; let b = &container.children[1]; let c = &container.children[2];
// A and B should be on the same row assert!((a.rect.y - b.rect.y).abs() < 1.0); // C should be on a different row (50px below) assert!( (c.rect.y - a.rect.y - 50.0).abs() < 1.0, "C should be 50px below A, got y diff {}", c.rect.y - a.rect.y ); }
#[test] fn flex_gap_adds_spacing() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; gap: 20px; }.item { width: 100px; height: 50px; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
let a = &container.children[0]; let b = &container.children[1]; let c = &container.children[2];
// With gap: 20px, positions should be: 0, 120, 240 assert!((a.rect.x - 0.0).abs() < 1.0); assert!( (b.rect.x - 120.0).abs() < 1.0, "B x should be 120, got {}", b.rect.x ); assert!( (c.rect.x - 240.0).abs() < 1.0, "C x should be 240, got {}", c.rect.x ); }
#[test] fn flex_order_changes_visual_order() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 600px; }.a { width: 100px; height: 50px; order: 2; }.b { width: 100px; height: 50px; order: 1; }.c { width: 100px; height: 50px; order: 3; }</style></head><body><div class="container"> <div class="a">A</div> <div class="b">B</div> <div class="c">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// DOM order: A(order:2), B(order:1), C(order:3) // Visual order: B(1), A(2), C(3) // B is at index 1 in DOM, A at 0, C at 2. // B (index 1) should be first visually (x ≈ 0) // A (index 0) should be second visually (x ≈ 100) // C (index 2) should be third visually (x ≈ 200) let a = &container.children[0]; // DOM index 0, order 2 let b = &container.children[1]; // DOM index 1, order 1 let c = &container.children[2]; // DOM index 2, order 3
assert!( b.rect.x < a.rect.x, "B (order:1) should be before A (order:2), B.x={} A.x={}", b.rect.x, a.rect.x ); assert!( a.rect.x < c.rect.x, "A (order:2) should be before C (order:3), A.x={} C.x={}", a.rect.x, c.rect.x ); }
#[test] fn flex_shrink_items() { let html_str = r#"<!DOCTYPE html><html><head><style>body { margin: 0; }.container { display: flex; width: 200px; }.item { width: 100px; height: 50px; flex-shrink: 1; }</style></head><body><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div></div></body></html>"#; let doc = we_html::parse_html(html_str); let tree = layout_doc(&doc);
let body = &tree.root.children[0]; let container = &body.children[0];
// 3 items * 100px = 300px in 200px container, shrink evenly // Each should be ~66.67px for (i, child) in container.children.iter().enumerate() { assert!( (child.rect.width - 200.0 / 3.0).abs() < 1.0, "item {} width should be ~66.67, got {}", i, child.rect.width ); } }
// ----------------------------------------------------------------------- // Absolute positioning tests // -----------------------------------------------------------------------
/// Helper to create: <html><body>...content...</body></html> and return /// (doc, html_id, body_id). fn make_html_body(doc: &mut Document) -> (NodeId, NodeId, NodeId) { let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); doc.append_child(root, html); doc.append_child(html, body); (root, html, body) }
#[test] fn absolute_positioned_with_top_left() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // <div style="position: relative; width: 400px; height: 300px;"> // <div style="position: absolute; top: 10px; left: 20px; width: 100px; height: 50px;"></div> // </div> let container = doc.create_element("div"); let abs_child = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, abs_child); doc.set_attribute( container, "style", "position: relative; width: 400px; height: 300px;", ); doc.set_attribute( abs_child, "style", "position: absolute; top: 10px; left: 20px; width: 100px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let abs_box = &container_box.children[0];
assert_eq!(abs_box.position, Position::Absolute); assert_eq!(abs_box.rect.width, 100.0); assert_eq!(abs_box.rect.height, 50.0);
// The containing block is the container's padding box. // Container content starts at container_box.rect.x, container_box.rect.y. // Padding box x = container_box.rect.x - container_box.padding.left. let cb_x = container_box.rect.x - container_box.padding.left; let cb_y = container_box.rect.y - container_box.padding.top;
// abs_box content area x = cb_x + 20 (left) + 0 (margin) + 0 (border) + 0 (padding) assert!( (abs_box.rect.x - (cb_x + 20.0)).abs() < 0.01, "abs x should be cb_x + 20, got {} (cb_x={})", abs_box.rect.x, cb_x, ); assert!( (abs_box.rect.y - (cb_y + 10.0)).abs() < 0.01, "abs y should be cb_y + 10, got {} (cb_y={})", abs_box.rect.y, cb_y, ); }
#[test] fn absolute_does_not_affect_sibling_layout() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // <div style="position: relative;"> // <div style="position: absolute; top: 0; left: 0; width: 100px; height: 100px;"></div> // <p>Normal text</p> // </div> let container = doc.create_element("div"); let abs_child = doc.create_element("div"); let p = doc.create_element("p"); let text = doc.create_text("Normal text"); doc.append_child(body, container); doc.append_child(container, abs_child); doc.append_child(container, p); doc.append_child(p, text); doc.set_attribute(container, "style", "position: relative;"); doc.set_attribute( abs_child, "style", "position: absolute; top: 0; left: 0; width: 100px; height: 100px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// The <p> should start at the top of the container (abs child doesn't // push it down). Find the first in-flow child. let p_box = container_box .children .iter() .find(|c| c.position != Position::Absolute && c.position != Position::Fixed) .expect("should have an in-flow child");
// p's y should be at (or very near) the container's content y. let expected_y = container_box.rect.y; assert!( (p_box.rect.y - expected_y).abs() < 1.0, "p should start near container top: p.y={}, expected={}", p_box.rect.y, expected_y, ); }
#[test] fn fixed_positioned_relative_to_viewport() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // <div style="position: fixed; top: 5px; left: 10px; width: 50px; height: 30px;"></div> let fixed = doc.create_element("div"); doc.append_child(body, fixed); doc.set_attribute( fixed, "style", "position: fixed; top: 5px; left: 10px; width: 50px; height: 30px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let fixed_box = &body_box.children[0];
assert_eq!(fixed_box.position, Position::Fixed); assert_eq!(fixed_box.rect.width, 50.0); assert_eq!(fixed_box.rect.height, 30.0);
// Fixed should be relative to viewport (0,0). assert!( (fixed_box.rect.x - 10.0).abs() < 0.01, "fixed x should be 10, got {}", fixed_box.rect.x, ); assert!( (fixed_box.rect.y - 5.0).abs() < 0.01, "fixed y should be 5, got {}", fixed_box.rect.y, ); }
#[test] fn z_index_ordering() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // Create a positioned container with two abs children at different z-index. let container = doc.create_element("div"); let low = doc.create_element("div"); let high = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, low); doc.append_child(container, high); doc.set_attribute( container, "style", "position: relative; width: 200px; height: 200px;", ); doc.set_attribute( low, "style", "position: absolute; z-index: 1; top: 0; left: 0; width: 50px; height: 50px;", ); doc.set_attribute( high, "style", "position: absolute; z-index: 5; top: 0; left: 0; width: 50px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// Both should have correct z-index stored. let low_box = &container_box.children[0]; let high_box = &container_box.children[1]; assert_eq!(low_box.z_index, Some(1)); assert_eq!(high_box.z_index, Some(5)); }
#[test] fn absolute_stretching_left_right() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // <div style="position: relative; width: 400px; height: 300px;"> // <div style="position: absolute; left: 10px; right: 20px; height: 50px;"></div> // </div> let container = doc.create_element("div"); let abs_child = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, abs_child); doc.set_attribute( container, "style", "position: relative; width: 400px; height: 300px;", ); doc.set_attribute( abs_child, "style", "position: absolute; left: 10px; right: 20px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let abs_box = &container_box.children[0];
// Width should be: container_width - left - right = 400 - 10 - 20 = 370 assert!( (abs_box.rect.width - 370.0).abs() < 0.01, "stretched width should be 370, got {}", abs_box.rect.width, ); }
#[test] fn absolute_bottom_right_positioning() { let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); // <div style="position: relative; width: 400px; height: 300px;"> // <div style="position: absolute; bottom: 10px; right: 20px; width: 50px; height: 30px;"></div> // </div> let container = doc.create_element("div"); let abs_child = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, abs_child); doc.set_attribute( container, "style", "position: relative; width: 400px; height: 300px;", ); doc.set_attribute( abs_child, "style", "position: absolute; bottom: 10px; right: 20px; width: 50px; height: 30px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let abs_box = &container_box.children[0];
let cb_x = container_box.rect.x - container_box.padding.left; let cb_y = container_box.rect.y - container_box.padding.top; let cb_w = container_box.rect.width + container_box.padding.left + container_box.padding.right; let cb_h = container_box.rect.height + container_box.padding.top + container_box.padding.bottom;
// x = cb_x + cb_w - right - width = cb_x + cb_w - 20 - 50 let expected_x = cb_x + cb_w - 20.0 - 50.0; // y = cb_y + cb_h - bottom - height = cb_y + cb_h - 10 - 30 let expected_y = cb_y + cb_h - 10.0 - 30.0;
assert!( (abs_box.rect.x - expected_x).abs() < 0.01, "abs x with right should be {}, got {}", expected_x, abs_box.rect.x, ); assert!( (abs_box.rect.y - expected_y).abs() < 0.01, "abs y with bottom should be {}, got {}", expected_y, abs_box.rect.y, ); }
// ----------------------------------------------------------------------- // Sticky positioning tests // -----------------------------------------------------------------------
#[test] fn sticky_element_laid_out_in_normal_flow() { // A sticky element should participate in normal flow just like a // static or relative element — its siblings should be positioned // as if sticky doesn't exist. let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); let container = doc.create_element("div"); let before = doc.create_element("div"); let sticky = doc.create_element("div"); let after = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, before); doc.append_child(container, sticky); doc.append_child(container, after);
doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute(before, "style", "height: 50px;"); doc.set_attribute(sticky, "style", "position: sticky; top: 0; height: 30px;"); doc.set_attribute(after, "style", "height: 60px;");
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// All three children should be present (sticky is in-flow). let in_flow: Vec<&LayoutBox> = container_box.children.iter().collect(); assert!( in_flow.len() >= 3, "expected 3 children, got {}", in_flow.len() );
let before_box = &in_flow[0]; let sticky_box = &in_flow[1]; let after_box = &in_flow[2];
assert_eq!(sticky_box.position, Position::Sticky);
// Sticky element should be right after 'before' (at y = before.y + 50). let expected_sticky_y = before_box.rect.y + 50.0; assert!( (sticky_box.rect.y - expected_sticky_y).abs() < 1.0, "sticky y should be ~{}, got {}", expected_sticky_y, sticky_box.rect.y, );
// 'after' should follow the sticky element (at y = sticky.y + 30). let expected_after_y = sticky_box.rect.y + 30.0; assert!( (after_box.rect.y - expected_after_y).abs() < 1.0, "after y should be ~{}, got {}", expected_after_y, after_box.rect.y, ); }
#[test] fn sticky_constraint_rect_is_set() { // The layout engine should set `sticky_constraint` to the parent's // content rect for sticky children. let mut doc = Document::new(); let (_, _, body) = make_html_body(&mut doc); let container = doc.create_element("div"); let sticky = doc.create_element("div"); doc.append_child(body, container); doc.append_child(container, sticky);
doc.set_attribute(container, "style", "width: 400px; height: 300px;"); doc.set_attribute(sticky, "style", "position: sticky; top: 0; height: 50px;");
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let sticky_box = &container_box.children[0];
assert_eq!(sticky_box.position, Position::Sticky); let constraint = sticky_box .sticky_constraint .expect("sticky element should have a constraint rect");
// Constraint should match the container's content rect. assert!( (constraint.width - container_box.rect.width).abs() < 0.01, "constraint width should match container: {} vs {}", constraint.width, container_box.rect.width, ); assert!( (constraint.height - container_box.rect.height).abs() < 0.01, "constraint height should match container: {} vs {}", constraint.height, container_box.rect.height, ); }
// ----------------------------------------------------------------------- // Float layout tests // -----------------------------------------------------------------------
#[test] fn float_left_positioned_at_left_edge() { // A float:left element should be placed at the left edge of the container. let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute( float_elem, "style", "float: left; width: 100px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let float_box = &container_box.children[0];
assert_eq!(float_box.float, Float::Left); assert!( (float_box.rect.width - 100.0).abs() < 0.01, "float width: {}", float_box.rect.width ); assert!( (float_box.rect.height - 50.0).abs() < 0.01, "float height: {}", float_box.rect.height ); // Float should be at the left edge of the container (same x as container content). assert!( (float_box.rect.x - container_box.rect.x).abs() < 0.01, "float x should be at container left edge: {} vs {}", float_box.rect.x, container_box.rect.x, ); }
#[test] fn float_right_positioned_at_right_edge() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute( float_elem, "style", "float: right; width: 100px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let float_box = &container_box.children[0];
assert_eq!(float_box.float, Float::Right); // Float's right edge (content + padding + border + margin) should align // with the container's right content edge. let float_right = float_box.rect.x + float_box.rect.width; let container_right = container_box.rect.x + container_box.rect.width; assert!( (float_right - container_right).abs() < 0.01, "float right edge {} should match container right edge {}", float_right, container_right, ); }
#[test] fn two_left_floats_stack_horizontally() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float1 = doc.create_element("div"); let float2 = doc.create_element("div"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float1); doc.append_child(container, float2); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute(float1, "style", "float: left; width: 100px; height: 50px;"); doc.set_attribute(float2, "style", "float: left; width: 120px; height: 50px;");
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0]; let f1 = &container_box.children[0]; let f2 = &container_box.children[1];
// Second float should be placed to the right of the first. let f1_right = f1.rect.x + f1.rect.width + f1.padding.right + f1.border.right + f1.margin.right; assert!( f2.rect.x >= f1_right - 0.01, "second float x ({}) should be >= first float right edge ({})", f2.rect.x, f1_right, ); // Both should be on the same row (same y). assert!( (f1.rect.y - f2.rect.y).abs() < 0.01, "floats should be on the same row: {} vs {}", f1.rect.y, f2.rect.y, ); }
#[test] fn clear_both_moves_below_floats() { let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); let cleared = doc.create_element("div"); let text = doc.create_text("After clear"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.append_child(container, cleared); doc.append_child(cleared, text); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute( float_elem, "style", "float: left; width: 100px; height: 80px;", ); doc.set_attribute(cleared, "style", "clear: both;");
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// Find the cleared element (skip the float). let float_box = &container_box.children[0]; let cleared_box = &container_box.children[1];
let float_bottom = float_box.rect.y + float_box.rect.height + float_box.padding.bottom + float_box.border.bottom + float_box.margin.bottom; assert!( cleared_box.rect.y >= float_bottom - 0.01, "cleared element y ({}) should be >= float bottom ({})", cleared_box.rect.y, float_bottom, ); }
#[test] fn bfc_contains_floats() { // A container with overflow:hidden (establishes BFC) should expand // to contain its floated children. let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.set_attribute(container, "style", "width: 400px; overflow: hidden;"); doc.set_attribute( float_elem, "style", "float: left; width: 100px; height: 150px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// Container should be at least 150px tall to contain the float. assert!( container_box.rect.height >= 150.0 - 0.01, "BFC container height ({}) should be >= float height (150)", container_box.rect.height, ); }
#[test] fn inline_text_wraps_around_float() { // Text in a block should flow around a float. let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); let text_p = doc.create_element("p"); let text = doc.create_text("Some text content that wraps"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.append_child(container, text_p); doc.append_child(text_p, text); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute( float_elem, "style", "float: left; width: 100px; height: 80px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// Find the text paragraph (it should be the second child, after the float). let text_box = &container_box.children[1];
// The text lines that overlap with the float should be offset to // the right of the float. Check that the first line's x is shifted. if !text_box.lines.is_empty() { let first_line = &text_box.lines[0]; let float_box = &container_box.children[0]; let float_right = float_box.rect.x + float_box.rect.width + float_box.padding.right + float_box.border.right + float_box.margin.right; assert!( first_line.x >= float_right - 0.01, "first line x ({}) should be >= float right edge ({})", first_line.x, float_right, ); } }
#[test] fn bare_text_alongside_float_is_laid_out() { // When a container has only float children + bare text (no block // siblings), the text should still be wrapped in an anonymous block // and laid out correctly. let mut doc = Document::new(); let root = doc.root(); let html = doc.create_element("html"); let body = doc.create_element("body"); let container = doc.create_element("div"); let float_elem = doc.create_element("div"); let text = doc.create_text("Hello world"); doc.append_child(root, html); doc.append_child(html, body); doc.append_child(body, container); doc.append_child(container, float_elem); doc.append_child(container, text); doc.set_attribute(container, "style", "width: 400px;"); doc.set_attribute( float_elem, "style", "float: left; width: 100px; height: 50px;", );
let tree = layout_doc(&doc); let body_box = &tree.root.children[0]; let container_box = &body_box.children[0];
// The text should be wrapped in an anonymous block and produce text lines. let has_text = container_box.children.iter().any(|c| !c.lines.is_empty()); assert!( has_text, "bare text alongside a float should be laid out (found {} children, none with text lines)", container_box.children.len(), ); }}