browser_html.c 211 KB

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