browser_html.c 185 KB

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