browser_html.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537
  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. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  32. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  33. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  34. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  35. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  36. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  37. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  38. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  39. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  40. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  41. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  42. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  43. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  44. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  45. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  46. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  47. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  48. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  49. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  50. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  51. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  52. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  53. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  54. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  55. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  56. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  57. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  58. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  59. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  60. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  61. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  62. { 0, 0 }
  63. };
  64. const char *br_tag_name(unsigned int tag)
  65. {
  66. unsigned int i;
  67. for (i = 0; tag_names[i].name; i++)
  68. if (tag_names[i].tag == tag)
  69. return tag_names[i].name;
  70. return "?";
  71. }
  72. static unsigned int s_len(const char *s);
  73. #define TAG_HASH_SIZE 128
  74. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  75. static int tag_hash_ready;
  76. static unsigned int tag_hash_key(const char *name, unsigned int len)
  77. {
  78. unsigned int i, h = 2166136261u;
  79. for (i = 0; i < len; i++) {
  80. h ^= (unsigned char)name[i];
  81. h *= 16777619u;
  82. }
  83. return h & (TAG_HASH_SIZE - 1);
  84. }
  85. static void tag_hash_build(void)
  86. {
  87. unsigned int i;
  88. for (i = 0; i < TAG_HASH_SIZE; i++)
  89. tag_hash_idx[i] = 0xFF;
  90. for (i = 0; tag_names[i].name; i++) {
  91. unsigned int h = tag_hash_key(tag_names[i].name,
  92. s_len(tag_names[i].name));
  93. while (tag_hash_idx[h] != 0xFF)
  94. h = (h + 1) & (TAG_HASH_SIZE - 1);
  95. tag_hash_idx[h] = (unsigned char)i;
  96. }
  97. tag_hash_ready = 1;
  98. }
  99. unsigned int br_tag_from_name(const char *name)
  100. {
  101. char lower[16];
  102. unsigned int i, len, h;
  103. unsigned char idx;
  104. for (i = 0; i < 15 && name[i]; i++) {
  105. char c = name[i];
  106. if (c >= 'A' && c <= 'Z')
  107. c = (char)(c - 'A' + 'a');
  108. lower[i] = c;
  109. }
  110. lower[i] = '\0';
  111. len = i;
  112. if (!tag_hash_ready)
  113. tag_hash_build();
  114. h = tag_hash_key(lower, len);
  115. for (;;) {
  116. idx = tag_hash_idx[h];
  117. if (idx == 0xFF)
  118. return BR_TAG_UNKNOWN;
  119. {
  120. const char *n = tag_names[idx].name;
  121. unsigned int j;
  122. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  123. ;
  124. if (!lower[j] && !n[j])
  125. return tag_names[idx].tag;
  126. }
  127. h = (h + 1) & (TAG_HASH_SIZE - 1);
  128. }
  129. }
  130. int br_tag_is_void(unsigned int tag)
  131. {
  132. switch (tag) {
  133. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  134. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  135. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  136. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  137. case BR_TAG_AREA: case BR_TAG_COL:
  138. return 1;
  139. default:
  140. return 0;
  141. }
  142. }
  143. int br_tag_is_block(unsigned int tag)
  144. {
  145. switch (tag) {
  146. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  147. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  148. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  149. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  150. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  151. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  152. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  153. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  154. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  155. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  156. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  157. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  158. case BR_TAG_ADDRESS: case BR_TAG_HR:
  159. return 1;
  160. default:
  161. return 0;
  162. }
  163. }
  164. /* ---- string / entity helpers ---- */
  165. static unsigned int s_len(const char *s)
  166. {
  167. unsigned int n = 0;
  168. while (s && s[n])
  169. n++;
  170. return n;
  171. }
  172. static void s_copy(char *d, const char *s, unsigned int cap)
  173. {
  174. unsigned int i = 0;
  175. if (!cap)
  176. return;
  177. while (s && s[i] && i + 1 < cap) {
  178. d[i] = s[i];
  179. i++;
  180. }
  181. d[i] = '\0';
  182. }
  183. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  184. {
  185. unsigned int i;
  186. for (i = 0; i < n; i++) {
  187. char ca = a[i], cb = b[i];
  188. if (ca >= 'A' && ca <= 'Z')
  189. ca = (char)(ca - 'A' + 'a');
  190. if (cb >= 'A' && cb <= 'Z')
  191. cb = (char)(cb - 'A' + 'a');
  192. if (ca != cb)
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. static int find_ci(const char *hay, unsigned int len, const char *needle,
  198. unsigned int nlen, unsigned int from)
  199. {
  200. unsigned int i;
  201. if (nlen == 0 || from >= len)
  202. return -1;
  203. for (i = from; i + nlen <= len; i++)
  204. if (ci_eq_n(hay + i, needle, nlen))
  205. return (int)i;
  206. return -1;
  207. }
  208. static const struct {
  209. const char *name;
  210. const char *text;
  211. } entities[] = {
  212. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  213. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  214. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  215. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  216. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  217. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  218. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  219. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  220. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  221. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  222. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  223. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  224. { 0, 0 }
  225. };
  226. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  227. {
  228. if (*n + 1 < cap)
  229. out[(*n)++] = c;
  230. }
  231. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  232. char *out, unsigned int cap, unsigned int *n)
  233. {
  234. unsigned int start = *pos + 1, i = start;
  235. unsigned int code = 0;
  236. int ok = 0;
  237. while (i < len && s[i] != ';' && i - start < 12)
  238. i++;
  239. if (i >= len || s[i] != ';') {
  240. /* Not an entity: emit the '&' and advance past it. Leaving
  241. * *pos unchanged here used to spin forever on bare '&'. */
  242. emit(out, cap, n, '&');
  243. *pos = start;
  244. return;
  245. }
  246. if (s[start] == '#') {
  247. unsigned int j = start + 1;
  248. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  249. j++;
  250. for (; j < i; j++) {
  251. char c = s[j];
  252. int d = -1;
  253. if (c >= '0' && c <= '9') d = c - '0';
  254. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  255. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  256. if (d < 0) break;
  257. code = code * 16 + (unsigned int)d;
  258. ok = 1;
  259. }
  260. } else {
  261. for (; j < i; j++) {
  262. if (s[j] < '0' || s[j] > '9')
  263. break;
  264. code = code * 10 + (unsigned int)(s[j] - '0');
  265. ok = 1;
  266. }
  267. }
  268. } else {
  269. unsigned int k;
  270. for (k = 0; entities[k].name; k++) {
  271. unsigned int el = s_len(entities[k].name);
  272. unsigned int j;
  273. if (el != i - start)
  274. continue;
  275. for (j = 0; j < el; j++)
  276. if (s[start + j] != entities[k].name[j])
  277. break;
  278. if (j == el) {
  279. const char *t = entities[k].text;
  280. while (*t)
  281. emit(out, cap, n, *t++);
  282. *pos = i + 1;
  283. return;
  284. }
  285. }
  286. }
  287. if (ok && code) {
  288. if (code == 0x00A0)
  289. emit(out, cap, n, ' ');
  290. else if (code == 0x2013 || code == 0x2014)
  291. emit(out, cap, n, '-');
  292. else if (code == 0x2018 || code == 0x2019)
  293. emit(out, cap, n, '\'');
  294. else if (code == 0x201C || code == 0x201D)
  295. emit(out, cap, n, '"');
  296. else if (code == 0x2026) {
  297. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  298. emit(out, cap, n, '.');
  299. } else if (code <= 0xFF) {
  300. emit(out, cap, n, (char)code);
  301. } else {
  302. emit(out, cap, n, '?');
  303. }
  304. } else {
  305. emit(out, cap, n, '?');
  306. }
  307. *pos = i + 1;
  308. }
  309. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  310. * space and drops leading spaces. Returns the output length. */
  311. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  312. unsigned int cap, int collapse)
  313. {
  314. unsigned int i = 0, n = 0;
  315. while (i < len) {
  316. unsigned char c = (unsigned char)s[i];
  317. if (c == '&') {
  318. decode_entity(s, len, &i, out, cap, &n);
  319. continue;
  320. }
  321. if (c < 0x80) {
  322. i++;
  323. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  324. c == '\r' || c == '\f')) {
  325. if (n > 0 && out[n - 1] == ' ')
  326. continue;
  327. emit(out, cap, &n, ' ');
  328. continue;
  329. }
  330. if (!collapse && c == '\t') {
  331. emit(out, cap, &n, ' ');
  332. emit(out, cap, &n, ' ');
  333. emit(out, cap, &n, ' ');
  334. emit(out, cap, &n, ' ');
  335. continue;
  336. }
  337. emit(out, cap, &n, (char)c);
  338. continue;
  339. }
  340. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  341. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  342. unsigned int cp = ((c & 0x1F) << 6) |
  343. ((unsigned char)s[i + 1] & 0x3F);
  344. i += 2;
  345. if (cp == 0xA0)
  346. cp = ' ';
  347. if (cp <= 0xFF)
  348. emit(out, cap, &n, (char)cp);
  349. else
  350. emit(out, cap, &n, '?');
  351. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  352. unsigned int cp = ((c & 0x0F) << 12) |
  353. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  354. ((unsigned char)s[i + 2] & 0x3F);
  355. i += 3;
  356. if (cp == 0x2013 || cp == 0x2014) {
  357. emit(out, cap, &n, '-');
  358. } else if (cp == 0x2018 || cp == 0x2019) {
  359. emit(out, cap, &n, '\'');
  360. } else if (cp == 0x201C || cp == 0x201D) {
  361. emit(out, cap, &n, '"');
  362. } else if (cp == 0x2026) {
  363. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  364. emit(out, cap, &n, '.');
  365. } else {
  366. emit(out, cap, &n, '?');
  367. }
  368. } else {
  369. i++;
  370. emit(out, cap, &n, '?');
  371. }
  372. }
  373. out[n] = '\0';
  374. return n;
  375. }
  376. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  377. * space / empty. */
  378. static unsigned int store_attr(struct br_doc *d, const char *s,
  379. unsigned int len, unsigned int limit)
  380. {
  381. unsigned int n;
  382. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  383. return 0;
  384. if (len > limit)
  385. len = limit;
  386. n = decode_text(s, len, d->attrs + d->attrs_used,
  387. BR_ATTR_SIZE - d->attrs_used, 0);
  388. if (n == 0)
  389. return 0;
  390. {
  391. unsigned int off = d->attrs_used;
  392. d->attrs_used += n + 1;
  393. return off;
  394. }
  395. }
  396. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  397. {
  398. return off ? d->attrs + off : "";
  399. }
  400. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  401. static unsigned int store_raw(struct br_doc *d, const char *s,
  402. unsigned int len, unsigned int limit)
  403. {
  404. unsigned int off;
  405. if (len > limit)
  406. len = limit;
  407. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  408. return 0;
  409. off = d->attrs_used;
  410. {
  411. unsigned int i;
  412. for (i = 0; i < len; i++)
  413. d->attrs[off + i] = s[i];
  414. }
  415. d->attrs[off + len] = '\0';
  416. d->attrs_used += len + 1;
  417. return off;
  418. }
  419. #define VAR_HASH_SIZE 1024
  420. static unsigned int var_hash(const char *name, unsigned int len)
  421. {
  422. unsigned int i, h = 2166136261u;
  423. for (i = 0; i < len; i++) {
  424. h ^= (unsigned char)name[i];
  425. h *= 16777619u;
  426. }
  427. return h & (VAR_HASH_SIZE - 1);
  428. }
  429. static unsigned int css_var_find(struct br_doc *d, const char *name,
  430. unsigned int nlen)
  431. {
  432. unsigned int h = var_hash(name, nlen);
  433. for (;;) {
  434. unsigned int i = d->var_slot[h];
  435. if (!i)
  436. return BR_MAX_VARS;
  437. i--;
  438. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  439. const char *n = attr_ptr(d, d->var_name[i]);
  440. unsigned int j;
  441. for (j = 0; j < nlen && n[j] == name[j]; j++)
  442. ;
  443. if (j == nlen)
  444. return i;
  445. }
  446. h = (h + 1) & (VAR_HASH_SIZE - 1);
  447. }
  448. }
  449. static void css_var_set(struct br_doc *d, const char *name,
  450. unsigned int nlen, const char *val, unsigned int vlen)
  451. {
  452. unsigned int i, noff, voff;
  453. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  454. return;
  455. noff = store_raw(d, name, nlen, 63);
  456. voff = store_raw(d, val, vlen, 199);
  457. if (!noff || !voff)
  458. return;
  459. i = css_var_find(d, name, nlen);
  460. if (i >= BR_MAX_VARS) {
  461. unsigned int h;
  462. if (d->nvars >= BR_MAX_VARS)
  463. return;
  464. i = d->nvars++;
  465. d->var_name[i] = noff;
  466. h = var_hash(name, nlen);
  467. while (d->var_slot[h])
  468. h = (h + 1) & (VAR_HASH_SIZE - 1);
  469. d->var_slot[h] = (unsigned short)(i + 1);
  470. }
  471. d->var_val[i] = voff;
  472. }
  473. /* Find the end of a declaration value: the next ';' outside quotes and
  474. * parentheses. The caller passes the position just after the ':'. */
  475. static unsigned int css_value_end(const char *s, unsigned int len,
  476. unsigned int i)
  477. {
  478. int depth = 0;
  479. char quote = 0;
  480. while (i < len) {
  481. char c = s[i];
  482. if (quote) {
  483. if (c == quote)
  484. quote = 0;
  485. else if (c == '\\' && i + 1 < len)
  486. i++;
  487. } else if (c == '"' || c == '\'') {
  488. quote = c;
  489. } else if (c == '(') {
  490. depth++;
  491. } else if (c == ')' && depth > 0) {
  492. depth--;
  493. } else if (c == ';' && depth == 0) {
  494. break;
  495. }
  496. i++;
  497. }
  498. return i;
  499. }
  500. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  501. * second palette. We render the light page, so skip their custom-property
  502. * definitions; otherwise their last-wins values leak into the light rules. */
  503. static int css_dark_selector(const char *s, unsigned int len)
  504. {
  505. unsigned int i;
  506. for (i = 0; i + 4 <= len; i++) {
  507. if ((s[i] == 'n' || s[i] == 'N') &&
  508. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  509. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  510. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  511. (s[i + 4] == 't' || s[i + 4] == 'T'))
  512. return 1;
  513. if ((s[i] == 'd' || s[i] == 'D') &&
  514. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  515. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  516. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  517. return 1;
  518. if (i + 12 <= len &&
  519. (s[i] == 'c' || s[i] == 'C') &&
  520. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  521. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  522. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  523. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  524. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  525. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  526. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  527. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  528. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  529. s[i + 10] == '-' &&
  530. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  531. (s[i + 12] == 's' || s[i + 12] == 'S'))
  532. return 1;
  533. }
  534. return 0;
  535. }
  536. /* Collect --custom-property definitions from one declaration block. */
  537. static void css_collect_block(struct br_doc *d, const char *s,
  538. unsigned int len)
  539. {
  540. unsigned int i = 0;
  541. while (i < len) {
  542. unsigned int pstart, pend, vstart, vend;
  543. while (i < len && (is_space(s[i]) || s[i] == ';'))
  544. i++;
  545. if (i >= len)
  546. break;
  547. pstart = i;
  548. while (i < len && s[i] != ':' && s[i] != ';')
  549. i++;
  550. pend = i;
  551. if (i >= len || s[i] != ':') {
  552. if (i < len)
  553. i++;
  554. continue;
  555. }
  556. i++;
  557. vstart = i;
  558. i = css_value_end(s, len, i);
  559. vend = i;
  560. if (i < len)
  561. i++;
  562. while (pstart < pend && is_space(s[pstart]))
  563. pstart++;
  564. while (pend > pstart && is_space(s[pend - 1]))
  565. pend--;
  566. while (vstart < vend && is_space(s[vstart]))
  567. vstart++;
  568. while (vend > vstart && is_space(s[vend - 1]))
  569. vend--;
  570. if (vend - vstart >= 10 &&
  571. ci_eq_n(s + vend - 10, "!important", 10)) {
  572. vend -= 10;
  573. while (vend > vstart && is_space(s[vend - 1]))
  574. vend--;
  575. }
  576. if (pend - pstart >= 3 && s[pstart] == '-' &&
  577. s[pstart + 1] == '-')
  578. css_var_set(d, s + pstart, pend - pstart,
  579. s + vstart, vend - vstart);
  580. }
  581. }
  582. const char *br_href(struct br_doc *d, unsigned int node)
  583. {
  584. return attr_ptr(d, d->nodes[node].href_off);
  585. }
  586. const char *br_src(struct br_doc *d, unsigned int node)
  587. {
  588. return attr_ptr(d, d->nodes[node].src_off);
  589. }
  590. const char *br_alt(struct br_doc *d, unsigned int node)
  591. {
  592. return attr_ptr(d, d->nodes[node].alt_off);
  593. }
  594. const char *br_rel(struct br_doc *d, unsigned int node)
  595. {
  596. return attr_ptr(d, d->nodes[node].rel_off);
  597. }
  598. const char *br_value(struct br_doc *d, unsigned int node)
  599. {
  600. return attr_ptr(d, d->nodes[node].src_off);
  601. }
  602. const char *br_placeholder(struct br_doc *d, unsigned int node)
  603. {
  604. return attr_ptr(d, d->nodes[node].alt_off);
  605. }
  606. const char *br_type(struct br_doc *d, unsigned int node)
  607. {
  608. return attr_ptr(d, d->nodes[node].rel_off);
  609. }
  610. /* ---- document setup / node append ---- */
  611. static void rule_index_reset(struct br_doc *d)
  612. {
  613. unsigned int i;
  614. for (i = 0; i < BR_MAX_RULES; i++)
  615. d->rule_next[i] = 0xFFFF;
  616. for (i = 0; i < BR_RULE_HASH; i++)
  617. d->rule_bucket[i] = 0xFFFF;
  618. for (i = 0; i < BR_TAG_MAX; i++)
  619. d->rule_tag[i] = 0xFFFF;
  620. d->rule_any = 0xFFFF;
  621. }
  622. void br_doc_init(struct br_doc *d)
  623. {
  624. unsigned int i;
  625. for (i = 0; i < sizeof(*d); i++)
  626. ((unsigned char *)d)[i] = 0;
  627. rule_index_reset(d);
  628. d->nodes = nodes_store;
  629. d->text = text_store;
  630. d->attrs = attr_store;
  631. d->rules = rule_fallback;
  632. d->rule_cap = BR_RULE_FALLBACK;
  633. d->attrs_used = 1; /* offset 0 = "no attribute" */
  634. d->attrs[0] = '\0';
  635. d->nvars = 0;
  636. d->root = 0;
  637. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  638. d->var_slot[vi] = 0;
  639. root_font_px = 16;
  640. }
  641. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  642. unsigned int cap)
  643. {
  644. if (!buf || !cap) {
  645. d->rules = rule_fallback;
  646. d->rule_cap = BR_RULE_FALLBACK;
  647. d->nrules = 0;
  648. return;
  649. }
  650. if (cap > BR_MAX_RULES)
  651. cap = BR_MAX_RULES;
  652. d->rules = buf;
  653. d->rule_cap = cap;
  654. d->nrules = 0;
  655. }
  656. static void doc_reset(struct br_doc *d)
  657. {
  658. unsigned int i;
  659. d->nnode = 0;
  660. d->text_used = 0;
  661. d->attrs_used = 1;
  662. d->attrs[0] = '\0';
  663. d->nrules = 0;
  664. rule_index_reset(d);
  665. d->title[0] = '\0';
  666. d->inline_css_len = 0;
  667. d->nvars = 0;
  668. d->truncated = 0;
  669. d->plain = 0;
  670. for (i = 0; i < BR_MAX_NODES; i++) {
  671. d->nodes[i].type = BR_NODE_ELEM;
  672. d->nodes[i].tag = BR_TAG_UNKNOWN;
  673. d->nodes[i].flags = 0;
  674. d->nodes[i].parent = 0;
  675. d->nodes[i].first = d->nodes[i].last = 0;
  676. d->nodes[i].next = 0;
  677. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  678. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  679. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  680. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  681. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  682. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  683. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  684. d->nodes[i].img_index = -1;
  685. d->nodes[i].style_valid = 0;
  686. }
  687. }
  688. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  689. unsigned char type, unsigned char tag)
  690. {
  691. unsigned int idx;
  692. struct br_node *n;
  693. if (d->nnode >= BR_MAX_NODES) {
  694. d->truncated = 1;
  695. return 0;
  696. }
  697. idx = d->nnode++;
  698. n = &d->nodes[idx];
  699. n->type = type;
  700. n->tag = tag;
  701. n->parent = (unsigned short)parent;
  702. n->img_index = -1;
  703. if (d->nodes[parent].last)
  704. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  705. else
  706. d->nodes[parent].first = (unsigned short)idx;
  707. d->nodes[parent].last = (unsigned short)idx;
  708. return idx;
  709. }
  710. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  711. const char *s, unsigned int len, int pre)
  712. {
  713. char *dst;
  714. unsigned int n;
  715. unsigned int idx;
  716. if (len == 0)
  717. return 0;
  718. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  719. d->truncated = 1;
  720. return 0;
  721. }
  722. dst = d->text + d->text_used;
  723. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  724. if (n == 0) {
  725. d->text[d->text_used] = '\0';
  726. return 0;
  727. }
  728. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  729. if (!idx)
  730. return 0;
  731. d->nodes[idx].text_off = (unsigned short)d->text_used;
  732. d->nodes[idx].text_len = (unsigned short)n;
  733. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  734. d->text_used += n + 1;
  735. return idx;
  736. }
  737. /* ---- attribute parsing ---- */
  738. static int is_space(char c)
  739. {
  740. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  741. }
  742. static int str_eq(const char *a, const char *b);
  743. static unsigned int parse_uint(const char *s, unsigned int len)
  744. {
  745. unsigned int i = 0, v = 0;
  746. while (i < len && is_space(s[i]))
  747. i++;
  748. while (i < len && s[i] >= '0' && s[i] <= '9') {
  749. v = v * 10 + (unsigned int)(s[i] - '0');
  750. if (v > 4096)
  751. v = 4096;
  752. i++;
  753. }
  754. return v;
  755. }
  756. /* Parse attributes from `s` (between the tag name and '>'). */
  757. static void parse_attrs(struct br_doc *d, struct br_node *n,
  758. const char *s, unsigned int len)
  759. {
  760. unsigned int i = 0;
  761. while (i < len) {
  762. char name[16];
  763. unsigned int nl = 0, vstart, vlen;
  764. char quote = 0;
  765. while (i < len && is_space(s[i]))
  766. i++;
  767. if (i >= len || s[i] == '/')
  768. break;
  769. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  770. char c = s[i++];
  771. if (c >= 'A' && c <= 'Z')
  772. c = (char)(c - 'A' + 'a');
  773. if (nl + 1 < sizeof(name))
  774. name[nl++] = c;
  775. }
  776. name[nl] = '\0';
  777. while (i < len && is_space(s[i]))
  778. i++;
  779. if (i >= len || s[i] != '=') {
  780. if (str_eq(name, "hidden"))
  781. n->flags |= BR_NODE_HIDDEN;
  782. continue;
  783. }
  784. i++;
  785. while (i < len && is_space(s[i]))
  786. i++;
  787. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  788. quote = s[i++];
  789. vstart = i;
  790. while (i < len && s[i] != quote)
  791. i++;
  792. vlen = i - vstart;
  793. if (i < len)
  794. i++;
  795. } else {
  796. vstart = i;
  797. while (i < len && !is_space(s[i]) && s[i] != '>')
  798. i++;
  799. vlen = i - vstart;
  800. }
  801. if (!nl)
  802. continue;
  803. if (str_eq(name, "hidden")) {
  804. n->flags |= BR_NODE_HIDDEN;
  805. continue;
  806. }
  807. if (str_eq(name, "href"))
  808. n->href_off = store_attr(d, s + vstart, vlen, 480);
  809. else if (str_eq(name, "class"))
  810. n->class_off = store_attr(d, s + vstart, vlen, 120);
  811. else if (str_eq(name, "id"))
  812. n->id_off = store_attr(d, s + vstart, vlen, 64);
  813. else if (str_eq(name, "style"))
  814. n->style_off = store_attr(d, s + vstart, vlen, 240);
  815. else if (str_eq(name, "src"))
  816. n->src_off = store_attr(d, s + vstart, vlen, 400);
  817. else if (str_eq(name, "alt"))
  818. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  819. else if (str_eq(name, "color"))
  820. n->color_off = store_attr(d, s + vstart, vlen, 32);
  821. else if (str_eq(name, "rel"))
  822. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  823. else if (str_eq(name, "value"))
  824. n->src_off = store_attr(d, s + vstart, vlen, 220);
  825. else if (str_eq(name, "placeholder") &&
  826. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  827. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  828. else if (str_eq(name, "type") &&
  829. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  830. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  831. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  832. else if (str_eq(name, "width")) {
  833. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  834. if (vlen && s[vstart + vlen - 1] == '%')
  835. n->width_pct = 1;
  836. } else if (str_eq(name, "height")) {
  837. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  838. if (vlen && s[vstart + vlen - 1] == '%')
  839. n->height_pct = 1;
  840. }
  841. }
  842. }
  843. static int str_eq(const char *a, const char *b)
  844. {
  845. unsigned int i = 0;
  846. while (a[i] && b[i] && a[i] == b[i])
  847. i++;
  848. return a[i] == '\0' && b[i] == '\0';
  849. }
  850. /* ---- CSS ---- */
  851. enum {
  852. PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
  853. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  854. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  855. PROP_BORDER_W, PROP_BORDER_COLOR, PROP_LIST_NONE,
  856. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  857. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  858. PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  859. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT,
  860. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  861. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  862. PROP_COUNT
  863. };
  864. struct color_name {
  865. const char *name;
  866. unsigned short rgb;
  867. };
  868. static const struct color_name color_names[] = {
  869. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  870. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  871. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  872. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  873. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  874. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  875. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  876. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  877. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  878. { "darkgrey", 0x528A }, { 0, 0 }
  879. };
  880. static int hexval(char c)
  881. {
  882. if (c >= '0' && c <= '9')
  883. return c - '0';
  884. if (c >= 'a' && c <= 'f')
  885. return c - 'a' + 10;
  886. if (c >= 'A' && c <= 'F')
  887. return c - 'A' + 10;
  888. return -1;
  889. }
  890. static int named_color(const char *s, unsigned int len, unsigned short *out)
  891. {
  892. unsigned int i;
  893. for (i = 0; color_names[i].name; i++) {
  894. unsigned int j, n = s_len(color_names[i].name);
  895. if (n != len)
  896. continue;
  897. for (j = 0; j < n; j++) {
  898. char c = s[j];
  899. if (c >= 'A' && c <= 'Z')
  900. c = (char)(c - 'A' + 'a');
  901. if (c != color_names[i].name[j])
  902. break;
  903. }
  904. if (j == n) {
  905. *out = color_names[i].rgb;
  906. return 1;
  907. }
  908. }
  909. return 0;
  910. }
  911. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  912. * over white; the render target is a white page. */
  913. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  914. {
  915. unsigned int i = 0;
  916. while (i < len && is_space(s[i]))
  917. i++;
  918. if (i < len && s[i] == '#') {
  919. int d[6], nd = 0;
  920. i++;
  921. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  922. d[nd++] = hexval(s[i++]);
  923. if (nd == 3) {
  924. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  925. unsigned int b = (unsigned int)d[2];
  926. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  927. ((g * 17 >> 2) << 5) |
  928. (b * 17 >> 3));
  929. return 1;
  930. }
  931. if (nd >= 6) {
  932. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  933. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  934. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  935. *out = (unsigned short)(((r >> 3) << 11) |
  936. ((g >> 2) << 5) | (b >> 3));
  937. return 1;
  938. }
  939. return 0;
  940. }
  941. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  942. unsigned int v[3], k = 0, a = 255;
  943. int is_rgba = (i + 4 <= len && ci_eq_n(s + i, "rgba", 4));
  944. while (i < len && s[i] != '(')
  945. i++;
  946. if (i >= len)
  947. return 0;
  948. i++;
  949. while (k < 3 && i < len) {
  950. unsigned int x = 0;
  951. while (i < len && is_space(s[i]))
  952. i++;
  953. while (i < len && s[i] >= '0' && s[i] <= '9') {
  954. x = x * 10 + (unsigned int)(s[i] - '0');
  955. i++;
  956. }
  957. if (i < len && s[i] == '%') {
  958. x = x * 255 / 100;
  959. i++;
  960. }
  961. if (x > 255)
  962. x = 255;
  963. v[k++] = x;
  964. while (i < len && s[i] != ',' && s[i] != ')')
  965. i++;
  966. if (i < len && s[i] == ',')
  967. i++;
  968. }
  969. if (is_rgba && i < len) {
  970. unsigned int whole = 0, frac = 0, scale = 1;
  971. int percent = 0;
  972. while (i < len && is_space(s[i]))
  973. i++;
  974. while (i < len && s[i] >= '0' && s[i] <= '9') {
  975. whole = whole * 10 + (unsigned int)(s[i] - '0');
  976. i++;
  977. }
  978. if (i < len && s[i] == '.') {
  979. i++;
  980. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  981. frac = frac * 10 + (unsigned int)(s[i] - '0');
  982. scale *= 10;
  983. i++;
  984. }
  985. }
  986. while (i < len && is_space(s[i]))
  987. i++;
  988. if (i < len && s[i] == '%')
  989. percent = 1;
  990. if (percent) {
  991. a = whole > 100 ? 255 : whole * 255 / 100;
  992. } else {
  993. a = whole >= 1 ? 255 : (frac * 255) / scale;
  994. }
  995. if (a > 255)
  996. a = 255;
  997. }
  998. if (k == 3) {
  999. unsigned int r = v[0], g = v[1], b = v[2];
  1000. if (a < 255) {
  1001. r = (r * a + 255u * (255u - a)) / 255u;
  1002. g = (g * a + 255u * (255u - a)) / 255u;
  1003. b = (b * a + 255u * (255u - a)) / 255u;
  1004. }
  1005. *out = (unsigned short)(((r >> 3) << 11) |
  1006. ((g >> 2) << 5) | (b >> 3));
  1007. return 1;
  1008. }
  1009. return 0;
  1010. }
  1011. {
  1012. unsigned int j = i;
  1013. while (i < len && !is_space(s[i]))
  1014. i++;
  1015. return named_color(s + j, i - j, out);
  1016. }
  1017. }
  1018. /* Length values for margins/padding: px, ignore percentages/units. */
  1019. static unsigned int viewport_w;
  1020. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  1021. {
  1022. unsigned int i = *pos;
  1023. int sign = 1;
  1024. long v = 0; /* hundredths of a unit before conversion */
  1025. int digits = 0, frac = 0, unit = 0;
  1026. while (i < len && is_space(s[i]))
  1027. i++;
  1028. if (i < len && (s[i] == '-' || s[i] == '+')) {
  1029. if (s[i] == '-')
  1030. sign = -1;
  1031. i++;
  1032. }
  1033. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1034. v = v * 10 + (s[i] - '0');
  1035. digits = 1;
  1036. i++;
  1037. }
  1038. if (i < len && s[i] == '.') {
  1039. i++;
  1040. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1041. if (frac < 2) {
  1042. v = v * 10 + (s[i] - '0');
  1043. frac++;
  1044. }
  1045. i++;
  1046. }
  1047. }
  1048. while (frac < 2) {
  1049. v *= 10;
  1050. frac++;
  1051. }
  1052. while (frac > 2) {
  1053. v /= 10;
  1054. frac--;
  1055. }
  1056. if (i < len && s[i] == '%')
  1057. unit = 2;
  1058. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1059. s[i + 2] == 'm')
  1060. unit = 1;
  1061. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1062. unit = 3;
  1063. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  1064. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  1065. s[i] == 'a' || s[i] == 'd'))
  1066. i++;
  1067. *pos = i;
  1068. if (!digits)
  1069. return 0;
  1070. if (unit == 1)
  1071. v *= root_font_px;
  1072. else if (unit == 3)
  1073. v *= 16;
  1074. else if (unit == 2)
  1075. v = v * (long)viewport_w / 100;
  1076. v /= 100;
  1077. v *= sign;
  1078. if (v < -2048)
  1079. v = -2048;
  1080. if (v > 2048)
  1081. v = 2048;
  1082. return (int)v;
  1083. }
  1084. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  1085. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  1086. unsigned char *kind)
  1087. {
  1088. unsigned int i = 0;
  1089. unsigned long v = 0;
  1090. int digits = 0, frac = 0, pct = 0, rem = 0;
  1091. *val = 0;
  1092. *kind = 0;
  1093. while (i < len && is_space(s[i]))
  1094. i++;
  1095. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  1096. return 0;
  1097. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  1098. return 0;
  1099. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1100. v = v * 10 + (unsigned int)(s[i] - '0');
  1101. digits = 1;
  1102. i++;
  1103. }
  1104. if (i < len && s[i] == '.') {
  1105. i++;
  1106. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1107. if (frac < 2) {
  1108. v = v * 10 + (unsigned int)(s[i] - '0');
  1109. frac++;
  1110. }
  1111. i++;
  1112. }
  1113. }
  1114. while (frac < 2) {
  1115. v *= 10;
  1116. frac++;
  1117. }
  1118. while (frac > 2) {
  1119. v /= 10;
  1120. frac--;
  1121. }
  1122. if (!digits)
  1123. return 0;
  1124. while (i < len && is_space(s[i]))
  1125. i++;
  1126. if (i < len && s[i] == '%')
  1127. pct = 1;
  1128. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1129. s[i + 2] == 'm')
  1130. rem = 1;
  1131. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1132. rem = 2;
  1133. if (rem == 1)
  1134. v *= root_font_px;
  1135. else if (rem == 2)
  1136. v *= 16;
  1137. v /= 100;
  1138. if (pct) {
  1139. if (v > 100)
  1140. v = 100;
  1141. *kind = 2;
  1142. } else {
  1143. if (v > 2048)
  1144. v = 2048;
  1145. *kind = 1;
  1146. }
  1147. *val = (unsigned short)v;
  1148. return 1;
  1149. }
  1150. static int parse_font_px(const char *s, unsigned int len)
  1151. {
  1152. unsigned int i = 0;
  1153. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1154. int v1000, px;
  1155. int is_px = 1, is_rem = 0, is_pct = 0;
  1156. while (i < len && is_space(s[i]))
  1157. i++;
  1158. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  1159. return 24;
  1160. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  1161. return 32;
  1162. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  1163. return 14;
  1164. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1165. whole = whole * 10 + (s[i] - '0');
  1166. digits = 1;
  1167. i++;
  1168. }
  1169. if (i < len && s[i] == '.') {
  1170. i++;
  1171. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  1172. frac = frac * 10 + (s[i] - '0');
  1173. fscale *= 10;
  1174. i++;
  1175. }
  1176. }
  1177. if (!digits)
  1178. return 0;
  1179. while (i < len && is_space(s[i]))
  1180. i++;
  1181. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1182. is_rem = 1;
  1183. is_px = 0;
  1184. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1185. is_rem = 1;
  1186. is_px = 0;
  1187. } else if (i < len && s[i] == '%') {
  1188. is_pct = 1;
  1189. is_px = 0;
  1190. }
  1191. v1000 = whole * 1000 + frac * 1000 / fscale;
  1192. if (is_rem)
  1193. px = v1000 * root_font_px / 1000;
  1194. else if (is_pct)
  1195. px = v1000 * root_font_px / 100000;
  1196. else if (is_px)
  1197. px = v1000 / 1000;
  1198. else
  1199. px = v1000 / 1000;
  1200. if (px < 8)
  1201. px = 8;
  1202. if (px > 72)
  1203. px = 72;
  1204. return px;
  1205. }
  1206. static int parse_line_height(const char *s, unsigned int len,
  1207. unsigned short *px, unsigned char *pct)
  1208. {
  1209. unsigned int i = 0;
  1210. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1211. int v1000;
  1212. int is_px = 0, is_pct = 0;
  1213. *px = 0;
  1214. *pct = 0;
  1215. while (i < len && is_space(s[i]))
  1216. i++;
  1217. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  1218. return 0;
  1219. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1220. whole = whole * 10 + (s[i] - '0');
  1221. digits = 1;
  1222. i++;
  1223. }
  1224. if (i < len && s[i] == '.') {
  1225. i++;
  1226. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  1227. frac = frac * 10 + (s[i] - '0');
  1228. fscale *= 10;
  1229. i++;
  1230. }
  1231. }
  1232. if (!digits)
  1233. return 0;
  1234. while (i < len && is_space(s[i]))
  1235. i++;
  1236. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1237. is_px = 1;
  1238. else if (i < len && s[i] == '%')
  1239. is_pct = 1;
  1240. v1000 = whole * 1000 + frac * 1000 / fscale;
  1241. if (is_px) {
  1242. int v = v1000 / 1000;
  1243. if (v < 8)
  1244. v = 8;
  1245. if (v > 160)
  1246. v = 160;
  1247. *px = (unsigned short)v;
  1248. } else {
  1249. int v = v1000 / 10; /* unitless multiplier -> percent */
  1250. if (is_pct)
  1251. v = v1000 / 1000;
  1252. if (v < 50)
  1253. v = 50;
  1254. if (v > 400)
  1255. v = 400;
  1256. *pct = (unsigned char)v;
  1257. }
  1258. return 1;
  1259. }
  1260. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  1261. {
  1262. unsigned int i, j, n = s_len(needle);
  1263. for (i = 0; i + n <= len; i++) {
  1264. for (j = 0; j < n; j++) {
  1265. char a = s[i + j];
  1266. if (a >= 'A' && a <= 'Z')
  1267. a = (char)(a - 'A' + 'a');
  1268. if (a != needle[j])
  1269. break;
  1270. }
  1271. if (j == n)
  1272. return 1;
  1273. }
  1274. return 0;
  1275. }
  1276. /* Pull a colour out of a border/background shorthand. */
  1277. static int find_color(const char *s, unsigned int len, unsigned short *c)
  1278. {
  1279. unsigned int i;
  1280. for (i = 0; i < len; i++) {
  1281. if (s[i] == '#')
  1282. return parse_color(s + i, len - i, c);
  1283. if ((s[i] == 'r' || s[i] == 'R') &&
  1284. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  1285. return parse_color(s + i, len - i, c);
  1286. }
  1287. for (i = 0; i < len; ) {
  1288. unsigned int st, e;
  1289. while (i < len && (is_space(s[i]) || s[i] == ','))
  1290. i++;
  1291. st = i;
  1292. while (i < len && !is_space(s[i]) && s[i] != ',')
  1293. i++;
  1294. e = i;
  1295. if (e > st && parse_color(s + st, e - st, c))
  1296. return 1;
  1297. }
  1298. return 0;
  1299. }
  1300. static int parse_display(const char *s, unsigned int len)
  1301. {
  1302. unsigned int i;
  1303. while (len && is_space(s[0])) { s++; len--; }
  1304. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  1305. ;
  1306. if (i == 4 && ci_eq_n(s, "none", 4))
  1307. return BR_DISPLAY_NONE;
  1308. if (i == 5 && ci_eq_n(s, "block", 5))
  1309. return BR_DISPLAY_BLOCK;
  1310. if (i == 6 && ci_eq_n(s, "inline", 6))
  1311. return BR_DISPLAY_INLINE;
  1312. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  1313. return BR_DISPLAY_BLOCK;
  1314. if (i == 10 && ci_eq_n(s, "list-item", 10))
  1315. return BR_DISPLAY_BLOCK;
  1316. if (i == 4 && ci_eq_n(s, "flex", 4))
  1317. return BR_DISPLAY_FLEX;
  1318. if (i == 10 && ci_eq_n(s, "inline-flex", 10))
  1319. return BR_DISPLAY_FLEX;
  1320. if (i == 5 && ci_eq_n(s, "table", 5))
  1321. return BR_DISPLAY_BLOCK;
  1322. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  1323. return BR_DISPLAY_BLOCK;
  1324. /* Grid shells are common on news/wiki pages, but the two-column
  1325. * heuristic is not reliable for their nested area templates yet.
  1326. * Collapse them to block flow so the content stays readable. */
  1327. if (i == 4 && ci_eq_n(s, "grid", 4))
  1328. return BR_DISPLAY_BLOCK;
  1329. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  1330. return BR_DISPLAY_BLOCK;
  1331. return BR_DISPLAY_INLINE;
  1332. }
  1333. static void decl_add(struct br_decl *out, unsigned int *nout,
  1334. unsigned char prop, int val, int important)
  1335. {
  1336. if (*nout >= BR_DECLS)
  1337. return;
  1338. out[*nout].prop = prop;
  1339. out[*nout].val = (short)val;
  1340. out[*nout].important = (unsigned char)(important != 0);
  1341. (*nout)++;
  1342. }
  1343. static int parse_border_width(const char *s, unsigned int len)
  1344. {
  1345. unsigned int i = 0;
  1346. while (i < len) {
  1347. while (i < len && is_space(s[i]))
  1348. i++;
  1349. if (i >= len)
  1350. break;
  1351. if (s[i] == '#') {
  1352. while (i < len && !is_space(s[i]))
  1353. i++;
  1354. continue;
  1355. }
  1356. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1357. while (i < len && s[i] != ')')
  1358. i++;
  1359. if (i < len)
  1360. i++;
  1361. continue;
  1362. }
  1363. if (s[i] >= '0' && s[i] <= '9') {
  1364. int v = 0;
  1365. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1366. v = v * 10 + (s[i] - '0');
  1367. i++;
  1368. }
  1369. if (v < 0)
  1370. v = 0;
  1371. if (v > 3)
  1372. v = 3;
  1373. return v;
  1374. }
  1375. while (i < len && !is_space(s[i]))
  1376. i++;
  1377. }
  1378. return -1;
  1379. }
  1380. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  1381. * the input is copied verbatim when it contains no var(). */
  1382. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  1383. unsigned int len, char *out,
  1384. unsigned int cap, int depth)
  1385. {
  1386. unsigned int i = 0, n = 0;
  1387. if (depth > 8)
  1388. return 0;
  1389. while (i < len && n + 1 < cap) {
  1390. unsigned int p, inner, close, comma, nlen, fstart;
  1391. char quote;
  1392. int parens;
  1393. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  1394. out[n++] = s[i++];
  1395. continue;
  1396. }
  1397. p = i + 4;
  1398. quote = 0;
  1399. parens = 1;
  1400. while (p < len && parens) {
  1401. char c = s[p];
  1402. if (quote) {
  1403. if (c == quote)
  1404. quote = 0;
  1405. else if (c == '\\' && p + 1 < len)
  1406. p++;
  1407. } else if (c == '"' || c == '\'') {
  1408. quote = c;
  1409. } else if (c == '(') {
  1410. parens++;
  1411. } else if (c == ')') {
  1412. parens--;
  1413. }
  1414. p++;
  1415. }
  1416. if (parens) {
  1417. out[n++] = s[i++];
  1418. continue;
  1419. }
  1420. close = p - 1;
  1421. inner = i + 4;
  1422. while (inner < close && is_space(s[inner]))
  1423. inner++;
  1424. comma = inner;
  1425. parens = 0;
  1426. quote = 0;
  1427. while (comma < close) {
  1428. char c = s[comma];
  1429. if (quote) {
  1430. if (c == quote)
  1431. quote = 0;
  1432. } else if (c == '"' || c == '\'') {
  1433. quote = c;
  1434. } else if (c == '(') {
  1435. parens++;
  1436. } else if (c == ')' && parens > 0) {
  1437. parens--;
  1438. } else if (c == ',' && parens == 0) {
  1439. break;
  1440. }
  1441. comma++;
  1442. }
  1443. nlen = comma - inner;
  1444. while (nlen && is_space(s[inner + nlen - 1]))
  1445. nlen--;
  1446. fstart = comma < close ? comma + 1 : close;
  1447. while (fstart < close && is_space(s[fstart]))
  1448. fstart++;
  1449. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  1450. unsigned int vi = css_var_find(d, s + inner, nlen);
  1451. if (vi < BR_MAX_VARS) {
  1452. const char *v = attr_ptr(d, d->var_val[vi]);
  1453. unsigned int vn = css_expand_vars(d, v, s_len(v),
  1454. out + n,
  1455. cap - n, depth + 1);
  1456. n += vn;
  1457. } else if (fstart < close) {
  1458. unsigned int vn = css_expand_vars(d, s + fstart,
  1459. close - fstart,
  1460. out + n, cap - n,
  1461. depth + 1);
  1462. n += vn;
  1463. }
  1464. } else if (fstart < close) {
  1465. unsigned int vn = css_expand_vars(d, s + fstart,
  1466. close - fstart,
  1467. out + n, cap - n,
  1468. depth + 1);
  1469. n += vn;
  1470. }
  1471. i = p;
  1472. }
  1473. out[n] = '\0';
  1474. return n;
  1475. }
  1476. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  1477. unsigned int i)
  1478. {
  1479. while (i < len && (is_space(s[i]) || s[i] == ','))
  1480. i++;
  1481. return i;
  1482. }
  1483. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  1484. * argument). Returns 1 on success. */
  1485. static int parse_grid_track(const char *s, unsigned int len,
  1486. unsigned int *pp, unsigned short *val,
  1487. unsigned char *kind)
  1488. {
  1489. unsigned int i = grid_skip_sep(s, len, *pp);
  1490. unsigned int whole = 0, frac = 0, scale = 1;
  1491. int any = 0;
  1492. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  1493. unsigned int j = i + 7;
  1494. while (j < len && s[j] != ',')
  1495. j++;
  1496. if (j >= len)
  1497. return 0;
  1498. i = j + 1;
  1499. while (i < len && is_space(s[i]))
  1500. i++;
  1501. }
  1502. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  1503. int depth = 1;
  1504. i += 7;
  1505. while (i < len && depth) {
  1506. if (s[i] == '(')
  1507. depth++;
  1508. else if (s[i] == ')')
  1509. depth--;
  1510. i++;
  1511. }
  1512. *pp = i;
  1513. return 0;
  1514. }
  1515. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1516. whole = whole * 10 + (unsigned int)(s[i] - '0');
  1517. i++;
  1518. any = 1;
  1519. }
  1520. if (i < len && s[i] == '.') {
  1521. i++;
  1522. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1523. if (scale < 1000) {
  1524. frac = frac * 10 + (unsigned int)(s[i] - '0');
  1525. scale *= 10;
  1526. }
  1527. i++;
  1528. any = 1;
  1529. }
  1530. }
  1531. if (!any) {
  1532. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  1533. *val = 1;
  1534. *kind = 3;
  1535. *pp = i + 4;
  1536. return 1;
  1537. }
  1538. return 0;
  1539. }
  1540. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  1541. *val = (unsigned short)(whole ? whole : 1);
  1542. *kind = 3;
  1543. i += 2;
  1544. } else if (i < len && s[i] == '%') {
  1545. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1546. *kind = 2;
  1547. i++;
  1548. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1549. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1550. *kind = 1;
  1551. i += 3;
  1552. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1553. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1554. *kind = 1;
  1555. i += 2;
  1556. } else {
  1557. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1558. *kind = 1;
  1559. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1560. i += 2;
  1561. }
  1562. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  1563. i++;
  1564. *pp = i;
  1565. return 1;
  1566. }
  1567. static int parse_grid_cols(const char *s, unsigned int len,
  1568. unsigned short val[2], unsigned char kind[2])
  1569. {
  1570. unsigned int pos = 0;
  1571. int n = 0;
  1572. while (n < 2) {
  1573. unsigned int before = pos;
  1574. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  1575. break;
  1576. n++;
  1577. if (pos <= before)
  1578. break;
  1579. while (pos < len && is_space(s[pos]))
  1580. pos++;
  1581. if (pos >= len)
  1582. break;
  1583. }
  1584. return n;
  1585. }
  1586. static int parse_grid_slot(const char *s, unsigned int len)
  1587. {
  1588. if (contains_ci_len(s, len, "footer") ||
  1589. contains_ci_len(s, len, "header") ||
  1590. contains_ci_len(s, len, "sitenotice") ||
  1591. contains_ci_len(s, len, "navbar"))
  1592. return 1;
  1593. if (contains_ci_len(s, len, "columnstart") ||
  1594. contains_ci_len(s, len, "sidebar") ||
  1595. contains_ci_len(s, len, "left") ||
  1596. contains_ci_len(s, len, "start"))
  1597. return 2;
  1598. if (contains_ci_len(s, len, "pagecontent") ||
  1599. contains_ci_len(s, len, "content") ||
  1600. contains_ci_len(s, len, "main") ||
  1601. contains_ci_len(s, len, "right") ||
  1602. contains_ci_len(s, len, "end"))
  1603. return 3;
  1604. return 0;
  1605. }
  1606. static int css_has_var(const char *s, unsigned int len)
  1607. {
  1608. unsigned int i;
  1609. for (i = 0; i + 4 <= len; i++)
  1610. if ((s[i] == 'v' || s[i] == 'V') &&
  1611. ci_eq_n(s + i, "var(", 4))
  1612. return 1;
  1613. return 0;
  1614. }
  1615. static void parse_decls(struct br_doc *d, const char *s,
  1616. unsigned int len, struct br_decl *out,
  1617. unsigned int *nout)
  1618. {
  1619. static char expanded[4096];
  1620. if (css_has_var(s, len)) {
  1621. unsigned int elen = css_expand_vars(d, s, len, expanded,
  1622. sizeof(expanded), 0);
  1623. if (elen) {
  1624. s = expanded;
  1625. len = elen;
  1626. }
  1627. }
  1628. unsigned int i = 0;
  1629. *nout = 0;
  1630. while (i < len && *nout < BR_DECLS) {
  1631. unsigned int pstart, pend, vstart, vend, k;
  1632. char prop[24];
  1633. int important = 0;
  1634. while (i < len && (is_space(s[i]) || s[i] == ';'))
  1635. i++;
  1636. if (i >= len)
  1637. break;
  1638. pstart = i;
  1639. while (i < len && s[i] != ':' && s[i] != ';')
  1640. i++;
  1641. pend = i;
  1642. if (i >= len || s[i] != ':')
  1643. continue;
  1644. i++;
  1645. vstart = i;
  1646. while (i < len && s[i] != ';')
  1647. i++;
  1648. vend = i;
  1649. while (pstart < pend && is_space(s[pstart]))
  1650. pstart++;
  1651. while (pend > pstart && is_space(s[pend - 1]))
  1652. pend--;
  1653. while (vstart < vend && is_space(s[vstart]))
  1654. vstart++;
  1655. while (vend > vstart && is_space(s[vend - 1]))
  1656. vend--;
  1657. if (vend - vstart >= 10 &&
  1658. ci_eq_n(s + vend - 10, "!important", 10)) {
  1659. important = 1;
  1660. vend -= 10;
  1661. while (vend > vstart && is_space(s[vend - 1]))
  1662. vend--;
  1663. }
  1664. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  1665. char c = s[pstart + k];
  1666. if (c >= 'A' && c <= 'Z')
  1667. c = (char)(c - 'A' + 'a');
  1668. prop[k] = c;
  1669. }
  1670. prop[k] = '\0';
  1671. if (str_eq(prop, "color")) {
  1672. unsigned short c;
  1673. if (parse_color(s + vstart, vend - vstart, &c))
  1674. decl_add(out, nout, PROP_COLOR, c, important);
  1675. } else if (str_eq(prop, "background-color") ||
  1676. str_eq(prop, "background")) {
  1677. unsigned short c;
  1678. if (parse_color(s + vstart, vend - vstart, &c))
  1679. decl_add(out, nout, PROP_BG, c, important);
  1680. else if (str_eq(prop, "background") &&
  1681. find_color(s + vstart, vend - vstart, &c))
  1682. decl_add(out, nout, PROP_BG, c, important);
  1683. } else if (str_eq(prop, "font-size")) {
  1684. int px = parse_font_px(s + vstart, vend - vstart);
  1685. if (px > 0)
  1686. decl_add(out, nout, PROP_SCALE,
  1687. px >= 24 ? 2 : 1, important);
  1688. } else if (str_eq(prop, "font-weight")) {
  1689. int bold = 0;
  1690. unsigned int j;
  1691. if (vend - vstart >= 4 &&
  1692. ci_eq_n(s + vstart, "bold", 4))
  1693. bold = 1;
  1694. else {
  1695. int w = 0;
  1696. for (j = vstart; j < vend; j++)
  1697. if (s[j] >= '0' && s[j] <= '9')
  1698. w = w * 10 + (s[j] - '0');
  1699. if (w >= 500)
  1700. bold = 1;
  1701. }
  1702. decl_add(out, nout, PROP_BOLD, bold, important);
  1703. } else if (str_eq(prop, "text-align")) {
  1704. int a = BR_ALIGN_LEFT;
  1705. if (vend - vstart >= 6 &&
  1706. ci_eq_n(s + vstart, "center", 6))
  1707. a = BR_ALIGN_CENTER;
  1708. else if (vend - vstart >= 5 &&
  1709. ci_eq_n(s + vstart, "right", 5))
  1710. a = BR_ALIGN_RIGHT;
  1711. decl_add(out, nout, PROP_ALIGN, a, important);
  1712. } else if (str_eq(prop, "text-decoration")) {
  1713. int u = 0;
  1714. if (vend - vstart >= 9 &&
  1715. ci_eq_n(s + vstart, "underline", 9))
  1716. u = 1;
  1717. decl_add(out, nout, PROP_UNDERLINE, u, important);
  1718. } else if (str_eq(prop, "text-transform")) {
  1719. int u = (vend - vstart >= 9 &&
  1720. ci_eq_n(s + vstart, "uppercase", 9));
  1721. decl_add(out, nout, PROP_UPPER, u, important);
  1722. } else if (str_eq(prop, "display")) {
  1723. decl_add(out, nout, PROP_DISPLAY,
  1724. parse_display(s + vstart, vend - vstart),
  1725. important);
  1726. } else if (str_eq(prop, "visibility")) {
  1727. int h = contains_ci_len(s + vstart, vend - vstart,
  1728. "hidden") ||
  1729. contains_ci_len(s + vstart, vend - vstart,
  1730. "collapse");
  1731. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  1732. } else if (str_eq(prop, "opacity")) {
  1733. unsigned int q;
  1734. int h = 1, any = 0;
  1735. for (q = vstart; q < vend; q++) {
  1736. if (s[q] >= '1' && s[q] <= '9') {
  1737. h = 0;
  1738. break;
  1739. }
  1740. if (s[q] >= '0' && s[q] <= '9')
  1741. any = 1;
  1742. }
  1743. if (!any || contains_ci_len(s + vstart, vend - vstart,
  1744. "inherit"))
  1745. h = 0;
  1746. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  1747. } else if (str_eq(prop, "overflow") ||
  1748. str_eq(prop, "overflow-x") ||
  1749. str_eq(prop, "overflow-y")) {
  1750. int h = contains_ci_len(s + vstart, vend - vstart,
  1751. "hidden") ||
  1752. contains_ci_len(s + vstart, vend - vstart,
  1753. "clip");
  1754. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  1755. } else if (str_eq(prop, "float")) {
  1756. int f = 0;
  1757. if (vend - vstart >= 4 &&
  1758. ci_eq_n(s + vstart, "left", 4))
  1759. f = 1;
  1760. else if (vend - vstart >= 5 &&
  1761. ci_eq_n(s + vstart, "right", 5))
  1762. f = 2;
  1763. decl_add(out, nout, PROP_FLOAT, f, important);
  1764. } else if (str_eq(prop, "flex-grow") ||
  1765. str_eq(prop, "flex")) {
  1766. unsigned int q = vstart;
  1767. int g = 0;
  1768. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  1769. q++;
  1770. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  1771. g = g * 10 + (s[q] - '0');
  1772. q++;
  1773. }
  1774. if (g > 100)
  1775. g = 100;
  1776. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  1777. } else if (str_eq(prop, "flex-direction")) {
  1778. decl_add(out, nout, PROP_FLEX_COL,
  1779. contains_ci_len(s + vstart, vend - vstart,
  1780. "column"), important);
  1781. } else if (str_eq(prop, "flex-wrap")) {
  1782. int wrap = 0;
  1783. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  1784. wrap = 2;
  1785. else if (contains_ci_len(s + vstart, vend - vstart,
  1786. "wrap"))
  1787. wrap = 1;
  1788. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  1789. } else if (str_eq(prop, "position")) {
  1790. int p = 0;
  1791. if (contains_ci_len(s + vstart, vend - vstart,
  1792. "absolute"))
  1793. p = 1;
  1794. else if (contains_ci_len(s + vstart, vend - vstart,
  1795. "fixed") ||
  1796. contains_ci_len(s + vstart, vend - vstart,
  1797. "sticky"))
  1798. p = 2;
  1799. decl_add(out, nout, PROP_POS, p, important);
  1800. } else if (str_eq(prop, "left") || str_eq(prop, "top")) {
  1801. int is_left = prop[0] == 'l';
  1802. unsigned int pos = 0;
  1803. if (!(vend - vstart >= 4 &&
  1804. ci_eq_n(s + vstart, "auto", 4))) {
  1805. int v = parse_len(s + vstart, vend - vstart, &pos);
  1806. if (v > 32000)
  1807. v = 32000;
  1808. else if (v < -32000)
  1809. v = -32000;
  1810. decl_add(out, nout,
  1811. is_left ? PROP_LEFT : PROP_TOP, v, important);
  1812. decl_add(out, nout,
  1813. is_left ? PROP_LEFT_SET : PROP_TOP_SET, 1,
  1814. important);
  1815. }
  1816. } else if (str_eq(prop, "grid-template-columns") ||
  1817. str_eq(prop, "grid-template")) {
  1818. const char *v = s + vstart;
  1819. unsigned int vl = vend - vstart;
  1820. unsigned short val[2];
  1821. unsigned char kind[2];
  1822. int n, q;
  1823. if (str_eq(prop, "grid-template")) {
  1824. for (q = 0; q < (int)vl; q++)
  1825. if (v[q] == '/') {
  1826. q++;
  1827. while (q < (int)vl && is_space(v[q]))
  1828. q++;
  1829. v += q;
  1830. vl -= (unsigned int)q;
  1831. break;
  1832. }
  1833. }
  1834. n = parse_grid_cols(v, vl, val, kind);
  1835. if (n > 0) {
  1836. decl_add(out, nout, PROP_WIDTH, val[0], important);
  1837. decl_add(out, nout, PROP_WIDTH_KIND, kind[0], important);
  1838. decl_add(out, nout, PROP_GRID_NCOLS, n, important);
  1839. }
  1840. if (n > 1) {
  1841. decl_add(out, nout, PROP_MAXW, val[1], important);
  1842. decl_add(out, nout, PROP_MAXW_KIND, kind[1], important);
  1843. }
  1844. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  1845. unsigned int gp = 0;
  1846. unsigned short gv = 0;
  1847. unsigned char gk = 1;
  1848. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  1849. if (gv > 120)
  1850. gv = 120;
  1851. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  1852. }
  1853. } else if (str_eq(prop, "grid-area")) {
  1854. int slot = parse_grid_slot(s + vstart, vend - vstart);
  1855. if (slot)
  1856. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  1857. } else if (str_eq(prop, "width") ||
  1858. str_eq(prop, "max-width") ||
  1859. str_eq(prop, "height")) {
  1860. unsigned short v;
  1861. unsigned char kind;
  1862. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  1863. if (str_eq(prop, "width")) {
  1864. decl_add(out, nout, PROP_WIDTH, v, important);
  1865. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  1866. important);
  1867. } else if (str_eq(prop, "max-width")) {
  1868. decl_add(out, nout, PROP_MAXW, v, important);
  1869. decl_add(out, nout, PROP_MAXW_KIND, kind,
  1870. important);
  1871. } else {
  1872. decl_add(out, nout, PROP_HEIGHT, v, important);
  1873. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  1874. important);
  1875. }
  1876. }
  1877. } else if (str_eq(prop, "line-height")) {
  1878. unsigned short px;
  1879. unsigned char pct;
  1880. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  1881. if (px) {
  1882. decl_add(out, nout, PROP_LH_PX, px, important);
  1883. decl_add(out, nout, PROP_LH_PCT, 0, important);
  1884. } else {
  1885. decl_add(out, nout, PROP_LH_PX, 0, important);
  1886. decl_add(out, nout, PROP_LH_PCT, pct, important);
  1887. }
  1888. }
  1889. } else if (str_eq(prop, "list-style") ||
  1890. str_eq(prop, "list-style-type")) {
  1891. int none = (vend - vstart >= 4 &&
  1892. contains_ci_len(s + vstart, vend - vstart,
  1893. "none"));
  1894. decl_add(out, nout, PROP_LIST_NONE, none, important);
  1895. } else if (str_eq(prop, "border") ||
  1896. str_eq(prop, "border-top") ||
  1897. str_eq(prop, "border-right") ||
  1898. str_eq(prop, "border-bottom") ||
  1899. str_eq(prop, "border-left") ||
  1900. str_eq(prop, "border-color") ||
  1901. str_eq(prop, "border-width")) {
  1902. unsigned short c;
  1903. int bw;
  1904. int have_color;
  1905. if (vend - vstart >= 4 &&
  1906. ci_eq_n(s + vstart, "none", 4)) {
  1907. decl_add(out, nout, PROP_BORDER_W, 0, important);
  1908. } else {
  1909. bw = parse_border_width(s + vstart,
  1910. vend - vstart);
  1911. have_color = find_color(s + vstart,
  1912. vend - vstart, &c);
  1913. if (have_color) {
  1914. decl_add(out, nout, PROP_BORDER_COLOR, c,
  1915. important);
  1916. if (bw < 0)
  1917. bw = 1;
  1918. }
  1919. if (bw >= 0) {
  1920. if (bw > 3)
  1921. bw = 3;
  1922. decl_add(out, nout, PROP_BORDER_W, bw,
  1923. important);
  1924. }
  1925. }
  1926. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  1927. int vals[4], nv = 0;
  1928. unsigned int pos = 0;
  1929. int is_pad = prop[0] == 'p';
  1930. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  1931. while (pos < vend - vstart && nv < 4) {
  1932. vals[nv++] = parse_len(s + vstart,
  1933. vend - vstart, &pos);
  1934. while (pos < vend - vstart &&
  1935. is_space(s[vstart + pos]))
  1936. pos++;
  1937. }
  1938. if (nv == 1) {
  1939. vals[1] = vals[2] = vals[3] = vals[0];
  1940. } else if (nv == 2) {
  1941. vals[2] = vals[0];
  1942. vals[3] = vals[1];
  1943. } else if (nv == 3) {
  1944. vals[3] = vals[1];
  1945. }
  1946. if (nv > 0) {
  1947. decl_add(out, nout, base, vals[0], important);
  1948. decl_add(out, nout, (unsigned char)(base + 1),
  1949. vals[1], important);
  1950. decl_add(out, nout, (unsigned char)(base + 2),
  1951. vals[2], important);
  1952. decl_add(out, nout, (unsigned char)(base + 3),
  1953. vals[3], important);
  1954. }
  1955. } else if (str_eq(prop, "margin-top") ||
  1956. str_eq(prop, "margin-right") ||
  1957. str_eq(prop, "margin-bottom") ||
  1958. str_eq(prop, "margin-left") ||
  1959. str_eq(prop, "padding-top") ||
  1960. str_eq(prop, "padding-right") ||
  1961. str_eq(prop, "padding-bottom") ||
  1962. str_eq(prop, "padding-left")) {
  1963. unsigned int pos = 0;
  1964. int v = parse_len(s + vstart, vend - vstart, &pos);
  1965. unsigned char p = PROP_MT;
  1966. const char *side;
  1967. if (prop[0] == 'p')
  1968. p = PROP_PT;
  1969. side = prop + (prop[0] == 'p' ? 8 : 7);
  1970. if (str_eq(side, "right"))
  1971. p = (unsigned char)(p + 1);
  1972. else if (str_eq(side, "bottom"))
  1973. p = (unsigned char)(p + 2);
  1974. else if (str_eq(side, "left"))
  1975. p = (unsigned char)(p + 3);
  1976. decl_add(out, nout, p, v, important);
  1977. }
  1978. }
  1979. }
  1980. /* Skip @media/@font-face/@keyframes/@import without descending into their
  1981. * nested blocks. This is the mobile/base rules path: media enhancements are
  1982. * deliberately ignored for now. */
  1983. static unsigned int skip_at_rule(const char *s, unsigned int len,
  1984. unsigned int i)
  1985. {
  1986. unsigned int j = i;
  1987. int depth;
  1988. while (j < len && s[j] != '{' && s[j] != ';')
  1989. j++;
  1990. if (j >= len)
  1991. return len;
  1992. if (s[j] == ';')
  1993. return j + 1;
  1994. depth = 1;
  1995. j++;
  1996. while (j < len && depth) {
  1997. if (s[j] == '{')
  1998. depth++;
  1999. else if (s[j] == '}')
  2000. depth--;
  2001. j++;
  2002. }
  2003. return j;
  2004. }
  2005. static unsigned int viewport_w = 1024;
  2006. static unsigned short theme_text = 0x0000;
  2007. static unsigned short theme_bg = 0xFFFF;
  2008. static unsigned short theme_panel = 0xEF5D;
  2009. static unsigned short theme_link = 0x001B;
  2010. void br_set_theme(unsigned short text, unsigned short bg,
  2011. unsigned short panel, unsigned short link)
  2012. {
  2013. theme_text = text;
  2014. theme_bg = bg;
  2015. theme_panel = panel;
  2016. theme_link = link;
  2017. }
  2018. void br_set_viewport(unsigned int width)
  2019. {
  2020. viewport_w = width ? width : 1024;
  2021. }
  2022. static unsigned int media_len_px(const char *s, unsigned int len)
  2023. {
  2024. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  2025. int em = 0;
  2026. while (i < len && is_space(s[i]))
  2027. i++;
  2028. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2029. whole = whole * 10 + (unsigned int)(s[i] - '0');
  2030. i++;
  2031. }
  2032. if (i < len && s[i] == '.') {
  2033. i++;
  2034. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  2035. frac = frac * 10 + (unsigned int)(s[i] - '0');
  2036. scale *= 10;
  2037. i++;
  2038. }
  2039. }
  2040. while (i < len && is_space(s[i]))
  2041. i++;
  2042. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2043. em = 1;
  2044. i += 2;
  2045. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2046. em = 1;
  2047. i += 3;
  2048. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  2049. i += 2;
  2050. }
  2051. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2052. if (em)
  2053. return v * 16 / 100;
  2054. return v / 100;
  2055. }
  2056. /* One parenthesised media feature: min-width/max-width and the modern
  2057. * (width <= 600px) form. Unknown features are treated as true. */
  2058. static int media_feature_matches(const char *s, unsigned int len)
  2059. {
  2060. unsigned int i;
  2061. while (len && is_space(*s)) { s++; len--; }
  2062. while (len && is_space(s[len - 1])) len--;
  2063. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  2064. /* The browser canvas is dark, but sites express their light
  2065. * palette in the base rules; the dark overrides are usually
  2066. * invisible against our default surfaces. Pick the light set. */
  2067. return !contains_ci_len(s, len, "dark");
  2068. }
  2069. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  2070. for (i = 9; i < len && s[i] != ':'; i++)
  2071. ;
  2072. if (i < len)
  2073. return viewport_w >= media_len_px(s + i + 1,
  2074. len - i - 1);
  2075. return 1;
  2076. }
  2077. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  2078. for (i = 9; i < len && s[i] != ':'; i++)
  2079. ;
  2080. if (i < len)
  2081. return viewport_w <= media_len_px(s + i + 1,
  2082. len - i - 1);
  2083. return 1;
  2084. }
  2085. for (i = 0; i < len; i++) {
  2086. if (s[i] == '<' || s[i] == '>') {
  2087. int max = s[i] == '<';
  2088. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  2089. if (i + 1 < len && s[i + 1] == '=')
  2090. v = media_len_px(s + i + 2, len - i - 2);
  2091. return max ? viewport_w <= v : viewport_w >= v;
  2092. }
  2093. }
  2094. return 1;
  2095. }
  2096. static int media_query_matches(const char *s, unsigned int len)
  2097. {
  2098. unsigned int i;
  2099. if (contains_ci_len(s, len, "print") &&
  2100. !contains_ci_len(s, len, "screen"))
  2101. return 0;
  2102. if (contains_ci_len(s, len, "not "))
  2103. return 0;
  2104. for (i = 0; i < len; i++) {
  2105. if (s[i] == '(') {
  2106. unsigned int st = ++i;
  2107. while (i < len && s[i] != ')')
  2108. i++;
  2109. if (!media_feature_matches(s + st, i - st))
  2110. return 0;
  2111. }
  2112. }
  2113. return 1;
  2114. }
  2115. static int media_list_matches(const char *s, unsigned int len)
  2116. {
  2117. unsigned int i = 0;
  2118. while (i < len) {
  2119. unsigned int st = i;
  2120. while (i < len && s[i] != ',')
  2121. i++;
  2122. if (media_query_matches(s + st, i - st))
  2123. return 1;
  2124. i++;
  2125. }
  2126. return 0;
  2127. }
  2128. static unsigned int rule_hash(const char *s, unsigned int len)
  2129. {
  2130. unsigned int h = 2166136261u, i;
  2131. for (i = 0; i < len; i++) {
  2132. char c = s[i];
  2133. if (c >= 'A' && c <= 'Z')
  2134. c = (char)(c - 'A' + 'a');
  2135. h ^= (unsigned char)c;
  2136. h *= 16777619u;
  2137. }
  2138. return h % BR_RULE_HASH;
  2139. }
  2140. /* Index a parsed rule by the most selective component of its rightmost
  2141. * compound selector. br_style_for() then only visits rules that can
  2142. * possibly match a node instead of scanning every rule. */
  2143. static void rule_index_add(struct br_doc *d, unsigned int idx,
  2144. const char *key, unsigned int len)
  2145. {
  2146. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  2147. unsigned short *head;
  2148. for (i = 0; i < len; i++) {
  2149. if (key[i] == '.' || key[i] == '#') {
  2150. unsigned int st = i + 1, n = 0;
  2151. while (st + n < len && is_ident(key[st + n]))
  2152. n++;
  2153. if (key[i] == '#') {
  2154. ipos = st;
  2155. ilen = n;
  2156. } else if (n) {
  2157. cpos = st;
  2158. clen = n;
  2159. }
  2160. i = st + n - 1;
  2161. }
  2162. }
  2163. if (ilen)
  2164. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  2165. else if (clen)
  2166. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  2167. else if (len == 5 && ci_eq_n(key, ":root", 5))
  2168. head = &d->rule_tag[BR_TAG_HTML];
  2169. else if (len && is_ident(key[0])) {
  2170. char name[16];
  2171. unsigned int n = 0, tag;
  2172. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  2173. name[n] = key[n];
  2174. n++;
  2175. }
  2176. name[n] = '\0';
  2177. tag = br_tag_from_name(name);
  2178. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  2179. head = &d->rule_tag[tag];
  2180. else
  2181. head = &d->rule_any;
  2182. } else {
  2183. head = &d->rule_any;
  2184. }
  2185. d->rule_next[idx] = *head;
  2186. *head = (unsigned short)idx;
  2187. }
  2188. static unsigned int selector_specificity(const char *s, unsigned int len);
  2189. static int css_collect_only;
  2190. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  2191. {
  2192. unsigned int i = 0;
  2193. while (i < len && d->nrules < d->rule_cap) {
  2194. unsigned int sstart, send, bstart, bend, j;
  2195. struct br_decl decls[BR_DECLS];
  2196. unsigned int nd;
  2197. while (i < len && is_space(s[i]))
  2198. i++;
  2199. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  2200. i += 2;
  2201. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  2202. i++;
  2203. i += 2;
  2204. continue;
  2205. }
  2206. if (i >= len || s[i] == '}')
  2207. break;
  2208. if (s[i] == '@') {
  2209. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  2210. unsigned int q = i + 6;
  2211. while (q < len && s[q] != '{')
  2212. q++;
  2213. if (q < len &&
  2214. media_list_matches(s + i + 6,
  2215. q - (i + 6))) {
  2216. unsigned int b = q + 1, e = b;
  2217. int depth = 1;
  2218. while (e < len && depth) {
  2219. if (s[e] == '{')
  2220. depth++;
  2221. else if (s[e] == '}')
  2222. depth--;
  2223. e++;
  2224. }
  2225. if (depth == 0)
  2226. parse_css(d, s + b,
  2227. (e - 1) - b);
  2228. i = e;
  2229. } else {
  2230. i = skip_at_rule(s, len, i);
  2231. }
  2232. continue;
  2233. }
  2234. i = skip_at_rule(s, len, i);
  2235. continue;
  2236. }
  2237. sstart = i;
  2238. while (i < len && s[i] != '{')
  2239. i++;
  2240. if (i >= len)
  2241. break;
  2242. send = i;
  2243. i++;
  2244. bstart = i;
  2245. while (i < len && s[i] != '}')
  2246. i++;
  2247. bend = i;
  2248. if (i < len)
  2249. i++;
  2250. while (sstart < send && is_space(s[sstart]))
  2251. sstart++;
  2252. while (send > sstart && is_space(s[send - 1]))
  2253. send--;
  2254. if (sstart == send)
  2255. continue;
  2256. if (!css_dark_selector(s + sstart, send - sstart))
  2257. css_collect_block(d, s + bstart, bend - bstart);
  2258. if (css_collect_only)
  2259. continue;
  2260. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  2261. if (!nd)
  2262. continue;
  2263. /* Split a comma-separated selector list into separate rules. */
  2264. j = sstart;
  2265. while (j <= send && d->nrules < d->rule_cap) {
  2266. unsigned int q = j, e, k;
  2267. char sel[80];
  2268. unsigned int off;
  2269. while (q < send && s[q] != ',')
  2270. q++;
  2271. e = q;
  2272. while (j < e && is_space(s[j]))
  2273. j++;
  2274. while (e > j && is_space(s[e - 1]))
  2275. e--;
  2276. if (e > j) {
  2277. /* Ignore single selectors with sibling combinators,
  2278. * pseudo-classes or attribute tests: they need
  2279. * state/attributes we do not model. The other
  2280. * selectors in the list still apply. */
  2281. for (k = j; k < e; ) {
  2282. if (s[k] == '+' || s[k] == '~' ||
  2283. s[k] == '[')
  2284. break;
  2285. if (s[k] == ':') {
  2286. if (k + 5 <= e &&
  2287. ci_eq_n(s + k, ":root", 5)) {
  2288. k += 5;
  2289. continue;
  2290. }
  2291. break;
  2292. }
  2293. k++;
  2294. }
  2295. if (k < e) {
  2296. j = q + 1;
  2297. if (q >= send)
  2298. break;
  2299. continue;
  2300. }
  2301. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  2302. sel[k] = s[j + k];
  2303. sel[k] = '\0';
  2304. off = store_attr(d, sel, k, 79);
  2305. if (off) {
  2306. struct br_rule *r = &d->rules[d->nrules++];
  2307. unsigned int ks = 0, q;
  2308. for (q = 0; q < k; q++)
  2309. if (sel[q] == ' ' || sel[q] == '>')
  2310. ks = q + 1;
  2311. r->sel_off = off;
  2312. r->key_off = store_attr(d, sel + ks,
  2313. k - ks, 79);
  2314. r->spec = (unsigned short)selector_specificity(sel, k);
  2315. r->single = 1;
  2316. {
  2317. unsigned int z;
  2318. for (z = 0; z < k; z++)
  2319. if (is_space(sel[z]) || sel[z] == '>') {
  2320. r->single = 0;
  2321. break;
  2322. }
  2323. }
  2324. r->nd = (unsigned char)nd;
  2325. for (k = 0; k < nd; k++)
  2326. r->d[k] = decls[k];
  2327. rule_index_add(d, d->nrules - 1,
  2328. attr_ptr(d, r->key_off),
  2329. s_len(attr_ptr(d, r->key_off)));
  2330. }
  2331. }
  2332. j = q + 1;
  2333. if (q >= send)
  2334. break;
  2335. }
  2336. }
  2337. }
  2338. /* Sites often set html { font-size: 62.5% } and then size everything in
  2339. * rem. Detect that root size before parsing the sheet so a rem resolves to
  2340. * the same pixels a real browser would use. */
  2341. static void detect_root_font(const char *s, unsigned int len)
  2342. {
  2343. unsigned int i;
  2344. for (i = 0; i + 4 < len; i++) {
  2345. unsigned int j, end, sel, q;
  2346. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  2347. int exact;
  2348. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  2349. s[i + 3] == 'l'))
  2350. continue;
  2351. if (i + 4 < len && s[i + 4] != '{' && s[i + 4] != ',' &&
  2352. s[i + 4] != '.' && s[i + 4] != ':' && s[i + 4] != '[')
  2353. continue;
  2354. sel = i;
  2355. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  2356. s[sel - 1] != ';' && s[sel - 1] != ',')
  2357. sel--;
  2358. q = sel;
  2359. while (q < i && is_space(s[q]))
  2360. q++;
  2361. exact = (q == i);
  2362. if (!exact && i + 4 < len && s[i + 4] != ',' &&
  2363. s[i + 4] != '.')
  2364. continue;
  2365. j = i + 4;
  2366. while (j < len && is_space(s[j]))
  2367. j++;
  2368. if (j >= len || s[j] != '{')
  2369. continue;
  2370. while (j < len && s[j] != '{')
  2371. j++;
  2372. if (j >= len)
  2373. break;
  2374. end = j + 1;
  2375. while (end < len && s[end] != '}')
  2376. end++;
  2377. for (; j + 9 < end; j++) {
  2378. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  2379. s[j + 3] == 't' && s[j + 4] == '-' &&
  2380. s[j + 5] == 's' && s[j + 6] == 'i' &&
  2381. s[j + 7] == 'z' && s[j + 8] == 'e'))
  2382. continue;
  2383. j += 9;
  2384. while (j < end && s[j] != ':')
  2385. j++;
  2386. if (j >= end)
  2387. break;
  2388. j++;
  2389. while (j < end && is_space(s[j]))
  2390. j++;
  2391. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2392. v = v * 10 + (s[j] - '0');
  2393. digits = 1;
  2394. j++;
  2395. }
  2396. if (j < end && s[j] == '.') {
  2397. j++;
  2398. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2399. v = v * 10 + (s[j] - '0');
  2400. frac++;
  2401. j++;
  2402. }
  2403. }
  2404. while (frac < 2) {
  2405. v *= 10;
  2406. frac++;
  2407. }
  2408. while (frac > 2) {
  2409. v /= 10;
  2410. frac--;
  2411. }
  2412. if (!digits)
  2413. break;
  2414. if (j < end && s[j] == '%')
  2415. pct = 1;
  2416. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  2417. px = 1;
  2418. if (pct)
  2419. v = (v * 16 + 5000) / 10000;
  2420. else if (px)
  2421. v /= 100;
  2422. else
  2423. v = v * 16 / 100;
  2424. if (exact && v >= 8 && v <= 24)
  2425. root_font_px = v;
  2426. if (exact)
  2427. return;
  2428. }
  2429. }
  2430. }
  2431. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  2432. {
  2433. detect_root_font(css, len);
  2434. css_collect_only = 1;
  2435. parse_css(d, css, len);
  2436. css_collect_only = 0;
  2437. parse_css(d, css, len);
  2438. }
  2439. void br_parse_inline_css(struct br_doc *d)
  2440. {
  2441. if (d->inline_css_len) {
  2442. css_collect_only = 1;
  2443. parse_css(d, css_store, d->inline_css_len);
  2444. css_collect_only = 0;
  2445. parse_css(d, css_store, d->inline_css_len);
  2446. }
  2447. d->inline_css_len = 0;
  2448. }
  2449. void br_reset_styles(struct br_doc *d)
  2450. {
  2451. unsigned int i;
  2452. for (i = 0; i < d->nnode; i++)
  2453. d->nodes[i].style_valid = 0;
  2454. }
  2455. /* ---- selector matching ---- */
  2456. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  2457. unsigned int clen)
  2458. {
  2459. const char *s = attr_ptr(d, d->nodes[node].class_off);
  2460. unsigned int i = 0;
  2461. while (s[i]) {
  2462. unsigned int st, n;
  2463. while (s[i] && is_space(s[i]))
  2464. i++;
  2465. st = i;
  2466. while (s[i] && !is_space(s[i]))
  2467. i++;
  2468. n = i - st;
  2469. if (n == clen && ci_eq_n(s + st, cls, n))
  2470. return 1;
  2471. }
  2472. return 0;
  2473. }
  2474. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  2475. unsigned int len)
  2476. {
  2477. const char *s = attr_ptr(d, d->nodes[node].id_off);
  2478. unsigned int i;
  2479. for (i = 0; i < len; i++)
  2480. if (s[i] != id[i])
  2481. return 0;
  2482. return s[len] == '\0';
  2483. }
  2484. static int is_ident(char c)
  2485. {
  2486. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  2487. (c >= '0' && c <= '9') || c == '-' || c == '_';
  2488. }
  2489. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  2490. unsigned int len)
  2491. {
  2492. unsigned int i = 0;
  2493. int got = 0;
  2494. if (i < len && s[i] == '*') {
  2495. i++;
  2496. got = 1;
  2497. } else if (i < len && is_ident(s[i])) {
  2498. char name[16];
  2499. unsigned int n = 0, tag;
  2500. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  2501. name[n++] = s[i++];
  2502. name[n] = '\0';
  2503. tag = br_tag_from_name(name);
  2504. if (tag != d->nodes[node].tag)
  2505. return 0;
  2506. got = 1;
  2507. }
  2508. while (i < len) {
  2509. if (s[i] == '.') {
  2510. unsigned int st;
  2511. i++;
  2512. st = i;
  2513. while (i < len && is_ident(s[i]))
  2514. i++;
  2515. if (!class_has(d, node, s + st, i - st))
  2516. return 0;
  2517. got = 1;
  2518. } else if (s[i] == '#') {
  2519. unsigned int st;
  2520. i++;
  2521. st = i;
  2522. while (i < len && is_ident(s[i]))
  2523. i++;
  2524. if (!id_eq(d, node, s + st, i - st))
  2525. return 0;
  2526. got = 1;
  2527. } else if (s[i] == ':') {
  2528. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  2529. (i + 5 == len || !is_ident(s[i + 5]))) {
  2530. if (d->nodes[node].tag != BR_TAG_HTML)
  2531. return 0;
  2532. i += 5;
  2533. got = 1;
  2534. } else {
  2535. return 0; /* other pseudo-classes are not modelled */
  2536. }
  2537. } else {
  2538. i++;
  2539. }
  2540. }
  2541. return got;
  2542. }
  2543. static unsigned int selector_specificity(const char *s, unsigned int len)
  2544. {
  2545. unsigned int i, spec = 0;
  2546. for (i = 0; i < len; i++) {
  2547. if (s[i] == '#')
  2548. spec += 100;
  2549. else if (s[i] == '.')
  2550. spec += 10;
  2551. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  2552. if (is_ident(s[i]))
  2553. spec += 1;
  2554. }
  2555. }
  2556. return spec;
  2557. }
  2558. /* Match one complex selector; returns specificity or -1. */
  2559. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  2560. unsigned int len, int right_done)
  2561. {
  2562. struct {
  2563. unsigned int start, end;
  2564. int child;
  2565. } parts[8];
  2566. int np = 0;
  2567. unsigned int i = 0;
  2568. int right;
  2569. while (i < len) {
  2570. int child = 0;
  2571. unsigned int st, e;
  2572. while (i < len && is_space(s[i]))
  2573. i++;
  2574. if (i < len && s[i] == '>') {
  2575. child = 1;
  2576. i++;
  2577. while (i < len && is_space(s[i]))
  2578. i++;
  2579. }
  2580. if (i >= len)
  2581. break;
  2582. st = i;
  2583. while (i < len && !is_space(s[i]) && s[i] != '>')
  2584. i++;
  2585. e = i;
  2586. if (np < 8) {
  2587. parts[np].start = st;
  2588. parts[np].end = e;
  2589. parts[np].child = child;
  2590. np++;
  2591. }
  2592. }
  2593. if (!np)
  2594. return -1;
  2595. if (!right_done &&
  2596. !compound_match(d, node, s + parts[np - 1].start,
  2597. parts[np - 1].end - parts[np - 1].start))
  2598. return -1;
  2599. right = np - 1;
  2600. while (right > 0) {
  2601. unsigned int p = d->nodes[node].parent;
  2602. if (!p)
  2603. return -1;
  2604. if (parts[right].child) {
  2605. if (!compound_match(d, p, s + parts[right - 1].start,
  2606. parts[right - 1].end -
  2607. parts[right - 1].start))
  2608. return -1;
  2609. node = p;
  2610. } else {
  2611. while (p && !compound_match(d, p,
  2612. s + parts[right - 1].start,
  2613. parts[right - 1].end -
  2614. parts[right - 1].start))
  2615. p = d->nodes[p].parent;
  2616. if (!p)
  2617. return -1;
  2618. node = p;
  2619. }
  2620. right--;
  2621. }
  2622. return (int)selector_specificity(s, len);
  2623. }
  2624. #define RULE_MATCH_MAX 512
  2625. struct rule_match {
  2626. unsigned short idx;
  2627. short spec;
  2628. };
  2629. static struct rule_match rule_matches[RULE_MATCH_MAX];
  2630. static unsigned int nrule_matches;
  2631. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  2632. {
  2633. struct br_rule *r = &d->rules[idx];
  2634. const char *key = attr_ptr(d, r->key_off);
  2635. int spec, key_ok = 0;
  2636. if (key[0]) {
  2637. if (!compound_match(d, node, key, s_len(key)))
  2638. return;
  2639. key_ok = 1;
  2640. }
  2641. spec = (r->single && key_ok) ? (int)r->spec :
  2642. selector_match(d, node, attr_ptr(d, r->sel_off),
  2643. s_len(attr_ptr(d, r->sel_off)), key_ok);
  2644. if (spec < 0)
  2645. return;
  2646. if (nrule_matches < RULE_MATCH_MAX) {
  2647. rule_matches[nrule_matches].idx = (unsigned short)idx;
  2648. rule_matches[nrule_matches].spec = (short)spec;
  2649. nrule_matches++;
  2650. }
  2651. }
  2652. static void rule_scan_list(struct br_doc *d, unsigned int node,
  2653. unsigned short head)
  2654. {
  2655. while (head != 0xFFFF) {
  2656. rule_try(d, node, head);
  2657. head = d->rule_next[head];
  2658. }
  2659. }
  2660. static void rule_scan_class(struct br_doc *d, unsigned int node,
  2661. const char *cls, unsigned int len)
  2662. {
  2663. if (len)
  2664. rule_scan_list(d, node,
  2665. d->rule_bucket[rule_hash(cls, len)]);
  2666. }
  2667. static void rule_collect(struct br_doc *d, unsigned int node)
  2668. {
  2669. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  2670. const char *id = attr_ptr(d, d->nodes[node].id_off);
  2671. unsigned int tag = d->nodes[node].tag;
  2672. unsigned int i;
  2673. nrule_matches = 0;
  2674. rule_scan_list(d, node, d->rule_any);
  2675. if (tag < BR_TAG_MAX)
  2676. rule_scan_list(d, node, d->rule_tag[tag]);
  2677. if (id[0])
  2678. rule_scan_list(d, node,
  2679. d->rule_bucket[rule_hash(id, s_len(id))]);
  2680. i = 0;
  2681. while (cls[i]) {
  2682. unsigned int st, n;
  2683. while (cls[i] && is_space(cls[i]))
  2684. i++;
  2685. st = i;
  2686. while (cls[i] && !is_space(cls[i]))
  2687. i++;
  2688. n = i - st;
  2689. if (n)
  2690. rule_scan_class(d, node, cls + st, n);
  2691. }
  2692. /* Rules must cascade in document order even though the index visits
  2693. * them in bucket order. */
  2694. for (i = 1; i < nrule_matches; i++) {
  2695. struct rule_match m = rule_matches[i];
  2696. unsigned int j = i;
  2697. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  2698. rule_matches[j] = rule_matches[j - 1];
  2699. j--;
  2700. }
  2701. rule_matches[j] = m;
  2702. }
  2703. }
  2704. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  2705. {
  2706. switch (dcl->prop) {
  2707. case PROP_COLOR:
  2708. st->color = (unsigned short)dcl->val;
  2709. break;
  2710. case PROP_BG:
  2711. st->bg = (unsigned short)dcl->val;
  2712. st->has_bg = 1;
  2713. break;
  2714. case PROP_SCALE:
  2715. st->scale = (unsigned char)(dcl->val >= 2 ? 2 : 1);
  2716. break;
  2717. case PROP_BOLD:
  2718. st->bold = (unsigned char)(dcl->val != 0);
  2719. break;
  2720. case PROP_ALIGN:
  2721. st->align = (unsigned char)dcl->val;
  2722. break;
  2723. case PROP_UNDERLINE:
  2724. st->underline = (unsigned char)(dcl->val != 0);
  2725. break;
  2726. case PROP_DISPLAY:
  2727. st->display = (unsigned char)dcl->val;
  2728. break;
  2729. case PROP_VIS_HIDDEN:
  2730. st->vis_hidden = (unsigned char)(dcl->val != 0);
  2731. break;
  2732. case PROP_OPACITY_HIDDEN:
  2733. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  2734. break;
  2735. case PROP_OVERFLOW_HIDDEN:
  2736. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  2737. break;
  2738. case PROP_MT: st->mt = dcl->val; break;
  2739. case PROP_MR: st->mr = dcl->val; break;
  2740. case PROP_MB: st->mb = dcl->val; break;
  2741. case PROP_ML: st->ml = dcl->val; break;
  2742. case PROP_PT: st->pt = dcl->val; break;
  2743. case PROP_PR: st->pr = dcl->val; break;
  2744. case PROP_PB: st->pb = dcl->val; break;
  2745. case PROP_PL: st->pl = dcl->val; break;
  2746. case PROP_BORDER_W:
  2747. st->border_w = (unsigned char)dcl->val;
  2748. if (st->border_w && !st->border_color)
  2749. st->border_color = st->color;
  2750. break;
  2751. case PROP_BORDER_COLOR:
  2752. st->border_color = (unsigned short)dcl->val;
  2753. break;
  2754. case PROP_LIST_NONE:
  2755. st->no_bullet = (unsigned char)(dcl->val != 0);
  2756. break;
  2757. case PROP_WIDTH:
  2758. st->width = (unsigned short)dcl->val;
  2759. break;
  2760. case PROP_WIDTH_KIND:
  2761. st->width_kind = (unsigned char)dcl->val;
  2762. break;
  2763. case PROP_MAXW:
  2764. st->max_width = (unsigned short)dcl->val;
  2765. break;
  2766. case PROP_MAXW_KIND:
  2767. st->max_width_kind = (unsigned char)dcl->val;
  2768. break;
  2769. case PROP_HEIGHT:
  2770. st->height = (unsigned short)dcl->val;
  2771. break;
  2772. case PROP_HEIGHT_KIND:
  2773. st->height_kind = (unsigned char)dcl->val;
  2774. break;
  2775. case PROP_LH_PCT:
  2776. st->line_h_pct = (unsigned char)dcl->val;
  2777. break;
  2778. case PROP_LH_PX:
  2779. st->line_h_px = (unsigned short)dcl->val;
  2780. break;
  2781. case PROP_UPPER:
  2782. st->uppercase = (unsigned char)(dcl->val != 0);
  2783. break;
  2784. case PROP_FLOAT:
  2785. st->float_left = (unsigned char)dcl->val;
  2786. break;
  2787. case PROP_FLEX_GROW:
  2788. st->flex_grow = (unsigned char)dcl->val;
  2789. break;
  2790. case PROP_FLEX_COL:
  2791. st->flex_col = (unsigned char)(dcl->val != 0);
  2792. break;
  2793. case PROP_FLEX_WRAP:
  2794. st->flex_wrap = (unsigned char)(dcl->val != 0);
  2795. break;
  2796. case PROP_GRID_NCOLS:
  2797. st->grid_ncols = (unsigned char)dcl->val;
  2798. break;
  2799. case PROP_GRID_GAP:
  2800. st->grid_gap = (unsigned char)dcl->val;
  2801. break;
  2802. case PROP_GRID_SLOT:
  2803. st->grid_slot = (unsigned char)dcl->val;
  2804. break;
  2805. case PROP_POS:
  2806. st->pos = (unsigned char)dcl->val;
  2807. break;
  2808. case PROP_LEFT:
  2809. st->left = dcl->val;
  2810. break;
  2811. case PROP_TOP:
  2812. st->top = dcl->val;
  2813. break;
  2814. case PROP_LEFT_SET:
  2815. st->left_set = (unsigned char)(dcl->val != 0);
  2816. break;
  2817. case PROP_TOP_SET:
  2818. st->top_set = (unsigned char)(dcl->val != 0);
  2819. break;
  2820. default:
  2821. break;
  2822. }
  2823. }
  2824. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  2825. int spec, short *best, short *best_imp)
  2826. {
  2827. unsigned char p = dcl->prop;
  2828. if (p >= PROP_COUNT)
  2829. return;
  2830. if (dcl->important) {
  2831. if (spec >= best_imp[p]) {
  2832. apply_decl(st, dcl);
  2833. best_imp[p] = (short)spec;
  2834. }
  2835. } else if (best_imp[p] < 0 && spec >= best[p]) {
  2836. apply_decl(st, dcl);
  2837. best[p] = (short)spec;
  2838. }
  2839. }
  2840. void br_style_for(struct br_doc *d, unsigned int node,
  2841. const struct br_style *parent, struct br_style *out)
  2842. {
  2843. unsigned int tag;
  2844. short best[PROP_COUNT], best_imp[PROP_COUNT];
  2845. unsigned int i;
  2846. if (d->nodes[node].style_valid) {
  2847. *out = d->nodes[node].style;
  2848. return;
  2849. }
  2850. *out = *parent;
  2851. out->has_bg = 0;
  2852. out->float_left = 0;
  2853. out->border_w = 0;
  2854. out->border_color = 0;
  2855. out->width = out->max_width = out->height = 0;
  2856. out->width_kind = out->max_width_kind = out->height_kind = 0;
  2857. out->mt = out->mr = out->mb = out->ml = 0;
  2858. out->pt = out->pr = out->pb = out->pl = 0;
  2859. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  2860. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  2861. out->pos = out->left_set = out->top_set = 0;
  2862. out->left = out->top = 0;
  2863. out->vis_hidden = out->opacity_hidden = 0;
  2864. out->overflow_hidden = 0;
  2865. tag = d->nodes[node].tag;
  2866. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  2867. BR_DISPLAY_INLINE;
  2868. for (i = 0; i < PROP_COUNT; i++) {
  2869. best[i] = -1;
  2870. best_imp[i] = -1;
  2871. }
  2872. switch (tag) {
  2873. case BR_TAG_HEAD:
  2874. case BR_TAG_TITLE:
  2875. case BR_TAG_META:
  2876. case BR_TAG_LINK:
  2877. case BR_TAG_BASE:
  2878. case BR_TAG_SCRIPT:
  2879. case BR_TAG_STYLE:
  2880. out->display = BR_DISPLAY_NONE;
  2881. break;
  2882. case BR_TAG_BODY:
  2883. out->mt = out->mb = out->ml = out->mr = 4;
  2884. break;
  2885. case BR_TAG_P:
  2886. out->mt = out->mb = 8;
  2887. break;
  2888. case BR_TAG_H1:
  2889. out->scale = 2; out->bold = 1;
  2890. out->mt = 10; out->mb = 6;
  2891. break;
  2892. case BR_TAG_H2:
  2893. out->scale = 2; out->bold = 1;
  2894. out->mt = 8; out->mb = 4;
  2895. break;
  2896. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  2897. out->bold = 1; out->mt = 6; out->mb = 3;
  2898. break;
  2899. case BR_TAG_UL: case BR_TAG_OL:
  2900. out->mt = out->mb = 6; out->pl = 20;
  2901. break;
  2902. case BR_TAG_LI:
  2903. out->mt = out->mb = 2;
  2904. break;
  2905. case BR_TAG_BLOCKQUOTE:
  2906. out->mt = out->mb = 6; out->ml = out->mr = 16;
  2907. break;
  2908. case BR_TAG_PRE:
  2909. out->mt = out->mb = 8; out->pl = 8;
  2910. out->has_bg = 1; out->bg = theme_panel;
  2911. break;
  2912. case BR_TAG_CENTER:
  2913. out->align = BR_ALIGN_CENTER;
  2914. break;
  2915. case BR_TAG_A:
  2916. out->color = theme_link; out->underline = 1;
  2917. break;
  2918. case BR_TAG_B: case BR_TAG_STRONG:
  2919. out->bold = 1;
  2920. break;
  2921. case BR_TAG_CODE:
  2922. out->color = 0x8010;
  2923. break;
  2924. case BR_TAG_TD: case BR_TAG_TH:
  2925. out->pr = 12;
  2926. break;
  2927. case BR_TAG_VIDEO:
  2928. out->display = BR_DISPLAY_BLOCK;
  2929. break;
  2930. default:
  2931. break;
  2932. }
  2933. /* presentational <font color>: author CSS below still wins */
  2934. if (d->nodes[node].color_off) {
  2935. unsigned short c;
  2936. const char *v = attr_ptr(d, d->nodes[node].color_off);
  2937. if (parse_color(v, s_len(v), &c))
  2938. out->color = c;
  2939. }
  2940. rule_collect(d, node);
  2941. for (i = 0; i < nrule_matches; i++) {
  2942. struct br_rule *r = &d->rules[rule_matches[i].idx];
  2943. int spec = rule_matches[i].spec;
  2944. unsigned int k;
  2945. for (k = 0; k < r->nd; k++)
  2946. cascade_decl(out, &r->d[k], spec, best, best_imp);
  2947. }
  2948. if (d->nodes[node].style_off) {
  2949. struct br_decl decls[BR_DECLS];
  2950. unsigned int nd, k;
  2951. const char *v = attr_ptr(d, d->nodes[node].style_off);
  2952. parse_decls(d, v, s_len(v), decls, &nd);
  2953. for (k = 0; k < nd; k++)
  2954. cascade_decl(out, &decls[k], 1000, best, best_imp);
  2955. }
  2956. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  2957. out->display = BR_DISPLAY_NONE;
  2958. if (out->vis_hidden || out->opacity_hidden ||
  2959. (out->overflow_hidden && out->height_kind == 1 &&
  2960. out->height == 0))
  2961. out->display = BR_DISPLAY_NONE;
  2962. d->nodes[node].style = *out;
  2963. d->nodes[node].style_valid = 1;
  2964. }
  2965. /* ---- HTML parser ---- */
  2966. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  2967. unsigned int sp, int *pre)
  2968. {
  2969. unsigned int i;
  2970. *pre = 0;
  2971. for (i = 1; i <= sp; i++)
  2972. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  2973. (*pre)++;
  2974. }
  2975. static void implicit_close(struct br_doc *d, unsigned short *stack,
  2976. unsigned int *sp, unsigned int tag)
  2977. {
  2978. while (*sp > 0) {
  2979. unsigned int top = d->nodes[stack[*sp]].tag;
  2980. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  2981. (*sp)--;
  2982. continue;
  2983. }
  2984. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  2985. (*sp)--;
  2986. continue;
  2987. }
  2988. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  2989. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  2990. (*sp)--;
  2991. continue;
  2992. }
  2993. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  2994. (*sp)--;
  2995. continue;
  2996. }
  2997. break;
  2998. }
  2999. }
  3000. static int has_attr_token(const char *s, unsigned int len, const char *name)
  3001. {
  3002. unsigned int i, n = s_len(name);
  3003. for (i = 0; i + n <= len; i++) {
  3004. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  3005. continue;
  3006. if (!ci_eq_n(s + i, name, n))
  3007. continue;
  3008. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  3009. return 1;
  3010. }
  3011. return 0;
  3012. }
  3013. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  3014. {
  3015. unsigned short stack[64];
  3016. unsigned int sp = 0;
  3017. unsigned int i = 0;
  3018. int pre = 0;
  3019. unsigned int css_len = 0;
  3020. if (!d)
  3021. return -1;
  3022. if (!d->nodes)
  3023. br_doc_init(d);
  3024. doc_reset(d);
  3025. stack[0] = 0;
  3026. d->nodes[0].type = BR_NODE_ELEM;
  3027. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3028. while (i < len) {
  3029. if (html[i] != '<') {
  3030. unsigned int st = i;
  3031. while (i < len && html[i] != '<')
  3032. i++;
  3033. if (sp <= 63)
  3034. append_text(d, stack[sp], html + st, i - st, pre);
  3035. continue;
  3036. }
  3037. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  3038. html[i + 3] == '-') {
  3039. int p = find_ci(html, len, "-->", 3, i + 4);
  3040. i = p >= 0 ? (unsigned int)p + 3 : len;
  3041. continue;
  3042. }
  3043. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  3044. while (i < len && html[i] != '>')
  3045. i++;
  3046. if (i < len)
  3047. i++;
  3048. continue;
  3049. }
  3050. if (i + 1 < len && html[i + 1] == '/') {
  3051. unsigned int ns = i + 2, ne, tag;
  3052. while (ns < len && is_space(html[ns]))
  3053. ns++;
  3054. ne = ns;
  3055. while (ne < len && is_ident(html[ne]))
  3056. ne++;
  3057. if (ne == ns) {
  3058. i++;
  3059. continue;
  3060. }
  3061. {
  3062. char name[16];
  3063. unsigned int k, n = ne - ns;
  3064. if (n >= sizeof(name))
  3065. n = sizeof(name) - 1;
  3066. for (k = 0; k < n; k++)
  3067. name[k] = html[ns + k];
  3068. name[n] = '\0';
  3069. tag = br_tag_from_name(name);
  3070. }
  3071. i = ne;
  3072. while (i < len && html[i] != '>')
  3073. i++;
  3074. if (i < len)
  3075. i++;
  3076. {
  3077. unsigned int k;
  3078. for (k = sp; k > 0; k--) {
  3079. if (d->nodes[stack[k]].tag == tag) {
  3080. sp = k - 1;
  3081. break;
  3082. }
  3083. }
  3084. stack_recount_pre(d, stack, sp, &pre);
  3085. }
  3086. continue;
  3087. }
  3088. /* start tag */
  3089. {
  3090. unsigned int ns = i + 1, ne, tag, k, n;
  3091. char name[16];
  3092. unsigned int attr_start, attr_end;
  3093. int selfclose = 0;
  3094. while (ns < len && is_space(html[ns]))
  3095. ns++;
  3096. ne = ns;
  3097. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  3098. ne++;
  3099. if (ne == ns) {
  3100. /* stray '<' */
  3101. append_text(d, stack[sp], html + i, 1, pre);
  3102. i++;
  3103. continue;
  3104. }
  3105. n = ne - ns;
  3106. if (n >= sizeof(name))
  3107. n = sizeof(name) - 1;
  3108. for (k = 0; k < n; k++)
  3109. name[k] = html[ns + k];
  3110. name[n] = '\0';
  3111. tag = br_tag_from_name(name);
  3112. attr_start = ne;
  3113. {
  3114. unsigned int q = ne;
  3115. char quote = 0;
  3116. while (q < len) {
  3117. char c = html[q];
  3118. if (quote) {
  3119. if (c == quote)
  3120. quote = 0;
  3121. } else if (c == '"' || c == '\'') {
  3122. quote = c;
  3123. } else if (c == '>') {
  3124. break;
  3125. }
  3126. q++;
  3127. }
  3128. attr_end = q;
  3129. if (q > attr_start && html[q - 1] == '/')
  3130. selfclose = 1;
  3131. i = q < len ? q + 1 : len;
  3132. }
  3133. if (tag == BR_TAG_SCRIPT) {
  3134. int p = find_ci(html, len, "</script", 8, i);
  3135. i = p >= 0 ? (unsigned int)p + 8 : len;
  3136. while (i < len && html[i] != '>')
  3137. i++;
  3138. if (i < len)
  3139. i++;
  3140. continue;
  3141. }
  3142. if (tag == BR_TAG_SVG && !selfclose) {
  3143. /* Inline SVG is not rendered; skip its subtree so
  3144. * logos and icon sprites do not consume the node
  3145. * arena. */
  3146. int p = find_ci(html, len, "</svg>", 6, i);
  3147. i = p >= 0 ? (unsigned int)p + 6 : len;
  3148. continue;
  3149. }
  3150. if (tag == BR_TAG_DETAILS && !selfclose &&
  3151. !has_attr_token(html + attr_start,
  3152. attr_end - attr_start, "open")) {
  3153. /* Closed <details> is collapsed (and this browser has
  3154. * no script to open it); skipping the subtree keeps
  3155. * mobile mega-menus out of the node arena. */
  3156. int p = find_ci(html, len, "</details>", 10, i);
  3157. i = p >= 0 ? (unsigned int)p + 10 : len;
  3158. continue;
  3159. }
  3160. if (tag == BR_TAG_STYLE) {
  3161. int p = find_ci(html, len, "</style", 7, i);
  3162. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3163. unsigned int cend = end;
  3164. while (cend > i && is_space(html[cend - 1]))
  3165. cend--;
  3166. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  3167. for (k = i; k < cend; k++)
  3168. css_store[css_len++] = html[k];
  3169. css_store[css_len++] = '\n';
  3170. }
  3171. i = p >= 0 ? (unsigned int)p + 7 : len;
  3172. while (i < len && html[i] != '>')
  3173. i++;
  3174. if (i < len)
  3175. i++;
  3176. continue;
  3177. }
  3178. if (tag == BR_TAG_TITLE) {
  3179. int p = find_ci(html, len, "</title", 7, i);
  3180. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3181. unsigned int tn = 0;
  3182. char tmp[BR_TITLE_MAX];
  3183. tn = decode_text(html + i, end - i, tmp,
  3184. sizeof(tmp), 1);
  3185. (void)tn;
  3186. s_copy(d->title, tmp, sizeof(d->title));
  3187. i = p >= 0 ? (unsigned int)p + 7 : len;
  3188. while (i < len && html[i] != '>')
  3189. i++;
  3190. if (i < len)
  3191. i++;
  3192. continue;
  3193. }
  3194. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  3195. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  3196. unsigned int idx;
  3197. implicit_close(d, stack, &sp, tag);
  3198. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  3199. (unsigned char)tag);
  3200. if (idx)
  3201. parse_attrs(d, &d->nodes[idx],
  3202. html + attr_start,
  3203. attr_end - attr_start);
  3204. continue;
  3205. }
  3206. if (sp >= 63) {
  3207. d->truncated = 1;
  3208. continue;
  3209. }
  3210. implicit_close(d, stack, &sp, tag);
  3211. {
  3212. unsigned int idx = node_append(d, stack[sp],
  3213. BR_NODE_ELEM,
  3214. (unsigned char)tag);
  3215. if (!idx)
  3216. continue;
  3217. parse_attrs(d, &d->nodes[idx],
  3218. html + attr_start,
  3219. attr_end - attr_start);
  3220. if (!selfclose) {
  3221. sp++;
  3222. stack[sp] = (unsigned short)idx;
  3223. if (tag == BR_TAG_PRE)
  3224. pre++;
  3225. }
  3226. }
  3227. }
  3228. }
  3229. d->inline_css_len = css_len;
  3230. return 0;
  3231. }
  3232. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  3233. {
  3234. if (!d->nodes)
  3235. br_doc_init(d);
  3236. doc_reset(d);
  3237. d->plain = 1;
  3238. d->nodes[0].type = BR_NODE_ELEM;
  3239. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3240. append_text(d, 0, text, len, 1);
  3241. return 0;
  3242. }
  3243. /* A standalone image response: a synthetic document with one <img>. The
  3244. * caller decodes the already-fetched body into image slot 0 and sets the
  3245. * returned node's img_index. */
  3246. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  3247. {
  3248. unsigned int idx;
  3249. if (!d->nodes)
  3250. br_doc_init(d);
  3251. doc_reset(d);
  3252. d->nodes[0].type = BR_NODE_ELEM;
  3253. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3254. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  3255. if (!idx)
  3256. return 0;
  3257. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  3258. return idx;
  3259. }