browser_html.c 263 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395
  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. /* href is stored even when empty: `<a href="">` is a link to the page
  542. * itself (a hand cursor, a click target; scripts usually cancel the
  543. * click, like Bootstrap's data-toggle="modal"). Offset 0 would mean "no
  544. * href", so an empty value gets its own NUL byte. */
  545. static unsigned int store_href(struct br_doc *d, const char *s,
  546. unsigned int len)
  547. {
  548. unsigned int off;
  549. if (len)
  550. return store_attr(d, s, len, BR_URL_ATTR_MAX);
  551. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  552. return 0;
  553. off = d->attrs_used;
  554. d->attrs[d->attrs_used++] = '\0';
  555. return off;
  556. }
  557. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  558. {
  559. return off ? d->attrs + off : "";
  560. }
  561. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  562. static unsigned int store_raw(struct br_doc *d, const char *s,
  563. unsigned int len, unsigned int limit)
  564. {
  565. unsigned int off;
  566. if (len > limit)
  567. len = limit;
  568. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  569. return 0;
  570. off = d->attrs_used;
  571. {
  572. unsigned int i;
  573. for (i = 0; i < len; i++)
  574. d->attrs[off + i] = s[i];
  575. }
  576. d->attrs[off + len] = '\0';
  577. d->attrs_used += len + 1;
  578. return off;
  579. }
  580. #define VAR_HASH_SIZE 1024
  581. static unsigned int var_hash(const char *name, unsigned int len)
  582. {
  583. unsigned int i, h = 2166136261u;
  584. for (i = 0; i < len; i++) {
  585. h ^= (unsigned char)name[i];
  586. h *= 16777619u;
  587. }
  588. return h & (VAR_HASH_SIZE - 1);
  589. }
  590. static unsigned int css_var_find(struct br_doc *d, const char *name,
  591. unsigned int nlen)
  592. {
  593. unsigned int h = var_hash(name, nlen);
  594. for (;;) {
  595. unsigned int i = d->var_slot[h];
  596. if (!i)
  597. return BR_MAX_VARS;
  598. i--;
  599. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  600. const char *n = attr_ptr(d, d->var_name[i]);
  601. unsigned int j;
  602. for (j = 0; j < nlen && n[j] == name[j]; j++)
  603. ;
  604. if (j == nlen)
  605. return i;
  606. }
  607. h = (h + 1) & (VAR_HASH_SIZE - 1);
  608. }
  609. }
  610. #define BR_VAR_ROOT 1
  611. #define BR_VAR_CONFLICT 2
  612. #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
  613. /* Custom properties live in one document-wide table (var() is expanded
  614. * when a sheet is parsed, not per element). Definitions on :root, html,
  615. * body or * are the theme and always apply, last one winning. A definition
  616. * on any other selector is a local override: it is used while it is the
  617. * only value seen for that name, and a second, different local value marks
  618. * the name ambiguous so var() falls back. Last-wins across components was
  619. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  620. * .fa-fw, and every icon became 100% wide. */
  621. static void css_var_set(struct br_doc *d, const char *name,
  622. unsigned int nlen, const char *val, unsigned int vlen,
  623. int root)
  624. {
  625. unsigned int i, noff, voff;
  626. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  627. return;
  628. i = css_var_find(d, name, nlen);
  629. if (i < BR_MAX_VARS && !root) {
  630. const char *old = attr_ptr(d, d->var_val[i]);
  631. unsigned int k = 0;
  632. d->var_flags[i] |= BR_VAR_SCOPED;
  633. if (d->var_flags[i] & BR_VAR_ROOT)
  634. return;
  635. while (k < vlen && old[k] == val[k])
  636. k++;
  637. if (!(k == vlen && old[k] == '\0'))
  638. d->var_flags[i] |= BR_VAR_CONFLICT;
  639. return;
  640. }
  641. noff = store_raw(d, name, nlen, 63);
  642. voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
  643. if (!noff || !voff)
  644. return;
  645. if (i >= BR_MAX_VARS) {
  646. unsigned int h;
  647. if (d->nvars >= BR_MAX_VARS)
  648. return;
  649. i = d->nvars++;
  650. d->var_name[i] = noff;
  651. h = var_hash(name, nlen);
  652. while (d->var_slot[h])
  653. h = (h + 1) & (VAR_HASH_SIZE - 1);
  654. d->var_slot[h] = (unsigned short)(i + 1);
  655. d->var_flags[i] = 0;
  656. }
  657. d->var_val[i] = voff;
  658. if (root)
  659. d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
  660. (d->var_flags[i] & BR_VAR_SCOPED));
  661. else
  662. d->var_flags[i] |= BR_VAR_SCOPED;
  663. }
  664. /* 1 when every selector of the list targets the document root (the theme
  665. * scope for custom properties): :root, html, body, * or :host, optionally
  666. * with classes/attributes but no combinator. */
  667. static int css_root_selector(const char *s, unsigned int len)
  668. {
  669. unsigned int i = 0;
  670. while (i < len) {
  671. unsigned int st, en;
  672. while (i < len && (is_space(s[i]) || s[i] == ','))
  673. i++;
  674. if (i >= len)
  675. break;
  676. st = i;
  677. while (i < len && s[i] != ',')
  678. i++;
  679. en = i;
  680. while (en > st && is_space(s[en - 1]))
  681. en--;
  682. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  683. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  684. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  685. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  686. s[st] == '*'))
  687. return 0;
  688. for (; st < en; st++)
  689. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  690. s[st] == '~')
  691. return 0;
  692. }
  693. return 1;
  694. }
  695. /* Find the end of a declaration value: the next ';' outside quotes and
  696. * parentheses. The caller passes the position just after the ':'. */
  697. static unsigned int css_value_end(const char *s, unsigned int len,
  698. unsigned int i)
  699. {
  700. int depth = 0;
  701. char quote = 0;
  702. while (i < len) {
  703. char c = s[i];
  704. if (quote) {
  705. if (c == quote)
  706. quote = 0;
  707. else if (c == '\\' && i + 1 < len)
  708. i++;
  709. } else if (c == '"' || c == '\'') {
  710. quote = c;
  711. } else if (c == '(') {
  712. depth++;
  713. } else if (c == ')' && depth > 0) {
  714. depth--;
  715. } else if (c == ';' && depth == 0) {
  716. break;
  717. }
  718. i++;
  719. }
  720. return i;
  721. }
  722. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  723. * second palette. We render the light page, so skip their custom-property
  724. * definitions; otherwise their last-wins values leak into the light rules. */
  725. static int css_dark_selector(const char *s, unsigned int len)
  726. {
  727. unsigned int i;
  728. for (i = 0; i + 4 <= len; i++) {
  729. if ((s[i] == 'n' || s[i] == 'N') &&
  730. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  731. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  732. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  733. (s[i + 4] == 't' || s[i + 4] == 'T'))
  734. return 1;
  735. if ((s[i] == 'd' || s[i] == 'D') &&
  736. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  737. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  738. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  739. return 1;
  740. if (i + 12 <= len &&
  741. (s[i] == 'c' || s[i] == 'C') &&
  742. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  743. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  744. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  745. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  746. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  747. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  748. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  749. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  750. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  751. s[i + 10] == '-' &&
  752. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  753. (s[i + 12] == 's' || s[i + 12] == 'S'))
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. /* Collect --custom-property definitions from one declaration block. */
  759. static void css_collect_block(struct br_doc *d, const char *s,
  760. unsigned int len, int root)
  761. {
  762. unsigned int i = 0;
  763. while (i < len) {
  764. unsigned int pstart, pend, vstart, vend;
  765. while (i < len && (is_space(s[i]) || s[i] == ';'))
  766. i++;
  767. if (i >= len)
  768. break;
  769. pstart = i;
  770. while (i < len && s[i] != ':' && s[i] != ';')
  771. i++;
  772. pend = i;
  773. if (i >= len || s[i] != ':') {
  774. if (i < len)
  775. i++;
  776. continue;
  777. }
  778. i++;
  779. vstart = i;
  780. i = css_value_end(s, len, i);
  781. vend = i;
  782. if (i < len)
  783. i++;
  784. while (pstart < pend && is_space(s[pstart]))
  785. pstart++;
  786. while (pend > pstart && is_space(s[pend - 1]))
  787. pend--;
  788. while (vstart < vend && is_space(s[vstart]))
  789. vstart++;
  790. while (vend > vstart && is_space(s[vend - 1]))
  791. vend--;
  792. if (vend - vstart >= 10 &&
  793. ci_eq_n(s + vend - 10, "!important", 10)) {
  794. vend -= 10;
  795. while (vend > vstart && is_space(s[vend - 1]))
  796. vend--;
  797. }
  798. if (pend - pstart >= 3 && s[pstart] == '-' &&
  799. s[pstart + 1] == '-')
  800. css_var_set(d, s + pstart, pend - pstart,
  801. s + vstart, vend - vstart, root);
  802. }
  803. }
  804. const char *br_href(struct br_doc *d, unsigned int node)
  805. {
  806. return attr_ptr(d, d->nodes[node].href_off);
  807. }
  808. const char *br_src(struct br_doc *d, unsigned int node)
  809. {
  810. return attr_ptr(d, d->nodes[node].src_off);
  811. }
  812. const char *br_alt(struct br_doc *d, unsigned int node)
  813. {
  814. return attr_ptr(d, d->nodes[node].alt_off);
  815. }
  816. const char *br_rel(struct br_doc *d, unsigned int node)
  817. {
  818. return attr_ptr(d, d->nodes[node].rel_off);
  819. }
  820. const char *br_value(struct br_doc *d, unsigned int node)
  821. {
  822. return attr_ptr(d, d->nodes[node].src_off);
  823. }
  824. const char *br_placeholder(struct br_doc *d, unsigned int node)
  825. {
  826. return attr_ptr(d, d->nodes[node].alt_off);
  827. }
  828. const char *br_type(struct br_doc *d, unsigned int node)
  829. {
  830. return attr_ptr(d, d->nodes[node].rel_off);
  831. }
  832. /* ---- document setup / node append ---- */
  833. static void rule_index_reset(struct br_doc *d)
  834. {
  835. unsigned int i;
  836. for (i = 0; i < BR_MAX_RULES; i++)
  837. d->rule_next[i] = 0xFFFF;
  838. for (i = 0; i < BR_RULE_HASH; i++)
  839. d->rule_bucket[i] = 0xFFFF;
  840. for (i = 0; i < BR_TAG_MAX; i++)
  841. d->rule_tag[i] = 0xFFFF;
  842. d->rule_any = 0xFFFF;
  843. }
  844. void br_doc_init(struct br_doc *d)
  845. {
  846. unsigned int i;
  847. for (i = 0; i < sizeof(*d); i++)
  848. ((unsigned char *)d)[i] = 0;
  849. rule_index_reset(d);
  850. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  851. d->text = malloc(BR_TEXT_SIZE);
  852. d->attrs = malloc(BR_ATTR_SIZE);
  853. if (!d->nodes || !d->text || !d->attrs) {
  854. free(d->nodes);
  855. free(d->text);
  856. free(d->attrs);
  857. d->nodes = 0;
  858. d->text = 0;
  859. d->attrs = 0;
  860. return;
  861. }
  862. d->rules = rule_fallback;
  863. d->rule_cap = BR_RULE_FALLBACK;
  864. d->attrs_used = 1; /* offset 0 = "no attribute" */
  865. d->attrs[0] = '\0';
  866. d->nvars = 0;
  867. d->nlayers = 0;
  868. d->root = 0;
  869. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  870. d->var_slot[vi] = 0;
  871. root_font_px = 16;
  872. bg_url_count = 0;
  873. bg_url_pool_used = 0;
  874. font_reset();
  875. }
  876. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  877. unsigned int cap)
  878. {
  879. if (!buf || !cap) {
  880. d->rules = rule_fallback;
  881. d->rule_cap = BR_RULE_FALLBACK;
  882. d->nrules = 0;
  883. return;
  884. }
  885. if (cap > BR_MAX_RULES)
  886. cap = BR_MAX_RULES;
  887. d->rules = buf;
  888. d->rule_cap = cap;
  889. d->nrules = 0;
  890. }
  891. static void doc_reset(struct br_doc *d)
  892. {
  893. unsigned int i;
  894. d->nnode = 0;
  895. d->text_used = 0;
  896. d->attrs_used = 1;
  897. d->attrs[0] = '\0';
  898. d->nrules = 0;
  899. d->ncontent = 0;
  900. d->npe_rules = 0;
  901. rule_index_reset(d);
  902. d->title[0] = '\0';
  903. d->inline_css_len = 0;
  904. d->nvars = 0;
  905. d->nlayers = 0;
  906. d->truncated = 0;
  907. d->plain = 0;
  908. d->script_src = 0;
  909. d->nscripts = 0;
  910. d->scripts_truncated = 0;
  911. font_reset();
  912. for (i = 0; i < BR_MAX_NODES; i++) {
  913. d->nodes[i].type = BR_NODE_ELEM;
  914. d->nodes[i].tag = BR_TAG_UNKNOWN;
  915. d->nodes[i].flags = 0;
  916. /* The cached disabled/open/inert bits must not survive into
  917. * the next page: a slot that held a disabled control made
  918. * Docusaurus' search field unfocusable after navigating (and
  919. * real hardware does not hand out zeroed memory). */
  920. d->nodes[i].attr_flags = 0;
  921. d->nodes[i].parent = 0;
  922. d->nodes[i].first = d->nodes[i].last = 0;
  923. d->nodes[i].next = 0;
  924. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  925. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  926. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  927. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  928. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  929. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  930. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  931. d->nodes[i].name_off = 0;
  932. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  933. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  934. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  935. d->nodes[i].svg_rx = 0;
  936. d->nodes[i].img_index = -1;
  937. d->nodes[i].bg_img_index = -1;
  938. d->nodes[i].style_valid = 0;
  939. d->nodes[i].pe_state = 0;
  940. }
  941. }
  942. static void node_zero(struct br_node *n);
  943. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  944. unsigned char type, unsigned char tag)
  945. {
  946. unsigned int idx;
  947. struct br_node *n;
  948. if (d->nnode >= BR_MAX_NODES) {
  949. d->truncated = 1;
  950. return 0;
  951. }
  952. idx = d->nnode++;
  953. n = &d->nodes[idx];
  954. node_zero(n); /* every field, attr_flags included */
  955. n->type = type;
  956. n->tag = tag;
  957. n->parent = (unsigned short)parent;
  958. if (d->nodes[parent].last)
  959. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  960. else
  961. d->nodes[parent].first = (unsigned short)idx;
  962. d->nodes[parent].last = (unsigned short)idx;
  963. return idx;
  964. }
  965. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  966. const char *s, unsigned int len, int pre)
  967. {
  968. char *dst;
  969. unsigned int n;
  970. unsigned int idx;
  971. if (len == 0)
  972. return 0;
  973. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  974. d->truncated = 1;
  975. return 0;
  976. }
  977. dst = d->text + d->text_used;
  978. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  979. if (n == 0) {
  980. d->text[d->text_used] = '\0';
  981. return 0;
  982. }
  983. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  984. if (!idx)
  985. return 0;
  986. d->nodes[idx].text_off = d->text_used;
  987. d->nodes[idx].text_len = (unsigned short)n;
  988. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  989. d->text_used += n + 1;
  990. return idx;
  991. }
  992. /* Raw text child (no entity decoding, whitespace kept): the body of a
  993. * data-block <script> such as type="application/json", which scripts read
  994. * through textContent. Bodies longer than a text node holds are dropped. */
  995. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  996. const char *s, unsigned int len)
  997. {
  998. unsigned int idx, i;
  999. if (len == 0 || len > 0xFFFFu)
  1000. return 0;
  1001. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  1002. d->truncated = 1;
  1003. return 0;
  1004. }
  1005. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  1006. if (!idx)
  1007. return 0;
  1008. for (i = 0; i < len; i++)
  1009. d->text[d->text_used + i] = s[i];
  1010. d->text[d->text_used + len] = '\0';
  1011. d->nodes[idx].text_off = d->text_used;
  1012. d->nodes[idx].text_len = (unsigned short)len;
  1013. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  1014. d->text_used += len + 1;
  1015. return idx;
  1016. }
  1017. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  1018. static int same_string(const char *a, const char *b)
  1019. {
  1020. while (*a && *a == *b) {
  1021. a++;
  1022. b++;
  1023. }
  1024. return *a == *b;
  1025. }
  1026. static int same_string_n(const char *a, const char *b, unsigned int n)
  1027. {
  1028. unsigned int i;
  1029. for (i = 0; i < n; i++)
  1030. if (a[i] != b[i])
  1031. return 0;
  1032. return 1;
  1033. }
  1034. static int node_detached(struct br_doc *d, unsigned int node)
  1035. {
  1036. unsigned int guard = 0;
  1037. while (node && node < d->nnode && guard++ <= d->nnode) {
  1038. if (d->nodes[node].flags & BR_NODE_DETACHED)
  1039. return 1;
  1040. node = d->nodes[node].parent;
  1041. }
  1042. return 0;
  1043. }
  1044. int br_node_detached(struct br_doc *d, unsigned int node)
  1045. {
  1046. return !d || node >= d->nnode || node_detached(d, node);
  1047. }
  1048. unsigned int br_find_id(struct br_doc *d, const char *id)
  1049. {
  1050. unsigned int i;
  1051. if (!d || !id)
  1052. return 0;
  1053. for (i = 0; i < d->nnode; i++) {
  1054. struct br_node *n = &d->nodes[i];
  1055. if (n->type == BR_NODE_ELEM && n->id_off &&
  1056. same_string(d->attrs + n->id_off, id) &&
  1057. !node_detached(d, i))
  1058. return i + 1;
  1059. }
  1060. return 0;
  1061. }
  1062. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1063. unsigned int cap, unsigned int *used)
  1064. {
  1065. unsigned int c;
  1066. if (node >= d->nnode)
  1067. return;
  1068. if (d->nodes[node].type == BR_NODE_TEXT) {
  1069. unsigned int len = d->nodes[node].text_len;
  1070. unsigned int off = d->nodes[node].text_off;
  1071. if (*used + len >= cap)
  1072. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1073. for (c = 0; c < len; c++)
  1074. out[*used + c] = d->text[off + c];
  1075. *used += len;
  1076. return;
  1077. }
  1078. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1079. text_walk(d, c, out, cap, used);
  1080. }
  1081. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1082. char *out, unsigned int cap)
  1083. {
  1084. unsigned int used = 0;
  1085. if (!d || !out || cap == 0 || node >= d->nnode)
  1086. return 0;
  1087. text_walk(d, node, out, cap, &used);
  1088. out[used] = '\0';
  1089. return used;
  1090. }
  1091. int br_set_text_content(struct br_doc *d, unsigned int node,
  1092. const char *text, unsigned int len)
  1093. {
  1094. struct br_node *n;
  1095. unsigned int idx;
  1096. unsigned int i;
  1097. if (!d || node >= d->nnode)
  1098. return -1;
  1099. /* Detach the old subtree; its arena storage is reclaimed on the next
  1100. * navigation. */
  1101. {
  1102. unsigned int c;
  1103. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1104. d->nodes[c].flags |= BR_NODE_DETACHED;
  1105. }
  1106. d->nodes[node].first = d->nodes[node].last = 0;
  1107. d->nodes[node].style_valid = 0;
  1108. d->nodes[node].pe_state = 0;
  1109. if (len == 0)
  1110. return 0;
  1111. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1112. d->truncated = 1;
  1113. return -1;
  1114. }
  1115. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1116. if (!idx)
  1117. return -1;
  1118. for (i = 0; i < len; i++)
  1119. d->text[d->text_used + i] = text[i];
  1120. d->text[d->text_used + len] = '\0';
  1121. n = &d->nodes[idx];
  1122. n->text_off = d->text_used;
  1123. n->text_len = (unsigned short)len;
  1124. n->flags = 0;
  1125. d->text_used += len + 1;
  1126. return 0;
  1127. }
  1128. /* Replace the value of an existing text or comment node in place, keeping the
  1129. * node handle (and therefore the JS wrapper) valid. */
  1130. int br_set_text_node(struct br_doc *d, unsigned int node,
  1131. const char *text, unsigned int len)
  1132. {
  1133. struct br_node *n;
  1134. unsigned int i;
  1135. if (!d || node >= d->nnode)
  1136. return -1;
  1137. n = &d->nodes[node];
  1138. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1139. return -1;
  1140. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1141. d->truncated = 1;
  1142. return -1;
  1143. }
  1144. for (i = 0; i < len; i++)
  1145. d->text[d->text_used + i] = text[i];
  1146. d->text[d->text_used + len] = '\0';
  1147. n->text_off = d->text_used;
  1148. n->text_len = (unsigned short)len;
  1149. d->text_used += len + 1;
  1150. return 0;
  1151. }
  1152. /* ---- runtime DOM construction and mutation ---- */
  1153. static unsigned int text_len(const char *s)
  1154. {
  1155. unsigned int n = 0;
  1156. while (s && s[n])
  1157. n++;
  1158. return n;
  1159. }
  1160. static void node_zero(struct br_node *n)
  1161. {
  1162. unsigned int i;
  1163. for (i = 0; i < sizeof(*n); i++)
  1164. ((unsigned char *)n)[i] = 0;
  1165. n->img_index = -1;
  1166. n->bg_img_index = -1;
  1167. }
  1168. static void node_unlink(struct br_doc *d, unsigned int handle)
  1169. {
  1170. unsigned int node, parent, c, prev = 0;
  1171. if (!d || handle == 0 || handle > d->nnode)
  1172. return;
  1173. node = handle - 1;
  1174. parent = d->nodes[node].parent;
  1175. if (parent >= d->nnode) {
  1176. d->nodes[node].parent = 0;
  1177. d->nodes[node].next = 0;
  1178. return;
  1179. }
  1180. c = d->nodes[parent].first;
  1181. while (c && c != node) {
  1182. prev = c;
  1183. c = d->nodes[c].next;
  1184. }
  1185. if (c == node) {
  1186. if (prev)
  1187. d->nodes[prev].next = d->nodes[node].next;
  1188. else
  1189. d->nodes[parent].first = d->nodes[node].next;
  1190. if (d->nodes[parent].last == node)
  1191. d->nodes[parent].last = (unsigned short)prev;
  1192. }
  1193. d->nodes[node].parent = 0;
  1194. d->nodes[node].next = 0;
  1195. d->nodes[node].flags |= BR_NODE_DETACHED;
  1196. }
  1197. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1198. const char *s, unsigned int len);
  1199. static unsigned int find_tag_end(const char *s, unsigned int i,
  1200. unsigned int len)
  1201. {
  1202. char quote = 0;
  1203. while (i < len) {
  1204. char c = s[i];
  1205. if (quote) {
  1206. if (c == quote)
  1207. quote = 0;
  1208. } else if (c == '"' || c == '\'') {
  1209. quote = c;
  1210. } else if (c == '>') {
  1211. return i;
  1212. }
  1213. i++;
  1214. }
  1215. return len;
  1216. }
  1217. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1218. const char *html, unsigned int len)
  1219. {
  1220. unsigned int stack[64];
  1221. unsigned int sp = 1, i = 0;
  1222. if (!d || node >= d->nnode)
  1223. return -1;
  1224. stack[0] = node;
  1225. {
  1226. unsigned int c;
  1227. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1228. d->nodes[c].flags |= BR_NODE_DETACHED;
  1229. }
  1230. d->nodes[node].first = d->nodes[node].last = 0;
  1231. d->nodes[node].style_valid = 0;
  1232. d->nodes[node].pe_state = 0;
  1233. while (i < len) {
  1234. if (html[i] != '<') {
  1235. unsigned int start = i, end;
  1236. while (i < len && html[i] != '<')
  1237. i++;
  1238. end = i;
  1239. if (end > start) {
  1240. /* Entities decoded like the page parser does
  1241. * (createTextNode stays literal). */
  1242. char *tb = (char *)malloc(end - start + 1);
  1243. unsigned int tl = 0, h = 0;
  1244. if (tb) {
  1245. tl = decode_text(html + start, end - start,
  1246. tb, end - start + 1, 1);
  1247. h = br_create_text(d, tb, tl);
  1248. free(tb);
  1249. }
  1250. if (h)
  1251. br_append_child(d, stack[sp - 1], h);
  1252. }
  1253. continue;
  1254. }
  1255. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1256. html[i + 3] == '-') {
  1257. int p = find_ci(html, len, "-->", 3, i + 4);
  1258. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1259. unsigned int h = br_create_comment(d, html + i + 4,
  1260. end > i + 4 ?
  1261. end - i - 4 : 0);
  1262. if (h)
  1263. br_append_child(d, stack[sp - 1], h);
  1264. i = p >= 0 ? (unsigned int)p + 3 : len;
  1265. continue;
  1266. }
  1267. if (html[i + 1] == '/') {
  1268. unsigned int p = find_tag_end(html, i + 2, len);
  1269. i = p < len ? p + 1 : len;
  1270. if (sp > 1)
  1271. sp--;
  1272. continue;
  1273. }
  1274. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1275. unsigned int p = find_tag_end(html, i + 2, len);
  1276. i = p < len ? p + 1 : len;
  1277. continue;
  1278. }
  1279. {
  1280. unsigned int p = find_tag_end(html, i + 1, len);
  1281. unsigned int ns = i + 1, ne, as, ae;
  1282. unsigned int h, k;
  1283. char name[32];
  1284. int selfclose = 0;
  1285. while (ns < p && is_space(html[ns]))
  1286. ns++;
  1287. ne = ns;
  1288. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1289. ne++;
  1290. as = ne;
  1291. ae = p;
  1292. if (ae > as && html[ae - 1] == '/') {
  1293. selfclose = 1;
  1294. ae--;
  1295. }
  1296. if (ne == ns) {
  1297. i = p < len ? p + 1 : len;
  1298. continue;
  1299. }
  1300. if (ne - ns >= sizeof(name)) {
  1301. i = p < len ? p + 1 : len;
  1302. continue;
  1303. }
  1304. for (k = 0; k < ne - ns; k++)
  1305. name[k] = html[ns + k];
  1306. name[ne - ns] = '\0';
  1307. h = br_create_element(d, name, ne - ns);
  1308. if (!h) {
  1309. i = p < len ? p + 1 : len;
  1310. continue;
  1311. }
  1312. if (ae > as)
  1313. parse_attrs(d, &d->nodes[h - 1], html + as,
  1314. ae - as);
  1315. br_append_child(d, stack[sp - 1], h);
  1316. {
  1317. unsigned int tag = d->nodes[h - 1].tag;
  1318. if (!selfclose && !br_tag_is_void(tag) &&
  1319. sp < sizeof(stack) / sizeof(stack[0]))
  1320. stack[sp++] = h - 1;
  1321. }
  1322. i = p < len ? p + 1 : len;
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1328. {
  1329. unsigned int i;
  1330. if (!d)
  1331. return 0;
  1332. for (i = 0; i < d->nnode; i++)
  1333. if (d->nodes[i].type == BR_NODE_ELEM &&
  1334. d->nodes[i].tag == (unsigned char)tag &&
  1335. !node_detached(d, i))
  1336. return i + 1;
  1337. return 0;
  1338. }
  1339. unsigned int br_create_element(struct br_doc *d, const char *name,
  1340. unsigned int len)
  1341. {
  1342. struct br_node *n;
  1343. unsigned int idx;
  1344. if (!d || !name)
  1345. return 0;
  1346. if (d->nnode >= BR_MAX_NODES) {
  1347. d->truncated = 1;
  1348. return 0;
  1349. }
  1350. idx = d->nnode++;
  1351. n = &d->nodes[idx];
  1352. node_zero(n);
  1353. n->type = BR_NODE_ELEM;
  1354. n->flags = BR_NODE_DETACHED;
  1355. n->tag = (unsigned char)br_tag_from_name(name);
  1356. if (n->tag == BR_TAG_UNKNOWN && len)
  1357. n->name_off = store_attr(d, name, len, 31);
  1358. return idx + 1;
  1359. }
  1360. unsigned int br_create_text(struct br_doc *d, const char *text,
  1361. unsigned int len)
  1362. {
  1363. struct br_node *n;
  1364. unsigned int idx, i;
  1365. if (!d)
  1366. return 0;
  1367. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1368. d->truncated = 1;
  1369. return 0;
  1370. }
  1371. idx = d->nnode++;
  1372. n = &d->nodes[idx];
  1373. node_zero(n);
  1374. n->type = BR_NODE_TEXT;
  1375. n->flags = BR_NODE_DETACHED;
  1376. n->text_off = d->text_used;
  1377. n->text_len = (unsigned short)len;
  1378. for (i = 0; i < len; i++)
  1379. d->text[d->text_used + i] = text[i];
  1380. d->text[d->text_used + len] = '\0';
  1381. d->text_used += len + 1;
  1382. return idx + 1;
  1383. }
  1384. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1385. unsigned int len)
  1386. {
  1387. unsigned int h = br_create_text(d, text, len);
  1388. if (h)
  1389. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1390. return h;
  1391. }
  1392. unsigned int br_create_fragment(struct br_doc *d)
  1393. {
  1394. struct br_node *n;
  1395. unsigned int idx;
  1396. if (!d)
  1397. return 0;
  1398. if (d->nnode >= BR_MAX_NODES) {
  1399. d->truncated = 1;
  1400. return 0;
  1401. }
  1402. idx = d->nnode++;
  1403. n = &d->nodes[idx];
  1404. node_zero(n);
  1405. n->type = BR_NODE_FRAGMENT;
  1406. n->flags = BR_NODE_DETACHED;
  1407. return idx + 1;
  1408. }
  1409. int br_append_child(struct br_doc *d, unsigned int parent,
  1410. unsigned int child)
  1411. {
  1412. unsigned int p;
  1413. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1414. parent == child - 1)
  1415. return -1;
  1416. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1417. /* Appending a fragment inserts its children, not the fragment. */
  1418. unsigned int c = d->nodes[child - 1].first;
  1419. while (c) {
  1420. unsigned int next = d->nodes[c].next;
  1421. br_append_child(d, parent, c + 1);
  1422. c = next;
  1423. }
  1424. return 0;
  1425. }
  1426. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1427. if (p == child - 1)
  1428. return -1;
  1429. node_unlink(d, child);
  1430. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1431. d->nodes[child - 1].parent = (unsigned short)parent;
  1432. d->nodes[child - 1].next = 0;
  1433. if (d->nodes[parent].last)
  1434. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1435. else
  1436. d->nodes[parent].first = (unsigned short)(child - 1);
  1437. d->nodes[parent].last = (unsigned short)(child - 1);
  1438. return 0;
  1439. }
  1440. int br_insert_before(struct br_doc *d, unsigned int parent,
  1441. unsigned int child, unsigned int before)
  1442. {
  1443. unsigned int prev = 0, c;
  1444. if (before == 0)
  1445. return br_append_child(d, parent, child);
  1446. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1447. before > d->nnode || parent == child - 1)
  1448. return -1;
  1449. if (d->nodes[before - 1].parent != parent)
  1450. return -1;
  1451. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1452. unsigned int c = d->nodes[child - 1].first;
  1453. while (c) {
  1454. unsigned int next = d->nodes[c].next;
  1455. br_insert_before(d, parent, c + 1, before);
  1456. c = next;
  1457. }
  1458. return 0;
  1459. }
  1460. {
  1461. unsigned int p;
  1462. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1463. if (p == child - 1)
  1464. return -1;
  1465. }
  1466. node_unlink(d, child);
  1467. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1468. c = d->nodes[parent].first;
  1469. while (c && c != before - 1) {
  1470. prev = c;
  1471. c = d->nodes[c].next;
  1472. }
  1473. if (c != before - 1)
  1474. return -1;
  1475. d->nodes[child - 1].parent = (unsigned short)parent;
  1476. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1477. if (prev)
  1478. d->nodes[prev].next = (unsigned short)(child - 1);
  1479. else
  1480. d->nodes[parent].first = (unsigned short)(child - 1);
  1481. return 0;
  1482. }
  1483. int br_remove_child(struct br_doc *d, unsigned int parent,
  1484. unsigned int child)
  1485. {
  1486. if (!d || child == 0 || child > d->nnode ||
  1487. d->nodes[child - 1].parent != parent)
  1488. return -1;
  1489. node_unlink(d, child);
  1490. return 0;
  1491. }
  1492. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1493. unsigned int child)
  1494. {
  1495. unsigned int c;
  1496. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1497. return 0;
  1498. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1499. if (c == child - 1)
  1500. return child;
  1501. return 0;
  1502. }
  1503. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1504. const char *name)
  1505. {
  1506. struct br_node *n;
  1507. const char *p, *end, *found = 0;
  1508. if (!d || node >= d->nnode || !name)
  1509. return 0;
  1510. n = &d->nodes[node];
  1511. p = d->attrs + n->extra_off;
  1512. end = p + n->extra_len;
  1513. while (p + 1 < end) {
  1514. const char *an = p;
  1515. const char *av;
  1516. p += text_len(p) + 1;
  1517. if (p >= end)
  1518. break;
  1519. av = p;
  1520. p += text_len(p) + 1;
  1521. if (same_string(an, name))
  1522. found = av;
  1523. }
  1524. if (found)
  1525. return found;
  1526. if (same_string(name, "id") && n->id_off)
  1527. return d->attrs + n->id_off;
  1528. if (same_string(name, "class") && n->class_off)
  1529. return d->attrs + n->class_off;
  1530. if (same_string(name, "style") && n->style_off)
  1531. return d->attrs + n->style_off;
  1532. if (same_string(name, "href") && n->href_off)
  1533. return d->attrs + n->href_off;
  1534. if (same_string(name, "src") && n->src_off)
  1535. return d->attrs + n->src_off;
  1536. if (same_string(name, "alt") && n->alt_off)
  1537. return d->attrs + n->alt_off;
  1538. if (same_string(name, "rel") && n->rel_off)
  1539. return d->attrs + n->rel_off;
  1540. return 0;
  1541. }
  1542. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1543. const char *name, const char *value,
  1544. unsigned int len)
  1545. {
  1546. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1547. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1548. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1549. * exactly like parse_attrs(); script-built inputs (Vue sets
  1550. * type="password" after createElement) must land there too. */
  1551. if (control && same_string(name, "type")) {
  1552. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1553. return;
  1554. }
  1555. if (control && same_string(name, "value")) {
  1556. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1557. return;
  1558. }
  1559. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1560. same_string(name, "placeholder")) {
  1561. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1562. return;
  1563. }
  1564. if (same_string(name, "id"))
  1565. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1566. else if (same_string(name, "class"))
  1567. n->class_off = len ? store_attr(d, value, len,
  1568. BR_CLASS_MAX) : 0;
  1569. else if (same_string(name, "style"))
  1570. n->style_off = len ? store_attr(d, value, len,
  1571. BR_STYLE_ATTR_MAX) : 0;
  1572. else if (same_string(name, "href"))
  1573. n->href_off = store_href(d, value, len);
  1574. else if (same_string(name, "src"))
  1575. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1576. else if (same_string(name, "alt"))
  1577. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1578. else if (same_string(name, "rel"))
  1579. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1580. }
  1581. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1582. const char *value, unsigned int len)
  1583. {
  1584. struct br_node *n;
  1585. unsigned int start, nl, need;
  1586. const char *p, *old, *oldend;
  1587. if (!d || node >= d->nnode || !name)
  1588. return -1;
  1589. n = &d->nodes[node];
  1590. nl = text_len(name);
  1591. need = nl + 1 + len + 1;
  1592. start = d->attrs_used;
  1593. old = d->attrs + n->extra_off;
  1594. oldend = old + n->extra_len;
  1595. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1596. d->truncated = 1;
  1597. return -1;
  1598. }
  1599. p = old;
  1600. while (p + 1 < oldend) {
  1601. const char *an = p;
  1602. const char *av;
  1603. unsigned int anl, avl, k;
  1604. anl = text_len(an);
  1605. p += anl + 1;
  1606. if (p >= oldend)
  1607. break;
  1608. av = p;
  1609. avl = text_len(av);
  1610. p += avl + 1;
  1611. if (same_string(an, name))
  1612. continue;
  1613. for (k = 0; k < anl; k++)
  1614. d->attrs[d->attrs_used++] = an[k];
  1615. d->attrs[d->attrs_used++] = '\0';
  1616. for (k = 0; k < avl; k++)
  1617. d->attrs[d->attrs_used++] = av[k];
  1618. d->attrs[d->attrs_used++] = '\0';
  1619. }
  1620. {
  1621. unsigned int k;
  1622. for (k = 0; k < nl; k++)
  1623. d->attrs[d->attrs_used++] = name[k];
  1624. d->attrs[d->attrs_used++] = '\0';
  1625. for (k = 0; k < len; k++)
  1626. d->attrs[d->attrs_used++] = value[k];
  1627. d->attrs[d->attrs_used++] = '\0';
  1628. }
  1629. n = &d->nodes[node];
  1630. n->extra_off = start;
  1631. n->extra_len = (unsigned short)(d->attrs_used - start);
  1632. n->style_valid = 0;
  1633. n->pe_state = 0;
  1634. attr_note_known(d, n, name, value, len);
  1635. /* A script that sets a style/class (Angular's ng-style) or a new src
  1636. * after the first image scan re-arms the node for the loader; -2
  1637. * ("nothing to load") would otherwise be final. */
  1638. if ((same_string(name, "style") || same_string(name, "class")) &&
  1639. n->bg_img_index == -2)
  1640. n->bg_img_index = -1;
  1641. if (same_string(name, "src") && n->img_index == -2)
  1642. n->img_index = -1;
  1643. return 0;
  1644. }
  1645. static void clear_attr_flag(struct br_node *n, const char *name);
  1646. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1647. {
  1648. struct br_node *n;
  1649. unsigned int start;
  1650. const char *p, *oldend;
  1651. int found = 0;
  1652. if (!d || node >= d->nnode || !name)
  1653. return -1;
  1654. n = &d->nodes[node];
  1655. start = d->attrs_used;
  1656. p = d->attrs + n->extra_off;
  1657. oldend = p + n->extra_len;
  1658. if (start + n->extra_len > BR_ATTR_SIZE)
  1659. return -1;
  1660. while (p + 1 < oldend) {
  1661. const char *an = p;
  1662. const char *av;
  1663. unsigned int anl, avl, k;
  1664. anl = text_len(an);
  1665. p += anl + 1;
  1666. if (p >= oldend)
  1667. break;
  1668. av = p;
  1669. avl = text_len(av);
  1670. p += avl + 1;
  1671. if (same_string(an, name)) {
  1672. found = 1;
  1673. continue;
  1674. }
  1675. for (k = 0; k < anl; k++)
  1676. d->attrs[d->attrs_used++] = an[k];
  1677. d->attrs[d->attrs_used++] = '\0';
  1678. for (k = 0; k < avl; k++)
  1679. d->attrs[d->attrs_used++] = av[k];
  1680. d->attrs[d->attrs_used++] = '\0';
  1681. }
  1682. n = &d->nodes[node];
  1683. n->extra_off = start;
  1684. n->extra_len = (unsigned short)(d->attrs_used - start);
  1685. n->style_valid = 0;
  1686. n->pe_state = 0;
  1687. attr_note_known(d, n, name, "", 0);
  1688. if (found)
  1689. clear_attr_flag(n, name);
  1690. return found ? 0 : -1;
  1691. }
  1692. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1693. {
  1694. struct br_node *src;
  1695. unsigned int h;
  1696. const char *name;
  1697. if (!d || node == 0 || node > d->nnode)
  1698. return 0;
  1699. src = &d->nodes[node - 1];
  1700. if (src->type == BR_NODE_TEXT)
  1701. return br_create_text(d, d->text + src->text_off, src->text_len);
  1702. if (src->type == BR_NODE_COMMENT)
  1703. return br_create_comment(d, d->text + src->text_off,
  1704. src->text_len);
  1705. if (src->type == BR_NODE_FRAGMENT)
  1706. h = br_create_fragment(d);
  1707. else {
  1708. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1709. name = d->attrs + src->name_off;
  1710. else
  1711. name = br_tag_name(src->tag);
  1712. h = br_create_element(d, name, text_len(name));
  1713. }
  1714. if (!h)
  1715. return 0;
  1716. {
  1717. const char *p = d->attrs + src->extra_off;
  1718. const char *end = p + src->extra_len;
  1719. while (p + 1 < end) {
  1720. const char *an = p;
  1721. const char *av;
  1722. unsigned int anl, avl;
  1723. anl = text_len(an);
  1724. p += anl + 1;
  1725. if (p >= end)
  1726. break;
  1727. av = p;
  1728. avl = text_len(av);
  1729. p += avl + 1;
  1730. /* h is a 1-based handle; br_set_attr takes the
  1731. * node index (the old call stamped the copy's
  1732. * attributes onto the previous node). */
  1733. br_set_attr(d, h - 1, an, av, avl);
  1734. }
  1735. }
  1736. if (deep) {
  1737. unsigned int c;
  1738. for (c = src->first; c; c = d->nodes[c].next) {
  1739. unsigned int ch = br_clone_node(d, c + 1, 1);
  1740. if (ch)
  1741. br_append_child(d, h - 1, ch);
  1742. }
  1743. }
  1744. return h;
  1745. }
  1746. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1747. unsigned int len);
  1748. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1749. unsigned int *st, unsigned int *en);
  1750. /* querySelector(All) and Element.matches use the cascade's selector engine
  1751. * (sel_match(), below). */
  1752. static int match_selector(struct br_doc *d, unsigned int node,
  1753. const char *sel, unsigned int len)
  1754. {
  1755. while (len > 0 && sel[0] == ' ') {
  1756. sel++;
  1757. len--;
  1758. }
  1759. while (len > 0 && sel[len - 1] == ' ')
  1760. len--;
  1761. return len && sel_match(d, node, sel, len);
  1762. }
  1763. static int query_add(unsigned int *out, unsigned int max,
  1764. unsigned int *count, unsigned int node)
  1765. {
  1766. unsigned int i;
  1767. for (i = 0; i < *count; i++)
  1768. if (out[i] == node + 1)
  1769. return 0;
  1770. if (*count < max)
  1771. out[(*count)++] = node + 1;
  1772. return 0;
  1773. }
  1774. static void query_walk(struct br_doc *d, unsigned int node,
  1775. const char *sel, unsigned int len, unsigned int *out,
  1776. unsigned int max, unsigned int *count)
  1777. {
  1778. unsigned int c;
  1779. if (d->nodes[node].type == BR_NODE_ELEM &&
  1780. match_selector(d, node, sel, len))
  1781. query_add(out, max, count, node);
  1782. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1783. query_walk(d, c, sel, len, out, max, count);
  1784. }
  1785. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1786. {
  1787. unsigned int len;
  1788. if (!d || !selector || node == 0 || node > d->nnode)
  1789. return 0;
  1790. len = text_len(selector);
  1791. while (len > 0 && selector[0] == ' ') {
  1792. selector++;
  1793. len--;
  1794. }
  1795. while (len > 0 && selector[len - 1] == ' ')
  1796. len--;
  1797. {
  1798. unsigned int pos = 0, st, en;
  1799. while (sel_list_next(selector, len, &pos, &st, &en))
  1800. if (match_selector(d, node - 1, selector + st, en - st))
  1801. return 1;
  1802. }
  1803. return 0;
  1804. }
  1805. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1806. const char *selector, unsigned int *out,
  1807. unsigned int max)
  1808. {
  1809. unsigned int count = 0, i = 0, len, start, end;
  1810. if (!d || !selector || !out || max == 0)
  1811. return 0;
  1812. len = text_len(selector);
  1813. while (sel_list_next(selector, len, &i, &start, &end)) {
  1814. if (root == 0) {
  1815. unsigned int n;
  1816. for (n = 1; n < d->nnode; n++)
  1817. if (d->nodes[n].type == BR_NODE_ELEM &&
  1818. !node_detached(d, n) &&
  1819. match_selector(d, n, selector + start,
  1820. end - start))
  1821. query_add(out, max, &count, n);
  1822. } else if (root <= d->nnode) {
  1823. unsigned int c;
  1824. for (c = d->nodes[root - 1].first; c;
  1825. c = d->nodes[c].next)
  1826. query_walk(d, c, selector + start,
  1827. end - start, out, max, &count);
  1828. }
  1829. }
  1830. return count;
  1831. }
  1832. /* ---- attribute parsing ---- */
  1833. static int is_space(char c)
  1834. {
  1835. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1836. }
  1837. static int str_eq(const char *a, const char *b);
  1838. static unsigned int parse_uint(const char *s, unsigned int len)
  1839. {
  1840. unsigned int i = 0, v = 0;
  1841. while (i < len && is_space(s[i]))
  1842. i++;
  1843. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1844. v = v * 10 + (unsigned int)(s[i] - '0');
  1845. if (v > 4096)
  1846. v = 4096;
  1847. i++;
  1848. }
  1849. return v;
  1850. }
  1851. static int contains_ci_len(const char *s, unsigned int len,
  1852. const char *needle);
  1853. static int parse_svg_viewbox(const char *s, unsigned int len,
  1854. unsigned short *w, unsigned short *h)
  1855. {
  1856. unsigned int i = 0, v[4], n = 0;
  1857. while (i < len && n < 4) {
  1858. unsigned int x = 0;
  1859. int any = 0;
  1860. while (i < len && (is_space(s[i]) || s[i] == ','))
  1861. i++;
  1862. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1863. x = x * 10 + (unsigned int)(s[i] - '0');
  1864. i++;
  1865. any = 1;
  1866. }
  1867. /* viewBox values are commonly fractional ("0 0 340.16
  1868. * 340.16"). Consume the fraction so it is not mistaken for
  1869. * the next number; the integer part is enough for the aspect
  1870. * ratio used by the renderer. */
  1871. if (i < len && s[i] == '.') {
  1872. i++;
  1873. while (i < len && s[i] >= '0' && s[i] <= '9')
  1874. i++;
  1875. }
  1876. if (!any) {
  1877. if (i < len)
  1878. i++;
  1879. continue;
  1880. }
  1881. v[n++] = x;
  1882. }
  1883. if (n < 4)
  1884. return 0;
  1885. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1886. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1887. return *w && *h;
  1888. }
  1889. /* The inverse of note_attr_flag() for a removed attribute. The cached
  1890. * bit used to outlive the attribute: `input.disabled = false` removed
  1891. * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
  1892. * once (Docusaurus' search box while its index loads) never took focus
  1893. * again. */
  1894. static void clear_attr_flag(struct br_node *n, const char *name)
  1895. {
  1896. if (str_eq(name, "data-collapse-target"))
  1897. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
  1898. else if (str_eq(name, "data-collapse-trigger"))
  1899. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
  1900. else if (str_eq(name, "is-open"))
  1901. n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
  1902. else if (str_eq(name, "disabled"))
  1903. n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
  1904. else if (str_eq(name, "open"))
  1905. n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
  1906. else if (str_eq(name, "inert"))
  1907. n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
  1908. }
  1909. static void note_attr_flag(struct br_node *n, const char *name)
  1910. {
  1911. if (str_eq(name, "data-collapse-target"))
  1912. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1913. else if (str_eq(name, "data-collapse-trigger"))
  1914. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1915. else if (str_eq(name, "is-open"))
  1916. n->attr_flags |= BR_ATTR_IS_OPEN;
  1917. else if (str_eq(name, "disabled"))
  1918. n->attr_flags |= BR_ATTR_DISABLED;
  1919. else if (str_eq(name, "open"))
  1920. n->attr_flags |= BR_ATTR_OPEN;
  1921. else if (str_eq(name, "inert"))
  1922. n->attr_flags |= BR_ATTR_INERT;
  1923. }
  1924. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1925. * second pass so the block stays contiguous even though the known attributes
  1926. * above also use the shared attr arena. */
  1927. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1928. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1929. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1930. * written. */
  1931. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1932. {
  1933. static const struct { const char *e; char c; } ents[] = {
  1934. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1935. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1936. { 0, 0 }
  1937. };
  1938. unsigned int i = 0, n = 0;
  1939. while (i < len) {
  1940. int k, done = 0;
  1941. if (s[i] == '&') {
  1942. for (k = 0; ents[k].e; k++) {
  1943. unsigned int el = (unsigned int)strlen(ents[k].e);
  1944. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1945. out[n++] = ents[k].c;
  1946. i += el;
  1947. done = 1;
  1948. break;
  1949. }
  1950. }
  1951. if (!done && i + 3 < len && s[i + 1] == '#') {
  1952. unsigned int j = i + 2, v = 0;
  1953. int hex = s[j] == 'x' || s[j] == 'X';
  1954. if (hex)
  1955. j++;
  1956. while (j < len && j < i + 10 &&
  1957. ((s[j] >= '0' && s[j] <= '9') ||
  1958. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1959. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1960. char c = s[j++];
  1961. v = v * (hex ? 16 : 10) +
  1962. (unsigned int)(c <= '9' ? c - '0' :
  1963. (c | 0x20) - 'a' + 10);
  1964. }
  1965. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1966. out[n++] = (char)v;
  1967. i = j + 1;
  1968. done = 1;
  1969. }
  1970. }
  1971. }
  1972. if (!done)
  1973. out[n++] = s[i++];
  1974. }
  1975. return n;
  1976. }
  1977. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1978. const char *s, unsigned int len)
  1979. {
  1980. unsigned int i = 0;
  1981. unsigned int start = d->attrs_used;
  1982. while (i < len) {
  1983. char name[32];
  1984. unsigned int nl = 0, vstart, vlen;
  1985. char quote = 0;
  1986. while (i < len && is_space(s[i]))
  1987. i++;
  1988. if (i >= len || s[i] == '/')
  1989. break;
  1990. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1991. char c = s[i++];
  1992. if (c >= 'A' && c <= 'Z')
  1993. c = (char)(c - 'A' + 'a');
  1994. if (nl + 1 < sizeof(name))
  1995. name[nl++] = c;
  1996. }
  1997. name[nl] = '\0';
  1998. while (i < len && is_space(s[i]))
  1999. i++;
  2000. if (i < len && s[i] == '=') {
  2001. i++;
  2002. while (i < len && is_space(s[i]))
  2003. i++;
  2004. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2005. quote = s[i++];
  2006. vstart = i;
  2007. while (i < len && s[i] != quote)
  2008. i++;
  2009. vlen = i - vstart;
  2010. if (i < len)
  2011. i++;
  2012. } else {
  2013. vstart = i;
  2014. while (i < len && !is_space(s[i]) && s[i] != '>')
  2015. i++;
  2016. vlen = i - vstart;
  2017. }
  2018. } else {
  2019. /* Boolean attribute: expose it with an empty value. */
  2020. vstart = 0;
  2021. vlen = 0;
  2022. }
  2023. if (!nl)
  2024. continue;
  2025. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  2026. d->truncated = 1;
  2027. break;
  2028. }
  2029. {
  2030. unsigned int k;
  2031. for (k = 0; k < nl; k++)
  2032. d->attrs[d->attrs_used++] = name[k];
  2033. d->attrs[d->attrs_used++] = '\0';
  2034. d->attrs_used += attr_unescape(s + vstart, vlen,
  2035. d->attrs + d->attrs_used);
  2036. d->attrs[d->attrs_used++] = '\0';
  2037. }
  2038. }
  2039. if (d->attrs_used > start) {
  2040. n->extra_off = start;
  2041. n->extra_len = (unsigned short)(d->attrs_used - start);
  2042. }
  2043. }
  2044. /* Parse attributes from `s` (between the tag name and '>'). */
  2045. static void parse_attrs(struct br_doc *d, struct br_node *n,
  2046. const char *s, unsigned int len)
  2047. {
  2048. unsigned int i = 0;
  2049. while (i < len) {
  2050. char name[32];
  2051. unsigned int nl = 0, vstart, vlen;
  2052. char quote = 0;
  2053. while (i < len && is_space(s[i]))
  2054. i++;
  2055. if (i >= len || s[i] == '/')
  2056. break;
  2057. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  2058. char c = s[i++];
  2059. if (c >= 'A' && c <= 'Z')
  2060. c = (char)(c - 'A' + 'a');
  2061. if (nl + 1 < sizeof(name))
  2062. name[nl++] = c;
  2063. }
  2064. name[nl] = '\0';
  2065. while (i < len && is_space(s[i]))
  2066. i++;
  2067. if (i >= len || s[i] != '=') {
  2068. if (str_eq(name, "hidden"))
  2069. n->flags |= BR_NODE_HIDDEN;
  2070. note_attr_flag(n, name);
  2071. continue;
  2072. }
  2073. i++;
  2074. while (i < len && is_space(s[i]))
  2075. i++;
  2076. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2077. quote = s[i++];
  2078. vstart = i;
  2079. while (i < len && s[i] != quote)
  2080. i++;
  2081. vlen = i - vstart;
  2082. if (i < len)
  2083. i++;
  2084. } else {
  2085. vstart = i;
  2086. while (i < len && !is_space(s[i]) && s[i] != '>')
  2087. i++;
  2088. vlen = i - vstart;
  2089. }
  2090. if (!nl)
  2091. continue;
  2092. if (str_eq(name, "hidden")) {
  2093. n->flags |= BR_NODE_HIDDEN;
  2094. continue;
  2095. }
  2096. note_attr_flag(n, name);
  2097. if (str_eq(name, "href"))
  2098. n->href_off = store_href(d, s + vstart, vlen);
  2099. else if (str_eq(name, "class"))
  2100. n->class_off = store_attr(d, s + vstart, vlen,
  2101. BR_CLASS_MAX);
  2102. else if (str_eq(name, "id"))
  2103. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2104. else if (str_eq(name, "style"))
  2105. n->style_off = store_attr(d, s + vstart, vlen,
  2106. BR_STYLE_ATTR_MAX);
  2107. else if (str_eq(name, "src"))
  2108. n->src_off = store_attr(d, s + vstart, vlen,
  2109. BR_URL_ATTR_MAX);
  2110. else if (str_eq(name, "alt"))
  2111. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2112. else if (str_eq(name, "color"))
  2113. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2114. else if (str_eq(name, "rel"))
  2115. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2116. else if (str_eq(name, "value"))
  2117. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2118. else if (str_eq(name, "placeholder") &&
  2119. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2120. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2121. else if (str_eq(name, "type") &&
  2122. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2123. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2124. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2125. else if (str_eq(name, "width")) {
  2126. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2127. if (vlen && s[vstart + vlen - 1] == '%')
  2128. n->width_pct = 1;
  2129. } else if (str_eq(name, "height")) {
  2130. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2131. if (vlen && s[vstart + vlen - 1] == '%')
  2132. n->height_pct = 1;
  2133. } else if (str_eq(name, "colspan")) {
  2134. unsigned int v = parse_uint(s + vstart, vlen);
  2135. if (v < 1)
  2136. v = 1;
  2137. if (v > 16)
  2138. v = 16;
  2139. n->colspan = (unsigned char)v;
  2140. } else if (str_eq(name, "rowspan")) {
  2141. unsigned int v = parse_uint(s + vstart, vlen);
  2142. if (v < 1)
  2143. v = 1;
  2144. if (v > 16)
  2145. v = 16;
  2146. n->rowspan = (unsigned char)v;
  2147. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2148. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2149. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2150. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2151. } else if (str_eq(name, "x2")) {
  2152. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2153. } else if (str_eq(name, "y2")) {
  2154. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2155. } else if (str_eq(name, "rx")) {
  2156. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2157. } else if (str_eq(name, "d")) {
  2158. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2159. } else if (str_eq(name, "viewbox")) {
  2160. unsigned short vw = 0, vh = 0;
  2161. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2162. n->width_attr = vw;
  2163. n->height_attr = vh;
  2164. n->flags |= BR_NODE_VIEWBOX;
  2165. }
  2166. } else if (str_eq(name, "text-anchor")) {
  2167. if (contains_ci_len(s + vstart, vlen, "middle"))
  2168. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2169. else if (contains_ci_len(s + vstart, vlen, "end"))
  2170. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2171. }
  2172. }
  2173. parse_attrs_generic(d, n, s, len);
  2174. }
  2175. static int str_eq(const char *a, const char *b)
  2176. {
  2177. unsigned int i = 0;
  2178. while (a[i] && b[i] && a[i] == b[i])
  2179. i++;
  2180. return a[i] == '\0' && b[i] == '\0';
  2181. }
  2182. /* ---- CSS ---- */
  2183. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2184. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2185. {
  2186. return parse_color(s, len, out);
  2187. }
  2188. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2189. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2190. {
  2191. while (len && is_space(*s)) {
  2192. s++;
  2193. len--;
  2194. }
  2195. while (len && is_space(s[len - 1]))
  2196. len--;
  2197. if (!len)
  2198. return 0;
  2199. if (len == 4 && ci_eq_n(s, "none", 4))
  2200. return BR_PAINT_NONE;
  2201. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2202. return BR_PAINT_CURRENT;
  2203. if (len > 4 && ci_eq_n(s, "url(", 4))
  2204. return BR_PAINT_URL;
  2205. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2206. return BR_PAINT_NONE;
  2207. if (parse_color(s, len, c))
  2208. return BR_PAINT_COLOR;
  2209. return 0;
  2210. }
  2211. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2212. static int svg_len16(const char *s, unsigned int len)
  2213. {
  2214. unsigned int i = 0;
  2215. int v = 0, frac = 0, digits = 0;
  2216. while (i < len && is_space(s[i]))
  2217. i++;
  2218. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2219. if (v < 4000)
  2220. v = v * 10 + (s[i] - '0');
  2221. i++;
  2222. digits++;
  2223. }
  2224. if (i < len && s[i] == '.') {
  2225. int scale = 1;
  2226. i++;
  2227. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2228. if (scale < 10000) {
  2229. frac = frac * 10 + (s[i] - '0');
  2230. scale *= 10;
  2231. }
  2232. i++;
  2233. digits++;
  2234. }
  2235. frac = frac * 16 / scale;
  2236. }
  2237. if (!digits)
  2238. return -1;
  2239. return v * 16 + frac;
  2240. }
  2241. /* Inside an <svg> subtree (including the <svg> element itself). */
  2242. static int in_svg(const struct br_doc *d, unsigned int node)
  2243. {
  2244. unsigned int k;
  2245. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2246. if (d->nodes[node].tag == BR_TAG_SVG)
  2247. return 1;
  2248. node = d->nodes[node].parent;
  2249. }
  2250. return 0;
  2251. }
  2252. enum {
  2253. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2254. PROP_BG_REPEAT,
  2255. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2256. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2257. PROP_FONT_PX, PROP_FONT_UNIT,
  2258. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2259. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2260. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2261. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2262. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2263. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2264. PROP_BORDER_W, PROP_BORDER_COLOR,
  2265. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2266. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2267. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2268. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2269. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2270. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2271. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2272. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2273. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2274. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2275. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2276. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2277. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2278. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2279. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2280. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2281. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2282. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2283. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2284. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2285. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2286. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2287. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2288. PROP_ZINDEX, PROP_ZSET,
  2289. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2290. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2291. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2292. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2293. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2294. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2295. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2296. PROP_TX_PCT, PROP_TY_PCT, PROP_TR_MASK, PROP_TR_MS, PROP_TR_EASE,
  2297. PROP_PE_NONE,
  2298. PROP_COUNT
  2299. };
  2300. struct color_name {
  2301. const char *name;
  2302. unsigned short rgb;
  2303. };
  2304. static const struct color_name color_names[] = {
  2305. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2306. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2307. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2308. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2309. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2310. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2311. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2312. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2313. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2314. { "darkgrey", 0x528A }, { 0, 0 }
  2315. };
  2316. static int hexval(char c)
  2317. {
  2318. if (c >= '0' && c <= '9')
  2319. return c - '0';
  2320. if (c >= 'a' && c <= 'f')
  2321. return c - 'a' + 10;
  2322. if (c >= 'A' && c <= 'F')
  2323. return c - 'A' + 10;
  2324. return -1;
  2325. }
  2326. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2327. {
  2328. unsigned int i;
  2329. for (i = 0; color_names[i].name; i++) {
  2330. unsigned int j, n = s_len(color_names[i].name);
  2331. if (n != len)
  2332. continue;
  2333. for (j = 0; j < n; j++) {
  2334. char c = s[j];
  2335. if (c >= 'A' && c <= 'Z')
  2336. c = (char)(c - 'A' + 'a');
  2337. if (c != color_names[i].name[j])
  2338. break;
  2339. }
  2340. if (j == n) {
  2341. *out = color_names[i].rgb;
  2342. return 1;
  2343. }
  2344. }
  2345. return 0;
  2346. }
  2347. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2348. * over white; the render target is a white page. */
  2349. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2350. {
  2351. unsigned int i = 0;
  2352. while (i < len && is_space(s[i]))
  2353. i++;
  2354. if (i < len && s[i] == '#') {
  2355. int d[6], nd = 0;
  2356. i++;
  2357. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2358. d[nd++] = hexval(s[i++]);
  2359. if (nd == 3) {
  2360. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2361. unsigned int b = (unsigned int)d[2];
  2362. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2363. ((g * 17 >> 2) << 5) |
  2364. (b * 17 >> 3));
  2365. return 1;
  2366. }
  2367. if (nd >= 6) {
  2368. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2369. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2370. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2371. *out = (unsigned short)(((r >> 3) << 11) |
  2372. ((g >> 2) << 5) | (b >> 3));
  2373. return 1;
  2374. }
  2375. return 0;
  2376. }
  2377. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2378. unsigned int v[3], k = 0, a = 255;
  2379. while (i < len && s[i] != '(')
  2380. i++;
  2381. if (i >= len)
  2382. return 0;
  2383. i++;
  2384. while (k < 3 && i < len) {
  2385. unsigned int x = 0;
  2386. int percent = 0;
  2387. while (i < len && (is_space(s[i]) || s[i] == ','))
  2388. i++;
  2389. if (i >= len || s[i] < '0' || s[i] > '9')
  2390. return 0;
  2391. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2392. x = x * 10 + (unsigned int)(s[i] - '0');
  2393. i++;
  2394. }
  2395. if (i < len && s[i] == '%') {
  2396. percent = 1;
  2397. i++;
  2398. }
  2399. if (percent)
  2400. x = x * 255 / 100;
  2401. if (x > 255)
  2402. x = 255;
  2403. v[k++] = x;
  2404. }
  2405. /* Optional alpha in both syntaxes:
  2406. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2407. {
  2408. unsigned int whole = 0, frac = 0, scale = 1;
  2409. int percent = 0;
  2410. while (i < len && (is_space(s[i]) || s[i] == ','))
  2411. i++;
  2412. if (i < len && s[i] == '/') {
  2413. i++;
  2414. while (i < len && is_space(s[i]))
  2415. i++;
  2416. }
  2417. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2418. * used to read as opaque black. */
  2419. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2420. (s[i] == '.' && i + 1 < len &&
  2421. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2422. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2423. whole = whole * 10 +
  2424. (unsigned int)(s[i] - '0');
  2425. i++;
  2426. }
  2427. if (i < len && s[i] == '.') {
  2428. i++;
  2429. while (i < len && s[i] >= '0' &&
  2430. s[i] <= '9') {
  2431. if (scale < 1000) {
  2432. frac = frac * 10 +
  2433. (unsigned int)(s[i] - '0');
  2434. scale *= 10;
  2435. }
  2436. i++;
  2437. }
  2438. }
  2439. while (i < len && is_space(s[i]))
  2440. i++;
  2441. if (i < len && s[i] == '%')
  2442. percent = 1;
  2443. if (percent)
  2444. a = whole > 100 ? 255 :
  2445. whole * 255 / 100;
  2446. else if (whole >= 1)
  2447. a = 255;
  2448. else
  2449. a = frac * 255 / scale;
  2450. }
  2451. }
  2452. if (k == 3) {
  2453. unsigned int r = v[0], g = v[1], b = v[2];
  2454. if (a < 255) {
  2455. r = (r * a + 255u * (255u - a)) / 255u;
  2456. g = (g * a + 255u * (255u - a)) / 255u;
  2457. b = (b * a + 255u * (255u - a)) / 255u;
  2458. }
  2459. *out = (unsigned short)(((r >> 3) << 11) |
  2460. ((g >> 2) << 5) | (b >> 3));
  2461. return 1;
  2462. }
  2463. return 0;
  2464. }
  2465. {
  2466. unsigned int j = i;
  2467. while (i < len && !is_space(s[i]))
  2468. i++;
  2469. return named_color(s + j, i - j, out);
  2470. }
  2471. }
  2472. /* Fully transparent colours have no RGB565 representation. They are common
  2473. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2474. * only on hover; painting them black would draw an outline around every
  2475. * pill and card. */
  2476. static int color_is_transparent(const char *s, unsigned int len)
  2477. {
  2478. unsigned int i = 0, n = 0;
  2479. int last = 0, prev = 0, d;
  2480. while (i < len && is_space(s[i]))
  2481. i++;
  2482. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2483. return 1;
  2484. if (i >= len || s[i] != '#')
  2485. return 0;
  2486. i++;
  2487. while (i < len && hexval(s[i]) >= 0) {
  2488. d = hexval(s[i]);
  2489. prev = last;
  2490. last = d;
  2491. n++;
  2492. i++;
  2493. }
  2494. while (i < len && is_space(s[i]))
  2495. i++;
  2496. if (i != len)
  2497. return 0;
  2498. if (n == 4)
  2499. return last == 0;
  2500. if (n == 8)
  2501. return prev == 0 && last == 0;
  2502. return 0;
  2503. }
  2504. static int register_bg_url(const char *s, unsigned int len)
  2505. {
  2506. char tmp[BR_BG_URL_MAX + 1];
  2507. unsigned int i, n = 0;
  2508. char q = 0;
  2509. while (len && is_space(*s)) {
  2510. s++;
  2511. len--;
  2512. }
  2513. while (len && is_space(s[len - 1]))
  2514. len--;
  2515. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2516. s[len - 1] == s[0]) {
  2517. q = s[0];
  2518. s++;
  2519. len -= 2;
  2520. }
  2521. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2522. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2523. if (q && s[i] == '\\' && i + 1 < len) {
  2524. i++;
  2525. if (s[i] == '\n')
  2526. continue;
  2527. }
  2528. tmp[n++] = s[i];
  2529. }
  2530. tmp[n] = '\0';
  2531. if (!n)
  2532. return 0;
  2533. for (i = 0; i < bg_url_count; i++)
  2534. if (!strcmp(bg_urls[i], tmp))
  2535. return (int)i + 1;
  2536. if (bg_url_count >= BR_BG_URLS ||
  2537. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2538. return 0;
  2539. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2540. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2541. bg_url_pool_used += n + 1;
  2542. return (int)(++bg_url_count);
  2543. }
  2544. /* Length values for margins/padding: px, ignore percentages/units. */
  2545. static unsigned int viewport_w;
  2546. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2547. {
  2548. unsigned int i = *pos;
  2549. int sign = 1;
  2550. long v = 0; /* hundredths of a unit before conversion */
  2551. int digits = 0, frac = 0, unit = 0;
  2552. while (i < len && is_space(s[i]))
  2553. i++;
  2554. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2555. if (s[i] == '-')
  2556. sign = -1;
  2557. i++;
  2558. }
  2559. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2560. v = v * 10 + (s[i] - '0');
  2561. digits = 1;
  2562. i++;
  2563. }
  2564. if (i < len && s[i] == '.') {
  2565. i++;
  2566. /* Minified CSS writes ".5rem" without a leading zero. */
  2567. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2568. if (frac < 2) {
  2569. v = v * 10 + (s[i] - '0');
  2570. frac++;
  2571. digits = 1;
  2572. }
  2573. i++;
  2574. }
  2575. }
  2576. while (frac < 2) {
  2577. v *= 10;
  2578. frac++;
  2579. }
  2580. while (frac > 2) {
  2581. v /= 10;
  2582. frac--;
  2583. }
  2584. if (i < len && s[i] == '%')
  2585. unit = 2;
  2586. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2587. s[i + 2] == 'm')
  2588. unit = 1;
  2589. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2590. unit = 3;
  2591. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2592. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2593. s[i] == 'a' || s[i] == 'd'))
  2594. i++;
  2595. *pos = i;
  2596. if (!digits)
  2597. return 0;
  2598. if (unit == 1)
  2599. v *= root_font_px;
  2600. else if (unit == 3)
  2601. v *= root_font_px;
  2602. else if (unit == 2)
  2603. v = v * (long)viewport_w / 100;
  2604. v /= 100;
  2605. v *= sign;
  2606. if (v < -2048)
  2607. v = -2048;
  2608. if (v > 2048)
  2609. v = 2048;
  2610. return (int)v;
  2611. }
  2612. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2613. * treats it as a zero-length token; margins need to keep it so block layout
  2614. * can centre a box with a definite width. */
  2615. #define CSS_LEN_AUTO (-32768)
  2616. /* A box length with its unit preserved for layout-time resolution:
  2617. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2618. * rem and whole pixels for px. */
  2619. static int parse_css_len(const char *s, unsigned int len,
  2620. unsigned int *pos, int *unit)
  2621. {
  2622. unsigned int i = *pos;
  2623. int sign = 1;
  2624. long v = 0;
  2625. int digits = 0, frac = 0;
  2626. *unit = 0;
  2627. while (i < len && is_space(s[i]))
  2628. i++;
  2629. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2630. if (s[i] == '-')
  2631. sign = -1;
  2632. i++;
  2633. }
  2634. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2635. v = v * 10 + (s[i] - '0');
  2636. digits = 1;
  2637. i++;
  2638. }
  2639. if (i < len && s[i] == '.') {
  2640. i++;
  2641. /* Leading-dot fractions are common in minified CSS
  2642. * (".5rem", ".3rem"). */
  2643. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2644. if (frac < 2) {
  2645. v = v * 10 + (s[i] - '0');
  2646. frac++;
  2647. digits = 1;
  2648. }
  2649. i++;
  2650. }
  2651. }
  2652. while (frac < 2) {
  2653. v *= 10;
  2654. frac++;
  2655. }
  2656. if (!digits) {
  2657. *pos = i;
  2658. return 0;
  2659. }
  2660. if (i < len && s[i] == '%') {
  2661. *unit = 1;
  2662. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2663. s[i + 2] == 'm') {
  2664. *unit = 3;
  2665. i += 3;
  2666. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2667. *unit = 2;
  2668. i += 2;
  2669. } else {
  2670. v /= 100;
  2671. }
  2672. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2673. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2674. s[i] == 'a' || s[i] == 'd'))
  2675. i++;
  2676. *pos = i;
  2677. v *= sign;
  2678. if (v < -32768)
  2679. v = -32768;
  2680. if (v > 32767)
  2681. v = 32767;
  2682. return (int)v;
  2683. }
  2684. static int parse_margin_len(const char *s, unsigned int len,
  2685. unsigned int *pos, int *unit)
  2686. {
  2687. unsigned int i = *pos;
  2688. while (i < len && is_space(s[i]))
  2689. i++;
  2690. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2691. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2692. *pos = i + 4;
  2693. *unit = 4;
  2694. return CSS_LEN_AUTO;
  2695. }
  2696. return parse_css_len(s, len, pos, unit);
  2697. }
  2698. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2699. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2700. static int parse_margin_calc(const char *s, unsigned int len,
  2701. unsigned int *pos, int *pct, int *px)
  2702. {
  2703. unsigned int i = *pos, j;
  2704. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2705. return 0;
  2706. i += 5;
  2707. j = i;
  2708. while (j < len && s[j] != ')')
  2709. j++;
  2710. if (j >= len)
  2711. return 0;
  2712. if (!parse_calc(s + i, j - i, pct, px))
  2713. return 0;
  2714. *pos = j + 1;
  2715. return 1;
  2716. }
  2717. /* calc(P% +/- Npx) for the common width/margin forms. */
  2718. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2719. {
  2720. unsigned int i = 0;
  2721. int sign = 1;
  2722. int have = 0;
  2723. *pct = 0;
  2724. *px = 0;
  2725. while (i < len) {
  2726. unsigned int j;
  2727. long whole = 0, frac = 0, scale = 1;
  2728. int digits = 0;
  2729. while (i < len && is_space(s[i]))
  2730. i++;
  2731. if (i >= len)
  2732. break;
  2733. if (s[i] == '+' || s[i] == '-') {
  2734. sign = s[i] == '-' ? -1 : 1;
  2735. i++;
  2736. continue;
  2737. }
  2738. if (s[i] < '0' || s[i] > '9') {
  2739. if (s[i] == '.') {
  2740. /* fall through to the number parser */
  2741. } else {
  2742. i++;
  2743. continue;
  2744. }
  2745. }
  2746. j = i;
  2747. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2748. whole = whole * 10 + (s[j] - '0');
  2749. digits = 1;
  2750. j++;
  2751. }
  2752. if (j < len && s[j] == '.') {
  2753. j++;
  2754. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2755. frac = frac * 10 + (s[j] - '0');
  2756. scale *= 10;
  2757. j++;
  2758. }
  2759. }
  2760. if (!digits) {
  2761. i = j + 1;
  2762. continue;
  2763. }
  2764. while (j < len && is_space(s[j]))
  2765. j++;
  2766. if (j < len && s[j] == '%') {
  2767. *pct = sign * (int)(whole + (scale > 1 ?
  2768. frac * 100 / scale : 0));
  2769. have = 1;
  2770. j++;
  2771. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2772. *px = sign * (int)whole;
  2773. have = 1;
  2774. j += 2;
  2775. }
  2776. i = j;
  2777. while (i < len && !is_space(s[i]))
  2778. i++;
  2779. sign = 1;
  2780. }
  2781. return have;
  2782. }
  2783. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2784. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2785. unsigned char *kind)
  2786. {
  2787. unsigned int i = 0;
  2788. unsigned long v = 0;
  2789. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2790. *val = 0;
  2791. *kind = 0;
  2792. while (i < len && is_space(s[i]))
  2793. i++;
  2794. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2795. return 0;
  2796. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2797. return 0;
  2798. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2799. v = v * 10 + (unsigned int)(s[i] - '0');
  2800. digits = 1;
  2801. i++;
  2802. }
  2803. if (i < len && s[i] == '.') {
  2804. i++;
  2805. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2806. if (frac < 2) {
  2807. v = v * 10 + (unsigned int)(s[i] - '0');
  2808. frac++;
  2809. digits = 1;
  2810. }
  2811. i++;
  2812. }
  2813. }
  2814. while (frac < 2) {
  2815. v *= 10;
  2816. frac++;
  2817. }
  2818. while (frac > 2) {
  2819. v /= 10;
  2820. frac--;
  2821. }
  2822. if (!digits)
  2823. return 0;
  2824. while (i < len && is_space(s[i]))
  2825. i++;
  2826. if (i < len && s[i] == '%')
  2827. pct = 1;
  2828. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2829. s[i + 2] == 'm')
  2830. rem = 1;
  2831. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2832. rem = 2;
  2833. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2834. vw = 1;
  2835. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2836. ; /* px: already the default scale */
  2837. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2838. bad_unit = 1;
  2839. if (bad_unit)
  2840. return 0;
  2841. if (rem == 1 || rem == 2) {
  2842. if (v > 65535)
  2843. v = 65535;
  2844. *val = (unsigned short)v;
  2845. *kind = rem == 1 ? 5 : 4;
  2846. return 1;
  2847. }
  2848. v /= 100;
  2849. if (vw)
  2850. v = v * viewport_w / 100;
  2851. if (pct) {
  2852. if (v > 100)
  2853. v = 100;
  2854. *kind = 2;
  2855. } else {
  2856. if (v > 2048)
  2857. v = 2048;
  2858. *kind = 1;
  2859. }
  2860. *val = (unsigned short)v;
  2861. return 1;
  2862. }
  2863. static int parse_font_px(const char *s, unsigned int len)
  2864. {
  2865. unsigned int i = 0;
  2866. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2867. int v1000, px;
  2868. int is_px = 1, is_rem = 0, is_pct = 0;
  2869. while (i < len && is_space(s[i]))
  2870. i++;
  2871. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2872. return 24;
  2873. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2874. return 32;
  2875. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2876. return 14;
  2877. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2878. whole = whole * 10 + (s[i] - '0');
  2879. digits = 1;
  2880. i++;
  2881. }
  2882. if (i < len && s[i] == '.') {
  2883. i++;
  2884. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2885. if (fscale < 1000) {
  2886. frac = frac * 10 + (s[i] - '0');
  2887. fscale *= 10;
  2888. }
  2889. digits = 1;
  2890. i++;
  2891. }
  2892. }
  2893. if (!digits)
  2894. return 0;
  2895. while (i < len && is_space(s[i]))
  2896. i++;
  2897. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2898. is_rem = 1;
  2899. is_px = 0;
  2900. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2901. is_rem = 1;
  2902. is_px = 0;
  2903. } else if (i < len && s[i] == '%') {
  2904. is_pct = 1;
  2905. is_px = 0;
  2906. }
  2907. v1000 = whole * 1000 + frac * 1000 / fscale;
  2908. if (is_rem)
  2909. px = v1000 * root_font_px / 1000;
  2910. else if (is_pct)
  2911. px = v1000 * root_font_px / 100000;
  2912. else if (is_px)
  2913. px = v1000 / 1000;
  2914. else
  2915. px = v1000 / 1000;
  2916. if (px < 8)
  2917. px = 8;
  2918. if (px > 72)
  2919. px = 72;
  2920. return px;
  2921. }
  2922. /* font-size keeping the unit so em/% resolve against the parent face. */
  2923. static int parse_font_size(const char *s, unsigned int len,
  2924. unsigned short *raw, unsigned char *unit)
  2925. {
  2926. unsigned int i = 0;
  2927. int whole = 0, frac = 0, scale = 1, digits = 0;
  2928. int v100;
  2929. *raw = 0;
  2930. *unit = 0;
  2931. while (i < len && is_space(s[i]))
  2932. i++;
  2933. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2934. *raw = 24;
  2935. *unit = 1;
  2936. return 1;
  2937. }
  2938. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2939. *raw = 32;
  2940. *unit = 1;
  2941. return 1;
  2942. }
  2943. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2944. *raw = 14;
  2945. *unit = 1;
  2946. return 1;
  2947. }
  2948. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2949. whole = whole * 10 + (s[i] - '0');
  2950. digits = 1;
  2951. i++;
  2952. }
  2953. if (i < len && s[i] == '.') {
  2954. i++;
  2955. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2956. frac = frac * 10 + (s[i] - '0');
  2957. scale *= 10;
  2958. i++;
  2959. }
  2960. }
  2961. if (!digits && !frac)
  2962. return 0;
  2963. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2964. while (i < len && is_space(s[i]))
  2965. i++;
  2966. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2967. *raw = (unsigned short)v100;
  2968. *unit = 3;
  2969. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2970. *raw = (unsigned short)v100;
  2971. *unit = 2;
  2972. } else if (i < len && s[i] == '%') {
  2973. /* Keep hundredths: 62.5% is a common root size and truncating
  2974. * it to 62% makes every rem 1px too small. */
  2975. *raw = (unsigned short)v100;
  2976. *unit = 4;
  2977. } else {
  2978. *raw = (unsigned short)(v100 / 100);
  2979. *unit = 1;
  2980. }
  2981. return 1;
  2982. }
  2983. static int parse_line_height(const char *s, unsigned int len,
  2984. unsigned short *px, unsigned char *pct)
  2985. {
  2986. unsigned int i = 0;
  2987. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2988. int v1000;
  2989. int is_px = 0, is_pct = 0;
  2990. *px = 0;
  2991. *pct = 0;
  2992. while (i < len && is_space(s[i]))
  2993. i++;
  2994. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2995. return 0;
  2996. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2997. whole = whole * 10 + (s[i] - '0');
  2998. digits = 1;
  2999. i++;
  3000. }
  3001. if (i < len && s[i] == '.') {
  3002. i++;
  3003. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  3004. frac = frac * 10 + (s[i] - '0');
  3005. fscale *= 10;
  3006. digits = 1;
  3007. i++;
  3008. }
  3009. }
  3010. if (!digits)
  3011. return 0;
  3012. while (i < len && is_space(s[i]))
  3013. i++;
  3014. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3015. is_px = 1;
  3016. else if (i < len && s[i] == '%')
  3017. is_pct = 1;
  3018. v1000 = whole * 1000 + frac * 1000 / fscale;
  3019. /* rem is an absolute length (the root font is known by the time
  3020. * rules are parsed, as for font-size); em behaves like a multiplier. */
  3021. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3022. v1000 = v1000 * root_font_px;
  3023. is_px = 1;
  3024. }
  3025. if (is_px) {
  3026. int v = v1000 / 1000;
  3027. if (v < 8)
  3028. v = 8;
  3029. if (v > 160)
  3030. v = 160;
  3031. *px = (unsigned short)v;
  3032. } else {
  3033. int v = v1000 / 10; /* unitless multiplier -> percent */
  3034. if (is_pct)
  3035. v = v1000 / 1000;
  3036. if (v < 50)
  3037. v = 50;
  3038. if (v > 255) /* stored in an unsigned char */
  3039. v = 255;
  3040. *pct = (unsigned char)v;
  3041. }
  3042. return 1;
  3043. }
  3044. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  3045. {
  3046. unsigned int i, j, n = s_len(needle);
  3047. for (i = 0; i + n <= len; i++) {
  3048. for (j = 0; j < n; j++) {
  3049. char a = s[i + j];
  3050. if (a >= 'A' && a <= 'Z')
  3051. a = (char)(a - 'A' + 'a');
  3052. if (a != needle[j])
  3053. break;
  3054. }
  3055. if (j == n)
  3056. return 1;
  3057. }
  3058. return 0;
  3059. }
  3060. /* Pull a colour out of a border/background shorthand. */
  3061. /* Background shorthands can hold several layers before the final solid
  3062. * colour. The colour is conventionally the last token (e.g. a stack of
  3063. * gradients followed by var(--bg)), so take the rightmost colour rather
  3064. * than the first gradient stop. */
  3065. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3066. {
  3067. int last = -1;
  3068. unsigned int i;
  3069. for (i = 0; i < len; i++) {
  3070. if (s[i] == '#')
  3071. last = (int)i;
  3072. else if ((s[i] == 'r' || s[i] == 'R') &&
  3073. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3074. last = (int)i;
  3075. }
  3076. if (last >= 0)
  3077. return parse_color(s + last, len - (unsigned int)last, c);
  3078. while (i > 0) {
  3079. unsigned int st, e;
  3080. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3081. s[i - 1] == ')' || s[i - 1] == '('))
  3082. i--;
  3083. e = i;
  3084. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3085. s[i - 1] != ')' && s[i - 1] != '(')
  3086. i--;
  3087. st = i;
  3088. if (e > st && parse_color(s + st, e - st, c))
  3089. return 1;
  3090. }
  3091. return 0;
  3092. }
  3093. static int parse_display(const char *s, unsigned int len)
  3094. {
  3095. unsigned int i;
  3096. while (len && is_space(s[0])) { s++; len--; }
  3097. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3098. ;
  3099. if (i == 4 && ci_eq_n(s, "none", 4))
  3100. return BR_DISPLAY_NONE;
  3101. if (i == 5 && ci_eq_n(s, "block", 5))
  3102. return BR_DISPLAY_BLOCK;
  3103. if (i == 6 && ci_eq_n(s, "inline", 6))
  3104. return BR_DISPLAY_INLINE;
  3105. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3106. return BR_DISPLAY_INLINE_BLOCK;
  3107. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3108. return BR_DISPLAY_BLOCK;
  3109. if (i == 4 && ci_eq_n(s, "flex", 4))
  3110. return BR_DISPLAY_FLEX;
  3111. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3112. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3113. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3114. * block instead of an inline box. */
  3115. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3116. return BR_DISPLAY_BLOCK;
  3117. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3118. return BR_DISPLAY_BLOCK;
  3119. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3120. return BR_DISPLAY_FLEX;
  3121. if (i == 5 && ci_eq_n(s, "table", 5))
  3122. return BR_DISPLAY_BLOCK;
  3123. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3124. return BR_DISPLAY_BLOCK;
  3125. /* Grid shells are common on news/wiki pages, but the two-column
  3126. * heuristic is not reliable for their nested area templates yet.
  3127. * Collapse them to block flow so the content stays readable. */
  3128. if (i == 4 && ci_eq_n(s, "grid", 4))
  3129. return BR_DISPLAY_BLOCK;
  3130. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3131. return BR_DISPLAY_BLOCK;
  3132. return BR_DISPLAY_INLINE;
  3133. }
  3134. static void decl_add(struct br_decl *out, unsigned int *nout,
  3135. unsigned char prop, int val, int important)
  3136. {
  3137. if (*nout >= BR_DECLS)
  3138. return;
  3139. out[*nout].prop = prop;
  3140. out[*nout].val = (short)val;
  3141. out[*nout].important = (unsigned char)(important != 0);
  3142. (*nout)++;
  3143. }
  3144. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3145. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3146. unsigned int *start, unsigned int *end)
  3147. {
  3148. unsigned int i = *pos;
  3149. while (i < len && is_space(s[i]))
  3150. i++;
  3151. if (i >= len)
  3152. return 0;
  3153. *start = i;
  3154. while (i < len && !is_space(s[i]))
  3155. i++;
  3156. *end = i;
  3157. *pos = i;
  3158. return 1;
  3159. }
  3160. static int bg_number(const char *s, unsigned int len, signed short *val,
  3161. unsigned char *kind)
  3162. {
  3163. unsigned int i = 0;
  3164. long v = 0;
  3165. int neg = 0, digits = 0, pct = 0;
  3166. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3167. neg = s[i] == '-';
  3168. i++;
  3169. }
  3170. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3171. v = v * 10 + (s[i] - '0');
  3172. digits = 1;
  3173. i++;
  3174. }
  3175. if (i < len && s[i] == '.') {
  3176. i++;
  3177. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3178. digits = 1;
  3179. i++;
  3180. }
  3181. }
  3182. if (!digits)
  3183. return 0;
  3184. if (i < len && s[i] == '%') {
  3185. pct = 1;
  3186. i++;
  3187. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3188. i += 2;
  3189. }
  3190. if (i != len)
  3191. return 0;
  3192. if (neg)
  3193. v = -v;
  3194. if (v < -4096)
  3195. v = -4096;
  3196. if (v > 4096)
  3197. v = 4096;
  3198. *val = (signed short)v;
  3199. *kind = (unsigned char)(pct ? 1 : 0);
  3200. return 1;
  3201. }
  3202. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3203. int *vertical)
  3204. {
  3205. *vertical = 0;
  3206. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3207. *val = 0;
  3208. return 1;
  3209. }
  3210. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3211. *val = 100;
  3212. return 1;
  3213. }
  3214. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3215. *val = 0;
  3216. *vertical = 1;
  3217. return 1;
  3218. }
  3219. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3220. *val = 100;
  3221. *vertical = 1;
  3222. return 1;
  3223. }
  3224. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3225. *val = 50;
  3226. return 1;
  3227. }
  3228. return 0;
  3229. }
  3230. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3231. const char *s, unsigned int len, int important)
  3232. {
  3233. unsigned int pos = 0, ts, te;
  3234. signed short val[2] = { 50, 50 };
  3235. unsigned char kind[2] = { 1, 1 };
  3236. int vert[2] = { 0, 0 }, n = 0;
  3237. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3238. signed short v;
  3239. int is_vert;
  3240. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3241. val[n] = v;
  3242. kind[n] = 1;
  3243. vert[n] = is_vert;
  3244. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3245. val[n] = v;
  3246. vert[n] = 0;
  3247. } else {
  3248. n++;
  3249. continue;
  3250. }
  3251. n++;
  3252. }
  3253. if (n == 1) {
  3254. if (vert[0]) {
  3255. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3256. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3257. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3258. important);
  3259. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3260. } else {
  3261. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3262. important);
  3263. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3264. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3265. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3266. }
  3267. return;
  3268. }
  3269. if (n >= 2) {
  3270. int x = vert[0] && !vert[1] ? 1 : 0;
  3271. int y = 1 - x;
  3272. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3273. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3274. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3275. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3276. }
  3277. }
  3278. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3279. const char *s, unsigned int len, int important)
  3280. {
  3281. unsigned int pos = 0, ts, te;
  3282. signed short w = 0, h = 0;
  3283. unsigned char kw = 0, kh = 0;
  3284. int n = 0;
  3285. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3286. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3287. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3288. return;
  3289. }
  3290. if (n == 0 && te - ts == 7 &&
  3291. ci_eq_n(s + ts, "contain", 7)) {
  3292. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3293. return;
  3294. }
  3295. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3296. n++;
  3297. continue;
  3298. }
  3299. {
  3300. signed short v;
  3301. unsigned char k;
  3302. if (bg_number(s + ts, te - ts, &v, &k)) {
  3303. if (n == 0) {
  3304. w = v;
  3305. kw = k;
  3306. } else {
  3307. h = v;
  3308. kh = k;
  3309. }
  3310. }
  3311. }
  3312. n++;
  3313. }
  3314. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3315. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3316. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3317. (void)kh;
  3318. }
  3319. /* Parse the `position / size` part of the background shorthand. The image
  3320. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3321. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3322. const char *v, unsigned int len,
  3323. int important)
  3324. {
  3325. unsigned int i, slash = 0, depth = 0, p, first, count;
  3326. char q = 0;
  3327. signed short nv;
  3328. int vert;
  3329. unsigned char kind;
  3330. for (i = 0; i < len; i++) {
  3331. char c = v[i];
  3332. if (q) {
  3333. if (c == q)
  3334. q = 0;
  3335. continue;
  3336. }
  3337. if (c == '\'' || c == '"') {
  3338. q = c;
  3339. continue;
  3340. }
  3341. if (c == '(') {
  3342. depth++;
  3343. continue;
  3344. }
  3345. if (c == ')') {
  3346. if (depth)
  3347. depth--;
  3348. continue;
  3349. }
  3350. if (c == '/' && !depth) {
  3351. slash = i;
  3352. break;
  3353. }
  3354. }
  3355. if (!slash)
  3356. return;
  3357. /* Up to two position tokens immediately before the slash. */
  3358. p = slash;
  3359. first = slash;
  3360. count = 0;
  3361. while (count < 2) {
  3362. unsigned int e;
  3363. while (p > 0 && is_space(v[p - 1]))
  3364. p--;
  3365. e = p;
  3366. while (p > 0 && !is_space(v[p - 1]))
  3367. p--;
  3368. if (e == p)
  3369. break;
  3370. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3371. bg_number(v + p, e - p, &nv, &kind)) {
  3372. first = p;
  3373. count++;
  3374. } else {
  3375. break;
  3376. }
  3377. }
  3378. if (count)
  3379. add_bg_position(out, nout, v + first, slash - first,
  3380. important);
  3381. /* Up to two size tokens after the slash. */
  3382. p = slash + 1;
  3383. while (p < len && is_space(v[p]))
  3384. p++;
  3385. first = p;
  3386. count = 0;
  3387. while (count < 2) {
  3388. unsigned int ts = p;
  3389. while (p < len && !is_space(v[p]))
  3390. p++;
  3391. if (p == ts)
  3392. break;
  3393. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3394. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3395. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3396. bg_number(v + ts, p - ts, &nv, &kind)) {
  3397. count++;
  3398. while (p < len && is_space(v[p]))
  3399. p++;
  3400. } else {
  3401. break;
  3402. }
  3403. }
  3404. if (count)
  3405. add_bg_size(out, nout, v + first, p - first, important);
  3406. }
  3407. static int parse_fixed_100(const char *s, unsigned int len,
  3408. unsigned int *pos, signed short *out)
  3409. {
  3410. unsigned int i = *pos;
  3411. long whole = 0, frac = 0;
  3412. int neg = 0, digits = 0, fd = 0;
  3413. while (i < len && is_space(s[i]))
  3414. i++;
  3415. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3416. neg = s[i] == '-';
  3417. i++;
  3418. }
  3419. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3420. whole = whole * 10 + (s[i] - '0');
  3421. if (whole > 327)
  3422. whole = 327;
  3423. digits = 1;
  3424. i++;
  3425. }
  3426. if (i < len && s[i] == '.') {
  3427. i++;
  3428. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3429. if (fd < 2) {
  3430. frac = frac * 10 + (s[i] - '0');
  3431. fd++;
  3432. digits = 1;
  3433. }
  3434. i++;
  3435. }
  3436. }
  3437. if (!digits)
  3438. return 0;
  3439. while (fd++ < 2)
  3440. frac *= 10;
  3441. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3442. *pos = i;
  3443. return 1;
  3444. }
  3445. /* One translate() argument: px (or rem/em/vw through parse_len), or a
  3446. * percentage of the element's own box, which only layout can resolve. */
  3447. static int translate_arg(const char *v, unsigned int vl, unsigned int *off,
  3448. int *pct)
  3449. {
  3450. unsigned int q = *off;
  3451. signed short h100;
  3452. *pct = 0;
  3453. while (q < vl && is_space(v[q]))
  3454. q++;
  3455. if (parse_fixed_100(v, vl, &q, &h100) && q < vl && v[q] == '%') {
  3456. *off = q + 1;
  3457. *pct = 1;
  3458. return h100 / 100;
  3459. }
  3460. return parse_len(v, vl, off);
  3461. }
  3462. /* "0.3s" / ".15s" / "300ms" -> milliseconds; -1 when not a time. */
  3463. static int parse_time_ms(const char *s, unsigned int len, unsigned int *pos)
  3464. {
  3465. unsigned int i = *pos;
  3466. long v = 0, scale = 1000;
  3467. int digits = 0;
  3468. while (i < len && is_space(s[i]))
  3469. i++;
  3470. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3471. v = v * 10 + (s[i] - '0');
  3472. digits = 1;
  3473. i++;
  3474. }
  3475. if (i < len && s[i] == '.') {
  3476. i++;
  3477. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3478. if (scale > 1) {
  3479. v = v * 10 + (s[i] - '0');
  3480. scale /= 10;
  3481. }
  3482. digits = 1;
  3483. i++;
  3484. }
  3485. }
  3486. if (!digits)
  3487. return -1;
  3488. if (i + 1 < len && s[i] == 'm' && s[i + 1] == 's') {
  3489. *pos = i + 2;
  3490. v = v * scale / 1000;
  3491. } else if (i < len && s[i] == 's') {
  3492. *pos = i + 1;
  3493. v = v * scale;
  3494. } else {
  3495. return -1;
  3496. }
  3497. if (v > 10000)
  3498. v = 10000;
  3499. return (int)v;
  3500. }
  3501. static int ease_code(const char *s, unsigned int n)
  3502. {
  3503. if (contains_ci_len(s, n, "ease-in-out"))
  3504. return BR_EASE_IN_OUT;
  3505. if (contains_ci_len(s, n, "ease-out"))
  3506. return BR_EASE_OUT;
  3507. if (contains_ci_len(s, n, "ease-in"))
  3508. return BR_EASE_IN;
  3509. if (contains_ci_len(s, n, "linear"))
  3510. return BR_EASE_LINEAR;
  3511. if (contains_ci_len(s, n, "cubic-bezier"))
  3512. return BR_EASE_OUT;
  3513. return BR_EASE_EASE;
  3514. }
  3515. static int tr_prop_mask(const char *s, unsigned int n)
  3516. {
  3517. int m = 0;
  3518. if (contains_ci_len(s, n, "all"))
  3519. m |= BR_TR_OPACITY | BR_TR_TRANSFORM;
  3520. if (contains_ci_len(s, n, "opacity"))
  3521. m |= BR_TR_OPACITY;
  3522. if (contains_ci_len(s, n, "transform"))
  3523. m |= BR_TR_TRANSFORM;
  3524. return m;
  3525. }
  3526. /* `transition` (shorthand, comma list) and its longhands, reduced to the
  3527. * set of animated properties the engine supports (opacity, transform),
  3528. * one duration (the longest listed) and the first easing. A shorthand
  3529. * item without a property name means `all`. */
  3530. static void add_transition(struct br_decl *out, unsigned int *nout,
  3531. const char *prop,
  3532. const char *v, unsigned int vl, int important)
  3533. {
  3534. unsigned int i = 0;
  3535. int mask = 0, ms = 0, ease = -1;
  3536. if (str_eq(prop, "transition-property")) {
  3537. decl_add(out, nout, PROP_TR_MASK, tr_prop_mask(v, vl),
  3538. important);
  3539. return;
  3540. }
  3541. if (str_eq(prop, "transition-timing-function")) {
  3542. decl_add(out, nout, PROP_TR_EASE, ease_code(v, vl), important);
  3543. return;
  3544. }
  3545. while (i < vl) {
  3546. unsigned int a = i, e, q;
  3547. int t, item_ms = -1;
  3548. while (i < vl && v[i] != ',') {
  3549. if (v[i] == '(')
  3550. while (i < vl && v[i] != ')')
  3551. i++;
  3552. i++;
  3553. }
  3554. e = i < vl ? i : vl;
  3555. for (q = a; q < e; q++) {
  3556. unsigned int p = q;
  3557. if ((q == a || is_space(v[q - 1])) &&
  3558. (t = parse_time_ms(v, e, &p)) >= 0) {
  3559. if (item_ms < 0)
  3560. item_ms = t; /* the second time is the delay */
  3561. q = p;
  3562. }
  3563. }
  3564. if (str_eq(prop, "transition-duration")) {
  3565. if (item_ms > ms)
  3566. ms = item_ms;
  3567. } else if (item_ms > 0) {
  3568. int m = tr_prop_mask(v + a, e - a);
  3569. if (!m && !contains_ci_len(v + a, e - a, "none"))
  3570. m = BR_TR_OPACITY | BR_TR_TRANSFORM;
  3571. if (m) {
  3572. mask |= m;
  3573. if (item_ms > ms)
  3574. ms = item_ms;
  3575. if (ease < 0)
  3576. ease = ease_code(v + a, e - a);
  3577. }
  3578. }
  3579. i = e + 1;
  3580. }
  3581. if (str_eq(prop, "transition-duration")) {
  3582. decl_add(out, nout, PROP_TR_MS, ms, important);
  3583. return;
  3584. }
  3585. decl_add(out, nout, PROP_TR_MASK, mask, important);
  3586. decl_add(out, nout, PROP_TR_MS, ms, important);
  3587. decl_add(out, nout, PROP_TR_EASE, ease < 0 ? BR_EASE_EASE : ease,
  3588. important);
  3589. }
  3590. static int find_fn(const char *v, unsigned int len, const char *name,
  3591. unsigned int *arg)
  3592. {
  3593. unsigned int n = s_len(name), i;
  3594. for (i = 0; i + n <= len; i++) {
  3595. unsigned int j;
  3596. if (!ci_eq_n(v + i, name, n))
  3597. continue;
  3598. j = i + n;
  3599. while (j < len && is_space(v[j]))
  3600. j++;
  3601. if (j < len && v[j] == '(') {
  3602. *arg = j + 1;
  3603. return 1;
  3604. }
  3605. }
  3606. return 0;
  3607. }
  3608. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3609. const char *s, unsigned int len,
  3610. int important)
  3611. {
  3612. unsigned int pos = 0, ts, te;
  3613. signed short val[2] = { 50, 50 };
  3614. unsigned char kind[2] = { 1, 1 };
  3615. int vert[2] = { 0, 0 }, n = 0;
  3616. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3617. signed short v;
  3618. int is_vert;
  3619. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3620. val[n] = v;
  3621. kind[n] = 1;
  3622. vert[n] = is_vert;
  3623. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3624. val[n] = v;
  3625. vert[n] = 0;
  3626. } else {
  3627. n++;
  3628. continue;
  3629. }
  3630. n++;
  3631. }
  3632. if (n == 1) {
  3633. if (vert[0]) {
  3634. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3635. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3636. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3637. important);
  3638. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3639. } else {
  3640. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3641. important);
  3642. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3643. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3644. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3645. }
  3646. return;
  3647. }
  3648. if (n >= 2) {
  3649. int x = vert[0] && !vert[1] ? 1 : 0;
  3650. int y = 1 - x;
  3651. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3652. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3653. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3654. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3655. }
  3656. }
  3657. /* Copy a background value with every url(...) body blanked (kept as
  3658. * `url()`), for the keyword and colour scans of the shorthand. */
  3659. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3660. unsigned int cap)
  3661. {
  3662. unsigned int i = 0, n = 0;
  3663. while (i < len && n + 1 < cap) {
  3664. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3665. char q = 0;
  3666. memcpy(out + n, "url(", 4);
  3667. n += 4;
  3668. i += 4;
  3669. while (i < len) {
  3670. if (q) {
  3671. if (s[i] == '\\' && i + 1 < len)
  3672. i++;
  3673. else if (s[i] == q)
  3674. q = 0;
  3675. } else if (s[i] == '"' || s[i] == '\'') {
  3676. q = s[i];
  3677. } else if (s[i] == ')') {
  3678. break;
  3679. }
  3680. i++;
  3681. }
  3682. continue; /* the ')' is copied below */
  3683. }
  3684. out[n++] = s[i++];
  3685. }
  3686. out[n] = '\0';
  3687. return n;
  3688. }
  3689. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3690. const char *v, unsigned int vl, int important)
  3691. {
  3692. unsigned int i;
  3693. for (i = 0; i + 4 <= vl; i++) {
  3694. if (ci_eq_n(v + i, "url(", 4)) {
  3695. unsigned int st = i + 4, en = st;
  3696. char q = 0;
  3697. int idx;
  3698. /* `)` inside a quoted data: URL (an SVG's
  3699. * rgba(...)) does not end it. */
  3700. while (en < vl) {
  3701. if (q) {
  3702. if (v[en] == '\\' && en + 1 < vl)
  3703. en++;
  3704. else if (v[en] == q)
  3705. q = 0;
  3706. } else if (v[en] == '"' || v[en] == '\'') {
  3707. q = v[en];
  3708. } else if (v[en] == ')') {
  3709. break;
  3710. }
  3711. en++;
  3712. }
  3713. idx = register_bg_url(v + st, en - st);
  3714. if (idx)
  3715. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3716. important);
  3717. return;
  3718. }
  3719. }
  3720. }
  3721. static int parse_len_token(const char *s, unsigned int len, int *out)
  3722. {
  3723. unsigned int pos = 0;
  3724. int v;
  3725. if (!len)
  3726. return 0;
  3727. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3728. return 0;
  3729. v = parse_len(s, len, &pos);
  3730. if (pos == 0)
  3731. return 0;
  3732. *out = v;
  3733. return 1;
  3734. }
  3735. static void expand_four(int v[4], int n)
  3736. {
  3737. if (n == 1) {
  3738. v[1] = v[2] = v[3] = v[0];
  3739. } else if (n == 2) {
  3740. v[2] = v[0];
  3741. v[3] = v[1];
  3742. } else if (n == 3) {
  3743. v[3] = v[1];
  3744. }
  3745. }
  3746. /* Custom properties defined in the declaration block being expanded. A
  3747. * component that sets a variable and uses it in the same rule (Infima's
  3748. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3749. * var(--ifm-col-width) }`) means its own value: the element matching the
  3750. * rule gets it. The document-wide table cannot know that, because
  3751. * `.col` and `.col--N` give the name a dozen different values. */
  3752. static int css_env_active; /* css_local holds an element's variables */
  3753. #define CSS_LOCAL_VARS 48
  3754. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3755. unsigned int i);
  3756. static struct {
  3757. const char *name, *val;
  3758. unsigned short nlen, vlen;
  3759. } css_local[CSS_LOCAL_VARS];
  3760. static unsigned int css_nlocal;
  3761. static int css_local_find(const char *name, unsigned int nlen)
  3762. {
  3763. unsigned int k;
  3764. for (k = 0; k < css_nlocal; k++)
  3765. if (css_local[k].nlen == nlen &&
  3766. !memcmp(css_local[k].name, name, nlen))
  3767. return (int)k;
  3768. return -1;
  3769. }
  3770. static int css_local_find_before(const char *name, unsigned int nlen,
  3771. unsigned int end)
  3772. {
  3773. unsigned int k;
  3774. for (k = 0; k < end; k++)
  3775. if (css_local[k].nlen == nlen &&
  3776. !memcmp(css_local[k].name, name, nlen))
  3777. return (int)k;
  3778. return -1;
  3779. }
  3780. static void css_local_append(const char *s, unsigned int len);
  3781. /* Collect the top-level `--name: value` declarations of a block. */
  3782. static void css_local_collect(const char *s, unsigned int len)
  3783. {
  3784. css_nlocal = 0;
  3785. css_local_append(s, len);
  3786. }
  3787. static void css_local_append(const char *s, unsigned int len)
  3788. {
  3789. unsigned int i = 0;
  3790. while (i < len) {
  3791. unsigned int ns, ne, vs, ve;
  3792. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3793. i++;
  3794. ns = i;
  3795. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3796. i++;
  3797. if (i >= len || s[i] != ':') {
  3798. if (i < len && s[i] == '{')
  3799. i = css_skip_nested(s, len, i + 1);
  3800. continue;
  3801. }
  3802. ne = i;
  3803. while (ne > ns && is_space(s[ne - 1]))
  3804. ne--;
  3805. vs = i + 1;
  3806. ve = css_value_end(s, len, vs);
  3807. i = ve;
  3808. while (vs < ve && is_space(s[vs]))
  3809. vs++;
  3810. while (ve > vs && is_space(s[ve - 1]))
  3811. ve--;
  3812. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3813. ve -= 10;
  3814. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3815. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3816. continue;
  3817. css_local[css_nlocal].name = s + ns;
  3818. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3819. css_local[css_nlocal].val = s + vs;
  3820. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3821. css_nlocal++;
  3822. }
  3823. }
  3824. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3825. * the input is copied verbatim when it contains no var(). */
  3826. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3827. unsigned int len, char *out,
  3828. unsigned int cap, int depth)
  3829. {
  3830. unsigned int i = 0, n = 0;
  3831. if (depth > 8)
  3832. return 0;
  3833. while (i < len && n + 1 < cap) {
  3834. unsigned int p, inner, close, comma, nlen, fstart;
  3835. char quote;
  3836. int parens, glue;
  3837. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3838. out[n++] = s[i++];
  3839. continue;
  3840. }
  3841. p = i + 4;
  3842. quote = 0;
  3843. parens = 1;
  3844. while (p < len && parens) {
  3845. char c = s[p];
  3846. if (quote) {
  3847. if (c == quote)
  3848. quote = 0;
  3849. else if (c == '\\' && p + 1 < len)
  3850. p++;
  3851. } else if (c == '"' || c == '\'') {
  3852. quote = c;
  3853. } else if (c == '(') {
  3854. parens++;
  3855. } else if (c == ')') {
  3856. parens--;
  3857. }
  3858. p++;
  3859. }
  3860. if (parens) {
  3861. out[n++] = s[i++];
  3862. continue;
  3863. }
  3864. close = p - 1;
  3865. inner = i + 4;
  3866. /* var() substitutes tokens: minified Infima writes
  3867. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3868. * must not become the one token `700.875rem`. Separate the
  3869. * value from a glued neighbour on both sides. */
  3870. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3871. out[n - 1] != '(' && out[n - 1] != ',' &&
  3872. out[n - 1] != ':' && out[n - 1] != '/')
  3873. out[n++] = ' ';
  3874. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3875. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3876. s[p] != '!' && s[p] != '}';
  3877. while (inner < close && is_space(s[inner]))
  3878. inner++;
  3879. comma = inner;
  3880. parens = 0;
  3881. quote = 0;
  3882. while (comma < close) {
  3883. char c = s[comma];
  3884. if (quote) {
  3885. if (c == quote)
  3886. quote = 0;
  3887. } else if (c == '"' || c == '\'') {
  3888. quote = c;
  3889. } else if (c == '(') {
  3890. parens++;
  3891. } else if (c == ')' && parens > 0) {
  3892. parens--;
  3893. } else if (c == ',' && parens == 0) {
  3894. break;
  3895. }
  3896. comma++;
  3897. }
  3898. nlen = comma - inner;
  3899. while (nlen && is_space(s[inner + nlen - 1]))
  3900. nlen--;
  3901. fstart = comma < close ? comma + 1 : close;
  3902. while (fstart < close && is_space(s[fstart]))
  3903. fstart++;
  3904. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3905. unsigned int vi = css_var_find(d, s + inner, nlen);
  3906. int li = css_local_find(s + inner, nlen);
  3907. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3908. s + close + 1) {
  3909. /* (not the definition's own value) */
  3910. n += css_expand_vars(d, css_local[li].val,
  3911. css_local[li].vlen,
  3912. out + n, cap - n,
  3913. depth + 1);
  3914. i = p;
  3915. if (glue && n + 1 < cap)
  3916. out[n++] = ' ';
  3917. continue;
  3918. }
  3919. if (vi < BR_MAX_VARS &&
  3920. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3921. vi = BR_MAX_VARS;
  3922. if (vi < BR_MAX_VARS) {
  3923. const char *v = attr_ptr(d, d->var_val[vi]);
  3924. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3925. out + n,
  3926. cap - n, depth + 1);
  3927. n += vn;
  3928. } else if (fstart < close) {
  3929. unsigned int vn = css_expand_vars(d, s + fstart,
  3930. close - fstart,
  3931. out + n, cap - n,
  3932. depth + 1);
  3933. n += vn;
  3934. }
  3935. } else if (fstart < close) {
  3936. unsigned int vn = css_expand_vars(d, s + fstart,
  3937. close - fstart,
  3938. out + n, cap - n,
  3939. depth + 1);
  3940. n += vn;
  3941. }
  3942. i = p;
  3943. if (glue && n + 1 < cap)
  3944. out[n++] = ' ';
  3945. }
  3946. out[n] = '\0';
  3947. return n;
  3948. }
  3949. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3950. unsigned int i)
  3951. {
  3952. while (i < len && (is_space(s[i]) || s[i] == ','))
  3953. i++;
  3954. return i;
  3955. }
  3956. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3957. * argument). Returns 1 on success. */
  3958. static int parse_grid_track(const char *s, unsigned int len,
  3959. unsigned int *pp, unsigned short *val,
  3960. unsigned char *kind)
  3961. {
  3962. unsigned int i = grid_skip_sep(s, len, *pp);
  3963. unsigned int whole = 0, frac = 0, scale = 1;
  3964. int any = 0;
  3965. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3966. unsigned int j = i + 7;
  3967. while (j < len && s[j] != ',')
  3968. j++;
  3969. if (j >= len)
  3970. return 0;
  3971. i = j + 1;
  3972. while (i < len && is_space(s[i]))
  3973. i++;
  3974. }
  3975. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3976. int depth = 1;
  3977. i += 7;
  3978. while (i < len && depth) {
  3979. if (s[i] == '(')
  3980. depth++;
  3981. else if (s[i] == ')')
  3982. depth--;
  3983. i++;
  3984. }
  3985. *pp = i;
  3986. return 0;
  3987. }
  3988. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3989. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3990. i++;
  3991. any = 1;
  3992. }
  3993. if (i < len && s[i] == '.') {
  3994. i++;
  3995. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3996. if (scale < 1000) {
  3997. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3998. scale *= 10;
  3999. }
  4000. i++;
  4001. any = 1;
  4002. }
  4003. }
  4004. if (!any) {
  4005. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  4006. *val = 1;
  4007. *kind = 3;
  4008. *pp = i + 4;
  4009. return 1;
  4010. }
  4011. return 0;
  4012. }
  4013. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  4014. *val = (unsigned short)(whole ? whole : 1);
  4015. *kind = 3;
  4016. i += 2;
  4017. } else if (i < len && s[i] == '%') {
  4018. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  4019. *kind = 2;
  4020. i++;
  4021. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  4022. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  4023. *kind = 1;
  4024. i += 3;
  4025. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  4026. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  4027. *kind = 1;
  4028. i += 2;
  4029. } else {
  4030. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  4031. *kind = 1;
  4032. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  4033. i += 2;
  4034. }
  4035. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  4036. i++;
  4037. *pp = i;
  4038. return 1;
  4039. }
  4040. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  4041. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  4042. * keep the two-column path). */
  4043. static int grid_repeat_count(const char *s, unsigned int len)
  4044. {
  4045. unsigned int i = 0;
  4046. int n = 0;
  4047. while (i < len && is_space(s[i]))
  4048. i++;
  4049. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  4050. i += 7;
  4051. while (i < len && is_space(s[i]))
  4052. i++;
  4053. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4054. n = n * 10 + (s[i] - '0');
  4055. i++;
  4056. }
  4057. if (n < 1 || n > 12)
  4058. return 0;
  4059. while (i < len && (is_space(s[i]) || s[i] == ','))
  4060. i++;
  4061. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  4062. return n;
  4063. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  4064. i += 7;
  4065. while (i < len && is_space(s[i]))
  4066. i++;
  4067. if (i < len && s[i] == '0') {
  4068. i++;
  4069. while (i < len && (is_space(s[i]) ||
  4070. s[i] == ',' || s[i] == 'p' ||
  4071. s[i] == 'x'))
  4072. i++;
  4073. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  4074. return n;
  4075. }
  4076. }
  4077. return 0;
  4078. }
  4079. /* "1fr 1fr 1fr" */
  4080. for (;;) {
  4081. while (i < len && is_space(s[i]))
  4082. i++;
  4083. if (i >= len)
  4084. break;
  4085. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  4086. (i + 3 == len || is_space(s[i + 3]))) {
  4087. n++;
  4088. i += 3;
  4089. } else {
  4090. return 0;
  4091. }
  4092. }
  4093. return n >= 3 ? n : 0;
  4094. }
  4095. static int parse_grid_cols(const char *s, unsigned int len,
  4096. unsigned short val[2], unsigned char kind[2])
  4097. {
  4098. unsigned int pos = 0;
  4099. int n = 0;
  4100. while (n < 2) {
  4101. unsigned int before = pos;
  4102. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  4103. break;
  4104. n++;
  4105. if (pos <= before)
  4106. break;
  4107. while (pos < len && is_space(s[pos]))
  4108. pos++;
  4109. if (pos >= len)
  4110. break;
  4111. }
  4112. return n;
  4113. }
  4114. static int parse_grid_slot(const char *s, unsigned int len)
  4115. {
  4116. if (contains_ci_len(s, len, "footer") ||
  4117. contains_ci_len(s, len, "header") ||
  4118. contains_ci_len(s, len, "sitenotice") ||
  4119. contains_ci_len(s, len, "navbar"))
  4120. return 1;
  4121. if (contains_ci_len(s, len, "columnstart") ||
  4122. contains_ci_len(s, len, "sidebar") ||
  4123. contains_ci_len(s, len, "left") ||
  4124. contains_ci_len(s, len, "start"))
  4125. return 2;
  4126. if (contains_ci_len(s, len, "pagecontent") ||
  4127. contains_ci_len(s, len, "content") ||
  4128. contains_ci_len(s, len, "main") ||
  4129. contains_ci_len(s, len, "right") ||
  4130. contains_ci_len(s, len, "end"))
  4131. return 3;
  4132. return 0;
  4133. }
  4134. static int css_has_var(const char *s, unsigned int len)
  4135. {
  4136. unsigned int i;
  4137. for (i = 0; i + 4 <= len; i++)
  4138. if ((s[i] == 'v' || s[i] == 'V') &&
  4139. ci_eq_n(s + i, "var(", 4))
  4140. return 1;
  4141. return 0;
  4142. }
  4143. static unsigned int css_skip_comment(const char *s, unsigned int len,
  4144. unsigned int i)
  4145. {
  4146. i += 2;
  4147. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  4148. i++;
  4149. if (i + 1 < len)
  4150. i += 2;
  4151. else
  4152. i = len;
  4153. return i;
  4154. }
  4155. static unsigned int css_skip_string(const char *s, unsigned int len,
  4156. unsigned int i)
  4157. {
  4158. char q = s[i++];
  4159. while (i < len && s[i] != q) {
  4160. if (s[i] == '\\' && i + 1 < len)
  4161. i++;
  4162. i++;
  4163. }
  4164. if (i < len)
  4165. i++;
  4166. return i;
  4167. }
  4168. /* Find the '}' that closes a declaration block. i points just past the
  4169. * opening '{'. Comments and quoted strings are skipped: a brace inside
  4170. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  4171. * selectors such as "Highlight-Stories {" without a closing brace). */
  4172. static unsigned int css_block_end(const char *s, unsigned int len,
  4173. unsigned int i)
  4174. {
  4175. int depth = 0;
  4176. while (i < len) {
  4177. char c = s[i];
  4178. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4179. i = css_skip_comment(s, len, i);
  4180. continue;
  4181. }
  4182. if (c == '"' || c == '\'') {
  4183. i = css_skip_string(s, len, i);
  4184. continue;
  4185. }
  4186. if (c == '{') {
  4187. depth++;
  4188. } else if (c == '}') {
  4189. if (depth == 0)
  4190. return i;
  4191. depth--;
  4192. }
  4193. i++;
  4194. }
  4195. return len;
  4196. }
  4197. /* Skip a block whose opening '{' has already been consumed. */
  4198. static unsigned int css_skip_nested(const char *s, unsigned int len,
  4199. unsigned int i)
  4200. {
  4201. int depth = 1;
  4202. while (i < len && depth) {
  4203. char c = s[i];
  4204. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4205. i = css_skip_comment(s, len, i);
  4206. continue;
  4207. }
  4208. if (c == '"' || c == '\'') {
  4209. i = css_skip_string(s, len, i);
  4210. continue;
  4211. }
  4212. if (c == '{')
  4213. depth++;
  4214. else if (c == '}')
  4215. depth--;
  4216. i++;
  4217. }
  4218. return i;
  4219. }
  4220. /* ---- @font-face ------------------------------------------------------- */
  4221. static void font_trim(const char **sp, unsigned int *len)
  4222. {
  4223. const char *s = *sp;
  4224. unsigned int n = *len;
  4225. while (n && is_space(*s)) {
  4226. s++;
  4227. n--;
  4228. }
  4229. while (n && is_space(s[n - 1]))
  4230. n--;
  4231. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4232. s++;
  4233. n -= 2;
  4234. }
  4235. *sp = s;
  4236. *len = n;
  4237. }
  4238. static int font_name_eq(const char *a, const char *b, unsigned int blen)
  4239. {
  4240. unsigned int k;
  4241. if (s_len(a) != blen)
  4242. return 0;
  4243. for (k = 0; k < blen; k++) {
  4244. char x = a[k], y = b[k];
  4245. if (x >= 'A' && x <= 'Z')
  4246. x = (char)(x - 'A' + 'a');
  4247. if (y >= 'A' && y <= 'Z')
  4248. y = (char)(y - 'A' + 'a');
  4249. if (x != y)
  4250. return 0;
  4251. }
  4252. return 1;
  4253. }
  4254. static void font_copy(char *dst, unsigned int cap, const char *src,
  4255. unsigned int n)
  4256. {
  4257. unsigned int k;
  4258. for (k = 0; k < n && src[k] && k + 1 < cap; k++)
  4259. dst[k] = src[k];
  4260. dst[k] = '\0';
  4261. }
  4262. /* Register one @font-face. Every (family, weight range, style) is its own
  4263. * entry; a later rule with the same triple replaces the URL, as the CSS
  4264. * font-face collection does. family_id is the 1-based id of the family's
  4265. * first entry, which is what font-family declarations store. */
  4266. static void font_add(const char *family, unsigned int flen,
  4267. const char *url, unsigned int ulen,
  4268. unsigned int wmin, unsigned int wmax, int italic)
  4269. {
  4270. unsigned int i;
  4271. int fam = 0;
  4272. struct br_font_def *f;
  4273. font_trim(&family, &flen);
  4274. font_trim(&url, &ulen);
  4275. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4276. return;
  4277. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4278. f = &font_defs[i];
  4279. if (!font_name_eq(f->family, family, flen))
  4280. continue;
  4281. if (!fam)
  4282. fam = f->family_id;
  4283. if (f->wmin != wmin || f->wmax != wmax ||
  4284. f->italic != (unsigned char)italic)
  4285. continue;
  4286. font_copy(f->url, sizeof(f->url), url, ulen);
  4287. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4288. return;
  4289. }
  4290. if (font_face_count >= BR_MAX_FACES)
  4291. return;
  4292. f = &font_defs[font_face_count];
  4293. font_copy(f->family, sizeof(f->family), family, flen);
  4294. font_copy(f->url, sizeof(f->url), url, ulen);
  4295. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4296. f->wmin = (unsigned short)wmin;
  4297. f->wmax = (unsigned short)wmax;
  4298. f->italic = (unsigned char)italic;
  4299. f->used = 0;
  4300. font_face_count++;
  4301. f->family_id = (unsigned char)(fam ? fam : font_face_count);
  4302. /* A family that a font-family declaration already resolved (an
  4303. * earlier @font-face of it) stays wanted for its new variants. */
  4304. if (fam && font_defs[fam - 1].used)
  4305. f->used = 1;
  4306. }
  4307. /* Look up the first name in a font-family list that has an @font-face and
  4308. * mark that family as used, so the browser only downloads faces the page
  4309. * refers to. */
  4310. static int font_family_id(const char *s, unsigned int len)
  4311. {
  4312. unsigned int i = 0;
  4313. while (i < len) {
  4314. unsigned int start, end, q;
  4315. while (i < len && (is_space(s[i]) || s[i] == ','))
  4316. i++;
  4317. if (i >= len)
  4318. break;
  4319. if (s[i] == '"' || s[i] == '\'') {
  4320. char quote = s[i++];
  4321. start = i;
  4322. while (i < len && s[i] != quote)
  4323. i++;
  4324. end = i;
  4325. if (i < len)
  4326. i++;
  4327. } else {
  4328. start = i;
  4329. while (i < len && s[i] != ',')
  4330. i++;
  4331. end = i;
  4332. while (end > start && is_space(s[end - 1]))
  4333. end--;
  4334. }
  4335. if (end <= start)
  4336. continue;
  4337. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4338. int fam;
  4339. unsigned int k;
  4340. if (!font_name_eq(font_defs[q].family, s + start,
  4341. end - start))
  4342. continue;
  4343. fam = font_defs[q].family_id;
  4344. for (k = 0; k < (unsigned int)font_face_count; k++)
  4345. if (font_defs[k].family_id == fam)
  4346. font_defs[k].used = 1;
  4347. return fam;
  4348. }
  4349. }
  4350. return 0;
  4351. }
  4352. /* font-weight descriptor: `normal`, `bold`, a number, or a variable font's
  4353. * `100 900` range. */
  4354. static void font_parse_weight(const char *s, unsigned int len,
  4355. unsigned int *wmin, unsigned int *wmax)
  4356. {
  4357. unsigned int i = 0, n = 0, w[2] = { 400, 400 };
  4358. while (i < len && n < 2) {
  4359. unsigned int v = 0, st;
  4360. while (i < len && is_space(s[i]))
  4361. i++;
  4362. st = i;
  4363. while (i < len && !is_space(s[i]))
  4364. i++;
  4365. if (i == st)
  4366. break;
  4367. if (i - st == 4 && ci_eq_n(s + st, "bold", 4)) {
  4368. v = 700;
  4369. } else if (i - st == 6 && ci_eq_n(s + st, "bolder", 6)) {
  4370. v = 700;
  4371. } else if (i - st == 7 && ci_eq_n(s + st, "lighter", 7)) {
  4372. v = 300;
  4373. } else if (s[st] >= '0' && s[st] <= '9') {
  4374. unsigned int k;
  4375. for (k = st; k < i && s[k] >= '0' && s[k] <= '9'; k++)
  4376. v = v * 10 + (unsigned int)(s[k] - '0');
  4377. } else {
  4378. v = 400;
  4379. }
  4380. if (v < 1)
  4381. v = 1;
  4382. if (v > 1000)
  4383. v = 1000;
  4384. w[n++] = v;
  4385. }
  4386. if (n == 1)
  4387. w[1] = w[0];
  4388. if (w[0] > w[1]) {
  4389. unsigned int t = w[0];
  4390. w[0] = w[1];
  4391. w[1] = t;
  4392. }
  4393. *wmin = w[0];
  4394. *wmax = w[1];
  4395. }
  4396. static int font_hex(char c)
  4397. {
  4398. if (c >= '0' && c <= '9')
  4399. return c - '0';
  4400. if (c >= 'a' && c <= 'f')
  4401. return c - 'a' + 10;
  4402. if (c >= 'A' && c <= 'F')
  4403. return c - 'A' + 10;
  4404. return -1;
  4405. }
  4406. /* Whether a unicode-range list covers basic Latin. Google Fonts splits a
  4407. * family into per-script subsets with the same family/weight/style; only
  4408. * the one holding ASCII is useful here (the text path is Latin-1). */
  4409. static int font_range_has_latin(const char *s, unsigned int len)
  4410. {
  4411. unsigned int i = 0;
  4412. while (i < len) {
  4413. unsigned int lo = 0, hi, digits = 0, wild = 0;
  4414. while (i < len && (is_space(s[i]) || s[i] == ','))
  4415. i++;
  4416. if (i + 2 > len || (s[i] != 'U' && s[i] != 'u') ||
  4417. s[i + 1] != '+') {
  4418. while (i < len && s[i] != ',')
  4419. i++;
  4420. continue;
  4421. }
  4422. i += 2;
  4423. while (i < len && digits < 6 &&
  4424. (font_hex(s[i]) >= 0 || s[i] == '?')) {
  4425. if (s[i] == '?') {
  4426. lo <<= 4;
  4427. wild++;
  4428. } else {
  4429. lo = (lo << 4) | (unsigned int)font_hex(s[i]);
  4430. }
  4431. digits++;
  4432. i++;
  4433. }
  4434. hi = lo | ((1u << (wild * 4)) - 1u);
  4435. if (!wild && i < len && s[i] == '-') {
  4436. hi = 0;
  4437. i++;
  4438. while (i < len && font_hex(s[i]) >= 0) {
  4439. hi = (hi << 4) | (unsigned int)font_hex(s[i]);
  4440. i++;
  4441. }
  4442. }
  4443. if (digits && lo <= 'a' && hi >= 'a')
  4444. return 1;
  4445. while (i < len && s[i] != ',')
  4446. i++;
  4447. }
  4448. return 0;
  4449. }
  4450. /* Pick the first `src` entry the text engine can load: a TrueType/OpenType
  4451. * file or WOFF 1.0, by its format() hint or, without one, by its
  4452. * extension. local() names, WOFF2, EOT, SVG fonts and data: URLs (they do
  4453. * not fit BR_FACE_URL) are skipped. */
  4454. static int font_pick_src(const char *s, unsigned int len, char *url,
  4455. unsigned int cap, unsigned int *ulen)
  4456. {
  4457. unsigned int i = 0;
  4458. while (i < len) {
  4459. unsigned int is_ = i, ie, us = 0, ue = 0, q, depth = 0;
  4460. int quote = 0, ok;
  4461. const char *up;
  4462. unsigned int un;
  4463. /* One comma-separated item, commas inside url()/quotes kept. */
  4464. while (i < len) {
  4465. char c = s[i];
  4466. if (quote) {
  4467. if (c == quote)
  4468. quote = 0;
  4469. } else if (c == '"' || c == '\'') {
  4470. quote = c;
  4471. } else if (c == '(') {
  4472. depth++;
  4473. } else if (c == ')') {
  4474. if (depth)
  4475. depth--;
  4476. } else if (c == ',' && !depth) {
  4477. break;
  4478. }
  4479. i++;
  4480. }
  4481. ie = i;
  4482. i++;
  4483. for (q = is_; q + 4 <= ie; q++)
  4484. if (ci_eq_n(s + q, "url(", 4))
  4485. break;
  4486. if (q + 4 > ie)
  4487. continue;
  4488. us = q + 4;
  4489. for (ue = us; ue < ie && s[ue] != ')'; ue++)
  4490. if (s[ue] == '"' || s[ue] == '\'') {
  4491. char qc = s[ue++];
  4492. while (ue < ie && s[ue] != qc)
  4493. ue++;
  4494. }
  4495. up = s + us;
  4496. un = ue - us;
  4497. font_trim(&up, &un);
  4498. if (!un || un >= cap || (un >= 5 && ci_eq_n(up, "data:", 5)))
  4499. continue;
  4500. ok = 1;
  4501. if (contains_ci_len(s + ue, ie - ue, "format(")) {
  4502. const char *fp = s + ue;
  4503. unsigned int fn = ie - ue;
  4504. ok = (contains_ci_len(fp, fn, "truetype") ||
  4505. contains_ci_len(fp, fn, "opentype") ||
  4506. contains_ci_len(fp, fn, "woff")) &&
  4507. !contains_ci_len(fp, fn, "woff2") &&
  4508. !contains_ci_len(fp, fn, "embedded");
  4509. } else {
  4510. unsigned int e = 0;
  4511. while (e < un && up[e] != '?' && up[e] != '#')
  4512. e++;
  4513. if ((e >= 6 && ci_eq_n(up + e - 6, ".woff2", 6)) ||
  4514. (e >= 4 && ci_eq_n(up + e - 4, ".eot", 4)) ||
  4515. (e >= 4 && ci_eq_n(up + e - 4, ".svg", 4)))
  4516. ok = 0;
  4517. }
  4518. if (!ok)
  4519. continue;
  4520. for (q = 0; q < un; q++)
  4521. url[q] = up[q];
  4522. url[un] = '\0';
  4523. *ulen = un;
  4524. return 1;
  4525. }
  4526. return 0;
  4527. }
  4528. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4529. * at-rules, so faces must be registered before any font-family declaration
  4530. * in the same sheet is resolved. */
  4531. static void scan_font_faces(const char *s, unsigned int len)
  4532. {
  4533. unsigned int i = 0;
  4534. while (i + 10 <= len) {
  4535. unsigned int j, end;
  4536. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4537. unsigned int flen = 0, ulen = 0, p;
  4538. unsigned int wmin = 400, wmax = 400;
  4539. int have_family = 0, have_url = 0, italic = 0, latin = 1;
  4540. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4541. i++;
  4542. continue;
  4543. }
  4544. j = i + 10;
  4545. while (j < len && s[j] != '{' && s[j] != ';')
  4546. j++;
  4547. if (j >= len || s[j] == ';') {
  4548. i = j + 1;
  4549. continue;
  4550. }
  4551. end = css_skip_nested(s, len, j + 1);
  4552. p = j + 1;
  4553. while (p < end) {
  4554. unsigned int ds, de, vs, ve, quote = 0;
  4555. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4556. p++;
  4557. if (p >= end)
  4558. break;
  4559. ds = p;
  4560. while (p < end && s[p] != ':' && s[p] != ';' &&
  4561. s[p] != '}')
  4562. p++;
  4563. de = p;
  4564. if (p >= end || s[p] != ':') {
  4565. while (p < end && s[p] != ';')
  4566. p++;
  4567. continue;
  4568. }
  4569. p++;
  4570. vs = p;
  4571. /* A quoted URL may contain ';' (data: URLs do). */
  4572. while (p < end && (quote || (s[p] != ';' &&
  4573. s[p] != '}'))) {
  4574. if (quote && s[p] == (char)quote)
  4575. quote = 0;
  4576. else if (!quote && (s[p] == '"' || s[p] == '\''))
  4577. quote = (unsigned char)s[p];
  4578. p++;
  4579. }
  4580. ve = p;
  4581. while (ds < de && is_space(s[ds]))
  4582. ds++;
  4583. while (de > ds && is_space(s[de - 1]))
  4584. de--;
  4585. while (vs < ve && is_space(s[vs]))
  4586. vs++;
  4587. while (ve > vs && is_space(s[ve - 1]))
  4588. ve--;
  4589. if (de - ds == 11 &&
  4590. ci_eq_n(s + ds, "font-family", 11)) {
  4591. const char *fp = s + vs;
  4592. unsigned int fn = ve - vs;
  4593. font_trim(&fp, &fn);
  4594. if (fn && fn < sizeof(family)) {
  4595. unsigned int k;
  4596. for (k = 0; k < fn; k++)
  4597. family[k] = fp[k];
  4598. family[fn] = '\0';
  4599. flen = fn;
  4600. have_family = 1;
  4601. }
  4602. } else if (de - ds == 3 &&
  4603. ci_eq_n(s + ds, "src", 3)) {
  4604. /* A later src (the old IE `src: eot;` hack
  4605. * is followed by the real list) wins when
  4606. * it has a usable entry. */
  4607. if (font_pick_src(s + vs, ve - vs, url,
  4608. sizeof(url), &ulen))
  4609. have_url = 1;
  4610. } else if (de - ds == 11 &&
  4611. ci_eq_n(s + ds, "font-weight", 11)) {
  4612. font_parse_weight(s + vs, ve - vs, &wmin,
  4613. &wmax);
  4614. } else if (de - ds == 10 &&
  4615. ci_eq_n(s + ds, "font-style", 10)) {
  4616. italic = contains_ci_len(s + vs, ve - vs,
  4617. "italic") ||
  4618. contains_ci_len(s + vs, ve - vs,
  4619. "oblique");
  4620. } else if (de - ds == 13 &&
  4621. ci_eq_n(s + ds, "unicode-range", 13)) {
  4622. latin = font_range_has_latin(s + vs, ve - vs);
  4623. }
  4624. }
  4625. if (have_family && have_url && latin)
  4626. font_add(family, flen, url, ulen, wmin, wmax, italic);
  4627. i = end;
  4628. }
  4629. }
  4630. /* One border-radius length. Unlike parse_size(), percentages keep their
  4631. * kind: the drawer scales them to the box, not the viewport. */
  4632. static int parse_radius_value(const char *s, unsigned int len,
  4633. unsigned int *pos, unsigned short *val,
  4634. unsigned char *kind)
  4635. {
  4636. unsigned int i = *pos;
  4637. unsigned long v = 0;
  4638. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4639. while (i < len && is_space(s[i]))
  4640. i++;
  4641. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4642. v = v * 10 + (unsigned int)(s[i] - '0');
  4643. digits = 1;
  4644. i++;
  4645. }
  4646. if (i < len && s[i] == '.') {
  4647. i++;
  4648. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4649. fdigits = 1;
  4650. if (frac < 2) {
  4651. v = v * 10 + (unsigned int)(s[i] - '0');
  4652. frac++;
  4653. }
  4654. i++;
  4655. }
  4656. }
  4657. while (frac < 2) {
  4658. v *= 10;
  4659. frac++;
  4660. }
  4661. while (i < len && is_space(s[i]))
  4662. i++;
  4663. if (i < len && s[i] == '%') {
  4664. pct = 1;
  4665. i++;
  4666. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4667. s[i + 2] == 'm') {
  4668. rem = 1;
  4669. i += 3;
  4670. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4671. rem = 2;
  4672. i += 2;
  4673. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4674. i += 2;
  4675. } else if (i < len && is_ident(s[i])) {
  4676. return 0;
  4677. }
  4678. if (!digits && !fdigits)
  4679. return 0;
  4680. if (rem == 1)
  4681. v *= root_font_px;
  4682. else if (rem == 2)
  4683. v *= root_font_px;
  4684. v /= 100;
  4685. if (v > 2048)
  4686. v = 2048;
  4687. *val = (unsigned short)v;
  4688. *kind = pct ? 2 : 1;
  4689. *pos = i;
  4690. return 1;
  4691. }
  4692. /* font: [style] [variant] [weight] size[/line-height] family */
  4693. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4694. const char *v, unsigned int len, int important)
  4695. {
  4696. unsigned int pos = 0, ts, te;
  4697. unsigned short size_raw = 0;
  4698. unsigned char size_unit = 0;
  4699. int have_size = 0;
  4700. int bold = 0, italic = 0;
  4701. int lh_px = 0, lh_pct = 0, mono = 0, face;
  4702. unsigned int fam_at = len;
  4703. while (next_token(v, len, &pos, &ts, &te)) {
  4704. unsigned int tl = te - ts;
  4705. unsigned int slash = 0;
  4706. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4707. bold = 1;
  4708. continue;
  4709. }
  4710. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4711. continue;
  4712. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4713. italic = 1;
  4714. continue;
  4715. }
  4716. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4717. italic = 1;
  4718. continue;
  4719. }
  4720. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4721. continue;
  4722. for (slash = 0; slash < tl; slash++)
  4723. if (v[ts + slash] == '/')
  4724. break;
  4725. if (slash == tl) {
  4726. unsigned int q;
  4727. int weight = 0;
  4728. for (q = 0; q < tl; q++) {
  4729. if (v[ts + q] < '0' || v[ts + q] > '9')
  4730. break;
  4731. weight = weight * 10 + (v[ts + q] - '0');
  4732. }
  4733. if (q == tl && weight >= 100 && weight <= 900) {
  4734. bold = weight >= 500;
  4735. continue;
  4736. }
  4737. }
  4738. {
  4739. unsigned short raw;
  4740. unsigned char unit;
  4741. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4742. &raw, &unit))
  4743. continue;
  4744. size_raw = raw;
  4745. size_unit = unit;
  4746. }
  4747. if (!have_size)
  4748. fam_at = te; /* the family list follows the size */
  4749. have_size = 1;
  4750. if (slash < tl) {
  4751. unsigned int lstart = ts + slash + 1;
  4752. unsigned short lhp = 0;
  4753. unsigned char lhpct = 0;
  4754. if (!parse_line_height(v + lstart, te - lstart,
  4755. &lhp, &lhpct)) {
  4756. lh_px = 0;
  4757. lh_pct = 0;
  4758. } else {
  4759. lh_px = lhp;
  4760. lh_pct = lhpct;
  4761. }
  4762. }
  4763. }
  4764. if (!have_size)
  4765. return;
  4766. mono = contains_ci_len(v, len, "mono");
  4767. face = font_family_id(v + fam_at, len - fam_at);
  4768. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4769. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4770. decl_add(out, nout, PROP_BOLD, bold, important);
  4771. decl_add(out, nout, PROP_ITALIC, italic, important);
  4772. decl_add(out, nout, PROP_MONO, mono, important);
  4773. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4774. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4775. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4776. }
  4777. static int css_collect_only;
  4778. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4779. * index into d->layer_hash. css_layer_name is the dotted path of the
  4780. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4781. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4782. * layer's own rules therefore lose to its sublayers (the spec says the
  4783. * reverse), which no page we render depends on. */
  4784. static unsigned char css_cur_layer;
  4785. static char css_layer_name[128];
  4786. static unsigned int css_anon_layers;
  4787. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4788. {
  4789. unsigned int h = 2166136261u, i;
  4790. for (i = 0; i < len; i++) {
  4791. h ^= (unsigned char)s[i];
  4792. h *= 16777619u;
  4793. }
  4794. return h ? h : 1;
  4795. }
  4796. /* Register (or look up) the layer `name` nested under the current path and
  4797. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4798. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4799. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4800. unsigned int nlen, char *path,
  4801. unsigned int cap)
  4802. {
  4803. unsigned int pl = s_len(css_layer_name), i, h;
  4804. char anon[16];
  4805. while (nlen && is_space(*name)) { name++; nlen--; }
  4806. while (nlen && is_space(name[nlen - 1])) nlen--;
  4807. if (!nlen) {
  4808. /* An anonymous layer is unique; it can never be reopened.
  4809. * The collect-only (custom property) pass does not need a
  4810. * rank, so it must not burn a table slot. */
  4811. unsigned int n = ++css_anon_layers, k = 0;
  4812. char rev[10];
  4813. if (css_collect_only)
  4814. return 0;
  4815. anon[0] = '#';
  4816. while (n && k < sizeof(rev)) {
  4817. rev[k++] = (char)('0' + n % 10);
  4818. n /= 10;
  4819. }
  4820. for (i = 0; i < k; i++)
  4821. anon[1 + i] = rev[k - 1 - i];
  4822. name = anon;
  4823. nlen = k + 1;
  4824. }
  4825. if (pl + 1 + nlen >= cap)
  4826. return 0;
  4827. memcpy(path, css_layer_name, pl);
  4828. if (pl)
  4829. path[pl++] = '.';
  4830. memcpy(path + pl, name, nlen);
  4831. path[pl + nlen] = '\0';
  4832. h = layer_name_hash(path, pl + nlen);
  4833. for (i = 0; i < d->nlayers; i++)
  4834. if (d->layer_hash[i] == h)
  4835. return (unsigned char)(i + 1);
  4836. if (d->nlayers >= BR_MAX_LAYERS)
  4837. return 0;
  4838. d->layer_hash[d->nlayers++] = h;
  4839. return (unsigned char)d->nlayers;
  4840. }
  4841. /* @supports: the engine cannot answer feature queries, so the block
  4842. * applies unless it is negated (`not (...)` usually guards a fallback for
  4843. * old browsers) or asks for selector() support. */
  4844. static int supports_matches(const char *s, unsigned int len)
  4845. {
  4846. while (len && is_space(*s)) { s++; len--; }
  4847. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4848. (len == 3 || is_space(s[3]) || s[3] == '('))
  4849. return 0;
  4850. if (contains_ci_len(s, len, "selector("))
  4851. return 0;
  4852. return 1;
  4853. }
  4854. /* Append one code point of generated content as Latin-1. Typography the
  4855. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4856. * and anything else outside Latin-1 are dropped. */
  4857. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4858. unsigned int cp)
  4859. {
  4860. const char *sub = 0;
  4861. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4862. sub = "-";
  4863. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4864. sub = "'";
  4865. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4866. sub = "\"";
  4867. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4868. sub = "\xB7";
  4869. else if (cp == 0x2026)
  4870. sub = "...";
  4871. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4872. sub = ">";
  4873. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4874. sub = "<";
  4875. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4876. cp == 0x2003 || cp == 0x202F)
  4877. sub = " ";
  4878. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4879. sub = "\xD7";
  4880. else if (cp == 0x2713 || cp == 0x2714)
  4881. sub = "v";
  4882. if (sub) {
  4883. while (*sub && *n + 1 < cap)
  4884. out[(*n)++] = *sub++;
  4885. return;
  4886. }
  4887. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4888. out[(*n)++] = (char)cp;
  4889. else if (cp == '\n' && *n + 1 < cap)
  4890. out[(*n)++] = ' ';
  4891. }
  4892. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4893. * content index + 1, or 0 for none/empty (counters and url() are not
  4894. * generated). */
  4895. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4896. {
  4897. char out[160];
  4898. unsigned int i = 0, n = 0, off;
  4899. while (i < len) {
  4900. char c = s[i];
  4901. if (c == '"' || c == '\'') {
  4902. char q = c;
  4903. i++;
  4904. while (i < len && s[i] != q) {
  4905. unsigned int cp;
  4906. if (s[i] == '\\' && i + 1 < len) {
  4907. unsigned int h = 0, k = 0;
  4908. i++;
  4909. while (k < 6 && i < len &&
  4910. ((s[i] >= '0' && s[i] <= '9') ||
  4911. ((s[i] | 32) >= 'a' &&
  4912. (s[i] | 32) <= 'f'))) {
  4913. char x = (char)(s[i] | 32);
  4914. h = h * 16 + (unsigned int)(x <= '9' ?
  4915. x - '0' : x - 'a' + 10);
  4916. i++;
  4917. k++;
  4918. }
  4919. if (k) {
  4920. if (i < len && s[i] == ' ')
  4921. i++;
  4922. cp = h;
  4923. } else {
  4924. cp = (unsigned char)s[i++];
  4925. }
  4926. } else if ((unsigned char)s[i] >= 0xC0) {
  4927. unsigned char b = (unsigned char)s[i++];
  4928. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4929. cp = b & (0x3F >> more);
  4930. while (more-- && i < len &&
  4931. ((unsigned char)s[i] & 0xC0) == 0x80)
  4932. cp = (cp << 6) | (s[i++] & 0x3F);
  4933. } else {
  4934. cp = (unsigned char)s[i++];
  4935. }
  4936. content_put(out, &n, sizeof(out), cp);
  4937. }
  4938. i++;
  4939. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4940. unsigned int st = i + 5, e = st;
  4941. while (e < len && s[e] != ')')
  4942. e++;
  4943. while (st < e && is_space(s[st]))
  4944. st++;
  4945. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4946. out[n++] = '\001';
  4947. while (st < e && !is_space(s[st]))
  4948. out[n++] = s[st++];
  4949. out[n++] = '\002';
  4950. }
  4951. i = e + 1;
  4952. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4953. ci_eq_n(s + i, "close-quot", 10))) {
  4954. content_put(out, &n, sizeof(out), '"');
  4955. i += 10;
  4956. } else if (c == '(') {
  4957. unsigned int depth = 0;
  4958. /* counter(), url(), ...: skip the arguments */
  4959. for (; i < len; i++) {
  4960. if (s[i] == '(')
  4961. depth++;
  4962. else if (s[i] == ')' && --depth == 0)
  4963. break;
  4964. }
  4965. i++;
  4966. } else {
  4967. i++;
  4968. }
  4969. }
  4970. if (!n) {
  4971. /* `content: ""` still creates the box: icon
  4972. * pseudo-elements are empty strings with a size and a
  4973. * background image. */
  4974. for (i = 0; i < len; i++)
  4975. if (s[i] == '"' || s[i] == '\'')
  4976. return BR_CONTENT_EMPTY;
  4977. return 0;
  4978. }
  4979. if (d->ncontent >= BR_MAX_CONTENT)
  4980. return 0;
  4981. off = store_raw(d, out, n, sizeof(out));
  4982. if (!off)
  4983. return 0;
  4984. d->content_off[d->ncontent++] = off;
  4985. return d->ncontent;
  4986. }
  4987. /* ---- calc() folding ---------------------------------------------------
  4988. *
  4989. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4990. * Frameworks build their scales from arithmetic on custom properties
  4991. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4992. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4993. * calc() in a declaration block is folded into that canonical form:
  4994. * a linear combination of %, px (rem/em at the root font size, vw against
  4995. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4996. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4997. * the calc() text unchanged. Values are hundredths. */
  4998. static unsigned int viewport_w; /* defined with the media code */
  4999. struct calc_val {
  5000. long px, pct, num;
  5001. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  5002. };
  5003. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  5004. struct calc_val *v, int depth);
  5005. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  5006. {
  5007. while (*i < len && is_space(s[*i]))
  5008. (*i)++;
  5009. }
  5010. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  5011. struct calc_val *v, int depth)
  5012. {
  5013. long whole = 0, frac = 0, scale = 1, n;
  5014. int sign = 1, digits = 0;
  5015. calc_space(s, len, i);
  5016. if (*i < len && (s[*i] == '(' ||
  5017. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  5018. *i += s[*i] == '(' ? 1 : 5;
  5019. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  5020. return 0;
  5021. calc_space(s, len, i);
  5022. if (*i >= len || s[*i] != ')')
  5023. return 0;
  5024. (*i)++;
  5025. return 1;
  5026. }
  5027. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  5028. sign = s[*i] == '-' ? -1 : 1;
  5029. (*i)++;
  5030. }
  5031. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  5032. whole = whole * 10 + (s[*i] - '0');
  5033. digits = 1;
  5034. (*i)++;
  5035. if (whole > 1000000)
  5036. return 0;
  5037. }
  5038. if (*i < len && s[*i] == '.') {
  5039. (*i)++;
  5040. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  5041. if (scale < 10000) {
  5042. frac = frac * 10 + (s[*i] - '0');
  5043. scale *= 10;
  5044. }
  5045. digits = 1;
  5046. (*i)++;
  5047. }
  5048. }
  5049. if (!digits)
  5050. return 0;
  5051. n = sign * (whole * 100 + frac * 100 / scale);
  5052. v->px = v->pct = v->num = 0;
  5053. if (*i < len && s[*i] == '%') {
  5054. (*i)++;
  5055. v->pct = n;
  5056. v->kind = 2;
  5057. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  5058. *i += 3;
  5059. v->px = n * root_font_px;
  5060. v->kind = 1;
  5061. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  5062. *i += 2;
  5063. v->px = n * root_font_px;
  5064. v->kind = 1;
  5065. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  5066. *i += 2;
  5067. v->px = n;
  5068. v->kind = 1;
  5069. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  5070. *i += 2;
  5071. v->px = n * (long)viewport_w / 100;
  5072. v->kind = 1;
  5073. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  5074. return 0; /* vh, deg, fr, ch, ... */
  5075. } else {
  5076. v->num = n;
  5077. v->kind = 4;
  5078. }
  5079. return 1;
  5080. }
  5081. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  5082. struct calc_val *v, int depth)
  5083. {
  5084. if (!calc_unary(s, len, i, v, depth))
  5085. return 0;
  5086. for (;;) {
  5087. struct calc_val r;
  5088. char op;
  5089. calc_space(s, len, i);
  5090. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  5091. return 1;
  5092. op = s[(*i)++];
  5093. if (!calc_unary(s, len, i, &r, depth))
  5094. return 0;
  5095. if (op == '/') {
  5096. if (r.kind != 4 || !r.num)
  5097. return 0;
  5098. v->px = v->px * 100 / r.num;
  5099. v->pct = v->pct * 100 / r.num;
  5100. v->num = v->num * 100 / r.num;
  5101. } else if (r.kind == 4) {
  5102. v->px = v->px * r.num / 100;
  5103. v->pct = v->pct * r.num / 100;
  5104. v->num = v->num * r.num / 100;
  5105. } else if (v->kind == 4) {
  5106. long k = v->num;
  5107. *v = r;
  5108. v->px = v->px * k / 100;
  5109. v->pct = v->pct * k / 100;
  5110. } else {
  5111. return 0; /* length * length */
  5112. }
  5113. }
  5114. }
  5115. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  5116. struct calc_val *v, int depth)
  5117. {
  5118. if (!calc_product(s, len, i, v, depth))
  5119. return 0;
  5120. for (;;) {
  5121. struct calc_val r;
  5122. int neg;
  5123. calc_space(s, len, i);
  5124. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  5125. return 1;
  5126. neg = s[(*i)++] == '-';
  5127. if (!calc_product(s, len, i, &r, depth))
  5128. return 0;
  5129. if ((v->kind == 4) != (r.kind == 4))
  5130. return 0; /* number + length */
  5131. v->px += neg ? -r.px : r.px;
  5132. v->pct += neg ? -r.pct : r.pct;
  5133. v->num += neg ? -r.num : r.num;
  5134. v->kind |= r.kind;
  5135. }
  5136. }
  5137. /* Hundredths -> "12", "-0.5", "33.33". */
  5138. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  5139. {
  5140. char tmp[24];
  5141. unsigned int n = 0, k = 0;
  5142. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  5143. unsigned long w = a / 100, f = a % 100;
  5144. if (v < 0)
  5145. tmp[n++] = '-';
  5146. {
  5147. char rev[12];
  5148. unsigned int r = 0;
  5149. do {
  5150. rev[r++] = (char)('0' + w % 10);
  5151. w /= 10;
  5152. } while (w && r < sizeof(rev));
  5153. while (r)
  5154. tmp[n++] = rev[--r];
  5155. }
  5156. if (f) {
  5157. tmp[n++] = '.';
  5158. tmp[n++] = (char)('0' + f / 10);
  5159. if (f % 10)
  5160. tmp[n++] = (char)('0' + f % 10);
  5161. }
  5162. for (k = 0; k < n && k < cap; k++)
  5163. o[k] = tmp[k];
  5164. return k;
  5165. }
  5166. /* Copy s to out with every foldable calc() replaced; returns the length,
  5167. * or 0 when nothing was folded (the caller keeps the original). */
  5168. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  5169. unsigned int cap)
  5170. {
  5171. unsigned int i = 0, n = 0;
  5172. int changed = 0;
  5173. while (i < len && n + 40 < cap) {
  5174. struct calc_val v;
  5175. unsigned int j = i + 5;
  5176. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  5177. (i == 0 || !is_ident(s[i - 1])))) {
  5178. out[n++] = s[i++];
  5179. continue;
  5180. }
  5181. if (!calc_sum(s, len, &j, &v, 0) ||
  5182. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  5183. out[n++] = s[i++];
  5184. continue;
  5185. }
  5186. j++;
  5187. /* Minified CSS glues tokens to the function's parentheses
  5188. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  5189. * a folded `10px16px` would be one token. */
  5190. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  5191. out[n - 1] != '(' && out[n - 1] != ',')
  5192. out[n++] = ' ';
  5193. if (v.kind == 4) {
  5194. n += calc_fmt(out + n, cap - n, v.num);
  5195. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  5196. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  5197. -50 : 50)) /
  5198. 100 * 100);
  5199. out[n++] = 'p';
  5200. out[n++] = 'x';
  5201. } else if (!v.px) {
  5202. n += calc_fmt(out + n, cap - n, v.pct);
  5203. out[n++] = '%';
  5204. } else {
  5205. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  5206. memcpy(out + n, "calc(", 5);
  5207. n += 5;
  5208. n += calc_fmt(out + n, cap - n, v.pct);
  5209. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  5210. n += 4;
  5211. n += calc_fmt(out + n, cap - n, px * 100);
  5212. memcpy(out + n, "px)", 3);
  5213. n += 3;
  5214. }
  5215. i = j;
  5216. changed = 1;
  5217. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  5218. s[i] != ',' && s[i] != '!' && s[i] != '}')
  5219. out[n++] = ' ';
  5220. }
  5221. if (!changed || i < len)
  5222. return 0;
  5223. out[n] = '\0';
  5224. return n;
  5225. }
  5226. static int css_has_calc(const char *s, unsigned int len)
  5227. {
  5228. unsigned int i;
  5229. for (i = 0; i + 5 <= len; i++)
  5230. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  5231. return 1;
  5232. return 0;
  5233. }
  5234. static void parse_decls(struct br_doc *d, const char *s,
  5235. unsigned int len, struct br_decl *out,
  5236. unsigned int *nout)
  5237. {
  5238. static char expanded[4096];
  5239. static char folded[4096];
  5240. if (css_has_var(s, len)) {
  5241. unsigned int elen;
  5242. if (!css_env_active)
  5243. css_local_collect(s, len);
  5244. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  5245. 0);
  5246. if (!css_env_active)
  5247. css_nlocal = 0;
  5248. if (elen) {
  5249. s = expanded;
  5250. len = elen;
  5251. }
  5252. }
  5253. if (css_has_calc(s, len)) {
  5254. unsigned int flen = css_fold_calc(s, len, folded,
  5255. sizeof(folded));
  5256. if (flen) {
  5257. s = folded;
  5258. len = flen;
  5259. }
  5260. }
  5261. unsigned int i = 0;
  5262. *nout = 0;
  5263. while (i < len && *nout < BR_DECLS) {
  5264. unsigned int pstart, pend, vstart, vend, k;
  5265. char prop[24];
  5266. int important = 0;
  5267. while (i < len && (is_space(s[i]) || s[i] == ';'))
  5268. i++;
  5269. if (i >= len)
  5270. break;
  5271. /* A nested at-rule (@media/@supports) inside the declaration
  5272. * block carries its own selector context; skip it rather than
  5273. * reading its prelude as a declaration. */
  5274. if (s[i] == '@') {
  5275. while (i < len && s[i] != '{' && s[i] != ';')
  5276. i++;
  5277. if (i < len && s[i] == '{')
  5278. i = css_skip_nested(s, len, i + 1);
  5279. else if (i < len)
  5280. i++;
  5281. continue;
  5282. }
  5283. pstart = i;
  5284. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  5285. i++;
  5286. if (i < len && s[i] == '{') {
  5287. /* Nested style rule: skip its block. */
  5288. i = css_skip_nested(s, len, i + 1);
  5289. continue;
  5290. }
  5291. pend = i;
  5292. if (i >= len || s[i] != ':')
  5293. continue;
  5294. i++;
  5295. vstart = i;
  5296. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  5297. * does not end the value. */
  5298. i = css_value_end(s, len, i);
  5299. vend = i;
  5300. while (pstart < pend && is_space(s[pstart]))
  5301. pstart++;
  5302. while (pend > pstart && is_space(s[pend - 1]))
  5303. pend--;
  5304. while (vstart < vend && is_space(s[vstart]))
  5305. vstart++;
  5306. while (vend > vstart && is_space(s[vend - 1]))
  5307. vend--;
  5308. if (vend - vstart >= 10 &&
  5309. ci_eq_n(s + vend - 10, "!important", 10)) {
  5310. important = 1;
  5311. vend -= 10;
  5312. while (vend > vstart && is_space(s[vend - 1]))
  5313. vend--;
  5314. }
  5315. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  5316. char c = s[pstart + k];
  5317. if (c >= 'A' && c <= 'Z')
  5318. c = (char)(c - 'A' + 'a');
  5319. prop[k] = c;
  5320. }
  5321. prop[k] = '\0';
  5322. if (str_eq(prop, "color")) {
  5323. unsigned short c;
  5324. if (parse_color(s + vstart, vend - vstart, &c))
  5325. decl_add(out, nout, PROP_COLOR, c, important);
  5326. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  5327. /* SVG paint only: an HTML element's fill never paints
  5328. * its background (tagesschau sets fill on buttons). */
  5329. int is_fill = prop[0] == 'f';
  5330. unsigned short c;
  5331. int mode = paint_mode(s + vstart, vend - vstart, &c);
  5332. if (mode) {
  5333. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  5334. PROP_STROKE_MODE, mode, important);
  5335. if (mode == BR_PAINT_COLOR)
  5336. decl_add(out, nout, is_fill ? PROP_FILL :
  5337. PROP_STROKE, (short)c,
  5338. important);
  5339. }
  5340. } else if (str_eq(prop, "stroke-width")) {
  5341. int sw = svg_len16(s + vstart, vend - vstart);
  5342. if (sw >= 0)
  5343. decl_add(out, nout, PROP_STROKE_W, sw,
  5344. important);
  5345. } else if (str_eq(prop, "fill-rule")) {
  5346. decl_add(out, nout, PROP_FILL_RULE,
  5347. contains_ci_len(s + vstart, vend - vstart,
  5348. "evenodd") ? 1 : 0,
  5349. important);
  5350. } else if (str_eq(prop, "text-anchor")) {
  5351. int a = BR_ALIGN_LEFT;
  5352. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  5353. a = BR_ALIGN_CENTER;
  5354. else if (contains_ci_len(s + vstart, vend - vstart,
  5355. "end"))
  5356. a = BR_ALIGN_RIGHT;
  5357. decl_add(out, nout, PROP_ALIGN, a, important);
  5358. } else if (str_eq(prop, "background-image")) {
  5359. add_bg_image_decl(out, nout, s + vstart,
  5360. vend - vstart, important);
  5361. } else if (str_eq(prop, "background-repeat")) {
  5362. int r = 0;
  5363. if (contains_ci_len(s + vstart, vend - vstart,
  5364. "no-repeat"))
  5365. r = 1;
  5366. else if (contains_ci_len(s + vstart, vend - vstart,
  5367. "repeat-x"))
  5368. r = 2;
  5369. else if (contains_ci_len(s + vstart, vend - vstart,
  5370. "repeat-y"))
  5371. r = 3;
  5372. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  5373. } else if (str_eq(prop, "background-position")) {
  5374. add_bg_position(out, nout, s + vstart, vend - vstart,
  5375. important);
  5376. } else if (str_eq(prop, "background-size")) {
  5377. add_bg_size(out, nout, s + vstart, vend - vstart,
  5378. important);
  5379. } else if (str_eq(prop, "background-color") ||
  5380. str_eq(prop, "background")) {
  5381. unsigned short c;
  5382. int is_bg = str_eq(prop, "background");
  5383. static char bgv[1024];
  5384. const char *vs = s + vstart;
  5385. unsigned int vl = vend - vstart;
  5386. if (is_bg) {
  5387. add_bg_image_decl(out, nout, s + vstart,
  5388. vend - vstart, important);
  5389. /* Keywords and colours are read with url()
  5390. * contents blanked: a data: SVG carries its
  5391. * own fill="rgba(...)". */
  5392. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  5393. vs = bgv;
  5394. add_bg_shorthand_extra(out, nout,
  5395. vs, vl,
  5396. important);
  5397. if (contains_ci_len(vs, vl,
  5398. "no-repeat"))
  5399. decl_add(out, nout, PROP_BG_REPEAT, 1,
  5400. important);
  5401. else if (contains_ci_len(vs, vl,
  5402. "repeat-x"))
  5403. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5404. important);
  5405. else if (contains_ci_len(vs, vl,
  5406. "repeat-y"))
  5407. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5408. important);
  5409. }
  5410. /* `transparent`/`none` are real values: they must
  5411. * beat a lower-specificity colour (and tell form
  5412. * controls the author styled them). */
  5413. if (color_is_transparent(vs, vl) ||
  5414. (vl == 4 &&
  5415. ci_eq_n(vs, "none", 4)))
  5416. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5417. else if (parse_color(vs, vl, &c))
  5418. decl_add(out, nout, PROP_BG, c, important);
  5419. else if (is_bg &&
  5420. find_color_last(vs, vl, &c))
  5421. decl_add(out, nout, PROP_BG, c, important);
  5422. } else if (str_eq(prop, "font-size")) {
  5423. unsigned short raw;
  5424. unsigned char unit;
  5425. if (parse_font_size(s + vstart, vend - vstart,
  5426. &raw, &unit)) {
  5427. decl_add(out, nout, PROP_FONT_PX, raw,
  5428. important);
  5429. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5430. important);
  5431. }
  5432. } else if (str_eq(prop, "font-weight")) {
  5433. int bold = 0;
  5434. unsigned int j;
  5435. if (vend - vstart >= 4 &&
  5436. (ci_eq_n(s + vstart, "bold", 4) ||
  5437. (vend - vstart >= 6 &&
  5438. ci_eq_n(s + vstart, "bolder", 6))))
  5439. bold = 1;
  5440. else {
  5441. int w = 0;
  5442. for (j = vstart; j < vend; j++)
  5443. if (s[j] >= '0' && s[j] <= '9')
  5444. w = w * 10 + (s[j] - '0');
  5445. /* CSS font matching: with only a 400 and a
  5446. * 700 face, 500 (Infima's "semibold" menus)
  5447. * resolves to the regular face; 600 and up
  5448. * to the bold one. */
  5449. if (w >= 600)
  5450. bold = 1;
  5451. }
  5452. decl_add(out, nout, PROP_BOLD, bold, important);
  5453. } else if (str_eq(prop, "font-style")) {
  5454. int italic = (vend - vstart >= 6 &&
  5455. ci_eq_n(s + vstart, "italic", 6));
  5456. decl_add(out, nout, PROP_ITALIC, italic, important);
  5457. } else if (str_eq(prop, "font-family")) {
  5458. unsigned int j;
  5459. int mono = 0;
  5460. int face = font_family_id(s + vstart, vend - vstart);
  5461. for (j = vstart; j + 4 <= vend; j++)
  5462. if (ci_eq_n(s + j, "mono", 4))
  5463. mono = 1;
  5464. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5465. decl_add(out, nout, PROP_MONO, mono, important);
  5466. } else if (str_eq(prop, "font")) {
  5467. add_font_shorthand(out, nout, s + vstart,
  5468. vend - vstart, important);
  5469. } else if (str_eq(prop, "text-align")) {
  5470. int a = BR_ALIGN_LEFT;
  5471. if (vend - vstart >= 6 &&
  5472. ci_eq_n(s + vstart, "center", 6))
  5473. a = BR_ALIGN_CENTER;
  5474. else if (vend - vstart >= 5 &&
  5475. ci_eq_n(s + vstart, "right", 5))
  5476. a = BR_ALIGN_RIGHT;
  5477. decl_add(out, nout, PROP_ALIGN, a, important);
  5478. } else if (str_eq(prop, "text-decoration")) {
  5479. int u = 0, stv = 0, ov = 0;
  5480. if (contains_ci_len(s + vstart, vend - vstart,
  5481. "underline"))
  5482. u = 1;
  5483. if (contains_ci_len(s + vstart, vend - vstart,
  5484. "line-through"))
  5485. stv = 1;
  5486. if (contains_ci_len(s + vstart, vend - vstart,
  5487. "overline"))
  5488. ov = 1;
  5489. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5490. decl_add(out, nout, PROP_STRIKE, stv, important);
  5491. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5492. } else if (str_eq(prop, "pointer-events")) {
  5493. decl_add(out, nout, PROP_PE_NONE,
  5494. vend - vstart >= 4 &&
  5495. ci_eq_n(s + vstart, "none", 4), important);
  5496. } else if (str_eq(prop, "cursor")) {
  5497. int c = parse_cursor(s + vstart, vend - vstart);
  5498. if (c >= 0)
  5499. decl_add(out, nout, PROP_CURSOR, c, important);
  5500. } else if (str_eq(prop, "text-transform")) {
  5501. int u = (vend - vstart >= 9 &&
  5502. ci_eq_n(s + vstart, "uppercase", 9));
  5503. decl_add(out, nout, PROP_UPPER, u, important);
  5504. } else if (str_eq(prop, "white-space")) {
  5505. int nw = contains_ci_len(s + vstart, vend - vstart,
  5506. "nowrap") ||
  5507. contains_ci_len(s + vstart, vend - vstart,
  5508. "pre");
  5509. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5510. } else if (str_eq(prop, "display")) {
  5511. decl_add(out, nout, PROP_DISPLAY,
  5512. parse_display(s + vstart, vend - vstart),
  5513. important);
  5514. } else if (str_eq(prop, "visibility")) {
  5515. int h = contains_ci_len(s + vstart, vend - vstart,
  5516. "hidden") ||
  5517. contains_ci_len(s + vstart, vend - vstart,
  5518. "collapse");
  5519. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5520. } else if (str_eq(prop, "opacity")) {
  5521. /* Stored as transparency (255 - alpha), so a missing
  5522. * declaration means opaque. `0`, `.5`, `0.9`, `50%`;
  5523. * anything unparsable (inherit, var leftovers) is 1. */
  5524. unsigned int q = vstart;
  5525. signed short h100;
  5526. int a = 255;
  5527. if (parse_fixed_100(s, vend, &q, &h100)) {
  5528. int pct = q < vend && s[q] == '%';
  5529. long v = pct ? h100 / 100 : h100;
  5530. if (v < 0)
  5531. v = 0;
  5532. if (v > 100)
  5533. v = 100;
  5534. a = (int)(v * 255 / 100);
  5535. }
  5536. decl_add(out, nout, PROP_OPACITY_HIDDEN, 255 - a,
  5537. important);
  5538. } else if (str_eq(prop, "overflow") ||
  5539. str_eq(prop, "overflow-x") ||
  5540. str_eq(prop, "overflow-y")) {
  5541. int h = contains_ci_len(s + vstart, vend - vstart,
  5542. "hidden") ||
  5543. contains_ci_len(s + vstart, vend - vstart,
  5544. "clip");
  5545. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5546. } else if (str_eq(prop, "float")) {
  5547. int f = 0;
  5548. if (vend - vstart >= 4 &&
  5549. ci_eq_n(s + vstart, "left", 4))
  5550. f = 1;
  5551. else if (vend - vstart >= 5 &&
  5552. ci_eq_n(s + vstart, "right", 5))
  5553. f = 2;
  5554. decl_add(out, nout, PROP_FLOAT, f, important);
  5555. } else if (str_eq(prop, "clear")) {
  5556. int c = 0;
  5557. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5558. c = 3;
  5559. else if (contains_ci_len(s + vstart, vend - vstart,
  5560. "left"))
  5561. c = 1;
  5562. else if (contains_ci_len(s + vstart, vend - vstart,
  5563. "right"))
  5564. c = 2;
  5565. decl_add(out, nout, PROP_CLEAR, c, important);
  5566. } else if (str_eq(prop, "flex-grow") ||
  5567. str_eq(prop, "flex")) {
  5568. unsigned int q = vstart;
  5569. int g = 0;
  5570. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5571. q++;
  5572. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5573. ci_eq_n(s + q, "auto", 4))
  5574. g = 1; /* flex: auto = 1 1 auto */
  5575. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5576. g = g * 10 + (s[q] - '0');
  5577. q++;
  5578. }
  5579. if (g > 100)
  5580. g = 100;
  5581. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5582. if (str_eq(prop, "flex")) {
  5583. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5584. * basis is the item's main size, which our flex
  5585. * row reads from width (Infima's grid:
  5586. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5587. * of the grow number, then any unitless shrink. */
  5588. unsigned int t;
  5589. while (q < vend && (s[q] == '.' ||
  5590. (s[q] >= '0' && s[q] <= '9')))
  5591. q++;
  5592. for (t = 0; t < 2 && q < vend; t++) {
  5593. unsigned int st, e, k2;
  5594. unsigned short bv;
  5595. unsigned char bk;
  5596. while (q < vend && is_space(s[q]))
  5597. q++;
  5598. st = q;
  5599. while (q < vend && !is_space(s[q]))
  5600. q++;
  5601. e = q;
  5602. for (k2 = st; k2 < e; k2++)
  5603. if (!(s[k2] == '.' ||
  5604. (s[k2] >= '0' &&
  5605. s[k2] <= '9')))
  5606. break;
  5607. if (k2 == e)
  5608. continue; /* shrink */
  5609. if (parse_size(s + st, e - st, &bv,
  5610. &bk) && bk && bv) {
  5611. decl_add(out, nout, PROP_WIDTH,
  5612. bv, important);
  5613. decl_add(out, nout,
  5614. PROP_WIDTH_KIND, bk,
  5615. important);
  5616. }
  5617. break;
  5618. }
  5619. }
  5620. } else if (str_eq(prop, "flex-basis")) {
  5621. unsigned short bv;
  5622. unsigned char bk;
  5623. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5624. bk && bv) {
  5625. decl_add(out, nout, PROP_WIDTH, bv, important);
  5626. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5627. important);
  5628. }
  5629. } else if (str_eq(prop, "flex-direction")) {
  5630. decl_add(out, nout, PROP_FLEX_COL,
  5631. contains_ci_len(s + vstart, vend - vstart,
  5632. "column"), important);
  5633. } else if (str_eq(prop, "flex-wrap")) {
  5634. int wrap = 0;
  5635. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5636. wrap = 2;
  5637. else if (contains_ci_len(s + vstart, vend - vstart,
  5638. "wrap"))
  5639. wrap = 1;
  5640. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5641. } else if (str_eq(prop, "justify-content")) {
  5642. int j = 0;
  5643. if (contains_ci_len(s + vstart, vend - vstart,
  5644. "space-between"))
  5645. j = 3;
  5646. else if (contains_ci_len(s + vstart, vend - vstart,
  5647. "space-around"))
  5648. j = 4;
  5649. else if (contains_ci_len(s + vstart, vend - vstart,
  5650. "space-evenly"))
  5651. j = 5;
  5652. else if (contains_ci_len(s + vstart, vend - vstart,
  5653. "center"))
  5654. j = 1;
  5655. else if (contains_ci_len(s + vstart, vend - vstart,
  5656. "flex-end") ||
  5657. contains_ci_len(s + vstart, vend - vstart,
  5658. "end"))
  5659. j = 2;
  5660. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5661. } else if (str_eq(prop, "align-self")) {
  5662. /* 0 auto (the container's align-items), else
  5663. * align-items + 1. */
  5664. int a = 1;
  5665. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5666. a = 0;
  5667. else if (contains_ci_len(s + vstart, vend - vstart,
  5668. "center"))
  5669. a = 2;
  5670. else if (contains_ci_len(s + vstart, vend - vstart,
  5671. "flex-end") ||
  5672. contains_ci_len(s + vstart, vend - vstart,
  5673. "end"))
  5674. a = 3;
  5675. else if (contains_ci_len(s + vstart, vend - vstart,
  5676. "start") ||
  5677. contains_ci_len(s + vstart, vend - vstart,
  5678. "baseline"))
  5679. a = 4;
  5680. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5681. } else if (str_eq(prop, "align-items")) {
  5682. int a = 0;
  5683. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5684. a = 1;
  5685. else if (contains_ci_len(s + vstart, vend - vstart,
  5686. "flex-end") ||
  5687. contains_ci_len(s + vstart, vend - vstart,
  5688. "end"))
  5689. a = 2;
  5690. else if (contains_ci_len(s + vstart, vend - vstart,
  5691. "start") ||
  5692. contains_ci_len(s + vstart, vend - vstart,
  5693. "baseline"))
  5694. a = 3; /* not stretched */
  5695. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5696. } else if (str_eq(prop, "position")) {
  5697. int p = 0;
  5698. if (contains_ci_len(s + vstart, vend - vstart,
  5699. "absolute"))
  5700. p = 1;
  5701. else if (contains_ci_len(s + vstart, vend - vstart,
  5702. "fixed"))
  5703. p = 2;
  5704. else if (contains_ci_len(s + vstart, vend - vstart,
  5705. "relative"))
  5706. p = 3;
  5707. else if (contains_ci_len(s + vstart, vend - vstart,
  5708. "sticky"))
  5709. p = 4;
  5710. decl_add(out, nout, PROP_POS, p, important);
  5711. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5712. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5713. int which = str_eq(prop, "left") ? 0 :
  5714. str_eq(prop, "top") ? 1 :
  5715. str_eq(prop, "right") ? 2 : 3;
  5716. unsigned int pos = 0;
  5717. if (!(vend - vstart >= 4 &&
  5718. ci_eq_n(s + vstart, "auto", 4))) {
  5719. int v = parse_len(s + vstart, vend - vstart, &pos);
  5720. if (v > 32000)
  5721. v = 32000;
  5722. else if (v < -32000)
  5723. v = -32000;
  5724. decl_add(out, nout,
  5725. (unsigned char)(which == 0 ? PROP_LEFT :
  5726. which == 1 ? PROP_TOP :
  5727. which == 2 ? PROP_RIGHT :
  5728. PROP_BOTTOM),
  5729. v, important);
  5730. decl_add(out, nout,
  5731. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5732. which == 1 ? PROP_TOP_SET :
  5733. which == 2 ? PROP_RIGHT_SET :
  5734. PROP_BOTTOM_SET),
  5735. 1, important);
  5736. }
  5737. } else if (str_eq(prop, "z-index")) {
  5738. unsigned int q = vstart;
  5739. int sign = 1;
  5740. int v = 0;
  5741. int any = 0;
  5742. if (q < vend && s[q] == '-') {
  5743. sign = -1;
  5744. q++;
  5745. }
  5746. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5747. v = v * 10 + (s[q] - '0');
  5748. any = 1;
  5749. q++;
  5750. }
  5751. if (any) {
  5752. v *= sign;
  5753. if (v > 32000)
  5754. v = 32000;
  5755. else if (v < -32000)
  5756. v = -32000;
  5757. decl_add(out, nout, PROP_ZINDEX, v, important);
  5758. decl_add(out, nout, PROP_ZSET, 1, important);
  5759. } else if (vend - vstart >= 4 &&
  5760. ci_eq_n(s + vstart, "auto", 4)) {
  5761. decl_add(out, nout, PROP_ZSET, 0, important);
  5762. }
  5763. } else if (str_eq(prop, "transform")) {
  5764. const char *v = s + vstart;
  5765. unsigned int vl = vend - vstart;
  5766. int tx = 0, ty = 0, txp = 0, typ = 0;
  5767. int is_x = contains_ci_len(v, vl, "translatex");
  5768. int is_y = contains_ci_len(v, vl, "translatey");
  5769. unsigned int off = 0, arg;
  5770. if (contains_ci_len(v, vl, "translate")) {
  5771. while (off < vl && v[off] != '(')
  5772. off++;
  5773. if (off < vl)
  5774. off++;
  5775. if (!is_y)
  5776. tx = translate_arg(v, vl, &off, &txp);
  5777. if (!is_x) {
  5778. while (off < vl &&
  5779. (is_space(v[off]) || v[off] == ','))
  5780. off++;
  5781. ty = translate_arg(v, vl, &off, &typ);
  5782. }
  5783. decl_add(out, nout, PROP_TX_PCT, txp, important);
  5784. decl_add(out, nout, PROP_TY_PCT, typ, important);
  5785. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5786. important);
  5787. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5788. important);
  5789. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5790. important);
  5791. }
  5792. if (find_fn(v, vl, "scale", &arg)) {
  5793. unsigned int q = arg;
  5794. signed short sx = 100, sy = 100;
  5795. if (parse_fixed_100(v, vl, &q, &sx)) {
  5796. unsigned int r = q;
  5797. while (r < vl &&
  5798. (is_space(v[r]) || v[r] == ','))
  5799. r++;
  5800. if (!parse_fixed_100(v, vl, &r, &sy))
  5801. sy = sx;
  5802. decl_add(out, nout, PROP_SCALE_X, sx,
  5803. important);
  5804. decl_add(out, nout, PROP_SCALE_Y, sy,
  5805. important);
  5806. decl_add(out, nout,
  5807. PROP_TRANSFORM_PAINT, 1,
  5808. important);
  5809. decl_add(out, nout,
  5810. PROP_TRANSFORM_SET, 1,
  5811. important);
  5812. }
  5813. }
  5814. if (find_fn(v, vl, "rotate", &arg)) {
  5815. unsigned int q = arg;
  5816. signed short deg;
  5817. if (parse_fixed_100(v, vl, &q, &deg)) {
  5818. decl_add(out, nout, PROP_ROTATE, deg,
  5819. important);
  5820. decl_add(out, nout,
  5821. PROP_TRANSFORM_PAINT, 1,
  5822. important);
  5823. decl_add(out, nout,
  5824. PROP_TRANSFORM_SET, 1,
  5825. important);
  5826. }
  5827. }
  5828. } else if (str_eq(prop, "transition") ||
  5829. str_eq(prop, "transition-property") ||
  5830. str_eq(prop, "transition-duration") ||
  5831. str_eq(prop, "transition-timing-function")) {
  5832. add_transition(out, nout, prop, s + vstart,
  5833. vend - vstart, important);
  5834. } else if (str_eq(prop, "transform-origin")) {
  5835. add_transform_origin(out, nout, s + vstart,
  5836. vend - vstart, important);
  5837. } else if (str_eq(prop, "grid-template-columns") ||
  5838. str_eq(prop, "grid-template")) {
  5839. const char *v = s + vstart;
  5840. unsigned int vl = vend - vstart;
  5841. unsigned short val[2];
  5842. unsigned char kind[2];
  5843. int n, q;
  5844. if (str_eq(prop, "grid-template")) {
  5845. for (q = 0; q < (int)vl; q++)
  5846. if (v[q] == '/') {
  5847. q++;
  5848. while (q < (int)vl && is_space(v[q]))
  5849. q++;
  5850. v += q;
  5851. vl -= (unsigned int)q;
  5852. break;
  5853. }
  5854. }
  5855. if (grid_repeat_count(v, vl) >= 1) {
  5856. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5857. * Tailwind's grid-cols-N. N equal columns
  5858. * (GRID_FIXED + N); one column is plain block
  5859. * flow, which also resets a smaller breakpoint's
  5860. * count. */
  5861. int cnt = grid_repeat_count(v, vl);
  5862. decl_add(out, nout, PROP_GRID_AUTO,
  5863. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5864. important);
  5865. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5866. important);
  5867. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5868. contains_ci_len(v, vl, "auto-fill")) {
  5869. unsigned int mp = 0;
  5870. int found = 0;
  5871. unsigned short mw = 200;
  5872. unsigned char mk = 1;
  5873. for (q = 0; q + 7 <= (int)vl; q++)
  5874. if (ci_eq_n(v + q, "minmax(", 7)) {
  5875. mp = (unsigned int)q + 7;
  5876. found = 1;
  5877. break;
  5878. }
  5879. if (found)
  5880. parse_grid_track(v, vl, &mp, &mw, &mk);
  5881. if (mk != 1 || mw < 80)
  5882. mw = 200;
  5883. if (mw > 480)
  5884. mw = 480;
  5885. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5886. decl_add(out, nout, PROP_WIDTH, mw, important);
  5887. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5888. } else {
  5889. n = parse_grid_cols(v, vl, val, kind);
  5890. if (n > 0) {
  5891. decl_add(out, nout, PROP_WIDTH, val[0],
  5892. important);
  5893. decl_add(out, nout, PROP_WIDTH_KIND,
  5894. kind[0], important);
  5895. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5896. important);
  5897. }
  5898. if (n > 1) {
  5899. decl_add(out, nout, PROP_MAXW, val[1],
  5900. important);
  5901. decl_add(out, nout, PROP_MAXW_KIND,
  5902. kind[1], important);
  5903. }
  5904. }
  5905. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5906. /* From the value, not the block start: gap was
  5907. * ignored on nearly every site. */
  5908. unsigned int gp = vstart;
  5909. unsigned short gv = 0;
  5910. unsigned char gk = 1;
  5911. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5912. if (gv > 120)
  5913. gv = 120;
  5914. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5915. }
  5916. } else if (str_eq(prop, "grid-area")) {
  5917. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5918. if (slot)
  5919. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5920. } else if (str_eq(prop, "width") ||
  5921. str_eq(prop, "max-width") ||
  5922. str_eq(prop, "min-width") ||
  5923. str_eq(prop, "height") ||
  5924. str_eq(prop, "max-height") ||
  5925. str_eq(prop, "min-height")) {
  5926. unsigned short v;
  5927. unsigned char kind;
  5928. if (vend - vstart >= 5 &&
  5929. ci_eq_n(s + vstart, "calc(", 5)) {
  5930. int pct, px;
  5931. if (parse_calc(s + vstart + 5,
  5932. vend - vstart - 5, &pct, &px)) {
  5933. if (str_eq(prop, "width")) {
  5934. decl_add(out, nout, PROP_WIDTH,
  5935. 0, important);
  5936. decl_add(out, nout,
  5937. PROP_WIDTH_KIND, 3,
  5938. important);
  5939. decl_add(out, nout,
  5940. PROP_WIDTH_CALC_PCT,
  5941. pct, important);
  5942. decl_add(out, nout,
  5943. PROP_WIDTH_CALC_PX,
  5944. px, important);
  5945. } else if (str_eq(prop, "max-width")) {
  5946. decl_add(out, nout, PROP_MAXW,
  5947. 0, important);
  5948. decl_add(out, nout,
  5949. PROP_MAXW_KIND, 3,
  5950. important);
  5951. decl_add(out, nout,
  5952. PROP_MAXW_CALC_PCT,
  5953. pct, important);
  5954. decl_add(out, nout,
  5955. PROP_MAXW_CALC_PX,
  5956. px, important);
  5957. } else if (str_eq(prop, "min-width")) {
  5958. decl_add(out, nout, PROP_MINW,
  5959. 0, important);
  5960. decl_add(out, nout,
  5961. PROP_MINW_KIND, 3,
  5962. important);
  5963. decl_add(out, nout,
  5964. PROP_MINW_CALC_PCT,
  5965. pct, important);
  5966. decl_add(out, nout,
  5967. PROP_MINW_CALC_PX,
  5968. px, important);
  5969. } else if (str_eq(prop, "height")) {
  5970. decl_add(out, nout, PROP_HEIGHT,
  5971. 0, important);
  5972. decl_add(out, nout,
  5973. PROP_HEIGHT_KIND, 3,
  5974. important);
  5975. decl_add(out, nout,
  5976. PROP_HEIGHT_CALC_PCT,
  5977. pct, important);
  5978. decl_add(out, nout,
  5979. PROP_HEIGHT_CALC_PX,
  5980. px, important);
  5981. } else if (str_eq(prop, "max-height")) {
  5982. decl_add(out, nout, PROP_MAXH,
  5983. 0, important);
  5984. decl_add(out, nout,
  5985. PROP_MAXH_KIND, 3,
  5986. important);
  5987. decl_add(out, nout,
  5988. PROP_MAXH_CALC_PCT,
  5989. pct, important);
  5990. decl_add(out, nout,
  5991. PROP_MAXH_CALC_PX,
  5992. px, important);
  5993. } else {
  5994. decl_add(out, nout, PROP_MINH,
  5995. 0, important);
  5996. decl_add(out, nout,
  5997. PROP_MINH_KIND, 3,
  5998. important);
  5999. decl_add(out, nout,
  6000. PROP_MINH_CALC_PCT,
  6001. pct, important);
  6002. decl_add(out, nout,
  6003. PROP_MINH_CALC_PX,
  6004. px, important);
  6005. }
  6006. }
  6007. continue;
  6008. }
  6009. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  6010. if (str_eq(prop, "width")) {
  6011. decl_add(out, nout, PROP_WIDTH, v, important);
  6012. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  6013. important);
  6014. } else if (str_eq(prop, "max-width")) {
  6015. decl_add(out, nout, PROP_MAXW, v, important);
  6016. decl_add(out, nout, PROP_MAXW_KIND, kind,
  6017. important);
  6018. } else if (str_eq(prop, "min-width")) {
  6019. decl_add(out, nout, PROP_MINW, v, important);
  6020. decl_add(out, nout, PROP_MINW_KIND, kind,
  6021. important);
  6022. } else if (str_eq(prop, "max-height")) {
  6023. decl_add(out, nout, PROP_MAXH, v, important);
  6024. decl_add(out, nout, PROP_MAXH_KIND, kind,
  6025. important);
  6026. } else if (str_eq(prop, "min-height")) {
  6027. decl_add(out, nout, PROP_MINH, v, important);
  6028. decl_add(out, nout, PROP_MINH_KIND, kind,
  6029. important);
  6030. } else {
  6031. decl_add(out, nout, PROP_HEIGHT, v, important);
  6032. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  6033. important);
  6034. }
  6035. }
  6036. } else if (str_eq(prop, "content")) {
  6037. decl_add(out, nout, PROP_CONTENT,
  6038. css_collect_only ? 0 :
  6039. parse_content(d, s + vstart, vend - vstart),
  6040. important);
  6041. } else if (str_eq(prop, "aspect-ratio")) {
  6042. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  6043. * preferred height of an auto-height box. */
  6044. unsigned int q = vstart;
  6045. unsigned int a[2] = { 0, 1 }, k;
  6046. for (k = 0; k < 2; k++) {
  6047. unsigned int whole = 0, frac = 0, sc = 1;
  6048. while (q < vend && is_space(s[q]))
  6049. q++;
  6050. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  6051. s[q] != '.'))
  6052. break;
  6053. while (q < vend && s[q] >= '0' && s[q] <= '9')
  6054. whole = whole * 10 +
  6055. (unsigned int)(s[q++] - '0');
  6056. if (q < vend && s[q] == '.') {
  6057. q++;
  6058. while (q < vend && s[q] >= '0' &&
  6059. s[q] <= '9') {
  6060. if (sc < 100) {
  6061. frac = frac * 10 +
  6062. (unsigned int)(s[q] - '0');
  6063. sc *= 10;
  6064. }
  6065. q++;
  6066. }
  6067. }
  6068. a[k] = whole * 100 + frac * (100 / sc);
  6069. while (q < vend && is_space(s[q]))
  6070. q++;
  6071. if (k == 0 && (q >= vend || s[q] != '/')) {
  6072. a[1] = 100;
  6073. break;
  6074. }
  6075. q++;
  6076. }
  6077. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  6078. a[0] = a[1] = 0;
  6079. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  6080. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  6081. } else if (str_eq(prop, "box-sizing")) {
  6082. int bb = (vend - vstart >= 10 &&
  6083. ci_eq_n(s + vstart, "border-box", 10));
  6084. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  6085. } else if (str_eq(prop, "line-height")) {
  6086. unsigned short px;
  6087. unsigned char pct;
  6088. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  6089. if (px) {
  6090. decl_add(out, nout, PROP_LH_PX, px, important);
  6091. decl_add(out, nout, PROP_LH_PCT, 0, important);
  6092. } else {
  6093. decl_add(out, nout, PROP_LH_PX, 0, important);
  6094. decl_add(out, nout, PROP_LH_PCT, pct, important);
  6095. }
  6096. }
  6097. } else if (str_eq(prop, "list-style") ||
  6098. str_eq(prop, "list-style-type")) {
  6099. int ls = BR_LIST_AUTO;
  6100. if (contains_ci_len(s + vstart, vend - vstart,
  6101. "none"))
  6102. ls = BR_LIST_NONE;
  6103. else if (contains_ci_len(s + vstart, vend - vstart,
  6104. "circle"))
  6105. ls = BR_LIST_CIRCLE;
  6106. else if (contains_ci_len(s + vstart, vend - vstart,
  6107. "square"))
  6108. ls = BR_LIST_SQUARE;
  6109. else if (contains_ci_len(s + vstart, vend - vstart,
  6110. "lower-alpha") ||
  6111. contains_ci_len(s + vstart, vend - vstart,
  6112. "lower-latin"))
  6113. ls = BR_LIST_LOWER_ALPHA;
  6114. else if (contains_ci_len(s + vstart, vend - vstart,
  6115. "upper-alpha") ||
  6116. contains_ci_len(s + vstart, vend - vstart,
  6117. "upper-latin"))
  6118. ls = BR_LIST_UPPER_ALPHA;
  6119. else if (contains_ci_len(s + vstart, vend - vstart,
  6120. "lower-roman"))
  6121. ls = BR_LIST_LOWER_ROMAN;
  6122. else if (contains_ci_len(s + vstart, vend - vstart,
  6123. "upper-roman"))
  6124. ls = BR_LIST_UPPER_ROMAN;
  6125. else if (contains_ci_len(s + vstart, vend - vstart,
  6126. "decimal"))
  6127. ls = BR_LIST_DECIMAL;
  6128. else if (contains_ci_len(s + vstart, vend - vstart,
  6129. "disc"))
  6130. ls = BR_LIST_DISC;
  6131. decl_add(out, nout, PROP_LIST_NONE,
  6132. ls == BR_LIST_NONE, important);
  6133. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  6134. } else if (str_eq(prop, "border-radius") ||
  6135. str_eq(prop, "border-top-left-radius") ||
  6136. str_eq(prop, "border-top-right-radius") ||
  6137. str_eq(prop, "border-bottom-right-radius") ||
  6138. str_eq(prop, "border-bottom-left-radius")) {
  6139. unsigned short v[4];
  6140. unsigned char k[4];
  6141. unsigned int pos = 0;
  6142. int n = 0;
  6143. while (pos < vend - vstart && n < 4) {
  6144. while (pos < vend - vstart &&
  6145. is_space(s[vstart + pos]))
  6146. pos++;
  6147. if (pos >= vend - vstart ||
  6148. s[vstart + pos] == '/')
  6149. break;
  6150. if (!parse_radius_value(s + vstart,
  6151. vend - vstart, &pos,
  6152. &v[n], &k[n]))
  6153. break;
  6154. n++;
  6155. }
  6156. if (n) {
  6157. unsigned short r[4];
  6158. unsigned char rk[4];
  6159. int i;
  6160. if (n == 1) {
  6161. for (i = 0; i < 4; i++) {
  6162. r[i] = v[0];
  6163. rk[i] = k[0];
  6164. }
  6165. } else if (n == 2) {
  6166. r[0] = r[2] = v[0];
  6167. r[1] = r[3] = v[1];
  6168. rk[0] = rk[2] = k[0];
  6169. rk[1] = rk[3] = k[1];
  6170. } else if (n == 3) {
  6171. r[0] = v[0];
  6172. r[1] = r[3] = v[1];
  6173. r[2] = v[2];
  6174. rk[0] = k[0];
  6175. rk[1] = rk[3] = k[1];
  6176. rk[2] = k[2];
  6177. } else {
  6178. for (i = 0; i < 4; i++) {
  6179. r[i] = v[i];
  6180. rk[i] = k[i];
  6181. }
  6182. }
  6183. for (i = 0; i < 4; i++)
  6184. r[i] = rk[i] == 2 ? 255 :
  6185. (r[i] > 255 ? 255 : r[i]);
  6186. if (str_eq(prop, "border-top-left-radius")) {
  6187. decl_add(out, nout, PROP_RADIUS_TL,
  6188. (short)r[0], important);
  6189. } else if (str_eq(prop,
  6190. "border-top-right-radius")) {
  6191. decl_add(out, nout, PROP_RADIUS_TR,
  6192. (short)r[0], important);
  6193. } else if (str_eq(prop,
  6194. "border-bottom-right-radius")) {
  6195. decl_add(out, nout, PROP_RADIUS_BR,
  6196. (short)r[0], important);
  6197. } else if (str_eq(prop,
  6198. "border-bottom-left-radius")) {
  6199. decl_add(out, nout, PROP_RADIUS_BL,
  6200. (short)r[0], important);
  6201. } else {
  6202. decl_add(out, nout, PROP_RADIUS_TL,
  6203. (short)r[0], important);
  6204. decl_add(out, nout, PROP_RADIUS_TR,
  6205. (short)r[1], important);
  6206. decl_add(out, nout, PROP_RADIUS_BR,
  6207. (short)r[2], important);
  6208. decl_add(out, nout, PROP_RADIUS_BL,
  6209. (short)r[3], important);
  6210. }
  6211. }
  6212. } else if (str_eq(prop, "border") ||
  6213. str_eq(prop, "border-top") ||
  6214. str_eq(prop, "border-right") ||
  6215. str_eq(prop, "border-bottom") ||
  6216. str_eq(prop, "border-left") ||
  6217. str_eq(prop, "border-color") ||
  6218. str_eq(prop, "border-width") ||
  6219. str_eq(prop, "border-style") ||
  6220. str_eq(prop, "border-top-width") ||
  6221. str_eq(prop, "border-right-width") ||
  6222. str_eq(prop, "border-bottom-width") ||
  6223. str_eq(prop, "border-left-width") ||
  6224. str_eq(prop, "border-top-color") ||
  6225. str_eq(prop, "border-right-color") ||
  6226. str_eq(prop, "border-bottom-color") ||
  6227. str_eq(prop, "border-left-color") ||
  6228. str_eq(prop, "border-top-style") ||
  6229. str_eq(prop, "border-right-style") ||
  6230. str_eq(prop, "border-bottom-style") ||
  6231. str_eq(prop, "border-left-style")) {
  6232. const char *v = s + vstart;
  6233. unsigned int vl = vend - vstart;
  6234. unsigned int pos = 0, ts, te;
  6235. int widths[4], colors[4], styles[4], trans[4];
  6236. int nw = 0, nc = 0, ns = 0;
  6237. int side = -1;
  6238. int is_wl = str_eq(prop, "border-width");
  6239. int is_cl = str_eq(prop, "border-color");
  6240. int is_sl = str_eq(prop, "border-style");
  6241. int i;
  6242. if (str_eq(prop, "border-top")) side = 0;
  6243. else if (str_eq(prop, "border-right")) side = 1;
  6244. else if (str_eq(prop, "border-bottom")) side = 2;
  6245. else if (str_eq(prop, "border-left")) side = 3;
  6246. else if (str_eq(prop, "border-top-width")) {
  6247. side = 0; is_wl = 2;
  6248. } else if (str_eq(prop, "border-right-width")) {
  6249. side = 1; is_wl = 2;
  6250. } else if (str_eq(prop, "border-bottom-width")) {
  6251. side = 2; is_wl = 2;
  6252. } else if (str_eq(prop, "border-left-width")) {
  6253. side = 3; is_wl = 2;
  6254. } else if (str_eq(prop, "border-top-color")) {
  6255. side = 0; is_cl = 2;
  6256. } else if (str_eq(prop, "border-right-color")) {
  6257. side = 1; is_cl = 2;
  6258. } else if (str_eq(prop, "border-bottom-color")) {
  6259. side = 2; is_cl = 2;
  6260. } else if (str_eq(prop, "border-left-color")) {
  6261. side = 3; is_cl = 2;
  6262. } else if (str_eq(prop, "border-top-style")) {
  6263. side = 0; is_sl = 2;
  6264. } else if (str_eq(prop, "border-right-style")) {
  6265. side = 1; is_sl = 2;
  6266. } else if (str_eq(prop, "border-bottom-style")) {
  6267. side = 2; is_sl = 2;
  6268. } else if (str_eq(prop, "border-left-style")) {
  6269. side = 3; is_sl = 2;
  6270. }
  6271. if (is_wl) {
  6272. while (nw < 4 && next_token(v, vl, &pos,
  6273. &ts, &te)) {
  6274. int x;
  6275. if (!parse_len_token(v + ts, te - ts,
  6276. &x))
  6277. break;
  6278. widths[nw++] = x;
  6279. }
  6280. if (nw) {
  6281. if (is_wl == 1)
  6282. expand_four(widths, nw);
  6283. if (side >= 0 && is_wl == 2) {
  6284. decl_add(out, nout,
  6285. PROP_BW_T + side,
  6286. widths[0], important);
  6287. } else {
  6288. for (i = 0; i < 4; i++)
  6289. decl_add(out, nout,
  6290. PROP_BW_T + i,
  6291. widths[i],
  6292. important);
  6293. }
  6294. }
  6295. } else if (is_cl) {
  6296. unsigned short c;
  6297. int t;
  6298. while (nc < 4 && next_token(v, vl, &pos,
  6299. &ts, &te)) {
  6300. t = color_is_transparent(v + ts,
  6301. te - ts);
  6302. if (!t && !parse_color(v + ts,
  6303. te - ts, &c))
  6304. break;
  6305. colors[nc] = t ? 0 : c;
  6306. trans[nc] = t;
  6307. nc++;
  6308. }
  6309. if (nc) {
  6310. if (is_cl == 1) {
  6311. expand_four(colors, nc);
  6312. expand_four(trans, nc);
  6313. }
  6314. if (side >= 0 && is_cl == 2) {
  6315. decl_add(out, nout,
  6316. PROP_BC_T + side,
  6317. colors[0], important);
  6318. decl_add(out, nout,
  6319. PROP_BT_T + side,
  6320. trans[0], important);
  6321. } else {
  6322. for (i = 0; i < 4; i++) {
  6323. decl_add(out, nout,
  6324. PROP_BC_T + i,
  6325. colors[i],
  6326. important);
  6327. decl_add(out, nout,
  6328. PROP_BT_T + i,
  6329. trans[i],
  6330. important);
  6331. }
  6332. }
  6333. }
  6334. } else if (is_sl) {
  6335. while (ns < 4 && next_token(v, vl, &pos,
  6336. &ts, &te)) {
  6337. int stv;
  6338. if ((te - ts == 4 &&
  6339. ci_eq_n(v + ts, "none", 4)) ||
  6340. (te - ts == 6 &&
  6341. ci_eq_n(v + ts, "hidden", 6)))
  6342. stv = 0;
  6343. else if (te - ts == 6 &&
  6344. ci_eq_n(v + ts,
  6345. "dashed", 6))
  6346. stv = 2;
  6347. else if (te - ts == 6 &&
  6348. ci_eq_n(v + ts,
  6349. "dotted", 6))
  6350. stv = 3;
  6351. else
  6352. stv = 1;
  6353. styles[ns++] = stv;
  6354. }
  6355. if (ns) {
  6356. if (is_sl == 1)
  6357. expand_four(styles, ns);
  6358. if (is_sl == 2 && side >= 0) {
  6359. if (!styles[0])
  6360. decl_add(out, nout,
  6361. PROP_BW_T + side,
  6362. 0, important);
  6363. else
  6364. decl_add(out, nout,
  6365. PROP_BS_T + side,
  6366. styles[0] - 1,
  6367. important);
  6368. } else {
  6369. for (i = 0; i < 4; i++) {
  6370. if (!styles[i])
  6371. decl_add(out, nout,
  6372. PROP_BW_T + i,
  6373. 0,
  6374. important);
  6375. else
  6376. decl_add(out, nout,
  6377. PROP_BS_T + i,
  6378. styles[i] - 1,
  6379. important);
  6380. }
  6381. }
  6382. }
  6383. } else {
  6384. int bw = -1, bc = -1, style_none = 0;
  6385. int bstyle = -1, btrans = 0;
  6386. while (next_token(v, vl, &pos, &ts, &te)) {
  6387. unsigned short c;
  6388. int x;
  6389. if ((te - ts == 4 &&
  6390. ci_eq_n(v + ts, "none", 4)) ||
  6391. (te - ts == 6 &&
  6392. ci_eq_n(v + ts, "hidden", 6))) {
  6393. style_none = 1;
  6394. } else if (te - ts == 6 &&
  6395. ci_eq_n(v + ts, "dashed", 6)) {
  6396. bstyle = BR_BORDER_DASHED;
  6397. } else if (te - ts == 6 &&
  6398. ci_eq_n(v + ts, "dotted", 6)) {
  6399. bstyle = BR_BORDER_DOTTED;
  6400. } else if (te - ts == 5 &&
  6401. ci_eq_n(v + ts, "solid", 5)) {
  6402. bstyle = BR_BORDER_SOLID;
  6403. } else if (parse_len_token(v + ts,
  6404. te - ts, &x)) {
  6405. bw = x;
  6406. } else if (color_is_transparent(v + ts,
  6407. te - ts)) {
  6408. bc = 0;
  6409. btrans = 1;
  6410. } else if (parse_color(v + ts,
  6411. te - ts, &c)) {
  6412. bc = c;
  6413. btrans = 0;
  6414. }
  6415. }
  6416. if (style_none)
  6417. bw = 0;
  6418. if (bc >= 0 && bw < 0)
  6419. bw = 1;
  6420. if (side < 0) {
  6421. if (bw >= 0)
  6422. decl_add(out, nout,
  6423. PROP_BORDER_W, bw,
  6424. important);
  6425. if (bc >= 0)
  6426. decl_add(out, nout,
  6427. PROP_BORDER_COLOR, bc,
  6428. important);
  6429. if (bc >= 0)
  6430. for (i = 0; i < 4; i++)
  6431. decl_add(out, nout,
  6432. PROP_BT_T + i,
  6433. btrans,
  6434. important);
  6435. if (bstyle >= 0)
  6436. for (i = 0; i < 4; i++)
  6437. decl_add(out, nout,
  6438. PROP_BS_T + i,
  6439. bstyle,
  6440. important);
  6441. } else {
  6442. if (bw >= 0)
  6443. decl_add(out, nout,
  6444. PROP_BW_T + side, bw,
  6445. important);
  6446. if (bc >= 0)
  6447. decl_add(out, nout,
  6448. PROP_BC_T + side, bc,
  6449. important);
  6450. if (bc >= 0)
  6451. decl_add(out, nout,
  6452. PROP_BT_T + side,
  6453. btrans, important);
  6454. if (bstyle >= 0)
  6455. decl_add(out, nout,
  6456. PROP_BS_T + side,
  6457. bstyle, important);
  6458. }
  6459. }
  6460. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6461. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6462. unsigned int pos = 0;
  6463. int is_pad = prop[0] == 'p';
  6464. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6465. while (pos < vend - vstart && nv < 4) {
  6466. units[nv] = 0;
  6467. if (parse_margin_calc(s + vstart,
  6468. vend - vstart, &pos,
  6469. &vals[nv], &cpx[nv])) {
  6470. units[nv] = 5;
  6471. } else {
  6472. vals[nv] = parse_margin_len(
  6473. s + vstart, vend - vstart,
  6474. &pos, &units[nv]);
  6475. }
  6476. nv++;
  6477. while (pos < vend - vstart &&
  6478. is_space(s[vstart + pos]))
  6479. pos++;
  6480. }
  6481. if (nv == 1) {
  6482. vals[1] = vals[2] = vals[3] = vals[0];
  6483. units[1] = units[2] = units[3] = units[0];
  6484. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6485. } else if (nv == 2) {
  6486. vals[2] = vals[0];
  6487. vals[3] = vals[1];
  6488. units[2] = units[0];
  6489. units[3] = units[1];
  6490. cpx[2] = cpx[0];
  6491. cpx[3] = cpx[1];
  6492. } else if (nv == 3) {
  6493. vals[3] = vals[1];
  6494. units[3] = units[1];
  6495. cpx[3] = cpx[1];
  6496. }
  6497. if (nv > 0) {
  6498. int k;
  6499. for (k = 0; k < 4; k++) {
  6500. int v = vals[k];
  6501. int u = units[k];
  6502. int au = u == 4;
  6503. if (au)
  6504. v = 0;
  6505. if (is_pad && au)
  6506. u = 0;
  6507. decl_add(out, nout,
  6508. (unsigned char)(base + k), v,
  6509. important);
  6510. decl_add(out, nout,
  6511. (unsigned char)
  6512. ((is_pad ? PROP_PT_U :
  6513. PROP_MT_U) + k),
  6514. u, important);
  6515. if (u == 5)
  6516. decl_add(out, nout,
  6517. (unsigned char)
  6518. ((is_pad ? PROP_PT_CPX :
  6519. PROP_MT_CPX) + k),
  6520. cpx[k], important);
  6521. if (!is_pad && (k == 1 || k == 3))
  6522. decl_add(out, nout,
  6523. k == 1 ? PROP_MR_AUTO :
  6524. PROP_ML_AUTO,
  6525. au, important);
  6526. }
  6527. }
  6528. } else if (str_eq(prop, "margin-top") ||
  6529. str_eq(prop, "margin-right") ||
  6530. str_eq(prop, "margin-bottom") ||
  6531. str_eq(prop, "margin-left") ||
  6532. str_eq(prop, "padding-top") ||
  6533. str_eq(prop, "padding-right") ||
  6534. str_eq(prop, "padding-bottom") ||
  6535. str_eq(prop, "padding-left")) {
  6536. unsigned int pos = 0;
  6537. int unit = 0;
  6538. int v = 0, cpx = 0;
  6539. unsigned char p = PROP_MT;
  6540. const char *side;
  6541. int au;
  6542. if (parse_margin_calc(s + vstart, vend - vstart,
  6543. &pos, &v, &cpx))
  6544. unit = 5;
  6545. else
  6546. v = parse_margin_len(s + vstart,
  6547. vend - vstart, &pos,
  6548. &unit);
  6549. au = unit == 4;
  6550. if (au)
  6551. v = 0;
  6552. if (prop[0] == 'p') {
  6553. p = PROP_PT;
  6554. if (au)
  6555. unit = 0;
  6556. }
  6557. side = prop + (prop[0] == 'p' ? 8 : 7);
  6558. if (str_eq(side, "right"))
  6559. p = (unsigned char)(p + 1);
  6560. else if (str_eq(side, "bottom"))
  6561. p = (unsigned char)(p + 2);
  6562. else if (str_eq(side, "left"))
  6563. p = (unsigned char)(p + 3);
  6564. decl_add(out, nout, p, v, important);
  6565. decl_add(out, nout,
  6566. (unsigned char)((prop[0] == 'p' ?
  6567. PROP_PT_U : PROP_MT_U) +
  6568. (p - (prop[0] == 'p' ?
  6569. PROP_PT : PROP_MT))),
  6570. unit, important);
  6571. if (unit == 5)
  6572. decl_add(out, nout,
  6573. (unsigned char)((prop[0] == 'p' ?
  6574. PROP_PT_CPX : PROP_MT_CPX) +
  6575. (p - (prop[0] == 'p' ?
  6576. PROP_PT : PROP_MT))),
  6577. cpx, important);
  6578. if (str_eq(side, "right"))
  6579. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6580. else if (str_eq(side, "left"))
  6581. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6582. }
  6583. }
  6584. }
  6585. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6586. * nested blocks. This is the mobile/base rules path: media enhancements are
  6587. * deliberately ignored for now. */
  6588. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6589. unsigned int i)
  6590. {
  6591. unsigned int j = i;
  6592. while (j < len && s[j] != '{' && s[j] != ';')
  6593. j++;
  6594. if (j >= len)
  6595. return len;
  6596. if (s[j] == ';')
  6597. return j + 1;
  6598. j++;
  6599. return css_skip_nested(s, len, j);
  6600. }
  6601. static unsigned int viewport_w = 1024;
  6602. static unsigned short theme_text = 0x0000;
  6603. static unsigned short theme_bg = 0xFFFF;
  6604. static unsigned short theme_panel = 0xEF5D;
  6605. static unsigned short theme_link = 0x001B;
  6606. void br_set_theme(unsigned short text, unsigned short bg,
  6607. unsigned short panel, unsigned short link)
  6608. {
  6609. theme_text = text;
  6610. theme_bg = bg;
  6611. theme_panel = panel;
  6612. theme_link = link;
  6613. }
  6614. void br_set_viewport(unsigned int width)
  6615. {
  6616. viewport_w = width ? width : 1024;
  6617. }
  6618. static unsigned int media_len_px(const char *s, unsigned int len)
  6619. {
  6620. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6621. int em = 0;
  6622. while (i < len && is_space(s[i]))
  6623. i++;
  6624. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6625. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6626. i++;
  6627. }
  6628. if (i < len && s[i] == '.') {
  6629. i++;
  6630. /* Consume every fractional digit even when there are more
  6631. * than the three we keep: leaving the tail behind made the
  6632. * unit check miss "em" (74.9375em became 74px). */
  6633. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6634. if (scale < 1000) {
  6635. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6636. scale *= 10;
  6637. }
  6638. i++;
  6639. }
  6640. }
  6641. while (i < len && is_space(s[i]))
  6642. i++;
  6643. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6644. em = 1;
  6645. i += 2;
  6646. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6647. em = 1;
  6648. i += 3;
  6649. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6650. i += 2;
  6651. }
  6652. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6653. if (em)
  6654. return v * 16 / 100;
  6655. return v / 100;
  6656. }
  6657. /* One parenthesised media feature: min-width/max-width and the modern
  6658. * (width <= 600px) form. Unknown features are treated as true. */
  6659. static int media_feature_matches(const char *s, unsigned int len)
  6660. {
  6661. unsigned int i;
  6662. while (len && is_space(*s)) { s++; len--; }
  6663. while (len && is_space(s[len - 1])) len--;
  6664. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6665. /* There is no user-selectable colour scheme. Ignore both
  6666. * variants so a page's base palette is used; sites whose base
  6667. * is dark (like the project docs) then stay dark instead of
  6668. * flipping to their light media override. */
  6669. return 0;
  6670. }
  6671. {
  6672. /* Environment features with a known answer: a mouse-driven
  6673. * screen with normal contrast and no forced colours. Google
  6674. * puts `border:1px solid CanvasText` on its buttons inside
  6675. * `(forced-colors:active)`. */
  6676. static const struct { const char *name, *yes; } env[] = {
  6677. { "forced-colors", "none" },
  6678. { "-ms-high-contrast", "" },
  6679. { "prefers-contrast", "no-preference" },
  6680. { "inverted-colors", "none" },
  6681. { "any-hover", "hover" },
  6682. { "hover", "hover" },
  6683. { "any-pointer", "fine" },
  6684. { "pointer", "fine" },
  6685. { "prefers-reduced-motion", "no-preference" },
  6686. { "prefers-reduced-transparency", "no-preference" },
  6687. { "display-mode", "browser" },
  6688. };
  6689. unsigned int k;
  6690. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6691. unsigned int nl = s_len(env[k].name), j;
  6692. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6693. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6694. continue;
  6695. for (j = nl; j < len && s[j] != ':'; j++)
  6696. ;
  6697. if (j >= len) /* boolean form: (hover) */
  6698. return env[k].yes[0] &&
  6699. !ci_eq_n(env[k].yes, "none", 4);
  6700. j++;
  6701. while (j < len && is_space(s[j]))
  6702. j++;
  6703. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6704. ci_eq_n(s + j, env[k].yes, len - j);
  6705. }
  6706. }
  6707. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6708. for (i = 9; i < len && s[i] != ':'; i++)
  6709. ;
  6710. if (i < len)
  6711. return viewport_w >= media_len_px(s + i + 1,
  6712. len - i - 1);
  6713. return 1;
  6714. }
  6715. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6716. for (i = 9; i < len && s[i] != ':'; i++)
  6717. ;
  6718. if (i < len)
  6719. return viewport_w <= media_len_px(s + i + 1,
  6720. len - i - 1);
  6721. return 1;
  6722. }
  6723. for (i = 0; i < len; i++) {
  6724. if (s[i] == '<' || s[i] == '>') {
  6725. int max = s[i] == '<';
  6726. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6727. if (i + 1 < len && s[i + 1] == '=')
  6728. v = media_len_px(s + i + 2, len - i - 2);
  6729. return max ? viewport_w <= v : viewport_w >= v;
  6730. }
  6731. }
  6732. return 1;
  6733. }
  6734. static int media_query_matches(const char *s, unsigned int len)
  6735. {
  6736. unsigned int i;
  6737. if (contains_ci_len(s, len, "print") &&
  6738. !contains_ci_len(s, len, "screen"))
  6739. return 0;
  6740. if (contains_ci_len(s, len, "not "))
  6741. return 0;
  6742. for (i = 0; i < len; i++) {
  6743. if (s[i] == '(') {
  6744. unsigned int st = ++i;
  6745. while (i < len && s[i] != ')')
  6746. i++;
  6747. if (!media_feature_matches(s + st, i - st))
  6748. return 0;
  6749. }
  6750. }
  6751. return 1;
  6752. }
  6753. static int media_list_matches(const char *s, unsigned int len)
  6754. {
  6755. unsigned int i = 0;
  6756. while (i < len) {
  6757. unsigned int st = i;
  6758. while (i < len && s[i] != ',')
  6759. i++;
  6760. if (media_query_matches(s + st, i - st))
  6761. return 1;
  6762. i++;
  6763. }
  6764. return 0;
  6765. }
  6766. int br_media_matches(const char *query)
  6767. {
  6768. return media_list_matches(query, s_len(query));
  6769. }
  6770. static unsigned int rule_hash(const char *s, unsigned int len)
  6771. {
  6772. unsigned int h = 2166136261u, i;
  6773. for (i = 0; i < len; i++) {
  6774. char c = s[i];
  6775. if (c >= 'A' && c <= 'Z')
  6776. c = (char)(c - 'A' + 'a');
  6777. h ^= (unsigned char)c;
  6778. h *= 16777619u;
  6779. }
  6780. return h % BR_RULE_HASH;
  6781. }
  6782. /* Index a parsed rule by the most selective component of its rightmost
  6783. * compound selector. br_style_for() then only visits rules that can
  6784. * possibly match a node instead of scanning every rule. */
  6785. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6786. unsigned int len);
  6787. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6788. char *out, unsigned int cap, unsigned int *olen);
  6789. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6790. const char *key, unsigned int len)
  6791. {
  6792. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6793. * "md:flex", the class the element carries, not under "md". */
  6794. char cname[96], iname[96];
  6795. unsigned int i, clen = 0, ilen = 0;
  6796. unsigned short *head;
  6797. for (i = 0; i < len; i++) {
  6798. if (key[i] == '\\') {
  6799. i++;
  6800. continue;
  6801. }
  6802. if (key[i] == '(' || key[i] == '[') {
  6803. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6804. i = sel_skip_group(key, i, len) - 1;
  6805. continue;
  6806. }
  6807. if (key[i] == '.' || key[i] == '#') {
  6808. char tmp[96];
  6809. unsigned int n, e;
  6810. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6811. if (n && n + 1 < sizeof(tmp)) {
  6812. if (key[i] == '#') {
  6813. memcpy(iname, tmp, n);
  6814. ilen = n;
  6815. } else {
  6816. memcpy(cname, tmp, n);
  6817. clen = n;
  6818. }
  6819. }
  6820. i = e - 1;
  6821. }
  6822. }
  6823. if (ilen)
  6824. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6825. else if (clen)
  6826. head = &d->rule_bucket[rule_hash(cname, clen)];
  6827. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6828. head = &d->rule_tag[BR_TAG_HTML];
  6829. else if (len && is_ident(key[0])) {
  6830. char name[16];
  6831. unsigned int n = 0, tag;
  6832. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6833. name[n] = key[n];
  6834. n++;
  6835. }
  6836. name[n] = '\0';
  6837. tag = br_tag_from_name(name);
  6838. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6839. head = &d->rule_tag[tag];
  6840. else
  6841. head = &d->rule_any;
  6842. } else {
  6843. head = &d->rule_any;
  6844. }
  6845. d->rule_next[idx] = *head;
  6846. *head = (unsigned short)idx;
  6847. }
  6848. #define SEL_PARTS 16
  6849. struct sel_part {
  6850. unsigned short st, en;
  6851. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6852. };
  6853. static unsigned int selector_specificity(const char *s, unsigned int len);
  6854. static int css_selector_supported(const char *s, unsigned int len);
  6855. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6856. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6857. unsigned int *st, unsigned int *en);
  6858. static int css_collect_only;
  6859. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6860. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6861. * selector with any other pseudo-element. */
  6862. static int css_pseudo_element(char *sel, unsigned int *len)
  6863. {
  6864. static const struct {
  6865. const char *s;
  6866. unsigned char l, pe;
  6867. } pe[] = {
  6868. { "::before", 8, 1 }, { "::after", 7, 2 },
  6869. { ":before", 7, 1 }, { ":after", 6, 2 },
  6870. };
  6871. unsigned int k, n = *len;
  6872. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6873. unsigned int l = pe[k].l;
  6874. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6875. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6876. continue; /* matched the tail of "::x" */
  6877. n -= l;
  6878. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6879. sel[n - 1] == '+' || sel[n - 1] == '~')
  6880. sel[n++] = '*';
  6881. sel[n] = '\0';
  6882. *len = n;
  6883. return pe[k].pe;
  6884. }
  6885. }
  6886. for (k = 0; k + 1 < n; k++)
  6887. if (sel[k] == ':' && sel[k + 1] == ':')
  6888. return -1;
  6889. return 0;
  6890. }
  6891. /* Does a declaration block use or define a custom property whose value
  6892. * depends on the element (defined by some non-root rule)? Such rules are
  6893. * re-expanded per element at cascade time. */
  6894. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6895. unsigned int len)
  6896. {
  6897. unsigned char f = 0;
  6898. unsigned int i, k;
  6899. for (i = 0; i + 6 < len; i++) {
  6900. unsigned int st, e, vi;
  6901. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6902. continue;
  6903. st = i + 4;
  6904. while (st < len && is_space(s[st]))
  6905. st++;
  6906. e = st;
  6907. while (e < len && s[e] != ',' && s[e] != ')' &&
  6908. !is_space(s[e]))
  6909. e++;
  6910. vi = css_var_find(d, s + st, e - st);
  6911. if (vi < BR_MAX_VARS &&
  6912. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6913. f |= BR_RULE_USES_SCOPED;
  6914. break;
  6915. }
  6916. }
  6917. css_local_collect(s, len);
  6918. for (k = 0; k < css_nlocal; k++) {
  6919. unsigned int vi = css_var_find(d, css_local[k].name,
  6920. css_local[k].nlen);
  6921. if (vi < BR_MAX_VARS &&
  6922. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6923. f |= BR_RULE_DEFINES_SCOPED;
  6924. }
  6925. css_nlocal = 0;
  6926. return f;
  6927. }
  6928. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6929. {
  6930. unsigned int i = 0;
  6931. while (i < len && d->nrules < d->rule_cap) {
  6932. unsigned int sstart, send, bstart, bend;
  6933. struct br_decl decls[BR_DECLS];
  6934. unsigned int nd, scope_off = 0;
  6935. unsigned char scope_flags = 0;
  6936. while (i < len && is_space(s[i]))
  6937. i++;
  6938. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6939. i += 2;
  6940. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6941. i++;
  6942. i += 2;
  6943. continue;
  6944. }
  6945. if (i >= len || s[i] == '}')
  6946. break;
  6947. if (s[i] == '@') {
  6948. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6949. unsigned int q = i + 6;
  6950. while (q < len && s[q] != '{')
  6951. q++;
  6952. if (q < len &&
  6953. media_list_matches(s + i + 6,
  6954. q - (i + 6))) {
  6955. unsigned int b = q + 1;
  6956. unsigned int e = css_skip_nested(s, len, b);
  6957. if (e > b)
  6958. parse_css(d, s + b,
  6959. (e - 1) - b);
  6960. i = e;
  6961. } else {
  6962. i = skip_at_rule(s, len, i);
  6963. }
  6964. continue;
  6965. }
  6966. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6967. (i + 6 == len || is_space(s[i + 6]) ||
  6968. s[i + 6] == '{' || s[i + 6] == ';')) {
  6969. unsigned int q = i + 6, nst = q;
  6970. while (q < len && s[q] != '{' && s[q] != ';')
  6971. q++;
  6972. if (q >= len) {
  6973. i = len;
  6974. continue;
  6975. }
  6976. if (s[q] == ';') {
  6977. /* `@layer a, b;` only fixes the order. */
  6978. unsigned int st = nst;
  6979. char path[128];
  6980. while (st < q) {
  6981. unsigned int en = st;
  6982. while (en < q && s[en] != ',')
  6983. en++;
  6984. if (en > st)
  6985. css_layer_register(d, s + st,
  6986. en - st,
  6987. path,
  6988. sizeof(path));
  6989. st = en + 1;
  6990. }
  6991. i = q + 1;
  6992. continue;
  6993. }
  6994. {
  6995. unsigned int b = q + 1;
  6996. unsigned int e = css_skip_nested(s, len, b);
  6997. unsigned char saved = css_cur_layer;
  6998. char saved_name[128], path[128];
  6999. unsigned char rank;
  7000. memcpy(saved_name, css_layer_name,
  7001. sizeof(saved_name));
  7002. rank = css_layer_register(d, s + nst, q - nst,
  7003. path,
  7004. sizeof(path));
  7005. if (rank) {
  7006. css_cur_layer = rank;
  7007. memcpy(css_layer_name, path,
  7008. sizeof(css_layer_name));
  7009. }
  7010. if (e > b)
  7011. parse_css(d, s + b, (e - 1) - b);
  7012. css_cur_layer = saved;
  7013. memcpy(css_layer_name, saved_name,
  7014. sizeof(css_layer_name));
  7015. i = e;
  7016. }
  7017. continue;
  7018. }
  7019. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  7020. unsigned int q = i + 9;
  7021. while (q < len && s[q] != '{' && s[q] != ';')
  7022. q++;
  7023. if (q < len && s[q] == '{' &&
  7024. supports_matches(s + i + 9, q - (i + 9))) {
  7025. unsigned int b = q + 1;
  7026. unsigned int e = css_skip_nested(s, len, b);
  7027. if (e > b)
  7028. parse_css(d, s + b, (e - 1) - b);
  7029. i = e;
  7030. } else {
  7031. i = skip_at_rule(s, len, i);
  7032. }
  7033. continue;
  7034. }
  7035. i = skip_at_rule(s, len, i);
  7036. continue;
  7037. }
  7038. sstart = i;
  7039. while (i < len && s[i] != '{')
  7040. i++;
  7041. if (i >= len)
  7042. break;
  7043. send = i;
  7044. i++;
  7045. bstart = i;
  7046. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  7047. * to the matching close brace; stopping at the first '}' left
  7048. * the parser out of sync and dropped the rest of the sheet
  7049. * (nd-aktuell lost its desktop rules that way). */
  7050. bend = css_block_end(s, len, i);
  7051. i = bend < len ? bend + 1 : len;
  7052. while (sstart < send && is_space(s[sstart]))
  7053. sstart++;
  7054. while (send > sstart && is_space(s[send - 1]))
  7055. send--;
  7056. if (sstart == send)
  7057. continue;
  7058. if (!css_dark_selector(s + sstart, send - sstart))
  7059. css_collect_block(d, s + bstart, bend - bstart,
  7060. css_root_selector(s + sstart,
  7061. send - sstart));
  7062. if (css_collect_only)
  7063. continue;
  7064. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  7065. {
  7066. unsigned char sc = bend - bstart <= 4000 ?
  7067. css_block_scope(d, s + bstart, bend - bstart) :
  7068. 0;
  7069. scope_flags = 0;
  7070. scope_off = 0;
  7071. if (sc) {
  7072. scope_off = store_raw(d, s + bstart,
  7073. bend - bstart,
  7074. bend - bstart);
  7075. if (scope_off)
  7076. scope_flags = sc;
  7077. }
  7078. }
  7079. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  7080. continue;
  7081. /* Split a comma-separated selector list into separate rules.
  7082. * A selector the engine cannot match is dropped; the others
  7083. * in the list still apply. */
  7084. {
  7085. const char *list = s + sstart;
  7086. unsigned int ll = send - sstart, pos = 0, st, en;
  7087. while (d->nrules < d->rule_cap &&
  7088. sel_list_next(list, ll, &pos, &st, &en)) {
  7089. struct sel_part parts[SEL_PARTS];
  7090. struct br_rule *r;
  7091. char sel[160];
  7092. unsigned int k, off;
  7093. int np, pe;
  7094. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  7095. k++)
  7096. sel[k] = is_space(list[st + k]) ? ' ' :
  7097. list[st + k];
  7098. if (st + k < en)
  7099. continue; /* too long */
  7100. sel[k] = '\0';
  7101. pe = css_pseudo_element(sel, &k);
  7102. if (pe < 0 || !css_selector_supported(sel, k))
  7103. continue;
  7104. np = sel_split(sel, k, parts);
  7105. off = store_attr(d, sel, k, 159);
  7106. if (np <= 0 || !off)
  7107. continue;
  7108. r = &d->rules[d->nrules++];
  7109. r->sel_off = off;
  7110. r->key_off = store_attr(d, sel + parts[np - 1].st,
  7111. parts[np - 1].en -
  7112. parts[np - 1].st, 159);
  7113. r->spec = (unsigned short)selector_specificity(sel, k);
  7114. r->single = np == 1;
  7115. r->pe = (unsigned char)pe;
  7116. r->layer = css_cur_layer;
  7117. r->var_scope = scope_flags;
  7118. r->raw_off = scope_off;
  7119. r->raw_len = (unsigned short)(bend - bstart);
  7120. if (pe)
  7121. d->npe_rules++;
  7122. r->nd = (unsigned char)nd;
  7123. for (k = 0; k < nd; k++)
  7124. r->d[k] = decls[k];
  7125. rule_index_add(d, d->nrules - 1,
  7126. attr_ptr(d, r->key_off),
  7127. s_len(attr_ptr(d, r->key_off)));
  7128. }
  7129. }
  7130. }
  7131. }
  7132. /* Sites often set html { font-size: 62.5% } and then size everything in
  7133. * rem. Detect that root size before parsing the sheet so a rem resolves to
  7134. * the same pixels a real browser would use. */
  7135. static void detect_root_font(const char *s, unsigned int len)
  7136. {
  7137. unsigned int i;
  7138. for (i = 0; i + 4 < len; i++) {
  7139. unsigned int j, end, body, sel, q, h;
  7140. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  7141. int exact;
  7142. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  7143. s[i + 3] == 'l'))
  7144. continue;
  7145. h = i + 4;
  7146. while (h < len && is_space(s[h]))
  7147. h++;
  7148. if (h < len && s[h] != '{' && s[h] != ',' &&
  7149. s[h] != '.' && s[h] != ':' && s[h] != '[')
  7150. continue;
  7151. sel = i;
  7152. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  7153. s[sel - 1] != ';' && s[sel - 1] != ',')
  7154. sel--;
  7155. q = sel;
  7156. while (q < i && is_space(s[q]))
  7157. q++;
  7158. exact = (q == i);
  7159. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  7160. continue;
  7161. j = h;
  7162. if (j >= len || s[j] != '{')
  7163. continue;
  7164. while (j < len && s[j] != '{')
  7165. j++;
  7166. if (j >= len)
  7167. break;
  7168. end = j + 1;
  7169. body = end;
  7170. while (end < len && s[end] != '}')
  7171. end++;
  7172. for (; j + 9 < end; j++) {
  7173. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  7174. s[j + 3] == 't' && s[j + 4] == '-' &&
  7175. s[j + 5] == 's' && s[j + 6] == 'i' &&
  7176. s[j + 7] == 'z' && s[j + 8] == 'e'))
  7177. continue;
  7178. j += 9;
  7179. while (j < end && s[j] != ':')
  7180. j++;
  7181. if (j >= end)
  7182. break;
  7183. j++;
  7184. while (j < end && is_space(s[j]))
  7185. j++;
  7186. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7187. v = v * 10 + (s[j] - '0');
  7188. digits = 1;
  7189. j++;
  7190. }
  7191. if (j < end && s[j] == '.') {
  7192. j++;
  7193. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7194. v = v * 10 + (s[j] - '0');
  7195. frac++;
  7196. j++;
  7197. }
  7198. }
  7199. while (frac < 2) {
  7200. v *= 10;
  7201. frac++;
  7202. }
  7203. while (frac > 2) {
  7204. v /= 10;
  7205. frac--;
  7206. }
  7207. if (!digits)
  7208. break;
  7209. if (j < end && s[j] == '%')
  7210. pct = 1;
  7211. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  7212. px = 1;
  7213. if (pct)
  7214. v = (v * 16 + 5000) / 10000;
  7215. else if (px)
  7216. v /= 100;
  7217. else
  7218. v = v * 16 / 100;
  7219. if (exact && v >= 8 && v <= 24)
  7220. root_font_px = v;
  7221. if (exact)
  7222. return;
  7223. }
  7224. /* html { font: 10px/1 Verdana } also sets the root size. */
  7225. for (j = body; j + 5 <= end; j++) {
  7226. unsigned int p, ts, te;
  7227. if (!ci_eq_n(s + j, "font:", 5))
  7228. continue;
  7229. p = j + 5;
  7230. while (next_token(s, end, &p, &ts, &te)) {
  7231. int fv = parse_font_px(s + ts, te - ts);
  7232. if (fv >= 8 && fv <= 24) {
  7233. if (exact)
  7234. root_font_px = fv;
  7235. break;
  7236. }
  7237. }
  7238. if (exact)
  7239. return;
  7240. break;
  7241. }
  7242. }
  7243. }
  7244. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  7245. {
  7246. scan_font_faces(css, len);
  7247. detect_root_font(css, len);
  7248. css_collect_only = 1;
  7249. parse_css(d, css, len);
  7250. css_collect_only = 0;
  7251. parse_css(d, css, len);
  7252. }
  7253. void br_parse_inline_css(struct br_doc *d)
  7254. {
  7255. if (d->inline_css_len) {
  7256. scan_font_faces(css_store, d->inline_css_len);
  7257. detect_root_font(css_store, d->inline_css_len);
  7258. css_collect_only = 1;
  7259. parse_css(d, css_store, d->inline_css_len);
  7260. css_collect_only = 0;
  7261. parse_css(d, css_store, d->inline_css_len);
  7262. }
  7263. d->inline_css_len = 0;
  7264. }
  7265. void br_reset_styles(struct br_doc *d)
  7266. {
  7267. unsigned int i;
  7268. for (i = 0; i < d->nnode; i++) {
  7269. d->nodes[i].style_valid = 0;
  7270. d->nodes[i].pe_state = 0;
  7271. }
  7272. }
  7273. /* ---- selector matching ---- */
  7274. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  7275. unsigned int clen)
  7276. {
  7277. const char *s = attr_ptr(d, d->nodes[node].class_off);
  7278. unsigned int i = 0;
  7279. while (s[i]) {
  7280. unsigned int st, n;
  7281. while (s[i] && is_space(s[i]))
  7282. i++;
  7283. st = i;
  7284. while (s[i] && !is_space(s[i]))
  7285. i++;
  7286. n = i - st;
  7287. if (n == clen && ci_eq_n(s + st, cls, n))
  7288. return 1;
  7289. }
  7290. return 0;
  7291. }
  7292. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  7293. unsigned int len)
  7294. {
  7295. const char *s = attr_ptr(d, d->nodes[node].id_off);
  7296. unsigned int i;
  7297. for (i = 0; i < len; i++)
  7298. if (s[i] != id[i])
  7299. return 0;
  7300. return s[len] == '\0';
  7301. }
  7302. static int is_ident(char c)
  7303. {
  7304. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  7305. (c >= '0' && c <= '9') || c == '-' || c == '_';
  7306. }
  7307. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  7308. {
  7309. unsigned int n = 0;
  7310. while (lit[n])
  7311. n++;
  7312. return n == nlen && ci_eq_n(name, lit, nlen);
  7313. }
  7314. static int attr_present(struct br_doc *d, unsigned int node,
  7315. const char *name, unsigned int nlen)
  7316. {
  7317. struct br_node *n = &d->nodes[node];
  7318. if (attr_is(name, nlen, "data-collapse-target"))
  7319. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  7320. if (attr_is(name, nlen, "data-collapse-trigger"))
  7321. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  7322. if (attr_is(name, nlen, "is-open"))
  7323. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  7324. if (attr_is(name, nlen, "disabled"))
  7325. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  7326. if (attr_is(name, nlen, "open"))
  7327. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  7328. if (attr_is(name, nlen, "inert"))
  7329. return (n->attr_flags & BR_ATTR_INERT) != 0;
  7330. if (attr_is(name, nlen, "hidden"))
  7331. return (n->flags & BR_NODE_HIDDEN) != 0;
  7332. if (attr_is(name, nlen, "href"))
  7333. return attr_ptr(d, n->href_off)[0] != 0;
  7334. if (attr_is(name, nlen, "class"))
  7335. return attr_ptr(d, n->class_off)[0] != 0;
  7336. if (attr_is(name, nlen, "id"))
  7337. return attr_ptr(d, n->id_off)[0] != 0;
  7338. if (attr_is(name, nlen, "src"))
  7339. return attr_ptr(d, n->src_off)[0] != 0;
  7340. if (attr_is(name, nlen, "alt"))
  7341. return attr_ptr(d, n->alt_off)[0] != 0;
  7342. if (attr_is(name, nlen, "rel"))
  7343. return attr_ptr(d, n->rel_off)[0] != 0;
  7344. return 0;
  7345. }
  7346. /* ---- Selector engine ----
  7347. *
  7348. * One matcher serves the cascade (rule_try), querySelector(All) and
  7349. * Element.matches. A compound is an optional type/universal selector
  7350. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  7351. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  7352. * '~' combinators and is matched right to left with backtracking.
  7353. *
  7354. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  7355. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  7356. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  7357. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  7358. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  7359. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  7360. * and pseudo-elements never match an element; the cascade strips
  7361. * ::before/::after before it gets here. */
  7362. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7363. unsigned int len);
  7364. /* i is at '(' or '['; returns the index just past the matching close. */
  7365. static unsigned int sel_skip_group(const char *s, unsigned int i,
  7366. unsigned int len)
  7367. {
  7368. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  7369. int depth = 0;
  7370. for (; i < len; i++) {
  7371. char c = s[i];
  7372. if (quote) {
  7373. if (c == '\\')
  7374. i++;
  7375. else if (c == quote)
  7376. quote = 0;
  7377. continue;
  7378. }
  7379. if (c == '\\')
  7380. i++;
  7381. else if (c == '"' || c == '\'')
  7382. quote = c;
  7383. else if (c == open)
  7384. depth++;
  7385. else if (c == close && --depth == 0)
  7386. return i + 1;
  7387. }
  7388. return len;
  7389. }
  7390. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  7391. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  7392. char *out, unsigned int cap, unsigned int *olen)
  7393. {
  7394. unsigned int n = 0;
  7395. while (i < len) {
  7396. char c = s[i];
  7397. if (c == '\\' && i + 1 < len) {
  7398. c = s[i + 1];
  7399. i += 2;
  7400. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  7401. i++;
  7402. } else {
  7403. break;
  7404. }
  7405. if (n + 1 < cap)
  7406. out[n++] = c;
  7407. }
  7408. out[n] = '\0';
  7409. *olen = n;
  7410. return i;
  7411. }
  7412. /* Next top-level item of a comma-separated selector list. */
  7413. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  7414. unsigned int *st, unsigned int *en)
  7415. {
  7416. unsigned int i = *pos;
  7417. while (i < len && (is_space(s[i]) || s[i] == ','))
  7418. i++;
  7419. if (i >= len)
  7420. return 0;
  7421. *st = i;
  7422. while (i < len && s[i] != ',') {
  7423. if (s[i] == '(' || s[i] == '[')
  7424. i = sel_skip_group(s, i, len);
  7425. else if (s[i] == '\\' && i + 1 < len)
  7426. i += 2;
  7427. else if (s[i] == '"' || s[i] == '\'') {
  7428. char q = s[i++];
  7429. while (i < len && s[i] != q)
  7430. i++;
  7431. i++;
  7432. } else
  7433. i++;
  7434. }
  7435. if (i > len)
  7436. i = len;
  7437. *en = i;
  7438. while (*en > *st && is_space(s[*en - 1]))
  7439. (*en)--;
  7440. *pos = i;
  7441. return 1;
  7442. }
  7443. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7444. unsigned int len)
  7445. {
  7446. unsigned int pos = 0, st, en;
  7447. while (sel_list_next(s, len, &pos, &st, &en))
  7448. if (sel_match(d, node, s + st, en - st))
  7449. return 1;
  7450. return 0;
  7451. }
  7452. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7453. {
  7454. if (d->nodes[a].tag != d->nodes[b].tag)
  7455. return 0;
  7456. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7457. return 1;
  7458. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7459. attr_ptr(d, d->nodes[b].name_off));
  7460. }
  7461. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7462. {
  7463. unsigned int c, prev = 0;
  7464. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7465. c = d->nodes[c].next)
  7466. if (d->nodes[c].type == BR_NODE_ELEM)
  7467. prev = c;
  7468. return c == node ? prev : 0;
  7469. }
  7470. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7471. {
  7472. unsigned int c;
  7473. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7474. if (d->nodes[c].type == BR_NODE_ELEM)
  7475. return c;
  7476. return 0;
  7477. }
  7478. /* 1-based position among the element siblings (of the same type), counted
  7479. * from the start or, with from_end, from the end. */
  7480. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7481. int from_end)
  7482. {
  7483. unsigned int c;
  7484. int pos = 0, total = 0;
  7485. for (c = d->nodes[d->nodes[node].parent].first; c;
  7486. c = d->nodes[c].next) {
  7487. if (d->nodes[c].type != BR_NODE_ELEM)
  7488. continue;
  7489. if (of_type && !sel_same_type(d, c, node))
  7490. continue;
  7491. total++;
  7492. if (c == node)
  7493. pos = total;
  7494. }
  7495. if (!pos)
  7496. return 0;
  7497. return from_end ? total - pos + 1 : pos;
  7498. }
  7499. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7500. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7501. {
  7502. unsigned int i = 0;
  7503. int sign = 1, num = 0, digits = 0;
  7504. while (len && is_space(s[len - 1]))
  7505. len--;
  7506. while (i < len && is_space(s[i]))
  7507. i++;
  7508. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7509. *a = 2;
  7510. *b = 1;
  7511. return 1;
  7512. }
  7513. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7514. *a = 2;
  7515. *b = 0;
  7516. return 1;
  7517. }
  7518. if (i < len && (s[i] == '+' || s[i] == '-'))
  7519. sign = s[i++] == '-' ? -1 : 1;
  7520. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7521. num = num * 10 + (s[i++] - '0');
  7522. digits = 1;
  7523. }
  7524. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7525. *a = sign * (digits ? num : 1);
  7526. *b = 0;
  7527. i++;
  7528. while (i < len && is_space(s[i]))
  7529. i++;
  7530. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7531. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7532. while (i < len && is_space(s[i]))
  7533. i++;
  7534. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7535. bv = bv * 10 + (s[i++] - '0');
  7536. bd = 1;
  7537. }
  7538. if (!bd)
  7539. return 0;
  7540. *b = bs * bv;
  7541. }
  7542. } else {
  7543. if (!digits)
  7544. return 0;
  7545. *a = 0;
  7546. *b = sign * num;
  7547. }
  7548. return i == len;
  7549. }
  7550. static int sel_nth_ok(int a, int b, int pos)
  7551. {
  7552. if (pos <= 0)
  7553. return 0;
  7554. if (!a)
  7555. return pos == b;
  7556. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7557. }
  7558. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7559. const char *name, int op, const char *want,
  7560. unsigned int wl, int icase)
  7561. {
  7562. const char *have = br_find_attr(d, node, name);
  7563. unsigned int n, i;
  7564. if (!have) {
  7565. /* Boolean attributes the parser keeps as flags. */
  7566. if (!attr_present(d, node, name, text_len(name)))
  7567. return 0;
  7568. have = "";
  7569. }
  7570. if (!op)
  7571. return 1;
  7572. n = text_len(have);
  7573. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7574. switch (op) {
  7575. case '=':
  7576. return n == wl && SEL_EQ(have, want, wl);
  7577. case '^':
  7578. return wl && n >= wl && SEL_EQ(have, want, wl);
  7579. case '$':
  7580. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7581. case '*':
  7582. if (!wl)
  7583. return 0;
  7584. for (i = 0; i + wl <= n; i++)
  7585. if (SEL_EQ(have + i, want, wl))
  7586. return 1;
  7587. return 0;
  7588. case '|':
  7589. return n >= wl && SEL_EQ(have, want, wl) &&
  7590. (n == wl || have[wl] == '-');
  7591. case '~':
  7592. if (!wl)
  7593. return 0;
  7594. for (i = 0; i < n; ) {
  7595. unsigned int st;
  7596. while (i < n && is_space(have[i]))
  7597. i++;
  7598. st = i;
  7599. while (i < n && !is_space(have[i]))
  7600. i++;
  7601. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7602. return 1;
  7603. }
  7604. return 0;
  7605. }
  7606. #undef SEL_EQ
  7607. return 0;
  7608. }
  7609. static int sel_empty(struct br_doc *d, unsigned int node)
  7610. {
  7611. unsigned int c;
  7612. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7613. if (d->nodes[c].type == BR_NODE_ELEM)
  7614. return 0;
  7615. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7616. return 0;
  7617. }
  7618. return 1;
  7619. }
  7620. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7621. unsigned int len)
  7622. {
  7623. unsigned int c;
  7624. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7625. if (d->nodes[c].type != BR_NODE_ELEM)
  7626. continue;
  7627. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7628. return 1;
  7629. }
  7630. return 0;
  7631. }
  7632. /* :has() with an optional leading relative combinator per list item. */
  7633. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7634. unsigned int len)
  7635. {
  7636. unsigned int pos = 0, st, en, c;
  7637. while (sel_list_next(s, len, &pos, &st, &en)) {
  7638. char comb = s[st];
  7639. if (comb == '>' || comb == '+' || comb == '~') {
  7640. st++;
  7641. while (st < en && is_space(s[st]))
  7642. st++;
  7643. } else {
  7644. comb = ' ';
  7645. }
  7646. if (st >= en)
  7647. continue;
  7648. if (comb == '>') {
  7649. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7650. if (d->nodes[c].type == BR_NODE_ELEM &&
  7651. sel_match(d, c, s + st, en - st))
  7652. return 1;
  7653. } else if (comb == '+') {
  7654. c = sel_next_elem(d, node);
  7655. if (c && sel_match(d, c, s + st, en - st))
  7656. return 1;
  7657. } else if (comb == '~') {
  7658. for (c = sel_next_elem(d, node); c;
  7659. c = sel_next_elem(d, c))
  7660. if (sel_match(d, c, s + st, en - st))
  7661. return 1;
  7662. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7663. return 1;
  7664. }
  7665. }
  7666. return 0;
  7667. }
  7668. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7669. {
  7670. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7671. }
  7672. /* Pseudo-class names the engine models; anything else never matches and
  7673. * makes a top-level rule unsupported (see css_selector_supported()). */
  7674. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7675. {
  7676. static const char *const plain[] = {
  7677. "root", "link", "any-link", "visited", "first-child",
  7678. "last-child", "only-child", "first-of-type", "last-of-type",
  7679. "only-of-type", "empty", "checked", "disabled", "enabled",
  7680. "required", "optional",
  7681. };
  7682. static const char *const func[] = {
  7683. "not", "is", "where", "matches", "-webkit-any", "has",
  7684. "nth-child", "nth-last-child", "nth-of-type",
  7685. "nth-last-of-type",
  7686. };
  7687. unsigned int k;
  7688. if (fn) {
  7689. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7690. if (sel_name_is(name, nl, func[k]))
  7691. return 1;
  7692. return 0;
  7693. }
  7694. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7695. if (sel_name_is(name, nl, plain[k]))
  7696. return 1;
  7697. return 0;
  7698. }
  7699. static int sel_form_control(struct br_doc *d, unsigned int node)
  7700. {
  7701. unsigned int t = d->nodes[node].tag;
  7702. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7703. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7704. t == BR_TAG_OPTION;
  7705. }
  7706. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7707. unsigned int nl, const char *arg, unsigned int al,
  7708. int fn)
  7709. {
  7710. struct br_node *n = &d->nodes[node];
  7711. int a, b;
  7712. if (fn) {
  7713. if (sel_name_is(name, nl, "not"))
  7714. return !sel_list_match(d, node, arg, al);
  7715. if (sel_name_is(name, nl, "is") ||
  7716. sel_name_is(name, nl, "where") ||
  7717. sel_name_is(name, nl, "matches") ||
  7718. sel_name_is(name, nl, "-webkit-any"))
  7719. return sel_list_match(d, node, arg, al);
  7720. if (sel_name_is(name, nl, "has"))
  7721. return sel_has(d, node, arg, al);
  7722. if (!sel_nth_parse(arg, al, &a, &b))
  7723. return 0;
  7724. if (sel_name_is(name, nl, "nth-child"))
  7725. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7726. if (sel_name_is(name, nl, "nth-last-child"))
  7727. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7728. if (sel_name_is(name, nl, "nth-of-type"))
  7729. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7730. if (sel_name_is(name, nl, "nth-last-of-type"))
  7731. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7732. return 0;
  7733. }
  7734. if (sel_name_is(name, nl, "root"))
  7735. return n->tag == BR_TAG_HTML;
  7736. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7737. /* No history state: every link is unvisited. */
  7738. return n->tag == BR_TAG_A && n->href_off;
  7739. if (sel_name_is(name, nl, "first-child"))
  7740. return sel_sibling_pos(d, node, 0, 0) == 1;
  7741. if (sel_name_is(name, nl, "last-child"))
  7742. return sel_sibling_pos(d, node, 0, 1) == 1;
  7743. if (sel_name_is(name, nl, "only-child"))
  7744. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7745. sel_sibling_pos(d, node, 0, 1) == 1;
  7746. if (sel_name_is(name, nl, "first-of-type"))
  7747. return sel_sibling_pos(d, node, 1, 0) == 1;
  7748. if (sel_name_is(name, nl, "last-of-type"))
  7749. return sel_sibling_pos(d, node, 1, 1) == 1;
  7750. if (sel_name_is(name, nl, "only-of-type"))
  7751. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7752. sel_sibling_pos(d, node, 1, 1) == 1;
  7753. if (sel_name_is(name, nl, "empty"))
  7754. return sel_empty(d, node);
  7755. if (sel_name_is(name, nl, "checked"))
  7756. return br_find_attr(d, node, "checked") != 0 ||
  7757. (n->tag == BR_TAG_OPTION &&
  7758. br_find_attr(d, node, "selected") != 0);
  7759. if (sel_name_is(name, nl, "disabled"))
  7760. return sel_form_control(d, node) &&
  7761. (n->attr_flags & BR_ATTR_DISABLED);
  7762. if (sel_name_is(name, nl, "enabled"))
  7763. return sel_form_control(d, node) &&
  7764. !(n->attr_flags & BR_ATTR_DISABLED);
  7765. if (sel_name_is(name, nl, "required"))
  7766. return sel_form_control(d, node) &&
  7767. br_find_attr(d, node, "required") != 0;
  7768. if (sel_name_is(name, nl, "optional"))
  7769. return sel_form_control(d, node) &&
  7770. !br_find_attr(d, node, "required");
  7771. return 0;
  7772. }
  7773. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7774. unsigned int len)
  7775. {
  7776. char buf[64];
  7777. unsigned int i = 0, bl;
  7778. if (d->nodes[node].type != BR_NODE_ELEM)
  7779. return 0;
  7780. if (i < len && s[i] == '*') {
  7781. i++;
  7782. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7783. unsigned int tag;
  7784. i = sel_ident(s, i, len, buf, 32, &bl);
  7785. tag = br_tag_from_name(buf);
  7786. if (tag == BR_TAG_UNKNOWN) {
  7787. const char *raw = attr_ptr(d,
  7788. d->nodes[node].name_off);
  7789. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7790. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7791. return 0;
  7792. } else if (tag != d->nodes[node].tag) {
  7793. return 0;
  7794. }
  7795. }
  7796. if (i >= len)
  7797. return i > 0;
  7798. while (i < len) {
  7799. char c = s[i];
  7800. if (c == '.' || c == '#') {
  7801. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7802. if (!bl)
  7803. return 0;
  7804. if (c == '.' ? !class_has(d, node, buf, bl) :
  7805. !id_eq(d, node, buf, bl))
  7806. return 0;
  7807. } else if (c == '[') {
  7808. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7809. unsigned int vs = 0, vl = 0, k;
  7810. char name[32];
  7811. int op = 0, icase = 0;
  7812. while (j < e && is_space(s[j]))
  7813. j++;
  7814. k = 0;
  7815. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7816. k + 1 < sizeof(name)) {
  7817. char ch = s[j++];
  7818. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7819. (char)(ch + 32) : ch;
  7820. }
  7821. name[k] = '\0';
  7822. if (!k)
  7823. return 0;
  7824. while (j < e && is_space(s[j]))
  7825. j++;
  7826. if (j < e && s[j] == '=') {
  7827. op = '=';
  7828. j++;
  7829. } else if (j + 1 < e && s[j + 1] == '=' &&
  7830. (s[j] == '~' || s[j] == '|' ||
  7831. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7832. op = s[j];
  7833. j += 2;
  7834. }
  7835. if (op) {
  7836. while (j < e && is_space(s[j]))
  7837. j++;
  7838. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7839. char q = s[j++];
  7840. vs = j;
  7841. while (j < e && s[j] != q)
  7842. j++;
  7843. vl = j - vs;
  7844. j++;
  7845. } else {
  7846. vs = j;
  7847. while (j < e && s[j] != ']' &&
  7848. !is_space(s[j]))
  7849. j++;
  7850. vl = j - vs;
  7851. }
  7852. while (j < e && is_space(s[j]))
  7853. j++;
  7854. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7855. icase = 1;
  7856. }
  7857. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7858. icase))
  7859. return 0;
  7860. i = e;
  7861. } else if (c == ':') {
  7862. unsigned int st, nl, as = 0, al = 0;
  7863. int fn = 0;
  7864. if (i + 1 < len && s[i + 1] == ':')
  7865. return 0; /* pseudo-element */
  7866. st = ++i;
  7867. while (i < len && is_ident(s[i]))
  7868. i++;
  7869. nl = i - st;
  7870. if (i < len && s[i] == '(') {
  7871. unsigned int e = sel_skip_group(s, i, len);
  7872. fn = 1;
  7873. as = i + 1;
  7874. al = e > as ? e - as - 1 : 0;
  7875. i = e;
  7876. }
  7877. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7878. return 0;
  7879. } else {
  7880. return 0;
  7881. }
  7882. }
  7883. return 1;
  7884. }
  7885. /* Split a complex selector into compounds; returns the count or -1. */
  7886. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7887. {
  7888. unsigned int i = 0;
  7889. int n = 0;
  7890. char comb = 0;
  7891. while (i < len && is_space(s[i]))
  7892. i++;
  7893. while (i < len) {
  7894. unsigned int st = i;
  7895. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7896. s[i] != '+' && s[i] != '~') {
  7897. if (s[i] == '(' || s[i] == '[')
  7898. i = sel_skip_group(s, i, len);
  7899. else if (s[i] == '\\' && i + 1 < len)
  7900. i += 2;
  7901. else
  7902. i++;
  7903. }
  7904. if (i == st || n == SEL_PARTS)
  7905. return -1;
  7906. p[n].st = (unsigned short)st;
  7907. p[n].en = (unsigned short)(i > len ? len : i);
  7908. p[n].comb = comb;
  7909. n++;
  7910. comb = 0;
  7911. while (i < len && is_space(s[i])) {
  7912. comb = ' ';
  7913. i++;
  7914. }
  7915. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7916. comb = s[i++];
  7917. while (i < len && is_space(s[i]))
  7918. i++;
  7919. if (i >= len)
  7920. return -1;
  7921. }
  7922. }
  7923. return n;
  7924. }
  7925. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7926. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7927. * parent when the page starts with <html> (node 0 is that element; see
  7928. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7929. static int sel_parent(struct br_doc *d, unsigned int node)
  7930. {
  7931. unsigned int p = d->nodes[node].parent;
  7932. if (p)
  7933. return (int)p;
  7934. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7935. d->nodes[0].tag == BR_TAG_HTML &&
  7936. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7937. return 0;
  7938. return -1;
  7939. }
  7940. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7941. const struct sel_part *p, int k)
  7942. {
  7943. unsigned int c, pl;
  7944. const char *ps;
  7945. if (k == 0)
  7946. return 1;
  7947. ps = s + p[k - 1].st;
  7948. pl = p[k - 1].en - p[k - 1].st;
  7949. switch (p[k].comb) {
  7950. case '>': {
  7951. int pc = sel_parent(d, node);
  7952. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7953. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7954. }
  7955. case '+':
  7956. c = sel_prev_elem(d, node);
  7957. return c && compound_match(d, c, ps, pl) &&
  7958. sel_from(d, c, s, p, k - 1);
  7959. case '~':
  7960. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7961. if (compound_match(d, c, ps, pl) &&
  7962. sel_from(d, c, s, p, k - 1))
  7963. return 1;
  7964. return 0;
  7965. default: {
  7966. int pc;
  7967. for (pc = sel_parent(d, node); pc >= 0;
  7968. pc = sel_parent(d, (unsigned int)pc))
  7969. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7970. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7971. return 1;
  7972. return 0;
  7973. }
  7974. }
  7975. }
  7976. /* Match one complex selector. right_done: the caller already matched the
  7977. * rightmost compound (the rule index key). */
  7978. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7979. unsigned int len, int right_done)
  7980. {
  7981. struct sel_part parts[SEL_PARTS];
  7982. int np = sel_split(s, len, parts);
  7983. if (np <= 0)
  7984. return 0;
  7985. if (!right_done &&
  7986. !compound_match(d, node, s + parts[np - 1].st,
  7987. parts[np - 1].en - parts[np - 1].st))
  7988. return 0;
  7989. return sel_from(d, node, s, parts, np - 1);
  7990. }
  7991. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7992. unsigned int len)
  7993. {
  7994. return selector_match(d, node, s, len, 0);
  7995. }
  7996. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7997. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7998. * and :where counts nothing. */
  7999. static unsigned int selector_specificity(const char *s, unsigned int len)
  8000. {
  8001. unsigned int i = 0, spec = 0, bl;
  8002. char buf[64];
  8003. while (i < len) {
  8004. char c = s[i];
  8005. if (c == '#' || c == '.') {
  8006. spec += c == '#' ? 100 : 10;
  8007. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  8008. } else if (c == '[') {
  8009. spec += 10;
  8010. i = sel_skip_group(s, i, len);
  8011. } else if (c == ':') {
  8012. unsigned int st, nl;
  8013. if (i + 1 < len && s[i + 1] == ':') {
  8014. spec += 1;
  8015. i += 2;
  8016. while (i < len && is_ident(s[i]))
  8017. i++;
  8018. continue;
  8019. }
  8020. st = ++i;
  8021. while (i < len && is_ident(s[i]))
  8022. i++;
  8023. nl = i - st;
  8024. if (i < len && s[i] == '(') {
  8025. unsigned int e = sel_skip_group(s, i, len);
  8026. if (sel_name_is(s + st, nl, "where")) {
  8027. /* zero */
  8028. } else if (sel_name_is(s + st, nl, "not") ||
  8029. sel_name_is(s + st, nl, "is") ||
  8030. sel_name_is(s + st, nl, "has") ||
  8031. sel_name_is(s + st, nl, "matches") ||
  8032. sel_name_is(s + st, nl,
  8033. "-webkit-any")) {
  8034. unsigned int pos = 0, as, ae, best = 0;
  8035. unsigned int al = e > i + 1 ?
  8036. e - i - 2 : 0;
  8037. while (sel_list_next(s + i + 1, al,
  8038. &pos, &as, &ae)) {
  8039. unsigned int v =
  8040. selector_specificity(
  8041. s + i + 1 + as,
  8042. ae - as);
  8043. if (v > best)
  8044. best = v;
  8045. }
  8046. spec += best;
  8047. } else {
  8048. spec += 10;
  8049. }
  8050. i = e;
  8051. } else if (sel_name_is(s + st, nl, "before") ||
  8052. sel_name_is(s + st, nl, "after")) {
  8053. spec += 1;
  8054. } else {
  8055. spec += 10;
  8056. }
  8057. } else if (is_ident(c) || c == '\\') {
  8058. spec += 1;
  8059. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  8060. } else {
  8061. i++;
  8062. }
  8063. }
  8064. return spec;
  8065. }
  8066. /* Cascade filter: drop a selector the engine cannot match (user-state
  8067. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  8068. * ones the caller handles), so it does not waste a rule slot. Arguments of
  8069. * functional pseudo-classes are not checked: an unknown pseudo-class there
  8070. * is simply false, which is right for :not(:hover). */
  8071. static int css_selector_supported(const char *s, unsigned int len)
  8072. {
  8073. struct sel_part parts[SEL_PARTS];
  8074. unsigned int i = 0;
  8075. if (sel_split(s, len, parts) <= 0)
  8076. return 0;
  8077. while (i < len) {
  8078. if (s[i] == '(' || s[i] == '[') {
  8079. i = sel_skip_group(s, i, len);
  8080. } else if (s[i] == '\\') {
  8081. i += 2;
  8082. } else if (s[i] == ':') {
  8083. unsigned int st, nl;
  8084. int fn;
  8085. if (i + 1 < len && s[i + 1] == ':')
  8086. return 0;
  8087. st = ++i;
  8088. while (i < len && is_ident(s[i]))
  8089. i++;
  8090. nl = i - st;
  8091. fn = i < len && s[i] == '(';
  8092. if (!sel_pseudo_known(s + st, nl, fn))
  8093. return 0;
  8094. } else {
  8095. i++;
  8096. }
  8097. }
  8098. return 1;
  8099. }
  8100. #define RULE_MATCH_MAX 512
  8101. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  8102. static int cascade_pe;
  8103. struct rule_match {
  8104. unsigned short idx;
  8105. short spec; /* precedence of normal declarations */
  8106. short spec_imp; /* precedence of !important declarations */
  8107. };
  8108. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  8109. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  8110. * unlayered rules, then the style attribute. !important reverses the
  8111. * layer order: unlayered lowest, the first layer highest, the style
  8112. * attribute above all. Both fit a short. */
  8113. #define CASCADE_BAND 512
  8114. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  8115. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  8116. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  8117. {
  8118. unsigned int band;
  8119. if (spec >= CASCADE_BAND)
  8120. spec = CASCADE_BAND - 1;
  8121. if (important)
  8122. band = layer ? CASCADE_UNLAYERED - layer : 0;
  8123. else
  8124. band = layer ? layer : CASCADE_UNLAYERED;
  8125. return (int)(band * CASCADE_BAND + spec);
  8126. }
  8127. static struct rule_match rule_matches[RULE_MATCH_MAX];
  8128. static unsigned int nrule_matches;
  8129. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  8130. {
  8131. struct br_rule *r = &d->rules[idx];
  8132. const char *key = attr_ptr(d, r->key_off);
  8133. int spec, key_ok = 0;
  8134. if (r->pe != cascade_pe)
  8135. return;
  8136. if (key[0]) {
  8137. if (!compound_match(d, node, key, s_len(key)))
  8138. return;
  8139. key_ok = 1;
  8140. }
  8141. if (!(r->single && key_ok) &&
  8142. !selector_match(d, node, attr_ptr(d, r->sel_off),
  8143. s_len(attr_ptr(d, r->sel_off)), key_ok))
  8144. return;
  8145. spec = (int)r->spec;
  8146. if (nrule_matches < RULE_MATCH_MAX) {
  8147. rule_matches[nrule_matches].idx = (unsigned short)idx;
  8148. rule_matches[nrule_matches].spec =
  8149. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  8150. rule_matches[nrule_matches].spec_imp =
  8151. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  8152. nrule_matches++;
  8153. }
  8154. }
  8155. static void rule_scan_list(struct br_doc *d, unsigned int node,
  8156. unsigned short head)
  8157. {
  8158. while (head != 0xFFFF) {
  8159. rule_try(d, node, head);
  8160. head = d->rule_next[head];
  8161. }
  8162. }
  8163. static void rule_scan_class(struct br_doc *d, unsigned int node,
  8164. const char *cls, unsigned int len)
  8165. {
  8166. if (len)
  8167. rule_scan_list(d, node,
  8168. d->rule_bucket[rule_hash(cls, len)]);
  8169. }
  8170. static void rule_collect(struct br_doc *d, unsigned int node)
  8171. {
  8172. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  8173. const char *id = attr_ptr(d, d->nodes[node].id_off);
  8174. unsigned int tag = d->nodes[node].tag;
  8175. unsigned int i;
  8176. nrule_matches = 0;
  8177. rule_scan_list(d, node, d->rule_any);
  8178. if (tag < BR_TAG_MAX)
  8179. rule_scan_list(d, node, d->rule_tag[tag]);
  8180. if (id[0])
  8181. rule_scan_list(d, node,
  8182. d->rule_bucket[rule_hash(id, s_len(id))]);
  8183. i = 0;
  8184. while (cls[i]) {
  8185. unsigned int st, n;
  8186. while (cls[i] && is_space(cls[i]))
  8187. i++;
  8188. st = i;
  8189. while (cls[i] && !is_space(cls[i]))
  8190. i++;
  8191. n = i - st;
  8192. if (n)
  8193. rule_scan_class(d, node, cls + st, n);
  8194. }
  8195. /* Rules must cascade in document order even though the index visits
  8196. * them in bucket order. */
  8197. for (i = 1; i < nrule_matches; i++) {
  8198. struct rule_match m = rule_matches[i];
  8199. unsigned int j = i;
  8200. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  8201. rule_matches[j] = rule_matches[j - 1];
  8202. j--;
  8203. }
  8204. rule_matches[j] = m;
  8205. }
  8206. }
  8207. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  8208. int font_px)
  8209. {
  8210. if (*kind == 4) {
  8211. *val = (unsigned short)(*val * font_px / 100);
  8212. *kind = 1;
  8213. } else if (*kind == 5) {
  8214. *val = (unsigned short)(*val * root_font_px / 100);
  8215. *kind = 1;
  8216. }
  8217. }
  8218. static void resolve_size_units(struct br_style *st)
  8219. {
  8220. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  8221. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  8222. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  8223. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  8224. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  8225. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  8226. }
  8227. static void set_border_side_w(struct br_style *st, int side, int v)
  8228. {
  8229. if (v < 0)
  8230. v = 0;
  8231. if (v > 32)
  8232. v = 32;
  8233. st->border_w[side] = (unsigned char)v;
  8234. if (st->border_w[side] && !st->border_c[side])
  8235. st->border_c[side] = st->color;
  8236. }
  8237. static void set_border_side_c(struct br_style *st, int side, int v)
  8238. {
  8239. st->border_c[side] = (unsigned short)v;
  8240. }
  8241. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  8242. {
  8243. switch (dcl->prop) {
  8244. case PROP_COLOR:
  8245. st->color = (unsigned short)dcl->val;
  8246. break;
  8247. case PROP_BG:
  8248. st->bg = (unsigned short)dcl->val;
  8249. st->has_bg = 1;
  8250. st->bg_set = 1;
  8251. break;
  8252. case PROP_BG_NONE:
  8253. st->has_bg = 0;
  8254. st->bg_set = 1;
  8255. break;
  8256. case PROP_BG_IMAGE:
  8257. st->bg_image = (unsigned short)dcl->val;
  8258. break;
  8259. case PROP_BG_REPEAT:
  8260. st->bg_repeat = (unsigned char)dcl->val;
  8261. break;
  8262. case PROP_BG_POS_X_KIND:
  8263. st->bg_pos_x_kind = (unsigned char)dcl->val;
  8264. break;
  8265. case PROP_BG_POS_X:
  8266. st->bg_pos_x = (signed short)dcl->val;
  8267. break;
  8268. case PROP_BG_POS_Y_KIND:
  8269. st->bg_pos_y_kind = (unsigned char)dcl->val;
  8270. break;
  8271. case PROP_BG_POS_Y:
  8272. st->bg_pos_y = (signed short)dcl->val;
  8273. break;
  8274. case PROP_BG_SIZE_KIND:
  8275. st->bg_size_kind = (unsigned char)dcl->val;
  8276. break;
  8277. case PROP_BG_SIZE_W:
  8278. st->bg_size_w = (unsigned short)dcl->val;
  8279. break;
  8280. case PROP_BG_SIZE_H:
  8281. st->bg_size_h = (unsigned short)dcl->val;
  8282. break;
  8283. case PROP_FONT_PX:
  8284. st->font_raw = (unsigned short)dcl->val;
  8285. break;
  8286. case PROP_FONT_UNIT:
  8287. st->font_unit = (unsigned char)dcl->val;
  8288. break;
  8289. case PROP_BOLD:
  8290. st->bold = (unsigned char)(dcl->val != 0);
  8291. break;
  8292. case PROP_ITALIC:
  8293. st->italic = (unsigned char)(dcl->val != 0);
  8294. break;
  8295. case PROP_MONO:
  8296. st->mono = (unsigned char)(dcl->val != 0);
  8297. break;
  8298. case PROP_FONT_FACE:
  8299. st->font_face = (unsigned char)dcl->val;
  8300. break;
  8301. case PROP_ALIGN:
  8302. st->align = (unsigned char)dcl->val;
  8303. break;
  8304. case PROP_UNDERLINE:
  8305. st->underline = (unsigned char)(dcl->val != 0);
  8306. break;
  8307. case PROP_STRIKE:
  8308. st->strike = (unsigned char)(dcl->val != 0);
  8309. break;
  8310. case PROP_OVERLINE:
  8311. st->overline = (unsigned char)(dcl->val != 0);
  8312. break;
  8313. case PROP_DISPLAY:
  8314. st->display = (unsigned char)(dcl->val &
  8315. ~BR_DISPLAY_INLINE_FLAG);
  8316. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  8317. st->list_item = 0;
  8318. break;
  8319. case PROP_VIS_HIDDEN:
  8320. st->vis_hidden = (unsigned char)(dcl->val != 0);
  8321. break;
  8322. case PROP_OPACITY_HIDDEN:
  8323. st->opacity = (unsigned char)(255 - (dcl->val & 255));
  8324. st->opacity_hidden = (unsigned char)(st->opacity == 0);
  8325. break;
  8326. case PROP_FILL_MODE:
  8327. st->fill_mode = (unsigned char)dcl->val;
  8328. break;
  8329. case PROP_FILL:
  8330. st->fill = (unsigned short)dcl->val;
  8331. break;
  8332. case PROP_STROKE_MODE:
  8333. st->stroke_mode = (unsigned char)dcl->val;
  8334. break;
  8335. case PROP_STROKE:
  8336. st->stroke = (unsigned short)dcl->val;
  8337. break;
  8338. case PROP_STROKE_W:
  8339. st->stroke_w = (unsigned short)dcl->val;
  8340. break;
  8341. case PROP_FILL_RULE:
  8342. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  8343. break;
  8344. case PROP_OVERFLOW_HIDDEN:
  8345. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  8346. break;
  8347. case PROP_MT: st->mt = dcl->val; break;
  8348. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  8349. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  8350. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  8351. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  8352. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  8353. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  8354. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  8355. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  8356. case PROP_MR: st->mr = dcl->val; break;
  8357. case PROP_MB: st->mb = dcl->val; break;
  8358. case PROP_ML: st->ml = dcl->val; break;
  8359. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  8360. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  8361. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  8362. break;
  8363. case PROP_PT: st->pt = dcl->val; break;
  8364. case PROP_PR: st->pr = dcl->val; break;
  8365. case PROP_PB: st->pb = dcl->val; break;
  8366. case PROP_PL: st->pl = dcl->val; break;
  8367. case PROP_MT_U:
  8368. case PROP_MR_U:
  8369. case PROP_MB_U:
  8370. case PROP_ML_U:
  8371. case PROP_PT_U:
  8372. case PROP_PR_U:
  8373. case PROP_PB_U:
  8374. case PROP_PL_U:
  8375. st->mp_unit[dcl->prop - PROP_MT_U] =
  8376. (unsigned char)dcl->val;
  8377. break;
  8378. case PROP_BORDER_W:
  8379. set_border_side_w(st, 0, dcl->val);
  8380. set_border_side_w(st, 1, dcl->val);
  8381. set_border_side_w(st, 2, dcl->val);
  8382. set_border_side_w(st, 3, dcl->val);
  8383. break;
  8384. case PROP_BORDER_COLOR:
  8385. set_border_side_c(st, 0, dcl->val);
  8386. set_border_side_c(st, 1, dcl->val);
  8387. set_border_side_c(st, 2, dcl->val);
  8388. set_border_side_c(st, 3, dcl->val);
  8389. break;
  8390. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  8391. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  8392. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  8393. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  8394. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  8395. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  8396. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  8397. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  8398. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  8399. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  8400. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  8401. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  8402. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  8403. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  8404. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  8405. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  8406. case PROP_RADIUS_TL:
  8407. case PROP_RADIUS_TR:
  8408. case PROP_RADIUS_BR:
  8409. case PROP_RADIUS_BL:
  8410. st->radius[dcl->prop - PROP_RADIUS_TL] =
  8411. (unsigned char)dcl->val;
  8412. break;
  8413. case PROP_LIST_NONE:
  8414. st->no_bullet = (unsigned char)(dcl->val != 0);
  8415. break;
  8416. case PROP_LIST_STYLE:
  8417. st->list_style = (unsigned char)dcl->val;
  8418. break;
  8419. case PROP_CONTENT:
  8420. st->content = (unsigned short)dcl->val;
  8421. break;
  8422. case PROP_ASPECT_W:
  8423. st->aspect_w = (unsigned short)dcl->val;
  8424. break;
  8425. case PROP_ASPECT_H:
  8426. st->aspect_h = (unsigned short)dcl->val;
  8427. break;
  8428. case PROP_NOWRAP:
  8429. st->nowrap = (unsigned char)(dcl->val != 0);
  8430. break;
  8431. case PROP_WIDTH:
  8432. st->width = (unsigned short)dcl->val;
  8433. break;
  8434. case PROP_WIDTH_KIND:
  8435. st->width_kind = (unsigned char)dcl->val;
  8436. break;
  8437. case PROP_WIDTH_CALC_PCT:
  8438. st->width_calc_pct = dcl->val;
  8439. break;
  8440. case PROP_WIDTH_CALC_PX:
  8441. st->width_calc_px = dcl->val;
  8442. break;
  8443. case PROP_MAXW:
  8444. st->max_width = (unsigned short)dcl->val;
  8445. break;
  8446. case PROP_MAXW_KIND:
  8447. st->max_width_kind = (unsigned char)dcl->val;
  8448. break;
  8449. case PROP_MAXW_CALC_PCT:
  8450. st->max_width_calc_pct = (signed short)dcl->val;
  8451. break;
  8452. case PROP_MAXW_CALC_PX:
  8453. st->max_width_calc_px = (signed short)dcl->val;
  8454. break;
  8455. case PROP_MINW:
  8456. st->min_width = (unsigned short)dcl->val;
  8457. break;
  8458. case PROP_MINW_KIND:
  8459. st->min_width_kind = (unsigned char)dcl->val;
  8460. break;
  8461. case PROP_MINW_CALC_PCT:
  8462. st->min_width_calc_pct = (signed short)dcl->val;
  8463. break;
  8464. case PROP_MINW_CALC_PX:
  8465. st->min_width_calc_px = (signed short)dcl->val;
  8466. break;
  8467. case PROP_HEIGHT:
  8468. st->height = (unsigned short)dcl->val;
  8469. break;
  8470. case PROP_HEIGHT_KIND:
  8471. st->height_kind = (unsigned char)dcl->val;
  8472. break;
  8473. case PROP_HEIGHT_CALC_PCT:
  8474. st->height_calc_pct = (signed short)dcl->val;
  8475. break;
  8476. case PROP_HEIGHT_CALC_PX:
  8477. st->height_calc_px = (signed short)dcl->val;
  8478. break;
  8479. case PROP_MINH:
  8480. st->min_height = (unsigned short)dcl->val;
  8481. break;
  8482. case PROP_MINH_KIND:
  8483. st->min_height_kind = (unsigned char)dcl->val;
  8484. break;
  8485. case PROP_MINH_CALC_PCT:
  8486. st->min_height_calc_pct = (signed short)dcl->val;
  8487. break;
  8488. case PROP_MINH_CALC_PX:
  8489. st->min_height_calc_px = (signed short)dcl->val;
  8490. break;
  8491. case PROP_MAXH:
  8492. st->max_height = (unsigned short)dcl->val;
  8493. break;
  8494. case PROP_MAXH_KIND:
  8495. st->max_height_kind = (unsigned char)dcl->val;
  8496. break;
  8497. case PROP_MAXH_CALC_PCT:
  8498. st->max_height_calc_pct = (signed short)dcl->val;
  8499. break;
  8500. case PROP_MAXH_CALC_PX:
  8501. st->max_height_calc_px = (signed short)dcl->val;
  8502. break;
  8503. case PROP_LH_PCT:
  8504. st->line_h_pct = (unsigned char)dcl->val;
  8505. break;
  8506. case PROP_LH_PX:
  8507. st->line_h_px = (unsigned short)dcl->val;
  8508. break;
  8509. case PROP_UPPER:
  8510. st->uppercase = (unsigned char)(dcl->val != 0);
  8511. break;
  8512. case PROP_CURSOR:
  8513. st->cursor = (unsigned char)dcl->val;
  8514. break;
  8515. case PROP_PE_NONE:
  8516. st->pe_none = (unsigned char)(dcl->val != 0);
  8517. break;
  8518. case PROP_TX_PCT:
  8519. st->tx_pct = (unsigned char)(dcl->val != 0);
  8520. break;
  8521. case PROP_TY_PCT:
  8522. st->ty_pct = (unsigned char)(dcl->val != 0);
  8523. break;
  8524. case PROP_TR_MASK:
  8525. st->tr_mask = (unsigned char)dcl->val;
  8526. break;
  8527. case PROP_TR_MS:
  8528. st->tr_ms = (unsigned short)dcl->val;
  8529. break;
  8530. case PROP_TR_EASE:
  8531. st->tr_ease = (unsigned char)dcl->val;
  8532. break;
  8533. case PROP_FLOAT:
  8534. st->float_left = (unsigned char)dcl->val;
  8535. break;
  8536. case PROP_CLEAR:
  8537. st->clear = (unsigned char)dcl->val;
  8538. break;
  8539. case PROP_FLEX_GROW:
  8540. st->flex_grow = (unsigned char)dcl->val;
  8541. break;
  8542. case PROP_FLEX_COL:
  8543. st->flex_col = (unsigned char)(dcl->val != 0);
  8544. break;
  8545. case PROP_FLEX_WRAP:
  8546. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8547. break;
  8548. case PROP_JUSTIFY:
  8549. st->justify = (unsigned char)dcl->val;
  8550. break;
  8551. case PROP_ALIGN_ITEMS:
  8552. st->align_items = (unsigned char)dcl->val;
  8553. break;
  8554. case PROP_ALIGN_SELF:
  8555. st->align_self = (unsigned char)dcl->val;
  8556. break;
  8557. case PROP_GRID_NCOLS:
  8558. st->grid_ncols = (unsigned char)dcl->val;
  8559. break;
  8560. case PROP_GRID_GAP:
  8561. st->grid_gap = (unsigned char)dcl->val;
  8562. break;
  8563. case PROP_GRID_SLOT:
  8564. st->grid_slot = (unsigned char)dcl->val;
  8565. break;
  8566. case PROP_GRID_AUTO:
  8567. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8568. break;
  8569. case PROP_POS:
  8570. st->pos = (unsigned char)dcl->val;
  8571. break;
  8572. case PROP_LEFT:
  8573. st->left = dcl->val;
  8574. break;
  8575. case PROP_TOP:
  8576. st->top = dcl->val;
  8577. break;
  8578. case PROP_RIGHT:
  8579. st->right = dcl->val;
  8580. break;
  8581. case PROP_BOTTOM:
  8582. st->bottom = dcl->val;
  8583. break;
  8584. case PROP_LEFT_SET:
  8585. st->left_set = (unsigned char)(dcl->val != 0);
  8586. break;
  8587. case PROP_TOP_SET:
  8588. st->top_set = (unsigned char)(dcl->val != 0);
  8589. break;
  8590. case PROP_RIGHT_SET:
  8591. st->right_set = (unsigned char)(dcl->val != 0);
  8592. break;
  8593. case PROP_BOTTOM_SET:
  8594. st->bottom_set = (unsigned char)(dcl->val != 0);
  8595. break;
  8596. case PROP_ZINDEX:
  8597. st->z_index = dcl->val;
  8598. break;
  8599. case PROP_ZSET:
  8600. st->z_set = (unsigned char)(dcl->val != 0);
  8601. break;
  8602. case PROP_TRANSFORM_X:
  8603. st->tx = dcl->val;
  8604. break;
  8605. case PROP_TRANSFORM_Y:
  8606. st->ty = dcl->val;
  8607. break;
  8608. case PROP_TRANSFORM_SET:
  8609. st->transform_set = (unsigned char)(dcl->val != 0);
  8610. break;
  8611. case PROP_TRANSFORM_PAINT:
  8612. st->transform_scale = (unsigned char)(dcl->val != 0);
  8613. break;
  8614. case PROP_SCALE_X:
  8615. st->scale_x = (signed short)dcl->val;
  8616. break;
  8617. case PROP_SCALE_Y:
  8618. st->scale_y = (signed short)dcl->val;
  8619. break;
  8620. case PROP_ROTATE:
  8621. st->rotate_x100 = (signed short)dcl->val;
  8622. break;
  8623. case PROP_ORIGIN_X_KIND:
  8624. st->origin_x_kind = (unsigned char)dcl->val;
  8625. break;
  8626. case PROP_ORIGIN_X:
  8627. st->origin_x = (signed short)dcl->val;
  8628. break;
  8629. case PROP_ORIGIN_Y_KIND:
  8630. st->origin_y_kind = (unsigned char)dcl->val;
  8631. break;
  8632. case PROP_ORIGIN_Y:
  8633. st->origin_y = (signed short)dcl->val;
  8634. break;
  8635. default:
  8636. break;
  8637. }
  8638. }
  8639. /* The element's custom properties for rules flagged USES_SCOPED: the
  8640. * definitions of its matched rules in cascade order (precedence first,
  8641. * then document order), over the document-wide table. Inheritance from
  8642. * ancestors is not modelled; a name no matched rule defines falls back to
  8643. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8644. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8645. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8646. static void css_env_build(struct br_doc *d)
  8647. {
  8648. static unsigned short order[RULE_MATCH_MAX];
  8649. unsigned int i, need = 0, n = 0;
  8650. css_env_active = 0;
  8651. css_nlocal = 0;
  8652. for (i = 0; i < nrule_matches; i++)
  8653. if (d->rules[rule_matches[i].idx].var_scope &
  8654. BR_RULE_USES_SCOPED)
  8655. need = 1;
  8656. if (!need)
  8657. return;
  8658. /* Definitions apply lowest precedence first so the winner is
  8659. * written last (stable: document order within a precedence). */
  8660. for (i = 0; i < nrule_matches; i++) {
  8661. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8662. unsigned int j = n;
  8663. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8664. continue;
  8665. while (j > 0 && rule_matches[order[j - 1]].spec >
  8666. rule_matches[i].spec) {
  8667. order[j] = order[j - 1];
  8668. j--;
  8669. }
  8670. order[j] = i;
  8671. n++;
  8672. }
  8673. for (i = 0; i < n; i++) {
  8674. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8675. const char *raw = attr_ptr(d, r->raw_off);
  8676. unsigned int save = css_nlocal, k;
  8677. /* Collect this block's definitions after the earlier ones,
  8678. * replacing a name already present. */
  8679. {
  8680. unsigned int base = save;
  8681. css_local_append(raw, r->raw_len);
  8682. for (k = base; k < css_nlocal; k++) {
  8683. int prev = css_local_find_before(
  8684. css_local[k].name, css_local[k].nlen, base);
  8685. if (prev >= 0) {
  8686. css_local[prev] = css_local[k];
  8687. css_local[k].nlen = 0;
  8688. }
  8689. }
  8690. }
  8691. }
  8692. css_env_active = 1;
  8693. }
  8694. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8695. int spec, int spec_imp, short *best, short *best_imp)
  8696. {
  8697. unsigned char p = dcl->prop;
  8698. if (p >= PROP_COUNT)
  8699. return;
  8700. /* A transparent background competes with a colour for one slot. */
  8701. if (p == PROP_BG_NONE)
  8702. p = PROP_BG;
  8703. if (dcl->important) {
  8704. if (spec_imp >= best_imp[p]) {
  8705. apply_decl(st, dcl);
  8706. best_imp[p] = (short)spec_imp;
  8707. }
  8708. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8709. apply_decl(st, dcl);
  8710. best[p] = (short)spec;
  8711. }
  8712. }
  8713. void br_style_for(struct br_doc *d, unsigned int node,
  8714. const struct br_style *parent, struct br_style *out)
  8715. {
  8716. unsigned int tag;
  8717. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8718. unsigned int i;
  8719. if (!cascade_pe && d->nodes[node].style_valid) {
  8720. *out = d->nodes[node].style;
  8721. return;
  8722. }
  8723. *out = *parent;
  8724. out->has_bg = 0;
  8725. out->bg_set = 0;
  8726. out->bg_image = 0;
  8727. out->bg_repeat = 0;
  8728. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8729. out->bg_pos_x = out->bg_pos_y = 0;
  8730. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8731. out->float_left = 0;
  8732. out->clear = 0;
  8733. out->list_item = 0;
  8734. out->font_unit = 0;
  8735. for (i = 0; i < 4; i++) {
  8736. out->border_w[i] = 0;
  8737. out->border_c[i] = 0;
  8738. out->border_s[i] = BR_BORDER_SOLID;
  8739. out->radius[i] = 0;
  8740. }
  8741. out->width = out->max_width = out->min_width = out->height = 0;
  8742. out->min_height = out->max_height = 0;
  8743. out->width_calc_pct = out->width_calc_px = 0;
  8744. out->height_calc_pct = out->height_calc_px = 0;
  8745. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8746. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8747. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8748. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8749. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8750. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8751. out->box_sizing = 0;
  8752. out->ml_auto = out->mr_auto = 0;
  8753. out->mt = out->mr = out->mb = out->ml = 0;
  8754. out->pt = out->pr = out->pb = out->pl = 0;
  8755. for (i = 0; i < 8; i++) {
  8756. out->mp_unit[i] = 0;
  8757. out->mp_calc_px[i] = 0;
  8758. }
  8759. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8760. out->justify = out->align_items = out->align_self = 0;
  8761. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8762. out->grid_auto = 0;
  8763. out->aspect_w = out->aspect_h = 0;
  8764. out->pos = out->left_set = out->top_set = 0;
  8765. out->right_set = out->bottom_set = 0;
  8766. out->left = out->top = out->right = out->bottom = 0;
  8767. out->z_index = 0;
  8768. out->z_set = 0;
  8769. out->tx = out->ty = 0;
  8770. out->tx_pct = out->ty_pct = 0;
  8771. out->transform_set = 0;
  8772. out->opacity = 255;
  8773. out->tr_mask = 0;
  8774. out->tr_ms = 0;
  8775. out->tr_ease = BR_EASE_EASE;
  8776. out->transform_scale = 0;
  8777. out->scale_x = out->scale_y = 100;
  8778. out->rotate_x100 = 0;
  8779. out->origin_x = out->origin_y = 50;
  8780. out->origin_x_kind = out->origin_y_kind = 1;
  8781. out->vis_hidden = out->opacity_hidden = 0;
  8782. out->inline_flex = 0;
  8783. out->overflow_hidden = 0;
  8784. out->content = 0;
  8785. /* A pseudo-element is an anonymous inline box: none of the
  8786. * element's UA defaults or presentational hints apply to it. */
  8787. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8788. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8789. BR_DISPLAY_INLINE;
  8790. for (i = 0; i < PROP_COUNT; i++) {
  8791. best[i] = -1;
  8792. best_imp[i] = -1;
  8793. }
  8794. if (!cascade_pe)
  8795. switch (tag) {
  8796. case BR_TAG_HEAD:
  8797. case BR_TAG_TITLE:
  8798. case BR_TAG_META:
  8799. case BR_TAG_LINK:
  8800. case BR_TAG_BASE:
  8801. case BR_TAG_SCRIPT:
  8802. case BR_TAG_STYLE:
  8803. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8804. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8805. out->display = BR_DISPLAY_NONE;
  8806. break;
  8807. case BR_TAG_BODY:
  8808. /* The UA default: the page itself has no gutter, so a site's
  8809. * `body { margin: 0 }` really starts at the canvas edge. */
  8810. out->mt = out->mb = out->ml = out->mr = 8;
  8811. break;
  8812. case BR_TAG_P:
  8813. out->mt = out->mb = 8;
  8814. break;
  8815. case BR_TAG_H1:
  8816. out->font_px = 32; out->bold = 1;
  8817. out->mt = 10; out->mb = 6;
  8818. break;
  8819. case BR_TAG_H2:
  8820. out->font_px = 24; out->bold = 1;
  8821. out->mt = 8; out->mb = 4;
  8822. break;
  8823. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8824. out->bold = 1; out->mt = 6; out->mb = 3;
  8825. if (tag == BR_TAG_H3)
  8826. out->font_px = 19;
  8827. else if (tag == BR_TAG_H4)
  8828. out->font_px = 17;
  8829. else if (tag == BR_TAG_H5)
  8830. out->font_px = 15;
  8831. else
  8832. out->font_px = 13;
  8833. break;
  8834. case BR_TAG_UL: case BR_TAG_OL:
  8835. out->mt = out->mb = 6; out->pl = 20;
  8836. break;
  8837. case BR_TAG_LI:
  8838. out->mt = out->mb = 2;
  8839. out->list_item = 1;
  8840. break;
  8841. case BR_TAG_BLOCKQUOTE:
  8842. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8843. break;
  8844. case BR_TAG_PRE:
  8845. out->mt = out->mb = 8; out->pl = 8;
  8846. out->has_bg = 1; out->bg = theme_panel;
  8847. out->mono = 1;
  8848. break;
  8849. case BR_TAG_CENTER:
  8850. out->align = BR_ALIGN_CENTER;
  8851. break;
  8852. case BR_TAG_A:
  8853. out->color = theme_link; out->underline = 1;
  8854. break;
  8855. case BR_TAG_B: case BR_TAG_STRONG:
  8856. out->bold = 1;
  8857. break;
  8858. case BR_TAG_I: case BR_TAG_EM:
  8859. out->italic = 1;
  8860. break;
  8861. case BR_TAG_U:
  8862. out->underline = 1;
  8863. break;
  8864. case BR_TAG_S: case BR_TAG_STRIKE:
  8865. out->strike = 1;
  8866. break;
  8867. case BR_TAG_CODE:
  8868. out->color = 0x8010;
  8869. out->mono = 1;
  8870. break;
  8871. case BR_TAG_SMALL:
  8872. out->font_px = 13;
  8873. break;
  8874. case BR_TAG_BIG:
  8875. out->font_px = 20;
  8876. break;
  8877. case BR_TAG_TD: case BR_TAG_TH:
  8878. out->pr = 12;
  8879. /* A cell establishes left-aligned content even inside a legacy
  8880. * <center> (HN's outer page wrapper); author CSS/align below
  8881. * can still override this. */
  8882. out->align = BR_ALIGN_LEFT;
  8883. break;
  8884. case BR_TAG_VIDEO:
  8885. out->display = BR_DISPLAY_BLOCK;
  8886. break;
  8887. case BR_TAG_SVG:
  8888. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8889. * external-link icon follows its link text on the same line.
  8890. * Flex rows still treat it as an item (flex_wants_row). */
  8891. out->display = BR_DISPLAY_INLINE;
  8892. break;
  8893. default:
  8894. break;
  8895. }
  8896. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8897. * presentational hints; author CSS still wins because it is applied
  8898. * below. */
  8899. if (!cascade_pe) {
  8900. unsigned short bgc;
  8901. const char *bga = br_find_attr(d, node, "bgcolor");
  8902. const char *ala = br_find_attr(d, node, "align");
  8903. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8904. out->bg = bgc;
  8905. out->has_bg = 1;
  8906. }
  8907. if (ala) {
  8908. if (same_string(ala, "right"))
  8909. out->align = BR_ALIGN_RIGHT;
  8910. else if (same_string(ala, "center"))
  8911. out->align = BR_ALIGN_CENTER;
  8912. else if (same_string(ala, "left"))
  8913. out->align = BR_ALIGN_LEFT;
  8914. }
  8915. }
  8916. /* presentational <font color>: author CSS below still wins */
  8917. if (!cascade_pe && d->nodes[node].color_off) {
  8918. unsigned short c;
  8919. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8920. if (parse_color(v, s_len(v), &c))
  8921. out->color = c;
  8922. }
  8923. /* SVG presentation attributes; CSS declarations still win below. */
  8924. if (!cascade_pe && in_svg(d, node)) {
  8925. const char *fa = br_find_attr(d, node, "fill");
  8926. const char *sa = br_find_attr(d, node, "stroke");
  8927. const char *wa = br_find_attr(d, node, "stroke-width");
  8928. const char *ra = br_find_attr(d, node, "fill-rule");
  8929. const char *ca = br_find_attr(d, node, "color");
  8930. unsigned short c;
  8931. int mode;
  8932. if (ca && parse_color(ca, s_len(ca), &c))
  8933. out->color = c;
  8934. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8935. out->fill_mode = (unsigned char)mode;
  8936. if (mode == BR_PAINT_COLOR)
  8937. out->fill = c;
  8938. }
  8939. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8940. out->stroke_mode = (unsigned char)mode;
  8941. if (mode == BR_PAINT_COLOR)
  8942. out->stroke = c;
  8943. }
  8944. if (wa) {
  8945. int sw = svg_len16(wa, s_len(wa));
  8946. if (sw >= 0)
  8947. out->stroke_w = (unsigned short)sw;
  8948. }
  8949. if (ra)
  8950. out->fill_evenodd = same_string(ra, "evenodd");
  8951. }
  8952. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8953. * Author CSS below can still override this. */
  8954. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8955. unsigned int p = d->nodes[node].parent;
  8956. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8957. !br_find_attr(d, p, "open"))
  8958. out->display = BR_DISPLAY_NONE;
  8959. }
  8960. rule_collect(d, node);
  8961. css_env_build(d);
  8962. for (i = 0; i < nrule_matches; i++) {
  8963. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8964. int spec = rule_matches[i].spec;
  8965. int spec_imp = rule_matches[i].spec_imp;
  8966. unsigned int k;
  8967. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8968. /* Re-expand against this element's variables. */
  8969. struct br_decl decls[BR_DECLS];
  8970. unsigned int nd;
  8971. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8972. decls, &nd);
  8973. for (k = 0; k < nd; k++)
  8974. cascade_decl(out, &decls[k], spec, spec_imp,
  8975. best, best_imp);
  8976. continue;
  8977. }
  8978. for (k = 0; k < r->nd; k++)
  8979. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8980. best_imp);
  8981. }
  8982. css_env_active = 0;
  8983. css_nlocal = 0;
  8984. if (!cascade_pe && d->nodes[node].style_off) {
  8985. struct br_decl decls[BR_DECLS];
  8986. unsigned int nd, k;
  8987. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8988. parse_decls(d, v, s_len(v), decls, &nd);
  8989. for (k = 0; k < nd; k++)
  8990. cascade_decl(out, &decls[k],
  8991. CASCADE_INLINE * CASCADE_BAND,
  8992. CASCADE_INLINE * CASCADE_BAND, best,
  8993. best_imp);
  8994. }
  8995. if (out->font_unit) {
  8996. int px = out->font_raw;
  8997. if (out->font_unit == 2)
  8998. px = parent->font_px * out->font_raw / 100;
  8999. else if (out->font_unit == 3)
  9000. px = root_font_px * out->font_raw / 100;
  9001. else if (out->font_unit == 4)
  9002. px = parent->font_px * out->font_raw / 10000;
  9003. if (px < 8)
  9004. px = 8;
  9005. if (px > 72)
  9006. px = 72;
  9007. out->font_px = (unsigned char)px;
  9008. }
  9009. resolve_size_units(out);
  9010. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  9011. out->display = BR_DISPLAY_NONE;
  9012. /* Invisible in-flow content is dropped (it would otherwise take
  9013. * space this engine cannot hide well). An absolute/fixed box keeps
  9014. * its display: it moves nothing, and scripts measure it while it is
  9015. * still transparent (Bootstrap's tooltip is `opacity: 0` until it
  9016. * gets `.in`). The layout lays it out without painting it. */
  9017. if (((out->vis_hidden || out->opacity_hidden) &&
  9018. out->pos != 1 && out->pos != 2) ||
  9019. (out->overflow_hidden && out->height_kind == 1 &&
  9020. out->height == 0))
  9021. out->display = BR_DISPLAY_NONE;
  9022. if (cascade_pe)
  9023. return;
  9024. d->nodes[node].style = *out;
  9025. d->nodes[node].style_valid = 1;
  9026. }
  9027. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  9028. const struct br_style *elem, struct br_style *out,
  9029. char *buf, unsigned int cap)
  9030. {
  9031. struct br_node *n;
  9032. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  9033. const char *c;
  9034. unsigned int len = 0;
  9035. if (out) {
  9036. out->content = 0;
  9037. out->display = BR_DISPLAY_NONE;
  9038. }
  9039. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  9040. (pe != 1 && pe != 2))
  9041. return 0;
  9042. n = &d->nodes[node];
  9043. if (n->type != BR_NODE_ELEM ||
  9044. ((n->pe_state & chk) && !(n->pe_state & has)))
  9045. return 0;
  9046. cascade_pe = pe;
  9047. br_style_for(d, node, elem, out);
  9048. cascade_pe = 0;
  9049. if (out->content == BR_CONTENT_EMPTY) {
  9050. /* No text; the caller may still draw the box (an icon
  9051. * with a size and a background image). */
  9052. buf[0] = '\0';
  9053. n->pe_state |= chk;
  9054. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  9055. out->pos != 2 && (out->width_kind || out->height_kind) &&
  9056. (out->has_bg || out->bg_image))
  9057. n->pe_state |= has;
  9058. else
  9059. out->display = BR_DISPLAY_NONE;
  9060. return 0;
  9061. }
  9062. if (out->content && out->display != BR_DISPLAY_NONE &&
  9063. out->pos != 1 && out->pos != 2) {
  9064. c = attr_ptr(d, d->content_off[out->content - 1]);
  9065. while (*c && len + 1 < cap) {
  9066. if (*c == '\001') {
  9067. char name[32];
  9068. unsigned int k = 0;
  9069. const char *v;
  9070. c++;
  9071. while (*c && *c != '\002' && k + 1 < sizeof(name))
  9072. name[k++] = *c++;
  9073. name[k] = '\0';
  9074. if (*c == '\002')
  9075. c++;
  9076. v = br_find_attr(d, node, name);
  9077. while (v && *v && len + 1 < cap)
  9078. buf[len++] = *v++;
  9079. continue;
  9080. }
  9081. buf[len++] = *c++;
  9082. }
  9083. }
  9084. buf[len] = '\0';
  9085. n->pe_state |= chk;
  9086. if (len)
  9087. n->pe_state |= has;
  9088. return len;
  9089. }
  9090. /* ---- HTML parser ---- */
  9091. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  9092. unsigned int sp, int *pre)
  9093. {
  9094. unsigned int i;
  9095. *pre = 0;
  9096. for (i = 1; i <= sp; i++)
  9097. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  9098. (*pre)++;
  9099. }
  9100. static void implicit_close(struct br_doc *d, unsigned short *stack,
  9101. unsigned int *sp, unsigned int tag)
  9102. {
  9103. while (*sp > 0) {
  9104. unsigned int top = d->nodes[stack[*sp]].tag;
  9105. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  9106. (*sp)--;
  9107. continue;
  9108. }
  9109. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  9110. (*sp)--;
  9111. continue;
  9112. }
  9113. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  9114. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  9115. (*sp)--;
  9116. continue;
  9117. }
  9118. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  9119. (*sp)--;
  9120. continue;
  9121. }
  9122. break;
  9123. }
  9124. }
  9125. static int has_attr_token(const char *s, unsigned int len, const char *name)
  9126. {
  9127. unsigned int i, n = s_len(name);
  9128. for (i = 0; i + n <= len; i++) {
  9129. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  9130. continue;
  9131. if (!ci_eq_n(s + i, name, n))
  9132. continue;
  9133. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  9134. return 1;
  9135. }
  9136. return 0;
  9137. }
  9138. static int stack_has_svg(const struct br_doc *d,
  9139. const unsigned short *stack, unsigned int sp)
  9140. {
  9141. unsigned int k;
  9142. for (k = 0; k <= sp && k < 64; k++)
  9143. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  9144. return 1;
  9145. return 0;
  9146. }
  9147. /* Skip an element subtree by finding its closing tag. Used for SVG
  9148. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  9149. * overwrite the document title. */
  9150. static unsigned int skip_element(const char *html, unsigned int len,
  9151. unsigned int i, const char *name,
  9152. unsigned int n)
  9153. {
  9154. char close[40];
  9155. unsigned int k = 0, p;
  9156. close[k++] = '<';
  9157. close[k++] = '/';
  9158. while (k < sizeof(close) - 1 && n--)
  9159. close[k++] = *name++;
  9160. p = (unsigned int)find_ci(html, len, close, k, i);
  9161. if ((int)p < 0)
  9162. return len;
  9163. p += k;
  9164. while (p < len && html[p] != '>')
  9165. p++;
  9166. if (p < len)
  9167. p++;
  9168. return p;
  9169. }
  9170. /* Store a <script src=...> value in the attribute arena. Kept separate from
  9171. * parse_attrs() so script-only attributes cannot be confused with the
  9172. * value/d attributes that also use src_off on other elements. */
  9173. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  9174. unsigned int len)
  9175. {
  9176. unsigned int i = 0;
  9177. while (i < len) {
  9178. unsigned int ns, ne, vs, ve;
  9179. char quote = 0;
  9180. while (i < len && is_space(s[i]))
  9181. i++;
  9182. if (i >= len || s[i] == '/')
  9183. break;
  9184. ns = i;
  9185. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  9186. i++;
  9187. ne = i;
  9188. while (i < len && is_space(s[i]))
  9189. i++;
  9190. if (i >= len || s[i] != '=')
  9191. continue;
  9192. i++;
  9193. while (i < len && is_space(s[i]))
  9194. i++;
  9195. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9196. quote = s[i++];
  9197. vs = i;
  9198. while (i < len && s[i] != quote)
  9199. i++;
  9200. ve = i;
  9201. if (i < len)
  9202. i++;
  9203. } else {
  9204. vs = i;
  9205. while (i < len && !is_space(s[i]) && s[i] != '>')
  9206. i++;
  9207. ve = i;
  9208. }
  9209. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  9210. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  9211. }
  9212. return 0;
  9213. }
  9214. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  9215. * application/json, text/template, ...) and non-JS type values. External
  9216. * src scripts are allowed; the browser fetches them separately. Returns 1
  9217. * for a classic script, 2 for type="module", 0 to skip (also for a
  9218. * `nomodule` fallback, since module scripts run). */
  9219. static int script_is_javascript(const char *s, unsigned int len)
  9220. {
  9221. int kind = 1;
  9222. unsigned int i = 0;
  9223. while (i < len) {
  9224. unsigned int ns, ne, vs = 0, ve = 0;
  9225. char quote = 0;
  9226. int has_val = 0;
  9227. while (i < len && is_space(s[i]))
  9228. i++;
  9229. if (i >= len)
  9230. break;
  9231. ns = i;
  9232. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  9233. i++;
  9234. ne = i;
  9235. while (i < len && is_space(s[i]))
  9236. i++;
  9237. if (i < len && s[i] == '=') {
  9238. i++;
  9239. while (i < len && is_space(s[i]))
  9240. i++;
  9241. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9242. quote = s[i++];
  9243. vs = i;
  9244. while (i < len && s[i] != quote)
  9245. i++;
  9246. ve = i;
  9247. if (i < len)
  9248. i++;
  9249. } else {
  9250. vs = i;
  9251. while (i < len && !is_space(s[i]) && s[i] != '/')
  9252. i++;
  9253. ve = i;
  9254. }
  9255. has_val = 1;
  9256. } else if (i == ne) {
  9257. i++;
  9258. }
  9259. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  9260. return 0;
  9261. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  9262. unsigned int vn = has_val ? ve - vs : 0;
  9263. if (vn == 0)
  9264. continue;
  9265. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  9266. kind = 2;
  9267. continue;
  9268. }
  9269. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  9270. (vn == 22 &&
  9271. ci_eq_n(s + vs, "application/javascript", 22)) ||
  9272. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  9273. (vn == 22 &&
  9274. ci_eq_n(s + vs, "application/ecmascript", 22)))
  9275. continue;
  9276. return 0;
  9277. }
  9278. }
  9279. return kind;
  9280. }
  9281. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  9282. {
  9283. unsigned short stack[64];
  9284. unsigned int sp = 0;
  9285. unsigned int i = 0;
  9286. int pre = 0;
  9287. unsigned int css_len = 0;
  9288. if (!d)
  9289. return -1;
  9290. if (!d->nodes)
  9291. br_doc_init(d);
  9292. if (!d->nodes || !d->text || !d->attrs)
  9293. return -1;
  9294. doc_reset(d);
  9295. d->script_src = html;
  9296. stack[0] = 0;
  9297. d->nodes[0].type = BR_NODE_ELEM;
  9298. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9299. while (i < len) {
  9300. if (html[i] != '<') {
  9301. unsigned int st = i;
  9302. while (i < len && html[i] != '<')
  9303. i++;
  9304. if (sp <= 63)
  9305. append_text(d, stack[sp], html + st, i - st, pre);
  9306. continue;
  9307. }
  9308. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  9309. html[i + 3] == '-') {
  9310. int p = find_ci(html, len, "-->", 3, i + 4);
  9311. i = p >= 0 ? (unsigned int)p + 3 : len;
  9312. continue;
  9313. }
  9314. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  9315. while (i < len && html[i] != '>')
  9316. i++;
  9317. if (i < len)
  9318. i++;
  9319. continue;
  9320. }
  9321. if (i + 1 < len && html[i + 1] == '/') {
  9322. unsigned int ns = i + 2, ne, tag, k, n;
  9323. char name[32];
  9324. while (ns < len && is_space(html[ns]))
  9325. ns++;
  9326. ne = ns;
  9327. while (ne < len && is_ident(html[ne]))
  9328. ne++;
  9329. if (ne == ns) {
  9330. i++;
  9331. continue;
  9332. }
  9333. n = ne - ns;
  9334. if (n >= sizeof(name))
  9335. n = sizeof(name) - 1;
  9336. for (k = 0; k < n; k++)
  9337. name[k] = html[ns + k];
  9338. name[n] = '\0';
  9339. tag = br_tag_from_name(name);
  9340. i = ne;
  9341. while (i < len && html[i] != '>')
  9342. i++;
  9343. if (i < len)
  9344. i++;
  9345. for (k = sp; k > 0; k--) {
  9346. struct br_node *nd = &d->nodes[stack[k]];
  9347. if (tag == BR_TAG_UNKNOWN) {
  9348. const char *raw = attr_ptr(d, nd->name_off);
  9349. if (nd->tag == BR_TAG_UNKNOWN &&
  9350. ci_eq_n(raw, name, n) &&
  9351. raw[n] == '\0') {
  9352. sp = k - 1;
  9353. break;
  9354. }
  9355. } else if (nd->tag == tag) {
  9356. sp = k - 1;
  9357. break;
  9358. }
  9359. }
  9360. stack_recount_pre(d, stack, sp, &pre);
  9361. continue;
  9362. }
  9363. /* start tag */
  9364. {
  9365. unsigned int ns = i + 1, ne, tag, k, n;
  9366. char name[16];
  9367. unsigned int attr_start, attr_end;
  9368. int selfclose = 0;
  9369. while (ns < len && is_space(html[ns]))
  9370. ns++;
  9371. ne = ns;
  9372. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  9373. ne++;
  9374. if (ne == ns) {
  9375. /* stray '<' */
  9376. append_text(d, stack[sp], html + i, 1, pre);
  9377. i++;
  9378. continue;
  9379. }
  9380. n = ne - ns;
  9381. if (n >= sizeof(name))
  9382. n = sizeof(name) - 1;
  9383. for (k = 0; k < n; k++)
  9384. name[k] = html[ns + k];
  9385. name[n] = '\0';
  9386. tag = br_tag_from_name(name);
  9387. attr_start = ne;
  9388. {
  9389. unsigned int q = ne;
  9390. char quote = 0;
  9391. while (q < len) {
  9392. char c = html[q];
  9393. if (quote) {
  9394. if (c == quote)
  9395. quote = 0;
  9396. } else if (c == '"' || c == '\'') {
  9397. quote = c;
  9398. } else if (c == '>') {
  9399. break;
  9400. }
  9401. q++;
  9402. }
  9403. attr_end = q;
  9404. /* `/>` closes the tag only when the slash is
  9405. * not the end of an unquoted attribute value:
  9406. * Docusaurus writes `<a href=/installation/>`,
  9407. * which used to self-close the link and leave
  9408. * its label outside it. */
  9409. if (q > attr_start && html[q - 1] == '/' &&
  9410. (q - 1 == attr_start ||
  9411. is_space(html[q - 2]) ||
  9412. html[q - 2] == '"' || html[q - 2] == '\''))
  9413. selfclose = 1;
  9414. i = q < len ? q + 1 : len;
  9415. }
  9416. if (tag == BR_TAG_SCRIPT) {
  9417. int p = find_ci(html, len, "</script", 8, i);
  9418. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9419. unsigned int idx;
  9420. struct br_script *script = 0;
  9421. int kind = script_is_javascript(html + attr_start,
  9422. attr_end - attr_start);
  9423. if (kind) {
  9424. if (d->nscripts < BR_MAX_SCRIPTS) {
  9425. script = &d->scripts[d->nscripts++];
  9426. script->module = kind == 2;
  9427. script->node = 0;
  9428. script->off = i;
  9429. script->len = end > i ? end - i : 0;
  9430. script->src_off = script_src_attr(
  9431. d, html + attr_start,
  9432. attr_end - attr_start);
  9433. } else {
  9434. d->scripts_truncated = 1;
  9435. }
  9436. }
  9437. /* Keep the element in the DOM even though its body is
  9438. * executed separately: scripts (Piwik, analytics)
  9439. * insert a sibling after getElementsByTagName('script')
  9440. * [0], and the element must exist for that to work. */
  9441. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9442. (unsigned char)tag);
  9443. if (idx)
  9444. parse_attrs(d, &d->nodes[idx],
  9445. html + attr_start,
  9446. attr_end - attr_start);
  9447. if (script)
  9448. script->node = idx;
  9449. if (idx && !kind && end > i)
  9450. append_raw_text(d, idx, html + i, end - i);
  9451. i = p >= 0 ? (unsigned int)p + 8 : len;
  9452. while (i < len && html[i] != '>')
  9453. i++;
  9454. if (i < len)
  9455. i++;
  9456. continue;
  9457. }
  9458. /* Scripting is always enabled, so <noscript> is a
  9459. * raw-text element (HTML "in head/in body, scripting
  9460. * flag set"): its markup must not reach the DOM or the
  9461. * cascade. google.com puts a meta refresh and
  9462. * `table,div,span,p{display:none}` in one, which hid
  9463. * the whole page and left only the "click here" link. */
  9464. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9465. int p = find_ci(html, len, "</noscript", 10, i);
  9466. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9467. unsigned int idx;
  9468. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9469. (unsigned char)tag);
  9470. if (idx) {
  9471. parse_attrs(d, &d->nodes[idx],
  9472. html + attr_start,
  9473. attr_end - attr_start);
  9474. if (end > i)
  9475. append_raw_text(d, idx, html + i,
  9476. end - i);
  9477. }
  9478. i = p >= 0 ? (unsigned int)p + 10 : len;
  9479. while (i < len && html[i] != '>')
  9480. i++;
  9481. if (i < len)
  9482. i++;
  9483. continue;
  9484. }
  9485. if (stack_has_svg(d, stack, sp) &&
  9486. (tag == BR_TAG_TITLE ||
  9487. (tag == BR_TAG_UNKNOWN &&
  9488. (ci_eq_n(name, "desc", 4) ||
  9489. ci_eq_n(name, "metadata", 8))))) {
  9490. i = skip_element(html, len, i, name, n);
  9491. continue;
  9492. }
  9493. /* A closed <details> is collapsed at style time, not
  9494. * parse time: the <summary> (the hamburger button on
  9495. * tagesschau and many mobile menus) must stay in the
  9496. * DOM. */
  9497. if (tag == BR_TAG_STYLE) {
  9498. int p = find_ci(html, len, "</style", 7, i);
  9499. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9500. unsigned int cend = end;
  9501. unsigned int idx;
  9502. while (cend > i && is_space(html[cend - 1]))
  9503. cend--;
  9504. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9505. for (k = i; k < cend; k++)
  9506. css_store[css_len++] = html[k];
  9507. css_store[css_len++] = '\n';
  9508. }
  9509. /* The CSS is collected above, but keep a display:none
  9510. * element so getElementsByTagName('style')/parentNode
  9511. * behave like a real document. */
  9512. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9513. (unsigned char)tag);
  9514. if (idx)
  9515. parse_attrs(d, &d->nodes[idx],
  9516. html + attr_start,
  9517. attr_end - attr_start);
  9518. i = p >= 0 ? (unsigned int)p + 7 : len;
  9519. while (i < len && html[i] != '>')
  9520. i++;
  9521. if (i < len)
  9522. i++;
  9523. continue;
  9524. }
  9525. if (tag == BR_TAG_TITLE) {
  9526. int p = find_ci(html, len, "</title", 7, i);
  9527. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9528. unsigned int tn = 0;
  9529. char tmp[BR_TITLE_MAX];
  9530. tn = decode_text(html + i, end - i, tmp,
  9531. sizeof(tmp), 1);
  9532. (void)tn;
  9533. s_copy(d->title, tmp, sizeof(d->title));
  9534. i = p >= 0 ? (unsigned int)p + 7 : len;
  9535. while (i < len && html[i] != '>')
  9536. i++;
  9537. if (i < len)
  9538. i++;
  9539. continue;
  9540. }
  9541. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9542. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9543. unsigned int idx;
  9544. implicit_close(d, stack, &sp, tag);
  9545. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9546. (unsigned char)tag);
  9547. if (idx)
  9548. parse_attrs(d, &d->nodes[idx],
  9549. html + attr_start,
  9550. attr_end - attr_start);
  9551. continue;
  9552. }
  9553. if (sp >= 63) {
  9554. d->truncated = 1;
  9555. continue;
  9556. }
  9557. implicit_close(d, stack, &sp, tag);
  9558. {
  9559. unsigned int idx = node_append(d, stack[sp],
  9560. BR_NODE_ELEM,
  9561. (unsigned char)tag);
  9562. if (!idx)
  9563. continue;
  9564. parse_attrs(d, &d->nodes[idx],
  9565. html + attr_start,
  9566. attr_end - attr_start);
  9567. if (tag == BR_TAG_UNKNOWN)
  9568. d->nodes[idx].name_off =
  9569. store_attr(d, name, n, 31);
  9570. /* HTML ignores the self-closing flag on a
  9571. * normal element (`<div/>` opens a div); only
  9572. * SVG content (and unknown/custom tags, kept
  9573. * as before) honours it. */
  9574. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9575. tag != BR_TAG_SVG &&
  9576. !stack_has_svg(d, stack, sp))
  9577. selfclose = 0;
  9578. if (!selfclose) {
  9579. sp++;
  9580. stack[sp] = (unsigned short)idx;
  9581. if (tag == BR_TAG_PRE)
  9582. pre++;
  9583. }
  9584. }
  9585. }
  9586. }
  9587. d->inline_css_len = css_len;
  9588. return 0;
  9589. }
  9590. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9591. {
  9592. if (!d->nodes)
  9593. br_doc_init(d);
  9594. if (!d->nodes || !d->text || !d->attrs)
  9595. return -1;
  9596. doc_reset(d);
  9597. d->plain = 1;
  9598. d->nodes[0].type = BR_NODE_ELEM;
  9599. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9600. append_text(d, 0, text, len, 1);
  9601. return 0;
  9602. }
  9603. /* A standalone image response: a synthetic document with one <img>. The
  9604. * caller decodes the already-fetched body into image slot 0 and sets the
  9605. * returned node's img_index. */
  9606. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9607. {
  9608. unsigned int idx;
  9609. if (!d->nodes)
  9610. br_doc_init(d);
  9611. if (!d->nodes || !d->text || !d->attrs)
  9612. return 0;
  9613. doc_reset(d);
  9614. d->nodes[0].type = BR_NODE_ELEM;
  9615. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9616. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9617. if (!idx)
  9618. return 0;
  9619. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9620. return idx;
  9621. }