browser_html.c 213 KB

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