browser_html.c 214 KB

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