browser_html.c 257 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173
  1. /* browser_html: see browser_html.h. A deliberately tolerant parser: bad
  2. * nesting is repaired, unknown tags are transparent, and the CSS engine only
  3. * implements the properties in the browser's documented subset. */
  4. #include "browser_html.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. static int is_space(char c);
  8. static int is_ident(char c);
  9. /* ---- storage (one document per process) ----
  10. *
  11. * The node/text/attribute arenas are allocated from the ring-3 heap, not
  12. * static BSS: a large article needs several MB and the process image lives
  13. * in the 4 MB user window. heap_reset() on navigation releases them with
  14. * the rest of the page heap. */
  15. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  16. static char css_store[BR_CSS_SIZE];
  17. static int root_font_px = 16;
  18. /* Distinct background-image URLs per page (app stores give every card an
  19. * icon through an inline style). */
  20. #define BR_BG_URLS 512
  21. /* Background URLs live in one pool: a data: URL (Infima's menu chevron is
  22. * an inline SVG) is far longer than a fixed slot. */
  23. #define BR_BG_URL_POOL (128 * 1024)
  24. #define BR_BG_URL_MAX 8191
  25. static const char *bg_urls[BR_BG_URLS];
  26. static char bg_url_pool[BR_BG_URL_POOL];
  27. static unsigned int bg_url_pool_used;
  28. static unsigned int bg_url_count;
  29. /* @font-face definitions, one document at a time. The URL is resolved by
  30. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  31. struct br_font_def {
  32. char family[BR_FACE_FAMILY];
  33. char url[BR_FACE_URL];
  34. char base[192];
  35. unsigned short wmin, wmax; /* font-weight descriptor (a range for
  36. * variable fonts) */
  37. unsigned char italic; /* font-style italic/oblique */
  38. unsigned char family_id; /* first entry of the same family */
  39. unsigned char used; /* a font-family declaration names it */
  40. };
  41. static struct br_font_def font_defs[BR_MAX_FACES];
  42. static int font_face_count;
  43. static char font_base[192];
  44. static void font_reset(void)
  45. {
  46. memset(font_defs, 0, sizeof(font_defs));
  47. font_face_count = 0;
  48. font_base[0] = '\0';
  49. }
  50. void br_set_font_base(const char *base)
  51. {
  52. unsigned int i = 0;
  53. if (!base)
  54. base = "";
  55. while (base[i] && i + 1 < sizeof(font_base)) {
  56. font_base[i] = base[i];
  57. i++;
  58. }
  59. font_base[i] = '\0';
  60. }
  61. int br_font_face_count(void)
  62. {
  63. return font_face_count;
  64. }
  65. const char *br_font_face_url(int id)
  66. {
  67. if (id < 1 || id > font_face_count)
  68. return "";
  69. return font_defs[id - 1].url;
  70. }
  71. const char *br_font_face_base(int id)
  72. {
  73. if (id < 1 || id > font_face_count)
  74. return "";
  75. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  76. font_base;
  77. }
  78. const char *br_font_face_family(int id)
  79. {
  80. if (id < 1 || id > font_face_count)
  81. return "";
  82. return font_defs[id - 1].family;
  83. }
  84. int br_font_face_info(int id, struct br_font_face_info *out)
  85. {
  86. const struct br_font_def *f;
  87. if (id < 1 || id > font_face_count || !out)
  88. return -1;
  89. f = &font_defs[id - 1];
  90. out->family_id = f->family_id;
  91. out->wmin = f->wmin;
  92. out->wmax = f->wmax;
  93. out->italic = f->italic;
  94. out->used = f->used;
  95. return 0;
  96. }
  97. const char *br_bg_image_url(unsigned int idx)
  98. {
  99. if (idx >= bg_url_count)
  100. return "";
  101. return bg_urls[idx];
  102. }
  103. unsigned int br_root_font_px(void)
  104. {
  105. return (unsigned int)root_font_px;
  106. }
  107. /* ---- tag table ---- */
  108. struct tag_name {
  109. const char *name;
  110. unsigned char tag;
  111. };
  112. static const struct tag_name tag_names[] = {
  113. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  114. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  115. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  116. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  117. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  118. { "template", BR_TAG_TEMPLATE },
  119. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  120. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  121. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  122. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  123. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  124. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  125. { "picture", BR_TAG_PICTURE },
  126. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  127. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  128. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  129. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  130. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  131. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  132. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  133. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  134. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  135. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  136. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  137. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  138. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  139. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  140. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  141. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  142. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  143. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  144. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  145. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  146. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  147. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  148. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  149. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  150. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  151. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  152. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  153. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  154. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  155. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  156. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  157. { 0, 0 }
  158. };
  159. const char *br_tag_name(unsigned int tag)
  160. {
  161. unsigned int i;
  162. for (i = 0; tag_names[i].name; i++)
  163. if (tag_names[i].tag == tag)
  164. return tag_names[i].name;
  165. return "?";
  166. }
  167. static unsigned int s_len(const char *s);
  168. #define TAG_HASH_SIZE 128
  169. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  170. static int tag_hash_ready;
  171. static unsigned int tag_hash_key(const char *name, unsigned int len)
  172. {
  173. unsigned int i, h = 2166136261u;
  174. for (i = 0; i < len; i++) {
  175. h ^= (unsigned char)name[i];
  176. h *= 16777619u;
  177. }
  178. return h & (TAG_HASH_SIZE - 1);
  179. }
  180. static void tag_hash_build(void)
  181. {
  182. unsigned int i;
  183. for (i = 0; i < TAG_HASH_SIZE; i++)
  184. tag_hash_idx[i] = 0xFF;
  185. for (i = 0; tag_names[i].name; i++) {
  186. unsigned int h = tag_hash_key(tag_names[i].name,
  187. s_len(tag_names[i].name));
  188. while (tag_hash_idx[h] != 0xFF)
  189. h = (h + 1) & (TAG_HASH_SIZE - 1);
  190. tag_hash_idx[h] = (unsigned char)i;
  191. }
  192. tag_hash_ready = 1;
  193. }
  194. unsigned int br_tag_from_name(const char *name)
  195. {
  196. char lower[16];
  197. unsigned int i, len, h;
  198. unsigned char idx;
  199. for (i = 0; i < 15 && name[i]; i++) {
  200. char c = name[i];
  201. if (c >= 'A' && c <= 'Z')
  202. c = (char)(c - 'A' + 'a');
  203. lower[i] = c;
  204. }
  205. lower[i] = '\0';
  206. len = i;
  207. if (!tag_hash_ready)
  208. tag_hash_build();
  209. h = tag_hash_key(lower, len);
  210. for (;;) {
  211. idx = tag_hash_idx[h];
  212. if (idx == 0xFF)
  213. return BR_TAG_UNKNOWN;
  214. {
  215. const char *n = tag_names[idx].name;
  216. unsigned int j;
  217. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  218. ;
  219. if (!lower[j] && !n[j])
  220. return tag_names[idx].tag;
  221. }
  222. h = (h + 1) & (TAG_HASH_SIZE - 1);
  223. }
  224. }
  225. int br_tag_is_void(unsigned int tag)
  226. {
  227. switch (tag) {
  228. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  229. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  230. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  231. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  232. case BR_TAG_AREA: case BR_TAG_COL:
  233. return 1;
  234. default:
  235. return 0;
  236. }
  237. }
  238. int br_tag_is_block(unsigned int tag)
  239. {
  240. switch (tag) {
  241. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  242. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  243. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  244. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  245. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  246. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  247. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  248. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  249. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  250. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  251. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  252. case BR_TAG_PICTURE:
  253. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  254. case BR_TAG_ADDRESS: case BR_TAG_HR:
  255. return 1;
  256. default:
  257. return 0;
  258. }
  259. }
  260. /* ---- string / entity helpers ---- */
  261. static unsigned int s_len(const char *s)
  262. {
  263. unsigned int n = 0;
  264. while (s && s[n])
  265. n++;
  266. return n;
  267. }
  268. static void s_copy(char *d, const char *s, unsigned int cap)
  269. {
  270. unsigned int i = 0;
  271. if (!cap)
  272. return;
  273. while (s && s[i] && i + 1 < cap) {
  274. d[i] = s[i];
  275. i++;
  276. }
  277. d[i] = '\0';
  278. }
  279. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  280. {
  281. unsigned int i;
  282. for (i = 0; i < n; i++) {
  283. char ca = a[i], cb = b[i];
  284. if (ca >= 'A' && ca <= 'Z')
  285. ca = (char)(ca - 'A' + 'a');
  286. if (cb >= 'A' && cb <= 'Z')
  287. cb = (char)(cb - 'A' + 'a');
  288. if (ca != cb)
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. #define CURSOR_CSS_(e, css, file) css,
  294. static const char *const css_cursor_names[CURSOR_COUNT] = {
  295. CURSOR_LIST(CURSOR_CSS_)
  296. };
  297. #undef CURSOR_CSS_
  298. /* CSS `cursor`: the keyword after the last comma (url() images before it
  299. * are not supported, so the fallback keyword is what counts). Returns
  300. * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
  301. * anything else (the declaration is then ignored). */
  302. static int parse_cursor(const char *v, unsigned int n)
  303. {
  304. unsigned int a = 0, b, i;
  305. for (i = 0; i < n; i++)
  306. if (v[i] == ',')
  307. a = i + 1;
  308. while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
  309. a++;
  310. b = a;
  311. while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
  312. (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
  313. b++;
  314. if (b == a)
  315. return -1;
  316. if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
  317. return BR_CURSOR_AUTO;
  318. if (b - a == 4 && ci_eq_n(v + a, "none", 4))
  319. return CURSOR_DEFAULT + 1;
  320. for (i = 0; i < CURSOR_COUNT; i++)
  321. if (strlen(css_cursor_names[i]) == b - a &&
  322. ci_eq_n(v + a, css_cursor_names[i], b - a))
  323. return (int)i + 1;
  324. return -1;
  325. }
  326. static int find_ci(const char *hay, unsigned int len, const char *needle,
  327. unsigned int nlen, unsigned int from)
  328. {
  329. unsigned int i;
  330. if (nlen == 0 || from >= len)
  331. return -1;
  332. for (i = from; i + nlen <= len; i++)
  333. if (ci_eq_n(hay + i, needle, nlen))
  334. return (int)i;
  335. return -1;
  336. }
  337. static const struct {
  338. const char *name;
  339. const char *text;
  340. } entities[] = {
  341. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  342. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  343. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  344. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  345. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  346. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  347. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  348. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  349. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  350. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  351. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  352. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  353. { 0, 0 }
  354. };
  355. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  356. {
  357. if (*n + 1 < cap)
  358. out[(*n)++] = c;
  359. }
  360. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  361. char *out, unsigned int cap, unsigned int *n)
  362. {
  363. unsigned int start = *pos + 1, i = start;
  364. unsigned int code = 0;
  365. int ok = 0;
  366. while (i < len && s[i] != ';' && i - start < 12)
  367. i++;
  368. if (i >= len || s[i] != ';') {
  369. /* Not an entity: emit the '&' and advance past it. Leaving
  370. * *pos unchanged here used to spin forever on bare '&'. */
  371. emit(out, cap, n, '&');
  372. *pos = start;
  373. return;
  374. }
  375. if (s[start] == '#') {
  376. unsigned int j = start + 1;
  377. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  378. j++;
  379. for (; j < i; j++) {
  380. char c = s[j];
  381. int d = -1;
  382. if (c >= '0' && c <= '9') d = c - '0';
  383. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  384. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  385. if (d < 0) break;
  386. code = code * 16 + (unsigned int)d;
  387. ok = 1;
  388. }
  389. } else {
  390. for (; j < i; j++) {
  391. if (s[j] < '0' || s[j] > '9')
  392. break;
  393. code = code * 10 + (unsigned int)(s[j] - '0');
  394. ok = 1;
  395. }
  396. }
  397. } else {
  398. unsigned int k;
  399. for (k = 0; entities[k].name; k++) {
  400. unsigned int el = s_len(entities[k].name);
  401. unsigned int j;
  402. if (el != i - start)
  403. continue;
  404. for (j = 0; j < el; j++)
  405. if (s[start + j] != entities[k].name[j])
  406. break;
  407. if (j == el) {
  408. const char *t = entities[k].text;
  409. while (*t)
  410. emit(out, cap, n, *t++);
  411. *pos = i + 1;
  412. return;
  413. }
  414. }
  415. }
  416. if (ok && code) {
  417. if (code == 0x00A0)
  418. emit(out, cap, n, ' ');
  419. else if ((code >= 0x2010 && code <= 0x2015) ||
  420. code == 0x2212) /* hyphens, dashes */
  421. emit(out, cap, n, '-');
  422. else if (code == 0x2018 || code == 0x2019)
  423. emit(out, cap, n, '\'');
  424. else if (code == 0x201C || code == 0x201D)
  425. emit(out, cap, n, '"');
  426. else if (code == 0x2026) {
  427. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  428. emit(out, cap, n, '.');
  429. } else if (code <= 0xFF) {
  430. emit(out, cap, n, (char)code);
  431. } else {
  432. emit(out, cap, n, '?');
  433. }
  434. } else {
  435. emit(out, cap, n, '?');
  436. }
  437. *pos = i + 1;
  438. }
  439. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  440. * space and drops leading spaces. Returns the output length. */
  441. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  442. unsigned int cap, int collapse)
  443. {
  444. unsigned int i = 0, n = 0;
  445. while (i < len) {
  446. unsigned char c = (unsigned char)s[i];
  447. if (c == '&') {
  448. decode_entity(s, len, &i, out, cap, &n);
  449. continue;
  450. }
  451. if (c < 0x80) {
  452. i++;
  453. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  454. c == '\r' || c == '\f')) {
  455. if (n > 0 && out[n - 1] == ' ')
  456. continue;
  457. emit(out, cap, &n, ' ');
  458. continue;
  459. }
  460. if (!collapse && c == '\t') {
  461. emit(out, cap, &n, ' ');
  462. emit(out, cap, &n, ' ');
  463. emit(out, cap, &n, ' ');
  464. emit(out, cap, &n, ' ');
  465. continue;
  466. }
  467. emit(out, cap, &n, (char)c);
  468. continue;
  469. }
  470. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  471. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  472. unsigned int cp = ((c & 0x1F) << 6) |
  473. ((unsigned char)s[i + 1] & 0x3F);
  474. i += 2;
  475. if (cp == 0xA0)
  476. cp = ' ';
  477. else if (cp == 0xB7)
  478. cp = '-';
  479. else if (cp == 0xD7)
  480. cp = 'x';
  481. else if (cp == 0xB0)
  482. cp = 'o';
  483. if (cp <= 0xFF)
  484. emit(out, cap, &n, (char)cp);
  485. else
  486. emit(out, cap, &n, '?');
  487. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  488. unsigned int cp = ((c & 0x0F) << 12) |
  489. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  490. ((unsigned char)s[i + 2] & 0x3F);
  491. i += 3;
  492. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212) {
  493. emit(out, cap, &n, '-');
  494. } else if (cp == 0x2018 || cp == 0x2019) {
  495. emit(out, cap, &n, '\'');
  496. } else if (cp == 0x201C || cp == 0x201D) {
  497. emit(out, cap, &n, '"');
  498. } else if (cp == 0x2026) {
  499. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  500. emit(out, cap, &n, '.');
  501. } else if (cp == 0x2192) {
  502. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  503. } else if (cp == 0x2264) {
  504. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  505. } else {
  506. emit(out, cap, &n, '?');
  507. }
  508. } else {
  509. i++;
  510. emit(out, cap, &n, '?');
  511. }
  512. }
  513. out[n] = '\0';
  514. return n;
  515. }
  516. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  517. * space / empty. */
  518. /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
  519. * cap silently dropped every class past it (arstechnica's header lost its
  520. * background, alignment and all responsive variants). */
  521. #define BR_CLASS_MAX 2048
  522. #define BR_STYLE_ATTR_MAX 2048
  523. static unsigned int store_attr(struct br_doc *d, const char *s,
  524. unsigned int len, unsigned int limit)
  525. {
  526. unsigned int n;
  527. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  528. return 0;
  529. if (len > limit)
  530. len = limit;
  531. n = decode_text(s, len, d->attrs + d->attrs_used,
  532. BR_ATTR_SIZE - d->attrs_used, 0);
  533. if (n == 0)
  534. return 0;
  535. {
  536. unsigned int off = d->attrs_used;
  537. d->attrs_used += n + 1;
  538. return off;
  539. }
  540. }
  541. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  542. {
  543. return off ? d->attrs + off : "";
  544. }
  545. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  546. static unsigned int store_raw(struct br_doc *d, const char *s,
  547. unsigned int len, unsigned int limit)
  548. {
  549. unsigned int off;
  550. if (len > limit)
  551. len = limit;
  552. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  553. return 0;
  554. off = d->attrs_used;
  555. {
  556. unsigned int i;
  557. for (i = 0; i < len; i++)
  558. d->attrs[off + i] = s[i];
  559. }
  560. d->attrs[off + len] = '\0';
  561. d->attrs_used += len + 1;
  562. return off;
  563. }
  564. #define VAR_HASH_SIZE 1024
  565. static unsigned int var_hash(const char *name, unsigned int len)
  566. {
  567. unsigned int i, h = 2166136261u;
  568. for (i = 0; i < len; i++) {
  569. h ^= (unsigned char)name[i];
  570. h *= 16777619u;
  571. }
  572. return h & (VAR_HASH_SIZE - 1);
  573. }
  574. static unsigned int css_var_find(struct br_doc *d, const char *name,
  575. unsigned int nlen)
  576. {
  577. unsigned int h = var_hash(name, nlen);
  578. for (;;) {
  579. unsigned int i = d->var_slot[h];
  580. if (!i)
  581. return BR_MAX_VARS;
  582. i--;
  583. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  584. const char *n = attr_ptr(d, d->var_name[i]);
  585. unsigned int j;
  586. for (j = 0; j < nlen && n[j] == name[j]; j++)
  587. ;
  588. if (j == nlen)
  589. return i;
  590. }
  591. h = (h + 1) & (VAR_HASH_SIZE - 1);
  592. }
  593. }
  594. #define BR_VAR_ROOT 1
  595. #define BR_VAR_CONFLICT 2
  596. #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
  597. /* Custom properties live in one document-wide table (var() is expanded
  598. * when a sheet is parsed, not per element). Definitions on :root, html,
  599. * body or * are the theme and always apply, last one winning. A definition
  600. * on any other selector is a local override: it is used while it is the
  601. * only value seen for that name, and a second, different local value marks
  602. * the name ambiguous so var() falls back. Last-wins across components was
  603. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  604. * .fa-fw, and every icon became 100% wide. */
  605. static void css_var_set(struct br_doc *d, const char *name,
  606. unsigned int nlen, const char *val, unsigned int vlen,
  607. int root)
  608. {
  609. unsigned int i, noff, voff;
  610. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  611. return;
  612. i = css_var_find(d, name, nlen);
  613. if (i < BR_MAX_VARS && !root) {
  614. const char *old = attr_ptr(d, d->var_val[i]);
  615. unsigned int k = 0;
  616. d->var_flags[i] |= BR_VAR_SCOPED;
  617. if (d->var_flags[i] & BR_VAR_ROOT)
  618. return;
  619. while (k < vlen && old[k] == val[k])
  620. k++;
  621. if (!(k == vlen && old[k] == '\0'))
  622. d->var_flags[i] |= BR_VAR_CONFLICT;
  623. return;
  624. }
  625. noff = store_raw(d, name, nlen, 63);
  626. voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
  627. if (!noff || !voff)
  628. return;
  629. if (i >= BR_MAX_VARS) {
  630. unsigned int h;
  631. if (d->nvars >= BR_MAX_VARS)
  632. return;
  633. i = d->nvars++;
  634. d->var_name[i] = noff;
  635. h = var_hash(name, nlen);
  636. while (d->var_slot[h])
  637. h = (h + 1) & (VAR_HASH_SIZE - 1);
  638. d->var_slot[h] = (unsigned short)(i + 1);
  639. d->var_flags[i] = 0;
  640. }
  641. d->var_val[i] = voff;
  642. if (root)
  643. d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
  644. (d->var_flags[i] & BR_VAR_SCOPED));
  645. else
  646. d->var_flags[i] |= BR_VAR_SCOPED;
  647. }
  648. /* 1 when every selector of the list targets the document root (the theme
  649. * scope for custom properties): :root, html, body, * or :host, optionally
  650. * with classes/attributes but no combinator. */
  651. static int css_root_selector(const char *s, unsigned int len)
  652. {
  653. unsigned int i = 0;
  654. while (i < len) {
  655. unsigned int st, en;
  656. while (i < len && (is_space(s[i]) || s[i] == ','))
  657. i++;
  658. if (i >= len)
  659. break;
  660. st = i;
  661. while (i < len && s[i] != ',')
  662. i++;
  663. en = i;
  664. while (en > st && is_space(s[en - 1]))
  665. en--;
  666. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  667. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  668. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  669. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  670. s[st] == '*'))
  671. return 0;
  672. for (; st < en; st++)
  673. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  674. s[st] == '~')
  675. return 0;
  676. }
  677. return 1;
  678. }
  679. /* Find the end of a declaration value: the next ';' outside quotes and
  680. * parentheses. The caller passes the position just after the ':'. */
  681. static unsigned int css_value_end(const char *s, unsigned int len,
  682. unsigned int i)
  683. {
  684. int depth = 0;
  685. char quote = 0;
  686. while (i < len) {
  687. char c = s[i];
  688. if (quote) {
  689. if (c == quote)
  690. quote = 0;
  691. else if (c == '\\' && i + 1 < len)
  692. i++;
  693. } else if (c == '"' || c == '\'') {
  694. quote = c;
  695. } else if (c == '(') {
  696. depth++;
  697. } else if (c == ')' && depth > 0) {
  698. depth--;
  699. } else if (c == ';' && depth == 0) {
  700. break;
  701. }
  702. i++;
  703. }
  704. return i;
  705. }
  706. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  707. * second palette. We render the light page, so skip their custom-property
  708. * definitions; otherwise their last-wins values leak into the light rules. */
  709. static int css_dark_selector(const char *s, unsigned int len)
  710. {
  711. unsigned int i;
  712. for (i = 0; i + 4 <= len; i++) {
  713. if ((s[i] == 'n' || s[i] == 'N') &&
  714. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  715. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  716. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  717. (s[i + 4] == 't' || s[i + 4] == 'T'))
  718. return 1;
  719. if ((s[i] == 'd' || s[i] == 'D') &&
  720. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  721. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  722. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  723. return 1;
  724. if (i + 12 <= len &&
  725. (s[i] == 'c' || s[i] == 'C') &&
  726. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  727. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  728. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  729. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  730. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  731. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  732. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  733. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  734. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  735. s[i + 10] == '-' &&
  736. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  737. (s[i + 12] == 's' || s[i + 12] == 'S'))
  738. return 1;
  739. }
  740. return 0;
  741. }
  742. /* Collect --custom-property definitions from one declaration block. */
  743. static void css_collect_block(struct br_doc *d, const char *s,
  744. unsigned int len, int root)
  745. {
  746. unsigned int i = 0;
  747. while (i < len) {
  748. unsigned int pstart, pend, vstart, vend;
  749. while (i < len && (is_space(s[i]) || s[i] == ';'))
  750. i++;
  751. if (i >= len)
  752. break;
  753. pstart = i;
  754. while (i < len && s[i] != ':' && s[i] != ';')
  755. i++;
  756. pend = i;
  757. if (i >= len || s[i] != ':') {
  758. if (i < len)
  759. i++;
  760. continue;
  761. }
  762. i++;
  763. vstart = i;
  764. i = css_value_end(s, len, i);
  765. vend = i;
  766. if (i < len)
  767. i++;
  768. while (pstart < pend && is_space(s[pstart]))
  769. pstart++;
  770. while (pend > pstart && is_space(s[pend - 1]))
  771. pend--;
  772. while (vstart < vend && is_space(s[vstart]))
  773. vstart++;
  774. while (vend > vstart && is_space(s[vend - 1]))
  775. vend--;
  776. if (vend - vstart >= 10 &&
  777. ci_eq_n(s + vend - 10, "!important", 10)) {
  778. vend -= 10;
  779. while (vend > vstart && is_space(s[vend - 1]))
  780. vend--;
  781. }
  782. if (pend - pstart >= 3 && s[pstart] == '-' &&
  783. s[pstart + 1] == '-')
  784. css_var_set(d, s + pstart, pend - pstart,
  785. s + vstart, vend - vstart, root);
  786. }
  787. }
  788. const char *br_href(struct br_doc *d, unsigned int node)
  789. {
  790. return attr_ptr(d, d->nodes[node].href_off);
  791. }
  792. const char *br_src(struct br_doc *d, unsigned int node)
  793. {
  794. return attr_ptr(d, d->nodes[node].src_off);
  795. }
  796. const char *br_alt(struct br_doc *d, unsigned int node)
  797. {
  798. return attr_ptr(d, d->nodes[node].alt_off);
  799. }
  800. const char *br_rel(struct br_doc *d, unsigned int node)
  801. {
  802. return attr_ptr(d, d->nodes[node].rel_off);
  803. }
  804. const char *br_value(struct br_doc *d, unsigned int node)
  805. {
  806. return attr_ptr(d, d->nodes[node].src_off);
  807. }
  808. const char *br_placeholder(struct br_doc *d, unsigned int node)
  809. {
  810. return attr_ptr(d, d->nodes[node].alt_off);
  811. }
  812. const char *br_type(struct br_doc *d, unsigned int node)
  813. {
  814. return attr_ptr(d, d->nodes[node].rel_off);
  815. }
  816. /* ---- document setup / node append ---- */
  817. static void rule_index_reset(struct br_doc *d)
  818. {
  819. unsigned int i;
  820. for (i = 0; i < BR_MAX_RULES; i++)
  821. d->rule_next[i] = 0xFFFF;
  822. for (i = 0; i < BR_RULE_HASH; i++)
  823. d->rule_bucket[i] = 0xFFFF;
  824. for (i = 0; i < BR_TAG_MAX; i++)
  825. d->rule_tag[i] = 0xFFFF;
  826. d->rule_any = 0xFFFF;
  827. }
  828. void br_doc_init(struct br_doc *d)
  829. {
  830. unsigned int i;
  831. for (i = 0; i < sizeof(*d); i++)
  832. ((unsigned char *)d)[i] = 0;
  833. rule_index_reset(d);
  834. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  835. d->text = malloc(BR_TEXT_SIZE);
  836. d->attrs = malloc(BR_ATTR_SIZE);
  837. if (!d->nodes || !d->text || !d->attrs) {
  838. free(d->nodes);
  839. free(d->text);
  840. free(d->attrs);
  841. d->nodes = 0;
  842. d->text = 0;
  843. d->attrs = 0;
  844. return;
  845. }
  846. d->rules = rule_fallback;
  847. d->rule_cap = BR_RULE_FALLBACK;
  848. d->attrs_used = 1; /* offset 0 = "no attribute" */
  849. d->attrs[0] = '\0';
  850. d->nvars = 0;
  851. d->nlayers = 0;
  852. d->root = 0;
  853. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  854. d->var_slot[vi] = 0;
  855. root_font_px = 16;
  856. bg_url_count = 0;
  857. bg_url_pool_used = 0;
  858. font_reset();
  859. }
  860. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  861. unsigned int cap)
  862. {
  863. if (!buf || !cap) {
  864. d->rules = rule_fallback;
  865. d->rule_cap = BR_RULE_FALLBACK;
  866. d->nrules = 0;
  867. return;
  868. }
  869. if (cap > BR_MAX_RULES)
  870. cap = BR_MAX_RULES;
  871. d->rules = buf;
  872. d->rule_cap = cap;
  873. d->nrules = 0;
  874. }
  875. static void doc_reset(struct br_doc *d)
  876. {
  877. unsigned int i;
  878. d->nnode = 0;
  879. d->text_used = 0;
  880. d->attrs_used = 1;
  881. d->attrs[0] = '\0';
  882. d->nrules = 0;
  883. d->ncontent = 0;
  884. d->npe_rules = 0;
  885. rule_index_reset(d);
  886. d->title[0] = '\0';
  887. d->inline_css_len = 0;
  888. d->nvars = 0;
  889. d->nlayers = 0;
  890. d->truncated = 0;
  891. d->plain = 0;
  892. d->script_src = 0;
  893. d->nscripts = 0;
  894. d->scripts_truncated = 0;
  895. font_reset();
  896. for (i = 0; i < BR_MAX_NODES; i++) {
  897. d->nodes[i].type = BR_NODE_ELEM;
  898. d->nodes[i].tag = BR_TAG_UNKNOWN;
  899. d->nodes[i].flags = 0;
  900. /* The cached disabled/open/inert bits must not survive into
  901. * the next page: a slot that held a disabled control made
  902. * Docusaurus' search field unfocusable after navigating (and
  903. * real hardware does not hand out zeroed memory). */
  904. d->nodes[i].attr_flags = 0;
  905. d->nodes[i].parent = 0;
  906. d->nodes[i].first = d->nodes[i].last = 0;
  907. d->nodes[i].next = 0;
  908. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  909. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  910. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  911. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  912. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  913. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  914. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  915. d->nodes[i].name_off = 0;
  916. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  917. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  918. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  919. d->nodes[i].svg_rx = 0;
  920. d->nodes[i].img_index = -1;
  921. d->nodes[i].bg_img_index = -1;
  922. d->nodes[i].style_valid = 0;
  923. d->nodes[i].pe_state = 0;
  924. }
  925. }
  926. static void node_zero(struct br_node *n);
  927. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  928. unsigned char type, unsigned char tag)
  929. {
  930. unsigned int idx;
  931. struct br_node *n;
  932. if (d->nnode >= BR_MAX_NODES) {
  933. d->truncated = 1;
  934. return 0;
  935. }
  936. idx = d->nnode++;
  937. n = &d->nodes[idx];
  938. node_zero(n); /* every field, attr_flags included */
  939. n->type = type;
  940. n->tag = tag;
  941. n->parent = (unsigned short)parent;
  942. if (d->nodes[parent].last)
  943. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  944. else
  945. d->nodes[parent].first = (unsigned short)idx;
  946. d->nodes[parent].last = (unsigned short)idx;
  947. return idx;
  948. }
  949. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  950. const char *s, unsigned int len, int pre)
  951. {
  952. char *dst;
  953. unsigned int n;
  954. unsigned int idx;
  955. if (len == 0)
  956. return 0;
  957. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  958. d->truncated = 1;
  959. return 0;
  960. }
  961. dst = d->text + d->text_used;
  962. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  963. if (n == 0) {
  964. d->text[d->text_used] = '\0';
  965. return 0;
  966. }
  967. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  968. if (!idx)
  969. return 0;
  970. d->nodes[idx].text_off = d->text_used;
  971. d->nodes[idx].text_len = (unsigned short)n;
  972. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  973. d->text_used += n + 1;
  974. return idx;
  975. }
  976. /* Raw text child (no entity decoding, whitespace kept): the body of a
  977. * data-block <script> such as type="application/json", which scripts read
  978. * through textContent. Bodies longer than a text node holds are dropped. */
  979. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  980. const char *s, unsigned int len)
  981. {
  982. unsigned int idx, i;
  983. if (len == 0 || len > 0xFFFFu)
  984. return 0;
  985. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  986. d->truncated = 1;
  987. return 0;
  988. }
  989. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  990. if (!idx)
  991. return 0;
  992. for (i = 0; i < len; i++)
  993. d->text[d->text_used + i] = s[i];
  994. d->text[d->text_used + len] = '\0';
  995. d->nodes[idx].text_off = d->text_used;
  996. d->nodes[idx].text_len = (unsigned short)len;
  997. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  998. d->text_used += len + 1;
  999. return idx;
  1000. }
  1001. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  1002. static int same_string(const char *a, const char *b)
  1003. {
  1004. while (*a && *a == *b) {
  1005. a++;
  1006. b++;
  1007. }
  1008. return *a == *b;
  1009. }
  1010. static int same_string_n(const char *a, const char *b, unsigned int n)
  1011. {
  1012. unsigned int i;
  1013. for (i = 0; i < n; i++)
  1014. if (a[i] != b[i])
  1015. return 0;
  1016. return 1;
  1017. }
  1018. static int node_detached(struct br_doc *d, unsigned int node)
  1019. {
  1020. unsigned int guard = 0;
  1021. while (node && node < d->nnode && guard++ <= d->nnode) {
  1022. if (d->nodes[node].flags & BR_NODE_DETACHED)
  1023. return 1;
  1024. node = d->nodes[node].parent;
  1025. }
  1026. return 0;
  1027. }
  1028. int br_node_detached(struct br_doc *d, unsigned int node)
  1029. {
  1030. return !d || node >= d->nnode || node_detached(d, node);
  1031. }
  1032. unsigned int br_find_id(struct br_doc *d, const char *id)
  1033. {
  1034. unsigned int i;
  1035. if (!d || !id)
  1036. return 0;
  1037. for (i = 0; i < d->nnode; i++) {
  1038. struct br_node *n = &d->nodes[i];
  1039. if (n->type == BR_NODE_ELEM && n->id_off &&
  1040. same_string(d->attrs + n->id_off, id) &&
  1041. !node_detached(d, i))
  1042. return i + 1;
  1043. }
  1044. return 0;
  1045. }
  1046. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1047. unsigned int cap, unsigned int *used)
  1048. {
  1049. unsigned int c;
  1050. if (node >= d->nnode)
  1051. return;
  1052. if (d->nodes[node].type == BR_NODE_TEXT) {
  1053. unsigned int len = d->nodes[node].text_len;
  1054. unsigned int off = d->nodes[node].text_off;
  1055. if (*used + len >= cap)
  1056. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1057. for (c = 0; c < len; c++)
  1058. out[*used + c] = d->text[off + c];
  1059. *used += len;
  1060. return;
  1061. }
  1062. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1063. text_walk(d, c, out, cap, used);
  1064. }
  1065. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1066. char *out, unsigned int cap)
  1067. {
  1068. unsigned int used = 0;
  1069. if (!d || !out || cap == 0 || node >= d->nnode)
  1070. return 0;
  1071. text_walk(d, node, out, cap, &used);
  1072. out[used] = '\0';
  1073. return used;
  1074. }
  1075. int br_set_text_content(struct br_doc *d, unsigned int node,
  1076. const char *text, unsigned int len)
  1077. {
  1078. struct br_node *n;
  1079. unsigned int idx;
  1080. unsigned int i;
  1081. if (!d || node >= d->nnode)
  1082. return -1;
  1083. /* Detach the old subtree; its arena storage is reclaimed on the next
  1084. * navigation. */
  1085. {
  1086. unsigned int c;
  1087. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1088. d->nodes[c].flags |= BR_NODE_DETACHED;
  1089. }
  1090. d->nodes[node].first = d->nodes[node].last = 0;
  1091. d->nodes[node].style_valid = 0;
  1092. d->nodes[node].pe_state = 0;
  1093. if (len == 0)
  1094. return 0;
  1095. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1096. d->truncated = 1;
  1097. return -1;
  1098. }
  1099. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1100. if (!idx)
  1101. return -1;
  1102. for (i = 0; i < len; i++)
  1103. d->text[d->text_used + i] = text[i];
  1104. d->text[d->text_used + len] = '\0';
  1105. n = &d->nodes[idx];
  1106. n->text_off = d->text_used;
  1107. n->text_len = (unsigned short)len;
  1108. n->flags = 0;
  1109. d->text_used += len + 1;
  1110. return 0;
  1111. }
  1112. /* Replace the value of an existing text or comment node in place, keeping the
  1113. * node handle (and therefore the JS wrapper) valid. */
  1114. int br_set_text_node(struct br_doc *d, unsigned int node,
  1115. const char *text, unsigned int len)
  1116. {
  1117. struct br_node *n;
  1118. unsigned int i;
  1119. if (!d || node >= d->nnode)
  1120. return -1;
  1121. n = &d->nodes[node];
  1122. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1123. return -1;
  1124. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1125. d->truncated = 1;
  1126. return -1;
  1127. }
  1128. for (i = 0; i < len; i++)
  1129. d->text[d->text_used + i] = text[i];
  1130. d->text[d->text_used + len] = '\0';
  1131. n->text_off = d->text_used;
  1132. n->text_len = (unsigned short)len;
  1133. d->text_used += len + 1;
  1134. return 0;
  1135. }
  1136. /* ---- runtime DOM construction and mutation ---- */
  1137. static unsigned int text_len(const char *s)
  1138. {
  1139. unsigned int n = 0;
  1140. while (s && s[n])
  1141. n++;
  1142. return n;
  1143. }
  1144. static void node_zero(struct br_node *n)
  1145. {
  1146. unsigned int i;
  1147. for (i = 0; i < sizeof(*n); i++)
  1148. ((unsigned char *)n)[i] = 0;
  1149. n->img_index = -1;
  1150. n->bg_img_index = -1;
  1151. }
  1152. static void node_unlink(struct br_doc *d, unsigned int handle)
  1153. {
  1154. unsigned int node, parent, c, prev = 0;
  1155. if (!d || handle == 0 || handle > d->nnode)
  1156. return;
  1157. node = handle - 1;
  1158. parent = d->nodes[node].parent;
  1159. if (parent >= d->nnode) {
  1160. d->nodes[node].parent = 0;
  1161. d->nodes[node].next = 0;
  1162. return;
  1163. }
  1164. c = d->nodes[parent].first;
  1165. while (c && c != node) {
  1166. prev = c;
  1167. c = d->nodes[c].next;
  1168. }
  1169. if (c == node) {
  1170. if (prev)
  1171. d->nodes[prev].next = d->nodes[node].next;
  1172. else
  1173. d->nodes[parent].first = d->nodes[node].next;
  1174. if (d->nodes[parent].last == node)
  1175. d->nodes[parent].last = (unsigned short)prev;
  1176. }
  1177. d->nodes[node].parent = 0;
  1178. d->nodes[node].next = 0;
  1179. d->nodes[node].flags |= BR_NODE_DETACHED;
  1180. }
  1181. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1182. const char *s, unsigned int len);
  1183. static unsigned int find_tag_end(const char *s, unsigned int i,
  1184. unsigned int len)
  1185. {
  1186. char quote = 0;
  1187. while (i < len) {
  1188. char c = s[i];
  1189. if (quote) {
  1190. if (c == quote)
  1191. quote = 0;
  1192. } else if (c == '"' || c == '\'') {
  1193. quote = c;
  1194. } else if (c == '>') {
  1195. return i;
  1196. }
  1197. i++;
  1198. }
  1199. return len;
  1200. }
  1201. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1202. const char *html, unsigned int len)
  1203. {
  1204. unsigned int stack[64];
  1205. unsigned int sp = 1, i = 0;
  1206. if (!d || node >= d->nnode)
  1207. return -1;
  1208. stack[0] = node;
  1209. {
  1210. unsigned int c;
  1211. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1212. d->nodes[c].flags |= BR_NODE_DETACHED;
  1213. }
  1214. d->nodes[node].first = d->nodes[node].last = 0;
  1215. d->nodes[node].style_valid = 0;
  1216. d->nodes[node].pe_state = 0;
  1217. while (i < len) {
  1218. if (html[i] != '<') {
  1219. unsigned int start = i, end;
  1220. while (i < len && html[i] != '<')
  1221. i++;
  1222. end = i;
  1223. if (end > start) {
  1224. /* Entities decoded like the page parser does
  1225. * (createTextNode stays literal). */
  1226. char *tb = (char *)malloc(end - start + 1);
  1227. unsigned int tl = 0, h = 0;
  1228. if (tb) {
  1229. tl = decode_text(html + start, end - start,
  1230. tb, end - start + 1, 1);
  1231. h = br_create_text(d, tb, tl);
  1232. free(tb);
  1233. }
  1234. if (h)
  1235. br_append_child(d, stack[sp - 1], h);
  1236. }
  1237. continue;
  1238. }
  1239. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1240. html[i + 3] == '-') {
  1241. int p = find_ci(html, len, "-->", 3, i + 4);
  1242. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1243. unsigned int h = br_create_comment(d, html + i + 4,
  1244. end > i + 4 ?
  1245. end - i - 4 : 0);
  1246. if (h)
  1247. br_append_child(d, stack[sp - 1], h);
  1248. i = p >= 0 ? (unsigned int)p + 3 : len;
  1249. continue;
  1250. }
  1251. if (html[i + 1] == '/') {
  1252. unsigned int p = find_tag_end(html, i + 2, len);
  1253. i = p < len ? p + 1 : len;
  1254. if (sp > 1)
  1255. sp--;
  1256. continue;
  1257. }
  1258. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1259. unsigned int p = find_tag_end(html, i + 2, len);
  1260. i = p < len ? p + 1 : len;
  1261. continue;
  1262. }
  1263. {
  1264. unsigned int p = find_tag_end(html, i + 1, len);
  1265. unsigned int ns = i + 1, ne, as, ae;
  1266. unsigned int h, k;
  1267. char name[32];
  1268. int selfclose = 0;
  1269. while (ns < p && is_space(html[ns]))
  1270. ns++;
  1271. ne = ns;
  1272. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1273. ne++;
  1274. as = ne;
  1275. ae = p;
  1276. if (ae > as && html[ae - 1] == '/') {
  1277. selfclose = 1;
  1278. ae--;
  1279. }
  1280. if (ne == ns) {
  1281. i = p < len ? p + 1 : len;
  1282. continue;
  1283. }
  1284. if (ne - ns >= sizeof(name)) {
  1285. i = p < len ? p + 1 : len;
  1286. continue;
  1287. }
  1288. for (k = 0; k < ne - ns; k++)
  1289. name[k] = html[ns + k];
  1290. name[ne - ns] = '\0';
  1291. h = br_create_element(d, name, ne - ns);
  1292. if (!h) {
  1293. i = p < len ? p + 1 : len;
  1294. continue;
  1295. }
  1296. if (ae > as)
  1297. parse_attrs(d, &d->nodes[h - 1], html + as,
  1298. ae - as);
  1299. br_append_child(d, stack[sp - 1], h);
  1300. {
  1301. unsigned int tag = d->nodes[h - 1].tag;
  1302. if (!selfclose && !br_tag_is_void(tag) &&
  1303. sp < sizeof(stack) / sizeof(stack[0]))
  1304. stack[sp++] = h - 1;
  1305. }
  1306. i = p < len ? p + 1 : len;
  1307. }
  1308. }
  1309. return 0;
  1310. }
  1311. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1312. {
  1313. unsigned int i;
  1314. if (!d)
  1315. return 0;
  1316. for (i = 0; i < d->nnode; i++)
  1317. if (d->nodes[i].type == BR_NODE_ELEM &&
  1318. d->nodes[i].tag == (unsigned char)tag &&
  1319. !node_detached(d, i))
  1320. return i + 1;
  1321. return 0;
  1322. }
  1323. unsigned int br_create_element(struct br_doc *d, const char *name,
  1324. unsigned int len)
  1325. {
  1326. struct br_node *n;
  1327. unsigned int idx;
  1328. if (!d || !name)
  1329. return 0;
  1330. if (d->nnode >= BR_MAX_NODES) {
  1331. d->truncated = 1;
  1332. return 0;
  1333. }
  1334. idx = d->nnode++;
  1335. n = &d->nodes[idx];
  1336. node_zero(n);
  1337. n->type = BR_NODE_ELEM;
  1338. n->flags = BR_NODE_DETACHED;
  1339. n->tag = (unsigned char)br_tag_from_name(name);
  1340. if (n->tag == BR_TAG_UNKNOWN && len)
  1341. n->name_off = store_attr(d, name, len, 31);
  1342. return idx + 1;
  1343. }
  1344. unsigned int br_create_text(struct br_doc *d, const char *text,
  1345. unsigned int len)
  1346. {
  1347. struct br_node *n;
  1348. unsigned int idx, i;
  1349. if (!d)
  1350. return 0;
  1351. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1352. d->truncated = 1;
  1353. return 0;
  1354. }
  1355. idx = d->nnode++;
  1356. n = &d->nodes[idx];
  1357. node_zero(n);
  1358. n->type = BR_NODE_TEXT;
  1359. n->flags = BR_NODE_DETACHED;
  1360. n->text_off = d->text_used;
  1361. n->text_len = (unsigned short)len;
  1362. for (i = 0; i < len; i++)
  1363. d->text[d->text_used + i] = text[i];
  1364. d->text[d->text_used + len] = '\0';
  1365. d->text_used += len + 1;
  1366. return idx + 1;
  1367. }
  1368. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1369. unsigned int len)
  1370. {
  1371. unsigned int h = br_create_text(d, text, len);
  1372. if (h)
  1373. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1374. return h;
  1375. }
  1376. unsigned int br_create_fragment(struct br_doc *d)
  1377. {
  1378. struct br_node *n;
  1379. unsigned int idx;
  1380. if (!d)
  1381. return 0;
  1382. if (d->nnode >= BR_MAX_NODES) {
  1383. d->truncated = 1;
  1384. return 0;
  1385. }
  1386. idx = d->nnode++;
  1387. n = &d->nodes[idx];
  1388. node_zero(n);
  1389. n->type = BR_NODE_FRAGMENT;
  1390. n->flags = BR_NODE_DETACHED;
  1391. return idx + 1;
  1392. }
  1393. int br_append_child(struct br_doc *d, unsigned int parent,
  1394. unsigned int child)
  1395. {
  1396. unsigned int p;
  1397. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1398. parent == child - 1)
  1399. return -1;
  1400. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1401. /* Appending a fragment inserts its children, not the fragment. */
  1402. unsigned int c = d->nodes[child - 1].first;
  1403. while (c) {
  1404. unsigned int next = d->nodes[c].next;
  1405. br_append_child(d, parent, c + 1);
  1406. c = next;
  1407. }
  1408. return 0;
  1409. }
  1410. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1411. if (p == child - 1)
  1412. return -1;
  1413. node_unlink(d, child);
  1414. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1415. d->nodes[child - 1].parent = (unsigned short)parent;
  1416. d->nodes[child - 1].next = 0;
  1417. if (d->nodes[parent].last)
  1418. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1419. else
  1420. d->nodes[parent].first = (unsigned short)(child - 1);
  1421. d->nodes[parent].last = (unsigned short)(child - 1);
  1422. return 0;
  1423. }
  1424. int br_insert_before(struct br_doc *d, unsigned int parent,
  1425. unsigned int child, unsigned int before)
  1426. {
  1427. unsigned int prev = 0, c;
  1428. if (before == 0)
  1429. return br_append_child(d, parent, child);
  1430. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1431. before > d->nnode || parent == child - 1)
  1432. return -1;
  1433. if (d->nodes[before - 1].parent != parent)
  1434. return -1;
  1435. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1436. unsigned int c = d->nodes[child - 1].first;
  1437. while (c) {
  1438. unsigned int next = d->nodes[c].next;
  1439. br_insert_before(d, parent, c + 1, before);
  1440. c = next;
  1441. }
  1442. return 0;
  1443. }
  1444. {
  1445. unsigned int p;
  1446. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1447. if (p == child - 1)
  1448. return -1;
  1449. }
  1450. node_unlink(d, child);
  1451. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1452. c = d->nodes[parent].first;
  1453. while (c && c != before - 1) {
  1454. prev = c;
  1455. c = d->nodes[c].next;
  1456. }
  1457. if (c != before - 1)
  1458. return -1;
  1459. d->nodes[child - 1].parent = (unsigned short)parent;
  1460. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1461. if (prev)
  1462. d->nodes[prev].next = (unsigned short)(child - 1);
  1463. else
  1464. d->nodes[parent].first = (unsigned short)(child - 1);
  1465. return 0;
  1466. }
  1467. int br_remove_child(struct br_doc *d, unsigned int parent,
  1468. unsigned int child)
  1469. {
  1470. if (!d || child == 0 || child > d->nnode ||
  1471. d->nodes[child - 1].parent != parent)
  1472. return -1;
  1473. node_unlink(d, child);
  1474. return 0;
  1475. }
  1476. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1477. unsigned int child)
  1478. {
  1479. unsigned int c;
  1480. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1481. return 0;
  1482. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1483. if (c == child - 1)
  1484. return child;
  1485. return 0;
  1486. }
  1487. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1488. const char *name)
  1489. {
  1490. struct br_node *n;
  1491. const char *p, *end, *found = 0;
  1492. if (!d || node >= d->nnode || !name)
  1493. return 0;
  1494. n = &d->nodes[node];
  1495. p = d->attrs + n->extra_off;
  1496. end = p + n->extra_len;
  1497. while (p + 1 < end) {
  1498. const char *an = p;
  1499. const char *av;
  1500. p += text_len(p) + 1;
  1501. if (p >= end)
  1502. break;
  1503. av = p;
  1504. p += text_len(p) + 1;
  1505. if (same_string(an, name))
  1506. found = av;
  1507. }
  1508. if (found)
  1509. return found;
  1510. if (same_string(name, "id") && n->id_off)
  1511. return d->attrs + n->id_off;
  1512. if (same_string(name, "class") && n->class_off)
  1513. return d->attrs + n->class_off;
  1514. if (same_string(name, "style") && n->style_off)
  1515. return d->attrs + n->style_off;
  1516. if (same_string(name, "href") && n->href_off)
  1517. return d->attrs + n->href_off;
  1518. if (same_string(name, "src") && n->src_off)
  1519. return d->attrs + n->src_off;
  1520. if (same_string(name, "alt") && n->alt_off)
  1521. return d->attrs + n->alt_off;
  1522. if (same_string(name, "rel") && n->rel_off)
  1523. return d->attrs + n->rel_off;
  1524. return 0;
  1525. }
  1526. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1527. const char *name, const char *value,
  1528. unsigned int len)
  1529. {
  1530. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1531. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1532. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1533. * exactly like parse_attrs(); script-built inputs (Vue sets
  1534. * type="password" after createElement) must land there too. */
  1535. if (control && same_string(name, "type")) {
  1536. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1537. return;
  1538. }
  1539. if (control && same_string(name, "value")) {
  1540. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1541. return;
  1542. }
  1543. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1544. same_string(name, "placeholder")) {
  1545. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1546. return;
  1547. }
  1548. if (same_string(name, "id"))
  1549. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1550. else if (same_string(name, "class"))
  1551. n->class_off = len ? store_attr(d, value, len,
  1552. BR_CLASS_MAX) : 0;
  1553. else if (same_string(name, "style"))
  1554. n->style_off = len ? store_attr(d, value, len,
  1555. BR_STYLE_ATTR_MAX) : 0;
  1556. else if (same_string(name, "href"))
  1557. n->href_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1558. else if (same_string(name, "src"))
  1559. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1560. else if (same_string(name, "alt"))
  1561. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1562. else if (same_string(name, "rel"))
  1563. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1564. }
  1565. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1566. const char *value, unsigned int len)
  1567. {
  1568. struct br_node *n;
  1569. unsigned int start, nl, need;
  1570. const char *p, *old, *oldend;
  1571. if (!d || node >= d->nnode || !name)
  1572. return -1;
  1573. n = &d->nodes[node];
  1574. nl = text_len(name);
  1575. need = nl + 1 + len + 1;
  1576. start = d->attrs_used;
  1577. old = d->attrs + n->extra_off;
  1578. oldend = old + n->extra_len;
  1579. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1580. d->truncated = 1;
  1581. return -1;
  1582. }
  1583. p = old;
  1584. while (p + 1 < oldend) {
  1585. const char *an = p;
  1586. const char *av;
  1587. unsigned int anl, avl, k;
  1588. anl = text_len(an);
  1589. p += anl + 1;
  1590. if (p >= oldend)
  1591. break;
  1592. av = p;
  1593. avl = text_len(av);
  1594. p += avl + 1;
  1595. if (same_string(an, name))
  1596. continue;
  1597. for (k = 0; k < anl; k++)
  1598. d->attrs[d->attrs_used++] = an[k];
  1599. d->attrs[d->attrs_used++] = '\0';
  1600. for (k = 0; k < avl; k++)
  1601. d->attrs[d->attrs_used++] = av[k];
  1602. d->attrs[d->attrs_used++] = '\0';
  1603. }
  1604. {
  1605. unsigned int k;
  1606. for (k = 0; k < nl; k++)
  1607. d->attrs[d->attrs_used++] = name[k];
  1608. d->attrs[d->attrs_used++] = '\0';
  1609. for (k = 0; k < len; k++)
  1610. d->attrs[d->attrs_used++] = value[k];
  1611. d->attrs[d->attrs_used++] = '\0';
  1612. }
  1613. n = &d->nodes[node];
  1614. n->extra_off = start;
  1615. n->extra_len = (unsigned short)(d->attrs_used - start);
  1616. n->style_valid = 0;
  1617. n->pe_state = 0;
  1618. attr_note_known(d, n, name, value, len);
  1619. /* A script that sets a style/class (Angular's ng-style) or a new src
  1620. * after the first image scan re-arms the node for the loader; -2
  1621. * ("nothing to load") would otherwise be final. */
  1622. if ((same_string(name, "style") || same_string(name, "class")) &&
  1623. n->bg_img_index == -2)
  1624. n->bg_img_index = -1;
  1625. if (same_string(name, "src") && n->img_index == -2)
  1626. n->img_index = -1;
  1627. return 0;
  1628. }
  1629. static void clear_attr_flag(struct br_node *n, const char *name);
  1630. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1631. {
  1632. struct br_node *n;
  1633. unsigned int start;
  1634. const char *p, *oldend;
  1635. int found = 0;
  1636. if (!d || node >= d->nnode || !name)
  1637. return -1;
  1638. n = &d->nodes[node];
  1639. start = d->attrs_used;
  1640. p = d->attrs + n->extra_off;
  1641. oldend = p + n->extra_len;
  1642. if (start + n->extra_len > BR_ATTR_SIZE)
  1643. return -1;
  1644. while (p + 1 < oldend) {
  1645. const char *an = p;
  1646. const char *av;
  1647. unsigned int anl, avl, k;
  1648. anl = text_len(an);
  1649. p += anl + 1;
  1650. if (p >= oldend)
  1651. break;
  1652. av = p;
  1653. avl = text_len(av);
  1654. p += avl + 1;
  1655. if (same_string(an, name)) {
  1656. found = 1;
  1657. continue;
  1658. }
  1659. for (k = 0; k < anl; k++)
  1660. d->attrs[d->attrs_used++] = an[k];
  1661. d->attrs[d->attrs_used++] = '\0';
  1662. for (k = 0; k < avl; k++)
  1663. d->attrs[d->attrs_used++] = av[k];
  1664. d->attrs[d->attrs_used++] = '\0';
  1665. }
  1666. n = &d->nodes[node];
  1667. n->extra_off = start;
  1668. n->extra_len = (unsigned short)(d->attrs_used - start);
  1669. n->style_valid = 0;
  1670. n->pe_state = 0;
  1671. attr_note_known(d, n, name, "", 0);
  1672. if (found)
  1673. clear_attr_flag(n, name);
  1674. return found ? 0 : -1;
  1675. }
  1676. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1677. {
  1678. struct br_node *src;
  1679. unsigned int h;
  1680. const char *name;
  1681. if (!d || node == 0 || node > d->nnode)
  1682. return 0;
  1683. src = &d->nodes[node - 1];
  1684. if (src->type == BR_NODE_TEXT)
  1685. return br_create_text(d, d->text + src->text_off, src->text_len);
  1686. if (src->type == BR_NODE_COMMENT)
  1687. return br_create_comment(d, d->text + src->text_off,
  1688. src->text_len);
  1689. if (src->type == BR_NODE_FRAGMENT)
  1690. h = br_create_fragment(d);
  1691. else {
  1692. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1693. name = d->attrs + src->name_off;
  1694. else
  1695. name = br_tag_name(src->tag);
  1696. h = br_create_element(d, name, text_len(name));
  1697. }
  1698. if (!h)
  1699. return 0;
  1700. {
  1701. const char *p = d->attrs + src->extra_off;
  1702. const char *end = p + src->extra_len;
  1703. while (p + 1 < end) {
  1704. const char *an = p;
  1705. const char *av;
  1706. unsigned int anl, avl;
  1707. anl = text_len(an);
  1708. p += anl + 1;
  1709. if (p >= end)
  1710. break;
  1711. av = p;
  1712. avl = text_len(av);
  1713. p += avl + 1;
  1714. /* h is a 1-based handle; br_set_attr takes the
  1715. * node index (the old call stamped the copy's
  1716. * attributes onto the previous node). */
  1717. br_set_attr(d, h - 1, an, av, avl);
  1718. }
  1719. }
  1720. if (deep) {
  1721. unsigned int c;
  1722. for (c = src->first; c; c = d->nodes[c].next) {
  1723. unsigned int ch = br_clone_node(d, c + 1, 1);
  1724. if (ch)
  1725. br_append_child(d, h - 1, ch);
  1726. }
  1727. }
  1728. return h;
  1729. }
  1730. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1731. unsigned int len);
  1732. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1733. unsigned int *st, unsigned int *en);
  1734. /* querySelector(All) and Element.matches use the cascade's selector engine
  1735. * (sel_match(), below). */
  1736. static int match_selector(struct br_doc *d, unsigned int node,
  1737. const char *sel, unsigned int len)
  1738. {
  1739. while (len > 0 && sel[0] == ' ') {
  1740. sel++;
  1741. len--;
  1742. }
  1743. while (len > 0 && sel[len - 1] == ' ')
  1744. len--;
  1745. return len && sel_match(d, node, sel, len);
  1746. }
  1747. static int query_add(unsigned int *out, unsigned int max,
  1748. unsigned int *count, unsigned int node)
  1749. {
  1750. unsigned int i;
  1751. for (i = 0; i < *count; i++)
  1752. if (out[i] == node + 1)
  1753. return 0;
  1754. if (*count < max)
  1755. out[(*count)++] = node + 1;
  1756. return 0;
  1757. }
  1758. static void query_walk(struct br_doc *d, unsigned int node,
  1759. const char *sel, unsigned int len, unsigned int *out,
  1760. unsigned int max, unsigned int *count)
  1761. {
  1762. unsigned int c;
  1763. if (d->nodes[node].type == BR_NODE_ELEM &&
  1764. match_selector(d, node, sel, len))
  1765. query_add(out, max, count, node);
  1766. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1767. query_walk(d, c, sel, len, out, max, count);
  1768. }
  1769. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1770. {
  1771. unsigned int len;
  1772. if (!d || !selector || node == 0 || node > d->nnode)
  1773. return 0;
  1774. len = text_len(selector);
  1775. while (len > 0 && selector[0] == ' ') {
  1776. selector++;
  1777. len--;
  1778. }
  1779. while (len > 0 && selector[len - 1] == ' ')
  1780. len--;
  1781. {
  1782. unsigned int pos = 0, st, en;
  1783. while (sel_list_next(selector, len, &pos, &st, &en))
  1784. if (match_selector(d, node - 1, selector + st, en - st))
  1785. return 1;
  1786. }
  1787. return 0;
  1788. }
  1789. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1790. const char *selector, unsigned int *out,
  1791. unsigned int max)
  1792. {
  1793. unsigned int count = 0, i = 0, len, start, end;
  1794. if (!d || !selector || !out || max == 0)
  1795. return 0;
  1796. len = text_len(selector);
  1797. while (sel_list_next(selector, len, &i, &start, &end)) {
  1798. if (root == 0) {
  1799. unsigned int n;
  1800. for (n = 1; n < d->nnode; n++)
  1801. if (d->nodes[n].type == BR_NODE_ELEM &&
  1802. !node_detached(d, n) &&
  1803. match_selector(d, n, selector + start,
  1804. end - start))
  1805. query_add(out, max, &count, n);
  1806. } else if (root <= d->nnode) {
  1807. unsigned int c;
  1808. for (c = d->nodes[root - 1].first; c;
  1809. c = d->nodes[c].next)
  1810. query_walk(d, c, selector + start,
  1811. end - start, out, max, &count);
  1812. }
  1813. }
  1814. return count;
  1815. }
  1816. /* ---- attribute parsing ---- */
  1817. static int is_space(char c)
  1818. {
  1819. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1820. }
  1821. static int str_eq(const char *a, const char *b);
  1822. static unsigned int parse_uint(const char *s, unsigned int len)
  1823. {
  1824. unsigned int i = 0, v = 0;
  1825. while (i < len && is_space(s[i]))
  1826. i++;
  1827. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1828. v = v * 10 + (unsigned int)(s[i] - '0');
  1829. if (v > 4096)
  1830. v = 4096;
  1831. i++;
  1832. }
  1833. return v;
  1834. }
  1835. static int contains_ci_len(const char *s, unsigned int len,
  1836. const char *needle);
  1837. static int parse_svg_viewbox(const char *s, unsigned int len,
  1838. unsigned short *w, unsigned short *h)
  1839. {
  1840. unsigned int i = 0, v[4], n = 0;
  1841. while (i < len && n < 4) {
  1842. unsigned int x = 0;
  1843. int any = 0;
  1844. while (i < len && (is_space(s[i]) || s[i] == ','))
  1845. i++;
  1846. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1847. x = x * 10 + (unsigned int)(s[i] - '0');
  1848. i++;
  1849. any = 1;
  1850. }
  1851. /* viewBox values are commonly fractional ("0 0 340.16
  1852. * 340.16"). Consume the fraction so it is not mistaken for
  1853. * the next number; the integer part is enough for the aspect
  1854. * ratio used by the renderer. */
  1855. if (i < len && s[i] == '.') {
  1856. i++;
  1857. while (i < len && s[i] >= '0' && s[i] <= '9')
  1858. i++;
  1859. }
  1860. if (!any) {
  1861. if (i < len)
  1862. i++;
  1863. continue;
  1864. }
  1865. v[n++] = x;
  1866. }
  1867. if (n < 4)
  1868. return 0;
  1869. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1870. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1871. return *w && *h;
  1872. }
  1873. /* The inverse of note_attr_flag() for a removed attribute. The cached
  1874. * bit used to outlive the attribute: `input.disabled = false` removed
  1875. * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
  1876. * once (Docusaurus' search box while its index loads) never took focus
  1877. * again. */
  1878. static void clear_attr_flag(struct br_node *n, const char *name)
  1879. {
  1880. if (str_eq(name, "data-collapse-target"))
  1881. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
  1882. else if (str_eq(name, "data-collapse-trigger"))
  1883. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
  1884. else if (str_eq(name, "is-open"))
  1885. n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
  1886. else if (str_eq(name, "disabled"))
  1887. n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
  1888. else if (str_eq(name, "open"))
  1889. n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
  1890. else if (str_eq(name, "inert"))
  1891. n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
  1892. }
  1893. static void note_attr_flag(struct br_node *n, const char *name)
  1894. {
  1895. if (str_eq(name, "data-collapse-target"))
  1896. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1897. else if (str_eq(name, "data-collapse-trigger"))
  1898. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1899. else if (str_eq(name, "is-open"))
  1900. n->attr_flags |= BR_ATTR_IS_OPEN;
  1901. else if (str_eq(name, "disabled"))
  1902. n->attr_flags |= BR_ATTR_DISABLED;
  1903. else if (str_eq(name, "open"))
  1904. n->attr_flags |= BR_ATTR_OPEN;
  1905. else if (str_eq(name, "inert"))
  1906. n->attr_flags |= BR_ATTR_INERT;
  1907. }
  1908. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1909. * second pass so the block stays contiguous even though the known attributes
  1910. * above also use the shared attr arena. */
  1911. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1912. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1913. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1914. * written. */
  1915. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1916. {
  1917. static const struct { const char *e; char c; } ents[] = {
  1918. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1919. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1920. { 0, 0 }
  1921. };
  1922. unsigned int i = 0, n = 0;
  1923. while (i < len) {
  1924. int k, done = 0;
  1925. if (s[i] == '&') {
  1926. for (k = 0; ents[k].e; k++) {
  1927. unsigned int el = (unsigned int)strlen(ents[k].e);
  1928. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1929. out[n++] = ents[k].c;
  1930. i += el;
  1931. done = 1;
  1932. break;
  1933. }
  1934. }
  1935. if (!done && i + 3 < len && s[i + 1] == '#') {
  1936. unsigned int j = i + 2, v = 0;
  1937. int hex = s[j] == 'x' || s[j] == 'X';
  1938. if (hex)
  1939. j++;
  1940. while (j < len && j < i + 10 &&
  1941. ((s[j] >= '0' && s[j] <= '9') ||
  1942. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1943. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1944. char c = s[j++];
  1945. v = v * (hex ? 16 : 10) +
  1946. (unsigned int)(c <= '9' ? c - '0' :
  1947. (c | 0x20) - 'a' + 10);
  1948. }
  1949. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1950. out[n++] = (char)v;
  1951. i = j + 1;
  1952. done = 1;
  1953. }
  1954. }
  1955. }
  1956. if (!done)
  1957. out[n++] = s[i++];
  1958. }
  1959. return n;
  1960. }
  1961. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1962. const char *s, unsigned int len)
  1963. {
  1964. unsigned int i = 0;
  1965. unsigned int start = d->attrs_used;
  1966. while (i < len) {
  1967. char name[32];
  1968. unsigned int nl = 0, vstart, vlen;
  1969. char quote = 0;
  1970. while (i < len && is_space(s[i]))
  1971. i++;
  1972. if (i >= len || s[i] == '/')
  1973. break;
  1974. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1975. char c = s[i++];
  1976. if (c >= 'A' && c <= 'Z')
  1977. c = (char)(c - 'A' + 'a');
  1978. if (nl + 1 < sizeof(name))
  1979. name[nl++] = c;
  1980. }
  1981. name[nl] = '\0';
  1982. while (i < len && is_space(s[i]))
  1983. i++;
  1984. if (i < len && s[i] == '=') {
  1985. i++;
  1986. while (i < len && is_space(s[i]))
  1987. i++;
  1988. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1989. quote = s[i++];
  1990. vstart = i;
  1991. while (i < len && s[i] != quote)
  1992. i++;
  1993. vlen = i - vstart;
  1994. if (i < len)
  1995. i++;
  1996. } else {
  1997. vstart = i;
  1998. while (i < len && !is_space(s[i]) && s[i] != '>')
  1999. i++;
  2000. vlen = i - vstart;
  2001. }
  2002. } else {
  2003. /* Boolean attribute: expose it with an empty value. */
  2004. vstart = 0;
  2005. vlen = 0;
  2006. }
  2007. if (!nl)
  2008. continue;
  2009. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  2010. d->truncated = 1;
  2011. break;
  2012. }
  2013. {
  2014. unsigned int k;
  2015. for (k = 0; k < nl; k++)
  2016. d->attrs[d->attrs_used++] = name[k];
  2017. d->attrs[d->attrs_used++] = '\0';
  2018. d->attrs_used += attr_unescape(s + vstart, vlen,
  2019. d->attrs + d->attrs_used);
  2020. d->attrs[d->attrs_used++] = '\0';
  2021. }
  2022. }
  2023. if (d->attrs_used > start) {
  2024. n->extra_off = start;
  2025. n->extra_len = (unsigned short)(d->attrs_used - start);
  2026. }
  2027. }
  2028. /* Parse attributes from `s` (between the tag name and '>'). */
  2029. static void parse_attrs(struct br_doc *d, struct br_node *n,
  2030. const char *s, unsigned int len)
  2031. {
  2032. unsigned int i = 0;
  2033. while (i < len) {
  2034. char name[32];
  2035. unsigned int nl = 0, vstart, vlen;
  2036. char quote = 0;
  2037. while (i < len && is_space(s[i]))
  2038. i++;
  2039. if (i >= len || s[i] == '/')
  2040. break;
  2041. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  2042. char c = s[i++];
  2043. if (c >= 'A' && c <= 'Z')
  2044. c = (char)(c - 'A' + 'a');
  2045. if (nl + 1 < sizeof(name))
  2046. name[nl++] = c;
  2047. }
  2048. name[nl] = '\0';
  2049. while (i < len && is_space(s[i]))
  2050. i++;
  2051. if (i >= len || s[i] != '=') {
  2052. if (str_eq(name, "hidden"))
  2053. n->flags |= BR_NODE_HIDDEN;
  2054. note_attr_flag(n, name);
  2055. continue;
  2056. }
  2057. i++;
  2058. while (i < len && is_space(s[i]))
  2059. i++;
  2060. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2061. quote = s[i++];
  2062. vstart = i;
  2063. while (i < len && s[i] != quote)
  2064. i++;
  2065. vlen = i - vstart;
  2066. if (i < len)
  2067. i++;
  2068. } else {
  2069. vstart = i;
  2070. while (i < len && !is_space(s[i]) && s[i] != '>')
  2071. i++;
  2072. vlen = i - vstart;
  2073. }
  2074. if (!nl)
  2075. continue;
  2076. if (str_eq(name, "hidden")) {
  2077. n->flags |= BR_NODE_HIDDEN;
  2078. continue;
  2079. }
  2080. note_attr_flag(n, name);
  2081. if (str_eq(name, "href"))
  2082. n->href_off = store_attr(d, s + vstart, vlen,
  2083. BR_URL_ATTR_MAX);
  2084. else if (str_eq(name, "class"))
  2085. n->class_off = store_attr(d, s + vstart, vlen,
  2086. BR_CLASS_MAX);
  2087. else if (str_eq(name, "id"))
  2088. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2089. else if (str_eq(name, "style"))
  2090. n->style_off = store_attr(d, s + vstart, vlen,
  2091. BR_STYLE_ATTR_MAX);
  2092. else if (str_eq(name, "src"))
  2093. n->src_off = store_attr(d, s + vstart, vlen,
  2094. BR_URL_ATTR_MAX);
  2095. else if (str_eq(name, "alt"))
  2096. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2097. else if (str_eq(name, "color"))
  2098. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2099. else if (str_eq(name, "rel"))
  2100. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2101. else if (str_eq(name, "value"))
  2102. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2103. else if (str_eq(name, "placeholder") &&
  2104. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2105. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2106. else if (str_eq(name, "type") &&
  2107. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2108. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2109. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2110. else if (str_eq(name, "width")) {
  2111. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2112. if (vlen && s[vstart + vlen - 1] == '%')
  2113. n->width_pct = 1;
  2114. } else if (str_eq(name, "height")) {
  2115. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2116. if (vlen && s[vstart + vlen - 1] == '%')
  2117. n->height_pct = 1;
  2118. } else if (str_eq(name, "colspan")) {
  2119. unsigned int v = parse_uint(s + vstart, vlen);
  2120. if (v < 1)
  2121. v = 1;
  2122. if (v > 16)
  2123. v = 16;
  2124. n->colspan = (unsigned char)v;
  2125. } else if (str_eq(name, "rowspan")) {
  2126. unsigned int v = parse_uint(s + vstart, vlen);
  2127. if (v < 1)
  2128. v = 1;
  2129. if (v > 16)
  2130. v = 16;
  2131. n->rowspan = (unsigned char)v;
  2132. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2133. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2134. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2135. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2136. } else if (str_eq(name, "x2")) {
  2137. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2138. } else if (str_eq(name, "y2")) {
  2139. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2140. } else if (str_eq(name, "rx")) {
  2141. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2142. } else if (str_eq(name, "d")) {
  2143. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2144. } else if (str_eq(name, "viewbox")) {
  2145. unsigned short vw = 0, vh = 0;
  2146. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2147. n->width_attr = vw;
  2148. n->height_attr = vh;
  2149. n->flags |= BR_NODE_VIEWBOX;
  2150. }
  2151. } else if (str_eq(name, "text-anchor")) {
  2152. if (contains_ci_len(s + vstart, vlen, "middle"))
  2153. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2154. else if (contains_ci_len(s + vstart, vlen, "end"))
  2155. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2156. }
  2157. }
  2158. parse_attrs_generic(d, n, s, len);
  2159. }
  2160. static int str_eq(const char *a, const char *b)
  2161. {
  2162. unsigned int i = 0;
  2163. while (a[i] && b[i] && a[i] == b[i])
  2164. i++;
  2165. return a[i] == '\0' && b[i] == '\0';
  2166. }
  2167. /* ---- CSS ---- */
  2168. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2169. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2170. {
  2171. return parse_color(s, len, out);
  2172. }
  2173. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2174. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2175. {
  2176. while (len && is_space(*s)) {
  2177. s++;
  2178. len--;
  2179. }
  2180. while (len && is_space(s[len - 1]))
  2181. len--;
  2182. if (!len)
  2183. return 0;
  2184. if (len == 4 && ci_eq_n(s, "none", 4))
  2185. return BR_PAINT_NONE;
  2186. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2187. return BR_PAINT_CURRENT;
  2188. if (len > 4 && ci_eq_n(s, "url(", 4))
  2189. return BR_PAINT_URL;
  2190. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2191. return BR_PAINT_NONE;
  2192. if (parse_color(s, len, c))
  2193. return BR_PAINT_COLOR;
  2194. return 0;
  2195. }
  2196. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2197. static int svg_len16(const char *s, unsigned int len)
  2198. {
  2199. unsigned int i = 0;
  2200. int v = 0, frac = 0, digits = 0;
  2201. while (i < len && is_space(s[i]))
  2202. i++;
  2203. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2204. if (v < 4000)
  2205. v = v * 10 + (s[i] - '0');
  2206. i++;
  2207. digits++;
  2208. }
  2209. if (i < len && s[i] == '.') {
  2210. int scale = 1;
  2211. i++;
  2212. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2213. if (scale < 10000) {
  2214. frac = frac * 10 + (s[i] - '0');
  2215. scale *= 10;
  2216. }
  2217. i++;
  2218. digits++;
  2219. }
  2220. frac = frac * 16 / scale;
  2221. }
  2222. if (!digits)
  2223. return -1;
  2224. return v * 16 + frac;
  2225. }
  2226. /* Inside an <svg> subtree (including the <svg> element itself). */
  2227. static int in_svg(const struct br_doc *d, unsigned int node)
  2228. {
  2229. unsigned int k;
  2230. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2231. if (d->nodes[node].tag == BR_TAG_SVG)
  2232. return 1;
  2233. node = d->nodes[node].parent;
  2234. }
  2235. return 0;
  2236. }
  2237. enum {
  2238. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2239. PROP_BG_REPEAT,
  2240. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2241. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2242. PROP_FONT_PX, PROP_FONT_UNIT,
  2243. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2244. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2245. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2246. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2247. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2248. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2249. PROP_BORDER_W, PROP_BORDER_COLOR,
  2250. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2251. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2252. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2253. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2254. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2255. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2256. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2257. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2258. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2259. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2260. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2261. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2262. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2263. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2264. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2265. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2266. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2267. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2268. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2269. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2270. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2271. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2272. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2273. PROP_ZINDEX, PROP_ZSET,
  2274. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2275. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2276. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2277. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2278. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2279. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2280. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2281. PROP_COUNT
  2282. };
  2283. struct color_name {
  2284. const char *name;
  2285. unsigned short rgb;
  2286. };
  2287. static const struct color_name color_names[] = {
  2288. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2289. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2290. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2291. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2292. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2293. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2294. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2295. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2296. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2297. { "darkgrey", 0x528A }, { 0, 0 }
  2298. };
  2299. static int hexval(char c)
  2300. {
  2301. if (c >= '0' && c <= '9')
  2302. return c - '0';
  2303. if (c >= 'a' && c <= 'f')
  2304. return c - 'a' + 10;
  2305. if (c >= 'A' && c <= 'F')
  2306. return c - 'A' + 10;
  2307. return -1;
  2308. }
  2309. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2310. {
  2311. unsigned int i;
  2312. for (i = 0; color_names[i].name; i++) {
  2313. unsigned int j, n = s_len(color_names[i].name);
  2314. if (n != len)
  2315. continue;
  2316. for (j = 0; j < n; j++) {
  2317. char c = s[j];
  2318. if (c >= 'A' && c <= 'Z')
  2319. c = (char)(c - 'A' + 'a');
  2320. if (c != color_names[i].name[j])
  2321. break;
  2322. }
  2323. if (j == n) {
  2324. *out = color_names[i].rgb;
  2325. return 1;
  2326. }
  2327. }
  2328. return 0;
  2329. }
  2330. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2331. * over white; the render target is a white page. */
  2332. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2333. {
  2334. unsigned int i = 0;
  2335. while (i < len && is_space(s[i]))
  2336. i++;
  2337. if (i < len && s[i] == '#') {
  2338. int d[6], nd = 0;
  2339. i++;
  2340. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2341. d[nd++] = hexval(s[i++]);
  2342. if (nd == 3) {
  2343. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2344. unsigned int b = (unsigned int)d[2];
  2345. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2346. ((g * 17 >> 2) << 5) |
  2347. (b * 17 >> 3));
  2348. return 1;
  2349. }
  2350. if (nd >= 6) {
  2351. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2352. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2353. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2354. *out = (unsigned short)(((r >> 3) << 11) |
  2355. ((g >> 2) << 5) | (b >> 3));
  2356. return 1;
  2357. }
  2358. return 0;
  2359. }
  2360. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2361. unsigned int v[3], k = 0, a = 255;
  2362. while (i < len && s[i] != '(')
  2363. i++;
  2364. if (i >= len)
  2365. return 0;
  2366. i++;
  2367. while (k < 3 && i < len) {
  2368. unsigned int x = 0;
  2369. int percent = 0;
  2370. while (i < len && (is_space(s[i]) || s[i] == ','))
  2371. i++;
  2372. if (i >= len || s[i] < '0' || s[i] > '9')
  2373. return 0;
  2374. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2375. x = x * 10 + (unsigned int)(s[i] - '0');
  2376. i++;
  2377. }
  2378. if (i < len && s[i] == '%') {
  2379. percent = 1;
  2380. i++;
  2381. }
  2382. if (percent)
  2383. x = x * 255 / 100;
  2384. if (x > 255)
  2385. x = 255;
  2386. v[k++] = x;
  2387. }
  2388. /* Optional alpha in both syntaxes:
  2389. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2390. {
  2391. unsigned int whole = 0, frac = 0, scale = 1;
  2392. int percent = 0;
  2393. while (i < len && (is_space(s[i]) || s[i] == ','))
  2394. i++;
  2395. if (i < len && s[i] == '/') {
  2396. i++;
  2397. while (i < len && is_space(s[i]))
  2398. i++;
  2399. }
  2400. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2401. * used to read as opaque black. */
  2402. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2403. (s[i] == '.' && i + 1 < len &&
  2404. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2405. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2406. whole = whole * 10 +
  2407. (unsigned int)(s[i] - '0');
  2408. i++;
  2409. }
  2410. if (i < len && s[i] == '.') {
  2411. i++;
  2412. while (i < len && s[i] >= '0' &&
  2413. s[i] <= '9') {
  2414. if (scale < 1000) {
  2415. frac = frac * 10 +
  2416. (unsigned int)(s[i] - '0');
  2417. scale *= 10;
  2418. }
  2419. i++;
  2420. }
  2421. }
  2422. while (i < len && is_space(s[i]))
  2423. i++;
  2424. if (i < len && s[i] == '%')
  2425. percent = 1;
  2426. if (percent)
  2427. a = whole > 100 ? 255 :
  2428. whole * 255 / 100;
  2429. else if (whole >= 1)
  2430. a = 255;
  2431. else
  2432. a = frac * 255 / scale;
  2433. }
  2434. }
  2435. if (k == 3) {
  2436. unsigned int r = v[0], g = v[1], b = v[2];
  2437. if (a < 255) {
  2438. r = (r * a + 255u * (255u - a)) / 255u;
  2439. g = (g * a + 255u * (255u - a)) / 255u;
  2440. b = (b * a + 255u * (255u - a)) / 255u;
  2441. }
  2442. *out = (unsigned short)(((r >> 3) << 11) |
  2443. ((g >> 2) << 5) | (b >> 3));
  2444. return 1;
  2445. }
  2446. return 0;
  2447. }
  2448. {
  2449. unsigned int j = i;
  2450. while (i < len && !is_space(s[i]))
  2451. i++;
  2452. return named_color(s + j, i - j, out);
  2453. }
  2454. }
  2455. /* Fully transparent colours have no RGB565 representation. They are common
  2456. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2457. * only on hover; painting them black would draw an outline around every
  2458. * pill and card. */
  2459. static int color_is_transparent(const char *s, unsigned int len)
  2460. {
  2461. unsigned int i = 0, n = 0;
  2462. int last = 0, prev = 0, d;
  2463. while (i < len && is_space(s[i]))
  2464. i++;
  2465. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2466. return 1;
  2467. if (i >= len || s[i] != '#')
  2468. return 0;
  2469. i++;
  2470. while (i < len && hexval(s[i]) >= 0) {
  2471. d = hexval(s[i]);
  2472. prev = last;
  2473. last = d;
  2474. n++;
  2475. i++;
  2476. }
  2477. while (i < len && is_space(s[i]))
  2478. i++;
  2479. if (i != len)
  2480. return 0;
  2481. if (n == 4)
  2482. return last == 0;
  2483. if (n == 8)
  2484. return prev == 0 && last == 0;
  2485. return 0;
  2486. }
  2487. static int register_bg_url(const char *s, unsigned int len)
  2488. {
  2489. char tmp[BR_BG_URL_MAX + 1];
  2490. unsigned int i, n = 0;
  2491. char q = 0;
  2492. while (len && is_space(*s)) {
  2493. s++;
  2494. len--;
  2495. }
  2496. while (len && is_space(s[len - 1]))
  2497. len--;
  2498. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2499. s[len - 1] == s[0]) {
  2500. q = s[0];
  2501. s++;
  2502. len -= 2;
  2503. }
  2504. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2505. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2506. if (q && s[i] == '\\' && i + 1 < len) {
  2507. i++;
  2508. if (s[i] == '\n')
  2509. continue;
  2510. }
  2511. tmp[n++] = s[i];
  2512. }
  2513. tmp[n] = '\0';
  2514. if (!n)
  2515. return 0;
  2516. for (i = 0; i < bg_url_count; i++)
  2517. if (!strcmp(bg_urls[i], tmp))
  2518. return (int)i + 1;
  2519. if (bg_url_count >= BR_BG_URLS ||
  2520. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2521. return 0;
  2522. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2523. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2524. bg_url_pool_used += n + 1;
  2525. return (int)(++bg_url_count);
  2526. }
  2527. /* Length values for margins/padding: px, ignore percentages/units. */
  2528. static unsigned int viewport_w;
  2529. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2530. {
  2531. unsigned int i = *pos;
  2532. int sign = 1;
  2533. long v = 0; /* hundredths of a unit before conversion */
  2534. int digits = 0, frac = 0, unit = 0;
  2535. while (i < len && is_space(s[i]))
  2536. i++;
  2537. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2538. if (s[i] == '-')
  2539. sign = -1;
  2540. i++;
  2541. }
  2542. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2543. v = v * 10 + (s[i] - '0');
  2544. digits = 1;
  2545. i++;
  2546. }
  2547. if (i < len && s[i] == '.') {
  2548. i++;
  2549. /* Minified CSS writes ".5rem" without a leading zero. */
  2550. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2551. if (frac < 2) {
  2552. v = v * 10 + (s[i] - '0');
  2553. frac++;
  2554. digits = 1;
  2555. }
  2556. i++;
  2557. }
  2558. }
  2559. while (frac < 2) {
  2560. v *= 10;
  2561. frac++;
  2562. }
  2563. while (frac > 2) {
  2564. v /= 10;
  2565. frac--;
  2566. }
  2567. if (i < len && s[i] == '%')
  2568. unit = 2;
  2569. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2570. s[i + 2] == 'm')
  2571. unit = 1;
  2572. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2573. unit = 3;
  2574. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2575. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2576. s[i] == 'a' || s[i] == 'd'))
  2577. i++;
  2578. *pos = i;
  2579. if (!digits)
  2580. return 0;
  2581. if (unit == 1)
  2582. v *= root_font_px;
  2583. else if (unit == 3)
  2584. v *= root_font_px;
  2585. else if (unit == 2)
  2586. v = v * (long)viewport_w / 100;
  2587. v /= 100;
  2588. v *= sign;
  2589. if (v < -2048)
  2590. v = -2048;
  2591. if (v > 2048)
  2592. v = 2048;
  2593. return (int)v;
  2594. }
  2595. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2596. * treats it as a zero-length token; margins need to keep it so block layout
  2597. * can centre a box with a definite width. */
  2598. #define CSS_LEN_AUTO (-32768)
  2599. /* A box length with its unit preserved for layout-time resolution:
  2600. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2601. * rem and whole pixels for px. */
  2602. static int parse_css_len(const char *s, unsigned int len,
  2603. unsigned int *pos, int *unit)
  2604. {
  2605. unsigned int i = *pos;
  2606. int sign = 1;
  2607. long v = 0;
  2608. int digits = 0, frac = 0;
  2609. *unit = 0;
  2610. while (i < len && is_space(s[i]))
  2611. i++;
  2612. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2613. if (s[i] == '-')
  2614. sign = -1;
  2615. i++;
  2616. }
  2617. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2618. v = v * 10 + (s[i] - '0');
  2619. digits = 1;
  2620. i++;
  2621. }
  2622. if (i < len && s[i] == '.') {
  2623. i++;
  2624. /* Leading-dot fractions are common in minified CSS
  2625. * (".5rem", ".3rem"). */
  2626. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2627. if (frac < 2) {
  2628. v = v * 10 + (s[i] - '0');
  2629. frac++;
  2630. digits = 1;
  2631. }
  2632. i++;
  2633. }
  2634. }
  2635. while (frac < 2) {
  2636. v *= 10;
  2637. frac++;
  2638. }
  2639. if (!digits) {
  2640. *pos = i;
  2641. return 0;
  2642. }
  2643. if (i < len && s[i] == '%') {
  2644. *unit = 1;
  2645. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2646. s[i + 2] == 'm') {
  2647. *unit = 3;
  2648. i += 3;
  2649. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2650. *unit = 2;
  2651. i += 2;
  2652. } else {
  2653. v /= 100;
  2654. }
  2655. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2656. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2657. s[i] == 'a' || s[i] == 'd'))
  2658. i++;
  2659. *pos = i;
  2660. v *= sign;
  2661. if (v < -32768)
  2662. v = -32768;
  2663. if (v > 32767)
  2664. v = 32767;
  2665. return (int)v;
  2666. }
  2667. static int parse_margin_len(const char *s, unsigned int len,
  2668. unsigned int *pos, int *unit)
  2669. {
  2670. unsigned int i = *pos;
  2671. while (i < len && is_space(s[i]))
  2672. i++;
  2673. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2674. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2675. *pos = i + 4;
  2676. *unit = 4;
  2677. return CSS_LEN_AUTO;
  2678. }
  2679. return parse_css_len(s, len, pos, unit);
  2680. }
  2681. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2682. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2683. static int parse_margin_calc(const char *s, unsigned int len,
  2684. unsigned int *pos, int *pct, int *px)
  2685. {
  2686. unsigned int i = *pos, j;
  2687. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2688. return 0;
  2689. i += 5;
  2690. j = i;
  2691. while (j < len && s[j] != ')')
  2692. j++;
  2693. if (j >= len)
  2694. return 0;
  2695. if (!parse_calc(s + i, j - i, pct, px))
  2696. return 0;
  2697. *pos = j + 1;
  2698. return 1;
  2699. }
  2700. /* calc(P% +/- Npx) for the common width/margin forms. */
  2701. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2702. {
  2703. unsigned int i = 0;
  2704. int sign = 1;
  2705. int have = 0;
  2706. *pct = 0;
  2707. *px = 0;
  2708. while (i < len) {
  2709. unsigned int j;
  2710. long whole = 0, frac = 0, scale = 1;
  2711. int digits = 0;
  2712. while (i < len && is_space(s[i]))
  2713. i++;
  2714. if (i >= len)
  2715. break;
  2716. if (s[i] == '+' || s[i] == '-') {
  2717. sign = s[i] == '-' ? -1 : 1;
  2718. i++;
  2719. continue;
  2720. }
  2721. if (s[i] < '0' || s[i] > '9') {
  2722. if (s[i] == '.') {
  2723. /* fall through to the number parser */
  2724. } else {
  2725. i++;
  2726. continue;
  2727. }
  2728. }
  2729. j = i;
  2730. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2731. whole = whole * 10 + (s[j] - '0');
  2732. digits = 1;
  2733. j++;
  2734. }
  2735. if (j < len && s[j] == '.') {
  2736. j++;
  2737. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2738. frac = frac * 10 + (s[j] - '0');
  2739. scale *= 10;
  2740. j++;
  2741. }
  2742. }
  2743. if (!digits) {
  2744. i = j + 1;
  2745. continue;
  2746. }
  2747. while (j < len && is_space(s[j]))
  2748. j++;
  2749. if (j < len && s[j] == '%') {
  2750. *pct = sign * (int)(whole + (scale > 1 ?
  2751. frac * 100 / scale : 0));
  2752. have = 1;
  2753. j++;
  2754. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2755. *px = sign * (int)whole;
  2756. have = 1;
  2757. j += 2;
  2758. }
  2759. i = j;
  2760. while (i < len && !is_space(s[i]))
  2761. i++;
  2762. sign = 1;
  2763. }
  2764. return have;
  2765. }
  2766. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2767. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2768. unsigned char *kind)
  2769. {
  2770. unsigned int i = 0;
  2771. unsigned long v = 0;
  2772. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2773. *val = 0;
  2774. *kind = 0;
  2775. while (i < len && is_space(s[i]))
  2776. i++;
  2777. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2778. return 0;
  2779. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2780. return 0;
  2781. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2782. v = v * 10 + (unsigned int)(s[i] - '0');
  2783. digits = 1;
  2784. i++;
  2785. }
  2786. if (i < len && s[i] == '.') {
  2787. i++;
  2788. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2789. if (frac < 2) {
  2790. v = v * 10 + (unsigned int)(s[i] - '0');
  2791. frac++;
  2792. digits = 1;
  2793. }
  2794. i++;
  2795. }
  2796. }
  2797. while (frac < 2) {
  2798. v *= 10;
  2799. frac++;
  2800. }
  2801. while (frac > 2) {
  2802. v /= 10;
  2803. frac--;
  2804. }
  2805. if (!digits)
  2806. return 0;
  2807. while (i < len && is_space(s[i]))
  2808. i++;
  2809. if (i < len && s[i] == '%')
  2810. pct = 1;
  2811. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2812. s[i + 2] == 'm')
  2813. rem = 1;
  2814. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2815. rem = 2;
  2816. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2817. vw = 1;
  2818. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2819. ; /* px: already the default scale */
  2820. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2821. bad_unit = 1;
  2822. if (bad_unit)
  2823. return 0;
  2824. if (rem == 1 || rem == 2) {
  2825. if (v > 65535)
  2826. v = 65535;
  2827. *val = (unsigned short)v;
  2828. *kind = rem == 1 ? 5 : 4;
  2829. return 1;
  2830. }
  2831. v /= 100;
  2832. if (vw)
  2833. v = v * viewport_w / 100;
  2834. if (pct) {
  2835. if (v > 100)
  2836. v = 100;
  2837. *kind = 2;
  2838. } else {
  2839. if (v > 2048)
  2840. v = 2048;
  2841. *kind = 1;
  2842. }
  2843. *val = (unsigned short)v;
  2844. return 1;
  2845. }
  2846. static int parse_font_px(const char *s, unsigned int len)
  2847. {
  2848. unsigned int i = 0;
  2849. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2850. int v1000, px;
  2851. int is_px = 1, is_rem = 0, is_pct = 0;
  2852. while (i < len && is_space(s[i]))
  2853. i++;
  2854. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2855. return 24;
  2856. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2857. return 32;
  2858. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2859. return 14;
  2860. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2861. whole = whole * 10 + (s[i] - '0');
  2862. digits = 1;
  2863. i++;
  2864. }
  2865. if (i < len && s[i] == '.') {
  2866. i++;
  2867. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2868. if (fscale < 1000) {
  2869. frac = frac * 10 + (s[i] - '0');
  2870. fscale *= 10;
  2871. }
  2872. digits = 1;
  2873. i++;
  2874. }
  2875. }
  2876. if (!digits)
  2877. return 0;
  2878. while (i < len && is_space(s[i]))
  2879. i++;
  2880. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2881. is_rem = 1;
  2882. is_px = 0;
  2883. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2884. is_rem = 1;
  2885. is_px = 0;
  2886. } else if (i < len && s[i] == '%') {
  2887. is_pct = 1;
  2888. is_px = 0;
  2889. }
  2890. v1000 = whole * 1000 + frac * 1000 / fscale;
  2891. if (is_rem)
  2892. px = v1000 * root_font_px / 1000;
  2893. else if (is_pct)
  2894. px = v1000 * root_font_px / 100000;
  2895. else if (is_px)
  2896. px = v1000 / 1000;
  2897. else
  2898. px = v1000 / 1000;
  2899. if (px < 8)
  2900. px = 8;
  2901. if (px > 72)
  2902. px = 72;
  2903. return px;
  2904. }
  2905. /* font-size keeping the unit so em/% resolve against the parent face. */
  2906. static int parse_font_size(const char *s, unsigned int len,
  2907. unsigned short *raw, unsigned char *unit)
  2908. {
  2909. unsigned int i = 0;
  2910. int whole = 0, frac = 0, scale = 1, digits = 0;
  2911. int v100;
  2912. *raw = 0;
  2913. *unit = 0;
  2914. while (i < len && is_space(s[i]))
  2915. i++;
  2916. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2917. *raw = 24;
  2918. *unit = 1;
  2919. return 1;
  2920. }
  2921. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2922. *raw = 32;
  2923. *unit = 1;
  2924. return 1;
  2925. }
  2926. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2927. *raw = 14;
  2928. *unit = 1;
  2929. return 1;
  2930. }
  2931. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2932. whole = whole * 10 + (s[i] - '0');
  2933. digits = 1;
  2934. i++;
  2935. }
  2936. if (i < len && s[i] == '.') {
  2937. i++;
  2938. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2939. frac = frac * 10 + (s[i] - '0');
  2940. scale *= 10;
  2941. i++;
  2942. }
  2943. }
  2944. if (!digits && !frac)
  2945. return 0;
  2946. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2947. while (i < len && is_space(s[i]))
  2948. i++;
  2949. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2950. *raw = (unsigned short)v100;
  2951. *unit = 3;
  2952. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2953. *raw = (unsigned short)v100;
  2954. *unit = 2;
  2955. } else if (i < len && s[i] == '%') {
  2956. /* Keep hundredths: 62.5% is a common root size and truncating
  2957. * it to 62% makes every rem 1px too small. */
  2958. *raw = (unsigned short)v100;
  2959. *unit = 4;
  2960. } else {
  2961. *raw = (unsigned short)(v100 / 100);
  2962. *unit = 1;
  2963. }
  2964. return 1;
  2965. }
  2966. static int parse_line_height(const char *s, unsigned int len,
  2967. unsigned short *px, unsigned char *pct)
  2968. {
  2969. unsigned int i = 0;
  2970. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2971. int v1000;
  2972. int is_px = 0, is_pct = 0;
  2973. *px = 0;
  2974. *pct = 0;
  2975. while (i < len && is_space(s[i]))
  2976. i++;
  2977. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2978. return 0;
  2979. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2980. whole = whole * 10 + (s[i] - '0');
  2981. digits = 1;
  2982. i++;
  2983. }
  2984. if (i < len && s[i] == '.') {
  2985. i++;
  2986. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2987. frac = frac * 10 + (s[i] - '0');
  2988. fscale *= 10;
  2989. digits = 1;
  2990. i++;
  2991. }
  2992. }
  2993. if (!digits)
  2994. return 0;
  2995. while (i < len && is_space(s[i]))
  2996. i++;
  2997. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2998. is_px = 1;
  2999. else if (i < len && s[i] == '%')
  3000. is_pct = 1;
  3001. v1000 = whole * 1000 + frac * 1000 / fscale;
  3002. /* rem is an absolute length (the root font is known by the time
  3003. * rules are parsed, as for font-size); em behaves like a multiplier. */
  3004. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3005. v1000 = v1000 * root_font_px;
  3006. is_px = 1;
  3007. }
  3008. if (is_px) {
  3009. int v = v1000 / 1000;
  3010. if (v < 8)
  3011. v = 8;
  3012. if (v > 160)
  3013. v = 160;
  3014. *px = (unsigned short)v;
  3015. } else {
  3016. int v = v1000 / 10; /* unitless multiplier -> percent */
  3017. if (is_pct)
  3018. v = v1000 / 1000;
  3019. if (v < 50)
  3020. v = 50;
  3021. if (v > 255) /* stored in an unsigned char */
  3022. v = 255;
  3023. *pct = (unsigned char)v;
  3024. }
  3025. return 1;
  3026. }
  3027. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  3028. {
  3029. unsigned int i, j, n = s_len(needle);
  3030. for (i = 0; i + n <= len; i++) {
  3031. for (j = 0; j < n; j++) {
  3032. char a = s[i + j];
  3033. if (a >= 'A' && a <= 'Z')
  3034. a = (char)(a - 'A' + 'a');
  3035. if (a != needle[j])
  3036. break;
  3037. }
  3038. if (j == n)
  3039. return 1;
  3040. }
  3041. return 0;
  3042. }
  3043. /* Pull a colour out of a border/background shorthand. */
  3044. /* Background shorthands can hold several layers before the final solid
  3045. * colour. The colour is conventionally the last token (e.g. a stack of
  3046. * gradients followed by var(--bg)), so take the rightmost colour rather
  3047. * than the first gradient stop. */
  3048. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3049. {
  3050. int last = -1;
  3051. unsigned int i;
  3052. for (i = 0; i < len; i++) {
  3053. if (s[i] == '#')
  3054. last = (int)i;
  3055. else if ((s[i] == 'r' || s[i] == 'R') &&
  3056. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3057. last = (int)i;
  3058. }
  3059. if (last >= 0)
  3060. return parse_color(s + last, len - (unsigned int)last, c);
  3061. while (i > 0) {
  3062. unsigned int st, e;
  3063. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3064. s[i - 1] == ')' || s[i - 1] == '('))
  3065. i--;
  3066. e = i;
  3067. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3068. s[i - 1] != ')' && s[i - 1] != '(')
  3069. i--;
  3070. st = i;
  3071. if (e > st && parse_color(s + st, e - st, c))
  3072. return 1;
  3073. }
  3074. return 0;
  3075. }
  3076. static int parse_display(const char *s, unsigned int len)
  3077. {
  3078. unsigned int i;
  3079. while (len && is_space(s[0])) { s++; len--; }
  3080. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3081. ;
  3082. if (i == 4 && ci_eq_n(s, "none", 4))
  3083. return BR_DISPLAY_NONE;
  3084. if (i == 5 && ci_eq_n(s, "block", 5))
  3085. return BR_DISPLAY_BLOCK;
  3086. if (i == 6 && ci_eq_n(s, "inline", 6))
  3087. return BR_DISPLAY_INLINE;
  3088. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3089. return BR_DISPLAY_INLINE_BLOCK;
  3090. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3091. return BR_DISPLAY_BLOCK;
  3092. if (i == 4 && ci_eq_n(s, "flex", 4))
  3093. return BR_DISPLAY_FLEX;
  3094. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3095. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3096. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3097. * block instead of an inline box. */
  3098. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3099. return BR_DISPLAY_BLOCK;
  3100. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3101. return BR_DISPLAY_BLOCK;
  3102. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3103. return BR_DISPLAY_FLEX;
  3104. if (i == 5 && ci_eq_n(s, "table", 5))
  3105. return BR_DISPLAY_BLOCK;
  3106. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3107. return BR_DISPLAY_BLOCK;
  3108. /* Grid shells are common on news/wiki pages, but the two-column
  3109. * heuristic is not reliable for their nested area templates yet.
  3110. * Collapse them to block flow so the content stays readable. */
  3111. if (i == 4 && ci_eq_n(s, "grid", 4))
  3112. return BR_DISPLAY_BLOCK;
  3113. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3114. return BR_DISPLAY_BLOCK;
  3115. return BR_DISPLAY_INLINE;
  3116. }
  3117. static void decl_add(struct br_decl *out, unsigned int *nout,
  3118. unsigned char prop, int val, int important)
  3119. {
  3120. if (*nout >= BR_DECLS)
  3121. return;
  3122. out[*nout].prop = prop;
  3123. out[*nout].val = (short)val;
  3124. out[*nout].important = (unsigned char)(important != 0);
  3125. (*nout)++;
  3126. }
  3127. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3128. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3129. unsigned int *start, unsigned int *end)
  3130. {
  3131. unsigned int i = *pos;
  3132. while (i < len && is_space(s[i]))
  3133. i++;
  3134. if (i >= len)
  3135. return 0;
  3136. *start = i;
  3137. while (i < len && !is_space(s[i]))
  3138. i++;
  3139. *end = i;
  3140. *pos = i;
  3141. return 1;
  3142. }
  3143. static int bg_number(const char *s, unsigned int len, signed short *val,
  3144. unsigned char *kind)
  3145. {
  3146. unsigned int i = 0;
  3147. long v = 0;
  3148. int neg = 0, digits = 0, pct = 0;
  3149. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3150. neg = s[i] == '-';
  3151. i++;
  3152. }
  3153. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3154. v = v * 10 + (s[i] - '0');
  3155. digits = 1;
  3156. i++;
  3157. }
  3158. if (i < len && s[i] == '.') {
  3159. i++;
  3160. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3161. digits = 1;
  3162. i++;
  3163. }
  3164. }
  3165. if (!digits)
  3166. return 0;
  3167. if (i < len && s[i] == '%') {
  3168. pct = 1;
  3169. i++;
  3170. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3171. i += 2;
  3172. }
  3173. if (i != len)
  3174. return 0;
  3175. if (neg)
  3176. v = -v;
  3177. if (v < -4096)
  3178. v = -4096;
  3179. if (v > 4096)
  3180. v = 4096;
  3181. *val = (signed short)v;
  3182. *kind = (unsigned char)(pct ? 1 : 0);
  3183. return 1;
  3184. }
  3185. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3186. int *vertical)
  3187. {
  3188. *vertical = 0;
  3189. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3190. *val = 0;
  3191. return 1;
  3192. }
  3193. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3194. *val = 100;
  3195. return 1;
  3196. }
  3197. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3198. *val = 0;
  3199. *vertical = 1;
  3200. return 1;
  3201. }
  3202. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3203. *val = 100;
  3204. *vertical = 1;
  3205. return 1;
  3206. }
  3207. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3208. *val = 50;
  3209. return 1;
  3210. }
  3211. return 0;
  3212. }
  3213. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3214. const char *s, unsigned int len, int important)
  3215. {
  3216. unsigned int pos = 0, ts, te;
  3217. signed short val[2] = { 50, 50 };
  3218. unsigned char kind[2] = { 1, 1 };
  3219. int vert[2] = { 0, 0 }, n = 0;
  3220. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3221. signed short v;
  3222. int is_vert;
  3223. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3224. val[n] = v;
  3225. kind[n] = 1;
  3226. vert[n] = is_vert;
  3227. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3228. val[n] = v;
  3229. vert[n] = 0;
  3230. } else {
  3231. n++;
  3232. continue;
  3233. }
  3234. n++;
  3235. }
  3236. if (n == 1) {
  3237. if (vert[0]) {
  3238. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3239. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3240. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3241. important);
  3242. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3243. } else {
  3244. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3245. important);
  3246. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3247. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3248. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3249. }
  3250. return;
  3251. }
  3252. if (n >= 2) {
  3253. int x = vert[0] && !vert[1] ? 1 : 0;
  3254. int y = 1 - x;
  3255. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3256. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3257. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3258. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3259. }
  3260. }
  3261. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3262. const char *s, unsigned int len, int important)
  3263. {
  3264. unsigned int pos = 0, ts, te;
  3265. signed short w = 0, h = 0;
  3266. unsigned char kw = 0, kh = 0;
  3267. int n = 0;
  3268. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3269. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3270. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3271. return;
  3272. }
  3273. if (n == 0 && te - ts == 7 &&
  3274. ci_eq_n(s + ts, "contain", 7)) {
  3275. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3276. return;
  3277. }
  3278. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3279. n++;
  3280. continue;
  3281. }
  3282. {
  3283. signed short v;
  3284. unsigned char k;
  3285. if (bg_number(s + ts, te - ts, &v, &k)) {
  3286. if (n == 0) {
  3287. w = v;
  3288. kw = k;
  3289. } else {
  3290. h = v;
  3291. kh = k;
  3292. }
  3293. }
  3294. }
  3295. n++;
  3296. }
  3297. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3298. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3299. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3300. (void)kh;
  3301. }
  3302. /* Parse the `position / size` part of the background shorthand. The image
  3303. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3304. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3305. const char *v, unsigned int len,
  3306. int important)
  3307. {
  3308. unsigned int i, slash = 0, depth = 0, p, first, count;
  3309. char q = 0;
  3310. signed short nv;
  3311. int vert;
  3312. unsigned char kind;
  3313. for (i = 0; i < len; i++) {
  3314. char c = v[i];
  3315. if (q) {
  3316. if (c == q)
  3317. q = 0;
  3318. continue;
  3319. }
  3320. if (c == '\'' || c == '"') {
  3321. q = c;
  3322. continue;
  3323. }
  3324. if (c == '(') {
  3325. depth++;
  3326. continue;
  3327. }
  3328. if (c == ')') {
  3329. if (depth)
  3330. depth--;
  3331. continue;
  3332. }
  3333. if (c == '/' && !depth) {
  3334. slash = i;
  3335. break;
  3336. }
  3337. }
  3338. if (!slash)
  3339. return;
  3340. /* Up to two position tokens immediately before the slash. */
  3341. p = slash;
  3342. first = slash;
  3343. count = 0;
  3344. while (count < 2) {
  3345. unsigned int e;
  3346. while (p > 0 && is_space(v[p - 1]))
  3347. p--;
  3348. e = p;
  3349. while (p > 0 && !is_space(v[p - 1]))
  3350. p--;
  3351. if (e == p)
  3352. break;
  3353. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3354. bg_number(v + p, e - p, &nv, &kind)) {
  3355. first = p;
  3356. count++;
  3357. } else {
  3358. break;
  3359. }
  3360. }
  3361. if (count)
  3362. add_bg_position(out, nout, v + first, slash - first,
  3363. important);
  3364. /* Up to two size tokens after the slash. */
  3365. p = slash + 1;
  3366. while (p < len && is_space(v[p]))
  3367. p++;
  3368. first = p;
  3369. count = 0;
  3370. while (count < 2) {
  3371. unsigned int ts = p;
  3372. while (p < len && !is_space(v[p]))
  3373. p++;
  3374. if (p == ts)
  3375. break;
  3376. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3377. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3378. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3379. bg_number(v + ts, p - ts, &nv, &kind)) {
  3380. count++;
  3381. while (p < len && is_space(v[p]))
  3382. p++;
  3383. } else {
  3384. break;
  3385. }
  3386. }
  3387. if (count)
  3388. add_bg_size(out, nout, v + first, p - first, important);
  3389. }
  3390. static int parse_fixed_100(const char *s, unsigned int len,
  3391. unsigned int *pos, signed short *out)
  3392. {
  3393. unsigned int i = *pos;
  3394. long whole = 0, frac = 0;
  3395. int neg = 0, digits = 0, fd = 0;
  3396. while (i < len && is_space(s[i]))
  3397. i++;
  3398. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3399. neg = s[i] == '-';
  3400. i++;
  3401. }
  3402. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3403. whole = whole * 10 + (s[i] - '0');
  3404. if (whole > 327)
  3405. whole = 327;
  3406. digits = 1;
  3407. i++;
  3408. }
  3409. if (i < len && s[i] == '.') {
  3410. i++;
  3411. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3412. if (fd < 2) {
  3413. frac = frac * 10 + (s[i] - '0');
  3414. fd++;
  3415. digits = 1;
  3416. }
  3417. i++;
  3418. }
  3419. }
  3420. if (!digits)
  3421. return 0;
  3422. while (fd++ < 2)
  3423. frac *= 10;
  3424. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3425. *pos = i;
  3426. return 1;
  3427. }
  3428. static int find_fn(const char *v, unsigned int len, const char *name,
  3429. unsigned int *arg)
  3430. {
  3431. unsigned int n = s_len(name), i;
  3432. for (i = 0; i + n <= len; i++) {
  3433. unsigned int j;
  3434. if (!ci_eq_n(v + i, name, n))
  3435. continue;
  3436. j = i + n;
  3437. while (j < len && is_space(v[j]))
  3438. j++;
  3439. if (j < len && v[j] == '(') {
  3440. *arg = j + 1;
  3441. return 1;
  3442. }
  3443. }
  3444. return 0;
  3445. }
  3446. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3447. const char *s, unsigned int len,
  3448. int important)
  3449. {
  3450. unsigned int pos = 0, ts, te;
  3451. signed short val[2] = { 50, 50 };
  3452. unsigned char kind[2] = { 1, 1 };
  3453. int vert[2] = { 0, 0 }, n = 0;
  3454. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3455. signed short v;
  3456. int is_vert;
  3457. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3458. val[n] = v;
  3459. kind[n] = 1;
  3460. vert[n] = is_vert;
  3461. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3462. val[n] = v;
  3463. vert[n] = 0;
  3464. } else {
  3465. n++;
  3466. continue;
  3467. }
  3468. n++;
  3469. }
  3470. if (n == 1) {
  3471. if (vert[0]) {
  3472. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3473. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3474. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3475. important);
  3476. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3477. } else {
  3478. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3479. important);
  3480. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3481. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3482. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3483. }
  3484. return;
  3485. }
  3486. if (n >= 2) {
  3487. int x = vert[0] && !vert[1] ? 1 : 0;
  3488. int y = 1 - x;
  3489. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3490. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3491. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3492. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3493. }
  3494. }
  3495. /* Copy a background value with every url(...) body blanked (kept as
  3496. * `url()`), for the keyword and colour scans of the shorthand. */
  3497. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3498. unsigned int cap)
  3499. {
  3500. unsigned int i = 0, n = 0;
  3501. while (i < len && n + 1 < cap) {
  3502. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3503. char q = 0;
  3504. memcpy(out + n, "url(", 4);
  3505. n += 4;
  3506. i += 4;
  3507. while (i < len) {
  3508. if (q) {
  3509. if (s[i] == '\\' && i + 1 < len)
  3510. i++;
  3511. else if (s[i] == q)
  3512. q = 0;
  3513. } else if (s[i] == '"' || s[i] == '\'') {
  3514. q = s[i];
  3515. } else if (s[i] == ')') {
  3516. break;
  3517. }
  3518. i++;
  3519. }
  3520. continue; /* the ')' is copied below */
  3521. }
  3522. out[n++] = s[i++];
  3523. }
  3524. out[n] = '\0';
  3525. return n;
  3526. }
  3527. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3528. const char *v, unsigned int vl, int important)
  3529. {
  3530. unsigned int i;
  3531. for (i = 0; i + 4 <= vl; i++) {
  3532. if (ci_eq_n(v + i, "url(", 4)) {
  3533. unsigned int st = i + 4, en = st;
  3534. char q = 0;
  3535. int idx;
  3536. /* `)` inside a quoted data: URL (an SVG's
  3537. * rgba(...)) does not end it. */
  3538. while (en < vl) {
  3539. if (q) {
  3540. if (v[en] == '\\' && en + 1 < vl)
  3541. en++;
  3542. else if (v[en] == q)
  3543. q = 0;
  3544. } else if (v[en] == '"' || v[en] == '\'') {
  3545. q = v[en];
  3546. } else if (v[en] == ')') {
  3547. break;
  3548. }
  3549. en++;
  3550. }
  3551. idx = register_bg_url(v + st, en - st);
  3552. if (idx)
  3553. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3554. important);
  3555. return;
  3556. }
  3557. }
  3558. }
  3559. static int parse_len_token(const char *s, unsigned int len, int *out)
  3560. {
  3561. unsigned int pos = 0;
  3562. int v;
  3563. if (!len)
  3564. return 0;
  3565. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3566. return 0;
  3567. v = parse_len(s, len, &pos);
  3568. if (pos == 0)
  3569. return 0;
  3570. *out = v;
  3571. return 1;
  3572. }
  3573. static void expand_four(int v[4], int n)
  3574. {
  3575. if (n == 1) {
  3576. v[1] = v[2] = v[3] = v[0];
  3577. } else if (n == 2) {
  3578. v[2] = v[0];
  3579. v[3] = v[1];
  3580. } else if (n == 3) {
  3581. v[3] = v[1];
  3582. }
  3583. }
  3584. /* Custom properties defined in the declaration block being expanded. A
  3585. * component that sets a variable and uses it in the same rule (Infima's
  3586. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3587. * var(--ifm-col-width) }`) means its own value: the element matching the
  3588. * rule gets it. The document-wide table cannot know that, because
  3589. * `.col` and `.col--N` give the name a dozen different values. */
  3590. static int css_env_active; /* css_local holds an element's variables */
  3591. #define CSS_LOCAL_VARS 48
  3592. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3593. unsigned int i);
  3594. static struct {
  3595. const char *name, *val;
  3596. unsigned short nlen, vlen;
  3597. } css_local[CSS_LOCAL_VARS];
  3598. static unsigned int css_nlocal;
  3599. static int css_local_find(const char *name, unsigned int nlen)
  3600. {
  3601. unsigned int k;
  3602. for (k = 0; k < css_nlocal; k++)
  3603. if (css_local[k].nlen == nlen &&
  3604. !memcmp(css_local[k].name, name, nlen))
  3605. return (int)k;
  3606. return -1;
  3607. }
  3608. static int css_local_find_before(const char *name, unsigned int nlen,
  3609. unsigned int end)
  3610. {
  3611. unsigned int k;
  3612. for (k = 0; k < end; k++)
  3613. if (css_local[k].nlen == nlen &&
  3614. !memcmp(css_local[k].name, name, nlen))
  3615. return (int)k;
  3616. return -1;
  3617. }
  3618. static void css_local_append(const char *s, unsigned int len);
  3619. /* Collect the top-level `--name: value` declarations of a block. */
  3620. static void css_local_collect(const char *s, unsigned int len)
  3621. {
  3622. css_nlocal = 0;
  3623. css_local_append(s, len);
  3624. }
  3625. static void css_local_append(const char *s, unsigned int len)
  3626. {
  3627. unsigned int i = 0;
  3628. while (i < len) {
  3629. unsigned int ns, ne, vs, ve;
  3630. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3631. i++;
  3632. ns = i;
  3633. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3634. i++;
  3635. if (i >= len || s[i] != ':') {
  3636. if (i < len && s[i] == '{')
  3637. i = css_skip_nested(s, len, i + 1);
  3638. continue;
  3639. }
  3640. ne = i;
  3641. while (ne > ns && is_space(s[ne - 1]))
  3642. ne--;
  3643. vs = i + 1;
  3644. ve = css_value_end(s, len, vs);
  3645. i = ve;
  3646. while (vs < ve && is_space(s[vs]))
  3647. vs++;
  3648. while (ve > vs && is_space(s[ve - 1]))
  3649. ve--;
  3650. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3651. ve -= 10;
  3652. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3653. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3654. continue;
  3655. css_local[css_nlocal].name = s + ns;
  3656. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3657. css_local[css_nlocal].val = s + vs;
  3658. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3659. css_nlocal++;
  3660. }
  3661. }
  3662. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3663. * the input is copied verbatim when it contains no var(). */
  3664. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3665. unsigned int len, char *out,
  3666. unsigned int cap, int depth)
  3667. {
  3668. unsigned int i = 0, n = 0;
  3669. if (depth > 8)
  3670. return 0;
  3671. while (i < len && n + 1 < cap) {
  3672. unsigned int p, inner, close, comma, nlen, fstart;
  3673. char quote;
  3674. int parens, glue;
  3675. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3676. out[n++] = s[i++];
  3677. continue;
  3678. }
  3679. p = i + 4;
  3680. quote = 0;
  3681. parens = 1;
  3682. while (p < len && parens) {
  3683. char c = s[p];
  3684. if (quote) {
  3685. if (c == quote)
  3686. quote = 0;
  3687. else if (c == '\\' && p + 1 < len)
  3688. p++;
  3689. } else if (c == '"' || c == '\'') {
  3690. quote = c;
  3691. } else if (c == '(') {
  3692. parens++;
  3693. } else if (c == ')') {
  3694. parens--;
  3695. }
  3696. p++;
  3697. }
  3698. if (parens) {
  3699. out[n++] = s[i++];
  3700. continue;
  3701. }
  3702. close = p - 1;
  3703. inner = i + 4;
  3704. /* var() substitutes tokens: minified Infima writes
  3705. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3706. * must not become the one token `700.875rem`. Separate the
  3707. * value from a glued neighbour on both sides. */
  3708. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3709. out[n - 1] != '(' && out[n - 1] != ',' &&
  3710. out[n - 1] != ':' && out[n - 1] != '/')
  3711. out[n++] = ' ';
  3712. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3713. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3714. s[p] != '!' && s[p] != '}';
  3715. while (inner < close && is_space(s[inner]))
  3716. inner++;
  3717. comma = inner;
  3718. parens = 0;
  3719. quote = 0;
  3720. while (comma < close) {
  3721. char c = s[comma];
  3722. if (quote) {
  3723. if (c == quote)
  3724. quote = 0;
  3725. } else if (c == '"' || c == '\'') {
  3726. quote = c;
  3727. } else if (c == '(') {
  3728. parens++;
  3729. } else if (c == ')' && parens > 0) {
  3730. parens--;
  3731. } else if (c == ',' && parens == 0) {
  3732. break;
  3733. }
  3734. comma++;
  3735. }
  3736. nlen = comma - inner;
  3737. while (nlen && is_space(s[inner + nlen - 1]))
  3738. nlen--;
  3739. fstart = comma < close ? comma + 1 : close;
  3740. while (fstart < close && is_space(s[fstart]))
  3741. fstart++;
  3742. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3743. unsigned int vi = css_var_find(d, s + inner, nlen);
  3744. int li = css_local_find(s + inner, nlen);
  3745. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3746. s + close + 1) {
  3747. /* (not the definition's own value) */
  3748. n += css_expand_vars(d, css_local[li].val,
  3749. css_local[li].vlen,
  3750. out + n, cap - n,
  3751. depth + 1);
  3752. i = p;
  3753. if (glue && n + 1 < cap)
  3754. out[n++] = ' ';
  3755. continue;
  3756. }
  3757. if (vi < BR_MAX_VARS &&
  3758. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3759. vi = BR_MAX_VARS;
  3760. if (vi < BR_MAX_VARS) {
  3761. const char *v = attr_ptr(d, d->var_val[vi]);
  3762. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3763. out + n,
  3764. cap - n, depth + 1);
  3765. n += vn;
  3766. } else if (fstart < close) {
  3767. unsigned int vn = css_expand_vars(d, s + fstart,
  3768. close - fstart,
  3769. out + n, cap - n,
  3770. depth + 1);
  3771. n += vn;
  3772. }
  3773. } else if (fstart < close) {
  3774. unsigned int vn = css_expand_vars(d, s + fstart,
  3775. close - fstart,
  3776. out + n, cap - n,
  3777. depth + 1);
  3778. n += vn;
  3779. }
  3780. i = p;
  3781. if (glue && n + 1 < cap)
  3782. out[n++] = ' ';
  3783. }
  3784. out[n] = '\0';
  3785. return n;
  3786. }
  3787. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3788. unsigned int i)
  3789. {
  3790. while (i < len && (is_space(s[i]) || s[i] == ','))
  3791. i++;
  3792. return i;
  3793. }
  3794. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3795. * argument). Returns 1 on success. */
  3796. static int parse_grid_track(const char *s, unsigned int len,
  3797. unsigned int *pp, unsigned short *val,
  3798. unsigned char *kind)
  3799. {
  3800. unsigned int i = grid_skip_sep(s, len, *pp);
  3801. unsigned int whole = 0, frac = 0, scale = 1;
  3802. int any = 0;
  3803. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3804. unsigned int j = i + 7;
  3805. while (j < len && s[j] != ',')
  3806. j++;
  3807. if (j >= len)
  3808. return 0;
  3809. i = j + 1;
  3810. while (i < len && is_space(s[i]))
  3811. i++;
  3812. }
  3813. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3814. int depth = 1;
  3815. i += 7;
  3816. while (i < len && depth) {
  3817. if (s[i] == '(')
  3818. depth++;
  3819. else if (s[i] == ')')
  3820. depth--;
  3821. i++;
  3822. }
  3823. *pp = i;
  3824. return 0;
  3825. }
  3826. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3827. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3828. i++;
  3829. any = 1;
  3830. }
  3831. if (i < len && s[i] == '.') {
  3832. i++;
  3833. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3834. if (scale < 1000) {
  3835. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3836. scale *= 10;
  3837. }
  3838. i++;
  3839. any = 1;
  3840. }
  3841. }
  3842. if (!any) {
  3843. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3844. *val = 1;
  3845. *kind = 3;
  3846. *pp = i + 4;
  3847. return 1;
  3848. }
  3849. return 0;
  3850. }
  3851. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3852. *val = (unsigned short)(whole ? whole : 1);
  3853. *kind = 3;
  3854. i += 2;
  3855. } else if (i < len && s[i] == '%') {
  3856. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3857. *kind = 2;
  3858. i++;
  3859. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3860. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3861. *kind = 1;
  3862. i += 3;
  3863. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3864. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3865. *kind = 1;
  3866. i += 2;
  3867. } else {
  3868. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3869. *kind = 1;
  3870. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3871. i += 2;
  3872. }
  3873. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3874. i++;
  3875. *pp = i;
  3876. return 1;
  3877. }
  3878. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3879. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3880. * keep the two-column path). */
  3881. static int grid_repeat_count(const char *s, unsigned int len)
  3882. {
  3883. unsigned int i = 0;
  3884. int n = 0;
  3885. while (i < len && is_space(s[i]))
  3886. i++;
  3887. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3888. i += 7;
  3889. while (i < len && is_space(s[i]))
  3890. i++;
  3891. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3892. n = n * 10 + (s[i] - '0');
  3893. i++;
  3894. }
  3895. if (n < 1 || n > 12)
  3896. return 0;
  3897. while (i < len && (is_space(s[i]) || s[i] == ','))
  3898. i++;
  3899. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3900. return n;
  3901. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3902. i += 7;
  3903. while (i < len && is_space(s[i]))
  3904. i++;
  3905. if (i < len && s[i] == '0') {
  3906. i++;
  3907. while (i < len && (is_space(s[i]) ||
  3908. s[i] == ',' || s[i] == 'p' ||
  3909. s[i] == 'x'))
  3910. i++;
  3911. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3912. return n;
  3913. }
  3914. }
  3915. return 0;
  3916. }
  3917. /* "1fr 1fr 1fr" */
  3918. for (;;) {
  3919. while (i < len && is_space(s[i]))
  3920. i++;
  3921. if (i >= len)
  3922. break;
  3923. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3924. (i + 3 == len || is_space(s[i + 3]))) {
  3925. n++;
  3926. i += 3;
  3927. } else {
  3928. return 0;
  3929. }
  3930. }
  3931. return n >= 3 ? n : 0;
  3932. }
  3933. static int parse_grid_cols(const char *s, unsigned int len,
  3934. unsigned short val[2], unsigned char kind[2])
  3935. {
  3936. unsigned int pos = 0;
  3937. int n = 0;
  3938. while (n < 2) {
  3939. unsigned int before = pos;
  3940. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3941. break;
  3942. n++;
  3943. if (pos <= before)
  3944. break;
  3945. while (pos < len && is_space(s[pos]))
  3946. pos++;
  3947. if (pos >= len)
  3948. break;
  3949. }
  3950. return n;
  3951. }
  3952. static int parse_grid_slot(const char *s, unsigned int len)
  3953. {
  3954. if (contains_ci_len(s, len, "footer") ||
  3955. contains_ci_len(s, len, "header") ||
  3956. contains_ci_len(s, len, "sitenotice") ||
  3957. contains_ci_len(s, len, "navbar"))
  3958. return 1;
  3959. if (contains_ci_len(s, len, "columnstart") ||
  3960. contains_ci_len(s, len, "sidebar") ||
  3961. contains_ci_len(s, len, "left") ||
  3962. contains_ci_len(s, len, "start"))
  3963. return 2;
  3964. if (contains_ci_len(s, len, "pagecontent") ||
  3965. contains_ci_len(s, len, "content") ||
  3966. contains_ci_len(s, len, "main") ||
  3967. contains_ci_len(s, len, "right") ||
  3968. contains_ci_len(s, len, "end"))
  3969. return 3;
  3970. return 0;
  3971. }
  3972. static int css_has_var(const char *s, unsigned int len)
  3973. {
  3974. unsigned int i;
  3975. for (i = 0; i + 4 <= len; i++)
  3976. if ((s[i] == 'v' || s[i] == 'V') &&
  3977. ci_eq_n(s + i, "var(", 4))
  3978. return 1;
  3979. return 0;
  3980. }
  3981. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3982. unsigned int i)
  3983. {
  3984. i += 2;
  3985. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3986. i++;
  3987. if (i + 1 < len)
  3988. i += 2;
  3989. else
  3990. i = len;
  3991. return i;
  3992. }
  3993. static unsigned int css_skip_string(const char *s, unsigned int len,
  3994. unsigned int i)
  3995. {
  3996. char q = s[i++];
  3997. while (i < len && s[i] != q) {
  3998. if (s[i] == '\\' && i + 1 < len)
  3999. i++;
  4000. i++;
  4001. }
  4002. if (i < len)
  4003. i++;
  4004. return i;
  4005. }
  4006. /* Find the '}' that closes a declaration block. i points just past the
  4007. * opening '{'. Comments and quoted strings are skipped: a brace inside
  4008. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  4009. * selectors such as "Highlight-Stories {" without a closing brace). */
  4010. static unsigned int css_block_end(const char *s, unsigned int len,
  4011. unsigned int i)
  4012. {
  4013. int depth = 0;
  4014. while (i < len) {
  4015. char c = s[i];
  4016. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4017. i = css_skip_comment(s, len, i);
  4018. continue;
  4019. }
  4020. if (c == '"' || c == '\'') {
  4021. i = css_skip_string(s, len, i);
  4022. continue;
  4023. }
  4024. if (c == '{') {
  4025. depth++;
  4026. } else if (c == '}') {
  4027. if (depth == 0)
  4028. return i;
  4029. depth--;
  4030. }
  4031. i++;
  4032. }
  4033. return len;
  4034. }
  4035. /* Skip a block whose opening '{' has already been consumed. */
  4036. static unsigned int css_skip_nested(const char *s, unsigned int len,
  4037. unsigned int i)
  4038. {
  4039. int depth = 1;
  4040. while (i < len && depth) {
  4041. char c = s[i];
  4042. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4043. i = css_skip_comment(s, len, i);
  4044. continue;
  4045. }
  4046. if (c == '"' || c == '\'') {
  4047. i = css_skip_string(s, len, i);
  4048. continue;
  4049. }
  4050. if (c == '{')
  4051. depth++;
  4052. else if (c == '}')
  4053. depth--;
  4054. i++;
  4055. }
  4056. return i;
  4057. }
  4058. /* ---- @font-face ------------------------------------------------------- */
  4059. static void font_trim(const char **sp, unsigned int *len)
  4060. {
  4061. const char *s = *sp;
  4062. unsigned int n = *len;
  4063. while (n && is_space(*s)) {
  4064. s++;
  4065. n--;
  4066. }
  4067. while (n && is_space(s[n - 1]))
  4068. n--;
  4069. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4070. s++;
  4071. n -= 2;
  4072. }
  4073. *sp = s;
  4074. *len = n;
  4075. }
  4076. static int font_name_eq(const char *a, const char *b, unsigned int blen)
  4077. {
  4078. unsigned int k;
  4079. if (s_len(a) != blen)
  4080. return 0;
  4081. for (k = 0; k < blen; k++) {
  4082. char x = a[k], y = b[k];
  4083. if (x >= 'A' && x <= 'Z')
  4084. x = (char)(x - 'A' + 'a');
  4085. if (y >= 'A' && y <= 'Z')
  4086. y = (char)(y - 'A' + 'a');
  4087. if (x != y)
  4088. return 0;
  4089. }
  4090. return 1;
  4091. }
  4092. static void font_copy(char *dst, unsigned int cap, const char *src,
  4093. unsigned int n)
  4094. {
  4095. unsigned int k;
  4096. for (k = 0; k < n && src[k] && k + 1 < cap; k++)
  4097. dst[k] = src[k];
  4098. dst[k] = '\0';
  4099. }
  4100. /* Register one @font-face. Every (family, weight range, style) is its own
  4101. * entry; a later rule with the same triple replaces the URL, as the CSS
  4102. * font-face collection does. family_id is the 1-based id of the family's
  4103. * first entry, which is what font-family declarations store. */
  4104. static void font_add(const char *family, unsigned int flen,
  4105. const char *url, unsigned int ulen,
  4106. unsigned int wmin, unsigned int wmax, int italic)
  4107. {
  4108. unsigned int i;
  4109. int fam = 0;
  4110. struct br_font_def *f;
  4111. font_trim(&family, &flen);
  4112. font_trim(&url, &ulen);
  4113. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4114. return;
  4115. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4116. f = &font_defs[i];
  4117. if (!font_name_eq(f->family, family, flen))
  4118. continue;
  4119. if (!fam)
  4120. fam = f->family_id;
  4121. if (f->wmin != wmin || f->wmax != wmax ||
  4122. f->italic != (unsigned char)italic)
  4123. continue;
  4124. font_copy(f->url, sizeof(f->url), url, ulen);
  4125. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4126. return;
  4127. }
  4128. if (font_face_count >= BR_MAX_FACES)
  4129. return;
  4130. f = &font_defs[font_face_count];
  4131. font_copy(f->family, sizeof(f->family), family, flen);
  4132. font_copy(f->url, sizeof(f->url), url, ulen);
  4133. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4134. f->wmin = (unsigned short)wmin;
  4135. f->wmax = (unsigned short)wmax;
  4136. f->italic = (unsigned char)italic;
  4137. f->used = 0;
  4138. font_face_count++;
  4139. f->family_id = (unsigned char)(fam ? fam : font_face_count);
  4140. /* A family that a font-family declaration already resolved (an
  4141. * earlier @font-face of it) stays wanted for its new variants. */
  4142. if (fam && font_defs[fam - 1].used)
  4143. f->used = 1;
  4144. }
  4145. /* Look up the first name in a font-family list that has an @font-face and
  4146. * mark that family as used, so the browser only downloads faces the page
  4147. * refers to. */
  4148. static int font_family_id(const char *s, unsigned int len)
  4149. {
  4150. unsigned int i = 0;
  4151. while (i < len) {
  4152. unsigned int start, end, q;
  4153. while (i < len && (is_space(s[i]) || s[i] == ','))
  4154. i++;
  4155. if (i >= len)
  4156. break;
  4157. if (s[i] == '"' || s[i] == '\'') {
  4158. char quote = s[i++];
  4159. start = i;
  4160. while (i < len && s[i] != quote)
  4161. i++;
  4162. end = i;
  4163. if (i < len)
  4164. i++;
  4165. } else {
  4166. start = i;
  4167. while (i < len && s[i] != ',')
  4168. i++;
  4169. end = i;
  4170. while (end > start && is_space(s[end - 1]))
  4171. end--;
  4172. }
  4173. if (end <= start)
  4174. continue;
  4175. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4176. int fam;
  4177. unsigned int k;
  4178. if (!font_name_eq(font_defs[q].family, s + start,
  4179. end - start))
  4180. continue;
  4181. fam = font_defs[q].family_id;
  4182. for (k = 0; k < (unsigned int)font_face_count; k++)
  4183. if (font_defs[k].family_id == fam)
  4184. font_defs[k].used = 1;
  4185. return fam;
  4186. }
  4187. }
  4188. return 0;
  4189. }
  4190. /* font-weight descriptor: `normal`, `bold`, a number, or a variable font's
  4191. * `100 900` range. */
  4192. static void font_parse_weight(const char *s, unsigned int len,
  4193. unsigned int *wmin, unsigned int *wmax)
  4194. {
  4195. unsigned int i = 0, n = 0, w[2] = { 400, 400 };
  4196. while (i < len && n < 2) {
  4197. unsigned int v = 0, st;
  4198. while (i < len && is_space(s[i]))
  4199. i++;
  4200. st = i;
  4201. while (i < len && !is_space(s[i]))
  4202. i++;
  4203. if (i == st)
  4204. break;
  4205. if (i - st == 4 && ci_eq_n(s + st, "bold", 4)) {
  4206. v = 700;
  4207. } else if (i - st == 6 && ci_eq_n(s + st, "bolder", 6)) {
  4208. v = 700;
  4209. } else if (i - st == 7 && ci_eq_n(s + st, "lighter", 7)) {
  4210. v = 300;
  4211. } else if (s[st] >= '0' && s[st] <= '9') {
  4212. unsigned int k;
  4213. for (k = st; k < i && s[k] >= '0' && s[k] <= '9'; k++)
  4214. v = v * 10 + (unsigned int)(s[k] - '0');
  4215. } else {
  4216. v = 400;
  4217. }
  4218. if (v < 1)
  4219. v = 1;
  4220. if (v > 1000)
  4221. v = 1000;
  4222. w[n++] = v;
  4223. }
  4224. if (n == 1)
  4225. w[1] = w[0];
  4226. if (w[0] > w[1]) {
  4227. unsigned int t = w[0];
  4228. w[0] = w[1];
  4229. w[1] = t;
  4230. }
  4231. *wmin = w[0];
  4232. *wmax = w[1];
  4233. }
  4234. static int font_hex(char c)
  4235. {
  4236. if (c >= '0' && c <= '9')
  4237. return c - '0';
  4238. if (c >= 'a' && c <= 'f')
  4239. return c - 'a' + 10;
  4240. if (c >= 'A' && c <= 'F')
  4241. return c - 'A' + 10;
  4242. return -1;
  4243. }
  4244. /* Whether a unicode-range list covers basic Latin. Google Fonts splits a
  4245. * family into per-script subsets with the same family/weight/style; only
  4246. * the one holding ASCII is useful here (the text path is Latin-1). */
  4247. static int font_range_has_latin(const char *s, unsigned int len)
  4248. {
  4249. unsigned int i = 0;
  4250. while (i < len) {
  4251. unsigned int lo = 0, hi, digits = 0, wild = 0;
  4252. while (i < len && (is_space(s[i]) || s[i] == ','))
  4253. i++;
  4254. if (i + 2 > len || (s[i] != 'U' && s[i] != 'u') ||
  4255. s[i + 1] != '+') {
  4256. while (i < len && s[i] != ',')
  4257. i++;
  4258. continue;
  4259. }
  4260. i += 2;
  4261. while (i < len && digits < 6 &&
  4262. (font_hex(s[i]) >= 0 || s[i] == '?')) {
  4263. if (s[i] == '?') {
  4264. lo <<= 4;
  4265. wild++;
  4266. } else {
  4267. lo = (lo << 4) | (unsigned int)font_hex(s[i]);
  4268. }
  4269. digits++;
  4270. i++;
  4271. }
  4272. hi = lo | ((1u << (wild * 4)) - 1u);
  4273. if (!wild && i < len && s[i] == '-') {
  4274. hi = 0;
  4275. i++;
  4276. while (i < len && font_hex(s[i]) >= 0) {
  4277. hi = (hi << 4) | (unsigned int)font_hex(s[i]);
  4278. i++;
  4279. }
  4280. }
  4281. if (digits && lo <= 'a' && hi >= 'a')
  4282. return 1;
  4283. while (i < len && s[i] != ',')
  4284. i++;
  4285. }
  4286. return 0;
  4287. }
  4288. /* Pick the first `src` entry the text engine can load: a TrueType/OpenType
  4289. * file or WOFF 1.0, by its format() hint or, without one, by its
  4290. * extension. local() names, WOFF2, EOT, SVG fonts and data: URLs (they do
  4291. * not fit BR_FACE_URL) are skipped. */
  4292. static int font_pick_src(const char *s, unsigned int len, char *url,
  4293. unsigned int cap, unsigned int *ulen)
  4294. {
  4295. unsigned int i = 0;
  4296. while (i < len) {
  4297. unsigned int is_ = i, ie, us = 0, ue = 0, q, depth = 0;
  4298. int quote = 0, ok;
  4299. const char *up;
  4300. unsigned int un;
  4301. /* One comma-separated item, commas inside url()/quotes kept. */
  4302. while (i < len) {
  4303. char c = s[i];
  4304. if (quote) {
  4305. if (c == quote)
  4306. quote = 0;
  4307. } else if (c == '"' || c == '\'') {
  4308. quote = c;
  4309. } else if (c == '(') {
  4310. depth++;
  4311. } else if (c == ')') {
  4312. if (depth)
  4313. depth--;
  4314. } else if (c == ',' && !depth) {
  4315. break;
  4316. }
  4317. i++;
  4318. }
  4319. ie = i;
  4320. i++;
  4321. for (q = is_; q + 4 <= ie; q++)
  4322. if (ci_eq_n(s + q, "url(", 4))
  4323. break;
  4324. if (q + 4 > ie)
  4325. continue;
  4326. us = q + 4;
  4327. for (ue = us; ue < ie && s[ue] != ')'; ue++)
  4328. if (s[ue] == '"' || s[ue] == '\'') {
  4329. char qc = s[ue++];
  4330. while (ue < ie && s[ue] != qc)
  4331. ue++;
  4332. }
  4333. up = s + us;
  4334. un = ue - us;
  4335. font_trim(&up, &un);
  4336. if (!un || un >= cap || (un >= 5 && ci_eq_n(up, "data:", 5)))
  4337. continue;
  4338. ok = 1;
  4339. if (contains_ci_len(s + ue, ie - ue, "format(")) {
  4340. const char *fp = s + ue;
  4341. unsigned int fn = ie - ue;
  4342. ok = (contains_ci_len(fp, fn, "truetype") ||
  4343. contains_ci_len(fp, fn, "opentype") ||
  4344. contains_ci_len(fp, fn, "woff")) &&
  4345. !contains_ci_len(fp, fn, "woff2") &&
  4346. !contains_ci_len(fp, fn, "embedded");
  4347. } else {
  4348. unsigned int e = 0;
  4349. while (e < un && up[e] != '?' && up[e] != '#')
  4350. e++;
  4351. if ((e >= 6 && ci_eq_n(up + e - 6, ".woff2", 6)) ||
  4352. (e >= 4 && ci_eq_n(up + e - 4, ".eot", 4)) ||
  4353. (e >= 4 && ci_eq_n(up + e - 4, ".svg", 4)))
  4354. ok = 0;
  4355. }
  4356. if (!ok)
  4357. continue;
  4358. for (q = 0; q < un; q++)
  4359. url[q] = up[q];
  4360. url[un] = '\0';
  4361. *ulen = un;
  4362. return 1;
  4363. }
  4364. return 0;
  4365. }
  4366. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4367. * at-rules, so faces must be registered before any font-family declaration
  4368. * in the same sheet is resolved. */
  4369. static void scan_font_faces(const char *s, unsigned int len)
  4370. {
  4371. unsigned int i = 0;
  4372. while (i + 10 <= len) {
  4373. unsigned int j, end;
  4374. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4375. unsigned int flen = 0, ulen = 0, p;
  4376. unsigned int wmin = 400, wmax = 400;
  4377. int have_family = 0, have_url = 0, italic = 0, latin = 1;
  4378. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4379. i++;
  4380. continue;
  4381. }
  4382. j = i + 10;
  4383. while (j < len && s[j] != '{' && s[j] != ';')
  4384. j++;
  4385. if (j >= len || s[j] == ';') {
  4386. i = j + 1;
  4387. continue;
  4388. }
  4389. end = css_skip_nested(s, len, j + 1);
  4390. p = j + 1;
  4391. while (p < end) {
  4392. unsigned int ds, de, vs, ve, quote = 0;
  4393. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4394. p++;
  4395. if (p >= end)
  4396. break;
  4397. ds = p;
  4398. while (p < end && s[p] != ':' && s[p] != ';' &&
  4399. s[p] != '}')
  4400. p++;
  4401. de = p;
  4402. if (p >= end || s[p] != ':') {
  4403. while (p < end && s[p] != ';')
  4404. p++;
  4405. continue;
  4406. }
  4407. p++;
  4408. vs = p;
  4409. /* A quoted URL may contain ';' (data: URLs do). */
  4410. while (p < end && (quote || (s[p] != ';' &&
  4411. s[p] != '}'))) {
  4412. if (quote && s[p] == (char)quote)
  4413. quote = 0;
  4414. else if (!quote && (s[p] == '"' || s[p] == '\''))
  4415. quote = (unsigned char)s[p];
  4416. p++;
  4417. }
  4418. ve = p;
  4419. while (ds < de && is_space(s[ds]))
  4420. ds++;
  4421. while (de > ds && is_space(s[de - 1]))
  4422. de--;
  4423. while (vs < ve && is_space(s[vs]))
  4424. vs++;
  4425. while (ve > vs && is_space(s[ve - 1]))
  4426. ve--;
  4427. if (de - ds == 11 &&
  4428. ci_eq_n(s + ds, "font-family", 11)) {
  4429. const char *fp = s + vs;
  4430. unsigned int fn = ve - vs;
  4431. font_trim(&fp, &fn);
  4432. if (fn && fn < sizeof(family)) {
  4433. unsigned int k;
  4434. for (k = 0; k < fn; k++)
  4435. family[k] = fp[k];
  4436. family[fn] = '\0';
  4437. flen = fn;
  4438. have_family = 1;
  4439. }
  4440. } else if (de - ds == 3 &&
  4441. ci_eq_n(s + ds, "src", 3)) {
  4442. /* A later src (the old IE `src: eot;` hack
  4443. * is followed by the real list) wins when
  4444. * it has a usable entry. */
  4445. if (font_pick_src(s + vs, ve - vs, url,
  4446. sizeof(url), &ulen))
  4447. have_url = 1;
  4448. } else if (de - ds == 11 &&
  4449. ci_eq_n(s + ds, "font-weight", 11)) {
  4450. font_parse_weight(s + vs, ve - vs, &wmin,
  4451. &wmax);
  4452. } else if (de - ds == 10 &&
  4453. ci_eq_n(s + ds, "font-style", 10)) {
  4454. italic = contains_ci_len(s + vs, ve - vs,
  4455. "italic") ||
  4456. contains_ci_len(s + vs, ve - vs,
  4457. "oblique");
  4458. } else if (de - ds == 13 &&
  4459. ci_eq_n(s + ds, "unicode-range", 13)) {
  4460. latin = font_range_has_latin(s + vs, ve - vs);
  4461. }
  4462. }
  4463. if (have_family && have_url && latin)
  4464. font_add(family, flen, url, ulen, wmin, wmax, italic);
  4465. i = end;
  4466. }
  4467. }
  4468. /* One border-radius length. Unlike parse_size(), percentages keep their
  4469. * kind: the drawer scales them to the box, not the viewport. */
  4470. static int parse_radius_value(const char *s, unsigned int len,
  4471. unsigned int *pos, unsigned short *val,
  4472. unsigned char *kind)
  4473. {
  4474. unsigned int i = *pos;
  4475. unsigned long v = 0;
  4476. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4477. while (i < len && is_space(s[i]))
  4478. i++;
  4479. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4480. v = v * 10 + (unsigned int)(s[i] - '0');
  4481. digits = 1;
  4482. i++;
  4483. }
  4484. if (i < len && s[i] == '.') {
  4485. i++;
  4486. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4487. fdigits = 1;
  4488. if (frac < 2) {
  4489. v = v * 10 + (unsigned int)(s[i] - '0');
  4490. frac++;
  4491. }
  4492. i++;
  4493. }
  4494. }
  4495. while (frac < 2) {
  4496. v *= 10;
  4497. frac++;
  4498. }
  4499. while (i < len && is_space(s[i]))
  4500. i++;
  4501. if (i < len && s[i] == '%') {
  4502. pct = 1;
  4503. i++;
  4504. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4505. s[i + 2] == 'm') {
  4506. rem = 1;
  4507. i += 3;
  4508. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4509. rem = 2;
  4510. i += 2;
  4511. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4512. i += 2;
  4513. } else if (i < len && is_ident(s[i])) {
  4514. return 0;
  4515. }
  4516. if (!digits && !fdigits)
  4517. return 0;
  4518. if (rem == 1)
  4519. v *= root_font_px;
  4520. else if (rem == 2)
  4521. v *= root_font_px;
  4522. v /= 100;
  4523. if (v > 2048)
  4524. v = 2048;
  4525. *val = (unsigned short)v;
  4526. *kind = pct ? 2 : 1;
  4527. *pos = i;
  4528. return 1;
  4529. }
  4530. /* font: [style] [variant] [weight] size[/line-height] family */
  4531. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4532. const char *v, unsigned int len, int important)
  4533. {
  4534. unsigned int pos = 0, ts, te;
  4535. unsigned short size_raw = 0;
  4536. unsigned char size_unit = 0;
  4537. int have_size = 0;
  4538. int bold = 0, italic = 0;
  4539. int lh_px = 0, lh_pct = 0, mono = 0, face;
  4540. unsigned int fam_at = len;
  4541. while (next_token(v, len, &pos, &ts, &te)) {
  4542. unsigned int tl = te - ts;
  4543. unsigned int slash = 0;
  4544. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4545. bold = 1;
  4546. continue;
  4547. }
  4548. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4549. continue;
  4550. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4551. italic = 1;
  4552. continue;
  4553. }
  4554. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4555. italic = 1;
  4556. continue;
  4557. }
  4558. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4559. continue;
  4560. for (slash = 0; slash < tl; slash++)
  4561. if (v[ts + slash] == '/')
  4562. break;
  4563. if (slash == tl) {
  4564. unsigned int q;
  4565. int weight = 0;
  4566. for (q = 0; q < tl; q++) {
  4567. if (v[ts + q] < '0' || v[ts + q] > '9')
  4568. break;
  4569. weight = weight * 10 + (v[ts + q] - '0');
  4570. }
  4571. if (q == tl && weight >= 100 && weight <= 900) {
  4572. bold = weight >= 500;
  4573. continue;
  4574. }
  4575. }
  4576. {
  4577. unsigned short raw;
  4578. unsigned char unit;
  4579. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4580. &raw, &unit))
  4581. continue;
  4582. size_raw = raw;
  4583. size_unit = unit;
  4584. }
  4585. if (!have_size)
  4586. fam_at = te; /* the family list follows the size */
  4587. have_size = 1;
  4588. if (slash < tl) {
  4589. unsigned int lstart = ts + slash + 1;
  4590. unsigned short lhp = 0;
  4591. unsigned char lhpct = 0;
  4592. if (!parse_line_height(v + lstart, te - lstart,
  4593. &lhp, &lhpct)) {
  4594. lh_px = 0;
  4595. lh_pct = 0;
  4596. } else {
  4597. lh_px = lhp;
  4598. lh_pct = lhpct;
  4599. }
  4600. }
  4601. }
  4602. if (!have_size)
  4603. return;
  4604. mono = contains_ci_len(v, len, "mono");
  4605. face = font_family_id(v + fam_at, len - fam_at);
  4606. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4607. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4608. decl_add(out, nout, PROP_BOLD, bold, important);
  4609. decl_add(out, nout, PROP_ITALIC, italic, important);
  4610. decl_add(out, nout, PROP_MONO, mono, important);
  4611. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4612. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4613. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4614. }
  4615. static int css_collect_only;
  4616. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4617. * index into d->layer_hash. css_layer_name is the dotted path of the
  4618. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4619. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4620. * layer's own rules therefore lose to its sublayers (the spec says the
  4621. * reverse), which no page we render depends on. */
  4622. static unsigned char css_cur_layer;
  4623. static char css_layer_name[128];
  4624. static unsigned int css_anon_layers;
  4625. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4626. {
  4627. unsigned int h = 2166136261u, i;
  4628. for (i = 0; i < len; i++) {
  4629. h ^= (unsigned char)s[i];
  4630. h *= 16777619u;
  4631. }
  4632. return h ? h : 1;
  4633. }
  4634. /* Register (or look up) the layer `name` nested under the current path and
  4635. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4636. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4637. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4638. unsigned int nlen, char *path,
  4639. unsigned int cap)
  4640. {
  4641. unsigned int pl = s_len(css_layer_name), i, h;
  4642. char anon[16];
  4643. while (nlen && is_space(*name)) { name++; nlen--; }
  4644. while (nlen && is_space(name[nlen - 1])) nlen--;
  4645. if (!nlen) {
  4646. /* An anonymous layer is unique; it can never be reopened.
  4647. * The collect-only (custom property) pass does not need a
  4648. * rank, so it must not burn a table slot. */
  4649. unsigned int n = ++css_anon_layers, k = 0;
  4650. char rev[10];
  4651. if (css_collect_only)
  4652. return 0;
  4653. anon[0] = '#';
  4654. while (n && k < sizeof(rev)) {
  4655. rev[k++] = (char)('0' + n % 10);
  4656. n /= 10;
  4657. }
  4658. for (i = 0; i < k; i++)
  4659. anon[1 + i] = rev[k - 1 - i];
  4660. name = anon;
  4661. nlen = k + 1;
  4662. }
  4663. if (pl + 1 + nlen >= cap)
  4664. return 0;
  4665. memcpy(path, css_layer_name, pl);
  4666. if (pl)
  4667. path[pl++] = '.';
  4668. memcpy(path + pl, name, nlen);
  4669. path[pl + nlen] = '\0';
  4670. h = layer_name_hash(path, pl + nlen);
  4671. for (i = 0; i < d->nlayers; i++)
  4672. if (d->layer_hash[i] == h)
  4673. return (unsigned char)(i + 1);
  4674. if (d->nlayers >= BR_MAX_LAYERS)
  4675. return 0;
  4676. d->layer_hash[d->nlayers++] = h;
  4677. return (unsigned char)d->nlayers;
  4678. }
  4679. /* @supports: the engine cannot answer feature queries, so the block
  4680. * applies unless it is negated (`not (...)` usually guards a fallback for
  4681. * old browsers) or asks for selector() support. */
  4682. static int supports_matches(const char *s, unsigned int len)
  4683. {
  4684. while (len && is_space(*s)) { s++; len--; }
  4685. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4686. (len == 3 || is_space(s[3]) || s[3] == '('))
  4687. return 0;
  4688. if (contains_ci_len(s, len, "selector("))
  4689. return 0;
  4690. return 1;
  4691. }
  4692. /* Append one code point of generated content as Latin-1. Typography the
  4693. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4694. * and anything else outside Latin-1 are dropped. */
  4695. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4696. unsigned int cp)
  4697. {
  4698. const char *sub = 0;
  4699. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4700. sub = "-";
  4701. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4702. sub = "'";
  4703. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4704. sub = "\"";
  4705. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4706. sub = "\xB7";
  4707. else if (cp == 0x2026)
  4708. sub = "...";
  4709. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4710. sub = ">";
  4711. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4712. sub = "<";
  4713. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4714. cp == 0x2003 || cp == 0x202F)
  4715. sub = " ";
  4716. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4717. sub = "\xD7";
  4718. else if (cp == 0x2713 || cp == 0x2714)
  4719. sub = "v";
  4720. if (sub) {
  4721. while (*sub && *n + 1 < cap)
  4722. out[(*n)++] = *sub++;
  4723. return;
  4724. }
  4725. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4726. out[(*n)++] = (char)cp;
  4727. else if (cp == '\n' && *n + 1 < cap)
  4728. out[(*n)++] = ' ';
  4729. }
  4730. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4731. * content index + 1, or 0 for none/empty (counters and url() are not
  4732. * generated). */
  4733. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4734. {
  4735. char out[160];
  4736. unsigned int i = 0, n = 0, off;
  4737. while (i < len) {
  4738. char c = s[i];
  4739. if (c == '"' || c == '\'') {
  4740. char q = c;
  4741. i++;
  4742. while (i < len && s[i] != q) {
  4743. unsigned int cp;
  4744. if (s[i] == '\\' && i + 1 < len) {
  4745. unsigned int h = 0, k = 0;
  4746. i++;
  4747. while (k < 6 && i < len &&
  4748. ((s[i] >= '0' && s[i] <= '9') ||
  4749. ((s[i] | 32) >= 'a' &&
  4750. (s[i] | 32) <= 'f'))) {
  4751. char x = (char)(s[i] | 32);
  4752. h = h * 16 + (unsigned int)(x <= '9' ?
  4753. x - '0' : x - 'a' + 10);
  4754. i++;
  4755. k++;
  4756. }
  4757. if (k) {
  4758. if (i < len && s[i] == ' ')
  4759. i++;
  4760. cp = h;
  4761. } else {
  4762. cp = (unsigned char)s[i++];
  4763. }
  4764. } else if ((unsigned char)s[i] >= 0xC0) {
  4765. unsigned char b = (unsigned char)s[i++];
  4766. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4767. cp = b & (0x3F >> more);
  4768. while (more-- && i < len &&
  4769. ((unsigned char)s[i] & 0xC0) == 0x80)
  4770. cp = (cp << 6) | (s[i++] & 0x3F);
  4771. } else {
  4772. cp = (unsigned char)s[i++];
  4773. }
  4774. content_put(out, &n, sizeof(out), cp);
  4775. }
  4776. i++;
  4777. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4778. unsigned int st = i + 5, e = st;
  4779. while (e < len && s[e] != ')')
  4780. e++;
  4781. while (st < e && is_space(s[st]))
  4782. st++;
  4783. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4784. out[n++] = '\001';
  4785. while (st < e && !is_space(s[st]))
  4786. out[n++] = s[st++];
  4787. out[n++] = '\002';
  4788. }
  4789. i = e + 1;
  4790. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4791. ci_eq_n(s + i, "close-quot", 10))) {
  4792. content_put(out, &n, sizeof(out), '"');
  4793. i += 10;
  4794. } else if (c == '(') {
  4795. unsigned int depth = 0;
  4796. /* counter(), url(), ...: skip the arguments */
  4797. for (; i < len; i++) {
  4798. if (s[i] == '(')
  4799. depth++;
  4800. else if (s[i] == ')' && --depth == 0)
  4801. break;
  4802. }
  4803. i++;
  4804. } else {
  4805. i++;
  4806. }
  4807. }
  4808. if (!n) {
  4809. /* `content: ""` still creates the box: icon
  4810. * pseudo-elements are empty strings with a size and a
  4811. * background image. */
  4812. for (i = 0; i < len; i++)
  4813. if (s[i] == '"' || s[i] == '\'')
  4814. return BR_CONTENT_EMPTY;
  4815. return 0;
  4816. }
  4817. if (d->ncontent >= BR_MAX_CONTENT)
  4818. return 0;
  4819. off = store_raw(d, out, n, sizeof(out));
  4820. if (!off)
  4821. return 0;
  4822. d->content_off[d->ncontent++] = off;
  4823. return d->ncontent;
  4824. }
  4825. /* ---- calc() folding ---------------------------------------------------
  4826. *
  4827. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4828. * Frameworks build their scales from arithmetic on custom properties
  4829. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4830. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4831. * calc() in a declaration block is folded into that canonical form:
  4832. * a linear combination of %, px (rem/em at the root font size, vw against
  4833. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4834. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4835. * the calc() text unchanged. Values are hundredths. */
  4836. static unsigned int viewport_w; /* defined with the media code */
  4837. struct calc_val {
  4838. long px, pct, num;
  4839. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  4840. };
  4841. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4842. struct calc_val *v, int depth);
  4843. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  4844. {
  4845. while (*i < len && is_space(s[*i]))
  4846. (*i)++;
  4847. }
  4848. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  4849. struct calc_val *v, int depth)
  4850. {
  4851. long whole = 0, frac = 0, scale = 1, n;
  4852. int sign = 1, digits = 0;
  4853. calc_space(s, len, i);
  4854. if (*i < len && (s[*i] == '(' ||
  4855. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  4856. *i += s[*i] == '(' ? 1 : 5;
  4857. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  4858. return 0;
  4859. calc_space(s, len, i);
  4860. if (*i >= len || s[*i] != ')')
  4861. return 0;
  4862. (*i)++;
  4863. return 1;
  4864. }
  4865. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  4866. sign = s[*i] == '-' ? -1 : 1;
  4867. (*i)++;
  4868. }
  4869. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4870. whole = whole * 10 + (s[*i] - '0');
  4871. digits = 1;
  4872. (*i)++;
  4873. if (whole > 1000000)
  4874. return 0;
  4875. }
  4876. if (*i < len && s[*i] == '.') {
  4877. (*i)++;
  4878. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4879. if (scale < 10000) {
  4880. frac = frac * 10 + (s[*i] - '0');
  4881. scale *= 10;
  4882. }
  4883. digits = 1;
  4884. (*i)++;
  4885. }
  4886. }
  4887. if (!digits)
  4888. return 0;
  4889. n = sign * (whole * 100 + frac * 100 / scale);
  4890. v->px = v->pct = v->num = 0;
  4891. if (*i < len && s[*i] == '%') {
  4892. (*i)++;
  4893. v->pct = n;
  4894. v->kind = 2;
  4895. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  4896. *i += 3;
  4897. v->px = n * root_font_px;
  4898. v->kind = 1;
  4899. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  4900. *i += 2;
  4901. v->px = n * root_font_px;
  4902. v->kind = 1;
  4903. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  4904. *i += 2;
  4905. v->px = n;
  4906. v->kind = 1;
  4907. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  4908. *i += 2;
  4909. v->px = n * (long)viewport_w / 100;
  4910. v->kind = 1;
  4911. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  4912. return 0; /* vh, deg, fr, ch, ... */
  4913. } else {
  4914. v->num = n;
  4915. v->kind = 4;
  4916. }
  4917. return 1;
  4918. }
  4919. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  4920. struct calc_val *v, int depth)
  4921. {
  4922. if (!calc_unary(s, len, i, v, depth))
  4923. return 0;
  4924. for (;;) {
  4925. struct calc_val r;
  4926. char op;
  4927. calc_space(s, len, i);
  4928. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  4929. return 1;
  4930. op = s[(*i)++];
  4931. if (!calc_unary(s, len, i, &r, depth))
  4932. return 0;
  4933. if (op == '/') {
  4934. if (r.kind != 4 || !r.num)
  4935. return 0;
  4936. v->px = v->px * 100 / r.num;
  4937. v->pct = v->pct * 100 / r.num;
  4938. v->num = v->num * 100 / r.num;
  4939. } else if (r.kind == 4) {
  4940. v->px = v->px * r.num / 100;
  4941. v->pct = v->pct * r.num / 100;
  4942. v->num = v->num * r.num / 100;
  4943. } else if (v->kind == 4) {
  4944. long k = v->num;
  4945. *v = r;
  4946. v->px = v->px * k / 100;
  4947. v->pct = v->pct * k / 100;
  4948. } else {
  4949. return 0; /* length * length */
  4950. }
  4951. }
  4952. }
  4953. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4954. struct calc_val *v, int depth)
  4955. {
  4956. if (!calc_product(s, len, i, v, depth))
  4957. return 0;
  4958. for (;;) {
  4959. struct calc_val r;
  4960. int neg;
  4961. calc_space(s, len, i);
  4962. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  4963. return 1;
  4964. neg = s[(*i)++] == '-';
  4965. if (!calc_product(s, len, i, &r, depth))
  4966. return 0;
  4967. if ((v->kind == 4) != (r.kind == 4))
  4968. return 0; /* number + length */
  4969. v->px += neg ? -r.px : r.px;
  4970. v->pct += neg ? -r.pct : r.pct;
  4971. v->num += neg ? -r.num : r.num;
  4972. v->kind |= r.kind;
  4973. }
  4974. }
  4975. /* Hundredths -> "12", "-0.5", "33.33". */
  4976. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  4977. {
  4978. char tmp[24];
  4979. unsigned int n = 0, k = 0;
  4980. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  4981. unsigned long w = a / 100, f = a % 100;
  4982. if (v < 0)
  4983. tmp[n++] = '-';
  4984. {
  4985. char rev[12];
  4986. unsigned int r = 0;
  4987. do {
  4988. rev[r++] = (char)('0' + w % 10);
  4989. w /= 10;
  4990. } while (w && r < sizeof(rev));
  4991. while (r)
  4992. tmp[n++] = rev[--r];
  4993. }
  4994. if (f) {
  4995. tmp[n++] = '.';
  4996. tmp[n++] = (char)('0' + f / 10);
  4997. if (f % 10)
  4998. tmp[n++] = (char)('0' + f % 10);
  4999. }
  5000. for (k = 0; k < n && k < cap; k++)
  5001. o[k] = tmp[k];
  5002. return k;
  5003. }
  5004. /* Copy s to out with every foldable calc() replaced; returns the length,
  5005. * or 0 when nothing was folded (the caller keeps the original). */
  5006. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  5007. unsigned int cap)
  5008. {
  5009. unsigned int i = 0, n = 0;
  5010. int changed = 0;
  5011. while (i < len && n + 40 < cap) {
  5012. struct calc_val v;
  5013. unsigned int j = i + 5;
  5014. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  5015. (i == 0 || !is_ident(s[i - 1])))) {
  5016. out[n++] = s[i++];
  5017. continue;
  5018. }
  5019. if (!calc_sum(s, len, &j, &v, 0) ||
  5020. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  5021. out[n++] = s[i++];
  5022. continue;
  5023. }
  5024. j++;
  5025. /* Minified CSS glues tokens to the function's parentheses
  5026. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  5027. * a folded `10px16px` would be one token. */
  5028. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  5029. out[n - 1] != '(' && out[n - 1] != ',')
  5030. out[n++] = ' ';
  5031. if (v.kind == 4) {
  5032. n += calc_fmt(out + n, cap - n, v.num);
  5033. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  5034. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  5035. -50 : 50)) /
  5036. 100 * 100);
  5037. out[n++] = 'p';
  5038. out[n++] = 'x';
  5039. } else if (!v.px) {
  5040. n += calc_fmt(out + n, cap - n, v.pct);
  5041. out[n++] = '%';
  5042. } else {
  5043. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  5044. memcpy(out + n, "calc(", 5);
  5045. n += 5;
  5046. n += calc_fmt(out + n, cap - n, v.pct);
  5047. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  5048. n += 4;
  5049. n += calc_fmt(out + n, cap - n, px * 100);
  5050. memcpy(out + n, "px)", 3);
  5051. n += 3;
  5052. }
  5053. i = j;
  5054. changed = 1;
  5055. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  5056. s[i] != ',' && s[i] != '!' && s[i] != '}')
  5057. out[n++] = ' ';
  5058. }
  5059. if (!changed || i < len)
  5060. return 0;
  5061. out[n] = '\0';
  5062. return n;
  5063. }
  5064. static int css_has_calc(const char *s, unsigned int len)
  5065. {
  5066. unsigned int i;
  5067. for (i = 0; i + 5 <= len; i++)
  5068. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  5069. return 1;
  5070. return 0;
  5071. }
  5072. static void parse_decls(struct br_doc *d, const char *s,
  5073. unsigned int len, struct br_decl *out,
  5074. unsigned int *nout)
  5075. {
  5076. static char expanded[4096];
  5077. static char folded[4096];
  5078. if (css_has_var(s, len)) {
  5079. unsigned int elen;
  5080. if (!css_env_active)
  5081. css_local_collect(s, len);
  5082. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  5083. 0);
  5084. if (!css_env_active)
  5085. css_nlocal = 0;
  5086. if (elen) {
  5087. s = expanded;
  5088. len = elen;
  5089. }
  5090. }
  5091. if (css_has_calc(s, len)) {
  5092. unsigned int flen = css_fold_calc(s, len, folded,
  5093. sizeof(folded));
  5094. if (flen) {
  5095. s = folded;
  5096. len = flen;
  5097. }
  5098. }
  5099. unsigned int i = 0;
  5100. *nout = 0;
  5101. while (i < len && *nout < BR_DECLS) {
  5102. unsigned int pstart, pend, vstart, vend, k;
  5103. char prop[24];
  5104. int important = 0;
  5105. while (i < len && (is_space(s[i]) || s[i] == ';'))
  5106. i++;
  5107. if (i >= len)
  5108. break;
  5109. /* A nested at-rule (@media/@supports) inside the declaration
  5110. * block carries its own selector context; skip it rather than
  5111. * reading its prelude as a declaration. */
  5112. if (s[i] == '@') {
  5113. while (i < len && s[i] != '{' && s[i] != ';')
  5114. i++;
  5115. if (i < len && s[i] == '{')
  5116. i = css_skip_nested(s, len, i + 1);
  5117. else if (i < len)
  5118. i++;
  5119. continue;
  5120. }
  5121. pstart = i;
  5122. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  5123. i++;
  5124. if (i < len && s[i] == '{') {
  5125. /* Nested style rule: skip its block. */
  5126. i = css_skip_nested(s, len, i + 1);
  5127. continue;
  5128. }
  5129. pend = i;
  5130. if (i >= len || s[i] != ':')
  5131. continue;
  5132. i++;
  5133. vstart = i;
  5134. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  5135. * does not end the value. */
  5136. i = css_value_end(s, len, i);
  5137. vend = i;
  5138. while (pstart < pend && is_space(s[pstart]))
  5139. pstart++;
  5140. while (pend > pstart && is_space(s[pend - 1]))
  5141. pend--;
  5142. while (vstart < vend && is_space(s[vstart]))
  5143. vstart++;
  5144. while (vend > vstart && is_space(s[vend - 1]))
  5145. vend--;
  5146. if (vend - vstart >= 10 &&
  5147. ci_eq_n(s + vend - 10, "!important", 10)) {
  5148. important = 1;
  5149. vend -= 10;
  5150. while (vend > vstart && is_space(s[vend - 1]))
  5151. vend--;
  5152. }
  5153. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  5154. char c = s[pstart + k];
  5155. if (c >= 'A' && c <= 'Z')
  5156. c = (char)(c - 'A' + 'a');
  5157. prop[k] = c;
  5158. }
  5159. prop[k] = '\0';
  5160. if (str_eq(prop, "color")) {
  5161. unsigned short c;
  5162. if (parse_color(s + vstart, vend - vstart, &c))
  5163. decl_add(out, nout, PROP_COLOR, c, important);
  5164. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  5165. /* SVG paint only: an HTML element's fill never paints
  5166. * its background (tagesschau sets fill on buttons). */
  5167. int is_fill = prop[0] == 'f';
  5168. unsigned short c;
  5169. int mode = paint_mode(s + vstart, vend - vstart, &c);
  5170. if (mode) {
  5171. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  5172. PROP_STROKE_MODE, mode, important);
  5173. if (mode == BR_PAINT_COLOR)
  5174. decl_add(out, nout, is_fill ? PROP_FILL :
  5175. PROP_STROKE, (short)c,
  5176. important);
  5177. }
  5178. } else if (str_eq(prop, "stroke-width")) {
  5179. int sw = svg_len16(s + vstart, vend - vstart);
  5180. if (sw >= 0)
  5181. decl_add(out, nout, PROP_STROKE_W, sw,
  5182. important);
  5183. } else if (str_eq(prop, "fill-rule")) {
  5184. decl_add(out, nout, PROP_FILL_RULE,
  5185. contains_ci_len(s + vstart, vend - vstart,
  5186. "evenodd") ? 1 : 0,
  5187. important);
  5188. } else if (str_eq(prop, "text-anchor")) {
  5189. int a = BR_ALIGN_LEFT;
  5190. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  5191. a = BR_ALIGN_CENTER;
  5192. else if (contains_ci_len(s + vstart, vend - vstart,
  5193. "end"))
  5194. a = BR_ALIGN_RIGHT;
  5195. decl_add(out, nout, PROP_ALIGN, a, important);
  5196. } else if (str_eq(prop, "background-image")) {
  5197. add_bg_image_decl(out, nout, s + vstart,
  5198. vend - vstart, important);
  5199. } else if (str_eq(prop, "background-repeat")) {
  5200. int r = 0;
  5201. if (contains_ci_len(s + vstart, vend - vstart,
  5202. "no-repeat"))
  5203. r = 1;
  5204. else if (contains_ci_len(s + vstart, vend - vstart,
  5205. "repeat-x"))
  5206. r = 2;
  5207. else if (contains_ci_len(s + vstart, vend - vstart,
  5208. "repeat-y"))
  5209. r = 3;
  5210. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  5211. } else if (str_eq(prop, "background-position")) {
  5212. add_bg_position(out, nout, s + vstart, vend - vstart,
  5213. important);
  5214. } else if (str_eq(prop, "background-size")) {
  5215. add_bg_size(out, nout, s + vstart, vend - vstart,
  5216. important);
  5217. } else if (str_eq(prop, "background-color") ||
  5218. str_eq(prop, "background")) {
  5219. unsigned short c;
  5220. int is_bg = str_eq(prop, "background");
  5221. static char bgv[1024];
  5222. const char *vs = s + vstart;
  5223. unsigned int vl = vend - vstart;
  5224. if (is_bg) {
  5225. add_bg_image_decl(out, nout, s + vstart,
  5226. vend - vstart, important);
  5227. /* Keywords and colours are read with url()
  5228. * contents blanked: a data: SVG carries its
  5229. * own fill="rgba(...)". */
  5230. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  5231. vs = bgv;
  5232. add_bg_shorthand_extra(out, nout,
  5233. vs, vl,
  5234. important);
  5235. if (contains_ci_len(vs, vl,
  5236. "no-repeat"))
  5237. decl_add(out, nout, PROP_BG_REPEAT, 1,
  5238. important);
  5239. else if (contains_ci_len(vs, vl,
  5240. "repeat-x"))
  5241. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5242. important);
  5243. else if (contains_ci_len(vs, vl,
  5244. "repeat-y"))
  5245. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5246. important);
  5247. }
  5248. /* `transparent`/`none` are real values: they must
  5249. * beat a lower-specificity colour (and tell form
  5250. * controls the author styled them). */
  5251. if (color_is_transparent(vs, vl) ||
  5252. (vl == 4 &&
  5253. ci_eq_n(vs, "none", 4)))
  5254. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5255. else if (parse_color(vs, vl, &c))
  5256. decl_add(out, nout, PROP_BG, c, important);
  5257. else if (is_bg &&
  5258. find_color_last(vs, vl, &c))
  5259. decl_add(out, nout, PROP_BG, c, important);
  5260. } else if (str_eq(prop, "font-size")) {
  5261. unsigned short raw;
  5262. unsigned char unit;
  5263. if (parse_font_size(s + vstart, vend - vstart,
  5264. &raw, &unit)) {
  5265. decl_add(out, nout, PROP_FONT_PX, raw,
  5266. important);
  5267. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5268. important);
  5269. }
  5270. } else if (str_eq(prop, "font-weight")) {
  5271. int bold = 0;
  5272. unsigned int j;
  5273. if (vend - vstart >= 4 &&
  5274. (ci_eq_n(s + vstart, "bold", 4) ||
  5275. (vend - vstart >= 6 &&
  5276. ci_eq_n(s + vstart, "bolder", 6))))
  5277. bold = 1;
  5278. else {
  5279. int w = 0;
  5280. for (j = vstart; j < vend; j++)
  5281. if (s[j] >= '0' && s[j] <= '9')
  5282. w = w * 10 + (s[j] - '0');
  5283. /* CSS font matching: with only a 400 and a
  5284. * 700 face, 500 (Infima's "semibold" menus)
  5285. * resolves to the regular face; 600 and up
  5286. * to the bold one. */
  5287. if (w >= 600)
  5288. bold = 1;
  5289. }
  5290. decl_add(out, nout, PROP_BOLD, bold, important);
  5291. } else if (str_eq(prop, "font-style")) {
  5292. int italic = (vend - vstart >= 6 &&
  5293. ci_eq_n(s + vstart, "italic", 6));
  5294. decl_add(out, nout, PROP_ITALIC, italic, important);
  5295. } else if (str_eq(prop, "font-family")) {
  5296. unsigned int j;
  5297. int mono = 0;
  5298. int face = font_family_id(s + vstart, vend - vstart);
  5299. for (j = vstart; j + 4 <= vend; j++)
  5300. if (ci_eq_n(s + j, "mono", 4))
  5301. mono = 1;
  5302. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5303. decl_add(out, nout, PROP_MONO, mono, important);
  5304. } else if (str_eq(prop, "font")) {
  5305. add_font_shorthand(out, nout, s + vstart,
  5306. vend - vstart, important);
  5307. } else if (str_eq(prop, "text-align")) {
  5308. int a = BR_ALIGN_LEFT;
  5309. if (vend - vstart >= 6 &&
  5310. ci_eq_n(s + vstart, "center", 6))
  5311. a = BR_ALIGN_CENTER;
  5312. else if (vend - vstart >= 5 &&
  5313. ci_eq_n(s + vstart, "right", 5))
  5314. a = BR_ALIGN_RIGHT;
  5315. decl_add(out, nout, PROP_ALIGN, a, important);
  5316. } else if (str_eq(prop, "text-decoration")) {
  5317. int u = 0, stv = 0, ov = 0;
  5318. if (contains_ci_len(s + vstart, vend - vstart,
  5319. "underline"))
  5320. u = 1;
  5321. if (contains_ci_len(s + vstart, vend - vstart,
  5322. "line-through"))
  5323. stv = 1;
  5324. if (contains_ci_len(s + vstart, vend - vstart,
  5325. "overline"))
  5326. ov = 1;
  5327. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5328. decl_add(out, nout, PROP_STRIKE, stv, important);
  5329. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5330. } else if (str_eq(prop, "cursor")) {
  5331. int c = parse_cursor(s + vstart, vend - vstart);
  5332. if (c >= 0)
  5333. decl_add(out, nout, PROP_CURSOR, c, important);
  5334. } else if (str_eq(prop, "text-transform")) {
  5335. int u = (vend - vstart >= 9 &&
  5336. ci_eq_n(s + vstart, "uppercase", 9));
  5337. decl_add(out, nout, PROP_UPPER, u, important);
  5338. } else if (str_eq(prop, "white-space")) {
  5339. int nw = contains_ci_len(s + vstart, vend - vstart,
  5340. "nowrap") ||
  5341. contains_ci_len(s + vstart, vend - vstart,
  5342. "pre");
  5343. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5344. } else if (str_eq(prop, "display")) {
  5345. decl_add(out, nout, PROP_DISPLAY,
  5346. parse_display(s + vstart, vend - vstart),
  5347. important);
  5348. } else if (str_eq(prop, "visibility")) {
  5349. int h = contains_ci_len(s + vstart, vend - vstart,
  5350. "hidden") ||
  5351. contains_ci_len(s + vstart, vend - vstart,
  5352. "collapse");
  5353. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5354. } else if (str_eq(prop, "opacity")) {
  5355. unsigned int q;
  5356. int h = 1, any = 0;
  5357. for (q = vstart; q < vend; q++) {
  5358. if (s[q] >= '1' && s[q] <= '9') {
  5359. h = 0;
  5360. break;
  5361. }
  5362. if (s[q] >= '0' && s[q] <= '9')
  5363. any = 1;
  5364. }
  5365. if (!any || contains_ci_len(s + vstart, vend - vstart,
  5366. "inherit"))
  5367. h = 0;
  5368. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  5369. } else if (str_eq(prop, "overflow") ||
  5370. str_eq(prop, "overflow-x") ||
  5371. str_eq(prop, "overflow-y")) {
  5372. int h = contains_ci_len(s + vstart, vend - vstart,
  5373. "hidden") ||
  5374. contains_ci_len(s + vstart, vend - vstart,
  5375. "clip");
  5376. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5377. } else if (str_eq(prop, "float")) {
  5378. int f = 0;
  5379. if (vend - vstart >= 4 &&
  5380. ci_eq_n(s + vstart, "left", 4))
  5381. f = 1;
  5382. else if (vend - vstart >= 5 &&
  5383. ci_eq_n(s + vstart, "right", 5))
  5384. f = 2;
  5385. decl_add(out, nout, PROP_FLOAT, f, important);
  5386. } else if (str_eq(prop, "clear")) {
  5387. int c = 0;
  5388. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5389. c = 3;
  5390. else if (contains_ci_len(s + vstart, vend - vstart,
  5391. "left"))
  5392. c = 1;
  5393. else if (contains_ci_len(s + vstart, vend - vstart,
  5394. "right"))
  5395. c = 2;
  5396. decl_add(out, nout, PROP_CLEAR, c, important);
  5397. } else if (str_eq(prop, "flex-grow") ||
  5398. str_eq(prop, "flex")) {
  5399. unsigned int q = vstart;
  5400. int g = 0;
  5401. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5402. q++;
  5403. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5404. ci_eq_n(s + q, "auto", 4))
  5405. g = 1; /* flex: auto = 1 1 auto */
  5406. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5407. g = g * 10 + (s[q] - '0');
  5408. q++;
  5409. }
  5410. if (g > 100)
  5411. g = 100;
  5412. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5413. if (str_eq(prop, "flex")) {
  5414. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5415. * basis is the item's main size, which our flex
  5416. * row reads from width (Infima's grid:
  5417. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5418. * of the grow number, then any unitless shrink. */
  5419. unsigned int t;
  5420. while (q < vend && (s[q] == '.' ||
  5421. (s[q] >= '0' && s[q] <= '9')))
  5422. q++;
  5423. for (t = 0; t < 2 && q < vend; t++) {
  5424. unsigned int st, e, k2;
  5425. unsigned short bv;
  5426. unsigned char bk;
  5427. while (q < vend && is_space(s[q]))
  5428. q++;
  5429. st = q;
  5430. while (q < vend && !is_space(s[q]))
  5431. q++;
  5432. e = q;
  5433. for (k2 = st; k2 < e; k2++)
  5434. if (!(s[k2] == '.' ||
  5435. (s[k2] >= '0' &&
  5436. s[k2] <= '9')))
  5437. break;
  5438. if (k2 == e)
  5439. continue; /* shrink */
  5440. if (parse_size(s + st, e - st, &bv,
  5441. &bk) && bk && bv) {
  5442. decl_add(out, nout, PROP_WIDTH,
  5443. bv, important);
  5444. decl_add(out, nout,
  5445. PROP_WIDTH_KIND, bk,
  5446. important);
  5447. }
  5448. break;
  5449. }
  5450. }
  5451. } else if (str_eq(prop, "flex-basis")) {
  5452. unsigned short bv;
  5453. unsigned char bk;
  5454. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5455. bk && bv) {
  5456. decl_add(out, nout, PROP_WIDTH, bv, important);
  5457. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5458. important);
  5459. }
  5460. } else if (str_eq(prop, "flex-direction")) {
  5461. decl_add(out, nout, PROP_FLEX_COL,
  5462. contains_ci_len(s + vstart, vend - vstart,
  5463. "column"), important);
  5464. } else if (str_eq(prop, "flex-wrap")) {
  5465. int wrap = 0;
  5466. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5467. wrap = 2;
  5468. else if (contains_ci_len(s + vstart, vend - vstart,
  5469. "wrap"))
  5470. wrap = 1;
  5471. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5472. } else if (str_eq(prop, "justify-content")) {
  5473. int j = 0;
  5474. if (contains_ci_len(s + vstart, vend - vstart,
  5475. "space-between"))
  5476. j = 3;
  5477. else if (contains_ci_len(s + vstart, vend - vstart,
  5478. "space-around"))
  5479. j = 4;
  5480. else if (contains_ci_len(s + vstart, vend - vstart,
  5481. "space-evenly"))
  5482. j = 5;
  5483. else if (contains_ci_len(s + vstart, vend - vstart,
  5484. "center"))
  5485. j = 1;
  5486. else if (contains_ci_len(s + vstart, vend - vstart,
  5487. "flex-end") ||
  5488. contains_ci_len(s + vstart, vend - vstart,
  5489. "end"))
  5490. j = 2;
  5491. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5492. } else if (str_eq(prop, "align-self")) {
  5493. /* 0 auto (the container's align-items), else
  5494. * align-items + 1. */
  5495. int a = 1;
  5496. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5497. a = 0;
  5498. else if (contains_ci_len(s + vstart, vend - vstart,
  5499. "center"))
  5500. a = 2;
  5501. else if (contains_ci_len(s + vstart, vend - vstart,
  5502. "flex-end") ||
  5503. contains_ci_len(s + vstart, vend - vstart,
  5504. "end"))
  5505. a = 3;
  5506. else if (contains_ci_len(s + vstart, vend - vstart,
  5507. "start") ||
  5508. contains_ci_len(s + vstart, vend - vstart,
  5509. "baseline"))
  5510. a = 4;
  5511. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5512. } else if (str_eq(prop, "align-items")) {
  5513. int a = 0;
  5514. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5515. a = 1;
  5516. else if (contains_ci_len(s + vstart, vend - vstart,
  5517. "flex-end") ||
  5518. contains_ci_len(s + vstart, vend - vstart,
  5519. "end"))
  5520. a = 2;
  5521. else if (contains_ci_len(s + vstart, vend - vstart,
  5522. "start") ||
  5523. contains_ci_len(s + vstart, vend - vstart,
  5524. "baseline"))
  5525. a = 3; /* not stretched */
  5526. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5527. } else if (str_eq(prop, "position")) {
  5528. int p = 0;
  5529. if (contains_ci_len(s + vstart, vend - vstart,
  5530. "absolute"))
  5531. p = 1;
  5532. else if (contains_ci_len(s + vstart, vend - vstart,
  5533. "fixed"))
  5534. p = 2;
  5535. else if (contains_ci_len(s + vstart, vend - vstart,
  5536. "relative"))
  5537. p = 3;
  5538. else if (contains_ci_len(s + vstart, vend - vstart,
  5539. "sticky"))
  5540. p = 4;
  5541. decl_add(out, nout, PROP_POS, p, important);
  5542. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5543. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5544. int which = str_eq(prop, "left") ? 0 :
  5545. str_eq(prop, "top") ? 1 :
  5546. str_eq(prop, "right") ? 2 : 3;
  5547. unsigned int pos = 0;
  5548. if (!(vend - vstart >= 4 &&
  5549. ci_eq_n(s + vstart, "auto", 4))) {
  5550. int v = parse_len(s + vstart, vend - vstart, &pos);
  5551. if (v > 32000)
  5552. v = 32000;
  5553. else if (v < -32000)
  5554. v = -32000;
  5555. decl_add(out, nout,
  5556. (unsigned char)(which == 0 ? PROP_LEFT :
  5557. which == 1 ? PROP_TOP :
  5558. which == 2 ? PROP_RIGHT :
  5559. PROP_BOTTOM),
  5560. v, important);
  5561. decl_add(out, nout,
  5562. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5563. which == 1 ? PROP_TOP_SET :
  5564. which == 2 ? PROP_RIGHT_SET :
  5565. PROP_BOTTOM_SET),
  5566. 1, important);
  5567. }
  5568. } else if (str_eq(prop, "z-index")) {
  5569. unsigned int q = vstart;
  5570. int sign = 1;
  5571. int v = 0;
  5572. int any = 0;
  5573. if (q < vend && s[q] == '-') {
  5574. sign = -1;
  5575. q++;
  5576. }
  5577. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5578. v = v * 10 + (s[q] - '0');
  5579. any = 1;
  5580. q++;
  5581. }
  5582. if (any) {
  5583. v *= sign;
  5584. if (v > 32000)
  5585. v = 32000;
  5586. else if (v < -32000)
  5587. v = -32000;
  5588. decl_add(out, nout, PROP_ZINDEX, v, important);
  5589. decl_add(out, nout, PROP_ZSET, 1, important);
  5590. } else if (vend - vstart >= 4 &&
  5591. ci_eq_n(s + vstart, "auto", 4)) {
  5592. decl_add(out, nout, PROP_ZSET, 0, important);
  5593. }
  5594. } else if (str_eq(prop, "transform")) {
  5595. const char *v = s + vstart;
  5596. unsigned int vl = vend - vstart;
  5597. int tx = 0, ty = 0;
  5598. int is_x = contains_ci_len(v, vl, "translatex");
  5599. int is_y = contains_ci_len(v, vl, "translatey");
  5600. unsigned int off = 0, arg;
  5601. if (contains_ci_len(v, vl, "translate")) {
  5602. while (off < vl && v[off] != '(')
  5603. off++;
  5604. if (off < vl)
  5605. off++;
  5606. if (!is_y)
  5607. tx = parse_len(v, vl, &off);
  5608. if (!is_x) {
  5609. while (off < vl &&
  5610. (is_space(v[off]) || v[off] == ','))
  5611. off++;
  5612. ty = parse_len(v, vl, &off);
  5613. }
  5614. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5615. important);
  5616. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5617. important);
  5618. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5619. important);
  5620. }
  5621. if (find_fn(v, vl, "scale", &arg)) {
  5622. unsigned int q = arg;
  5623. signed short sx = 100, sy = 100;
  5624. if (parse_fixed_100(v, vl, &q, &sx)) {
  5625. unsigned int r = q;
  5626. while (r < vl &&
  5627. (is_space(v[r]) || v[r] == ','))
  5628. r++;
  5629. if (!parse_fixed_100(v, vl, &r, &sy))
  5630. sy = sx;
  5631. decl_add(out, nout, PROP_SCALE_X, sx,
  5632. important);
  5633. decl_add(out, nout, PROP_SCALE_Y, sy,
  5634. important);
  5635. decl_add(out, nout,
  5636. PROP_TRANSFORM_PAINT, 1,
  5637. important);
  5638. decl_add(out, nout,
  5639. PROP_TRANSFORM_SET, 1,
  5640. important);
  5641. }
  5642. }
  5643. if (find_fn(v, vl, "rotate", &arg)) {
  5644. unsigned int q = arg;
  5645. signed short deg;
  5646. if (parse_fixed_100(v, vl, &q, &deg)) {
  5647. decl_add(out, nout, PROP_ROTATE, deg,
  5648. important);
  5649. decl_add(out, nout,
  5650. PROP_TRANSFORM_PAINT, 1,
  5651. important);
  5652. decl_add(out, nout,
  5653. PROP_TRANSFORM_SET, 1,
  5654. important);
  5655. }
  5656. }
  5657. } else if (str_eq(prop, "transform-origin")) {
  5658. add_transform_origin(out, nout, s + vstart,
  5659. vend - vstart, important);
  5660. } else if (str_eq(prop, "grid-template-columns") ||
  5661. str_eq(prop, "grid-template")) {
  5662. const char *v = s + vstart;
  5663. unsigned int vl = vend - vstart;
  5664. unsigned short val[2];
  5665. unsigned char kind[2];
  5666. int n, q;
  5667. if (str_eq(prop, "grid-template")) {
  5668. for (q = 0; q < (int)vl; q++)
  5669. if (v[q] == '/') {
  5670. q++;
  5671. while (q < (int)vl && is_space(v[q]))
  5672. q++;
  5673. v += q;
  5674. vl -= (unsigned int)q;
  5675. break;
  5676. }
  5677. }
  5678. if (grid_repeat_count(v, vl) >= 1) {
  5679. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5680. * Tailwind's grid-cols-N. N equal columns
  5681. * (GRID_FIXED + N); one column is plain block
  5682. * flow, which also resets a smaller breakpoint's
  5683. * count. */
  5684. int cnt = grid_repeat_count(v, vl);
  5685. decl_add(out, nout, PROP_GRID_AUTO,
  5686. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5687. important);
  5688. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5689. important);
  5690. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5691. contains_ci_len(v, vl, "auto-fill")) {
  5692. unsigned int mp = 0;
  5693. int found = 0;
  5694. unsigned short mw = 200;
  5695. unsigned char mk = 1;
  5696. for (q = 0; q + 7 <= (int)vl; q++)
  5697. if (ci_eq_n(v + q, "minmax(", 7)) {
  5698. mp = (unsigned int)q + 7;
  5699. found = 1;
  5700. break;
  5701. }
  5702. if (found)
  5703. parse_grid_track(v, vl, &mp, &mw, &mk);
  5704. if (mk != 1 || mw < 80)
  5705. mw = 200;
  5706. if (mw > 480)
  5707. mw = 480;
  5708. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5709. decl_add(out, nout, PROP_WIDTH, mw, important);
  5710. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5711. } else {
  5712. n = parse_grid_cols(v, vl, val, kind);
  5713. if (n > 0) {
  5714. decl_add(out, nout, PROP_WIDTH, val[0],
  5715. important);
  5716. decl_add(out, nout, PROP_WIDTH_KIND,
  5717. kind[0], important);
  5718. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5719. important);
  5720. }
  5721. if (n > 1) {
  5722. decl_add(out, nout, PROP_MAXW, val[1],
  5723. important);
  5724. decl_add(out, nout, PROP_MAXW_KIND,
  5725. kind[1], important);
  5726. }
  5727. }
  5728. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5729. /* From the value, not the block start: gap was
  5730. * ignored on nearly every site. */
  5731. unsigned int gp = vstart;
  5732. unsigned short gv = 0;
  5733. unsigned char gk = 1;
  5734. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5735. if (gv > 120)
  5736. gv = 120;
  5737. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5738. }
  5739. } else if (str_eq(prop, "grid-area")) {
  5740. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5741. if (slot)
  5742. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5743. } else if (str_eq(prop, "width") ||
  5744. str_eq(prop, "max-width") ||
  5745. str_eq(prop, "min-width") ||
  5746. str_eq(prop, "height") ||
  5747. str_eq(prop, "max-height") ||
  5748. str_eq(prop, "min-height")) {
  5749. unsigned short v;
  5750. unsigned char kind;
  5751. if (vend - vstart >= 5 &&
  5752. ci_eq_n(s + vstart, "calc(", 5)) {
  5753. int pct, px;
  5754. if (parse_calc(s + vstart + 5,
  5755. vend - vstart - 5, &pct, &px)) {
  5756. if (str_eq(prop, "width")) {
  5757. decl_add(out, nout, PROP_WIDTH,
  5758. 0, important);
  5759. decl_add(out, nout,
  5760. PROP_WIDTH_KIND, 3,
  5761. important);
  5762. decl_add(out, nout,
  5763. PROP_WIDTH_CALC_PCT,
  5764. pct, important);
  5765. decl_add(out, nout,
  5766. PROP_WIDTH_CALC_PX,
  5767. px, important);
  5768. } else if (str_eq(prop, "max-width")) {
  5769. decl_add(out, nout, PROP_MAXW,
  5770. 0, important);
  5771. decl_add(out, nout,
  5772. PROP_MAXW_KIND, 3,
  5773. important);
  5774. decl_add(out, nout,
  5775. PROP_MAXW_CALC_PCT,
  5776. pct, important);
  5777. decl_add(out, nout,
  5778. PROP_MAXW_CALC_PX,
  5779. px, important);
  5780. } else if (str_eq(prop, "min-width")) {
  5781. decl_add(out, nout, PROP_MINW,
  5782. 0, important);
  5783. decl_add(out, nout,
  5784. PROP_MINW_KIND, 3,
  5785. important);
  5786. decl_add(out, nout,
  5787. PROP_MINW_CALC_PCT,
  5788. pct, important);
  5789. decl_add(out, nout,
  5790. PROP_MINW_CALC_PX,
  5791. px, important);
  5792. } else if (str_eq(prop, "height")) {
  5793. decl_add(out, nout, PROP_HEIGHT,
  5794. 0, important);
  5795. decl_add(out, nout,
  5796. PROP_HEIGHT_KIND, 3,
  5797. important);
  5798. decl_add(out, nout,
  5799. PROP_HEIGHT_CALC_PCT,
  5800. pct, important);
  5801. decl_add(out, nout,
  5802. PROP_HEIGHT_CALC_PX,
  5803. px, important);
  5804. } else if (str_eq(prop, "max-height")) {
  5805. decl_add(out, nout, PROP_MAXH,
  5806. 0, important);
  5807. decl_add(out, nout,
  5808. PROP_MAXH_KIND, 3,
  5809. important);
  5810. decl_add(out, nout,
  5811. PROP_MAXH_CALC_PCT,
  5812. pct, important);
  5813. decl_add(out, nout,
  5814. PROP_MAXH_CALC_PX,
  5815. px, important);
  5816. } else {
  5817. decl_add(out, nout, PROP_MINH,
  5818. 0, important);
  5819. decl_add(out, nout,
  5820. PROP_MINH_KIND, 3,
  5821. important);
  5822. decl_add(out, nout,
  5823. PROP_MINH_CALC_PCT,
  5824. pct, important);
  5825. decl_add(out, nout,
  5826. PROP_MINH_CALC_PX,
  5827. px, important);
  5828. }
  5829. }
  5830. continue;
  5831. }
  5832. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  5833. if (str_eq(prop, "width")) {
  5834. decl_add(out, nout, PROP_WIDTH, v, important);
  5835. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  5836. important);
  5837. } else if (str_eq(prop, "max-width")) {
  5838. decl_add(out, nout, PROP_MAXW, v, important);
  5839. decl_add(out, nout, PROP_MAXW_KIND, kind,
  5840. important);
  5841. } else if (str_eq(prop, "min-width")) {
  5842. decl_add(out, nout, PROP_MINW, v, important);
  5843. decl_add(out, nout, PROP_MINW_KIND, kind,
  5844. important);
  5845. } else if (str_eq(prop, "max-height")) {
  5846. decl_add(out, nout, PROP_MAXH, v, important);
  5847. decl_add(out, nout, PROP_MAXH_KIND, kind,
  5848. important);
  5849. } else if (str_eq(prop, "min-height")) {
  5850. decl_add(out, nout, PROP_MINH, v, important);
  5851. decl_add(out, nout, PROP_MINH_KIND, kind,
  5852. important);
  5853. } else {
  5854. decl_add(out, nout, PROP_HEIGHT, v, important);
  5855. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  5856. important);
  5857. }
  5858. }
  5859. } else if (str_eq(prop, "content")) {
  5860. decl_add(out, nout, PROP_CONTENT,
  5861. css_collect_only ? 0 :
  5862. parse_content(d, s + vstart, vend - vstart),
  5863. important);
  5864. } else if (str_eq(prop, "aspect-ratio")) {
  5865. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  5866. * preferred height of an auto-height box. */
  5867. unsigned int q = vstart;
  5868. unsigned int a[2] = { 0, 1 }, k;
  5869. for (k = 0; k < 2; k++) {
  5870. unsigned int whole = 0, frac = 0, sc = 1;
  5871. while (q < vend && is_space(s[q]))
  5872. q++;
  5873. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  5874. s[q] != '.'))
  5875. break;
  5876. while (q < vend && s[q] >= '0' && s[q] <= '9')
  5877. whole = whole * 10 +
  5878. (unsigned int)(s[q++] - '0');
  5879. if (q < vend && s[q] == '.') {
  5880. q++;
  5881. while (q < vend && s[q] >= '0' &&
  5882. s[q] <= '9') {
  5883. if (sc < 100) {
  5884. frac = frac * 10 +
  5885. (unsigned int)(s[q] - '0');
  5886. sc *= 10;
  5887. }
  5888. q++;
  5889. }
  5890. }
  5891. a[k] = whole * 100 + frac * (100 / sc);
  5892. while (q < vend && is_space(s[q]))
  5893. q++;
  5894. if (k == 0 && (q >= vend || s[q] != '/')) {
  5895. a[1] = 100;
  5896. break;
  5897. }
  5898. q++;
  5899. }
  5900. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5901. a[0] = a[1] = 0;
  5902. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5903. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5904. } else if (str_eq(prop, "box-sizing")) {
  5905. int bb = (vend - vstart >= 10 &&
  5906. ci_eq_n(s + vstart, "border-box", 10));
  5907. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5908. } else if (str_eq(prop, "line-height")) {
  5909. unsigned short px;
  5910. unsigned char pct;
  5911. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5912. if (px) {
  5913. decl_add(out, nout, PROP_LH_PX, px, important);
  5914. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5915. } else {
  5916. decl_add(out, nout, PROP_LH_PX, 0, important);
  5917. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5918. }
  5919. }
  5920. } else if (str_eq(prop, "list-style") ||
  5921. str_eq(prop, "list-style-type")) {
  5922. int ls = BR_LIST_AUTO;
  5923. if (contains_ci_len(s + vstart, vend - vstart,
  5924. "none"))
  5925. ls = BR_LIST_NONE;
  5926. else if (contains_ci_len(s + vstart, vend - vstart,
  5927. "circle"))
  5928. ls = BR_LIST_CIRCLE;
  5929. else if (contains_ci_len(s + vstart, vend - vstart,
  5930. "square"))
  5931. ls = BR_LIST_SQUARE;
  5932. else if (contains_ci_len(s + vstart, vend - vstart,
  5933. "lower-alpha") ||
  5934. contains_ci_len(s + vstart, vend - vstart,
  5935. "lower-latin"))
  5936. ls = BR_LIST_LOWER_ALPHA;
  5937. else if (contains_ci_len(s + vstart, vend - vstart,
  5938. "upper-alpha") ||
  5939. contains_ci_len(s + vstart, vend - vstart,
  5940. "upper-latin"))
  5941. ls = BR_LIST_UPPER_ALPHA;
  5942. else if (contains_ci_len(s + vstart, vend - vstart,
  5943. "lower-roman"))
  5944. ls = BR_LIST_LOWER_ROMAN;
  5945. else if (contains_ci_len(s + vstart, vend - vstart,
  5946. "upper-roman"))
  5947. ls = BR_LIST_UPPER_ROMAN;
  5948. else if (contains_ci_len(s + vstart, vend - vstart,
  5949. "decimal"))
  5950. ls = BR_LIST_DECIMAL;
  5951. else if (contains_ci_len(s + vstart, vend - vstart,
  5952. "disc"))
  5953. ls = BR_LIST_DISC;
  5954. decl_add(out, nout, PROP_LIST_NONE,
  5955. ls == BR_LIST_NONE, important);
  5956. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5957. } else if (str_eq(prop, "border-radius") ||
  5958. str_eq(prop, "border-top-left-radius") ||
  5959. str_eq(prop, "border-top-right-radius") ||
  5960. str_eq(prop, "border-bottom-right-radius") ||
  5961. str_eq(prop, "border-bottom-left-radius")) {
  5962. unsigned short v[4];
  5963. unsigned char k[4];
  5964. unsigned int pos = 0;
  5965. int n = 0;
  5966. while (pos < vend - vstart && n < 4) {
  5967. while (pos < vend - vstart &&
  5968. is_space(s[vstart + pos]))
  5969. pos++;
  5970. if (pos >= vend - vstart ||
  5971. s[vstart + pos] == '/')
  5972. break;
  5973. if (!parse_radius_value(s + vstart,
  5974. vend - vstart, &pos,
  5975. &v[n], &k[n]))
  5976. break;
  5977. n++;
  5978. }
  5979. if (n) {
  5980. unsigned short r[4];
  5981. unsigned char rk[4];
  5982. int i;
  5983. if (n == 1) {
  5984. for (i = 0; i < 4; i++) {
  5985. r[i] = v[0];
  5986. rk[i] = k[0];
  5987. }
  5988. } else if (n == 2) {
  5989. r[0] = r[2] = v[0];
  5990. r[1] = r[3] = v[1];
  5991. rk[0] = rk[2] = k[0];
  5992. rk[1] = rk[3] = k[1];
  5993. } else if (n == 3) {
  5994. r[0] = v[0];
  5995. r[1] = r[3] = v[1];
  5996. r[2] = v[2];
  5997. rk[0] = k[0];
  5998. rk[1] = rk[3] = k[1];
  5999. rk[2] = k[2];
  6000. } else {
  6001. for (i = 0; i < 4; i++) {
  6002. r[i] = v[i];
  6003. rk[i] = k[i];
  6004. }
  6005. }
  6006. for (i = 0; i < 4; i++)
  6007. r[i] = rk[i] == 2 ? 255 :
  6008. (r[i] > 255 ? 255 : r[i]);
  6009. if (str_eq(prop, "border-top-left-radius")) {
  6010. decl_add(out, nout, PROP_RADIUS_TL,
  6011. (short)r[0], important);
  6012. } else if (str_eq(prop,
  6013. "border-top-right-radius")) {
  6014. decl_add(out, nout, PROP_RADIUS_TR,
  6015. (short)r[0], important);
  6016. } else if (str_eq(prop,
  6017. "border-bottom-right-radius")) {
  6018. decl_add(out, nout, PROP_RADIUS_BR,
  6019. (short)r[0], important);
  6020. } else if (str_eq(prop,
  6021. "border-bottom-left-radius")) {
  6022. decl_add(out, nout, PROP_RADIUS_BL,
  6023. (short)r[0], important);
  6024. } else {
  6025. decl_add(out, nout, PROP_RADIUS_TL,
  6026. (short)r[0], important);
  6027. decl_add(out, nout, PROP_RADIUS_TR,
  6028. (short)r[1], important);
  6029. decl_add(out, nout, PROP_RADIUS_BR,
  6030. (short)r[2], important);
  6031. decl_add(out, nout, PROP_RADIUS_BL,
  6032. (short)r[3], important);
  6033. }
  6034. }
  6035. } else if (str_eq(prop, "border") ||
  6036. str_eq(prop, "border-top") ||
  6037. str_eq(prop, "border-right") ||
  6038. str_eq(prop, "border-bottom") ||
  6039. str_eq(prop, "border-left") ||
  6040. str_eq(prop, "border-color") ||
  6041. str_eq(prop, "border-width") ||
  6042. str_eq(prop, "border-style") ||
  6043. str_eq(prop, "border-top-width") ||
  6044. str_eq(prop, "border-right-width") ||
  6045. str_eq(prop, "border-bottom-width") ||
  6046. str_eq(prop, "border-left-width") ||
  6047. str_eq(prop, "border-top-color") ||
  6048. str_eq(prop, "border-right-color") ||
  6049. str_eq(prop, "border-bottom-color") ||
  6050. str_eq(prop, "border-left-color") ||
  6051. str_eq(prop, "border-top-style") ||
  6052. str_eq(prop, "border-right-style") ||
  6053. str_eq(prop, "border-bottom-style") ||
  6054. str_eq(prop, "border-left-style")) {
  6055. const char *v = s + vstart;
  6056. unsigned int vl = vend - vstart;
  6057. unsigned int pos = 0, ts, te;
  6058. int widths[4], colors[4], styles[4], trans[4];
  6059. int nw = 0, nc = 0, ns = 0;
  6060. int side = -1;
  6061. int is_wl = str_eq(prop, "border-width");
  6062. int is_cl = str_eq(prop, "border-color");
  6063. int is_sl = str_eq(prop, "border-style");
  6064. int i;
  6065. if (str_eq(prop, "border-top")) side = 0;
  6066. else if (str_eq(prop, "border-right")) side = 1;
  6067. else if (str_eq(prop, "border-bottom")) side = 2;
  6068. else if (str_eq(prop, "border-left")) side = 3;
  6069. else if (str_eq(prop, "border-top-width")) {
  6070. side = 0; is_wl = 2;
  6071. } else if (str_eq(prop, "border-right-width")) {
  6072. side = 1; is_wl = 2;
  6073. } else if (str_eq(prop, "border-bottom-width")) {
  6074. side = 2; is_wl = 2;
  6075. } else if (str_eq(prop, "border-left-width")) {
  6076. side = 3; is_wl = 2;
  6077. } else if (str_eq(prop, "border-top-color")) {
  6078. side = 0; is_cl = 2;
  6079. } else if (str_eq(prop, "border-right-color")) {
  6080. side = 1; is_cl = 2;
  6081. } else if (str_eq(prop, "border-bottom-color")) {
  6082. side = 2; is_cl = 2;
  6083. } else if (str_eq(prop, "border-left-color")) {
  6084. side = 3; is_cl = 2;
  6085. } else if (str_eq(prop, "border-top-style")) {
  6086. side = 0; is_sl = 2;
  6087. } else if (str_eq(prop, "border-right-style")) {
  6088. side = 1; is_sl = 2;
  6089. } else if (str_eq(prop, "border-bottom-style")) {
  6090. side = 2; is_sl = 2;
  6091. } else if (str_eq(prop, "border-left-style")) {
  6092. side = 3; is_sl = 2;
  6093. }
  6094. if (is_wl) {
  6095. while (nw < 4 && next_token(v, vl, &pos,
  6096. &ts, &te)) {
  6097. int x;
  6098. if (!parse_len_token(v + ts, te - ts,
  6099. &x))
  6100. break;
  6101. widths[nw++] = x;
  6102. }
  6103. if (nw) {
  6104. if (is_wl == 1)
  6105. expand_four(widths, nw);
  6106. if (side >= 0 && is_wl == 2) {
  6107. decl_add(out, nout,
  6108. PROP_BW_T + side,
  6109. widths[0], important);
  6110. } else {
  6111. for (i = 0; i < 4; i++)
  6112. decl_add(out, nout,
  6113. PROP_BW_T + i,
  6114. widths[i],
  6115. important);
  6116. }
  6117. }
  6118. } else if (is_cl) {
  6119. unsigned short c;
  6120. int t;
  6121. while (nc < 4 && next_token(v, vl, &pos,
  6122. &ts, &te)) {
  6123. t = color_is_transparent(v + ts,
  6124. te - ts);
  6125. if (!t && !parse_color(v + ts,
  6126. te - ts, &c))
  6127. break;
  6128. colors[nc] = t ? 0 : c;
  6129. trans[nc] = t;
  6130. nc++;
  6131. }
  6132. if (nc) {
  6133. if (is_cl == 1) {
  6134. expand_four(colors, nc);
  6135. expand_four(trans, nc);
  6136. }
  6137. if (side >= 0 && is_cl == 2) {
  6138. decl_add(out, nout,
  6139. PROP_BC_T + side,
  6140. colors[0], important);
  6141. decl_add(out, nout,
  6142. PROP_BT_T + side,
  6143. trans[0], important);
  6144. } else {
  6145. for (i = 0; i < 4; i++) {
  6146. decl_add(out, nout,
  6147. PROP_BC_T + i,
  6148. colors[i],
  6149. important);
  6150. decl_add(out, nout,
  6151. PROP_BT_T + i,
  6152. trans[i],
  6153. important);
  6154. }
  6155. }
  6156. }
  6157. } else if (is_sl) {
  6158. while (ns < 4 && next_token(v, vl, &pos,
  6159. &ts, &te)) {
  6160. int stv;
  6161. if ((te - ts == 4 &&
  6162. ci_eq_n(v + ts, "none", 4)) ||
  6163. (te - ts == 6 &&
  6164. ci_eq_n(v + ts, "hidden", 6)))
  6165. stv = 0;
  6166. else if (te - ts == 6 &&
  6167. ci_eq_n(v + ts,
  6168. "dashed", 6))
  6169. stv = 2;
  6170. else if (te - ts == 6 &&
  6171. ci_eq_n(v + ts,
  6172. "dotted", 6))
  6173. stv = 3;
  6174. else
  6175. stv = 1;
  6176. styles[ns++] = stv;
  6177. }
  6178. if (ns) {
  6179. if (is_sl == 1)
  6180. expand_four(styles, ns);
  6181. if (is_sl == 2 && side >= 0) {
  6182. if (!styles[0])
  6183. decl_add(out, nout,
  6184. PROP_BW_T + side,
  6185. 0, important);
  6186. else
  6187. decl_add(out, nout,
  6188. PROP_BS_T + side,
  6189. styles[0] - 1,
  6190. important);
  6191. } else {
  6192. for (i = 0; i < 4; i++) {
  6193. if (!styles[i])
  6194. decl_add(out, nout,
  6195. PROP_BW_T + i,
  6196. 0,
  6197. important);
  6198. else
  6199. decl_add(out, nout,
  6200. PROP_BS_T + i,
  6201. styles[i] - 1,
  6202. important);
  6203. }
  6204. }
  6205. }
  6206. } else {
  6207. int bw = -1, bc = -1, style_none = 0;
  6208. int bstyle = -1, btrans = 0;
  6209. while (next_token(v, vl, &pos, &ts, &te)) {
  6210. unsigned short c;
  6211. int x;
  6212. if ((te - ts == 4 &&
  6213. ci_eq_n(v + ts, "none", 4)) ||
  6214. (te - ts == 6 &&
  6215. ci_eq_n(v + ts, "hidden", 6))) {
  6216. style_none = 1;
  6217. } else if (te - ts == 6 &&
  6218. ci_eq_n(v + ts, "dashed", 6)) {
  6219. bstyle = BR_BORDER_DASHED;
  6220. } else if (te - ts == 6 &&
  6221. ci_eq_n(v + ts, "dotted", 6)) {
  6222. bstyle = BR_BORDER_DOTTED;
  6223. } else if (te - ts == 5 &&
  6224. ci_eq_n(v + ts, "solid", 5)) {
  6225. bstyle = BR_BORDER_SOLID;
  6226. } else if (parse_len_token(v + ts,
  6227. te - ts, &x)) {
  6228. bw = x;
  6229. } else if (color_is_transparent(v + ts,
  6230. te - ts)) {
  6231. bc = 0;
  6232. btrans = 1;
  6233. } else if (parse_color(v + ts,
  6234. te - ts, &c)) {
  6235. bc = c;
  6236. btrans = 0;
  6237. }
  6238. }
  6239. if (style_none)
  6240. bw = 0;
  6241. if (bc >= 0 && bw < 0)
  6242. bw = 1;
  6243. if (side < 0) {
  6244. if (bw >= 0)
  6245. decl_add(out, nout,
  6246. PROP_BORDER_W, bw,
  6247. important);
  6248. if (bc >= 0)
  6249. decl_add(out, nout,
  6250. PROP_BORDER_COLOR, bc,
  6251. important);
  6252. if (bc >= 0)
  6253. for (i = 0; i < 4; i++)
  6254. decl_add(out, nout,
  6255. PROP_BT_T + i,
  6256. btrans,
  6257. important);
  6258. if (bstyle >= 0)
  6259. for (i = 0; i < 4; i++)
  6260. decl_add(out, nout,
  6261. PROP_BS_T + i,
  6262. bstyle,
  6263. important);
  6264. } else {
  6265. if (bw >= 0)
  6266. decl_add(out, nout,
  6267. PROP_BW_T + side, bw,
  6268. important);
  6269. if (bc >= 0)
  6270. decl_add(out, nout,
  6271. PROP_BC_T + side, bc,
  6272. important);
  6273. if (bc >= 0)
  6274. decl_add(out, nout,
  6275. PROP_BT_T + side,
  6276. btrans, important);
  6277. if (bstyle >= 0)
  6278. decl_add(out, nout,
  6279. PROP_BS_T + side,
  6280. bstyle, important);
  6281. }
  6282. }
  6283. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6284. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6285. unsigned int pos = 0;
  6286. int is_pad = prop[0] == 'p';
  6287. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6288. while (pos < vend - vstart && nv < 4) {
  6289. units[nv] = 0;
  6290. if (parse_margin_calc(s + vstart,
  6291. vend - vstart, &pos,
  6292. &vals[nv], &cpx[nv])) {
  6293. units[nv] = 5;
  6294. } else {
  6295. vals[nv] = parse_margin_len(
  6296. s + vstart, vend - vstart,
  6297. &pos, &units[nv]);
  6298. }
  6299. nv++;
  6300. while (pos < vend - vstart &&
  6301. is_space(s[vstart + pos]))
  6302. pos++;
  6303. }
  6304. if (nv == 1) {
  6305. vals[1] = vals[2] = vals[3] = vals[0];
  6306. units[1] = units[2] = units[3] = units[0];
  6307. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6308. } else if (nv == 2) {
  6309. vals[2] = vals[0];
  6310. vals[3] = vals[1];
  6311. units[2] = units[0];
  6312. units[3] = units[1];
  6313. cpx[2] = cpx[0];
  6314. cpx[3] = cpx[1];
  6315. } else if (nv == 3) {
  6316. vals[3] = vals[1];
  6317. units[3] = units[1];
  6318. cpx[3] = cpx[1];
  6319. }
  6320. if (nv > 0) {
  6321. int k;
  6322. for (k = 0; k < 4; k++) {
  6323. int v = vals[k];
  6324. int u = units[k];
  6325. int au = u == 4;
  6326. if (au)
  6327. v = 0;
  6328. if (is_pad && au)
  6329. u = 0;
  6330. decl_add(out, nout,
  6331. (unsigned char)(base + k), v,
  6332. important);
  6333. decl_add(out, nout,
  6334. (unsigned char)
  6335. ((is_pad ? PROP_PT_U :
  6336. PROP_MT_U) + k),
  6337. u, important);
  6338. if (u == 5)
  6339. decl_add(out, nout,
  6340. (unsigned char)
  6341. ((is_pad ? PROP_PT_CPX :
  6342. PROP_MT_CPX) + k),
  6343. cpx[k], important);
  6344. if (!is_pad && (k == 1 || k == 3))
  6345. decl_add(out, nout,
  6346. k == 1 ? PROP_MR_AUTO :
  6347. PROP_ML_AUTO,
  6348. au, important);
  6349. }
  6350. }
  6351. } else if (str_eq(prop, "margin-top") ||
  6352. str_eq(prop, "margin-right") ||
  6353. str_eq(prop, "margin-bottom") ||
  6354. str_eq(prop, "margin-left") ||
  6355. str_eq(prop, "padding-top") ||
  6356. str_eq(prop, "padding-right") ||
  6357. str_eq(prop, "padding-bottom") ||
  6358. str_eq(prop, "padding-left")) {
  6359. unsigned int pos = 0;
  6360. int unit = 0;
  6361. int v = 0, cpx = 0;
  6362. unsigned char p = PROP_MT;
  6363. const char *side;
  6364. int au;
  6365. if (parse_margin_calc(s + vstart, vend - vstart,
  6366. &pos, &v, &cpx))
  6367. unit = 5;
  6368. else
  6369. v = parse_margin_len(s + vstart,
  6370. vend - vstart, &pos,
  6371. &unit);
  6372. au = unit == 4;
  6373. if (au)
  6374. v = 0;
  6375. if (prop[0] == 'p') {
  6376. p = PROP_PT;
  6377. if (au)
  6378. unit = 0;
  6379. }
  6380. side = prop + (prop[0] == 'p' ? 8 : 7);
  6381. if (str_eq(side, "right"))
  6382. p = (unsigned char)(p + 1);
  6383. else if (str_eq(side, "bottom"))
  6384. p = (unsigned char)(p + 2);
  6385. else if (str_eq(side, "left"))
  6386. p = (unsigned char)(p + 3);
  6387. decl_add(out, nout, p, v, important);
  6388. decl_add(out, nout,
  6389. (unsigned char)((prop[0] == 'p' ?
  6390. PROP_PT_U : PROP_MT_U) +
  6391. (p - (prop[0] == 'p' ?
  6392. PROP_PT : PROP_MT))),
  6393. unit, important);
  6394. if (unit == 5)
  6395. decl_add(out, nout,
  6396. (unsigned char)((prop[0] == 'p' ?
  6397. PROP_PT_CPX : PROP_MT_CPX) +
  6398. (p - (prop[0] == 'p' ?
  6399. PROP_PT : PROP_MT))),
  6400. cpx, important);
  6401. if (str_eq(side, "right"))
  6402. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6403. else if (str_eq(side, "left"))
  6404. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6405. }
  6406. }
  6407. }
  6408. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6409. * nested blocks. This is the mobile/base rules path: media enhancements are
  6410. * deliberately ignored for now. */
  6411. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6412. unsigned int i)
  6413. {
  6414. unsigned int j = i;
  6415. while (j < len && s[j] != '{' && s[j] != ';')
  6416. j++;
  6417. if (j >= len)
  6418. return len;
  6419. if (s[j] == ';')
  6420. return j + 1;
  6421. j++;
  6422. return css_skip_nested(s, len, j);
  6423. }
  6424. static unsigned int viewport_w = 1024;
  6425. static unsigned short theme_text = 0x0000;
  6426. static unsigned short theme_bg = 0xFFFF;
  6427. static unsigned short theme_panel = 0xEF5D;
  6428. static unsigned short theme_link = 0x001B;
  6429. void br_set_theme(unsigned short text, unsigned short bg,
  6430. unsigned short panel, unsigned short link)
  6431. {
  6432. theme_text = text;
  6433. theme_bg = bg;
  6434. theme_panel = panel;
  6435. theme_link = link;
  6436. }
  6437. void br_set_viewport(unsigned int width)
  6438. {
  6439. viewport_w = width ? width : 1024;
  6440. }
  6441. static unsigned int media_len_px(const char *s, unsigned int len)
  6442. {
  6443. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6444. int em = 0;
  6445. while (i < len && is_space(s[i]))
  6446. i++;
  6447. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6448. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6449. i++;
  6450. }
  6451. if (i < len && s[i] == '.') {
  6452. i++;
  6453. /* Consume every fractional digit even when there are more
  6454. * than the three we keep: leaving the tail behind made the
  6455. * unit check miss "em" (74.9375em became 74px). */
  6456. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6457. if (scale < 1000) {
  6458. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6459. scale *= 10;
  6460. }
  6461. i++;
  6462. }
  6463. }
  6464. while (i < len && is_space(s[i]))
  6465. i++;
  6466. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6467. em = 1;
  6468. i += 2;
  6469. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6470. em = 1;
  6471. i += 3;
  6472. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6473. i += 2;
  6474. }
  6475. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6476. if (em)
  6477. return v * 16 / 100;
  6478. return v / 100;
  6479. }
  6480. /* One parenthesised media feature: min-width/max-width and the modern
  6481. * (width <= 600px) form. Unknown features are treated as true. */
  6482. static int media_feature_matches(const char *s, unsigned int len)
  6483. {
  6484. unsigned int i;
  6485. while (len && is_space(*s)) { s++; len--; }
  6486. while (len && is_space(s[len - 1])) len--;
  6487. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6488. /* There is no user-selectable colour scheme. Ignore both
  6489. * variants so a page's base palette is used; sites whose base
  6490. * is dark (like the project docs) then stay dark instead of
  6491. * flipping to their light media override. */
  6492. return 0;
  6493. }
  6494. {
  6495. /* Environment features with a known answer: a mouse-driven
  6496. * screen with normal contrast and no forced colours. Google
  6497. * puts `border:1px solid CanvasText` on its buttons inside
  6498. * `(forced-colors:active)`. */
  6499. static const struct { const char *name, *yes; } env[] = {
  6500. { "forced-colors", "none" },
  6501. { "-ms-high-contrast", "" },
  6502. { "prefers-contrast", "no-preference" },
  6503. { "inverted-colors", "none" },
  6504. { "any-hover", "hover" },
  6505. { "hover", "hover" },
  6506. { "any-pointer", "fine" },
  6507. { "pointer", "fine" },
  6508. { "prefers-reduced-motion", "no-preference" },
  6509. { "prefers-reduced-transparency", "no-preference" },
  6510. { "display-mode", "browser" },
  6511. };
  6512. unsigned int k;
  6513. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6514. unsigned int nl = s_len(env[k].name), j;
  6515. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6516. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6517. continue;
  6518. for (j = nl; j < len && s[j] != ':'; j++)
  6519. ;
  6520. if (j >= len) /* boolean form: (hover) */
  6521. return env[k].yes[0] &&
  6522. !ci_eq_n(env[k].yes, "none", 4);
  6523. j++;
  6524. while (j < len && is_space(s[j]))
  6525. j++;
  6526. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6527. ci_eq_n(s + j, env[k].yes, len - j);
  6528. }
  6529. }
  6530. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6531. for (i = 9; i < len && s[i] != ':'; i++)
  6532. ;
  6533. if (i < len)
  6534. return viewport_w >= media_len_px(s + i + 1,
  6535. len - i - 1);
  6536. return 1;
  6537. }
  6538. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6539. for (i = 9; i < len && s[i] != ':'; i++)
  6540. ;
  6541. if (i < len)
  6542. return viewport_w <= media_len_px(s + i + 1,
  6543. len - i - 1);
  6544. return 1;
  6545. }
  6546. for (i = 0; i < len; i++) {
  6547. if (s[i] == '<' || s[i] == '>') {
  6548. int max = s[i] == '<';
  6549. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6550. if (i + 1 < len && s[i + 1] == '=')
  6551. v = media_len_px(s + i + 2, len - i - 2);
  6552. return max ? viewport_w <= v : viewport_w >= v;
  6553. }
  6554. }
  6555. return 1;
  6556. }
  6557. static int media_query_matches(const char *s, unsigned int len)
  6558. {
  6559. unsigned int i;
  6560. if (contains_ci_len(s, len, "print") &&
  6561. !contains_ci_len(s, len, "screen"))
  6562. return 0;
  6563. if (contains_ci_len(s, len, "not "))
  6564. return 0;
  6565. for (i = 0; i < len; i++) {
  6566. if (s[i] == '(') {
  6567. unsigned int st = ++i;
  6568. while (i < len && s[i] != ')')
  6569. i++;
  6570. if (!media_feature_matches(s + st, i - st))
  6571. return 0;
  6572. }
  6573. }
  6574. return 1;
  6575. }
  6576. static int media_list_matches(const char *s, unsigned int len)
  6577. {
  6578. unsigned int i = 0;
  6579. while (i < len) {
  6580. unsigned int st = i;
  6581. while (i < len && s[i] != ',')
  6582. i++;
  6583. if (media_query_matches(s + st, i - st))
  6584. return 1;
  6585. i++;
  6586. }
  6587. return 0;
  6588. }
  6589. int br_media_matches(const char *query)
  6590. {
  6591. return media_list_matches(query, s_len(query));
  6592. }
  6593. static unsigned int rule_hash(const char *s, unsigned int len)
  6594. {
  6595. unsigned int h = 2166136261u, i;
  6596. for (i = 0; i < len; i++) {
  6597. char c = s[i];
  6598. if (c >= 'A' && c <= 'Z')
  6599. c = (char)(c - 'A' + 'a');
  6600. h ^= (unsigned char)c;
  6601. h *= 16777619u;
  6602. }
  6603. return h % BR_RULE_HASH;
  6604. }
  6605. /* Index a parsed rule by the most selective component of its rightmost
  6606. * compound selector. br_style_for() then only visits rules that can
  6607. * possibly match a node instead of scanning every rule. */
  6608. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6609. unsigned int len);
  6610. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6611. char *out, unsigned int cap, unsigned int *olen);
  6612. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6613. const char *key, unsigned int len)
  6614. {
  6615. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6616. * "md:flex", the class the element carries, not under "md". */
  6617. char cname[96], iname[96];
  6618. unsigned int i, clen = 0, ilen = 0;
  6619. unsigned short *head;
  6620. for (i = 0; i < len; i++) {
  6621. if (key[i] == '\\') {
  6622. i++;
  6623. continue;
  6624. }
  6625. if (key[i] == '(' || key[i] == '[') {
  6626. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6627. i = sel_skip_group(key, i, len) - 1;
  6628. continue;
  6629. }
  6630. if (key[i] == '.' || key[i] == '#') {
  6631. char tmp[96];
  6632. unsigned int n, e;
  6633. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6634. if (n && n + 1 < sizeof(tmp)) {
  6635. if (key[i] == '#') {
  6636. memcpy(iname, tmp, n);
  6637. ilen = n;
  6638. } else {
  6639. memcpy(cname, tmp, n);
  6640. clen = n;
  6641. }
  6642. }
  6643. i = e - 1;
  6644. }
  6645. }
  6646. if (ilen)
  6647. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6648. else if (clen)
  6649. head = &d->rule_bucket[rule_hash(cname, clen)];
  6650. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6651. head = &d->rule_tag[BR_TAG_HTML];
  6652. else if (len && is_ident(key[0])) {
  6653. char name[16];
  6654. unsigned int n = 0, tag;
  6655. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6656. name[n] = key[n];
  6657. n++;
  6658. }
  6659. name[n] = '\0';
  6660. tag = br_tag_from_name(name);
  6661. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6662. head = &d->rule_tag[tag];
  6663. else
  6664. head = &d->rule_any;
  6665. } else {
  6666. head = &d->rule_any;
  6667. }
  6668. d->rule_next[idx] = *head;
  6669. *head = (unsigned short)idx;
  6670. }
  6671. #define SEL_PARTS 16
  6672. struct sel_part {
  6673. unsigned short st, en;
  6674. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6675. };
  6676. static unsigned int selector_specificity(const char *s, unsigned int len);
  6677. static int css_selector_supported(const char *s, unsigned int len);
  6678. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6679. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6680. unsigned int *st, unsigned int *en);
  6681. static int css_collect_only;
  6682. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6683. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6684. * selector with any other pseudo-element. */
  6685. static int css_pseudo_element(char *sel, unsigned int *len)
  6686. {
  6687. static const struct {
  6688. const char *s;
  6689. unsigned char l, pe;
  6690. } pe[] = {
  6691. { "::before", 8, 1 }, { "::after", 7, 2 },
  6692. { ":before", 7, 1 }, { ":after", 6, 2 },
  6693. };
  6694. unsigned int k, n = *len;
  6695. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6696. unsigned int l = pe[k].l;
  6697. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6698. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6699. continue; /* matched the tail of "::x" */
  6700. n -= l;
  6701. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6702. sel[n - 1] == '+' || sel[n - 1] == '~')
  6703. sel[n++] = '*';
  6704. sel[n] = '\0';
  6705. *len = n;
  6706. return pe[k].pe;
  6707. }
  6708. }
  6709. for (k = 0; k + 1 < n; k++)
  6710. if (sel[k] == ':' && sel[k + 1] == ':')
  6711. return -1;
  6712. return 0;
  6713. }
  6714. /* Does a declaration block use or define a custom property whose value
  6715. * depends on the element (defined by some non-root rule)? Such rules are
  6716. * re-expanded per element at cascade time. */
  6717. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6718. unsigned int len)
  6719. {
  6720. unsigned char f = 0;
  6721. unsigned int i, k;
  6722. for (i = 0; i + 6 < len; i++) {
  6723. unsigned int st, e, vi;
  6724. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6725. continue;
  6726. st = i + 4;
  6727. while (st < len && is_space(s[st]))
  6728. st++;
  6729. e = st;
  6730. while (e < len && s[e] != ',' && s[e] != ')' &&
  6731. !is_space(s[e]))
  6732. e++;
  6733. vi = css_var_find(d, s + st, e - st);
  6734. if (vi < BR_MAX_VARS &&
  6735. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6736. f |= BR_RULE_USES_SCOPED;
  6737. break;
  6738. }
  6739. }
  6740. css_local_collect(s, len);
  6741. for (k = 0; k < css_nlocal; k++) {
  6742. unsigned int vi = css_var_find(d, css_local[k].name,
  6743. css_local[k].nlen);
  6744. if (vi < BR_MAX_VARS &&
  6745. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6746. f |= BR_RULE_DEFINES_SCOPED;
  6747. }
  6748. css_nlocal = 0;
  6749. return f;
  6750. }
  6751. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6752. {
  6753. unsigned int i = 0;
  6754. while (i < len && d->nrules < d->rule_cap) {
  6755. unsigned int sstart, send, bstart, bend;
  6756. struct br_decl decls[BR_DECLS];
  6757. unsigned int nd, scope_off = 0;
  6758. unsigned char scope_flags = 0;
  6759. while (i < len && is_space(s[i]))
  6760. i++;
  6761. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6762. i += 2;
  6763. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6764. i++;
  6765. i += 2;
  6766. continue;
  6767. }
  6768. if (i >= len || s[i] == '}')
  6769. break;
  6770. if (s[i] == '@') {
  6771. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6772. unsigned int q = i + 6;
  6773. while (q < len && s[q] != '{')
  6774. q++;
  6775. if (q < len &&
  6776. media_list_matches(s + i + 6,
  6777. q - (i + 6))) {
  6778. unsigned int b = q + 1;
  6779. unsigned int e = css_skip_nested(s, len, b);
  6780. if (e > b)
  6781. parse_css(d, s + b,
  6782. (e - 1) - b);
  6783. i = e;
  6784. } else {
  6785. i = skip_at_rule(s, len, i);
  6786. }
  6787. continue;
  6788. }
  6789. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6790. (i + 6 == len || is_space(s[i + 6]) ||
  6791. s[i + 6] == '{' || s[i + 6] == ';')) {
  6792. unsigned int q = i + 6, nst = q;
  6793. while (q < len && s[q] != '{' && s[q] != ';')
  6794. q++;
  6795. if (q >= len) {
  6796. i = len;
  6797. continue;
  6798. }
  6799. if (s[q] == ';') {
  6800. /* `@layer a, b;` only fixes the order. */
  6801. unsigned int st = nst;
  6802. char path[128];
  6803. while (st < q) {
  6804. unsigned int en = st;
  6805. while (en < q && s[en] != ',')
  6806. en++;
  6807. if (en > st)
  6808. css_layer_register(d, s + st,
  6809. en - st,
  6810. path,
  6811. sizeof(path));
  6812. st = en + 1;
  6813. }
  6814. i = q + 1;
  6815. continue;
  6816. }
  6817. {
  6818. unsigned int b = q + 1;
  6819. unsigned int e = css_skip_nested(s, len, b);
  6820. unsigned char saved = css_cur_layer;
  6821. char saved_name[128], path[128];
  6822. unsigned char rank;
  6823. memcpy(saved_name, css_layer_name,
  6824. sizeof(saved_name));
  6825. rank = css_layer_register(d, s + nst, q - nst,
  6826. path,
  6827. sizeof(path));
  6828. if (rank) {
  6829. css_cur_layer = rank;
  6830. memcpy(css_layer_name, path,
  6831. sizeof(css_layer_name));
  6832. }
  6833. if (e > b)
  6834. parse_css(d, s + b, (e - 1) - b);
  6835. css_cur_layer = saved;
  6836. memcpy(css_layer_name, saved_name,
  6837. sizeof(css_layer_name));
  6838. i = e;
  6839. }
  6840. continue;
  6841. }
  6842. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  6843. unsigned int q = i + 9;
  6844. while (q < len && s[q] != '{' && s[q] != ';')
  6845. q++;
  6846. if (q < len && s[q] == '{' &&
  6847. supports_matches(s + i + 9, q - (i + 9))) {
  6848. unsigned int b = q + 1;
  6849. unsigned int e = css_skip_nested(s, len, b);
  6850. if (e > b)
  6851. parse_css(d, s + b, (e - 1) - b);
  6852. i = e;
  6853. } else {
  6854. i = skip_at_rule(s, len, i);
  6855. }
  6856. continue;
  6857. }
  6858. i = skip_at_rule(s, len, i);
  6859. continue;
  6860. }
  6861. sstart = i;
  6862. while (i < len && s[i] != '{')
  6863. i++;
  6864. if (i >= len)
  6865. break;
  6866. send = i;
  6867. i++;
  6868. bstart = i;
  6869. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  6870. * to the matching close brace; stopping at the first '}' left
  6871. * the parser out of sync and dropped the rest of the sheet
  6872. * (nd-aktuell lost its desktop rules that way). */
  6873. bend = css_block_end(s, len, i);
  6874. i = bend < len ? bend + 1 : len;
  6875. while (sstart < send && is_space(s[sstart]))
  6876. sstart++;
  6877. while (send > sstart && is_space(s[send - 1]))
  6878. send--;
  6879. if (sstart == send)
  6880. continue;
  6881. if (!css_dark_selector(s + sstart, send - sstart))
  6882. css_collect_block(d, s + bstart, bend - bstart,
  6883. css_root_selector(s + sstart,
  6884. send - sstart));
  6885. if (css_collect_only)
  6886. continue;
  6887. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  6888. {
  6889. unsigned char sc = bend - bstart <= 4000 ?
  6890. css_block_scope(d, s + bstart, bend - bstart) :
  6891. 0;
  6892. scope_flags = 0;
  6893. scope_off = 0;
  6894. if (sc) {
  6895. scope_off = store_raw(d, s + bstart,
  6896. bend - bstart,
  6897. bend - bstart);
  6898. if (scope_off)
  6899. scope_flags = sc;
  6900. }
  6901. }
  6902. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  6903. continue;
  6904. /* Split a comma-separated selector list into separate rules.
  6905. * A selector the engine cannot match is dropped; the others
  6906. * in the list still apply. */
  6907. {
  6908. const char *list = s + sstart;
  6909. unsigned int ll = send - sstart, pos = 0, st, en;
  6910. while (d->nrules < d->rule_cap &&
  6911. sel_list_next(list, ll, &pos, &st, &en)) {
  6912. struct sel_part parts[SEL_PARTS];
  6913. struct br_rule *r;
  6914. char sel[160];
  6915. unsigned int k, off;
  6916. int np, pe;
  6917. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  6918. k++)
  6919. sel[k] = is_space(list[st + k]) ? ' ' :
  6920. list[st + k];
  6921. if (st + k < en)
  6922. continue; /* too long */
  6923. sel[k] = '\0';
  6924. pe = css_pseudo_element(sel, &k);
  6925. if (pe < 0 || !css_selector_supported(sel, k))
  6926. continue;
  6927. np = sel_split(sel, k, parts);
  6928. off = store_attr(d, sel, k, 159);
  6929. if (np <= 0 || !off)
  6930. continue;
  6931. r = &d->rules[d->nrules++];
  6932. r->sel_off = off;
  6933. r->key_off = store_attr(d, sel + parts[np - 1].st,
  6934. parts[np - 1].en -
  6935. parts[np - 1].st, 159);
  6936. r->spec = (unsigned short)selector_specificity(sel, k);
  6937. r->single = np == 1;
  6938. r->pe = (unsigned char)pe;
  6939. r->layer = css_cur_layer;
  6940. r->var_scope = scope_flags;
  6941. r->raw_off = scope_off;
  6942. r->raw_len = (unsigned short)(bend - bstart);
  6943. if (pe)
  6944. d->npe_rules++;
  6945. r->nd = (unsigned char)nd;
  6946. for (k = 0; k < nd; k++)
  6947. r->d[k] = decls[k];
  6948. rule_index_add(d, d->nrules - 1,
  6949. attr_ptr(d, r->key_off),
  6950. s_len(attr_ptr(d, r->key_off)));
  6951. }
  6952. }
  6953. }
  6954. }
  6955. /* Sites often set html { font-size: 62.5% } and then size everything in
  6956. * rem. Detect that root size before parsing the sheet so a rem resolves to
  6957. * the same pixels a real browser would use. */
  6958. static void detect_root_font(const char *s, unsigned int len)
  6959. {
  6960. unsigned int i;
  6961. for (i = 0; i + 4 < len; i++) {
  6962. unsigned int j, end, body, sel, q, h;
  6963. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  6964. int exact;
  6965. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  6966. s[i + 3] == 'l'))
  6967. continue;
  6968. h = i + 4;
  6969. while (h < len && is_space(s[h]))
  6970. h++;
  6971. if (h < len && s[h] != '{' && s[h] != ',' &&
  6972. s[h] != '.' && s[h] != ':' && s[h] != '[')
  6973. continue;
  6974. sel = i;
  6975. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  6976. s[sel - 1] != ';' && s[sel - 1] != ',')
  6977. sel--;
  6978. q = sel;
  6979. while (q < i && is_space(s[q]))
  6980. q++;
  6981. exact = (q == i);
  6982. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  6983. continue;
  6984. j = h;
  6985. if (j >= len || s[j] != '{')
  6986. continue;
  6987. while (j < len && s[j] != '{')
  6988. j++;
  6989. if (j >= len)
  6990. break;
  6991. end = j + 1;
  6992. body = end;
  6993. while (end < len && s[end] != '}')
  6994. end++;
  6995. for (; j + 9 < end; j++) {
  6996. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  6997. s[j + 3] == 't' && s[j + 4] == '-' &&
  6998. s[j + 5] == 's' && s[j + 6] == 'i' &&
  6999. s[j + 7] == 'z' && s[j + 8] == 'e'))
  7000. continue;
  7001. j += 9;
  7002. while (j < end && s[j] != ':')
  7003. j++;
  7004. if (j >= end)
  7005. break;
  7006. j++;
  7007. while (j < end && is_space(s[j]))
  7008. j++;
  7009. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7010. v = v * 10 + (s[j] - '0');
  7011. digits = 1;
  7012. j++;
  7013. }
  7014. if (j < end && s[j] == '.') {
  7015. j++;
  7016. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7017. v = v * 10 + (s[j] - '0');
  7018. frac++;
  7019. j++;
  7020. }
  7021. }
  7022. while (frac < 2) {
  7023. v *= 10;
  7024. frac++;
  7025. }
  7026. while (frac > 2) {
  7027. v /= 10;
  7028. frac--;
  7029. }
  7030. if (!digits)
  7031. break;
  7032. if (j < end && s[j] == '%')
  7033. pct = 1;
  7034. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  7035. px = 1;
  7036. if (pct)
  7037. v = (v * 16 + 5000) / 10000;
  7038. else if (px)
  7039. v /= 100;
  7040. else
  7041. v = v * 16 / 100;
  7042. if (exact && v >= 8 && v <= 24)
  7043. root_font_px = v;
  7044. if (exact)
  7045. return;
  7046. }
  7047. /* html { font: 10px/1 Verdana } also sets the root size. */
  7048. for (j = body; j + 5 <= end; j++) {
  7049. unsigned int p, ts, te;
  7050. if (!ci_eq_n(s + j, "font:", 5))
  7051. continue;
  7052. p = j + 5;
  7053. while (next_token(s, end, &p, &ts, &te)) {
  7054. int fv = parse_font_px(s + ts, te - ts);
  7055. if (fv >= 8 && fv <= 24) {
  7056. if (exact)
  7057. root_font_px = fv;
  7058. break;
  7059. }
  7060. }
  7061. if (exact)
  7062. return;
  7063. break;
  7064. }
  7065. }
  7066. }
  7067. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  7068. {
  7069. scan_font_faces(css, len);
  7070. detect_root_font(css, len);
  7071. css_collect_only = 1;
  7072. parse_css(d, css, len);
  7073. css_collect_only = 0;
  7074. parse_css(d, css, len);
  7075. }
  7076. void br_parse_inline_css(struct br_doc *d)
  7077. {
  7078. if (d->inline_css_len) {
  7079. scan_font_faces(css_store, d->inline_css_len);
  7080. detect_root_font(css_store, d->inline_css_len);
  7081. css_collect_only = 1;
  7082. parse_css(d, css_store, d->inline_css_len);
  7083. css_collect_only = 0;
  7084. parse_css(d, css_store, d->inline_css_len);
  7085. }
  7086. d->inline_css_len = 0;
  7087. }
  7088. void br_reset_styles(struct br_doc *d)
  7089. {
  7090. unsigned int i;
  7091. for (i = 0; i < d->nnode; i++) {
  7092. d->nodes[i].style_valid = 0;
  7093. d->nodes[i].pe_state = 0;
  7094. }
  7095. }
  7096. /* ---- selector matching ---- */
  7097. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  7098. unsigned int clen)
  7099. {
  7100. const char *s = attr_ptr(d, d->nodes[node].class_off);
  7101. unsigned int i = 0;
  7102. while (s[i]) {
  7103. unsigned int st, n;
  7104. while (s[i] && is_space(s[i]))
  7105. i++;
  7106. st = i;
  7107. while (s[i] && !is_space(s[i]))
  7108. i++;
  7109. n = i - st;
  7110. if (n == clen && ci_eq_n(s + st, cls, n))
  7111. return 1;
  7112. }
  7113. return 0;
  7114. }
  7115. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  7116. unsigned int len)
  7117. {
  7118. const char *s = attr_ptr(d, d->nodes[node].id_off);
  7119. unsigned int i;
  7120. for (i = 0; i < len; i++)
  7121. if (s[i] != id[i])
  7122. return 0;
  7123. return s[len] == '\0';
  7124. }
  7125. static int is_ident(char c)
  7126. {
  7127. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  7128. (c >= '0' && c <= '9') || c == '-' || c == '_';
  7129. }
  7130. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  7131. {
  7132. unsigned int n = 0;
  7133. while (lit[n])
  7134. n++;
  7135. return n == nlen && ci_eq_n(name, lit, nlen);
  7136. }
  7137. static int attr_present(struct br_doc *d, unsigned int node,
  7138. const char *name, unsigned int nlen)
  7139. {
  7140. struct br_node *n = &d->nodes[node];
  7141. if (attr_is(name, nlen, "data-collapse-target"))
  7142. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  7143. if (attr_is(name, nlen, "data-collapse-trigger"))
  7144. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  7145. if (attr_is(name, nlen, "is-open"))
  7146. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  7147. if (attr_is(name, nlen, "disabled"))
  7148. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  7149. if (attr_is(name, nlen, "open"))
  7150. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  7151. if (attr_is(name, nlen, "inert"))
  7152. return (n->attr_flags & BR_ATTR_INERT) != 0;
  7153. if (attr_is(name, nlen, "hidden"))
  7154. return (n->flags & BR_NODE_HIDDEN) != 0;
  7155. if (attr_is(name, nlen, "href"))
  7156. return attr_ptr(d, n->href_off)[0] != 0;
  7157. if (attr_is(name, nlen, "class"))
  7158. return attr_ptr(d, n->class_off)[0] != 0;
  7159. if (attr_is(name, nlen, "id"))
  7160. return attr_ptr(d, n->id_off)[0] != 0;
  7161. if (attr_is(name, nlen, "src"))
  7162. return attr_ptr(d, n->src_off)[0] != 0;
  7163. if (attr_is(name, nlen, "alt"))
  7164. return attr_ptr(d, n->alt_off)[0] != 0;
  7165. if (attr_is(name, nlen, "rel"))
  7166. return attr_ptr(d, n->rel_off)[0] != 0;
  7167. return 0;
  7168. }
  7169. /* ---- Selector engine ----
  7170. *
  7171. * One matcher serves the cascade (rule_try), querySelector(All) and
  7172. * Element.matches. A compound is an optional type/universal selector
  7173. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  7174. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  7175. * '~' combinators and is matched right to left with backtracking.
  7176. *
  7177. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  7178. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  7179. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  7180. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  7181. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  7182. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  7183. * and pseudo-elements never match an element; the cascade strips
  7184. * ::before/::after before it gets here. */
  7185. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7186. unsigned int len);
  7187. /* i is at '(' or '['; returns the index just past the matching close. */
  7188. static unsigned int sel_skip_group(const char *s, unsigned int i,
  7189. unsigned int len)
  7190. {
  7191. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  7192. int depth = 0;
  7193. for (; i < len; i++) {
  7194. char c = s[i];
  7195. if (quote) {
  7196. if (c == '\\')
  7197. i++;
  7198. else if (c == quote)
  7199. quote = 0;
  7200. continue;
  7201. }
  7202. if (c == '\\')
  7203. i++;
  7204. else if (c == '"' || c == '\'')
  7205. quote = c;
  7206. else if (c == open)
  7207. depth++;
  7208. else if (c == close && --depth == 0)
  7209. return i + 1;
  7210. }
  7211. return len;
  7212. }
  7213. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  7214. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  7215. char *out, unsigned int cap, unsigned int *olen)
  7216. {
  7217. unsigned int n = 0;
  7218. while (i < len) {
  7219. char c = s[i];
  7220. if (c == '\\' && i + 1 < len) {
  7221. c = s[i + 1];
  7222. i += 2;
  7223. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  7224. i++;
  7225. } else {
  7226. break;
  7227. }
  7228. if (n + 1 < cap)
  7229. out[n++] = c;
  7230. }
  7231. out[n] = '\0';
  7232. *olen = n;
  7233. return i;
  7234. }
  7235. /* Next top-level item of a comma-separated selector list. */
  7236. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  7237. unsigned int *st, unsigned int *en)
  7238. {
  7239. unsigned int i = *pos;
  7240. while (i < len && (is_space(s[i]) || s[i] == ','))
  7241. i++;
  7242. if (i >= len)
  7243. return 0;
  7244. *st = i;
  7245. while (i < len && s[i] != ',') {
  7246. if (s[i] == '(' || s[i] == '[')
  7247. i = sel_skip_group(s, i, len);
  7248. else if (s[i] == '\\' && i + 1 < len)
  7249. i += 2;
  7250. else if (s[i] == '"' || s[i] == '\'') {
  7251. char q = s[i++];
  7252. while (i < len && s[i] != q)
  7253. i++;
  7254. i++;
  7255. } else
  7256. i++;
  7257. }
  7258. if (i > len)
  7259. i = len;
  7260. *en = i;
  7261. while (*en > *st && is_space(s[*en - 1]))
  7262. (*en)--;
  7263. *pos = i;
  7264. return 1;
  7265. }
  7266. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7267. unsigned int len)
  7268. {
  7269. unsigned int pos = 0, st, en;
  7270. while (sel_list_next(s, len, &pos, &st, &en))
  7271. if (sel_match(d, node, s + st, en - st))
  7272. return 1;
  7273. return 0;
  7274. }
  7275. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7276. {
  7277. if (d->nodes[a].tag != d->nodes[b].tag)
  7278. return 0;
  7279. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7280. return 1;
  7281. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7282. attr_ptr(d, d->nodes[b].name_off));
  7283. }
  7284. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7285. {
  7286. unsigned int c, prev = 0;
  7287. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7288. c = d->nodes[c].next)
  7289. if (d->nodes[c].type == BR_NODE_ELEM)
  7290. prev = c;
  7291. return c == node ? prev : 0;
  7292. }
  7293. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7294. {
  7295. unsigned int c;
  7296. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7297. if (d->nodes[c].type == BR_NODE_ELEM)
  7298. return c;
  7299. return 0;
  7300. }
  7301. /* 1-based position among the element siblings (of the same type), counted
  7302. * from the start or, with from_end, from the end. */
  7303. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7304. int from_end)
  7305. {
  7306. unsigned int c;
  7307. int pos = 0, total = 0;
  7308. for (c = d->nodes[d->nodes[node].parent].first; c;
  7309. c = d->nodes[c].next) {
  7310. if (d->nodes[c].type != BR_NODE_ELEM)
  7311. continue;
  7312. if (of_type && !sel_same_type(d, c, node))
  7313. continue;
  7314. total++;
  7315. if (c == node)
  7316. pos = total;
  7317. }
  7318. if (!pos)
  7319. return 0;
  7320. return from_end ? total - pos + 1 : pos;
  7321. }
  7322. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7323. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7324. {
  7325. unsigned int i = 0;
  7326. int sign = 1, num = 0, digits = 0;
  7327. while (len && is_space(s[len - 1]))
  7328. len--;
  7329. while (i < len && is_space(s[i]))
  7330. i++;
  7331. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7332. *a = 2;
  7333. *b = 1;
  7334. return 1;
  7335. }
  7336. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7337. *a = 2;
  7338. *b = 0;
  7339. return 1;
  7340. }
  7341. if (i < len && (s[i] == '+' || s[i] == '-'))
  7342. sign = s[i++] == '-' ? -1 : 1;
  7343. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7344. num = num * 10 + (s[i++] - '0');
  7345. digits = 1;
  7346. }
  7347. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7348. *a = sign * (digits ? num : 1);
  7349. *b = 0;
  7350. i++;
  7351. while (i < len && is_space(s[i]))
  7352. i++;
  7353. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7354. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7355. while (i < len && is_space(s[i]))
  7356. i++;
  7357. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7358. bv = bv * 10 + (s[i++] - '0');
  7359. bd = 1;
  7360. }
  7361. if (!bd)
  7362. return 0;
  7363. *b = bs * bv;
  7364. }
  7365. } else {
  7366. if (!digits)
  7367. return 0;
  7368. *a = 0;
  7369. *b = sign * num;
  7370. }
  7371. return i == len;
  7372. }
  7373. static int sel_nth_ok(int a, int b, int pos)
  7374. {
  7375. if (pos <= 0)
  7376. return 0;
  7377. if (!a)
  7378. return pos == b;
  7379. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7380. }
  7381. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7382. const char *name, int op, const char *want,
  7383. unsigned int wl, int icase)
  7384. {
  7385. const char *have = br_find_attr(d, node, name);
  7386. unsigned int n, i;
  7387. if (!have) {
  7388. /* Boolean attributes the parser keeps as flags. */
  7389. if (!attr_present(d, node, name, text_len(name)))
  7390. return 0;
  7391. have = "";
  7392. }
  7393. if (!op)
  7394. return 1;
  7395. n = text_len(have);
  7396. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7397. switch (op) {
  7398. case '=':
  7399. return n == wl && SEL_EQ(have, want, wl);
  7400. case '^':
  7401. return wl && n >= wl && SEL_EQ(have, want, wl);
  7402. case '$':
  7403. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7404. case '*':
  7405. if (!wl)
  7406. return 0;
  7407. for (i = 0; i + wl <= n; i++)
  7408. if (SEL_EQ(have + i, want, wl))
  7409. return 1;
  7410. return 0;
  7411. case '|':
  7412. return n >= wl && SEL_EQ(have, want, wl) &&
  7413. (n == wl || have[wl] == '-');
  7414. case '~':
  7415. if (!wl)
  7416. return 0;
  7417. for (i = 0; i < n; ) {
  7418. unsigned int st;
  7419. while (i < n && is_space(have[i]))
  7420. i++;
  7421. st = i;
  7422. while (i < n && !is_space(have[i]))
  7423. i++;
  7424. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7425. return 1;
  7426. }
  7427. return 0;
  7428. }
  7429. #undef SEL_EQ
  7430. return 0;
  7431. }
  7432. static int sel_empty(struct br_doc *d, unsigned int node)
  7433. {
  7434. unsigned int c;
  7435. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7436. if (d->nodes[c].type == BR_NODE_ELEM)
  7437. return 0;
  7438. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7439. return 0;
  7440. }
  7441. return 1;
  7442. }
  7443. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7444. unsigned int len)
  7445. {
  7446. unsigned int c;
  7447. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7448. if (d->nodes[c].type != BR_NODE_ELEM)
  7449. continue;
  7450. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7451. return 1;
  7452. }
  7453. return 0;
  7454. }
  7455. /* :has() with an optional leading relative combinator per list item. */
  7456. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7457. unsigned int len)
  7458. {
  7459. unsigned int pos = 0, st, en, c;
  7460. while (sel_list_next(s, len, &pos, &st, &en)) {
  7461. char comb = s[st];
  7462. if (comb == '>' || comb == '+' || comb == '~') {
  7463. st++;
  7464. while (st < en && is_space(s[st]))
  7465. st++;
  7466. } else {
  7467. comb = ' ';
  7468. }
  7469. if (st >= en)
  7470. continue;
  7471. if (comb == '>') {
  7472. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7473. if (d->nodes[c].type == BR_NODE_ELEM &&
  7474. sel_match(d, c, s + st, en - st))
  7475. return 1;
  7476. } else if (comb == '+') {
  7477. c = sel_next_elem(d, node);
  7478. if (c && sel_match(d, c, s + st, en - st))
  7479. return 1;
  7480. } else if (comb == '~') {
  7481. for (c = sel_next_elem(d, node); c;
  7482. c = sel_next_elem(d, c))
  7483. if (sel_match(d, c, s + st, en - st))
  7484. return 1;
  7485. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7486. return 1;
  7487. }
  7488. }
  7489. return 0;
  7490. }
  7491. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7492. {
  7493. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7494. }
  7495. /* Pseudo-class names the engine models; anything else never matches and
  7496. * makes a top-level rule unsupported (see css_selector_supported()). */
  7497. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7498. {
  7499. static const char *const plain[] = {
  7500. "root", "link", "any-link", "visited", "first-child",
  7501. "last-child", "only-child", "first-of-type", "last-of-type",
  7502. "only-of-type", "empty", "checked", "disabled", "enabled",
  7503. "required", "optional",
  7504. };
  7505. static const char *const func[] = {
  7506. "not", "is", "where", "matches", "-webkit-any", "has",
  7507. "nth-child", "nth-last-child", "nth-of-type",
  7508. "nth-last-of-type",
  7509. };
  7510. unsigned int k;
  7511. if (fn) {
  7512. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7513. if (sel_name_is(name, nl, func[k]))
  7514. return 1;
  7515. return 0;
  7516. }
  7517. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7518. if (sel_name_is(name, nl, plain[k]))
  7519. return 1;
  7520. return 0;
  7521. }
  7522. static int sel_form_control(struct br_doc *d, unsigned int node)
  7523. {
  7524. unsigned int t = d->nodes[node].tag;
  7525. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7526. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7527. t == BR_TAG_OPTION;
  7528. }
  7529. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7530. unsigned int nl, const char *arg, unsigned int al,
  7531. int fn)
  7532. {
  7533. struct br_node *n = &d->nodes[node];
  7534. int a, b;
  7535. if (fn) {
  7536. if (sel_name_is(name, nl, "not"))
  7537. return !sel_list_match(d, node, arg, al);
  7538. if (sel_name_is(name, nl, "is") ||
  7539. sel_name_is(name, nl, "where") ||
  7540. sel_name_is(name, nl, "matches") ||
  7541. sel_name_is(name, nl, "-webkit-any"))
  7542. return sel_list_match(d, node, arg, al);
  7543. if (sel_name_is(name, nl, "has"))
  7544. return sel_has(d, node, arg, al);
  7545. if (!sel_nth_parse(arg, al, &a, &b))
  7546. return 0;
  7547. if (sel_name_is(name, nl, "nth-child"))
  7548. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7549. if (sel_name_is(name, nl, "nth-last-child"))
  7550. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7551. if (sel_name_is(name, nl, "nth-of-type"))
  7552. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7553. if (sel_name_is(name, nl, "nth-last-of-type"))
  7554. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7555. return 0;
  7556. }
  7557. if (sel_name_is(name, nl, "root"))
  7558. return n->tag == BR_TAG_HTML;
  7559. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7560. /* No history state: every link is unvisited. */
  7561. return n->tag == BR_TAG_A && n->href_off;
  7562. if (sel_name_is(name, nl, "first-child"))
  7563. return sel_sibling_pos(d, node, 0, 0) == 1;
  7564. if (sel_name_is(name, nl, "last-child"))
  7565. return sel_sibling_pos(d, node, 0, 1) == 1;
  7566. if (sel_name_is(name, nl, "only-child"))
  7567. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7568. sel_sibling_pos(d, node, 0, 1) == 1;
  7569. if (sel_name_is(name, nl, "first-of-type"))
  7570. return sel_sibling_pos(d, node, 1, 0) == 1;
  7571. if (sel_name_is(name, nl, "last-of-type"))
  7572. return sel_sibling_pos(d, node, 1, 1) == 1;
  7573. if (sel_name_is(name, nl, "only-of-type"))
  7574. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7575. sel_sibling_pos(d, node, 1, 1) == 1;
  7576. if (sel_name_is(name, nl, "empty"))
  7577. return sel_empty(d, node);
  7578. if (sel_name_is(name, nl, "checked"))
  7579. return br_find_attr(d, node, "checked") != 0 ||
  7580. (n->tag == BR_TAG_OPTION &&
  7581. br_find_attr(d, node, "selected") != 0);
  7582. if (sel_name_is(name, nl, "disabled"))
  7583. return sel_form_control(d, node) &&
  7584. (n->attr_flags & BR_ATTR_DISABLED);
  7585. if (sel_name_is(name, nl, "enabled"))
  7586. return sel_form_control(d, node) &&
  7587. !(n->attr_flags & BR_ATTR_DISABLED);
  7588. if (sel_name_is(name, nl, "required"))
  7589. return sel_form_control(d, node) &&
  7590. br_find_attr(d, node, "required") != 0;
  7591. if (sel_name_is(name, nl, "optional"))
  7592. return sel_form_control(d, node) &&
  7593. !br_find_attr(d, node, "required");
  7594. return 0;
  7595. }
  7596. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7597. unsigned int len)
  7598. {
  7599. char buf[64];
  7600. unsigned int i = 0, bl;
  7601. if (d->nodes[node].type != BR_NODE_ELEM)
  7602. return 0;
  7603. if (i < len && s[i] == '*') {
  7604. i++;
  7605. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7606. unsigned int tag;
  7607. i = sel_ident(s, i, len, buf, 32, &bl);
  7608. tag = br_tag_from_name(buf);
  7609. if (tag == BR_TAG_UNKNOWN) {
  7610. const char *raw = attr_ptr(d,
  7611. d->nodes[node].name_off);
  7612. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7613. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7614. return 0;
  7615. } else if (tag != d->nodes[node].tag) {
  7616. return 0;
  7617. }
  7618. }
  7619. if (i >= len)
  7620. return i > 0;
  7621. while (i < len) {
  7622. char c = s[i];
  7623. if (c == '.' || c == '#') {
  7624. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7625. if (!bl)
  7626. return 0;
  7627. if (c == '.' ? !class_has(d, node, buf, bl) :
  7628. !id_eq(d, node, buf, bl))
  7629. return 0;
  7630. } else if (c == '[') {
  7631. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7632. unsigned int vs = 0, vl = 0, k;
  7633. char name[32];
  7634. int op = 0, icase = 0;
  7635. while (j < e && is_space(s[j]))
  7636. j++;
  7637. k = 0;
  7638. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7639. k + 1 < sizeof(name)) {
  7640. char ch = s[j++];
  7641. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7642. (char)(ch + 32) : ch;
  7643. }
  7644. name[k] = '\0';
  7645. if (!k)
  7646. return 0;
  7647. while (j < e && is_space(s[j]))
  7648. j++;
  7649. if (j < e && s[j] == '=') {
  7650. op = '=';
  7651. j++;
  7652. } else if (j + 1 < e && s[j + 1] == '=' &&
  7653. (s[j] == '~' || s[j] == '|' ||
  7654. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7655. op = s[j];
  7656. j += 2;
  7657. }
  7658. if (op) {
  7659. while (j < e && is_space(s[j]))
  7660. j++;
  7661. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7662. char q = s[j++];
  7663. vs = j;
  7664. while (j < e && s[j] != q)
  7665. j++;
  7666. vl = j - vs;
  7667. j++;
  7668. } else {
  7669. vs = j;
  7670. while (j < e && s[j] != ']' &&
  7671. !is_space(s[j]))
  7672. j++;
  7673. vl = j - vs;
  7674. }
  7675. while (j < e && is_space(s[j]))
  7676. j++;
  7677. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7678. icase = 1;
  7679. }
  7680. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7681. icase))
  7682. return 0;
  7683. i = e;
  7684. } else if (c == ':') {
  7685. unsigned int st, nl, as = 0, al = 0;
  7686. int fn = 0;
  7687. if (i + 1 < len && s[i + 1] == ':')
  7688. return 0; /* pseudo-element */
  7689. st = ++i;
  7690. while (i < len && is_ident(s[i]))
  7691. i++;
  7692. nl = i - st;
  7693. if (i < len && s[i] == '(') {
  7694. unsigned int e = sel_skip_group(s, i, len);
  7695. fn = 1;
  7696. as = i + 1;
  7697. al = e > as ? e - as - 1 : 0;
  7698. i = e;
  7699. }
  7700. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7701. return 0;
  7702. } else {
  7703. return 0;
  7704. }
  7705. }
  7706. return 1;
  7707. }
  7708. /* Split a complex selector into compounds; returns the count or -1. */
  7709. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7710. {
  7711. unsigned int i = 0;
  7712. int n = 0;
  7713. char comb = 0;
  7714. while (i < len && is_space(s[i]))
  7715. i++;
  7716. while (i < len) {
  7717. unsigned int st = i;
  7718. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7719. s[i] != '+' && s[i] != '~') {
  7720. if (s[i] == '(' || s[i] == '[')
  7721. i = sel_skip_group(s, i, len);
  7722. else if (s[i] == '\\' && i + 1 < len)
  7723. i += 2;
  7724. else
  7725. i++;
  7726. }
  7727. if (i == st || n == SEL_PARTS)
  7728. return -1;
  7729. p[n].st = (unsigned short)st;
  7730. p[n].en = (unsigned short)(i > len ? len : i);
  7731. p[n].comb = comb;
  7732. n++;
  7733. comb = 0;
  7734. while (i < len && is_space(s[i])) {
  7735. comb = ' ';
  7736. i++;
  7737. }
  7738. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7739. comb = s[i++];
  7740. while (i < len && is_space(s[i]))
  7741. i++;
  7742. if (i >= len)
  7743. return -1;
  7744. }
  7745. }
  7746. return n;
  7747. }
  7748. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7749. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7750. * parent when the page starts with <html> (node 0 is that element; see
  7751. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7752. static int sel_parent(struct br_doc *d, unsigned int node)
  7753. {
  7754. unsigned int p = d->nodes[node].parent;
  7755. if (p)
  7756. return (int)p;
  7757. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7758. d->nodes[0].tag == BR_TAG_HTML &&
  7759. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7760. return 0;
  7761. return -1;
  7762. }
  7763. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7764. const struct sel_part *p, int k)
  7765. {
  7766. unsigned int c, pl;
  7767. const char *ps;
  7768. if (k == 0)
  7769. return 1;
  7770. ps = s + p[k - 1].st;
  7771. pl = p[k - 1].en - p[k - 1].st;
  7772. switch (p[k].comb) {
  7773. case '>': {
  7774. int pc = sel_parent(d, node);
  7775. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7776. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7777. }
  7778. case '+':
  7779. c = sel_prev_elem(d, node);
  7780. return c && compound_match(d, c, ps, pl) &&
  7781. sel_from(d, c, s, p, k - 1);
  7782. case '~':
  7783. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7784. if (compound_match(d, c, ps, pl) &&
  7785. sel_from(d, c, s, p, k - 1))
  7786. return 1;
  7787. return 0;
  7788. default: {
  7789. int pc;
  7790. for (pc = sel_parent(d, node); pc >= 0;
  7791. pc = sel_parent(d, (unsigned int)pc))
  7792. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7793. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7794. return 1;
  7795. return 0;
  7796. }
  7797. }
  7798. }
  7799. /* Match one complex selector. right_done: the caller already matched the
  7800. * rightmost compound (the rule index key). */
  7801. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7802. unsigned int len, int right_done)
  7803. {
  7804. struct sel_part parts[SEL_PARTS];
  7805. int np = sel_split(s, len, parts);
  7806. if (np <= 0)
  7807. return 0;
  7808. if (!right_done &&
  7809. !compound_match(d, node, s + parts[np - 1].st,
  7810. parts[np - 1].en - parts[np - 1].st))
  7811. return 0;
  7812. return sel_from(d, node, s, parts, np - 1);
  7813. }
  7814. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7815. unsigned int len)
  7816. {
  7817. return selector_match(d, node, s, len, 0);
  7818. }
  7819. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7820. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7821. * and :where counts nothing. */
  7822. static unsigned int selector_specificity(const char *s, unsigned int len)
  7823. {
  7824. unsigned int i = 0, spec = 0, bl;
  7825. char buf[64];
  7826. while (i < len) {
  7827. char c = s[i];
  7828. if (c == '#' || c == '.') {
  7829. spec += c == '#' ? 100 : 10;
  7830. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7831. } else if (c == '[') {
  7832. spec += 10;
  7833. i = sel_skip_group(s, i, len);
  7834. } else if (c == ':') {
  7835. unsigned int st, nl;
  7836. if (i + 1 < len && s[i + 1] == ':') {
  7837. spec += 1;
  7838. i += 2;
  7839. while (i < len && is_ident(s[i]))
  7840. i++;
  7841. continue;
  7842. }
  7843. st = ++i;
  7844. while (i < len && is_ident(s[i]))
  7845. i++;
  7846. nl = i - st;
  7847. if (i < len && s[i] == '(') {
  7848. unsigned int e = sel_skip_group(s, i, len);
  7849. if (sel_name_is(s + st, nl, "where")) {
  7850. /* zero */
  7851. } else if (sel_name_is(s + st, nl, "not") ||
  7852. sel_name_is(s + st, nl, "is") ||
  7853. sel_name_is(s + st, nl, "has") ||
  7854. sel_name_is(s + st, nl, "matches") ||
  7855. sel_name_is(s + st, nl,
  7856. "-webkit-any")) {
  7857. unsigned int pos = 0, as, ae, best = 0;
  7858. unsigned int al = e > i + 1 ?
  7859. e - i - 2 : 0;
  7860. while (sel_list_next(s + i + 1, al,
  7861. &pos, &as, &ae)) {
  7862. unsigned int v =
  7863. selector_specificity(
  7864. s + i + 1 + as,
  7865. ae - as);
  7866. if (v > best)
  7867. best = v;
  7868. }
  7869. spec += best;
  7870. } else {
  7871. spec += 10;
  7872. }
  7873. i = e;
  7874. } else if (sel_name_is(s + st, nl, "before") ||
  7875. sel_name_is(s + st, nl, "after")) {
  7876. spec += 1;
  7877. } else {
  7878. spec += 10;
  7879. }
  7880. } else if (is_ident(c) || c == '\\') {
  7881. spec += 1;
  7882. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  7883. } else {
  7884. i++;
  7885. }
  7886. }
  7887. return spec;
  7888. }
  7889. /* Cascade filter: drop a selector the engine cannot match (user-state
  7890. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  7891. * ones the caller handles), so it does not waste a rule slot. Arguments of
  7892. * functional pseudo-classes are not checked: an unknown pseudo-class there
  7893. * is simply false, which is right for :not(:hover). */
  7894. static int css_selector_supported(const char *s, unsigned int len)
  7895. {
  7896. struct sel_part parts[SEL_PARTS];
  7897. unsigned int i = 0;
  7898. if (sel_split(s, len, parts) <= 0)
  7899. return 0;
  7900. while (i < len) {
  7901. if (s[i] == '(' || s[i] == '[') {
  7902. i = sel_skip_group(s, i, len);
  7903. } else if (s[i] == '\\') {
  7904. i += 2;
  7905. } else if (s[i] == ':') {
  7906. unsigned int st, nl;
  7907. int fn;
  7908. if (i + 1 < len && s[i + 1] == ':')
  7909. return 0;
  7910. st = ++i;
  7911. while (i < len && is_ident(s[i]))
  7912. i++;
  7913. nl = i - st;
  7914. fn = i < len && s[i] == '(';
  7915. if (!sel_pseudo_known(s + st, nl, fn))
  7916. return 0;
  7917. } else {
  7918. i++;
  7919. }
  7920. }
  7921. return 1;
  7922. }
  7923. #define RULE_MATCH_MAX 512
  7924. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  7925. static int cascade_pe;
  7926. struct rule_match {
  7927. unsigned short idx;
  7928. short spec; /* precedence of normal declarations */
  7929. short spec_imp; /* precedence of !important declarations */
  7930. };
  7931. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  7932. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  7933. * unlayered rules, then the style attribute. !important reverses the
  7934. * layer order: unlayered lowest, the first layer highest, the style
  7935. * attribute above all. Both fit a short. */
  7936. #define CASCADE_BAND 512
  7937. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  7938. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  7939. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  7940. {
  7941. unsigned int band;
  7942. if (spec >= CASCADE_BAND)
  7943. spec = CASCADE_BAND - 1;
  7944. if (important)
  7945. band = layer ? CASCADE_UNLAYERED - layer : 0;
  7946. else
  7947. band = layer ? layer : CASCADE_UNLAYERED;
  7948. return (int)(band * CASCADE_BAND + spec);
  7949. }
  7950. static struct rule_match rule_matches[RULE_MATCH_MAX];
  7951. static unsigned int nrule_matches;
  7952. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  7953. {
  7954. struct br_rule *r = &d->rules[idx];
  7955. const char *key = attr_ptr(d, r->key_off);
  7956. int spec, key_ok = 0;
  7957. if (r->pe != cascade_pe)
  7958. return;
  7959. if (key[0]) {
  7960. if (!compound_match(d, node, key, s_len(key)))
  7961. return;
  7962. key_ok = 1;
  7963. }
  7964. if (!(r->single && key_ok) &&
  7965. !selector_match(d, node, attr_ptr(d, r->sel_off),
  7966. s_len(attr_ptr(d, r->sel_off)), key_ok))
  7967. return;
  7968. spec = (int)r->spec;
  7969. if (nrule_matches < RULE_MATCH_MAX) {
  7970. rule_matches[nrule_matches].idx = (unsigned short)idx;
  7971. rule_matches[nrule_matches].spec =
  7972. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  7973. rule_matches[nrule_matches].spec_imp =
  7974. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  7975. nrule_matches++;
  7976. }
  7977. }
  7978. static void rule_scan_list(struct br_doc *d, unsigned int node,
  7979. unsigned short head)
  7980. {
  7981. while (head != 0xFFFF) {
  7982. rule_try(d, node, head);
  7983. head = d->rule_next[head];
  7984. }
  7985. }
  7986. static void rule_scan_class(struct br_doc *d, unsigned int node,
  7987. const char *cls, unsigned int len)
  7988. {
  7989. if (len)
  7990. rule_scan_list(d, node,
  7991. d->rule_bucket[rule_hash(cls, len)]);
  7992. }
  7993. static void rule_collect(struct br_doc *d, unsigned int node)
  7994. {
  7995. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  7996. const char *id = attr_ptr(d, d->nodes[node].id_off);
  7997. unsigned int tag = d->nodes[node].tag;
  7998. unsigned int i;
  7999. nrule_matches = 0;
  8000. rule_scan_list(d, node, d->rule_any);
  8001. if (tag < BR_TAG_MAX)
  8002. rule_scan_list(d, node, d->rule_tag[tag]);
  8003. if (id[0])
  8004. rule_scan_list(d, node,
  8005. d->rule_bucket[rule_hash(id, s_len(id))]);
  8006. i = 0;
  8007. while (cls[i]) {
  8008. unsigned int st, n;
  8009. while (cls[i] && is_space(cls[i]))
  8010. i++;
  8011. st = i;
  8012. while (cls[i] && !is_space(cls[i]))
  8013. i++;
  8014. n = i - st;
  8015. if (n)
  8016. rule_scan_class(d, node, cls + st, n);
  8017. }
  8018. /* Rules must cascade in document order even though the index visits
  8019. * them in bucket order. */
  8020. for (i = 1; i < nrule_matches; i++) {
  8021. struct rule_match m = rule_matches[i];
  8022. unsigned int j = i;
  8023. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  8024. rule_matches[j] = rule_matches[j - 1];
  8025. j--;
  8026. }
  8027. rule_matches[j] = m;
  8028. }
  8029. }
  8030. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  8031. int font_px)
  8032. {
  8033. if (*kind == 4) {
  8034. *val = (unsigned short)(*val * font_px / 100);
  8035. *kind = 1;
  8036. } else if (*kind == 5) {
  8037. *val = (unsigned short)(*val * root_font_px / 100);
  8038. *kind = 1;
  8039. }
  8040. }
  8041. static void resolve_size_units(struct br_style *st)
  8042. {
  8043. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  8044. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  8045. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  8046. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  8047. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  8048. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  8049. }
  8050. static void set_border_side_w(struct br_style *st, int side, int v)
  8051. {
  8052. if (v < 0)
  8053. v = 0;
  8054. if (v > 32)
  8055. v = 32;
  8056. st->border_w[side] = (unsigned char)v;
  8057. if (st->border_w[side] && !st->border_c[side])
  8058. st->border_c[side] = st->color;
  8059. }
  8060. static void set_border_side_c(struct br_style *st, int side, int v)
  8061. {
  8062. st->border_c[side] = (unsigned short)v;
  8063. }
  8064. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  8065. {
  8066. switch (dcl->prop) {
  8067. case PROP_COLOR:
  8068. st->color = (unsigned short)dcl->val;
  8069. break;
  8070. case PROP_BG:
  8071. st->bg = (unsigned short)dcl->val;
  8072. st->has_bg = 1;
  8073. st->bg_set = 1;
  8074. break;
  8075. case PROP_BG_NONE:
  8076. st->has_bg = 0;
  8077. st->bg_set = 1;
  8078. break;
  8079. case PROP_BG_IMAGE:
  8080. st->bg_image = (unsigned short)dcl->val;
  8081. break;
  8082. case PROP_BG_REPEAT:
  8083. st->bg_repeat = (unsigned char)dcl->val;
  8084. break;
  8085. case PROP_BG_POS_X_KIND:
  8086. st->bg_pos_x_kind = (unsigned char)dcl->val;
  8087. break;
  8088. case PROP_BG_POS_X:
  8089. st->bg_pos_x = (signed short)dcl->val;
  8090. break;
  8091. case PROP_BG_POS_Y_KIND:
  8092. st->bg_pos_y_kind = (unsigned char)dcl->val;
  8093. break;
  8094. case PROP_BG_POS_Y:
  8095. st->bg_pos_y = (signed short)dcl->val;
  8096. break;
  8097. case PROP_BG_SIZE_KIND:
  8098. st->bg_size_kind = (unsigned char)dcl->val;
  8099. break;
  8100. case PROP_BG_SIZE_W:
  8101. st->bg_size_w = (unsigned short)dcl->val;
  8102. break;
  8103. case PROP_BG_SIZE_H:
  8104. st->bg_size_h = (unsigned short)dcl->val;
  8105. break;
  8106. case PROP_FONT_PX:
  8107. st->font_raw = (unsigned short)dcl->val;
  8108. break;
  8109. case PROP_FONT_UNIT:
  8110. st->font_unit = (unsigned char)dcl->val;
  8111. break;
  8112. case PROP_BOLD:
  8113. st->bold = (unsigned char)(dcl->val != 0);
  8114. break;
  8115. case PROP_ITALIC:
  8116. st->italic = (unsigned char)(dcl->val != 0);
  8117. break;
  8118. case PROP_MONO:
  8119. st->mono = (unsigned char)(dcl->val != 0);
  8120. break;
  8121. case PROP_FONT_FACE:
  8122. st->font_face = (unsigned char)dcl->val;
  8123. break;
  8124. case PROP_ALIGN:
  8125. st->align = (unsigned char)dcl->val;
  8126. break;
  8127. case PROP_UNDERLINE:
  8128. st->underline = (unsigned char)(dcl->val != 0);
  8129. break;
  8130. case PROP_STRIKE:
  8131. st->strike = (unsigned char)(dcl->val != 0);
  8132. break;
  8133. case PROP_OVERLINE:
  8134. st->overline = (unsigned char)(dcl->val != 0);
  8135. break;
  8136. case PROP_DISPLAY:
  8137. st->display = (unsigned char)(dcl->val &
  8138. ~BR_DISPLAY_INLINE_FLAG);
  8139. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  8140. st->list_item = 0;
  8141. break;
  8142. case PROP_VIS_HIDDEN:
  8143. st->vis_hidden = (unsigned char)(dcl->val != 0);
  8144. break;
  8145. case PROP_OPACITY_HIDDEN:
  8146. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  8147. break;
  8148. case PROP_FILL_MODE:
  8149. st->fill_mode = (unsigned char)dcl->val;
  8150. break;
  8151. case PROP_FILL:
  8152. st->fill = (unsigned short)dcl->val;
  8153. break;
  8154. case PROP_STROKE_MODE:
  8155. st->stroke_mode = (unsigned char)dcl->val;
  8156. break;
  8157. case PROP_STROKE:
  8158. st->stroke = (unsigned short)dcl->val;
  8159. break;
  8160. case PROP_STROKE_W:
  8161. st->stroke_w = (unsigned short)dcl->val;
  8162. break;
  8163. case PROP_FILL_RULE:
  8164. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  8165. break;
  8166. case PROP_OVERFLOW_HIDDEN:
  8167. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  8168. break;
  8169. case PROP_MT: st->mt = dcl->val; break;
  8170. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  8171. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  8172. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  8173. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  8174. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  8175. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  8176. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  8177. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  8178. case PROP_MR: st->mr = dcl->val; break;
  8179. case PROP_MB: st->mb = dcl->val; break;
  8180. case PROP_ML: st->ml = dcl->val; break;
  8181. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  8182. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  8183. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  8184. break;
  8185. case PROP_PT: st->pt = dcl->val; break;
  8186. case PROP_PR: st->pr = dcl->val; break;
  8187. case PROP_PB: st->pb = dcl->val; break;
  8188. case PROP_PL: st->pl = dcl->val; break;
  8189. case PROP_MT_U:
  8190. case PROP_MR_U:
  8191. case PROP_MB_U:
  8192. case PROP_ML_U:
  8193. case PROP_PT_U:
  8194. case PROP_PR_U:
  8195. case PROP_PB_U:
  8196. case PROP_PL_U:
  8197. st->mp_unit[dcl->prop - PROP_MT_U] =
  8198. (unsigned char)dcl->val;
  8199. break;
  8200. case PROP_BORDER_W:
  8201. set_border_side_w(st, 0, dcl->val);
  8202. set_border_side_w(st, 1, dcl->val);
  8203. set_border_side_w(st, 2, dcl->val);
  8204. set_border_side_w(st, 3, dcl->val);
  8205. break;
  8206. case PROP_BORDER_COLOR:
  8207. set_border_side_c(st, 0, dcl->val);
  8208. set_border_side_c(st, 1, dcl->val);
  8209. set_border_side_c(st, 2, dcl->val);
  8210. set_border_side_c(st, 3, dcl->val);
  8211. break;
  8212. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  8213. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  8214. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  8215. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  8216. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  8217. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  8218. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  8219. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  8220. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  8221. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  8222. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  8223. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  8224. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  8225. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  8226. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  8227. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  8228. case PROP_RADIUS_TL:
  8229. case PROP_RADIUS_TR:
  8230. case PROP_RADIUS_BR:
  8231. case PROP_RADIUS_BL:
  8232. st->radius[dcl->prop - PROP_RADIUS_TL] =
  8233. (unsigned char)dcl->val;
  8234. break;
  8235. case PROP_LIST_NONE:
  8236. st->no_bullet = (unsigned char)(dcl->val != 0);
  8237. break;
  8238. case PROP_LIST_STYLE:
  8239. st->list_style = (unsigned char)dcl->val;
  8240. break;
  8241. case PROP_CONTENT:
  8242. st->content = (unsigned short)dcl->val;
  8243. break;
  8244. case PROP_ASPECT_W:
  8245. st->aspect_w = (unsigned short)dcl->val;
  8246. break;
  8247. case PROP_ASPECT_H:
  8248. st->aspect_h = (unsigned short)dcl->val;
  8249. break;
  8250. case PROP_NOWRAP:
  8251. st->nowrap = (unsigned char)(dcl->val != 0);
  8252. break;
  8253. case PROP_WIDTH:
  8254. st->width = (unsigned short)dcl->val;
  8255. break;
  8256. case PROP_WIDTH_KIND:
  8257. st->width_kind = (unsigned char)dcl->val;
  8258. break;
  8259. case PROP_WIDTH_CALC_PCT:
  8260. st->width_calc_pct = dcl->val;
  8261. break;
  8262. case PROP_WIDTH_CALC_PX:
  8263. st->width_calc_px = dcl->val;
  8264. break;
  8265. case PROP_MAXW:
  8266. st->max_width = (unsigned short)dcl->val;
  8267. break;
  8268. case PROP_MAXW_KIND:
  8269. st->max_width_kind = (unsigned char)dcl->val;
  8270. break;
  8271. case PROP_MAXW_CALC_PCT:
  8272. st->max_width_calc_pct = (signed short)dcl->val;
  8273. break;
  8274. case PROP_MAXW_CALC_PX:
  8275. st->max_width_calc_px = (signed short)dcl->val;
  8276. break;
  8277. case PROP_MINW:
  8278. st->min_width = (unsigned short)dcl->val;
  8279. break;
  8280. case PROP_MINW_KIND:
  8281. st->min_width_kind = (unsigned char)dcl->val;
  8282. break;
  8283. case PROP_MINW_CALC_PCT:
  8284. st->min_width_calc_pct = (signed short)dcl->val;
  8285. break;
  8286. case PROP_MINW_CALC_PX:
  8287. st->min_width_calc_px = (signed short)dcl->val;
  8288. break;
  8289. case PROP_HEIGHT:
  8290. st->height = (unsigned short)dcl->val;
  8291. break;
  8292. case PROP_HEIGHT_KIND:
  8293. st->height_kind = (unsigned char)dcl->val;
  8294. break;
  8295. case PROP_HEIGHT_CALC_PCT:
  8296. st->height_calc_pct = (signed short)dcl->val;
  8297. break;
  8298. case PROP_HEIGHT_CALC_PX:
  8299. st->height_calc_px = (signed short)dcl->val;
  8300. break;
  8301. case PROP_MINH:
  8302. st->min_height = (unsigned short)dcl->val;
  8303. break;
  8304. case PROP_MINH_KIND:
  8305. st->min_height_kind = (unsigned char)dcl->val;
  8306. break;
  8307. case PROP_MINH_CALC_PCT:
  8308. st->min_height_calc_pct = (signed short)dcl->val;
  8309. break;
  8310. case PROP_MINH_CALC_PX:
  8311. st->min_height_calc_px = (signed short)dcl->val;
  8312. break;
  8313. case PROP_MAXH:
  8314. st->max_height = (unsigned short)dcl->val;
  8315. break;
  8316. case PROP_MAXH_KIND:
  8317. st->max_height_kind = (unsigned char)dcl->val;
  8318. break;
  8319. case PROP_MAXH_CALC_PCT:
  8320. st->max_height_calc_pct = (signed short)dcl->val;
  8321. break;
  8322. case PROP_MAXH_CALC_PX:
  8323. st->max_height_calc_px = (signed short)dcl->val;
  8324. break;
  8325. case PROP_LH_PCT:
  8326. st->line_h_pct = (unsigned char)dcl->val;
  8327. break;
  8328. case PROP_LH_PX:
  8329. st->line_h_px = (unsigned short)dcl->val;
  8330. break;
  8331. case PROP_UPPER:
  8332. st->uppercase = (unsigned char)(dcl->val != 0);
  8333. break;
  8334. case PROP_CURSOR:
  8335. st->cursor = (unsigned char)dcl->val;
  8336. break;
  8337. case PROP_FLOAT:
  8338. st->float_left = (unsigned char)dcl->val;
  8339. break;
  8340. case PROP_CLEAR:
  8341. st->clear = (unsigned char)dcl->val;
  8342. break;
  8343. case PROP_FLEX_GROW:
  8344. st->flex_grow = (unsigned char)dcl->val;
  8345. break;
  8346. case PROP_FLEX_COL:
  8347. st->flex_col = (unsigned char)(dcl->val != 0);
  8348. break;
  8349. case PROP_FLEX_WRAP:
  8350. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8351. break;
  8352. case PROP_JUSTIFY:
  8353. st->justify = (unsigned char)dcl->val;
  8354. break;
  8355. case PROP_ALIGN_ITEMS:
  8356. st->align_items = (unsigned char)dcl->val;
  8357. break;
  8358. case PROP_ALIGN_SELF:
  8359. st->align_self = (unsigned char)dcl->val;
  8360. break;
  8361. case PROP_GRID_NCOLS:
  8362. st->grid_ncols = (unsigned char)dcl->val;
  8363. break;
  8364. case PROP_GRID_GAP:
  8365. st->grid_gap = (unsigned char)dcl->val;
  8366. break;
  8367. case PROP_GRID_SLOT:
  8368. st->grid_slot = (unsigned char)dcl->val;
  8369. break;
  8370. case PROP_GRID_AUTO:
  8371. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8372. break;
  8373. case PROP_POS:
  8374. st->pos = (unsigned char)dcl->val;
  8375. break;
  8376. case PROP_LEFT:
  8377. st->left = dcl->val;
  8378. break;
  8379. case PROP_TOP:
  8380. st->top = dcl->val;
  8381. break;
  8382. case PROP_RIGHT:
  8383. st->right = dcl->val;
  8384. break;
  8385. case PROP_BOTTOM:
  8386. st->bottom = dcl->val;
  8387. break;
  8388. case PROP_LEFT_SET:
  8389. st->left_set = (unsigned char)(dcl->val != 0);
  8390. break;
  8391. case PROP_TOP_SET:
  8392. st->top_set = (unsigned char)(dcl->val != 0);
  8393. break;
  8394. case PROP_RIGHT_SET:
  8395. st->right_set = (unsigned char)(dcl->val != 0);
  8396. break;
  8397. case PROP_BOTTOM_SET:
  8398. st->bottom_set = (unsigned char)(dcl->val != 0);
  8399. break;
  8400. case PROP_ZINDEX:
  8401. st->z_index = dcl->val;
  8402. break;
  8403. case PROP_ZSET:
  8404. st->z_set = (unsigned char)(dcl->val != 0);
  8405. break;
  8406. case PROP_TRANSFORM_X:
  8407. st->tx = dcl->val;
  8408. break;
  8409. case PROP_TRANSFORM_Y:
  8410. st->ty = dcl->val;
  8411. break;
  8412. case PROP_TRANSFORM_SET:
  8413. st->transform_set = (unsigned char)(dcl->val != 0);
  8414. break;
  8415. case PROP_TRANSFORM_PAINT:
  8416. st->transform_scale = (unsigned char)(dcl->val != 0);
  8417. break;
  8418. case PROP_SCALE_X:
  8419. st->scale_x = (signed short)dcl->val;
  8420. break;
  8421. case PROP_SCALE_Y:
  8422. st->scale_y = (signed short)dcl->val;
  8423. break;
  8424. case PROP_ROTATE:
  8425. st->rotate_x100 = (signed short)dcl->val;
  8426. break;
  8427. case PROP_ORIGIN_X_KIND:
  8428. st->origin_x_kind = (unsigned char)dcl->val;
  8429. break;
  8430. case PROP_ORIGIN_X:
  8431. st->origin_x = (signed short)dcl->val;
  8432. break;
  8433. case PROP_ORIGIN_Y_KIND:
  8434. st->origin_y_kind = (unsigned char)dcl->val;
  8435. break;
  8436. case PROP_ORIGIN_Y:
  8437. st->origin_y = (signed short)dcl->val;
  8438. break;
  8439. default:
  8440. break;
  8441. }
  8442. }
  8443. /* The element's custom properties for rules flagged USES_SCOPED: the
  8444. * definitions of its matched rules in cascade order (precedence first,
  8445. * then document order), over the document-wide table. Inheritance from
  8446. * ancestors is not modelled; a name no matched rule defines falls back to
  8447. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8448. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8449. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8450. static void css_env_build(struct br_doc *d)
  8451. {
  8452. static unsigned short order[RULE_MATCH_MAX];
  8453. unsigned int i, need = 0, n = 0;
  8454. css_env_active = 0;
  8455. css_nlocal = 0;
  8456. for (i = 0; i < nrule_matches; i++)
  8457. if (d->rules[rule_matches[i].idx].var_scope &
  8458. BR_RULE_USES_SCOPED)
  8459. need = 1;
  8460. if (!need)
  8461. return;
  8462. /* Definitions apply lowest precedence first so the winner is
  8463. * written last (stable: document order within a precedence). */
  8464. for (i = 0; i < nrule_matches; i++) {
  8465. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8466. unsigned int j = n;
  8467. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8468. continue;
  8469. while (j > 0 && rule_matches[order[j - 1]].spec >
  8470. rule_matches[i].spec) {
  8471. order[j] = order[j - 1];
  8472. j--;
  8473. }
  8474. order[j] = i;
  8475. n++;
  8476. }
  8477. for (i = 0; i < n; i++) {
  8478. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8479. const char *raw = attr_ptr(d, r->raw_off);
  8480. unsigned int save = css_nlocal, k;
  8481. /* Collect this block's definitions after the earlier ones,
  8482. * replacing a name already present. */
  8483. {
  8484. unsigned int base = save;
  8485. css_local_append(raw, r->raw_len);
  8486. for (k = base; k < css_nlocal; k++) {
  8487. int prev = css_local_find_before(
  8488. css_local[k].name, css_local[k].nlen, base);
  8489. if (prev >= 0) {
  8490. css_local[prev] = css_local[k];
  8491. css_local[k].nlen = 0;
  8492. }
  8493. }
  8494. }
  8495. }
  8496. css_env_active = 1;
  8497. }
  8498. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8499. int spec, int spec_imp, short *best, short *best_imp)
  8500. {
  8501. unsigned char p = dcl->prop;
  8502. if (p >= PROP_COUNT)
  8503. return;
  8504. /* A transparent background competes with a colour for one slot. */
  8505. if (p == PROP_BG_NONE)
  8506. p = PROP_BG;
  8507. if (dcl->important) {
  8508. if (spec_imp >= best_imp[p]) {
  8509. apply_decl(st, dcl);
  8510. best_imp[p] = (short)spec_imp;
  8511. }
  8512. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8513. apply_decl(st, dcl);
  8514. best[p] = (short)spec;
  8515. }
  8516. }
  8517. void br_style_for(struct br_doc *d, unsigned int node,
  8518. const struct br_style *parent, struct br_style *out)
  8519. {
  8520. unsigned int tag;
  8521. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8522. unsigned int i;
  8523. if (!cascade_pe && d->nodes[node].style_valid) {
  8524. *out = d->nodes[node].style;
  8525. return;
  8526. }
  8527. *out = *parent;
  8528. out->has_bg = 0;
  8529. out->bg_set = 0;
  8530. out->bg_image = 0;
  8531. out->bg_repeat = 0;
  8532. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8533. out->bg_pos_x = out->bg_pos_y = 0;
  8534. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8535. out->float_left = 0;
  8536. out->clear = 0;
  8537. out->list_item = 0;
  8538. out->font_unit = 0;
  8539. for (i = 0; i < 4; i++) {
  8540. out->border_w[i] = 0;
  8541. out->border_c[i] = 0;
  8542. out->border_s[i] = BR_BORDER_SOLID;
  8543. out->radius[i] = 0;
  8544. }
  8545. out->width = out->max_width = out->min_width = out->height = 0;
  8546. out->min_height = out->max_height = 0;
  8547. out->width_calc_pct = out->width_calc_px = 0;
  8548. out->height_calc_pct = out->height_calc_px = 0;
  8549. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8550. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8551. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8552. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8553. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8554. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8555. out->box_sizing = 0;
  8556. out->ml_auto = out->mr_auto = 0;
  8557. out->mt = out->mr = out->mb = out->ml = 0;
  8558. out->pt = out->pr = out->pb = out->pl = 0;
  8559. for (i = 0; i < 8; i++) {
  8560. out->mp_unit[i] = 0;
  8561. out->mp_calc_px[i] = 0;
  8562. }
  8563. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8564. out->justify = out->align_items = out->align_self = 0;
  8565. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8566. out->grid_auto = 0;
  8567. out->aspect_w = out->aspect_h = 0;
  8568. out->pos = out->left_set = out->top_set = 0;
  8569. out->right_set = out->bottom_set = 0;
  8570. out->left = out->top = out->right = out->bottom = 0;
  8571. out->z_index = 0;
  8572. out->z_set = 0;
  8573. out->tx = out->ty = 0;
  8574. out->transform_set = 0;
  8575. out->transform_scale = 0;
  8576. out->scale_x = out->scale_y = 100;
  8577. out->rotate_x100 = 0;
  8578. out->origin_x = out->origin_y = 50;
  8579. out->origin_x_kind = out->origin_y_kind = 1;
  8580. out->vis_hidden = out->opacity_hidden = 0;
  8581. out->inline_flex = 0;
  8582. out->overflow_hidden = 0;
  8583. out->content = 0;
  8584. /* A pseudo-element is an anonymous inline box: none of the
  8585. * element's UA defaults or presentational hints apply to it. */
  8586. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8587. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8588. BR_DISPLAY_INLINE;
  8589. for (i = 0; i < PROP_COUNT; i++) {
  8590. best[i] = -1;
  8591. best_imp[i] = -1;
  8592. }
  8593. if (!cascade_pe)
  8594. switch (tag) {
  8595. case BR_TAG_HEAD:
  8596. case BR_TAG_TITLE:
  8597. case BR_TAG_META:
  8598. case BR_TAG_LINK:
  8599. case BR_TAG_BASE:
  8600. case BR_TAG_SCRIPT:
  8601. case BR_TAG_STYLE:
  8602. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8603. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8604. out->display = BR_DISPLAY_NONE;
  8605. break;
  8606. case BR_TAG_BODY:
  8607. /* The UA default: the page itself has no gutter, so a site's
  8608. * `body { margin: 0 }` really starts at the canvas edge. */
  8609. out->mt = out->mb = out->ml = out->mr = 8;
  8610. break;
  8611. case BR_TAG_P:
  8612. out->mt = out->mb = 8;
  8613. break;
  8614. case BR_TAG_H1:
  8615. out->font_px = 32; out->bold = 1;
  8616. out->mt = 10; out->mb = 6;
  8617. break;
  8618. case BR_TAG_H2:
  8619. out->font_px = 24; out->bold = 1;
  8620. out->mt = 8; out->mb = 4;
  8621. break;
  8622. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8623. out->bold = 1; out->mt = 6; out->mb = 3;
  8624. if (tag == BR_TAG_H3)
  8625. out->font_px = 19;
  8626. else if (tag == BR_TAG_H4)
  8627. out->font_px = 17;
  8628. else if (tag == BR_TAG_H5)
  8629. out->font_px = 15;
  8630. else
  8631. out->font_px = 13;
  8632. break;
  8633. case BR_TAG_UL: case BR_TAG_OL:
  8634. out->mt = out->mb = 6; out->pl = 20;
  8635. break;
  8636. case BR_TAG_LI:
  8637. out->mt = out->mb = 2;
  8638. out->list_item = 1;
  8639. break;
  8640. case BR_TAG_BLOCKQUOTE:
  8641. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8642. break;
  8643. case BR_TAG_PRE:
  8644. out->mt = out->mb = 8; out->pl = 8;
  8645. out->has_bg = 1; out->bg = theme_panel;
  8646. out->mono = 1;
  8647. break;
  8648. case BR_TAG_CENTER:
  8649. out->align = BR_ALIGN_CENTER;
  8650. break;
  8651. case BR_TAG_A:
  8652. out->color = theme_link; out->underline = 1;
  8653. break;
  8654. case BR_TAG_B: case BR_TAG_STRONG:
  8655. out->bold = 1;
  8656. break;
  8657. case BR_TAG_I: case BR_TAG_EM:
  8658. out->italic = 1;
  8659. break;
  8660. case BR_TAG_U:
  8661. out->underline = 1;
  8662. break;
  8663. case BR_TAG_S: case BR_TAG_STRIKE:
  8664. out->strike = 1;
  8665. break;
  8666. case BR_TAG_CODE:
  8667. out->color = 0x8010;
  8668. out->mono = 1;
  8669. break;
  8670. case BR_TAG_SMALL:
  8671. out->font_px = 13;
  8672. break;
  8673. case BR_TAG_BIG:
  8674. out->font_px = 20;
  8675. break;
  8676. case BR_TAG_TD: case BR_TAG_TH:
  8677. out->pr = 12;
  8678. /* A cell establishes left-aligned content even inside a legacy
  8679. * <center> (HN's outer page wrapper); author CSS/align below
  8680. * can still override this. */
  8681. out->align = BR_ALIGN_LEFT;
  8682. break;
  8683. case BR_TAG_VIDEO:
  8684. out->display = BR_DISPLAY_BLOCK;
  8685. break;
  8686. case BR_TAG_SVG:
  8687. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8688. * external-link icon follows its link text on the same line.
  8689. * Flex rows still treat it as an item (flex_wants_row). */
  8690. out->display = BR_DISPLAY_INLINE;
  8691. break;
  8692. default:
  8693. break;
  8694. }
  8695. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8696. * presentational hints; author CSS still wins because it is applied
  8697. * below. */
  8698. if (!cascade_pe) {
  8699. unsigned short bgc;
  8700. const char *bga = br_find_attr(d, node, "bgcolor");
  8701. const char *ala = br_find_attr(d, node, "align");
  8702. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8703. out->bg = bgc;
  8704. out->has_bg = 1;
  8705. }
  8706. if (ala) {
  8707. if (same_string(ala, "right"))
  8708. out->align = BR_ALIGN_RIGHT;
  8709. else if (same_string(ala, "center"))
  8710. out->align = BR_ALIGN_CENTER;
  8711. else if (same_string(ala, "left"))
  8712. out->align = BR_ALIGN_LEFT;
  8713. }
  8714. }
  8715. /* presentational <font color>: author CSS below still wins */
  8716. if (!cascade_pe && d->nodes[node].color_off) {
  8717. unsigned short c;
  8718. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8719. if (parse_color(v, s_len(v), &c))
  8720. out->color = c;
  8721. }
  8722. /* SVG presentation attributes; CSS declarations still win below. */
  8723. if (!cascade_pe && in_svg(d, node)) {
  8724. const char *fa = br_find_attr(d, node, "fill");
  8725. const char *sa = br_find_attr(d, node, "stroke");
  8726. const char *wa = br_find_attr(d, node, "stroke-width");
  8727. const char *ra = br_find_attr(d, node, "fill-rule");
  8728. const char *ca = br_find_attr(d, node, "color");
  8729. unsigned short c;
  8730. int mode;
  8731. if (ca && parse_color(ca, s_len(ca), &c))
  8732. out->color = c;
  8733. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8734. out->fill_mode = (unsigned char)mode;
  8735. if (mode == BR_PAINT_COLOR)
  8736. out->fill = c;
  8737. }
  8738. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8739. out->stroke_mode = (unsigned char)mode;
  8740. if (mode == BR_PAINT_COLOR)
  8741. out->stroke = c;
  8742. }
  8743. if (wa) {
  8744. int sw = svg_len16(wa, s_len(wa));
  8745. if (sw >= 0)
  8746. out->stroke_w = (unsigned short)sw;
  8747. }
  8748. if (ra)
  8749. out->fill_evenodd = same_string(ra, "evenodd");
  8750. }
  8751. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8752. * Author CSS below can still override this. */
  8753. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8754. unsigned int p = d->nodes[node].parent;
  8755. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8756. !br_find_attr(d, p, "open"))
  8757. out->display = BR_DISPLAY_NONE;
  8758. }
  8759. rule_collect(d, node);
  8760. css_env_build(d);
  8761. for (i = 0; i < nrule_matches; i++) {
  8762. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8763. int spec = rule_matches[i].spec;
  8764. int spec_imp = rule_matches[i].spec_imp;
  8765. unsigned int k;
  8766. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8767. /* Re-expand against this element's variables. */
  8768. struct br_decl decls[BR_DECLS];
  8769. unsigned int nd;
  8770. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8771. decls, &nd);
  8772. for (k = 0; k < nd; k++)
  8773. cascade_decl(out, &decls[k], spec, spec_imp,
  8774. best, best_imp);
  8775. continue;
  8776. }
  8777. for (k = 0; k < r->nd; k++)
  8778. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8779. best_imp);
  8780. }
  8781. css_env_active = 0;
  8782. css_nlocal = 0;
  8783. if (!cascade_pe && d->nodes[node].style_off) {
  8784. struct br_decl decls[BR_DECLS];
  8785. unsigned int nd, k;
  8786. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8787. parse_decls(d, v, s_len(v), decls, &nd);
  8788. for (k = 0; k < nd; k++)
  8789. cascade_decl(out, &decls[k],
  8790. CASCADE_INLINE * CASCADE_BAND,
  8791. CASCADE_INLINE * CASCADE_BAND, best,
  8792. best_imp);
  8793. }
  8794. if (out->font_unit) {
  8795. int px = out->font_raw;
  8796. if (out->font_unit == 2)
  8797. px = parent->font_px * out->font_raw / 100;
  8798. else if (out->font_unit == 3)
  8799. px = root_font_px * out->font_raw / 100;
  8800. else if (out->font_unit == 4)
  8801. px = parent->font_px * out->font_raw / 10000;
  8802. if (px < 8)
  8803. px = 8;
  8804. if (px > 72)
  8805. px = 72;
  8806. out->font_px = (unsigned char)px;
  8807. }
  8808. resolve_size_units(out);
  8809. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  8810. out->display = BR_DISPLAY_NONE;
  8811. if (out->vis_hidden || out->opacity_hidden ||
  8812. (out->overflow_hidden && out->height_kind == 1 &&
  8813. out->height == 0))
  8814. out->display = BR_DISPLAY_NONE;
  8815. if (cascade_pe)
  8816. return;
  8817. d->nodes[node].style = *out;
  8818. d->nodes[node].style_valid = 1;
  8819. }
  8820. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  8821. const struct br_style *elem, struct br_style *out,
  8822. char *buf, unsigned int cap)
  8823. {
  8824. struct br_node *n;
  8825. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  8826. const char *c;
  8827. unsigned int len = 0;
  8828. if (out) {
  8829. out->content = 0;
  8830. out->display = BR_DISPLAY_NONE;
  8831. }
  8832. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  8833. (pe != 1 && pe != 2))
  8834. return 0;
  8835. n = &d->nodes[node];
  8836. if (n->type != BR_NODE_ELEM ||
  8837. ((n->pe_state & chk) && !(n->pe_state & has)))
  8838. return 0;
  8839. cascade_pe = pe;
  8840. br_style_for(d, node, elem, out);
  8841. cascade_pe = 0;
  8842. if (out->content == BR_CONTENT_EMPTY) {
  8843. /* No text; the caller may still draw the box (an icon
  8844. * with a size and a background image). */
  8845. buf[0] = '\0';
  8846. n->pe_state |= chk;
  8847. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  8848. out->pos != 2 && (out->width_kind || out->height_kind) &&
  8849. (out->has_bg || out->bg_image))
  8850. n->pe_state |= has;
  8851. else
  8852. out->display = BR_DISPLAY_NONE;
  8853. return 0;
  8854. }
  8855. if (out->content && out->display != BR_DISPLAY_NONE &&
  8856. out->pos != 1 && out->pos != 2) {
  8857. c = attr_ptr(d, d->content_off[out->content - 1]);
  8858. while (*c && len + 1 < cap) {
  8859. if (*c == '\001') {
  8860. char name[32];
  8861. unsigned int k = 0;
  8862. const char *v;
  8863. c++;
  8864. while (*c && *c != '\002' && k + 1 < sizeof(name))
  8865. name[k++] = *c++;
  8866. name[k] = '\0';
  8867. if (*c == '\002')
  8868. c++;
  8869. v = br_find_attr(d, node, name);
  8870. while (v && *v && len + 1 < cap)
  8871. buf[len++] = *v++;
  8872. continue;
  8873. }
  8874. buf[len++] = *c++;
  8875. }
  8876. }
  8877. buf[len] = '\0';
  8878. n->pe_state |= chk;
  8879. if (len)
  8880. n->pe_state |= has;
  8881. return len;
  8882. }
  8883. /* ---- HTML parser ---- */
  8884. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  8885. unsigned int sp, int *pre)
  8886. {
  8887. unsigned int i;
  8888. *pre = 0;
  8889. for (i = 1; i <= sp; i++)
  8890. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  8891. (*pre)++;
  8892. }
  8893. static void implicit_close(struct br_doc *d, unsigned short *stack,
  8894. unsigned int *sp, unsigned int tag)
  8895. {
  8896. while (*sp > 0) {
  8897. unsigned int top = d->nodes[stack[*sp]].tag;
  8898. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  8899. (*sp)--;
  8900. continue;
  8901. }
  8902. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  8903. (*sp)--;
  8904. continue;
  8905. }
  8906. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  8907. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  8908. (*sp)--;
  8909. continue;
  8910. }
  8911. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  8912. (*sp)--;
  8913. continue;
  8914. }
  8915. break;
  8916. }
  8917. }
  8918. static int has_attr_token(const char *s, unsigned int len, const char *name)
  8919. {
  8920. unsigned int i, n = s_len(name);
  8921. for (i = 0; i + n <= len; i++) {
  8922. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  8923. continue;
  8924. if (!ci_eq_n(s + i, name, n))
  8925. continue;
  8926. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  8927. return 1;
  8928. }
  8929. return 0;
  8930. }
  8931. static int stack_has_svg(const struct br_doc *d,
  8932. const unsigned short *stack, unsigned int sp)
  8933. {
  8934. unsigned int k;
  8935. for (k = 0; k <= sp && k < 64; k++)
  8936. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  8937. return 1;
  8938. return 0;
  8939. }
  8940. /* Skip an element subtree by finding its closing tag. Used for SVG
  8941. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  8942. * overwrite the document title. */
  8943. static unsigned int skip_element(const char *html, unsigned int len,
  8944. unsigned int i, const char *name,
  8945. unsigned int n)
  8946. {
  8947. char close[40];
  8948. unsigned int k = 0, p;
  8949. close[k++] = '<';
  8950. close[k++] = '/';
  8951. while (k < sizeof(close) - 1 && n--)
  8952. close[k++] = *name++;
  8953. p = (unsigned int)find_ci(html, len, close, k, i);
  8954. if ((int)p < 0)
  8955. return len;
  8956. p += k;
  8957. while (p < len && html[p] != '>')
  8958. p++;
  8959. if (p < len)
  8960. p++;
  8961. return p;
  8962. }
  8963. /* Store a <script src=...> value in the attribute arena. Kept separate from
  8964. * parse_attrs() so script-only attributes cannot be confused with the
  8965. * value/d attributes that also use src_off on other elements. */
  8966. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  8967. unsigned int len)
  8968. {
  8969. unsigned int i = 0;
  8970. while (i < len) {
  8971. unsigned int ns, ne, vs, ve;
  8972. char quote = 0;
  8973. while (i < len && is_space(s[i]))
  8974. i++;
  8975. if (i >= len || s[i] == '/')
  8976. break;
  8977. ns = i;
  8978. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  8979. i++;
  8980. ne = i;
  8981. while (i < len && is_space(s[i]))
  8982. i++;
  8983. if (i >= len || s[i] != '=')
  8984. continue;
  8985. i++;
  8986. while (i < len && is_space(s[i]))
  8987. i++;
  8988. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8989. quote = s[i++];
  8990. vs = i;
  8991. while (i < len && s[i] != quote)
  8992. i++;
  8993. ve = i;
  8994. if (i < len)
  8995. i++;
  8996. } else {
  8997. vs = i;
  8998. while (i < len && !is_space(s[i]) && s[i] != '>')
  8999. i++;
  9000. ve = i;
  9001. }
  9002. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  9003. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  9004. }
  9005. return 0;
  9006. }
  9007. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  9008. * application/json, text/template, ...) and non-JS type values. External
  9009. * src scripts are allowed; the browser fetches them separately. Returns 1
  9010. * for a classic script, 2 for type="module", 0 to skip (also for a
  9011. * `nomodule` fallback, since module scripts run). */
  9012. static int script_is_javascript(const char *s, unsigned int len)
  9013. {
  9014. int kind = 1;
  9015. unsigned int i = 0;
  9016. while (i < len) {
  9017. unsigned int ns, ne, vs = 0, ve = 0;
  9018. char quote = 0;
  9019. int has_val = 0;
  9020. while (i < len && is_space(s[i]))
  9021. i++;
  9022. if (i >= len)
  9023. break;
  9024. ns = i;
  9025. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  9026. i++;
  9027. ne = i;
  9028. while (i < len && is_space(s[i]))
  9029. i++;
  9030. if (i < len && s[i] == '=') {
  9031. i++;
  9032. while (i < len && is_space(s[i]))
  9033. i++;
  9034. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9035. quote = s[i++];
  9036. vs = i;
  9037. while (i < len && s[i] != quote)
  9038. i++;
  9039. ve = i;
  9040. if (i < len)
  9041. i++;
  9042. } else {
  9043. vs = i;
  9044. while (i < len && !is_space(s[i]) && s[i] != '/')
  9045. i++;
  9046. ve = i;
  9047. }
  9048. has_val = 1;
  9049. } else if (i == ne) {
  9050. i++;
  9051. }
  9052. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  9053. return 0;
  9054. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  9055. unsigned int vn = has_val ? ve - vs : 0;
  9056. if (vn == 0)
  9057. continue;
  9058. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  9059. kind = 2;
  9060. continue;
  9061. }
  9062. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  9063. (vn == 22 &&
  9064. ci_eq_n(s + vs, "application/javascript", 22)) ||
  9065. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  9066. (vn == 22 &&
  9067. ci_eq_n(s + vs, "application/ecmascript", 22)))
  9068. continue;
  9069. return 0;
  9070. }
  9071. }
  9072. return kind;
  9073. }
  9074. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  9075. {
  9076. unsigned short stack[64];
  9077. unsigned int sp = 0;
  9078. unsigned int i = 0;
  9079. int pre = 0;
  9080. unsigned int css_len = 0;
  9081. if (!d)
  9082. return -1;
  9083. if (!d->nodes)
  9084. br_doc_init(d);
  9085. if (!d->nodes || !d->text || !d->attrs)
  9086. return -1;
  9087. doc_reset(d);
  9088. d->script_src = html;
  9089. stack[0] = 0;
  9090. d->nodes[0].type = BR_NODE_ELEM;
  9091. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9092. while (i < len) {
  9093. if (html[i] != '<') {
  9094. unsigned int st = i;
  9095. while (i < len && html[i] != '<')
  9096. i++;
  9097. if (sp <= 63)
  9098. append_text(d, stack[sp], html + st, i - st, pre);
  9099. continue;
  9100. }
  9101. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  9102. html[i + 3] == '-') {
  9103. int p = find_ci(html, len, "-->", 3, i + 4);
  9104. i = p >= 0 ? (unsigned int)p + 3 : len;
  9105. continue;
  9106. }
  9107. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  9108. while (i < len && html[i] != '>')
  9109. i++;
  9110. if (i < len)
  9111. i++;
  9112. continue;
  9113. }
  9114. if (i + 1 < len && html[i + 1] == '/') {
  9115. unsigned int ns = i + 2, ne, tag, k, n;
  9116. char name[32];
  9117. while (ns < len && is_space(html[ns]))
  9118. ns++;
  9119. ne = ns;
  9120. while (ne < len && is_ident(html[ne]))
  9121. ne++;
  9122. if (ne == ns) {
  9123. i++;
  9124. continue;
  9125. }
  9126. n = ne - ns;
  9127. if (n >= sizeof(name))
  9128. n = sizeof(name) - 1;
  9129. for (k = 0; k < n; k++)
  9130. name[k] = html[ns + k];
  9131. name[n] = '\0';
  9132. tag = br_tag_from_name(name);
  9133. i = ne;
  9134. while (i < len && html[i] != '>')
  9135. i++;
  9136. if (i < len)
  9137. i++;
  9138. for (k = sp; k > 0; k--) {
  9139. struct br_node *nd = &d->nodes[stack[k]];
  9140. if (tag == BR_TAG_UNKNOWN) {
  9141. const char *raw = attr_ptr(d, nd->name_off);
  9142. if (nd->tag == BR_TAG_UNKNOWN &&
  9143. ci_eq_n(raw, name, n) &&
  9144. raw[n] == '\0') {
  9145. sp = k - 1;
  9146. break;
  9147. }
  9148. } else if (nd->tag == tag) {
  9149. sp = k - 1;
  9150. break;
  9151. }
  9152. }
  9153. stack_recount_pre(d, stack, sp, &pre);
  9154. continue;
  9155. }
  9156. /* start tag */
  9157. {
  9158. unsigned int ns = i + 1, ne, tag, k, n;
  9159. char name[16];
  9160. unsigned int attr_start, attr_end;
  9161. int selfclose = 0;
  9162. while (ns < len && is_space(html[ns]))
  9163. ns++;
  9164. ne = ns;
  9165. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  9166. ne++;
  9167. if (ne == ns) {
  9168. /* stray '<' */
  9169. append_text(d, stack[sp], html + i, 1, pre);
  9170. i++;
  9171. continue;
  9172. }
  9173. n = ne - ns;
  9174. if (n >= sizeof(name))
  9175. n = sizeof(name) - 1;
  9176. for (k = 0; k < n; k++)
  9177. name[k] = html[ns + k];
  9178. name[n] = '\0';
  9179. tag = br_tag_from_name(name);
  9180. attr_start = ne;
  9181. {
  9182. unsigned int q = ne;
  9183. char quote = 0;
  9184. while (q < len) {
  9185. char c = html[q];
  9186. if (quote) {
  9187. if (c == quote)
  9188. quote = 0;
  9189. } else if (c == '"' || c == '\'') {
  9190. quote = c;
  9191. } else if (c == '>') {
  9192. break;
  9193. }
  9194. q++;
  9195. }
  9196. attr_end = q;
  9197. /* `/>` closes the tag only when the slash is
  9198. * not the end of an unquoted attribute value:
  9199. * Docusaurus writes `<a href=/installation/>`,
  9200. * which used to self-close the link and leave
  9201. * its label outside it. */
  9202. if (q > attr_start && html[q - 1] == '/' &&
  9203. (q - 1 == attr_start ||
  9204. is_space(html[q - 2]) ||
  9205. html[q - 2] == '"' || html[q - 2] == '\''))
  9206. selfclose = 1;
  9207. i = q < len ? q + 1 : len;
  9208. }
  9209. if (tag == BR_TAG_SCRIPT) {
  9210. int p = find_ci(html, len, "</script", 8, i);
  9211. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9212. unsigned int idx;
  9213. struct br_script *script = 0;
  9214. int kind = script_is_javascript(html + attr_start,
  9215. attr_end - attr_start);
  9216. if (kind) {
  9217. if (d->nscripts < BR_MAX_SCRIPTS) {
  9218. script = &d->scripts[d->nscripts++];
  9219. script->module = kind == 2;
  9220. script->node = 0;
  9221. script->off = i;
  9222. script->len = end > i ? end - i : 0;
  9223. script->src_off = script_src_attr(
  9224. d, html + attr_start,
  9225. attr_end - attr_start);
  9226. } else {
  9227. d->scripts_truncated = 1;
  9228. }
  9229. }
  9230. /* Keep the element in the DOM even though its body is
  9231. * executed separately: scripts (Piwik, analytics)
  9232. * insert a sibling after getElementsByTagName('script')
  9233. * [0], and the element must exist for that to work. */
  9234. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9235. (unsigned char)tag);
  9236. if (idx)
  9237. parse_attrs(d, &d->nodes[idx],
  9238. html + attr_start,
  9239. attr_end - attr_start);
  9240. if (script)
  9241. script->node = idx;
  9242. if (idx && !kind && end > i)
  9243. append_raw_text(d, idx, html + i, end - i);
  9244. i = p >= 0 ? (unsigned int)p + 8 : len;
  9245. while (i < len && html[i] != '>')
  9246. i++;
  9247. if (i < len)
  9248. i++;
  9249. continue;
  9250. }
  9251. /* Scripting is always enabled, so <noscript> is a
  9252. * raw-text element (HTML "in head/in body, scripting
  9253. * flag set"): its markup must not reach the DOM or the
  9254. * cascade. google.com puts a meta refresh and
  9255. * `table,div,span,p{display:none}` in one, which hid
  9256. * the whole page and left only the "click here" link. */
  9257. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9258. int p = find_ci(html, len, "</noscript", 10, i);
  9259. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9260. unsigned int idx;
  9261. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9262. (unsigned char)tag);
  9263. if (idx) {
  9264. parse_attrs(d, &d->nodes[idx],
  9265. html + attr_start,
  9266. attr_end - attr_start);
  9267. if (end > i)
  9268. append_raw_text(d, idx, html + i,
  9269. end - i);
  9270. }
  9271. i = p >= 0 ? (unsigned int)p + 10 : len;
  9272. while (i < len && html[i] != '>')
  9273. i++;
  9274. if (i < len)
  9275. i++;
  9276. continue;
  9277. }
  9278. if (stack_has_svg(d, stack, sp) &&
  9279. (tag == BR_TAG_TITLE ||
  9280. (tag == BR_TAG_UNKNOWN &&
  9281. (ci_eq_n(name, "desc", 4) ||
  9282. ci_eq_n(name, "metadata", 8))))) {
  9283. i = skip_element(html, len, i, name, n);
  9284. continue;
  9285. }
  9286. /* A closed <details> is collapsed at style time, not
  9287. * parse time: the <summary> (the hamburger button on
  9288. * tagesschau and many mobile menus) must stay in the
  9289. * DOM. */
  9290. if (tag == BR_TAG_STYLE) {
  9291. int p = find_ci(html, len, "</style", 7, i);
  9292. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9293. unsigned int cend = end;
  9294. unsigned int idx;
  9295. while (cend > i && is_space(html[cend - 1]))
  9296. cend--;
  9297. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9298. for (k = i; k < cend; k++)
  9299. css_store[css_len++] = html[k];
  9300. css_store[css_len++] = '\n';
  9301. }
  9302. /* The CSS is collected above, but keep a display:none
  9303. * element so getElementsByTagName('style')/parentNode
  9304. * behave like a real document. */
  9305. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9306. (unsigned char)tag);
  9307. if (idx)
  9308. parse_attrs(d, &d->nodes[idx],
  9309. html + attr_start,
  9310. attr_end - attr_start);
  9311. i = p >= 0 ? (unsigned int)p + 7 : len;
  9312. while (i < len && html[i] != '>')
  9313. i++;
  9314. if (i < len)
  9315. i++;
  9316. continue;
  9317. }
  9318. if (tag == BR_TAG_TITLE) {
  9319. int p = find_ci(html, len, "</title", 7, i);
  9320. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9321. unsigned int tn = 0;
  9322. char tmp[BR_TITLE_MAX];
  9323. tn = decode_text(html + i, end - i, tmp,
  9324. sizeof(tmp), 1);
  9325. (void)tn;
  9326. s_copy(d->title, tmp, sizeof(d->title));
  9327. i = p >= 0 ? (unsigned int)p + 7 : len;
  9328. while (i < len && html[i] != '>')
  9329. i++;
  9330. if (i < len)
  9331. i++;
  9332. continue;
  9333. }
  9334. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9335. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9336. unsigned int idx;
  9337. implicit_close(d, stack, &sp, tag);
  9338. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9339. (unsigned char)tag);
  9340. if (idx)
  9341. parse_attrs(d, &d->nodes[idx],
  9342. html + attr_start,
  9343. attr_end - attr_start);
  9344. continue;
  9345. }
  9346. if (sp >= 63) {
  9347. d->truncated = 1;
  9348. continue;
  9349. }
  9350. implicit_close(d, stack, &sp, tag);
  9351. {
  9352. unsigned int idx = node_append(d, stack[sp],
  9353. BR_NODE_ELEM,
  9354. (unsigned char)tag);
  9355. if (!idx)
  9356. continue;
  9357. parse_attrs(d, &d->nodes[idx],
  9358. html + attr_start,
  9359. attr_end - attr_start);
  9360. if (tag == BR_TAG_UNKNOWN)
  9361. d->nodes[idx].name_off =
  9362. store_attr(d, name, n, 31);
  9363. /* HTML ignores the self-closing flag on a
  9364. * normal element (`<div/>` opens a div); only
  9365. * SVG content (and unknown/custom tags, kept
  9366. * as before) honours it. */
  9367. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9368. tag != BR_TAG_SVG &&
  9369. !stack_has_svg(d, stack, sp))
  9370. selfclose = 0;
  9371. if (!selfclose) {
  9372. sp++;
  9373. stack[sp] = (unsigned short)idx;
  9374. if (tag == BR_TAG_PRE)
  9375. pre++;
  9376. }
  9377. }
  9378. }
  9379. }
  9380. d->inline_css_len = css_len;
  9381. return 0;
  9382. }
  9383. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9384. {
  9385. if (!d->nodes)
  9386. br_doc_init(d);
  9387. if (!d->nodes || !d->text || !d->attrs)
  9388. return -1;
  9389. doc_reset(d);
  9390. d->plain = 1;
  9391. d->nodes[0].type = BR_NODE_ELEM;
  9392. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9393. append_text(d, 0, text, len, 1);
  9394. return 0;
  9395. }
  9396. /* A standalone image response: a synthetic document with one <img>. The
  9397. * caller decodes the already-fetched body into image slot 0 and sets the
  9398. * returned node's img_index. */
  9399. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9400. {
  9401. unsigned int idx;
  9402. if (!d->nodes)
  9403. br_doc_init(d);
  9404. if (!d->nodes || !d->text || !d->attrs)
  9405. return 0;
  9406. doc_reset(d);
  9407. d->nodes[0].type = BR_NODE_ELEM;
  9408. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9409. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9410. if (!idx)
  9411. return 0;
  9412. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9413. return idx;
  9414. }