browser_html.c 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830
  1. /* browser_html: see browser_html.h. A deliberately tolerant parser: bad
  2. * nesting is repaired, unknown tags are transparent, and the CSS engine only
  3. * implements the properties in the browser's documented subset. */
  4. #include "browser_html.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. static int is_space(char c);
  8. static int is_ident(char c);
  9. /* ---- storage (one document per process) ----
  10. *
  11. * The node/text/attribute arenas are allocated from the ring-3 heap, not
  12. * static BSS: a large article needs several MB and the process image lives
  13. * in the 4 MB user window. heap_reset() on navigation releases them with
  14. * the rest of the page heap. */
  15. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  16. static char css_store[BR_CSS_SIZE];
  17. static int root_font_px = 16;
  18. #define BR_BG_URLS 16
  19. static char bg_urls[BR_BG_URLS][192];
  20. static unsigned int bg_url_count;
  21. /* @font-face definitions, one document at a time. The URL is resolved by
  22. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  23. struct br_font_def {
  24. char family[BR_FACE_FAMILY];
  25. char url[BR_FACE_URL];
  26. char base[192];
  27. unsigned char used;
  28. };
  29. static struct br_font_def font_defs[BR_MAX_FACES];
  30. static int font_face_count;
  31. static char font_base[192];
  32. static void font_reset(void)
  33. {
  34. memset(font_defs, 0, sizeof(font_defs));
  35. font_face_count = 0;
  36. font_base[0] = '\0';
  37. }
  38. void br_set_font_base(const char *base)
  39. {
  40. unsigned int i = 0;
  41. if (!base)
  42. base = "";
  43. while (base[i] && i + 1 < sizeof(font_base)) {
  44. font_base[i] = base[i];
  45. i++;
  46. }
  47. font_base[i] = '\0';
  48. }
  49. int br_font_face_count(void)
  50. {
  51. return font_face_count;
  52. }
  53. const char *br_font_face_url(int id)
  54. {
  55. if (id < 1 || id > font_face_count)
  56. return "";
  57. return font_defs[id - 1].url;
  58. }
  59. const char *br_font_face_base(int id)
  60. {
  61. if (id < 1 || id > font_face_count)
  62. return "";
  63. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  64. font_base;
  65. }
  66. const char *br_font_face_family(int id)
  67. {
  68. if (id < 1 || id > font_face_count)
  69. return "";
  70. return font_defs[id - 1].family;
  71. }
  72. const char *br_bg_image_url(unsigned int idx)
  73. {
  74. if (idx >= bg_url_count)
  75. return "";
  76. return bg_urls[idx];
  77. }
  78. unsigned int br_root_font_px(void)
  79. {
  80. return (unsigned int)root_font_px;
  81. }
  82. /* ---- tag table ---- */
  83. struct tag_name {
  84. const char *name;
  85. unsigned char tag;
  86. };
  87. static const struct tag_name tag_names[] = {
  88. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  89. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  90. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  91. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  92. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  93. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  94. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  95. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  96. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  97. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  98. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  99. { "picture", BR_TAG_PICTURE },
  100. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  101. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  102. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  103. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  104. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  105. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  106. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  107. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  108. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  109. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  110. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  111. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  112. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  113. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  114. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  115. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  116. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  117. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  118. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  119. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  120. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  121. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  122. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  123. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  124. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  125. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  126. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  127. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  128. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  129. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  130. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  131. { 0, 0 }
  132. };
  133. const char *br_tag_name(unsigned int tag)
  134. {
  135. unsigned int i;
  136. for (i = 0; tag_names[i].name; i++)
  137. if (tag_names[i].tag == tag)
  138. return tag_names[i].name;
  139. return "?";
  140. }
  141. static unsigned int s_len(const char *s);
  142. #define TAG_HASH_SIZE 128
  143. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  144. static int tag_hash_ready;
  145. static unsigned int tag_hash_key(const char *name, unsigned int len)
  146. {
  147. unsigned int i, h = 2166136261u;
  148. for (i = 0; i < len; i++) {
  149. h ^= (unsigned char)name[i];
  150. h *= 16777619u;
  151. }
  152. return h & (TAG_HASH_SIZE - 1);
  153. }
  154. static void tag_hash_build(void)
  155. {
  156. unsigned int i;
  157. for (i = 0; i < TAG_HASH_SIZE; i++)
  158. tag_hash_idx[i] = 0xFF;
  159. for (i = 0; tag_names[i].name; i++) {
  160. unsigned int h = tag_hash_key(tag_names[i].name,
  161. s_len(tag_names[i].name));
  162. while (tag_hash_idx[h] != 0xFF)
  163. h = (h + 1) & (TAG_HASH_SIZE - 1);
  164. tag_hash_idx[h] = (unsigned char)i;
  165. }
  166. tag_hash_ready = 1;
  167. }
  168. unsigned int br_tag_from_name(const char *name)
  169. {
  170. char lower[16];
  171. unsigned int i, len, h;
  172. unsigned char idx;
  173. for (i = 0; i < 15 && name[i]; i++) {
  174. char c = name[i];
  175. if (c >= 'A' && c <= 'Z')
  176. c = (char)(c - 'A' + 'a');
  177. lower[i] = c;
  178. }
  179. lower[i] = '\0';
  180. len = i;
  181. if (!tag_hash_ready)
  182. tag_hash_build();
  183. h = tag_hash_key(lower, len);
  184. for (;;) {
  185. idx = tag_hash_idx[h];
  186. if (idx == 0xFF)
  187. return BR_TAG_UNKNOWN;
  188. {
  189. const char *n = tag_names[idx].name;
  190. unsigned int j;
  191. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  192. ;
  193. if (!lower[j] && !n[j])
  194. return tag_names[idx].tag;
  195. }
  196. h = (h + 1) & (TAG_HASH_SIZE - 1);
  197. }
  198. }
  199. int br_tag_is_void(unsigned int tag)
  200. {
  201. switch (tag) {
  202. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  203. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  204. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  205. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  206. case BR_TAG_AREA: case BR_TAG_COL:
  207. return 1;
  208. default:
  209. return 0;
  210. }
  211. }
  212. int br_tag_is_block(unsigned int tag)
  213. {
  214. switch (tag) {
  215. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  216. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  217. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  218. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  219. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  220. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  221. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  222. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  223. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  224. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  225. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  226. case BR_TAG_PICTURE:
  227. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  228. case BR_TAG_ADDRESS: case BR_TAG_HR:
  229. return 1;
  230. default:
  231. return 0;
  232. }
  233. }
  234. /* ---- string / entity helpers ---- */
  235. static unsigned int s_len(const char *s)
  236. {
  237. unsigned int n = 0;
  238. while (s && s[n])
  239. n++;
  240. return n;
  241. }
  242. static void s_copy(char *d, const char *s, unsigned int cap)
  243. {
  244. unsigned int i = 0;
  245. if (!cap)
  246. return;
  247. while (s && s[i] && i + 1 < cap) {
  248. d[i] = s[i];
  249. i++;
  250. }
  251. d[i] = '\0';
  252. }
  253. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  254. {
  255. unsigned int i;
  256. for (i = 0; i < n; i++) {
  257. char ca = a[i], cb = b[i];
  258. if (ca >= 'A' && ca <= 'Z')
  259. ca = (char)(ca - 'A' + 'a');
  260. if (cb >= 'A' && cb <= 'Z')
  261. cb = (char)(cb - 'A' + 'a');
  262. if (ca != cb)
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. static int find_ci(const char *hay, unsigned int len, const char *needle,
  268. unsigned int nlen, unsigned int from)
  269. {
  270. unsigned int i;
  271. if (nlen == 0 || from >= len)
  272. return -1;
  273. for (i = from; i + nlen <= len; i++)
  274. if (ci_eq_n(hay + i, needle, nlen))
  275. return (int)i;
  276. return -1;
  277. }
  278. static const struct {
  279. const char *name;
  280. const char *text;
  281. } entities[] = {
  282. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  283. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  284. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  285. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  286. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  287. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  288. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  289. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  290. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  291. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  292. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  293. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  294. { 0, 0 }
  295. };
  296. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  297. {
  298. if (*n + 1 < cap)
  299. out[(*n)++] = c;
  300. }
  301. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  302. char *out, unsigned int cap, unsigned int *n)
  303. {
  304. unsigned int start = *pos + 1, i = start;
  305. unsigned int code = 0;
  306. int ok = 0;
  307. while (i < len && s[i] != ';' && i - start < 12)
  308. i++;
  309. if (i >= len || s[i] != ';') {
  310. /* Not an entity: emit the '&' and advance past it. Leaving
  311. * *pos unchanged here used to spin forever on bare '&'. */
  312. emit(out, cap, n, '&');
  313. *pos = start;
  314. return;
  315. }
  316. if (s[start] == '#') {
  317. unsigned int j = start + 1;
  318. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  319. j++;
  320. for (; j < i; j++) {
  321. char c = s[j];
  322. int d = -1;
  323. if (c >= '0' && c <= '9') d = c - '0';
  324. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  325. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  326. if (d < 0) break;
  327. code = code * 16 + (unsigned int)d;
  328. ok = 1;
  329. }
  330. } else {
  331. for (; j < i; j++) {
  332. if (s[j] < '0' || s[j] > '9')
  333. break;
  334. code = code * 10 + (unsigned int)(s[j] - '0');
  335. ok = 1;
  336. }
  337. }
  338. } else {
  339. unsigned int k;
  340. for (k = 0; entities[k].name; k++) {
  341. unsigned int el = s_len(entities[k].name);
  342. unsigned int j;
  343. if (el != i - start)
  344. continue;
  345. for (j = 0; j < el; j++)
  346. if (s[start + j] != entities[k].name[j])
  347. break;
  348. if (j == el) {
  349. const char *t = entities[k].text;
  350. while (*t)
  351. emit(out, cap, n, *t++);
  352. *pos = i + 1;
  353. return;
  354. }
  355. }
  356. }
  357. if (ok && code) {
  358. if (code == 0x00A0)
  359. emit(out, cap, n, ' ');
  360. else if (code == 0x2013 || code == 0x2014)
  361. emit(out, cap, n, '-');
  362. else if (code == 0x2018 || code == 0x2019)
  363. emit(out, cap, n, '\'');
  364. else if (code == 0x201C || code == 0x201D)
  365. emit(out, cap, n, '"');
  366. else if (code == 0x2026) {
  367. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  368. emit(out, cap, n, '.');
  369. } else if (code <= 0xFF) {
  370. emit(out, cap, n, (char)code);
  371. } else {
  372. emit(out, cap, n, '?');
  373. }
  374. } else {
  375. emit(out, cap, n, '?');
  376. }
  377. *pos = i + 1;
  378. }
  379. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  380. * space and drops leading spaces. Returns the output length. */
  381. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  382. unsigned int cap, int collapse)
  383. {
  384. unsigned int i = 0, n = 0;
  385. while (i < len) {
  386. unsigned char c = (unsigned char)s[i];
  387. if (c == '&') {
  388. decode_entity(s, len, &i, out, cap, &n);
  389. continue;
  390. }
  391. if (c < 0x80) {
  392. i++;
  393. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  394. c == '\r' || c == '\f')) {
  395. if (n > 0 && out[n - 1] == ' ')
  396. continue;
  397. emit(out, cap, &n, ' ');
  398. continue;
  399. }
  400. if (!collapse && c == '\t') {
  401. emit(out, cap, &n, ' ');
  402. emit(out, cap, &n, ' ');
  403. emit(out, cap, &n, ' ');
  404. emit(out, cap, &n, ' ');
  405. continue;
  406. }
  407. emit(out, cap, &n, (char)c);
  408. continue;
  409. }
  410. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  411. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  412. unsigned int cp = ((c & 0x1F) << 6) |
  413. ((unsigned char)s[i + 1] & 0x3F);
  414. i += 2;
  415. if (cp == 0xA0)
  416. cp = ' ';
  417. else if (cp == 0xB7)
  418. cp = '-';
  419. else if (cp == 0xD7)
  420. cp = 'x';
  421. else if (cp == 0xB0)
  422. cp = 'o';
  423. if (cp <= 0xFF)
  424. emit(out, cap, &n, (char)cp);
  425. else
  426. emit(out, cap, &n, '?');
  427. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  428. unsigned int cp = ((c & 0x0F) << 12) |
  429. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  430. ((unsigned char)s[i + 2] & 0x3F);
  431. i += 3;
  432. if (cp == 0x2013 || cp == 0x2014) {
  433. emit(out, cap, &n, '-');
  434. } else if (cp == 0x2018 || cp == 0x2019) {
  435. emit(out, cap, &n, '\'');
  436. } else if (cp == 0x201C || cp == 0x201D) {
  437. emit(out, cap, &n, '"');
  438. } else if (cp == 0x2026) {
  439. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  440. emit(out, cap, &n, '.');
  441. } else if (cp == 0x2192) {
  442. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  443. } else if (cp == 0x2264) {
  444. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  445. } else {
  446. emit(out, cap, &n, '?');
  447. }
  448. } else {
  449. i++;
  450. emit(out, cap, &n, '?');
  451. }
  452. }
  453. out[n] = '\0';
  454. return n;
  455. }
  456. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  457. * space / empty. */
  458. static unsigned int store_attr(struct br_doc *d, const char *s,
  459. unsigned int len, unsigned int limit)
  460. {
  461. unsigned int n;
  462. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  463. return 0;
  464. if (len > limit)
  465. len = limit;
  466. n = decode_text(s, len, d->attrs + d->attrs_used,
  467. BR_ATTR_SIZE - d->attrs_used, 0);
  468. if (n == 0)
  469. return 0;
  470. {
  471. unsigned int off = d->attrs_used;
  472. d->attrs_used += n + 1;
  473. return off;
  474. }
  475. }
  476. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  477. {
  478. return off ? d->attrs + off : "";
  479. }
  480. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  481. static unsigned int store_raw(struct br_doc *d, const char *s,
  482. unsigned int len, unsigned int limit)
  483. {
  484. unsigned int off;
  485. if (len > limit)
  486. len = limit;
  487. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  488. return 0;
  489. off = d->attrs_used;
  490. {
  491. unsigned int i;
  492. for (i = 0; i < len; i++)
  493. d->attrs[off + i] = s[i];
  494. }
  495. d->attrs[off + len] = '\0';
  496. d->attrs_used += len + 1;
  497. return off;
  498. }
  499. #define VAR_HASH_SIZE 1024
  500. static unsigned int var_hash(const char *name, unsigned int len)
  501. {
  502. unsigned int i, h = 2166136261u;
  503. for (i = 0; i < len; i++) {
  504. h ^= (unsigned char)name[i];
  505. h *= 16777619u;
  506. }
  507. return h & (VAR_HASH_SIZE - 1);
  508. }
  509. static unsigned int css_var_find(struct br_doc *d, const char *name,
  510. unsigned int nlen)
  511. {
  512. unsigned int h = var_hash(name, nlen);
  513. for (;;) {
  514. unsigned int i = d->var_slot[h];
  515. if (!i)
  516. return BR_MAX_VARS;
  517. i--;
  518. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  519. const char *n = attr_ptr(d, d->var_name[i]);
  520. unsigned int j;
  521. for (j = 0; j < nlen && n[j] == name[j]; j++)
  522. ;
  523. if (j == nlen)
  524. return i;
  525. }
  526. h = (h + 1) & (VAR_HASH_SIZE - 1);
  527. }
  528. }
  529. static void css_var_set(struct br_doc *d, const char *name,
  530. unsigned int nlen, const char *val, unsigned int vlen)
  531. {
  532. unsigned int i, noff, voff;
  533. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  534. return;
  535. noff = store_raw(d, name, nlen, 63);
  536. voff = store_raw(d, val, vlen, 199);
  537. if (!noff || !voff)
  538. return;
  539. i = css_var_find(d, name, nlen);
  540. if (i >= BR_MAX_VARS) {
  541. unsigned int h;
  542. if (d->nvars >= BR_MAX_VARS)
  543. return;
  544. i = d->nvars++;
  545. d->var_name[i] = noff;
  546. h = var_hash(name, nlen);
  547. while (d->var_slot[h])
  548. h = (h + 1) & (VAR_HASH_SIZE - 1);
  549. d->var_slot[h] = (unsigned short)(i + 1);
  550. }
  551. d->var_val[i] = voff;
  552. }
  553. /* Find the end of a declaration value: the next ';' outside quotes and
  554. * parentheses. The caller passes the position just after the ':'. */
  555. static unsigned int css_value_end(const char *s, unsigned int len,
  556. unsigned int i)
  557. {
  558. int depth = 0;
  559. char quote = 0;
  560. while (i < len) {
  561. char c = s[i];
  562. if (quote) {
  563. if (c == quote)
  564. quote = 0;
  565. else if (c == '\\' && i + 1 < len)
  566. i++;
  567. } else if (c == '"' || c == '\'') {
  568. quote = c;
  569. } else if (c == '(') {
  570. depth++;
  571. } else if (c == ')' && depth > 0) {
  572. depth--;
  573. } else if (c == ';' && depth == 0) {
  574. break;
  575. }
  576. i++;
  577. }
  578. return i;
  579. }
  580. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  581. * second palette. We render the light page, so skip their custom-property
  582. * definitions; otherwise their last-wins values leak into the light rules. */
  583. static int css_dark_selector(const char *s, unsigned int len)
  584. {
  585. unsigned int i;
  586. for (i = 0; i + 4 <= len; i++) {
  587. if ((s[i] == 'n' || s[i] == 'N') &&
  588. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  589. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  590. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  591. (s[i + 4] == 't' || s[i + 4] == 'T'))
  592. return 1;
  593. if ((s[i] == 'd' || s[i] == 'D') &&
  594. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  595. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  596. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  597. return 1;
  598. if (i + 12 <= len &&
  599. (s[i] == 'c' || s[i] == 'C') &&
  600. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  601. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  602. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  603. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  604. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  605. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  606. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  607. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  608. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  609. s[i + 10] == '-' &&
  610. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  611. (s[i + 12] == 's' || s[i + 12] == 'S'))
  612. return 1;
  613. }
  614. return 0;
  615. }
  616. /* Collect --custom-property definitions from one declaration block. */
  617. static void css_collect_block(struct br_doc *d, const char *s,
  618. unsigned int len)
  619. {
  620. unsigned int i = 0;
  621. while (i < len) {
  622. unsigned int pstart, pend, vstart, vend;
  623. while (i < len && (is_space(s[i]) || s[i] == ';'))
  624. i++;
  625. if (i >= len)
  626. break;
  627. pstart = i;
  628. while (i < len && s[i] != ':' && s[i] != ';')
  629. i++;
  630. pend = i;
  631. if (i >= len || s[i] != ':') {
  632. if (i < len)
  633. i++;
  634. continue;
  635. }
  636. i++;
  637. vstart = i;
  638. i = css_value_end(s, len, i);
  639. vend = i;
  640. if (i < len)
  641. i++;
  642. while (pstart < pend && is_space(s[pstart]))
  643. pstart++;
  644. while (pend > pstart && is_space(s[pend - 1]))
  645. pend--;
  646. while (vstart < vend && is_space(s[vstart]))
  647. vstart++;
  648. while (vend > vstart && is_space(s[vend - 1]))
  649. vend--;
  650. if (vend - vstart >= 10 &&
  651. ci_eq_n(s + vend - 10, "!important", 10)) {
  652. vend -= 10;
  653. while (vend > vstart && is_space(s[vend - 1]))
  654. vend--;
  655. }
  656. if (pend - pstart >= 3 && s[pstart] == '-' &&
  657. s[pstart + 1] == '-')
  658. css_var_set(d, s + pstart, pend - pstart,
  659. s + vstart, vend - vstart);
  660. }
  661. }
  662. const char *br_href(struct br_doc *d, unsigned int node)
  663. {
  664. return attr_ptr(d, d->nodes[node].href_off);
  665. }
  666. const char *br_src(struct br_doc *d, unsigned int node)
  667. {
  668. return attr_ptr(d, d->nodes[node].src_off);
  669. }
  670. const char *br_alt(struct br_doc *d, unsigned int node)
  671. {
  672. return attr_ptr(d, d->nodes[node].alt_off);
  673. }
  674. const char *br_rel(struct br_doc *d, unsigned int node)
  675. {
  676. return attr_ptr(d, d->nodes[node].rel_off);
  677. }
  678. const char *br_value(struct br_doc *d, unsigned int node)
  679. {
  680. return attr_ptr(d, d->nodes[node].src_off);
  681. }
  682. const char *br_placeholder(struct br_doc *d, unsigned int node)
  683. {
  684. return attr_ptr(d, d->nodes[node].alt_off);
  685. }
  686. const char *br_type(struct br_doc *d, unsigned int node)
  687. {
  688. return attr_ptr(d, d->nodes[node].rel_off);
  689. }
  690. /* ---- document setup / node append ---- */
  691. static void rule_index_reset(struct br_doc *d)
  692. {
  693. unsigned int i;
  694. for (i = 0; i < BR_MAX_RULES; i++)
  695. d->rule_next[i] = 0xFFFF;
  696. for (i = 0; i < BR_RULE_HASH; i++)
  697. d->rule_bucket[i] = 0xFFFF;
  698. for (i = 0; i < BR_TAG_MAX; i++)
  699. d->rule_tag[i] = 0xFFFF;
  700. d->rule_any = 0xFFFF;
  701. }
  702. void br_doc_init(struct br_doc *d)
  703. {
  704. unsigned int i;
  705. for (i = 0; i < sizeof(*d); i++)
  706. ((unsigned char *)d)[i] = 0;
  707. rule_index_reset(d);
  708. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  709. d->text = malloc(BR_TEXT_SIZE);
  710. d->attrs = malloc(BR_ATTR_SIZE);
  711. if (!d->nodes || !d->text || !d->attrs) {
  712. free(d->nodes);
  713. free(d->text);
  714. free(d->attrs);
  715. d->nodes = 0;
  716. d->text = 0;
  717. d->attrs = 0;
  718. return;
  719. }
  720. d->rules = rule_fallback;
  721. d->rule_cap = BR_RULE_FALLBACK;
  722. d->attrs_used = 1; /* offset 0 = "no attribute" */
  723. d->attrs[0] = '\0';
  724. d->nvars = 0;
  725. d->root = 0;
  726. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  727. d->var_slot[vi] = 0;
  728. root_font_px = 16;
  729. bg_url_count = 0;
  730. font_reset();
  731. }
  732. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  733. unsigned int cap)
  734. {
  735. if (!buf || !cap) {
  736. d->rules = rule_fallback;
  737. d->rule_cap = BR_RULE_FALLBACK;
  738. d->nrules = 0;
  739. return;
  740. }
  741. if (cap > BR_MAX_RULES)
  742. cap = BR_MAX_RULES;
  743. d->rules = buf;
  744. d->rule_cap = cap;
  745. d->nrules = 0;
  746. }
  747. static void doc_reset(struct br_doc *d)
  748. {
  749. unsigned int i;
  750. d->nnode = 0;
  751. d->text_used = 0;
  752. d->attrs_used = 1;
  753. d->attrs[0] = '\0';
  754. d->nrules = 0;
  755. rule_index_reset(d);
  756. d->title[0] = '\0';
  757. d->inline_css_len = 0;
  758. d->nvars = 0;
  759. d->truncated = 0;
  760. d->plain = 0;
  761. d->script_src = 0;
  762. d->nscripts = 0;
  763. d->scripts_truncated = 0;
  764. font_reset();
  765. for (i = 0; i < BR_MAX_NODES; i++) {
  766. d->nodes[i].type = BR_NODE_ELEM;
  767. d->nodes[i].tag = BR_TAG_UNKNOWN;
  768. d->nodes[i].flags = 0;
  769. d->nodes[i].parent = 0;
  770. d->nodes[i].first = d->nodes[i].last = 0;
  771. d->nodes[i].next = 0;
  772. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  773. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  774. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  775. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  776. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  777. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  778. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  779. d->nodes[i].name_off = 0;
  780. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  781. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  782. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  783. d->nodes[i].svg_rx = 0;
  784. d->nodes[i].img_index = -1;
  785. d->nodes[i].bg_img_index = -1;
  786. d->nodes[i].style_valid = 0;
  787. }
  788. }
  789. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  790. unsigned char type, unsigned char tag)
  791. {
  792. unsigned int idx;
  793. struct br_node *n;
  794. if (d->nnode >= BR_MAX_NODES) {
  795. d->truncated = 1;
  796. return 0;
  797. }
  798. idx = d->nnode++;
  799. n = &d->nodes[idx];
  800. n->type = type;
  801. n->tag = tag;
  802. n->parent = (unsigned short)parent;
  803. n->img_index = -1;
  804. n->bg_img_index = -1;
  805. if (d->nodes[parent].last)
  806. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  807. else
  808. d->nodes[parent].first = (unsigned short)idx;
  809. d->nodes[parent].last = (unsigned short)idx;
  810. return idx;
  811. }
  812. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  813. const char *s, unsigned int len, int pre)
  814. {
  815. char *dst;
  816. unsigned int n;
  817. unsigned int idx;
  818. if (len == 0)
  819. return 0;
  820. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  821. d->truncated = 1;
  822. return 0;
  823. }
  824. dst = d->text + d->text_used;
  825. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  826. if (n == 0) {
  827. d->text[d->text_used] = '\0';
  828. return 0;
  829. }
  830. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  831. if (!idx)
  832. return 0;
  833. d->nodes[idx].text_off = (unsigned short)d->text_used;
  834. d->nodes[idx].text_len = (unsigned short)n;
  835. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  836. d->text_used += n + 1;
  837. return idx;
  838. }
  839. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  840. static int same_string(const char *a, const char *b)
  841. {
  842. while (*a && *a == *b) {
  843. a++;
  844. b++;
  845. }
  846. return *a == *b;
  847. }
  848. static int same_string_n(const char *a, const char *b, unsigned int n)
  849. {
  850. unsigned int i;
  851. for (i = 0; i < n; i++)
  852. if (a[i] != b[i])
  853. return 0;
  854. return 1;
  855. }
  856. static int node_detached(struct br_doc *d, unsigned int node)
  857. {
  858. unsigned int guard = 0;
  859. while (node && node < d->nnode && guard++ <= d->nnode) {
  860. if (d->nodes[node].flags & BR_NODE_DETACHED)
  861. return 1;
  862. node = d->nodes[node].parent;
  863. }
  864. return 0;
  865. }
  866. unsigned int br_find_id(struct br_doc *d, const char *id)
  867. {
  868. unsigned int i;
  869. if (!d || !id)
  870. return 0;
  871. for (i = 0; i < d->nnode; i++) {
  872. struct br_node *n = &d->nodes[i];
  873. if (n->type == BR_NODE_ELEM && n->id_off &&
  874. same_string(d->attrs + n->id_off, id) &&
  875. !node_detached(d, i))
  876. return i + 1;
  877. }
  878. return 0;
  879. }
  880. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  881. unsigned int cap, unsigned int *used)
  882. {
  883. unsigned int c;
  884. if (node >= d->nnode)
  885. return;
  886. if (d->nodes[node].type == BR_NODE_TEXT) {
  887. unsigned int len = d->nodes[node].text_len;
  888. unsigned int off = d->nodes[node].text_off;
  889. if (*used + len >= cap)
  890. len = *used + 1 < cap ? cap - *used - 1 : 0;
  891. for (c = 0; c < len; c++)
  892. out[*used + c] = d->text[off + c];
  893. *used += len;
  894. return;
  895. }
  896. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  897. text_walk(d, c, out, cap, used);
  898. }
  899. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  900. char *out, unsigned int cap)
  901. {
  902. unsigned int used = 0;
  903. if (!d || !out || cap == 0 || node >= d->nnode)
  904. return 0;
  905. text_walk(d, node, out, cap, &used);
  906. out[used] = '\0';
  907. return used;
  908. }
  909. int br_set_text_content(struct br_doc *d, unsigned int node,
  910. const char *text, unsigned int len)
  911. {
  912. struct br_node *n;
  913. unsigned int idx;
  914. unsigned int i;
  915. if (!d || node >= d->nnode)
  916. return -1;
  917. /* Detach the old subtree; its arena storage is reclaimed on the next
  918. * navigation. */
  919. {
  920. unsigned int c;
  921. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  922. d->nodes[c].flags |= BR_NODE_DETACHED;
  923. }
  924. d->nodes[node].first = d->nodes[node].last = 0;
  925. d->nodes[node].style_valid = 0;
  926. if (len == 0)
  927. return 0;
  928. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  929. d->truncated = 1;
  930. return -1;
  931. }
  932. idx = node_append(d, node, BR_NODE_TEXT, 0);
  933. if (!idx)
  934. return -1;
  935. for (i = 0; i < len; i++)
  936. d->text[d->text_used + i] = text[i];
  937. d->text[d->text_used + len] = '\0';
  938. n = &d->nodes[idx];
  939. n->text_off = (unsigned short)d->text_used;
  940. n->text_len = (unsigned short)len;
  941. n->flags = 0;
  942. d->text_used += len + 1;
  943. return 0;
  944. }
  945. /* Replace the value of an existing text or comment node in place, keeping the
  946. * node handle (and therefore the JS wrapper) valid. */
  947. int br_set_text_node(struct br_doc *d, unsigned int node,
  948. const char *text, unsigned int len)
  949. {
  950. struct br_node *n;
  951. unsigned int i;
  952. if (!d || node >= d->nnode)
  953. return -1;
  954. n = &d->nodes[node];
  955. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  956. return -1;
  957. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  958. d->truncated = 1;
  959. return -1;
  960. }
  961. for (i = 0; i < len; i++)
  962. d->text[d->text_used + i] = text[i];
  963. d->text[d->text_used + len] = '\0';
  964. n->text_off = (unsigned short)d->text_used;
  965. n->text_len = (unsigned short)len;
  966. d->text_used += len + 1;
  967. return 0;
  968. }
  969. /* ---- runtime DOM construction and mutation ---- */
  970. static unsigned int text_len(const char *s)
  971. {
  972. unsigned int n = 0;
  973. while (s && s[n])
  974. n++;
  975. return n;
  976. }
  977. static void node_zero(struct br_node *n)
  978. {
  979. unsigned int i;
  980. for (i = 0; i < sizeof(*n); i++)
  981. ((unsigned char *)n)[i] = 0;
  982. n->img_index = -1;
  983. n->bg_img_index = -1;
  984. }
  985. static void node_unlink(struct br_doc *d, unsigned int handle)
  986. {
  987. unsigned int node, parent, c, prev = 0;
  988. if (!d || handle == 0 || handle > d->nnode)
  989. return;
  990. node = handle - 1;
  991. parent = d->nodes[node].parent;
  992. if (parent >= d->nnode) {
  993. d->nodes[node].parent = 0;
  994. d->nodes[node].next = 0;
  995. return;
  996. }
  997. c = d->nodes[parent].first;
  998. while (c && c != node) {
  999. prev = c;
  1000. c = d->nodes[c].next;
  1001. }
  1002. if (c == node) {
  1003. if (prev)
  1004. d->nodes[prev].next = d->nodes[node].next;
  1005. else
  1006. d->nodes[parent].first = d->nodes[node].next;
  1007. if (d->nodes[parent].last == node)
  1008. d->nodes[parent].last = (unsigned short)prev;
  1009. }
  1010. d->nodes[node].parent = 0;
  1011. d->nodes[node].next = 0;
  1012. d->nodes[node].flags |= BR_NODE_DETACHED;
  1013. }
  1014. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1015. const char *s, unsigned int len);
  1016. static unsigned int find_tag_end(const char *s, unsigned int i,
  1017. unsigned int len)
  1018. {
  1019. char quote = 0;
  1020. while (i < len) {
  1021. char c = s[i];
  1022. if (quote) {
  1023. if (c == quote)
  1024. quote = 0;
  1025. } else if (c == '"' || c == '\'') {
  1026. quote = c;
  1027. } else if (c == '>') {
  1028. return i;
  1029. }
  1030. i++;
  1031. }
  1032. return len;
  1033. }
  1034. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1035. const char *html, unsigned int len)
  1036. {
  1037. unsigned int stack[64];
  1038. unsigned int sp = 1, i = 0;
  1039. if (!d || node >= d->nnode)
  1040. return -1;
  1041. stack[0] = node;
  1042. {
  1043. unsigned int c;
  1044. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1045. d->nodes[c].flags |= BR_NODE_DETACHED;
  1046. }
  1047. d->nodes[node].first = d->nodes[node].last = 0;
  1048. d->nodes[node].style_valid = 0;
  1049. while (i < len) {
  1050. if (html[i] != '<') {
  1051. unsigned int start = i, end;
  1052. while (i < len && html[i] != '<')
  1053. i++;
  1054. end = i;
  1055. if (end > start) {
  1056. unsigned int h = br_create_text(d, html + start,
  1057. end - start);
  1058. if (h)
  1059. br_append_child(d, stack[sp - 1], h);
  1060. }
  1061. continue;
  1062. }
  1063. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1064. html[i + 3] == '-') {
  1065. int p = find_ci(html, len, "-->", 3, i + 4);
  1066. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1067. unsigned int h = br_create_comment(d, html + i + 4,
  1068. end > i + 4 ?
  1069. end - i - 4 : 0);
  1070. if (h)
  1071. br_append_child(d, stack[sp - 1], h);
  1072. i = p >= 0 ? (unsigned int)p + 3 : len;
  1073. continue;
  1074. }
  1075. if (html[i + 1] == '/') {
  1076. unsigned int p = find_tag_end(html, i + 2, len);
  1077. i = p < len ? p + 1 : len;
  1078. if (sp > 1)
  1079. sp--;
  1080. continue;
  1081. }
  1082. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1083. unsigned int p = find_tag_end(html, i + 2, len);
  1084. i = p < len ? p + 1 : len;
  1085. continue;
  1086. }
  1087. {
  1088. unsigned int p = find_tag_end(html, i + 1, len);
  1089. unsigned int ns = i + 1, ne, as, ae;
  1090. unsigned int h, k;
  1091. char name[32];
  1092. int selfclose = 0;
  1093. while (ns < p && is_space(html[ns]))
  1094. ns++;
  1095. ne = ns;
  1096. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1097. ne++;
  1098. as = ne;
  1099. ae = p;
  1100. if (ae > as && html[ae - 1] == '/') {
  1101. selfclose = 1;
  1102. ae--;
  1103. }
  1104. if (ne == ns) {
  1105. i = p < len ? p + 1 : len;
  1106. continue;
  1107. }
  1108. if (ne - ns >= sizeof(name)) {
  1109. i = p < len ? p + 1 : len;
  1110. continue;
  1111. }
  1112. for (k = 0; k < ne - ns; k++)
  1113. name[k] = html[ns + k];
  1114. name[ne - ns] = '\0';
  1115. h = br_create_element(d, name, ne - ns);
  1116. if (!h) {
  1117. i = p < len ? p + 1 : len;
  1118. continue;
  1119. }
  1120. if (ae > as)
  1121. parse_attrs(d, &d->nodes[h - 1], html + as,
  1122. ae - as);
  1123. br_append_child(d, stack[sp - 1], h);
  1124. {
  1125. unsigned int tag = d->nodes[h - 1].tag;
  1126. if (!selfclose && !br_tag_is_void(tag) &&
  1127. sp < sizeof(stack) / sizeof(stack[0]))
  1128. stack[sp++] = h - 1;
  1129. }
  1130. i = p < len ? p + 1 : len;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1136. {
  1137. unsigned int i;
  1138. if (!d)
  1139. return 0;
  1140. for (i = 0; i < d->nnode; i++)
  1141. if (d->nodes[i].type == BR_NODE_ELEM &&
  1142. d->nodes[i].tag == (unsigned char)tag &&
  1143. !node_detached(d, i))
  1144. return i + 1;
  1145. return 0;
  1146. }
  1147. unsigned int br_create_element(struct br_doc *d, const char *name,
  1148. unsigned int len)
  1149. {
  1150. struct br_node *n;
  1151. unsigned int idx;
  1152. if (!d || !name)
  1153. return 0;
  1154. if (d->nnode >= BR_MAX_NODES) {
  1155. d->truncated = 1;
  1156. return 0;
  1157. }
  1158. idx = d->nnode++;
  1159. n = &d->nodes[idx];
  1160. node_zero(n);
  1161. n->type = BR_NODE_ELEM;
  1162. n->flags = BR_NODE_DETACHED;
  1163. n->tag = (unsigned char)br_tag_from_name(name);
  1164. if (n->tag == BR_TAG_UNKNOWN && len)
  1165. n->name_off = store_attr(d, name, len, 31);
  1166. return idx + 1;
  1167. }
  1168. unsigned int br_create_text(struct br_doc *d, const char *text,
  1169. unsigned int len)
  1170. {
  1171. struct br_node *n;
  1172. unsigned int idx, i;
  1173. if (!d)
  1174. return 0;
  1175. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1176. d->truncated = 1;
  1177. return 0;
  1178. }
  1179. idx = d->nnode++;
  1180. n = &d->nodes[idx];
  1181. node_zero(n);
  1182. n->type = BR_NODE_TEXT;
  1183. n->flags = BR_NODE_DETACHED;
  1184. n->text_off = (unsigned short)d->text_used;
  1185. n->text_len = (unsigned short)len;
  1186. for (i = 0; i < len; i++)
  1187. d->text[d->text_used + i] = text[i];
  1188. d->text[d->text_used + len] = '\0';
  1189. d->text_used += len + 1;
  1190. return idx + 1;
  1191. }
  1192. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1193. unsigned int len)
  1194. {
  1195. unsigned int h = br_create_text(d, text, len);
  1196. if (h)
  1197. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1198. return h;
  1199. }
  1200. unsigned int br_create_fragment(struct br_doc *d)
  1201. {
  1202. struct br_node *n;
  1203. unsigned int idx;
  1204. if (!d)
  1205. return 0;
  1206. if (d->nnode >= BR_MAX_NODES) {
  1207. d->truncated = 1;
  1208. return 0;
  1209. }
  1210. idx = d->nnode++;
  1211. n = &d->nodes[idx];
  1212. node_zero(n);
  1213. n->type = BR_NODE_FRAGMENT;
  1214. n->flags = BR_NODE_DETACHED;
  1215. return idx + 1;
  1216. }
  1217. int br_append_child(struct br_doc *d, unsigned int parent,
  1218. unsigned int child)
  1219. {
  1220. unsigned int p;
  1221. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1222. parent == child - 1)
  1223. return -1;
  1224. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1225. /* Appending a fragment inserts its children, not the fragment. */
  1226. unsigned int c = d->nodes[child - 1].first;
  1227. while (c) {
  1228. unsigned int next = d->nodes[c].next;
  1229. br_append_child(d, parent, c + 1);
  1230. c = next;
  1231. }
  1232. return 0;
  1233. }
  1234. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1235. if (p == child - 1)
  1236. return -1;
  1237. node_unlink(d, child);
  1238. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1239. d->nodes[child - 1].parent = (unsigned short)parent;
  1240. d->nodes[child - 1].next = 0;
  1241. if (d->nodes[parent].last)
  1242. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1243. else
  1244. d->nodes[parent].first = (unsigned short)(child - 1);
  1245. d->nodes[parent].last = (unsigned short)(child - 1);
  1246. return 0;
  1247. }
  1248. int br_insert_before(struct br_doc *d, unsigned int parent,
  1249. unsigned int child, unsigned int before)
  1250. {
  1251. unsigned int prev = 0, c;
  1252. if (before == 0)
  1253. return br_append_child(d, parent, child);
  1254. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1255. before > d->nnode || parent == child - 1)
  1256. return -1;
  1257. if (d->nodes[before - 1].parent != parent)
  1258. return -1;
  1259. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1260. unsigned int c = d->nodes[child - 1].first;
  1261. while (c) {
  1262. unsigned int next = d->nodes[c].next;
  1263. br_insert_before(d, parent, c + 1, before);
  1264. c = next;
  1265. }
  1266. return 0;
  1267. }
  1268. {
  1269. unsigned int p;
  1270. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1271. if (p == child - 1)
  1272. return -1;
  1273. }
  1274. node_unlink(d, child);
  1275. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1276. c = d->nodes[parent].first;
  1277. while (c && c != before - 1) {
  1278. prev = c;
  1279. c = d->nodes[c].next;
  1280. }
  1281. if (c != before - 1)
  1282. return -1;
  1283. d->nodes[child - 1].parent = (unsigned short)parent;
  1284. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1285. if (prev)
  1286. d->nodes[prev].next = (unsigned short)(child - 1);
  1287. else
  1288. d->nodes[parent].first = (unsigned short)(child - 1);
  1289. return 0;
  1290. }
  1291. int br_remove_child(struct br_doc *d, unsigned int parent,
  1292. unsigned int child)
  1293. {
  1294. if (!d || child == 0 || child > d->nnode ||
  1295. d->nodes[child - 1].parent != parent)
  1296. return -1;
  1297. node_unlink(d, child);
  1298. return 0;
  1299. }
  1300. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1301. unsigned int child)
  1302. {
  1303. unsigned int c;
  1304. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1305. return 0;
  1306. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1307. if (c == child - 1)
  1308. return child;
  1309. return 0;
  1310. }
  1311. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1312. const char *name)
  1313. {
  1314. struct br_node *n;
  1315. const char *p, *end, *found = 0;
  1316. if (!d || node >= d->nnode || !name)
  1317. return 0;
  1318. n = &d->nodes[node];
  1319. p = d->attrs + n->extra_off;
  1320. end = p + n->extra_len;
  1321. while (p + 1 < end) {
  1322. const char *an = p;
  1323. const char *av;
  1324. p += text_len(p) + 1;
  1325. if (p >= end)
  1326. break;
  1327. av = p;
  1328. p += text_len(p) + 1;
  1329. if (same_string(an, name))
  1330. found = av;
  1331. }
  1332. if (found)
  1333. return found;
  1334. if (same_string(name, "id") && n->id_off)
  1335. return d->attrs + n->id_off;
  1336. if (same_string(name, "class") && n->class_off)
  1337. return d->attrs + n->class_off;
  1338. if (same_string(name, "style") && n->style_off)
  1339. return d->attrs + n->style_off;
  1340. if (same_string(name, "href") && n->href_off)
  1341. return d->attrs + n->href_off;
  1342. if (same_string(name, "src") && n->src_off)
  1343. return d->attrs + n->src_off;
  1344. if (same_string(name, "alt") && n->alt_off)
  1345. return d->attrs + n->alt_off;
  1346. if (same_string(name, "rel") && n->rel_off)
  1347. return d->attrs + n->rel_off;
  1348. return 0;
  1349. }
  1350. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1351. const char *name, const char *value,
  1352. unsigned int len)
  1353. {
  1354. if (same_string(name, "id"))
  1355. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1356. else if (same_string(name, "class"))
  1357. n->class_off = len ? store_attr(d, value, len, 120) : 0;
  1358. else if (same_string(name, "style"))
  1359. n->style_off = len ? store_attr(d, value, len, 240) : 0;
  1360. else if (same_string(name, "href"))
  1361. n->href_off = len ? store_attr(d, value, len, 480) : 0;
  1362. else if (same_string(name, "src"))
  1363. n->src_off = len ? store_attr(d, value, len, 400) : 0;
  1364. else if (same_string(name, "alt"))
  1365. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1366. else if (same_string(name, "rel"))
  1367. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1368. }
  1369. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1370. const char *value, unsigned int len)
  1371. {
  1372. struct br_node *n;
  1373. unsigned int start, nl, need;
  1374. const char *p, *old, *oldend;
  1375. if (!d || node >= d->nnode || !name)
  1376. return -1;
  1377. n = &d->nodes[node];
  1378. nl = text_len(name);
  1379. need = nl + 1 + len + 1;
  1380. start = d->attrs_used;
  1381. old = d->attrs + n->extra_off;
  1382. oldend = old + n->extra_len;
  1383. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1384. d->truncated = 1;
  1385. return -1;
  1386. }
  1387. p = old;
  1388. while (p + 1 < oldend) {
  1389. const char *an = p;
  1390. const char *av;
  1391. unsigned int anl, avl, k;
  1392. anl = text_len(an);
  1393. p += anl + 1;
  1394. if (p >= oldend)
  1395. break;
  1396. av = p;
  1397. avl = text_len(av);
  1398. p += avl + 1;
  1399. if (same_string(an, name))
  1400. continue;
  1401. for (k = 0; k < anl; k++)
  1402. d->attrs[d->attrs_used++] = an[k];
  1403. d->attrs[d->attrs_used++] = '\0';
  1404. for (k = 0; k < avl; k++)
  1405. d->attrs[d->attrs_used++] = av[k];
  1406. d->attrs[d->attrs_used++] = '\0';
  1407. }
  1408. {
  1409. unsigned int k;
  1410. for (k = 0; k < nl; k++)
  1411. d->attrs[d->attrs_used++] = name[k];
  1412. d->attrs[d->attrs_used++] = '\0';
  1413. for (k = 0; k < len; k++)
  1414. d->attrs[d->attrs_used++] = value[k];
  1415. d->attrs[d->attrs_used++] = '\0';
  1416. }
  1417. n = &d->nodes[node];
  1418. n->extra_off = start;
  1419. n->extra_len = (unsigned short)(d->attrs_used - start);
  1420. n->style_valid = 0;
  1421. attr_note_known(d, n, name, value, len);
  1422. return 0;
  1423. }
  1424. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1425. {
  1426. struct br_node *n;
  1427. unsigned int start;
  1428. const char *p, *oldend;
  1429. int found = 0;
  1430. if (!d || node >= d->nnode || !name)
  1431. return -1;
  1432. n = &d->nodes[node];
  1433. start = d->attrs_used;
  1434. p = d->attrs + n->extra_off;
  1435. oldend = p + n->extra_len;
  1436. if (start + n->extra_len > BR_ATTR_SIZE)
  1437. return -1;
  1438. while (p + 1 < oldend) {
  1439. const char *an = p;
  1440. const char *av;
  1441. unsigned int anl, avl, k;
  1442. anl = text_len(an);
  1443. p += anl + 1;
  1444. if (p >= oldend)
  1445. break;
  1446. av = p;
  1447. avl = text_len(av);
  1448. p += avl + 1;
  1449. if (same_string(an, name)) {
  1450. found = 1;
  1451. continue;
  1452. }
  1453. for (k = 0; k < anl; k++)
  1454. d->attrs[d->attrs_used++] = an[k];
  1455. d->attrs[d->attrs_used++] = '\0';
  1456. for (k = 0; k < avl; k++)
  1457. d->attrs[d->attrs_used++] = av[k];
  1458. d->attrs[d->attrs_used++] = '\0';
  1459. }
  1460. n = &d->nodes[node];
  1461. n->extra_off = start;
  1462. n->extra_len = (unsigned short)(d->attrs_used - start);
  1463. n->style_valid = 0;
  1464. attr_note_known(d, n, name, "", 0);
  1465. return found ? 0 : -1;
  1466. }
  1467. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1468. {
  1469. struct br_node *src;
  1470. unsigned int h;
  1471. const char *name;
  1472. if (!d || node == 0 || node > d->nnode)
  1473. return 0;
  1474. src = &d->nodes[node - 1];
  1475. if (src->type == BR_NODE_TEXT)
  1476. return br_create_text(d, d->text + src->text_off, src->text_len);
  1477. if (src->type == BR_NODE_COMMENT)
  1478. return br_create_comment(d, d->text + src->text_off,
  1479. src->text_len);
  1480. if (src->type == BR_NODE_FRAGMENT)
  1481. h = br_create_fragment(d);
  1482. else {
  1483. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1484. name = d->attrs + src->name_off;
  1485. else
  1486. name = br_tag_name(src->tag);
  1487. h = br_create_element(d, name, text_len(name));
  1488. }
  1489. if (!h)
  1490. return 0;
  1491. {
  1492. const char *p = d->attrs + src->extra_off;
  1493. const char *end = p + src->extra_len;
  1494. while (p + 1 < end) {
  1495. const char *an = p;
  1496. const char *av;
  1497. unsigned int anl, avl;
  1498. anl = text_len(an);
  1499. p += anl + 1;
  1500. if (p >= end)
  1501. break;
  1502. av = p;
  1503. avl = text_len(av);
  1504. p += avl + 1;
  1505. br_set_attr(d, h, an, av, avl);
  1506. }
  1507. }
  1508. if (deep) {
  1509. unsigned int c;
  1510. for (c = src->first; c; c = d->nodes[c].next) {
  1511. unsigned int ch = br_clone_node(d, c + 1, 1);
  1512. if (ch)
  1513. br_append_child(d, h - 1, ch);
  1514. }
  1515. }
  1516. return h;
  1517. }
  1518. static int attr_value_match(const char *have, const char *want,
  1519. unsigned int want_len, int op)
  1520. {
  1521. unsigned int n = text_len(have);
  1522. unsigned int i;
  1523. if (!op)
  1524. return have != 0;
  1525. if (!have)
  1526. return 0;
  1527. if (op == '=')
  1528. return n == want_len && same_string_n(have, want, want_len);
  1529. if (op == '^')
  1530. return n >= want_len && same_string_n(have, want, want_len);
  1531. if (op == '$') {
  1532. if (n < want_len)
  1533. return 0;
  1534. have += n - want_len;
  1535. for (i = 0; i < want_len; i++)
  1536. if (have[i] != want[i])
  1537. return 0;
  1538. return 1;
  1539. }
  1540. if (op == '*') {
  1541. for (i = 0; i + want_len <= n; i++)
  1542. if (same_string_n(have + i, want, want_len))
  1543. return 1;
  1544. return 0;
  1545. }
  1546. return 0;
  1547. }
  1548. static int class_contains(const char *classes, const char *want,
  1549. unsigned int want_len)
  1550. {
  1551. unsigned int i = 0;
  1552. if (!classes)
  1553. return 0;
  1554. while (classes[i]) {
  1555. unsigned int start;
  1556. while (classes[i] == ' ' || classes[i] == '\t' ||
  1557. classes[i] == '\r' || classes[i] == '\n')
  1558. i++;
  1559. start = i;
  1560. while (classes[i] && classes[i] != ' ' && classes[i] != '\t' &&
  1561. classes[i] != '\r' && classes[i] != '\n')
  1562. i++;
  1563. if (i - start == want_len &&
  1564. same_string_n(classes + start, want, want_len))
  1565. return 1;
  1566. }
  1567. return 0;
  1568. }
  1569. static int match_compound(struct br_doc *d, unsigned int node,
  1570. const char *sel, unsigned int len)
  1571. {
  1572. struct br_node *n = &d->nodes[node];
  1573. unsigned int i = 0;
  1574. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1575. sel[i] != '[' && sel[i] != ':')
  1576. i++;
  1577. if (i > 0 && !(i == 1 && sel[0] == '*')) {
  1578. char tag[32];
  1579. unsigned int tl = i < sizeof(tag) - 1 ? i : sizeof(tag) - 1;
  1580. unsigned int k;
  1581. for (k = 0; k < tl; k++)
  1582. tag[k] = sel[k];
  1583. tag[tl] = '\0';
  1584. if (n->tag == BR_TAG_UNKNOWN) {
  1585. if (!n->name_off ||
  1586. !same_string(d->attrs + n->name_off, tag))
  1587. return 0;
  1588. } else if (n->tag != br_tag_from_name(tag)) {
  1589. return 0;
  1590. }
  1591. }
  1592. while (i < len) {
  1593. if (sel[i] == '.') {
  1594. unsigned int start;
  1595. i++;
  1596. start = i;
  1597. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1598. sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
  1599. i++;
  1600. if (!class_contains(br_find_attr(d, node, "class"),
  1601. sel + start, i - start))
  1602. return 0;
  1603. } else if (sel[i] == '#') {
  1604. unsigned int start;
  1605. i++;
  1606. start = i;
  1607. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1608. sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
  1609. i++;
  1610. {
  1611. const char *id = br_find_attr(d, node, "id");
  1612. if (!id || !same_string_n(id, sel + start,
  1613. i - start) ||
  1614. text_len(id) != i - start)
  1615. return 0;
  1616. }
  1617. } else if (sel[i] == '[') {
  1618. unsigned int start, nl, op = 0, vs, ve;
  1619. char quote = 0;
  1620. i++;
  1621. start = i;
  1622. while (i < len && sel[i] != '=' && sel[i] != ']' &&
  1623. sel[i] != '^' && sel[i] != '$' && sel[i] != '*')
  1624. i++;
  1625. nl = i - start;
  1626. if (i < len && sel[i] != ']') {
  1627. op = sel[i++];
  1628. if (i < len && sel[i] == '=')
  1629. i++;
  1630. }
  1631. while (i < len && sel[i] == ' ')
  1632. i++;
  1633. if (i < len && (sel[i] == '"' || sel[i] == '\'')) {
  1634. quote = sel[i++];
  1635. vs = i;
  1636. while (i < len && sel[i] != quote)
  1637. i++;
  1638. ve = i;
  1639. if (i < len)
  1640. i++;
  1641. } else {
  1642. vs = i;
  1643. while (i < len && sel[i] != ']')
  1644. i++;
  1645. ve = i;
  1646. }
  1647. if (i < len && sel[i] == ']')
  1648. i++;
  1649. {
  1650. char name[32];
  1651. unsigned int k;
  1652. if (nl >= sizeof(name))
  1653. return 0;
  1654. for (k = 0; k < nl; k++)
  1655. name[k] = sel[start + k];
  1656. name[nl] = '\0';
  1657. if (!attr_value_match(br_find_attr(d, node, name),
  1658. sel + vs, ve - vs, op))
  1659. return 0;
  1660. }
  1661. } else if (sel[i] == ':') {
  1662. unsigned int start;
  1663. i++;
  1664. start = i;
  1665. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1666. sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
  1667. i++;
  1668. /* Every link is treated as unvisited: there is no
  1669. * history state, so a:link matches and a:visited does
  1670. * not. Other pseudo-classes (hover/focus/active) are
  1671. * deliberately unmatched instead of applying their
  1672. * styles permanently. */
  1673. if (i - start != 4 ||
  1674. !ci_eq_n(sel + start, "link", 4) ||
  1675. n->tag != BR_TAG_A || !n->href_off)
  1676. return 0;
  1677. } else {
  1678. return 0;
  1679. }
  1680. }
  1681. return 1;
  1682. }
  1683. static int match_selector(struct br_doc *d, unsigned int node,
  1684. const char *sel, unsigned int len)
  1685. {
  1686. unsigned int i, split = 0, depth = 0;
  1687. char quote = 0;
  1688. while (len > 0 && sel[0] == ' ') {
  1689. sel++;
  1690. len--;
  1691. }
  1692. while (len > 0 && sel[len - 1] == ' ')
  1693. len--;
  1694. if (len == 0)
  1695. return 0;
  1696. for (i = 0; i < len; i++) {
  1697. char c = sel[i];
  1698. if (quote) {
  1699. if (c == quote)
  1700. quote = 0;
  1701. } else if (c == '"' || c == '\'') {
  1702. quote = c;
  1703. } else if (c == '[') {
  1704. depth++;
  1705. } else if (c == ']' && depth) {
  1706. depth--;
  1707. } else if (!depth && c == ' ') {
  1708. split = i;
  1709. }
  1710. }
  1711. if (split) {
  1712. unsigned int p = d->nodes[node].parent;
  1713. if (!match_compound(d, node, sel + split + 1,
  1714. len - split - 1))
  1715. return 0;
  1716. while (p) {
  1717. unsigned int next = d->nodes[p].parent;
  1718. if (match_selector(d, p, sel, split))
  1719. return 1;
  1720. p = next;
  1721. }
  1722. return 0;
  1723. }
  1724. return match_compound(d, node, sel, len);
  1725. }
  1726. static int query_add(unsigned int *out, unsigned int max,
  1727. unsigned int *count, unsigned int node)
  1728. {
  1729. unsigned int i;
  1730. for (i = 0; i < *count; i++)
  1731. if (out[i] == node + 1)
  1732. return 0;
  1733. if (*count < max)
  1734. out[(*count)++] = node + 1;
  1735. return 0;
  1736. }
  1737. static void query_walk(struct br_doc *d, unsigned int node,
  1738. const char *sel, unsigned int len, unsigned int *out,
  1739. unsigned int max, unsigned int *count)
  1740. {
  1741. unsigned int c;
  1742. if (d->nodes[node].type == BR_NODE_ELEM &&
  1743. match_selector(d, node, sel, len))
  1744. query_add(out, max, count, node);
  1745. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1746. query_walk(d, c, sel, len, out, max, count);
  1747. }
  1748. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1749. {
  1750. unsigned int len;
  1751. if (!d || !selector || node == 0 || node > d->nnode)
  1752. return 0;
  1753. len = text_len(selector);
  1754. while (len > 0 && selector[0] == ' ') {
  1755. selector++;
  1756. len--;
  1757. }
  1758. while (len > 0 && selector[len - 1] == ' ')
  1759. len--;
  1760. return match_selector(d, node - 1, selector, len);
  1761. }
  1762. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1763. const char *selector, unsigned int *out,
  1764. unsigned int max)
  1765. {
  1766. unsigned int count = 0, i = 0;
  1767. if (!d || !selector || !out || max == 0)
  1768. return 0;
  1769. while (selector[i]) {
  1770. unsigned int start = i, depth = 0;
  1771. char quote = 0;
  1772. while (selector[i]) {
  1773. char c = selector[i];
  1774. if (quote) {
  1775. if (c == quote)
  1776. quote = 0;
  1777. } else if (c == '"' || c == '\'') {
  1778. quote = c;
  1779. } else if (c == '[') {
  1780. depth++;
  1781. } else if (c == ']' && depth) {
  1782. depth--;
  1783. } else if (!depth && c == ',') {
  1784. break;
  1785. }
  1786. i++;
  1787. }
  1788. if (i > start) {
  1789. if (root == 0) {
  1790. unsigned int n;
  1791. for (n = 1; n < d->nnode; n++)
  1792. if (d->nodes[n].type == BR_NODE_ELEM &&
  1793. !node_detached(d, n) &&
  1794. match_selector(d, n, selector + start,
  1795. i - start))
  1796. query_add(out, max, &count, n);
  1797. } else if (root <= d->nnode) {
  1798. unsigned int c;
  1799. for (c = d->nodes[root - 1].first; c;
  1800. c = d->nodes[c].next)
  1801. query_walk(d, c, selector + start,
  1802. i - start, out, max, &count);
  1803. }
  1804. }
  1805. if (selector[i] == ',')
  1806. i++;
  1807. }
  1808. return count;
  1809. }
  1810. /* ---- attribute parsing ---- */
  1811. static int is_space(char c)
  1812. {
  1813. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1814. }
  1815. static int str_eq(const char *a, const char *b);
  1816. static unsigned int parse_uint(const char *s, unsigned int len)
  1817. {
  1818. unsigned int i = 0, v = 0;
  1819. while (i < len && is_space(s[i]))
  1820. i++;
  1821. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1822. v = v * 10 + (unsigned int)(s[i] - '0');
  1823. if (v > 4096)
  1824. v = 4096;
  1825. i++;
  1826. }
  1827. return v;
  1828. }
  1829. static int contains_ci_len(const char *s, unsigned int len,
  1830. const char *needle);
  1831. static int parse_svg_viewbox(const char *s, unsigned int len,
  1832. unsigned short *w, unsigned short *h)
  1833. {
  1834. unsigned int i = 0, v[4], n = 0;
  1835. while (i < len && n < 4) {
  1836. unsigned int x = 0;
  1837. int any = 0;
  1838. while (i < len && (is_space(s[i]) || s[i] == ','))
  1839. i++;
  1840. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1841. x = x * 10 + (unsigned int)(s[i] - '0');
  1842. i++;
  1843. any = 1;
  1844. }
  1845. /* viewBox values are commonly fractional ("0 0 340.16
  1846. * 340.16"). Consume the fraction so it is not mistaken for
  1847. * the next number; the integer part is enough for the aspect
  1848. * ratio used by the renderer. */
  1849. if (i < len && s[i] == '.') {
  1850. i++;
  1851. while (i < len && s[i] >= '0' && s[i] <= '9')
  1852. i++;
  1853. }
  1854. if (!any) {
  1855. if (i < len)
  1856. i++;
  1857. continue;
  1858. }
  1859. v[n++] = x;
  1860. }
  1861. if (n < 4)
  1862. return 0;
  1863. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1864. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1865. return *w && *h;
  1866. }
  1867. static void note_attr_flag(struct br_node *n, const char *name)
  1868. {
  1869. if (str_eq(name, "data-collapse-target"))
  1870. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1871. else if (str_eq(name, "data-collapse-trigger"))
  1872. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1873. else if (str_eq(name, "is-open"))
  1874. n->attr_flags |= BR_ATTR_IS_OPEN;
  1875. else if (str_eq(name, "disabled"))
  1876. n->attr_flags |= BR_ATTR_DISABLED;
  1877. else if (str_eq(name, "open"))
  1878. n->attr_flags |= BR_ATTR_OPEN;
  1879. else if (str_eq(name, "inert"))
  1880. n->attr_flags |= BR_ATTR_INERT;
  1881. }
  1882. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1883. * second pass so the block stays contiguous even though the known attributes
  1884. * above also use the shared attr arena. */
  1885. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1886. const char *s, unsigned int len)
  1887. {
  1888. unsigned int i = 0;
  1889. unsigned int start = d->attrs_used;
  1890. while (i < len) {
  1891. char name[32];
  1892. unsigned int nl = 0, vstart, vlen;
  1893. char quote = 0;
  1894. while (i < len && is_space(s[i]))
  1895. i++;
  1896. if (i >= len || s[i] == '/')
  1897. break;
  1898. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1899. char c = s[i++];
  1900. if (c >= 'A' && c <= 'Z')
  1901. c = (char)(c - 'A' + 'a');
  1902. if (nl + 1 < sizeof(name))
  1903. name[nl++] = c;
  1904. }
  1905. name[nl] = '\0';
  1906. while (i < len && is_space(s[i]))
  1907. i++;
  1908. if (i < len && s[i] == '=') {
  1909. i++;
  1910. while (i < len && is_space(s[i]))
  1911. i++;
  1912. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1913. quote = s[i++];
  1914. vstart = i;
  1915. while (i < len && s[i] != quote)
  1916. i++;
  1917. vlen = i - vstart;
  1918. if (i < len)
  1919. i++;
  1920. } else {
  1921. vstart = i;
  1922. while (i < len && !is_space(s[i]) && s[i] != '>')
  1923. i++;
  1924. vlen = i - vstart;
  1925. }
  1926. } else {
  1927. /* Boolean attribute: expose it with an empty value. */
  1928. vstart = 0;
  1929. vlen = 0;
  1930. }
  1931. if (!nl)
  1932. continue;
  1933. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1934. d->truncated = 1;
  1935. break;
  1936. }
  1937. {
  1938. unsigned int k;
  1939. for (k = 0; k < nl; k++)
  1940. d->attrs[d->attrs_used++] = name[k];
  1941. d->attrs[d->attrs_used++] = '\0';
  1942. for (k = 0; k < vlen; k++)
  1943. d->attrs[d->attrs_used++] = s[vstart + k];
  1944. d->attrs[d->attrs_used++] = '\0';
  1945. }
  1946. }
  1947. if (d->attrs_used > start) {
  1948. n->extra_off = start;
  1949. n->extra_len = (unsigned short)(d->attrs_used - start);
  1950. }
  1951. }
  1952. /* Parse attributes from `s` (between the tag name and '>'). */
  1953. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1954. const char *s, unsigned int len)
  1955. {
  1956. unsigned int i = 0;
  1957. while (i < len) {
  1958. char name[32];
  1959. unsigned int nl = 0, vstart, vlen;
  1960. char quote = 0;
  1961. while (i < len && is_space(s[i]))
  1962. i++;
  1963. if (i >= len || s[i] == '/')
  1964. break;
  1965. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1966. char c = s[i++];
  1967. if (c >= 'A' && c <= 'Z')
  1968. c = (char)(c - 'A' + 'a');
  1969. if (nl + 1 < sizeof(name))
  1970. name[nl++] = c;
  1971. }
  1972. name[nl] = '\0';
  1973. while (i < len && is_space(s[i]))
  1974. i++;
  1975. if (i >= len || s[i] != '=') {
  1976. if (str_eq(name, "hidden"))
  1977. n->flags |= BR_NODE_HIDDEN;
  1978. note_attr_flag(n, name);
  1979. continue;
  1980. }
  1981. i++;
  1982. while (i < len && is_space(s[i]))
  1983. i++;
  1984. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1985. quote = s[i++];
  1986. vstart = i;
  1987. while (i < len && s[i] != quote)
  1988. i++;
  1989. vlen = i - vstart;
  1990. if (i < len)
  1991. i++;
  1992. } else {
  1993. vstart = i;
  1994. while (i < len && !is_space(s[i]) && s[i] != '>')
  1995. i++;
  1996. vlen = i - vstart;
  1997. }
  1998. if (!nl)
  1999. continue;
  2000. if (str_eq(name, "hidden")) {
  2001. n->flags |= BR_NODE_HIDDEN;
  2002. continue;
  2003. }
  2004. note_attr_flag(n, name);
  2005. if (str_eq(name, "href"))
  2006. n->href_off = store_attr(d, s + vstart, vlen, 480);
  2007. else if (str_eq(name, "class"))
  2008. n->class_off = store_attr(d, s + vstart, vlen, 120);
  2009. else if (str_eq(name, "id"))
  2010. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2011. else if (str_eq(name, "style"))
  2012. n->style_off = store_attr(d, s + vstart, vlen, 240);
  2013. else if (str_eq(name, "src"))
  2014. n->src_off = store_attr(d, s + vstart, vlen, 400);
  2015. else if (str_eq(name, "alt"))
  2016. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2017. else if (str_eq(name, "color"))
  2018. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2019. else if (str_eq(name, "rel"))
  2020. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2021. else if (str_eq(name, "value"))
  2022. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2023. else if (str_eq(name, "placeholder") &&
  2024. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2025. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2026. else if (str_eq(name, "type") &&
  2027. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2028. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2029. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2030. else if (str_eq(name, "width")) {
  2031. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2032. if (vlen && s[vstart + vlen - 1] == '%')
  2033. n->width_pct = 1;
  2034. } else if (str_eq(name, "height")) {
  2035. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2036. if (vlen && s[vstart + vlen - 1] == '%')
  2037. n->height_pct = 1;
  2038. } else if (str_eq(name, "colspan")) {
  2039. unsigned int v = parse_uint(s + vstart, vlen);
  2040. if (v < 1)
  2041. v = 1;
  2042. if (v > 16)
  2043. v = 16;
  2044. n->colspan = (unsigned char)v;
  2045. } else if (str_eq(name, "rowspan")) {
  2046. unsigned int v = parse_uint(s + vstart, vlen);
  2047. if (v < 1)
  2048. v = 1;
  2049. if (v > 16)
  2050. v = 16;
  2051. n->rowspan = (unsigned char)v;
  2052. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2053. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2054. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2055. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2056. } else if (str_eq(name, "x2")) {
  2057. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2058. } else if (str_eq(name, "y2")) {
  2059. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2060. } else if (str_eq(name, "rx")) {
  2061. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2062. } else if (str_eq(name, "d")) {
  2063. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2064. } else if (str_eq(name, "viewbox")) {
  2065. unsigned short vw = 0, vh = 0;
  2066. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2067. n->width_attr = vw;
  2068. n->height_attr = vh;
  2069. n->flags |= BR_NODE_VIEWBOX;
  2070. }
  2071. } else if (str_eq(name, "text-anchor")) {
  2072. if (contains_ci_len(s + vstart, vlen, "middle"))
  2073. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2074. else if (contains_ci_len(s + vstart, vlen, "end"))
  2075. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2076. }
  2077. }
  2078. parse_attrs_generic(d, n, s, len);
  2079. }
  2080. static int str_eq(const char *a, const char *b)
  2081. {
  2082. unsigned int i = 0;
  2083. while (a[i] && b[i] && a[i] == b[i])
  2084. i++;
  2085. return a[i] == '\0' && b[i] == '\0';
  2086. }
  2087. /* ---- CSS ---- */
  2088. enum {
  2089. PROP_COLOR, PROP_BG, PROP_BG_IMAGE, PROP_BG_REPEAT,
  2090. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2091. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2092. PROP_FONT_PX, PROP_FONT_UNIT,
  2093. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2094. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2095. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2096. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2097. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2098. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2099. PROP_BORDER_W, PROP_BORDER_COLOR,
  2100. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2101. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2102. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2103. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2104. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2105. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2106. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2107. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2108. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2109. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2110. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2111. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2112. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2113. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2114. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2115. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2116. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2117. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2118. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2119. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2120. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2121. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2122. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2123. PROP_ZINDEX, PROP_ZSET,
  2124. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2125. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2126. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2127. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2128. PROP_COUNT
  2129. };
  2130. struct color_name {
  2131. const char *name;
  2132. unsigned short rgb;
  2133. };
  2134. static const struct color_name color_names[] = {
  2135. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2136. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2137. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2138. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2139. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2140. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2141. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2142. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2143. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2144. { "darkgrey", 0x528A }, { 0, 0 }
  2145. };
  2146. static int hexval(char c)
  2147. {
  2148. if (c >= '0' && c <= '9')
  2149. return c - '0';
  2150. if (c >= 'a' && c <= 'f')
  2151. return c - 'a' + 10;
  2152. if (c >= 'A' && c <= 'F')
  2153. return c - 'A' + 10;
  2154. return -1;
  2155. }
  2156. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2157. {
  2158. unsigned int i;
  2159. for (i = 0; color_names[i].name; i++) {
  2160. unsigned int j, n = s_len(color_names[i].name);
  2161. if (n != len)
  2162. continue;
  2163. for (j = 0; j < n; j++) {
  2164. char c = s[j];
  2165. if (c >= 'A' && c <= 'Z')
  2166. c = (char)(c - 'A' + 'a');
  2167. if (c != color_names[i].name[j])
  2168. break;
  2169. }
  2170. if (j == n) {
  2171. *out = color_names[i].rgb;
  2172. return 1;
  2173. }
  2174. }
  2175. return 0;
  2176. }
  2177. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2178. * over white; the render target is a white page. */
  2179. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2180. {
  2181. unsigned int i = 0;
  2182. while (i < len && is_space(s[i]))
  2183. i++;
  2184. if (i < len && s[i] == '#') {
  2185. int d[6], nd = 0;
  2186. i++;
  2187. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2188. d[nd++] = hexval(s[i++]);
  2189. if (nd == 3) {
  2190. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2191. unsigned int b = (unsigned int)d[2];
  2192. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2193. ((g * 17 >> 2) << 5) |
  2194. (b * 17 >> 3));
  2195. return 1;
  2196. }
  2197. if (nd >= 6) {
  2198. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2199. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2200. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2201. *out = (unsigned short)(((r >> 3) << 11) |
  2202. ((g >> 2) << 5) | (b >> 3));
  2203. return 1;
  2204. }
  2205. return 0;
  2206. }
  2207. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2208. unsigned int v[3], k = 0, a = 255;
  2209. while (i < len && s[i] != '(')
  2210. i++;
  2211. if (i >= len)
  2212. return 0;
  2213. i++;
  2214. while (k < 3 && i < len) {
  2215. unsigned int x = 0;
  2216. int percent = 0;
  2217. while (i < len && (is_space(s[i]) || s[i] == ','))
  2218. i++;
  2219. if (i >= len || s[i] < '0' || s[i] > '9')
  2220. return 0;
  2221. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2222. x = x * 10 + (unsigned int)(s[i] - '0');
  2223. i++;
  2224. }
  2225. if (i < len && s[i] == '%') {
  2226. percent = 1;
  2227. i++;
  2228. }
  2229. if (percent)
  2230. x = x * 255 / 100;
  2231. if (x > 255)
  2232. x = 255;
  2233. v[k++] = x;
  2234. }
  2235. /* Optional alpha in both syntaxes:
  2236. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2237. {
  2238. unsigned int whole = 0, frac = 0, scale = 1;
  2239. int percent = 0;
  2240. while (i < len && (is_space(s[i]) || s[i] == ','))
  2241. i++;
  2242. if (i < len && s[i] == '/') {
  2243. i++;
  2244. while (i < len && is_space(s[i]))
  2245. i++;
  2246. }
  2247. if (i < len && s[i] >= '0' && s[i] <= '9') {
  2248. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2249. whole = whole * 10 +
  2250. (unsigned int)(s[i] - '0');
  2251. i++;
  2252. }
  2253. if (i < len && s[i] == '.') {
  2254. i++;
  2255. while (i < len && s[i] >= '0' &&
  2256. s[i] <= '9') {
  2257. if (scale < 1000) {
  2258. frac = frac * 10 +
  2259. (unsigned int)(s[i] - '0');
  2260. scale *= 10;
  2261. }
  2262. i++;
  2263. }
  2264. }
  2265. while (i < len && is_space(s[i]))
  2266. i++;
  2267. if (i < len && s[i] == '%')
  2268. percent = 1;
  2269. if (percent)
  2270. a = whole > 100 ? 255 :
  2271. whole * 255 / 100;
  2272. else if (whole >= 1)
  2273. a = 255;
  2274. else
  2275. a = frac * 255 / scale;
  2276. }
  2277. }
  2278. if (k == 3) {
  2279. unsigned int r = v[0], g = v[1], b = v[2];
  2280. if (a < 255) {
  2281. r = (r * a + 255u * (255u - a)) / 255u;
  2282. g = (g * a + 255u * (255u - a)) / 255u;
  2283. b = (b * a + 255u * (255u - a)) / 255u;
  2284. }
  2285. *out = (unsigned short)(((r >> 3) << 11) |
  2286. ((g >> 2) << 5) | (b >> 3));
  2287. return 1;
  2288. }
  2289. return 0;
  2290. }
  2291. {
  2292. unsigned int j = i;
  2293. while (i < len && !is_space(s[i]))
  2294. i++;
  2295. return named_color(s + j, i - j, out);
  2296. }
  2297. }
  2298. /* Fully transparent colours have no RGB565 representation. They are common
  2299. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2300. * only on hover; painting them black would draw an outline around every
  2301. * pill and card. */
  2302. static int color_is_transparent(const char *s, unsigned int len)
  2303. {
  2304. unsigned int i = 0, n = 0;
  2305. int last = 0, prev = 0, d;
  2306. while (i < len && is_space(s[i]))
  2307. i++;
  2308. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2309. return 1;
  2310. if (i >= len || s[i] != '#')
  2311. return 0;
  2312. i++;
  2313. while (i < len && hexval(s[i]) >= 0) {
  2314. d = hexval(s[i]);
  2315. prev = last;
  2316. last = d;
  2317. n++;
  2318. i++;
  2319. }
  2320. while (i < len && is_space(s[i]))
  2321. i++;
  2322. if (i != len)
  2323. return 0;
  2324. if (n == 4)
  2325. return last == 0;
  2326. if (n == 8)
  2327. return prev == 0 && last == 0;
  2328. return 0;
  2329. }
  2330. static int register_bg_url(const char *s, unsigned int len)
  2331. {
  2332. unsigned int i, j, n;
  2333. while (len && is_space(*s)) {
  2334. s++;
  2335. len--;
  2336. }
  2337. while (len && is_space(s[len - 1]))
  2338. len--;
  2339. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2340. s[len - 1] == s[0]) {
  2341. s++;
  2342. len -= 2;
  2343. }
  2344. if (len > 191)
  2345. len = 191;
  2346. for (i = 0; i < bg_url_count; i++) {
  2347. for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
  2348. ;
  2349. if (j == len && bg_urls[i][j] == '\0')
  2350. return (int)i + 1;
  2351. }
  2352. if (bg_url_count >= BR_BG_URLS || len == 0)
  2353. return 0;
  2354. n = len < 191 ? len : 191;
  2355. for (i = 0; i < n; i++)
  2356. bg_urls[bg_url_count][i] = s[i];
  2357. bg_urls[bg_url_count][n] = '\0';
  2358. return (int)(++bg_url_count);
  2359. }
  2360. /* Length values for margins/padding: px, ignore percentages/units. */
  2361. static unsigned int viewport_w;
  2362. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2363. {
  2364. unsigned int i = *pos;
  2365. int sign = 1;
  2366. long v = 0; /* hundredths of a unit before conversion */
  2367. int digits = 0, frac = 0, unit = 0;
  2368. while (i < len && is_space(s[i]))
  2369. i++;
  2370. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2371. if (s[i] == '-')
  2372. sign = -1;
  2373. i++;
  2374. }
  2375. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2376. v = v * 10 + (s[i] - '0');
  2377. digits = 1;
  2378. i++;
  2379. }
  2380. if (i < len && s[i] == '.') {
  2381. i++;
  2382. /* Minified CSS writes ".5rem" without a leading zero. */
  2383. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2384. if (frac < 2) {
  2385. v = v * 10 + (s[i] - '0');
  2386. frac++;
  2387. digits = 1;
  2388. }
  2389. i++;
  2390. }
  2391. }
  2392. while (frac < 2) {
  2393. v *= 10;
  2394. frac++;
  2395. }
  2396. while (frac > 2) {
  2397. v /= 10;
  2398. frac--;
  2399. }
  2400. if (i < len && s[i] == '%')
  2401. unit = 2;
  2402. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2403. s[i + 2] == 'm')
  2404. unit = 1;
  2405. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2406. unit = 3;
  2407. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2408. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2409. s[i] == 'a' || s[i] == 'd'))
  2410. i++;
  2411. *pos = i;
  2412. if (!digits)
  2413. return 0;
  2414. if (unit == 1)
  2415. v *= root_font_px;
  2416. else if (unit == 3)
  2417. v *= root_font_px;
  2418. else if (unit == 2)
  2419. v = v * (long)viewport_w / 100;
  2420. v /= 100;
  2421. v *= sign;
  2422. if (v < -2048)
  2423. v = -2048;
  2424. if (v > 2048)
  2425. v = 2048;
  2426. return (int)v;
  2427. }
  2428. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2429. * treats it as a zero-length token; margins need to keep it so block layout
  2430. * can centre a box with a definite width. */
  2431. #define CSS_LEN_AUTO (-32768)
  2432. /* A box length with its unit preserved for layout-time resolution:
  2433. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2434. * rem and whole pixels for px. */
  2435. static int parse_css_len(const char *s, unsigned int len,
  2436. unsigned int *pos, int *unit)
  2437. {
  2438. unsigned int i = *pos;
  2439. int sign = 1;
  2440. long v = 0;
  2441. int digits = 0, frac = 0;
  2442. *unit = 0;
  2443. while (i < len && is_space(s[i]))
  2444. i++;
  2445. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2446. if (s[i] == '-')
  2447. sign = -1;
  2448. i++;
  2449. }
  2450. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2451. v = v * 10 + (s[i] - '0');
  2452. digits = 1;
  2453. i++;
  2454. }
  2455. if (i < len && s[i] == '.') {
  2456. i++;
  2457. /* Leading-dot fractions are common in minified CSS
  2458. * (".5rem", ".3rem"). */
  2459. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2460. if (frac < 2) {
  2461. v = v * 10 + (s[i] - '0');
  2462. frac++;
  2463. digits = 1;
  2464. }
  2465. i++;
  2466. }
  2467. }
  2468. while (frac < 2) {
  2469. v *= 10;
  2470. frac++;
  2471. }
  2472. if (!digits) {
  2473. *pos = i;
  2474. return 0;
  2475. }
  2476. if (i < len && s[i] == '%') {
  2477. *unit = 1;
  2478. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2479. s[i + 2] == 'm') {
  2480. *unit = 3;
  2481. i += 3;
  2482. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2483. *unit = 2;
  2484. i += 2;
  2485. } else {
  2486. v /= 100;
  2487. }
  2488. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2489. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2490. s[i] == 'a' || s[i] == 'd'))
  2491. i++;
  2492. *pos = i;
  2493. v *= sign;
  2494. if (v < -32768)
  2495. v = -32768;
  2496. if (v > 32767)
  2497. v = 32767;
  2498. return (int)v;
  2499. }
  2500. static int parse_margin_len(const char *s, unsigned int len,
  2501. unsigned int *pos, int *unit)
  2502. {
  2503. unsigned int i = *pos;
  2504. while (i < len && is_space(s[i]))
  2505. i++;
  2506. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2507. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2508. *pos = i + 4;
  2509. *unit = 4;
  2510. return CSS_LEN_AUTO;
  2511. }
  2512. return parse_css_len(s, len, pos, unit);
  2513. }
  2514. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2515. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2516. static int parse_margin_calc(const char *s, unsigned int len,
  2517. unsigned int *pos, int *pct, int *px)
  2518. {
  2519. unsigned int i = *pos, j;
  2520. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2521. return 0;
  2522. i += 5;
  2523. j = i;
  2524. while (j < len && s[j] != ')')
  2525. j++;
  2526. if (j >= len)
  2527. return 0;
  2528. if (!parse_calc(s + i, j - i, pct, px))
  2529. return 0;
  2530. *pos = j + 1;
  2531. return 1;
  2532. }
  2533. /* calc(P% +/- Npx) for the common width/margin forms. */
  2534. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2535. {
  2536. unsigned int i = 0;
  2537. int sign = 1;
  2538. int have = 0;
  2539. *pct = 0;
  2540. *px = 0;
  2541. while (i < len) {
  2542. unsigned int j;
  2543. long whole = 0, frac = 0, scale = 1;
  2544. int digits = 0;
  2545. while (i < len && is_space(s[i]))
  2546. i++;
  2547. if (i >= len)
  2548. break;
  2549. if (s[i] == '+' || s[i] == '-') {
  2550. sign = s[i] == '-' ? -1 : 1;
  2551. i++;
  2552. continue;
  2553. }
  2554. if (s[i] < '0' || s[i] > '9') {
  2555. if (s[i] == '.') {
  2556. /* fall through to the number parser */
  2557. } else {
  2558. i++;
  2559. continue;
  2560. }
  2561. }
  2562. j = i;
  2563. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2564. whole = whole * 10 + (s[j] - '0');
  2565. digits = 1;
  2566. j++;
  2567. }
  2568. if (j < len && s[j] == '.') {
  2569. j++;
  2570. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2571. frac = frac * 10 + (s[j] - '0');
  2572. scale *= 10;
  2573. j++;
  2574. }
  2575. }
  2576. if (!digits) {
  2577. i = j + 1;
  2578. continue;
  2579. }
  2580. while (j < len && is_space(s[j]))
  2581. j++;
  2582. if (j < len && s[j] == '%') {
  2583. *pct = sign * (int)(whole + (scale > 1 ?
  2584. frac * 100 / scale : 0));
  2585. have = 1;
  2586. j++;
  2587. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2588. *px = sign * (int)whole;
  2589. have = 1;
  2590. j += 2;
  2591. }
  2592. i = j;
  2593. while (i < len && !is_space(s[i]))
  2594. i++;
  2595. sign = 1;
  2596. }
  2597. return have;
  2598. }
  2599. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2600. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2601. unsigned char *kind)
  2602. {
  2603. unsigned int i = 0;
  2604. unsigned long v = 0;
  2605. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2606. *val = 0;
  2607. *kind = 0;
  2608. while (i < len && is_space(s[i]))
  2609. i++;
  2610. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2611. return 0;
  2612. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2613. return 0;
  2614. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2615. v = v * 10 + (unsigned int)(s[i] - '0');
  2616. digits = 1;
  2617. i++;
  2618. }
  2619. if (i < len && s[i] == '.') {
  2620. i++;
  2621. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2622. if (frac < 2) {
  2623. v = v * 10 + (unsigned int)(s[i] - '0');
  2624. frac++;
  2625. digits = 1;
  2626. }
  2627. i++;
  2628. }
  2629. }
  2630. while (frac < 2) {
  2631. v *= 10;
  2632. frac++;
  2633. }
  2634. while (frac > 2) {
  2635. v /= 10;
  2636. frac--;
  2637. }
  2638. if (!digits)
  2639. return 0;
  2640. while (i < len && is_space(s[i]))
  2641. i++;
  2642. if (i < len && s[i] == '%')
  2643. pct = 1;
  2644. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2645. s[i + 2] == 'm')
  2646. rem = 1;
  2647. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2648. rem = 2;
  2649. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2650. vw = 1;
  2651. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2652. ; /* px: already the default scale */
  2653. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2654. bad_unit = 1;
  2655. if (bad_unit)
  2656. return 0;
  2657. if (rem == 1 || rem == 2) {
  2658. if (v > 65535)
  2659. v = 65535;
  2660. *val = (unsigned short)v;
  2661. *kind = rem == 1 ? 5 : 4;
  2662. return 1;
  2663. }
  2664. v /= 100;
  2665. if (vw)
  2666. v = v * viewport_w / 100;
  2667. if (pct) {
  2668. if (v > 100)
  2669. v = 100;
  2670. *kind = 2;
  2671. } else {
  2672. if (v > 2048)
  2673. v = 2048;
  2674. *kind = 1;
  2675. }
  2676. *val = (unsigned short)v;
  2677. return 1;
  2678. }
  2679. static int parse_font_px(const char *s, unsigned int len)
  2680. {
  2681. unsigned int i = 0;
  2682. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2683. int v1000, px;
  2684. int is_px = 1, is_rem = 0, is_pct = 0;
  2685. while (i < len && is_space(s[i]))
  2686. i++;
  2687. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2688. return 24;
  2689. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2690. return 32;
  2691. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2692. return 14;
  2693. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2694. whole = whole * 10 + (s[i] - '0');
  2695. digits = 1;
  2696. i++;
  2697. }
  2698. if (i < len && s[i] == '.') {
  2699. i++;
  2700. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2701. if (fscale < 1000) {
  2702. frac = frac * 10 + (s[i] - '0');
  2703. fscale *= 10;
  2704. }
  2705. digits = 1;
  2706. i++;
  2707. }
  2708. }
  2709. if (!digits)
  2710. return 0;
  2711. while (i < len && is_space(s[i]))
  2712. i++;
  2713. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2714. is_rem = 1;
  2715. is_px = 0;
  2716. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2717. is_rem = 1;
  2718. is_px = 0;
  2719. } else if (i < len && s[i] == '%') {
  2720. is_pct = 1;
  2721. is_px = 0;
  2722. }
  2723. v1000 = whole * 1000 + frac * 1000 / fscale;
  2724. if (is_rem)
  2725. px = v1000 * root_font_px / 1000;
  2726. else if (is_pct)
  2727. px = v1000 * root_font_px / 100000;
  2728. else if (is_px)
  2729. px = v1000 / 1000;
  2730. else
  2731. px = v1000 / 1000;
  2732. if (px < 8)
  2733. px = 8;
  2734. if (px > 72)
  2735. px = 72;
  2736. return px;
  2737. }
  2738. /* font-size keeping the unit so em/% resolve against the parent face. */
  2739. static int parse_font_size(const char *s, unsigned int len,
  2740. unsigned short *raw, unsigned char *unit)
  2741. {
  2742. unsigned int i = 0;
  2743. int whole = 0, frac = 0, scale = 1, digits = 0;
  2744. int v100;
  2745. *raw = 0;
  2746. *unit = 0;
  2747. while (i < len && is_space(s[i]))
  2748. i++;
  2749. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2750. *raw = 24;
  2751. *unit = 1;
  2752. return 1;
  2753. }
  2754. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2755. *raw = 32;
  2756. *unit = 1;
  2757. return 1;
  2758. }
  2759. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2760. *raw = 14;
  2761. *unit = 1;
  2762. return 1;
  2763. }
  2764. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2765. whole = whole * 10 + (s[i] - '0');
  2766. digits = 1;
  2767. i++;
  2768. }
  2769. if (i < len && s[i] == '.') {
  2770. i++;
  2771. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2772. frac = frac * 10 + (s[i] - '0');
  2773. scale *= 10;
  2774. i++;
  2775. }
  2776. }
  2777. if (!digits && !frac)
  2778. return 0;
  2779. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2780. while (i < len && is_space(s[i]))
  2781. i++;
  2782. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2783. *raw = (unsigned short)v100;
  2784. *unit = 3;
  2785. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2786. *raw = (unsigned short)v100;
  2787. *unit = 2;
  2788. } else if (i < len && s[i] == '%') {
  2789. /* Keep hundredths: 62.5% is a common root size and truncating
  2790. * it to 62% makes every rem 1px too small. */
  2791. *raw = (unsigned short)v100;
  2792. *unit = 4;
  2793. } else {
  2794. *raw = (unsigned short)(v100 / 100);
  2795. *unit = 1;
  2796. }
  2797. return 1;
  2798. }
  2799. static int parse_line_height(const char *s, unsigned int len,
  2800. unsigned short *px, unsigned char *pct)
  2801. {
  2802. unsigned int i = 0;
  2803. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2804. int v1000;
  2805. int is_px = 0, is_pct = 0;
  2806. *px = 0;
  2807. *pct = 0;
  2808. while (i < len && is_space(s[i]))
  2809. i++;
  2810. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2811. return 0;
  2812. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2813. whole = whole * 10 + (s[i] - '0');
  2814. digits = 1;
  2815. i++;
  2816. }
  2817. if (i < len && s[i] == '.') {
  2818. i++;
  2819. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2820. frac = frac * 10 + (s[i] - '0');
  2821. fscale *= 10;
  2822. digits = 1;
  2823. i++;
  2824. }
  2825. }
  2826. if (!digits)
  2827. return 0;
  2828. while (i < len && is_space(s[i]))
  2829. i++;
  2830. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2831. is_px = 1;
  2832. else if (i < len && s[i] == '%')
  2833. is_pct = 1;
  2834. v1000 = whole * 1000 + frac * 1000 / fscale;
  2835. if (is_px) {
  2836. int v = v1000 / 1000;
  2837. if (v < 8)
  2838. v = 8;
  2839. if (v > 160)
  2840. v = 160;
  2841. *px = (unsigned short)v;
  2842. } else {
  2843. int v = v1000 / 10; /* unitless multiplier -> percent */
  2844. if (is_pct)
  2845. v = v1000 / 1000;
  2846. if (v < 50)
  2847. v = 50;
  2848. if (v > 400)
  2849. v = 400;
  2850. *pct = (unsigned char)v;
  2851. }
  2852. return 1;
  2853. }
  2854. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2855. {
  2856. unsigned int i, j, n = s_len(needle);
  2857. for (i = 0; i + n <= len; i++) {
  2858. for (j = 0; j < n; j++) {
  2859. char a = s[i + j];
  2860. if (a >= 'A' && a <= 'Z')
  2861. a = (char)(a - 'A' + 'a');
  2862. if (a != needle[j])
  2863. break;
  2864. }
  2865. if (j == n)
  2866. return 1;
  2867. }
  2868. return 0;
  2869. }
  2870. /* Pull a colour out of a border/background shorthand. */
  2871. /* Background shorthands can hold several layers before the final solid
  2872. * colour. The colour is conventionally the last token (e.g. a stack of
  2873. * gradients followed by var(--bg)), so take the rightmost colour rather
  2874. * than the first gradient stop. */
  2875. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2876. {
  2877. int last = -1;
  2878. unsigned int i;
  2879. for (i = 0; i < len; i++) {
  2880. if (s[i] == '#')
  2881. last = (int)i;
  2882. else if ((s[i] == 'r' || s[i] == 'R') &&
  2883. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2884. last = (int)i;
  2885. }
  2886. if (last >= 0)
  2887. return parse_color(s + last, len - (unsigned int)last, c);
  2888. while (i > 0) {
  2889. unsigned int st, e;
  2890. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2891. s[i - 1] == ')' || s[i - 1] == '('))
  2892. i--;
  2893. e = i;
  2894. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  2895. s[i - 1] != ')' && s[i - 1] != '(')
  2896. i--;
  2897. st = i;
  2898. if (e > st && parse_color(s + st, e - st, c))
  2899. return 1;
  2900. }
  2901. return 0;
  2902. }
  2903. static int parse_display(const char *s, unsigned int len)
  2904. {
  2905. unsigned int i;
  2906. while (len && is_space(s[0])) { s++; len--; }
  2907. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  2908. ;
  2909. if (i == 4 && ci_eq_n(s, "none", 4))
  2910. return BR_DISPLAY_NONE;
  2911. if (i == 5 && ci_eq_n(s, "block", 5))
  2912. return BR_DISPLAY_BLOCK;
  2913. if (i == 6 && ci_eq_n(s, "inline", 6))
  2914. return BR_DISPLAY_INLINE;
  2915. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  2916. return BR_DISPLAY_INLINE_BLOCK;
  2917. if (i == 10 && ci_eq_n(s, "list-item", 10))
  2918. return BR_DISPLAY_BLOCK;
  2919. if (i == 4 && ci_eq_n(s, "flex", 4))
  2920. return BR_DISPLAY_FLEX;
  2921. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  2922. return BR_DISPLAY_FLEX;
  2923. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  2924. * block instead of an inline box. */
  2925. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  2926. return BR_DISPLAY_BLOCK;
  2927. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  2928. return BR_DISPLAY_BLOCK;
  2929. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  2930. return BR_DISPLAY_FLEX;
  2931. if (i == 5 && ci_eq_n(s, "table", 5))
  2932. return BR_DISPLAY_BLOCK;
  2933. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  2934. return BR_DISPLAY_BLOCK;
  2935. /* Grid shells are common on news/wiki pages, but the two-column
  2936. * heuristic is not reliable for their nested area templates yet.
  2937. * Collapse them to block flow so the content stays readable. */
  2938. if (i == 4 && ci_eq_n(s, "grid", 4))
  2939. return BR_DISPLAY_BLOCK;
  2940. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  2941. return BR_DISPLAY_BLOCK;
  2942. return BR_DISPLAY_INLINE;
  2943. }
  2944. static void decl_add(struct br_decl *out, unsigned int *nout,
  2945. unsigned char prop, int val, int important)
  2946. {
  2947. if (*nout >= BR_DECLS)
  2948. return;
  2949. out[*nout].prop = prop;
  2950. out[*nout].val = (short)val;
  2951. out[*nout].important = (unsigned char)(important != 0);
  2952. (*nout)++;
  2953. }
  2954. /* One whitespace-separated token. Returns 0 at the end of the value. */
  2955. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  2956. unsigned int *start, unsigned int *end)
  2957. {
  2958. unsigned int i = *pos;
  2959. while (i < len && is_space(s[i]))
  2960. i++;
  2961. if (i >= len)
  2962. return 0;
  2963. *start = i;
  2964. while (i < len && !is_space(s[i]))
  2965. i++;
  2966. *end = i;
  2967. *pos = i;
  2968. return 1;
  2969. }
  2970. static int bg_number(const char *s, unsigned int len, signed short *val,
  2971. unsigned char *kind)
  2972. {
  2973. unsigned int i = 0;
  2974. long v = 0;
  2975. int neg = 0, digits = 0, pct = 0;
  2976. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2977. neg = s[i] == '-';
  2978. i++;
  2979. }
  2980. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2981. v = v * 10 + (s[i] - '0');
  2982. digits = 1;
  2983. i++;
  2984. }
  2985. if (i < len && s[i] == '.') {
  2986. i++;
  2987. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2988. digits = 1;
  2989. i++;
  2990. }
  2991. }
  2992. if (!digits)
  2993. return 0;
  2994. if (i < len && s[i] == '%') {
  2995. pct = 1;
  2996. i++;
  2997. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  2998. i += 2;
  2999. }
  3000. if (i != len)
  3001. return 0;
  3002. if (neg)
  3003. v = -v;
  3004. if (v < -4096)
  3005. v = -4096;
  3006. if (v > 4096)
  3007. v = 4096;
  3008. *val = (signed short)v;
  3009. *kind = (unsigned char)(pct ? 1 : 0);
  3010. return 1;
  3011. }
  3012. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3013. int *vertical)
  3014. {
  3015. *vertical = 0;
  3016. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3017. *val = 0;
  3018. return 1;
  3019. }
  3020. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3021. *val = 100;
  3022. return 1;
  3023. }
  3024. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3025. *val = 0;
  3026. *vertical = 1;
  3027. return 1;
  3028. }
  3029. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3030. *val = 100;
  3031. *vertical = 1;
  3032. return 1;
  3033. }
  3034. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3035. *val = 50;
  3036. return 1;
  3037. }
  3038. return 0;
  3039. }
  3040. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3041. const char *s, unsigned int len, int important)
  3042. {
  3043. unsigned int pos = 0, ts, te;
  3044. signed short val[2] = { 50, 50 };
  3045. unsigned char kind[2] = { 1, 1 };
  3046. int vert[2] = { 0, 0 }, n = 0;
  3047. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3048. signed short v;
  3049. int is_vert;
  3050. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3051. val[n] = v;
  3052. kind[n] = 1;
  3053. vert[n] = is_vert;
  3054. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3055. val[n] = v;
  3056. vert[n] = 0;
  3057. } else {
  3058. n++;
  3059. continue;
  3060. }
  3061. n++;
  3062. }
  3063. if (n == 1) {
  3064. if (vert[0]) {
  3065. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3066. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3067. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3068. important);
  3069. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3070. } else {
  3071. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3072. important);
  3073. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3074. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3075. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3076. }
  3077. return;
  3078. }
  3079. if (n >= 2) {
  3080. int x = vert[0] && !vert[1] ? 1 : 0;
  3081. int y = 1 - x;
  3082. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3083. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3084. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3085. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3086. }
  3087. }
  3088. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3089. const char *s, unsigned int len, int important)
  3090. {
  3091. unsigned int pos = 0, ts, te;
  3092. signed short w = 0, h = 0;
  3093. unsigned char kw = 0, kh = 0;
  3094. int n = 0;
  3095. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3096. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3097. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3098. return;
  3099. }
  3100. if (n == 0 && te - ts == 7 &&
  3101. ci_eq_n(s + ts, "contain", 7)) {
  3102. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3103. return;
  3104. }
  3105. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3106. n++;
  3107. continue;
  3108. }
  3109. {
  3110. signed short v;
  3111. unsigned char k;
  3112. if (bg_number(s + ts, te - ts, &v, &k)) {
  3113. if (n == 0) {
  3114. w = v;
  3115. kw = k;
  3116. } else {
  3117. h = v;
  3118. kh = k;
  3119. }
  3120. }
  3121. }
  3122. n++;
  3123. }
  3124. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3125. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3126. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3127. (void)kh;
  3128. }
  3129. /* Parse the `position / size` part of the background shorthand. The image
  3130. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3131. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3132. const char *v, unsigned int len,
  3133. int important)
  3134. {
  3135. unsigned int i, slash = 0, depth = 0, p, first, count;
  3136. char q = 0;
  3137. signed short nv;
  3138. int vert;
  3139. unsigned char kind;
  3140. for (i = 0; i < len; i++) {
  3141. char c = v[i];
  3142. if (q) {
  3143. if (c == q)
  3144. q = 0;
  3145. continue;
  3146. }
  3147. if (c == '\'' || c == '"') {
  3148. q = c;
  3149. continue;
  3150. }
  3151. if (c == '(') {
  3152. depth++;
  3153. continue;
  3154. }
  3155. if (c == ')') {
  3156. if (depth)
  3157. depth--;
  3158. continue;
  3159. }
  3160. if (c == '/' && !depth) {
  3161. slash = i;
  3162. break;
  3163. }
  3164. }
  3165. if (!slash)
  3166. return;
  3167. /* Up to two position tokens immediately before the slash. */
  3168. p = slash;
  3169. first = slash;
  3170. count = 0;
  3171. while (count < 2) {
  3172. unsigned int e;
  3173. while (p > 0 && is_space(v[p - 1]))
  3174. p--;
  3175. e = p;
  3176. while (p > 0 && !is_space(v[p - 1]))
  3177. p--;
  3178. if (e == p)
  3179. break;
  3180. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3181. bg_number(v + p, e - p, &nv, &kind)) {
  3182. first = p;
  3183. count++;
  3184. } else {
  3185. break;
  3186. }
  3187. }
  3188. if (count)
  3189. add_bg_position(out, nout, v + first, slash - first,
  3190. important);
  3191. /* Up to two size tokens after the slash. */
  3192. p = slash + 1;
  3193. while (p < len && is_space(v[p]))
  3194. p++;
  3195. first = p;
  3196. count = 0;
  3197. while (count < 2) {
  3198. unsigned int ts = p;
  3199. while (p < len && !is_space(v[p]))
  3200. p++;
  3201. if (p == ts)
  3202. break;
  3203. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3204. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3205. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3206. bg_number(v + ts, p - ts, &nv, &kind)) {
  3207. count++;
  3208. while (p < len && is_space(v[p]))
  3209. p++;
  3210. } else {
  3211. break;
  3212. }
  3213. }
  3214. if (count)
  3215. add_bg_size(out, nout, v + first, p - first, important);
  3216. }
  3217. static int parse_fixed_100(const char *s, unsigned int len,
  3218. unsigned int *pos, signed short *out)
  3219. {
  3220. unsigned int i = *pos;
  3221. long whole = 0, frac = 0;
  3222. int neg = 0, digits = 0, fd = 0;
  3223. while (i < len && is_space(s[i]))
  3224. i++;
  3225. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3226. neg = s[i] == '-';
  3227. i++;
  3228. }
  3229. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3230. whole = whole * 10 + (s[i] - '0');
  3231. if (whole > 327)
  3232. whole = 327;
  3233. digits = 1;
  3234. i++;
  3235. }
  3236. if (i < len && s[i] == '.') {
  3237. i++;
  3238. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3239. if (fd < 2) {
  3240. frac = frac * 10 + (s[i] - '0');
  3241. fd++;
  3242. digits = 1;
  3243. }
  3244. i++;
  3245. }
  3246. }
  3247. if (!digits)
  3248. return 0;
  3249. while (fd++ < 2)
  3250. frac *= 10;
  3251. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3252. *pos = i;
  3253. return 1;
  3254. }
  3255. static int find_fn(const char *v, unsigned int len, const char *name,
  3256. unsigned int *arg)
  3257. {
  3258. unsigned int n = s_len(name), i;
  3259. for (i = 0; i + n <= len; i++) {
  3260. unsigned int j;
  3261. if (!ci_eq_n(v + i, name, n))
  3262. continue;
  3263. j = i + n;
  3264. while (j < len && is_space(v[j]))
  3265. j++;
  3266. if (j < len && v[j] == '(') {
  3267. *arg = j + 1;
  3268. return 1;
  3269. }
  3270. }
  3271. return 0;
  3272. }
  3273. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3274. const char *s, unsigned int len,
  3275. int important)
  3276. {
  3277. unsigned int pos = 0, ts, te;
  3278. signed short val[2] = { 50, 50 };
  3279. unsigned char kind[2] = { 1, 1 };
  3280. int vert[2] = { 0, 0 }, n = 0;
  3281. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3282. signed short v;
  3283. int is_vert;
  3284. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3285. val[n] = v;
  3286. kind[n] = 1;
  3287. vert[n] = is_vert;
  3288. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3289. val[n] = v;
  3290. vert[n] = 0;
  3291. } else {
  3292. n++;
  3293. continue;
  3294. }
  3295. n++;
  3296. }
  3297. if (n == 1) {
  3298. if (vert[0]) {
  3299. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3300. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3301. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3302. important);
  3303. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3304. } else {
  3305. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3306. important);
  3307. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3308. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3309. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3310. }
  3311. return;
  3312. }
  3313. if (n >= 2) {
  3314. int x = vert[0] && !vert[1] ? 1 : 0;
  3315. int y = 1 - x;
  3316. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3317. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3318. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3319. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3320. }
  3321. }
  3322. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3323. const char *v, unsigned int vl, int important)
  3324. {
  3325. unsigned int i;
  3326. for (i = 0; i + 4 <= vl; i++) {
  3327. if (ci_eq_n(v + i, "url(", 4)) {
  3328. unsigned int st = i + 4, en = st;
  3329. int idx;
  3330. while (en < vl && v[en] != ')')
  3331. en++;
  3332. idx = register_bg_url(v + st, en - st);
  3333. if (idx)
  3334. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3335. important);
  3336. return;
  3337. }
  3338. }
  3339. }
  3340. static int parse_len_token(const char *s, unsigned int len, int *out)
  3341. {
  3342. unsigned int pos = 0;
  3343. int v;
  3344. if (!len)
  3345. return 0;
  3346. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3347. return 0;
  3348. v = parse_len(s, len, &pos);
  3349. if (pos == 0)
  3350. return 0;
  3351. *out = v;
  3352. return 1;
  3353. }
  3354. static void expand_four(int v[4], int n)
  3355. {
  3356. if (n == 1) {
  3357. v[1] = v[2] = v[3] = v[0];
  3358. } else if (n == 2) {
  3359. v[2] = v[0];
  3360. v[3] = v[1];
  3361. } else if (n == 3) {
  3362. v[3] = v[1];
  3363. }
  3364. }
  3365. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3366. * the input is copied verbatim when it contains no var(). */
  3367. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3368. unsigned int len, char *out,
  3369. unsigned int cap, int depth)
  3370. {
  3371. unsigned int i = 0, n = 0;
  3372. if (depth > 8)
  3373. return 0;
  3374. while (i < len && n + 1 < cap) {
  3375. unsigned int p, inner, close, comma, nlen, fstart;
  3376. char quote;
  3377. int parens;
  3378. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3379. out[n++] = s[i++];
  3380. continue;
  3381. }
  3382. p = i + 4;
  3383. quote = 0;
  3384. parens = 1;
  3385. while (p < len && parens) {
  3386. char c = s[p];
  3387. if (quote) {
  3388. if (c == quote)
  3389. quote = 0;
  3390. else if (c == '\\' && p + 1 < len)
  3391. p++;
  3392. } else if (c == '"' || c == '\'') {
  3393. quote = c;
  3394. } else if (c == '(') {
  3395. parens++;
  3396. } else if (c == ')') {
  3397. parens--;
  3398. }
  3399. p++;
  3400. }
  3401. if (parens) {
  3402. out[n++] = s[i++];
  3403. continue;
  3404. }
  3405. close = p - 1;
  3406. inner = i + 4;
  3407. while (inner < close && is_space(s[inner]))
  3408. inner++;
  3409. comma = inner;
  3410. parens = 0;
  3411. quote = 0;
  3412. while (comma < close) {
  3413. char c = s[comma];
  3414. if (quote) {
  3415. if (c == quote)
  3416. quote = 0;
  3417. } else if (c == '"' || c == '\'') {
  3418. quote = c;
  3419. } else if (c == '(') {
  3420. parens++;
  3421. } else if (c == ')' && parens > 0) {
  3422. parens--;
  3423. } else if (c == ',' && parens == 0) {
  3424. break;
  3425. }
  3426. comma++;
  3427. }
  3428. nlen = comma - inner;
  3429. while (nlen && is_space(s[inner + nlen - 1]))
  3430. nlen--;
  3431. fstart = comma < close ? comma + 1 : close;
  3432. while (fstart < close && is_space(s[fstart]))
  3433. fstart++;
  3434. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3435. unsigned int vi = css_var_find(d, s + inner, nlen);
  3436. if (vi < BR_MAX_VARS) {
  3437. const char *v = attr_ptr(d, d->var_val[vi]);
  3438. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3439. out + n,
  3440. cap - n, depth + 1);
  3441. n += vn;
  3442. } else if (fstart < close) {
  3443. unsigned int vn = css_expand_vars(d, s + fstart,
  3444. close - fstart,
  3445. out + n, cap - n,
  3446. depth + 1);
  3447. n += vn;
  3448. }
  3449. } else if (fstart < close) {
  3450. unsigned int vn = css_expand_vars(d, s + fstart,
  3451. close - fstart,
  3452. out + n, cap - n,
  3453. depth + 1);
  3454. n += vn;
  3455. }
  3456. i = p;
  3457. }
  3458. out[n] = '\0';
  3459. return n;
  3460. }
  3461. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3462. unsigned int i)
  3463. {
  3464. while (i < len && (is_space(s[i]) || s[i] == ','))
  3465. i++;
  3466. return i;
  3467. }
  3468. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3469. * argument). Returns 1 on success. */
  3470. static int parse_grid_track(const char *s, unsigned int len,
  3471. unsigned int *pp, unsigned short *val,
  3472. unsigned char *kind)
  3473. {
  3474. unsigned int i = grid_skip_sep(s, len, *pp);
  3475. unsigned int whole = 0, frac = 0, scale = 1;
  3476. int any = 0;
  3477. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3478. unsigned int j = i + 7;
  3479. while (j < len && s[j] != ',')
  3480. j++;
  3481. if (j >= len)
  3482. return 0;
  3483. i = j + 1;
  3484. while (i < len && is_space(s[i]))
  3485. i++;
  3486. }
  3487. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3488. int depth = 1;
  3489. i += 7;
  3490. while (i < len && depth) {
  3491. if (s[i] == '(')
  3492. depth++;
  3493. else if (s[i] == ')')
  3494. depth--;
  3495. i++;
  3496. }
  3497. *pp = i;
  3498. return 0;
  3499. }
  3500. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3501. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3502. i++;
  3503. any = 1;
  3504. }
  3505. if (i < len && s[i] == '.') {
  3506. i++;
  3507. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3508. if (scale < 1000) {
  3509. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3510. scale *= 10;
  3511. }
  3512. i++;
  3513. any = 1;
  3514. }
  3515. }
  3516. if (!any) {
  3517. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3518. *val = 1;
  3519. *kind = 3;
  3520. *pp = i + 4;
  3521. return 1;
  3522. }
  3523. return 0;
  3524. }
  3525. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3526. *val = (unsigned short)(whole ? whole : 1);
  3527. *kind = 3;
  3528. i += 2;
  3529. } else if (i < len && s[i] == '%') {
  3530. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3531. *kind = 2;
  3532. i++;
  3533. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3534. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3535. *kind = 1;
  3536. i += 3;
  3537. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3538. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3539. *kind = 1;
  3540. i += 2;
  3541. } else {
  3542. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3543. *kind = 1;
  3544. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3545. i += 2;
  3546. }
  3547. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3548. i++;
  3549. *pp = i;
  3550. return 1;
  3551. }
  3552. static int parse_grid_cols(const char *s, unsigned int len,
  3553. unsigned short val[2], unsigned char kind[2])
  3554. {
  3555. unsigned int pos = 0;
  3556. int n = 0;
  3557. while (n < 2) {
  3558. unsigned int before = pos;
  3559. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3560. break;
  3561. n++;
  3562. if (pos <= before)
  3563. break;
  3564. while (pos < len && is_space(s[pos]))
  3565. pos++;
  3566. if (pos >= len)
  3567. break;
  3568. }
  3569. return n;
  3570. }
  3571. static int parse_grid_slot(const char *s, unsigned int len)
  3572. {
  3573. if (contains_ci_len(s, len, "footer") ||
  3574. contains_ci_len(s, len, "header") ||
  3575. contains_ci_len(s, len, "sitenotice") ||
  3576. contains_ci_len(s, len, "navbar"))
  3577. return 1;
  3578. if (contains_ci_len(s, len, "columnstart") ||
  3579. contains_ci_len(s, len, "sidebar") ||
  3580. contains_ci_len(s, len, "left") ||
  3581. contains_ci_len(s, len, "start"))
  3582. return 2;
  3583. if (contains_ci_len(s, len, "pagecontent") ||
  3584. contains_ci_len(s, len, "content") ||
  3585. contains_ci_len(s, len, "main") ||
  3586. contains_ci_len(s, len, "right") ||
  3587. contains_ci_len(s, len, "end"))
  3588. return 3;
  3589. return 0;
  3590. }
  3591. static int css_has_var(const char *s, unsigned int len)
  3592. {
  3593. unsigned int i;
  3594. for (i = 0; i + 4 <= len; i++)
  3595. if ((s[i] == 'v' || s[i] == 'V') &&
  3596. ci_eq_n(s + i, "var(", 4))
  3597. return 1;
  3598. return 0;
  3599. }
  3600. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3601. unsigned int i)
  3602. {
  3603. i += 2;
  3604. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3605. i++;
  3606. if (i + 1 < len)
  3607. i += 2;
  3608. else
  3609. i = len;
  3610. return i;
  3611. }
  3612. static unsigned int css_skip_string(const char *s, unsigned int len,
  3613. unsigned int i)
  3614. {
  3615. char q = s[i++];
  3616. while (i < len && s[i] != q) {
  3617. if (s[i] == '\\' && i + 1 < len)
  3618. i++;
  3619. i++;
  3620. }
  3621. if (i < len)
  3622. i++;
  3623. return i;
  3624. }
  3625. /* Find the '}' that closes a declaration block. i points just past the
  3626. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3627. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3628. * selectors such as "Highlight-Stories {" without a closing brace). */
  3629. static unsigned int css_block_end(const char *s, unsigned int len,
  3630. unsigned int i)
  3631. {
  3632. int depth = 0;
  3633. while (i < len) {
  3634. char c = s[i];
  3635. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3636. i = css_skip_comment(s, len, i);
  3637. continue;
  3638. }
  3639. if (c == '"' || c == '\'') {
  3640. i = css_skip_string(s, len, i);
  3641. continue;
  3642. }
  3643. if (c == '{') {
  3644. depth++;
  3645. } else if (c == '}') {
  3646. if (depth == 0)
  3647. return i;
  3648. depth--;
  3649. }
  3650. i++;
  3651. }
  3652. return len;
  3653. }
  3654. /* Skip a block whose opening '{' has already been consumed. */
  3655. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3656. unsigned int i)
  3657. {
  3658. int depth = 1;
  3659. while (i < len && depth) {
  3660. char c = s[i];
  3661. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3662. i = css_skip_comment(s, len, i);
  3663. continue;
  3664. }
  3665. if (c == '"' || c == '\'') {
  3666. i = css_skip_string(s, len, i);
  3667. continue;
  3668. }
  3669. if (c == '{')
  3670. depth++;
  3671. else if (c == '}')
  3672. depth--;
  3673. i++;
  3674. }
  3675. return i;
  3676. }
  3677. /* ---- @font-face ------------------------------------------------------- */
  3678. static void font_trim(const char **sp, unsigned int *len)
  3679. {
  3680. const char *s = *sp;
  3681. unsigned int n = *len;
  3682. while (n && is_space(*s)) {
  3683. s++;
  3684. n--;
  3685. }
  3686. while (n && is_space(s[n - 1]))
  3687. n--;
  3688. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3689. s++;
  3690. n -= 2;
  3691. }
  3692. *sp = s;
  3693. *len = n;
  3694. }
  3695. static void font_add(const char *family, unsigned int flen,
  3696. const char *url, unsigned int ulen)
  3697. {
  3698. unsigned int i, n = 0, k;
  3699. font_trim(&family, &flen);
  3700. font_trim(&url, &ulen);
  3701. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3702. return;
  3703. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3704. struct br_font_def *f = &font_defs[i];
  3705. if (s_len(f->family) != flen)
  3706. continue;
  3707. for (k = 0; k < flen; k++) {
  3708. char a = f->family[k], b = family[k];
  3709. if (a >= 'A' && a <= 'Z')
  3710. a = (char)(a - 'A' + 'a');
  3711. if (b >= 'A' && b <= 'Z')
  3712. b = (char)(b - 'A' + 'a');
  3713. if (a != b)
  3714. break;
  3715. }
  3716. if (k != flen)
  3717. continue;
  3718. n = 0;
  3719. while (url[n] && n + 1 < sizeof(f->url)) {
  3720. f->url[n] = url[n];
  3721. n++;
  3722. }
  3723. f->url[n] = '\0';
  3724. n = 0;
  3725. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3726. f->base[n] = font_base[n];
  3727. n++;
  3728. }
  3729. f->base[n] = '\0';
  3730. return;
  3731. }
  3732. if (font_face_count >= BR_MAX_FACES)
  3733. return;
  3734. {
  3735. struct br_font_def *f = &font_defs[font_face_count];
  3736. for (n = 0; n < flen; n++)
  3737. f->family[n] = family[n];
  3738. f->family[n] = '\0';
  3739. for (n = 0; n < ulen; n++)
  3740. f->url[n] = url[n];
  3741. f->url[n] = '\0';
  3742. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  3743. f->base[n] = font_base[n];
  3744. f->base[n] = '\0';
  3745. f->used = 1;
  3746. font_face_count++;
  3747. }
  3748. }
  3749. /* Look up the first name in a font-family list that has an @font-face. */
  3750. static int font_family_id(const char *s, unsigned int len)
  3751. {
  3752. unsigned int i = 0;
  3753. while (i < len) {
  3754. unsigned int start, end, q = 0;
  3755. while (i < len && (is_space(s[i]) || s[i] == ','))
  3756. i++;
  3757. if (i >= len)
  3758. break;
  3759. if (s[i] == '"' || s[i] == '\'') {
  3760. char quote = s[i++];
  3761. start = i;
  3762. while (i < len && s[i] != quote)
  3763. i++;
  3764. end = i;
  3765. if (i < len)
  3766. i++;
  3767. } else {
  3768. start = i;
  3769. while (i < len && s[i] != ',')
  3770. i++;
  3771. end = i;
  3772. while (end > start && is_space(s[end - 1]))
  3773. end--;
  3774. }
  3775. if (end > start) {
  3776. for (q = 0; q < (unsigned int)font_face_count; q++) {
  3777. const char *f = font_defs[q].family;
  3778. unsigned int fl = s_len(f), k;
  3779. if (fl != end - start)
  3780. continue;
  3781. for (k = 0; k < fl; k++) {
  3782. char a = f[k], b = s[start + k];
  3783. if (a >= 'A' && a <= 'Z')
  3784. a = (char)(a - 'A' + 'a');
  3785. if (b >= 'A' && b <= 'Z')
  3786. b = (char)(b - 'A' + 'a');
  3787. if (a != b)
  3788. break;
  3789. }
  3790. if (k == fl)
  3791. return (int)q + 1;
  3792. }
  3793. }
  3794. }
  3795. return 0;
  3796. }
  3797. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  3798. * at-rules, so faces must be registered before any font-family declaration
  3799. * in the same sheet is resolved. */
  3800. static void scan_font_faces(const char *s, unsigned int len)
  3801. {
  3802. unsigned int i = 0;
  3803. while (i + 10 <= len) {
  3804. unsigned int j, end;
  3805. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  3806. unsigned int flen = 0, ulen = 0, p;
  3807. int have_family = 0, have_url = 0;
  3808. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  3809. i++;
  3810. continue;
  3811. }
  3812. j = i + 10;
  3813. while (j < len && s[j] != '{' && s[j] != ';')
  3814. j++;
  3815. if (j >= len || s[j] == ';') {
  3816. i = j + 1;
  3817. continue;
  3818. }
  3819. end = css_skip_nested(s, len, j + 1);
  3820. p = j + 1;
  3821. while (p < end) {
  3822. unsigned int ds, de, vs, ve;
  3823. while (p < end && (is_space(s[p]) || s[p] == ';'))
  3824. p++;
  3825. if (p >= end)
  3826. break;
  3827. ds = p;
  3828. while (p < end && s[p] != ':' && s[p] != ';')
  3829. p++;
  3830. de = p;
  3831. if (p >= end || s[p] != ':') {
  3832. while (p < end && s[p] != ';')
  3833. p++;
  3834. continue;
  3835. }
  3836. p++;
  3837. vs = p;
  3838. while (p < end && s[p] != ';')
  3839. p++;
  3840. ve = p;
  3841. while (ds < de && is_space(s[ds]))
  3842. ds++;
  3843. while (de > ds && is_space(s[de - 1]))
  3844. de--;
  3845. while (vs < ve && is_space(s[vs]))
  3846. vs++;
  3847. while (ve > vs && is_space(s[ve - 1]))
  3848. ve--;
  3849. if (de - ds == 11 &&
  3850. ci_eq_n(s + ds, "font-family", 11)) {
  3851. const char *fp = s + vs;
  3852. unsigned int fn = ve - vs;
  3853. font_trim(&fp, &fn);
  3854. if (fn && fn < sizeof(family)) {
  3855. unsigned int k;
  3856. for (k = 0; k < fn; k++)
  3857. family[k] = fp[k];
  3858. family[fn] = '\0';
  3859. flen = fn;
  3860. have_family = 1;
  3861. }
  3862. } else if (de - ds == 3 &&
  3863. ci_eq_n(s + ds, "src", 3)) {
  3864. unsigned int q = vs;
  3865. while (q + 4 <= ve) {
  3866. unsigned int us, ue;
  3867. if (!ci_eq_n(s + q, "url(", 4)) {
  3868. q++;
  3869. continue;
  3870. }
  3871. q += 4;
  3872. us = q;
  3873. while (q < ve && s[q] != ')')
  3874. q++;
  3875. ue = q;
  3876. {
  3877. const char *up = s + us;
  3878. unsigned int un = ue - us;
  3879. font_trim(&up, &un);
  3880. if (un && un < sizeof(url) &&
  3881. !contains_ci_len(up, un,
  3882. "woff")) {
  3883. unsigned int k;
  3884. for (k = 0; k < un; k++)
  3885. url[k] = up[k];
  3886. url[un] = '\0';
  3887. ulen = un;
  3888. have_url = 1;
  3889. break;
  3890. }
  3891. }
  3892. }
  3893. }
  3894. }
  3895. if (have_family && have_url)
  3896. font_add(family, flen, url, ulen);
  3897. i = end;
  3898. }
  3899. }
  3900. /* One border-radius length. Unlike parse_size(), percentages keep their
  3901. * kind: the drawer scales them to the box, not the viewport. */
  3902. static int parse_radius_value(const char *s, unsigned int len,
  3903. unsigned int *pos, unsigned short *val,
  3904. unsigned char *kind)
  3905. {
  3906. unsigned int i = *pos;
  3907. unsigned long v = 0;
  3908. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  3909. while (i < len && is_space(s[i]))
  3910. i++;
  3911. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3912. v = v * 10 + (unsigned int)(s[i] - '0');
  3913. digits = 1;
  3914. i++;
  3915. }
  3916. if (i < len && s[i] == '.') {
  3917. i++;
  3918. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3919. fdigits = 1;
  3920. if (frac < 2) {
  3921. v = v * 10 + (unsigned int)(s[i] - '0');
  3922. frac++;
  3923. }
  3924. i++;
  3925. }
  3926. }
  3927. while (frac < 2) {
  3928. v *= 10;
  3929. frac++;
  3930. }
  3931. while (i < len && is_space(s[i]))
  3932. i++;
  3933. if (i < len && s[i] == '%') {
  3934. pct = 1;
  3935. i++;
  3936. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  3937. s[i + 2] == 'm') {
  3938. rem = 1;
  3939. i += 3;
  3940. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  3941. rem = 2;
  3942. i += 2;
  3943. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  3944. i += 2;
  3945. } else if (i < len && is_ident(s[i])) {
  3946. return 0;
  3947. }
  3948. if (!digits && !fdigits)
  3949. return 0;
  3950. if (rem == 1)
  3951. v *= root_font_px;
  3952. else if (rem == 2)
  3953. v *= root_font_px;
  3954. v /= 100;
  3955. if (v > 2048)
  3956. v = 2048;
  3957. *val = (unsigned short)v;
  3958. *kind = pct ? 2 : 1;
  3959. *pos = i;
  3960. return 1;
  3961. }
  3962. /* font: [style] [variant] [weight] size[/line-height] family */
  3963. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  3964. const char *v, unsigned int len, int important)
  3965. {
  3966. unsigned int pos = 0, ts, te;
  3967. unsigned short size_raw = 0;
  3968. unsigned char size_unit = 0;
  3969. int have_size = 0;
  3970. int bold = 0, italic = 0;
  3971. int lh_px = 0, lh_pct = 0, mono = 0;
  3972. while (next_token(v, len, &pos, &ts, &te)) {
  3973. unsigned int tl = te - ts;
  3974. unsigned int slash = 0;
  3975. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  3976. bold = 1;
  3977. continue;
  3978. }
  3979. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  3980. continue;
  3981. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  3982. italic = 1;
  3983. continue;
  3984. }
  3985. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  3986. italic = 1;
  3987. continue;
  3988. }
  3989. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  3990. continue;
  3991. for (slash = 0; slash < tl; slash++)
  3992. if (v[ts + slash] == '/')
  3993. break;
  3994. if (slash == tl) {
  3995. unsigned int q;
  3996. int weight = 0;
  3997. for (q = 0; q < tl; q++) {
  3998. if (v[ts + q] < '0' || v[ts + q] > '9')
  3999. break;
  4000. weight = weight * 10 + (v[ts + q] - '0');
  4001. }
  4002. if (q == tl && weight >= 100 && weight <= 900) {
  4003. bold = weight >= 500;
  4004. continue;
  4005. }
  4006. }
  4007. {
  4008. unsigned short raw;
  4009. unsigned char unit;
  4010. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4011. &raw, &unit))
  4012. continue;
  4013. size_raw = raw;
  4014. size_unit = unit;
  4015. }
  4016. have_size = 1;
  4017. if (slash < tl) {
  4018. unsigned int lstart = ts + slash + 1;
  4019. unsigned short lhp = 0;
  4020. unsigned char lhpct = 0;
  4021. if (!parse_line_height(v + lstart, te - lstart,
  4022. &lhp, &lhpct)) {
  4023. lh_px = 0;
  4024. lh_pct = 0;
  4025. } else {
  4026. lh_px = lhp;
  4027. lh_pct = lhpct;
  4028. }
  4029. }
  4030. }
  4031. if (!have_size)
  4032. return;
  4033. mono = contains_ci_len(v, len, "mono");
  4034. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4035. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4036. decl_add(out, nout, PROP_BOLD, bold, important);
  4037. decl_add(out, nout, PROP_ITALIC, italic, important);
  4038. decl_add(out, nout, PROP_MONO, mono, important);
  4039. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  4040. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4041. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4042. }
  4043. static void parse_decls(struct br_doc *d, const char *s,
  4044. unsigned int len, struct br_decl *out,
  4045. unsigned int *nout)
  4046. {
  4047. static char expanded[4096];
  4048. if (css_has_var(s, len)) {
  4049. unsigned int elen = css_expand_vars(d, s, len, expanded,
  4050. sizeof(expanded), 0);
  4051. if (elen) {
  4052. s = expanded;
  4053. len = elen;
  4054. }
  4055. }
  4056. unsigned int i = 0;
  4057. *nout = 0;
  4058. while (i < len && *nout < BR_DECLS) {
  4059. unsigned int pstart, pend, vstart, vend, k;
  4060. char prop[24];
  4061. int important = 0;
  4062. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4063. i++;
  4064. if (i >= len)
  4065. break;
  4066. /* A nested at-rule (@media/@supports) inside the declaration
  4067. * block carries its own selector context; skip it rather than
  4068. * reading its prelude as a declaration. */
  4069. if (s[i] == '@') {
  4070. while (i < len && s[i] != '{' && s[i] != ';')
  4071. i++;
  4072. if (i < len && s[i] == '{')
  4073. i = css_skip_nested(s, len, i + 1);
  4074. else if (i < len)
  4075. i++;
  4076. continue;
  4077. }
  4078. pstart = i;
  4079. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4080. i++;
  4081. if (i < len && s[i] == '{') {
  4082. /* Nested style rule: skip its block. */
  4083. i = css_skip_nested(s, len, i + 1);
  4084. continue;
  4085. }
  4086. pend = i;
  4087. if (i >= len || s[i] != ':')
  4088. continue;
  4089. i++;
  4090. vstart = i;
  4091. while (i < len && s[i] != ';')
  4092. i++;
  4093. vend = i;
  4094. while (pstart < pend && is_space(s[pstart]))
  4095. pstart++;
  4096. while (pend > pstart && is_space(s[pend - 1]))
  4097. pend--;
  4098. while (vstart < vend && is_space(s[vstart]))
  4099. vstart++;
  4100. while (vend > vstart && is_space(s[vend - 1]))
  4101. vend--;
  4102. if (vend - vstart >= 10 &&
  4103. ci_eq_n(s + vend - 10, "!important", 10)) {
  4104. important = 1;
  4105. vend -= 10;
  4106. while (vend > vstart && is_space(s[vend - 1]))
  4107. vend--;
  4108. }
  4109. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4110. char c = s[pstart + k];
  4111. if (c >= 'A' && c <= 'Z')
  4112. c = (char)(c - 'A' + 'a');
  4113. prop[k] = c;
  4114. }
  4115. prop[k] = '\0';
  4116. if (str_eq(prop, "color")) {
  4117. unsigned short c;
  4118. if (parse_color(s + vstart, vend - vstart, &c))
  4119. decl_add(out, nout, PROP_COLOR, c, important);
  4120. } else if (str_eq(prop, "fill")) {
  4121. unsigned short c;
  4122. if (parse_color(s + vstart, vend - vstart, &c)) {
  4123. decl_add(out, nout, PROP_BG, c, important);
  4124. decl_add(out, nout, PROP_COLOR, c, important);
  4125. }
  4126. } else if (str_eq(prop, "stroke")) {
  4127. unsigned short c;
  4128. if (parse_color(s + vstart, vend - vstart, &c)) {
  4129. decl_add(out, nout, PROP_BORDER_COLOR, c,
  4130. important);
  4131. decl_add(out, nout, PROP_BORDER_W, 1, important);
  4132. }
  4133. } else if (str_eq(prop, "stroke-width")) {
  4134. unsigned int j = vstart;
  4135. int sw = 0;
  4136. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  4137. sw = sw * 10 + (s[j] - '0');
  4138. j++;
  4139. }
  4140. if (sw < 1)
  4141. sw = 1;
  4142. if (sw > 3)
  4143. sw = 3;
  4144. decl_add(out, nout, PROP_BORDER_W, sw, important);
  4145. } else if (str_eq(prop, "text-anchor")) {
  4146. int a = BR_ALIGN_LEFT;
  4147. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4148. a = BR_ALIGN_CENTER;
  4149. else if (contains_ci_len(s + vstart, vend - vstart,
  4150. "end"))
  4151. a = BR_ALIGN_RIGHT;
  4152. decl_add(out, nout, PROP_ALIGN, a, important);
  4153. } else if (str_eq(prop, "background-image")) {
  4154. add_bg_image_decl(out, nout, s + vstart,
  4155. vend - vstart, important);
  4156. } else if (str_eq(prop, "background-repeat")) {
  4157. int r = 0;
  4158. if (contains_ci_len(s + vstart, vend - vstart,
  4159. "no-repeat"))
  4160. r = 1;
  4161. else if (contains_ci_len(s + vstart, vend - vstart,
  4162. "repeat-x"))
  4163. r = 2;
  4164. else if (contains_ci_len(s + vstart, vend - vstart,
  4165. "repeat-y"))
  4166. r = 3;
  4167. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  4168. } else if (str_eq(prop, "background-position")) {
  4169. add_bg_position(out, nout, s + vstart, vend - vstart,
  4170. important);
  4171. } else if (str_eq(prop, "background-size")) {
  4172. add_bg_size(out, nout, s + vstart, vend - vstart,
  4173. important);
  4174. } else if (str_eq(prop, "background-color") ||
  4175. str_eq(prop, "background")) {
  4176. unsigned short c;
  4177. int is_bg = str_eq(prop, "background");
  4178. if (is_bg) {
  4179. add_bg_image_decl(out, nout, s + vstart,
  4180. vend - vstart, important);
  4181. add_bg_shorthand_extra(out, nout,
  4182. s + vstart,
  4183. vend - vstart,
  4184. important);
  4185. if (contains_ci_len(s + vstart, vend - vstart,
  4186. "no-repeat"))
  4187. decl_add(out, nout, PROP_BG_REPEAT, 1,
  4188. important);
  4189. else if (contains_ci_len(s + vstart,
  4190. vend - vstart,
  4191. "repeat-x"))
  4192. decl_add(out, nout, PROP_BG_REPEAT, 2,
  4193. important);
  4194. else if (contains_ci_len(s + vstart,
  4195. vend - vstart,
  4196. "repeat-y"))
  4197. decl_add(out, nout, PROP_BG_REPEAT, 3,
  4198. important);
  4199. }
  4200. if (parse_color(s + vstart, vend - vstart, &c))
  4201. decl_add(out, nout, PROP_BG, c, important);
  4202. else if (is_bg &&
  4203. find_color_last(s + vstart, vend - vstart, &c))
  4204. decl_add(out, nout, PROP_BG, c, important);
  4205. } else if (str_eq(prop, "font-size")) {
  4206. unsigned short raw;
  4207. unsigned char unit;
  4208. if (parse_font_size(s + vstart, vend - vstart,
  4209. &raw, &unit)) {
  4210. decl_add(out, nout, PROP_FONT_PX, raw,
  4211. important);
  4212. decl_add(out, nout, PROP_FONT_UNIT, unit,
  4213. important);
  4214. }
  4215. } else if (str_eq(prop, "font-weight")) {
  4216. int bold = 0;
  4217. unsigned int j;
  4218. if (vend - vstart >= 4 &&
  4219. ci_eq_n(s + vstart, "bold", 4))
  4220. bold = 1;
  4221. else {
  4222. int w = 0;
  4223. for (j = vstart; j < vend; j++)
  4224. if (s[j] >= '0' && s[j] <= '9')
  4225. w = w * 10 + (s[j] - '0');
  4226. if (w >= 500)
  4227. bold = 1;
  4228. }
  4229. decl_add(out, nout, PROP_BOLD, bold, important);
  4230. } else if (str_eq(prop, "font-style")) {
  4231. int italic = (vend - vstart >= 6 &&
  4232. ci_eq_n(s + vstart, "italic", 6));
  4233. decl_add(out, nout, PROP_ITALIC, italic, important);
  4234. } else if (str_eq(prop, "font-family")) {
  4235. unsigned int j;
  4236. int mono = 0;
  4237. int face = font_family_id(s + vstart, vend - vstart);
  4238. for (j = vstart; j + 4 <= vend; j++)
  4239. if (ci_eq_n(s + j, "mono", 4))
  4240. mono = 1;
  4241. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4242. decl_add(out, nout, PROP_MONO, mono, important);
  4243. } else if (str_eq(prop, "font")) {
  4244. add_font_shorthand(out, nout, s + vstart,
  4245. vend - vstart, important);
  4246. } else if (str_eq(prop, "text-align")) {
  4247. int a = BR_ALIGN_LEFT;
  4248. if (vend - vstart >= 6 &&
  4249. ci_eq_n(s + vstart, "center", 6))
  4250. a = BR_ALIGN_CENTER;
  4251. else if (vend - vstart >= 5 &&
  4252. ci_eq_n(s + vstart, "right", 5))
  4253. a = BR_ALIGN_RIGHT;
  4254. decl_add(out, nout, PROP_ALIGN, a, important);
  4255. } else if (str_eq(prop, "text-decoration")) {
  4256. int u = 0, stv = 0, ov = 0;
  4257. if (contains_ci_len(s + vstart, vend - vstart,
  4258. "underline"))
  4259. u = 1;
  4260. if (contains_ci_len(s + vstart, vend - vstart,
  4261. "line-through"))
  4262. stv = 1;
  4263. if (contains_ci_len(s + vstart, vend - vstart,
  4264. "overline"))
  4265. ov = 1;
  4266. decl_add(out, nout, PROP_UNDERLINE, u, important);
  4267. decl_add(out, nout, PROP_STRIKE, stv, important);
  4268. decl_add(out, nout, PROP_OVERLINE, ov, important);
  4269. } else if (str_eq(prop, "text-transform")) {
  4270. int u = (vend - vstart >= 9 &&
  4271. ci_eq_n(s + vstart, "uppercase", 9));
  4272. decl_add(out, nout, PROP_UPPER, u, important);
  4273. } else if (str_eq(prop, "white-space")) {
  4274. int nw = contains_ci_len(s + vstart, vend - vstart,
  4275. "nowrap") ||
  4276. contains_ci_len(s + vstart, vend - vstart,
  4277. "pre");
  4278. decl_add(out, nout, PROP_NOWRAP, nw, important);
  4279. } else if (str_eq(prop, "display")) {
  4280. decl_add(out, nout, PROP_DISPLAY,
  4281. parse_display(s + vstart, vend - vstart),
  4282. important);
  4283. } else if (str_eq(prop, "visibility")) {
  4284. int h = contains_ci_len(s + vstart, vend - vstart,
  4285. "hidden") ||
  4286. contains_ci_len(s + vstart, vend - vstart,
  4287. "collapse");
  4288. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  4289. } else if (str_eq(prop, "opacity")) {
  4290. unsigned int q;
  4291. int h = 1, any = 0;
  4292. for (q = vstart; q < vend; q++) {
  4293. if (s[q] >= '1' && s[q] <= '9') {
  4294. h = 0;
  4295. break;
  4296. }
  4297. if (s[q] >= '0' && s[q] <= '9')
  4298. any = 1;
  4299. }
  4300. if (!any || contains_ci_len(s + vstart, vend - vstart,
  4301. "inherit"))
  4302. h = 0;
  4303. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  4304. } else if (str_eq(prop, "overflow") ||
  4305. str_eq(prop, "overflow-x") ||
  4306. str_eq(prop, "overflow-y")) {
  4307. int h = contains_ci_len(s + vstart, vend - vstart,
  4308. "hidden") ||
  4309. contains_ci_len(s + vstart, vend - vstart,
  4310. "clip");
  4311. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  4312. } else if (str_eq(prop, "float")) {
  4313. int f = 0;
  4314. if (vend - vstart >= 4 &&
  4315. ci_eq_n(s + vstart, "left", 4))
  4316. f = 1;
  4317. else if (vend - vstart >= 5 &&
  4318. ci_eq_n(s + vstart, "right", 5))
  4319. f = 2;
  4320. decl_add(out, nout, PROP_FLOAT, f, important);
  4321. } else if (str_eq(prop, "clear")) {
  4322. int c = 0;
  4323. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  4324. c = 3;
  4325. else if (contains_ci_len(s + vstart, vend - vstart,
  4326. "left"))
  4327. c = 1;
  4328. else if (contains_ci_len(s + vstart, vend - vstart,
  4329. "right"))
  4330. c = 2;
  4331. decl_add(out, nout, PROP_CLEAR, c, important);
  4332. } else if (str_eq(prop, "flex-grow") ||
  4333. str_eq(prop, "flex")) {
  4334. unsigned int q = vstart;
  4335. int g = 0;
  4336. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  4337. q++;
  4338. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4339. g = g * 10 + (s[q] - '0');
  4340. q++;
  4341. }
  4342. if (g > 100)
  4343. g = 100;
  4344. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  4345. } else if (str_eq(prop, "flex-direction")) {
  4346. decl_add(out, nout, PROP_FLEX_COL,
  4347. contains_ci_len(s + vstart, vend - vstart,
  4348. "column"), important);
  4349. } else if (str_eq(prop, "flex-wrap")) {
  4350. int wrap = 0;
  4351. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  4352. wrap = 2;
  4353. else if (contains_ci_len(s + vstart, vend - vstart,
  4354. "wrap"))
  4355. wrap = 1;
  4356. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  4357. } else if (str_eq(prop, "justify-content")) {
  4358. int j = 0;
  4359. if (contains_ci_len(s + vstart, vend - vstart,
  4360. "space-between"))
  4361. j = 3;
  4362. else if (contains_ci_len(s + vstart, vend - vstart,
  4363. "space-around"))
  4364. j = 4;
  4365. else if (contains_ci_len(s + vstart, vend - vstart,
  4366. "space-evenly"))
  4367. j = 5;
  4368. else if (contains_ci_len(s + vstart, vend - vstart,
  4369. "center"))
  4370. j = 1;
  4371. else if (contains_ci_len(s + vstart, vend - vstart,
  4372. "flex-end") ||
  4373. contains_ci_len(s + vstart, vend - vstart,
  4374. "end"))
  4375. j = 2;
  4376. decl_add(out, nout, PROP_JUSTIFY, j, important);
  4377. } else if (str_eq(prop, "align-items")) {
  4378. int a = 0;
  4379. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  4380. a = 1;
  4381. else if (contains_ci_len(s + vstart, vend - vstart,
  4382. "flex-end") ||
  4383. contains_ci_len(s + vstart, vend - vstart,
  4384. "end"))
  4385. a = 2;
  4386. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  4387. } else if (str_eq(prop, "position")) {
  4388. int p = 0;
  4389. if (contains_ci_len(s + vstart, vend - vstart,
  4390. "absolute"))
  4391. p = 1;
  4392. else if (contains_ci_len(s + vstart, vend - vstart,
  4393. "fixed"))
  4394. p = 2;
  4395. else if (contains_ci_len(s + vstart, vend - vstart,
  4396. "relative"))
  4397. p = 3;
  4398. else if (contains_ci_len(s + vstart, vend - vstart,
  4399. "sticky"))
  4400. p = 4;
  4401. decl_add(out, nout, PROP_POS, p, important);
  4402. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  4403. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  4404. int which = str_eq(prop, "left") ? 0 :
  4405. str_eq(prop, "top") ? 1 :
  4406. str_eq(prop, "right") ? 2 : 3;
  4407. unsigned int pos = 0;
  4408. if (!(vend - vstart >= 4 &&
  4409. ci_eq_n(s + vstart, "auto", 4))) {
  4410. int v = parse_len(s + vstart, vend - vstart, &pos);
  4411. if (v > 32000)
  4412. v = 32000;
  4413. else if (v < -32000)
  4414. v = -32000;
  4415. decl_add(out, nout,
  4416. (unsigned char)(which == 0 ? PROP_LEFT :
  4417. which == 1 ? PROP_TOP :
  4418. which == 2 ? PROP_RIGHT :
  4419. PROP_BOTTOM),
  4420. v, important);
  4421. decl_add(out, nout,
  4422. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  4423. which == 1 ? PROP_TOP_SET :
  4424. which == 2 ? PROP_RIGHT_SET :
  4425. PROP_BOTTOM_SET),
  4426. 1, important);
  4427. }
  4428. } else if (str_eq(prop, "z-index")) {
  4429. unsigned int q = vstart;
  4430. int sign = 1;
  4431. int v = 0;
  4432. int any = 0;
  4433. if (q < vend && s[q] == '-') {
  4434. sign = -1;
  4435. q++;
  4436. }
  4437. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4438. v = v * 10 + (s[q] - '0');
  4439. any = 1;
  4440. q++;
  4441. }
  4442. if (any) {
  4443. v *= sign;
  4444. if (v > 32000)
  4445. v = 32000;
  4446. else if (v < -32000)
  4447. v = -32000;
  4448. decl_add(out, nout, PROP_ZINDEX, v, important);
  4449. decl_add(out, nout, PROP_ZSET, 1, important);
  4450. } else if (vend - vstart >= 4 &&
  4451. ci_eq_n(s + vstart, "auto", 4)) {
  4452. decl_add(out, nout, PROP_ZSET, 0, important);
  4453. }
  4454. } else if (str_eq(prop, "transform")) {
  4455. const char *v = s + vstart;
  4456. unsigned int vl = vend - vstart;
  4457. int tx = 0, ty = 0;
  4458. int is_x = contains_ci_len(v, vl, "translatex");
  4459. int is_y = contains_ci_len(v, vl, "translatey");
  4460. unsigned int off = 0, arg;
  4461. if (contains_ci_len(v, vl, "translate")) {
  4462. while (off < vl && v[off] != '(')
  4463. off++;
  4464. if (off < vl)
  4465. off++;
  4466. if (!is_y)
  4467. tx = parse_len(v, vl, &off);
  4468. if (!is_x) {
  4469. while (off < vl &&
  4470. (is_space(v[off]) || v[off] == ','))
  4471. off++;
  4472. ty = parse_len(v, vl, &off);
  4473. }
  4474. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  4475. important);
  4476. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  4477. important);
  4478. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  4479. important);
  4480. }
  4481. if (find_fn(v, vl, "scale", &arg)) {
  4482. unsigned int q = arg;
  4483. signed short sx = 100, sy = 100;
  4484. if (parse_fixed_100(v, vl, &q, &sx)) {
  4485. unsigned int r = q;
  4486. while (r < vl &&
  4487. (is_space(v[r]) || v[r] == ','))
  4488. r++;
  4489. if (!parse_fixed_100(v, vl, &r, &sy))
  4490. sy = sx;
  4491. decl_add(out, nout, PROP_SCALE_X, sx,
  4492. important);
  4493. decl_add(out, nout, PROP_SCALE_Y, sy,
  4494. important);
  4495. decl_add(out, nout,
  4496. PROP_TRANSFORM_PAINT, 1,
  4497. important);
  4498. decl_add(out, nout,
  4499. PROP_TRANSFORM_SET, 1,
  4500. important);
  4501. }
  4502. }
  4503. if (find_fn(v, vl, "rotate", &arg)) {
  4504. unsigned int q = arg;
  4505. signed short deg;
  4506. if (parse_fixed_100(v, vl, &q, &deg)) {
  4507. decl_add(out, nout, PROP_ROTATE, deg,
  4508. important);
  4509. decl_add(out, nout,
  4510. PROP_TRANSFORM_PAINT, 1,
  4511. important);
  4512. decl_add(out, nout,
  4513. PROP_TRANSFORM_SET, 1,
  4514. important);
  4515. }
  4516. }
  4517. } else if (str_eq(prop, "transform-origin")) {
  4518. add_transform_origin(out, nout, s + vstart,
  4519. vend - vstart, important);
  4520. } else if (str_eq(prop, "grid-template-columns") ||
  4521. str_eq(prop, "grid-template")) {
  4522. const char *v = s + vstart;
  4523. unsigned int vl = vend - vstart;
  4524. unsigned short val[2];
  4525. unsigned char kind[2];
  4526. int n, q;
  4527. if (str_eq(prop, "grid-template")) {
  4528. for (q = 0; q < (int)vl; q++)
  4529. if (v[q] == '/') {
  4530. q++;
  4531. while (q < (int)vl && is_space(v[q]))
  4532. q++;
  4533. v += q;
  4534. vl -= (unsigned int)q;
  4535. break;
  4536. }
  4537. }
  4538. if (contains_ci_len(v, vl, "auto-fit") ||
  4539. contains_ci_len(v, vl, "auto-fill")) {
  4540. unsigned int mp = 0;
  4541. int found = 0;
  4542. unsigned short mw = 200;
  4543. unsigned char mk = 1;
  4544. for (q = 0; q + 7 <= (int)vl; q++)
  4545. if (ci_eq_n(v + q, "minmax(", 7)) {
  4546. mp = (unsigned int)q + 7;
  4547. found = 1;
  4548. break;
  4549. }
  4550. if (found)
  4551. parse_grid_track(v, vl, &mp, &mw, &mk);
  4552. if (mk != 1 || mw < 80)
  4553. mw = 200;
  4554. if (mw > 480)
  4555. mw = 480;
  4556. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  4557. decl_add(out, nout, PROP_WIDTH, mw, important);
  4558. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  4559. } else {
  4560. n = parse_grid_cols(v, vl, val, kind);
  4561. if (n > 0) {
  4562. decl_add(out, nout, PROP_WIDTH, val[0],
  4563. important);
  4564. decl_add(out, nout, PROP_WIDTH_KIND,
  4565. kind[0], important);
  4566. decl_add(out, nout, PROP_GRID_NCOLS, n,
  4567. important);
  4568. }
  4569. if (n > 1) {
  4570. decl_add(out, nout, PROP_MAXW, val[1],
  4571. important);
  4572. decl_add(out, nout, PROP_MAXW_KIND,
  4573. kind[1], important);
  4574. }
  4575. }
  4576. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  4577. unsigned int gp = 0;
  4578. unsigned short gv = 0;
  4579. unsigned char gk = 1;
  4580. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  4581. if (gv > 120)
  4582. gv = 120;
  4583. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  4584. }
  4585. } else if (str_eq(prop, "grid-area")) {
  4586. int slot = parse_grid_slot(s + vstart, vend - vstart);
  4587. if (slot)
  4588. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  4589. } else if (str_eq(prop, "width") ||
  4590. str_eq(prop, "max-width") ||
  4591. str_eq(prop, "min-width") ||
  4592. str_eq(prop, "height") ||
  4593. str_eq(prop, "max-height") ||
  4594. str_eq(prop, "min-height")) {
  4595. unsigned short v;
  4596. unsigned char kind;
  4597. if (vend - vstart >= 5 &&
  4598. ci_eq_n(s + vstart, "calc(", 5)) {
  4599. int pct, px;
  4600. if (parse_calc(s + vstart + 5,
  4601. vend - vstart - 5, &pct, &px)) {
  4602. if (str_eq(prop, "width")) {
  4603. decl_add(out, nout, PROP_WIDTH,
  4604. 0, important);
  4605. decl_add(out, nout,
  4606. PROP_WIDTH_KIND, 3,
  4607. important);
  4608. decl_add(out, nout,
  4609. PROP_WIDTH_CALC_PCT,
  4610. pct, important);
  4611. decl_add(out, nout,
  4612. PROP_WIDTH_CALC_PX,
  4613. px, important);
  4614. } else if (str_eq(prop, "max-width")) {
  4615. decl_add(out, nout, PROP_MAXW,
  4616. 0, important);
  4617. decl_add(out, nout,
  4618. PROP_MAXW_KIND, 3,
  4619. important);
  4620. decl_add(out, nout,
  4621. PROP_MAXW_CALC_PCT,
  4622. pct, important);
  4623. decl_add(out, nout,
  4624. PROP_MAXW_CALC_PX,
  4625. px, important);
  4626. } else if (str_eq(prop, "min-width")) {
  4627. decl_add(out, nout, PROP_MINW,
  4628. 0, important);
  4629. decl_add(out, nout,
  4630. PROP_MINW_KIND, 3,
  4631. important);
  4632. decl_add(out, nout,
  4633. PROP_MINW_CALC_PCT,
  4634. pct, important);
  4635. decl_add(out, nout,
  4636. PROP_MINW_CALC_PX,
  4637. px, important);
  4638. } else if (str_eq(prop, "height")) {
  4639. decl_add(out, nout, PROP_HEIGHT,
  4640. 0, important);
  4641. decl_add(out, nout,
  4642. PROP_HEIGHT_KIND, 3,
  4643. important);
  4644. decl_add(out, nout,
  4645. PROP_HEIGHT_CALC_PCT,
  4646. pct, important);
  4647. decl_add(out, nout,
  4648. PROP_HEIGHT_CALC_PX,
  4649. px, important);
  4650. } else if (str_eq(prop, "max-height")) {
  4651. decl_add(out, nout, PROP_MAXH,
  4652. 0, important);
  4653. decl_add(out, nout,
  4654. PROP_MAXH_KIND, 3,
  4655. important);
  4656. decl_add(out, nout,
  4657. PROP_MAXH_CALC_PCT,
  4658. pct, important);
  4659. decl_add(out, nout,
  4660. PROP_MAXH_CALC_PX,
  4661. px, important);
  4662. } else {
  4663. decl_add(out, nout, PROP_MINH,
  4664. 0, important);
  4665. decl_add(out, nout,
  4666. PROP_MINH_KIND, 3,
  4667. important);
  4668. decl_add(out, nout,
  4669. PROP_MINH_CALC_PCT,
  4670. pct, important);
  4671. decl_add(out, nout,
  4672. PROP_MINH_CALC_PX,
  4673. px, important);
  4674. }
  4675. }
  4676. continue;
  4677. }
  4678. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  4679. if (str_eq(prop, "width")) {
  4680. decl_add(out, nout, PROP_WIDTH, v, important);
  4681. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  4682. important);
  4683. } else if (str_eq(prop, "max-width")) {
  4684. decl_add(out, nout, PROP_MAXW, v, important);
  4685. decl_add(out, nout, PROP_MAXW_KIND, kind,
  4686. important);
  4687. } else if (str_eq(prop, "min-width")) {
  4688. decl_add(out, nout, PROP_MINW, v, important);
  4689. decl_add(out, nout, PROP_MINW_KIND, kind,
  4690. important);
  4691. } else if (str_eq(prop, "max-height")) {
  4692. decl_add(out, nout, PROP_MAXH, v, important);
  4693. decl_add(out, nout, PROP_MAXH_KIND, kind,
  4694. important);
  4695. } else if (str_eq(prop, "min-height")) {
  4696. decl_add(out, nout, PROP_MINH, v, important);
  4697. decl_add(out, nout, PROP_MINH_KIND, kind,
  4698. important);
  4699. } else {
  4700. decl_add(out, nout, PROP_HEIGHT, v, important);
  4701. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  4702. important);
  4703. }
  4704. }
  4705. } else if (str_eq(prop, "box-sizing")) {
  4706. int bb = (vend - vstart >= 10 &&
  4707. ci_eq_n(s + vstart, "border-box", 10));
  4708. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  4709. } else if (str_eq(prop, "line-height")) {
  4710. unsigned short px;
  4711. unsigned char pct;
  4712. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  4713. if (px) {
  4714. decl_add(out, nout, PROP_LH_PX, px, important);
  4715. decl_add(out, nout, PROP_LH_PCT, 0, important);
  4716. } else {
  4717. decl_add(out, nout, PROP_LH_PX, 0, important);
  4718. decl_add(out, nout, PROP_LH_PCT, pct, important);
  4719. }
  4720. }
  4721. } else if (str_eq(prop, "list-style") ||
  4722. str_eq(prop, "list-style-type")) {
  4723. int ls = BR_LIST_AUTO;
  4724. if (contains_ci_len(s + vstart, vend - vstart,
  4725. "none"))
  4726. ls = BR_LIST_NONE;
  4727. else if (contains_ci_len(s + vstart, vend - vstart,
  4728. "circle"))
  4729. ls = BR_LIST_CIRCLE;
  4730. else if (contains_ci_len(s + vstart, vend - vstart,
  4731. "square"))
  4732. ls = BR_LIST_SQUARE;
  4733. else if (contains_ci_len(s + vstart, vend - vstart,
  4734. "lower-alpha") ||
  4735. contains_ci_len(s + vstart, vend - vstart,
  4736. "lower-latin"))
  4737. ls = BR_LIST_LOWER_ALPHA;
  4738. else if (contains_ci_len(s + vstart, vend - vstart,
  4739. "upper-alpha") ||
  4740. contains_ci_len(s + vstart, vend - vstart,
  4741. "upper-latin"))
  4742. ls = BR_LIST_UPPER_ALPHA;
  4743. else if (contains_ci_len(s + vstart, vend - vstart,
  4744. "lower-roman"))
  4745. ls = BR_LIST_LOWER_ROMAN;
  4746. else if (contains_ci_len(s + vstart, vend - vstart,
  4747. "upper-roman"))
  4748. ls = BR_LIST_UPPER_ROMAN;
  4749. else if (contains_ci_len(s + vstart, vend - vstart,
  4750. "decimal"))
  4751. ls = BR_LIST_DECIMAL;
  4752. else if (contains_ci_len(s + vstart, vend - vstart,
  4753. "disc"))
  4754. ls = BR_LIST_DISC;
  4755. decl_add(out, nout, PROP_LIST_NONE,
  4756. ls == BR_LIST_NONE, important);
  4757. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  4758. } else if (str_eq(prop, "border-radius") ||
  4759. str_eq(prop, "border-top-left-radius") ||
  4760. str_eq(prop, "border-top-right-radius") ||
  4761. str_eq(prop, "border-bottom-right-radius") ||
  4762. str_eq(prop, "border-bottom-left-radius")) {
  4763. unsigned short v[4];
  4764. unsigned char k[4];
  4765. unsigned int pos = 0;
  4766. int n = 0;
  4767. while (pos < vend - vstart && n < 4) {
  4768. while (pos < vend - vstart &&
  4769. is_space(s[vstart + pos]))
  4770. pos++;
  4771. if (pos >= vend - vstart ||
  4772. s[vstart + pos] == '/')
  4773. break;
  4774. if (!parse_radius_value(s + vstart,
  4775. vend - vstart, &pos,
  4776. &v[n], &k[n]))
  4777. break;
  4778. n++;
  4779. }
  4780. if (n) {
  4781. unsigned short r[4];
  4782. unsigned char rk[4];
  4783. int i;
  4784. if (n == 1) {
  4785. for (i = 0; i < 4; i++) {
  4786. r[i] = v[0];
  4787. rk[i] = k[0];
  4788. }
  4789. } else if (n == 2) {
  4790. r[0] = r[2] = v[0];
  4791. r[1] = r[3] = v[1];
  4792. rk[0] = rk[2] = k[0];
  4793. rk[1] = rk[3] = k[1];
  4794. } else if (n == 3) {
  4795. r[0] = v[0];
  4796. r[1] = r[3] = v[1];
  4797. r[2] = v[2];
  4798. rk[0] = k[0];
  4799. rk[1] = rk[3] = k[1];
  4800. rk[2] = k[2];
  4801. } else {
  4802. for (i = 0; i < 4; i++) {
  4803. r[i] = v[i];
  4804. rk[i] = k[i];
  4805. }
  4806. }
  4807. for (i = 0; i < 4; i++)
  4808. r[i] = rk[i] == 2 ? 255 :
  4809. (r[i] > 255 ? 255 : r[i]);
  4810. if (str_eq(prop, "border-top-left-radius")) {
  4811. decl_add(out, nout, PROP_RADIUS_TL,
  4812. (short)r[0], important);
  4813. } else if (str_eq(prop,
  4814. "border-top-right-radius")) {
  4815. decl_add(out, nout, PROP_RADIUS_TR,
  4816. (short)r[0], important);
  4817. } else if (str_eq(prop,
  4818. "border-bottom-right-radius")) {
  4819. decl_add(out, nout, PROP_RADIUS_BR,
  4820. (short)r[0], important);
  4821. } else if (str_eq(prop,
  4822. "border-bottom-left-radius")) {
  4823. decl_add(out, nout, PROP_RADIUS_BL,
  4824. (short)r[0], important);
  4825. } else {
  4826. decl_add(out, nout, PROP_RADIUS_TL,
  4827. (short)r[0], important);
  4828. decl_add(out, nout, PROP_RADIUS_TR,
  4829. (short)r[1], important);
  4830. decl_add(out, nout, PROP_RADIUS_BR,
  4831. (short)r[2], important);
  4832. decl_add(out, nout, PROP_RADIUS_BL,
  4833. (short)r[3], important);
  4834. }
  4835. }
  4836. } else if (str_eq(prop, "border") ||
  4837. str_eq(prop, "border-top") ||
  4838. str_eq(prop, "border-right") ||
  4839. str_eq(prop, "border-bottom") ||
  4840. str_eq(prop, "border-left") ||
  4841. str_eq(prop, "border-color") ||
  4842. str_eq(prop, "border-width") ||
  4843. str_eq(prop, "border-style") ||
  4844. str_eq(prop, "border-top-width") ||
  4845. str_eq(prop, "border-right-width") ||
  4846. str_eq(prop, "border-bottom-width") ||
  4847. str_eq(prop, "border-left-width") ||
  4848. str_eq(prop, "border-top-color") ||
  4849. str_eq(prop, "border-right-color") ||
  4850. str_eq(prop, "border-bottom-color") ||
  4851. str_eq(prop, "border-left-color") ||
  4852. str_eq(prop, "border-top-style") ||
  4853. str_eq(prop, "border-right-style") ||
  4854. str_eq(prop, "border-bottom-style") ||
  4855. str_eq(prop, "border-left-style")) {
  4856. const char *v = s + vstart;
  4857. unsigned int vl = vend - vstart;
  4858. unsigned int pos = 0, ts, te;
  4859. int widths[4], colors[4], styles[4], trans[4];
  4860. int nw = 0, nc = 0, ns = 0;
  4861. int side = -1;
  4862. int is_wl = str_eq(prop, "border-width");
  4863. int is_cl = str_eq(prop, "border-color");
  4864. int is_sl = str_eq(prop, "border-style");
  4865. int i;
  4866. if (str_eq(prop, "border-top")) side = 0;
  4867. else if (str_eq(prop, "border-right")) side = 1;
  4868. else if (str_eq(prop, "border-bottom")) side = 2;
  4869. else if (str_eq(prop, "border-left")) side = 3;
  4870. else if (str_eq(prop, "border-top-width")) {
  4871. side = 0; is_wl = 2;
  4872. } else if (str_eq(prop, "border-right-width")) {
  4873. side = 1; is_wl = 2;
  4874. } else if (str_eq(prop, "border-bottom-width")) {
  4875. side = 2; is_wl = 2;
  4876. } else if (str_eq(prop, "border-left-width")) {
  4877. side = 3; is_wl = 2;
  4878. } else if (str_eq(prop, "border-top-color")) {
  4879. side = 0; is_cl = 2;
  4880. } else if (str_eq(prop, "border-right-color")) {
  4881. side = 1; is_cl = 2;
  4882. } else if (str_eq(prop, "border-bottom-color")) {
  4883. side = 2; is_cl = 2;
  4884. } else if (str_eq(prop, "border-left-color")) {
  4885. side = 3; is_cl = 2;
  4886. } else if (str_eq(prop, "border-top-style")) {
  4887. side = 0; is_sl = 2;
  4888. } else if (str_eq(prop, "border-right-style")) {
  4889. side = 1; is_sl = 2;
  4890. } else if (str_eq(prop, "border-bottom-style")) {
  4891. side = 2; is_sl = 2;
  4892. } else if (str_eq(prop, "border-left-style")) {
  4893. side = 3; is_sl = 2;
  4894. }
  4895. if (is_wl) {
  4896. while (nw < 4 && next_token(v, vl, &pos,
  4897. &ts, &te)) {
  4898. int x;
  4899. if (!parse_len_token(v + ts, te - ts,
  4900. &x))
  4901. break;
  4902. widths[nw++] = x;
  4903. }
  4904. if (nw) {
  4905. if (is_wl == 1)
  4906. expand_four(widths, nw);
  4907. if (side >= 0 && is_wl == 2) {
  4908. decl_add(out, nout,
  4909. PROP_BW_T + side,
  4910. widths[0], important);
  4911. } else {
  4912. for (i = 0; i < 4; i++)
  4913. decl_add(out, nout,
  4914. PROP_BW_T + i,
  4915. widths[i],
  4916. important);
  4917. }
  4918. }
  4919. } else if (is_cl) {
  4920. unsigned short c;
  4921. int t;
  4922. while (nc < 4 && next_token(v, vl, &pos,
  4923. &ts, &te)) {
  4924. t = color_is_transparent(v + ts,
  4925. te - ts);
  4926. if (!t && !parse_color(v + ts,
  4927. te - ts, &c))
  4928. break;
  4929. colors[nc] = t ? 0 : c;
  4930. trans[nc] = t;
  4931. nc++;
  4932. }
  4933. if (nc) {
  4934. if (is_cl == 1) {
  4935. expand_four(colors, nc);
  4936. expand_four(trans, nc);
  4937. }
  4938. if (side >= 0 && is_cl == 2) {
  4939. decl_add(out, nout,
  4940. PROP_BC_T + side,
  4941. colors[0], important);
  4942. decl_add(out, nout,
  4943. PROP_BT_T + side,
  4944. trans[0], important);
  4945. } else {
  4946. for (i = 0; i < 4; i++) {
  4947. decl_add(out, nout,
  4948. PROP_BC_T + i,
  4949. colors[i],
  4950. important);
  4951. decl_add(out, nout,
  4952. PROP_BT_T + i,
  4953. trans[i],
  4954. important);
  4955. }
  4956. }
  4957. }
  4958. } else if (is_sl) {
  4959. while (ns < 4 && next_token(v, vl, &pos,
  4960. &ts, &te)) {
  4961. int stv;
  4962. if ((te - ts == 4 &&
  4963. ci_eq_n(v + ts, "none", 4)) ||
  4964. (te - ts == 6 &&
  4965. ci_eq_n(v + ts, "hidden", 6)))
  4966. stv = 0;
  4967. else if (te - ts == 6 &&
  4968. ci_eq_n(v + ts,
  4969. "dashed", 6))
  4970. stv = 2;
  4971. else if (te - ts == 6 &&
  4972. ci_eq_n(v + ts,
  4973. "dotted", 6))
  4974. stv = 3;
  4975. else
  4976. stv = 1;
  4977. styles[ns++] = stv;
  4978. }
  4979. if (ns) {
  4980. if (is_sl == 1)
  4981. expand_four(styles, ns);
  4982. if (is_sl == 2 && side >= 0) {
  4983. if (!styles[0])
  4984. decl_add(out, nout,
  4985. PROP_BW_T + side,
  4986. 0, important);
  4987. else
  4988. decl_add(out, nout,
  4989. PROP_BS_T + side,
  4990. styles[0] - 1,
  4991. important);
  4992. } else {
  4993. for (i = 0; i < 4; i++) {
  4994. if (!styles[i])
  4995. decl_add(out, nout,
  4996. PROP_BW_T + i,
  4997. 0,
  4998. important);
  4999. else
  5000. decl_add(out, nout,
  5001. PROP_BS_T + i,
  5002. styles[i] - 1,
  5003. important);
  5004. }
  5005. }
  5006. }
  5007. } else {
  5008. int bw = -1, bc = -1, style_none = 0;
  5009. int bstyle = -1, btrans = 0;
  5010. while (next_token(v, vl, &pos, &ts, &te)) {
  5011. unsigned short c;
  5012. int x;
  5013. if ((te - ts == 4 &&
  5014. ci_eq_n(v + ts, "none", 4)) ||
  5015. (te - ts == 6 &&
  5016. ci_eq_n(v + ts, "hidden", 6))) {
  5017. style_none = 1;
  5018. } else if (te - ts == 6 &&
  5019. ci_eq_n(v + ts, "dashed", 6)) {
  5020. bstyle = BR_BORDER_DASHED;
  5021. } else if (te - ts == 6 &&
  5022. ci_eq_n(v + ts, "dotted", 6)) {
  5023. bstyle = BR_BORDER_DOTTED;
  5024. } else if (te - ts == 5 &&
  5025. ci_eq_n(v + ts, "solid", 5)) {
  5026. bstyle = BR_BORDER_SOLID;
  5027. } else if (parse_len_token(v + ts,
  5028. te - ts, &x)) {
  5029. bw = x;
  5030. } else if (color_is_transparent(v + ts,
  5031. te - ts)) {
  5032. bc = 0;
  5033. btrans = 1;
  5034. } else if (parse_color(v + ts,
  5035. te - ts, &c)) {
  5036. bc = c;
  5037. btrans = 0;
  5038. }
  5039. }
  5040. if (style_none)
  5041. bw = 0;
  5042. if (bc >= 0 && bw < 0)
  5043. bw = 1;
  5044. if (side < 0) {
  5045. if (bw >= 0)
  5046. decl_add(out, nout,
  5047. PROP_BORDER_W, bw,
  5048. important);
  5049. if (bc >= 0)
  5050. decl_add(out, nout,
  5051. PROP_BORDER_COLOR, bc,
  5052. important);
  5053. if (bc >= 0)
  5054. for (i = 0; i < 4; i++)
  5055. decl_add(out, nout,
  5056. PROP_BT_T + i,
  5057. btrans,
  5058. important);
  5059. if (bstyle >= 0)
  5060. for (i = 0; i < 4; i++)
  5061. decl_add(out, nout,
  5062. PROP_BS_T + i,
  5063. bstyle,
  5064. important);
  5065. } else {
  5066. if (bw >= 0)
  5067. decl_add(out, nout,
  5068. PROP_BW_T + side, bw,
  5069. important);
  5070. if (bc >= 0)
  5071. decl_add(out, nout,
  5072. PROP_BC_T + side, bc,
  5073. important);
  5074. if (bc >= 0)
  5075. decl_add(out, nout,
  5076. PROP_BT_T + side,
  5077. btrans, important);
  5078. if (bstyle >= 0)
  5079. decl_add(out, nout,
  5080. PROP_BS_T + side,
  5081. bstyle, important);
  5082. }
  5083. }
  5084. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  5085. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  5086. unsigned int pos = 0;
  5087. int is_pad = prop[0] == 'p';
  5088. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  5089. while (pos < vend - vstart && nv < 4) {
  5090. units[nv] = 0;
  5091. if (parse_margin_calc(s + vstart,
  5092. vend - vstart, &pos,
  5093. &vals[nv], &cpx[nv])) {
  5094. units[nv] = 5;
  5095. } else {
  5096. vals[nv] = parse_margin_len(
  5097. s + vstart, vend - vstart,
  5098. &pos, &units[nv]);
  5099. }
  5100. nv++;
  5101. while (pos < vend - vstart &&
  5102. is_space(s[vstart + pos]))
  5103. pos++;
  5104. }
  5105. if (nv == 1) {
  5106. vals[1] = vals[2] = vals[3] = vals[0];
  5107. units[1] = units[2] = units[3] = units[0];
  5108. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  5109. } else if (nv == 2) {
  5110. vals[2] = vals[0];
  5111. vals[3] = vals[1];
  5112. units[2] = units[0];
  5113. units[3] = units[1];
  5114. cpx[2] = cpx[0];
  5115. cpx[3] = cpx[1];
  5116. } else if (nv == 3) {
  5117. vals[3] = vals[1];
  5118. units[3] = units[1];
  5119. cpx[3] = cpx[1];
  5120. }
  5121. if (nv > 0) {
  5122. int k;
  5123. for (k = 0; k < 4; k++) {
  5124. int v = vals[k];
  5125. int u = units[k];
  5126. int au = u == 4;
  5127. if (au)
  5128. v = 0;
  5129. if (is_pad && au)
  5130. u = 0;
  5131. decl_add(out, nout,
  5132. (unsigned char)(base + k), v,
  5133. important);
  5134. decl_add(out, nout,
  5135. (unsigned char)
  5136. ((is_pad ? PROP_PT_U :
  5137. PROP_MT_U) + k),
  5138. u, important);
  5139. if (u == 5)
  5140. decl_add(out, nout,
  5141. (unsigned char)
  5142. ((is_pad ? PROP_PT_CPX :
  5143. PROP_MT_CPX) + k),
  5144. cpx[k], important);
  5145. if (!is_pad && (k == 1 || k == 3))
  5146. decl_add(out, nout,
  5147. k == 1 ? PROP_MR_AUTO :
  5148. PROP_ML_AUTO,
  5149. au, important);
  5150. }
  5151. }
  5152. } else if (str_eq(prop, "margin-top") ||
  5153. str_eq(prop, "margin-right") ||
  5154. str_eq(prop, "margin-bottom") ||
  5155. str_eq(prop, "margin-left") ||
  5156. str_eq(prop, "padding-top") ||
  5157. str_eq(prop, "padding-right") ||
  5158. str_eq(prop, "padding-bottom") ||
  5159. str_eq(prop, "padding-left")) {
  5160. unsigned int pos = 0;
  5161. int unit = 0;
  5162. int v = 0, cpx = 0;
  5163. unsigned char p = PROP_MT;
  5164. const char *side;
  5165. int au;
  5166. if (parse_margin_calc(s + vstart, vend - vstart,
  5167. &pos, &v, &cpx))
  5168. unit = 5;
  5169. else
  5170. v = parse_margin_len(s + vstart,
  5171. vend - vstart, &pos,
  5172. &unit);
  5173. au = unit == 4;
  5174. if (au)
  5175. v = 0;
  5176. if (prop[0] == 'p') {
  5177. p = PROP_PT;
  5178. if (au)
  5179. unit = 0;
  5180. }
  5181. side = prop + (prop[0] == 'p' ? 8 : 7);
  5182. if (str_eq(side, "right"))
  5183. p = (unsigned char)(p + 1);
  5184. else if (str_eq(side, "bottom"))
  5185. p = (unsigned char)(p + 2);
  5186. else if (str_eq(side, "left"))
  5187. p = (unsigned char)(p + 3);
  5188. decl_add(out, nout, p, v, important);
  5189. decl_add(out, nout,
  5190. (unsigned char)((prop[0] == 'p' ?
  5191. PROP_PT_U : PROP_MT_U) +
  5192. (p - (prop[0] == 'p' ?
  5193. PROP_PT : PROP_MT))),
  5194. unit, important);
  5195. if (unit == 5)
  5196. decl_add(out, nout,
  5197. (unsigned char)((prop[0] == 'p' ?
  5198. PROP_PT_CPX : PROP_MT_CPX) +
  5199. (p - (prop[0] == 'p' ?
  5200. PROP_PT : PROP_MT))),
  5201. cpx, important);
  5202. if (str_eq(side, "right"))
  5203. decl_add(out, nout, PROP_MR_AUTO, au, important);
  5204. else if (str_eq(side, "left"))
  5205. decl_add(out, nout, PROP_ML_AUTO, au, important);
  5206. }
  5207. }
  5208. }
  5209. /* Skip @media/@font-face/@keyframes/@import without descending into their
  5210. * nested blocks. This is the mobile/base rules path: media enhancements are
  5211. * deliberately ignored for now. */
  5212. static unsigned int skip_at_rule(const char *s, unsigned int len,
  5213. unsigned int i)
  5214. {
  5215. unsigned int j = i;
  5216. while (j < len && s[j] != '{' && s[j] != ';')
  5217. j++;
  5218. if (j >= len)
  5219. return len;
  5220. if (s[j] == ';')
  5221. return j + 1;
  5222. j++;
  5223. return css_skip_nested(s, len, j);
  5224. }
  5225. static unsigned int viewport_w = 1024;
  5226. static unsigned short theme_text = 0x0000;
  5227. static unsigned short theme_bg = 0xFFFF;
  5228. static unsigned short theme_panel = 0xEF5D;
  5229. static unsigned short theme_link = 0x001B;
  5230. void br_set_theme(unsigned short text, unsigned short bg,
  5231. unsigned short panel, unsigned short link)
  5232. {
  5233. theme_text = text;
  5234. theme_bg = bg;
  5235. theme_panel = panel;
  5236. theme_link = link;
  5237. }
  5238. void br_set_viewport(unsigned int width)
  5239. {
  5240. viewport_w = width ? width : 1024;
  5241. }
  5242. static unsigned int media_len_px(const char *s, unsigned int len)
  5243. {
  5244. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  5245. int em = 0;
  5246. while (i < len && is_space(s[i]))
  5247. i++;
  5248. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5249. whole = whole * 10 + (unsigned int)(s[i] - '0');
  5250. i++;
  5251. }
  5252. if (i < len && s[i] == '.') {
  5253. i++;
  5254. /* Consume every fractional digit even when there are more
  5255. * than the three we keep: leaving the tail behind made the
  5256. * unit check miss "em" (74.9375em became 74px). */
  5257. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5258. if (scale < 1000) {
  5259. frac = frac * 10 + (unsigned int)(s[i] - '0');
  5260. scale *= 10;
  5261. }
  5262. i++;
  5263. }
  5264. }
  5265. while (i < len && is_space(s[i]))
  5266. i++;
  5267. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  5268. em = 1;
  5269. i += 2;
  5270. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  5271. em = 1;
  5272. i += 3;
  5273. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  5274. i += 2;
  5275. }
  5276. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  5277. if (em)
  5278. return v * 16 / 100;
  5279. return v / 100;
  5280. }
  5281. /* One parenthesised media feature: min-width/max-width and the modern
  5282. * (width <= 600px) form. Unknown features are treated as true. */
  5283. static int media_feature_matches(const char *s, unsigned int len)
  5284. {
  5285. unsigned int i;
  5286. while (len && is_space(*s)) { s++; len--; }
  5287. while (len && is_space(s[len - 1])) len--;
  5288. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  5289. /* There is no user-selectable colour scheme. Ignore both
  5290. * variants so a page's base palette is used; sites whose base
  5291. * is dark (like the project docs) then stay dark instead of
  5292. * flipping to their light media override. */
  5293. return 0;
  5294. }
  5295. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  5296. for (i = 9; i < len && s[i] != ':'; i++)
  5297. ;
  5298. if (i < len)
  5299. return viewport_w >= media_len_px(s + i + 1,
  5300. len - i - 1);
  5301. return 1;
  5302. }
  5303. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  5304. for (i = 9; i < len && s[i] != ':'; i++)
  5305. ;
  5306. if (i < len)
  5307. return viewport_w <= media_len_px(s + i + 1,
  5308. len - i - 1);
  5309. return 1;
  5310. }
  5311. for (i = 0; i < len; i++) {
  5312. if (s[i] == '<' || s[i] == '>') {
  5313. int max = s[i] == '<';
  5314. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  5315. if (i + 1 < len && s[i + 1] == '=')
  5316. v = media_len_px(s + i + 2, len - i - 2);
  5317. return max ? viewport_w <= v : viewport_w >= v;
  5318. }
  5319. }
  5320. return 1;
  5321. }
  5322. static int media_query_matches(const char *s, unsigned int len)
  5323. {
  5324. unsigned int i;
  5325. if (contains_ci_len(s, len, "print") &&
  5326. !contains_ci_len(s, len, "screen"))
  5327. return 0;
  5328. if (contains_ci_len(s, len, "not "))
  5329. return 0;
  5330. for (i = 0; i < len; i++) {
  5331. if (s[i] == '(') {
  5332. unsigned int st = ++i;
  5333. while (i < len && s[i] != ')')
  5334. i++;
  5335. if (!media_feature_matches(s + st, i - st))
  5336. return 0;
  5337. }
  5338. }
  5339. return 1;
  5340. }
  5341. static int media_list_matches(const char *s, unsigned int len)
  5342. {
  5343. unsigned int i = 0;
  5344. while (i < len) {
  5345. unsigned int st = i;
  5346. while (i < len && s[i] != ',')
  5347. i++;
  5348. if (media_query_matches(s + st, i - st))
  5349. return 1;
  5350. i++;
  5351. }
  5352. return 0;
  5353. }
  5354. static unsigned int rule_hash(const char *s, unsigned int len)
  5355. {
  5356. unsigned int h = 2166136261u, i;
  5357. for (i = 0; i < len; i++) {
  5358. char c = s[i];
  5359. if (c >= 'A' && c <= 'Z')
  5360. c = (char)(c - 'A' + 'a');
  5361. h ^= (unsigned char)c;
  5362. h *= 16777619u;
  5363. }
  5364. return h % BR_RULE_HASH;
  5365. }
  5366. /* Index a parsed rule by the most selective component of its rightmost
  5367. * compound selector. br_style_for() then only visits rules that can
  5368. * possibly match a node instead of scanning every rule. */
  5369. static void rule_index_add(struct br_doc *d, unsigned int idx,
  5370. const char *key, unsigned int len)
  5371. {
  5372. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  5373. unsigned short *head;
  5374. for (i = 0; i < len; i++) {
  5375. if (key[i] == '.' || key[i] == '#') {
  5376. unsigned int st = i + 1, n = 0;
  5377. while (st + n < len && is_ident(key[st + n]))
  5378. n++;
  5379. if (key[i] == '#') {
  5380. ipos = st;
  5381. ilen = n;
  5382. } else if (n) {
  5383. cpos = st;
  5384. clen = n;
  5385. }
  5386. i = st + n - 1;
  5387. }
  5388. }
  5389. if (ilen)
  5390. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  5391. else if (clen)
  5392. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  5393. else if (len == 5 && ci_eq_n(key, ":root", 5))
  5394. head = &d->rule_tag[BR_TAG_HTML];
  5395. else if (len && is_ident(key[0])) {
  5396. char name[16];
  5397. unsigned int n = 0, tag;
  5398. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  5399. name[n] = key[n];
  5400. n++;
  5401. }
  5402. name[n] = '\0';
  5403. tag = br_tag_from_name(name);
  5404. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  5405. head = &d->rule_tag[tag];
  5406. else
  5407. head = &d->rule_any;
  5408. } else {
  5409. head = &d->rule_any;
  5410. }
  5411. d->rule_next[idx] = *head;
  5412. *head = (unsigned short)idx;
  5413. }
  5414. static unsigned int selector_specificity(const char *s, unsigned int len);
  5415. static int css_collect_only;
  5416. /* Simple structural pseudo-classes that can be answered from the node tree.
  5417. * Returns the token length or 0. Kept in sync with compound_match(). */
  5418. static unsigned int structural_pseudo_len(const char *s, unsigned int i,
  5419. unsigned int end)
  5420. {
  5421. static const struct {
  5422. const char *name;
  5423. unsigned char len;
  5424. } p[] = {
  5425. { ":first-child", 12 }, { ":last-child", 11 },
  5426. { ":first-of-type", 14 }, { ":last-of-type", 13 },
  5427. { ":only-child", 11 }, { ":only-of-type", 13 },
  5428. };
  5429. unsigned int k;
  5430. for (k = 0; k < sizeof(p) / sizeof(p[0]); k++) {
  5431. unsigned int l = p[k].len;
  5432. if (i + l <= end && ci_eq_n(s + i, p[k].name, l) &&
  5433. (i + l == end || !is_ident(s[i + l])))
  5434. return l;
  5435. }
  5436. return 0;
  5437. }
  5438. static int structural_pseudo_match(struct br_doc *d, unsigned int node,
  5439. const char *name, unsigned int len)
  5440. {
  5441. unsigned int parent = d->nodes[node].parent;
  5442. unsigned int c;
  5443. int before = 0, after = 0;
  5444. int of_type = contains_ci_len(name, len, "of-type");
  5445. unsigned int tag = d->nodes[node].tag;
  5446. if (!parent)
  5447. /* The root element is the only element child of the
  5448. * document node. */
  5449. return 1;
  5450. for (c = d->nodes[parent].first; c && c != node;
  5451. c = d->nodes[c].next) {
  5452. if (d->nodes[c].type != BR_NODE_ELEM)
  5453. continue;
  5454. if (of_type && d->nodes[c].tag != tag)
  5455. continue;
  5456. before = 1;
  5457. break;
  5458. }
  5459. for (c = d->nodes[node].next; c; c = d->nodes[c].next) {
  5460. if (d->nodes[c].type != BR_NODE_ELEM)
  5461. continue;
  5462. if (of_type && d->nodes[c].tag != tag)
  5463. continue;
  5464. after = 1;
  5465. break;
  5466. }
  5467. if (contains_ci_len(name, len, "first"))
  5468. return !before;
  5469. if (contains_ci_len(name, len, "last"))
  5470. return !after;
  5471. if (contains_ci_len(name, len, "only"))
  5472. return !before && !after;
  5473. return 0;
  5474. }
  5475. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  5476. {
  5477. unsigned int i = 0;
  5478. while (i < len && d->nrules < d->rule_cap) {
  5479. unsigned int sstart, send, bstart, bend, j;
  5480. struct br_decl decls[BR_DECLS];
  5481. unsigned int nd;
  5482. while (i < len && is_space(s[i]))
  5483. i++;
  5484. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  5485. i += 2;
  5486. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  5487. i++;
  5488. i += 2;
  5489. continue;
  5490. }
  5491. if (i >= len || s[i] == '}')
  5492. break;
  5493. if (s[i] == '@') {
  5494. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  5495. unsigned int q = i + 6;
  5496. while (q < len && s[q] != '{')
  5497. q++;
  5498. if (q < len &&
  5499. media_list_matches(s + i + 6,
  5500. q - (i + 6))) {
  5501. unsigned int b = q + 1;
  5502. unsigned int e = css_skip_nested(s, len, b);
  5503. if (e > b)
  5504. parse_css(d, s + b,
  5505. (e - 1) - b);
  5506. i = e;
  5507. } else {
  5508. i = skip_at_rule(s, len, i);
  5509. }
  5510. continue;
  5511. }
  5512. i = skip_at_rule(s, len, i);
  5513. continue;
  5514. }
  5515. sstart = i;
  5516. while (i < len && s[i] != '{')
  5517. i++;
  5518. if (i >= len)
  5519. break;
  5520. send = i;
  5521. i++;
  5522. bstart = i;
  5523. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  5524. * to the matching close brace; stopping at the first '}' left
  5525. * the parser out of sync and dropped the rest of the sheet
  5526. * (nd-aktuell lost its desktop rules that way). */
  5527. bend = css_block_end(s, len, i);
  5528. i = bend < len ? bend + 1 : len;
  5529. while (sstart < send && is_space(s[sstart]))
  5530. sstart++;
  5531. while (send > sstart && is_space(s[send - 1]))
  5532. send--;
  5533. if (sstart == send)
  5534. continue;
  5535. if (!css_dark_selector(s + sstart, send - sstart))
  5536. css_collect_block(d, s + bstart, bend - bstart);
  5537. if (css_collect_only)
  5538. continue;
  5539. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  5540. if (!nd)
  5541. continue;
  5542. /* Split a comma-separated selector list into separate rules. */
  5543. j = sstart;
  5544. while (j <= send && d->nrules < d->rule_cap) {
  5545. unsigned int q = j, e, k;
  5546. char sel[160];
  5547. unsigned int off;
  5548. while (q < send && s[q] != ',')
  5549. q++;
  5550. e = q;
  5551. while (j < e && is_space(s[j]))
  5552. j++;
  5553. while (e > j && is_space(s[e - 1]))
  5554. e--;
  5555. if (e > j) {
  5556. /* Ignore single selectors with sibling combinators,
  5557. * pseudo-classes or attribute tests: they need
  5558. * state/attributes we do not model. The other
  5559. * selectors in the list still apply. */
  5560. for (k = j; k < e; ) {
  5561. if (s[k] == '+' || s[k] == '~')
  5562. break;
  5563. if (s[k] == ':') {
  5564. if (k + 5 <= e &&
  5565. ci_eq_n(s + k, ":root", 5)) {
  5566. k += 5;
  5567. continue;
  5568. }
  5569. if (k + 5 <= e &&
  5570. ci_eq_n(s + k, ":link", 5) &&
  5571. (k + 5 == e ||
  5572. !is_ident(s[k + 5]))) {
  5573. k += 5;
  5574. continue;
  5575. }
  5576. if (k + 5 <= e &&
  5577. ci_eq_n(s + k, ":not(", 5)) {
  5578. unsigned int n = k + 5;
  5579. int nd = 1;
  5580. while (n < e && nd) {
  5581. if (s[n] == '(')
  5582. nd++;
  5583. else if (s[n] == ')')
  5584. nd--;
  5585. n++;
  5586. }
  5587. if (nd)
  5588. break;
  5589. k = n;
  5590. continue;
  5591. }
  5592. {
  5593. unsigned int sl =
  5594. structural_pseudo_len(
  5595. s, k, e);
  5596. if (sl) {
  5597. k += sl;
  5598. continue;
  5599. }
  5600. }
  5601. break;
  5602. }
  5603. k++;
  5604. }
  5605. if (k < e) {
  5606. j = q + 1;
  5607. if (q >= send)
  5608. break;
  5609. continue;
  5610. }
  5611. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  5612. sel[k] = s[j + k];
  5613. sel[k] = '\0';
  5614. off = store_attr(d, sel, k, 159);
  5615. if (off) {
  5616. struct br_rule *r = &d->rules[d->nrules++];
  5617. unsigned int ks = 0, q;
  5618. for (q = 0; q < k; q++)
  5619. if (sel[q] == ' ' || sel[q] == '>')
  5620. ks = q + 1;
  5621. r->sel_off = off;
  5622. r->key_off = store_attr(d, sel + ks,
  5623. k - ks, 159);
  5624. r->spec = (unsigned short)selector_specificity(sel, k);
  5625. r->single = 1;
  5626. {
  5627. unsigned int z;
  5628. for (z = 0; z < k; z++)
  5629. if (is_space(sel[z]) || sel[z] == '>') {
  5630. r->single = 0;
  5631. break;
  5632. }
  5633. }
  5634. r->nd = (unsigned char)nd;
  5635. for (k = 0; k < nd; k++)
  5636. r->d[k] = decls[k];
  5637. rule_index_add(d, d->nrules - 1,
  5638. attr_ptr(d, r->key_off),
  5639. s_len(attr_ptr(d, r->key_off)));
  5640. }
  5641. }
  5642. j = q + 1;
  5643. if (q >= send)
  5644. break;
  5645. }
  5646. }
  5647. }
  5648. /* Sites often set html { font-size: 62.5% } and then size everything in
  5649. * rem. Detect that root size before parsing the sheet so a rem resolves to
  5650. * the same pixels a real browser would use. */
  5651. static void detect_root_font(const char *s, unsigned int len)
  5652. {
  5653. unsigned int i;
  5654. for (i = 0; i + 4 < len; i++) {
  5655. unsigned int j, end, body, sel, q, h;
  5656. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  5657. int exact;
  5658. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  5659. s[i + 3] == 'l'))
  5660. continue;
  5661. h = i + 4;
  5662. while (h < len && is_space(s[h]))
  5663. h++;
  5664. if (h < len && s[h] != '{' && s[h] != ',' &&
  5665. s[h] != '.' && s[h] != ':' && s[h] != '[')
  5666. continue;
  5667. sel = i;
  5668. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  5669. s[sel - 1] != ';' && s[sel - 1] != ',')
  5670. sel--;
  5671. q = sel;
  5672. while (q < i && is_space(s[q]))
  5673. q++;
  5674. exact = (q == i);
  5675. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  5676. continue;
  5677. j = h;
  5678. if (j >= len || s[j] != '{')
  5679. continue;
  5680. while (j < len && s[j] != '{')
  5681. j++;
  5682. if (j >= len)
  5683. break;
  5684. end = j + 1;
  5685. body = end;
  5686. while (end < len && s[end] != '}')
  5687. end++;
  5688. for (; j + 9 < end; j++) {
  5689. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  5690. s[j + 3] == 't' && s[j + 4] == '-' &&
  5691. s[j + 5] == 's' && s[j + 6] == 'i' &&
  5692. s[j + 7] == 'z' && s[j + 8] == 'e'))
  5693. continue;
  5694. j += 9;
  5695. while (j < end && s[j] != ':')
  5696. j++;
  5697. if (j >= end)
  5698. break;
  5699. j++;
  5700. while (j < end && is_space(s[j]))
  5701. j++;
  5702. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5703. v = v * 10 + (s[j] - '0');
  5704. digits = 1;
  5705. j++;
  5706. }
  5707. if (j < end && s[j] == '.') {
  5708. j++;
  5709. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5710. v = v * 10 + (s[j] - '0');
  5711. frac++;
  5712. j++;
  5713. }
  5714. }
  5715. while (frac < 2) {
  5716. v *= 10;
  5717. frac++;
  5718. }
  5719. while (frac > 2) {
  5720. v /= 10;
  5721. frac--;
  5722. }
  5723. if (!digits)
  5724. break;
  5725. if (j < end && s[j] == '%')
  5726. pct = 1;
  5727. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  5728. px = 1;
  5729. if (pct)
  5730. v = (v * 16 + 5000) / 10000;
  5731. else if (px)
  5732. v /= 100;
  5733. else
  5734. v = v * 16 / 100;
  5735. if (exact && v >= 8 && v <= 24)
  5736. root_font_px = v;
  5737. if (exact)
  5738. return;
  5739. }
  5740. /* html { font: 10px/1 Verdana } also sets the root size. */
  5741. for (j = body; j + 5 <= end; j++) {
  5742. unsigned int p, ts, te;
  5743. if (!ci_eq_n(s + j, "font:", 5))
  5744. continue;
  5745. p = j + 5;
  5746. while (next_token(s, end, &p, &ts, &te)) {
  5747. int fv = parse_font_px(s + ts, te - ts);
  5748. if (fv >= 8 && fv <= 24) {
  5749. if (exact)
  5750. root_font_px = fv;
  5751. break;
  5752. }
  5753. }
  5754. if (exact)
  5755. return;
  5756. break;
  5757. }
  5758. }
  5759. }
  5760. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  5761. {
  5762. scan_font_faces(css, len);
  5763. detect_root_font(css, len);
  5764. css_collect_only = 1;
  5765. parse_css(d, css, len);
  5766. css_collect_only = 0;
  5767. parse_css(d, css, len);
  5768. }
  5769. void br_parse_inline_css(struct br_doc *d)
  5770. {
  5771. if (d->inline_css_len) {
  5772. scan_font_faces(css_store, d->inline_css_len);
  5773. detect_root_font(css_store, d->inline_css_len);
  5774. css_collect_only = 1;
  5775. parse_css(d, css_store, d->inline_css_len);
  5776. css_collect_only = 0;
  5777. parse_css(d, css_store, d->inline_css_len);
  5778. }
  5779. d->inline_css_len = 0;
  5780. }
  5781. void br_reset_styles(struct br_doc *d)
  5782. {
  5783. unsigned int i;
  5784. for (i = 0; i < d->nnode; i++)
  5785. d->nodes[i].style_valid = 0;
  5786. }
  5787. /* ---- selector matching ---- */
  5788. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  5789. unsigned int clen)
  5790. {
  5791. const char *s = attr_ptr(d, d->nodes[node].class_off);
  5792. unsigned int i = 0;
  5793. while (s[i]) {
  5794. unsigned int st, n;
  5795. while (s[i] && is_space(s[i]))
  5796. i++;
  5797. st = i;
  5798. while (s[i] && !is_space(s[i]))
  5799. i++;
  5800. n = i - st;
  5801. if (n == clen && ci_eq_n(s + st, cls, n))
  5802. return 1;
  5803. }
  5804. return 0;
  5805. }
  5806. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  5807. unsigned int len)
  5808. {
  5809. const char *s = attr_ptr(d, d->nodes[node].id_off);
  5810. unsigned int i;
  5811. for (i = 0; i < len; i++)
  5812. if (s[i] != id[i])
  5813. return 0;
  5814. return s[len] == '\0';
  5815. }
  5816. static int is_ident(char c)
  5817. {
  5818. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  5819. (c >= '0' && c <= '9') || c == '-' || c == '_';
  5820. }
  5821. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  5822. {
  5823. unsigned int n = 0;
  5824. while (lit[n])
  5825. n++;
  5826. return n == nlen && ci_eq_n(name, lit, nlen);
  5827. }
  5828. static int attr_present(struct br_doc *d, unsigned int node,
  5829. const char *name, unsigned int nlen)
  5830. {
  5831. struct br_node *n = &d->nodes[node];
  5832. if (attr_is(name, nlen, "data-collapse-target"))
  5833. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  5834. if (attr_is(name, nlen, "data-collapse-trigger"))
  5835. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  5836. if (attr_is(name, nlen, "is-open"))
  5837. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  5838. if (attr_is(name, nlen, "disabled"))
  5839. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  5840. if (attr_is(name, nlen, "open"))
  5841. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  5842. if (attr_is(name, nlen, "inert"))
  5843. return (n->attr_flags & BR_ATTR_INERT) != 0;
  5844. if (attr_is(name, nlen, "hidden"))
  5845. return (n->flags & BR_NODE_HIDDEN) != 0;
  5846. if (attr_is(name, nlen, "href"))
  5847. return attr_ptr(d, n->href_off)[0] != 0;
  5848. if (attr_is(name, nlen, "class"))
  5849. return attr_ptr(d, n->class_off)[0] != 0;
  5850. if (attr_is(name, nlen, "id"))
  5851. return attr_ptr(d, n->id_off)[0] != 0;
  5852. if (attr_is(name, nlen, "src"))
  5853. return attr_ptr(d, n->src_off)[0] != 0;
  5854. if (attr_is(name, nlen, "alt"))
  5855. return attr_ptr(d, n->alt_off)[0] != 0;
  5856. if (attr_is(name, nlen, "rel"))
  5857. return attr_ptr(d, n->rel_off)[0] != 0;
  5858. return 0;
  5859. }
  5860. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  5861. unsigned int len)
  5862. {
  5863. unsigned int i = 0;
  5864. int got = 0;
  5865. if (i < len && s[i] == '*') {
  5866. i++;
  5867. got = 1;
  5868. } else if (i < len && is_ident(s[i])) {
  5869. char name[16];
  5870. unsigned int n = 0, tag;
  5871. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  5872. name[n++] = s[i++];
  5873. name[n] = '\0';
  5874. tag = br_tag_from_name(name);
  5875. if (tag == BR_TAG_UNKNOWN) {
  5876. const char *raw = attr_ptr(d,
  5877. d->nodes[node].name_off);
  5878. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  5879. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  5880. return 0;
  5881. } else if (tag != d->nodes[node].tag) {
  5882. return 0;
  5883. }
  5884. got = 1;
  5885. }
  5886. while (i < len) {
  5887. if (s[i] == '.') {
  5888. unsigned int st;
  5889. i++;
  5890. st = i;
  5891. while (i < len && is_ident(s[i]))
  5892. i++;
  5893. if (!class_has(d, node, s + st, i - st))
  5894. return 0;
  5895. got = 1;
  5896. } else if (s[i] == '#') {
  5897. unsigned int st;
  5898. i++;
  5899. st = i;
  5900. while (i < len && is_ident(s[i]))
  5901. i++;
  5902. if (!id_eq(d, node, s + st, i - st))
  5903. return 0;
  5904. got = 1;
  5905. } else if (s[i] == '[') {
  5906. unsigned int st, e;
  5907. i++;
  5908. st = e = i;
  5909. while (e < len && s[e] != ']' && s[e] != '=' &&
  5910. !is_space(s[e]))
  5911. e++;
  5912. if (e == st || !attr_present(d, node, s + st, e - st))
  5913. return 0;
  5914. while (i < len && s[i] != ']')
  5915. i++;
  5916. if (i < len)
  5917. i++;
  5918. got = 1;
  5919. } else if (s[i] == ':') {
  5920. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  5921. (i + 5 == len || !is_ident(s[i + 5]))) {
  5922. if (d->nodes[node].tag != BR_TAG_HTML)
  5923. return 0;
  5924. i += 5;
  5925. got = 1;
  5926. } else if (i + 5 <= len && ci_eq_n(s + i, ":link", 5) &&
  5927. (i + 5 == len || !is_ident(s[i + 5]))) {
  5928. /* No history state: every link is unvisited. */
  5929. if (d->nodes[node].tag != BR_TAG_A ||
  5930. !d->nodes[node].href_off)
  5931. return 0;
  5932. i += 5;
  5933. got = 1;
  5934. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  5935. unsigned int st = i + 5, e = st;
  5936. int depth = 1;
  5937. while (e < len && depth) {
  5938. if (s[e] == '(')
  5939. depth++;
  5940. else if (s[e] == ')')
  5941. depth--;
  5942. if (depth)
  5943. e++;
  5944. }
  5945. if (e >= len)
  5946. return 0;
  5947. for (unsigned int z = st; z < e; z++)
  5948. if (s[z] == ':')
  5949. return 0;
  5950. if (compound_match(d, node, s + st, e - st))
  5951. return 0;
  5952. i = e + 1;
  5953. got = 1;
  5954. } else {
  5955. unsigned int sl = structural_pseudo_len(s, i,
  5956. len);
  5957. if (!sl)
  5958. return 0; /* other pseudo-classes are
  5959. * not modelled */
  5960. if (!structural_pseudo_match(d, node, s + i, sl))
  5961. return 0;
  5962. i += sl;
  5963. got = 1;
  5964. }
  5965. } else {
  5966. i++;
  5967. }
  5968. }
  5969. return got;
  5970. }
  5971. static unsigned int selector_specificity(const char *s, unsigned int len)
  5972. {
  5973. unsigned int i, spec = 0;
  5974. for (i = 0; i < len; i++) {
  5975. if (s[i] == '#')
  5976. spec += 100;
  5977. else if (s[i] == '.' || s[i] == '[' ||
  5978. (s[i] == ':' && !(i + 5 <= len &&
  5979. ci_eq_n(s + i, ":root", 5))))
  5980. spec += 10;
  5981. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  5982. if (is_ident(s[i]))
  5983. spec += 1;
  5984. }
  5985. }
  5986. return spec;
  5987. }
  5988. /* Match one complex selector; returns specificity or -1. */
  5989. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  5990. unsigned int len, int right_done)
  5991. {
  5992. struct {
  5993. unsigned int start, end;
  5994. int child;
  5995. } parts[8];
  5996. int np = 0;
  5997. unsigned int i = 0;
  5998. int right;
  5999. while (i < len) {
  6000. int child = 0;
  6001. unsigned int st, e;
  6002. while (i < len && is_space(s[i]))
  6003. i++;
  6004. if (i < len && s[i] == '>') {
  6005. child = 1;
  6006. i++;
  6007. while (i < len && is_space(s[i]))
  6008. i++;
  6009. }
  6010. if (i >= len)
  6011. break;
  6012. st = i;
  6013. while (i < len && !is_space(s[i]) && s[i] != '>')
  6014. i++;
  6015. e = i;
  6016. if (np < 8) {
  6017. parts[np].start = st;
  6018. parts[np].end = e;
  6019. parts[np].child = child;
  6020. np++;
  6021. }
  6022. }
  6023. if (!np)
  6024. return -1;
  6025. if (!right_done &&
  6026. !compound_match(d, node, s + parts[np - 1].start,
  6027. parts[np - 1].end - parts[np - 1].start))
  6028. return -1;
  6029. right = np - 1;
  6030. while (right > 0) {
  6031. unsigned int p = d->nodes[node].parent;
  6032. if (!p)
  6033. return -1;
  6034. if (parts[right].child) {
  6035. if (!compound_match(d, p, s + parts[right - 1].start,
  6036. parts[right - 1].end -
  6037. parts[right - 1].start))
  6038. return -1;
  6039. node = p;
  6040. } else {
  6041. while (p && !compound_match(d, p,
  6042. s + parts[right - 1].start,
  6043. parts[right - 1].end -
  6044. parts[right - 1].start))
  6045. p = d->nodes[p].parent;
  6046. if (!p)
  6047. return -1;
  6048. node = p;
  6049. }
  6050. right--;
  6051. }
  6052. return (int)selector_specificity(s, len);
  6053. }
  6054. #define RULE_MATCH_MAX 512
  6055. struct rule_match {
  6056. unsigned short idx;
  6057. short spec;
  6058. };
  6059. static struct rule_match rule_matches[RULE_MATCH_MAX];
  6060. static unsigned int nrule_matches;
  6061. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  6062. {
  6063. struct br_rule *r = &d->rules[idx];
  6064. const char *key = attr_ptr(d, r->key_off);
  6065. int spec, key_ok = 0;
  6066. if (key[0]) {
  6067. if (!compound_match(d, node, key, s_len(key)))
  6068. return;
  6069. key_ok = 1;
  6070. }
  6071. spec = (r->single && key_ok) ? (int)r->spec :
  6072. selector_match(d, node, attr_ptr(d, r->sel_off),
  6073. s_len(attr_ptr(d, r->sel_off)), key_ok);
  6074. if (spec < 0)
  6075. return;
  6076. if (nrule_matches < RULE_MATCH_MAX) {
  6077. rule_matches[nrule_matches].idx = (unsigned short)idx;
  6078. rule_matches[nrule_matches].spec = (short)spec;
  6079. nrule_matches++;
  6080. }
  6081. }
  6082. static void rule_scan_list(struct br_doc *d, unsigned int node,
  6083. unsigned short head)
  6084. {
  6085. while (head != 0xFFFF) {
  6086. rule_try(d, node, head);
  6087. head = d->rule_next[head];
  6088. }
  6089. }
  6090. static void rule_scan_class(struct br_doc *d, unsigned int node,
  6091. const char *cls, unsigned int len)
  6092. {
  6093. if (len)
  6094. rule_scan_list(d, node,
  6095. d->rule_bucket[rule_hash(cls, len)]);
  6096. }
  6097. static void rule_collect(struct br_doc *d, unsigned int node)
  6098. {
  6099. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  6100. const char *id = attr_ptr(d, d->nodes[node].id_off);
  6101. unsigned int tag = d->nodes[node].tag;
  6102. unsigned int i;
  6103. nrule_matches = 0;
  6104. rule_scan_list(d, node, d->rule_any);
  6105. if (tag < BR_TAG_MAX)
  6106. rule_scan_list(d, node, d->rule_tag[tag]);
  6107. if (id[0])
  6108. rule_scan_list(d, node,
  6109. d->rule_bucket[rule_hash(id, s_len(id))]);
  6110. i = 0;
  6111. while (cls[i]) {
  6112. unsigned int st, n;
  6113. while (cls[i] && is_space(cls[i]))
  6114. i++;
  6115. st = i;
  6116. while (cls[i] && !is_space(cls[i]))
  6117. i++;
  6118. n = i - st;
  6119. if (n)
  6120. rule_scan_class(d, node, cls + st, n);
  6121. }
  6122. /* Rules must cascade in document order even though the index visits
  6123. * them in bucket order. */
  6124. for (i = 1; i < nrule_matches; i++) {
  6125. struct rule_match m = rule_matches[i];
  6126. unsigned int j = i;
  6127. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  6128. rule_matches[j] = rule_matches[j - 1];
  6129. j--;
  6130. }
  6131. rule_matches[j] = m;
  6132. }
  6133. }
  6134. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  6135. int font_px)
  6136. {
  6137. if (*kind == 4) {
  6138. *val = (unsigned short)(*val * font_px / 100);
  6139. *kind = 1;
  6140. } else if (*kind == 5) {
  6141. *val = (unsigned short)(*val * root_font_px / 100);
  6142. *kind = 1;
  6143. }
  6144. }
  6145. static void resolve_size_units(struct br_style *st)
  6146. {
  6147. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  6148. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  6149. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  6150. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  6151. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  6152. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  6153. }
  6154. static void set_border_side_w(struct br_style *st, int side, int v)
  6155. {
  6156. if (v < 0)
  6157. v = 0;
  6158. if (v > 32)
  6159. v = 32;
  6160. st->border_w[side] = (unsigned char)v;
  6161. if (st->border_w[side] && !st->border_c[side])
  6162. st->border_c[side] = st->color;
  6163. }
  6164. static void set_border_side_c(struct br_style *st, int side, int v)
  6165. {
  6166. st->border_c[side] = (unsigned short)v;
  6167. }
  6168. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  6169. {
  6170. switch (dcl->prop) {
  6171. case PROP_COLOR:
  6172. st->color = (unsigned short)dcl->val;
  6173. break;
  6174. case PROP_BG:
  6175. st->bg = (unsigned short)dcl->val;
  6176. st->has_bg = 1;
  6177. break;
  6178. case PROP_BG_IMAGE:
  6179. st->bg_image = (unsigned short)dcl->val;
  6180. break;
  6181. case PROP_BG_REPEAT:
  6182. st->bg_repeat = (unsigned char)dcl->val;
  6183. break;
  6184. case PROP_BG_POS_X_KIND:
  6185. st->bg_pos_x_kind = (unsigned char)dcl->val;
  6186. break;
  6187. case PROP_BG_POS_X:
  6188. st->bg_pos_x = (signed short)dcl->val;
  6189. break;
  6190. case PROP_BG_POS_Y_KIND:
  6191. st->bg_pos_y_kind = (unsigned char)dcl->val;
  6192. break;
  6193. case PROP_BG_POS_Y:
  6194. st->bg_pos_y = (signed short)dcl->val;
  6195. break;
  6196. case PROP_BG_SIZE_KIND:
  6197. st->bg_size_kind = (unsigned char)dcl->val;
  6198. break;
  6199. case PROP_BG_SIZE_W:
  6200. st->bg_size_w = (unsigned short)dcl->val;
  6201. break;
  6202. case PROP_BG_SIZE_H:
  6203. st->bg_size_h = (unsigned short)dcl->val;
  6204. break;
  6205. case PROP_FONT_PX:
  6206. st->font_raw = (unsigned short)dcl->val;
  6207. break;
  6208. case PROP_FONT_UNIT:
  6209. st->font_unit = (unsigned char)dcl->val;
  6210. break;
  6211. case PROP_BOLD:
  6212. st->bold = (unsigned char)(dcl->val != 0);
  6213. break;
  6214. case PROP_ITALIC:
  6215. st->italic = (unsigned char)(dcl->val != 0);
  6216. break;
  6217. case PROP_MONO:
  6218. st->mono = (unsigned char)(dcl->val != 0);
  6219. break;
  6220. case PROP_FONT_FACE:
  6221. st->font_face = (unsigned char)dcl->val;
  6222. break;
  6223. case PROP_ALIGN:
  6224. st->align = (unsigned char)dcl->val;
  6225. break;
  6226. case PROP_UNDERLINE:
  6227. st->underline = (unsigned char)(dcl->val != 0);
  6228. break;
  6229. case PROP_STRIKE:
  6230. st->strike = (unsigned char)(dcl->val != 0);
  6231. break;
  6232. case PROP_OVERLINE:
  6233. st->overline = (unsigned char)(dcl->val != 0);
  6234. break;
  6235. case PROP_DISPLAY:
  6236. st->display = (unsigned char)dcl->val;
  6237. st->list_item = 0;
  6238. break;
  6239. case PROP_VIS_HIDDEN:
  6240. st->vis_hidden = (unsigned char)(dcl->val != 0);
  6241. break;
  6242. case PROP_OPACITY_HIDDEN:
  6243. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  6244. break;
  6245. case PROP_OVERFLOW_HIDDEN:
  6246. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  6247. break;
  6248. case PROP_MT: st->mt = dcl->val; break;
  6249. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  6250. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  6251. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  6252. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  6253. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  6254. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  6255. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  6256. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  6257. case PROP_MR: st->mr = dcl->val; break;
  6258. case PROP_MB: st->mb = dcl->val; break;
  6259. case PROP_ML: st->ml = dcl->val; break;
  6260. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  6261. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  6262. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  6263. break;
  6264. case PROP_PT: st->pt = dcl->val; break;
  6265. case PROP_PR: st->pr = dcl->val; break;
  6266. case PROP_PB: st->pb = dcl->val; break;
  6267. case PROP_PL: st->pl = dcl->val; break;
  6268. case PROP_MT_U:
  6269. case PROP_MR_U:
  6270. case PROP_MB_U:
  6271. case PROP_ML_U:
  6272. case PROP_PT_U:
  6273. case PROP_PR_U:
  6274. case PROP_PB_U:
  6275. case PROP_PL_U:
  6276. st->mp_unit[dcl->prop - PROP_MT_U] =
  6277. (unsigned char)dcl->val;
  6278. break;
  6279. case PROP_BORDER_W:
  6280. set_border_side_w(st, 0, dcl->val);
  6281. set_border_side_w(st, 1, dcl->val);
  6282. set_border_side_w(st, 2, dcl->val);
  6283. set_border_side_w(st, 3, dcl->val);
  6284. break;
  6285. case PROP_BORDER_COLOR:
  6286. set_border_side_c(st, 0, dcl->val);
  6287. set_border_side_c(st, 1, dcl->val);
  6288. set_border_side_c(st, 2, dcl->val);
  6289. set_border_side_c(st, 3, dcl->val);
  6290. break;
  6291. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  6292. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  6293. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  6294. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  6295. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  6296. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  6297. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  6298. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  6299. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  6300. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  6301. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  6302. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  6303. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  6304. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  6305. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  6306. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  6307. case PROP_RADIUS_TL:
  6308. case PROP_RADIUS_TR:
  6309. case PROP_RADIUS_BR:
  6310. case PROP_RADIUS_BL:
  6311. st->radius[dcl->prop - PROP_RADIUS_TL] =
  6312. (unsigned char)dcl->val;
  6313. break;
  6314. case PROP_LIST_NONE:
  6315. st->no_bullet = (unsigned char)(dcl->val != 0);
  6316. break;
  6317. case PROP_LIST_STYLE:
  6318. st->list_style = (unsigned char)dcl->val;
  6319. break;
  6320. case PROP_NOWRAP:
  6321. st->nowrap = (unsigned char)(dcl->val != 0);
  6322. break;
  6323. case PROP_WIDTH:
  6324. st->width = (unsigned short)dcl->val;
  6325. break;
  6326. case PROP_WIDTH_KIND:
  6327. st->width_kind = (unsigned char)dcl->val;
  6328. break;
  6329. case PROP_WIDTH_CALC_PCT:
  6330. st->width_calc_pct = dcl->val;
  6331. break;
  6332. case PROP_WIDTH_CALC_PX:
  6333. st->width_calc_px = dcl->val;
  6334. break;
  6335. case PROP_MAXW:
  6336. st->max_width = (unsigned short)dcl->val;
  6337. break;
  6338. case PROP_MAXW_KIND:
  6339. st->max_width_kind = (unsigned char)dcl->val;
  6340. break;
  6341. case PROP_MAXW_CALC_PCT:
  6342. st->max_width_calc_pct = (signed short)dcl->val;
  6343. break;
  6344. case PROP_MAXW_CALC_PX:
  6345. st->max_width_calc_px = (signed short)dcl->val;
  6346. break;
  6347. case PROP_MINW:
  6348. st->min_width = (unsigned short)dcl->val;
  6349. break;
  6350. case PROP_MINW_KIND:
  6351. st->min_width_kind = (unsigned char)dcl->val;
  6352. break;
  6353. case PROP_MINW_CALC_PCT:
  6354. st->min_width_calc_pct = (signed short)dcl->val;
  6355. break;
  6356. case PROP_MINW_CALC_PX:
  6357. st->min_width_calc_px = (signed short)dcl->val;
  6358. break;
  6359. case PROP_HEIGHT:
  6360. st->height = (unsigned short)dcl->val;
  6361. break;
  6362. case PROP_HEIGHT_KIND:
  6363. st->height_kind = (unsigned char)dcl->val;
  6364. break;
  6365. case PROP_HEIGHT_CALC_PCT:
  6366. st->height_calc_pct = (signed short)dcl->val;
  6367. break;
  6368. case PROP_HEIGHT_CALC_PX:
  6369. st->height_calc_px = (signed short)dcl->val;
  6370. break;
  6371. case PROP_MINH:
  6372. st->min_height = (unsigned short)dcl->val;
  6373. break;
  6374. case PROP_MINH_KIND:
  6375. st->min_height_kind = (unsigned char)dcl->val;
  6376. break;
  6377. case PROP_MINH_CALC_PCT:
  6378. st->min_height_calc_pct = (signed short)dcl->val;
  6379. break;
  6380. case PROP_MINH_CALC_PX:
  6381. st->min_height_calc_px = (signed short)dcl->val;
  6382. break;
  6383. case PROP_MAXH:
  6384. st->max_height = (unsigned short)dcl->val;
  6385. break;
  6386. case PROP_MAXH_KIND:
  6387. st->max_height_kind = (unsigned char)dcl->val;
  6388. break;
  6389. case PROP_MAXH_CALC_PCT:
  6390. st->max_height_calc_pct = (signed short)dcl->val;
  6391. break;
  6392. case PROP_MAXH_CALC_PX:
  6393. st->max_height_calc_px = (signed short)dcl->val;
  6394. break;
  6395. case PROP_LH_PCT:
  6396. st->line_h_pct = (unsigned char)dcl->val;
  6397. break;
  6398. case PROP_LH_PX:
  6399. st->line_h_px = (unsigned short)dcl->val;
  6400. break;
  6401. case PROP_UPPER:
  6402. st->uppercase = (unsigned char)(dcl->val != 0);
  6403. break;
  6404. case PROP_FLOAT:
  6405. st->float_left = (unsigned char)dcl->val;
  6406. break;
  6407. case PROP_CLEAR:
  6408. st->clear = (unsigned char)dcl->val;
  6409. break;
  6410. case PROP_FLEX_GROW:
  6411. st->flex_grow = (unsigned char)dcl->val;
  6412. break;
  6413. case PROP_FLEX_COL:
  6414. st->flex_col = (unsigned char)(dcl->val != 0);
  6415. break;
  6416. case PROP_FLEX_WRAP:
  6417. st->flex_wrap = (unsigned char)(dcl->val != 0);
  6418. break;
  6419. case PROP_JUSTIFY:
  6420. st->justify = (unsigned char)dcl->val;
  6421. break;
  6422. case PROP_ALIGN_ITEMS:
  6423. st->align_items = (unsigned char)dcl->val;
  6424. break;
  6425. case PROP_GRID_NCOLS:
  6426. st->grid_ncols = (unsigned char)dcl->val;
  6427. break;
  6428. case PROP_GRID_GAP:
  6429. st->grid_gap = (unsigned char)dcl->val;
  6430. break;
  6431. case PROP_GRID_SLOT:
  6432. st->grid_slot = (unsigned char)dcl->val;
  6433. break;
  6434. case PROP_GRID_AUTO:
  6435. st->grid_auto = (unsigned char)(dcl->val != 0);
  6436. break;
  6437. case PROP_POS:
  6438. st->pos = (unsigned char)dcl->val;
  6439. break;
  6440. case PROP_LEFT:
  6441. st->left = dcl->val;
  6442. break;
  6443. case PROP_TOP:
  6444. st->top = dcl->val;
  6445. break;
  6446. case PROP_RIGHT:
  6447. st->right = dcl->val;
  6448. break;
  6449. case PROP_BOTTOM:
  6450. st->bottom = dcl->val;
  6451. break;
  6452. case PROP_LEFT_SET:
  6453. st->left_set = (unsigned char)(dcl->val != 0);
  6454. break;
  6455. case PROP_TOP_SET:
  6456. st->top_set = (unsigned char)(dcl->val != 0);
  6457. break;
  6458. case PROP_RIGHT_SET:
  6459. st->right_set = (unsigned char)(dcl->val != 0);
  6460. break;
  6461. case PROP_BOTTOM_SET:
  6462. st->bottom_set = (unsigned char)(dcl->val != 0);
  6463. break;
  6464. case PROP_ZINDEX:
  6465. st->z_index = dcl->val;
  6466. break;
  6467. case PROP_ZSET:
  6468. st->z_set = (unsigned char)(dcl->val != 0);
  6469. break;
  6470. case PROP_TRANSFORM_X:
  6471. st->tx = dcl->val;
  6472. break;
  6473. case PROP_TRANSFORM_Y:
  6474. st->ty = dcl->val;
  6475. break;
  6476. case PROP_TRANSFORM_SET:
  6477. st->transform_set = (unsigned char)(dcl->val != 0);
  6478. break;
  6479. case PROP_TRANSFORM_PAINT:
  6480. st->transform_scale = (unsigned char)(dcl->val != 0);
  6481. break;
  6482. case PROP_SCALE_X:
  6483. st->scale_x = (signed short)dcl->val;
  6484. break;
  6485. case PROP_SCALE_Y:
  6486. st->scale_y = (signed short)dcl->val;
  6487. break;
  6488. case PROP_ROTATE:
  6489. st->rotate_x100 = (signed short)dcl->val;
  6490. break;
  6491. case PROP_ORIGIN_X_KIND:
  6492. st->origin_x_kind = (unsigned char)dcl->val;
  6493. break;
  6494. case PROP_ORIGIN_X:
  6495. st->origin_x = (signed short)dcl->val;
  6496. break;
  6497. case PROP_ORIGIN_Y_KIND:
  6498. st->origin_y_kind = (unsigned char)dcl->val;
  6499. break;
  6500. case PROP_ORIGIN_Y:
  6501. st->origin_y = (signed short)dcl->val;
  6502. break;
  6503. default:
  6504. break;
  6505. }
  6506. }
  6507. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  6508. int spec, short *best, short *best_imp)
  6509. {
  6510. unsigned char p = dcl->prop;
  6511. if (p >= PROP_COUNT)
  6512. return;
  6513. if (dcl->important) {
  6514. if (spec >= best_imp[p]) {
  6515. apply_decl(st, dcl);
  6516. best_imp[p] = (short)spec;
  6517. }
  6518. } else if (best_imp[p] < 0 && spec >= best[p]) {
  6519. apply_decl(st, dcl);
  6520. best[p] = (short)spec;
  6521. }
  6522. }
  6523. void br_style_for(struct br_doc *d, unsigned int node,
  6524. const struct br_style *parent, struct br_style *out)
  6525. {
  6526. unsigned int tag;
  6527. short best[PROP_COUNT], best_imp[PROP_COUNT];
  6528. unsigned int i;
  6529. if (d->nodes[node].style_valid) {
  6530. *out = d->nodes[node].style;
  6531. return;
  6532. }
  6533. *out = *parent;
  6534. out->has_bg = 0;
  6535. out->bg_image = 0;
  6536. out->bg_repeat = 0;
  6537. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  6538. out->bg_pos_x = out->bg_pos_y = 0;
  6539. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  6540. out->float_left = 0;
  6541. out->clear = 0;
  6542. out->list_item = 0;
  6543. out->font_unit = 0;
  6544. for (i = 0; i < 4; i++) {
  6545. out->border_w[i] = 0;
  6546. out->border_c[i] = 0;
  6547. out->border_s[i] = BR_BORDER_SOLID;
  6548. out->radius[i] = 0;
  6549. }
  6550. out->width = out->max_width = out->min_width = out->height = 0;
  6551. out->min_height = out->max_height = 0;
  6552. out->width_calc_pct = out->width_calc_px = 0;
  6553. out->height_calc_pct = out->height_calc_px = 0;
  6554. out->min_height_calc_pct = out->min_height_calc_px = 0;
  6555. out->max_height_calc_pct = out->max_height_calc_px = 0;
  6556. out->min_width_calc_pct = out->min_width_calc_px = 0;
  6557. out->max_width_calc_pct = out->max_width_calc_px = 0;
  6558. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  6559. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  6560. out->box_sizing = 0;
  6561. out->ml_auto = out->mr_auto = 0;
  6562. out->mt = out->mr = out->mb = out->ml = 0;
  6563. out->pt = out->pr = out->pb = out->pl = 0;
  6564. for (i = 0; i < 8; i++) {
  6565. out->mp_unit[i] = 0;
  6566. out->mp_calc_px[i] = 0;
  6567. }
  6568. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  6569. out->justify = out->align_items = 0;
  6570. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  6571. out->grid_auto = 0;
  6572. out->pos = out->left_set = out->top_set = 0;
  6573. out->right_set = out->bottom_set = 0;
  6574. out->left = out->top = out->right = out->bottom = 0;
  6575. out->z_index = 0;
  6576. out->z_set = 0;
  6577. out->tx = out->ty = 0;
  6578. out->transform_set = 0;
  6579. out->transform_scale = 0;
  6580. out->scale_x = out->scale_y = 100;
  6581. out->rotate_x100 = 0;
  6582. out->origin_x = out->origin_y = 50;
  6583. out->origin_x_kind = out->origin_y_kind = 1;
  6584. out->vis_hidden = out->opacity_hidden = 0;
  6585. out->overflow_hidden = 0;
  6586. tag = d->nodes[node].tag;
  6587. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  6588. BR_DISPLAY_INLINE;
  6589. for (i = 0; i < PROP_COUNT; i++) {
  6590. best[i] = -1;
  6591. best_imp[i] = -1;
  6592. }
  6593. switch (tag) {
  6594. case BR_TAG_HEAD:
  6595. case BR_TAG_TITLE:
  6596. case BR_TAG_META:
  6597. case BR_TAG_LINK:
  6598. case BR_TAG_BASE:
  6599. case BR_TAG_SCRIPT:
  6600. case BR_TAG_STYLE:
  6601. out->display = BR_DISPLAY_NONE;
  6602. break;
  6603. case BR_TAG_BODY:
  6604. out->mt = out->mb = out->ml = out->mr = 4;
  6605. break;
  6606. case BR_TAG_P:
  6607. out->mt = out->mb = 8;
  6608. break;
  6609. case BR_TAG_H1:
  6610. out->font_px = 32; out->bold = 1;
  6611. out->mt = 10; out->mb = 6;
  6612. break;
  6613. case BR_TAG_H2:
  6614. out->font_px = 24; out->bold = 1;
  6615. out->mt = 8; out->mb = 4;
  6616. break;
  6617. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  6618. out->bold = 1; out->mt = 6; out->mb = 3;
  6619. if (tag == BR_TAG_H3)
  6620. out->font_px = 19;
  6621. else if (tag == BR_TAG_H4)
  6622. out->font_px = 17;
  6623. else if (tag == BR_TAG_H5)
  6624. out->font_px = 15;
  6625. else
  6626. out->font_px = 13;
  6627. break;
  6628. case BR_TAG_UL: case BR_TAG_OL:
  6629. out->mt = out->mb = 6; out->pl = 20;
  6630. break;
  6631. case BR_TAG_LI:
  6632. out->mt = out->mb = 2;
  6633. out->list_item = 1;
  6634. break;
  6635. case BR_TAG_BLOCKQUOTE:
  6636. out->mt = out->mb = 6; out->ml = out->mr = 16;
  6637. break;
  6638. case BR_TAG_PRE:
  6639. out->mt = out->mb = 8; out->pl = 8;
  6640. out->has_bg = 1; out->bg = theme_panel;
  6641. out->mono = 1;
  6642. break;
  6643. case BR_TAG_CENTER:
  6644. out->align = BR_ALIGN_CENTER;
  6645. break;
  6646. case BR_TAG_A:
  6647. out->color = theme_link; out->underline = 1;
  6648. break;
  6649. case BR_TAG_B: case BR_TAG_STRONG:
  6650. out->bold = 1;
  6651. break;
  6652. case BR_TAG_I: case BR_TAG_EM:
  6653. out->italic = 1;
  6654. break;
  6655. case BR_TAG_U:
  6656. out->underline = 1;
  6657. break;
  6658. case BR_TAG_S: case BR_TAG_STRIKE:
  6659. out->strike = 1;
  6660. break;
  6661. case BR_TAG_CODE:
  6662. out->color = 0x8010;
  6663. out->mono = 1;
  6664. break;
  6665. case BR_TAG_SMALL:
  6666. out->font_px = 13;
  6667. break;
  6668. case BR_TAG_BIG:
  6669. out->font_px = 20;
  6670. break;
  6671. case BR_TAG_TD: case BR_TAG_TH:
  6672. out->pr = 12;
  6673. /* A cell establishes left-aligned content even inside a legacy
  6674. * <center> (HN's outer page wrapper); author CSS/align below
  6675. * can still override this. */
  6676. out->align = BR_ALIGN_LEFT;
  6677. break;
  6678. case BR_TAG_VIDEO:
  6679. out->display = BR_DISPLAY_BLOCK;
  6680. break;
  6681. case BR_TAG_SVG:
  6682. /* The renderer treats an SVG as one replaced box; keeping it
  6683. * block-level also gives it a usable intrinsic width inside
  6684. * flex items such as a header logo. */
  6685. out->display = BR_DISPLAY_BLOCK;
  6686. break;
  6687. default:
  6688. break;
  6689. }
  6690. /* Legacy bgcolor/align attributes (tables/cells/body) are
  6691. * presentational hints; author CSS still wins because it is applied
  6692. * below. */
  6693. {
  6694. unsigned short bgc;
  6695. const char *bga = br_find_attr(d, node, "bgcolor");
  6696. const char *ala = br_find_attr(d, node, "align");
  6697. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  6698. out->bg = bgc;
  6699. out->has_bg = 1;
  6700. }
  6701. if (ala) {
  6702. if (same_string(ala, "right"))
  6703. out->align = BR_ALIGN_RIGHT;
  6704. else if (same_string(ala, "center"))
  6705. out->align = BR_ALIGN_CENTER;
  6706. else if (same_string(ala, "left"))
  6707. out->align = BR_ALIGN_LEFT;
  6708. }
  6709. }
  6710. /* presentational <font color>: author CSS below still wins */
  6711. if (d->nodes[node].color_off) {
  6712. unsigned short c;
  6713. const char *v = attr_ptr(d, d->nodes[node].color_off);
  6714. if (parse_color(v, s_len(v), &c))
  6715. out->color = c;
  6716. }
  6717. /* SVG fill/stroke attributes; CSS declarations still win below. */
  6718. {
  6719. const char *fa = br_find_attr(d, node, "fill");
  6720. const char *sa = br_find_attr(d, node, "stroke");
  6721. unsigned short c;
  6722. if (fa && parse_color(fa, s_len(fa), &c)) {
  6723. out->bg = c;
  6724. out->has_bg = 1;
  6725. out->color = c;
  6726. }
  6727. if (sa && parse_color(sa, s_len(sa), &c)) {
  6728. out->border_c[0] = c;
  6729. out->border_w[0] = 1;
  6730. }
  6731. }
  6732. /* UA behaviour for a closed <details>: only its <summary> is shown.
  6733. * Author CSS below can still override this. */
  6734. if (tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  6735. unsigned int p = d->nodes[node].parent;
  6736. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  6737. !br_find_attr(d, p, "open"))
  6738. out->display = BR_DISPLAY_NONE;
  6739. }
  6740. rule_collect(d, node);
  6741. for (i = 0; i < nrule_matches; i++) {
  6742. struct br_rule *r = &d->rules[rule_matches[i].idx];
  6743. int spec = rule_matches[i].spec;
  6744. unsigned int k;
  6745. for (k = 0; k < r->nd; k++)
  6746. cascade_decl(out, &r->d[k], spec, best, best_imp);
  6747. }
  6748. if (d->nodes[node].style_off) {
  6749. struct br_decl decls[BR_DECLS];
  6750. unsigned int nd, k;
  6751. const char *v = attr_ptr(d, d->nodes[node].style_off);
  6752. parse_decls(d, v, s_len(v), decls, &nd);
  6753. for (k = 0; k < nd; k++)
  6754. cascade_decl(out, &decls[k], 1000, best, best_imp);
  6755. }
  6756. if (out->font_unit) {
  6757. int px = out->font_raw;
  6758. if (out->font_unit == 2)
  6759. px = parent->font_px * out->font_raw / 100;
  6760. else if (out->font_unit == 3)
  6761. px = root_font_px * out->font_raw / 100;
  6762. else if (out->font_unit == 4)
  6763. px = parent->font_px * out->font_raw / 10000;
  6764. if (px < 8)
  6765. px = 8;
  6766. if (px > 72)
  6767. px = 72;
  6768. out->font_px = (unsigned char)px;
  6769. }
  6770. resolve_size_units(out);
  6771. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  6772. out->display = BR_DISPLAY_NONE;
  6773. if (out->vis_hidden || out->opacity_hidden ||
  6774. (out->overflow_hidden && out->height_kind == 1 &&
  6775. out->height == 0))
  6776. out->display = BR_DISPLAY_NONE;
  6777. d->nodes[node].style = *out;
  6778. d->nodes[node].style_valid = 1;
  6779. }
  6780. /* ---- HTML parser ---- */
  6781. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  6782. unsigned int sp, int *pre)
  6783. {
  6784. unsigned int i;
  6785. *pre = 0;
  6786. for (i = 1; i <= sp; i++)
  6787. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  6788. (*pre)++;
  6789. }
  6790. static void implicit_close(struct br_doc *d, unsigned short *stack,
  6791. unsigned int *sp, unsigned int tag)
  6792. {
  6793. while (*sp > 0) {
  6794. unsigned int top = d->nodes[stack[*sp]].tag;
  6795. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  6796. (*sp)--;
  6797. continue;
  6798. }
  6799. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  6800. (*sp)--;
  6801. continue;
  6802. }
  6803. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  6804. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  6805. (*sp)--;
  6806. continue;
  6807. }
  6808. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  6809. (*sp)--;
  6810. continue;
  6811. }
  6812. break;
  6813. }
  6814. }
  6815. static int has_attr_token(const char *s, unsigned int len, const char *name)
  6816. {
  6817. unsigned int i, n = s_len(name);
  6818. for (i = 0; i + n <= len; i++) {
  6819. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  6820. continue;
  6821. if (!ci_eq_n(s + i, name, n))
  6822. continue;
  6823. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  6824. return 1;
  6825. }
  6826. return 0;
  6827. }
  6828. static int stack_has_svg(const struct br_doc *d,
  6829. const unsigned short *stack, unsigned int sp)
  6830. {
  6831. unsigned int k;
  6832. for (k = 0; k <= sp && k < 64; k++)
  6833. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  6834. return 1;
  6835. return 0;
  6836. }
  6837. /* Skip an element subtree by finding its closing tag. Used for SVG
  6838. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  6839. * overwrite the document title. */
  6840. static unsigned int skip_element(const char *html, unsigned int len,
  6841. unsigned int i, const char *name,
  6842. unsigned int n)
  6843. {
  6844. char close[40];
  6845. unsigned int k = 0, p;
  6846. close[k++] = '<';
  6847. close[k++] = '/';
  6848. while (k < sizeof(close) - 1 && n--)
  6849. close[k++] = *name++;
  6850. p = (unsigned int)find_ci(html, len, close, k, i);
  6851. if ((int)p < 0)
  6852. return len;
  6853. p += k;
  6854. while (p < len && html[p] != '>')
  6855. p++;
  6856. if (p < len)
  6857. p++;
  6858. return p;
  6859. }
  6860. /* Store a <script src=...> value in the attribute arena. Kept separate from
  6861. * parse_attrs() so script-only attributes cannot be confused with the
  6862. * value/d attributes that also use src_off on other elements. */
  6863. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  6864. unsigned int len)
  6865. {
  6866. unsigned int i = 0;
  6867. while (i < len) {
  6868. unsigned int ns, ne, vs, ve;
  6869. char quote = 0;
  6870. while (i < len && is_space(s[i]))
  6871. i++;
  6872. if (i >= len || s[i] == '/')
  6873. break;
  6874. ns = i;
  6875. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  6876. i++;
  6877. ne = i;
  6878. while (i < len && is_space(s[i]))
  6879. i++;
  6880. if (i >= len || s[i] != '=')
  6881. continue;
  6882. i++;
  6883. while (i < len && is_space(s[i]))
  6884. i++;
  6885. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6886. quote = s[i++];
  6887. vs = i;
  6888. while (i < len && s[i] != quote)
  6889. i++;
  6890. ve = i;
  6891. if (i < len)
  6892. i++;
  6893. } else {
  6894. vs = i;
  6895. while (i < len && !is_space(s[i]) && s[i] != '>')
  6896. i++;
  6897. ve = i;
  6898. }
  6899. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  6900. return store_attr(d, s + vs, ve - vs, 400);
  6901. }
  6902. return 0;
  6903. }
  6904. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  6905. * application/json, text/template, ...) and non-JS type values. External
  6906. * src scripts are allowed; the browser fetches them separately. */
  6907. static int script_is_javascript(const char *s, unsigned int len)
  6908. {
  6909. unsigned int i = 0;
  6910. while (i < len) {
  6911. unsigned int ns, ne, vs = 0, ve = 0;
  6912. char quote = 0;
  6913. int has_val = 0;
  6914. while (i < len && is_space(s[i]))
  6915. i++;
  6916. if (i >= len)
  6917. break;
  6918. ns = i;
  6919. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  6920. i++;
  6921. ne = i;
  6922. while (i < len && is_space(s[i]))
  6923. i++;
  6924. if (i < len && s[i] == '=') {
  6925. i++;
  6926. while (i < len && is_space(s[i]))
  6927. i++;
  6928. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6929. quote = s[i++];
  6930. vs = i;
  6931. while (i < len && s[i] != quote)
  6932. i++;
  6933. ve = i;
  6934. if (i < len)
  6935. i++;
  6936. } else {
  6937. vs = i;
  6938. while (i < len && !is_space(s[i]) && s[i] != '/')
  6939. i++;
  6940. ve = i;
  6941. }
  6942. has_val = 1;
  6943. } else if (i == ne) {
  6944. i++;
  6945. }
  6946. if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
  6947. unsigned int vn = has_val ? ve - vs : 0;
  6948. if (vn == 0)
  6949. return 1;
  6950. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  6951. (vn == 22 &&
  6952. ci_eq_n(s + vs, "application/javascript", 22)) ||
  6953. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  6954. (vn == 22 &&
  6955. ci_eq_n(s + vs, "application/ecmascript", 22)))
  6956. return 1;
  6957. return 0;
  6958. }
  6959. }
  6960. return 1;
  6961. }
  6962. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  6963. {
  6964. unsigned short stack[64];
  6965. unsigned int sp = 0;
  6966. unsigned int i = 0;
  6967. int pre = 0;
  6968. unsigned int css_len = 0;
  6969. if (!d)
  6970. return -1;
  6971. if (!d->nodes)
  6972. br_doc_init(d);
  6973. if (!d->nodes || !d->text || !d->attrs)
  6974. return -1;
  6975. doc_reset(d);
  6976. d->script_src = html;
  6977. stack[0] = 0;
  6978. d->nodes[0].type = BR_NODE_ELEM;
  6979. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6980. while (i < len) {
  6981. if (html[i] != '<') {
  6982. unsigned int st = i;
  6983. while (i < len && html[i] != '<')
  6984. i++;
  6985. if (sp <= 63)
  6986. append_text(d, stack[sp], html + st, i - st, pre);
  6987. continue;
  6988. }
  6989. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  6990. html[i + 3] == '-') {
  6991. int p = find_ci(html, len, "-->", 3, i + 4);
  6992. i = p >= 0 ? (unsigned int)p + 3 : len;
  6993. continue;
  6994. }
  6995. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  6996. while (i < len && html[i] != '>')
  6997. i++;
  6998. if (i < len)
  6999. i++;
  7000. continue;
  7001. }
  7002. if (i + 1 < len && html[i + 1] == '/') {
  7003. unsigned int ns = i + 2, ne, tag, k, n;
  7004. char name[32];
  7005. while (ns < len && is_space(html[ns]))
  7006. ns++;
  7007. ne = ns;
  7008. while (ne < len && is_ident(html[ne]))
  7009. ne++;
  7010. if (ne == ns) {
  7011. i++;
  7012. continue;
  7013. }
  7014. n = ne - ns;
  7015. if (n >= sizeof(name))
  7016. n = sizeof(name) - 1;
  7017. for (k = 0; k < n; k++)
  7018. name[k] = html[ns + k];
  7019. name[n] = '\0';
  7020. tag = br_tag_from_name(name);
  7021. i = ne;
  7022. while (i < len && html[i] != '>')
  7023. i++;
  7024. if (i < len)
  7025. i++;
  7026. for (k = sp; k > 0; k--) {
  7027. struct br_node *nd = &d->nodes[stack[k]];
  7028. if (tag == BR_TAG_UNKNOWN) {
  7029. const char *raw = attr_ptr(d, nd->name_off);
  7030. if (nd->tag == BR_TAG_UNKNOWN &&
  7031. ci_eq_n(raw, name, n) &&
  7032. raw[n] == '\0') {
  7033. sp = k - 1;
  7034. break;
  7035. }
  7036. } else if (nd->tag == tag) {
  7037. sp = k - 1;
  7038. break;
  7039. }
  7040. }
  7041. stack_recount_pre(d, stack, sp, &pre);
  7042. continue;
  7043. }
  7044. /* start tag */
  7045. {
  7046. unsigned int ns = i + 1, ne, tag, k, n;
  7047. char name[16];
  7048. unsigned int attr_start, attr_end;
  7049. int selfclose = 0;
  7050. while (ns < len && is_space(html[ns]))
  7051. ns++;
  7052. ne = ns;
  7053. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  7054. ne++;
  7055. if (ne == ns) {
  7056. /* stray '<' */
  7057. append_text(d, stack[sp], html + i, 1, pre);
  7058. i++;
  7059. continue;
  7060. }
  7061. n = ne - ns;
  7062. if (n >= sizeof(name))
  7063. n = sizeof(name) - 1;
  7064. for (k = 0; k < n; k++)
  7065. name[k] = html[ns + k];
  7066. name[n] = '\0';
  7067. tag = br_tag_from_name(name);
  7068. attr_start = ne;
  7069. {
  7070. unsigned int q = ne;
  7071. char quote = 0;
  7072. while (q < len) {
  7073. char c = html[q];
  7074. if (quote) {
  7075. if (c == quote)
  7076. quote = 0;
  7077. } else if (c == '"' || c == '\'') {
  7078. quote = c;
  7079. } else if (c == '>') {
  7080. break;
  7081. }
  7082. q++;
  7083. }
  7084. attr_end = q;
  7085. if (q > attr_start && html[q - 1] == '/')
  7086. selfclose = 1;
  7087. i = q < len ? q + 1 : len;
  7088. }
  7089. if (tag == BR_TAG_SCRIPT) {
  7090. int p = find_ci(html, len, "</script", 8, i);
  7091. unsigned int end = p >= 0 ? (unsigned int)p : len;
  7092. unsigned int idx;
  7093. if (script_is_javascript(html + attr_start,
  7094. attr_end - attr_start)) {
  7095. if (d->nscripts < BR_MAX_SCRIPTS) {
  7096. struct br_script *script =
  7097. &d->scripts[d->nscripts++];
  7098. script->off = i;
  7099. script->len = end > i ? end - i : 0;
  7100. script->src_off = script_src_attr(
  7101. d, html + attr_start,
  7102. attr_end - attr_start);
  7103. } else {
  7104. d->scripts_truncated = 1;
  7105. }
  7106. }
  7107. /* Keep the element in the DOM even though its body is
  7108. * executed separately: scripts (Piwik, analytics)
  7109. * insert a sibling after getElementsByTagName('script')
  7110. * [0], and the element must exist for that to work. */
  7111. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  7112. (unsigned char)tag);
  7113. if (idx)
  7114. parse_attrs(d, &d->nodes[idx],
  7115. html + attr_start,
  7116. attr_end - attr_start);
  7117. i = p >= 0 ? (unsigned int)p + 8 : len;
  7118. while (i < len && html[i] != '>')
  7119. i++;
  7120. if (i < len)
  7121. i++;
  7122. continue;
  7123. }
  7124. if (stack_has_svg(d, stack, sp) &&
  7125. (tag == BR_TAG_TITLE ||
  7126. (tag == BR_TAG_UNKNOWN &&
  7127. (ci_eq_n(name, "desc", 4) ||
  7128. ci_eq_n(name, "metadata", 8))))) {
  7129. i = skip_element(html, len, i, name, n);
  7130. continue;
  7131. }
  7132. /* A closed <details> is collapsed at style time, not
  7133. * parse time: the <summary> (the hamburger button on
  7134. * tagesschau and many mobile menus) must stay in the
  7135. * DOM. */
  7136. if (tag == BR_TAG_STYLE) {
  7137. int p = find_ci(html, len, "</style", 7, i);
  7138. unsigned int end = p >= 0 ? (unsigned int)p : len;
  7139. unsigned int cend = end;
  7140. unsigned int idx;
  7141. while (cend > i && is_space(html[cend - 1]))
  7142. cend--;
  7143. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  7144. for (k = i; k < cend; k++)
  7145. css_store[css_len++] = html[k];
  7146. css_store[css_len++] = '\n';
  7147. }
  7148. /* The CSS is collected above, but keep a display:none
  7149. * element so getElementsByTagName('style')/parentNode
  7150. * behave like a real document. */
  7151. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  7152. (unsigned char)tag);
  7153. if (idx)
  7154. parse_attrs(d, &d->nodes[idx],
  7155. html + attr_start,
  7156. attr_end - attr_start);
  7157. i = p >= 0 ? (unsigned int)p + 7 : len;
  7158. while (i < len && html[i] != '>')
  7159. i++;
  7160. if (i < len)
  7161. i++;
  7162. continue;
  7163. }
  7164. if (tag == BR_TAG_TITLE) {
  7165. int p = find_ci(html, len, "</title", 7, i);
  7166. unsigned int end = p >= 0 ? (unsigned int)p : len;
  7167. unsigned int tn = 0;
  7168. char tmp[BR_TITLE_MAX];
  7169. tn = decode_text(html + i, end - i, tmp,
  7170. sizeof(tmp), 1);
  7171. (void)tn;
  7172. s_copy(d->title, tmp, sizeof(d->title));
  7173. i = p >= 0 ? (unsigned int)p + 7 : len;
  7174. while (i < len && html[i] != '>')
  7175. i++;
  7176. if (i < len)
  7177. i++;
  7178. continue;
  7179. }
  7180. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  7181. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  7182. unsigned int idx;
  7183. implicit_close(d, stack, &sp, tag);
  7184. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  7185. (unsigned char)tag);
  7186. if (idx)
  7187. parse_attrs(d, &d->nodes[idx],
  7188. html + attr_start,
  7189. attr_end - attr_start);
  7190. continue;
  7191. }
  7192. if (sp >= 63) {
  7193. d->truncated = 1;
  7194. continue;
  7195. }
  7196. implicit_close(d, stack, &sp, tag);
  7197. {
  7198. unsigned int idx = node_append(d, stack[sp],
  7199. BR_NODE_ELEM,
  7200. (unsigned char)tag);
  7201. if (!idx)
  7202. continue;
  7203. parse_attrs(d, &d->nodes[idx],
  7204. html + attr_start,
  7205. attr_end - attr_start);
  7206. if (tag == BR_TAG_UNKNOWN)
  7207. d->nodes[idx].name_off =
  7208. store_attr(d, name, n, 31);
  7209. if (!selfclose) {
  7210. sp++;
  7211. stack[sp] = (unsigned short)idx;
  7212. if (tag == BR_TAG_PRE)
  7213. pre++;
  7214. }
  7215. }
  7216. }
  7217. }
  7218. d->inline_css_len = css_len;
  7219. return 0;
  7220. }
  7221. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  7222. {
  7223. if (!d->nodes)
  7224. br_doc_init(d);
  7225. if (!d->nodes || !d->text || !d->attrs)
  7226. return -1;
  7227. doc_reset(d);
  7228. d->plain = 1;
  7229. d->nodes[0].type = BR_NODE_ELEM;
  7230. d->nodes[0].tag = BR_TAG_UNKNOWN;
  7231. append_text(d, 0, text, len, 1);
  7232. return 0;
  7233. }
  7234. /* A standalone image response: a synthetic document with one <img>. The
  7235. * caller decodes the already-fetched body into image slot 0 and sets the
  7236. * returned node's img_index. */
  7237. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  7238. {
  7239. unsigned int idx;
  7240. if (!d->nodes)
  7241. br_doc_init(d);
  7242. if (!d->nodes || !d->text || !d->attrs)
  7243. return 0;
  7244. doc_reset(d);
  7245. d->nodes[0].type = BR_NODE_ELEM;
  7246. d->nodes[0].tag = BR_TAG_UNKNOWN;
  7247. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  7248. if (!idx)
  7249. return 0;
  7250. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  7251. return idx;
  7252. }