browser_html.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203
  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. for (i = 0; i < BR_MAX_NODES; i++) {
  683. d->nodes[i].type = BR_NODE_ELEM;
  684. d->nodes[i].tag = BR_TAG_UNKNOWN;
  685. d->nodes[i].flags = 0;
  686. d->nodes[i].parent = 0;
  687. d->nodes[i].first = d->nodes[i].last = 0;
  688. d->nodes[i].next = 0;
  689. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  690. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  691. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  692. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  693. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  694. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  695. d->nodes[i].name_off = 0;
  696. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  697. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  698. d->nodes[i].svg_rx = 0;
  699. d->nodes[i].img_index = -1;
  700. d->nodes[i].style_valid = 0;
  701. }
  702. }
  703. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  704. unsigned char type, unsigned char tag)
  705. {
  706. unsigned int idx;
  707. struct br_node *n;
  708. if (d->nnode >= BR_MAX_NODES) {
  709. d->truncated = 1;
  710. return 0;
  711. }
  712. idx = d->nnode++;
  713. n = &d->nodes[idx];
  714. n->type = type;
  715. n->tag = tag;
  716. n->parent = (unsigned short)parent;
  717. n->img_index = -1;
  718. if (d->nodes[parent].last)
  719. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  720. else
  721. d->nodes[parent].first = (unsigned short)idx;
  722. d->nodes[parent].last = (unsigned short)idx;
  723. return idx;
  724. }
  725. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  726. const char *s, unsigned int len, int pre)
  727. {
  728. char *dst;
  729. unsigned int n;
  730. unsigned int idx;
  731. if (len == 0)
  732. return 0;
  733. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  734. d->truncated = 1;
  735. return 0;
  736. }
  737. dst = d->text + d->text_used;
  738. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  739. if (n == 0) {
  740. d->text[d->text_used] = '\0';
  741. return 0;
  742. }
  743. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  744. if (!idx)
  745. return 0;
  746. d->nodes[idx].text_off = (unsigned short)d->text_used;
  747. d->nodes[idx].text_len = (unsigned short)n;
  748. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  749. d->text_used += n + 1;
  750. return idx;
  751. }
  752. /* ---- attribute parsing ---- */
  753. static int is_space(char c)
  754. {
  755. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  756. }
  757. static int str_eq(const char *a, const char *b);
  758. static unsigned int parse_uint(const char *s, unsigned int len)
  759. {
  760. unsigned int i = 0, v = 0;
  761. while (i < len && is_space(s[i]))
  762. i++;
  763. while (i < len && s[i] >= '0' && s[i] <= '9') {
  764. v = v * 10 + (unsigned int)(s[i] - '0');
  765. if (v > 4096)
  766. v = 4096;
  767. i++;
  768. }
  769. return v;
  770. }
  771. static int contains_ci_len(const char *s, unsigned int len,
  772. const char *needle);
  773. static int parse_svg_viewbox(const char *s, unsigned int len,
  774. unsigned short *w, unsigned short *h)
  775. {
  776. unsigned int i = 0, v[4], n = 0;
  777. while (i < len && n < 4) {
  778. unsigned int x = 0;
  779. int any = 0;
  780. while (i < len && (is_space(s[i]) || s[i] == ','))
  781. i++;
  782. while (i < len && s[i] >= '0' && s[i] <= '9') {
  783. x = x * 10 + (unsigned int)(s[i] - '0');
  784. i++;
  785. any = 1;
  786. }
  787. if (!any) {
  788. if (i < len)
  789. i++;
  790. continue;
  791. }
  792. v[n++] = x;
  793. }
  794. if (n < 4)
  795. return 0;
  796. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  797. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  798. return *w && *h;
  799. }
  800. static void note_attr_flag(struct br_node *n, const char *name)
  801. {
  802. if (str_eq(name, "data-collapse-target"))
  803. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  804. else if (str_eq(name, "data-collapse-trigger"))
  805. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  806. else if (str_eq(name, "is-open"))
  807. n->attr_flags |= BR_ATTR_IS_OPEN;
  808. else if (str_eq(name, "disabled"))
  809. n->attr_flags |= BR_ATTR_DISABLED;
  810. else if (str_eq(name, "open"))
  811. n->attr_flags |= BR_ATTR_OPEN;
  812. else if (str_eq(name, "inert"))
  813. n->attr_flags |= BR_ATTR_INERT;
  814. }
  815. /* Parse attributes from `s` (between the tag name and '>'). */
  816. static void parse_attrs(struct br_doc *d, struct br_node *n,
  817. const char *s, unsigned int len)
  818. {
  819. unsigned int i = 0;
  820. while (i < len) {
  821. char name[32];
  822. unsigned int nl = 0, vstart, vlen;
  823. char quote = 0;
  824. while (i < len && is_space(s[i]))
  825. i++;
  826. if (i >= len || s[i] == '/')
  827. break;
  828. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  829. char c = s[i++];
  830. if (c >= 'A' && c <= 'Z')
  831. c = (char)(c - 'A' + 'a');
  832. if (nl + 1 < sizeof(name))
  833. name[nl++] = c;
  834. }
  835. name[nl] = '\0';
  836. while (i < len && is_space(s[i]))
  837. i++;
  838. if (i >= len || s[i] != '=') {
  839. if (str_eq(name, "hidden"))
  840. n->flags |= BR_NODE_HIDDEN;
  841. note_attr_flag(n, name);
  842. continue;
  843. }
  844. i++;
  845. while (i < len && is_space(s[i]))
  846. i++;
  847. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  848. quote = s[i++];
  849. vstart = i;
  850. while (i < len && s[i] != quote)
  851. i++;
  852. vlen = i - vstart;
  853. if (i < len)
  854. i++;
  855. } else {
  856. vstart = i;
  857. while (i < len && !is_space(s[i]) && s[i] != '>')
  858. i++;
  859. vlen = i - vstart;
  860. }
  861. if (!nl)
  862. continue;
  863. if (str_eq(name, "hidden")) {
  864. n->flags |= BR_NODE_HIDDEN;
  865. continue;
  866. }
  867. note_attr_flag(n, name);
  868. if (str_eq(name, "href"))
  869. n->href_off = store_attr(d, s + vstart, vlen, 480);
  870. else if (str_eq(name, "class"))
  871. n->class_off = store_attr(d, s + vstart, vlen, 120);
  872. else if (str_eq(name, "id"))
  873. n->id_off = store_attr(d, s + vstart, vlen, 64);
  874. else if (str_eq(name, "style"))
  875. n->style_off = store_attr(d, s + vstart, vlen, 240);
  876. else if (str_eq(name, "src"))
  877. n->src_off = store_attr(d, s + vstart, vlen, 400);
  878. else if (str_eq(name, "alt"))
  879. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  880. else if (str_eq(name, "color"))
  881. n->color_off = store_attr(d, s + vstart, vlen, 32);
  882. else if (str_eq(name, "rel"))
  883. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  884. else if (str_eq(name, "value"))
  885. n->src_off = store_attr(d, s + vstart, vlen, 220);
  886. else if (str_eq(name, "placeholder") &&
  887. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  888. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  889. else if (str_eq(name, "type") &&
  890. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  891. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  892. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  893. else if (str_eq(name, "width")) {
  894. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  895. if (vlen && s[vstart + vlen - 1] == '%')
  896. n->width_pct = 1;
  897. } else if (str_eq(name, "height")) {
  898. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  899. if (vlen && s[vstart + vlen - 1] == '%')
  900. n->height_pct = 1;
  901. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  902. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  903. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  904. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  905. } else if (str_eq(name, "x2")) {
  906. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  907. } else if (str_eq(name, "y2")) {
  908. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  909. } else if (str_eq(name, "rx")) {
  910. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  911. } else if (str_eq(name, "d")) {
  912. n->src_off = store_attr(d, s + vstart, vlen, 300);
  913. } else if (str_eq(name, "viewbox")) {
  914. unsigned short vw = 0, vh = 0;
  915. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  916. n->width_attr = vw;
  917. n->height_attr = vh;
  918. n->flags |= BR_NODE_VIEWBOX;
  919. }
  920. } else if (str_eq(name, "text-anchor")) {
  921. if (contains_ci_len(s + vstart, vlen, "middle"))
  922. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  923. else if (contains_ci_len(s + vstart, vlen, "end"))
  924. n->attr_flags |= BR_ATTR_ANCHOR_END;
  925. }
  926. }
  927. }
  928. static int str_eq(const char *a, const char *b)
  929. {
  930. unsigned int i = 0;
  931. while (a[i] && b[i] && a[i] == b[i])
  932. i++;
  933. return a[i] == '\0' && b[i] == '\0';
  934. }
  935. /* ---- CSS ---- */
  936. enum {
  937. PROP_COLOR, PROP_BG, PROP_FONT_PX, PROP_BOLD, PROP_ITALIC, PROP_MONO,
  938. PROP_ALIGN, PROP_UNDERLINE,
  939. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  940. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  941. PROP_BORDER_W, PROP_BORDER_COLOR,
  942. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  943. PROP_LIST_NONE,
  944. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  945. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  946. PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  947. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  948. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  949. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  950. PROP_COUNT
  951. };
  952. struct color_name {
  953. const char *name;
  954. unsigned short rgb;
  955. };
  956. static const struct color_name color_names[] = {
  957. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  958. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  959. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  960. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  961. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  962. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  963. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  964. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  965. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  966. { "darkgrey", 0x528A }, { 0, 0 }
  967. };
  968. static int hexval(char c)
  969. {
  970. if (c >= '0' && c <= '9')
  971. return c - '0';
  972. if (c >= 'a' && c <= 'f')
  973. return c - 'a' + 10;
  974. if (c >= 'A' && c <= 'F')
  975. return c - 'A' + 10;
  976. return -1;
  977. }
  978. static int named_color(const char *s, unsigned int len, unsigned short *out)
  979. {
  980. unsigned int i;
  981. for (i = 0; color_names[i].name; i++) {
  982. unsigned int j, n = s_len(color_names[i].name);
  983. if (n != len)
  984. continue;
  985. for (j = 0; j < n; j++) {
  986. char c = s[j];
  987. if (c >= 'A' && c <= 'Z')
  988. c = (char)(c - 'A' + 'a');
  989. if (c != color_names[i].name[j])
  990. break;
  991. }
  992. if (j == n) {
  993. *out = color_names[i].rgb;
  994. return 1;
  995. }
  996. }
  997. return 0;
  998. }
  999. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  1000. * over white; the render target is a white page. */
  1001. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  1002. {
  1003. unsigned int i = 0;
  1004. while (i < len && is_space(s[i]))
  1005. i++;
  1006. if (i < len && s[i] == '#') {
  1007. int d[6], nd = 0;
  1008. i++;
  1009. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  1010. d[nd++] = hexval(s[i++]);
  1011. if (nd == 3) {
  1012. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  1013. unsigned int b = (unsigned int)d[2];
  1014. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  1015. ((g * 17 >> 2) << 5) |
  1016. (b * 17 >> 3));
  1017. return 1;
  1018. }
  1019. if (nd >= 6) {
  1020. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  1021. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  1022. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  1023. *out = (unsigned short)(((r >> 3) << 11) |
  1024. ((g >> 2) << 5) | (b >> 3));
  1025. return 1;
  1026. }
  1027. return 0;
  1028. }
  1029. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1030. unsigned int v[3], k = 0, a = 255;
  1031. while (i < len && s[i] != '(')
  1032. i++;
  1033. if (i >= len)
  1034. return 0;
  1035. i++;
  1036. while (k < 3 && i < len) {
  1037. unsigned int x = 0;
  1038. int percent = 0;
  1039. while (i < len && (is_space(s[i]) || s[i] == ','))
  1040. i++;
  1041. if (i >= len || s[i] < '0' || s[i] > '9')
  1042. return 0;
  1043. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1044. x = x * 10 + (unsigned int)(s[i] - '0');
  1045. i++;
  1046. }
  1047. if (i < len && s[i] == '%') {
  1048. percent = 1;
  1049. i++;
  1050. }
  1051. if (percent)
  1052. x = x * 255 / 100;
  1053. if (x > 255)
  1054. x = 255;
  1055. v[k++] = x;
  1056. }
  1057. /* Optional alpha in both syntaxes:
  1058. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  1059. {
  1060. unsigned int whole = 0, frac = 0, scale = 1;
  1061. int percent = 0;
  1062. while (i < len && (is_space(s[i]) || s[i] == ','))
  1063. i++;
  1064. if (i < len && s[i] == '/') {
  1065. i++;
  1066. while (i < len && is_space(s[i]))
  1067. i++;
  1068. }
  1069. if (i < len && s[i] >= '0' && s[i] <= '9') {
  1070. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1071. whole = whole * 10 +
  1072. (unsigned int)(s[i] - '0');
  1073. i++;
  1074. }
  1075. if (i < len && s[i] == '.') {
  1076. i++;
  1077. while (i < len && s[i] >= '0' &&
  1078. s[i] <= '9') {
  1079. if (scale < 1000) {
  1080. frac = frac * 10 +
  1081. (unsigned int)(s[i] - '0');
  1082. scale *= 10;
  1083. }
  1084. i++;
  1085. }
  1086. }
  1087. while (i < len && is_space(s[i]))
  1088. i++;
  1089. if (i < len && s[i] == '%')
  1090. percent = 1;
  1091. if (percent)
  1092. a = whole > 100 ? 255 :
  1093. whole * 255 / 100;
  1094. else if (whole >= 1)
  1095. a = 255;
  1096. else
  1097. a = frac * 255 / scale;
  1098. }
  1099. }
  1100. if (k == 3) {
  1101. unsigned int r = v[0], g = v[1], b = v[2];
  1102. if (a < 255) {
  1103. r = (r * a + 255u * (255u - a)) / 255u;
  1104. g = (g * a + 255u * (255u - a)) / 255u;
  1105. b = (b * a + 255u * (255u - a)) / 255u;
  1106. }
  1107. *out = (unsigned short)(((r >> 3) << 11) |
  1108. ((g >> 2) << 5) | (b >> 3));
  1109. return 1;
  1110. }
  1111. return 0;
  1112. }
  1113. {
  1114. unsigned int j = i;
  1115. while (i < len && !is_space(s[i]))
  1116. i++;
  1117. return named_color(s + j, i - j, out);
  1118. }
  1119. }
  1120. /* Length values for margins/padding: px, ignore percentages/units. */
  1121. static unsigned int viewport_w;
  1122. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  1123. {
  1124. unsigned int i = *pos;
  1125. int sign = 1;
  1126. long v = 0; /* hundredths of a unit before conversion */
  1127. int digits = 0, frac = 0, unit = 0;
  1128. while (i < len && is_space(s[i]))
  1129. i++;
  1130. if (i < len && (s[i] == '-' || s[i] == '+')) {
  1131. if (s[i] == '-')
  1132. sign = -1;
  1133. i++;
  1134. }
  1135. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1136. v = v * 10 + (s[i] - '0');
  1137. digits = 1;
  1138. i++;
  1139. }
  1140. if (i < len && s[i] == '.') {
  1141. i++;
  1142. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1143. if (frac < 2) {
  1144. v = v * 10 + (s[i] - '0');
  1145. frac++;
  1146. }
  1147. i++;
  1148. }
  1149. }
  1150. while (frac < 2) {
  1151. v *= 10;
  1152. frac++;
  1153. }
  1154. while (frac > 2) {
  1155. v /= 10;
  1156. frac--;
  1157. }
  1158. if (i < len && s[i] == '%')
  1159. unit = 2;
  1160. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1161. s[i + 2] == 'm')
  1162. unit = 1;
  1163. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1164. unit = 3;
  1165. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  1166. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  1167. s[i] == 'a' || s[i] == 'd'))
  1168. i++;
  1169. *pos = i;
  1170. if (!digits)
  1171. return 0;
  1172. if (unit == 1)
  1173. v *= root_font_px;
  1174. else if (unit == 3)
  1175. v *= 16;
  1176. else if (unit == 2)
  1177. v = v * (long)viewport_w / 100;
  1178. v /= 100;
  1179. v *= sign;
  1180. if (v < -2048)
  1181. v = -2048;
  1182. if (v > 2048)
  1183. v = 2048;
  1184. return (int)v;
  1185. }
  1186. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  1187. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  1188. unsigned char *kind)
  1189. {
  1190. unsigned int i = 0;
  1191. unsigned long v = 0;
  1192. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  1193. *val = 0;
  1194. *kind = 0;
  1195. while (i < len && is_space(s[i]))
  1196. i++;
  1197. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  1198. return 0;
  1199. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  1200. return 0;
  1201. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1202. v = v * 10 + (unsigned int)(s[i] - '0');
  1203. digits = 1;
  1204. i++;
  1205. }
  1206. if (i < len && s[i] == '.') {
  1207. i++;
  1208. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1209. if (frac < 2) {
  1210. v = v * 10 + (unsigned int)(s[i] - '0');
  1211. frac++;
  1212. }
  1213. i++;
  1214. }
  1215. }
  1216. while (frac < 2) {
  1217. v *= 10;
  1218. frac++;
  1219. }
  1220. while (frac > 2) {
  1221. v /= 10;
  1222. frac--;
  1223. }
  1224. if (!digits)
  1225. return 0;
  1226. while (i < len && is_space(s[i]))
  1227. i++;
  1228. if (i < len && s[i] == '%')
  1229. pct = 1;
  1230. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1231. s[i + 2] == 'm')
  1232. rem = 1;
  1233. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1234. rem = 2;
  1235. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  1236. vw = 1;
  1237. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  1238. ; /* px: already the default scale */
  1239. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  1240. bad_unit = 1;
  1241. if (bad_unit)
  1242. return 0;
  1243. if (rem == 1)
  1244. v *= root_font_px;
  1245. else if (rem == 2)
  1246. v *= 16;
  1247. v /= 100;
  1248. if (vw)
  1249. v = v * viewport_w / 100;
  1250. if (pct) {
  1251. if (v > 100)
  1252. v = 100;
  1253. *kind = 2;
  1254. } else {
  1255. if (v > 2048)
  1256. v = 2048;
  1257. *kind = 1;
  1258. }
  1259. *val = (unsigned short)v;
  1260. return 1;
  1261. }
  1262. static int parse_font_px(const char *s, unsigned int len)
  1263. {
  1264. unsigned int i = 0;
  1265. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1266. int v1000, px;
  1267. int is_px = 1, is_rem = 0, is_pct = 0;
  1268. while (i < len && is_space(s[i]))
  1269. i++;
  1270. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  1271. return 24;
  1272. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  1273. return 32;
  1274. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  1275. return 14;
  1276. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1277. whole = whole * 10 + (s[i] - '0');
  1278. digits = 1;
  1279. i++;
  1280. }
  1281. if (i < len && s[i] == '.') {
  1282. i++;
  1283. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1284. if (fscale < 1000) {
  1285. frac = frac * 10 + (s[i] - '0');
  1286. fscale *= 10;
  1287. }
  1288. i++;
  1289. }
  1290. }
  1291. if (!digits)
  1292. return 0;
  1293. while (i < len && is_space(s[i]))
  1294. i++;
  1295. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1296. is_rem = 1;
  1297. is_px = 0;
  1298. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1299. is_rem = 1;
  1300. is_px = 0;
  1301. } else if (i < len && s[i] == '%') {
  1302. is_pct = 1;
  1303. is_px = 0;
  1304. }
  1305. v1000 = whole * 1000 + frac * 1000 / fscale;
  1306. if (is_rem)
  1307. px = v1000 * root_font_px / 1000;
  1308. else if (is_pct)
  1309. px = v1000 * root_font_px / 100000;
  1310. else if (is_px)
  1311. px = v1000 / 1000;
  1312. else
  1313. px = v1000 / 1000;
  1314. if (px < 8)
  1315. px = 8;
  1316. if (px > 72)
  1317. px = 72;
  1318. return px;
  1319. }
  1320. static int parse_line_height(const char *s, unsigned int len,
  1321. unsigned short *px, unsigned char *pct)
  1322. {
  1323. unsigned int i = 0;
  1324. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1325. int v1000;
  1326. int is_px = 0, is_pct = 0;
  1327. *px = 0;
  1328. *pct = 0;
  1329. while (i < len && is_space(s[i]))
  1330. i++;
  1331. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  1332. return 0;
  1333. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1334. whole = whole * 10 + (s[i] - '0');
  1335. digits = 1;
  1336. i++;
  1337. }
  1338. if (i < len && s[i] == '.') {
  1339. i++;
  1340. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  1341. frac = frac * 10 + (s[i] - '0');
  1342. fscale *= 10;
  1343. i++;
  1344. }
  1345. }
  1346. if (!digits)
  1347. return 0;
  1348. while (i < len && is_space(s[i]))
  1349. i++;
  1350. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1351. is_px = 1;
  1352. else if (i < len && s[i] == '%')
  1353. is_pct = 1;
  1354. v1000 = whole * 1000 + frac * 1000 / fscale;
  1355. if (is_px) {
  1356. int v = v1000 / 1000;
  1357. if (v < 8)
  1358. v = 8;
  1359. if (v > 160)
  1360. v = 160;
  1361. *px = (unsigned short)v;
  1362. } else {
  1363. int v = v1000 / 10; /* unitless multiplier -> percent */
  1364. if (is_pct)
  1365. v = v1000 / 1000;
  1366. if (v < 50)
  1367. v = 50;
  1368. if (v > 400)
  1369. v = 400;
  1370. *pct = (unsigned char)v;
  1371. }
  1372. return 1;
  1373. }
  1374. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  1375. {
  1376. unsigned int i, j, n = s_len(needle);
  1377. for (i = 0; i + n <= len; i++) {
  1378. for (j = 0; j < n; j++) {
  1379. char a = s[i + j];
  1380. if (a >= 'A' && a <= 'Z')
  1381. a = (char)(a - 'A' + 'a');
  1382. if (a != needle[j])
  1383. break;
  1384. }
  1385. if (j == n)
  1386. return 1;
  1387. }
  1388. return 0;
  1389. }
  1390. /* Pull a colour out of a border/background shorthand. */
  1391. static int find_color(const char *s, unsigned int len, unsigned short *c)
  1392. {
  1393. unsigned int i;
  1394. for (i = 0; i < len; i++) {
  1395. if (s[i] == '#')
  1396. return parse_color(s + i, len - i, c);
  1397. if ((s[i] == 'r' || s[i] == 'R') &&
  1398. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  1399. return parse_color(s + i, len - i, c);
  1400. }
  1401. for (i = 0; i < len; ) {
  1402. unsigned int st, e;
  1403. while (i < len && (is_space(s[i]) || s[i] == ','))
  1404. i++;
  1405. st = i;
  1406. while (i < len && !is_space(s[i]) && s[i] != ',')
  1407. i++;
  1408. e = i;
  1409. if (e > st && parse_color(s + st, e - st, c))
  1410. return 1;
  1411. }
  1412. return 0;
  1413. }
  1414. /* Background shorthands can hold several layers before the final solid
  1415. * colour. The colour is conventionally the last token (e.g. a stack of
  1416. * gradients followed by var(--bg)), so take the rightmost colour rather
  1417. * than the first gradient stop. */
  1418. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  1419. {
  1420. int last = -1;
  1421. unsigned int i;
  1422. for (i = 0; i < len; i++) {
  1423. if (s[i] == '#')
  1424. last = (int)i;
  1425. else if ((s[i] == 'r' || s[i] == 'R') &&
  1426. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  1427. last = (int)i;
  1428. }
  1429. if (last >= 0)
  1430. return parse_color(s + last, len - (unsigned int)last, c);
  1431. while (i > 0) {
  1432. unsigned int st, e;
  1433. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  1434. s[i - 1] == ')' || s[i - 1] == '('))
  1435. i--;
  1436. e = i;
  1437. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  1438. s[i - 1] != ')' && s[i - 1] != '(')
  1439. i--;
  1440. st = i;
  1441. if (e > st && parse_color(s + st, e - st, c))
  1442. return 1;
  1443. }
  1444. return 0;
  1445. }
  1446. static int parse_display(const char *s, unsigned int len)
  1447. {
  1448. unsigned int i;
  1449. while (len && is_space(s[0])) { s++; len--; }
  1450. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  1451. ;
  1452. if (i == 4 && ci_eq_n(s, "none", 4))
  1453. return BR_DISPLAY_NONE;
  1454. if (i == 5 && ci_eq_n(s, "block", 5))
  1455. return BR_DISPLAY_BLOCK;
  1456. if (i == 6 && ci_eq_n(s, "inline", 6))
  1457. return BR_DISPLAY_INLINE;
  1458. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  1459. return BR_DISPLAY_INLINE_BLOCK;
  1460. if (i == 10 && ci_eq_n(s, "list-item", 10))
  1461. return BR_DISPLAY_BLOCK;
  1462. if (i == 4 && ci_eq_n(s, "flex", 4))
  1463. return BR_DISPLAY_FLEX;
  1464. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  1465. return BR_DISPLAY_FLEX;
  1466. if (i == 5 && ci_eq_n(s, "table", 5))
  1467. return BR_DISPLAY_BLOCK;
  1468. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  1469. return BR_DISPLAY_BLOCK;
  1470. /* Grid shells are common on news/wiki pages, but the two-column
  1471. * heuristic is not reliable for their nested area templates yet.
  1472. * Collapse them to block flow so the content stays readable. */
  1473. if (i == 4 && ci_eq_n(s, "grid", 4))
  1474. return BR_DISPLAY_BLOCK;
  1475. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  1476. return BR_DISPLAY_BLOCK;
  1477. return BR_DISPLAY_INLINE;
  1478. }
  1479. static void decl_add(struct br_decl *out, unsigned int *nout,
  1480. unsigned char prop, int val, int important)
  1481. {
  1482. if (*nout >= BR_DECLS)
  1483. return;
  1484. out[*nout].prop = prop;
  1485. out[*nout].val = (short)val;
  1486. out[*nout].important = (unsigned char)(important != 0);
  1487. (*nout)++;
  1488. }
  1489. static int parse_border_width(const char *s, unsigned int len)
  1490. {
  1491. unsigned int i = 0;
  1492. while (i < len) {
  1493. while (i < len && is_space(s[i]))
  1494. i++;
  1495. if (i >= len)
  1496. break;
  1497. if (s[i] == '#') {
  1498. while (i < len && !is_space(s[i]))
  1499. i++;
  1500. continue;
  1501. }
  1502. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1503. while (i < len && s[i] != ')')
  1504. i++;
  1505. if (i < len)
  1506. i++;
  1507. continue;
  1508. }
  1509. if (s[i] >= '0' && s[i] <= '9') {
  1510. int v = 0;
  1511. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1512. v = v * 10 + (s[i] - '0');
  1513. i++;
  1514. }
  1515. if (v < 0)
  1516. v = 0;
  1517. if (v > 3)
  1518. v = 3;
  1519. return v;
  1520. }
  1521. while (i < len && !is_space(s[i]))
  1522. i++;
  1523. }
  1524. return -1;
  1525. }
  1526. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  1527. * the input is copied verbatim when it contains no var(). */
  1528. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  1529. unsigned int len, char *out,
  1530. unsigned int cap, int depth)
  1531. {
  1532. unsigned int i = 0, n = 0;
  1533. if (depth > 8)
  1534. return 0;
  1535. while (i < len && n + 1 < cap) {
  1536. unsigned int p, inner, close, comma, nlen, fstart;
  1537. char quote;
  1538. int parens;
  1539. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  1540. out[n++] = s[i++];
  1541. continue;
  1542. }
  1543. p = i + 4;
  1544. quote = 0;
  1545. parens = 1;
  1546. while (p < len && parens) {
  1547. char c = s[p];
  1548. if (quote) {
  1549. if (c == quote)
  1550. quote = 0;
  1551. else if (c == '\\' && p + 1 < len)
  1552. p++;
  1553. } else if (c == '"' || c == '\'') {
  1554. quote = c;
  1555. } else if (c == '(') {
  1556. parens++;
  1557. } else if (c == ')') {
  1558. parens--;
  1559. }
  1560. p++;
  1561. }
  1562. if (parens) {
  1563. out[n++] = s[i++];
  1564. continue;
  1565. }
  1566. close = p - 1;
  1567. inner = i + 4;
  1568. while (inner < close && is_space(s[inner]))
  1569. inner++;
  1570. comma = inner;
  1571. parens = 0;
  1572. quote = 0;
  1573. while (comma < close) {
  1574. char c = s[comma];
  1575. if (quote) {
  1576. if (c == quote)
  1577. quote = 0;
  1578. } else if (c == '"' || c == '\'') {
  1579. quote = c;
  1580. } else if (c == '(') {
  1581. parens++;
  1582. } else if (c == ')' && parens > 0) {
  1583. parens--;
  1584. } else if (c == ',' && parens == 0) {
  1585. break;
  1586. }
  1587. comma++;
  1588. }
  1589. nlen = comma - inner;
  1590. while (nlen && is_space(s[inner + nlen - 1]))
  1591. nlen--;
  1592. fstart = comma < close ? comma + 1 : close;
  1593. while (fstart < close && is_space(s[fstart]))
  1594. fstart++;
  1595. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  1596. unsigned int vi = css_var_find(d, s + inner, nlen);
  1597. if (vi < BR_MAX_VARS) {
  1598. const char *v = attr_ptr(d, d->var_val[vi]);
  1599. unsigned int vn = css_expand_vars(d, v, s_len(v),
  1600. out + n,
  1601. cap - n, depth + 1);
  1602. n += vn;
  1603. } else if (fstart < close) {
  1604. unsigned int vn = css_expand_vars(d, s + fstart,
  1605. close - fstart,
  1606. out + n, cap - n,
  1607. depth + 1);
  1608. n += vn;
  1609. }
  1610. } else if (fstart < close) {
  1611. unsigned int vn = css_expand_vars(d, s + fstart,
  1612. close - fstart,
  1613. out + n, cap - n,
  1614. depth + 1);
  1615. n += vn;
  1616. }
  1617. i = p;
  1618. }
  1619. out[n] = '\0';
  1620. return n;
  1621. }
  1622. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  1623. unsigned int i)
  1624. {
  1625. while (i < len && (is_space(s[i]) || s[i] == ','))
  1626. i++;
  1627. return i;
  1628. }
  1629. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  1630. * argument). Returns 1 on success. */
  1631. static int parse_grid_track(const char *s, unsigned int len,
  1632. unsigned int *pp, unsigned short *val,
  1633. unsigned char *kind)
  1634. {
  1635. unsigned int i = grid_skip_sep(s, len, *pp);
  1636. unsigned int whole = 0, frac = 0, scale = 1;
  1637. int any = 0;
  1638. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  1639. unsigned int j = i + 7;
  1640. while (j < len && s[j] != ',')
  1641. j++;
  1642. if (j >= len)
  1643. return 0;
  1644. i = j + 1;
  1645. while (i < len && is_space(s[i]))
  1646. i++;
  1647. }
  1648. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  1649. int depth = 1;
  1650. i += 7;
  1651. while (i < len && depth) {
  1652. if (s[i] == '(')
  1653. depth++;
  1654. else if (s[i] == ')')
  1655. depth--;
  1656. i++;
  1657. }
  1658. *pp = i;
  1659. return 0;
  1660. }
  1661. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1662. whole = whole * 10 + (unsigned int)(s[i] - '0');
  1663. i++;
  1664. any = 1;
  1665. }
  1666. if (i < len && s[i] == '.') {
  1667. i++;
  1668. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1669. if (scale < 1000) {
  1670. frac = frac * 10 + (unsigned int)(s[i] - '0');
  1671. scale *= 10;
  1672. }
  1673. i++;
  1674. any = 1;
  1675. }
  1676. }
  1677. if (!any) {
  1678. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  1679. *val = 1;
  1680. *kind = 3;
  1681. *pp = i + 4;
  1682. return 1;
  1683. }
  1684. return 0;
  1685. }
  1686. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  1687. *val = (unsigned short)(whole ? whole : 1);
  1688. *kind = 3;
  1689. i += 2;
  1690. } else if (i < len && s[i] == '%') {
  1691. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1692. *kind = 2;
  1693. i++;
  1694. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1695. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1696. *kind = 1;
  1697. i += 3;
  1698. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1699. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1700. *kind = 1;
  1701. i += 2;
  1702. } else {
  1703. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1704. *kind = 1;
  1705. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1706. i += 2;
  1707. }
  1708. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  1709. i++;
  1710. *pp = i;
  1711. return 1;
  1712. }
  1713. static int parse_grid_cols(const char *s, unsigned int len,
  1714. unsigned short val[2], unsigned char kind[2])
  1715. {
  1716. unsigned int pos = 0;
  1717. int n = 0;
  1718. while (n < 2) {
  1719. unsigned int before = pos;
  1720. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  1721. break;
  1722. n++;
  1723. if (pos <= before)
  1724. break;
  1725. while (pos < len && is_space(s[pos]))
  1726. pos++;
  1727. if (pos >= len)
  1728. break;
  1729. }
  1730. return n;
  1731. }
  1732. static int parse_grid_slot(const char *s, unsigned int len)
  1733. {
  1734. if (contains_ci_len(s, len, "footer") ||
  1735. contains_ci_len(s, len, "header") ||
  1736. contains_ci_len(s, len, "sitenotice") ||
  1737. contains_ci_len(s, len, "navbar"))
  1738. return 1;
  1739. if (contains_ci_len(s, len, "columnstart") ||
  1740. contains_ci_len(s, len, "sidebar") ||
  1741. contains_ci_len(s, len, "left") ||
  1742. contains_ci_len(s, len, "start"))
  1743. return 2;
  1744. if (contains_ci_len(s, len, "pagecontent") ||
  1745. contains_ci_len(s, len, "content") ||
  1746. contains_ci_len(s, len, "main") ||
  1747. contains_ci_len(s, len, "right") ||
  1748. contains_ci_len(s, len, "end"))
  1749. return 3;
  1750. return 0;
  1751. }
  1752. static int css_has_var(const char *s, unsigned int len)
  1753. {
  1754. unsigned int i;
  1755. for (i = 0; i + 4 <= len; i++)
  1756. if ((s[i] == 'v' || s[i] == 'V') &&
  1757. ci_eq_n(s + i, "var(", 4))
  1758. return 1;
  1759. return 0;
  1760. }
  1761. static unsigned int css_skip_comment(const char *s, unsigned int len,
  1762. unsigned int i)
  1763. {
  1764. i += 2;
  1765. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  1766. i++;
  1767. if (i + 1 < len)
  1768. i += 2;
  1769. else
  1770. i = len;
  1771. return i;
  1772. }
  1773. static unsigned int css_skip_string(const char *s, unsigned int len,
  1774. unsigned int i)
  1775. {
  1776. char q = s[i++];
  1777. while (i < len && s[i] != q) {
  1778. if (s[i] == '\\' && i + 1 < len)
  1779. i++;
  1780. i++;
  1781. }
  1782. if (i < len)
  1783. i++;
  1784. return i;
  1785. }
  1786. /* Find the '}' that closes a declaration block. i points just past the
  1787. * opening '{'. Comments and quoted strings are skipped: a brace inside
  1788. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  1789. * selectors such as "Highlight-Stories {" without a closing brace). */
  1790. static unsigned int css_block_end(const char *s, unsigned int len,
  1791. unsigned int i)
  1792. {
  1793. int depth = 0;
  1794. while (i < len) {
  1795. char c = s[i];
  1796. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  1797. i = css_skip_comment(s, len, i);
  1798. continue;
  1799. }
  1800. if (c == '"' || c == '\'') {
  1801. i = css_skip_string(s, len, i);
  1802. continue;
  1803. }
  1804. if (c == '{') {
  1805. depth++;
  1806. } else if (c == '}') {
  1807. if (depth == 0)
  1808. return i;
  1809. depth--;
  1810. }
  1811. i++;
  1812. }
  1813. return len;
  1814. }
  1815. /* Skip a block whose opening '{' has already been consumed. */
  1816. static unsigned int css_skip_nested(const char *s, unsigned int len,
  1817. unsigned int i)
  1818. {
  1819. int depth = 1;
  1820. while (i < len && depth) {
  1821. char c = s[i];
  1822. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  1823. i = css_skip_comment(s, len, i);
  1824. continue;
  1825. }
  1826. if (c == '"' || c == '\'') {
  1827. i = css_skip_string(s, len, i);
  1828. continue;
  1829. }
  1830. if (c == '{')
  1831. depth++;
  1832. else if (c == '}')
  1833. depth--;
  1834. i++;
  1835. }
  1836. return i;
  1837. }
  1838. /* One border-radius length. Unlike parse_size(), percentages keep their
  1839. * kind: the drawer scales them to the box, not the viewport. */
  1840. static int parse_radius_value(const char *s, unsigned int len,
  1841. unsigned int *pos, unsigned short *val,
  1842. unsigned char *kind)
  1843. {
  1844. unsigned int i = *pos;
  1845. unsigned long v = 0;
  1846. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  1847. while (i < len && is_space(s[i]))
  1848. i++;
  1849. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1850. v = v * 10 + (unsigned int)(s[i] - '0');
  1851. digits = 1;
  1852. i++;
  1853. }
  1854. if (i < len && s[i] == '.') {
  1855. i++;
  1856. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1857. fdigits = 1;
  1858. if (frac < 2) {
  1859. v = v * 10 + (unsigned int)(s[i] - '0');
  1860. frac++;
  1861. }
  1862. i++;
  1863. }
  1864. }
  1865. while (frac < 2) {
  1866. v *= 10;
  1867. frac++;
  1868. }
  1869. while (i < len && is_space(s[i]))
  1870. i++;
  1871. if (i < len && s[i] == '%') {
  1872. pct = 1;
  1873. i++;
  1874. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1875. s[i + 2] == 'm') {
  1876. rem = 1;
  1877. i += 3;
  1878. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  1879. rem = 2;
  1880. i += 2;
  1881. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  1882. i += 2;
  1883. } else if (i < len && is_ident(s[i])) {
  1884. return 0;
  1885. }
  1886. if (!digits && !fdigits)
  1887. return 0;
  1888. if (rem == 1)
  1889. v *= root_font_px;
  1890. else if (rem == 2)
  1891. v *= 16;
  1892. v /= 100;
  1893. if (v > 2048)
  1894. v = 2048;
  1895. *val = (unsigned short)v;
  1896. *kind = pct ? 2 : 1;
  1897. *pos = i;
  1898. return 1;
  1899. }
  1900. static void parse_decls(struct br_doc *d, const char *s,
  1901. unsigned int len, struct br_decl *out,
  1902. unsigned int *nout)
  1903. {
  1904. static char expanded[4096];
  1905. if (css_has_var(s, len)) {
  1906. unsigned int elen = css_expand_vars(d, s, len, expanded,
  1907. sizeof(expanded), 0);
  1908. if (elen) {
  1909. s = expanded;
  1910. len = elen;
  1911. }
  1912. }
  1913. unsigned int i = 0;
  1914. *nout = 0;
  1915. while (i < len && *nout < BR_DECLS) {
  1916. unsigned int pstart, pend, vstart, vend, k;
  1917. char prop[24];
  1918. int important = 0;
  1919. while (i < len && (is_space(s[i]) || s[i] == ';'))
  1920. i++;
  1921. if (i >= len)
  1922. break;
  1923. /* A nested at-rule (@media/@supports) inside the declaration
  1924. * block carries its own selector context; skip it rather than
  1925. * reading its prelude as a declaration. */
  1926. if (s[i] == '@') {
  1927. while (i < len && s[i] != '{' && s[i] != ';')
  1928. i++;
  1929. if (i < len && s[i] == '{')
  1930. i = css_skip_nested(s, len, i + 1);
  1931. else if (i < len)
  1932. i++;
  1933. continue;
  1934. }
  1935. pstart = i;
  1936. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  1937. i++;
  1938. if (i < len && s[i] == '{') {
  1939. /* Nested style rule: skip its block. */
  1940. i = css_skip_nested(s, len, i + 1);
  1941. continue;
  1942. }
  1943. pend = i;
  1944. if (i >= len || s[i] != ':')
  1945. continue;
  1946. i++;
  1947. vstart = i;
  1948. while (i < len && s[i] != ';')
  1949. i++;
  1950. vend = i;
  1951. while (pstart < pend && is_space(s[pstart]))
  1952. pstart++;
  1953. while (pend > pstart && is_space(s[pend - 1]))
  1954. pend--;
  1955. while (vstart < vend && is_space(s[vstart]))
  1956. vstart++;
  1957. while (vend > vstart && is_space(s[vend - 1]))
  1958. vend--;
  1959. if (vend - vstart >= 10 &&
  1960. ci_eq_n(s + vend - 10, "!important", 10)) {
  1961. important = 1;
  1962. vend -= 10;
  1963. while (vend > vstart && is_space(s[vend - 1]))
  1964. vend--;
  1965. }
  1966. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  1967. char c = s[pstart + k];
  1968. if (c >= 'A' && c <= 'Z')
  1969. c = (char)(c - 'A' + 'a');
  1970. prop[k] = c;
  1971. }
  1972. prop[k] = '\0';
  1973. if (str_eq(prop, "color")) {
  1974. unsigned short c;
  1975. if (parse_color(s + vstart, vend - vstart, &c))
  1976. decl_add(out, nout, PROP_COLOR, c, important);
  1977. } else if (str_eq(prop, "fill")) {
  1978. unsigned short c;
  1979. if (parse_color(s + vstart, vend - vstart, &c)) {
  1980. decl_add(out, nout, PROP_BG, c, important);
  1981. decl_add(out, nout, PROP_COLOR, c, important);
  1982. }
  1983. } else if (str_eq(prop, "stroke")) {
  1984. unsigned short c;
  1985. if (parse_color(s + vstart, vend - vstart, &c)) {
  1986. decl_add(out, nout, PROP_BORDER_COLOR, c,
  1987. important);
  1988. decl_add(out, nout, PROP_BORDER_W, 1, important);
  1989. }
  1990. } else if (str_eq(prop, "stroke-width")) {
  1991. unsigned int j = vstart;
  1992. int sw = 0;
  1993. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  1994. sw = sw * 10 + (s[j] - '0');
  1995. j++;
  1996. }
  1997. if (sw < 1)
  1998. sw = 1;
  1999. if (sw > 3)
  2000. sw = 3;
  2001. decl_add(out, nout, PROP_BORDER_W, sw, important);
  2002. } else if (str_eq(prop, "text-anchor")) {
  2003. int a = BR_ALIGN_LEFT;
  2004. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  2005. a = BR_ALIGN_CENTER;
  2006. else if (contains_ci_len(s + vstart, vend - vstart,
  2007. "end"))
  2008. a = BR_ALIGN_RIGHT;
  2009. decl_add(out, nout, PROP_ALIGN, a, important);
  2010. } else if (str_eq(prop, "background-color") ||
  2011. str_eq(prop, "background")) {
  2012. unsigned short c;
  2013. if (parse_color(s + vstart, vend - vstart, &c))
  2014. decl_add(out, nout, PROP_BG, c, important);
  2015. else if (str_eq(prop, "background") &&
  2016. find_color_last(s + vstart, vend - vstart, &c))
  2017. decl_add(out, nout, PROP_BG, c, important);
  2018. } else if (str_eq(prop, "font-size")) {
  2019. int px = parse_font_px(s + vstart, vend - vstart);
  2020. if (px > 0)
  2021. decl_add(out, nout, PROP_FONT_PX, px,
  2022. important);
  2023. } else if (str_eq(prop, "font-weight")) {
  2024. int bold = 0;
  2025. unsigned int j;
  2026. if (vend - vstart >= 4 &&
  2027. ci_eq_n(s + vstart, "bold", 4))
  2028. bold = 1;
  2029. else {
  2030. int w = 0;
  2031. for (j = vstart; j < vend; j++)
  2032. if (s[j] >= '0' && s[j] <= '9')
  2033. w = w * 10 + (s[j] - '0');
  2034. if (w >= 500)
  2035. bold = 1;
  2036. }
  2037. decl_add(out, nout, PROP_BOLD, bold, important);
  2038. } else if (str_eq(prop, "font-style")) {
  2039. int italic = (vend - vstart >= 6 &&
  2040. ci_eq_n(s + vstart, "italic", 6));
  2041. decl_add(out, nout, PROP_ITALIC, italic, important);
  2042. } else if (str_eq(prop, "font-family")) {
  2043. unsigned int j;
  2044. int mono = 0;
  2045. for (j = vstart; j + 4 <= vend; j++)
  2046. if (ci_eq_n(s + j, "mono", 4))
  2047. mono = 1;
  2048. decl_add(out, nout, PROP_MONO, mono, important);
  2049. } else if (str_eq(prop, "text-align")) {
  2050. int a = BR_ALIGN_LEFT;
  2051. if (vend - vstart >= 6 &&
  2052. ci_eq_n(s + vstart, "center", 6))
  2053. a = BR_ALIGN_CENTER;
  2054. else if (vend - vstart >= 5 &&
  2055. ci_eq_n(s + vstart, "right", 5))
  2056. a = BR_ALIGN_RIGHT;
  2057. decl_add(out, nout, PROP_ALIGN, a, important);
  2058. } else if (str_eq(prop, "text-decoration")) {
  2059. int u = 0;
  2060. if (vend - vstart >= 9 &&
  2061. ci_eq_n(s + vstart, "underline", 9))
  2062. u = 1;
  2063. decl_add(out, nout, PROP_UNDERLINE, u, important);
  2064. } else if (str_eq(prop, "text-transform")) {
  2065. int u = (vend - vstart >= 9 &&
  2066. ci_eq_n(s + vstart, "uppercase", 9));
  2067. decl_add(out, nout, PROP_UPPER, u, important);
  2068. } else if (str_eq(prop, "display")) {
  2069. decl_add(out, nout, PROP_DISPLAY,
  2070. parse_display(s + vstart, vend - vstart),
  2071. important);
  2072. } else if (str_eq(prop, "visibility")) {
  2073. int h = contains_ci_len(s + vstart, vend - vstart,
  2074. "hidden") ||
  2075. contains_ci_len(s + vstart, vend - vstart,
  2076. "collapse");
  2077. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  2078. } else if (str_eq(prop, "opacity")) {
  2079. unsigned int q;
  2080. int h = 1, any = 0;
  2081. for (q = vstart; q < vend; q++) {
  2082. if (s[q] >= '1' && s[q] <= '9') {
  2083. h = 0;
  2084. break;
  2085. }
  2086. if (s[q] >= '0' && s[q] <= '9')
  2087. any = 1;
  2088. }
  2089. if (!any || contains_ci_len(s + vstart, vend - vstart,
  2090. "inherit"))
  2091. h = 0;
  2092. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  2093. } else if (str_eq(prop, "overflow") ||
  2094. str_eq(prop, "overflow-x") ||
  2095. str_eq(prop, "overflow-y")) {
  2096. int h = contains_ci_len(s + vstart, vend - vstart,
  2097. "hidden") ||
  2098. contains_ci_len(s + vstart, vend - vstart,
  2099. "clip");
  2100. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  2101. } else if (str_eq(prop, "float")) {
  2102. int f = 0;
  2103. if (vend - vstart >= 4 &&
  2104. ci_eq_n(s + vstart, "left", 4))
  2105. f = 1;
  2106. else if (vend - vstart >= 5 &&
  2107. ci_eq_n(s + vstart, "right", 5))
  2108. f = 2;
  2109. decl_add(out, nout, PROP_FLOAT, f, important);
  2110. } else if (str_eq(prop, "flex-grow") ||
  2111. str_eq(prop, "flex")) {
  2112. unsigned int q = vstart;
  2113. int g = 0;
  2114. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  2115. q++;
  2116. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  2117. g = g * 10 + (s[q] - '0');
  2118. q++;
  2119. }
  2120. if (g > 100)
  2121. g = 100;
  2122. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  2123. } else if (str_eq(prop, "flex-direction")) {
  2124. decl_add(out, nout, PROP_FLEX_COL,
  2125. contains_ci_len(s + vstart, vend - vstart,
  2126. "column"), important);
  2127. } else if (str_eq(prop, "flex-wrap")) {
  2128. int wrap = 0;
  2129. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  2130. wrap = 2;
  2131. else if (contains_ci_len(s + vstart, vend - vstart,
  2132. "wrap"))
  2133. wrap = 1;
  2134. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  2135. } else if (str_eq(prop, "position")) {
  2136. int p = 0;
  2137. if (contains_ci_len(s + vstart, vend - vstart,
  2138. "absolute"))
  2139. p = 1;
  2140. else if (contains_ci_len(s + vstart, vend - vstart,
  2141. "fixed"))
  2142. p = 2;
  2143. /* position:sticky stays in the flow. Treating it as
  2144. * fixed painted the header over the first content
  2145. * block; a static header is safer than hidden text. */
  2146. decl_add(out, nout, PROP_POS, p, important);
  2147. } else if (str_eq(prop, "left") || str_eq(prop, "top")) {
  2148. int is_left = prop[0] == 'l';
  2149. unsigned int pos = 0;
  2150. if (!(vend - vstart >= 4 &&
  2151. ci_eq_n(s + vstart, "auto", 4))) {
  2152. int v = parse_len(s + vstart, vend - vstart, &pos);
  2153. if (v > 32000)
  2154. v = 32000;
  2155. else if (v < -32000)
  2156. v = -32000;
  2157. decl_add(out, nout,
  2158. is_left ? PROP_LEFT : PROP_TOP, v, important);
  2159. decl_add(out, nout,
  2160. is_left ? PROP_LEFT_SET : PROP_TOP_SET, 1,
  2161. important);
  2162. }
  2163. } else if (str_eq(prop, "grid-template-columns") ||
  2164. str_eq(prop, "grid-template")) {
  2165. const char *v = s + vstart;
  2166. unsigned int vl = vend - vstart;
  2167. unsigned short val[2];
  2168. unsigned char kind[2];
  2169. int n, q;
  2170. if (str_eq(prop, "grid-template")) {
  2171. for (q = 0; q < (int)vl; q++)
  2172. if (v[q] == '/') {
  2173. q++;
  2174. while (q < (int)vl && is_space(v[q]))
  2175. q++;
  2176. v += q;
  2177. vl -= (unsigned int)q;
  2178. break;
  2179. }
  2180. }
  2181. if (contains_ci_len(v, vl, "auto-fit") ||
  2182. contains_ci_len(v, vl, "auto-fill")) {
  2183. unsigned int mp = 0;
  2184. int found = 0;
  2185. unsigned short mw = 200;
  2186. unsigned char mk = 1;
  2187. for (q = 0; q + 7 <= (int)vl; q++)
  2188. if (ci_eq_n(v + q, "minmax(", 7)) {
  2189. mp = (unsigned int)q + 7;
  2190. found = 1;
  2191. break;
  2192. }
  2193. if (found)
  2194. parse_grid_track(v, vl, &mp, &mw, &mk);
  2195. if (mk != 1 || mw < 80)
  2196. mw = 200;
  2197. if (mw > 480)
  2198. mw = 480;
  2199. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  2200. decl_add(out, nout, PROP_WIDTH, mw, important);
  2201. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  2202. } else {
  2203. n = parse_grid_cols(v, vl, val, kind);
  2204. if (n > 0) {
  2205. decl_add(out, nout, PROP_WIDTH, val[0],
  2206. important);
  2207. decl_add(out, nout, PROP_WIDTH_KIND,
  2208. kind[0], important);
  2209. decl_add(out, nout, PROP_GRID_NCOLS, n,
  2210. important);
  2211. }
  2212. if (n > 1) {
  2213. decl_add(out, nout, PROP_MAXW, val[1],
  2214. important);
  2215. decl_add(out, nout, PROP_MAXW_KIND,
  2216. kind[1], important);
  2217. }
  2218. }
  2219. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  2220. unsigned int gp = 0;
  2221. unsigned short gv = 0;
  2222. unsigned char gk = 1;
  2223. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  2224. if (gv > 120)
  2225. gv = 120;
  2226. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  2227. }
  2228. } else if (str_eq(prop, "grid-area")) {
  2229. int slot = parse_grid_slot(s + vstart, vend - vstart);
  2230. if (slot)
  2231. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  2232. } else if (str_eq(prop, "width") ||
  2233. str_eq(prop, "max-width") ||
  2234. str_eq(prop, "height")) {
  2235. unsigned short v;
  2236. unsigned char kind;
  2237. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  2238. if (str_eq(prop, "width")) {
  2239. decl_add(out, nout, PROP_WIDTH, v, important);
  2240. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  2241. important);
  2242. } else if (str_eq(prop, "max-width")) {
  2243. decl_add(out, nout, PROP_MAXW, v, important);
  2244. decl_add(out, nout, PROP_MAXW_KIND, kind,
  2245. important);
  2246. } else {
  2247. decl_add(out, nout, PROP_HEIGHT, v, important);
  2248. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  2249. important);
  2250. }
  2251. }
  2252. } else if (str_eq(prop, "line-height")) {
  2253. unsigned short px;
  2254. unsigned char pct;
  2255. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  2256. if (px) {
  2257. decl_add(out, nout, PROP_LH_PX, px, important);
  2258. decl_add(out, nout, PROP_LH_PCT, 0, important);
  2259. } else {
  2260. decl_add(out, nout, PROP_LH_PX, 0, important);
  2261. decl_add(out, nout, PROP_LH_PCT, pct, important);
  2262. }
  2263. }
  2264. } else if (str_eq(prop, "list-style") ||
  2265. str_eq(prop, "list-style-type")) {
  2266. int none = (vend - vstart >= 4 &&
  2267. contains_ci_len(s + vstart, vend - vstart,
  2268. "none"));
  2269. decl_add(out, nout, PROP_LIST_NONE, none, important);
  2270. } else if (str_eq(prop, "border-radius") ||
  2271. str_eq(prop, "border-top-left-radius") ||
  2272. str_eq(prop, "border-top-right-radius") ||
  2273. str_eq(prop, "border-bottom-right-radius") ||
  2274. str_eq(prop, "border-bottom-left-radius")) {
  2275. unsigned short v[4];
  2276. unsigned char k[4];
  2277. unsigned int pos = 0;
  2278. int n = 0;
  2279. while (pos < vend - vstart && n < 4) {
  2280. while (pos < vend - vstart &&
  2281. is_space(s[vstart + pos]))
  2282. pos++;
  2283. if (pos >= vend - vstart ||
  2284. s[vstart + pos] == '/')
  2285. break;
  2286. if (!parse_radius_value(s + vstart,
  2287. vend - vstart, &pos,
  2288. &v[n], &k[n]))
  2289. break;
  2290. n++;
  2291. }
  2292. if (n) {
  2293. unsigned short r[4];
  2294. unsigned char rk[4];
  2295. int i;
  2296. if (n == 1) {
  2297. for (i = 0; i < 4; i++) {
  2298. r[i] = v[0];
  2299. rk[i] = k[0];
  2300. }
  2301. } else if (n == 2) {
  2302. r[0] = r[2] = v[0];
  2303. r[1] = r[3] = v[1];
  2304. rk[0] = rk[2] = k[0];
  2305. rk[1] = rk[3] = k[1];
  2306. } else if (n == 3) {
  2307. r[0] = v[0];
  2308. r[1] = r[3] = v[1];
  2309. r[2] = v[2];
  2310. rk[0] = k[0];
  2311. rk[1] = rk[3] = k[1];
  2312. rk[2] = k[2];
  2313. } else {
  2314. for (i = 0; i < 4; i++) {
  2315. r[i] = v[i];
  2316. rk[i] = k[i];
  2317. }
  2318. }
  2319. for (i = 0; i < 4; i++)
  2320. r[i] = rk[i] == 2 ? 255 :
  2321. (r[i] > 255 ? 255 : r[i]);
  2322. if (str_eq(prop, "border-top-left-radius")) {
  2323. decl_add(out, nout, PROP_RADIUS_TL,
  2324. (short)r[0], important);
  2325. } else if (str_eq(prop,
  2326. "border-top-right-radius")) {
  2327. decl_add(out, nout, PROP_RADIUS_TR,
  2328. (short)r[0], important);
  2329. } else if (str_eq(prop,
  2330. "border-bottom-right-radius")) {
  2331. decl_add(out, nout, PROP_RADIUS_BR,
  2332. (short)r[0], important);
  2333. } else if (str_eq(prop,
  2334. "border-bottom-left-radius")) {
  2335. decl_add(out, nout, PROP_RADIUS_BL,
  2336. (short)r[0], important);
  2337. } else {
  2338. decl_add(out, nout, PROP_RADIUS_TL,
  2339. (short)r[0], important);
  2340. decl_add(out, nout, PROP_RADIUS_TR,
  2341. (short)r[1], important);
  2342. decl_add(out, nout, PROP_RADIUS_BR,
  2343. (short)r[2], important);
  2344. decl_add(out, nout, PROP_RADIUS_BL,
  2345. (short)r[3], important);
  2346. }
  2347. }
  2348. } else if (str_eq(prop, "border") ||
  2349. str_eq(prop, "border-top") ||
  2350. str_eq(prop, "border-right") ||
  2351. str_eq(prop, "border-bottom") ||
  2352. str_eq(prop, "border-left") ||
  2353. str_eq(prop, "border-color") ||
  2354. str_eq(prop, "border-width")) {
  2355. unsigned short c;
  2356. int bw;
  2357. int have_color;
  2358. if (vend - vstart >= 4 &&
  2359. ci_eq_n(s + vstart, "none", 4)) {
  2360. decl_add(out, nout, PROP_BORDER_W, 0, important);
  2361. } else {
  2362. bw = parse_border_width(s + vstart,
  2363. vend - vstart);
  2364. have_color = find_color(s + vstart,
  2365. vend - vstart, &c);
  2366. if (have_color) {
  2367. decl_add(out, nout, PROP_BORDER_COLOR, c,
  2368. important);
  2369. if (bw < 0)
  2370. bw = 1;
  2371. }
  2372. if (bw >= 0) {
  2373. if (bw > 3)
  2374. bw = 3;
  2375. decl_add(out, nout, PROP_BORDER_W, bw,
  2376. important);
  2377. }
  2378. }
  2379. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  2380. int vals[4], nv = 0;
  2381. unsigned int pos = 0;
  2382. int is_pad = prop[0] == 'p';
  2383. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  2384. while (pos < vend - vstart && nv < 4) {
  2385. vals[nv++] = parse_len(s + vstart,
  2386. vend - vstart, &pos);
  2387. while (pos < vend - vstart &&
  2388. is_space(s[vstart + pos]))
  2389. pos++;
  2390. }
  2391. if (nv == 1) {
  2392. vals[1] = vals[2] = vals[3] = vals[0];
  2393. } else if (nv == 2) {
  2394. vals[2] = vals[0];
  2395. vals[3] = vals[1];
  2396. } else if (nv == 3) {
  2397. vals[3] = vals[1];
  2398. }
  2399. if (nv > 0) {
  2400. decl_add(out, nout, base, vals[0], important);
  2401. decl_add(out, nout, (unsigned char)(base + 1),
  2402. vals[1], important);
  2403. decl_add(out, nout, (unsigned char)(base + 2),
  2404. vals[2], important);
  2405. decl_add(out, nout, (unsigned char)(base + 3),
  2406. vals[3], important);
  2407. }
  2408. } else if (str_eq(prop, "margin-top") ||
  2409. str_eq(prop, "margin-right") ||
  2410. str_eq(prop, "margin-bottom") ||
  2411. str_eq(prop, "margin-left") ||
  2412. str_eq(prop, "padding-top") ||
  2413. str_eq(prop, "padding-right") ||
  2414. str_eq(prop, "padding-bottom") ||
  2415. str_eq(prop, "padding-left")) {
  2416. unsigned int pos = 0;
  2417. int v = parse_len(s + vstart, vend - vstart, &pos);
  2418. unsigned char p = PROP_MT;
  2419. const char *side;
  2420. if (prop[0] == 'p')
  2421. p = PROP_PT;
  2422. side = prop + (prop[0] == 'p' ? 8 : 7);
  2423. if (str_eq(side, "right"))
  2424. p = (unsigned char)(p + 1);
  2425. else if (str_eq(side, "bottom"))
  2426. p = (unsigned char)(p + 2);
  2427. else if (str_eq(side, "left"))
  2428. p = (unsigned char)(p + 3);
  2429. decl_add(out, nout, p, v, important);
  2430. }
  2431. }
  2432. }
  2433. /* Skip @media/@font-face/@keyframes/@import without descending into their
  2434. * nested blocks. This is the mobile/base rules path: media enhancements are
  2435. * deliberately ignored for now. */
  2436. static unsigned int skip_at_rule(const char *s, unsigned int len,
  2437. unsigned int i)
  2438. {
  2439. unsigned int j = i;
  2440. while (j < len && s[j] != '{' && s[j] != ';')
  2441. j++;
  2442. if (j >= len)
  2443. return len;
  2444. if (s[j] == ';')
  2445. return j + 1;
  2446. j++;
  2447. return css_skip_nested(s, len, j);
  2448. }
  2449. static unsigned int viewport_w = 1024;
  2450. static unsigned short theme_text = 0x0000;
  2451. static unsigned short theme_bg = 0xFFFF;
  2452. static unsigned short theme_panel = 0xEF5D;
  2453. static unsigned short theme_link = 0x001B;
  2454. void br_set_theme(unsigned short text, unsigned short bg,
  2455. unsigned short panel, unsigned short link)
  2456. {
  2457. theme_text = text;
  2458. theme_bg = bg;
  2459. theme_panel = panel;
  2460. theme_link = link;
  2461. }
  2462. void br_set_viewport(unsigned int width)
  2463. {
  2464. viewport_w = width ? width : 1024;
  2465. }
  2466. static unsigned int media_len_px(const char *s, unsigned int len)
  2467. {
  2468. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  2469. int em = 0;
  2470. while (i < len && is_space(s[i]))
  2471. i++;
  2472. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2473. whole = whole * 10 + (unsigned int)(s[i] - '0');
  2474. i++;
  2475. }
  2476. if (i < len && s[i] == '.') {
  2477. i++;
  2478. /* Consume every fractional digit even when there are more
  2479. * than the three we keep: leaving the tail behind made the
  2480. * unit check miss "em" (74.9375em became 74px). */
  2481. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2482. if (scale < 1000) {
  2483. frac = frac * 10 + (unsigned int)(s[i] - '0');
  2484. scale *= 10;
  2485. }
  2486. i++;
  2487. }
  2488. }
  2489. while (i < len && is_space(s[i]))
  2490. i++;
  2491. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2492. em = 1;
  2493. i += 2;
  2494. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2495. em = 1;
  2496. i += 3;
  2497. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  2498. i += 2;
  2499. }
  2500. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2501. if (em)
  2502. return v * 16 / 100;
  2503. return v / 100;
  2504. }
  2505. /* One parenthesised media feature: min-width/max-width and the modern
  2506. * (width <= 600px) form. Unknown features are treated as true. */
  2507. static int media_feature_matches(const char *s, unsigned int len)
  2508. {
  2509. unsigned int i;
  2510. while (len && is_space(*s)) { s++; len--; }
  2511. while (len && is_space(s[len - 1])) len--;
  2512. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  2513. /* There is no user-selectable colour scheme. Ignore both
  2514. * variants so a page's base palette is used; sites whose base
  2515. * is dark (like the project docs) then stay dark instead of
  2516. * flipping to their light media override. */
  2517. return 0;
  2518. }
  2519. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  2520. for (i = 9; i < len && s[i] != ':'; i++)
  2521. ;
  2522. if (i < len)
  2523. return viewport_w >= media_len_px(s + i + 1,
  2524. len - i - 1);
  2525. return 1;
  2526. }
  2527. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  2528. for (i = 9; i < len && s[i] != ':'; i++)
  2529. ;
  2530. if (i < len)
  2531. return viewport_w <= media_len_px(s + i + 1,
  2532. len - i - 1);
  2533. return 1;
  2534. }
  2535. for (i = 0; i < len; i++) {
  2536. if (s[i] == '<' || s[i] == '>') {
  2537. int max = s[i] == '<';
  2538. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  2539. if (i + 1 < len && s[i + 1] == '=')
  2540. v = media_len_px(s + i + 2, len - i - 2);
  2541. return max ? viewport_w <= v : viewport_w >= v;
  2542. }
  2543. }
  2544. return 1;
  2545. }
  2546. static int media_query_matches(const char *s, unsigned int len)
  2547. {
  2548. unsigned int i;
  2549. if (contains_ci_len(s, len, "print") &&
  2550. !contains_ci_len(s, len, "screen"))
  2551. return 0;
  2552. if (contains_ci_len(s, len, "not "))
  2553. return 0;
  2554. for (i = 0; i < len; i++) {
  2555. if (s[i] == '(') {
  2556. unsigned int st = ++i;
  2557. while (i < len && s[i] != ')')
  2558. i++;
  2559. if (!media_feature_matches(s + st, i - st))
  2560. return 0;
  2561. }
  2562. }
  2563. return 1;
  2564. }
  2565. static int media_list_matches(const char *s, unsigned int len)
  2566. {
  2567. unsigned int i = 0;
  2568. while (i < len) {
  2569. unsigned int st = i;
  2570. while (i < len && s[i] != ',')
  2571. i++;
  2572. if (media_query_matches(s + st, i - st))
  2573. return 1;
  2574. i++;
  2575. }
  2576. return 0;
  2577. }
  2578. static unsigned int rule_hash(const char *s, unsigned int len)
  2579. {
  2580. unsigned int h = 2166136261u, i;
  2581. for (i = 0; i < len; i++) {
  2582. char c = s[i];
  2583. if (c >= 'A' && c <= 'Z')
  2584. c = (char)(c - 'A' + 'a');
  2585. h ^= (unsigned char)c;
  2586. h *= 16777619u;
  2587. }
  2588. return h % BR_RULE_HASH;
  2589. }
  2590. /* Index a parsed rule by the most selective component of its rightmost
  2591. * compound selector. br_style_for() then only visits rules that can
  2592. * possibly match a node instead of scanning every rule. */
  2593. static void rule_index_add(struct br_doc *d, unsigned int idx,
  2594. const char *key, unsigned int len)
  2595. {
  2596. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  2597. unsigned short *head;
  2598. for (i = 0; i < len; i++) {
  2599. if (key[i] == '.' || key[i] == '#') {
  2600. unsigned int st = i + 1, n = 0;
  2601. while (st + n < len && is_ident(key[st + n]))
  2602. n++;
  2603. if (key[i] == '#') {
  2604. ipos = st;
  2605. ilen = n;
  2606. } else if (n) {
  2607. cpos = st;
  2608. clen = n;
  2609. }
  2610. i = st + n - 1;
  2611. }
  2612. }
  2613. if (ilen)
  2614. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  2615. else if (clen)
  2616. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  2617. else if (len == 5 && ci_eq_n(key, ":root", 5))
  2618. head = &d->rule_tag[BR_TAG_HTML];
  2619. else if (len && is_ident(key[0])) {
  2620. char name[16];
  2621. unsigned int n = 0, tag;
  2622. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  2623. name[n] = key[n];
  2624. n++;
  2625. }
  2626. name[n] = '\0';
  2627. tag = br_tag_from_name(name);
  2628. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  2629. head = &d->rule_tag[tag];
  2630. else
  2631. head = &d->rule_any;
  2632. } else {
  2633. head = &d->rule_any;
  2634. }
  2635. d->rule_next[idx] = *head;
  2636. *head = (unsigned short)idx;
  2637. }
  2638. static unsigned int selector_specificity(const char *s, unsigned int len);
  2639. static int css_collect_only;
  2640. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  2641. {
  2642. unsigned int i = 0;
  2643. while (i < len && d->nrules < d->rule_cap) {
  2644. unsigned int sstart, send, bstart, bend, j;
  2645. struct br_decl decls[BR_DECLS];
  2646. unsigned int nd;
  2647. while (i < len && is_space(s[i]))
  2648. i++;
  2649. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  2650. i += 2;
  2651. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  2652. i++;
  2653. i += 2;
  2654. continue;
  2655. }
  2656. if (i >= len || s[i] == '}')
  2657. break;
  2658. if (s[i] == '@') {
  2659. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  2660. unsigned int q = i + 6;
  2661. while (q < len && s[q] != '{')
  2662. q++;
  2663. if (q < len &&
  2664. media_list_matches(s + i + 6,
  2665. q - (i + 6))) {
  2666. unsigned int b = q + 1;
  2667. unsigned int e = css_skip_nested(s, len, b);
  2668. if (e > b)
  2669. parse_css(d, s + b,
  2670. (e - 1) - b);
  2671. i = e;
  2672. } else {
  2673. i = skip_at_rule(s, len, i);
  2674. }
  2675. continue;
  2676. }
  2677. i = skip_at_rule(s, len, i);
  2678. continue;
  2679. }
  2680. sstart = i;
  2681. while (i < len && s[i] != '{')
  2682. i++;
  2683. if (i >= len)
  2684. break;
  2685. send = i;
  2686. i++;
  2687. bstart = i;
  2688. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  2689. * to the matching close brace; stopping at the first '}' left
  2690. * the parser out of sync and dropped the rest of the sheet
  2691. * (nd-aktuell lost its desktop rules that way). */
  2692. bend = css_block_end(s, len, i);
  2693. i = bend < len ? bend + 1 : len;
  2694. while (sstart < send && is_space(s[sstart]))
  2695. sstart++;
  2696. while (send > sstart && is_space(s[send - 1]))
  2697. send--;
  2698. if (sstart == send)
  2699. continue;
  2700. if (!css_dark_selector(s + sstart, send - sstart))
  2701. css_collect_block(d, s + bstart, bend - bstart);
  2702. if (css_collect_only)
  2703. continue;
  2704. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  2705. if (!nd)
  2706. continue;
  2707. /* Split a comma-separated selector list into separate rules. */
  2708. j = sstart;
  2709. while (j <= send && d->nrules < d->rule_cap) {
  2710. unsigned int q = j, e, k;
  2711. char sel[160];
  2712. unsigned int off;
  2713. while (q < send && s[q] != ',')
  2714. q++;
  2715. e = q;
  2716. while (j < e && is_space(s[j]))
  2717. j++;
  2718. while (e > j && is_space(s[e - 1]))
  2719. e--;
  2720. if (e > j) {
  2721. /* Ignore single selectors with sibling combinators,
  2722. * pseudo-classes or attribute tests: they need
  2723. * state/attributes we do not model. The other
  2724. * selectors in the list still apply. */
  2725. for (k = j; k < e; ) {
  2726. if (s[k] == '+' || s[k] == '~')
  2727. break;
  2728. if (s[k] == ':') {
  2729. if (k + 5 <= e &&
  2730. ci_eq_n(s + k, ":root", 5)) {
  2731. k += 5;
  2732. continue;
  2733. }
  2734. if (k + 5 <= e &&
  2735. ci_eq_n(s + k, ":not(", 5)) {
  2736. unsigned int n = k + 5;
  2737. int nd = 1;
  2738. while (n < e && nd) {
  2739. if (s[n] == '(')
  2740. nd++;
  2741. else if (s[n] == ')')
  2742. nd--;
  2743. n++;
  2744. }
  2745. if (nd)
  2746. break;
  2747. k = n;
  2748. continue;
  2749. }
  2750. break;
  2751. }
  2752. k++;
  2753. }
  2754. if (k < e) {
  2755. j = q + 1;
  2756. if (q >= send)
  2757. break;
  2758. continue;
  2759. }
  2760. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  2761. sel[k] = s[j + k];
  2762. sel[k] = '\0';
  2763. off = store_attr(d, sel, k, 159);
  2764. if (off) {
  2765. struct br_rule *r = &d->rules[d->nrules++];
  2766. unsigned int ks = 0, q;
  2767. for (q = 0; q < k; q++)
  2768. if (sel[q] == ' ' || sel[q] == '>')
  2769. ks = q + 1;
  2770. r->sel_off = off;
  2771. r->key_off = store_attr(d, sel + ks,
  2772. k - ks, 159);
  2773. r->spec = (unsigned short)selector_specificity(sel, k);
  2774. r->single = 1;
  2775. {
  2776. unsigned int z;
  2777. for (z = 0; z < k; z++)
  2778. if (is_space(sel[z]) || sel[z] == '>') {
  2779. r->single = 0;
  2780. break;
  2781. }
  2782. }
  2783. r->nd = (unsigned char)nd;
  2784. for (k = 0; k < nd; k++)
  2785. r->d[k] = decls[k];
  2786. rule_index_add(d, d->nrules - 1,
  2787. attr_ptr(d, r->key_off),
  2788. s_len(attr_ptr(d, r->key_off)));
  2789. }
  2790. }
  2791. j = q + 1;
  2792. if (q >= send)
  2793. break;
  2794. }
  2795. }
  2796. }
  2797. /* Sites often set html { font-size: 62.5% } and then size everything in
  2798. * rem. Detect that root size before parsing the sheet so a rem resolves to
  2799. * the same pixels a real browser would use. */
  2800. static void detect_root_font(const char *s, unsigned int len)
  2801. {
  2802. unsigned int i;
  2803. for (i = 0; i + 4 < len; i++) {
  2804. unsigned int j, end, sel, q;
  2805. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  2806. int exact;
  2807. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  2808. s[i + 3] == 'l'))
  2809. continue;
  2810. if (i + 4 < len && s[i + 4] != '{' && s[i + 4] != ',' &&
  2811. s[i + 4] != '.' && s[i + 4] != ':' && s[i + 4] != '[')
  2812. continue;
  2813. sel = i;
  2814. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  2815. s[sel - 1] != ';' && s[sel - 1] != ',')
  2816. sel--;
  2817. q = sel;
  2818. while (q < i && is_space(s[q]))
  2819. q++;
  2820. exact = (q == i);
  2821. if (!exact && i + 4 < len && s[i + 4] != ',' &&
  2822. s[i + 4] != '.')
  2823. continue;
  2824. j = i + 4;
  2825. while (j < len && is_space(s[j]))
  2826. j++;
  2827. if (j >= len || s[j] != '{')
  2828. continue;
  2829. while (j < len && s[j] != '{')
  2830. j++;
  2831. if (j >= len)
  2832. break;
  2833. end = j + 1;
  2834. while (end < len && s[end] != '}')
  2835. end++;
  2836. for (; j + 9 < end; j++) {
  2837. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  2838. s[j + 3] == 't' && s[j + 4] == '-' &&
  2839. s[j + 5] == 's' && s[j + 6] == 'i' &&
  2840. s[j + 7] == 'z' && s[j + 8] == 'e'))
  2841. continue;
  2842. j += 9;
  2843. while (j < end && s[j] != ':')
  2844. j++;
  2845. if (j >= end)
  2846. break;
  2847. j++;
  2848. while (j < end && is_space(s[j]))
  2849. j++;
  2850. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2851. v = v * 10 + (s[j] - '0');
  2852. digits = 1;
  2853. j++;
  2854. }
  2855. if (j < end && s[j] == '.') {
  2856. j++;
  2857. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2858. v = v * 10 + (s[j] - '0');
  2859. frac++;
  2860. j++;
  2861. }
  2862. }
  2863. while (frac < 2) {
  2864. v *= 10;
  2865. frac++;
  2866. }
  2867. while (frac > 2) {
  2868. v /= 10;
  2869. frac--;
  2870. }
  2871. if (!digits)
  2872. break;
  2873. if (j < end && s[j] == '%')
  2874. pct = 1;
  2875. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  2876. px = 1;
  2877. if (pct)
  2878. v = (v * 16 + 5000) / 10000;
  2879. else if (px)
  2880. v /= 100;
  2881. else
  2882. v = v * 16 / 100;
  2883. if (exact && v >= 8 && v <= 24)
  2884. root_font_px = v;
  2885. if (exact)
  2886. return;
  2887. }
  2888. }
  2889. }
  2890. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  2891. {
  2892. detect_root_font(css, len);
  2893. css_collect_only = 1;
  2894. parse_css(d, css, len);
  2895. css_collect_only = 0;
  2896. parse_css(d, css, len);
  2897. }
  2898. void br_parse_inline_css(struct br_doc *d)
  2899. {
  2900. if (d->inline_css_len) {
  2901. css_collect_only = 1;
  2902. parse_css(d, css_store, d->inline_css_len);
  2903. css_collect_only = 0;
  2904. parse_css(d, css_store, d->inline_css_len);
  2905. }
  2906. d->inline_css_len = 0;
  2907. }
  2908. void br_reset_styles(struct br_doc *d)
  2909. {
  2910. unsigned int i;
  2911. for (i = 0; i < d->nnode; i++)
  2912. d->nodes[i].style_valid = 0;
  2913. }
  2914. /* ---- selector matching ---- */
  2915. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  2916. unsigned int clen)
  2917. {
  2918. const char *s = attr_ptr(d, d->nodes[node].class_off);
  2919. unsigned int i = 0;
  2920. while (s[i]) {
  2921. unsigned int st, n;
  2922. while (s[i] && is_space(s[i]))
  2923. i++;
  2924. st = i;
  2925. while (s[i] && !is_space(s[i]))
  2926. i++;
  2927. n = i - st;
  2928. if (n == clen && ci_eq_n(s + st, cls, n))
  2929. return 1;
  2930. }
  2931. return 0;
  2932. }
  2933. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  2934. unsigned int len)
  2935. {
  2936. const char *s = attr_ptr(d, d->nodes[node].id_off);
  2937. unsigned int i;
  2938. for (i = 0; i < len; i++)
  2939. if (s[i] != id[i])
  2940. return 0;
  2941. return s[len] == '\0';
  2942. }
  2943. static int is_ident(char c)
  2944. {
  2945. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  2946. (c >= '0' && c <= '9') || c == '-' || c == '_';
  2947. }
  2948. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  2949. {
  2950. unsigned int n = 0;
  2951. while (lit[n])
  2952. n++;
  2953. return n == nlen && ci_eq_n(name, lit, nlen);
  2954. }
  2955. static int attr_present(struct br_doc *d, unsigned int node,
  2956. const char *name, unsigned int nlen)
  2957. {
  2958. struct br_node *n = &d->nodes[node];
  2959. if (attr_is(name, nlen, "data-collapse-target"))
  2960. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  2961. if (attr_is(name, nlen, "data-collapse-trigger"))
  2962. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  2963. if (attr_is(name, nlen, "is-open"))
  2964. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  2965. if (attr_is(name, nlen, "disabled"))
  2966. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  2967. if (attr_is(name, nlen, "open"))
  2968. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  2969. if (attr_is(name, nlen, "inert"))
  2970. return (n->attr_flags & BR_ATTR_INERT) != 0;
  2971. if (attr_is(name, nlen, "hidden"))
  2972. return (n->flags & BR_NODE_HIDDEN) != 0;
  2973. if (attr_is(name, nlen, "href"))
  2974. return attr_ptr(d, n->href_off)[0] != 0;
  2975. if (attr_is(name, nlen, "class"))
  2976. return attr_ptr(d, n->class_off)[0] != 0;
  2977. if (attr_is(name, nlen, "id"))
  2978. return attr_ptr(d, n->id_off)[0] != 0;
  2979. if (attr_is(name, nlen, "src"))
  2980. return attr_ptr(d, n->src_off)[0] != 0;
  2981. if (attr_is(name, nlen, "alt"))
  2982. return attr_ptr(d, n->alt_off)[0] != 0;
  2983. if (attr_is(name, nlen, "rel"))
  2984. return attr_ptr(d, n->rel_off)[0] != 0;
  2985. return 0;
  2986. }
  2987. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  2988. unsigned int len)
  2989. {
  2990. unsigned int i = 0;
  2991. int got = 0;
  2992. if (i < len && s[i] == '*') {
  2993. i++;
  2994. got = 1;
  2995. } else if (i < len && is_ident(s[i])) {
  2996. char name[16];
  2997. unsigned int n = 0, tag;
  2998. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  2999. name[n++] = s[i++];
  3000. name[n] = '\0';
  3001. tag = br_tag_from_name(name);
  3002. if (tag == BR_TAG_UNKNOWN) {
  3003. const char *raw = attr_ptr(d,
  3004. d->nodes[node].name_off);
  3005. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  3006. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  3007. return 0;
  3008. } else if (tag != d->nodes[node].tag) {
  3009. return 0;
  3010. }
  3011. got = 1;
  3012. }
  3013. while (i < len) {
  3014. if (s[i] == '.') {
  3015. unsigned int st;
  3016. i++;
  3017. st = i;
  3018. while (i < len && is_ident(s[i]))
  3019. i++;
  3020. if (!class_has(d, node, s + st, i - st))
  3021. return 0;
  3022. got = 1;
  3023. } else if (s[i] == '#') {
  3024. unsigned int st;
  3025. i++;
  3026. st = i;
  3027. while (i < len && is_ident(s[i]))
  3028. i++;
  3029. if (!id_eq(d, node, s + st, i - st))
  3030. return 0;
  3031. got = 1;
  3032. } else if (s[i] == '[') {
  3033. unsigned int st, e;
  3034. i++;
  3035. st = e = i;
  3036. while (e < len && s[e] != ']' && s[e] != '=' &&
  3037. !is_space(s[e]))
  3038. e++;
  3039. if (e == st || !attr_present(d, node, s + st, e - st))
  3040. return 0;
  3041. while (i < len && s[i] != ']')
  3042. i++;
  3043. if (i < len)
  3044. i++;
  3045. got = 1;
  3046. } else if (s[i] == ':') {
  3047. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  3048. (i + 5 == len || !is_ident(s[i + 5]))) {
  3049. if (d->nodes[node].tag != BR_TAG_HTML)
  3050. return 0;
  3051. i += 5;
  3052. got = 1;
  3053. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  3054. unsigned int st = i + 5, e = st;
  3055. int depth = 1;
  3056. while (e < len && depth) {
  3057. if (s[e] == '(')
  3058. depth++;
  3059. else if (s[e] == ')')
  3060. depth--;
  3061. if (depth)
  3062. e++;
  3063. }
  3064. if (e >= len)
  3065. return 0;
  3066. for (unsigned int z = st; z < e; z++)
  3067. if (s[z] == ':')
  3068. return 0;
  3069. if (compound_match(d, node, s + st, e - st))
  3070. return 0;
  3071. i = e + 1;
  3072. got = 1;
  3073. } else {
  3074. return 0; /* other pseudo-classes are not modelled */
  3075. }
  3076. } else {
  3077. i++;
  3078. }
  3079. }
  3080. return got;
  3081. }
  3082. static unsigned int selector_specificity(const char *s, unsigned int len)
  3083. {
  3084. unsigned int i, spec = 0;
  3085. for (i = 0; i < len; i++) {
  3086. if (s[i] == '#')
  3087. spec += 100;
  3088. else if (s[i] == '.' || s[i] == '[' ||
  3089. (s[i] == ':' && !(i + 5 <= len &&
  3090. ci_eq_n(s + i, ":root", 5))))
  3091. spec += 10;
  3092. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  3093. if (is_ident(s[i]))
  3094. spec += 1;
  3095. }
  3096. }
  3097. return spec;
  3098. }
  3099. /* Match one complex selector; returns specificity or -1. */
  3100. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  3101. unsigned int len, int right_done)
  3102. {
  3103. struct {
  3104. unsigned int start, end;
  3105. int child;
  3106. } parts[8];
  3107. int np = 0;
  3108. unsigned int i = 0;
  3109. int right;
  3110. while (i < len) {
  3111. int child = 0;
  3112. unsigned int st, e;
  3113. while (i < len && is_space(s[i]))
  3114. i++;
  3115. if (i < len && s[i] == '>') {
  3116. child = 1;
  3117. i++;
  3118. while (i < len && is_space(s[i]))
  3119. i++;
  3120. }
  3121. if (i >= len)
  3122. break;
  3123. st = i;
  3124. while (i < len && !is_space(s[i]) && s[i] != '>')
  3125. i++;
  3126. e = i;
  3127. if (np < 8) {
  3128. parts[np].start = st;
  3129. parts[np].end = e;
  3130. parts[np].child = child;
  3131. np++;
  3132. }
  3133. }
  3134. if (!np)
  3135. return -1;
  3136. if (!right_done &&
  3137. !compound_match(d, node, s + parts[np - 1].start,
  3138. parts[np - 1].end - parts[np - 1].start))
  3139. return -1;
  3140. right = np - 1;
  3141. while (right > 0) {
  3142. unsigned int p = d->nodes[node].parent;
  3143. if (!p)
  3144. return -1;
  3145. if (parts[right].child) {
  3146. if (!compound_match(d, p, s + parts[right - 1].start,
  3147. parts[right - 1].end -
  3148. parts[right - 1].start))
  3149. return -1;
  3150. node = p;
  3151. } else {
  3152. while (p && !compound_match(d, p,
  3153. s + parts[right - 1].start,
  3154. parts[right - 1].end -
  3155. parts[right - 1].start))
  3156. p = d->nodes[p].parent;
  3157. if (!p)
  3158. return -1;
  3159. node = p;
  3160. }
  3161. right--;
  3162. }
  3163. return (int)selector_specificity(s, len);
  3164. }
  3165. #define RULE_MATCH_MAX 512
  3166. struct rule_match {
  3167. unsigned short idx;
  3168. short spec;
  3169. };
  3170. static struct rule_match rule_matches[RULE_MATCH_MAX];
  3171. static unsigned int nrule_matches;
  3172. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  3173. {
  3174. struct br_rule *r = &d->rules[idx];
  3175. const char *key = attr_ptr(d, r->key_off);
  3176. int spec, key_ok = 0;
  3177. if (key[0]) {
  3178. if (!compound_match(d, node, key, s_len(key)))
  3179. return;
  3180. key_ok = 1;
  3181. }
  3182. spec = (r->single && key_ok) ? (int)r->spec :
  3183. selector_match(d, node, attr_ptr(d, r->sel_off),
  3184. s_len(attr_ptr(d, r->sel_off)), key_ok);
  3185. if (spec < 0)
  3186. return;
  3187. if (nrule_matches < RULE_MATCH_MAX) {
  3188. rule_matches[nrule_matches].idx = (unsigned short)idx;
  3189. rule_matches[nrule_matches].spec = (short)spec;
  3190. nrule_matches++;
  3191. }
  3192. }
  3193. static void rule_scan_list(struct br_doc *d, unsigned int node,
  3194. unsigned short head)
  3195. {
  3196. while (head != 0xFFFF) {
  3197. rule_try(d, node, head);
  3198. head = d->rule_next[head];
  3199. }
  3200. }
  3201. static void rule_scan_class(struct br_doc *d, unsigned int node,
  3202. const char *cls, unsigned int len)
  3203. {
  3204. if (len)
  3205. rule_scan_list(d, node,
  3206. d->rule_bucket[rule_hash(cls, len)]);
  3207. }
  3208. static void rule_collect(struct br_doc *d, unsigned int node)
  3209. {
  3210. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  3211. const char *id = attr_ptr(d, d->nodes[node].id_off);
  3212. unsigned int tag = d->nodes[node].tag;
  3213. unsigned int i;
  3214. nrule_matches = 0;
  3215. rule_scan_list(d, node, d->rule_any);
  3216. if (tag < BR_TAG_MAX)
  3217. rule_scan_list(d, node, d->rule_tag[tag]);
  3218. if (id[0])
  3219. rule_scan_list(d, node,
  3220. d->rule_bucket[rule_hash(id, s_len(id))]);
  3221. i = 0;
  3222. while (cls[i]) {
  3223. unsigned int st, n;
  3224. while (cls[i] && is_space(cls[i]))
  3225. i++;
  3226. st = i;
  3227. while (cls[i] && !is_space(cls[i]))
  3228. i++;
  3229. n = i - st;
  3230. if (n)
  3231. rule_scan_class(d, node, cls + st, n);
  3232. }
  3233. /* Rules must cascade in document order even though the index visits
  3234. * them in bucket order. */
  3235. for (i = 1; i < nrule_matches; i++) {
  3236. struct rule_match m = rule_matches[i];
  3237. unsigned int j = i;
  3238. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  3239. rule_matches[j] = rule_matches[j - 1];
  3240. j--;
  3241. }
  3242. rule_matches[j] = m;
  3243. }
  3244. }
  3245. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  3246. {
  3247. switch (dcl->prop) {
  3248. case PROP_COLOR:
  3249. st->color = (unsigned short)dcl->val;
  3250. break;
  3251. case PROP_BG:
  3252. st->bg = (unsigned short)dcl->val;
  3253. st->has_bg = 1;
  3254. break;
  3255. case PROP_FONT_PX:
  3256. if (dcl->val < 8)
  3257. st->font_px = 8;
  3258. else if (dcl->val > 48)
  3259. st->font_px = 48;
  3260. else
  3261. st->font_px = (unsigned char)dcl->val;
  3262. break;
  3263. case PROP_BOLD:
  3264. st->bold = (unsigned char)(dcl->val != 0);
  3265. break;
  3266. case PROP_ITALIC:
  3267. st->italic = (unsigned char)(dcl->val != 0);
  3268. break;
  3269. case PROP_MONO:
  3270. st->mono = (unsigned char)(dcl->val != 0);
  3271. break;
  3272. case PROP_ALIGN:
  3273. st->align = (unsigned char)dcl->val;
  3274. break;
  3275. case PROP_UNDERLINE:
  3276. st->underline = (unsigned char)(dcl->val != 0);
  3277. break;
  3278. case PROP_DISPLAY:
  3279. st->display = (unsigned char)dcl->val;
  3280. break;
  3281. case PROP_VIS_HIDDEN:
  3282. st->vis_hidden = (unsigned char)(dcl->val != 0);
  3283. break;
  3284. case PROP_OPACITY_HIDDEN:
  3285. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  3286. break;
  3287. case PROP_OVERFLOW_HIDDEN:
  3288. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  3289. break;
  3290. case PROP_MT: st->mt = dcl->val; break;
  3291. case PROP_MR: st->mr = dcl->val; break;
  3292. case PROP_MB: st->mb = dcl->val; break;
  3293. case PROP_ML: st->ml = dcl->val; break;
  3294. case PROP_PT: st->pt = dcl->val; break;
  3295. case PROP_PR: st->pr = dcl->val; break;
  3296. case PROP_PB: st->pb = dcl->val; break;
  3297. case PROP_PL: st->pl = dcl->val; break;
  3298. case PROP_BORDER_W:
  3299. st->border_w = (unsigned char)dcl->val;
  3300. if (st->border_w && !st->border_color)
  3301. st->border_color = st->color;
  3302. break;
  3303. case PROP_BORDER_COLOR:
  3304. st->border_color = (unsigned short)dcl->val;
  3305. break;
  3306. case PROP_RADIUS_TL:
  3307. case PROP_RADIUS_TR:
  3308. case PROP_RADIUS_BR:
  3309. case PROP_RADIUS_BL:
  3310. st->radius[dcl->prop - PROP_RADIUS_TL] =
  3311. (unsigned char)dcl->val;
  3312. break;
  3313. case PROP_LIST_NONE:
  3314. st->no_bullet = (unsigned char)(dcl->val != 0);
  3315. break;
  3316. case PROP_WIDTH:
  3317. st->width = (unsigned short)dcl->val;
  3318. break;
  3319. case PROP_WIDTH_KIND:
  3320. st->width_kind = (unsigned char)dcl->val;
  3321. break;
  3322. case PROP_MAXW:
  3323. st->max_width = (unsigned short)dcl->val;
  3324. break;
  3325. case PROP_MAXW_KIND:
  3326. st->max_width_kind = (unsigned char)dcl->val;
  3327. break;
  3328. case PROP_HEIGHT:
  3329. st->height = (unsigned short)dcl->val;
  3330. break;
  3331. case PROP_HEIGHT_KIND:
  3332. st->height_kind = (unsigned char)dcl->val;
  3333. break;
  3334. case PROP_LH_PCT:
  3335. st->line_h_pct = (unsigned char)dcl->val;
  3336. break;
  3337. case PROP_LH_PX:
  3338. st->line_h_px = (unsigned short)dcl->val;
  3339. break;
  3340. case PROP_UPPER:
  3341. st->uppercase = (unsigned char)(dcl->val != 0);
  3342. break;
  3343. case PROP_FLOAT:
  3344. st->float_left = (unsigned char)dcl->val;
  3345. break;
  3346. case PROP_FLEX_GROW:
  3347. st->flex_grow = (unsigned char)dcl->val;
  3348. break;
  3349. case PROP_FLEX_COL:
  3350. st->flex_col = (unsigned char)(dcl->val != 0);
  3351. break;
  3352. case PROP_FLEX_WRAP:
  3353. st->flex_wrap = (unsigned char)(dcl->val != 0);
  3354. break;
  3355. case PROP_GRID_NCOLS:
  3356. st->grid_ncols = (unsigned char)dcl->val;
  3357. break;
  3358. case PROP_GRID_GAP:
  3359. st->grid_gap = (unsigned char)dcl->val;
  3360. break;
  3361. case PROP_GRID_SLOT:
  3362. st->grid_slot = (unsigned char)dcl->val;
  3363. break;
  3364. case PROP_GRID_AUTO:
  3365. st->grid_auto = (unsigned char)(dcl->val != 0);
  3366. break;
  3367. case PROP_POS:
  3368. st->pos = (unsigned char)dcl->val;
  3369. break;
  3370. case PROP_LEFT:
  3371. st->left = dcl->val;
  3372. break;
  3373. case PROP_TOP:
  3374. st->top = dcl->val;
  3375. break;
  3376. case PROP_LEFT_SET:
  3377. st->left_set = (unsigned char)(dcl->val != 0);
  3378. break;
  3379. case PROP_TOP_SET:
  3380. st->top_set = (unsigned char)(dcl->val != 0);
  3381. break;
  3382. default:
  3383. break;
  3384. }
  3385. }
  3386. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  3387. int spec, short *best, short *best_imp)
  3388. {
  3389. unsigned char p = dcl->prop;
  3390. if (p >= PROP_COUNT)
  3391. return;
  3392. if (dcl->important) {
  3393. if (spec >= best_imp[p]) {
  3394. apply_decl(st, dcl);
  3395. best_imp[p] = (short)spec;
  3396. }
  3397. } else if (best_imp[p] < 0 && spec >= best[p]) {
  3398. apply_decl(st, dcl);
  3399. best[p] = (short)spec;
  3400. }
  3401. }
  3402. void br_style_for(struct br_doc *d, unsigned int node,
  3403. const struct br_style *parent, struct br_style *out)
  3404. {
  3405. unsigned int tag;
  3406. short best[PROP_COUNT], best_imp[PROP_COUNT];
  3407. unsigned int i;
  3408. if (d->nodes[node].style_valid) {
  3409. *out = d->nodes[node].style;
  3410. return;
  3411. }
  3412. *out = *parent;
  3413. out->has_bg = 0;
  3414. out->float_left = 0;
  3415. out->border_w = 0;
  3416. out->border_color = 0;
  3417. for (i = 0; i < 4; i++)
  3418. out->radius[i] = 0;
  3419. out->width = out->max_width = out->height = 0;
  3420. out->width_kind = out->max_width_kind = out->height_kind = 0;
  3421. out->mt = out->mr = out->mb = out->ml = 0;
  3422. out->pt = out->pr = out->pb = out->pl = 0;
  3423. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  3424. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  3425. out->grid_auto = 0;
  3426. out->pos = out->left_set = out->top_set = 0;
  3427. out->left = out->top = 0;
  3428. out->vis_hidden = out->opacity_hidden = 0;
  3429. out->overflow_hidden = 0;
  3430. tag = d->nodes[node].tag;
  3431. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  3432. BR_DISPLAY_INLINE;
  3433. for (i = 0; i < PROP_COUNT; i++) {
  3434. best[i] = -1;
  3435. best_imp[i] = -1;
  3436. }
  3437. switch (tag) {
  3438. case BR_TAG_HEAD:
  3439. case BR_TAG_TITLE:
  3440. case BR_TAG_META:
  3441. case BR_TAG_LINK:
  3442. case BR_TAG_BASE:
  3443. case BR_TAG_SCRIPT:
  3444. case BR_TAG_STYLE:
  3445. out->display = BR_DISPLAY_NONE;
  3446. break;
  3447. case BR_TAG_BODY:
  3448. out->mt = out->mb = out->ml = out->mr = 4;
  3449. break;
  3450. case BR_TAG_P:
  3451. out->mt = out->mb = 8;
  3452. break;
  3453. case BR_TAG_H1:
  3454. out->font_px = 32; out->bold = 1;
  3455. out->mt = 10; out->mb = 6;
  3456. break;
  3457. case BR_TAG_H2:
  3458. out->font_px = 24; out->bold = 1;
  3459. out->mt = 8; out->mb = 4;
  3460. break;
  3461. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  3462. out->bold = 1; out->mt = 6; out->mb = 3;
  3463. if (tag == BR_TAG_H3)
  3464. out->font_px = 19;
  3465. else if (tag == BR_TAG_H4)
  3466. out->font_px = 17;
  3467. else if (tag == BR_TAG_H5)
  3468. out->font_px = 15;
  3469. else
  3470. out->font_px = 13;
  3471. break;
  3472. case BR_TAG_UL: case BR_TAG_OL:
  3473. out->mt = out->mb = 6; out->pl = 20;
  3474. break;
  3475. case BR_TAG_LI:
  3476. out->mt = out->mb = 2;
  3477. break;
  3478. case BR_TAG_BLOCKQUOTE:
  3479. out->mt = out->mb = 6; out->ml = out->mr = 16;
  3480. break;
  3481. case BR_TAG_PRE:
  3482. out->mt = out->mb = 8; out->pl = 8;
  3483. out->has_bg = 1; out->bg = theme_panel;
  3484. out->mono = 1;
  3485. break;
  3486. case BR_TAG_CENTER:
  3487. out->align = BR_ALIGN_CENTER;
  3488. break;
  3489. case BR_TAG_A:
  3490. out->color = theme_link; out->underline = 1;
  3491. break;
  3492. case BR_TAG_B: case BR_TAG_STRONG:
  3493. out->bold = 1;
  3494. break;
  3495. case BR_TAG_I: case BR_TAG_EM:
  3496. out->italic = 1;
  3497. break;
  3498. case BR_TAG_CODE:
  3499. out->color = 0x8010;
  3500. out->mono = 1;
  3501. break;
  3502. case BR_TAG_SMALL:
  3503. out->font_px = 13;
  3504. break;
  3505. case BR_TAG_BIG:
  3506. out->font_px = 20;
  3507. break;
  3508. case BR_TAG_TD: case BR_TAG_TH:
  3509. out->pr = 12;
  3510. break;
  3511. case BR_TAG_VIDEO:
  3512. out->display = BR_DISPLAY_BLOCK;
  3513. break;
  3514. default:
  3515. break;
  3516. }
  3517. /* presentational <font color>: author CSS below still wins */
  3518. if (d->nodes[node].color_off) {
  3519. unsigned short c;
  3520. const char *v = attr_ptr(d, d->nodes[node].color_off);
  3521. if (parse_color(v, s_len(v), &c))
  3522. out->color = c;
  3523. }
  3524. rule_collect(d, node);
  3525. for (i = 0; i < nrule_matches; i++) {
  3526. struct br_rule *r = &d->rules[rule_matches[i].idx];
  3527. int spec = rule_matches[i].spec;
  3528. unsigned int k;
  3529. for (k = 0; k < r->nd; k++)
  3530. cascade_decl(out, &r->d[k], spec, best, best_imp);
  3531. }
  3532. if (d->nodes[node].style_off) {
  3533. struct br_decl decls[BR_DECLS];
  3534. unsigned int nd, k;
  3535. const char *v = attr_ptr(d, d->nodes[node].style_off);
  3536. parse_decls(d, v, s_len(v), decls, &nd);
  3537. for (k = 0; k < nd; k++)
  3538. cascade_decl(out, &decls[k], 1000, best, best_imp);
  3539. }
  3540. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  3541. out->display = BR_DISPLAY_NONE;
  3542. if (out->vis_hidden || out->opacity_hidden ||
  3543. (out->overflow_hidden && out->height_kind == 1 &&
  3544. out->height == 0))
  3545. out->display = BR_DISPLAY_NONE;
  3546. d->nodes[node].style = *out;
  3547. d->nodes[node].style_valid = 1;
  3548. }
  3549. /* ---- HTML parser ---- */
  3550. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  3551. unsigned int sp, int *pre)
  3552. {
  3553. unsigned int i;
  3554. *pre = 0;
  3555. for (i = 1; i <= sp; i++)
  3556. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  3557. (*pre)++;
  3558. }
  3559. static void implicit_close(struct br_doc *d, unsigned short *stack,
  3560. unsigned int *sp, unsigned int tag)
  3561. {
  3562. while (*sp > 0) {
  3563. unsigned int top = d->nodes[stack[*sp]].tag;
  3564. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  3565. (*sp)--;
  3566. continue;
  3567. }
  3568. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  3569. (*sp)--;
  3570. continue;
  3571. }
  3572. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  3573. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  3574. (*sp)--;
  3575. continue;
  3576. }
  3577. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  3578. (*sp)--;
  3579. continue;
  3580. }
  3581. break;
  3582. }
  3583. }
  3584. static int has_attr_token(const char *s, unsigned int len, const char *name)
  3585. {
  3586. unsigned int i, n = s_len(name);
  3587. for (i = 0; i + n <= len; i++) {
  3588. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  3589. continue;
  3590. if (!ci_eq_n(s + i, name, n))
  3591. continue;
  3592. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  3593. return 1;
  3594. }
  3595. return 0;
  3596. }
  3597. static int stack_has_svg(const struct br_doc *d,
  3598. const unsigned short *stack, unsigned int sp)
  3599. {
  3600. unsigned int k;
  3601. for (k = 0; k <= sp && k < 64; k++)
  3602. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  3603. return 1;
  3604. return 0;
  3605. }
  3606. /* Skip an element subtree by finding its closing tag. Used for SVG
  3607. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  3608. * overwrite the document title. */
  3609. static unsigned int skip_element(const char *html, unsigned int len,
  3610. unsigned int i, const char *name,
  3611. unsigned int n)
  3612. {
  3613. char close[40];
  3614. unsigned int k = 0, p;
  3615. close[k++] = '<';
  3616. close[k++] = '/';
  3617. while (k < sizeof(close) - 1 && n--)
  3618. close[k++] = *name++;
  3619. p = (unsigned int)find_ci(html, len, close, k, i);
  3620. if ((int)p < 0)
  3621. return len;
  3622. p += k;
  3623. while (p < len && html[p] != '>')
  3624. p++;
  3625. if (p < len)
  3626. p++;
  3627. return p;
  3628. }
  3629. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  3630. {
  3631. unsigned short stack[64];
  3632. unsigned int sp = 0;
  3633. unsigned int i = 0;
  3634. int pre = 0;
  3635. unsigned int css_len = 0;
  3636. if (!d)
  3637. return -1;
  3638. if (!d->nodes)
  3639. br_doc_init(d);
  3640. doc_reset(d);
  3641. stack[0] = 0;
  3642. d->nodes[0].type = BR_NODE_ELEM;
  3643. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3644. while (i < len) {
  3645. if (html[i] != '<') {
  3646. unsigned int st = i;
  3647. while (i < len && html[i] != '<')
  3648. i++;
  3649. if (sp <= 63)
  3650. append_text(d, stack[sp], html + st, i - st, pre);
  3651. continue;
  3652. }
  3653. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  3654. html[i + 3] == '-') {
  3655. int p = find_ci(html, len, "-->", 3, i + 4);
  3656. i = p >= 0 ? (unsigned int)p + 3 : len;
  3657. continue;
  3658. }
  3659. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  3660. while (i < len && html[i] != '>')
  3661. i++;
  3662. if (i < len)
  3663. i++;
  3664. continue;
  3665. }
  3666. if (i + 1 < len && html[i + 1] == '/') {
  3667. unsigned int ns = i + 2, ne, tag, k, n;
  3668. char name[32];
  3669. while (ns < len && is_space(html[ns]))
  3670. ns++;
  3671. ne = ns;
  3672. while (ne < len && is_ident(html[ne]))
  3673. ne++;
  3674. if (ne == ns) {
  3675. i++;
  3676. continue;
  3677. }
  3678. n = ne - ns;
  3679. if (n >= sizeof(name))
  3680. n = sizeof(name) - 1;
  3681. for (k = 0; k < n; k++)
  3682. name[k] = html[ns + k];
  3683. name[n] = '\0';
  3684. tag = br_tag_from_name(name);
  3685. i = ne;
  3686. while (i < len && html[i] != '>')
  3687. i++;
  3688. if (i < len)
  3689. i++;
  3690. for (k = sp; k > 0; k--) {
  3691. struct br_node *nd = &d->nodes[stack[k]];
  3692. if (tag == BR_TAG_UNKNOWN) {
  3693. const char *raw = attr_ptr(d, nd->name_off);
  3694. if (nd->tag == BR_TAG_UNKNOWN &&
  3695. ci_eq_n(raw, name, n) &&
  3696. raw[n] == '\0') {
  3697. sp = k - 1;
  3698. break;
  3699. }
  3700. } else if (nd->tag == tag) {
  3701. sp = k - 1;
  3702. break;
  3703. }
  3704. }
  3705. stack_recount_pre(d, stack, sp, &pre);
  3706. continue;
  3707. }
  3708. /* start tag */
  3709. {
  3710. unsigned int ns = i + 1, ne, tag, k, n;
  3711. char name[16];
  3712. unsigned int attr_start, attr_end;
  3713. int selfclose = 0;
  3714. while (ns < len && is_space(html[ns]))
  3715. ns++;
  3716. ne = ns;
  3717. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  3718. ne++;
  3719. if (ne == ns) {
  3720. /* stray '<' */
  3721. append_text(d, stack[sp], html + i, 1, pre);
  3722. i++;
  3723. continue;
  3724. }
  3725. n = ne - ns;
  3726. if (n >= sizeof(name))
  3727. n = sizeof(name) - 1;
  3728. for (k = 0; k < n; k++)
  3729. name[k] = html[ns + k];
  3730. name[n] = '\0';
  3731. tag = br_tag_from_name(name);
  3732. attr_start = ne;
  3733. {
  3734. unsigned int q = ne;
  3735. char quote = 0;
  3736. while (q < len) {
  3737. char c = html[q];
  3738. if (quote) {
  3739. if (c == quote)
  3740. quote = 0;
  3741. } else if (c == '"' || c == '\'') {
  3742. quote = c;
  3743. } else if (c == '>') {
  3744. break;
  3745. }
  3746. q++;
  3747. }
  3748. attr_end = q;
  3749. if (q > attr_start && html[q - 1] == '/')
  3750. selfclose = 1;
  3751. i = q < len ? q + 1 : len;
  3752. }
  3753. if (tag == BR_TAG_SCRIPT) {
  3754. int p = find_ci(html, len, "</script", 8, i);
  3755. i = p >= 0 ? (unsigned int)p + 8 : len;
  3756. while (i < len && html[i] != '>')
  3757. i++;
  3758. if (i < len)
  3759. i++;
  3760. continue;
  3761. }
  3762. if (stack_has_svg(d, stack, sp) &&
  3763. (tag == BR_TAG_TITLE ||
  3764. (tag == BR_TAG_UNKNOWN &&
  3765. (ci_eq_n(name, "desc", 4) ||
  3766. ci_eq_n(name, "metadata", 8))))) {
  3767. i = skip_element(html, len, i, name, n);
  3768. continue;
  3769. }
  3770. if (tag == BR_TAG_DETAILS && !selfclose &&
  3771. !has_attr_token(html + attr_start,
  3772. attr_end - attr_start, "open")) {
  3773. /* Closed <details> is collapsed (and this browser has
  3774. * no script to open it); skipping the subtree keeps
  3775. * mobile mega-menus out of the node arena. */
  3776. int p = find_ci(html, len, "</details>", 10, i);
  3777. i = p >= 0 ? (unsigned int)p + 10 : len;
  3778. continue;
  3779. }
  3780. if (tag == BR_TAG_STYLE) {
  3781. int p = find_ci(html, len, "</style", 7, i);
  3782. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3783. unsigned int cend = end;
  3784. while (cend > i && is_space(html[cend - 1]))
  3785. cend--;
  3786. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  3787. for (k = i; k < cend; k++)
  3788. css_store[css_len++] = html[k];
  3789. css_store[css_len++] = '\n';
  3790. }
  3791. i = p >= 0 ? (unsigned int)p + 7 : len;
  3792. while (i < len && html[i] != '>')
  3793. i++;
  3794. if (i < len)
  3795. i++;
  3796. continue;
  3797. }
  3798. if (tag == BR_TAG_TITLE) {
  3799. int p = find_ci(html, len, "</title", 7, i);
  3800. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3801. unsigned int tn = 0;
  3802. char tmp[BR_TITLE_MAX];
  3803. tn = decode_text(html + i, end - i, tmp,
  3804. sizeof(tmp), 1);
  3805. (void)tn;
  3806. s_copy(d->title, tmp, sizeof(d->title));
  3807. i = p >= 0 ? (unsigned int)p + 7 : len;
  3808. while (i < len && html[i] != '>')
  3809. i++;
  3810. if (i < len)
  3811. i++;
  3812. continue;
  3813. }
  3814. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  3815. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  3816. unsigned int idx;
  3817. implicit_close(d, stack, &sp, tag);
  3818. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  3819. (unsigned char)tag);
  3820. if (idx)
  3821. parse_attrs(d, &d->nodes[idx],
  3822. html + attr_start,
  3823. attr_end - attr_start);
  3824. continue;
  3825. }
  3826. if (sp >= 63) {
  3827. d->truncated = 1;
  3828. continue;
  3829. }
  3830. implicit_close(d, stack, &sp, tag);
  3831. {
  3832. unsigned int idx = node_append(d, stack[sp],
  3833. BR_NODE_ELEM,
  3834. (unsigned char)tag);
  3835. if (!idx)
  3836. continue;
  3837. parse_attrs(d, &d->nodes[idx],
  3838. html + attr_start,
  3839. attr_end - attr_start);
  3840. if (tag == BR_TAG_UNKNOWN)
  3841. d->nodes[idx].name_off =
  3842. store_attr(d, name, n, 31);
  3843. if (!selfclose) {
  3844. sp++;
  3845. stack[sp] = (unsigned short)idx;
  3846. if (tag == BR_TAG_PRE)
  3847. pre++;
  3848. }
  3849. }
  3850. }
  3851. }
  3852. d->inline_css_len = css_len;
  3853. return 0;
  3854. }
  3855. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  3856. {
  3857. if (!d->nodes)
  3858. br_doc_init(d);
  3859. doc_reset(d);
  3860. d->plain = 1;
  3861. d->nodes[0].type = BR_NODE_ELEM;
  3862. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3863. append_text(d, 0, text, len, 1);
  3864. return 0;
  3865. }
  3866. /* A standalone image response: a synthetic document with one <img>. The
  3867. * caller decodes the already-fetched body into image slot 0 and sets the
  3868. * returned node's img_index. */
  3869. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  3870. {
  3871. unsigned int idx;
  3872. if (!d->nodes)
  3873. br_doc_init(d);
  3874. doc_reset(d);
  3875. d->nodes[0].type = BR_NODE_ELEM;
  3876. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3877. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  3878. if (!idx)
  3879. return 0;
  3880. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  3881. return idx;
  3882. }