browser_html.c 81 KB

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