| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173 |
- /* browser_html: see browser_html.h. A deliberately tolerant parser: bad
- * nesting is repaired, unknown tags are transparent, and the CSS engine only
- * implements the properties in the browser's documented subset. */
- #include "browser_html.h"
- #include <stdlib.h>
- #include <string.h>
- static int is_space(char c);
- static int is_ident(char c);
- /* ---- storage (one document per process) ----
- *
- * The node/text/attribute arenas are allocated from the ring-3 heap, not
- * static BSS: a large article needs several MB and the process image lives
- * in the 4 MB user window. heap_reset() on navigation releases them with
- * the rest of the page heap. */
- static struct br_rule rule_fallback[BR_RULE_FALLBACK];
- static char css_store[BR_CSS_SIZE];
- static int root_font_px = 16;
- /* Distinct background-image URLs per page (app stores give every card an
- * icon through an inline style). */
- #define BR_BG_URLS 512
- /* Background URLs live in one pool: a data: URL (Infima's menu chevron is
- * an inline SVG) is far longer than a fixed slot. */
- #define BR_BG_URL_POOL (128 * 1024)
- #define BR_BG_URL_MAX 8191
- static const char *bg_urls[BR_BG_URLS];
- static char bg_url_pool[BR_BG_URL_POOL];
- static unsigned int bg_url_pool_used;
- static unsigned int bg_url_count;
- /* @font-face definitions, one document at a time. The URL is resolved by
- * the browser against `base` (the sheet URL, or the page URL for <style>). */
- struct br_font_def {
- char family[BR_FACE_FAMILY];
- char url[BR_FACE_URL];
- char base[192];
- unsigned short wmin, wmax; /* font-weight descriptor (a range for
- * variable fonts) */
- unsigned char italic; /* font-style italic/oblique */
- unsigned char family_id; /* first entry of the same family */
- unsigned char used; /* a font-family declaration names it */
- };
- static struct br_font_def font_defs[BR_MAX_FACES];
- static int font_face_count;
- static char font_base[192];
- static void font_reset(void)
- {
- memset(font_defs, 0, sizeof(font_defs));
- font_face_count = 0;
- font_base[0] = '\0';
- }
- void br_set_font_base(const char *base)
- {
- unsigned int i = 0;
- if (!base)
- base = "";
- while (base[i] && i + 1 < sizeof(font_base)) {
- font_base[i] = base[i];
- i++;
- }
- font_base[i] = '\0';
- }
- int br_font_face_count(void)
- {
- return font_face_count;
- }
- const char *br_font_face_url(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].url;
- }
- const char *br_font_face_base(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
- font_base;
- }
- const char *br_font_face_family(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].family;
- }
- int br_font_face_info(int id, struct br_font_face_info *out)
- {
- const struct br_font_def *f;
- if (id < 1 || id > font_face_count || !out)
- return -1;
- f = &font_defs[id - 1];
- out->family_id = f->family_id;
- out->wmin = f->wmin;
- out->wmax = f->wmax;
- out->italic = f->italic;
- out->used = f->used;
- return 0;
- }
- const char *br_bg_image_url(unsigned int idx)
- {
- if (idx >= bg_url_count)
- return "";
- return bg_urls[idx];
- }
- unsigned int br_root_font_px(void)
- {
- return (unsigned int)root_font_px;
- }
- /* ---- tag table ---- */
- struct tag_name {
- const char *name;
- unsigned char tag;
- };
- static const struct tag_name tag_names[] = {
- { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
- { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
- { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
- { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
- { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
- { "template", BR_TAG_TEMPLATE },
- { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
- { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
- { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
- { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
- { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
- { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
- { "picture", BR_TAG_PICTURE },
- { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
- { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
- { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
- { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
- { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
- { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
- { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
- { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
- { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
- { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
- { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
- { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
- { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
- { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
- { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
- { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
- { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
- { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
- { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
- { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
- { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
- { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
- { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
- { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
- { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
- { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
- { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
- { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
- { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
- { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
- { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
- { 0, 0 }
- };
- const char *br_tag_name(unsigned int tag)
- {
- unsigned int i;
- for (i = 0; tag_names[i].name; i++)
- if (tag_names[i].tag == tag)
- return tag_names[i].name;
- return "?";
- }
- static unsigned int s_len(const char *s);
- #define TAG_HASH_SIZE 128
- static unsigned char tag_hash_idx[TAG_HASH_SIZE];
- static int tag_hash_ready;
- static unsigned int tag_hash_key(const char *name, unsigned int len)
- {
- unsigned int i, h = 2166136261u;
- for (i = 0; i < len; i++) {
- h ^= (unsigned char)name[i];
- h *= 16777619u;
- }
- return h & (TAG_HASH_SIZE - 1);
- }
- static void tag_hash_build(void)
- {
- unsigned int i;
- for (i = 0; i < TAG_HASH_SIZE; i++)
- tag_hash_idx[i] = 0xFF;
- for (i = 0; tag_names[i].name; i++) {
- unsigned int h = tag_hash_key(tag_names[i].name,
- s_len(tag_names[i].name));
- while (tag_hash_idx[h] != 0xFF)
- h = (h + 1) & (TAG_HASH_SIZE - 1);
- tag_hash_idx[h] = (unsigned char)i;
- }
- tag_hash_ready = 1;
- }
- unsigned int br_tag_from_name(const char *name)
- {
- char lower[16];
- unsigned int i, len, h;
- unsigned char idx;
- for (i = 0; i < 15 && name[i]; i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- lower[i] = c;
- }
- lower[i] = '\0';
- len = i;
- if (!tag_hash_ready)
- tag_hash_build();
- h = tag_hash_key(lower, len);
- for (;;) {
- idx = tag_hash_idx[h];
- if (idx == 0xFF)
- return BR_TAG_UNKNOWN;
- {
- const char *n = tag_names[idx].name;
- unsigned int j;
- for (j = 0; lower[j] && n[j] == lower[j]; j++)
- ;
- if (!lower[j] && !n[j])
- return tag_names[idx].tag;
- }
- h = (h + 1) & (TAG_HASH_SIZE - 1);
- }
- }
- int br_tag_is_void(unsigned int tag)
- {
- switch (tag) {
- case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
- case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
- case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
- case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
- case BR_TAG_AREA: case BR_TAG_COL:
- return 1;
- default:
- return 0;
- }
- }
- int br_tag_is_block(unsigned int tag)
- {
- switch (tag) {
- case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
- case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
- case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
- case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
- case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
- case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
- case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
- case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
- case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
- case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
- case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
- case BR_TAG_PICTURE:
- case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
- case BR_TAG_ADDRESS: case BR_TAG_HR:
- return 1;
- default:
- return 0;
- }
- }
- /* ---- string / entity helpers ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static int ci_eq_n(const char *a, const char *b, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++) {
- char ca = a[i], cb = b[i];
- if (ca >= 'A' && ca <= 'Z')
- ca = (char)(ca - 'A' + 'a');
- if (cb >= 'A' && cb <= 'Z')
- cb = (char)(cb - 'A' + 'a');
- if (ca != cb)
- return 0;
- }
- return 1;
- }
- #define CURSOR_CSS_(e, css, file) css,
- static const char *const css_cursor_names[CURSOR_COUNT] = {
- CURSOR_LIST(CURSOR_CSS_)
- };
- #undef CURSOR_CSS_
- /* CSS `cursor`: the keyword after the last comma (url() images before it
- * are not supported, so the fallback keyword is what counts). Returns
- * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
- * anything else (the declaration is then ignored). */
- static int parse_cursor(const char *v, unsigned int n)
- {
- unsigned int a = 0, b, i;
- for (i = 0; i < n; i++)
- if (v[i] == ',')
- a = i + 1;
- while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
- a++;
- b = a;
- while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
- (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
- b++;
- if (b == a)
- return -1;
- if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
- return BR_CURSOR_AUTO;
- if (b - a == 4 && ci_eq_n(v + a, "none", 4))
- return CURSOR_DEFAULT + 1;
- for (i = 0; i < CURSOR_COUNT; i++)
- if (strlen(css_cursor_names[i]) == b - a &&
- ci_eq_n(v + a, css_cursor_names[i], b - a))
- return (int)i + 1;
- return -1;
- }
- static int find_ci(const char *hay, unsigned int len, const char *needle,
- unsigned int nlen, unsigned int from)
- {
- unsigned int i;
- if (nlen == 0 || from >= len)
- return -1;
- for (i = from; i + nlen <= len; i++)
- if (ci_eq_n(hay + i, needle, nlen))
- return (int)i;
- return -1;
- }
- static const struct {
- const char *name;
- const char *text;
- } entities[] = {
- { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
- { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
- { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
- { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
- { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
- { "times", "x" }, { "divide", "/" }, { "deg", "o" },
- { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
- { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
- { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
- { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
- { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
- { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
- { 0, 0 }
- };
- static void emit(char *out, unsigned int cap, unsigned int *n, char c)
- {
- if (*n + 1 < cap)
- out[(*n)++] = c;
- }
- static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
- char *out, unsigned int cap, unsigned int *n)
- {
- unsigned int start = *pos + 1, i = start;
- unsigned int code = 0;
- int ok = 0;
- while (i < len && s[i] != ';' && i - start < 12)
- i++;
- if (i >= len || s[i] != ';') {
- /* Not an entity: emit the '&' and advance past it. Leaving
- * *pos unchanged here used to spin forever on bare '&'. */
- emit(out, cap, n, '&');
- *pos = start;
- return;
- }
- if (s[start] == '#') {
- unsigned int j = start + 1;
- if (j < i && (s[j] == 'x' || s[j] == 'X')) {
- j++;
- for (; j < i; j++) {
- char c = s[j];
- int d = -1;
- if (c >= '0' && c <= '9') d = c - '0';
- else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
- else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
- if (d < 0) break;
- code = code * 16 + (unsigned int)d;
- ok = 1;
- }
- } else {
- for (; j < i; j++) {
- if (s[j] < '0' || s[j] > '9')
- break;
- code = code * 10 + (unsigned int)(s[j] - '0');
- ok = 1;
- }
- }
- } else {
- unsigned int k;
- for (k = 0; entities[k].name; k++) {
- unsigned int el = s_len(entities[k].name);
- unsigned int j;
- if (el != i - start)
- continue;
- for (j = 0; j < el; j++)
- if (s[start + j] != entities[k].name[j])
- break;
- if (j == el) {
- const char *t = entities[k].text;
- while (*t)
- emit(out, cap, n, *t++);
- *pos = i + 1;
- return;
- }
- }
- }
- if (ok && code) {
- if (code == 0x00A0)
- emit(out, cap, n, ' ');
- else if ((code >= 0x2010 && code <= 0x2015) ||
- code == 0x2212) /* hyphens, dashes */
- emit(out, cap, n, '-');
- else if (code == 0x2018 || code == 0x2019)
- emit(out, cap, n, '\'');
- else if (code == 0x201C || code == 0x201D)
- emit(out, cap, n, '"');
- else if (code == 0x2026) {
- emit(out, cap, n, '.'); emit(out, cap, n, '.');
- emit(out, cap, n, '.');
- } else if (code <= 0xFF) {
- emit(out, cap, n, (char)code);
- } else {
- emit(out, cap, n, '?');
- }
- } else {
- emit(out, cap, n, '?');
- }
- *pos = i + 1;
- }
- /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
- * space and drops leading spaces. Returns the output length. */
- static unsigned int decode_text(const char *s, unsigned int len, char *out,
- unsigned int cap, int collapse)
- {
- unsigned int i = 0, n = 0;
- while (i < len) {
- unsigned char c = (unsigned char)s[i];
- if (c == '&') {
- decode_entity(s, len, &i, out, cap, &n);
- continue;
- }
- if (c < 0x80) {
- i++;
- if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
- c == '\r' || c == '\f')) {
- if (n > 0 && out[n - 1] == ' ')
- continue;
- emit(out, cap, &n, ' ');
- continue;
- }
- if (!collapse && c == '\t') {
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- continue;
- }
- emit(out, cap, &n, (char)c);
- continue;
- }
- /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
- if ((c & 0xE0) == 0xC0 && i + 1 < len) {
- unsigned int cp = ((c & 0x1F) << 6) |
- ((unsigned char)s[i + 1] & 0x3F);
- i += 2;
- if (cp == 0xA0)
- cp = ' ';
- else if (cp == 0xB7)
- cp = '-';
- else if (cp == 0xD7)
- cp = 'x';
- else if (cp == 0xB0)
- cp = 'o';
- if (cp <= 0xFF)
- emit(out, cap, &n, (char)cp);
- else
- emit(out, cap, &n, '?');
- } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
- unsigned int cp = ((c & 0x0F) << 12) |
- (((unsigned char)s[i + 1] & 0x3F) << 6) |
- ((unsigned char)s[i + 2] & 0x3F);
- i += 3;
- if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212) {
- emit(out, cap, &n, '-');
- } else if (cp == 0x2018 || cp == 0x2019) {
- emit(out, cap, &n, '\'');
- } else if (cp == 0x201C || cp == 0x201D) {
- emit(out, cap, &n, '"');
- } else if (cp == 0x2026) {
- emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
- emit(out, cap, &n, '.');
- } else if (cp == 0x2192) {
- emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
- } else if (cp == 0x2264) {
- emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
- } else {
- emit(out, cap, &n, '?');
- }
- } else {
- i++;
- emit(out, cap, &n, '?');
- }
- }
- out[n] = '\0';
- return n;
- }
- /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
- * space / empty. */
- /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
- * cap silently dropped every class past it (arstechnica's header lost its
- * background, alignment and all responsive variants). */
- #define BR_CLASS_MAX 2048
- #define BR_STYLE_ATTR_MAX 2048
- static unsigned int store_attr(struct br_doc *d, const char *s,
- unsigned int len, unsigned int limit)
- {
- unsigned int n;
- if (d->attrs_used + 2 >= BR_ATTR_SIZE)
- return 0;
- if (len > limit)
- len = limit;
- n = decode_text(s, len, d->attrs + d->attrs_used,
- BR_ATTR_SIZE - d->attrs_used, 0);
- if (n == 0)
- return 0;
- {
- unsigned int off = d->attrs_used;
- d->attrs_used += n + 1;
- return off;
- }
- }
- static const char *attr_ptr(struct br_doc *d, unsigned int off)
- {
- return off ? d->attrs + off : "";
- }
- /* Copy a raw CSS string into the attr arena (no entity decoding). */
- static unsigned int store_raw(struct br_doc *d, const char *s,
- unsigned int len, unsigned int limit)
- {
- unsigned int off;
- if (len > limit)
- len = limit;
- if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
- return 0;
- off = d->attrs_used;
- {
- unsigned int i;
- for (i = 0; i < len; i++)
- d->attrs[off + i] = s[i];
- }
- d->attrs[off + len] = '\0';
- d->attrs_used += len + 1;
- return off;
- }
- #define VAR_HASH_SIZE 1024
- static unsigned int var_hash(const char *name, unsigned int len)
- {
- unsigned int i, h = 2166136261u;
- for (i = 0; i < len; i++) {
- h ^= (unsigned char)name[i];
- h *= 16777619u;
- }
- return h & (VAR_HASH_SIZE - 1);
- }
- static unsigned int css_var_find(struct br_doc *d, const char *name,
- unsigned int nlen)
- {
- unsigned int h = var_hash(name, nlen);
- for (;;) {
- unsigned int i = d->var_slot[h];
- if (!i)
- return BR_MAX_VARS;
- i--;
- if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
- const char *n = attr_ptr(d, d->var_name[i]);
- unsigned int j;
- for (j = 0; j < nlen && n[j] == name[j]; j++)
- ;
- if (j == nlen)
- return i;
- }
- h = (h + 1) & (VAR_HASH_SIZE - 1);
- }
- }
- #define BR_VAR_ROOT 1
- #define BR_VAR_CONFLICT 2
- #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
- /* Custom properties live in one document-wide table (var() is expanded
- * when a sheet is parsed, not per element). Definitions on :root, html,
- * body or * are the theme and always apply, last one winning. A definition
- * on any other selector is a local override: it is used while it is the
- * only value seen for that name, and a second, different local value marks
- * the name ambiguous so var() falls back. Last-wins across components was
- * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
- * .fa-fw, and every icon became 100% wide. */
- static void css_var_set(struct br_doc *d, const char *name,
- unsigned int nlen, const char *val, unsigned int vlen,
- int root)
- {
- unsigned int i, noff, voff;
- if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
- return;
- i = css_var_find(d, name, nlen);
- if (i < BR_MAX_VARS && !root) {
- const char *old = attr_ptr(d, d->var_val[i]);
- unsigned int k = 0;
- d->var_flags[i] |= BR_VAR_SCOPED;
- if (d->var_flags[i] & BR_VAR_ROOT)
- return;
- while (k < vlen && old[k] == val[k])
- k++;
- if (!(k == vlen && old[k] == '\0'))
- d->var_flags[i] |= BR_VAR_CONFLICT;
- return;
- }
- noff = store_raw(d, name, nlen, 63);
- voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
- if (!noff || !voff)
- return;
- if (i >= BR_MAX_VARS) {
- unsigned int h;
- if (d->nvars >= BR_MAX_VARS)
- return;
- i = d->nvars++;
- d->var_name[i] = noff;
- h = var_hash(name, nlen);
- while (d->var_slot[h])
- h = (h + 1) & (VAR_HASH_SIZE - 1);
- d->var_slot[h] = (unsigned short)(i + 1);
- d->var_flags[i] = 0;
- }
- d->var_val[i] = voff;
- if (root)
- d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
- (d->var_flags[i] & BR_VAR_SCOPED));
- else
- d->var_flags[i] |= BR_VAR_SCOPED;
- }
- /* 1 when every selector of the list targets the document root (the theme
- * scope for custom properties): :root, html, body, * or :host, optionally
- * with classes/attributes but no combinator. */
- static int css_root_selector(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int st, en;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len)
- break;
- st = i;
- while (i < len && s[i] != ',')
- i++;
- en = i;
- while (en > st && is_space(s[en - 1]))
- en--;
- if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
- (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
- (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
- (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
- s[st] == '*'))
- return 0;
- for (; st < en; st++)
- if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
- s[st] == '~')
- return 0;
- }
- return 1;
- }
- /* Find the end of a declaration value: the next ';' outside quotes and
- * parentheses. The caller passes the position just after the ':'. */
- static unsigned int css_value_end(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 0;
- char quote = 0;
- while (i < len) {
- char c = s[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- else if (c == '\\' && i + 1 < len)
- i++;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- depth++;
- } else if (c == ')' && depth > 0) {
- depth--;
- } else if (c == ';' && depth == 0) {
- break;
- }
- i++;
- }
- return i;
- }
- /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
- * second palette. We render the light page, so skip their custom-property
- * definitions; otherwise their last-wins values leak into the light rules. */
- static int css_dark_selector(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 <= len; i++) {
- if ((s[i] == 'n' || s[i] == 'N') &&
- (s[i + 1] == 'i' || s[i + 1] == 'I') &&
- (s[i + 2] == 'g' || s[i + 2] == 'G') &&
- (s[i + 3] == 'h' || s[i + 3] == 'H') &&
- (s[i + 4] == 't' || s[i + 4] == 'T'))
- return 1;
- if ((s[i] == 'd' || s[i] == 'D') &&
- (s[i + 1] == 'a' || s[i + 1] == 'A') &&
- (s[i + 2] == 'r' || s[i + 2] == 'R') &&
- (s[i + 3] == 'k' || s[i + 3] == 'K'))
- return 1;
- if (i + 12 <= len &&
- (s[i] == 'c' || s[i] == 'C') &&
- (s[i + 1] == 'l' || s[i + 1] == 'L') &&
- (s[i + 2] == 'i' || s[i + 2] == 'I') &&
- (s[i + 3] == 'e' || s[i + 3] == 'E') &&
- (s[i + 4] == 'n' || s[i + 4] == 'N') &&
- (s[i + 5] == 't' || s[i + 5] == 'T') &&
- (s[i + 6] == 'p' || s[i + 6] == 'P') &&
- (s[i + 7] == 'r' || s[i + 7] == 'R') &&
- (s[i + 8] == 'e' || s[i + 8] == 'E') &&
- (s[i + 9] == 'f' || s[i + 9] == 'F') &&
- s[i + 10] == '-' &&
- (s[i + 11] == 'o' || s[i + 11] == 'O') &&
- (s[i + 12] == 's' || s[i + 12] == 'S'))
- return 1;
- }
- return 0;
- }
- /* Collect --custom-property definitions from one declaration block. */
- static void css_collect_block(struct br_doc *d, const char *s,
- unsigned int len, int root)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int pstart, pend, vstart, vend;
- while (i < len && (is_space(s[i]) || s[i] == ';'))
- i++;
- if (i >= len)
- break;
- pstart = i;
- while (i < len && s[i] != ':' && s[i] != ';')
- i++;
- pend = i;
- if (i >= len || s[i] != ':') {
- if (i < len)
- i++;
- continue;
- }
- i++;
- vstart = i;
- i = css_value_end(s, len, i);
- vend = i;
- if (i < len)
- i++;
- while (pstart < pend && is_space(s[pstart]))
- pstart++;
- while (pend > pstart && is_space(s[pend - 1]))
- pend--;
- while (vstart < vend && is_space(s[vstart]))
- vstart++;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- if (vend - vstart >= 10 &&
- ci_eq_n(s + vend - 10, "!important", 10)) {
- vend -= 10;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- }
- if (pend - pstart >= 3 && s[pstart] == '-' &&
- s[pstart + 1] == '-')
- css_var_set(d, s + pstart, pend - pstart,
- s + vstart, vend - vstart, root);
- }
- }
- const char *br_href(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].href_off);
- }
- const char *br_src(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].src_off);
- }
- const char *br_alt(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].alt_off);
- }
- const char *br_rel(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].rel_off);
- }
- const char *br_value(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].src_off);
- }
- const char *br_placeholder(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].alt_off);
- }
- const char *br_type(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].rel_off);
- }
- /* ---- document setup / node append ---- */
- static void rule_index_reset(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < BR_MAX_RULES; i++)
- d->rule_next[i] = 0xFFFF;
- for (i = 0; i < BR_RULE_HASH; i++)
- d->rule_bucket[i] = 0xFFFF;
- for (i = 0; i < BR_TAG_MAX; i++)
- d->rule_tag[i] = 0xFFFF;
- d->rule_any = 0xFFFF;
- }
- void br_doc_init(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*d); i++)
- ((unsigned char *)d)[i] = 0;
- rule_index_reset(d);
- d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
- d->text = malloc(BR_TEXT_SIZE);
- d->attrs = malloc(BR_ATTR_SIZE);
- if (!d->nodes || !d->text || !d->attrs) {
- free(d->nodes);
- free(d->text);
- free(d->attrs);
- d->nodes = 0;
- d->text = 0;
- d->attrs = 0;
- return;
- }
- d->rules = rule_fallback;
- d->rule_cap = BR_RULE_FALLBACK;
- d->attrs_used = 1; /* offset 0 = "no attribute" */
- d->attrs[0] = '\0';
- d->nvars = 0;
- d->nlayers = 0;
- d->root = 0;
- for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
- d->var_slot[vi] = 0;
- root_font_px = 16;
- bg_url_count = 0;
- bg_url_pool_used = 0;
- font_reset();
- }
- void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
- unsigned int cap)
- {
- if (!buf || !cap) {
- d->rules = rule_fallback;
- d->rule_cap = BR_RULE_FALLBACK;
- d->nrules = 0;
- return;
- }
- if (cap > BR_MAX_RULES)
- cap = BR_MAX_RULES;
- d->rules = buf;
- d->rule_cap = cap;
- d->nrules = 0;
- }
- static void doc_reset(struct br_doc *d)
- {
- unsigned int i;
- d->nnode = 0;
- d->text_used = 0;
- d->attrs_used = 1;
- d->attrs[0] = '\0';
- d->nrules = 0;
- d->ncontent = 0;
- d->npe_rules = 0;
- rule_index_reset(d);
- d->title[0] = '\0';
- d->inline_css_len = 0;
- d->nvars = 0;
- d->nlayers = 0;
- d->truncated = 0;
- d->plain = 0;
- d->script_src = 0;
- d->nscripts = 0;
- d->scripts_truncated = 0;
- font_reset();
- for (i = 0; i < BR_MAX_NODES; i++) {
- d->nodes[i].type = BR_NODE_ELEM;
- d->nodes[i].tag = BR_TAG_UNKNOWN;
- d->nodes[i].flags = 0;
- /* The cached disabled/open/inert bits must not survive into
- * the next page: a slot that held a disabled control made
- * Docusaurus' search field unfocusable after navigating (and
- * real hardware does not hand out zeroed memory). */
- d->nodes[i].attr_flags = 0;
- d->nodes[i].parent = 0;
- d->nodes[i].first = d->nodes[i].last = 0;
- d->nodes[i].next = 0;
- d->nodes[i].text_off = d->nodes[i].text_len = 0;
- d->nodes[i].href_off = d->nodes[i].class_off = 0;
- d->nodes[i].id_off = d->nodes[i].style_off = 0;
- d->nodes[i].src_off = d->nodes[i].alt_off = 0;
- d->nodes[i].color_off = d->nodes[i].rel_off = 0;
- d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
- d->nodes[i].colspan = d->nodes[i].rowspan = 0;
- d->nodes[i].name_off = 0;
- d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
- d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
- d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
- d->nodes[i].svg_rx = 0;
- d->nodes[i].img_index = -1;
- d->nodes[i].bg_img_index = -1;
- d->nodes[i].style_valid = 0;
- d->nodes[i].pe_state = 0;
- }
- }
- static void node_zero(struct br_node *n);
- static unsigned int node_append(struct br_doc *d, unsigned int parent,
- unsigned char type, unsigned char tag)
- {
- unsigned int idx;
- struct br_node *n;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n); /* every field, attr_flags included */
- n->type = type;
- n->tag = tag;
- n->parent = (unsigned short)parent;
- if (d->nodes[parent].last)
- d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
- else
- d->nodes[parent].first = (unsigned short)idx;
- d->nodes[parent].last = (unsigned short)idx;
- return idx;
- }
- static unsigned int append_text(struct br_doc *d, unsigned int parent,
- const char *s, unsigned int len, int pre)
- {
- char *dst;
- unsigned int n;
- unsigned int idx;
- if (len == 0)
- return 0;
- if (d->text_used + 2 >= BR_TEXT_SIZE) {
- d->truncated = 1;
- return 0;
- }
- dst = d->text + d->text_used;
- n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
- if (n == 0) {
- d->text[d->text_used] = '\0';
- return 0;
- }
- idx = node_append(d, parent, BR_NODE_TEXT, 0);
- if (!idx)
- return 0;
- d->nodes[idx].text_off = d->text_used;
- d->nodes[idx].text_len = (unsigned short)n;
- d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
- d->text_used += n + 1;
- return idx;
- }
- /* Raw text child (no entity decoding, whitespace kept): the body of a
- * data-block <script> such as type="application/json", which scripts read
- * through textContent. Bodies longer than a text node holds are dropped. */
- static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
- const char *s, unsigned int len)
- {
- unsigned int idx, i;
- if (len == 0 || len > 0xFFFFu)
- return 0;
- if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
- d->truncated = 1;
- return 0;
- }
- idx = node_append(d, parent, BR_NODE_TEXT, 0);
- if (!idx)
- return 0;
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = s[i];
- d->text[d->text_used + len] = '\0';
- d->nodes[idx].text_off = d->text_used;
- d->nodes[idx].text_len = (unsigned short)len;
- d->nodes[idx].flags = BR_NODE_PREFORMAT;
- d->text_used += len + 1;
- return idx;
- }
- /* ---- runtime DOM helpers (JavaScript bindings) ---- */
- static int same_string(const char *a, const char *b)
- {
- while (*a && *a == *b) {
- a++;
- b++;
- }
- return *a == *b;
- }
- static int same_string_n(const char *a, const char *b, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++)
- if (a[i] != b[i])
- return 0;
- return 1;
- }
- static int node_detached(struct br_doc *d, unsigned int node)
- {
- unsigned int guard = 0;
- while (node && node < d->nnode && guard++ <= d->nnode) {
- if (d->nodes[node].flags & BR_NODE_DETACHED)
- return 1;
- node = d->nodes[node].parent;
- }
- return 0;
- }
- int br_node_detached(struct br_doc *d, unsigned int node)
- {
- return !d || node >= d->nnode || node_detached(d, node);
- }
- unsigned int br_find_id(struct br_doc *d, const char *id)
- {
- unsigned int i;
- if (!d || !id)
- return 0;
- for (i = 0; i < d->nnode; i++) {
- struct br_node *n = &d->nodes[i];
- if (n->type == BR_NODE_ELEM && n->id_off &&
- same_string(d->attrs + n->id_off, id) &&
- !node_detached(d, i))
- return i + 1;
- }
- return 0;
- }
- static void text_walk(struct br_doc *d, unsigned int node, char *out,
- unsigned int cap, unsigned int *used)
- {
- unsigned int c;
- if (node >= d->nnode)
- return;
- if (d->nodes[node].type == BR_NODE_TEXT) {
- unsigned int len = d->nodes[node].text_len;
- unsigned int off = d->nodes[node].text_off;
- if (*used + len >= cap)
- len = *used + 1 < cap ? cap - *used - 1 : 0;
- for (c = 0; c < len; c++)
- out[*used + c] = d->text[off + c];
- *used += len;
- return;
- }
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- text_walk(d, c, out, cap, used);
- }
- unsigned int br_text_content(struct br_doc *d, unsigned int node,
- char *out, unsigned int cap)
- {
- unsigned int used = 0;
- if (!d || !out || cap == 0 || node >= d->nnode)
- return 0;
- text_walk(d, node, out, cap, &used);
- out[used] = '\0';
- return used;
- }
- int br_set_text_content(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len)
- {
- struct br_node *n;
- unsigned int idx;
- unsigned int i;
- if (!d || node >= d->nnode)
- return -1;
- /* Detach the old subtree; its arena storage is reclaimed on the next
- * navigation. */
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- d->nodes[c].flags |= BR_NODE_DETACHED;
- }
- d->nodes[node].first = d->nodes[node].last = 0;
- d->nodes[node].style_valid = 0;
- d->nodes[node].pe_state = 0;
- if (len == 0)
- return 0;
- if (d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return -1;
- }
- idx = node_append(d, node, BR_NODE_TEXT, 0);
- if (!idx)
- return -1;
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- n = &d->nodes[idx];
- n->text_off = d->text_used;
- n->text_len = (unsigned short)len;
- n->flags = 0;
- d->text_used += len + 1;
- return 0;
- }
- /* Replace the value of an existing text or comment node in place, keeping the
- * node handle (and therefore the JS wrapper) valid. */
- int br_set_text_node(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len)
- {
- struct br_node *n;
- unsigned int i;
- if (!d || node >= d->nnode)
- return -1;
- n = &d->nodes[node];
- if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
- return -1;
- if (d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return -1;
- }
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- n->text_off = d->text_used;
- n->text_len = (unsigned short)len;
- d->text_used += len + 1;
- return 0;
- }
- /* ---- runtime DOM construction and mutation ---- */
- static unsigned int text_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void node_zero(struct br_node *n)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*n); i++)
- ((unsigned char *)n)[i] = 0;
- n->img_index = -1;
- n->bg_img_index = -1;
- }
- static void node_unlink(struct br_doc *d, unsigned int handle)
- {
- unsigned int node, parent, c, prev = 0;
- if (!d || handle == 0 || handle > d->nnode)
- return;
- node = handle - 1;
- parent = d->nodes[node].parent;
- if (parent >= d->nnode) {
- d->nodes[node].parent = 0;
- d->nodes[node].next = 0;
- return;
- }
- c = d->nodes[parent].first;
- while (c && c != node) {
- prev = c;
- c = d->nodes[c].next;
- }
- if (c == node) {
- if (prev)
- d->nodes[prev].next = d->nodes[node].next;
- else
- d->nodes[parent].first = d->nodes[node].next;
- if (d->nodes[parent].last == node)
- d->nodes[parent].last = (unsigned short)prev;
- }
- d->nodes[node].parent = 0;
- d->nodes[node].next = 0;
- d->nodes[node].flags |= BR_NODE_DETACHED;
- }
- static void parse_attrs(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len);
- static unsigned int find_tag_end(const char *s, unsigned int i,
- unsigned int len)
- {
- char quote = 0;
- while (i < len) {
- char c = s[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '>') {
- return i;
- }
- i++;
- }
- return len;
- }
- int br_set_inner_html(struct br_doc *d, unsigned int node,
- const char *html, unsigned int len)
- {
- unsigned int stack[64];
- unsigned int sp = 1, i = 0;
- if (!d || node >= d->nnode)
- return -1;
- stack[0] = node;
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- d->nodes[c].flags |= BR_NODE_DETACHED;
- }
- d->nodes[node].first = d->nodes[node].last = 0;
- d->nodes[node].style_valid = 0;
- d->nodes[node].pe_state = 0;
- while (i < len) {
- if (html[i] != '<') {
- unsigned int start = i, end;
- while (i < len && html[i] != '<')
- i++;
- end = i;
- if (end > start) {
- /* Entities decoded like the page parser does
- * (createTextNode stays literal). */
- char *tb = (char *)malloc(end - start + 1);
- unsigned int tl = 0, h = 0;
- if (tb) {
- tl = decode_text(html + start, end - start,
- tb, end - start + 1, 1);
- h = br_create_text(d, tb, tl);
- free(tb);
- }
- if (h)
- br_append_child(d, stack[sp - 1], h);
- }
- continue;
- }
- if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
- html[i + 3] == '-') {
- int p = find_ci(html, len, "-->", 3, i + 4);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int h = br_create_comment(d, html + i + 4,
- end > i + 4 ?
- end - i - 4 : 0);
- if (h)
- br_append_child(d, stack[sp - 1], h);
- i = p >= 0 ? (unsigned int)p + 3 : len;
- continue;
- }
- if (html[i + 1] == '/') {
- unsigned int p = find_tag_end(html, i + 2, len);
- i = p < len ? p + 1 : len;
- if (sp > 1)
- sp--;
- continue;
- }
- if (html[i + 1] == '!' || html[i + 1] == '?') {
- unsigned int p = find_tag_end(html, i + 2, len);
- i = p < len ? p + 1 : len;
- continue;
- }
- {
- unsigned int p = find_tag_end(html, i + 1, len);
- unsigned int ns = i + 1, ne, as, ae;
- unsigned int h, k;
- char name[32];
- int selfclose = 0;
- while (ns < p && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < p && !is_space(html[ne]) && html[ne] != '/')
- ne++;
- as = ne;
- ae = p;
- if (ae > as && html[ae - 1] == '/') {
- selfclose = 1;
- ae--;
- }
- if (ne == ns) {
- i = p < len ? p + 1 : len;
- continue;
- }
- if (ne - ns >= sizeof(name)) {
- i = p < len ? p + 1 : len;
- continue;
- }
- for (k = 0; k < ne - ns; k++)
- name[k] = html[ns + k];
- name[ne - ns] = '\0';
- h = br_create_element(d, name, ne - ns);
- if (!h) {
- i = p < len ? p + 1 : len;
- continue;
- }
- if (ae > as)
- parse_attrs(d, &d->nodes[h - 1], html + as,
- ae - as);
- br_append_child(d, stack[sp - 1], h);
- {
- unsigned int tag = d->nodes[h - 1].tag;
- if (!selfclose && !br_tag_is_void(tag) &&
- sp < sizeof(stack) / sizeof(stack[0]))
- stack[sp++] = h - 1;
- }
- i = p < len ? p + 1 : len;
- }
- }
- return 0;
- }
- unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
- {
- unsigned int i;
- if (!d)
- return 0;
- for (i = 0; i < d->nnode; i++)
- if (d->nodes[i].type == BR_NODE_ELEM &&
- d->nodes[i].tag == (unsigned char)tag &&
- !node_detached(d, i))
- return i + 1;
- return 0;
- }
- unsigned int br_create_element(struct br_doc *d, const char *name,
- unsigned int len)
- {
- struct br_node *n;
- unsigned int idx;
- if (!d || !name)
- return 0;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_ELEM;
- n->flags = BR_NODE_DETACHED;
- n->tag = (unsigned char)br_tag_from_name(name);
- if (n->tag == BR_TAG_UNKNOWN && len)
- n->name_off = store_attr(d, name, len, 31);
- return idx + 1;
- }
- unsigned int br_create_text(struct br_doc *d, const char *text,
- unsigned int len)
- {
- struct br_node *n;
- unsigned int idx, i;
- if (!d)
- return 0;
- if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_TEXT;
- n->flags = BR_NODE_DETACHED;
- n->text_off = d->text_used;
- n->text_len = (unsigned short)len;
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- d->text_used += len + 1;
- return idx + 1;
- }
- unsigned int br_create_comment(struct br_doc *d, const char *text,
- unsigned int len)
- {
- unsigned int h = br_create_text(d, text, len);
- if (h)
- d->nodes[h - 1].type = BR_NODE_COMMENT;
- return h;
- }
- unsigned int br_create_fragment(struct br_doc *d)
- {
- struct br_node *n;
- unsigned int idx;
- if (!d)
- return 0;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_FRAGMENT;
- n->flags = BR_NODE_DETACHED;
- return idx + 1;
- }
- int br_append_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- unsigned int p;
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
- parent == child - 1)
- return -1;
- if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
- /* Appending a fragment inserts its children, not the fragment. */
- unsigned int c = d->nodes[child - 1].first;
- while (c) {
- unsigned int next = d->nodes[c].next;
- br_append_child(d, parent, c + 1);
- c = next;
- }
- return 0;
- }
- for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
- if (p == child - 1)
- return -1;
- node_unlink(d, child);
- d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
- d->nodes[child - 1].parent = (unsigned short)parent;
- d->nodes[child - 1].next = 0;
- if (d->nodes[parent].last)
- d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
- else
- d->nodes[parent].first = (unsigned short)(child - 1);
- d->nodes[parent].last = (unsigned short)(child - 1);
- return 0;
- }
- int br_insert_before(struct br_doc *d, unsigned int parent,
- unsigned int child, unsigned int before)
- {
- unsigned int prev = 0, c;
- if (before == 0)
- return br_append_child(d, parent, child);
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
- before > d->nnode || parent == child - 1)
- return -1;
- if (d->nodes[before - 1].parent != parent)
- return -1;
- if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
- unsigned int c = d->nodes[child - 1].first;
- while (c) {
- unsigned int next = d->nodes[c].next;
- br_insert_before(d, parent, c + 1, before);
- c = next;
- }
- return 0;
- }
- {
- unsigned int p;
- for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
- if (p == child - 1)
- return -1;
- }
- node_unlink(d, child);
- d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
- c = d->nodes[parent].first;
- while (c && c != before - 1) {
- prev = c;
- c = d->nodes[c].next;
- }
- if (c != before - 1)
- return -1;
- d->nodes[child - 1].parent = (unsigned short)parent;
- d->nodes[child - 1].next = (unsigned short)(before - 1);
- if (prev)
- d->nodes[prev].next = (unsigned short)(child - 1);
- else
- d->nodes[parent].first = (unsigned short)(child - 1);
- return 0;
- }
- int br_remove_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- if (!d || child == 0 || child > d->nnode ||
- d->nodes[child - 1].parent != parent)
- return -1;
- node_unlink(d, child);
- return 0;
- }
- unsigned int br_find_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- unsigned int c;
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
- return 0;
- for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
- if (c == child - 1)
- return child;
- return 0;
- }
- const char *br_find_attr(struct br_doc *d, unsigned int node,
- const char *name)
- {
- struct br_node *n;
- const char *p, *end, *found = 0;
- if (!d || node >= d->nnode || !name)
- return 0;
- n = &d->nodes[node];
- p = d->attrs + n->extra_off;
- end = p + n->extra_len;
- while (p + 1 < end) {
- const char *an = p;
- const char *av;
- p += text_len(p) + 1;
- if (p >= end)
- break;
- av = p;
- p += text_len(p) + 1;
- if (same_string(an, name))
- found = av;
- }
- if (found)
- return found;
- if (same_string(name, "id") && n->id_off)
- return d->attrs + n->id_off;
- if (same_string(name, "class") && n->class_off)
- return d->attrs + n->class_off;
- if (same_string(name, "style") && n->style_off)
- return d->attrs + n->style_off;
- if (same_string(name, "href") && n->href_off)
- return d->attrs + n->href_off;
- if (same_string(name, "src") && n->src_off)
- return d->attrs + n->src_off;
- if (same_string(name, "alt") && n->alt_off)
- return d->attrs + n->alt_off;
- if (same_string(name, "rel") && n->rel_off)
- return d->attrs + n->rel_off;
- return 0;
- }
- static void attr_note_known(struct br_doc *d, struct br_node *n,
- const char *name, const char *value,
- unsigned int len)
- {
- int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
- n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
- /* Form controls keep type/value/placeholder in the rel/src/alt slots,
- * exactly like parse_attrs(); script-built inputs (Vue sets
- * type="password" after createElement) must land there too. */
- if (control && same_string(name, "type")) {
- n->rel_off = len ? store_attr(d, value, len, 40) : 0;
- return;
- }
- if (control && same_string(name, "value")) {
- n->src_off = len ? store_attr(d, value, len, 220) : 0;
- return;
- }
- if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
- same_string(name, "placeholder")) {
- n->alt_off = len ? store_attr(d, value, len, 120) : 0;
- return;
- }
- if (same_string(name, "id"))
- n->id_off = len ? store_attr(d, value, len, 64) : 0;
- else if (same_string(name, "class"))
- n->class_off = len ? store_attr(d, value, len,
- BR_CLASS_MAX) : 0;
- else if (same_string(name, "style"))
- n->style_off = len ? store_attr(d, value, len,
- BR_STYLE_ATTR_MAX) : 0;
- else if (same_string(name, "href"))
- n->href_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
- else if (same_string(name, "src"))
- n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
- else if (same_string(name, "alt"))
- n->alt_off = len ? store_attr(d, value, len, 120) : 0;
- else if (same_string(name, "rel"))
- n->rel_off = len ? store_attr(d, value, len, 64) : 0;
- }
- int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
- const char *value, unsigned int len)
- {
- struct br_node *n;
- unsigned int start, nl, need;
- const char *p, *old, *oldend;
- if (!d || node >= d->nnode || !name)
- return -1;
- n = &d->nodes[node];
- nl = text_len(name);
- need = nl + 1 + len + 1;
- start = d->attrs_used;
- old = d->attrs + n->extra_off;
- oldend = old + n->extra_len;
- if (start + n->extra_len + need > BR_ATTR_SIZE) {
- d->truncated = 1;
- return -1;
- }
- p = old;
- while (p + 1 < oldend) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl, k;
- anl = text_len(an);
- p += anl + 1;
- if (p >= oldend)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- if (same_string(an, name))
- continue;
- for (k = 0; k < anl; k++)
- d->attrs[d->attrs_used++] = an[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < avl; k++)
- d->attrs[d->attrs_used++] = av[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- {
- unsigned int k;
- for (k = 0; k < nl; k++)
- d->attrs[d->attrs_used++] = name[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < len; k++)
- d->attrs[d->attrs_used++] = value[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- n = &d->nodes[node];
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- n->style_valid = 0;
- n->pe_state = 0;
- attr_note_known(d, n, name, value, len);
- /* A script that sets a style/class (Angular's ng-style) or a new src
- * after the first image scan re-arms the node for the loader; -2
- * ("nothing to load") would otherwise be final. */
- if ((same_string(name, "style") || same_string(name, "class")) &&
- n->bg_img_index == -2)
- n->bg_img_index = -1;
- if (same_string(name, "src") && n->img_index == -2)
- n->img_index = -1;
- return 0;
- }
- static void clear_attr_flag(struct br_node *n, const char *name);
- int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
- {
- struct br_node *n;
- unsigned int start;
- const char *p, *oldend;
- int found = 0;
- if (!d || node >= d->nnode || !name)
- return -1;
- n = &d->nodes[node];
- start = d->attrs_used;
- p = d->attrs + n->extra_off;
- oldend = p + n->extra_len;
- if (start + n->extra_len > BR_ATTR_SIZE)
- return -1;
- while (p + 1 < oldend) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl, k;
- anl = text_len(an);
- p += anl + 1;
- if (p >= oldend)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- if (same_string(an, name)) {
- found = 1;
- continue;
- }
- for (k = 0; k < anl; k++)
- d->attrs[d->attrs_used++] = an[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < avl; k++)
- d->attrs[d->attrs_used++] = av[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- n = &d->nodes[node];
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- n->style_valid = 0;
- n->pe_state = 0;
- attr_note_known(d, n, name, "", 0);
- if (found)
- clear_attr_flag(n, name);
- return found ? 0 : -1;
- }
- unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
- {
- struct br_node *src;
- unsigned int h;
- const char *name;
- if (!d || node == 0 || node > d->nnode)
- return 0;
- src = &d->nodes[node - 1];
- if (src->type == BR_NODE_TEXT)
- return br_create_text(d, d->text + src->text_off, src->text_len);
- if (src->type == BR_NODE_COMMENT)
- return br_create_comment(d, d->text + src->text_off,
- src->text_len);
- if (src->type == BR_NODE_FRAGMENT)
- h = br_create_fragment(d);
- else {
- if (src->tag == BR_TAG_UNKNOWN && src->name_off)
- name = d->attrs + src->name_off;
- else
- name = br_tag_name(src->tag);
- h = br_create_element(d, name, text_len(name));
- }
- if (!h)
- return 0;
- {
- const char *p = d->attrs + src->extra_off;
- const char *end = p + src->extra_len;
- while (p + 1 < end) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl;
- anl = text_len(an);
- p += anl + 1;
- if (p >= end)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- /* h is a 1-based handle; br_set_attr takes the
- * node index (the old call stamped the copy's
- * attributes onto the previous node). */
- br_set_attr(d, h - 1, an, av, avl);
- }
- }
- if (deep) {
- unsigned int c;
- for (c = src->first; c; c = d->nodes[c].next) {
- unsigned int ch = br_clone_node(d, c + 1, 1);
- if (ch)
- br_append_child(d, h - 1, ch);
- }
- }
- return h;
- }
- static int sel_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len);
- static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
- unsigned int *st, unsigned int *en);
- /* querySelector(All) and Element.matches use the cascade's selector engine
- * (sel_match(), below). */
- static int match_selector(struct br_doc *d, unsigned int node,
- const char *sel, unsigned int len)
- {
- while (len > 0 && sel[0] == ' ') {
- sel++;
- len--;
- }
- while (len > 0 && sel[len - 1] == ' ')
- len--;
- return len && sel_match(d, node, sel, len);
- }
- static int query_add(unsigned int *out, unsigned int max,
- unsigned int *count, unsigned int node)
- {
- unsigned int i;
- for (i = 0; i < *count; i++)
- if (out[i] == node + 1)
- return 0;
- if (*count < max)
- out[(*count)++] = node + 1;
- return 0;
- }
- static void query_walk(struct br_doc *d, unsigned int node,
- const char *sel, unsigned int len, unsigned int *out,
- unsigned int max, unsigned int *count)
- {
- unsigned int c;
- if (d->nodes[node].type == BR_NODE_ELEM &&
- match_selector(d, node, sel, len))
- query_add(out, max, count, node);
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- query_walk(d, c, sel, len, out, max, count);
- }
- int br_matches(struct br_doc *d, unsigned int node, const char *selector)
- {
- unsigned int len;
- if (!d || !selector || node == 0 || node > d->nnode)
- return 0;
- len = text_len(selector);
- while (len > 0 && selector[0] == ' ') {
- selector++;
- len--;
- }
- while (len > 0 && selector[len - 1] == ' ')
- len--;
- {
- unsigned int pos = 0, st, en;
- while (sel_list_next(selector, len, &pos, &st, &en))
- if (match_selector(d, node - 1, selector + st, en - st))
- return 1;
- }
- return 0;
- }
- unsigned int br_query_all(struct br_doc *d, unsigned int root,
- const char *selector, unsigned int *out,
- unsigned int max)
- {
- unsigned int count = 0, i = 0, len, start, end;
- if (!d || !selector || !out || max == 0)
- return 0;
- len = text_len(selector);
- while (sel_list_next(selector, len, &i, &start, &end)) {
- if (root == 0) {
- unsigned int n;
- for (n = 1; n < d->nnode; n++)
- if (d->nodes[n].type == BR_NODE_ELEM &&
- !node_detached(d, n) &&
- match_selector(d, n, selector + start,
- end - start))
- query_add(out, max, &count, n);
- } else if (root <= d->nnode) {
- unsigned int c;
- for (c = d->nodes[root - 1].first; c;
- c = d->nodes[c].next)
- query_walk(d, c, selector + start,
- end - start, out, max, &count);
- }
- }
- return count;
- }
- /* ---- attribute parsing ---- */
- static int is_space(char c)
- {
- return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
- }
- static int str_eq(const char *a, const char *b);
- static unsigned int parse_uint(const char *s, unsigned int len)
- {
- unsigned int i = 0, v = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- if (v > 4096)
- v = 4096;
- i++;
- }
- return v;
- }
- static int contains_ci_len(const char *s, unsigned int len,
- const char *needle);
- static int parse_svg_viewbox(const char *s, unsigned int len,
- unsigned short *w, unsigned short *h)
- {
- unsigned int i = 0, v[4], n = 0;
- while (i < len && n < 4) {
- unsigned int x = 0;
- int any = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- x = x * 10 + (unsigned int)(s[i] - '0');
- i++;
- any = 1;
- }
- /* viewBox values are commonly fractional ("0 0 340.16
- * 340.16"). Consume the fraction so it is not mistaken for
- * the next number; the integer part is enough for the aspect
- * ratio used by the renderer. */
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9')
- i++;
- }
- if (!any) {
- if (i < len)
- i++;
- continue;
- }
- v[n++] = x;
- }
- if (n < 4)
- return 0;
- *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
- *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
- return *w && *h;
- }
- /* The inverse of note_attr_flag() for a removed attribute. The cached
- * bit used to outlive the attribute: `input.disabled = false` removed
- * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
- * once (Docusaurus' search box while its index loads) never took focus
- * again. */
- static void clear_attr_flag(struct br_node *n, const char *name)
- {
- if (str_eq(name, "data-collapse-target"))
- n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
- else if (str_eq(name, "data-collapse-trigger"))
- n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
- else if (str_eq(name, "is-open"))
- n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
- else if (str_eq(name, "disabled"))
- n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
- else if (str_eq(name, "open"))
- n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
- else if (str_eq(name, "inert"))
- n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
- }
- static void note_attr_flag(struct br_node *n, const char *name)
- {
- if (str_eq(name, "data-collapse-target"))
- n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
- else if (str_eq(name, "data-collapse-trigger"))
- n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
- else if (str_eq(name, "is-open"))
- n->attr_flags |= BR_ATTR_IS_OPEN;
- else if (str_eq(name, "disabled"))
- n->attr_flags |= BR_ATTR_DISABLED;
- else if (str_eq(name, "open"))
- n->attr_flags |= BR_ATTR_OPEN;
- else if (str_eq(name, "inert"))
- n->attr_flags |= BR_ATTR_INERT;
- }
- /* Store a packed name/value list for getAttribute()/selectors. This is a
- * second pass so the block stays contiguous even though the known attributes
- * above also use the shared attr arena. */
- /* Attribute value copy with the markup entities decoded (& < >
- * " ' ' and ASCII numeric references); other bytes are kept
- * as they are, so URLs and JSON in attributes survive. Returns the bytes
- * written. */
- static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
- {
- static const struct { const char *e; char c; } ents[] = {
- { "&", '&' }, { "<", '<' }, { ">", '>' },
- { """, '"' }, { "'", '\'' }, { "'", '\'' },
- { 0, 0 }
- };
- unsigned int i = 0, n = 0;
- while (i < len) {
- int k, done = 0;
- if (s[i] == '&') {
- for (k = 0; ents[k].e; k++) {
- unsigned int el = (unsigned int)strlen(ents[k].e);
- if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
- out[n++] = ents[k].c;
- i += el;
- done = 1;
- break;
- }
- }
- if (!done && i + 3 < len && s[i + 1] == '#') {
- unsigned int j = i + 2, v = 0;
- int hex = s[j] == 'x' || s[j] == 'X';
- if (hex)
- j++;
- while (j < len && j < i + 10 &&
- ((s[j] >= '0' && s[j] <= '9') ||
- (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
- (s[j] >= 'A' && s[j] <= 'F'))))) {
- char c = s[j++];
- v = v * (hex ? 16 : 10) +
- (unsigned int)(c <= '9' ? c - '0' :
- (c | 0x20) - 'a' + 10);
- }
- if (j < len && s[j] == ';' && v > 0 && v < 128) {
- out[n++] = (char)v;
- i = j + 1;
- done = 1;
- }
- }
- }
- if (!done)
- out[n++] = s[i++];
- }
- return n;
- }
- static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len)
- {
- unsigned int i = 0;
- unsigned int start = d->attrs_used;
- while (i < len) {
- char name[32];
- unsigned int nl = 0, vstart, vlen;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
- char c = s[i++];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (nl + 1 < sizeof(name))
- name[nl++] = c;
- }
- name[nl] = '\0';
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '=') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vstart = i;
- while (i < len && s[i] != quote)
- i++;
- vlen = i - vstart;
- if (i < len)
- i++;
- } else {
- vstart = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- vlen = i - vstart;
- }
- } else {
- /* Boolean attribute: expose it with an empty value. */
- vstart = 0;
- vlen = 0;
- }
- if (!nl)
- continue;
- if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
- d->truncated = 1;
- break;
- }
- {
- unsigned int k;
- for (k = 0; k < nl; k++)
- d->attrs[d->attrs_used++] = name[k];
- d->attrs[d->attrs_used++] = '\0';
- d->attrs_used += attr_unescape(s + vstart, vlen,
- d->attrs + d->attrs_used);
- d->attrs[d->attrs_used++] = '\0';
- }
- }
- if (d->attrs_used > start) {
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- }
- }
- /* Parse attributes from `s` (between the tag name and '>'). */
- static void parse_attrs(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- char name[32];
- unsigned int nl = 0, vstart, vlen;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
- char c = s[i++];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (nl + 1 < sizeof(name))
- name[nl++] = c;
- }
- name[nl] = '\0';
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] != '=') {
- if (str_eq(name, "hidden"))
- n->flags |= BR_NODE_HIDDEN;
- note_attr_flag(n, name);
- continue;
- }
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vstart = i;
- while (i < len && s[i] != quote)
- i++;
- vlen = i - vstart;
- if (i < len)
- i++;
- } else {
- vstart = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- vlen = i - vstart;
- }
- if (!nl)
- continue;
- if (str_eq(name, "hidden")) {
- n->flags |= BR_NODE_HIDDEN;
- continue;
- }
- note_attr_flag(n, name);
- if (str_eq(name, "href"))
- n->href_off = store_attr(d, s + vstart, vlen,
- BR_URL_ATTR_MAX);
- else if (str_eq(name, "class"))
- n->class_off = store_attr(d, s + vstart, vlen,
- BR_CLASS_MAX);
- else if (str_eq(name, "id"))
- n->id_off = store_attr(d, s + vstart, vlen, 64);
- else if (str_eq(name, "style"))
- n->style_off = store_attr(d, s + vstart, vlen,
- BR_STYLE_ATTR_MAX);
- else if (str_eq(name, "src"))
- n->src_off = store_attr(d, s + vstart, vlen,
- BR_URL_ATTR_MAX);
- else if (str_eq(name, "alt"))
- n->alt_off = store_attr(d, s + vstart, vlen, 120);
- else if (str_eq(name, "color"))
- n->color_off = store_attr(d, s + vstart, vlen, 32);
- else if (str_eq(name, "rel"))
- n->rel_off = store_attr(d, s + vstart, vlen, 64);
- else if (str_eq(name, "value"))
- n->src_off = store_attr(d, s + vstart, vlen, 220);
- else if (str_eq(name, "placeholder") &&
- (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
- n->alt_off = store_attr(d, s + vstart, vlen, 120);
- else if (str_eq(name, "type") &&
- (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
- n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
- n->rel_off = store_attr(d, s + vstart, vlen, 40);
- else if (str_eq(name, "width")) {
- n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
- if (vlen && s[vstart + vlen - 1] == '%')
- n->width_pct = 1;
- } else if (str_eq(name, "height")) {
- n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
- if (vlen && s[vstart + vlen - 1] == '%')
- n->height_pct = 1;
- } else if (str_eq(name, "colspan")) {
- unsigned int v = parse_uint(s + vstart, vlen);
- if (v < 1)
- v = 1;
- if (v > 16)
- v = 16;
- n->colspan = (unsigned char)v;
- } else if (str_eq(name, "rowspan")) {
- unsigned int v = parse_uint(s + vstart, vlen);
- if (v < 1)
- v = 1;
- if (v > 16)
- v = 16;
- n->rowspan = (unsigned char)v;
- } else if (str_eq(name, "x") || str_eq(name, "x1")) {
- n->svg_x = (signed short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "y") || str_eq(name, "y1")) {
- n->svg_y = (signed short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "x2")) {
- n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "y2")) {
- n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "rx")) {
- n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "d")) {
- n->src_off = store_attr(d, s + vstart, vlen, 300);
- } else if (str_eq(name, "viewbox")) {
- unsigned short vw = 0, vh = 0;
- if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
- n->width_attr = vw;
- n->height_attr = vh;
- n->flags |= BR_NODE_VIEWBOX;
- }
- } else if (str_eq(name, "text-anchor")) {
- if (contains_ci_len(s + vstart, vlen, "middle"))
- n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
- else if (contains_ci_len(s + vstart, vlen, "end"))
- n->attr_flags |= BR_ATTR_ANCHOR_END;
- }
- }
- parse_attrs_generic(d, n, s, len);
- }
- static int str_eq(const char *a, const char *b)
- {
- unsigned int i = 0;
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- return a[i] == '\0' && b[i] == '\0';
- }
- /* ---- CSS ---- */
- static int parse_color(const char *s, unsigned int len, unsigned short *out);
- int br_parse_color(const char *s, unsigned int len, unsigned short *out)
- {
- return parse_color(s, len, out);
- }
- /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
- static int paint_mode(const char *s, unsigned int len, unsigned short *c)
- {
- while (len && is_space(*s)) {
- s++;
- len--;
- }
- while (len && is_space(s[len - 1]))
- len--;
- if (!len)
- return 0;
- if (len == 4 && ci_eq_n(s, "none", 4))
- return BR_PAINT_NONE;
- if (len == 12 && ci_eq_n(s, "currentcolor", 12))
- return BR_PAINT_CURRENT;
- if (len > 4 && ci_eq_n(s, "url(", 4))
- return BR_PAINT_URL;
- if (len == 11 && ci_eq_n(s, "transparent", 11))
- return BR_PAINT_NONE;
- if (parse_color(s, len, c))
- return BR_PAINT_COLOR;
- return 0;
- }
- /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
- static int svg_len16(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- int v = 0, frac = 0, digits = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (v < 4000)
- v = v * 10 + (s[i] - '0');
- i++;
- digits++;
- }
- if (i < len && s[i] == '.') {
- int scale = 1;
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 10000) {
- frac = frac * 10 + (s[i] - '0');
- scale *= 10;
- }
- i++;
- digits++;
- }
- frac = frac * 16 / scale;
- }
- if (!digits)
- return -1;
- return v * 16 + frac;
- }
- /* Inside an <svg> subtree (including the <svg> element itself). */
- static int in_svg(const struct br_doc *d, unsigned int node)
- {
- unsigned int k;
- for (k = 0; node && node < d->nnode && k < 64; k++) {
- if (d->nodes[node].tag == BR_TAG_SVG)
- return 1;
- node = d->nodes[node].parent;
- }
- return 0;
- }
- enum {
- PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
- PROP_BG_REPEAT,
- PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
- PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
- PROP_FONT_PX, PROP_FONT_UNIT,
- PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
- PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
- PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
- PROP_PT, PROP_PR, PROP_PB, PROP_PL,
- PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
- PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
- PROP_BORDER_W, PROP_BORDER_COLOR,
- PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
- PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
- PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
- PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
- PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
- PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
- PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
- PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
- PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
- PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
- PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
- PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
- PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
- PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
- PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
- PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
- PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
- PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
- PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
- PROP_JUSTIFY, PROP_ALIGN_ITEMS,
- PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
- PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
- PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
- PROP_ZINDEX, PROP_ZSET,
- PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
- PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
- PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
- PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
- PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
- PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
- PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
- PROP_COUNT
- };
- struct color_name {
- const char *name;
- unsigned short rgb;
- };
- static const struct color_name color_names[] = {
- { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
- { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
- { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
- { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
- { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
- { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
- { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
- { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
- { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
- { "darkgrey", 0x528A }, { 0, 0 }
- };
- static int hexval(char c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- return -1;
- }
- static int named_color(const char *s, unsigned int len, unsigned short *out)
- {
- unsigned int i;
- for (i = 0; color_names[i].name; i++) {
- unsigned int j, n = s_len(color_names[i].name);
- if (n != len)
- continue;
- for (j = 0; j < n; j++) {
- char c = s[j];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (c != color_names[i].name[j])
- break;
- }
- if (j == n) {
- *out = color_names[i].rgb;
- return 1;
- }
- }
- return 0;
- }
- /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
- * over white; the render target is a white page. */
- static int parse_color(const char *s, unsigned int len, unsigned short *out)
- {
- unsigned int i = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '#') {
- int d[6], nd = 0;
- i++;
- while (i < len && nd < 6 && hexval(s[i]) >= 0)
- d[nd++] = hexval(s[i++]);
- if (nd == 3) {
- unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
- unsigned int b = (unsigned int)d[2];
- *out = (unsigned short)(((r * 17 >> 3) << 11) |
- ((g * 17 >> 2) << 5) |
- (b * 17 >> 3));
- return 1;
- }
- if (nd >= 6) {
- unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
- unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
- unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
- *out = (unsigned short)(((r >> 3) << 11) |
- ((g >> 2) << 5) | (b >> 3));
- return 1;
- }
- return 0;
- }
- if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
- unsigned int v[3], k = 0, a = 255;
- while (i < len && s[i] != '(')
- i++;
- if (i >= len)
- return 0;
- i++;
- while (k < 3 && i < len) {
- unsigned int x = 0;
- int percent = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len || s[i] < '0' || s[i] > '9')
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- x = x * 10 + (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '%') {
- percent = 1;
- i++;
- }
- if (percent)
- x = x * 255 / 100;
- if (x > 255)
- x = 255;
- v[k++] = x;
- }
- /* Optional alpha in both syntaxes:
- * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
- {
- unsigned int whole = 0, frac = 0, scale = 1;
- int percent = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i < len && s[i] == '/') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- }
- /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
- * used to read as opaque black. */
- if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
- (s[i] == '.' && i + 1 < len &&
- s[i + 1] >= '0' && s[i + 1] <= '9'))) {
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 +
- (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' &&
- s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 +
- (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- }
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%')
- percent = 1;
- if (percent)
- a = whole > 100 ? 255 :
- whole * 255 / 100;
- else if (whole >= 1)
- a = 255;
- else
- a = frac * 255 / scale;
- }
- }
- if (k == 3) {
- unsigned int r = v[0], g = v[1], b = v[2];
- if (a < 255) {
- r = (r * a + 255u * (255u - a)) / 255u;
- g = (g * a + 255u * (255u - a)) / 255u;
- b = (b * a + 255u * (255u - a)) / 255u;
- }
- *out = (unsigned short)(((r >> 3) << 11) |
- ((g >> 2) << 5) | (b >> 3));
- return 1;
- }
- return 0;
- }
- {
- unsigned int j = i;
- while (i < len && !is_space(s[i]))
- i++;
- return named_color(s + j, i - j, out);
- }
- }
- /* Fully transparent colours have no RGB565 representation. They are common
- * as pre-allocated borders ("border:.1rem solid #0000") that become visible
- * only on hover; painting them black would draw an outline around every
- * pill and card. */
- static int color_is_transparent(const char *s, unsigned int len)
- {
- unsigned int i = 0, n = 0;
- int last = 0, prev = 0, d;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
- return 1;
- if (i >= len || s[i] != '#')
- return 0;
- i++;
- while (i < len && hexval(s[i]) >= 0) {
- d = hexval(s[i]);
- prev = last;
- last = d;
- n++;
- i++;
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i != len)
- return 0;
- if (n == 4)
- return last == 0;
- if (n == 8)
- return prev == 0 && last == 0;
- return 0;
- }
- static int register_bg_url(const char *s, unsigned int len)
- {
- char tmp[BR_BG_URL_MAX + 1];
- unsigned int i, n = 0;
- char q = 0;
- while (len && is_space(*s)) {
- s++;
- len--;
- }
- while (len && is_space(s[len - 1]))
- len--;
- if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
- s[len - 1] == s[0]) {
- q = s[0];
- s++;
- len -= 2;
- }
- /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
- for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
- if (q && s[i] == '\\' && i + 1 < len) {
- i++;
- if (s[i] == '\n')
- continue;
- }
- tmp[n++] = s[i];
- }
- tmp[n] = '\0';
- if (!n)
- return 0;
- for (i = 0; i < bg_url_count; i++)
- if (!strcmp(bg_urls[i], tmp))
- return (int)i + 1;
- if (bg_url_count >= BR_BG_URLS ||
- bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
- return 0;
- memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
- bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
- bg_url_pool_used += n + 1;
- return (int)(++bg_url_count);
- }
- /* Length values for margins/padding: px, ignore percentages/units. */
- static unsigned int viewport_w;
- static int parse_len(const char *s, unsigned int len, unsigned int *pos)
- {
- unsigned int i = *pos;
- int sign = 1;
- long v = 0; /* hundredths of a unit before conversion */
- int digits = 0, frac = 0, unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- if (s[i] == '-')
- sign = -1;
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Minified CSS writes ".5rem" without a leading zero. */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (i < len && s[i] == '%')
- unit = 2;
- else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm')
- unit = 1;
- else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
- unit = 3;
- while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
- s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
- s[i] == 'a' || s[i] == 'd'))
- i++;
- *pos = i;
- if (!digits)
- return 0;
- if (unit == 1)
- v *= root_font_px;
- else if (unit == 3)
- v *= root_font_px;
- else if (unit == 2)
- v = v * (long)viewport_w / 100;
- v /= 100;
- v *= sign;
- if (v < -2048)
- v = -2048;
- if (v > 2048)
- v = 2048;
- return (int)v;
- }
- /* Margin values may be the CSS keyword "auto". parse_len() deliberately
- * treats it as a zero-length token; margins need to keep it so block layout
- * can centre a box with a definite width. */
- #define CSS_LEN_AUTO (-32768)
- /* A box length with its unit preserved for layout-time resolution:
- * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
- * rem and whole pixels for px. */
- static int parse_css_len(const char *s, unsigned int len,
- unsigned int *pos, int *unit)
- {
- unsigned int i = *pos;
- int sign = 1;
- long v = 0;
- int digits = 0, frac = 0;
- *unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- if (s[i] == '-')
- sign = -1;
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Leading-dot fractions are common in minified CSS
- * (".5rem", ".3rem"). */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- if (!digits) {
- *pos = i;
- return 0;
- }
- if (i < len && s[i] == '%') {
- *unit = 1;
- } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm') {
- *unit = 3;
- i += 3;
- } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
- *unit = 2;
- i += 2;
- } else {
- v /= 100;
- }
- while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
- s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
- s[i] == 'a' || s[i] == 'd'))
- i++;
- *pos = i;
- v *= sign;
- if (v < -32768)
- v = -32768;
- if (v > 32767)
- v = 32767;
- return (int)v;
- }
- static int parse_margin_len(const char *s, unsigned int len,
- unsigned int *pos, int *unit)
- {
- unsigned int i = *pos;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
- (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
- *pos = i + 4;
- *unit = 4;
- return CSS_LEN_AUTO;
- }
- return parse_css_len(s, len, pos, unit);
- }
- static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
- /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
- static int parse_margin_calc(const char *s, unsigned int len,
- unsigned int *pos, int *pct, int *px)
- {
- unsigned int i = *pos, j;
- if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
- return 0;
- i += 5;
- j = i;
- while (j < len && s[j] != ')')
- j++;
- if (j >= len)
- return 0;
- if (!parse_calc(s + i, j - i, pct, px))
- return 0;
- *pos = j + 1;
- return 1;
- }
- /* calc(P% +/- Npx) for the common width/margin forms. */
- static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
- {
- unsigned int i = 0;
- int sign = 1;
- int have = 0;
- *pct = 0;
- *px = 0;
- while (i < len) {
- unsigned int j;
- long whole = 0, frac = 0, scale = 1;
- int digits = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- break;
- if (s[i] == '+' || s[i] == '-') {
- sign = s[i] == '-' ? -1 : 1;
- i++;
- continue;
- }
- if (s[i] < '0' || s[i] > '9') {
- if (s[i] == '.') {
- /* fall through to the number parser */
- } else {
- i++;
- continue;
- }
- }
- j = i;
- while (j < len && s[j] >= '0' && s[j] <= '9') {
- whole = whole * 10 + (s[j] - '0');
- digits = 1;
- j++;
- }
- if (j < len && s[j] == '.') {
- j++;
- while (j < len && s[j] >= '0' && s[j] <= '9') {
- frac = frac * 10 + (s[j] - '0');
- scale *= 10;
- j++;
- }
- }
- if (!digits) {
- i = j + 1;
- continue;
- }
- while (j < len && is_space(s[j]))
- j++;
- if (j < len && s[j] == '%') {
- *pct = sign * (int)(whole + (scale > 1 ?
- frac * 100 / scale : 0));
- have = 1;
- j++;
- } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
- *px = sign * (int)whole;
- have = 1;
- j += 2;
- }
- i = j;
- while (i < len && !is_space(s[i]))
- i++;
- sign = 1;
- }
- return have;
- }
- /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
- static int parse_size(const char *s, unsigned int len, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = 0;
- unsigned long v = 0;
- int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
- *val = 0;
- *kind = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
- return 0;
- if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (unsigned int)(s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%')
- pct = 1;
- else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm')
- rem = 1;
- else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
- rem = 2;
- else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
- vw = 1;
- else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
- ; /* px: already the default scale */
- else if (i < len && (is_ident(s[i]) || s[i] == '#'))
- bad_unit = 1;
- if (bad_unit)
- return 0;
- if (rem == 1 || rem == 2) {
- if (v > 65535)
- v = 65535;
- *val = (unsigned short)v;
- *kind = rem == 1 ? 5 : 4;
- return 1;
- }
- v /= 100;
- if (vw)
- v = v * viewport_w / 100;
- if (pct) {
- if (v > 100)
- v = 100;
- *kind = 2;
- } else {
- if (v > 2048)
- v = 2048;
- *kind = 1;
- }
- *val = (unsigned short)v;
- return 1;
- }
- static int parse_font_px(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, fscale = 1, digits = 0;
- int v1000, px;
- int is_px = 1, is_rem = 0, is_pct = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
- return 24;
- if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
- return 32;
- if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
- return 14;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (fscale < 1000) {
- frac = frac * 10 + (s[i] - '0');
- fscale *= 10;
- }
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- is_rem = 1;
- is_px = 0;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- is_rem = 1;
- is_px = 0;
- } else if (i < len && s[i] == '%') {
- is_pct = 1;
- is_px = 0;
- }
- v1000 = whole * 1000 + frac * 1000 / fscale;
- if (is_rem)
- px = v1000 * root_font_px / 1000;
- else if (is_pct)
- px = v1000 * root_font_px / 100000;
- else if (is_px)
- px = v1000 / 1000;
- else
- px = v1000 / 1000;
- if (px < 8)
- px = 8;
- if (px > 72)
- px = 72;
- return px;
- }
- /* font-size keeping the unit so em/% resolve against the parent face. */
- static int parse_font_size(const char *s, unsigned int len,
- unsigned short *raw, unsigned char *unit)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, scale = 1, digits = 0;
- int v100;
- *raw = 0;
- *unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
- *raw = 24;
- *unit = 1;
- return 1;
- }
- if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
- *raw = 32;
- *unit = 1;
- return 1;
- }
- if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
- *raw = 14;
- *unit = 1;
- return 1;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- frac = frac * 10 + (s[i] - '0');
- scale *= 10;
- i++;
- }
- }
- if (!digits && !frac)
- return 0;
- v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
- while (i < len && is_space(s[i]))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- *raw = (unsigned short)v100;
- *unit = 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- *raw = (unsigned short)v100;
- *unit = 2;
- } else if (i < len && s[i] == '%') {
- /* Keep hundredths: 62.5% is a common root size and truncating
- * it to 62% makes every rem 1px too small. */
- *raw = (unsigned short)v100;
- *unit = 4;
- } else {
- *raw = (unsigned short)(v100 / 100);
- *unit = 1;
- }
- return 1;
- }
- static int parse_line_height(const char *s, unsigned int len,
- unsigned short *px, unsigned char *pct)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, fscale = 1, digits = 0;
- int v1000;
- int is_px = 0, is_pct = 0;
- *px = 0;
- *pct = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
- frac = frac * 10 + (s[i] - '0');
- fscale *= 10;
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
- is_px = 1;
- else if (i < len && s[i] == '%')
- is_pct = 1;
- v1000 = whole * 1000 + frac * 1000 / fscale;
- /* rem is an absolute length (the root font is known by the time
- * rules are parsed, as for font-size); em behaves like a multiplier. */
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- v1000 = v1000 * root_font_px;
- is_px = 1;
- }
- if (is_px) {
- int v = v1000 / 1000;
- if (v < 8)
- v = 8;
- if (v > 160)
- v = 160;
- *px = (unsigned short)v;
- } else {
- int v = v1000 / 10; /* unitless multiplier -> percent */
- if (is_pct)
- v = v1000 / 1000;
- if (v < 50)
- v = 50;
- if (v > 255) /* stored in an unsigned char */
- v = 255;
- *pct = (unsigned char)v;
- }
- return 1;
- }
- static int contains_ci_len(const char *s, unsigned int len, const char *needle)
- {
- unsigned int i, j, n = s_len(needle);
- for (i = 0; i + n <= len; i++) {
- for (j = 0; j < n; j++) {
- char a = s[i + j];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != needle[j])
- break;
- }
- if (j == n)
- return 1;
- }
- return 0;
- }
- /* Pull a colour out of a border/background shorthand. */
- /* Background shorthands can hold several layers before the final solid
- * colour. The colour is conventionally the last token (e.g. a stack of
- * gradients followed by var(--bg)), so take the rightmost colour rather
- * than the first gradient stop. */
- static int find_color_last(const char *s, unsigned int len, unsigned short *c)
- {
- int last = -1;
- unsigned int i;
- for (i = 0; i < len; i++) {
- if (s[i] == '#')
- last = (int)i;
- else if ((s[i] == 'r' || s[i] == 'R') &&
- i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
- last = (int)i;
- }
- if (last >= 0)
- return parse_color(s + last, len - (unsigned int)last, c);
- while (i > 0) {
- unsigned int st, e;
- while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
- s[i - 1] == ')' || s[i - 1] == '('))
- i--;
- e = i;
- while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
- s[i - 1] != ')' && s[i - 1] != '(')
- i--;
- st = i;
- if (e > st && parse_color(s + st, e - st, c))
- return 1;
- }
- return 0;
- }
- static int parse_display(const char *s, unsigned int len)
- {
- unsigned int i;
- while (len && is_space(s[0])) { s++; len--; }
- for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
- ;
- if (i == 4 && ci_eq_n(s, "none", 4))
- return BR_DISPLAY_NONE;
- if (i == 5 && ci_eq_n(s, "block", 5))
- return BR_DISPLAY_BLOCK;
- if (i == 6 && ci_eq_n(s, "inline", 6))
- return BR_DISPLAY_INLINE;
- if (i == 12 && ci_eq_n(s, "inline-block", 12))
- return BR_DISPLAY_INLINE_BLOCK;
- if (i == 10 && ci_eq_n(s, "list-item", 10))
- return BR_DISPLAY_BLOCK;
- if (i == 4 && ci_eq_n(s, "flex", 4))
- return BR_DISPLAY_FLEX;
- if (i == 11 && ci_eq_n(s, "inline-flex", 11))
- return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
- /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
- * block instead of an inline box. */
- if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
- return BR_DISPLAY_BLOCK;
- if (i == 8 && ci_eq_n(s, "-moz-box", 8))
- return BR_DISPLAY_BLOCK;
- if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
- return BR_DISPLAY_FLEX;
- if (i == 5 && ci_eq_n(s, "table", 5))
- return BR_DISPLAY_BLOCK;
- if (i == 10 && ci_eq_n(s, "table-cell", 10))
- return BR_DISPLAY_BLOCK;
- /* Grid shells are common on news/wiki pages, but the two-column
- * heuristic is not reliable for their nested area templates yet.
- * Collapse them to block flow so the content stays readable. */
- if (i == 4 && ci_eq_n(s, "grid", 4))
- return BR_DISPLAY_BLOCK;
- if (i == 11 && ci_eq_n(s, "inline-grid", 11))
- return BR_DISPLAY_BLOCK;
- return BR_DISPLAY_INLINE;
- }
- static void decl_add(struct br_decl *out, unsigned int *nout,
- unsigned char prop, int val, int important)
- {
- if (*nout >= BR_DECLS)
- return;
- out[*nout].prop = prop;
- out[*nout].val = (short)val;
- out[*nout].important = (unsigned char)(important != 0);
- (*nout)++;
- }
- /* One whitespace-separated token. Returns 0 at the end of the value. */
- static int next_token(const char *s, unsigned int len, unsigned int *pos,
- unsigned int *start, unsigned int *end)
- {
- unsigned int i = *pos;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- return 0;
- *start = i;
- while (i < len && !is_space(s[i]))
- i++;
- *end = i;
- *pos = i;
- return 1;
- }
- static int bg_number(const char *s, unsigned int len, signed short *val,
- unsigned char *kind)
- {
- unsigned int i = 0;
- long v = 0;
- int neg = 0, digits = 0, pct = 0;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- neg = s[i] == '-';
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- if (i < len && s[i] == '%') {
- pct = 1;
- i++;
- } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
- i += 2;
- }
- if (i != len)
- return 0;
- if (neg)
- v = -v;
- if (v < -4096)
- v = -4096;
- if (v > 4096)
- v = 4096;
- *val = (signed short)v;
- *kind = (unsigned char)(pct ? 1 : 0);
- return 1;
- }
- static int bg_keyword(const char *s, unsigned int len, signed short *val,
- int *vertical)
- {
- *vertical = 0;
- if (len == 4 && ci_eq_n(s, "left", 4)) {
- *val = 0;
- return 1;
- }
- if (len == 5 && ci_eq_n(s, "right", 5)) {
- *val = 100;
- return 1;
- }
- if (len == 3 && ci_eq_n(s, "top", 3)) {
- *val = 0;
- *vertical = 1;
- return 1;
- }
- if (len == 6 && ci_eq_n(s, "bottom", 6)) {
- *val = 100;
- *vertical = 1;
- return 1;
- }
- if (len == 6 && ci_eq_n(s, "center", 6)) {
- *val = 50;
- return 1;
- }
- return 0;
- }
- static void add_bg_position(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- signed short val[2] = { 50, 50 };
- unsigned char kind[2] = { 1, 1 };
- int vert[2] = { 0, 0 }, n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- signed short v;
- int is_vert;
- if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
- val[n] = v;
- kind[n] = 1;
- vert[n] = is_vert;
- } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
- val[n] = v;
- vert[n] = 0;
- } else {
- n++;
- continue;
- }
- n++;
- }
- if (n == 1) {
- if (vert[0]) {
- decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
- decl_add(out, nout, PROP_BG_POS_X, 50, important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
- } else {
- decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_BG_POS_X, val[0], important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
- decl_add(out, nout, PROP_BG_POS_Y, 50, important);
- }
- return;
- }
- if (n >= 2) {
- int x = vert[0] && !vert[1] ? 1 : 0;
- int y = 1 - x;
- decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
- decl_add(out, nout, PROP_BG_POS_X, val[x], important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
- decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
- }
- }
- static void add_bg_size(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- signed short w = 0, h = 0;
- unsigned char kw = 0, kh = 0;
- int n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
- decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
- return;
- }
- if (n == 0 && te - ts == 7 &&
- ci_eq_n(s + ts, "contain", 7)) {
- decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
- return;
- }
- if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
- n++;
- continue;
- }
- {
- signed short v;
- unsigned char k;
- if (bg_number(s + ts, te - ts, &v, &k)) {
- if (n == 0) {
- w = v;
- kw = k;
- } else {
- h = v;
- kh = k;
- }
- }
- }
- n++;
- }
- decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
- decl_add(out, nout, PROP_BG_SIZE_W, w, important);
- decl_add(out, nout, PROP_BG_SIZE_H, h, important);
- (void)kh;
- }
- /* Parse the `position / size` part of the background shorthand. The image
- * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
- static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int len,
- int important)
- {
- unsigned int i, slash = 0, depth = 0, p, first, count;
- char q = 0;
- signed short nv;
- int vert;
- unsigned char kind;
- for (i = 0; i < len; i++) {
- char c = v[i];
- if (q) {
- if (c == q)
- q = 0;
- continue;
- }
- if (c == '\'' || c == '"') {
- q = c;
- continue;
- }
- if (c == '(') {
- depth++;
- continue;
- }
- if (c == ')') {
- if (depth)
- depth--;
- continue;
- }
- if (c == '/' && !depth) {
- slash = i;
- break;
- }
- }
- if (!slash)
- return;
- /* Up to two position tokens immediately before the slash. */
- p = slash;
- first = slash;
- count = 0;
- while (count < 2) {
- unsigned int e;
- while (p > 0 && is_space(v[p - 1]))
- p--;
- e = p;
- while (p > 0 && !is_space(v[p - 1]))
- p--;
- if (e == p)
- break;
- if (bg_keyword(v + p, e - p, &nv, &vert) ||
- bg_number(v + p, e - p, &nv, &kind)) {
- first = p;
- count++;
- } else {
- break;
- }
- }
- if (count)
- add_bg_position(out, nout, v + first, slash - first,
- important);
- /* Up to two size tokens after the slash. */
- p = slash + 1;
- while (p < len && is_space(v[p]))
- p++;
- first = p;
- count = 0;
- while (count < 2) {
- unsigned int ts = p;
- while (p < len && !is_space(v[p]))
- p++;
- if (p == ts)
- break;
- if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
- (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
- (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
- bg_number(v + ts, p - ts, &nv, &kind)) {
- count++;
- while (p < len && is_space(v[p]))
- p++;
- } else {
- break;
- }
- }
- if (count)
- add_bg_size(out, nout, v + first, p - first, important);
- }
- static int parse_fixed_100(const char *s, unsigned int len,
- unsigned int *pos, signed short *out)
- {
- unsigned int i = *pos;
- long whole = 0, frac = 0;
- int neg = 0, digits = 0, fd = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- neg = s[i] == '-';
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- if (whole > 327)
- whole = 327;
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (fd < 2) {
- frac = frac * 10 + (s[i] - '0');
- fd++;
- digits = 1;
- }
- i++;
- }
- }
- if (!digits)
- return 0;
- while (fd++ < 2)
- frac *= 10;
- *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
- *pos = i;
- return 1;
- }
- static int find_fn(const char *v, unsigned int len, const char *name,
- unsigned int *arg)
- {
- unsigned int n = s_len(name), i;
- for (i = 0; i + n <= len; i++) {
- unsigned int j;
- if (!ci_eq_n(v + i, name, n))
- continue;
- j = i + n;
- while (j < len && is_space(v[j]))
- j++;
- if (j < len && v[j] == '(') {
- *arg = j + 1;
- return 1;
- }
- }
- return 0;
- }
- static void add_transform_origin(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len,
- int important)
- {
- unsigned int pos = 0, ts, te;
- signed short val[2] = { 50, 50 };
- unsigned char kind[2] = { 1, 1 };
- int vert[2] = { 0, 0 }, n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- signed short v;
- int is_vert;
- if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
- val[n] = v;
- kind[n] = 1;
- vert[n] = is_vert;
- } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
- val[n] = v;
- vert[n] = 0;
- } else {
- n++;
- continue;
- }
- n++;
- }
- if (n == 1) {
- if (vert[0]) {
- decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
- decl_add(out, nout, PROP_ORIGIN_X, 50, important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
- } else {
- decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
- decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
- }
- return;
- }
- if (n >= 2) {
- int x = vert[0] && !vert[1] ? 1 : 0;
- int y = 1 - x;
- decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
- decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
- decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
- }
- }
- /* Copy a background value with every url(...) body blanked (kept as
- * `url()`), for the keyword and colour scans of the shorthand. */
- static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
- unsigned int cap)
- {
- unsigned int i = 0, n = 0;
- while (i < len && n + 1 < cap) {
- if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
- char q = 0;
- memcpy(out + n, "url(", 4);
- n += 4;
- i += 4;
- while (i < len) {
- if (q) {
- if (s[i] == '\\' && i + 1 < len)
- i++;
- else if (s[i] == q)
- q = 0;
- } else if (s[i] == '"' || s[i] == '\'') {
- q = s[i];
- } else if (s[i] == ')') {
- break;
- }
- i++;
- }
- continue; /* the ')' is copied below */
- }
- out[n++] = s[i++];
- }
- out[n] = '\0';
- return n;
- }
- static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int vl, int important)
- {
- unsigned int i;
- for (i = 0; i + 4 <= vl; i++) {
- if (ci_eq_n(v + i, "url(", 4)) {
- unsigned int st = i + 4, en = st;
- char q = 0;
- int idx;
- /* `)` inside a quoted data: URL (an SVG's
- * rgba(...)) does not end it. */
- while (en < vl) {
- if (q) {
- if (v[en] == '\\' && en + 1 < vl)
- en++;
- else if (v[en] == q)
- q = 0;
- } else if (v[en] == '"' || v[en] == '\'') {
- q = v[en];
- } else if (v[en] == ')') {
- break;
- }
- en++;
- }
- idx = register_bg_url(v + st, en - st);
- if (idx)
- decl_add(out, nout, PROP_BG_IMAGE, idx,
- important);
- return;
- }
- }
- }
- static int parse_len_token(const char *s, unsigned int len, int *out)
- {
- unsigned int pos = 0;
- int v;
- if (!len)
- return 0;
- if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
- return 0;
- v = parse_len(s, len, &pos);
- if (pos == 0)
- return 0;
- *out = v;
- return 1;
- }
- static void expand_four(int v[4], int n)
- {
- if (n == 1) {
- v[1] = v[2] = v[3] = v[0];
- } else if (n == 2) {
- v[2] = v[0];
- v[3] = v[1];
- } else if (n == 3) {
- v[3] = v[1];
- }
- }
- /* Custom properties defined in the declaration block being expanded. A
- * component that sets a variable and uses it in the same rule (Infima's
- * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
- * var(--ifm-col-width) }`) means its own value: the element matching the
- * rule gets it. The document-wide table cannot know that, because
- * `.col` and `.col--N` give the name a dozen different values. */
- static int css_env_active; /* css_local holds an element's variables */
- #define CSS_LOCAL_VARS 48
- static unsigned int css_skip_nested(const char *s, unsigned int len,
- unsigned int i);
- static struct {
- const char *name, *val;
- unsigned short nlen, vlen;
- } css_local[CSS_LOCAL_VARS];
- static unsigned int css_nlocal;
- static int css_local_find(const char *name, unsigned int nlen)
- {
- unsigned int k;
- for (k = 0; k < css_nlocal; k++)
- if (css_local[k].nlen == nlen &&
- !memcmp(css_local[k].name, name, nlen))
- return (int)k;
- return -1;
- }
- static int css_local_find_before(const char *name, unsigned int nlen,
- unsigned int end)
- {
- unsigned int k;
- for (k = 0; k < end; k++)
- if (css_local[k].nlen == nlen &&
- !memcmp(css_local[k].name, name, nlen))
- return (int)k;
- return -1;
- }
- static void css_local_append(const char *s, unsigned int len);
- /* Collect the top-level `--name: value` declarations of a block. */
- static void css_local_collect(const char *s, unsigned int len)
- {
- css_nlocal = 0;
- css_local_append(s, len);
- }
- static void css_local_append(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int ns, ne, vs, ve;
- while (i < len && (is_space(s[i]) || s[i] == ';'))
- i++;
- ns = i;
- while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
- i++;
- if (i >= len || s[i] != ':') {
- if (i < len && s[i] == '{')
- i = css_skip_nested(s, len, i + 1);
- continue;
- }
- ne = i;
- while (ne > ns && is_space(s[ne - 1]))
- ne--;
- vs = i + 1;
- ve = css_value_end(s, len, vs);
- i = ve;
- while (vs < ve && is_space(s[vs]))
- vs++;
- while (ve > vs && is_space(s[ve - 1]))
- ve--;
- if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
- ve -= 10;
- if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
- ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
- continue;
- css_local[css_nlocal].name = s + ns;
- css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
- css_local[css_nlocal].val = s + vs;
- css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
- css_nlocal++;
- }
- }
- /* Expand var(--name[, fallback]) references. Returns the expanded length;
- * the input is copied verbatim when it contains no var(). */
- static unsigned int css_expand_vars(struct br_doc *d, const char *s,
- unsigned int len, char *out,
- unsigned int cap, int depth)
- {
- unsigned int i = 0, n = 0;
- if (depth > 8)
- return 0;
- while (i < len && n + 1 < cap) {
- unsigned int p, inner, close, comma, nlen, fstart;
- char quote;
- int parens, glue;
- if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
- out[n++] = s[i++];
- continue;
- }
- p = i + 4;
- quote = 0;
- parens = 1;
- while (p < len && parens) {
- char c = s[p];
- if (quote) {
- if (c == quote)
- quote = 0;
- else if (c == '\\' && p + 1 < len)
- p++;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- parens++;
- } else if (c == ')') {
- parens--;
- }
- p++;
- }
- if (parens) {
- out[n++] = s[i++];
- continue;
- }
- close = p - 1;
- inner = i + 4;
- /* var() substitutes tokens: minified Infima writes
- * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
- * must not become the one token `700.875rem`. Separate the
- * value from a glued neighbour on both sides. */
- if (n && n + 1 < cap && !is_space(out[n - 1]) &&
- out[n - 1] != '(' && out[n - 1] != ',' &&
- out[n - 1] != ':' && out[n - 1] != '/')
- out[n++] = ' ';
- glue = p < len && !is_space(s[p]) && s[p] != ')' &&
- s[p] != ',' && s[p] != ';' && s[p] != '/' &&
- s[p] != '!' && s[p] != '}';
- while (inner < close && is_space(s[inner]))
- inner++;
- comma = inner;
- parens = 0;
- quote = 0;
- while (comma < close) {
- char c = s[comma];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- parens++;
- } else if (c == ')' && parens > 0) {
- parens--;
- } else if (c == ',' && parens == 0) {
- break;
- }
- comma++;
- }
- nlen = comma - inner;
- while (nlen && is_space(s[inner + nlen - 1]))
- nlen--;
- fstart = comma < close ? comma + 1 : close;
- while (fstart < close && is_space(s[fstart]))
- fstart++;
- if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
- unsigned int vi = css_var_find(d, s + inner, nlen);
- int li = css_local_find(s + inner, nlen);
- if (li >= 0 && css_local[li].val + css_local[li].vlen !=
- s + close + 1) {
- /* (not the definition's own value) */
- n += css_expand_vars(d, css_local[li].val,
- css_local[li].vlen,
- out + n, cap - n,
- depth + 1);
- i = p;
- if (glue && n + 1 < cap)
- out[n++] = ' ';
- continue;
- }
- if (vi < BR_MAX_VARS &&
- (d->var_flags[vi] & BR_VAR_CONFLICT))
- vi = BR_MAX_VARS;
- if (vi < BR_MAX_VARS) {
- const char *v = attr_ptr(d, d->var_val[vi]);
- unsigned int vn = css_expand_vars(d, v, s_len(v),
- out + n,
- cap - n, depth + 1);
- n += vn;
- } else if (fstart < close) {
- unsigned int vn = css_expand_vars(d, s + fstart,
- close - fstart,
- out + n, cap - n,
- depth + 1);
- n += vn;
- }
- } else if (fstart < close) {
- unsigned int vn = css_expand_vars(d, s + fstart,
- close - fstart,
- out + n, cap - n,
- depth + 1);
- n += vn;
- }
- i = p;
- if (glue && n + 1 < cap)
- out[n++] = ' ';
- }
- out[n] = '\0';
- return n;
- }
- static unsigned int grid_skip_sep(const char *s, unsigned int len,
- unsigned int i)
- {
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- return i;
- }
- /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
- * argument). Returns 1 on success. */
- static int parse_grid_track(const char *s, unsigned int len,
- unsigned int *pp, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = grid_skip_sep(s, len, *pp);
- unsigned int whole = 0, frac = 0, scale = 1;
- int any = 0;
- if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
- unsigned int j = i + 7;
- while (j < len && s[j] != ',')
- j++;
- if (j >= len)
- return 0;
- i = j + 1;
- while (i < len && is_space(s[i]))
- i++;
- }
- if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
- int depth = 1;
- i += 7;
- while (i < len && depth) {
- if (s[i] == '(')
- depth++;
- else if (s[i] == ')')
- depth--;
- i++;
- }
- *pp = i;
- return 0;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (unsigned int)(s[i] - '0');
- i++;
- any = 1;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 + (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- any = 1;
- }
- }
- if (!any) {
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
- *val = 1;
- *kind = 3;
- *pp = i + 4;
- return 1;
- }
- return 0;
- }
- if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
- *val = (unsigned short)(whole ? whole : 1);
- *kind = 3;
- i += 2;
- } else if (i < len && s[i] == '%') {
- *val = (unsigned short)(whole > 1000 ? 1000 : whole);
- *kind = 2;
- i++;
- } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- *val = (unsigned short)(whole * 16 + frac * 16 / scale);
- *kind = 1;
- i += 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- *val = (unsigned short)(whole * 16 + frac * 16 / scale);
- *kind = 1;
- i += 2;
- } else {
- *val = (unsigned short)(whole > 1000 ? 1000 : whole);
- *kind = 1;
- if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
- i += 2;
- }
- while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
- i++;
- *pp = i;
- return 1;
- }
- /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
- * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
- * keep the two-column path). */
- static int grid_repeat_count(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- int n = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
- i += 7;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- n = n * 10 + (s[i] - '0');
- i++;
- }
- if (n < 1 || n > 12)
- return 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
- return n;
- if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
- i += 7;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '0') {
- i++;
- while (i < len && (is_space(s[i]) ||
- s[i] == ',' || s[i] == 'p' ||
- s[i] == 'x'))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
- return n;
- }
- }
- return 0;
- }
- /* "1fr 1fr 1fr" */
- for (;;) {
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- break;
- if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
- (i + 3 == len || is_space(s[i + 3]))) {
- n++;
- i += 3;
- } else {
- return 0;
- }
- }
- return n >= 3 ? n : 0;
- }
- static int parse_grid_cols(const char *s, unsigned int len,
- unsigned short val[2], unsigned char kind[2])
- {
- unsigned int pos = 0;
- int n = 0;
- while (n < 2) {
- unsigned int before = pos;
- if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
- break;
- n++;
- if (pos <= before)
- break;
- while (pos < len && is_space(s[pos]))
- pos++;
- if (pos >= len)
- break;
- }
- return n;
- }
- static int parse_grid_slot(const char *s, unsigned int len)
- {
- if (contains_ci_len(s, len, "footer") ||
- contains_ci_len(s, len, "header") ||
- contains_ci_len(s, len, "sitenotice") ||
- contains_ci_len(s, len, "navbar"))
- return 1;
- if (contains_ci_len(s, len, "columnstart") ||
- contains_ci_len(s, len, "sidebar") ||
- contains_ci_len(s, len, "left") ||
- contains_ci_len(s, len, "start"))
- return 2;
- if (contains_ci_len(s, len, "pagecontent") ||
- contains_ci_len(s, len, "content") ||
- contains_ci_len(s, len, "main") ||
- contains_ci_len(s, len, "right") ||
- contains_ci_len(s, len, "end"))
- return 3;
- return 0;
- }
- static int css_has_var(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 <= len; i++)
- if ((s[i] == 'v' || s[i] == 'V') &&
- ci_eq_n(s + i, "var(", 4))
- return 1;
- return 0;
- }
- static unsigned int css_skip_comment(const char *s, unsigned int len,
- unsigned int i)
- {
- i += 2;
- while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
- i++;
- if (i + 1 < len)
- i += 2;
- else
- i = len;
- return i;
- }
- static unsigned int css_skip_string(const char *s, unsigned int len,
- unsigned int i)
- {
- char q = s[i++];
- while (i < len && s[i] != q) {
- if (s[i] == '\\' && i + 1 < len)
- i++;
- i++;
- }
- if (i < len)
- i++;
- return i;
- }
- /* Find the '}' that closes a declaration block. i points just past the
- * opening '{'. Comments and quoted strings are skipped: a brace inside
- * one must not unbalance the scan (nd-aktuell's CSS comments contain
- * selectors such as "Highlight-Stories {" without a closing brace). */
- static unsigned int css_block_end(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 0;
- while (i < len) {
- char c = s[i];
- if (c == '/' && i + 1 < len && s[i + 1] == '*') {
- i = css_skip_comment(s, len, i);
- continue;
- }
- if (c == '"' || c == '\'') {
- i = css_skip_string(s, len, i);
- continue;
- }
- if (c == '{') {
- depth++;
- } else if (c == '}') {
- if (depth == 0)
- return i;
- depth--;
- }
- i++;
- }
- return len;
- }
- /* Skip a block whose opening '{' has already been consumed. */
- static unsigned int css_skip_nested(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 1;
- while (i < len && depth) {
- char c = s[i];
- if (c == '/' && i + 1 < len && s[i + 1] == '*') {
- i = css_skip_comment(s, len, i);
- continue;
- }
- if (c == '"' || c == '\'') {
- i = css_skip_string(s, len, i);
- continue;
- }
- if (c == '{')
- depth++;
- else if (c == '}')
- depth--;
- i++;
- }
- return i;
- }
- /* ---- @font-face ------------------------------------------------------- */
- static void font_trim(const char **sp, unsigned int *len)
- {
- const char *s = *sp;
- unsigned int n = *len;
- while (n && is_space(*s)) {
- s++;
- n--;
- }
- while (n && is_space(s[n - 1]))
- n--;
- if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
- s++;
- n -= 2;
- }
- *sp = s;
- *len = n;
- }
- static int font_name_eq(const char *a, const char *b, unsigned int blen)
- {
- unsigned int k;
- if (s_len(a) != blen)
- return 0;
- for (k = 0; k < blen; k++) {
- char x = a[k], y = b[k];
- if (x >= 'A' && x <= 'Z')
- x = (char)(x - 'A' + 'a');
- if (y >= 'A' && y <= 'Z')
- y = (char)(y - 'A' + 'a');
- if (x != y)
- return 0;
- }
- return 1;
- }
- static void font_copy(char *dst, unsigned int cap, const char *src,
- unsigned int n)
- {
- unsigned int k;
- for (k = 0; k < n && src[k] && k + 1 < cap; k++)
- dst[k] = src[k];
- dst[k] = '\0';
- }
- /* Register one @font-face. Every (family, weight range, style) is its own
- * entry; a later rule with the same triple replaces the URL, as the CSS
- * font-face collection does. family_id is the 1-based id of the family's
- * first entry, which is what font-family declarations store. */
- static void font_add(const char *family, unsigned int flen,
- const char *url, unsigned int ulen,
- unsigned int wmin, unsigned int wmax, int italic)
- {
- unsigned int i;
- int fam = 0;
- struct br_font_def *f;
- font_trim(&family, &flen);
- font_trim(&url, &ulen);
- if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
- return;
- for (i = 0; i < (unsigned int)font_face_count; i++) {
- f = &font_defs[i];
- if (!font_name_eq(f->family, family, flen))
- continue;
- if (!fam)
- fam = f->family_id;
- if (f->wmin != wmin || f->wmax != wmax ||
- f->italic != (unsigned char)italic)
- continue;
- font_copy(f->url, sizeof(f->url), url, ulen);
- font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
- return;
- }
- if (font_face_count >= BR_MAX_FACES)
- return;
- f = &font_defs[font_face_count];
- font_copy(f->family, sizeof(f->family), family, flen);
- font_copy(f->url, sizeof(f->url), url, ulen);
- font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
- f->wmin = (unsigned short)wmin;
- f->wmax = (unsigned short)wmax;
- f->italic = (unsigned char)italic;
- f->used = 0;
- font_face_count++;
- f->family_id = (unsigned char)(fam ? fam : font_face_count);
- /* A family that a font-family declaration already resolved (an
- * earlier @font-face of it) stays wanted for its new variants. */
- if (fam && font_defs[fam - 1].used)
- f->used = 1;
- }
- /* Look up the first name in a font-family list that has an @font-face and
- * mark that family as used, so the browser only downloads faces the page
- * refers to. */
- static int font_family_id(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int start, end, q;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len)
- break;
- if (s[i] == '"' || s[i] == '\'') {
- char quote = s[i++];
- start = i;
- while (i < len && s[i] != quote)
- i++;
- end = i;
- if (i < len)
- i++;
- } else {
- start = i;
- while (i < len && s[i] != ',')
- i++;
- end = i;
- while (end > start && is_space(s[end - 1]))
- end--;
- }
- if (end <= start)
- continue;
- for (q = 0; q < (unsigned int)font_face_count; q++) {
- int fam;
- unsigned int k;
- if (!font_name_eq(font_defs[q].family, s + start,
- end - start))
- continue;
- fam = font_defs[q].family_id;
- for (k = 0; k < (unsigned int)font_face_count; k++)
- if (font_defs[k].family_id == fam)
- font_defs[k].used = 1;
- return fam;
- }
- }
- return 0;
- }
- /* font-weight descriptor: `normal`, `bold`, a number, or a variable font's
- * `100 900` range. */
- static void font_parse_weight(const char *s, unsigned int len,
- unsigned int *wmin, unsigned int *wmax)
- {
- unsigned int i = 0, n = 0, w[2] = { 400, 400 };
- while (i < len && n < 2) {
- unsigned int v = 0, st;
- while (i < len && is_space(s[i]))
- i++;
- st = i;
- while (i < len && !is_space(s[i]))
- i++;
- if (i == st)
- break;
- if (i - st == 4 && ci_eq_n(s + st, "bold", 4)) {
- v = 700;
- } else if (i - st == 6 && ci_eq_n(s + st, "bolder", 6)) {
- v = 700;
- } else if (i - st == 7 && ci_eq_n(s + st, "lighter", 7)) {
- v = 300;
- } else if (s[st] >= '0' && s[st] <= '9') {
- unsigned int k;
- for (k = st; k < i && s[k] >= '0' && s[k] <= '9'; k++)
- v = v * 10 + (unsigned int)(s[k] - '0');
- } else {
- v = 400;
- }
- if (v < 1)
- v = 1;
- if (v > 1000)
- v = 1000;
- w[n++] = v;
- }
- if (n == 1)
- w[1] = w[0];
- if (w[0] > w[1]) {
- unsigned int t = w[0];
- w[0] = w[1];
- w[1] = t;
- }
- *wmin = w[0];
- *wmax = w[1];
- }
- static int font_hex(char c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- return -1;
- }
- /* Whether a unicode-range list covers basic Latin. Google Fonts splits a
- * family into per-script subsets with the same family/weight/style; only
- * the one holding ASCII is useful here (the text path is Latin-1). */
- static int font_range_has_latin(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int lo = 0, hi, digits = 0, wild = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i + 2 > len || (s[i] != 'U' && s[i] != 'u') ||
- s[i + 1] != '+') {
- while (i < len && s[i] != ',')
- i++;
- continue;
- }
- i += 2;
- while (i < len && digits < 6 &&
- (font_hex(s[i]) >= 0 || s[i] == '?')) {
- if (s[i] == '?') {
- lo <<= 4;
- wild++;
- } else {
- lo = (lo << 4) | (unsigned int)font_hex(s[i]);
- }
- digits++;
- i++;
- }
- hi = lo | ((1u << (wild * 4)) - 1u);
- if (!wild && i < len && s[i] == '-') {
- hi = 0;
- i++;
- while (i < len && font_hex(s[i]) >= 0) {
- hi = (hi << 4) | (unsigned int)font_hex(s[i]);
- i++;
- }
- }
- if (digits && lo <= 'a' && hi >= 'a')
- return 1;
- while (i < len && s[i] != ',')
- i++;
- }
- return 0;
- }
- /* Pick the first `src` entry the text engine can load: a TrueType/OpenType
- * file or WOFF 1.0, by its format() hint or, without one, by its
- * extension. local() names, WOFF2, EOT, SVG fonts and data: URLs (they do
- * not fit BR_FACE_URL) are skipped. */
- static int font_pick_src(const char *s, unsigned int len, char *url,
- unsigned int cap, unsigned int *ulen)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int is_ = i, ie, us = 0, ue = 0, q, depth = 0;
- int quote = 0, ok;
- const char *up;
- unsigned int un;
- /* One comma-separated item, commas inside url()/quotes kept. */
- while (i < len) {
- char c = s[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- depth++;
- } else if (c == ')') {
- if (depth)
- depth--;
- } else if (c == ',' && !depth) {
- break;
- }
- i++;
- }
- ie = i;
- i++;
- for (q = is_; q + 4 <= ie; q++)
- if (ci_eq_n(s + q, "url(", 4))
- break;
- if (q + 4 > ie)
- continue;
- us = q + 4;
- for (ue = us; ue < ie && s[ue] != ')'; ue++)
- if (s[ue] == '"' || s[ue] == '\'') {
- char qc = s[ue++];
- while (ue < ie && s[ue] != qc)
- ue++;
- }
- up = s + us;
- un = ue - us;
- font_trim(&up, &un);
- if (!un || un >= cap || (un >= 5 && ci_eq_n(up, "data:", 5)))
- continue;
- ok = 1;
- if (contains_ci_len(s + ue, ie - ue, "format(")) {
- const char *fp = s + ue;
- unsigned int fn = ie - ue;
- ok = (contains_ci_len(fp, fn, "truetype") ||
- contains_ci_len(fp, fn, "opentype") ||
- contains_ci_len(fp, fn, "woff")) &&
- !contains_ci_len(fp, fn, "woff2") &&
- !contains_ci_len(fp, fn, "embedded");
- } else {
- unsigned int e = 0;
- while (e < un && up[e] != '?' && up[e] != '#')
- e++;
- if ((e >= 6 && ci_eq_n(up + e - 6, ".woff2", 6)) ||
- (e >= 4 && ci_eq_n(up + e - 4, ".eot", 4)) ||
- (e >= 4 && ci_eq_n(up + e - 4, ".svg", 4)))
- ok = 0;
- }
- if (!ok)
- continue;
- for (q = 0; q < un; q++)
- url[q] = up[q];
- url[un] = '\0';
- *ulen = un;
- return 1;
- }
- return 0;
- }
- /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
- * at-rules, so faces must be registered before any font-family declaration
- * in the same sheet is resolved. */
- static void scan_font_faces(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i + 10 <= len) {
- unsigned int j, end;
- char family[BR_FACE_FAMILY], url[BR_FACE_URL];
- unsigned int flen = 0, ulen = 0, p;
- unsigned int wmin = 400, wmax = 400;
- int have_family = 0, have_url = 0, italic = 0, latin = 1;
- if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
- i++;
- continue;
- }
- j = i + 10;
- while (j < len && s[j] != '{' && s[j] != ';')
- j++;
- if (j >= len || s[j] == ';') {
- i = j + 1;
- continue;
- }
- end = css_skip_nested(s, len, j + 1);
- p = j + 1;
- while (p < end) {
- unsigned int ds, de, vs, ve, quote = 0;
- while (p < end && (is_space(s[p]) || s[p] == ';'))
- p++;
- if (p >= end)
- break;
- ds = p;
- while (p < end && s[p] != ':' && s[p] != ';' &&
- s[p] != '}')
- p++;
- de = p;
- if (p >= end || s[p] != ':') {
- while (p < end && s[p] != ';')
- p++;
- continue;
- }
- p++;
- vs = p;
- /* A quoted URL may contain ';' (data: URLs do). */
- while (p < end && (quote || (s[p] != ';' &&
- s[p] != '}'))) {
- if (quote && s[p] == (char)quote)
- quote = 0;
- else if (!quote && (s[p] == '"' || s[p] == '\''))
- quote = (unsigned char)s[p];
- p++;
- }
- ve = p;
- while (ds < de && is_space(s[ds]))
- ds++;
- while (de > ds && is_space(s[de - 1]))
- de--;
- while (vs < ve && is_space(s[vs]))
- vs++;
- while (ve > vs && is_space(s[ve - 1]))
- ve--;
- if (de - ds == 11 &&
- ci_eq_n(s + ds, "font-family", 11)) {
- const char *fp = s + vs;
- unsigned int fn = ve - vs;
- font_trim(&fp, &fn);
- if (fn && fn < sizeof(family)) {
- unsigned int k;
- for (k = 0; k < fn; k++)
- family[k] = fp[k];
- family[fn] = '\0';
- flen = fn;
- have_family = 1;
- }
- } else if (de - ds == 3 &&
- ci_eq_n(s + ds, "src", 3)) {
- /* A later src (the old IE `src: eot;` hack
- * is followed by the real list) wins when
- * it has a usable entry. */
- if (font_pick_src(s + vs, ve - vs, url,
- sizeof(url), &ulen))
- have_url = 1;
- } else if (de - ds == 11 &&
- ci_eq_n(s + ds, "font-weight", 11)) {
- font_parse_weight(s + vs, ve - vs, &wmin,
- &wmax);
- } else if (de - ds == 10 &&
- ci_eq_n(s + ds, "font-style", 10)) {
- italic = contains_ci_len(s + vs, ve - vs,
- "italic") ||
- contains_ci_len(s + vs, ve - vs,
- "oblique");
- } else if (de - ds == 13 &&
- ci_eq_n(s + ds, "unicode-range", 13)) {
- latin = font_range_has_latin(s + vs, ve - vs);
- }
- }
- if (have_family && have_url && latin)
- font_add(family, flen, url, ulen, wmin, wmax, italic);
- i = end;
- }
- }
- /* One border-radius length. Unlike parse_size(), percentages keep their
- * kind: the drawer scales them to the box, not the viewport. */
- static int parse_radius_value(const char *s, unsigned int len,
- unsigned int *pos, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = *pos;
- unsigned long v = 0;
- int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- fdigits = 1;
- if (frac < 2) {
- v = v * 10 + (unsigned int)(s[i] - '0');
- frac++;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%') {
- pct = 1;
- i++;
- } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm') {
- rem = 1;
- i += 3;
- } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
- rem = 2;
- i += 2;
- } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
- i += 2;
- } else if (i < len && is_ident(s[i])) {
- return 0;
- }
- if (!digits && !fdigits)
- return 0;
- if (rem == 1)
- v *= root_font_px;
- else if (rem == 2)
- v *= root_font_px;
- v /= 100;
- if (v > 2048)
- v = 2048;
- *val = (unsigned short)v;
- *kind = pct ? 2 : 1;
- *pos = i;
- return 1;
- }
- /* font: [style] [variant] [weight] size[/line-height] family */
- static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- unsigned short size_raw = 0;
- unsigned char size_unit = 0;
- int have_size = 0;
- int bold = 0, italic = 0;
- int lh_px = 0, lh_pct = 0, mono = 0, face;
- unsigned int fam_at = len;
- while (next_token(v, len, &pos, &ts, &te)) {
- unsigned int tl = te - ts;
- unsigned int slash = 0;
- if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
- bold = 1;
- continue;
- }
- if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
- continue;
- if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
- italic = 1;
- continue;
- }
- if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
- italic = 1;
- continue;
- }
- if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
- continue;
- for (slash = 0; slash < tl; slash++)
- if (v[ts + slash] == '/')
- break;
- if (slash == tl) {
- unsigned int q;
- int weight = 0;
- for (q = 0; q < tl; q++) {
- if (v[ts + q] < '0' || v[ts + q] > '9')
- break;
- weight = weight * 10 + (v[ts + q] - '0');
- }
- if (q == tl && weight >= 100 && weight <= 900) {
- bold = weight >= 500;
- continue;
- }
- }
- {
- unsigned short raw;
- unsigned char unit;
- if (!parse_font_size(v + ts, slash < tl ? slash : tl,
- &raw, &unit))
- continue;
- size_raw = raw;
- size_unit = unit;
- }
- if (!have_size)
- fam_at = te; /* the family list follows the size */
- have_size = 1;
- if (slash < tl) {
- unsigned int lstart = ts + slash + 1;
- unsigned short lhp = 0;
- unsigned char lhpct = 0;
- if (!parse_line_height(v + lstart, te - lstart,
- &lhp, &lhpct)) {
- lh_px = 0;
- lh_pct = 0;
- } else {
- lh_px = lhp;
- lh_pct = lhpct;
- }
- }
- }
- if (!have_size)
- return;
- mono = contains_ci_len(v, len, "mono");
- face = font_family_id(v + fam_at, len - fam_at);
- decl_add(out, nout, PROP_FONT_PX, size_raw, important);
- decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
- decl_add(out, nout, PROP_BOLD, bold, important);
- decl_add(out, nout, PROP_ITALIC, italic, important);
- decl_add(out, nout, PROP_MONO, mono, important);
- decl_add(out, nout, PROP_FONT_FACE, face, important);
- decl_add(out, nout, PROP_LH_PX, lh_px, important);
- decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
- }
- static int css_collect_only;
- /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
- * index into d->layer_hash. css_layer_name is the dotted path of the
- * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
- * layer "a.b". Layers are ranked flat by first appearance; a parent
- * layer's own rules therefore lose to its sublayers (the spec says the
- * reverse), which no page we render depends on. */
- static unsigned char css_cur_layer;
- static char css_layer_name[128];
- static unsigned int css_anon_layers;
- static unsigned int layer_name_hash(const char *s, unsigned int len)
- {
- unsigned int h = 2166136261u, i;
- for (i = 0; i < len; i++) {
- h ^= (unsigned char)s[i];
- h *= 16777619u;
- }
- return h ? h : 1;
- }
- /* Register (or look up) the layer `name` nested under the current path and
- * return its 1-based rank; 0 when the table is full (rules then cascade as
- * unlayered, the old behaviour). `name` empty means an anonymous layer. */
- static unsigned char css_layer_register(struct br_doc *d, const char *name,
- unsigned int nlen, char *path,
- unsigned int cap)
- {
- unsigned int pl = s_len(css_layer_name), i, h;
- char anon[16];
- while (nlen && is_space(*name)) { name++; nlen--; }
- while (nlen && is_space(name[nlen - 1])) nlen--;
- if (!nlen) {
- /* An anonymous layer is unique; it can never be reopened.
- * The collect-only (custom property) pass does not need a
- * rank, so it must not burn a table slot. */
- unsigned int n = ++css_anon_layers, k = 0;
- char rev[10];
- if (css_collect_only)
- return 0;
- anon[0] = '#';
- while (n && k < sizeof(rev)) {
- rev[k++] = (char)('0' + n % 10);
- n /= 10;
- }
- for (i = 0; i < k; i++)
- anon[1 + i] = rev[k - 1 - i];
- name = anon;
- nlen = k + 1;
- }
- if (pl + 1 + nlen >= cap)
- return 0;
- memcpy(path, css_layer_name, pl);
- if (pl)
- path[pl++] = '.';
- memcpy(path + pl, name, nlen);
- path[pl + nlen] = '\0';
- h = layer_name_hash(path, pl + nlen);
- for (i = 0; i < d->nlayers; i++)
- if (d->layer_hash[i] == h)
- return (unsigned char)(i + 1);
- if (d->nlayers >= BR_MAX_LAYERS)
- return 0;
- d->layer_hash[d->nlayers++] = h;
- return (unsigned char)d->nlayers;
- }
- /* @supports: the engine cannot answer feature queries, so the block
- * applies unless it is negated (`not (...)` usually guards a fallback for
- * old browsers) or asks for selector() support. */
- static int supports_matches(const char *s, unsigned int len)
- {
- while (len && is_space(*s)) { s++; len--; }
- if (len >= 3 && ci_eq_n(s, "not", 3) &&
- (len == 3 || is_space(s[3]) || s[3] == '('))
- return 0;
- if (contains_ci_len(s, len, "selector("))
- return 0;
- return 1;
- }
- /* Append one code point of generated content as Latin-1. Typography the
- * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
- * and anything else outside Latin-1 are dropped. */
- static void content_put(char *out, unsigned int *n, unsigned int cap,
- unsigned int cp)
- {
- const char *sub = 0;
- if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
- sub = "-";
- else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
- sub = "'";
- else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
- sub = "\"";
- else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
- sub = "\xB7";
- else if (cp == 0x2026)
- sub = "...";
- else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
- sub = ">";
- else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
- sub = "<";
- else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
- cp == 0x2003 || cp == 0x202F)
- sub = " ";
- else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
- sub = "\xD7";
- else if (cp == 0x2713 || cp == 0x2714)
- sub = "v";
- if (sub) {
- while (*sub && *n + 1 < cap)
- out[(*n)++] = *sub++;
- return;
- }
- if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
- out[(*n)++] = (char)cp;
- else if (cp == '\n' && *n + 1 < cap)
- out[(*n)++] = ' ';
- }
- /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
- * content index + 1, or 0 for none/empty (counters and url() are not
- * generated). */
- static int parse_content(struct br_doc *d, const char *s, unsigned int len)
- {
- char out[160];
- unsigned int i = 0, n = 0, off;
- while (i < len) {
- char c = s[i];
- if (c == '"' || c == '\'') {
- char q = c;
- i++;
- while (i < len && s[i] != q) {
- unsigned int cp;
- if (s[i] == '\\' && i + 1 < len) {
- unsigned int h = 0, k = 0;
- i++;
- while (k < 6 && i < len &&
- ((s[i] >= '0' && s[i] <= '9') ||
- ((s[i] | 32) >= 'a' &&
- (s[i] | 32) <= 'f'))) {
- char x = (char)(s[i] | 32);
- h = h * 16 + (unsigned int)(x <= '9' ?
- x - '0' : x - 'a' + 10);
- i++;
- k++;
- }
- if (k) {
- if (i < len && s[i] == ' ')
- i++;
- cp = h;
- } else {
- cp = (unsigned char)s[i++];
- }
- } else if ((unsigned char)s[i] >= 0xC0) {
- unsigned char b = (unsigned char)s[i++];
- int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
- cp = b & (0x3F >> more);
- while (more-- && i < len &&
- ((unsigned char)s[i] & 0xC0) == 0x80)
- cp = (cp << 6) | (s[i++] & 0x3F);
- } else {
- cp = (unsigned char)s[i++];
- }
- content_put(out, &n, sizeof(out), cp);
- }
- i++;
- } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
- unsigned int st = i + 5, e = st;
- while (e < len && s[e] != ')')
- e++;
- while (st < e && is_space(s[st]))
- st++;
- if (n + (e - st) + 3 < sizeof(out) && e > st) {
- out[n++] = '\001';
- while (st < e && !is_space(s[st]))
- out[n++] = s[st++];
- out[n++] = '\002';
- }
- i = e + 1;
- } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
- ci_eq_n(s + i, "close-quot", 10))) {
- content_put(out, &n, sizeof(out), '"');
- i += 10;
- } else if (c == '(') {
- unsigned int depth = 0;
- /* counter(), url(), ...: skip the arguments */
- for (; i < len; i++) {
- if (s[i] == '(')
- depth++;
- else if (s[i] == ')' && --depth == 0)
- break;
- }
- i++;
- } else {
- i++;
- }
- }
- if (!n) {
- /* `content: ""` still creates the box: icon
- * pseudo-elements are empty strings with a size and a
- * background image. */
- for (i = 0; i < len; i++)
- if (s[i] == '"' || s[i] == '\'')
- return BR_CONTENT_EMPTY;
- return 0;
- }
- if (d->ncontent >= BR_MAX_CONTENT)
- return 0;
- off = store_raw(d, out, n, sizeof(out));
- if (!off)
- return 0;
- d->content_off[d->ncontent++] = off;
- return d->ncontent;
- }
- /* ---- calc() folding ---------------------------------------------------
- *
- * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
- * Frameworks build their scales from arithmetic on custom properties
- * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
- * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
- * calc() in a declaration block is folded into that canonical form:
- * a linear combination of %, px (rem/em at the root font size, vw against
- * the viewport) and unitless numbers, with *, / and nested parentheses.
- * Anything else (vh, var() left unresolved, mixed number + length) leaves
- * the calc() text unchanged. Values are hundredths. */
- static unsigned int viewport_w; /* defined with the media code */
- struct calc_val {
- long px, pct, num;
- unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
- };
- static int calc_sum(const char *s, unsigned int len, unsigned int *i,
- struct calc_val *v, int depth);
- static void calc_space(const char *s, unsigned int len, unsigned int *i)
- {
- while (*i < len && is_space(s[*i]))
- (*i)++;
- }
- static int calc_unary(const char *s, unsigned int len, unsigned int *i,
- struct calc_val *v, int depth)
- {
- long whole = 0, frac = 0, scale = 1, n;
- int sign = 1, digits = 0;
- calc_space(s, len, i);
- if (*i < len && (s[*i] == '(' ||
- (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
- *i += s[*i] == '(' ? 1 : 5;
- if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
- return 0;
- calc_space(s, len, i);
- if (*i >= len || s[*i] != ')')
- return 0;
- (*i)++;
- return 1;
- }
- if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
- sign = s[*i] == '-' ? -1 : 1;
- (*i)++;
- }
- while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
- whole = whole * 10 + (s[*i] - '0');
- digits = 1;
- (*i)++;
- if (whole > 1000000)
- return 0;
- }
- if (*i < len && s[*i] == '.') {
- (*i)++;
- while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
- if (scale < 10000) {
- frac = frac * 10 + (s[*i] - '0');
- scale *= 10;
- }
- digits = 1;
- (*i)++;
- }
- }
- if (!digits)
- return 0;
- n = sign * (whole * 100 + frac * 100 / scale);
- v->px = v->pct = v->num = 0;
- if (*i < len && s[*i] == '%') {
- (*i)++;
- v->pct = n;
- v->kind = 2;
- } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
- *i += 3;
- v->px = n * root_font_px;
- v->kind = 1;
- } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
- *i += 2;
- v->px = n * root_font_px;
- v->kind = 1;
- } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
- *i += 2;
- v->px = n;
- v->kind = 1;
- } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
- *i += 2;
- v->px = n * (long)viewport_w / 100;
- v->kind = 1;
- } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
- return 0; /* vh, deg, fr, ch, ... */
- } else {
- v->num = n;
- v->kind = 4;
- }
- return 1;
- }
- static int calc_product(const char *s, unsigned int len, unsigned int *i,
- struct calc_val *v, int depth)
- {
- if (!calc_unary(s, len, i, v, depth))
- return 0;
- for (;;) {
- struct calc_val r;
- char op;
- calc_space(s, len, i);
- if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
- return 1;
- op = s[(*i)++];
- if (!calc_unary(s, len, i, &r, depth))
- return 0;
- if (op == '/') {
- if (r.kind != 4 || !r.num)
- return 0;
- v->px = v->px * 100 / r.num;
- v->pct = v->pct * 100 / r.num;
- v->num = v->num * 100 / r.num;
- } else if (r.kind == 4) {
- v->px = v->px * r.num / 100;
- v->pct = v->pct * r.num / 100;
- v->num = v->num * r.num / 100;
- } else if (v->kind == 4) {
- long k = v->num;
- *v = r;
- v->px = v->px * k / 100;
- v->pct = v->pct * k / 100;
- } else {
- return 0; /* length * length */
- }
- }
- }
- static int calc_sum(const char *s, unsigned int len, unsigned int *i,
- struct calc_val *v, int depth)
- {
- if (!calc_product(s, len, i, v, depth))
- return 0;
- for (;;) {
- struct calc_val r;
- int neg;
- calc_space(s, len, i);
- if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
- return 1;
- neg = s[(*i)++] == '-';
- if (!calc_product(s, len, i, &r, depth))
- return 0;
- if ((v->kind == 4) != (r.kind == 4))
- return 0; /* number + length */
- v->px += neg ? -r.px : r.px;
- v->pct += neg ? -r.pct : r.pct;
- v->num += neg ? -r.num : r.num;
- v->kind |= r.kind;
- }
- }
- /* Hundredths -> "12", "-0.5", "33.33". */
- static unsigned int calc_fmt(char *o, unsigned int cap, long v)
- {
- char tmp[24];
- unsigned int n = 0, k = 0;
- unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
- unsigned long w = a / 100, f = a % 100;
- if (v < 0)
- tmp[n++] = '-';
- {
- char rev[12];
- unsigned int r = 0;
- do {
- rev[r++] = (char)('0' + w % 10);
- w /= 10;
- } while (w && r < sizeof(rev));
- while (r)
- tmp[n++] = rev[--r];
- }
- if (f) {
- tmp[n++] = '.';
- tmp[n++] = (char)('0' + f / 10);
- if (f % 10)
- tmp[n++] = (char)('0' + f % 10);
- }
- for (k = 0; k < n && k < cap; k++)
- o[k] = tmp[k];
- return k;
- }
- /* Copy s to out with every foldable calc() replaced; returns the length,
- * or 0 when nothing was folded (the caller keeps the original). */
- static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
- unsigned int cap)
- {
- unsigned int i = 0, n = 0;
- int changed = 0;
- while (i < len && n + 40 < cap) {
- struct calc_val v;
- unsigned int j = i + 5;
- if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
- (i == 0 || !is_ident(s[i - 1])))) {
- out[n++] = s[i++];
- continue;
- }
- if (!calc_sum(s, len, &j, &v, 0) ||
- (calc_space(s, len, &j), j >= len || s[j] != ')')) {
- out[n++] = s[i++];
- continue;
- }
- j++;
- /* Minified CSS glues tokens to the function's parentheses
- * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
- * a folded `10px16px` would be one token. */
- if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
- out[n - 1] != '(' && out[n - 1] != ',')
- out[n++] = ' ';
- if (v.kind == 4) {
- n += calc_fmt(out + n, cap - n, v.num);
- } else if (!(v.kind & 2) || (!v.pct && v.px)) {
- n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
- -50 : 50)) /
- 100 * 100);
- out[n++] = 'p';
- out[n++] = 'x';
- } else if (!v.px) {
- n += calc_fmt(out + n, cap - n, v.pct);
- out[n++] = '%';
- } else {
- long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
- memcpy(out + n, "calc(", 5);
- n += 5;
- n += calc_fmt(out + n, cap - n, v.pct);
- memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
- n += 4;
- n += calc_fmt(out + n, cap - n, px * 100);
- memcpy(out + n, "px)", 3);
- n += 3;
- }
- i = j;
- changed = 1;
- if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
- s[i] != ',' && s[i] != '!' && s[i] != '}')
- out[n++] = ' ';
- }
- if (!changed || i < len)
- return 0;
- out[n] = '\0';
- return n;
- }
- static int css_has_calc(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 5 <= len; i++)
- if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
- return 1;
- return 0;
- }
- static void parse_decls(struct br_doc *d, const char *s,
- unsigned int len, struct br_decl *out,
- unsigned int *nout)
- {
- static char expanded[4096];
- static char folded[4096];
- if (css_has_var(s, len)) {
- unsigned int elen;
- if (!css_env_active)
- css_local_collect(s, len);
- elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
- 0);
- if (!css_env_active)
- css_nlocal = 0;
- if (elen) {
- s = expanded;
- len = elen;
- }
- }
- if (css_has_calc(s, len)) {
- unsigned int flen = css_fold_calc(s, len, folded,
- sizeof(folded));
- if (flen) {
- s = folded;
- len = flen;
- }
- }
- unsigned int i = 0;
- *nout = 0;
- while (i < len && *nout < BR_DECLS) {
- unsigned int pstart, pend, vstart, vend, k;
- char prop[24];
- int important = 0;
- while (i < len && (is_space(s[i]) || s[i] == ';'))
- i++;
- if (i >= len)
- break;
- /* A nested at-rule (@media/@supports) inside the declaration
- * block carries its own selector context; skip it rather than
- * reading its prelude as a declaration. */
- if (s[i] == '@') {
- while (i < len && s[i] != '{' && s[i] != ';')
- i++;
- if (i < len && s[i] == '{')
- i = css_skip_nested(s, len, i + 1);
- else if (i < len)
- i++;
- continue;
- }
- pstart = i;
- while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
- i++;
- if (i < len && s[i] == '{') {
- /* Nested style rule: skip its block. */
- i = css_skip_nested(s, len, i + 1);
- continue;
- }
- pend = i;
- if (i >= len || s[i] != ':')
- continue;
- i++;
- vstart = i;
- /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
- * does not end the value. */
- i = css_value_end(s, len, i);
- vend = i;
- while (pstart < pend && is_space(s[pstart]))
- pstart++;
- while (pend > pstart && is_space(s[pend - 1]))
- pend--;
- while (vstart < vend && is_space(s[vstart]))
- vstart++;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- if (vend - vstart >= 10 &&
- ci_eq_n(s + vend - 10, "!important", 10)) {
- important = 1;
- vend -= 10;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- }
- for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
- char c = s[pstart + k];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- prop[k] = c;
- }
- prop[k] = '\0';
- if (str_eq(prop, "color")) {
- unsigned short c;
- if (parse_color(s + vstart, vend - vstart, &c))
- decl_add(out, nout, PROP_COLOR, c, important);
- } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
- /* SVG paint only: an HTML element's fill never paints
- * its background (tagesschau sets fill on buttons). */
- int is_fill = prop[0] == 'f';
- unsigned short c;
- int mode = paint_mode(s + vstart, vend - vstart, &c);
- if (mode) {
- decl_add(out, nout, is_fill ? PROP_FILL_MODE :
- PROP_STROKE_MODE, mode, important);
- if (mode == BR_PAINT_COLOR)
- decl_add(out, nout, is_fill ? PROP_FILL :
- PROP_STROKE, (short)c,
- important);
- }
- } else if (str_eq(prop, "stroke-width")) {
- int sw = svg_len16(s + vstart, vend - vstart);
- if (sw >= 0)
- decl_add(out, nout, PROP_STROKE_W, sw,
- important);
- } else if (str_eq(prop, "fill-rule")) {
- decl_add(out, nout, PROP_FILL_RULE,
- contains_ci_len(s + vstart, vend - vstart,
- "evenodd") ? 1 : 0,
- important);
- } else if (str_eq(prop, "text-anchor")) {
- int a = BR_ALIGN_LEFT;
- if (contains_ci_len(s + vstart, vend - vstart, "middle"))
- a = BR_ALIGN_CENTER;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "end"))
- a = BR_ALIGN_RIGHT;
- decl_add(out, nout, PROP_ALIGN, a, important);
- } else if (str_eq(prop, "background-image")) {
- add_bg_image_decl(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "background-repeat")) {
- int r = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "no-repeat"))
- r = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "repeat-x"))
- r = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "repeat-y"))
- r = 3;
- decl_add(out, nout, PROP_BG_REPEAT, r, important);
- } else if (str_eq(prop, "background-position")) {
- add_bg_position(out, nout, s + vstart, vend - vstart,
- important);
- } else if (str_eq(prop, "background-size")) {
- add_bg_size(out, nout, s + vstart, vend - vstart,
- important);
- } else if (str_eq(prop, "background-color") ||
- str_eq(prop, "background")) {
- unsigned short c;
- int is_bg = str_eq(prop, "background");
- static char bgv[1024];
- const char *vs = s + vstart;
- unsigned int vl = vend - vstart;
- if (is_bg) {
- add_bg_image_decl(out, nout, s + vstart,
- vend - vstart, important);
- /* Keywords and colours are read with url()
- * contents blanked: a data: SVG carries its
- * own fill="rgba(...)". */
- vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
- vs = bgv;
- add_bg_shorthand_extra(out, nout,
- vs, vl,
- important);
- if (contains_ci_len(vs, vl,
- "no-repeat"))
- decl_add(out, nout, PROP_BG_REPEAT, 1,
- important);
- else if (contains_ci_len(vs, vl,
- "repeat-x"))
- decl_add(out, nout, PROP_BG_REPEAT, 2,
- important);
- else if (contains_ci_len(vs, vl,
- "repeat-y"))
- decl_add(out, nout, PROP_BG_REPEAT, 3,
- important);
- }
- /* `transparent`/`none` are real values: they must
- * beat a lower-specificity colour (and tell form
- * controls the author styled them). */
- if (color_is_transparent(vs, vl) ||
- (vl == 4 &&
- ci_eq_n(vs, "none", 4)))
- decl_add(out, nout, PROP_BG_NONE, 1, important);
- else if (parse_color(vs, vl, &c))
- decl_add(out, nout, PROP_BG, c, important);
- else if (is_bg &&
- find_color_last(vs, vl, &c))
- decl_add(out, nout, PROP_BG, c, important);
- } else if (str_eq(prop, "font-size")) {
- unsigned short raw;
- unsigned char unit;
- if (parse_font_size(s + vstart, vend - vstart,
- &raw, &unit)) {
- decl_add(out, nout, PROP_FONT_PX, raw,
- important);
- decl_add(out, nout, PROP_FONT_UNIT, unit,
- important);
- }
- } else if (str_eq(prop, "font-weight")) {
- int bold = 0;
- unsigned int j;
- if (vend - vstart >= 4 &&
- (ci_eq_n(s + vstart, "bold", 4) ||
- (vend - vstart >= 6 &&
- ci_eq_n(s + vstart, "bolder", 6))))
- bold = 1;
- else {
- int w = 0;
- for (j = vstart; j < vend; j++)
- if (s[j] >= '0' && s[j] <= '9')
- w = w * 10 + (s[j] - '0');
- /* CSS font matching: with only a 400 and a
- * 700 face, 500 (Infima's "semibold" menus)
- * resolves to the regular face; 600 and up
- * to the bold one. */
- if (w >= 600)
- bold = 1;
- }
- decl_add(out, nout, PROP_BOLD, bold, important);
- } else if (str_eq(prop, "font-style")) {
- int italic = (vend - vstart >= 6 &&
- ci_eq_n(s + vstart, "italic", 6));
- decl_add(out, nout, PROP_ITALIC, italic, important);
- } else if (str_eq(prop, "font-family")) {
- unsigned int j;
- int mono = 0;
- int face = font_family_id(s + vstart, vend - vstart);
- for (j = vstart; j + 4 <= vend; j++)
- if (ci_eq_n(s + j, "mono", 4))
- mono = 1;
- decl_add(out, nout, PROP_FONT_FACE, face, important);
- decl_add(out, nout, PROP_MONO, mono, important);
- } else if (str_eq(prop, "font")) {
- add_font_shorthand(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "text-align")) {
- int a = BR_ALIGN_LEFT;
- if (vend - vstart >= 6 &&
- ci_eq_n(s + vstart, "center", 6))
- a = BR_ALIGN_CENTER;
- else if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "right", 5))
- a = BR_ALIGN_RIGHT;
- decl_add(out, nout, PROP_ALIGN, a, important);
- } else if (str_eq(prop, "text-decoration")) {
- int u = 0, stv = 0, ov = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "underline"))
- u = 1;
- if (contains_ci_len(s + vstart, vend - vstart,
- "line-through"))
- stv = 1;
- if (contains_ci_len(s + vstart, vend - vstart,
- "overline"))
- ov = 1;
- decl_add(out, nout, PROP_UNDERLINE, u, important);
- decl_add(out, nout, PROP_STRIKE, stv, important);
- decl_add(out, nout, PROP_OVERLINE, ov, important);
- } else if (str_eq(prop, "cursor")) {
- int c = parse_cursor(s + vstart, vend - vstart);
- if (c >= 0)
- decl_add(out, nout, PROP_CURSOR, c, important);
- } else if (str_eq(prop, "text-transform")) {
- int u = (vend - vstart >= 9 &&
- ci_eq_n(s + vstart, "uppercase", 9));
- decl_add(out, nout, PROP_UPPER, u, important);
- } else if (str_eq(prop, "white-space")) {
- int nw = contains_ci_len(s + vstart, vend - vstart,
- "nowrap") ||
- contains_ci_len(s + vstart, vend - vstart,
- "pre");
- decl_add(out, nout, PROP_NOWRAP, nw, important);
- } else if (str_eq(prop, "display")) {
- decl_add(out, nout, PROP_DISPLAY,
- parse_display(s + vstart, vend - vstart),
- important);
- } else if (str_eq(prop, "visibility")) {
- int h = contains_ci_len(s + vstart, vend - vstart,
- "hidden") ||
- contains_ci_len(s + vstart, vend - vstart,
- "collapse");
- decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
- } else if (str_eq(prop, "opacity")) {
- unsigned int q;
- int h = 1, any = 0;
- for (q = vstart; q < vend; q++) {
- if (s[q] >= '1' && s[q] <= '9') {
- h = 0;
- break;
- }
- if (s[q] >= '0' && s[q] <= '9')
- any = 1;
- }
- if (!any || contains_ci_len(s + vstart, vend - vstart,
- "inherit"))
- h = 0;
- decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
- } else if (str_eq(prop, "overflow") ||
- str_eq(prop, "overflow-x") ||
- str_eq(prop, "overflow-y")) {
- int h = contains_ci_len(s + vstart, vend - vstart,
- "hidden") ||
- contains_ci_len(s + vstart, vend - vstart,
- "clip");
- decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
- } else if (str_eq(prop, "float")) {
- int f = 0;
- if (vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "left", 4))
- f = 1;
- else if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "right", 5))
- f = 2;
- decl_add(out, nout, PROP_FLOAT, f, important);
- } else if (str_eq(prop, "clear")) {
- int c = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "both"))
- c = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "left"))
- c = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "right"))
- c = 2;
- decl_add(out, nout, PROP_CLEAR, c, important);
- } else if (str_eq(prop, "flex-grow") ||
- str_eq(prop, "flex")) {
- unsigned int q = vstart;
- int g = 0;
- while (q < vend && (s[q] == ' ' || s[q] == '\t'))
- q++;
- if (str_eq(prop, "flex") && vend - q >= 4 &&
- ci_eq_n(s + q, "auto", 4))
- g = 1; /* flex: auto = 1 1 auto */
- while (q < vend && s[q] >= '0' && s[q] <= '9') {
- g = g * 10 + (s[q] - '0');
- q++;
- }
- if (g > 100)
- g = 100;
- decl_add(out, nout, PROP_FLEX_GROW, g, important);
- if (str_eq(prop, "flex")) {
- /* flex: <grow> [<shrink>] [<basis>]: a definite
- * basis is the item's main size, which our flex
- * row reads from width (Infima's grid:
- * `.col--3 { flex: 0 0 25% }`). Skip the rest
- * of the grow number, then any unitless shrink. */
- unsigned int t;
- while (q < vend && (s[q] == '.' ||
- (s[q] >= '0' && s[q] <= '9')))
- q++;
- for (t = 0; t < 2 && q < vend; t++) {
- unsigned int st, e, k2;
- unsigned short bv;
- unsigned char bk;
- while (q < vend && is_space(s[q]))
- q++;
- st = q;
- while (q < vend && !is_space(s[q]))
- q++;
- e = q;
- for (k2 = st; k2 < e; k2++)
- if (!(s[k2] == '.' ||
- (s[k2] >= '0' &&
- s[k2] <= '9')))
- break;
- if (k2 == e)
- continue; /* shrink */
- if (parse_size(s + st, e - st, &bv,
- &bk) && bk && bv) {
- decl_add(out, nout, PROP_WIDTH,
- bv, important);
- decl_add(out, nout,
- PROP_WIDTH_KIND, bk,
- important);
- }
- break;
- }
- }
- } else if (str_eq(prop, "flex-basis")) {
- unsigned short bv;
- unsigned char bk;
- if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
- bk && bv) {
- decl_add(out, nout, PROP_WIDTH, bv, important);
- decl_add(out, nout, PROP_WIDTH_KIND, bk,
- important);
- }
- } else if (str_eq(prop, "flex-direction")) {
- decl_add(out, nout, PROP_FLEX_COL,
- contains_ci_len(s + vstart, vend - vstart,
- "column"), important);
- } else if (str_eq(prop, "flex-wrap")) {
- int wrap = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
- wrap = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "wrap"))
- wrap = 1;
- decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
- } else if (str_eq(prop, "justify-content")) {
- int j = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "space-between"))
- j = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "space-around"))
- j = 4;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "space-evenly"))
- j = 5;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "center"))
- j = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "flex-end") ||
- contains_ci_len(s + vstart, vend - vstart,
- "end"))
- j = 2;
- decl_add(out, nout, PROP_JUSTIFY, j, important);
- } else if (str_eq(prop, "align-self")) {
- /* 0 auto (the container's align-items), else
- * align-items + 1. */
- int a = 1;
- if (contains_ci_len(s + vstart, vend - vstart, "auto"))
- a = 0;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "center"))
- a = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "flex-end") ||
- contains_ci_len(s + vstart, vend - vstart,
- "end"))
- a = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "start") ||
- contains_ci_len(s + vstart, vend - vstart,
- "baseline"))
- a = 4;
- decl_add(out, nout, PROP_ALIGN_SELF, a, important);
- } else if (str_eq(prop, "align-items")) {
- int a = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "center"))
- a = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "flex-end") ||
- contains_ci_len(s + vstart, vend - vstart,
- "end"))
- a = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "start") ||
- contains_ci_len(s + vstart, vend - vstart,
- "baseline"))
- a = 3; /* not stretched */
- decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
- } else if (str_eq(prop, "position")) {
- int p = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "absolute"))
- p = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "fixed"))
- p = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "relative"))
- p = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "sticky"))
- p = 4;
- decl_add(out, nout, PROP_POS, p, important);
- } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
- str_eq(prop, "right") || str_eq(prop, "bottom")) {
- int which = str_eq(prop, "left") ? 0 :
- str_eq(prop, "top") ? 1 :
- str_eq(prop, "right") ? 2 : 3;
- unsigned int pos = 0;
- if (!(vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "auto", 4))) {
- int v = parse_len(s + vstart, vend - vstart, &pos);
- if (v > 32000)
- v = 32000;
- else if (v < -32000)
- v = -32000;
- decl_add(out, nout,
- (unsigned char)(which == 0 ? PROP_LEFT :
- which == 1 ? PROP_TOP :
- which == 2 ? PROP_RIGHT :
- PROP_BOTTOM),
- v, important);
- decl_add(out, nout,
- (unsigned char)(which == 0 ? PROP_LEFT_SET :
- which == 1 ? PROP_TOP_SET :
- which == 2 ? PROP_RIGHT_SET :
- PROP_BOTTOM_SET),
- 1, important);
- }
- } else if (str_eq(prop, "z-index")) {
- unsigned int q = vstart;
- int sign = 1;
- int v = 0;
- int any = 0;
- if (q < vend && s[q] == '-') {
- sign = -1;
- q++;
- }
- while (q < vend && s[q] >= '0' && s[q] <= '9') {
- v = v * 10 + (s[q] - '0');
- any = 1;
- q++;
- }
- if (any) {
- v *= sign;
- if (v > 32000)
- v = 32000;
- else if (v < -32000)
- v = -32000;
- decl_add(out, nout, PROP_ZINDEX, v, important);
- decl_add(out, nout, PROP_ZSET, 1, important);
- } else if (vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "auto", 4)) {
- decl_add(out, nout, PROP_ZSET, 0, important);
- }
- } else if (str_eq(prop, "transform")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- int tx = 0, ty = 0;
- int is_x = contains_ci_len(v, vl, "translatex");
- int is_y = contains_ci_len(v, vl, "translatey");
- unsigned int off = 0, arg;
- if (contains_ci_len(v, vl, "translate")) {
- while (off < vl && v[off] != '(')
- off++;
- if (off < vl)
- off++;
- if (!is_y)
- tx = parse_len(v, vl, &off);
- if (!is_x) {
- while (off < vl &&
- (is_space(v[off]) || v[off] == ','))
- off++;
- ty = parse_len(v, vl, &off);
- }
- decl_add(out, nout, PROP_TRANSFORM_X, tx,
- important);
- decl_add(out, nout, PROP_TRANSFORM_Y, ty,
- important);
- decl_add(out, nout, PROP_TRANSFORM_SET, 1,
- important);
- }
- if (find_fn(v, vl, "scale", &arg)) {
- unsigned int q = arg;
- signed short sx = 100, sy = 100;
- if (parse_fixed_100(v, vl, &q, &sx)) {
- unsigned int r = q;
- while (r < vl &&
- (is_space(v[r]) || v[r] == ','))
- r++;
- if (!parse_fixed_100(v, vl, &r, &sy))
- sy = sx;
- decl_add(out, nout, PROP_SCALE_X, sx,
- important);
- decl_add(out, nout, PROP_SCALE_Y, sy,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_PAINT, 1,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_SET, 1,
- important);
- }
- }
- if (find_fn(v, vl, "rotate", &arg)) {
- unsigned int q = arg;
- signed short deg;
- if (parse_fixed_100(v, vl, &q, °)) {
- decl_add(out, nout, PROP_ROTATE, deg,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_PAINT, 1,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_SET, 1,
- important);
- }
- }
- } else if (str_eq(prop, "transform-origin")) {
- add_transform_origin(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "grid-template-columns") ||
- str_eq(prop, "grid-template")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- unsigned short val[2];
- unsigned char kind[2];
- int n, q;
- if (str_eq(prop, "grid-template")) {
- for (q = 0; q < (int)vl; q++)
- if (v[q] == '/') {
- q++;
- while (q < (int)vl && is_space(v[q]))
- q++;
- v += q;
- vl -= (unsigned int)q;
- break;
- }
- }
- if (grid_repeat_count(v, vl) >= 1) {
- /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
- * Tailwind's grid-cols-N. N equal columns
- * (GRID_FIXED + N); one column is plain block
- * flow, which also resets a smaller breakpoint's
- * count. */
- int cnt = grid_repeat_count(v, vl);
- decl_add(out, nout, PROP_GRID_AUTO,
- cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
- important);
- decl_add(out, nout, PROP_GRID_NCOLS, 0,
- important);
- } else if (contains_ci_len(v, vl, "auto-fit") ||
- contains_ci_len(v, vl, "auto-fill")) {
- unsigned int mp = 0;
- int found = 0;
- unsigned short mw = 200;
- unsigned char mk = 1;
- for (q = 0; q + 7 <= (int)vl; q++)
- if (ci_eq_n(v + q, "minmax(", 7)) {
- mp = (unsigned int)q + 7;
- found = 1;
- break;
- }
- if (found)
- parse_grid_track(v, vl, &mp, &mw, &mk);
- if (mk != 1 || mw < 80)
- mw = 200;
- if (mw > 480)
- mw = 480;
- decl_add(out, nout, PROP_GRID_AUTO, 1, important);
- decl_add(out, nout, PROP_WIDTH, mw, important);
- decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
- } else {
- n = parse_grid_cols(v, vl, val, kind);
- if (n > 0) {
- decl_add(out, nout, PROP_WIDTH, val[0],
- important);
- decl_add(out, nout, PROP_WIDTH_KIND,
- kind[0], important);
- decl_add(out, nout, PROP_GRID_NCOLS, n,
- important);
- }
- if (n > 1) {
- decl_add(out, nout, PROP_MAXW, val[1],
- important);
- decl_add(out, nout, PROP_MAXW_KIND,
- kind[1], important);
- }
- }
- } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
- /* From the value, not the block start: gap was
- * ignored on nearly every site. */
- unsigned int gp = vstart;
- unsigned short gv = 0;
- unsigned char gk = 1;
- if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
- if (gv > 120)
- gv = 120;
- decl_add(out, nout, PROP_GRID_GAP, gv, important);
- }
- } else if (str_eq(prop, "grid-area")) {
- int slot = parse_grid_slot(s + vstart, vend - vstart);
- if (slot)
- decl_add(out, nout, PROP_GRID_SLOT, slot, important);
- } else if (str_eq(prop, "width") ||
- str_eq(prop, "max-width") ||
- str_eq(prop, "min-width") ||
- str_eq(prop, "height") ||
- str_eq(prop, "max-height") ||
- str_eq(prop, "min-height")) {
- unsigned short v;
- unsigned char kind;
- if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "calc(", 5)) {
- int pct, px;
- if (parse_calc(s + vstart + 5,
- vend - vstart - 5, &pct, &px)) {
- if (str_eq(prop, "width")) {
- decl_add(out, nout, PROP_WIDTH,
- 0, important);
- decl_add(out, nout,
- PROP_WIDTH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_WIDTH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_WIDTH_CALC_PX,
- px, important);
- } else if (str_eq(prop, "max-width")) {
- decl_add(out, nout, PROP_MAXW,
- 0, important);
- decl_add(out, nout,
- PROP_MAXW_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MAXW_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MAXW_CALC_PX,
- px, important);
- } else if (str_eq(prop, "min-width")) {
- decl_add(out, nout, PROP_MINW,
- 0, important);
- decl_add(out, nout,
- PROP_MINW_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MINW_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MINW_CALC_PX,
- px, important);
- } else if (str_eq(prop, "height")) {
- decl_add(out, nout, PROP_HEIGHT,
- 0, important);
- decl_add(out, nout,
- PROP_HEIGHT_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_HEIGHT_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_HEIGHT_CALC_PX,
- px, important);
- } else if (str_eq(prop, "max-height")) {
- decl_add(out, nout, PROP_MAXH,
- 0, important);
- decl_add(out, nout,
- PROP_MAXH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MAXH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MAXH_CALC_PX,
- px, important);
- } else {
- decl_add(out, nout, PROP_MINH,
- 0, important);
- decl_add(out, nout,
- PROP_MINH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MINH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MINH_CALC_PX,
- px, important);
- }
- }
- continue;
- }
- if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
- if (str_eq(prop, "width")) {
- decl_add(out, nout, PROP_WIDTH, v, important);
- decl_add(out, nout, PROP_WIDTH_KIND, kind,
- important);
- } else if (str_eq(prop, "max-width")) {
- decl_add(out, nout, PROP_MAXW, v, important);
- decl_add(out, nout, PROP_MAXW_KIND, kind,
- important);
- } else if (str_eq(prop, "min-width")) {
- decl_add(out, nout, PROP_MINW, v, important);
- decl_add(out, nout, PROP_MINW_KIND, kind,
- important);
- } else if (str_eq(prop, "max-height")) {
- decl_add(out, nout, PROP_MAXH, v, important);
- decl_add(out, nout, PROP_MAXH_KIND, kind,
- important);
- } else if (str_eq(prop, "min-height")) {
- decl_add(out, nout, PROP_MINH, v, important);
- decl_add(out, nout, PROP_MINH_KIND, kind,
- important);
- } else {
- decl_add(out, nout, PROP_HEIGHT, v, important);
- decl_add(out, nout, PROP_HEIGHT_KIND, kind,
- important);
- }
- }
- } else if (str_eq(prop, "content")) {
- decl_add(out, nout, PROP_CONTENT,
- css_collect_only ? 0 :
- parse_content(d, s + vstart, vend - vstart),
- important);
- } else if (str_eq(prop, "aspect-ratio")) {
- /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
- * preferred height of an auto-height box. */
- unsigned int q = vstart;
- unsigned int a[2] = { 0, 1 }, k;
- for (k = 0; k < 2; k++) {
- unsigned int whole = 0, frac = 0, sc = 1;
- while (q < vend && is_space(s[q]))
- q++;
- if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
- s[q] != '.'))
- break;
- while (q < vend && s[q] >= '0' && s[q] <= '9')
- whole = whole * 10 +
- (unsigned int)(s[q++] - '0');
- if (q < vend && s[q] == '.') {
- q++;
- while (q < vend && s[q] >= '0' &&
- s[q] <= '9') {
- if (sc < 100) {
- frac = frac * 10 +
- (unsigned int)(s[q] - '0');
- sc *= 10;
- }
- q++;
- }
- }
- a[k] = whole * 100 + frac * (100 / sc);
- while (q < vend && is_space(s[q]))
- q++;
- if (k == 0 && (q >= vend || s[q] != '/')) {
- a[1] = 100;
- break;
- }
- q++;
- }
- if (a[0] > 32000 || a[1] > 32000 || !a[1])
- a[0] = a[1] = 0;
- decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
- decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
- } else if (str_eq(prop, "box-sizing")) {
- int bb = (vend - vstart >= 10 &&
- ci_eq_n(s + vstart, "border-box", 10));
- decl_add(out, nout, PROP_BOX_SIZING, bb, important);
- } else if (str_eq(prop, "line-height")) {
- unsigned short px;
- unsigned char pct;
- if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
- if (px) {
- decl_add(out, nout, PROP_LH_PX, px, important);
- decl_add(out, nout, PROP_LH_PCT, 0, important);
- } else {
- decl_add(out, nout, PROP_LH_PX, 0, important);
- decl_add(out, nout, PROP_LH_PCT, pct, important);
- }
- }
- } else if (str_eq(prop, "list-style") ||
- str_eq(prop, "list-style-type")) {
- int ls = BR_LIST_AUTO;
- if (contains_ci_len(s + vstart, vend - vstart,
- "none"))
- ls = BR_LIST_NONE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "circle"))
- ls = BR_LIST_CIRCLE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "square"))
- ls = BR_LIST_SQUARE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "lower-alpha") ||
- contains_ci_len(s + vstart, vend - vstart,
- "lower-latin"))
- ls = BR_LIST_LOWER_ALPHA;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "upper-alpha") ||
- contains_ci_len(s + vstart, vend - vstart,
- "upper-latin"))
- ls = BR_LIST_UPPER_ALPHA;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "lower-roman"))
- ls = BR_LIST_LOWER_ROMAN;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "upper-roman"))
- ls = BR_LIST_UPPER_ROMAN;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "decimal"))
- ls = BR_LIST_DECIMAL;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "disc"))
- ls = BR_LIST_DISC;
- decl_add(out, nout, PROP_LIST_NONE,
- ls == BR_LIST_NONE, important);
- decl_add(out, nout, PROP_LIST_STYLE, ls, important);
- } else if (str_eq(prop, "border-radius") ||
- str_eq(prop, "border-top-left-radius") ||
- str_eq(prop, "border-top-right-radius") ||
- str_eq(prop, "border-bottom-right-radius") ||
- str_eq(prop, "border-bottom-left-radius")) {
- unsigned short v[4];
- unsigned char k[4];
- unsigned int pos = 0;
- int n = 0;
- while (pos < vend - vstart && n < 4) {
- while (pos < vend - vstart &&
- is_space(s[vstart + pos]))
- pos++;
- if (pos >= vend - vstart ||
- s[vstart + pos] == '/')
- break;
- if (!parse_radius_value(s + vstart,
- vend - vstart, &pos,
- &v[n], &k[n]))
- break;
- n++;
- }
- if (n) {
- unsigned short r[4];
- unsigned char rk[4];
- int i;
- if (n == 1) {
- for (i = 0; i < 4; i++) {
- r[i] = v[0];
- rk[i] = k[0];
- }
- } else if (n == 2) {
- r[0] = r[2] = v[0];
- r[1] = r[3] = v[1];
- rk[0] = rk[2] = k[0];
- rk[1] = rk[3] = k[1];
- } else if (n == 3) {
- r[0] = v[0];
- r[1] = r[3] = v[1];
- r[2] = v[2];
- rk[0] = k[0];
- rk[1] = rk[3] = k[1];
- rk[2] = k[2];
- } else {
- for (i = 0; i < 4; i++) {
- r[i] = v[i];
- rk[i] = k[i];
- }
- }
- for (i = 0; i < 4; i++)
- r[i] = rk[i] == 2 ? 255 :
- (r[i] > 255 ? 255 : r[i]);
- if (str_eq(prop, "border-top-left-radius")) {
- decl_add(out, nout, PROP_RADIUS_TL,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-top-right-radius")) {
- decl_add(out, nout, PROP_RADIUS_TR,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-bottom-right-radius")) {
- decl_add(out, nout, PROP_RADIUS_BR,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-bottom-left-radius")) {
- decl_add(out, nout, PROP_RADIUS_BL,
- (short)r[0], important);
- } else {
- decl_add(out, nout, PROP_RADIUS_TL,
- (short)r[0], important);
- decl_add(out, nout, PROP_RADIUS_TR,
- (short)r[1], important);
- decl_add(out, nout, PROP_RADIUS_BR,
- (short)r[2], important);
- decl_add(out, nout, PROP_RADIUS_BL,
- (short)r[3], important);
- }
- }
- } else if (str_eq(prop, "border") ||
- str_eq(prop, "border-top") ||
- str_eq(prop, "border-right") ||
- str_eq(prop, "border-bottom") ||
- str_eq(prop, "border-left") ||
- str_eq(prop, "border-color") ||
- str_eq(prop, "border-width") ||
- str_eq(prop, "border-style") ||
- str_eq(prop, "border-top-width") ||
- str_eq(prop, "border-right-width") ||
- str_eq(prop, "border-bottom-width") ||
- str_eq(prop, "border-left-width") ||
- str_eq(prop, "border-top-color") ||
- str_eq(prop, "border-right-color") ||
- str_eq(prop, "border-bottom-color") ||
- str_eq(prop, "border-left-color") ||
- str_eq(prop, "border-top-style") ||
- str_eq(prop, "border-right-style") ||
- str_eq(prop, "border-bottom-style") ||
- str_eq(prop, "border-left-style")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- unsigned int pos = 0, ts, te;
- int widths[4], colors[4], styles[4], trans[4];
- int nw = 0, nc = 0, ns = 0;
- int side = -1;
- int is_wl = str_eq(prop, "border-width");
- int is_cl = str_eq(prop, "border-color");
- int is_sl = str_eq(prop, "border-style");
- int i;
- if (str_eq(prop, "border-top")) side = 0;
- else if (str_eq(prop, "border-right")) side = 1;
- else if (str_eq(prop, "border-bottom")) side = 2;
- else if (str_eq(prop, "border-left")) side = 3;
- else if (str_eq(prop, "border-top-width")) {
- side = 0; is_wl = 2;
- } else if (str_eq(prop, "border-right-width")) {
- side = 1; is_wl = 2;
- } else if (str_eq(prop, "border-bottom-width")) {
- side = 2; is_wl = 2;
- } else if (str_eq(prop, "border-left-width")) {
- side = 3; is_wl = 2;
- } else if (str_eq(prop, "border-top-color")) {
- side = 0; is_cl = 2;
- } else if (str_eq(prop, "border-right-color")) {
- side = 1; is_cl = 2;
- } else if (str_eq(prop, "border-bottom-color")) {
- side = 2; is_cl = 2;
- } else if (str_eq(prop, "border-left-color")) {
- side = 3; is_cl = 2;
- } else if (str_eq(prop, "border-top-style")) {
- side = 0; is_sl = 2;
- } else if (str_eq(prop, "border-right-style")) {
- side = 1; is_sl = 2;
- } else if (str_eq(prop, "border-bottom-style")) {
- side = 2; is_sl = 2;
- } else if (str_eq(prop, "border-left-style")) {
- side = 3; is_sl = 2;
- }
- if (is_wl) {
- while (nw < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- int x;
- if (!parse_len_token(v + ts, te - ts,
- &x))
- break;
- widths[nw++] = x;
- }
- if (nw) {
- if (is_wl == 1)
- expand_four(widths, nw);
- if (side >= 0 && is_wl == 2) {
- decl_add(out, nout,
- PROP_BW_T + side,
- widths[0], important);
- } else {
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BW_T + i,
- widths[i],
- important);
- }
- }
- } else if (is_cl) {
- unsigned short c;
- int t;
- while (nc < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- t = color_is_transparent(v + ts,
- te - ts);
- if (!t && !parse_color(v + ts,
- te - ts, &c))
- break;
- colors[nc] = t ? 0 : c;
- trans[nc] = t;
- nc++;
- }
- if (nc) {
- if (is_cl == 1) {
- expand_four(colors, nc);
- expand_four(trans, nc);
- }
- if (side >= 0 && is_cl == 2) {
- decl_add(out, nout,
- PROP_BC_T + side,
- colors[0], important);
- decl_add(out, nout,
- PROP_BT_T + side,
- trans[0], important);
- } else {
- for (i = 0; i < 4; i++) {
- decl_add(out, nout,
- PROP_BC_T + i,
- colors[i],
- important);
- decl_add(out, nout,
- PROP_BT_T + i,
- trans[i],
- important);
- }
- }
- }
- } else if (is_sl) {
- while (ns < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- int stv;
- if ((te - ts == 4 &&
- ci_eq_n(v + ts, "none", 4)) ||
- (te - ts == 6 &&
- ci_eq_n(v + ts, "hidden", 6)))
- stv = 0;
- else if (te - ts == 6 &&
- ci_eq_n(v + ts,
- "dashed", 6))
- stv = 2;
- else if (te - ts == 6 &&
- ci_eq_n(v + ts,
- "dotted", 6))
- stv = 3;
- else
- stv = 1;
- styles[ns++] = stv;
- }
- if (ns) {
- if (is_sl == 1)
- expand_four(styles, ns);
- if (is_sl == 2 && side >= 0) {
- if (!styles[0])
- decl_add(out, nout,
- PROP_BW_T + side,
- 0, important);
- else
- decl_add(out, nout,
- PROP_BS_T + side,
- styles[0] - 1,
- important);
- } else {
- for (i = 0; i < 4; i++) {
- if (!styles[i])
- decl_add(out, nout,
- PROP_BW_T + i,
- 0,
- important);
- else
- decl_add(out, nout,
- PROP_BS_T + i,
- styles[i] - 1,
- important);
- }
- }
- }
- } else {
- int bw = -1, bc = -1, style_none = 0;
- int bstyle = -1, btrans = 0;
- while (next_token(v, vl, &pos, &ts, &te)) {
- unsigned short c;
- int x;
- if ((te - ts == 4 &&
- ci_eq_n(v + ts, "none", 4)) ||
- (te - ts == 6 &&
- ci_eq_n(v + ts, "hidden", 6))) {
- style_none = 1;
- } else if (te - ts == 6 &&
- ci_eq_n(v + ts, "dashed", 6)) {
- bstyle = BR_BORDER_DASHED;
- } else if (te - ts == 6 &&
- ci_eq_n(v + ts, "dotted", 6)) {
- bstyle = BR_BORDER_DOTTED;
- } else if (te - ts == 5 &&
- ci_eq_n(v + ts, "solid", 5)) {
- bstyle = BR_BORDER_SOLID;
- } else if (parse_len_token(v + ts,
- te - ts, &x)) {
- bw = x;
- } else if (color_is_transparent(v + ts,
- te - ts)) {
- bc = 0;
- btrans = 1;
- } else if (parse_color(v + ts,
- te - ts, &c)) {
- bc = c;
- btrans = 0;
- }
- }
- if (style_none)
- bw = 0;
- if (bc >= 0 && bw < 0)
- bw = 1;
- if (side < 0) {
- if (bw >= 0)
- decl_add(out, nout,
- PROP_BORDER_W, bw,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BORDER_COLOR, bc,
- important);
- if (bc >= 0)
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BT_T + i,
- btrans,
- important);
- if (bstyle >= 0)
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BS_T + i,
- bstyle,
- important);
- } else {
- if (bw >= 0)
- decl_add(out, nout,
- PROP_BW_T + side, bw,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BC_T + side, bc,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BT_T + side,
- btrans, important);
- if (bstyle >= 0)
- decl_add(out, nout,
- PROP_BS_T + side,
- bstyle, important);
- }
- }
- } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
- int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
- unsigned int pos = 0;
- int is_pad = prop[0] == 'p';
- unsigned char base = is_pad ? PROP_PT : PROP_MT;
- while (pos < vend - vstart && nv < 4) {
- units[nv] = 0;
- if (parse_margin_calc(s + vstart,
- vend - vstart, &pos,
- &vals[nv], &cpx[nv])) {
- units[nv] = 5;
- } else {
- vals[nv] = parse_margin_len(
- s + vstart, vend - vstart,
- &pos, &units[nv]);
- }
- nv++;
- while (pos < vend - vstart &&
- is_space(s[vstart + pos]))
- pos++;
- }
- if (nv == 1) {
- vals[1] = vals[2] = vals[3] = vals[0];
- units[1] = units[2] = units[3] = units[0];
- cpx[1] = cpx[2] = cpx[3] = cpx[0];
- } else if (nv == 2) {
- vals[2] = vals[0];
- vals[3] = vals[1];
- units[2] = units[0];
- units[3] = units[1];
- cpx[2] = cpx[0];
- cpx[3] = cpx[1];
- } else if (nv == 3) {
- vals[3] = vals[1];
- units[3] = units[1];
- cpx[3] = cpx[1];
- }
- if (nv > 0) {
- int k;
- for (k = 0; k < 4; k++) {
- int v = vals[k];
- int u = units[k];
- int au = u == 4;
- if (au)
- v = 0;
- if (is_pad && au)
- u = 0;
- decl_add(out, nout,
- (unsigned char)(base + k), v,
- important);
- decl_add(out, nout,
- (unsigned char)
- ((is_pad ? PROP_PT_U :
- PROP_MT_U) + k),
- u, important);
- if (u == 5)
- decl_add(out, nout,
- (unsigned char)
- ((is_pad ? PROP_PT_CPX :
- PROP_MT_CPX) + k),
- cpx[k], important);
- if (!is_pad && (k == 1 || k == 3))
- decl_add(out, nout,
- k == 1 ? PROP_MR_AUTO :
- PROP_ML_AUTO,
- au, important);
- }
- }
- } else if (str_eq(prop, "margin-top") ||
- str_eq(prop, "margin-right") ||
- str_eq(prop, "margin-bottom") ||
- str_eq(prop, "margin-left") ||
- str_eq(prop, "padding-top") ||
- str_eq(prop, "padding-right") ||
- str_eq(prop, "padding-bottom") ||
- str_eq(prop, "padding-left")) {
- unsigned int pos = 0;
- int unit = 0;
- int v = 0, cpx = 0;
- unsigned char p = PROP_MT;
- const char *side;
- int au;
- if (parse_margin_calc(s + vstart, vend - vstart,
- &pos, &v, &cpx))
- unit = 5;
- else
- v = parse_margin_len(s + vstart,
- vend - vstart, &pos,
- &unit);
- au = unit == 4;
- if (au)
- v = 0;
- if (prop[0] == 'p') {
- p = PROP_PT;
- if (au)
- unit = 0;
- }
- side = prop + (prop[0] == 'p' ? 8 : 7);
- if (str_eq(side, "right"))
- p = (unsigned char)(p + 1);
- else if (str_eq(side, "bottom"))
- p = (unsigned char)(p + 2);
- else if (str_eq(side, "left"))
- p = (unsigned char)(p + 3);
- decl_add(out, nout, p, v, important);
- decl_add(out, nout,
- (unsigned char)((prop[0] == 'p' ?
- PROP_PT_U : PROP_MT_U) +
- (p - (prop[0] == 'p' ?
- PROP_PT : PROP_MT))),
- unit, important);
- if (unit == 5)
- decl_add(out, nout,
- (unsigned char)((prop[0] == 'p' ?
- PROP_PT_CPX : PROP_MT_CPX) +
- (p - (prop[0] == 'p' ?
- PROP_PT : PROP_MT))),
- cpx, important);
- if (str_eq(side, "right"))
- decl_add(out, nout, PROP_MR_AUTO, au, important);
- else if (str_eq(side, "left"))
- decl_add(out, nout, PROP_ML_AUTO, au, important);
- }
- }
- }
- /* Skip @media/@font-face/@keyframes/@import without descending into their
- * nested blocks. This is the mobile/base rules path: media enhancements are
- * deliberately ignored for now. */
- static unsigned int skip_at_rule(const char *s, unsigned int len,
- unsigned int i)
- {
- unsigned int j = i;
- while (j < len && s[j] != '{' && s[j] != ';')
- j++;
- if (j >= len)
- return len;
- if (s[j] == ';')
- return j + 1;
- j++;
- return css_skip_nested(s, len, j);
- }
- static unsigned int viewport_w = 1024;
- static unsigned short theme_text = 0x0000;
- static unsigned short theme_bg = 0xFFFF;
- static unsigned short theme_panel = 0xEF5D;
- static unsigned short theme_link = 0x001B;
- void br_set_theme(unsigned short text, unsigned short bg,
- unsigned short panel, unsigned short link)
- {
- theme_text = text;
- theme_bg = bg;
- theme_panel = panel;
- theme_link = link;
- }
- void br_set_viewport(unsigned int width)
- {
- viewport_w = width ? width : 1024;
- }
- static unsigned int media_len_px(const char *s, unsigned int len)
- {
- unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
- int em = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Consume every fractional digit even when there are more
- * than the three we keep: leaving the tail behind made the
- * unit check miss "em" (74.9375em became 74px). */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 + (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- }
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- em = 1;
- i += 2;
- } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- em = 1;
- i += 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
- i += 2;
- }
- v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
- if (em)
- return v * 16 / 100;
- return v / 100;
- }
- /* One parenthesised media feature: min-width/max-width and the modern
- * (width <= 600px) form. Unknown features are treated as true. */
- static int media_feature_matches(const char *s, unsigned int len)
- {
- unsigned int i;
- while (len && is_space(*s)) { s++; len--; }
- while (len && is_space(s[len - 1])) len--;
- if (contains_ci_len(s, len, "prefers-color-scheme")) {
- /* There is no user-selectable colour scheme. Ignore both
- * variants so a page's base palette is used; sites whose base
- * is dark (like the project docs) then stay dark instead of
- * flipping to their light media override. */
- return 0;
- }
- {
- /* Environment features with a known answer: a mouse-driven
- * screen with normal contrast and no forced colours. Google
- * puts `border:1px solid CanvasText` on its buttons inside
- * `(forced-colors:active)`. */
- static const struct { const char *name, *yes; } env[] = {
- { "forced-colors", "none" },
- { "-ms-high-contrast", "" },
- { "prefers-contrast", "no-preference" },
- { "inverted-colors", "none" },
- { "any-hover", "hover" },
- { "hover", "hover" },
- { "any-pointer", "fine" },
- { "pointer", "fine" },
- { "prefers-reduced-motion", "no-preference" },
- { "prefers-reduced-transparency", "no-preference" },
- { "display-mode", "browser" },
- };
- unsigned int k;
- for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
- unsigned int nl = s_len(env[k].name), j;
- if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
- (len > nl && s[nl] != ':' && !is_space(s[nl])))
- continue;
- for (j = nl; j < len && s[j] != ':'; j++)
- ;
- if (j >= len) /* boolean form: (hover) */
- return env[k].yes[0] &&
- !ci_eq_n(env[k].yes, "none", 4);
- j++;
- while (j < len && is_space(s[j]))
- j++;
- return env[k].yes[0] && len - j == s_len(env[k].yes) &&
- ci_eq_n(s + j, env[k].yes, len - j);
- }
- }
- if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
- for (i = 9; i < len && s[i] != ':'; i++)
- ;
- if (i < len)
- return viewport_w >= media_len_px(s + i + 1,
- len - i - 1);
- return 1;
- }
- if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
- for (i = 9; i < len && s[i] != ':'; i++)
- ;
- if (i < len)
- return viewport_w <= media_len_px(s + i + 1,
- len - i - 1);
- return 1;
- }
- for (i = 0; i < len; i++) {
- if (s[i] == '<' || s[i] == '>') {
- int max = s[i] == '<';
- unsigned int v = media_len_px(s + i + 1, len - i - 1);
- if (i + 1 < len && s[i + 1] == '=')
- v = media_len_px(s + i + 2, len - i - 2);
- return max ? viewport_w <= v : viewport_w >= v;
- }
- }
- return 1;
- }
- static int media_query_matches(const char *s, unsigned int len)
- {
- unsigned int i;
- if (contains_ci_len(s, len, "print") &&
- !contains_ci_len(s, len, "screen"))
- return 0;
- if (contains_ci_len(s, len, "not "))
- return 0;
- for (i = 0; i < len; i++) {
- if (s[i] == '(') {
- unsigned int st = ++i;
- while (i < len && s[i] != ')')
- i++;
- if (!media_feature_matches(s + st, i - st))
- return 0;
- }
- }
- return 1;
- }
- static int media_list_matches(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int st = i;
- while (i < len && s[i] != ',')
- i++;
- if (media_query_matches(s + st, i - st))
- return 1;
- i++;
- }
- return 0;
- }
- int br_media_matches(const char *query)
- {
- return media_list_matches(query, s_len(query));
- }
- static unsigned int rule_hash(const char *s, unsigned int len)
- {
- unsigned int h = 2166136261u, i;
- for (i = 0; i < len; i++) {
- char c = s[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- h ^= (unsigned char)c;
- h *= 16777619u;
- }
- return h % BR_RULE_HASH;
- }
- /* Index a parsed rule by the most selective component of its rightmost
- * compound selector. br_style_for() then only visits rules that can
- * possibly match a node instead of scanning every rule. */
- static unsigned int sel_skip_group(const char *s, unsigned int i,
- unsigned int len);
- static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
- char *out, unsigned int cap, unsigned int *olen);
- static void rule_index_add(struct br_doc *d, unsigned int idx,
- const char *key, unsigned int len)
- {
- /* Unescaped names: Tailwind's ".md\:flex" must be filed under
- * "md:flex", the class the element carries, not under "md". */
- char cname[96], iname[96];
- unsigned int i, clen = 0, ilen = 0;
- unsigned short *head;
- for (i = 0; i < len; i++) {
- if (key[i] == '\\') {
- i++;
- continue;
- }
- if (key[i] == '(' || key[i] == '[') {
- /* :not(.x) / [href="a.b"] must not pick a bucket */
- i = sel_skip_group(key, i, len) - 1;
- continue;
- }
- if (key[i] == '.' || key[i] == '#') {
- char tmp[96];
- unsigned int n, e;
- e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
- if (n && n + 1 < sizeof(tmp)) {
- if (key[i] == '#') {
- memcpy(iname, tmp, n);
- ilen = n;
- } else {
- memcpy(cname, tmp, n);
- clen = n;
- }
- }
- i = e - 1;
- }
- }
- if (ilen)
- head = &d->rule_bucket[rule_hash(iname, ilen)];
- else if (clen)
- head = &d->rule_bucket[rule_hash(cname, clen)];
- else if (len == 5 && ci_eq_n(key, ":root", 5))
- head = &d->rule_tag[BR_TAG_HTML];
- else if (len && is_ident(key[0])) {
- char name[16];
- unsigned int n = 0, tag;
- while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
- name[n] = key[n];
- n++;
- }
- name[n] = '\0';
- tag = br_tag_from_name(name);
- if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
- head = &d->rule_tag[tag];
- else
- head = &d->rule_any;
- } else {
- head = &d->rule_any;
- }
- d->rule_next[idx] = *head;
- *head = (unsigned short)idx;
- }
- #define SEL_PARTS 16
- struct sel_part {
- unsigned short st, en;
- char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
- };
- static unsigned int selector_specificity(const char *s, unsigned int len);
- static int css_selector_supported(const char *s, unsigned int len);
- static int sel_split(const char *s, unsigned int len, struct sel_part *p);
- static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
- unsigned int *st, unsigned int *en);
- static int css_collect_only;
- /* Strip a trailing ::before/::after (or the legacy single-colon forms)
- * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
- * selector with any other pseudo-element. */
- static int css_pseudo_element(char *sel, unsigned int *len)
- {
- static const struct {
- const char *s;
- unsigned char l, pe;
- } pe[] = {
- { "::before", 8, 1 }, { "::after", 7, 2 },
- { ":before", 7, 1 }, { ":after", 6, 2 },
- };
- unsigned int k, n = *len;
- for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
- unsigned int l = pe[k].l;
- if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
- if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
- continue; /* matched the tail of "::x" */
- n -= l;
- if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
- sel[n - 1] == '+' || sel[n - 1] == '~')
- sel[n++] = '*';
- sel[n] = '\0';
- *len = n;
- return pe[k].pe;
- }
- }
- for (k = 0; k + 1 < n; k++)
- if (sel[k] == ':' && sel[k + 1] == ':')
- return -1;
- return 0;
- }
- /* Does a declaration block use or define a custom property whose value
- * depends on the element (defined by some non-root rule)? Such rules are
- * re-expanded per element at cascade time. */
- static unsigned char css_block_scope(struct br_doc *d, const char *s,
- unsigned int len)
- {
- unsigned char f = 0;
- unsigned int i, k;
- for (i = 0; i + 6 < len; i++) {
- unsigned int st, e, vi;
- if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
- continue;
- st = i + 4;
- while (st < len && is_space(s[st]))
- st++;
- e = st;
- while (e < len && s[e] != ',' && s[e] != ')' &&
- !is_space(s[e]))
- e++;
- vi = css_var_find(d, s + st, e - st);
- if (vi < BR_MAX_VARS &&
- (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
- f |= BR_RULE_USES_SCOPED;
- break;
- }
- }
- css_local_collect(s, len);
- for (k = 0; k < css_nlocal; k++) {
- unsigned int vi = css_var_find(d, css_local[k].name,
- css_local[k].nlen);
- if (vi < BR_MAX_VARS &&
- (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
- f |= BR_RULE_DEFINES_SCOPED;
- }
- css_nlocal = 0;
- return f;
- }
- static void parse_css(struct br_doc *d, const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len && d->nrules < d->rule_cap) {
- unsigned int sstart, send, bstart, bend;
- struct br_decl decls[BR_DECLS];
- unsigned int nd, scope_off = 0;
- unsigned char scope_flags = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
- i += 2;
- while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
- i++;
- i += 2;
- continue;
- }
- if (i >= len || s[i] == '}')
- break;
- if (s[i] == '@') {
- if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
- unsigned int q = i + 6;
- while (q < len && s[q] != '{')
- q++;
- if (q < len &&
- media_list_matches(s + i + 6,
- q - (i + 6))) {
- unsigned int b = q + 1;
- unsigned int e = css_skip_nested(s, len, b);
- if (e > b)
- parse_css(d, s + b,
- (e - 1) - b);
- i = e;
- } else {
- i = skip_at_rule(s, len, i);
- }
- continue;
- }
- if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
- (i + 6 == len || is_space(s[i + 6]) ||
- s[i + 6] == '{' || s[i + 6] == ';')) {
- unsigned int q = i + 6, nst = q;
- while (q < len && s[q] != '{' && s[q] != ';')
- q++;
- if (q >= len) {
- i = len;
- continue;
- }
- if (s[q] == ';') {
- /* `@layer a, b;` only fixes the order. */
- unsigned int st = nst;
- char path[128];
- while (st < q) {
- unsigned int en = st;
- while (en < q && s[en] != ',')
- en++;
- if (en > st)
- css_layer_register(d, s + st,
- en - st,
- path,
- sizeof(path));
- st = en + 1;
- }
- i = q + 1;
- continue;
- }
- {
- unsigned int b = q + 1;
- unsigned int e = css_skip_nested(s, len, b);
- unsigned char saved = css_cur_layer;
- char saved_name[128], path[128];
- unsigned char rank;
- memcpy(saved_name, css_layer_name,
- sizeof(saved_name));
- rank = css_layer_register(d, s + nst, q - nst,
- path,
- sizeof(path));
- if (rank) {
- css_cur_layer = rank;
- memcpy(css_layer_name, path,
- sizeof(css_layer_name));
- }
- if (e > b)
- parse_css(d, s + b, (e - 1) - b);
- css_cur_layer = saved;
- memcpy(css_layer_name, saved_name,
- sizeof(css_layer_name));
- i = e;
- }
- continue;
- }
- if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
- unsigned int q = i + 9;
- while (q < len && s[q] != '{' && s[q] != ';')
- q++;
- if (q < len && s[q] == '{' &&
- supports_matches(s + i + 9, q - (i + 9))) {
- unsigned int b = q + 1;
- unsigned int e = css_skip_nested(s, len, b);
- if (e > b)
- parse_css(d, s + b, (e - 1) - b);
- i = e;
- } else {
- i = skip_at_rule(s, len, i);
- }
- continue;
- }
- i = skip_at_rule(s, len, i);
- continue;
- }
- sstart = i;
- while (i < len && s[i] != '{')
- i++;
- if (i >= len)
- break;
- send = i;
- i++;
- bstart = i;
- /* CSS nesting puts @media (and &-rules) inside a rule. Scan
- * to the matching close brace; stopping at the first '}' left
- * the parser out of sync and dropped the rest of the sheet
- * (nd-aktuell lost its desktop rules that way). */
- bend = css_block_end(s, len, i);
- i = bend < len ? bend + 1 : len;
- while (sstart < send && is_space(s[sstart]))
- sstart++;
- while (send > sstart && is_space(s[send - 1]))
- send--;
- if (sstart == send)
- continue;
- if (!css_dark_selector(s + sstart, send - sstart))
- css_collect_block(d, s + bstart, bend - bstart,
- css_root_selector(s + sstart,
- send - sstart));
- if (css_collect_only)
- continue;
- parse_decls(d, s + bstart, bend - bstart, decls, &nd);
- {
- unsigned char sc = bend - bstart <= 4000 ?
- css_block_scope(d, s + bstart, bend - bstart) :
- 0;
- scope_flags = 0;
- scope_off = 0;
- if (sc) {
- scope_off = store_raw(d, s + bstart,
- bend - bstart,
- bend - bstart);
- if (scope_off)
- scope_flags = sc;
- }
- }
- if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
- continue;
- /* Split a comma-separated selector list into separate rules.
- * A selector the engine cannot match is dropped; the others
- * in the list still apply. */
- {
- const char *list = s + sstart;
- unsigned int ll = send - sstart, pos = 0, st, en;
- while (d->nrules < d->rule_cap &&
- sel_list_next(list, ll, &pos, &st, &en)) {
- struct sel_part parts[SEL_PARTS];
- struct br_rule *r;
- char sel[160];
- unsigned int k, off;
- int np, pe;
- for (k = 0; k < sizeof(sel) - 1 && st + k < en;
- k++)
- sel[k] = is_space(list[st + k]) ? ' ' :
- list[st + k];
- if (st + k < en)
- continue; /* too long */
- sel[k] = '\0';
- pe = css_pseudo_element(sel, &k);
- if (pe < 0 || !css_selector_supported(sel, k))
- continue;
- np = sel_split(sel, k, parts);
- off = store_attr(d, sel, k, 159);
- if (np <= 0 || !off)
- continue;
- r = &d->rules[d->nrules++];
- r->sel_off = off;
- r->key_off = store_attr(d, sel + parts[np - 1].st,
- parts[np - 1].en -
- parts[np - 1].st, 159);
- r->spec = (unsigned short)selector_specificity(sel, k);
- r->single = np == 1;
- r->pe = (unsigned char)pe;
- r->layer = css_cur_layer;
- r->var_scope = scope_flags;
- r->raw_off = scope_off;
- r->raw_len = (unsigned short)(bend - bstart);
- if (pe)
- d->npe_rules++;
- r->nd = (unsigned char)nd;
- for (k = 0; k < nd; k++)
- r->d[k] = decls[k];
- rule_index_add(d, d->nrules - 1,
- attr_ptr(d, r->key_off),
- s_len(attr_ptr(d, r->key_off)));
- }
- }
- }
- }
- /* Sites often set html { font-size: 62.5% } and then size everything in
- * rem. Detect that root size before parsing the sheet so a rem resolves to
- * the same pixels a real browser would use. */
- static void detect_root_font(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 < len; i++) {
- unsigned int j, end, body, sel, q, h;
- int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
- int exact;
- if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
- s[i + 3] == 'l'))
- continue;
- h = i + 4;
- while (h < len && is_space(s[h]))
- h++;
- if (h < len && s[h] != '{' && s[h] != ',' &&
- s[h] != '.' && s[h] != ':' && s[h] != '[')
- continue;
- sel = i;
- while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
- s[sel - 1] != ';' && s[sel - 1] != ',')
- sel--;
- q = sel;
- while (q < i && is_space(s[q]))
- q++;
- exact = (q == i);
- if (!exact && h < len && s[h] != ',' && s[h] != '.')
- continue;
- j = h;
- if (j >= len || s[j] != '{')
- continue;
- while (j < len && s[j] != '{')
- j++;
- if (j >= len)
- break;
- end = j + 1;
- body = end;
- while (end < len && s[end] != '}')
- end++;
- for (; j + 9 < end; j++) {
- if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
- s[j + 3] == 't' && s[j + 4] == '-' &&
- s[j + 5] == 's' && s[j + 6] == 'i' &&
- s[j + 7] == 'z' && s[j + 8] == 'e'))
- continue;
- j += 9;
- while (j < end && s[j] != ':')
- j++;
- if (j >= end)
- break;
- j++;
- while (j < end && is_space(s[j]))
- j++;
- while (j < end && s[j] >= '0' && s[j] <= '9') {
- v = v * 10 + (s[j] - '0');
- digits = 1;
- j++;
- }
- if (j < end && s[j] == '.') {
- j++;
- while (j < end && s[j] >= '0' && s[j] <= '9') {
- v = v * 10 + (s[j] - '0');
- frac++;
- j++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (!digits)
- break;
- if (j < end && s[j] == '%')
- pct = 1;
- else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
- px = 1;
- if (pct)
- v = (v * 16 + 5000) / 10000;
- else if (px)
- v /= 100;
- else
- v = v * 16 / 100;
- if (exact && v >= 8 && v <= 24)
- root_font_px = v;
- if (exact)
- return;
- }
- /* html { font: 10px/1 Verdana } also sets the root size. */
- for (j = body; j + 5 <= end; j++) {
- unsigned int p, ts, te;
- if (!ci_eq_n(s + j, "font:", 5))
- continue;
- p = j + 5;
- while (next_token(s, end, &p, &ts, &te)) {
- int fv = parse_font_px(s + ts, te - ts);
- if (fv >= 8 && fv <= 24) {
- if (exact)
- root_font_px = fv;
- break;
- }
- }
- if (exact)
- return;
- break;
- }
- }
- }
- void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
- {
- scan_font_faces(css, len);
- detect_root_font(css, len);
- css_collect_only = 1;
- parse_css(d, css, len);
- css_collect_only = 0;
- parse_css(d, css, len);
- }
- void br_parse_inline_css(struct br_doc *d)
- {
- if (d->inline_css_len) {
- scan_font_faces(css_store, d->inline_css_len);
- detect_root_font(css_store, d->inline_css_len);
- css_collect_only = 1;
- parse_css(d, css_store, d->inline_css_len);
- css_collect_only = 0;
- parse_css(d, css_store, d->inline_css_len);
- }
- d->inline_css_len = 0;
- }
- void br_reset_styles(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < d->nnode; i++) {
- d->nodes[i].style_valid = 0;
- d->nodes[i].pe_state = 0;
- }
- }
- /* ---- selector matching ---- */
- static int class_has(struct br_doc *d, unsigned int node, const char *cls,
- unsigned int clen)
- {
- const char *s = attr_ptr(d, d->nodes[node].class_off);
- unsigned int i = 0;
- while (s[i]) {
- unsigned int st, n;
- while (s[i] && is_space(s[i]))
- i++;
- st = i;
- while (s[i] && !is_space(s[i]))
- i++;
- n = i - st;
- if (n == clen && ci_eq_n(s + st, cls, n))
- return 1;
- }
- return 0;
- }
- static int id_eq(struct br_doc *d, unsigned int node, const char *id,
- unsigned int len)
- {
- const char *s = attr_ptr(d, d->nodes[node].id_off);
- unsigned int i;
- for (i = 0; i < len; i++)
- if (s[i] != id[i])
- return 0;
- return s[len] == '\0';
- }
- static int is_ident(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '-' || c == '_';
- }
- static int attr_is(const char *name, unsigned int nlen, const char *lit)
- {
- unsigned int n = 0;
- while (lit[n])
- n++;
- return n == nlen && ci_eq_n(name, lit, nlen);
- }
- static int attr_present(struct br_doc *d, unsigned int node,
- const char *name, unsigned int nlen)
- {
- struct br_node *n = &d->nodes[node];
- if (attr_is(name, nlen, "data-collapse-target"))
- return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
- if (attr_is(name, nlen, "data-collapse-trigger"))
- return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
- if (attr_is(name, nlen, "is-open"))
- return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
- if (attr_is(name, nlen, "disabled"))
- return (n->attr_flags & BR_ATTR_DISABLED) != 0;
- if (attr_is(name, nlen, "open"))
- return (n->attr_flags & BR_ATTR_OPEN) != 0;
- if (attr_is(name, nlen, "inert"))
- return (n->attr_flags & BR_ATTR_INERT) != 0;
- if (attr_is(name, nlen, "hidden"))
- return (n->flags & BR_NODE_HIDDEN) != 0;
- if (attr_is(name, nlen, "href"))
- return attr_ptr(d, n->href_off)[0] != 0;
- if (attr_is(name, nlen, "class"))
- return attr_ptr(d, n->class_off)[0] != 0;
- if (attr_is(name, nlen, "id"))
- return attr_ptr(d, n->id_off)[0] != 0;
- if (attr_is(name, nlen, "src"))
- return attr_ptr(d, n->src_off)[0] != 0;
- if (attr_is(name, nlen, "alt"))
- return attr_ptr(d, n->alt_off)[0] != 0;
- if (attr_is(name, nlen, "rel"))
- return attr_ptr(d, n->rel_off)[0] != 0;
- return 0;
- }
- /* ---- Selector engine ----
- *
- * One matcher serves the cascade (rule_try), querySelector(All) and
- * Element.matches. A compound is an optional type/universal selector
- * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
- * tokens; a complex selector joins compounds with descendant, '>', '+' and
- * '~' combinators and is matched right to left with backtracking.
- *
- * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
- * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
- * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
- * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
- * with selector lists, and :has (descendant, '>', '+' and '~' relative
- * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
- * and pseudo-elements never match an element; the cascade strips
- * ::before/::after before it gets here. */
- static int sel_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len);
- /* i is at '(' or '['; returns the index just past the matching close. */
- static unsigned int sel_skip_group(const char *s, unsigned int i,
- unsigned int len)
- {
- char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
- int depth = 0;
- for (; i < len; i++) {
- char c = s[i];
- if (quote) {
- if (c == '\\')
- i++;
- else if (c == quote)
- quote = 0;
- continue;
- }
- if (c == '\\')
- i++;
- else if (c == '"' || c == '\'')
- quote = c;
- else if (c == open)
- depth++;
- else if (c == close && --depth == 0)
- return i + 1;
- }
- return len;
- }
- /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
- static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
- char *out, unsigned int cap, unsigned int *olen)
- {
- unsigned int n = 0;
- while (i < len) {
- char c = s[i];
- if (c == '\\' && i + 1 < len) {
- c = s[i + 1];
- i += 2;
- } else if (is_ident(c) || (unsigned char)c >= 0x80) {
- i++;
- } else {
- break;
- }
- if (n + 1 < cap)
- out[n++] = c;
- }
- out[n] = '\0';
- *olen = n;
- return i;
- }
- /* Next top-level item of a comma-separated selector list. */
- static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
- unsigned int *st, unsigned int *en)
- {
- unsigned int i = *pos;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len)
- return 0;
- *st = i;
- while (i < len && s[i] != ',') {
- if (s[i] == '(' || s[i] == '[')
- i = sel_skip_group(s, i, len);
- else if (s[i] == '\\' && i + 1 < len)
- i += 2;
- else if (s[i] == '"' || s[i] == '\'') {
- char q = s[i++];
- while (i < len && s[i] != q)
- i++;
- i++;
- } else
- i++;
- }
- if (i > len)
- i = len;
- *en = i;
- while (*en > *st && is_space(s[*en - 1]))
- (*en)--;
- *pos = i;
- return 1;
- }
- static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- unsigned int pos = 0, st, en;
- while (sel_list_next(s, len, &pos, &st, &en))
- if (sel_match(d, node, s + st, en - st))
- return 1;
- return 0;
- }
- static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
- {
- if (d->nodes[a].tag != d->nodes[b].tag)
- return 0;
- if (d->nodes[a].tag != BR_TAG_UNKNOWN)
- return 1;
- return same_string(attr_ptr(d, d->nodes[a].name_off),
- attr_ptr(d, d->nodes[b].name_off));
- }
- static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
- {
- unsigned int c, prev = 0;
- for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
- c = d->nodes[c].next)
- if (d->nodes[c].type == BR_NODE_ELEM)
- prev = c;
- return c == node ? prev : 0;
- }
- static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
- {
- unsigned int c;
- for (c = d->nodes[node].next; c; c = d->nodes[c].next)
- if (d->nodes[c].type == BR_NODE_ELEM)
- return c;
- return 0;
- }
- /* 1-based position among the element siblings (of the same type), counted
- * from the start or, with from_end, from the end. */
- static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
- int from_end)
- {
- unsigned int c;
- int pos = 0, total = 0;
- for (c = d->nodes[d->nodes[node].parent].first; c;
- c = d->nodes[c].next) {
- if (d->nodes[c].type != BR_NODE_ELEM)
- continue;
- if (of_type && !sel_same_type(d, c, node))
- continue;
- total++;
- if (c == node)
- pos = total;
- }
- if (!pos)
- return 0;
- return from_end ? total - pos + 1 : pos;
- }
- /* an+b, odd, even. "of S" is not supported (returns 0). */
- static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
- {
- unsigned int i = 0;
- int sign = 1, num = 0, digits = 0;
- while (len && is_space(s[len - 1]))
- len--;
- while (i < len && is_space(s[i]))
- i++;
- if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
- *a = 2;
- *b = 1;
- return 1;
- }
- if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
- *a = 2;
- *b = 0;
- return 1;
- }
- if (i < len && (s[i] == '+' || s[i] == '-'))
- sign = s[i++] == '-' ? -1 : 1;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- num = num * 10 + (s[i++] - '0');
- digits = 1;
- }
- if (i < len && (s[i] == 'n' || s[i] == 'N')) {
- *a = sign * (digits ? num : 1);
- *b = 0;
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '+' || s[i] == '-')) {
- int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- bv = bv * 10 + (s[i++] - '0');
- bd = 1;
- }
- if (!bd)
- return 0;
- *b = bs * bv;
- }
- } else {
- if (!digits)
- return 0;
- *a = 0;
- *b = sign * num;
- }
- return i == len;
- }
- static int sel_nth_ok(int a, int b, int pos)
- {
- if (pos <= 0)
- return 0;
- if (!a)
- return pos == b;
- return (pos - b) / a >= 0 && (pos - b) % a == 0;
- }
- static int sel_attr_test(struct br_doc *d, unsigned int node,
- const char *name, int op, const char *want,
- unsigned int wl, int icase)
- {
- const char *have = br_find_attr(d, node, name);
- unsigned int n, i;
- if (!have) {
- /* Boolean attributes the parser keeps as flags. */
- if (!attr_present(d, node, name, text_len(name)))
- return 0;
- have = "";
- }
- if (!op)
- return 1;
- n = text_len(have);
- #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
- switch (op) {
- case '=':
- return n == wl && SEL_EQ(have, want, wl);
- case '^':
- return wl && n >= wl && SEL_EQ(have, want, wl);
- case '$':
- return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
- case '*':
- if (!wl)
- return 0;
- for (i = 0; i + wl <= n; i++)
- if (SEL_EQ(have + i, want, wl))
- return 1;
- return 0;
- case '|':
- return n >= wl && SEL_EQ(have, want, wl) &&
- (n == wl || have[wl] == '-');
- case '~':
- if (!wl)
- return 0;
- for (i = 0; i < n; ) {
- unsigned int st;
- while (i < n && is_space(have[i]))
- i++;
- st = i;
- while (i < n && !is_space(have[i]))
- i++;
- if (i - st == wl && SEL_EQ(have + st, want, wl))
- return 1;
- }
- return 0;
- }
- #undef SEL_EQ
- return 0;
- }
- static int sel_empty(struct br_doc *d, unsigned int node)
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
- if (d->nodes[c].type == BR_NODE_ELEM)
- return 0;
- if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
- return 0;
- }
- return 1;
- }
- static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
- if (d->nodes[c].type != BR_NODE_ELEM)
- continue;
- if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
- return 1;
- }
- return 0;
- }
- /* :has() with an optional leading relative combinator per list item. */
- static int sel_has(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- unsigned int pos = 0, st, en, c;
- while (sel_list_next(s, len, &pos, &st, &en)) {
- char comb = s[st];
- if (comb == '>' || comb == '+' || comb == '~') {
- st++;
- while (st < en && is_space(s[st]))
- st++;
- } else {
- comb = ' ';
- }
- if (st >= en)
- continue;
- if (comb == '>') {
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- if (d->nodes[c].type == BR_NODE_ELEM &&
- sel_match(d, c, s + st, en - st))
- return 1;
- } else if (comb == '+') {
- c = sel_next_elem(d, node);
- if (c && sel_match(d, c, s + st, en - st))
- return 1;
- } else if (comb == '~') {
- for (c = sel_next_elem(d, node); c;
- c = sel_next_elem(d, c))
- if (sel_match(d, c, s + st, en - st))
- return 1;
- } else if (sel_has_desc(d, node, s + st, en - st)) {
- return 1;
- }
- }
- return 0;
- }
- static int sel_name_is(const char *name, unsigned int nl, const char *lit)
- {
- return text_len(lit) == nl && ci_eq_n(name, lit, nl);
- }
- /* Pseudo-class names the engine models; anything else never matches and
- * makes a top-level rule unsupported (see css_selector_supported()). */
- static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
- {
- static const char *const plain[] = {
- "root", "link", "any-link", "visited", "first-child",
- "last-child", "only-child", "first-of-type", "last-of-type",
- "only-of-type", "empty", "checked", "disabled", "enabled",
- "required", "optional",
- };
- static const char *const func[] = {
- "not", "is", "where", "matches", "-webkit-any", "has",
- "nth-child", "nth-last-child", "nth-of-type",
- "nth-last-of-type",
- };
- unsigned int k;
- if (fn) {
- for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
- if (sel_name_is(name, nl, func[k]))
- return 1;
- return 0;
- }
- for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
- if (sel_name_is(name, nl, plain[k]))
- return 1;
- return 0;
- }
- static int sel_form_control(struct br_doc *d, unsigned int node)
- {
- unsigned int t = d->nodes[node].tag;
- return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
- t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
- t == BR_TAG_OPTION;
- }
- static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
- unsigned int nl, const char *arg, unsigned int al,
- int fn)
- {
- struct br_node *n = &d->nodes[node];
- int a, b;
- if (fn) {
- if (sel_name_is(name, nl, "not"))
- return !sel_list_match(d, node, arg, al);
- if (sel_name_is(name, nl, "is") ||
- sel_name_is(name, nl, "where") ||
- sel_name_is(name, nl, "matches") ||
- sel_name_is(name, nl, "-webkit-any"))
- return sel_list_match(d, node, arg, al);
- if (sel_name_is(name, nl, "has"))
- return sel_has(d, node, arg, al);
- if (!sel_nth_parse(arg, al, &a, &b))
- return 0;
- if (sel_name_is(name, nl, "nth-child"))
- return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
- if (sel_name_is(name, nl, "nth-last-child"))
- return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
- if (sel_name_is(name, nl, "nth-of-type"))
- return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
- if (sel_name_is(name, nl, "nth-last-of-type"))
- return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
- return 0;
- }
- if (sel_name_is(name, nl, "root"))
- return n->tag == BR_TAG_HTML;
- if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
- /* No history state: every link is unvisited. */
- return n->tag == BR_TAG_A && n->href_off;
- if (sel_name_is(name, nl, "first-child"))
- return sel_sibling_pos(d, node, 0, 0) == 1;
- if (sel_name_is(name, nl, "last-child"))
- return sel_sibling_pos(d, node, 0, 1) == 1;
- if (sel_name_is(name, nl, "only-child"))
- return sel_sibling_pos(d, node, 0, 0) == 1 &&
- sel_sibling_pos(d, node, 0, 1) == 1;
- if (sel_name_is(name, nl, "first-of-type"))
- return sel_sibling_pos(d, node, 1, 0) == 1;
- if (sel_name_is(name, nl, "last-of-type"))
- return sel_sibling_pos(d, node, 1, 1) == 1;
- if (sel_name_is(name, nl, "only-of-type"))
- return sel_sibling_pos(d, node, 1, 0) == 1 &&
- sel_sibling_pos(d, node, 1, 1) == 1;
- if (sel_name_is(name, nl, "empty"))
- return sel_empty(d, node);
- if (sel_name_is(name, nl, "checked"))
- return br_find_attr(d, node, "checked") != 0 ||
- (n->tag == BR_TAG_OPTION &&
- br_find_attr(d, node, "selected") != 0);
- if (sel_name_is(name, nl, "disabled"))
- return sel_form_control(d, node) &&
- (n->attr_flags & BR_ATTR_DISABLED);
- if (sel_name_is(name, nl, "enabled"))
- return sel_form_control(d, node) &&
- !(n->attr_flags & BR_ATTR_DISABLED);
- if (sel_name_is(name, nl, "required"))
- return sel_form_control(d, node) &&
- br_find_attr(d, node, "required") != 0;
- if (sel_name_is(name, nl, "optional"))
- return sel_form_control(d, node) &&
- !br_find_attr(d, node, "required");
- return 0;
- }
- static int compound_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- char buf[64];
- unsigned int i = 0, bl;
- if (d->nodes[node].type != BR_NODE_ELEM)
- return 0;
- if (i < len && s[i] == '*') {
- i++;
- } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
- unsigned int tag;
- i = sel_ident(s, i, len, buf, 32, &bl);
- tag = br_tag_from_name(buf);
- if (tag == BR_TAG_UNKNOWN) {
- const char *raw = attr_ptr(d,
- d->nodes[node].name_off);
- if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
- !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
- return 0;
- } else if (tag != d->nodes[node].tag) {
- return 0;
- }
- }
- if (i >= len)
- return i > 0;
- while (i < len) {
- char c = s[i];
- if (c == '.' || c == '#') {
- i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
- if (!bl)
- return 0;
- if (c == '.' ? !class_has(d, node, buf, bl) :
- !id_eq(d, node, buf, bl))
- return 0;
- } else if (c == '[') {
- unsigned int e = sel_skip_group(s, i, len), j = i + 1;
- unsigned int vs = 0, vl = 0, k;
- char name[32];
- int op = 0, icase = 0;
- while (j < e && is_space(s[j]))
- j++;
- k = 0;
- while (j < e && (is_ident(s[j]) || s[j] == ':') &&
- k + 1 < sizeof(name)) {
- char ch = s[j++];
- name[k++] = (ch >= 'A' && ch <= 'Z') ?
- (char)(ch + 32) : ch;
- }
- name[k] = '\0';
- if (!k)
- return 0;
- while (j < e && is_space(s[j]))
- j++;
- if (j < e && s[j] == '=') {
- op = '=';
- j++;
- } else if (j + 1 < e && s[j + 1] == '=' &&
- (s[j] == '~' || s[j] == '|' ||
- s[j] == '^' || s[j] == '$' || s[j] == '*')) {
- op = s[j];
- j += 2;
- }
- if (op) {
- while (j < e && is_space(s[j]))
- j++;
- if (j < e && (s[j] == '"' || s[j] == '\'')) {
- char q = s[j++];
- vs = j;
- while (j < e && s[j] != q)
- j++;
- vl = j - vs;
- j++;
- } else {
- vs = j;
- while (j < e && s[j] != ']' &&
- !is_space(s[j]))
- j++;
- vl = j - vs;
- }
- while (j < e && is_space(s[j]))
- j++;
- if (j < e && (s[j] == 'i' || s[j] == 'I'))
- icase = 1;
- }
- if (!sel_attr_test(d, node, name, op, s + vs, vl,
- icase))
- return 0;
- i = e;
- } else if (c == ':') {
- unsigned int st, nl, as = 0, al = 0;
- int fn = 0;
- if (i + 1 < len && s[i + 1] == ':')
- return 0; /* pseudo-element */
- st = ++i;
- while (i < len && is_ident(s[i]))
- i++;
- nl = i - st;
- if (i < len && s[i] == '(') {
- unsigned int e = sel_skip_group(s, i, len);
- fn = 1;
- as = i + 1;
- al = e > as ? e - as - 1 : 0;
- i = e;
- }
- if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
- return 0;
- } else {
- return 0;
- }
- }
- return 1;
- }
- /* Split a complex selector into compounds; returns the count or -1. */
- static int sel_split(const char *s, unsigned int len, struct sel_part *p)
- {
- unsigned int i = 0;
- int n = 0;
- char comb = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len) {
- unsigned int st = i;
- while (i < len && !is_space(s[i]) && s[i] != '>' &&
- s[i] != '+' && s[i] != '~') {
- if (s[i] == '(' || s[i] == '[')
- i = sel_skip_group(s, i, len);
- else if (s[i] == '\\' && i + 1 < len)
- i += 2;
- else
- i++;
- }
- if (i == st || n == SEL_PARTS)
- return -1;
- p[n].st = (unsigned short)st;
- p[n].en = (unsigned short)(i > len ? len : i);
- p[n].comb = comb;
- n++;
- comb = 0;
- while (i < len && is_space(s[i])) {
- comb = ' ';
- i++;
- }
- if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
- comb = s[i++];
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- return -1;
- }
- }
- return n;
- }
- /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
- /* The parent for combinator matching, or -1 at the top. Index 0 is a real
- * parent when the page starts with <html> (node 0 is that element; see
- * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
- static int sel_parent(struct br_doc *d, unsigned int node)
- {
- unsigned int p = d->nodes[node].parent;
- if (p)
- return (int)p;
- if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
- d->nodes[0].tag == BR_TAG_HTML &&
- !(d->nodes[node].flags & BR_NODE_DETACHED))
- return 0;
- return -1;
- }
- static int sel_from(struct br_doc *d, unsigned int node, const char *s,
- const struct sel_part *p, int k)
- {
- unsigned int c, pl;
- const char *ps;
- if (k == 0)
- return 1;
- ps = s + p[k - 1].st;
- pl = p[k - 1].en - p[k - 1].st;
- switch (p[k].comb) {
- case '>': {
- int pc = sel_parent(d, node);
- return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
- sel_from(d, (unsigned int)pc, s, p, k - 1);
- }
- case '+':
- c = sel_prev_elem(d, node);
- return c && compound_match(d, c, ps, pl) &&
- sel_from(d, c, s, p, k - 1);
- case '~':
- for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
- if (compound_match(d, c, ps, pl) &&
- sel_from(d, c, s, p, k - 1))
- return 1;
- return 0;
- default: {
- int pc;
- for (pc = sel_parent(d, node); pc >= 0;
- pc = sel_parent(d, (unsigned int)pc))
- if (compound_match(d, (unsigned int)pc, ps, pl) &&
- sel_from(d, (unsigned int)pc, s, p, k - 1))
- return 1;
- return 0;
- }
- }
- }
- /* Match one complex selector. right_done: the caller already matched the
- * rightmost compound (the rule index key). */
- static int selector_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len, int right_done)
- {
- struct sel_part parts[SEL_PARTS];
- int np = sel_split(s, len, parts);
- if (np <= 0)
- return 0;
- if (!right_done &&
- !compound_match(d, node, s + parts[np - 1].st,
- parts[np - 1].en - parts[np - 1].st))
- return 0;
- return sel_from(d, node, s, parts, np - 1);
- }
- static int sel_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- return selector_match(d, node, s, len, 0);
- }
- /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
- * types/pseudo-elements); :is/:not/:has take their most specific argument
- * and :where counts nothing. */
- static unsigned int selector_specificity(const char *s, unsigned int len)
- {
- unsigned int i = 0, spec = 0, bl;
- char buf[64];
- while (i < len) {
- char c = s[i];
- if (c == '#' || c == '.') {
- spec += c == '#' ? 100 : 10;
- i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
- } else if (c == '[') {
- spec += 10;
- i = sel_skip_group(s, i, len);
- } else if (c == ':') {
- unsigned int st, nl;
- if (i + 1 < len && s[i + 1] == ':') {
- spec += 1;
- i += 2;
- while (i < len && is_ident(s[i]))
- i++;
- continue;
- }
- st = ++i;
- while (i < len && is_ident(s[i]))
- i++;
- nl = i - st;
- if (i < len && s[i] == '(') {
- unsigned int e = sel_skip_group(s, i, len);
- if (sel_name_is(s + st, nl, "where")) {
- /* zero */
- } else if (sel_name_is(s + st, nl, "not") ||
- sel_name_is(s + st, nl, "is") ||
- sel_name_is(s + st, nl, "has") ||
- sel_name_is(s + st, nl, "matches") ||
- sel_name_is(s + st, nl,
- "-webkit-any")) {
- unsigned int pos = 0, as, ae, best = 0;
- unsigned int al = e > i + 1 ?
- e - i - 2 : 0;
- while (sel_list_next(s + i + 1, al,
- &pos, &as, &ae)) {
- unsigned int v =
- selector_specificity(
- s + i + 1 + as,
- ae - as);
- if (v > best)
- best = v;
- }
- spec += best;
- } else {
- spec += 10;
- }
- i = e;
- } else if (sel_name_is(s + st, nl, "before") ||
- sel_name_is(s + st, nl, "after")) {
- spec += 1;
- } else {
- spec += 10;
- }
- } else if (is_ident(c) || c == '\\') {
- spec += 1;
- i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
- } else {
- i++;
- }
- }
- return spec;
- }
- /* Cascade filter: drop a selector the engine cannot match (user-state
- * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
- * ones the caller handles), so it does not waste a rule slot. Arguments of
- * functional pseudo-classes are not checked: an unknown pseudo-class there
- * is simply false, which is right for :not(:hover). */
- static int css_selector_supported(const char *s, unsigned int len)
- {
- struct sel_part parts[SEL_PARTS];
- unsigned int i = 0;
- if (sel_split(s, len, parts) <= 0)
- return 0;
- while (i < len) {
- if (s[i] == '(' || s[i] == '[') {
- i = sel_skip_group(s, i, len);
- } else if (s[i] == '\\') {
- i += 2;
- } else if (s[i] == ':') {
- unsigned int st, nl;
- int fn;
- if (i + 1 < len && s[i + 1] == ':')
- return 0;
- st = ++i;
- while (i < len && is_ident(s[i]))
- i++;
- nl = i - st;
- fn = i < len && s[i] == '(';
- if (!sel_pseudo_known(s + st, nl, fn))
- return 0;
- } else {
- i++;
- }
- }
- return 1;
- }
- #define RULE_MATCH_MAX 512
- /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
- static int cascade_pe;
- struct rule_match {
- unsigned short idx;
- short spec; /* precedence of normal declarations */
- short spec_imp; /* precedence of !important declarations */
- };
- /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
- * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
- * unlayered rules, then the style attribute. !important reverses the
- * layer order: unlayered lowest, the first layer highest, the style
- * attribute above all. Both fit a short. */
- #define CASCADE_BAND 512
- #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
- #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
- static int cascade_rank(unsigned int layer, unsigned int spec, int important)
- {
- unsigned int band;
- if (spec >= CASCADE_BAND)
- spec = CASCADE_BAND - 1;
- if (important)
- band = layer ? CASCADE_UNLAYERED - layer : 0;
- else
- band = layer ? layer : CASCADE_UNLAYERED;
- return (int)(band * CASCADE_BAND + spec);
- }
- static struct rule_match rule_matches[RULE_MATCH_MAX];
- static unsigned int nrule_matches;
- static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
- {
- struct br_rule *r = &d->rules[idx];
- const char *key = attr_ptr(d, r->key_off);
- int spec, key_ok = 0;
- if (r->pe != cascade_pe)
- return;
- if (key[0]) {
- if (!compound_match(d, node, key, s_len(key)))
- return;
- key_ok = 1;
- }
- if (!(r->single && key_ok) &&
- !selector_match(d, node, attr_ptr(d, r->sel_off),
- s_len(attr_ptr(d, r->sel_off)), key_ok))
- return;
- spec = (int)r->spec;
- if (nrule_matches < RULE_MATCH_MAX) {
- rule_matches[nrule_matches].idx = (unsigned short)idx;
- rule_matches[nrule_matches].spec =
- (short)cascade_rank(r->layer, (unsigned int)spec, 0);
- rule_matches[nrule_matches].spec_imp =
- (short)cascade_rank(r->layer, (unsigned int)spec, 1);
- nrule_matches++;
- }
- }
- static void rule_scan_list(struct br_doc *d, unsigned int node,
- unsigned short head)
- {
- while (head != 0xFFFF) {
- rule_try(d, node, head);
- head = d->rule_next[head];
- }
- }
- static void rule_scan_class(struct br_doc *d, unsigned int node,
- const char *cls, unsigned int len)
- {
- if (len)
- rule_scan_list(d, node,
- d->rule_bucket[rule_hash(cls, len)]);
- }
- static void rule_collect(struct br_doc *d, unsigned int node)
- {
- const char *cls = attr_ptr(d, d->nodes[node].class_off);
- const char *id = attr_ptr(d, d->nodes[node].id_off);
- unsigned int tag = d->nodes[node].tag;
- unsigned int i;
- nrule_matches = 0;
- rule_scan_list(d, node, d->rule_any);
- if (tag < BR_TAG_MAX)
- rule_scan_list(d, node, d->rule_tag[tag]);
- if (id[0])
- rule_scan_list(d, node,
- d->rule_bucket[rule_hash(id, s_len(id))]);
- i = 0;
- while (cls[i]) {
- unsigned int st, n;
- while (cls[i] && is_space(cls[i]))
- i++;
- st = i;
- while (cls[i] && !is_space(cls[i]))
- i++;
- n = i - st;
- if (n)
- rule_scan_class(d, node, cls + st, n);
- }
- /* Rules must cascade in document order even though the index visits
- * them in bucket order. */
- for (i = 1; i < nrule_matches; i++) {
- struct rule_match m = rule_matches[i];
- unsigned int j = i;
- while (j > 0 && rule_matches[j - 1].idx > m.idx) {
- rule_matches[j] = rule_matches[j - 1];
- j--;
- }
- rule_matches[j] = m;
- }
- }
- static void resolve_size_field(unsigned short *val, unsigned char *kind,
- int font_px)
- {
- if (*kind == 4) {
- *val = (unsigned short)(*val * font_px / 100);
- *kind = 1;
- } else if (*kind == 5) {
- *val = (unsigned short)(*val * root_font_px / 100);
- *kind = 1;
- }
- }
- static void resolve_size_units(struct br_style *st)
- {
- resolve_size_field(&st->width, &st->width_kind, st->font_px);
- resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
- resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
- resolve_size_field(&st->height, &st->height_kind, st->font_px);
- resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
- resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
- }
- static void set_border_side_w(struct br_style *st, int side, int v)
- {
- if (v < 0)
- v = 0;
- if (v > 32)
- v = 32;
- st->border_w[side] = (unsigned char)v;
- if (st->border_w[side] && !st->border_c[side])
- st->border_c[side] = st->color;
- }
- static void set_border_side_c(struct br_style *st, int side, int v)
- {
- st->border_c[side] = (unsigned short)v;
- }
- static void apply_decl(struct br_style *st, const struct br_decl *dcl)
- {
- switch (dcl->prop) {
- case PROP_COLOR:
- st->color = (unsigned short)dcl->val;
- break;
- case PROP_BG:
- st->bg = (unsigned short)dcl->val;
- st->has_bg = 1;
- st->bg_set = 1;
- break;
- case PROP_BG_NONE:
- st->has_bg = 0;
- st->bg_set = 1;
- break;
- case PROP_BG_IMAGE:
- st->bg_image = (unsigned short)dcl->val;
- break;
- case PROP_BG_REPEAT:
- st->bg_repeat = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_X_KIND:
- st->bg_pos_x_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_X:
- st->bg_pos_x = (signed short)dcl->val;
- break;
- case PROP_BG_POS_Y_KIND:
- st->bg_pos_y_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_Y:
- st->bg_pos_y = (signed short)dcl->val;
- break;
- case PROP_BG_SIZE_KIND:
- st->bg_size_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_SIZE_W:
- st->bg_size_w = (unsigned short)dcl->val;
- break;
- case PROP_BG_SIZE_H:
- st->bg_size_h = (unsigned short)dcl->val;
- break;
- case PROP_FONT_PX:
- st->font_raw = (unsigned short)dcl->val;
- break;
- case PROP_FONT_UNIT:
- st->font_unit = (unsigned char)dcl->val;
- break;
- case PROP_BOLD:
- st->bold = (unsigned char)(dcl->val != 0);
- break;
- case PROP_ITALIC:
- st->italic = (unsigned char)(dcl->val != 0);
- break;
- case PROP_MONO:
- st->mono = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FONT_FACE:
- st->font_face = (unsigned char)dcl->val;
- break;
- case PROP_ALIGN:
- st->align = (unsigned char)dcl->val;
- break;
- case PROP_UNDERLINE:
- st->underline = (unsigned char)(dcl->val != 0);
- break;
- case PROP_STRIKE:
- st->strike = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OVERLINE:
- st->overline = (unsigned char)(dcl->val != 0);
- break;
- case PROP_DISPLAY:
- st->display = (unsigned char)(dcl->val &
- ~BR_DISPLAY_INLINE_FLAG);
- st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
- st->list_item = 0;
- break;
- case PROP_VIS_HIDDEN:
- st->vis_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OPACITY_HIDDEN:
- st->opacity_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FILL_MODE:
- st->fill_mode = (unsigned char)dcl->val;
- break;
- case PROP_FILL:
- st->fill = (unsigned short)dcl->val;
- break;
- case PROP_STROKE_MODE:
- st->stroke_mode = (unsigned char)dcl->val;
- break;
- case PROP_STROKE:
- st->stroke = (unsigned short)dcl->val;
- break;
- case PROP_STROKE_W:
- st->stroke_w = (unsigned short)dcl->val;
- break;
- case PROP_FILL_RULE:
- st->fill_evenodd = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OVERFLOW_HIDDEN:
- st->overflow_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_MT: st->mt = dcl->val; break;
- case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
- case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
- case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
- case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
- case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
- case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
- case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
- case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
- case PROP_MR: st->mr = dcl->val; break;
- case PROP_MB: st->mb = dcl->val; break;
- case PROP_ML: st->ml = dcl->val; break;
- case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
- case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
- case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
- break;
- case PROP_PT: st->pt = dcl->val; break;
- case PROP_PR: st->pr = dcl->val; break;
- case PROP_PB: st->pb = dcl->val; break;
- case PROP_PL: st->pl = dcl->val; break;
- case PROP_MT_U:
- case PROP_MR_U:
- case PROP_MB_U:
- case PROP_ML_U:
- case PROP_PT_U:
- case PROP_PR_U:
- case PROP_PB_U:
- case PROP_PL_U:
- st->mp_unit[dcl->prop - PROP_MT_U] =
- (unsigned char)dcl->val;
- break;
- case PROP_BORDER_W:
- set_border_side_w(st, 0, dcl->val);
- set_border_side_w(st, 1, dcl->val);
- set_border_side_w(st, 2, dcl->val);
- set_border_side_w(st, 3, dcl->val);
- break;
- case PROP_BORDER_COLOR:
- set_border_side_c(st, 0, dcl->val);
- set_border_side_c(st, 1, dcl->val);
- set_border_side_c(st, 2, dcl->val);
- set_border_side_c(st, 3, dcl->val);
- break;
- case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
- case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
- case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
- case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
- case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
- case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
- case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
- case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
- case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
- case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
- case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
- case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
- case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
- case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
- case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
- case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
- case PROP_RADIUS_TL:
- case PROP_RADIUS_TR:
- case PROP_RADIUS_BR:
- case PROP_RADIUS_BL:
- st->radius[dcl->prop - PROP_RADIUS_TL] =
- (unsigned char)dcl->val;
- break;
- case PROP_LIST_NONE:
- st->no_bullet = (unsigned char)(dcl->val != 0);
- break;
- case PROP_LIST_STYLE:
- st->list_style = (unsigned char)dcl->val;
- break;
- case PROP_CONTENT:
- st->content = (unsigned short)dcl->val;
- break;
- case PROP_ASPECT_W:
- st->aspect_w = (unsigned short)dcl->val;
- break;
- case PROP_ASPECT_H:
- st->aspect_h = (unsigned short)dcl->val;
- break;
- case PROP_NOWRAP:
- st->nowrap = (unsigned char)(dcl->val != 0);
- break;
- case PROP_WIDTH:
- st->width = (unsigned short)dcl->val;
- break;
- case PROP_WIDTH_KIND:
- st->width_kind = (unsigned char)dcl->val;
- break;
- case PROP_WIDTH_CALC_PCT:
- st->width_calc_pct = dcl->val;
- break;
- case PROP_WIDTH_CALC_PX:
- st->width_calc_px = dcl->val;
- break;
- case PROP_MAXW:
- st->max_width = (unsigned short)dcl->val;
- break;
- case PROP_MAXW_KIND:
- st->max_width_kind = (unsigned char)dcl->val;
- break;
- case PROP_MAXW_CALC_PCT:
- st->max_width_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MAXW_CALC_PX:
- st->max_width_calc_px = (signed short)dcl->val;
- break;
- case PROP_MINW:
- st->min_width = (unsigned short)dcl->val;
- break;
- case PROP_MINW_KIND:
- st->min_width_kind = (unsigned char)dcl->val;
- break;
- case PROP_MINW_CALC_PCT:
- st->min_width_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MINW_CALC_PX:
- st->min_width_calc_px = (signed short)dcl->val;
- break;
- case PROP_HEIGHT:
- st->height = (unsigned short)dcl->val;
- break;
- case PROP_HEIGHT_KIND:
- st->height_kind = (unsigned char)dcl->val;
- break;
- case PROP_HEIGHT_CALC_PCT:
- st->height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_HEIGHT_CALC_PX:
- st->height_calc_px = (signed short)dcl->val;
- break;
- case PROP_MINH:
- st->min_height = (unsigned short)dcl->val;
- break;
- case PROP_MINH_KIND:
- st->min_height_kind = (unsigned char)dcl->val;
- break;
- case PROP_MINH_CALC_PCT:
- st->min_height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MINH_CALC_PX:
- st->min_height_calc_px = (signed short)dcl->val;
- break;
- case PROP_MAXH:
- st->max_height = (unsigned short)dcl->val;
- break;
- case PROP_MAXH_KIND:
- st->max_height_kind = (unsigned char)dcl->val;
- break;
- case PROP_MAXH_CALC_PCT:
- st->max_height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MAXH_CALC_PX:
- st->max_height_calc_px = (signed short)dcl->val;
- break;
- case PROP_LH_PCT:
- st->line_h_pct = (unsigned char)dcl->val;
- break;
- case PROP_LH_PX:
- st->line_h_px = (unsigned short)dcl->val;
- break;
- case PROP_UPPER:
- st->uppercase = (unsigned char)(dcl->val != 0);
- break;
- case PROP_CURSOR:
- st->cursor = (unsigned char)dcl->val;
- break;
- case PROP_FLOAT:
- st->float_left = (unsigned char)dcl->val;
- break;
- case PROP_CLEAR:
- st->clear = (unsigned char)dcl->val;
- break;
- case PROP_FLEX_GROW:
- st->flex_grow = (unsigned char)dcl->val;
- break;
- case PROP_FLEX_COL:
- st->flex_col = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FLEX_WRAP:
- st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
- break;
- case PROP_JUSTIFY:
- st->justify = (unsigned char)dcl->val;
- break;
- case PROP_ALIGN_ITEMS:
- st->align_items = (unsigned char)dcl->val;
- break;
- case PROP_ALIGN_SELF:
- st->align_self = (unsigned char)dcl->val;
- break;
- case PROP_GRID_NCOLS:
- st->grid_ncols = (unsigned char)dcl->val;
- break;
- case PROP_GRID_GAP:
- st->grid_gap = (unsigned char)dcl->val;
- break;
- case PROP_GRID_SLOT:
- st->grid_slot = (unsigned char)dcl->val;
- break;
- case PROP_GRID_AUTO:
- st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
- break;
- case PROP_POS:
- st->pos = (unsigned char)dcl->val;
- break;
- case PROP_LEFT:
- st->left = dcl->val;
- break;
- case PROP_TOP:
- st->top = dcl->val;
- break;
- case PROP_RIGHT:
- st->right = dcl->val;
- break;
- case PROP_BOTTOM:
- st->bottom = dcl->val;
- break;
- case PROP_LEFT_SET:
- st->left_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TOP_SET:
- st->top_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_RIGHT_SET:
- st->right_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_BOTTOM_SET:
- st->bottom_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_ZINDEX:
- st->z_index = dcl->val;
- break;
- case PROP_ZSET:
- st->z_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TRANSFORM_X:
- st->tx = dcl->val;
- break;
- case PROP_TRANSFORM_Y:
- st->ty = dcl->val;
- break;
- case PROP_TRANSFORM_SET:
- st->transform_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TRANSFORM_PAINT:
- st->transform_scale = (unsigned char)(dcl->val != 0);
- break;
- case PROP_SCALE_X:
- st->scale_x = (signed short)dcl->val;
- break;
- case PROP_SCALE_Y:
- st->scale_y = (signed short)dcl->val;
- break;
- case PROP_ROTATE:
- st->rotate_x100 = (signed short)dcl->val;
- break;
- case PROP_ORIGIN_X_KIND:
- st->origin_x_kind = (unsigned char)dcl->val;
- break;
- case PROP_ORIGIN_X:
- st->origin_x = (signed short)dcl->val;
- break;
- case PROP_ORIGIN_Y_KIND:
- st->origin_y_kind = (unsigned char)dcl->val;
- break;
- case PROP_ORIGIN_Y:
- st->origin_y = (signed short)dcl->val;
- break;
- default:
- break;
- }
- }
- /* The element's custom properties for rules flagged USES_SCOPED: the
- * definitions of its matched rules in cascade order (precedence first,
- * then document order), over the document-wide table. Inheritance from
- * ancestors is not modelled; a name no matched rule defines falls back to
- * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
- * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
- * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
- static void css_env_build(struct br_doc *d)
- {
- static unsigned short order[RULE_MATCH_MAX];
- unsigned int i, need = 0, n = 0;
- css_env_active = 0;
- css_nlocal = 0;
- for (i = 0; i < nrule_matches; i++)
- if (d->rules[rule_matches[i].idx].var_scope &
- BR_RULE_USES_SCOPED)
- need = 1;
- if (!need)
- return;
- /* Definitions apply lowest precedence first so the winner is
- * written last (stable: document order within a precedence). */
- for (i = 0; i < nrule_matches; i++) {
- struct br_rule *r = &d->rules[rule_matches[i].idx];
- unsigned int j = n;
- if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
- continue;
- while (j > 0 && rule_matches[order[j - 1]].spec >
- rule_matches[i].spec) {
- order[j] = order[j - 1];
- j--;
- }
- order[j] = i;
- n++;
- }
- for (i = 0; i < n; i++) {
- struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
- const char *raw = attr_ptr(d, r->raw_off);
- unsigned int save = css_nlocal, k;
- /* Collect this block's definitions after the earlier ones,
- * replacing a name already present. */
- {
- unsigned int base = save;
- css_local_append(raw, r->raw_len);
- for (k = base; k < css_nlocal; k++) {
- int prev = css_local_find_before(
- css_local[k].name, css_local[k].nlen, base);
- if (prev >= 0) {
- css_local[prev] = css_local[k];
- css_local[k].nlen = 0;
- }
- }
- }
- }
- css_env_active = 1;
- }
- static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
- int spec, int spec_imp, short *best, short *best_imp)
- {
- unsigned char p = dcl->prop;
- if (p >= PROP_COUNT)
- return;
- /* A transparent background competes with a colour for one slot. */
- if (p == PROP_BG_NONE)
- p = PROP_BG;
- if (dcl->important) {
- if (spec_imp >= best_imp[p]) {
- apply_decl(st, dcl);
- best_imp[p] = (short)spec_imp;
- }
- } else if (best_imp[p] < 0 && spec >= best[p]) {
- apply_decl(st, dcl);
- best[p] = (short)spec;
- }
- }
- void br_style_for(struct br_doc *d, unsigned int node,
- const struct br_style *parent, struct br_style *out)
- {
- unsigned int tag;
- short best[PROP_COUNT], best_imp[PROP_COUNT];
- unsigned int i;
- if (!cascade_pe && d->nodes[node].style_valid) {
- *out = d->nodes[node].style;
- return;
- }
- *out = *parent;
- out->has_bg = 0;
- out->bg_set = 0;
- out->bg_image = 0;
- out->bg_repeat = 0;
- out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
- out->bg_pos_x = out->bg_pos_y = 0;
- out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
- out->float_left = 0;
- out->clear = 0;
- out->list_item = 0;
- out->font_unit = 0;
- for (i = 0; i < 4; i++) {
- out->border_w[i] = 0;
- out->border_c[i] = 0;
- out->border_s[i] = BR_BORDER_SOLID;
- out->radius[i] = 0;
- }
- out->width = out->max_width = out->min_width = out->height = 0;
- out->min_height = out->max_height = 0;
- out->width_calc_pct = out->width_calc_px = 0;
- out->height_calc_pct = out->height_calc_px = 0;
- out->min_height_calc_pct = out->min_height_calc_px = 0;
- out->max_height_calc_pct = out->max_height_calc_px = 0;
- out->min_width_calc_pct = out->min_width_calc_px = 0;
- out->max_width_calc_pct = out->max_width_calc_px = 0;
- out->width_kind = out->max_width_kind = out->min_width_kind = 0;
- out->height_kind = out->min_height_kind = out->max_height_kind = 0;
- out->box_sizing = 0;
- out->ml_auto = out->mr_auto = 0;
- out->mt = out->mr = out->mb = out->ml = 0;
- out->pt = out->pr = out->pb = out->pl = 0;
- for (i = 0; i < 8; i++) {
- out->mp_unit[i] = 0;
- out->mp_calc_px[i] = 0;
- }
- out->flex_grow = out->flex_col = out->flex_wrap = 0;
- out->justify = out->align_items = out->align_self = 0;
- out->grid_ncols = out->grid_gap = out->grid_slot = 0;
- out->grid_auto = 0;
- out->aspect_w = out->aspect_h = 0;
- out->pos = out->left_set = out->top_set = 0;
- out->right_set = out->bottom_set = 0;
- out->left = out->top = out->right = out->bottom = 0;
- out->z_index = 0;
- out->z_set = 0;
- out->tx = out->ty = 0;
- out->transform_set = 0;
- out->transform_scale = 0;
- out->scale_x = out->scale_y = 100;
- out->rotate_x100 = 0;
- out->origin_x = out->origin_y = 50;
- out->origin_x_kind = out->origin_y_kind = 1;
- out->vis_hidden = out->opacity_hidden = 0;
- out->inline_flex = 0;
- out->overflow_hidden = 0;
- out->content = 0;
- /* A pseudo-element is an anonymous inline box: none of the
- * element's UA defaults or presentational hints apply to it. */
- tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
- out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
- BR_DISPLAY_INLINE;
- for (i = 0; i < PROP_COUNT; i++) {
- best[i] = -1;
- best_imp[i] = -1;
- }
- if (!cascade_pe)
- switch (tag) {
- case BR_TAG_HEAD:
- case BR_TAG_TITLE:
- case BR_TAG_META:
- case BR_TAG_LINK:
- case BR_TAG_BASE:
- case BR_TAG_SCRIPT:
- case BR_TAG_STYLE:
- case BR_TAG_NOSCRIPT: /* scripting is always on */
- case BR_TAG_TEMPLATE: /* inert until cloned by script */
- out->display = BR_DISPLAY_NONE;
- break;
- case BR_TAG_BODY:
- /* The UA default: the page itself has no gutter, so a site's
- * `body { margin: 0 }` really starts at the canvas edge. */
- out->mt = out->mb = out->ml = out->mr = 8;
- break;
- case BR_TAG_P:
- out->mt = out->mb = 8;
- break;
- case BR_TAG_H1:
- out->font_px = 32; out->bold = 1;
- out->mt = 10; out->mb = 6;
- break;
- case BR_TAG_H2:
- out->font_px = 24; out->bold = 1;
- out->mt = 8; out->mb = 4;
- break;
- case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
- out->bold = 1; out->mt = 6; out->mb = 3;
- if (tag == BR_TAG_H3)
- out->font_px = 19;
- else if (tag == BR_TAG_H4)
- out->font_px = 17;
- else if (tag == BR_TAG_H5)
- out->font_px = 15;
- else
- out->font_px = 13;
- break;
- case BR_TAG_UL: case BR_TAG_OL:
- out->mt = out->mb = 6; out->pl = 20;
- break;
- case BR_TAG_LI:
- out->mt = out->mb = 2;
- out->list_item = 1;
- break;
- case BR_TAG_BLOCKQUOTE:
- out->mt = out->mb = 6; out->ml = out->mr = 16;
- break;
- case BR_TAG_PRE:
- out->mt = out->mb = 8; out->pl = 8;
- out->has_bg = 1; out->bg = theme_panel;
- out->mono = 1;
- break;
- case BR_TAG_CENTER:
- out->align = BR_ALIGN_CENTER;
- break;
- case BR_TAG_A:
- out->color = theme_link; out->underline = 1;
- break;
- case BR_TAG_B: case BR_TAG_STRONG:
- out->bold = 1;
- break;
- case BR_TAG_I: case BR_TAG_EM:
- out->italic = 1;
- break;
- case BR_TAG_U:
- out->underline = 1;
- break;
- case BR_TAG_S: case BR_TAG_STRIKE:
- out->strike = 1;
- break;
- case BR_TAG_CODE:
- out->color = 0x8010;
- out->mono = 1;
- break;
- case BR_TAG_SMALL:
- out->font_px = 13;
- break;
- case BR_TAG_BIG:
- out->font_px = 20;
- break;
- case BR_TAG_TD: case BR_TAG_TH:
- out->pr = 12;
- /* A cell establishes left-aligned content even inside a legacy
- * <center> (HN's outer page wrapper); author CSS/align below
- * can still override this. */
- out->align = BR_ALIGN_LEFT;
- break;
- case BR_TAG_VIDEO:
- out->display = BR_DISPLAY_BLOCK;
- break;
- case BR_TAG_SVG:
- /* An SVG is a replaced inline box, like <img>: Docusaurus'
- * external-link icon follows its link text on the same line.
- * Flex rows still treat it as an item (flex_wants_row). */
- out->display = BR_DISPLAY_INLINE;
- break;
- default:
- break;
- }
- /* Legacy bgcolor/align attributes (tables/cells/body) are
- * presentational hints; author CSS still wins because it is applied
- * below. */
- if (!cascade_pe) {
- unsigned short bgc;
- const char *bga = br_find_attr(d, node, "bgcolor");
- const char *ala = br_find_attr(d, node, "align");
- if (bga && parse_color(bga, s_len(bga), &bgc)) {
- out->bg = bgc;
- out->has_bg = 1;
- }
- if (ala) {
- if (same_string(ala, "right"))
- out->align = BR_ALIGN_RIGHT;
- else if (same_string(ala, "center"))
- out->align = BR_ALIGN_CENTER;
- else if (same_string(ala, "left"))
- out->align = BR_ALIGN_LEFT;
- }
- }
- /* presentational <font color>: author CSS below still wins */
- if (!cascade_pe && d->nodes[node].color_off) {
- unsigned short c;
- const char *v = attr_ptr(d, d->nodes[node].color_off);
- if (parse_color(v, s_len(v), &c))
- out->color = c;
- }
- /* SVG presentation attributes; CSS declarations still win below. */
- if (!cascade_pe && in_svg(d, node)) {
- const char *fa = br_find_attr(d, node, "fill");
- const char *sa = br_find_attr(d, node, "stroke");
- const char *wa = br_find_attr(d, node, "stroke-width");
- const char *ra = br_find_attr(d, node, "fill-rule");
- const char *ca = br_find_attr(d, node, "color");
- unsigned short c;
- int mode;
- if (ca && parse_color(ca, s_len(ca), &c))
- out->color = c;
- if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
- out->fill_mode = (unsigned char)mode;
- if (mode == BR_PAINT_COLOR)
- out->fill = c;
- }
- if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
- out->stroke_mode = (unsigned char)mode;
- if (mode == BR_PAINT_COLOR)
- out->stroke = c;
- }
- if (wa) {
- int sw = svg_len16(wa, s_len(wa));
- if (sw >= 0)
- out->stroke_w = (unsigned short)sw;
- }
- if (ra)
- out->fill_evenodd = same_string(ra, "evenodd");
- }
- /* UA behaviour for a closed <details>: only its <summary> is shown.
- * Author CSS below can still override this. */
- if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
- unsigned int p = d->nodes[node].parent;
- if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
- !br_find_attr(d, p, "open"))
- out->display = BR_DISPLAY_NONE;
- }
- rule_collect(d, node);
- css_env_build(d);
- for (i = 0; i < nrule_matches; i++) {
- struct br_rule *r = &d->rules[rule_matches[i].idx];
- int spec = rule_matches[i].spec;
- int spec_imp = rule_matches[i].spec_imp;
- unsigned int k;
- if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
- /* Re-expand against this element's variables. */
- struct br_decl decls[BR_DECLS];
- unsigned int nd;
- parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
- decls, &nd);
- for (k = 0; k < nd; k++)
- cascade_decl(out, &decls[k], spec, spec_imp,
- best, best_imp);
- continue;
- }
- for (k = 0; k < r->nd; k++)
- cascade_decl(out, &r->d[k], spec, spec_imp, best,
- best_imp);
- }
- css_env_active = 0;
- css_nlocal = 0;
- if (!cascade_pe && d->nodes[node].style_off) {
- struct br_decl decls[BR_DECLS];
- unsigned int nd, k;
- const char *v = attr_ptr(d, d->nodes[node].style_off);
- parse_decls(d, v, s_len(v), decls, &nd);
- for (k = 0; k < nd; k++)
- cascade_decl(out, &decls[k],
- CASCADE_INLINE * CASCADE_BAND,
- CASCADE_INLINE * CASCADE_BAND, best,
- best_imp);
- }
- if (out->font_unit) {
- int px = out->font_raw;
- if (out->font_unit == 2)
- px = parent->font_px * out->font_raw / 100;
- else if (out->font_unit == 3)
- px = root_font_px * out->font_raw / 100;
- else if (out->font_unit == 4)
- px = parent->font_px * out->font_raw / 10000;
- if (px < 8)
- px = 8;
- if (px > 72)
- px = 72;
- out->font_px = (unsigned char)px;
- }
- resolve_size_units(out);
- if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
- out->display = BR_DISPLAY_NONE;
- if (out->vis_hidden || out->opacity_hidden ||
- (out->overflow_hidden && out->height_kind == 1 &&
- out->height == 0))
- out->display = BR_DISPLAY_NONE;
- if (cascade_pe)
- return;
- d->nodes[node].style = *out;
- d->nodes[node].style_valid = 1;
- }
- unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
- const struct br_style *elem, struct br_style *out,
- char *buf, unsigned int cap)
- {
- struct br_node *n;
- unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
- const char *c;
- unsigned int len = 0;
- if (out) {
- out->content = 0;
- out->display = BR_DISPLAY_NONE;
- }
- if (!d || !d->npe_rules || node >= d->nnode || !cap ||
- (pe != 1 && pe != 2))
- return 0;
- n = &d->nodes[node];
- if (n->type != BR_NODE_ELEM ||
- ((n->pe_state & chk) && !(n->pe_state & has)))
- return 0;
- cascade_pe = pe;
- br_style_for(d, node, elem, out);
- cascade_pe = 0;
- if (out->content == BR_CONTENT_EMPTY) {
- /* No text; the caller may still draw the box (an icon
- * with a size and a background image). */
- buf[0] = '\0';
- n->pe_state |= chk;
- if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
- out->pos != 2 && (out->width_kind || out->height_kind) &&
- (out->has_bg || out->bg_image))
- n->pe_state |= has;
- else
- out->display = BR_DISPLAY_NONE;
- return 0;
- }
- if (out->content && out->display != BR_DISPLAY_NONE &&
- out->pos != 1 && out->pos != 2) {
- c = attr_ptr(d, d->content_off[out->content - 1]);
- while (*c && len + 1 < cap) {
- if (*c == '\001') {
- char name[32];
- unsigned int k = 0;
- const char *v;
- c++;
- while (*c && *c != '\002' && k + 1 < sizeof(name))
- name[k++] = *c++;
- name[k] = '\0';
- if (*c == '\002')
- c++;
- v = br_find_attr(d, node, name);
- while (v && *v && len + 1 < cap)
- buf[len++] = *v++;
- continue;
- }
- buf[len++] = *c++;
- }
- }
- buf[len] = '\0';
- n->pe_state |= chk;
- if (len)
- n->pe_state |= has;
- return len;
- }
- /* ---- HTML parser ---- */
- static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
- unsigned int sp, int *pre)
- {
- unsigned int i;
- *pre = 0;
- for (i = 1; i <= sp; i++)
- if (d->nodes[stack[i]].tag == BR_TAG_PRE)
- (*pre)++;
- }
- static void implicit_close(struct br_doc *d, unsigned short *stack,
- unsigned int *sp, unsigned int tag)
- {
- while (*sp > 0) {
- unsigned int top = d->nodes[stack[*sp]].tag;
- if (top == BR_TAG_P && br_tag_is_block(tag)) {
- (*sp)--;
- continue;
- }
- if (top == BR_TAG_LI && tag == BR_TAG_LI) {
- (*sp)--;
- continue;
- }
- if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
- (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
- (*sp)--;
- continue;
- }
- if (top == BR_TAG_TR && tag == BR_TAG_TR) {
- (*sp)--;
- continue;
- }
- break;
- }
- }
- static int has_attr_token(const char *s, unsigned int len, const char *name)
- {
- unsigned int i, n = s_len(name);
- for (i = 0; i + n <= len; i++) {
- if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
- continue;
- if (!ci_eq_n(s + i, name, n))
- continue;
- if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
- return 1;
- }
- return 0;
- }
- static int stack_has_svg(const struct br_doc *d,
- const unsigned short *stack, unsigned int sp)
- {
- unsigned int k;
- for (k = 0; k <= sp && k < 64; k++)
- if (d->nodes[stack[k]].tag == BR_TAG_SVG)
- return 1;
- return 0;
- }
- /* Skip an element subtree by finding its closing tag. Used for SVG
- * <title>/<desc>/<metadata> metadata, which must not enter the flow or
- * overwrite the document title. */
- static unsigned int skip_element(const char *html, unsigned int len,
- unsigned int i, const char *name,
- unsigned int n)
- {
- char close[40];
- unsigned int k = 0, p;
- close[k++] = '<';
- close[k++] = '/';
- while (k < sizeof(close) - 1 && n--)
- close[k++] = *name++;
- p = (unsigned int)find_ci(html, len, close, k, i);
- if ((int)p < 0)
- return len;
- p += k;
- while (p < len && html[p] != '>')
- p++;
- if (p < len)
- p++;
- return p;
- }
- /* Store a <script src=...> value in the attribute arena. Kept separate from
- * parse_attrs() so script-only attributes cannot be confused with the
- * value/d attributes that also use src_off on other elements. */
- static unsigned int script_src_attr(struct br_doc *d, const char *s,
- unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int ns, ne, vs, ve;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- ns = i;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
- i++;
- ne = i;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] != '=')
- continue;
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vs = i;
- while (i < len && s[i] != quote)
- i++;
- ve = i;
- if (i < len)
- i++;
- } else {
- vs = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- ve = i;
- }
- if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
- return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
- }
- return 0;
- }
- /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
- * application/json, text/template, ...) and non-JS type values. External
- * src scripts are allowed; the browser fetches them separately. Returns 1
- * for a classic script, 2 for type="module", 0 to skip (also for a
- * `nomodule` fallback, since module scripts run). */
- static int script_is_javascript(const char *s, unsigned int len)
- {
- int kind = 1;
- unsigned int i = 0;
- while (i < len) {
- unsigned int ns, ne, vs = 0, ve = 0;
- char quote = 0;
- int has_val = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- break;
- ns = i;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
- i++;
- ne = i;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '=') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vs = i;
- while (i < len && s[i] != quote)
- i++;
- ve = i;
- if (i < len)
- i++;
- } else {
- vs = i;
- while (i < len && !is_space(s[i]) && s[i] != '/')
- i++;
- ve = i;
- }
- has_val = 1;
- } else if (i == ne) {
- i++;
- }
- if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
- return 0;
- if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
- unsigned int vn = has_val ? ve - vs : 0;
- if (vn == 0)
- continue;
- if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
- kind = 2;
- continue;
- }
- if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
- (vn == 22 &&
- ci_eq_n(s + vs, "application/javascript", 22)) ||
- (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
- (vn == 22 &&
- ci_eq_n(s + vs, "application/ecmascript", 22)))
- continue;
- return 0;
- }
- }
- return kind;
- }
- int br_parse(struct br_doc *d, const char *html, unsigned int len)
- {
- unsigned short stack[64];
- unsigned int sp = 0;
- unsigned int i = 0;
- int pre = 0;
- unsigned int css_len = 0;
- if (!d)
- return -1;
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return -1;
- doc_reset(d);
- d->script_src = html;
- stack[0] = 0;
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- while (i < len) {
- if (html[i] != '<') {
- unsigned int st = i;
- while (i < len && html[i] != '<')
- i++;
- if (sp <= 63)
- append_text(d, stack[sp], html + st, i - st, pre);
- continue;
- }
- if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
- html[i + 3] == '-') {
- int p = find_ci(html, len, "-->", 3, i + 4);
- i = p >= 0 ? (unsigned int)p + 3 : len;
- continue;
- }
- if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (i + 1 < len && html[i + 1] == '/') {
- unsigned int ns = i + 2, ne, tag, k, n;
- char name[32];
- while (ns < len && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < len && is_ident(html[ne]))
- ne++;
- if (ne == ns) {
- i++;
- continue;
- }
- n = ne - ns;
- if (n >= sizeof(name))
- n = sizeof(name) - 1;
- for (k = 0; k < n; k++)
- name[k] = html[ns + k];
- name[n] = '\0';
- tag = br_tag_from_name(name);
- i = ne;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- for (k = sp; k > 0; k--) {
- struct br_node *nd = &d->nodes[stack[k]];
- if (tag == BR_TAG_UNKNOWN) {
- const char *raw = attr_ptr(d, nd->name_off);
- if (nd->tag == BR_TAG_UNKNOWN &&
- ci_eq_n(raw, name, n) &&
- raw[n] == '\0') {
- sp = k - 1;
- break;
- }
- } else if (nd->tag == tag) {
- sp = k - 1;
- break;
- }
- }
- stack_recount_pre(d, stack, sp, &pre);
- continue;
- }
- /* start tag */
- {
- unsigned int ns = i + 1, ne, tag, k, n;
- char name[16];
- unsigned int attr_start, attr_end;
- int selfclose = 0;
- while (ns < len && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
- ne++;
- if (ne == ns) {
- /* stray '<' */
- append_text(d, stack[sp], html + i, 1, pre);
- i++;
- continue;
- }
- n = ne - ns;
- if (n >= sizeof(name))
- n = sizeof(name) - 1;
- for (k = 0; k < n; k++)
- name[k] = html[ns + k];
- name[n] = '\0';
- tag = br_tag_from_name(name);
- attr_start = ne;
- {
- unsigned int q = ne;
- char quote = 0;
- while (q < len) {
- char c = html[q];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '>') {
- break;
- }
- q++;
- }
- attr_end = q;
- /* `/>` closes the tag only when the slash is
- * not the end of an unquoted attribute value:
- * Docusaurus writes `<a href=/installation/>`,
- * which used to self-close the link and leave
- * its label outside it. */
- if (q > attr_start && html[q - 1] == '/' &&
- (q - 1 == attr_start ||
- is_space(html[q - 2]) ||
- html[q - 2] == '"' || html[q - 2] == '\''))
- selfclose = 1;
- i = q < len ? q + 1 : len;
- }
- if (tag == BR_TAG_SCRIPT) {
- int p = find_ci(html, len, "</script", 8, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int idx;
- struct br_script *script = 0;
- int kind = script_is_javascript(html + attr_start,
- attr_end - attr_start);
- if (kind) {
- if (d->nscripts < BR_MAX_SCRIPTS) {
- script = &d->scripts[d->nscripts++];
- script->module = kind == 2;
- script->node = 0;
- script->off = i;
- script->len = end > i ? end - i : 0;
- script->src_off = script_src_attr(
- d, html + attr_start,
- attr_end - attr_start);
- } else {
- d->scripts_truncated = 1;
- }
- }
- /* Keep the element in the DOM even though its body is
- * executed separately: scripts (Piwik, analytics)
- * insert a sibling after getElementsByTagName('script')
- * [0], and the element must exist for that to work. */
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- if (script)
- script->node = idx;
- if (idx && !kind && end > i)
- append_raw_text(d, idx, html + i, end - i);
- i = p >= 0 ? (unsigned int)p + 8 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- /* Scripting is always enabled, so <noscript> is a
- * raw-text element (HTML "in head/in body, scripting
- * flag set"): its markup must not reach the DOM or the
- * cascade. google.com puts a meta refresh and
- * `table,div,span,p{display:none}` in one, which hid
- * the whole page and left only the "click here" link. */
- if (tag == BR_TAG_NOSCRIPT && !selfclose) {
- int p = find_ci(html, len, "</noscript", 10, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int idx;
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx) {
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- if (end > i)
- append_raw_text(d, idx, html + i,
- end - i);
- }
- i = p >= 0 ? (unsigned int)p + 10 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (stack_has_svg(d, stack, sp) &&
- (tag == BR_TAG_TITLE ||
- (tag == BR_TAG_UNKNOWN &&
- (ci_eq_n(name, "desc", 4) ||
- ci_eq_n(name, "metadata", 8))))) {
- i = skip_element(html, len, i, name, n);
- continue;
- }
- /* A closed <details> is collapsed at style time, not
- * parse time: the <summary> (the hamburger button on
- * tagesschau and many mobile menus) must stay in the
- * DOM. */
- if (tag == BR_TAG_STYLE) {
- int p = find_ci(html, len, "</style", 7, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int cend = end;
- unsigned int idx;
- while (cend > i && is_space(html[cend - 1]))
- cend--;
- if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
- for (k = i; k < cend; k++)
- css_store[css_len++] = html[k];
- css_store[css_len++] = '\n';
- }
- /* The CSS is collected above, but keep a display:none
- * element so getElementsByTagName('style')/parentNode
- * behave like a real document. */
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- i = p >= 0 ? (unsigned int)p + 7 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (tag == BR_TAG_TITLE) {
- int p = find_ci(html, len, "</title", 7, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int tn = 0;
- char tmp[BR_TITLE_MAX];
- tn = decode_text(html + i, end - i, tmp,
- sizeof(tmp), 1);
- (void)tn;
- s_copy(d->title, tmp, sizeof(d->title));
- i = p >= 0 ? (unsigned int)p + 7 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
- tag == BR_TAG_IMG || br_tag_is_void(tag)) {
- unsigned int idx;
- implicit_close(d, stack, &sp, tag);
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- continue;
- }
- if (sp >= 63) {
- d->truncated = 1;
- continue;
- }
- implicit_close(d, stack, &sp, tag);
- {
- unsigned int idx = node_append(d, stack[sp],
- BR_NODE_ELEM,
- (unsigned char)tag);
- if (!idx)
- continue;
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- if (tag == BR_TAG_UNKNOWN)
- d->nodes[idx].name_off =
- store_attr(d, name, n, 31);
- /* HTML ignores the self-closing flag on a
- * normal element (`<div/>` opens a div); only
- * SVG content (and unknown/custom tags, kept
- * as before) honours it. */
- if (selfclose && tag != BR_TAG_UNKNOWN &&
- tag != BR_TAG_SVG &&
- !stack_has_svg(d, stack, sp))
- selfclose = 0;
- if (!selfclose) {
- sp++;
- stack[sp] = (unsigned short)idx;
- if (tag == BR_TAG_PRE)
- pre++;
- }
- }
- }
- }
- d->inline_css_len = css_len;
- return 0;
- }
- int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
- {
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return -1;
- doc_reset(d);
- d->plain = 1;
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- append_text(d, 0, text, len, 1);
- return 0;
- }
- /* A standalone image response: a synthetic document with one <img>. The
- * caller decodes the already-fetched body into image slot 0 and sets the
- * returned node's img_index. */
- unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
- {
- unsigned int idx;
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return 0;
- doc_reset(d);
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
- if (!idx)
- return 0;
- d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
- return idx;
- }
|