browser_html.c 193 KB

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