browser_html.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472
  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. static int is_space(char c);
  6. static int is_ident(char c);
  7. /* ---- storage (one document per process) ---- */
  8. static struct br_node nodes_store[BR_MAX_NODES];
  9. static char text_store[BR_TEXT_SIZE];
  10. static char attr_store[BR_ATTR_SIZE];
  11. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  12. static char css_store[BR_CSS_SIZE];
  13. static int root_font_px = 16;
  14. /* ---- tag table ---- */
  15. struct tag_name {
  16. const char *name;
  17. unsigned char tag;
  18. };
  19. static const struct tag_name tag_names[] = {
  20. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  21. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  22. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  23. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  24. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  25. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  26. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  27. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  28. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  29. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  30. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  31. { "picture", BR_TAG_PICTURE },
  32. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  33. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  34. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  35. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  36. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  37. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  38. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  39. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  40. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  41. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  42. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  43. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  44. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  45. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  46. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  47. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  48. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  49. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  50. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  51. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  52. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  53. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  54. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  55. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  56. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  57. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  58. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  59. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  60. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  61. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  62. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  63. { 0, 0 }
  64. };
  65. const char *br_tag_name(unsigned int tag)
  66. {
  67. unsigned int i;
  68. for (i = 0; tag_names[i].name; i++)
  69. if (tag_names[i].tag == tag)
  70. return tag_names[i].name;
  71. return "?";
  72. }
  73. static unsigned int s_len(const char *s);
  74. #define TAG_HASH_SIZE 128
  75. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  76. static int tag_hash_ready;
  77. static unsigned int tag_hash_key(const char *name, unsigned int len)
  78. {
  79. unsigned int i, h = 2166136261u;
  80. for (i = 0; i < len; i++) {
  81. h ^= (unsigned char)name[i];
  82. h *= 16777619u;
  83. }
  84. return h & (TAG_HASH_SIZE - 1);
  85. }
  86. static void tag_hash_build(void)
  87. {
  88. unsigned int i;
  89. for (i = 0; i < TAG_HASH_SIZE; i++)
  90. tag_hash_idx[i] = 0xFF;
  91. for (i = 0; tag_names[i].name; i++) {
  92. unsigned int h = tag_hash_key(tag_names[i].name,
  93. s_len(tag_names[i].name));
  94. while (tag_hash_idx[h] != 0xFF)
  95. h = (h + 1) & (TAG_HASH_SIZE - 1);
  96. tag_hash_idx[h] = (unsigned char)i;
  97. }
  98. tag_hash_ready = 1;
  99. }
  100. unsigned int br_tag_from_name(const char *name)
  101. {
  102. char lower[16];
  103. unsigned int i, len, h;
  104. unsigned char idx;
  105. for (i = 0; i < 15 && name[i]; i++) {
  106. char c = name[i];
  107. if (c >= 'A' && c <= 'Z')
  108. c = (char)(c - 'A' + 'a');
  109. lower[i] = c;
  110. }
  111. lower[i] = '\0';
  112. len = i;
  113. if (!tag_hash_ready)
  114. tag_hash_build();
  115. h = tag_hash_key(lower, len);
  116. for (;;) {
  117. idx = tag_hash_idx[h];
  118. if (idx == 0xFF)
  119. return BR_TAG_UNKNOWN;
  120. {
  121. const char *n = tag_names[idx].name;
  122. unsigned int j;
  123. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  124. ;
  125. if (!lower[j] && !n[j])
  126. return tag_names[idx].tag;
  127. }
  128. h = (h + 1) & (TAG_HASH_SIZE - 1);
  129. }
  130. }
  131. int br_tag_is_void(unsigned int tag)
  132. {
  133. switch (tag) {
  134. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  135. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  136. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  137. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  138. case BR_TAG_AREA: case BR_TAG_COL:
  139. return 1;
  140. default:
  141. return 0;
  142. }
  143. }
  144. int br_tag_is_block(unsigned int tag)
  145. {
  146. switch (tag) {
  147. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  148. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  149. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  150. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  151. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  152. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  153. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  154. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  155. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  156. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  157. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  158. case BR_TAG_PICTURE:
  159. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  160. case BR_TAG_ADDRESS: case BR_TAG_HR:
  161. return 1;
  162. default:
  163. return 0;
  164. }
  165. }
  166. /* ---- string / entity helpers ---- */
  167. static unsigned int s_len(const char *s)
  168. {
  169. unsigned int n = 0;
  170. while (s && s[n])
  171. n++;
  172. return n;
  173. }
  174. static void s_copy(char *d, const char *s, unsigned int cap)
  175. {
  176. unsigned int i = 0;
  177. if (!cap)
  178. return;
  179. while (s && s[i] && i + 1 < cap) {
  180. d[i] = s[i];
  181. i++;
  182. }
  183. d[i] = '\0';
  184. }
  185. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  186. {
  187. unsigned int i;
  188. for (i = 0; i < n; i++) {
  189. char ca = a[i], cb = b[i];
  190. if (ca >= 'A' && ca <= 'Z')
  191. ca = (char)(ca - 'A' + 'a');
  192. if (cb >= 'A' && cb <= 'Z')
  193. cb = (char)(cb - 'A' + 'a');
  194. if (ca != cb)
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. static int find_ci(const char *hay, unsigned int len, const char *needle,
  200. unsigned int nlen, unsigned int from)
  201. {
  202. unsigned int i;
  203. if (nlen == 0 || from >= len)
  204. return -1;
  205. for (i = from; i + nlen <= len; i++)
  206. if (ci_eq_n(hay + i, needle, nlen))
  207. return (int)i;
  208. return -1;
  209. }
  210. static const struct {
  211. const char *name;
  212. const char *text;
  213. } entities[] = {
  214. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  215. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  216. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  217. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  218. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  219. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  220. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  221. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  222. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  223. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  224. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  225. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  226. { 0, 0 }
  227. };
  228. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  229. {
  230. if (*n + 1 < cap)
  231. out[(*n)++] = c;
  232. }
  233. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  234. char *out, unsigned int cap, unsigned int *n)
  235. {
  236. unsigned int start = *pos + 1, i = start;
  237. unsigned int code = 0;
  238. int ok = 0;
  239. while (i < len && s[i] != ';' && i - start < 12)
  240. i++;
  241. if (i >= len || s[i] != ';') {
  242. /* Not an entity: emit the '&' and advance past it. Leaving
  243. * *pos unchanged here used to spin forever on bare '&'. */
  244. emit(out, cap, n, '&');
  245. *pos = start;
  246. return;
  247. }
  248. if (s[start] == '#') {
  249. unsigned int j = start + 1;
  250. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  251. j++;
  252. for (; j < i; j++) {
  253. char c = s[j];
  254. int d = -1;
  255. if (c >= '0' && c <= '9') d = c - '0';
  256. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  257. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  258. if (d < 0) break;
  259. code = code * 16 + (unsigned int)d;
  260. ok = 1;
  261. }
  262. } else {
  263. for (; j < i; j++) {
  264. if (s[j] < '0' || s[j] > '9')
  265. break;
  266. code = code * 10 + (unsigned int)(s[j] - '0');
  267. ok = 1;
  268. }
  269. }
  270. } else {
  271. unsigned int k;
  272. for (k = 0; entities[k].name; k++) {
  273. unsigned int el = s_len(entities[k].name);
  274. unsigned int j;
  275. if (el != i - start)
  276. continue;
  277. for (j = 0; j < el; j++)
  278. if (s[start + j] != entities[k].name[j])
  279. break;
  280. if (j == el) {
  281. const char *t = entities[k].text;
  282. while (*t)
  283. emit(out, cap, n, *t++);
  284. *pos = i + 1;
  285. return;
  286. }
  287. }
  288. }
  289. if (ok && code) {
  290. if (code == 0x00A0)
  291. emit(out, cap, n, ' ');
  292. else if (code == 0x2013 || code == 0x2014)
  293. emit(out, cap, n, '-');
  294. else if (code == 0x2018 || code == 0x2019)
  295. emit(out, cap, n, '\'');
  296. else if (code == 0x201C || code == 0x201D)
  297. emit(out, cap, n, '"');
  298. else if (code == 0x2026) {
  299. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  300. emit(out, cap, n, '.');
  301. } else if (code <= 0xFF) {
  302. emit(out, cap, n, (char)code);
  303. } else {
  304. emit(out, cap, n, '?');
  305. }
  306. } else {
  307. emit(out, cap, n, '?');
  308. }
  309. *pos = i + 1;
  310. }
  311. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  312. * space and drops leading spaces. Returns the output length. */
  313. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  314. unsigned int cap, int collapse)
  315. {
  316. unsigned int i = 0, n = 0;
  317. while (i < len) {
  318. unsigned char c = (unsigned char)s[i];
  319. if (c == '&') {
  320. decode_entity(s, len, &i, out, cap, &n);
  321. continue;
  322. }
  323. if (c < 0x80) {
  324. i++;
  325. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  326. c == '\r' || c == '\f')) {
  327. if (n > 0 && out[n - 1] == ' ')
  328. continue;
  329. emit(out, cap, &n, ' ');
  330. continue;
  331. }
  332. if (!collapse && c == '\t') {
  333. emit(out, cap, &n, ' ');
  334. emit(out, cap, &n, ' ');
  335. emit(out, cap, &n, ' ');
  336. emit(out, cap, &n, ' ');
  337. continue;
  338. }
  339. emit(out, cap, &n, (char)c);
  340. continue;
  341. }
  342. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  343. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  344. unsigned int cp = ((c & 0x1F) << 6) |
  345. ((unsigned char)s[i + 1] & 0x3F);
  346. i += 2;
  347. if (cp == 0xA0)
  348. cp = ' ';
  349. else if (cp == 0xB7)
  350. cp = '-';
  351. else if (cp == 0xD7)
  352. cp = 'x';
  353. else if (cp == 0xB0)
  354. cp = 'o';
  355. if (cp <= 0xFF)
  356. emit(out, cap, &n, (char)cp);
  357. else
  358. emit(out, cap, &n, '?');
  359. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  360. unsigned int cp = ((c & 0x0F) << 12) |
  361. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  362. ((unsigned char)s[i + 2] & 0x3F);
  363. i += 3;
  364. if (cp == 0x2013 || cp == 0x2014) {
  365. emit(out, cap, &n, '-');
  366. } else if (cp == 0x2018 || cp == 0x2019) {
  367. emit(out, cap, &n, '\'');
  368. } else if (cp == 0x201C || cp == 0x201D) {
  369. emit(out, cap, &n, '"');
  370. } else if (cp == 0x2026) {
  371. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  372. emit(out, cap, &n, '.');
  373. } else if (cp == 0x2192) {
  374. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  375. } else if (cp == 0x2264) {
  376. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  377. } else {
  378. emit(out, cap, &n, '?');
  379. }
  380. } else {
  381. i++;
  382. emit(out, cap, &n, '?');
  383. }
  384. }
  385. out[n] = '\0';
  386. return n;
  387. }
  388. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  389. * space / empty. */
  390. static unsigned int store_attr(struct br_doc *d, const char *s,
  391. unsigned int len, unsigned int limit)
  392. {
  393. unsigned int n;
  394. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  395. return 0;
  396. if (len > limit)
  397. len = limit;
  398. n = decode_text(s, len, d->attrs + d->attrs_used,
  399. BR_ATTR_SIZE - d->attrs_used, 0);
  400. if (n == 0)
  401. return 0;
  402. {
  403. unsigned int off = d->attrs_used;
  404. d->attrs_used += n + 1;
  405. return off;
  406. }
  407. }
  408. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  409. {
  410. return off ? d->attrs + off : "";
  411. }
  412. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  413. static unsigned int store_raw(struct br_doc *d, const char *s,
  414. unsigned int len, unsigned int limit)
  415. {
  416. unsigned int off;
  417. if (len > limit)
  418. len = limit;
  419. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  420. return 0;
  421. off = d->attrs_used;
  422. {
  423. unsigned int i;
  424. for (i = 0; i < len; i++)
  425. d->attrs[off + i] = s[i];
  426. }
  427. d->attrs[off + len] = '\0';
  428. d->attrs_used += len + 1;
  429. return off;
  430. }
  431. #define VAR_HASH_SIZE 1024
  432. static unsigned int var_hash(const char *name, unsigned int len)
  433. {
  434. unsigned int i, h = 2166136261u;
  435. for (i = 0; i < len; i++) {
  436. h ^= (unsigned char)name[i];
  437. h *= 16777619u;
  438. }
  439. return h & (VAR_HASH_SIZE - 1);
  440. }
  441. static unsigned int css_var_find(struct br_doc *d, const char *name,
  442. unsigned int nlen)
  443. {
  444. unsigned int h = var_hash(name, nlen);
  445. for (;;) {
  446. unsigned int i = d->var_slot[h];
  447. if (!i)
  448. return BR_MAX_VARS;
  449. i--;
  450. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  451. const char *n = attr_ptr(d, d->var_name[i]);
  452. unsigned int j;
  453. for (j = 0; j < nlen && n[j] == name[j]; j++)
  454. ;
  455. if (j == nlen)
  456. return i;
  457. }
  458. h = (h + 1) & (VAR_HASH_SIZE - 1);
  459. }
  460. }
  461. static void css_var_set(struct br_doc *d, const char *name,
  462. unsigned int nlen, const char *val, unsigned int vlen)
  463. {
  464. unsigned int i, noff, voff;
  465. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  466. return;
  467. noff = store_raw(d, name, nlen, 63);
  468. voff = store_raw(d, val, vlen, 199);
  469. if (!noff || !voff)
  470. return;
  471. i = css_var_find(d, name, nlen);
  472. if (i >= BR_MAX_VARS) {
  473. unsigned int h;
  474. if (d->nvars >= BR_MAX_VARS)
  475. return;
  476. i = d->nvars++;
  477. d->var_name[i] = noff;
  478. h = var_hash(name, nlen);
  479. while (d->var_slot[h])
  480. h = (h + 1) & (VAR_HASH_SIZE - 1);
  481. d->var_slot[h] = (unsigned short)(i + 1);
  482. }
  483. d->var_val[i] = voff;
  484. }
  485. /* Find the end of a declaration value: the next ';' outside quotes and
  486. * parentheses. The caller passes the position just after the ':'. */
  487. static unsigned int css_value_end(const char *s, unsigned int len,
  488. unsigned int i)
  489. {
  490. int depth = 0;
  491. char quote = 0;
  492. while (i < len) {
  493. char c = s[i];
  494. if (quote) {
  495. if (c == quote)
  496. quote = 0;
  497. else if (c == '\\' && i + 1 < len)
  498. i++;
  499. } else if (c == '"' || c == '\'') {
  500. quote = c;
  501. } else if (c == '(') {
  502. depth++;
  503. } else if (c == ')' && depth > 0) {
  504. depth--;
  505. } else if (c == ';' && depth == 0) {
  506. break;
  507. }
  508. i++;
  509. }
  510. return i;
  511. }
  512. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  513. * second palette. We render the light page, so skip their custom-property
  514. * definitions; otherwise their last-wins values leak into the light rules. */
  515. static int css_dark_selector(const char *s, unsigned int len)
  516. {
  517. unsigned int i;
  518. for (i = 0; i + 4 <= len; i++) {
  519. if ((s[i] == 'n' || s[i] == 'N') &&
  520. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  521. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  522. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  523. (s[i + 4] == 't' || s[i + 4] == 'T'))
  524. return 1;
  525. if ((s[i] == 'd' || s[i] == 'D') &&
  526. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  527. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  528. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  529. return 1;
  530. if (i + 12 <= len &&
  531. (s[i] == 'c' || s[i] == 'C') &&
  532. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  533. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  534. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  535. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  536. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  537. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  538. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  539. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  540. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  541. s[i + 10] == '-' &&
  542. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  543. (s[i + 12] == 's' || s[i + 12] == 'S'))
  544. return 1;
  545. }
  546. return 0;
  547. }
  548. /* Collect --custom-property definitions from one declaration block. */
  549. static void css_collect_block(struct br_doc *d, const char *s,
  550. unsigned int len)
  551. {
  552. unsigned int i = 0;
  553. while (i < len) {
  554. unsigned int pstart, pend, vstart, vend;
  555. while (i < len && (is_space(s[i]) || s[i] == ';'))
  556. i++;
  557. if (i >= len)
  558. break;
  559. pstart = i;
  560. while (i < len && s[i] != ':' && s[i] != ';')
  561. i++;
  562. pend = i;
  563. if (i >= len || s[i] != ':') {
  564. if (i < len)
  565. i++;
  566. continue;
  567. }
  568. i++;
  569. vstart = i;
  570. i = css_value_end(s, len, i);
  571. vend = i;
  572. if (i < len)
  573. i++;
  574. while (pstart < pend && is_space(s[pstart]))
  575. pstart++;
  576. while (pend > pstart && is_space(s[pend - 1]))
  577. pend--;
  578. while (vstart < vend && is_space(s[vstart]))
  579. vstart++;
  580. while (vend > vstart && is_space(s[vend - 1]))
  581. vend--;
  582. if (vend - vstart >= 10 &&
  583. ci_eq_n(s + vend - 10, "!important", 10)) {
  584. vend -= 10;
  585. while (vend > vstart && is_space(s[vend - 1]))
  586. vend--;
  587. }
  588. if (pend - pstart >= 3 && s[pstart] == '-' &&
  589. s[pstart + 1] == '-')
  590. css_var_set(d, s + pstart, pend - pstart,
  591. s + vstart, vend - vstart);
  592. }
  593. }
  594. const char *br_href(struct br_doc *d, unsigned int node)
  595. {
  596. return attr_ptr(d, d->nodes[node].href_off);
  597. }
  598. const char *br_src(struct br_doc *d, unsigned int node)
  599. {
  600. return attr_ptr(d, d->nodes[node].src_off);
  601. }
  602. const char *br_alt(struct br_doc *d, unsigned int node)
  603. {
  604. return attr_ptr(d, d->nodes[node].alt_off);
  605. }
  606. const char *br_rel(struct br_doc *d, unsigned int node)
  607. {
  608. return attr_ptr(d, d->nodes[node].rel_off);
  609. }
  610. const char *br_value(struct br_doc *d, unsigned int node)
  611. {
  612. return attr_ptr(d, d->nodes[node].src_off);
  613. }
  614. const char *br_placeholder(struct br_doc *d, unsigned int node)
  615. {
  616. return attr_ptr(d, d->nodes[node].alt_off);
  617. }
  618. const char *br_type(struct br_doc *d, unsigned int node)
  619. {
  620. return attr_ptr(d, d->nodes[node].rel_off);
  621. }
  622. /* ---- document setup / node append ---- */
  623. static void rule_index_reset(struct br_doc *d)
  624. {
  625. unsigned int i;
  626. for (i = 0; i < BR_MAX_RULES; i++)
  627. d->rule_next[i] = 0xFFFF;
  628. for (i = 0; i < BR_RULE_HASH; i++)
  629. d->rule_bucket[i] = 0xFFFF;
  630. for (i = 0; i < BR_TAG_MAX; i++)
  631. d->rule_tag[i] = 0xFFFF;
  632. d->rule_any = 0xFFFF;
  633. }
  634. void br_doc_init(struct br_doc *d)
  635. {
  636. unsigned int i;
  637. for (i = 0; i < sizeof(*d); i++)
  638. ((unsigned char *)d)[i] = 0;
  639. rule_index_reset(d);
  640. d->nodes = nodes_store;
  641. d->text = text_store;
  642. d->attrs = attr_store;
  643. d->rules = rule_fallback;
  644. d->rule_cap = BR_RULE_FALLBACK;
  645. d->attrs_used = 1; /* offset 0 = "no attribute" */
  646. d->attrs[0] = '\0';
  647. d->nvars = 0;
  648. d->root = 0;
  649. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  650. d->var_slot[vi] = 0;
  651. root_font_px = 16;
  652. }
  653. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  654. unsigned int cap)
  655. {
  656. if (!buf || !cap) {
  657. d->rules = rule_fallback;
  658. d->rule_cap = BR_RULE_FALLBACK;
  659. d->nrules = 0;
  660. return;
  661. }
  662. if (cap > BR_MAX_RULES)
  663. cap = BR_MAX_RULES;
  664. d->rules = buf;
  665. d->rule_cap = cap;
  666. d->nrules = 0;
  667. }
  668. static void doc_reset(struct br_doc *d)
  669. {
  670. unsigned int i;
  671. d->nnode = 0;
  672. d->text_used = 0;
  673. d->attrs_used = 1;
  674. d->attrs[0] = '\0';
  675. d->nrules = 0;
  676. rule_index_reset(d);
  677. d->title[0] = '\0';
  678. d->inline_css_len = 0;
  679. d->nvars = 0;
  680. d->truncated = 0;
  681. d->plain = 0;
  682. d->script_src = 0;
  683. d->nscripts = 0;
  684. d->scripts_truncated = 0;
  685. for (i = 0; i < BR_MAX_NODES; i++) {
  686. d->nodes[i].type = BR_NODE_ELEM;
  687. d->nodes[i].tag = BR_TAG_UNKNOWN;
  688. d->nodes[i].flags = 0;
  689. d->nodes[i].parent = 0;
  690. d->nodes[i].first = d->nodes[i].last = 0;
  691. d->nodes[i].next = 0;
  692. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  693. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  694. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  695. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  696. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  697. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  698. d->nodes[i].name_off = 0;
  699. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  700. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  701. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  702. d->nodes[i].svg_rx = 0;
  703. d->nodes[i].img_index = -1;
  704. d->nodes[i].style_valid = 0;
  705. }
  706. }
  707. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  708. unsigned char type, unsigned char tag)
  709. {
  710. unsigned int idx;
  711. struct br_node *n;
  712. if (d->nnode >= BR_MAX_NODES) {
  713. d->truncated = 1;
  714. return 0;
  715. }
  716. idx = d->nnode++;
  717. n = &d->nodes[idx];
  718. n->type = type;
  719. n->tag = tag;
  720. n->parent = (unsigned short)parent;
  721. n->img_index = -1;
  722. if (d->nodes[parent].last)
  723. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  724. else
  725. d->nodes[parent].first = (unsigned short)idx;
  726. d->nodes[parent].last = (unsigned short)idx;
  727. return idx;
  728. }
  729. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  730. const char *s, unsigned int len, int pre)
  731. {
  732. char *dst;
  733. unsigned int n;
  734. unsigned int idx;
  735. if (len == 0)
  736. return 0;
  737. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  738. d->truncated = 1;
  739. return 0;
  740. }
  741. dst = d->text + d->text_used;
  742. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  743. if (n == 0) {
  744. d->text[d->text_used] = '\0';
  745. return 0;
  746. }
  747. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  748. if (!idx)
  749. return 0;
  750. d->nodes[idx].text_off = (unsigned short)d->text_used;
  751. d->nodes[idx].text_len = (unsigned short)n;
  752. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  753. d->text_used += n + 1;
  754. return idx;
  755. }
  756. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  757. static int same_string(const char *a, const char *b)
  758. {
  759. while (*a && *a == *b) {
  760. a++;
  761. b++;
  762. }
  763. return *a == *b;
  764. }
  765. static int same_string_n(const char *a, const char *b, unsigned int n)
  766. {
  767. unsigned int i;
  768. for (i = 0; i < n; i++)
  769. if (a[i] != b[i])
  770. return 0;
  771. return 1;
  772. }
  773. static int node_detached(struct br_doc *d, unsigned int node)
  774. {
  775. unsigned int guard = 0;
  776. while (node && node < d->nnode && guard++ <= d->nnode) {
  777. if (d->nodes[node].flags & BR_NODE_DETACHED)
  778. return 1;
  779. node = d->nodes[node].parent;
  780. }
  781. return 0;
  782. }
  783. unsigned int br_find_id(struct br_doc *d, const char *id)
  784. {
  785. unsigned int i;
  786. if (!d || !id)
  787. return 0;
  788. for (i = 0; i < d->nnode; i++) {
  789. struct br_node *n = &d->nodes[i];
  790. if (n->type == BR_NODE_ELEM && n->id_off &&
  791. same_string(d->attrs + n->id_off, id) &&
  792. !node_detached(d, i))
  793. return i + 1;
  794. }
  795. return 0;
  796. }
  797. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  798. unsigned int cap, unsigned int *used)
  799. {
  800. unsigned int c;
  801. if (node >= d->nnode)
  802. return;
  803. if (d->nodes[node].type == BR_NODE_TEXT) {
  804. unsigned int len = d->nodes[node].text_len;
  805. unsigned int off = d->nodes[node].text_off;
  806. if (*used + len >= cap)
  807. len = *used + 1 < cap ? cap - *used - 1 : 0;
  808. for (c = 0; c < len; c++)
  809. out[*used + c] = d->text[off + c];
  810. *used += len;
  811. return;
  812. }
  813. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  814. text_walk(d, c, out, cap, used);
  815. }
  816. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  817. char *out, unsigned int cap)
  818. {
  819. unsigned int used = 0;
  820. if (!d || !out || cap == 0 || node >= d->nnode)
  821. return 0;
  822. text_walk(d, node, out, cap, &used);
  823. out[used] = '\0';
  824. return used;
  825. }
  826. int br_set_text_content(struct br_doc *d, unsigned int node,
  827. const char *text, unsigned int len)
  828. {
  829. struct br_node *n;
  830. unsigned int idx;
  831. unsigned int i;
  832. if (!d || node >= d->nnode)
  833. return -1;
  834. /* Detach the old subtree; its arena storage is reclaimed on the next
  835. * navigation. */
  836. {
  837. unsigned int c;
  838. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  839. d->nodes[c].flags |= BR_NODE_DETACHED;
  840. }
  841. d->nodes[node].first = d->nodes[node].last = 0;
  842. d->nodes[node].style_valid = 0;
  843. if (len == 0)
  844. return 0;
  845. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  846. d->truncated = 1;
  847. return -1;
  848. }
  849. idx = node_append(d, node, BR_NODE_TEXT, 0);
  850. if (!idx)
  851. return -1;
  852. for (i = 0; i < len; i++)
  853. d->text[d->text_used + i] = text[i];
  854. d->text[d->text_used + len] = '\0';
  855. n = &d->nodes[idx];
  856. n->text_off = (unsigned short)d->text_used;
  857. n->text_len = (unsigned short)len;
  858. n->flags = 0;
  859. d->text_used += len + 1;
  860. return 0;
  861. }
  862. /* Replace the value of an existing text or comment node in place, keeping the
  863. * node handle (and therefore the JS wrapper) valid. */
  864. int br_set_text_node(struct br_doc *d, unsigned int node,
  865. const char *text, unsigned int len)
  866. {
  867. struct br_node *n;
  868. unsigned int i;
  869. if (!d || node >= d->nnode)
  870. return -1;
  871. n = &d->nodes[node];
  872. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  873. return -1;
  874. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  875. d->truncated = 1;
  876. return -1;
  877. }
  878. for (i = 0; i < len; i++)
  879. d->text[d->text_used + i] = text[i];
  880. d->text[d->text_used + len] = '\0';
  881. n->text_off = (unsigned short)d->text_used;
  882. n->text_len = (unsigned short)len;
  883. d->text_used += len + 1;
  884. return 0;
  885. }
  886. /* ---- runtime DOM construction and mutation ---- */
  887. static unsigned int text_len(const char *s)
  888. {
  889. unsigned int n = 0;
  890. while (s && s[n])
  891. n++;
  892. return n;
  893. }
  894. static void node_zero(struct br_node *n)
  895. {
  896. unsigned int i;
  897. for (i = 0; i < sizeof(*n); i++)
  898. ((unsigned char *)n)[i] = 0;
  899. n->img_index = -1;
  900. }
  901. static void node_unlink(struct br_doc *d, unsigned int handle)
  902. {
  903. unsigned int node, parent, c, prev = 0;
  904. if (!d || handle == 0 || handle > d->nnode)
  905. return;
  906. node = handle - 1;
  907. parent = d->nodes[node].parent;
  908. if (parent >= d->nnode) {
  909. d->nodes[node].parent = 0;
  910. d->nodes[node].next = 0;
  911. return;
  912. }
  913. c = d->nodes[parent].first;
  914. while (c && c != node) {
  915. prev = c;
  916. c = d->nodes[c].next;
  917. }
  918. if (c == node) {
  919. if (prev)
  920. d->nodes[prev].next = d->nodes[node].next;
  921. else
  922. d->nodes[parent].first = d->nodes[node].next;
  923. if (d->nodes[parent].last == node)
  924. d->nodes[parent].last = (unsigned short)prev;
  925. }
  926. d->nodes[node].parent = 0;
  927. d->nodes[node].next = 0;
  928. d->nodes[node].flags |= BR_NODE_DETACHED;
  929. }
  930. static void parse_attrs(struct br_doc *d, struct br_node *n,
  931. const char *s, unsigned int len);
  932. static unsigned int find_tag_end(const char *s, unsigned int i,
  933. unsigned int len)
  934. {
  935. char quote = 0;
  936. while (i < len) {
  937. char c = s[i];
  938. if (quote) {
  939. if (c == quote)
  940. quote = 0;
  941. } else if (c == '"' || c == '\'') {
  942. quote = c;
  943. } else if (c == '>') {
  944. return i;
  945. }
  946. i++;
  947. }
  948. return len;
  949. }
  950. int br_set_inner_html(struct br_doc *d, unsigned int node,
  951. const char *html, unsigned int len)
  952. {
  953. unsigned int stack[64];
  954. unsigned int sp = 1, i = 0;
  955. if (!d || node >= d->nnode)
  956. return -1;
  957. stack[0] = node;
  958. {
  959. unsigned int c;
  960. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  961. d->nodes[c].flags |= BR_NODE_DETACHED;
  962. }
  963. d->nodes[node].first = d->nodes[node].last = 0;
  964. d->nodes[node].style_valid = 0;
  965. while (i < len) {
  966. if (html[i] != '<') {
  967. unsigned int start = i, end;
  968. while (i < len && html[i] != '<')
  969. i++;
  970. end = i;
  971. if (end > start) {
  972. unsigned int h = br_create_text(d, html + start,
  973. end - start);
  974. if (h)
  975. br_append_child(d, stack[sp - 1], h);
  976. }
  977. continue;
  978. }
  979. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  980. html[i + 3] == '-') {
  981. int p = find_ci(html, len, "-->", 3, i + 4);
  982. unsigned int end = p >= 0 ? (unsigned int)p : len;
  983. unsigned int h = br_create_comment(d, html + i + 4,
  984. end > i + 4 ?
  985. end - i - 4 : 0);
  986. if (h)
  987. br_append_child(d, stack[sp - 1], h);
  988. i = p >= 0 ? (unsigned int)p + 3 : len;
  989. continue;
  990. }
  991. if (html[i + 1] == '/') {
  992. unsigned int p = find_tag_end(html, i + 2, len);
  993. i = p < len ? p + 1 : len;
  994. if (sp > 1)
  995. sp--;
  996. continue;
  997. }
  998. if (html[i + 1] == '!' || html[i + 1] == '?') {
  999. unsigned int p = find_tag_end(html, i + 2, len);
  1000. i = p < len ? p + 1 : len;
  1001. continue;
  1002. }
  1003. {
  1004. unsigned int p = find_tag_end(html, i + 1, len);
  1005. unsigned int ns = i + 1, ne, as, ae;
  1006. unsigned int h, k;
  1007. char name[32];
  1008. int selfclose = 0;
  1009. while (ns < p && is_space(html[ns]))
  1010. ns++;
  1011. ne = ns;
  1012. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1013. ne++;
  1014. as = ne;
  1015. ae = p;
  1016. if (ae > as && html[ae - 1] == '/') {
  1017. selfclose = 1;
  1018. ae--;
  1019. }
  1020. if (ne == ns) {
  1021. i = p < len ? p + 1 : len;
  1022. continue;
  1023. }
  1024. if (ne - ns >= sizeof(name)) {
  1025. i = p < len ? p + 1 : len;
  1026. continue;
  1027. }
  1028. for (k = 0; k < ne - ns; k++)
  1029. name[k] = html[ns + k];
  1030. name[ne - ns] = '\0';
  1031. h = br_create_element(d, name, ne - ns);
  1032. if (!h) {
  1033. i = p < len ? p + 1 : len;
  1034. continue;
  1035. }
  1036. if (ae > as)
  1037. parse_attrs(d, &d->nodes[h - 1], html + as,
  1038. ae - as);
  1039. br_append_child(d, stack[sp - 1], h);
  1040. {
  1041. unsigned int tag = d->nodes[h - 1].tag;
  1042. if (!selfclose && !br_tag_is_void(tag) &&
  1043. sp < sizeof(stack) / sizeof(stack[0]))
  1044. stack[sp++] = h - 1;
  1045. }
  1046. i = p < len ? p + 1 : len;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1052. {
  1053. unsigned int i;
  1054. if (!d)
  1055. return 0;
  1056. for (i = 0; i < d->nnode; i++)
  1057. if (d->nodes[i].type == BR_NODE_ELEM &&
  1058. d->nodes[i].tag == (unsigned char)tag &&
  1059. !node_detached(d, i))
  1060. return i + 1;
  1061. return 0;
  1062. }
  1063. unsigned int br_create_element(struct br_doc *d, const char *name,
  1064. unsigned int len)
  1065. {
  1066. struct br_node *n;
  1067. unsigned int idx;
  1068. if (!d || !name)
  1069. return 0;
  1070. if (d->nnode >= BR_MAX_NODES) {
  1071. d->truncated = 1;
  1072. return 0;
  1073. }
  1074. idx = d->nnode++;
  1075. n = &d->nodes[idx];
  1076. node_zero(n);
  1077. n->type = BR_NODE_ELEM;
  1078. n->flags = BR_NODE_DETACHED;
  1079. n->tag = (unsigned char)br_tag_from_name(name);
  1080. if (n->tag == BR_TAG_UNKNOWN && len)
  1081. n->name_off = store_attr(d, name, len, 31);
  1082. return idx + 1;
  1083. }
  1084. unsigned int br_create_text(struct br_doc *d, const char *text,
  1085. unsigned int len)
  1086. {
  1087. struct br_node *n;
  1088. unsigned int idx, i;
  1089. if (!d)
  1090. return 0;
  1091. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1092. d->truncated = 1;
  1093. return 0;
  1094. }
  1095. idx = d->nnode++;
  1096. n = &d->nodes[idx];
  1097. node_zero(n);
  1098. n->type = BR_NODE_TEXT;
  1099. n->flags = BR_NODE_DETACHED;
  1100. n->text_off = (unsigned short)d->text_used;
  1101. n->text_len = (unsigned short)len;
  1102. for (i = 0; i < len; i++)
  1103. d->text[d->text_used + i] = text[i];
  1104. d->text[d->text_used + len] = '\0';
  1105. d->text_used += len + 1;
  1106. return idx + 1;
  1107. }
  1108. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1109. unsigned int len)
  1110. {
  1111. unsigned int h = br_create_text(d, text, len);
  1112. if (h)
  1113. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1114. return h;
  1115. }
  1116. unsigned int br_create_fragment(struct br_doc *d)
  1117. {
  1118. struct br_node *n;
  1119. unsigned int idx;
  1120. if (!d)
  1121. return 0;
  1122. if (d->nnode >= BR_MAX_NODES) {
  1123. d->truncated = 1;
  1124. return 0;
  1125. }
  1126. idx = d->nnode++;
  1127. n = &d->nodes[idx];
  1128. node_zero(n);
  1129. n->type = BR_NODE_FRAGMENT;
  1130. n->flags = BR_NODE_DETACHED;
  1131. return idx + 1;
  1132. }
  1133. int br_append_child(struct br_doc *d, unsigned int parent,
  1134. unsigned int child)
  1135. {
  1136. unsigned int p;
  1137. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1138. parent == child - 1)
  1139. return -1;
  1140. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1141. /* Appending a fragment inserts its children, not the fragment. */
  1142. unsigned int c = d->nodes[child - 1].first;
  1143. while (c) {
  1144. unsigned int next = d->nodes[c].next;
  1145. br_append_child(d, parent, c + 1);
  1146. c = next;
  1147. }
  1148. return 0;
  1149. }
  1150. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1151. if (p == child - 1)
  1152. return -1;
  1153. node_unlink(d, child);
  1154. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1155. d->nodes[child - 1].parent = (unsigned short)parent;
  1156. d->nodes[child - 1].next = 0;
  1157. if (d->nodes[parent].last)
  1158. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1159. else
  1160. d->nodes[parent].first = (unsigned short)(child - 1);
  1161. d->nodes[parent].last = (unsigned short)(child - 1);
  1162. return 0;
  1163. }
  1164. int br_insert_before(struct br_doc *d, unsigned int parent,
  1165. unsigned int child, unsigned int before)
  1166. {
  1167. unsigned int prev = 0, c;
  1168. if (before == 0)
  1169. return br_append_child(d, parent, child);
  1170. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1171. before > d->nnode || parent == child - 1)
  1172. return -1;
  1173. if (d->nodes[before - 1].parent != parent)
  1174. return -1;
  1175. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1176. unsigned int c = d->nodes[child - 1].first;
  1177. while (c) {
  1178. unsigned int next = d->nodes[c].next;
  1179. br_insert_before(d, parent, c + 1, before);
  1180. c = next;
  1181. }
  1182. return 0;
  1183. }
  1184. {
  1185. unsigned int p;
  1186. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1187. if (p == child - 1)
  1188. return -1;
  1189. }
  1190. node_unlink(d, child);
  1191. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1192. c = d->nodes[parent].first;
  1193. while (c && c != before - 1) {
  1194. prev = c;
  1195. c = d->nodes[c].next;
  1196. }
  1197. if (c != before - 1)
  1198. return -1;
  1199. d->nodes[child - 1].parent = (unsigned short)parent;
  1200. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1201. if (prev)
  1202. d->nodes[prev].next = (unsigned short)(child - 1);
  1203. else
  1204. d->nodes[parent].first = (unsigned short)(child - 1);
  1205. return 0;
  1206. }
  1207. int br_remove_child(struct br_doc *d, unsigned int parent,
  1208. unsigned int child)
  1209. {
  1210. if (!d || child == 0 || child > d->nnode ||
  1211. d->nodes[child - 1].parent != parent)
  1212. return -1;
  1213. node_unlink(d, child);
  1214. return 0;
  1215. }
  1216. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1217. unsigned int child)
  1218. {
  1219. unsigned int c;
  1220. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1221. return 0;
  1222. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1223. if (c == child - 1)
  1224. return child;
  1225. return 0;
  1226. }
  1227. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1228. const char *name)
  1229. {
  1230. struct br_node *n;
  1231. const char *p, *end, *found = 0;
  1232. if (!d || node >= d->nnode || !name)
  1233. return 0;
  1234. n = &d->nodes[node];
  1235. p = d->attrs + n->extra_off;
  1236. end = p + n->extra_len;
  1237. while (p + 1 < end) {
  1238. const char *an = p;
  1239. const char *av;
  1240. p += text_len(p) + 1;
  1241. if (p >= end)
  1242. break;
  1243. av = p;
  1244. p += text_len(p) + 1;
  1245. if (same_string(an, name))
  1246. found = av;
  1247. }
  1248. if (found)
  1249. return found;
  1250. if (same_string(name, "id") && n->id_off)
  1251. return d->attrs + n->id_off;
  1252. if (same_string(name, "class") && n->class_off)
  1253. return d->attrs + n->class_off;
  1254. if (same_string(name, "style") && n->style_off)
  1255. return d->attrs + n->style_off;
  1256. if (same_string(name, "href") && n->href_off)
  1257. return d->attrs + n->href_off;
  1258. if (same_string(name, "src") && n->src_off)
  1259. return d->attrs + n->src_off;
  1260. if (same_string(name, "alt") && n->alt_off)
  1261. return d->attrs + n->alt_off;
  1262. if (same_string(name, "rel") && n->rel_off)
  1263. return d->attrs + n->rel_off;
  1264. return 0;
  1265. }
  1266. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1267. const char *name, const char *value,
  1268. unsigned int len)
  1269. {
  1270. if (same_string(name, "id"))
  1271. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1272. else if (same_string(name, "class"))
  1273. n->class_off = len ? store_attr(d, value, len, 120) : 0;
  1274. else if (same_string(name, "style"))
  1275. n->style_off = len ? store_attr(d, value, len, 240) : 0;
  1276. else if (same_string(name, "href"))
  1277. n->href_off = len ? store_attr(d, value, len, 480) : 0;
  1278. else if (same_string(name, "src"))
  1279. n->src_off = len ? store_attr(d, value, len, 400) : 0;
  1280. else if (same_string(name, "alt"))
  1281. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1282. else if (same_string(name, "rel"))
  1283. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1284. }
  1285. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1286. const char *value, unsigned int len)
  1287. {
  1288. struct br_node *n;
  1289. unsigned int start, nl, need;
  1290. const char *p, *old, *oldend;
  1291. if (!d || node >= d->nnode || !name)
  1292. return -1;
  1293. n = &d->nodes[node];
  1294. nl = text_len(name);
  1295. need = nl + 1 + len + 1;
  1296. start = d->attrs_used;
  1297. old = d->attrs + n->extra_off;
  1298. oldend = old + n->extra_len;
  1299. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1300. d->truncated = 1;
  1301. return -1;
  1302. }
  1303. p = old;
  1304. while (p + 1 < oldend) {
  1305. const char *an = p;
  1306. const char *av;
  1307. unsigned int anl, avl, k;
  1308. anl = text_len(an);
  1309. p += anl + 1;
  1310. if (p >= oldend)
  1311. break;
  1312. av = p;
  1313. avl = text_len(av);
  1314. p += avl + 1;
  1315. if (same_string(an, name))
  1316. continue;
  1317. for (k = 0; k < anl; k++)
  1318. d->attrs[d->attrs_used++] = an[k];
  1319. d->attrs[d->attrs_used++] = '\0';
  1320. for (k = 0; k < avl; k++)
  1321. d->attrs[d->attrs_used++] = av[k];
  1322. d->attrs[d->attrs_used++] = '\0';
  1323. }
  1324. {
  1325. unsigned int k;
  1326. for (k = 0; k < nl; k++)
  1327. d->attrs[d->attrs_used++] = name[k];
  1328. d->attrs[d->attrs_used++] = '\0';
  1329. for (k = 0; k < len; k++)
  1330. d->attrs[d->attrs_used++] = value[k];
  1331. d->attrs[d->attrs_used++] = '\0';
  1332. }
  1333. n = &d->nodes[node];
  1334. n->extra_off = start;
  1335. n->extra_len = (unsigned short)(d->attrs_used - start);
  1336. n->style_valid = 0;
  1337. attr_note_known(d, n, name, value, len);
  1338. return 0;
  1339. }
  1340. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1341. {
  1342. struct br_node *n;
  1343. unsigned int start;
  1344. const char *p, *oldend;
  1345. int found = 0;
  1346. if (!d || node >= d->nnode || !name)
  1347. return -1;
  1348. n = &d->nodes[node];
  1349. start = d->attrs_used;
  1350. p = d->attrs + n->extra_off;
  1351. oldend = p + n->extra_len;
  1352. if (start + n->extra_len > BR_ATTR_SIZE)
  1353. return -1;
  1354. while (p + 1 < oldend) {
  1355. const char *an = p;
  1356. const char *av;
  1357. unsigned int anl, avl, k;
  1358. anl = text_len(an);
  1359. p += anl + 1;
  1360. if (p >= oldend)
  1361. break;
  1362. av = p;
  1363. avl = text_len(av);
  1364. p += avl + 1;
  1365. if (same_string(an, name)) {
  1366. found = 1;
  1367. continue;
  1368. }
  1369. for (k = 0; k < anl; k++)
  1370. d->attrs[d->attrs_used++] = an[k];
  1371. d->attrs[d->attrs_used++] = '\0';
  1372. for (k = 0; k < avl; k++)
  1373. d->attrs[d->attrs_used++] = av[k];
  1374. d->attrs[d->attrs_used++] = '\0';
  1375. }
  1376. n = &d->nodes[node];
  1377. n->extra_off = start;
  1378. n->extra_len = (unsigned short)(d->attrs_used - start);
  1379. n->style_valid = 0;
  1380. attr_note_known(d, n, name, "", 0);
  1381. return found ? 0 : -1;
  1382. }
  1383. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1384. {
  1385. struct br_node *src;
  1386. unsigned int h;
  1387. const char *name;
  1388. if (!d || node == 0 || node > d->nnode)
  1389. return 0;
  1390. src = &d->nodes[node - 1];
  1391. if (src->type == BR_NODE_TEXT)
  1392. return br_create_text(d, d->text + src->text_off, src->text_len);
  1393. if (src->type == BR_NODE_COMMENT)
  1394. return br_create_comment(d, d->text + src->text_off,
  1395. src->text_len);
  1396. if (src->type == BR_NODE_FRAGMENT)
  1397. h = br_create_fragment(d);
  1398. else {
  1399. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1400. name = d->attrs + src->name_off;
  1401. else
  1402. name = br_tag_name(src->tag);
  1403. h = br_create_element(d, name, text_len(name));
  1404. }
  1405. if (!h)
  1406. return 0;
  1407. {
  1408. const char *p = d->attrs + src->extra_off;
  1409. const char *end = p + src->extra_len;
  1410. while (p + 1 < end) {
  1411. const char *an = p;
  1412. const char *av;
  1413. unsigned int anl, avl;
  1414. anl = text_len(an);
  1415. p += anl + 1;
  1416. if (p >= end)
  1417. break;
  1418. av = p;
  1419. avl = text_len(av);
  1420. p += avl + 1;
  1421. br_set_attr(d, h, an, av, avl);
  1422. }
  1423. }
  1424. if (deep) {
  1425. unsigned int c;
  1426. for (c = src->first; c; c = d->nodes[c].next) {
  1427. unsigned int ch = br_clone_node(d, c + 1, 1);
  1428. if (ch)
  1429. br_append_child(d, h - 1, ch);
  1430. }
  1431. }
  1432. return h;
  1433. }
  1434. static int attr_value_match(const char *have, const char *want,
  1435. unsigned int want_len, int op)
  1436. {
  1437. unsigned int n = text_len(have);
  1438. unsigned int i;
  1439. if (!op)
  1440. return have != 0;
  1441. if (!have)
  1442. return 0;
  1443. if (op == '=')
  1444. return n == want_len && same_string_n(have, want, want_len);
  1445. if (op == '^')
  1446. return n >= want_len && same_string_n(have, want, want_len);
  1447. if (op == '$') {
  1448. if (n < want_len)
  1449. return 0;
  1450. have += n - want_len;
  1451. for (i = 0; i < want_len; i++)
  1452. if (have[i] != want[i])
  1453. return 0;
  1454. return 1;
  1455. }
  1456. if (op == '*') {
  1457. for (i = 0; i + want_len <= n; i++)
  1458. if (same_string_n(have + i, want, want_len))
  1459. return 1;
  1460. return 0;
  1461. }
  1462. return 0;
  1463. }
  1464. static int class_contains(const char *classes, const char *want,
  1465. unsigned int want_len)
  1466. {
  1467. unsigned int i = 0;
  1468. if (!classes)
  1469. return 0;
  1470. while (classes[i]) {
  1471. unsigned int start;
  1472. while (classes[i] == ' ' || classes[i] == '\t' ||
  1473. classes[i] == '\r' || classes[i] == '\n')
  1474. i++;
  1475. start = i;
  1476. while (classes[i] && classes[i] != ' ' && classes[i] != '\t' &&
  1477. classes[i] != '\r' && classes[i] != '\n')
  1478. i++;
  1479. if (i - start == want_len &&
  1480. same_string_n(classes + start, want, want_len))
  1481. return 1;
  1482. }
  1483. return 0;
  1484. }
  1485. static int match_compound(struct br_doc *d, unsigned int node,
  1486. const char *sel, unsigned int len)
  1487. {
  1488. struct br_node *n = &d->nodes[node];
  1489. unsigned int i = 0;
  1490. while (i < len && sel[i] != '.' && sel[i] != '#' && sel[i] != '[')
  1491. i++;
  1492. if (i > 0 && !(i == 1 && sel[0] == '*')) {
  1493. char tag[32];
  1494. unsigned int tl = i < sizeof(tag) - 1 ? i : sizeof(tag) - 1;
  1495. unsigned int k;
  1496. for (k = 0; k < tl; k++)
  1497. tag[k] = sel[k];
  1498. tag[tl] = '\0';
  1499. if (n->tag == BR_TAG_UNKNOWN) {
  1500. if (!n->name_off ||
  1501. !same_string(d->attrs + n->name_off, tag))
  1502. return 0;
  1503. } else if (n->tag != br_tag_from_name(tag)) {
  1504. return 0;
  1505. }
  1506. }
  1507. while (i < len) {
  1508. if (sel[i] == '.') {
  1509. unsigned int start;
  1510. i++;
  1511. start = i;
  1512. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1513. sel[i] != '[' && sel[i] != ' ')
  1514. i++;
  1515. if (!class_contains(br_find_attr(d, node, "class"),
  1516. sel + start, i - start))
  1517. return 0;
  1518. } else if (sel[i] == '#') {
  1519. unsigned int start;
  1520. i++;
  1521. start = i;
  1522. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1523. sel[i] != '[' && sel[i] != ' ')
  1524. i++;
  1525. {
  1526. const char *id = br_find_attr(d, node, "id");
  1527. if (!id || !same_string_n(id, sel + start,
  1528. i - start) ||
  1529. text_len(id) != i - start)
  1530. return 0;
  1531. }
  1532. } else if (sel[i] == '[') {
  1533. unsigned int start, nl, op = 0, vs, ve;
  1534. char quote = 0;
  1535. i++;
  1536. start = i;
  1537. while (i < len && sel[i] != '=' && sel[i] != ']' &&
  1538. sel[i] != '^' && sel[i] != '$' && sel[i] != '*')
  1539. i++;
  1540. nl = i - start;
  1541. if (i < len && sel[i] != ']') {
  1542. op = sel[i++];
  1543. if (i < len && sel[i] == '=')
  1544. i++;
  1545. }
  1546. while (i < len && sel[i] == ' ')
  1547. i++;
  1548. if (i < len && (sel[i] == '"' || sel[i] == '\'')) {
  1549. quote = sel[i++];
  1550. vs = i;
  1551. while (i < len && sel[i] != quote)
  1552. i++;
  1553. ve = i;
  1554. if (i < len)
  1555. i++;
  1556. } else {
  1557. vs = i;
  1558. while (i < len && sel[i] != ']')
  1559. i++;
  1560. ve = i;
  1561. }
  1562. if (i < len && sel[i] == ']')
  1563. i++;
  1564. {
  1565. char name[32];
  1566. unsigned int k;
  1567. if (nl >= sizeof(name))
  1568. return 0;
  1569. for (k = 0; k < nl; k++)
  1570. name[k] = sel[start + k];
  1571. name[nl] = '\0';
  1572. if (!attr_value_match(br_find_attr(d, node, name),
  1573. sel + vs, ve - vs, op))
  1574. return 0;
  1575. }
  1576. } else {
  1577. return 0;
  1578. }
  1579. }
  1580. return 1;
  1581. }
  1582. static int match_selector(struct br_doc *d, unsigned int node,
  1583. const char *sel, unsigned int len)
  1584. {
  1585. unsigned int i, split = 0, depth = 0;
  1586. char quote = 0;
  1587. while (len > 0 && sel[0] == ' ') {
  1588. sel++;
  1589. len--;
  1590. }
  1591. while (len > 0 && sel[len - 1] == ' ')
  1592. len--;
  1593. if (len == 0)
  1594. return 0;
  1595. for (i = 0; i < len; i++) {
  1596. char c = sel[i];
  1597. if (quote) {
  1598. if (c == quote)
  1599. quote = 0;
  1600. } else if (c == '"' || c == '\'') {
  1601. quote = c;
  1602. } else if (c == '[') {
  1603. depth++;
  1604. } else if (c == ']' && depth) {
  1605. depth--;
  1606. } else if (!depth && c == ' ') {
  1607. split = i;
  1608. }
  1609. }
  1610. if (split) {
  1611. unsigned int p = d->nodes[node].parent;
  1612. if (!match_compound(d, node, sel + split + 1,
  1613. len - split - 1))
  1614. return 0;
  1615. while (p) {
  1616. unsigned int next = d->nodes[p].parent;
  1617. if (match_selector(d, p, sel, split))
  1618. return 1;
  1619. p = next;
  1620. }
  1621. return 0;
  1622. }
  1623. return match_compound(d, node, sel, len);
  1624. }
  1625. static int query_add(unsigned int *out, unsigned int max,
  1626. unsigned int *count, unsigned int node)
  1627. {
  1628. unsigned int i;
  1629. for (i = 0; i < *count; i++)
  1630. if (out[i] == node + 1)
  1631. return 0;
  1632. if (*count < max)
  1633. out[(*count)++] = node + 1;
  1634. return 0;
  1635. }
  1636. static void query_walk(struct br_doc *d, unsigned int node,
  1637. const char *sel, unsigned int len, unsigned int *out,
  1638. unsigned int max, unsigned int *count)
  1639. {
  1640. unsigned int c;
  1641. if (match_selector(d, node, sel, len))
  1642. query_add(out, max, count, node + 1);
  1643. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1644. query_walk(d, c, sel, len, out, max, count);
  1645. }
  1646. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1647. {
  1648. unsigned int len;
  1649. if (!d || !selector || node == 0 || node > d->nnode)
  1650. return 0;
  1651. len = text_len(selector);
  1652. while (len > 0 && selector[0] == ' ') {
  1653. selector++;
  1654. len--;
  1655. }
  1656. while (len > 0 && selector[len - 1] == ' ')
  1657. len--;
  1658. return match_selector(d, node - 1, selector, len);
  1659. }
  1660. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1661. const char *selector, unsigned int *out,
  1662. unsigned int max)
  1663. {
  1664. unsigned int count = 0, i = 0;
  1665. if (!d || !selector || !out || max == 0)
  1666. return 0;
  1667. while (selector[i]) {
  1668. unsigned int start = i, depth = 0;
  1669. char quote = 0;
  1670. while (selector[i]) {
  1671. char c = selector[i];
  1672. if (quote) {
  1673. if (c == quote)
  1674. quote = 0;
  1675. } else if (c == '"' || c == '\'') {
  1676. quote = c;
  1677. } else if (c == '[') {
  1678. depth++;
  1679. } else if (c == ']' && depth) {
  1680. depth--;
  1681. } else if (!depth && c == ',') {
  1682. break;
  1683. }
  1684. i++;
  1685. }
  1686. if (i > start) {
  1687. if (root == 0) {
  1688. unsigned int n;
  1689. for (n = 1; n < d->nnode; n++)
  1690. if (!node_detached(d, n) &&
  1691. match_selector(d, n, selector + start,
  1692. i - start))
  1693. query_add(out, max, &count,
  1694. n + 1);
  1695. } else if (root <= d->nnode) {
  1696. unsigned int c;
  1697. for (c = d->nodes[root - 1].first; c;
  1698. c = d->nodes[c].next)
  1699. query_walk(d, c, selector + start,
  1700. i - start, out, max, &count);
  1701. }
  1702. }
  1703. if (selector[i] == ',')
  1704. i++;
  1705. }
  1706. return count;
  1707. }
  1708. /* ---- attribute parsing ---- */
  1709. static int is_space(char c)
  1710. {
  1711. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1712. }
  1713. static int str_eq(const char *a, const char *b);
  1714. static unsigned int parse_uint(const char *s, unsigned int len)
  1715. {
  1716. unsigned int i = 0, v = 0;
  1717. while (i < len && is_space(s[i]))
  1718. i++;
  1719. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1720. v = v * 10 + (unsigned int)(s[i] - '0');
  1721. if (v > 4096)
  1722. v = 4096;
  1723. i++;
  1724. }
  1725. return v;
  1726. }
  1727. static int contains_ci_len(const char *s, unsigned int len,
  1728. const char *needle);
  1729. static int parse_svg_viewbox(const char *s, unsigned int len,
  1730. unsigned short *w, unsigned short *h)
  1731. {
  1732. unsigned int i = 0, v[4], n = 0;
  1733. while (i < len && n < 4) {
  1734. unsigned int x = 0;
  1735. int any = 0;
  1736. while (i < len && (is_space(s[i]) || s[i] == ','))
  1737. i++;
  1738. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1739. x = x * 10 + (unsigned int)(s[i] - '0');
  1740. i++;
  1741. any = 1;
  1742. }
  1743. if (!any) {
  1744. if (i < len)
  1745. i++;
  1746. continue;
  1747. }
  1748. v[n++] = x;
  1749. }
  1750. if (n < 4)
  1751. return 0;
  1752. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1753. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1754. return *w && *h;
  1755. }
  1756. static void note_attr_flag(struct br_node *n, const char *name)
  1757. {
  1758. if (str_eq(name, "data-collapse-target"))
  1759. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1760. else if (str_eq(name, "data-collapse-trigger"))
  1761. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1762. else if (str_eq(name, "is-open"))
  1763. n->attr_flags |= BR_ATTR_IS_OPEN;
  1764. else if (str_eq(name, "disabled"))
  1765. n->attr_flags |= BR_ATTR_DISABLED;
  1766. else if (str_eq(name, "open"))
  1767. n->attr_flags |= BR_ATTR_OPEN;
  1768. else if (str_eq(name, "inert"))
  1769. n->attr_flags |= BR_ATTR_INERT;
  1770. }
  1771. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1772. * second pass so the block stays contiguous even though the known attributes
  1773. * above also use the shared attr arena. */
  1774. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1775. const char *s, unsigned int len)
  1776. {
  1777. unsigned int i = 0;
  1778. unsigned int start = d->attrs_used;
  1779. while (i < len) {
  1780. char name[32];
  1781. unsigned int nl = 0, vstart, vlen;
  1782. char quote = 0;
  1783. while (i < len && is_space(s[i]))
  1784. i++;
  1785. if (i >= len || s[i] == '/')
  1786. break;
  1787. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1788. char c = s[i++];
  1789. if (c >= 'A' && c <= 'Z')
  1790. c = (char)(c - 'A' + 'a');
  1791. if (nl + 1 < sizeof(name))
  1792. name[nl++] = c;
  1793. }
  1794. name[nl] = '\0';
  1795. while (i < len && is_space(s[i]))
  1796. i++;
  1797. if (i < len && s[i] == '=') {
  1798. i++;
  1799. while (i < len && is_space(s[i]))
  1800. i++;
  1801. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1802. quote = s[i++];
  1803. vstart = i;
  1804. while (i < len && s[i] != quote)
  1805. i++;
  1806. vlen = i - vstart;
  1807. if (i < len)
  1808. i++;
  1809. } else {
  1810. vstart = i;
  1811. while (i < len && !is_space(s[i]) && s[i] != '>')
  1812. i++;
  1813. vlen = i - vstart;
  1814. }
  1815. } else {
  1816. /* Boolean attribute: expose it with an empty value. */
  1817. vstart = 0;
  1818. vlen = 0;
  1819. }
  1820. if (!nl)
  1821. continue;
  1822. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1823. d->truncated = 1;
  1824. break;
  1825. }
  1826. {
  1827. unsigned int k;
  1828. for (k = 0; k < nl; k++)
  1829. d->attrs[d->attrs_used++] = name[k];
  1830. d->attrs[d->attrs_used++] = '\0';
  1831. for (k = 0; k < vlen; k++)
  1832. d->attrs[d->attrs_used++] = s[vstart + k];
  1833. d->attrs[d->attrs_used++] = '\0';
  1834. }
  1835. }
  1836. if (d->attrs_used > start) {
  1837. n->extra_off = start;
  1838. n->extra_len = (unsigned short)(d->attrs_used - start);
  1839. }
  1840. }
  1841. /* Parse attributes from `s` (between the tag name and '>'). */
  1842. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1843. const char *s, unsigned int len)
  1844. {
  1845. unsigned int i = 0;
  1846. while (i < len) {
  1847. char name[32];
  1848. unsigned int nl = 0, vstart, vlen;
  1849. char quote = 0;
  1850. while (i < len && is_space(s[i]))
  1851. i++;
  1852. if (i >= len || s[i] == '/')
  1853. break;
  1854. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1855. char c = s[i++];
  1856. if (c >= 'A' && c <= 'Z')
  1857. c = (char)(c - 'A' + 'a');
  1858. if (nl + 1 < sizeof(name))
  1859. name[nl++] = c;
  1860. }
  1861. name[nl] = '\0';
  1862. while (i < len && is_space(s[i]))
  1863. i++;
  1864. if (i >= len || s[i] != '=') {
  1865. if (str_eq(name, "hidden"))
  1866. n->flags |= BR_NODE_HIDDEN;
  1867. note_attr_flag(n, name);
  1868. continue;
  1869. }
  1870. i++;
  1871. while (i < len && is_space(s[i]))
  1872. i++;
  1873. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1874. quote = s[i++];
  1875. vstart = i;
  1876. while (i < len && s[i] != quote)
  1877. i++;
  1878. vlen = i - vstart;
  1879. if (i < len)
  1880. i++;
  1881. } else {
  1882. vstart = i;
  1883. while (i < len && !is_space(s[i]) && s[i] != '>')
  1884. i++;
  1885. vlen = i - vstart;
  1886. }
  1887. if (!nl)
  1888. continue;
  1889. if (str_eq(name, "hidden")) {
  1890. n->flags |= BR_NODE_HIDDEN;
  1891. continue;
  1892. }
  1893. note_attr_flag(n, name);
  1894. if (str_eq(name, "href"))
  1895. n->href_off = store_attr(d, s + vstart, vlen, 480);
  1896. else if (str_eq(name, "class"))
  1897. n->class_off = store_attr(d, s + vstart, vlen, 120);
  1898. else if (str_eq(name, "id"))
  1899. n->id_off = store_attr(d, s + vstart, vlen, 64);
  1900. else if (str_eq(name, "style"))
  1901. n->style_off = store_attr(d, s + vstart, vlen, 240);
  1902. else if (str_eq(name, "src"))
  1903. n->src_off = store_attr(d, s + vstart, vlen, 400);
  1904. else if (str_eq(name, "alt"))
  1905. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  1906. else if (str_eq(name, "color"))
  1907. n->color_off = store_attr(d, s + vstart, vlen, 32);
  1908. else if (str_eq(name, "rel"))
  1909. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  1910. else if (str_eq(name, "value"))
  1911. n->src_off = store_attr(d, s + vstart, vlen, 220);
  1912. else if (str_eq(name, "placeholder") &&
  1913. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  1914. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  1915. else if (str_eq(name, "type") &&
  1916. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1917. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  1918. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  1919. else if (str_eq(name, "width")) {
  1920. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1921. if (vlen && s[vstart + vlen - 1] == '%')
  1922. n->width_pct = 1;
  1923. } else if (str_eq(name, "height")) {
  1924. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1925. if (vlen && s[vstart + vlen - 1] == '%')
  1926. n->height_pct = 1;
  1927. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  1928. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  1929. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  1930. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  1931. } else if (str_eq(name, "x2")) {
  1932. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1933. } else if (str_eq(name, "y2")) {
  1934. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1935. } else if (str_eq(name, "rx")) {
  1936. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  1937. } else if (str_eq(name, "d")) {
  1938. n->src_off = store_attr(d, s + vstart, vlen, 300);
  1939. } else if (str_eq(name, "viewbox")) {
  1940. unsigned short vw = 0, vh = 0;
  1941. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  1942. n->width_attr = vw;
  1943. n->height_attr = vh;
  1944. n->flags |= BR_NODE_VIEWBOX;
  1945. }
  1946. } else if (str_eq(name, "text-anchor")) {
  1947. if (contains_ci_len(s + vstart, vlen, "middle"))
  1948. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  1949. else if (contains_ci_len(s + vstart, vlen, "end"))
  1950. n->attr_flags |= BR_ATTR_ANCHOR_END;
  1951. }
  1952. }
  1953. parse_attrs_generic(d, n, s, len);
  1954. }
  1955. static int str_eq(const char *a, const char *b)
  1956. {
  1957. unsigned int i = 0;
  1958. while (a[i] && b[i] && a[i] == b[i])
  1959. i++;
  1960. return a[i] == '\0' && b[i] == '\0';
  1961. }
  1962. /* ---- CSS ---- */
  1963. enum {
  1964. PROP_COLOR, PROP_BG, PROP_FONT_PX, PROP_BOLD, PROP_ITALIC, PROP_MONO,
  1965. PROP_ALIGN, PROP_UNDERLINE,
  1966. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  1967. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  1968. PROP_BORDER_W, PROP_BORDER_COLOR,
  1969. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  1970. PROP_LIST_NONE,
  1971. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  1972. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  1973. PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  1974. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  1975. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  1976. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  1977. PROP_COUNT
  1978. };
  1979. struct color_name {
  1980. const char *name;
  1981. unsigned short rgb;
  1982. };
  1983. static const struct color_name color_names[] = {
  1984. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  1985. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  1986. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  1987. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  1988. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  1989. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  1990. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  1991. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  1992. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  1993. { "darkgrey", 0x528A }, { 0, 0 }
  1994. };
  1995. static int hexval(char c)
  1996. {
  1997. if (c >= '0' && c <= '9')
  1998. return c - '0';
  1999. if (c >= 'a' && c <= 'f')
  2000. return c - 'a' + 10;
  2001. if (c >= 'A' && c <= 'F')
  2002. return c - 'A' + 10;
  2003. return -1;
  2004. }
  2005. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2006. {
  2007. unsigned int i;
  2008. for (i = 0; color_names[i].name; i++) {
  2009. unsigned int j, n = s_len(color_names[i].name);
  2010. if (n != len)
  2011. continue;
  2012. for (j = 0; j < n; j++) {
  2013. char c = s[j];
  2014. if (c >= 'A' && c <= 'Z')
  2015. c = (char)(c - 'A' + 'a');
  2016. if (c != color_names[i].name[j])
  2017. break;
  2018. }
  2019. if (j == n) {
  2020. *out = color_names[i].rgb;
  2021. return 1;
  2022. }
  2023. }
  2024. return 0;
  2025. }
  2026. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2027. * over white; the render target is a white page. */
  2028. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2029. {
  2030. unsigned int i = 0;
  2031. while (i < len && is_space(s[i]))
  2032. i++;
  2033. if (i < len && s[i] == '#') {
  2034. int d[6], nd = 0;
  2035. i++;
  2036. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2037. d[nd++] = hexval(s[i++]);
  2038. if (nd == 3) {
  2039. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2040. unsigned int b = (unsigned int)d[2];
  2041. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2042. ((g * 17 >> 2) << 5) |
  2043. (b * 17 >> 3));
  2044. return 1;
  2045. }
  2046. if (nd >= 6) {
  2047. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2048. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2049. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2050. *out = (unsigned short)(((r >> 3) << 11) |
  2051. ((g >> 2) << 5) | (b >> 3));
  2052. return 1;
  2053. }
  2054. return 0;
  2055. }
  2056. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2057. unsigned int v[3], k = 0, a = 255;
  2058. while (i < len && s[i] != '(')
  2059. i++;
  2060. if (i >= len)
  2061. return 0;
  2062. i++;
  2063. while (k < 3 && i < len) {
  2064. unsigned int x = 0;
  2065. int percent = 0;
  2066. while (i < len && (is_space(s[i]) || s[i] == ','))
  2067. i++;
  2068. if (i >= len || s[i] < '0' || s[i] > '9')
  2069. return 0;
  2070. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2071. x = x * 10 + (unsigned int)(s[i] - '0');
  2072. i++;
  2073. }
  2074. if (i < len && s[i] == '%') {
  2075. percent = 1;
  2076. i++;
  2077. }
  2078. if (percent)
  2079. x = x * 255 / 100;
  2080. if (x > 255)
  2081. x = 255;
  2082. v[k++] = x;
  2083. }
  2084. /* Optional alpha in both syntaxes:
  2085. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2086. {
  2087. unsigned int whole = 0, frac = 0, scale = 1;
  2088. int percent = 0;
  2089. while (i < len && (is_space(s[i]) || s[i] == ','))
  2090. i++;
  2091. if (i < len && s[i] == '/') {
  2092. i++;
  2093. while (i < len && is_space(s[i]))
  2094. i++;
  2095. }
  2096. if (i < len && s[i] >= '0' && s[i] <= '9') {
  2097. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2098. whole = whole * 10 +
  2099. (unsigned int)(s[i] - '0');
  2100. i++;
  2101. }
  2102. if (i < len && s[i] == '.') {
  2103. i++;
  2104. while (i < len && s[i] >= '0' &&
  2105. s[i] <= '9') {
  2106. if (scale < 1000) {
  2107. frac = frac * 10 +
  2108. (unsigned int)(s[i] - '0');
  2109. scale *= 10;
  2110. }
  2111. i++;
  2112. }
  2113. }
  2114. while (i < len && is_space(s[i]))
  2115. i++;
  2116. if (i < len && s[i] == '%')
  2117. percent = 1;
  2118. if (percent)
  2119. a = whole > 100 ? 255 :
  2120. whole * 255 / 100;
  2121. else if (whole >= 1)
  2122. a = 255;
  2123. else
  2124. a = frac * 255 / scale;
  2125. }
  2126. }
  2127. if (k == 3) {
  2128. unsigned int r = v[0], g = v[1], b = v[2];
  2129. if (a < 255) {
  2130. r = (r * a + 255u * (255u - a)) / 255u;
  2131. g = (g * a + 255u * (255u - a)) / 255u;
  2132. b = (b * a + 255u * (255u - a)) / 255u;
  2133. }
  2134. *out = (unsigned short)(((r >> 3) << 11) |
  2135. ((g >> 2) << 5) | (b >> 3));
  2136. return 1;
  2137. }
  2138. return 0;
  2139. }
  2140. {
  2141. unsigned int j = i;
  2142. while (i < len && !is_space(s[i]))
  2143. i++;
  2144. return named_color(s + j, i - j, out);
  2145. }
  2146. }
  2147. /* Length values for margins/padding: px, ignore percentages/units. */
  2148. static unsigned int viewport_w;
  2149. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2150. {
  2151. unsigned int i = *pos;
  2152. int sign = 1;
  2153. long v = 0; /* hundredths of a unit before conversion */
  2154. int digits = 0, frac = 0, unit = 0;
  2155. while (i < len && is_space(s[i]))
  2156. i++;
  2157. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2158. if (s[i] == '-')
  2159. sign = -1;
  2160. i++;
  2161. }
  2162. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2163. v = v * 10 + (s[i] - '0');
  2164. digits = 1;
  2165. i++;
  2166. }
  2167. if (i < len && s[i] == '.') {
  2168. i++;
  2169. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2170. if (frac < 2) {
  2171. v = v * 10 + (s[i] - '0');
  2172. frac++;
  2173. }
  2174. i++;
  2175. }
  2176. }
  2177. while (frac < 2) {
  2178. v *= 10;
  2179. frac++;
  2180. }
  2181. while (frac > 2) {
  2182. v /= 10;
  2183. frac--;
  2184. }
  2185. if (i < len && s[i] == '%')
  2186. unit = 2;
  2187. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2188. s[i + 2] == 'm')
  2189. unit = 1;
  2190. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2191. unit = 3;
  2192. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2193. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2194. s[i] == 'a' || s[i] == 'd'))
  2195. i++;
  2196. *pos = i;
  2197. if (!digits)
  2198. return 0;
  2199. if (unit == 1)
  2200. v *= root_font_px;
  2201. else if (unit == 3)
  2202. v *= 16;
  2203. else if (unit == 2)
  2204. v = v * (long)viewport_w / 100;
  2205. v /= 100;
  2206. v *= sign;
  2207. if (v < -2048)
  2208. v = -2048;
  2209. if (v > 2048)
  2210. v = 2048;
  2211. return (int)v;
  2212. }
  2213. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2214. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2215. unsigned char *kind)
  2216. {
  2217. unsigned int i = 0;
  2218. unsigned long v = 0;
  2219. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2220. *val = 0;
  2221. *kind = 0;
  2222. while (i < len && is_space(s[i]))
  2223. i++;
  2224. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2225. return 0;
  2226. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2227. return 0;
  2228. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2229. v = v * 10 + (unsigned int)(s[i] - '0');
  2230. digits = 1;
  2231. i++;
  2232. }
  2233. if (i < len && s[i] == '.') {
  2234. i++;
  2235. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2236. if (frac < 2) {
  2237. v = v * 10 + (unsigned int)(s[i] - '0');
  2238. frac++;
  2239. }
  2240. i++;
  2241. }
  2242. }
  2243. while (frac < 2) {
  2244. v *= 10;
  2245. frac++;
  2246. }
  2247. while (frac > 2) {
  2248. v /= 10;
  2249. frac--;
  2250. }
  2251. if (!digits)
  2252. return 0;
  2253. while (i < len && is_space(s[i]))
  2254. i++;
  2255. if (i < len && s[i] == '%')
  2256. pct = 1;
  2257. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2258. s[i + 2] == 'm')
  2259. rem = 1;
  2260. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2261. rem = 2;
  2262. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2263. vw = 1;
  2264. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2265. ; /* px: already the default scale */
  2266. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2267. bad_unit = 1;
  2268. if (bad_unit)
  2269. return 0;
  2270. if (rem == 1)
  2271. v *= root_font_px;
  2272. else if (rem == 2)
  2273. v *= 16;
  2274. v /= 100;
  2275. if (vw)
  2276. v = v * viewport_w / 100;
  2277. if (pct) {
  2278. if (v > 100)
  2279. v = 100;
  2280. *kind = 2;
  2281. } else {
  2282. if (v > 2048)
  2283. v = 2048;
  2284. *kind = 1;
  2285. }
  2286. *val = (unsigned short)v;
  2287. return 1;
  2288. }
  2289. static int parse_font_px(const char *s, unsigned int len)
  2290. {
  2291. unsigned int i = 0;
  2292. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2293. int v1000, px;
  2294. int is_px = 1, is_rem = 0, is_pct = 0;
  2295. while (i < len && is_space(s[i]))
  2296. i++;
  2297. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2298. return 24;
  2299. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2300. return 32;
  2301. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2302. return 14;
  2303. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2304. whole = whole * 10 + (s[i] - '0');
  2305. digits = 1;
  2306. i++;
  2307. }
  2308. if (i < len && s[i] == '.') {
  2309. i++;
  2310. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2311. if (fscale < 1000) {
  2312. frac = frac * 10 + (s[i] - '0');
  2313. fscale *= 10;
  2314. }
  2315. i++;
  2316. }
  2317. }
  2318. if (!digits)
  2319. return 0;
  2320. while (i < len && is_space(s[i]))
  2321. i++;
  2322. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2323. is_rem = 1;
  2324. is_px = 0;
  2325. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2326. is_rem = 1;
  2327. is_px = 0;
  2328. } else if (i < len && s[i] == '%') {
  2329. is_pct = 1;
  2330. is_px = 0;
  2331. }
  2332. v1000 = whole * 1000 + frac * 1000 / fscale;
  2333. if (is_rem)
  2334. px = v1000 * root_font_px / 1000;
  2335. else if (is_pct)
  2336. px = v1000 * root_font_px / 100000;
  2337. else if (is_px)
  2338. px = v1000 / 1000;
  2339. else
  2340. px = v1000 / 1000;
  2341. if (px < 8)
  2342. px = 8;
  2343. if (px > 72)
  2344. px = 72;
  2345. return px;
  2346. }
  2347. static int parse_line_height(const char *s, unsigned int len,
  2348. unsigned short *px, unsigned char *pct)
  2349. {
  2350. unsigned int i = 0;
  2351. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2352. int v1000;
  2353. int is_px = 0, is_pct = 0;
  2354. *px = 0;
  2355. *pct = 0;
  2356. while (i < len && is_space(s[i]))
  2357. i++;
  2358. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2359. return 0;
  2360. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2361. whole = whole * 10 + (s[i] - '0');
  2362. digits = 1;
  2363. i++;
  2364. }
  2365. if (i < len && s[i] == '.') {
  2366. i++;
  2367. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2368. frac = frac * 10 + (s[i] - '0');
  2369. fscale *= 10;
  2370. i++;
  2371. }
  2372. }
  2373. if (!digits)
  2374. return 0;
  2375. while (i < len && is_space(s[i]))
  2376. i++;
  2377. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2378. is_px = 1;
  2379. else if (i < len && s[i] == '%')
  2380. is_pct = 1;
  2381. v1000 = whole * 1000 + frac * 1000 / fscale;
  2382. if (is_px) {
  2383. int v = v1000 / 1000;
  2384. if (v < 8)
  2385. v = 8;
  2386. if (v > 160)
  2387. v = 160;
  2388. *px = (unsigned short)v;
  2389. } else {
  2390. int v = v1000 / 10; /* unitless multiplier -> percent */
  2391. if (is_pct)
  2392. v = v1000 / 1000;
  2393. if (v < 50)
  2394. v = 50;
  2395. if (v > 400)
  2396. v = 400;
  2397. *pct = (unsigned char)v;
  2398. }
  2399. return 1;
  2400. }
  2401. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2402. {
  2403. unsigned int i, j, n = s_len(needle);
  2404. for (i = 0; i + n <= len; i++) {
  2405. for (j = 0; j < n; j++) {
  2406. char a = s[i + j];
  2407. if (a >= 'A' && a <= 'Z')
  2408. a = (char)(a - 'A' + 'a');
  2409. if (a != needle[j])
  2410. break;
  2411. }
  2412. if (j == n)
  2413. return 1;
  2414. }
  2415. return 0;
  2416. }
  2417. /* Pull a colour out of a border/background shorthand. */
  2418. static int find_color(const char *s, unsigned int len, unsigned short *c)
  2419. {
  2420. unsigned int i;
  2421. for (i = 0; i < len; i++) {
  2422. if (s[i] == '#')
  2423. return parse_color(s + i, len - i, c);
  2424. if ((s[i] == 'r' || s[i] == 'R') &&
  2425. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2426. return parse_color(s + i, len - i, c);
  2427. }
  2428. for (i = 0; i < len; ) {
  2429. unsigned int st, e;
  2430. while (i < len && (is_space(s[i]) || s[i] == ','))
  2431. i++;
  2432. st = i;
  2433. while (i < len && !is_space(s[i]) && s[i] != ',')
  2434. i++;
  2435. e = i;
  2436. if (e > st && parse_color(s + st, e - st, c))
  2437. return 1;
  2438. }
  2439. return 0;
  2440. }
  2441. /* Background shorthands can hold several layers before the final solid
  2442. * colour. The colour is conventionally the last token (e.g. a stack of
  2443. * gradients followed by var(--bg)), so take the rightmost colour rather
  2444. * than the first gradient stop. */
  2445. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2446. {
  2447. int last = -1;
  2448. unsigned int i;
  2449. for (i = 0; i < len; i++) {
  2450. if (s[i] == '#')
  2451. last = (int)i;
  2452. else if ((s[i] == 'r' || s[i] == 'R') &&
  2453. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2454. last = (int)i;
  2455. }
  2456. if (last >= 0)
  2457. return parse_color(s + last, len - (unsigned int)last, c);
  2458. while (i > 0) {
  2459. unsigned int st, e;
  2460. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2461. s[i - 1] == ')' || s[i - 1] == '('))
  2462. i--;
  2463. e = i;
  2464. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  2465. s[i - 1] != ')' && s[i - 1] != '(')
  2466. i--;
  2467. st = i;
  2468. if (e > st && parse_color(s + st, e - st, c))
  2469. return 1;
  2470. }
  2471. return 0;
  2472. }
  2473. static int parse_display(const char *s, unsigned int len)
  2474. {
  2475. unsigned int i;
  2476. while (len && is_space(s[0])) { s++; len--; }
  2477. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  2478. ;
  2479. if (i == 4 && ci_eq_n(s, "none", 4))
  2480. return BR_DISPLAY_NONE;
  2481. if (i == 5 && ci_eq_n(s, "block", 5))
  2482. return BR_DISPLAY_BLOCK;
  2483. if (i == 6 && ci_eq_n(s, "inline", 6))
  2484. return BR_DISPLAY_INLINE;
  2485. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  2486. return BR_DISPLAY_INLINE_BLOCK;
  2487. if (i == 10 && ci_eq_n(s, "list-item", 10))
  2488. return BR_DISPLAY_BLOCK;
  2489. if (i == 4 && ci_eq_n(s, "flex", 4))
  2490. return BR_DISPLAY_FLEX;
  2491. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  2492. return BR_DISPLAY_FLEX;
  2493. if (i == 5 && ci_eq_n(s, "table", 5))
  2494. return BR_DISPLAY_BLOCK;
  2495. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  2496. return BR_DISPLAY_BLOCK;
  2497. /* Grid shells are common on news/wiki pages, but the two-column
  2498. * heuristic is not reliable for their nested area templates yet.
  2499. * Collapse them to block flow so the content stays readable. */
  2500. if (i == 4 && ci_eq_n(s, "grid", 4))
  2501. return BR_DISPLAY_BLOCK;
  2502. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  2503. return BR_DISPLAY_BLOCK;
  2504. return BR_DISPLAY_INLINE;
  2505. }
  2506. static void decl_add(struct br_decl *out, unsigned int *nout,
  2507. unsigned char prop, int val, int important)
  2508. {
  2509. if (*nout >= BR_DECLS)
  2510. return;
  2511. out[*nout].prop = prop;
  2512. out[*nout].val = (short)val;
  2513. out[*nout].important = (unsigned char)(important != 0);
  2514. (*nout)++;
  2515. }
  2516. static int parse_border_width(const char *s, unsigned int len)
  2517. {
  2518. unsigned int i = 0;
  2519. while (i < len) {
  2520. while (i < len && is_space(s[i]))
  2521. i++;
  2522. if (i >= len)
  2523. break;
  2524. if (s[i] == '#') {
  2525. while (i < len && !is_space(s[i]))
  2526. i++;
  2527. continue;
  2528. }
  2529. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2530. while (i < len && s[i] != ')')
  2531. i++;
  2532. if (i < len)
  2533. i++;
  2534. continue;
  2535. }
  2536. if (s[i] >= '0' && s[i] <= '9') {
  2537. int v = 0;
  2538. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2539. v = v * 10 + (s[i] - '0');
  2540. i++;
  2541. }
  2542. if (v < 0)
  2543. v = 0;
  2544. if (v > 3)
  2545. v = 3;
  2546. return v;
  2547. }
  2548. while (i < len && !is_space(s[i]))
  2549. i++;
  2550. }
  2551. return -1;
  2552. }
  2553. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  2554. * the input is copied verbatim when it contains no var(). */
  2555. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  2556. unsigned int len, char *out,
  2557. unsigned int cap, int depth)
  2558. {
  2559. unsigned int i = 0, n = 0;
  2560. if (depth > 8)
  2561. return 0;
  2562. while (i < len && n + 1 < cap) {
  2563. unsigned int p, inner, close, comma, nlen, fstart;
  2564. char quote;
  2565. int parens;
  2566. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  2567. out[n++] = s[i++];
  2568. continue;
  2569. }
  2570. p = i + 4;
  2571. quote = 0;
  2572. parens = 1;
  2573. while (p < len && parens) {
  2574. char c = s[p];
  2575. if (quote) {
  2576. if (c == quote)
  2577. quote = 0;
  2578. else if (c == '\\' && p + 1 < len)
  2579. p++;
  2580. } else if (c == '"' || c == '\'') {
  2581. quote = c;
  2582. } else if (c == '(') {
  2583. parens++;
  2584. } else if (c == ')') {
  2585. parens--;
  2586. }
  2587. p++;
  2588. }
  2589. if (parens) {
  2590. out[n++] = s[i++];
  2591. continue;
  2592. }
  2593. close = p - 1;
  2594. inner = i + 4;
  2595. while (inner < close && is_space(s[inner]))
  2596. inner++;
  2597. comma = inner;
  2598. parens = 0;
  2599. quote = 0;
  2600. while (comma < close) {
  2601. char c = s[comma];
  2602. if (quote) {
  2603. if (c == quote)
  2604. quote = 0;
  2605. } else if (c == '"' || c == '\'') {
  2606. quote = c;
  2607. } else if (c == '(') {
  2608. parens++;
  2609. } else if (c == ')' && parens > 0) {
  2610. parens--;
  2611. } else if (c == ',' && parens == 0) {
  2612. break;
  2613. }
  2614. comma++;
  2615. }
  2616. nlen = comma - inner;
  2617. while (nlen && is_space(s[inner + nlen - 1]))
  2618. nlen--;
  2619. fstart = comma < close ? comma + 1 : close;
  2620. while (fstart < close && is_space(s[fstart]))
  2621. fstart++;
  2622. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  2623. unsigned int vi = css_var_find(d, s + inner, nlen);
  2624. if (vi < BR_MAX_VARS) {
  2625. const char *v = attr_ptr(d, d->var_val[vi]);
  2626. unsigned int vn = css_expand_vars(d, v, s_len(v),
  2627. out + n,
  2628. cap - n, depth + 1);
  2629. n += vn;
  2630. } else if (fstart < close) {
  2631. unsigned int vn = css_expand_vars(d, s + fstart,
  2632. close - fstart,
  2633. out + n, cap - n,
  2634. depth + 1);
  2635. n += vn;
  2636. }
  2637. } else if (fstart < close) {
  2638. unsigned int vn = css_expand_vars(d, s + fstart,
  2639. close - fstart,
  2640. out + n, cap - n,
  2641. depth + 1);
  2642. n += vn;
  2643. }
  2644. i = p;
  2645. }
  2646. out[n] = '\0';
  2647. return n;
  2648. }
  2649. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  2650. unsigned int i)
  2651. {
  2652. while (i < len && (is_space(s[i]) || s[i] == ','))
  2653. i++;
  2654. return i;
  2655. }
  2656. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  2657. * argument). Returns 1 on success. */
  2658. static int parse_grid_track(const char *s, unsigned int len,
  2659. unsigned int *pp, unsigned short *val,
  2660. unsigned char *kind)
  2661. {
  2662. unsigned int i = grid_skip_sep(s, len, *pp);
  2663. unsigned int whole = 0, frac = 0, scale = 1;
  2664. int any = 0;
  2665. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  2666. unsigned int j = i + 7;
  2667. while (j < len && s[j] != ',')
  2668. j++;
  2669. if (j >= len)
  2670. return 0;
  2671. i = j + 1;
  2672. while (i < len && is_space(s[i]))
  2673. i++;
  2674. }
  2675. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  2676. int depth = 1;
  2677. i += 7;
  2678. while (i < len && depth) {
  2679. if (s[i] == '(')
  2680. depth++;
  2681. else if (s[i] == ')')
  2682. depth--;
  2683. i++;
  2684. }
  2685. *pp = i;
  2686. return 0;
  2687. }
  2688. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2689. whole = whole * 10 + (unsigned int)(s[i] - '0');
  2690. i++;
  2691. any = 1;
  2692. }
  2693. if (i < len && s[i] == '.') {
  2694. i++;
  2695. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2696. if (scale < 1000) {
  2697. frac = frac * 10 + (unsigned int)(s[i] - '0');
  2698. scale *= 10;
  2699. }
  2700. i++;
  2701. any = 1;
  2702. }
  2703. }
  2704. if (!any) {
  2705. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  2706. *val = 1;
  2707. *kind = 3;
  2708. *pp = i + 4;
  2709. return 1;
  2710. }
  2711. return 0;
  2712. }
  2713. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  2714. *val = (unsigned short)(whole ? whole : 1);
  2715. *kind = 3;
  2716. i += 2;
  2717. } else if (i < len && s[i] == '%') {
  2718. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  2719. *kind = 2;
  2720. i++;
  2721. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2722. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  2723. *kind = 1;
  2724. i += 3;
  2725. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2726. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  2727. *kind = 1;
  2728. i += 2;
  2729. } else {
  2730. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  2731. *kind = 1;
  2732. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2733. i += 2;
  2734. }
  2735. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  2736. i++;
  2737. *pp = i;
  2738. return 1;
  2739. }
  2740. static int parse_grid_cols(const char *s, unsigned int len,
  2741. unsigned short val[2], unsigned char kind[2])
  2742. {
  2743. unsigned int pos = 0;
  2744. int n = 0;
  2745. while (n < 2) {
  2746. unsigned int before = pos;
  2747. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  2748. break;
  2749. n++;
  2750. if (pos <= before)
  2751. break;
  2752. while (pos < len && is_space(s[pos]))
  2753. pos++;
  2754. if (pos >= len)
  2755. break;
  2756. }
  2757. return n;
  2758. }
  2759. static int parse_grid_slot(const char *s, unsigned int len)
  2760. {
  2761. if (contains_ci_len(s, len, "footer") ||
  2762. contains_ci_len(s, len, "header") ||
  2763. contains_ci_len(s, len, "sitenotice") ||
  2764. contains_ci_len(s, len, "navbar"))
  2765. return 1;
  2766. if (contains_ci_len(s, len, "columnstart") ||
  2767. contains_ci_len(s, len, "sidebar") ||
  2768. contains_ci_len(s, len, "left") ||
  2769. contains_ci_len(s, len, "start"))
  2770. return 2;
  2771. if (contains_ci_len(s, len, "pagecontent") ||
  2772. contains_ci_len(s, len, "content") ||
  2773. contains_ci_len(s, len, "main") ||
  2774. contains_ci_len(s, len, "right") ||
  2775. contains_ci_len(s, len, "end"))
  2776. return 3;
  2777. return 0;
  2778. }
  2779. static int css_has_var(const char *s, unsigned int len)
  2780. {
  2781. unsigned int i;
  2782. for (i = 0; i + 4 <= len; i++)
  2783. if ((s[i] == 'v' || s[i] == 'V') &&
  2784. ci_eq_n(s + i, "var(", 4))
  2785. return 1;
  2786. return 0;
  2787. }
  2788. static unsigned int css_skip_comment(const char *s, unsigned int len,
  2789. unsigned int i)
  2790. {
  2791. i += 2;
  2792. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  2793. i++;
  2794. if (i + 1 < len)
  2795. i += 2;
  2796. else
  2797. i = len;
  2798. return i;
  2799. }
  2800. static unsigned int css_skip_string(const char *s, unsigned int len,
  2801. unsigned int i)
  2802. {
  2803. char q = s[i++];
  2804. while (i < len && s[i] != q) {
  2805. if (s[i] == '\\' && i + 1 < len)
  2806. i++;
  2807. i++;
  2808. }
  2809. if (i < len)
  2810. i++;
  2811. return i;
  2812. }
  2813. /* Find the '}' that closes a declaration block. i points just past the
  2814. * opening '{'. Comments and quoted strings are skipped: a brace inside
  2815. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  2816. * selectors such as "Highlight-Stories {" without a closing brace). */
  2817. static unsigned int css_block_end(const char *s, unsigned int len,
  2818. unsigned int i)
  2819. {
  2820. int depth = 0;
  2821. while (i < len) {
  2822. char c = s[i];
  2823. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  2824. i = css_skip_comment(s, len, i);
  2825. continue;
  2826. }
  2827. if (c == '"' || c == '\'') {
  2828. i = css_skip_string(s, len, i);
  2829. continue;
  2830. }
  2831. if (c == '{') {
  2832. depth++;
  2833. } else if (c == '}') {
  2834. if (depth == 0)
  2835. return i;
  2836. depth--;
  2837. }
  2838. i++;
  2839. }
  2840. return len;
  2841. }
  2842. /* Skip a block whose opening '{' has already been consumed. */
  2843. static unsigned int css_skip_nested(const char *s, unsigned int len,
  2844. unsigned int i)
  2845. {
  2846. int depth = 1;
  2847. while (i < len && depth) {
  2848. char c = s[i];
  2849. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  2850. i = css_skip_comment(s, len, i);
  2851. continue;
  2852. }
  2853. if (c == '"' || c == '\'') {
  2854. i = css_skip_string(s, len, i);
  2855. continue;
  2856. }
  2857. if (c == '{')
  2858. depth++;
  2859. else if (c == '}')
  2860. depth--;
  2861. i++;
  2862. }
  2863. return i;
  2864. }
  2865. /* One border-radius length. Unlike parse_size(), percentages keep their
  2866. * kind: the drawer scales them to the box, not the viewport. */
  2867. static int parse_radius_value(const char *s, unsigned int len,
  2868. unsigned int *pos, unsigned short *val,
  2869. unsigned char *kind)
  2870. {
  2871. unsigned int i = *pos;
  2872. unsigned long v = 0;
  2873. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  2874. while (i < len && is_space(s[i]))
  2875. i++;
  2876. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2877. v = v * 10 + (unsigned int)(s[i] - '0');
  2878. digits = 1;
  2879. i++;
  2880. }
  2881. if (i < len && s[i] == '.') {
  2882. i++;
  2883. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2884. fdigits = 1;
  2885. if (frac < 2) {
  2886. v = v * 10 + (unsigned int)(s[i] - '0');
  2887. frac++;
  2888. }
  2889. i++;
  2890. }
  2891. }
  2892. while (frac < 2) {
  2893. v *= 10;
  2894. frac++;
  2895. }
  2896. while (i < len && is_space(s[i]))
  2897. i++;
  2898. if (i < len && s[i] == '%') {
  2899. pct = 1;
  2900. i++;
  2901. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2902. s[i + 2] == 'm') {
  2903. rem = 1;
  2904. i += 3;
  2905. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2906. rem = 2;
  2907. i += 2;
  2908. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  2909. i += 2;
  2910. } else if (i < len && is_ident(s[i])) {
  2911. return 0;
  2912. }
  2913. if (!digits && !fdigits)
  2914. return 0;
  2915. if (rem == 1)
  2916. v *= root_font_px;
  2917. else if (rem == 2)
  2918. v *= 16;
  2919. v /= 100;
  2920. if (v > 2048)
  2921. v = 2048;
  2922. *val = (unsigned short)v;
  2923. *kind = pct ? 2 : 1;
  2924. *pos = i;
  2925. return 1;
  2926. }
  2927. static void parse_decls(struct br_doc *d, const char *s,
  2928. unsigned int len, struct br_decl *out,
  2929. unsigned int *nout)
  2930. {
  2931. static char expanded[4096];
  2932. if (css_has_var(s, len)) {
  2933. unsigned int elen = css_expand_vars(d, s, len, expanded,
  2934. sizeof(expanded), 0);
  2935. if (elen) {
  2936. s = expanded;
  2937. len = elen;
  2938. }
  2939. }
  2940. unsigned int i = 0;
  2941. *nout = 0;
  2942. while (i < len && *nout < BR_DECLS) {
  2943. unsigned int pstart, pend, vstart, vend, k;
  2944. char prop[24];
  2945. int important = 0;
  2946. while (i < len && (is_space(s[i]) || s[i] == ';'))
  2947. i++;
  2948. if (i >= len)
  2949. break;
  2950. /* A nested at-rule (@media/@supports) inside the declaration
  2951. * block carries its own selector context; skip it rather than
  2952. * reading its prelude as a declaration. */
  2953. if (s[i] == '@') {
  2954. while (i < len && s[i] != '{' && s[i] != ';')
  2955. i++;
  2956. if (i < len && s[i] == '{')
  2957. i = css_skip_nested(s, len, i + 1);
  2958. else if (i < len)
  2959. i++;
  2960. continue;
  2961. }
  2962. pstart = i;
  2963. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  2964. i++;
  2965. if (i < len && s[i] == '{') {
  2966. /* Nested style rule: skip its block. */
  2967. i = css_skip_nested(s, len, i + 1);
  2968. continue;
  2969. }
  2970. pend = i;
  2971. if (i >= len || s[i] != ':')
  2972. continue;
  2973. i++;
  2974. vstart = i;
  2975. while (i < len && s[i] != ';')
  2976. i++;
  2977. vend = i;
  2978. while (pstart < pend && is_space(s[pstart]))
  2979. pstart++;
  2980. while (pend > pstart && is_space(s[pend - 1]))
  2981. pend--;
  2982. while (vstart < vend && is_space(s[vstart]))
  2983. vstart++;
  2984. while (vend > vstart && is_space(s[vend - 1]))
  2985. vend--;
  2986. if (vend - vstart >= 10 &&
  2987. ci_eq_n(s + vend - 10, "!important", 10)) {
  2988. important = 1;
  2989. vend -= 10;
  2990. while (vend > vstart && is_space(s[vend - 1]))
  2991. vend--;
  2992. }
  2993. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  2994. char c = s[pstart + k];
  2995. if (c >= 'A' && c <= 'Z')
  2996. c = (char)(c - 'A' + 'a');
  2997. prop[k] = c;
  2998. }
  2999. prop[k] = '\0';
  3000. if (str_eq(prop, "color")) {
  3001. unsigned short c;
  3002. if (parse_color(s + vstart, vend - vstart, &c))
  3003. decl_add(out, nout, PROP_COLOR, c, important);
  3004. } else if (str_eq(prop, "fill")) {
  3005. unsigned short c;
  3006. if (parse_color(s + vstart, vend - vstart, &c)) {
  3007. decl_add(out, nout, PROP_BG, c, important);
  3008. decl_add(out, nout, PROP_COLOR, c, important);
  3009. }
  3010. } else if (str_eq(prop, "stroke")) {
  3011. unsigned short c;
  3012. if (parse_color(s + vstart, vend - vstart, &c)) {
  3013. decl_add(out, nout, PROP_BORDER_COLOR, c,
  3014. important);
  3015. decl_add(out, nout, PROP_BORDER_W, 1, important);
  3016. }
  3017. } else if (str_eq(prop, "stroke-width")) {
  3018. unsigned int j = vstart;
  3019. int sw = 0;
  3020. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  3021. sw = sw * 10 + (s[j] - '0');
  3022. j++;
  3023. }
  3024. if (sw < 1)
  3025. sw = 1;
  3026. if (sw > 3)
  3027. sw = 3;
  3028. decl_add(out, nout, PROP_BORDER_W, sw, important);
  3029. } else if (str_eq(prop, "text-anchor")) {
  3030. int a = BR_ALIGN_LEFT;
  3031. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  3032. a = BR_ALIGN_CENTER;
  3033. else if (contains_ci_len(s + vstart, vend - vstart,
  3034. "end"))
  3035. a = BR_ALIGN_RIGHT;
  3036. decl_add(out, nout, PROP_ALIGN, a, important);
  3037. } else if (str_eq(prop, "background-color") ||
  3038. str_eq(prop, "background")) {
  3039. unsigned short c;
  3040. if (parse_color(s + vstart, vend - vstart, &c))
  3041. decl_add(out, nout, PROP_BG, c, important);
  3042. else if (str_eq(prop, "background") &&
  3043. find_color_last(s + vstart, vend - vstart, &c))
  3044. decl_add(out, nout, PROP_BG, c, important);
  3045. } else if (str_eq(prop, "font-size")) {
  3046. int px = parse_font_px(s + vstart, vend - vstart);
  3047. if (px > 0)
  3048. decl_add(out, nout, PROP_FONT_PX, px,
  3049. important);
  3050. } else if (str_eq(prop, "font-weight")) {
  3051. int bold = 0;
  3052. unsigned int j;
  3053. if (vend - vstart >= 4 &&
  3054. ci_eq_n(s + vstart, "bold", 4))
  3055. bold = 1;
  3056. else {
  3057. int w = 0;
  3058. for (j = vstart; j < vend; j++)
  3059. if (s[j] >= '0' && s[j] <= '9')
  3060. w = w * 10 + (s[j] - '0');
  3061. if (w >= 500)
  3062. bold = 1;
  3063. }
  3064. decl_add(out, nout, PROP_BOLD, bold, important);
  3065. } else if (str_eq(prop, "font-style")) {
  3066. int italic = (vend - vstart >= 6 &&
  3067. ci_eq_n(s + vstart, "italic", 6));
  3068. decl_add(out, nout, PROP_ITALIC, italic, important);
  3069. } else if (str_eq(prop, "font-family")) {
  3070. unsigned int j;
  3071. int mono = 0;
  3072. for (j = vstart; j + 4 <= vend; j++)
  3073. if (ci_eq_n(s + j, "mono", 4))
  3074. mono = 1;
  3075. decl_add(out, nout, PROP_MONO, mono, important);
  3076. } else if (str_eq(prop, "text-align")) {
  3077. int a = BR_ALIGN_LEFT;
  3078. if (vend - vstart >= 6 &&
  3079. ci_eq_n(s + vstart, "center", 6))
  3080. a = BR_ALIGN_CENTER;
  3081. else if (vend - vstart >= 5 &&
  3082. ci_eq_n(s + vstart, "right", 5))
  3083. a = BR_ALIGN_RIGHT;
  3084. decl_add(out, nout, PROP_ALIGN, a, important);
  3085. } else if (str_eq(prop, "text-decoration")) {
  3086. int u = 0;
  3087. if (vend - vstart >= 9 &&
  3088. ci_eq_n(s + vstart, "underline", 9))
  3089. u = 1;
  3090. decl_add(out, nout, PROP_UNDERLINE, u, important);
  3091. } else if (str_eq(prop, "text-transform")) {
  3092. int u = (vend - vstart >= 9 &&
  3093. ci_eq_n(s + vstart, "uppercase", 9));
  3094. decl_add(out, nout, PROP_UPPER, u, important);
  3095. } else if (str_eq(prop, "display")) {
  3096. decl_add(out, nout, PROP_DISPLAY,
  3097. parse_display(s + vstart, vend - vstart),
  3098. important);
  3099. } else if (str_eq(prop, "visibility")) {
  3100. int h = contains_ci_len(s + vstart, vend - vstart,
  3101. "hidden") ||
  3102. contains_ci_len(s + vstart, vend - vstart,
  3103. "collapse");
  3104. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  3105. } else if (str_eq(prop, "opacity")) {
  3106. unsigned int q;
  3107. int h = 1, any = 0;
  3108. for (q = vstart; q < vend; q++) {
  3109. if (s[q] >= '1' && s[q] <= '9') {
  3110. h = 0;
  3111. break;
  3112. }
  3113. if (s[q] >= '0' && s[q] <= '9')
  3114. any = 1;
  3115. }
  3116. if (!any || contains_ci_len(s + vstart, vend - vstart,
  3117. "inherit"))
  3118. h = 0;
  3119. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  3120. } else if (str_eq(prop, "overflow") ||
  3121. str_eq(prop, "overflow-x") ||
  3122. str_eq(prop, "overflow-y")) {
  3123. int h = contains_ci_len(s + vstart, vend - vstart,
  3124. "hidden") ||
  3125. contains_ci_len(s + vstart, vend - vstart,
  3126. "clip");
  3127. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  3128. } else if (str_eq(prop, "float")) {
  3129. int f = 0;
  3130. if (vend - vstart >= 4 &&
  3131. ci_eq_n(s + vstart, "left", 4))
  3132. f = 1;
  3133. else if (vend - vstart >= 5 &&
  3134. ci_eq_n(s + vstart, "right", 5))
  3135. f = 2;
  3136. decl_add(out, nout, PROP_FLOAT, f, important);
  3137. } else if (str_eq(prop, "flex-grow") ||
  3138. str_eq(prop, "flex")) {
  3139. unsigned int q = vstart;
  3140. int g = 0;
  3141. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  3142. q++;
  3143. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  3144. g = g * 10 + (s[q] - '0');
  3145. q++;
  3146. }
  3147. if (g > 100)
  3148. g = 100;
  3149. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  3150. } else if (str_eq(prop, "flex-direction")) {
  3151. decl_add(out, nout, PROP_FLEX_COL,
  3152. contains_ci_len(s + vstart, vend - vstart,
  3153. "column"), important);
  3154. } else if (str_eq(prop, "flex-wrap")) {
  3155. int wrap = 0;
  3156. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  3157. wrap = 2;
  3158. else if (contains_ci_len(s + vstart, vend - vstart,
  3159. "wrap"))
  3160. wrap = 1;
  3161. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  3162. } else if (str_eq(prop, "position")) {
  3163. int p = 0;
  3164. if (contains_ci_len(s + vstart, vend - vstart,
  3165. "absolute"))
  3166. p = 1;
  3167. else if (contains_ci_len(s + vstart, vend - vstart,
  3168. "fixed"))
  3169. p = 2;
  3170. /* position:sticky stays in the flow. Treating it as
  3171. * fixed painted the header over the first content
  3172. * block; a static header is safer than hidden text. */
  3173. decl_add(out, nout, PROP_POS, p, important);
  3174. } else if (str_eq(prop, "left") || str_eq(prop, "top")) {
  3175. int is_left = prop[0] == 'l';
  3176. unsigned int pos = 0;
  3177. if (!(vend - vstart >= 4 &&
  3178. ci_eq_n(s + vstart, "auto", 4))) {
  3179. int v = parse_len(s + vstart, vend - vstart, &pos);
  3180. if (v > 32000)
  3181. v = 32000;
  3182. else if (v < -32000)
  3183. v = -32000;
  3184. decl_add(out, nout,
  3185. is_left ? PROP_LEFT : PROP_TOP, v, important);
  3186. decl_add(out, nout,
  3187. is_left ? PROP_LEFT_SET : PROP_TOP_SET, 1,
  3188. important);
  3189. }
  3190. } else if (str_eq(prop, "grid-template-columns") ||
  3191. str_eq(prop, "grid-template")) {
  3192. const char *v = s + vstart;
  3193. unsigned int vl = vend - vstart;
  3194. unsigned short val[2];
  3195. unsigned char kind[2];
  3196. int n, q;
  3197. if (str_eq(prop, "grid-template")) {
  3198. for (q = 0; q < (int)vl; q++)
  3199. if (v[q] == '/') {
  3200. q++;
  3201. while (q < (int)vl && is_space(v[q]))
  3202. q++;
  3203. v += q;
  3204. vl -= (unsigned int)q;
  3205. break;
  3206. }
  3207. }
  3208. if (contains_ci_len(v, vl, "auto-fit") ||
  3209. contains_ci_len(v, vl, "auto-fill")) {
  3210. unsigned int mp = 0;
  3211. int found = 0;
  3212. unsigned short mw = 200;
  3213. unsigned char mk = 1;
  3214. for (q = 0; q + 7 <= (int)vl; q++)
  3215. if (ci_eq_n(v + q, "minmax(", 7)) {
  3216. mp = (unsigned int)q + 7;
  3217. found = 1;
  3218. break;
  3219. }
  3220. if (found)
  3221. parse_grid_track(v, vl, &mp, &mw, &mk);
  3222. if (mk != 1 || mw < 80)
  3223. mw = 200;
  3224. if (mw > 480)
  3225. mw = 480;
  3226. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  3227. decl_add(out, nout, PROP_WIDTH, mw, important);
  3228. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  3229. } else {
  3230. n = parse_grid_cols(v, vl, val, kind);
  3231. if (n > 0) {
  3232. decl_add(out, nout, PROP_WIDTH, val[0],
  3233. important);
  3234. decl_add(out, nout, PROP_WIDTH_KIND,
  3235. kind[0], important);
  3236. decl_add(out, nout, PROP_GRID_NCOLS, n,
  3237. important);
  3238. }
  3239. if (n > 1) {
  3240. decl_add(out, nout, PROP_MAXW, val[1],
  3241. important);
  3242. decl_add(out, nout, PROP_MAXW_KIND,
  3243. kind[1], important);
  3244. }
  3245. }
  3246. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  3247. unsigned int gp = 0;
  3248. unsigned short gv = 0;
  3249. unsigned char gk = 1;
  3250. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  3251. if (gv > 120)
  3252. gv = 120;
  3253. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  3254. }
  3255. } else if (str_eq(prop, "grid-area")) {
  3256. int slot = parse_grid_slot(s + vstart, vend - vstart);
  3257. if (slot)
  3258. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  3259. } else if (str_eq(prop, "width") ||
  3260. str_eq(prop, "max-width") ||
  3261. str_eq(prop, "height")) {
  3262. unsigned short v;
  3263. unsigned char kind;
  3264. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  3265. if (str_eq(prop, "width")) {
  3266. decl_add(out, nout, PROP_WIDTH, v, important);
  3267. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  3268. important);
  3269. } else if (str_eq(prop, "max-width")) {
  3270. decl_add(out, nout, PROP_MAXW, v, important);
  3271. decl_add(out, nout, PROP_MAXW_KIND, kind,
  3272. important);
  3273. } else {
  3274. decl_add(out, nout, PROP_HEIGHT, v, important);
  3275. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  3276. important);
  3277. }
  3278. }
  3279. } else if (str_eq(prop, "line-height")) {
  3280. unsigned short px;
  3281. unsigned char pct;
  3282. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  3283. if (px) {
  3284. decl_add(out, nout, PROP_LH_PX, px, important);
  3285. decl_add(out, nout, PROP_LH_PCT, 0, important);
  3286. } else {
  3287. decl_add(out, nout, PROP_LH_PX, 0, important);
  3288. decl_add(out, nout, PROP_LH_PCT, pct, important);
  3289. }
  3290. }
  3291. } else if (str_eq(prop, "list-style") ||
  3292. str_eq(prop, "list-style-type")) {
  3293. int none = (vend - vstart >= 4 &&
  3294. contains_ci_len(s + vstart, vend - vstart,
  3295. "none"));
  3296. decl_add(out, nout, PROP_LIST_NONE, none, important);
  3297. } else if (str_eq(prop, "border-radius") ||
  3298. str_eq(prop, "border-top-left-radius") ||
  3299. str_eq(prop, "border-top-right-radius") ||
  3300. str_eq(prop, "border-bottom-right-radius") ||
  3301. str_eq(prop, "border-bottom-left-radius")) {
  3302. unsigned short v[4];
  3303. unsigned char k[4];
  3304. unsigned int pos = 0;
  3305. int n = 0;
  3306. while (pos < vend - vstart && n < 4) {
  3307. while (pos < vend - vstart &&
  3308. is_space(s[vstart + pos]))
  3309. pos++;
  3310. if (pos >= vend - vstart ||
  3311. s[vstart + pos] == '/')
  3312. break;
  3313. if (!parse_radius_value(s + vstart,
  3314. vend - vstart, &pos,
  3315. &v[n], &k[n]))
  3316. break;
  3317. n++;
  3318. }
  3319. if (n) {
  3320. unsigned short r[4];
  3321. unsigned char rk[4];
  3322. int i;
  3323. if (n == 1) {
  3324. for (i = 0; i < 4; i++) {
  3325. r[i] = v[0];
  3326. rk[i] = k[0];
  3327. }
  3328. } else if (n == 2) {
  3329. r[0] = r[2] = v[0];
  3330. r[1] = r[3] = v[1];
  3331. rk[0] = rk[2] = k[0];
  3332. rk[1] = rk[3] = k[1];
  3333. } else if (n == 3) {
  3334. r[0] = v[0];
  3335. r[1] = r[3] = v[1];
  3336. r[2] = v[2];
  3337. rk[0] = k[0];
  3338. rk[1] = rk[3] = k[1];
  3339. rk[2] = k[2];
  3340. } else {
  3341. for (i = 0; i < 4; i++) {
  3342. r[i] = v[i];
  3343. rk[i] = k[i];
  3344. }
  3345. }
  3346. for (i = 0; i < 4; i++)
  3347. r[i] = rk[i] == 2 ? 255 :
  3348. (r[i] > 255 ? 255 : r[i]);
  3349. if (str_eq(prop, "border-top-left-radius")) {
  3350. decl_add(out, nout, PROP_RADIUS_TL,
  3351. (short)r[0], important);
  3352. } else if (str_eq(prop,
  3353. "border-top-right-radius")) {
  3354. decl_add(out, nout, PROP_RADIUS_TR,
  3355. (short)r[0], important);
  3356. } else if (str_eq(prop,
  3357. "border-bottom-right-radius")) {
  3358. decl_add(out, nout, PROP_RADIUS_BR,
  3359. (short)r[0], important);
  3360. } else if (str_eq(prop,
  3361. "border-bottom-left-radius")) {
  3362. decl_add(out, nout, PROP_RADIUS_BL,
  3363. (short)r[0], important);
  3364. } else {
  3365. decl_add(out, nout, PROP_RADIUS_TL,
  3366. (short)r[0], important);
  3367. decl_add(out, nout, PROP_RADIUS_TR,
  3368. (short)r[1], important);
  3369. decl_add(out, nout, PROP_RADIUS_BR,
  3370. (short)r[2], important);
  3371. decl_add(out, nout, PROP_RADIUS_BL,
  3372. (short)r[3], important);
  3373. }
  3374. }
  3375. } else if (str_eq(prop, "border") ||
  3376. str_eq(prop, "border-top") ||
  3377. str_eq(prop, "border-right") ||
  3378. str_eq(prop, "border-bottom") ||
  3379. str_eq(prop, "border-left") ||
  3380. str_eq(prop, "border-color") ||
  3381. str_eq(prop, "border-width")) {
  3382. unsigned short c;
  3383. int bw;
  3384. int have_color;
  3385. if (vend - vstart >= 4 &&
  3386. ci_eq_n(s + vstart, "none", 4)) {
  3387. decl_add(out, nout, PROP_BORDER_W, 0, important);
  3388. } else {
  3389. bw = parse_border_width(s + vstart,
  3390. vend - vstart);
  3391. have_color = find_color(s + vstart,
  3392. vend - vstart, &c);
  3393. if (have_color) {
  3394. decl_add(out, nout, PROP_BORDER_COLOR, c,
  3395. important);
  3396. if (bw < 0)
  3397. bw = 1;
  3398. }
  3399. if (bw >= 0) {
  3400. if (bw > 3)
  3401. bw = 3;
  3402. decl_add(out, nout, PROP_BORDER_W, bw,
  3403. important);
  3404. }
  3405. }
  3406. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  3407. int vals[4], nv = 0;
  3408. unsigned int pos = 0;
  3409. int is_pad = prop[0] == 'p';
  3410. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  3411. while (pos < vend - vstart && nv < 4) {
  3412. vals[nv++] = parse_len(s + vstart,
  3413. vend - vstart, &pos);
  3414. while (pos < vend - vstart &&
  3415. is_space(s[vstart + pos]))
  3416. pos++;
  3417. }
  3418. if (nv == 1) {
  3419. vals[1] = vals[2] = vals[3] = vals[0];
  3420. } else if (nv == 2) {
  3421. vals[2] = vals[0];
  3422. vals[3] = vals[1];
  3423. } else if (nv == 3) {
  3424. vals[3] = vals[1];
  3425. }
  3426. if (nv > 0) {
  3427. decl_add(out, nout, base, vals[0], important);
  3428. decl_add(out, nout, (unsigned char)(base + 1),
  3429. vals[1], important);
  3430. decl_add(out, nout, (unsigned char)(base + 2),
  3431. vals[2], important);
  3432. decl_add(out, nout, (unsigned char)(base + 3),
  3433. vals[3], important);
  3434. }
  3435. } else if (str_eq(prop, "margin-top") ||
  3436. str_eq(prop, "margin-right") ||
  3437. str_eq(prop, "margin-bottom") ||
  3438. str_eq(prop, "margin-left") ||
  3439. str_eq(prop, "padding-top") ||
  3440. str_eq(prop, "padding-right") ||
  3441. str_eq(prop, "padding-bottom") ||
  3442. str_eq(prop, "padding-left")) {
  3443. unsigned int pos = 0;
  3444. int v = parse_len(s + vstart, vend - vstart, &pos);
  3445. unsigned char p = PROP_MT;
  3446. const char *side;
  3447. if (prop[0] == 'p')
  3448. p = PROP_PT;
  3449. side = prop + (prop[0] == 'p' ? 8 : 7);
  3450. if (str_eq(side, "right"))
  3451. p = (unsigned char)(p + 1);
  3452. else if (str_eq(side, "bottom"))
  3453. p = (unsigned char)(p + 2);
  3454. else if (str_eq(side, "left"))
  3455. p = (unsigned char)(p + 3);
  3456. decl_add(out, nout, p, v, important);
  3457. }
  3458. }
  3459. }
  3460. /* Skip @media/@font-face/@keyframes/@import without descending into their
  3461. * nested blocks. This is the mobile/base rules path: media enhancements are
  3462. * deliberately ignored for now. */
  3463. static unsigned int skip_at_rule(const char *s, unsigned int len,
  3464. unsigned int i)
  3465. {
  3466. unsigned int j = i;
  3467. while (j < len && s[j] != '{' && s[j] != ';')
  3468. j++;
  3469. if (j >= len)
  3470. return len;
  3471. if (s[j] == ';')
  3472. return j + 1;
  3473. j++;
  3474. return css_skip_nested(s, len, j);
  3475. }
  3476. static unsigned int viewport_w = 1024;
  3477. static unsigned short theme_text = 0x0000;
  3478. static unsigned short theme_bg = 0xFFFF;
  3479. static unsigned short theme_panel = 0xEF5D;
  3480. static unsigned short theme_link = 0x001B;
  3481. void br_set_theme(unsigned short text, unsigned short bg,
  3482. unsigned short panel, unsigned short link)
  3483. {
  3484. theme_text = text;
  3485. theme_bg = bg;
  3486. theme_panel = panel;
  3487. theme_link = link;
  3488. }
  3489. void br_set_viewport(unsigned int width)
  3490. {
  3491. viewport_w = width ? width : 1024;
  3492. }
  3493. static unsigned int media_len_px(const char *s, unsigned int len)
  3494. {
  3495. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  3496. int em = 0;
  3497. while (i < len && is_space(s[i]))
  3498. i++;
  3499. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3500. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3501. i++;
  3502. }
  3503. if (i < len && s[i] == '.') {
  3504. i++;
  3505. /* Consume every fractional digit even when there are more
  3506. * than the three we keep: leaving the tail behind made the
  3507. * unit check miss "em" (74.9375em became 74px). */
  3508. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3509. if (scale < 1000) {
  3510. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3511. scale *= 10;
  3512. }
  3513. i++;
  3514. }
  3515. }
  3516. while (i < len && is_space(s[i]))
  3517. i++;
  3518. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3519. em = 1;
  3520. i += 2;
  3521. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3522. em = 1;
  3523. i += 3;
  3524. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3525. i += 2;
  3526. }
  3527. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  3528. if (em)
  3529. return v * 16 / 100;
  3530. return v / 100;
  3531. }
  3532. /* One parenthesised media feature: min-width/max-width and the modern
  3533. * (width <= 600px) form. Unknown features are treated as true. */
  3534. static int media_feature_matches(const char *s, unsigned int len)
  3535. {
  3536. unsigned int i;
  3537. while (len && is_space(*s)) { s++; len--; }
  3538. while (len && is_space(s[len - 1])) len--;
  3539. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  3540. /* There is no user-selectable colour scheme. Ignore both
  3541. * variants so a page's base palette is used; sites whose base
  3542. * is dark (like the project docs) then stay dark instead of
  3543. * flipping to their light media override. */
  3544. return 0;
  3545. }
  3546. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  3547. for (i = 9; i < len && s[i] != ':'; i++)
  3548. ;
  3549. if (i < len)
  3550. return viewport_w >= media_len_px(s + i + 1,
  3551. len - i - 1);
  3552. return 1;
  3553. }
  3554. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  3555. for (i = 9; i < len && s[i] != ':'; i++)
  3556. ;
  3557. if (i < len)
  3558. return viewport_w <= media_len_px(s + i + 1,
  3559. len - i - 1);
  3560. return 1;
  3561. }
  3562. for (i = 0; i < len; i++) {
  3563. if (s[i] == '<' || s[i] == '>') {
  3564. int max = s[i] == '<';
  3565. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  3566. if (i + 1 < len && s[i + 1] == '=')
  3567. v = media_len_px(s + i + 2, len - i - 2);
  3568. return max ? viewport_w <= v : viewport_w >= v;
  3569. }
  3570. }
  3571. return 1;
  3572. }
  3573. static int media_query_matches(const char *s, unsigned int len)
  3574. {
  3575. unsigned int i;
  3576. if (contains_ci_len(s, len, "print") &&
  3577. !contains_ci_len(s, len, "screen"))
  3578. return 0;
  3579. if (contains_ci_len(s, len, "not "))
  3580. return 0;
  3581. for (i = 0; i < len; i++) {
  3582. if (s[i] == '(') {
  3583. unsigned int st = ++i;
  3584. while (i < len && s[i] != ')')
  3585. i++;
  3586. if (!media_feature_matches(s + st, i - st))
  3587. return 0;
  3588. }
  3589. }
  3590. return 1;
  3591. }
  3592. static int media_list_matches(const char *s, unsigned int len)
  3593. {
  3594. unsigned int i = 0;
  3595. while (i < len) {
  3596. unsigned int st = i;
  3597. while (i < len && s[i] != ',')
  3598. i++;
  3599. if (media_query_matches(s + st, i - st))
  3600. return 1;
  3601. i++;
  3602. }
  3603. return 0;
  3604. }
  3605. static unsigned int rule_hash(const char *s, unsigned int len)
  3606. {
  3607. unsigned int h = 2166136261u, i;
  3608. for (i = 0; i < len; i++) {
  3609. char c = s[i];
  3610. if (c >= 'A' && c <= 'Z')
  3611. c = (char)(c - 'A' + 'a');
  3612. h ^= (unsigned char)c;
  3613. h *= 16777619u;
  3614. }
  3615. return h % BR_RULE_HASH;
  3616. }
  3617. /* Index a parsed rule by the most selective component of its rightmost
  3618. * compound selector. br_style_for() then only visits rules that can
  3619. * possibly match a node instead of scanning every rule. */
  3620. static void rule_index_add(struct br_doc *d, unsigned int idx,
  3621. const char *key, unsigned int len)
  3622. {
  3623. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  3624. unsigned short *head;
  3625. for (i = 0; i < len; i++) {
  3626. if (key[i] == '.' || key[i] == '#') {
  3627. unsigned int st = i + 1, n = 0;
  3628. while (st + n < len && is_ident(key[st + n]))
  3629. n++;
  3630. if (key[i] == '#') {
  3631. ipos = st;
  3632. ilen = n;
  3633. } else if (n) {
  3634. cpos = st;
  3635. clen = n;
  3636. }
  3637. i = st + n - 1;
  3638. }
  3639. }
  3640. if (ilen)
  3641. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  3642. else if (clen)
  3643. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  3644. else if (len == 5 && ci_eq_n(key, ":root", 5))
  3645. head = &d->rule_tag[BR_TAG_HTML];
  3646. else if (len && is_ident(key[0])) {
  3647. char name[16];
  3648. unsigned int n = 0, tag;
  3649. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  3650. name[n] = key[n];
  3651. n++;
  3652. }
  3653. name[n] = '\0';
  3654. tag = br_tag_from_name(name);
  3655. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  3656. head = &d->rule_tag[tag];
  3657. else
  3658. head = &d->rule_any;
  3659. } else {
  3660. head = &d->rule_any;
  3661. }
  3662. d->rule_next[idx] = *head;
  3663. *head = (unsigned short)idx;
  3664. }
  3665. static unsigned int selector_specificity(const char *s, unsigned int len);
  3666. static int css_collect_only;
  3667. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  3668. {
  3669. unsigned int i = 0;
  3670. while (i < len && d->nrules < d->rule_cap) {
  3671. unsigned int sstart, send, bstart, bend, j;
  3672. struct br_decl decls[BR_DECLS];
  3673. unsigned int nd;
  3674. while (i < len && is_space(s[i]))
  3675. i++;
  3676. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  3677. i += 2;
  3678. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3679. i++;
  3680. i += 2;
  3681. continue;
  3682. }
  3683. if (i >= len || s[i] == '}')
  3684. break;
  3685. if (s[i] == '@') {
  3686. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  3687. unsigned int q = i + 6;
  3688. while (q < len && s[q] != '{')
  3689. q++;
  3690. if (q < len &&
  3691. media_list_matches(s + i + 6,
  3692. q - (i + 6))) {
  3693. unsigned int b = q + 1;
  3694. unsigned int e = css_skip_nested(s, len, b);
  3695. if (e > b)
  3696. parse_css(d, s + b,
  3697. (e - 1) - b);
  3698. i = e;
  3699. } else {
  3700. i = skip_at_rule(s, len, i);
  3701. }
  3702. continue;
  3703. }
  3704. i = skip_at_rule(s, len, i);
  3705. continue;
  3706. }
  3707. sstart = i;
  3708. while (i < len && s[i] != '{')
  3709. i++;
  3710. if (i >= len)
  3711. break;
  3712. send = i;
  3713. i++;
  3714. bstart = i;
  3715. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  3716. * to the matching close brace; stopping at the first '}' left
  3717. * the parser out of sync and dropped the rest of the sheet
  3718. * (nd-aktuell lost its desktop rules that way). */
  3719. bend = css_block_end(s, len, i);
  3720. i = bend < len ? bend + 1 : len;
  3721. while (sstart < send && is_space(s[sstart]))
  3722. sstart++;
  3723. while (send > sstart && is_space(s[send - 1]))
  3724. send--;
  3725. if (sstart == send)
  3726. continue;
  3727. if (!css_dark_selector(s + sstart, send - sstart))
  3728. css_collect_block(d, s + bstart, bend - bstart);
  3729. if (css_collect_only)
  3730. continue;
  3731. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  3732. if (!nd)
  3733. continue;
  3734. /* Split a comma-separated selector list into separate rules. */
  3735. j = sstart;
  3736. while (j <= send && d->nrules < d->rule_cap) {
  3737. unsigned int q = j, e, k;
  3738. char sel[160];
  3739. unsigned int off;
  3740. while (q < send && s[q] != ',')
  3741. q++;
  3742. e = q;
  3743. while (j < e && is_space(s[j]))
  3744. j++;
  3745. while (e > j && is_space(s[e - 1]))
  3746. e--;
  3747. if (e > j) {
  3748. /* Ignore single selectors with sibling combinators,
  3749. * pseudo-classes or attribute tests: they need
  3750. * state/attributes we do not model. The other
  3751. * selectors in the list still apply. */
  3752. for (k = j; k < e; ) {
  3753. if (s[k] == '+' || s[k] == '~')
  3754. break;
  3755. if (s[k] == ':') {
  3756. if (k + 5 <= e &&
  3757. ci_eq_n(s + k, ":root", 5)) {
  3758. k += 5;
  3759. continue;
  3760. }
  3761. if (k + 5 <= e &&
  3762. ci_eq_n(s + k, ":not(", 5)) {
  3763. unsigned int n = k + 5;
  3764. int nd = 1;
  3765. while (n < e && nd) {
  3766. if (s[n] == '(')
  3767. nd++;
  3768. else if (s[n] == ')')
  3769. nd--;
  3770. n++;
  3771. }
  3772. if (nd)
  3773. break;
  3774. k = n;
  3775. continue;
  3776. }
  3777. break;
  3778. }
  3779. k++;
  3780. }
  3781. if (k < e) {
  3782. j = q + 1;
  3783. if (q >= send)
  3784. break;
  3785. continue;
  3786. }
  3787. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  3788. sel[k] = s[j + k];
  3789. sel[k] = '\0';
  3790. off = store_attr(d, sel, k, 159);
  3791. if (off) {
  3792. struct br_rule *r = &d->rules[d->nrules++];
  3793. unsigned int ks = 0, q;
  3794. for (q = 0; q < k; q++)
  3795. if (sel[q] == ' ' || sel[q] == '>')
  3796. ks = q + 1;
  3797. r->sel_off = off;
  3798. r->key_off = store_attr(d, sel + ks,
  3799. k - ks, 159);
  3800. r->spec = (unsigned short)selector_specificity(sel, k);
  3801. r->single = 1;
  3802. {
  3803. unsigned int z;
  3804. for (z = 0; z < k; z++)
  3805. if (is_space(sel[z]) || sel[z] == '>') {
  3806. r->single = 0;
  3807. break;
  3808. }
  3809. }
  3810. r->nd = (unsigned char)nd;
  3811. for (k = 0; k < nd; k++)
  3812. r->d[k] = decls[k];
  3813. rule_index_add(d, d->nrules - 1,
  3814. attr_ptr(d, r->key_off),
  3815. s_len(attr_ptr(d, r->key_off)));
  3816. }
  3817. }
  3818. j = q + 1;
  3819. if (q >= send)
  3820. break;
  3821. }
  3822. }
  3823. }
  3824. /* Sites often set html { font-size: 62.5% } and then size everything in
  3825. * rem. Detect that root size before parsing the sheet so a rem resolves to
  3826. * the same pixels a real browser would use. */
  3827. static void detect_root_font(const char *s, unsigned int len)
  3828. {
  3829. unsigned int i;
  3830. for (i = 0; i + 4 < len; i++) {
  3831. unsigned int j, end, sel, q;
  3832. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  3833. int exact;
  3834. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  3835. s[i + 3] == 'l'))
  3836. continue;
  3837. if (i + 4 < len && s[i + 4] != '{' && s[i + 4] != ',' &&
  3838. s[i + 4] != '.' && s[i + 4] != ':' && s[i + 4] != '[')
  3839. continue;
  3840. sel = i;
  3841. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  3842. s[sel - 1] != ';' && s[sel - 1] != ',')
  3843. sel--;
  3844. q = sel;
  3845. while (q < i && is_space(s[q]))
  3846. q++;
  3847. exact = (q == i);
  3848. if (!exact && i + 4 < len && s[i + 4] != ',' &&
  3849. s[i + 4] != '.')
  3850. continue;
  3851. j = i + 4;
  3852. while (j < len && is_space(s[j]))
  3853. j++;
  3854. if (j >= len || s[j] != '{')
  3855. continue;
  3856. while (j < len && s[j] != '{')
  3857. j++;
  3858. if (j >= len)
  3859. break;
  3860. end = j + 1;
  3861. while (end < len && s[end] != '}')
  3862. end++;
  3863. for (; j + 9 < end; j++) {
  3864. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  3865. s[j + 3] == 't' && s[j + 4] == '-' &&
  3866. s[j + 5] == 's' && s[j + 6] == 'i' &&
  3867. s[j + 7] == 'z' && s[j + 8] == 'e'))
  3868. continue;
  3869. j += 9;
  3870. while (j < end && s[j] != ':')
  3871. j++;
  3872. if (j >= end)
  3873. break;
  3874. j++;
  3875. while (j < end && is_space(s[j]))
  3876. j++;
  3877. while (j < end && s[j] >= '0' && s[j] <= '9') {
  3878. v = v * 10 + (s[j] - '0');
  3879. digits = 1;
  3880. j++;
  3881. }
  3882. if (j < end && s[j] == '.') {
  3883. j++;
  3884. while (j < end && s[j] >= '0' && s[j] <= '9') {
  3885. v = v * 10 + (s[j] - '0');
  3886. frac++;
  3887. j++;
  3888. }
  3889. }
  3890. while (frac < 2) {
  3891. v *= 10;
  3892. frac++;
  3893. }
  3894. while (frac > 2) {
  3895. v /= 10;
  3896. frac--;
  3897. }
  3898. if (!digits)
  3899. break;
  3900. if (j < end && s[j] == '%')
  3901. pct = 1;
  3902. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  3903. px = 1;
  3904. if (pct)
  3905. v = (v * 16 + 5000) / 10000;
  3906. else if (px)
  3907. v /= 100;
  3908. else
  3909. v = v * 16 / 100;
  3910. if (exact && v >= 8 && v <= 24)
  3911. root_font_px = v;
  3912. if (exact)
  3913. return;
  3914. }
  3915. }
  3916. }
  3917. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  3918. {
  3919. detect_root_font(css, len);
  3920. css_collect_only = 1;
  3921. parse_css(d, css, len);
  3922. css_collect_only = 0;
  3923. parse_css(d, css, len);
  3924. }
  3925. void br_parse_inline_css(struct br_doc *d)
  3926. {
  3927. if (d->inline_css_len) {
  3928. css_collect_only = 1;
  3929. parse_css(d, css_store, d->inline_css_len);
  3930. css_collect_only = 0;
  3931. parse_css(d, css_store, d->inline_css_len);
  3932. }
  3933. d->inline_css_len = 0;
  3934. }
  3935. void br_reset_styles(struct br_doc *d)
  3936. {
  3937. unsigned int i;
  3938. for (i = 0; i < d->nnode; i++)
  3939. d->nodes[i].style_valid = 0;
  3940. }
  3941. /* ---- selector matching ---- */
  3942. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  3943. unsigned int clen)
  3944. {
  3945. const char *s = attr_ptr(d, d->nodes[node].class_off);
  3946. unsigned int i = 0;
  3947. while (s[i]) {
  3948. unsigned int st, n;
  3949. while (s[i] && is_space(s[i]))
  3950. i++;
  3951. st = i;
  3952. while (s[i] && !is_space(s[i]))
  3953. i++;
  3954. n = i - st;
  3955. if (n == clen && ci_eq_n(s + st, cls, n))
  3956. return 1;
  3957. }
  3958. return 0;
  3959. }
  3960. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  3961. unsigned int len)
  3962. {
  3963. const char *s = attr_ptr(d, d->nodes[node].id_off);
  3964. unsigned int i;
  3965. for (i = 0; i < len; i++)
  3966. if (s[i] != id[i])
  3967. return 0;
  3968. return s[len] == '\0';
  3969. }
  3970. static int is_ident(char c)
  3971. {
  3972. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  3973. (c >= '0' && c <= '9') || c == '-' || c == '_';
  3974. }
  3975. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  3976. {
  3977. unsigned int n = 0;
  3978. while (lit[n])
  3979. n++;
  3980. return n == nlen && ci_eq_n(name, lit, nlen);
  3981. }
  3982. static int attr_present(struct br_doc *d, unsigned int node,
  3983. const char *name, unsigned int nlen)
  3984. {
  3985. struct br_node *n = &d->nodes[node];
  3986. if (attr_is(name, nlen, "data-collapse-target"))
  3987. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  3988. if (attr_is(name, nlen, "data-collapse-trigger"))
  3989. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  3990. if (attr_is(name, nlen, "is-open"))
  3991. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  3992. if (attr_is(name, nlen, "disabled"))
  3993. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  3994. if (attr_is(name, nlen, "open"))
  3995. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  3996. if (attr_is(name, nlen, "inert"))
  3997. return (n->attr_flags & BR_ATTR_INERT) != 0;
  3998. if (attr_is(name, nlen, "hidden"))
  3999. return (n->flags & BR_NODE_HIDDEN) != 0;
  4000. if (attr_is(name, nlen, "href"))
  4001. return attr_ptr(d, n->href_off)[0] != 0;
  4002. if (attr_is(name, nlen, "class"))
  4003. return attr_ptr(d, n->class_off)[0] != 0;
  4004. if (attr_is(name, nlen, "id"))
  4005. return attr_ptr(d, n->id_off)[0] != 0;
  4006. if (attr_is(name, nlen, "src"))
  4007. return attr_ptr(d, n->src_off)[0] != 0;
  4008. if (attr_is(name, nlen, "alt"))
  4009. return attr_ptr(d, n->alt_off)[0] != 0;
  4010. if (attr_is(name, nlen, "rel"))
  4011. return attr_ptr(d, n->rel_off)[0] != 0;
  4012. return 0;
  4013. }
  4014. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  4015. unsigned int len)
  4016. {
  4017. unsigned int i = 0;
  4018. int got = 0;
  4019. if (i < len && s[i] == '*') {
  4020. i++;
  4021. got = 1;
  4022. } else if (i < len && is_ident(s[i])) {
  4023. char name[16];
  4024. unsigned int n = 0, tag;
  4025. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  4026. name[n++] = s[i++];
  4027. name[n] = '\0';
  4028. tag = br_tag_from_name(name);
  4029. if (tag == BR_TAG_UNKNOWN) {
  4030. const char *raw = attr_ptr(d,
  4031. d->nodes[node].name_off);
  4032. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  4033. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  4034. return 0;
  4035. } else if (tag != d->nodes[node].tag) {
  4036. return 0;
  4037. }
  4038. got = 1;
  4039. }
  4040. while (i < len) {
  4041. if (s[i] == '.') {
  4042. unsigned int st;
  4043. i++;
  4044. st = i;
  4045. while (i < len && is_ident(s[i]))
  4046. i++;
  4047. if (!class_has(d, node, s + st, i - st))
  4048. return 0;
  4049. got = 1;
  4050. } else if (s[i] == '#') {
  4051. unsigned int st;
  4052. i++;
  4053. st = i;
  4054. while (i < len && is_ident(s[i]))
  4055. i++;
  4056. if (!id_eq(d, node, s + st, i - st))
  4057. return 0;
  4058. got = 1;
  4059. } else if (s[i] == '[') {
  4060. unsigned int st, e;
  4061. i++;
  4062. st = e = i;
  4063. while (e < len && s[e] != ']' && s[e] != '=' &&
  4064. !is_space(s[e]))
  4065. e++;
  4066. if (e == st || !attr_present(d, node, s + st, e - st))
  4067. return 0;
  4068. while (i < len && s[i] != ']')
  4069. i++;
  4070. if (i < len)
  4071. i++;
  4072. got = 1;
  4073. } else if (s[i] == ':') {
  4074. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  4075. (i + 5 == len || !is_ident(s[i + 5]))) {
  4076. if (d->nodes[node].tag != BR_TAG_HTML)
  4077. return 0;
  4078. i += 5;
  4079. got = 1;
  4080. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  4081. unsigned int st = i + 5, e = st;
  4082. int depth = 1;
  4083. while (e < len && depth) {
  4084. if (s[e] == '(')
  4085. depth++;
  4086. else if (s[e] == ')')
  4087. depth--;
  4088. if (depth)
  4089. e++;
  4090. }
  4091. if (e >= len)
  4092. return 0;
  4093. for (unsigned int z = st; z < e; z++)
  4094. if (s[z] == ':')
  4095. return 0;
  4096. if (compound_match(d, node, s + st, e - st))
  4097. return 0;
  4098. i = e + 1;
  4099. got = 1;
  4100. } else {
  4101. return 0; /* other pseudo-classes are not modelled */
  4102. }
  4103. } else {
  4104. i++;
  4105. }
  4106. }
  4107. return got;
  4108. }
  4109. static unsigned int selector_specificity(const char *s, unsigned int len)
  4110. {
  4111. unsigned int i, spec = 0;
  4112. for (i = 0; i < len; i++) {
  4113. if (s[i] == '#')
  4114. spec += 100;
  4115. else if (s[i] == '.' || s[i] == '[' ||
  4116. (s[i] == ':' && !(i + 5 <= len &&
  4117. ci_eq_n(s + i, ":root", 5))))
  4118. spec += 10;
  4119. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  4120. if (is_ident(s[i]))
  4121. spec += 1;
  4122. }
  4123. }
  4124. return spec;
  4125. }
  4126. /* Match one complex selector; returns specificity or -1. */
  4127. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  4128. unsigned int len, int right_done)
  4129. {
  4130. struct {
  4131. unsigned int start, end;
  4132. int child;
  4133. } parts[8];
  4134. int np = 0;
  4135. unsigned int i = 0;
  4136. int right;
  4137. while (i < len) {
  4138. int child = 0;
  4139. unsigned int st, e;
  4140. while (i < len && is_space(s[i]))
  4141. i++;
  4142. if (i < len && s[i] == '>') {
  4143. child = 1;
  4144. i++;
  4145. while (i < len && is_space(s[i]))
  4146. i++;
  4147. }
  4148. if (i >= len)
  4149. break;
  4150. st = i;
  4151. while (i < len && !is_space(s[i]) && s[i] != '>')
  4152. i++;
  4153. e = i;
  4154. if (np < 8) {
  4155. parts[np].start = st;
  4156. parts[np].end = e;
  4157. parts[np].child = child;
  4158. np++;
  4159. }
  4160. }
  4161. if (!np)
  4162. return -1;
  4163. if (!right_done &&
  4164. !compound_match(d, node, s + parts[np - 1].start,
  4165. parts[np - 1].end - parts[np - 1].start))
  4166. return -1;
  4167. right = np - 1;
  4168. while (right > 0) {
  4169. unsigned int p = d->nodes[node].parent;
  4170. if (!p)
  4171. return -1;
  4172. if (parts[right].child) {
  4173. if (!compound_match(d, p, s + parts[right - 1].start,
  4174. parts[right - 1].end -
  4175. parts[right - 1].start))
  4176. return -1;
  4177. node = p;
  4178. } else {
  4179. while (p && !compound_match(d, p,
  4180. s + parts[right - 1].start,
  4181. parts[right - 1].end -
  4182. parts[right - 1].start))
  4183. p = d->nodes[p].parent;
  4184. if (!p)
  4185. return -1;
  4186. node = p;
  4187. }
  4188. right--;
  4189. }
  4190. return (int)selector_specificity(s, len);
  4191. }
  4192. #define RULE_MATCH_MAX 512
  4193. struct rule_match {
  4194. unsigned short idx;
  4195. short spec;
  4196. };
  4197. static struct rule_match rule_matches[RULE_MATCH_MAX];
  4198. static unsigned int nrule_matches;
  4199. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  4200. {
  4201. struct br_rule *r = &d->rules[idx];
  4202. const char *key = attr_ptr(d, r->key_off);
  4203. int spec, key_ok = 0;
  4204. if (key[0]) {
  4205. if (!compound_match(d, node, key, s_len(key)))
  4206. return;
  4207. key_ok = 1;
  4208. }
  4209. spec = (r->single && key_ok) ? (int)r->spec :
  4210. selector_match(d, node, attr_ptr(d, r->sel_off),
  4211. s_len(attr_ptr(d, r->sel_off)), key_ok);
  4212. if (spec < 0)
  4213. return;
  4214. if (nrule_matches < RULE_MATCH_MAX) {
  4215. rule_matches[nrule_matches].idx = (unsigned short)idx;
  4216. rule_matches[nrule_matches].spec = (short)spec;
  4217. nrule_matches++;
  4218. }
  4219. }
  4220. static void rule_scan_list(struct br_doc *d, unsigned int node,
  4221. unsigned short head)
  4222. {
  4223. while (head != 0xFFFF) {
  4224. rule_try(d, node, head);
  4225. head = d->rule_next[head];
  4226. }
  4227. }
  4228. static void rule_scan_class(struct br_doc *d, unsigned int node,
  4229. const char *cls, unsigned int len)
  4230. {
  4231. if (len)
  4232. rule_scan_list(d, node,
  4233. d->rule_bucket[rule_hash(cls, len)]);
  4234. }
  4235. static void rule_collect(struct br_doc *d, unsigned int node)
  4236. {
  4237. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  4238. const char *id = attr_ptr(d, d->nodes[node].id_off);
  4239. unsigned int tag = d->nodes[node].tag;
  4240. unsigned int i;
  4241. nrule_matches = 0;
  4242. rule_scan_list(d, node, d->rule_any);
  4243. if (tag < BR_TAG_MAX)
  4244. rule_scan_list(d, node, d->rule_tag[tag]);
  4245. if (id[0])
  4246. rule_scan_list(d, node,
  4247. d->rule_bucket[rule_hash(id, s_len(id))]);
  4248. i = 0;
  4249. while (cls[i]) {
  4250. unsigned int st, n;
  4251. while (cls[i] && is_space(cls[i]))
  4252. i++;
  4253. st = i;
  4254. while (cls[i] && !is_space(cls[i]))
  4255. i++;
  4256. n = i - st;
  4257. if (n)
  4258. rule_scan_class(d, node, cls + st, n);
  4259. }
  4260. /* Rules must cascade in document order even though the index visits
  4261. * them in bucket order. */
  4262. for (i = 1; i < nrule_matches; i++) {
  4263. struct rule_match m = rule_matches[i];
  4264. unsigned int j = i;
  4265. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  4266. rule_matches[j] = rule_matches[j - 1];
  4267. j--;
  4268. }
  4269. rule_matches[j] = m;
  4270. }
  4271. }
  4272. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  4273. {
  4274. switch (dcl->prop) {
  4275. case PROP_COLOR:
  4276. st->color = (unsigned short)dcl->val;
  4277. break;
  4278. case PROP_BG:
  4279. st->bg = (unsigned short)dcl->val;
  4280. st->has_bg = 1;
  4281. break;
  4282. case PROP_FONT_PX:
  4283. if (dcl->val < 8)
  4284. st->font_px = 8;
  4285. else if (dcl->val > 48)
  4286. st->font_px = 48;
  4287. else
  4288. st->font_px = (unsigned char)dcl->val;
  4289. break;
  4290. case PROP_BOLD:
  4291. st->bold = (unsigned char)(dcl->val != 0);
  4292. break;
  4293. case PROP_ITALIC:
  4294. st->italic = (unsigned char)(dcl->val != 0);
  4295. break;
  4296. case PROP_MONO:
  4297. st->mono = (unsigned char)(dcl->val != 0);
  4298. break;
  4299. case PROP_ALIGN:
  4300. st->align = (unsigned char)dcl->val;
  4301. break;
  4302. case PROP_UNDERLINE:
  4303. st->underline = (unsigned char)(dcl->val != 0);
  4304. break;
  4305. case PROP_DISPLAY:
  4306. st->display = (unsigned char)dcl->val;
  4307. break;
  4308. case PROP_VIS_HIDDEN:
  4309. st->vis_hidden = (unsigned char)(dcl->val != 0);
  4310. break;
  4311. case PROP_OPACITY_HIDDEN:
  4312. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  4313. break;
  4314. case PROP_OVERFLOW_HIDDEN:
  4315. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  4316. break;
  4317. case PROP_MT: st->mt = dcl->val; break;
  4318. case PROP_MR: st->mr = dcl->val; break;
  4319. case PROP_MB: st->mb = dcl->val; break;
  4320. case PROP_ML: st->ml = dcl->val; break;
  4321. case PROP_PT: st->pt = dcl->val; break;
  4322. case PROP_PR: st->pr = dcl->val; break;
  4323. case PROP_PB: st->pb = dcl->val; break;
  4324. case PROP_PL: st->pl = dcl->val; break;
  4325. case PROP_BORDER_W:
  4326. st->border_w = (unsigned char)dcl->val;
  4327. if (st->border_w && !st->border_color)
  4328. st->border_color = st->color;
  4329. break;
  4330. case PROP_BORDER_COLOR:
  4331. st->border_color = (unsigned short)dcl->val;
  4332. break;
  4333. case PROP_RADIUS_TL:
  4334. case PROP_RADIUS_TR:
  4335. case PROP_RADIUS_BR:
  4336. case PROP_RADIUS_BL:
  4337. st->radius[dcl->prop - PROP_RADIUS_TL] =
  4338. (unsigned char)dcl->val;
  4339. break;
  4340. case PROP_LIST_NONE:
  4341. st->no_bullet = (unsigned char)(dcl->val != 0);
  4342. break;
  4343. case PROP_WIDTH:
  4344. st->width = (unsigned short)dcl->val;
  4345. break;
  4346. case PROP_WIDTH_KIND:
  4347. st->width_kind = (unsigned char)dcl->val;
  4348. break;
  4349. case PROP_MAXW:
  4350. st->max_width = (unsigned short)dcl->val;
  4351. break;
  4352. case PROP_MAXW_KIND:
  4353. st->max_width_kind = (unsigned char)dcl->val;
  4354. break;
  4355. case PROP_HEIGHT:
  4356. st->height = (unsigned short)dcl->val;
  4357. break;
  4358. case PROP_HEIGHT_KIND:
  4359. st->height_kind = (unsigned char)dcl->val;
  4360. break;
  4361. case PROP_LH_PCT:
  4362. st->line_h_pct = (unsigned char)dcl->val;
  4363. break;
  4364. case PROP_LH_PX:
  4365. st->line_h_px = (unsigned short)dcl->val;
  4366. break;
  4367. case PROP_UPPER:
  4368. st->uppercase = (unsigned char)(dcl->val != 0);
  4369. break;
  4370. case PROP_FLOAT:
  4371. st->float_left = (unsigned char)dcl->val;
  4372. break;
  4373. case PROP_FLEX_GROW:
  4374. st->flex_grow = (unsigned char)dcl->val;
  4375. break;
  4376. case PROP_FLEX_COL:
  4377. st->flex_col = (unsigned char)(dcl->val != 0);
  4378. break;
  4379. case PROP_FLEX_WRAP:
  4380. st->flex_wrap = (unsigned char)(dcl->val != 0);
  4381. break;
  4382. case PROP_GRID_NCOLS:
  4383. st->grid_ncols = (unsigned char)dcl->val;
  4384. break;
  4385. case PROP_GRID_GAP:
  4386. st->grid_gap = (unsigned char)dcl->val;
  4387. break;
  4388. case PROP_GRID_SLOT:
  4389. st->grid_slot = (unsigned char)dcl->val;
  4390. break;
  4391. case PROP_GRID_AUTO:
  4392. st->grid_auto = (unsigned char)(dcl->val != 0);
  4393. break;
  4394. case PROP_POS:
  4395. st->pos = (unsigned char)dcl->val;
  4396. break;
  4397. case PROP_LEFT:
  4398. st->left = dcl->val;
  4399. break;
  4400. case PROP_TOP:
  4401. st->top = dcl->val;
  4402. break;
  4403. case PROP_LEFT_SET:
  4404. st->left_set = (unsigned char)(dcl->val != 0);
  4405. break;
  4406. case PROP_TOP_SET:
  4407. st->top_set = (unsigned char)(dcl->val != 0);
  4408. break;
  4409. default:
  4410. break;
  4411. }
  4412. }
  4413. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  4414. int spec, short *best, short *best_imp)
  4415. {
  4416. unsigned char p = dcl->prop;
  4417. if (p >= PROP_COUNT)
  4418. return;
  4419. if (dcl->important) {
  4420. if (spec >= best_imp[p]) {
  4421. apply_decl(st, dcl);
  4422. best_imp[p] = (short)spec;
  4423. }
  4424. } else if (best_imp[p] < 0 && spec >= best[p]) {
  4425. apply_decl(st, dcl);
  4426. best[p] = (short)spec;
  4427. }
  4428. }
  4429. void br_style_for(struct br_doc *d, unsigned int node,
  4430. const struct br_style *parent, struct br_style *out)
  4431. {
  4432. unsigned int tag;
  4433. short best[PROP_COUNT], best_imp[PROP_COUNT];
  4434. unsigned int i;
  4435. if (d->nodes[node].style_valid) {
  4436. *out = d->nodes[node].style;
  4437. return;
  4438. }
  4439. *out = *parent;
  4440. out->has_bg = 0;
  4441. out->float_left = 0;
  4442. out->border_w = 0;
  4443. out->border_color = 0;
  4444. for (i = 0; i < 4; i++)
  4445. out->radius[i] = 0;
  4446. out->width = out->max_width = out->height = 0;
  4447. out->width_kind = out->max_width_kind = out->height_kind = 0;
  4448. out->mt = out->mr = out->mb = out->ml = 0;
  4449. out->pt = out->pr = out->pb = out->pl = 0;
  4450. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  4451. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  4452. out->grid_auto = 0;
  4453. out->pos = out->left_set = out->top_set = 0;
  4454. out->left = out->top = 0;
  4455. out->vis_hidden = out->opacity_hidden = 0;
  4456. out->overflow_hidden = 0;
  4457. tag = d->nodes[node].tag;
  4458. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  4459. BR_DISPLAY_INLINE;
  4460. for (i = 0; i < PROP_COUNT; i++) {
  4461. best[i] = -1;
  4462. best_imp[i] = -1;
  4463. }
  4464. switch (tag) {
  4465. case BR_TAG_HEAD:
  4466. case BR_TAG_TITLE:
  4467. case BR_TAG_META:
  4468. case BR_TAG_LINK:
  4469. case BR_TAG_BASE:
  4470. case BR_TAG_SCRIPT:
  4471. case BR_TAG_STYLE:
  4472. out->display = BR_DISPLAY_NONE;
  4473. break;
  4474. case BR_TAG_BODY:
  4475. out->mt = out->mb = out->ml = out->mr = 4;
  4476. break;
  4477. case BR_TAG_P:
  4478. out->mt = out->mb = 8;
  4479. break;
  4480. case BR_TAG_H1:
  4481. out->font_px = 32; out->bold = 1;
  4482. out->mt = 10; out->mb = 6;
  4483. break;
  4484. case BR_TAG_H2:
  4485. out->font_px = 24; out->bold = 1;
  4486. out->mt = 8; out->mb = 4;
  4487. break;
  4488. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  4489. out->bold = 1; out->mt = 6; out->mb = 3;
  4490. if (tag == BR_TAG_H3)
  4491. out->font_px = 19;
  4492. else if (tag == BR_TAG_H4)
  4493. out->font_px = 17;
  4494. else if (tag == BR_TAG_H5)
  4495. out->font_px = 15;
  4496. else
  4497. out->font_px = 13;
  4498. break;
  4499. case BR_TAG_UL: case BR_TAG_OL:
  4500. out->mt = out->mb = 6; out->pl = 20;
  4501. break;
  4502. case BR_TAG_LI:
  4503. out->mt = out->mb = 2;
  4504. break;
  4505. case BR_TAG_BLOCKQUOTE:
  4506. out->mt = out->mb = 6; out->ml = out->mr = 16;
  4507. break;
  4508. case BR_TAG_PRE:
  4509. out->mt = out->mb = 8; out->pl = 8;
  4510. out->has_bg = 1; out->bg = theme_panel;
  4511. out->mono = 1;
  4512. break;
  4513. case BR_TAG_CENTER:
  4514. out->align = BR_ALIGN_CENTER;
  4515. break;
  4516. case BR_TAG_A:
  4517. out->color = theme_link; out->underline = 1;
  4518. break;
  4519. case BR_TAG_B: case BR_TAG_STRONG:
  4520. out->bold = 1;
  4521. break;
  4522. case BR_TAG_I: case BR_TAG_EM:
  4523. out->italic = 1;
  4524. break;
  4525. case BR_TAG_CODE:
  4526. out->color = 0x8010;
  4527. out->mono = 1;
  4528. break;
  4529. case BR_TAG_SMALL:
  4530. out->font_px = 13;
  4531. break;
  4532. case BR_TAG_BIG:
  4533. out->font_px = 20;
  4534. break;
  4535. case BR_TAG_TD: case BR_TAG_TH:
  4536. out->pr = 12;
  4537. break;
  4538. case BR_TAG_VIDEO:
  4539. out->display = BR_DISPLAY_BLOCK;
  4540. break;
  4541. default:
  4542. break;
  4543. }
  4544. /* presentational <font color>: author CSS below still wins */
  4545. if (d->nodes[node].color_off) {
  4546. unsigned short c;
  4547. const char *v = attr_ptr(d, d->nodes[node].color_off);
  4548. if (parse_color(v, s_len(v), &c))
  4549. out->color = c;
  4550. }
  4551. rule_collect(d, node);
  4552. for (i = 0; i < nrule_matches; i++) {
  4553. struct br_rule *r = &d->rules[rule_matches[i].idx];
  4554. int spec = rule_matches[i].spec;
  4555. unsigned int k;
  4556. for (k = 0; k < r->nd; k++)
  4557. cascade_decl(out, &r->d[k], spec, best, best_imp);
  4558. }
  4559. if (d->nodes[node].style_off) {
  4560. struct br_decl decls[BR_DECLS];
  4561. unsigned int nd, k;
  4562. const char *v = attr_ptr(d, d->nodes[node].style_off);
  4563. parse_decls(d, v, s_len(v), decls, &nd);
  4564. for (k = 0; k < nd; k++)
  4565. cascade_decl(out, &decls[k], 1000, best, best_imp);
  4566. }
  4567. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  4568. out->display = BR_DISPLAY_NONE;
  4569. if (out->vis_hidden || out->opacity_hidden ||
  4570. (out->overflow_hidden && out->height_kind == 1 &&
  4571. out->height == 0))
  4572. out->display = BR_DISPLAY_NONE;
  4573. d->nodes[node].style = *out;
  4574. d->nodes[node].style_valid = 1;
  4575. }
  4576. /* ---- HTML parser ---- */
  4577. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  4578. unsigned int sp, int *pre)
  4579. {
  4580. unsigned int i;
  4581. *pre = 0;
  4582. for (i = 1; i <= sp; i++)
  4583. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  4584. (*pre)++;
  4585. }
  4586. static void implicit_close(struct br_doc *d, unsigned short *stack,
  4587. unsigned int *sp, unsigned int tag)
  4588. {
  4589. while (*sp > 0) {
  4590. unsigned int top = d->nodes[stack[*sp]].tag;
  4591. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  4592. (*sp)--;
  4593. continue;
  4594. }
  4595. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  4596. (*sp)--;
  4597. continue;
  4598. }
  4599. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  4600. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  4601. (*sp)--;
  4602. continue;
  4603. }
  4604. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  4605. (*sp)--;
  4606. continue;
  4607. }
  4608. break;
  4609. }
  4610. }
  4611. static int has_attr_token(const char *s, unsigned int len, const char *name)
  4612. {
  4613. unsigned int i, n = s_len(name);
  4614. for (i = 0; i + n <= len; i++) {
  4615. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  4616. continue;
  4617. if (!ci_eq_n(s + i, name, n))
  4618. continue;
  4619. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  4620. return 1;
  4621. }
  4622. return 0;
  4623. }
  4624. static int stack_has_svg(const struct br_doc *d,
  4625. const unsigned short *stack, unsigned int sp)
  4626. {
  4627. unsigned int k;
  4628. for (k = 0; k <= sp && k < 64; k++)
  4629. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  4630. return 1;
  4631. return 0;
  4632. }
  4633. /* Skip an element subtree by finding its closing tag. Used for SVG
  4634. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  4635. * overwrite the document title. */
  4636. static unsigned int skip_element(const char *html, unsigned int len,
  4637. unsigned int i, const char *name,
  4638. unsigned int n)
  4639. {
  4640. char close[40];
  4641. unsigned int k = 0, p;
  4642. close[k++] = '<';
  4643. close[k++] = '/';
  4644. while (k < sizeof(close) - 1 && n--)
  4645. close[k++] = *name++;
  4646. p = (unsigned int)find_ci(html, len, close, k, i);
  4647. if ((int)p < 0)
  4648. return len;
  4649. p += k;
  4650. while (p < len && html[p] != '>')
  4651. p++;
  4652. if (p < len)
  4653. p++;
  4654. return p;
  4655. }
  4656. /* Store a <script src=...> value in the attribute arena. Kept separate from
  4657. * parse_attrs() so script-only attributes cannot be confused with the
  4658. * value/d attributes that also use src_off on other elements. */
  4659. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  4660. unsigned int len)
  4661. {
  4662. unsigned int i = 0;
  4663. while (i < len) {
  4664. unsigned int ns, ne, vs, ve;
  4665. char quote = 0;
  4666. while (i < len && is_space(s[i]))
  4667. i++;
  4668. if (i >= len || s[i] == '/')
  4669. break;
  4670. ns = i;
  4671. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  4672. i++;
  4673. ne = i;
  4674. while (i < len && is_space(s[i]))
  4675. i++;
  4676. if (i >= len || s[i] != '=')
  4677. continue;
  4678. i++;
  4679. while (i < len && is_space(s[i]))
  4680. i++;
  4681. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  4682. quote = s[i++];
  4683. vs = i;
  4684. while (i < len && s[i] != quote)
  4685. i++;
  4686. ve = i;
  4687. if (i < len)
  4688. i++;
  4689. } else {
  4690. vs = i;
  4691. while (i < len && !is_space(s[i]) && s[i] != '>')
  4692. i++;
  4693. ve = i;
  4694. }
  4695. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  4696. return store_attr(d, s + vs, ve - vs, 400);
  4697. }
  4698. return 0;
  4699. }
  4700. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  4701. * application/json, text/template, ...) and non-JS type values. External
  4702. * src scripts are allowed; the browser fetches them separately. */
  4703. static int script_is_javascript(const char *s, unsigned int len)
  4704. {
  4705. unsigned int i = 0;
  4706. while (i < len) {
  4707. unsigned int ns, ne, vs = 0, ve = 0;
  4708. char quote = 0;
  4709. int has_val = 0;
  4710. while (i < len && is_space(s[i]))
  4711. i++;
  4712. if (i >= len)
  4713. break;
  4714. ns = i;
  4715. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  4716. i++;
  4717. ne = i;
  4718. while (i < len && is_space(s[i]))
  4719. i++;
  4720. if (i < len && s[i] == '=') {
  4721. i++;
  4722. while (i < len && is_space(s[i]))
  4723. i++;
  4724. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  4725. quote = s[i++];
  4726. vs = i;
  4727. while (i < len && s[i] != quote)
  4728. i++;
  4729. ve = i;
  4730. if (i < len)
  4731. i++;
  4732. } else {
  4733. vs = i;
  4734. while (i < len && !is_space(s[i]) && s[i] != '/')
  4735. i++;
  4736. ve = i;
  4737. }
  4738. has_val = 1;
  4739. } else if (i == ne) {
  4740. i++;
  4741. }
  4742. if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
  4743. unsigned int vn = has_val ? ve - vs : 0;
  4744. if (vn == 0)
  4745. return 1;
  4746. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  4747. (vn == 22 &&
  4748. ci_eq_n(s + vs, "application/javascript", 22)) ||
  4749. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  4750. (vn == 22 &&
  4751. ci_eq_n(s + vs, "application/ecmascript", 22)))
  4752. return 1;
  4753. return 0;
  4754. }
  4755. }
  4756. return 1;
  4757. }
  4758. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  4759. {
  4760. unsigned short stack[64];
  4761. unsigned int sp = 0;
  4762. unsigned int i = 0;
  4763. int pre = 0;
  4764. unsigned int css_len = 0;
  4765. if (!d)
  4766. return -1;
  4767. if (!d->nodes)
  4768. br_doc_init(d);
  4769. doc_reset(d);
  4770. d->script_src = html;
  4771. stack[0] = 0;
  4772. d->nodes[0].type = BR_NODE_ELEM;
  4773. d->nodes[0].tag = BR_TAG_UNKNOWN;
  4774. while (i < len) {
  4775. if (html[i] != '<') {
  4776. unsigned int st = i;
  4777. while (i < len && html[i] != '<')
  4778. i++;
  4779. if (sp <= 63)
  4780. append_text(d, stack[sp], html + st, i - st, pre);
  4781. continue;
  4782. }
  4783. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  4784. html[i + 3] == '-') {
  4785. int p = find_ci(html, len, "-->", 3, i + 4);
  4786. i = p >= 0 ? (unsigned int)p + 3 : len;
  4787. continue;
  4788. }
  4789. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  4790. while (i < len && html[i] != '>')
  4791. i++;
  4792. if (i < len)
  4793. i++;
  4794. continue;
  4795. }
  4796. if (i + 1 < len && html[i + 1] == '/') {
  4797. unsigned int ns = i + 2, ne, tag, k, n;
  4798. char name[32];
  4799. while (ns < len && is_space(html[ns]))
  4800. ns++;
  4801. ne = ns;
  4802. while (ne < len && is_ident(html[ne]))
  4803. ne++;
  4804. if (ne == ns) {
  4805. i++;
  4806. continue;
  4807. }
  4808. n = ne - ns;
  4809. if (n >= sizeof(name))
  4810. n = sizeof(name) - 1;
  4811. for (k = 0; k < n; k++)
  4812. name[k] = html[ns + k];
  4813. name[n] = '\0';
  4814. tag = br_tag_from_name(name);
  4815. i = ne;
  4816. while (i < len && html[i] != '>')
  4817. i++;
  4818. if (i < len)
  4819. i++;
  4820. for (k = sp; k > 0; k--) {
  4821. struct br_node *nd = &d->nodes[stack[k]];
  4822. if (tag == BR_TAG_UNKNOWN) {
  4823. const char *raw = attr_ptr(d, nd->name_off);
  4824. if (nd->tag == BR_TAG_UNKNOWN &&
  4825. ci_eq_n(raw, name, n) &&
  4826. raw[n] == '\0') {
  4827. sp = k - 1;
  4828. break;
  4829. }
  4830. } else if (nd->tag == tag) {
  4831. sp = k - 1;
  4832. break;
  4833. }
  4834. }
  4835. stack_recount_pre(d, stack, sp, &pre);
  4836. continue;
  4837. }
  4838. /* start tag */
  4839. {
  4840. unsigned int ns = i + 1, ne, tag, k, n;
  4841. char name[16];
  4842. unsigned int attr_start, attr_end;
  4843. int selfclose = 0;
  4844. while (ns < len && is_space(html[ns]))
  4845. ns++;
  4846. ne = ns;
  4847. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  4848. ne++;
  4849. if (ne == ns) {
  4850. /* stray '<' */
  4851. append_text(d, stack[sp], html + i, 1, pre);
  4852. i++;
  4853. continue;
  4854. }
  4855. n = ne - ns;
  4856. if (n >= sizeof(name))
  4857. n = sizeof(name) - 1;
  4858. for (k = 0; k < n; k++)
  4859. name[k] = html[ns + k];
  4860. name[n] = '\0';
  4861. tag = br_tag_from_name(name);
  4862. attr_start = ne;
  4863. {
  4864. unsigned int q = ne;
  4865. char quote = 0;
  4866. while (q < len) {
  4867. char c = html[q];
  4868. if (quote) {
  4869. if (c == quote)
  4870. quote = 0;
  4871. } else if (c == '"' || c == '\'') {
  4872. quote = c;
  4873. } else if (c == '>') {
  4874. break;
  4875. }
  4876. q++;
  4877. }
  4878. attr_end = q;
  4879. if (q > attr_start && html[q - 1] == '/')
  4880. selfclose = 1;
  4881. i = q < len ? q + 1 : len;
  4882. }
  4883. if (tag == BR_TAG_SCRIPT) {
  4884. int p = find_ci(html, len, "</script", 8, i);
  4885. unsigned int end = p >= 0 ? (unsigned int)p : len;
  4886. if (script_is_javascript(html + attr_start,
  4887. attr_end - attr_start)) {
  4888. if (d->nscripts < BR_MAX_SCRIPTS) {
  4889. struct br_script *script =
  4890. &d->scripts[d->nscripts++];
  4891. script->off = i;
  4892. script->len = end > i ? end - i : 0;
  4893. script->src_off = script_src_attr(
  4894. d, html + attr_start,
  4895. attr_end - attr_start);
  4896. } else {
  4897. d->scripts_truncated = 1;
  4898. }
  4899. }
  4900. i = p >= 0 ? (unsigned int)p + 8 : len;
  4901. while (i < len && html[i] != '>')
  4902. i++;
  4903. if (i < len)
  4904. i++;
  4905. continue;
  4906. }
  4907. if (stack_has_svg(d, stack, sp) &&
  4908. (tag == BR_TAG_TITLE ||
  4909. (tag == BR_TAG_UNKNOWN &&
  4910. (ci_eq_n(name, "desc", 4) ||
  4911. ci_eq_n(name, "metadata", 8))))) {
  4912. i = skip_element(html, len, i, name, n);
  4913. continue;
  4914. }
  4915. if (tag == BR_TAG_DETAILS && !selfclose &&
  4916. !has_attr_token(html + attr_start,
  4917. attr_end - attr_start, "open")) {
  4918. /* Closed <details> is collapsed (and this browser has
  4919. * no script to open it); skipping the subtree keeps
  4920. * mobile mega-menus out of the node arena. */
  4921. int p = find_ci(html, len, "</details>", 10, i);
  4922. i = p >= 0 ? (unsigned int)p + 10 : len;
  4923. continue;
  4924. }
  4925. if (tag == BR_TAG_STYLE) {
  4926. int p = find_ci(html, len, "</style", 7, i);
  4927. unsigned int end = p >= 0 ? (unsigned int)p : len;
  4928. unsigned int cend = end;
  4929. while (cend > i && is_space(html[cend - 1]))
  4930. cend--;
  4931. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  4932. for (k = i; k < cend; k++)
  4933. css_store[css_len++] = html[k];
  4934. css_store[css_len++] = '\n';
  4935. }
  4936. i = p >= 0 ? (unsigned int)p + 7 : len;
  4937. while (i < len && html[i] != '>')
  4938. i++;
  4939. if (i < len)
  4940. i++;
  4941. continue;
  4942. }
  4943. if (tag == BR_TAG_TITLE) {
  4944. int p = find_ci(html, len, "</title", 7, i);
  4945. unsigned int end = p >= 0 ? (unsigned int)p : len;
  4946. unsigned int tn = 0;
  4947. char tmp[BR_TITLE_MAX];
  4948. tn = decode_text(html + i, end - i, tmp,
  4949. sizeof(tmp), 1);
  4950. (void)tn;
  4951. s_copy(d->title, tmp, sizeof(d->title));
  4952. i = p >= 0 ? (unsigned int)p + 7 : len;
  4953. while (i < len && html[i] != '>')
  4954. i++;
  4955. if (i < len)
  4956. i++;
  4957. continue;
  4958. }
  4959. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  4960. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  4961. unsigned int idx;
  4962. implicit_close(d, stack, &sp, tag);
  4963. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  4964. (unsigned char)tag);
  4965. if (idx)
  4966. parse_attrs(d, &d->nodes[idx],
  4967. html + attr_start,
  4968. attr_end - attr_start);
  4969. continue;
  4970. }
  4971. if (sp >= 63) {
  4972. d->truncated = 1;
  4973. continue;
  4974. }
  4975. implicit_close(d, stack, &sp, tag);
  4976. {
  4977. unsigned int idx = node_append(d, stack[sp],
  4978. BR_NODE_ELEM,
  4979. (unsigned char)tag);
  4980. if (!idx)
  4981. continue;
  4982. parse_attrs(d, &d->nodes[idx],
  4983. html + attr_start,
  4984. attr_end - attr_start);
  4985. if (tag == BR_TAG_UNKNOWN)
  4986. d->nodes[idx].name_off =
  4987. store_attr(d, name, n, 31);
  4988. if (!selfclose) {
  4989. sp++;
  4990. stack[sp] = (unsigned short)idx;
  4991. if (tag == BR_TAG_PRE)
  4992. pre++;
  4993. }
  4994. }
  4995. }
  4996. }
  4997. d->inline_css_len = css_len;
  4998. return 0;
  4999. }
  5000. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  5001. {
  5002. if (!d->nodes)
  5003. br_doc_init(d);
  5004. doc_reset(d);
  5005. d->plain = 1;
  5006. d->nodes[0].type = BR_NODE_ELEM;
  5007. d->nodes[0].tag = BR_TAG_UNKNOWN;
  5008. append_text(d, 0, text, len, 1);
  5009. return 0;
  5010. }
  5011. /* A standalone image response: a synthetic document with one <img>. The
  5012. * caller decodes the already-fetched body into image slot 0 and sets the
  5013. * returned node's img_index. */
  5014. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  5015. {
  5016. unsigned int idx;
  5017. if (!d->nodes)
  5018. br_doc_init(d);
  5019. doc_reset(d);
  5020. d->nodes[0].type = BR_NODE_ELEM;
  5021. d->nodes[0].tag = BR_TAG_UNKNOWN;
  5022. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  5023. if (!idx)
  5024. return 0;
  5025. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  5026. return idx;
  5027. }