browser_html.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  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. /* ---- storage (one document per process) ---- */
  6. static struct br_node nodes_store[BR_MAX_NODES];
  7. static char text_store[BR_TEXT_SIZE];
  8. static char attr_store[BR_ATTR_SIZE];
  9. static struct br_rule rule_store[BR_MAX_RULES];
  10. static char css_store[BR_CSS_SIZE];
  11. /* ---- tag table ---- */
  12. struct tag_name {
  13. const char *name;
  14. unsigned char tag;
  15. };
  16. static const struct tag_name tag_names[] = {
  17. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  18. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  19. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  20. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  21. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  22. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  23. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  24. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  25. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  26. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  27. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  28. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  29. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  30. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  31. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  32. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  33. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  34. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  35. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  36. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  37. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  38. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  39. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  40. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  41. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  42. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  43. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  44. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  45. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  46. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  47. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  48. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  49. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  50. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  51. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  52. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  53. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  54. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  55. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  56. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  57. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  58. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  59. { 0, 0 }
  60. };
  61. const char *br_tag_name(unsigned int tag)
  62. {
  63. unsigned int i;
  64. for (i = 0; tag_names[i].name; i++)
  65. if (tag_names[i].tag == tag)
  66. return tag_names[i].name;
  67. return "?";
  68. }
  69. unsigned int br_tag_from_name(const char *name)
  70. {
  71. unsigned int i, j;
  72. char lower[16];
  73. for (i = 0; i < 15 && name[i]; i++) {
  74. char c = name[i];
  75. if (c >= 'A' && c <= 'Z')
  76. c = (char)(c - 'A' + 'a');
  77. lower[i] = c;
  78. }
  79. lower[i] = '\0';
  80. for (i = 0; tag_names[i].name; i++) {
  81. for (j = 0; lower[j] && tag_names[i].name[j] == lower[j]; j++)
  82. ;
  83. if (!lower[j] && !tag_names[i].name[j])
  84. return tag_names[i].tag;
  85. }
  86. return BR_TAG_UNKNOWN;
  87. }
  88. int br_tag_is_void(unsigned int tag)
  89. {
  90. switch (tag) {
  91. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  92. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  93. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  94. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  95. case BR_TAG_AREA: case BR_TAG_COL:
  96. return 1;
  97. default:
  98. return 0;
  99. }
  100. }
  101. int br_tag_is_block(unsigned int tag)
  102. {
  103. switch (tag) {
  104. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  105. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  106. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  107. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  108. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  109. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  110. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  111. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  112. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  113. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  114. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  115. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  116. case BR_TAG_ADDRESS: case BR_TAG_HR:
  117. return 1;
  118. default:
  119. return 0;
  120. }
  121. }
  122. /* ---- string / entity helpers ---- */
  123. static unsigned int s_len(const char *s)
  124. {
  125. unsigned int n = 0;
  126. while (s && s[n])
  127. n++;
  128. return n;
  129. }
  130. static void s_copy(char *d, const char *s, unsigned int cap)
  131. {
  132. unsigned int i = 0;
  133. if (!cap)
  134. return;
  135. while (s && s[i] && i + 1 < cap) {
  136. d[i] = s[i];
  137. i++;
  138. }
  139. d[i] = '\0';
  140. }
  141. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  142. {
  143. unsigned int i;
  144. for (i = 0; i < n; i++) {
  145. char ca = a[i], cb = b[i];
  146. if (ca >= 'A' && ca <= 'Z')
  147. ca = (char)(ca - 'A' + 'a');
  148. if (cb >= 'A' && cb <= 'Z')
  149. cb = (char)(cb - 'A' + 'a');
  150. if (ca != cb)
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. static int find_ci(const char *hay, unsigned int len, const char *needle,
  156. unsigned int nlen, unsigned int from)
  157. {
  158. unsigned int i;
  159. if (nlen == 0 || from >= len)
  160. return -1;
  161. for (i = from; i + nlen <= len; i++)
  162. if (ci_eq_n(hay + i, needle, nlen))
  163. return (int)i;
  164. return -1;
  165. }
  166. static const struct {
  167. const char *name;
  168. const char *text;
  169. } entities[] = {
  170. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  171. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  172. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  173. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  174. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  175. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  176. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  177. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  178. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  179. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  180. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  181. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  182. { 0, 0 }
  183. };
  184. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  185. {
  186. if (*n + 1 < cap)
  187. out[(*n)++] = c;
  188. }
  189. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  190. char *out, unsigned int cap, unsigned int *n)
  191. {
  192. unsigned int start = *pos + 1, i = start;
  193. unsigned int code = 0;
  194. int ok = 0;
  195. while (i < len && s[i] != ';' && i - start < 12)
  196. i++;
  197. if (i >= len || s[i] != ';') {
  198. /* Not an entity: emit the '&' and advance past it. Leaving
  199. * *pos unchanged here used to spin forever on bare '&'. */
  200. emit(out, cap, n, '&');
  201. *pos = start;
  202. return;
  203. }
  204. if (s[start] == '#') {
  205. unsigned int j = start + 1;
  206. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  207. j++;
  208. for (; j < i; j++) {
  209. char c = s[j];
  210. int d = -1;
  211. if (c >= '0' && c <= '9') d = c - '0';
  212. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  213. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  214. if (d < 0) break;
  215. code = code * 16 + (unsigned int)d;
  216. ok = 1;
  217. }
  218. } else {
  219. for (; j < i; j++) {
  220. if (s[j] < '0' || s[j] > '9')
  221. break;
  222. code = code * 10 + (unsigned int)(s[j] - '0');
  223. ok = 1;
  224. }
  225. }
  226. } else {
  227. unsigned int k;
  228. for (k = 0; entities[k].name; k++) {
  229. unsigned int el = s_len(entities[k].name);
  230. unsigned int j;
  231. if (el != i - start)
  232. continue;
  233. for (j = 0; j < el; j++)
  234. if (s[start + j] != entities[k].name[j])
  235. break;
  236. if (j == el) {
  237. const char *t = entities[k].text;
  238. while (*t)
  239. emit(out, cap, n, *t++);
  240. *pos = i + 1;
  241. return;
  242. }
  243. }
  244. }
  245. if (ok && code) {
  246. if (code == 0x00A0)
  247. emit(out, cap, n, ' ');
  248. else if (code == 0x2013 || code == 0x2014)
  249. emit(out, cap, n, '-');
  250. else if (code == 0x2018 || code == 0x2019)
  251. emit(out, cap, n, '\'');
  252. else if (code == 0x201C || code == 0x201D)
  253. emit(out, cap, n, '"');
  254. else if (code == 0x2026) {
  255. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  256. emit(out, cap, n, '.');
  257. } else if (code <= 0xFF) {
  258. emit(out, cap, n, (char)code);
  259. } else {
  260. emit(out, cap, n, '?');
  261. }
  262. } else {
  263. emit(out, cap, n, '?');
  264. }
  265. *pos = i + 1;
  266. }
  267. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  268. * space and drops leading spaces. Returns the output length. */
  269. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  270. unsigned int cap, int collapse)
  271. {
  272. unsigned int i = 0, n = 0;
  273. while (i < len) {
  274. unsigned char c = (unsigned char)s[i];
  275. if (c == '&') {
  276. decode_entity(s, len, &i, out, cap, &n);
  277. continue;
  278. }
  279. if (c < 0x80) {
  280. i++;
  281. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  282. c == '\r' || c == '\f')) {
  283. if (n > 0 && out[n - 1] == ' ')
  284. continue;
  285. emit(out, cap, &n, ' ');
  286. continue;
  287. }
  288. if (!collapse && c == '\t') {
  289. emit(out, cap, &n, ' ');
  290. emit(out, cap, &n, ' ');
  291. emit(out, cap, &n, ' ');
  292. emit(out, cap, &n, ' ');
  293. continue;
  294. }
  295. emit(out, cap, &n, (char)c);
  296. continue;
  297. }
  298. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  299. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  300. unsigned int cp = ((c & 0x1F) << 6) |
  301. ((unsigned char)s[i + 1] & 0x3F);
  302. i += 2;
  303. if (cp == 0xA0)
  304. cp = ' ';
  305. if (cp <= 0xFF)
  306. emit(out, cap, &n, (char)cp);
  307. else
  308. emit(out, cap, &n, '?');
  309. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  310. unsigned int cp = ((c & 0x0F) << 12) |
  311. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  312. ((unsigned char)s[i + 2] & 0x3F);
  313. i += 3;
  314. if (cp == 0x2013 || cp == 0x2014) {
  315. emit(out, cap, &n, '-');
  316. } else if (cp == 0x2018 || cp == 0x2019) {
  317. emit(out, cap, &n, '\'');
  318. } else if (cp == 0x201C || cp == 0x201D) {
  319. emit(out, cap, &n, '"');
  320. } else if (cp == 0x2026) {
  321. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  322. emit(out, cap, &n, '.');
  323. } else {
  324. emit(out, cap, &n, '?');
  325. }
  326. } else {
  327. i++;
  328. emit(out, cap, &n, '?');
  329. }
  330. }
  331. out[n] = '\0';
  332. return n;
  333. }
  334. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  335. * space / empty. */
  336. static unsigned short store_attr(struct br_doc *d, const char *s,
  337. unsigned int len, unsigned int limit)
  338. {
  339. unsigned int n;
  340. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  341. return 0;
  342. if (len > limit)
  343. len = limit;
  344. n = decode_text(s, len, d->attrs + d->attrs_used,
  345. BR_ATTR_SIZE - d->attrs_used, 0);
  346. if (n == 0)
  347. return 0;
  348. {
  349. unsigned short off = (unsigned short)d->attrs_used;
  350. d->attrs_used += n + 1;
  351. return off;
  352. }
  353. }
  354. static const char *attr_ptr(struct br_doc *d, unsigned short off)
  355. {
  356. return off ? d->attrs + off : "";
  357. }
  358. const char *br_href(struct br_doc *d, unsigned int node)
  359. {
  360. return attr_ptr(d, d->nodes[node].href_off);
  361. }
  362. const char *br_src(struct br_doc *d, unsigned int node)
  363. {
  364. return attr_ptr(d, d->nodes[node].src_off);
  365. }
  366. const char *br_alt(struct br_doc *d, unsigned int node)
  367. {
  368. return attr_ptr(d, d->nodes[node].alt_off);
  369. }
  370. const char *br_rel(struct br_doc *d, unsigned int node)
  371. {
  372. return attr_ptr(d, d->nodes[node].rel_off);
  373. }
  374. /* ---- document setup / node append ---- */
  375. void br_doc_init(struct br_doc *d)
  376. {
  377. unsigned int i;
  378. for (i = 0; i < sizeof(*d); i++)
  379. ((unsigned char *)d)[i] = 0;
  380. d->nodes = nodes_store;
  381. d->text = text_store;
  382. d->attrs = attr_store;
  383. d->rules = rule_store;
  384. d->attrs_used = 1; /* offset 0 = "no attribute" */
  385. d->attrs[0] = '\0';
  386. d->root = 0;
  387. }
  388. static void doc_reset(struct br_doc *d)
  389. {
  390. unsigned int i;
  391. d->nnode = 0;
  392. d->text_used = 0;
  393. d->attrs_used = 1;
  394. d->attrs[0] = '\0';
  395. d->nrules = 0;
  396. d->title[0] = '\0';
  397. d->inline_css_len = 0;
  398. d->truncated = 0;
  399. d->plain = 0;
  400. for (i = 0; i < BR_MAX_NODES; i++) {
  401. d->nodes[i].type = BR_NODE_ELEM;
  402. d->nodes[i].tag = BR_TAG_UNKNOWN;
  403. d->nodes[i].flags = 0;
  404. d->nodes[i].parent = 0;
  405. d->nodes[i].first = d->nodes[i].last = 0;
  406. d->nodes[i].next = 0;
  407. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  408. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  409. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  410. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  411. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  412. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  413. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  414. d->nodes[i].img_index = -1;
  415. d->nodes[i].style_valid = 0;
  416. }
  417. }
  418. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  419. unsigned char type, unsigned char tag)
  420. {
  421. unsigned int idx;
  422. struct br_node *n;
  423. if (d->nnode >= BR_MAX_NODES) {
  424. d->truncated = 1;
  425. return 0;
  426. }
  427. idx = d->nnode++;
  428. n = &d->nodes[idx];
  429. n->type = type;
  430. n->tag = tag;
  431. n->parent = (unsigned short)parent;
  432. n->img_index = -1;
  433. if (d->nodes[parent].last)
  434. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  435. else
  436. d->nodes[parent].first = (unsigned short)idx;
  437. d->nodes[parent].last = (unsigned short)idx;
  438. return idx;
  439. }
  440. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  441. const char *s, unsigned int len, int pre)
  442. {
  443. char *dst;
  444. unsigned int n;
  445. unsigned int idx;
  446. if (len == 0)
  447. return 0;
  448. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  449. d->truncated = 1;
  450. return 0;
  451. }
  452. dst = d->text + d->text_used;
  453. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  454. if (n == 0) {
  455. d->text[d->text_used] = '\0';
  456. return 0;
  457. }
  458. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  459. if (!idx)
  460. return 0;
  461. d->nodes[idx].text_off = (unsigned short)d->text_used;
  462. d->nodes[idx].text_len = (unsigned short)n;
  463. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  464. d->text_used += n + 1;
  465. return idx;
  466. }
  467. /* ---- attribute parsing ---- */
  468. static int is_space(char c)
  469. {
  470. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  471. }
  472. static int str_eq(const char *a, const char *b);
  473. static unsigned int parse_uint(const char *s, unsigned int len)
  474. {
  475. unsigned int i = 0, v = 0;
  476. while (i < len && is_space(s[i]))
  477. i++;
  478. while (i < len && s[i] >= '0' && s[i] <= '9') {
  479. v = v * 10 + (unsigned int)(s[i] - '0');
  480. if (v > 4096)
  481. v = 4096;
  482. i++;
  483. }
  484. return v;
  485. }
  486. /* Parse attributes from `s` (between the tag name and '>'). */
  487. static void parse_attrs(struct br_doc *d, struct br_node *n,
  488. const char *s, unsigned int len)
  489. {
  490. unsigned int i = 0;
  491. while (i < len) {
  492. char name[16];
  493. unsigned int nl = 0, vstart, vlen;
  494. char quote = 0;
  495. while (i < len && is_space(s[i]))
  496. i++;
  497. if (i >= len || s[i] == '/')
  498. break;
  499. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  500. char c = s[i++];
  501. if (c >= 'A' && c <= 'Z')
  502. c = (char)(c - 'A' + 'a');
  503. if (nl + 1 < sizeof(name))
  504. name[nl++] = c;
  505. }
  506. name[nl] = '\0';
  507. while (i < len && is_space(s[i]))
  508. i++;
  509. if (i >= len || s[i] != '=') {
  510. /* boolean attribute; nothing we use needs it */
  511. continue;
  512. }
  513. i++;
  514. while (i < len && is_space(s[i]))
  515. i++;
  516. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  517. quote = s[i++];
  518. vstart = i;
  519. while (i < len && s[i] != quote)
  520. i++;
  521. vlen = i - vstart;
  522. if (i < len)
  523. i++;
  524. } else {
  525. vstart = i;
  526. while (i < len && !is_space(s[i]) && s[i] != '>')
  527. i++;
  528. vlen = i - vstart;
  529. }
  530. if (!nl)
  531. continue;
  532. if (str_eq(name, "href"))
  533. n->href_off = store_attr(d, s + vstart, vlen, 220);
  534. else if (str_eq(name, "class"))
  535. n->class_off = store_attr(d, s + vstart, vlen, 120);
  536. else if (str_eq(name, "id"))
  537. n->id_off = store_attr(d, s + vstart, vlen, 64);
  538. else if (str_eq(name, "style"))
  539. n->style_off = store_attr(d, s + vstart, vlen, 240);
  540. else if (str_eq(name, "src"))
  541. n->src_off = store_attr(d, s + vstart, vlen, 220);
  542. else if (str_eq(name, "alt"))
  543. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  544. else if (str_eq(name, "color"))
  545. n->color_off = store_attr(d, s + vstart, vlen, 32);
  546. else if (str_eq(name, "rel"))
  547. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  548. else if (str_eq(name, "width")) {
  549. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  550. if (vlen && s[vstart + vlen - 1] == '%')
  551. n->width_pct = 1;
  552. } else if (str_eq(name, "height")) {
  553. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  554. if (vlen && s[vstart + vlen - 1] == '%')
  555. n->height_pct = 1;
  556. }
  557. }
  558. }
  559. static int str_eq(const char *a, const char *b)
  560. {
  561. unsigned int i = 0;
  562. while (a[i] && b[i] && a[i] == b[i])
  563. i++;
  564. return a[i] == '\0' && b[i] == '\0';
  565. }
  566. /* ---- CSS ---- */
  567. enum {
  568. PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
  569. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  570. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  571. PROP_BORDER_W, PROP_BORDER_COLOR, PROP_LIST_NONE,
  572. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  573. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  574. PROP_FLOAT,
  575. PROP_COUNT
  576. };
  577. struct color_name {
  578. const char *name;
  579. unsigned short rgb;
  580. };
  581. static const struct color_name color_names[] = {
  582. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  583. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  584. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  585. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  586. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  587. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  588. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  589. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  590. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  591. { "darkgrey", 0x528A }, { 0, 0 }
  592. };
  593. static int hexval(char c)
  594. {
  595. if (c >= '0' && c <= '9')
  596. return c - '0';
  597. if (c >= 'a' && c <= 'f')
  598. return c - 'a' + 10;
  599. if (c >= 'A' && c <= 'F')
  600. return c - 'A' + 10;
  601. return -1;
  602. }
  603. static int named_color(const char *s, unsigned int len, unsigned short *out)
  604. {
  605. unsigned int i;
  606. for (i = 0; color_names[i].name; i++) {
  607. unsigned int j, n = s_len(color_names[i].name);
  608. if (n != len)
  609. continue;
  610. for (j = 0; j < n; j++) {
  611. char c = s[j];
  612. if (c >= 'A' && c <= 'Z')
  613. c = (char)(c - 'A' + 'a');
  614. if (c != color_names[i].name[j])
  615. break;
  616. }
  617. if (j == n) {
  618. *out = color_names[i].rgb;
  619. return 1;
  620. }
  621. }
  622. return 0;
  623. }
  624. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  625. * over white; the render target is a white page. */
  626. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  627. {
  628. unsigned int i = 0;
  629. while (i < len && is_space(s[i]))
  630. i++;
  631. if (i < len && s[i] == '#') {
  632. int d[6], nd = 0;
  633. i++;
  634. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  635. d[nd++] = hexval(s[i++]);
  636. if (nd == 3) {
  637. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  638. unsigned int b = (unsigned int)d[2];
  639. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  640. ((g * 17 >> 2) << 5) |
  641. (b * 17 >> 3));
  642. return 1;
  643. }
  644. if (nd >= 6) {
  645. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  646. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  647. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  648. *out = (unsigned short)(((r >> 3) << 11) |
  649. ((g >> 2) << 5) | (b >> 3));
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  655. unsigned int v[3], k = 0, a = 255;
  656. int is_rgba = (i + 4 <= len && ci_eq_n(s + i, "rgba", 4));
  657. while (i < len && s[i] != '(')
  658. i++;
  659. if (i >= len)
  660. return 0;
  661. i++;
  662. while (k < 3 && i < len) {
  663. unsigned int x = 0;
  664. while (i < len && is_space(s[i]))
  665. i++;
  666. while (i < len && s[i] >= '0' && s[i] <= '9') {
  667. x = x * 10 + (unsigned int)(s[i] - '0');
  668. i++;
  669. }
  670. if (i < len && s[i] == '%') {
  671. x = x * 255 / 100;
  672. i++;
  673. }
  674. if (x > 255)
  675. x = 255;
  676. v[k++] = x;
  677. while (i < len && s[i] != ',' && s[i] != ')')
  678. i++;
  679. if (i < len && s[i] == ',')
  680. i++;
  681. }
  682. if (is_rgba && i < len) {
  683. unsigned int whole = 0, frac = 0, scale = 1;
  684. int percent = 0;
  685. while (i < len && is_space(s[i]))
  686. i++;
  687. while (i < len && s[i] >= '0' && s[i] <= '9') {
  688. whole = whole * 10 + (unsigned int)(s[i] - '0');
  689. i++;
  690. }
  691. if (i < len && s[i] == '.') {
  692. i++;
  693. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  694. frac = frac * 10 + (unsigned int)(s[i] - '0');
  695. scale *= 10;
  696. i++;
  697. }
  698. }
  699. while (i < len && is_space(s[i]))
  700. i++;
  701. if (i < len && s[i] == '%')
  702. percent = 1;
  703. if (percent) {
  704. a = whole > 100 ? 255 : whole * 255 / 100;
  705. } else {
  706. a = whole >= 1 ? 255 : (frac * 255) / scale;
  707. }
  708. if (a > 255)
  709. a = 255;
  710. }
  711. if (k == 3) {
  712. unsigned int r = v[0], g = v[1], b = v[2];
  713. if (a < 255) {
  714. r = (r * a + 255u * (255u - a)) / 255u;
  715. g = (g * a + 255u * (255u - a)) / 255u;
  716. b = (b * a + 255u * (255u - a)) / 255u;
  717. }
  718. *out = (unsigned short)(((r >> 3) << 11) |
  719. ((g >> 2) << 5) | (b >> 3));
  720. return 1;
  721. }
  722. return 0;
  723. }
  724. {
  725. unsigned int j = i;
  726. while (i < len && !is_space(s[i]))
  727. i++;
  728. return named_color(s + j, i - j, out);
  729. }
  730. }
  731. /* Length values for margins/padding: px, ignore percentages/units. */
  732. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  733. {
  734. unsigned int i = *pos;
  735. int sign = 1;
  736. int v = 0;
  737. while (i < len && is_space(s[i]))
  738. i++;
  739. if (i < len && (s[i] == '-' || s[i] == '+')) {
  740. if (s[i] == '-')
  741. sign = -1;
  742. i++;
  743. }
  744. while (i < len && s[i] >= '0' && s[i] <= '9') {
  745. v = v * 10 + (s[i] - '0');
  746. i++;
  747. }
  748. if (i < len && s[i] == '.') {
  749. i++;
  750. while (i < len && s[i] >= '0' && s[i] <= '9')
  751. i++;
  752. }
  753. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  754. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  755. s[i] == 'a' || s[i] == 'd'))
  756. i++;
  757. *pos = i;
  758. v *= sign;
  759. if (v < -32)
  760. v = -32;
  761. if (v > 96)
  762. v = 96;
  763. return v;
  764. }
  765. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  766. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  767. unsigned char *kind)
  768. {
  769. unsigned int i = 0;
  770. unsigned int v = 0;
  771. int digits = 0;
  772. int pct = 0;
  773. *val = 0;
  774. *kind = 0;
  775. while (i < len && is_space(s[i]))
  776. i++;
  777. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  778. return 0;
  779. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  780. return 0;
  781. while (i < len && s[i] >= '0' && s[i] <= '9') {
  782. v = v * 10 + (unsigned int)(s[i] - '0');
  783. digits = 1;
  784. i++;
  785. }
  786. if (i < len && s[i] == '.') {
  787. i++;
  788. while (i < len && s[i] >= '0' && s[i] <= '9')
  789. i++;
  790. }
  791. if (!digits)
  792. return 0;
  793. while (i < len && is_space(s[i]))
  794. i++;
  795. if (i < len && s[i] == '%')
  796. pct = 1;
  797. if (pct) {
  798. if (v > 100)
  799. v = 100;
  800. *kind = 2;
  801. } else {
  802. if (v > 2048)
  803. v = 2048;
  804. *kind = 1;
  805. }
  806. *val = (unsigned short)v;
  807. return 1;
  808. }
  809. static int parse_font_px(const char *s, unsigned int len)
  810. {
  811. unsigned int i = 0;
  812. int whole = 0, frac = 0, fscale = 1, digits = 0;
  813. int v1000, px;
  814. int is_px = 1, is_rem = 0, is_pct = 0;
  815. while (i < len && is_space(s[i]))
  816. i++;
  817. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  818. return 24;
  819. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  820. return 32;
  821. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  822. return 14;
  823. while (i < len && s[i] >= '0' && s[i] <= '9') {
  824. whole = whole * 10 + (s[i] - '0');
  825. digits = 1;
  826. i++;
  827. }
  828. if (i < len && s[i] == '.') {
  829. i++;
  830. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  831. frac = frac * 10 + (s[i] - '0');
  832. fscale *= 10;
  833. i++;
  834. }
  835. }
  836. if (!digits)
  837. return 0;
  838. while (i < len && is_space(s[i]))
  839. i++;
  840. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  841. is_rem = 1;
  842. is_px = 0;
  843. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  844. is_rem = 1;
  845. is_px = 0;
  846. } else if (i < len && s[i] == '%') {
  847. is_pct = 1;
  848. is_px = 0;
  849. }
  850. v1000 = whole * 1000 + frac * 1000 / fscale;
  851. if (is_rem)
  852. px = v1000 * 16 / 1000;
  853. else if (is_pct)
  854. px = v1000 * 16 / 100000;
  855. else if (is_px)
  856. px = v1000 / 1000;
  857. else
  858. px = v1000 / 1000;
  859. if (px < 8)
  860. px = 8;
  861. if (px > 72)
  862. px = 72;
  863. return px;
  864. }
  865. static int parse_line_height(const char *s, unsigned int len,
  866. unsigned short *px, unsigned char *pct)
  867. {
  868. unsigned int i = 0;
  869. int whole = 0, frac = 0, fscale = 1, digits = 0;
  870. int v1000;
  871. int is_px = 0, is_pct = 0;
  872. *px = 0;
  873. *pct = 0;
  874. while (i < len && is_space(s[i]))
  875. i++;
  876. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  877. return 0;
  878. while (i < len && s[i] >= '0' && s[i] <= '9') {
  879. whole = whole * 10 + (s[i] - '0');
  880. digits = 1;
  881. i++;
  882. }
  883. if (i < len && s[i] == '.') {
  884. i++;
  885. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  886. frac = frac * 10 + (s[i] - '0');
  887. fscale *= 10;
  888. i++;
  889. }
  890. }
  891. if (!digits)
  892. return 0;
  893. while (i < len && is_space(s[i]))
  894. i++;
  895. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  896. is_px = 1;
  897. else if (i < len && s[i] == '%')
  898. is_pct = 1;
  899. v1000 = whole * 1000 + frac * 1000 / fscale;
  900. if (is_px) {
  901. int v = v1000 / 1000;
  902. if (v < 8)
  903. v = 8;
  904. if (v > 160)
  905. v = 160;
  906. *px = (unsigned short)v;
  907. } else {
  908. int v = v1000 / 10; /* unitless multiplier -> percent */
  909. if (is_pct)
  910. v = v1000 / 1000;
  911. if (v < 50)
  912. v = 50;
  913. if (v > 400)
  914. v = 400;
  915. *pct = (unsigned char)v;
  916. }
  917. return 1;
  918. }
  919. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  920. {
  921. unsigned int i, j, n = s_len(needle);
  922. for (i = 0; i + n <= len; i++) {
  923. for (j = 0; j < n; j++) {
  924. char a = s[i + j];
  925. if (a >= 'A' && a <= 'Z')
  926. a = (char)(a - 'A' + 'a');
  927. if (a != needle[j])
  928. break;
  929. }
  930. if (j == n)
  931. return 1;
  932. }
  933. return 0;
  934. }
  935. /* Pull a colour out of a border/background shorthand. */
  936. static int find_color(const char *s, unsigned int len, unsigned short *c)
  937. {
  938. unsigned int i;
  939. for (i = 0; i < len; i++) {
  940. if (s[i] == '#')
  941. return parse_color(s + i, len - i, c);
  942. if ((s[i] == 'r' || s[i] == 'R') &&
  943. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  944. return parse_color(s + i, len - i, c);
  945. }
  946. for (i = 0; i < len; ) {
  947. unsigned int st, e;
  948. while (i < len && (is_space(s[i]) || s[i] == ','))
  949. i++;
  950. st = i;
  951. while (i < len && !is_space(s[i]) && s[i] != ',')
  952. i++;
  953. e = i;
  954. if (e > st && parse_color(s + st, e - st, c))
  955. return 1;
  956. }
  957. return 0;
  958. }
  959. static int parse_display(const char *s, unsigned int len)
  960. {
  961. unsigned int i;
  962. while (len && is_space(s[0])) { s++; len--; }
  963. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  964. ;
  965. if (i == 4 && ci_eq_n(s, "none", 4))
  966. return BR_DISPLAY_NONE;
  967. if (i == 5 && ci_eq_n(s, "block", 5))
  968. return BR_DISPLAY_BLOCK;
  969. if (i == 6 && ci_eq_n(s, "inline", 6))
  970. return BR_DISPLAY_INLINE;
  971. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  972. return BR_DISPLAY_INLINE;
  973. if (i == 10 && ci_eq_n(s, "list-item", 10))
  974. return BR_DISPLAY_BLOCK;
  975. if (i == 4 && ci_eq_n(s, "flex", 4))
  976. return BR_DISPLAY_BLOCK;
  977. if (i == 5 && ci_eq_n(s, "table", 5))
  978. return BR_DISPLAY_BLOCK;
  979. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  980. return BR_DISPLAY_INLINE;
  981. if (i == 5 && ci_eq_n(s, "grid", 5))
  982. return BR_DISPLAY_BLOCK;
  983. return BR_DISPLAY_INLINE;
  984. }
  985. static void decl_add(struct br_decl *out, unsigned int *nout,
  986. unsigned char prop, int val, int important)
  987. {
  988. if (*nout >= BR_DECLS)
  989. return;
  990. out[*nout].prop = prop;
  991. out[*nout].val = (short)val;
  992. out[*nout].important = (unsigned char)(important != 0);
  993. (*nout)++;
  994. }
  995. static int parse_border_width(const char *s, unsigned int len)
  996. {
  997. unsigned int i = 0;
  998. while (i < len) {
  999. while (i < len && is_space(s[i]))
  1000. i++;
  1001. if (i >= len)
  1002. break;
  1003. if (s[i] == '#') {
  1004. while (i < len && !is_space(s[i]))
  1005. i++;
  1006. continue;
  1007. }
  1008. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1009. while (i < len && s[i] != ')')
  1010. i++;
  1011. if (i < len)
  1012. i++;
  1013. continue;
  1014. }
  1015. if (s[i] >= '0' && s[i] <= '9') {
  1016. int v = 0;
  1017. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1018. v = v * 10 + (s[i] - '0');
  1019. i++;
  1020. }
  1021. if (v < 0)
  1022. v = 0;
  1023. if (v > 3)
  1024. v = 3;
  1025. return v;
  1026. }
  1027. while (i < len && !is_space(s[i]))
  1028. i++;
  1029. }
  1030. return -1;
  1031. }
  1032. static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
  1033. unsigned int *nout)
  1034. {
  1035. unsigned int i = 0;
  1036. *nout = 0;
  1037. while (i < len && *nout < BR_DECLS) {
  1038. unsigned int pstart, pend, vstart, vend, k;
  1039. char prop[24];
  1040. int important = 0;
  1041. while (i < len && (is_space(s[i]) || s[i] == ';'))
  1042. i++;
  1043. if (i >= len)
  1044. break;
  1045. pstart = i;
  1046. while (i < len && s[i] != ':' && s[i] != ';')
  1047. i++;
  1048. pend = i;
  1049. if (i >= len || s[i] != ':')
  1050. continue;
  1051. i++;
  1052. vstart = i;
  1053. while (i < len && s[i] != ';')
  1054. i++;
  1055. vend = i;
  1056. while (pstart < pend && is_space(s[pstart]))
  1057. pstart++;
  1058. while (pend > pstart && is_space(s[pend - 1]))
  1059. pend--;
  1060. while (vstart < vend && is_space(s[vstart]))
  1061. vstart++;
  1062. while (vend > vstart && is_space(s[vend - 1]))
  1063. vend--;
  1064. if (vend - vstart >= 10 &&
  1065. ci_eq_n(s + vend - 10, "!important", 10)) {
  1066. important = 1;
  1067. vend -= 10;
  1068. while (vend > vstart && is_space(s[vend - 1]))
  1069. vend--;
  1070. }
  1071. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  1072. char c = s[pstart + k];
  1073. if (c >= 'A' && c <= 'Z')
  1074. c = (char)(c - 'A' + 'a');
  1075. prop[k] = c;
  1076. }
  1077. prop[k] = '\0';
  1078. if (str_eq(prop, "color")) {
  1079. unsigned short c;
  1080. if (parse_color(s + vstart, vend - vstart, &c))
  1081. decl_add(out, nout, PROP_COLOR, c, important);
  1082. } else if (str_eq(prop, "background-color") ||
  1083. str_eq(prop, "background")) {
  1084. unsigned short c;
  1085. if (parse_color(s + vstart, vend - vstart, &c))
  1086. decl_add(out, nout, PROP_BG, c, important);
  1087. else if (str_eq(prop, "background") &&
  1088. find_color(s + vstart, vend - vstart, &c))
  1089. decl_add(out, nout, PROP_BG, c, important);
  1090. } else if (str_eq(prop, "font-size")) {
  1091. int px = parse_font_px(s + vstart, vend - vstart);
  1092. if (px > 0)
  1093. decl_add(out, nout, PROP_SCALE,
  1094. px >= 24 ? 2 : 1, important);
  1095. } else if (str_eq(prop, "font-weight")) {
  1096. int bold = 0;
  1097. unsigned int j;
  1098. if (vend - vstart >= 4 &&
  1099. ci_eq_n(s + vstart, "bold", 4))
  1100. bold = 1;
  1101. else {
  1102. int w = 0;
  1103. for (j = vstart; j < vend; j++)
  1104. if (s[j] >= '0' && s[j] <= '9')
  1105. w = w * 10 + (s[j] - '0');
  1106. if (w >= 500)
  1107. bold = 1;
  1108. }
  1109. decl_add(out, nout, PROP_BOLD, bold, important);
  1110. } else if (str_eq(prop, "text-align")) {
  1111. int a = BR_ALIGN_LEFT;
  1112. if (vend - vstart >= 6 &&
  1113. ci_eq_n(s + vstart, "center", 6))
  1114. a = BR_ALIGN_CENTER;
  1115. else if (vend - vstart >= 5 &&
  1116. ci_eq_n(s + vstart, "right", 5))
  1117. a = BR_ALIGN_RIGHT;
  1118. decl_add(out, nout, PROP_ALIGN, a, important);
  1119. } else if (str_eq(prop, "text-decoration")) {
  1120. int u = 0;
  1121. if (vend - vstart >= 9 &&
  1122. ci_eq_n(s + vstart, "underline", 9))
  1123. u = 1;
  1124. decl_add(out, nout, PROP_UNDERLINE, u, important);
  1125. } else if (str_eq(prop, "text-transform")) {
  1126. int u = (vend - vstart >= 9 &&
  1127. ci_eq_n(s + vstart, "uppercase", 9));
  1128. decl_add(out, nout, PROP_UPPER, u, important);
  1129. } else if (str_eq(prop, "display")) {
  1130. decl_add(out, nout, PROP_DISPLAY,
  1131. parse_display(s + vstart, vend - vstart),
  1132. important);
  1133. } else if (str_eq(prop, "float")) {
  1134. int f = 0;
  1135. if (vend - vstart >= 4 &&
  1136. ci_eq_n(s + vstart, "left", 4))
  1137. f = 1;
  1138. else if (vend - vstart >= 5 &&
  1139. ci_eq_n(s + vstart, "right", 5))
  1140. f = 2;
  1141. decl_add(out, nout, PROP_FLOAT, f, important);
  1142. } else if (str_eq(prop, "width") ||
  1143. str_eq(prop, "max-width") ||
  1144. str_eq(prop, "height")) {
  1145. unsigned short v;
  1146. unsigned char kind;
  1147. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  1148. if (str_eq(prop, "width")) {
  1149. decl_add(out, nout, PROP_WIDTH, v, important);
  1150. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  1151. important);
  1152. } else if (str_eq(prop, "max-width")) {
  1153. decl_add(out, nout, PROP_MAXW, v, important);
  1154. decl_add(out, nout, PROP_MAXW_KIND, kind,
  1155. important);
  1156. } else {
  1157. decl_add(out, nout, PROP_HEIGHT, v, important);
  1158. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  1159. important);
  1160. }
  1161. }
  1162. } else if (str_eq(prop, "line-height")) {
  1163. unsigned short px;
  1164. unsigned char pct;
  1165. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  1166. if (px) {
  1167. decl_add(out, nout, PROP_LH_PX, px, important);
  1168. decl_add(out, nout, PROP_LH_PCT, 0, important);
  1169. } else {
  1170. decl_add(out, nout, PROP_LH_PX, 0, important);
  1171. decl_add(out, nout, PROP_LH_PCT, pct, important);
  1172. }
  1173. }
  1174. } else if (str_eq(prop, "list-style") ||
  1175. str_eq(prop, "list-style-type")) {
  1176. int none = (vend - vstart >= 4 &&
  1177. contains_ci_len(s + vstart, vend - vstart,
  1178. "none"));
  1179. decl_add(out, nout, PROP_LIST_NONE, none, important);
  1180. } else if (str_eq(prop, "border") ||
  1181. str_eq(prop, "border-top") ||
  1182. str_eq(prop, "border-right") ||
  1183. str_eq(prop, "border-bottom") ||
  1184. str_eq(prop, "border-left") ||
  1185. str_eq(prop, "border-color") ||
  1186. str_eq(prop, "border-width")) {
  1187. unsigned short c;
  1188. int bw;
  1189. int have_color;
  1190. if (vend - vstart >= 4 &&
  1191. ci_eq_n(s + vstart, "none", 4)) {
  1192. decl_add(out, nout, PROP_BORDER_W, 0, important);
  1193. } else {
  1194. bw = parse_border_width(s + vstart,
  1195. vend - vstart);
  1196. have_color = find_color(s + vstart,
  1197. vend - vstart, &c);
  1198. if (have_color) {
  1199. decl_add(out, nout, PROP_BORDER_COLOR, c,
  1200. important);
  1201. if (bw < 0)
  1202. bw = 1;
  1203. }
  1204. if (bw >= 0) {
  1205. if (bw > 3)
  1206. bw = 3;
  1207. decl_add(out, nout, PROP_BORDER_W, bw,
  1208. important);
  1209. }
  1210. }
  1211. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  1212. int vals[4], nv = 0;
  1213. unsigned int pos = 0;
  1214. int is_pad = prop[0] == 'p';
  1215. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  1216. while (pos < vend - vstart && nv < 4) {
  1217. vals[nv++] = parse_len(s + vstart,
  1218. vend - vstart, &pos);
  1219. while (pos < vend - vstart &&
  1220. is_space(s[vstart + pos]))
  1221. pos++;
  1222. }
  1223. if (nv == 1) {
  1224. vals[1] = vals[2] = vals[3] = vals[0];
  1225. } else if (nv == 2) {
  1226. vals[2] = vals[0];
  1227. vals[3] = vals[1];
  1228. } else if (nv == 3) {
  1229. vals[3] = vals[1];
  1230. }
  1231. if (nv > 0) {
  1232. decl_add(out, nout, base, vals[0], important);
  1233. decl_add(out, nout, (unsigned char)(base + 1),
  1234. vals[1], important);
  1235. decl_add(out, nout, (unsigned char)(base + 2),
  1236. vals[2], important);
  1237. decl_add(out, nout, (unsigned char)(base + 3),
  1238. vals[3], important);
  1239. }
  1240. } else if (str_eq(prop, "margin-top") ||
  1241. str_eq(prop, "margin-right") ||
  1242. str_eq(prop, "margin-bottom") ||
  1243. str_eq(prop, "margin-left") ||
  1244. str_eq(prop, "padding-top") ||
  1245. str_eq(prop, "padding-right") ||
  1246. str_eq(prop, "padding-bottom") ||
  1247. str_eq(prop, "padding-left")) {
  1248. unsigned int pos = 0;
  1249. int v = parse_len(s + vstart, vend - vstart, &pos);
  1250. unsigned char p = PROP_MT;
  1251. const char *side;
  1252. if (prop[0] == 'p')
  1253. p = PROP_PT;
  1254. side = prop + (prop[0] == 'p' ? 8 : 7);
  1255. if (str_eq(side, "right"))
  1256. p = (unsigned char)(p + 1);
  1257. else if (str_eq(side, "bottom"))
  1258. p = (unsigned char)(p + 2);
  1259. else if (str_eq(side, "left"))
  1260. p = (unsigned char)(p + 3);
  1261. decl_add(out, nout, p, v, important);
  1262. }
  1263. }
  1264. }
  1265. /* Skip @media/@font-face/@keyframes/@import without descending into their
  1266. * nested blocks. This is the mobile/base rules path: media enhancements are
  1267. * deliberately ignored for now. */
  1268. static unsigned int skip_at_rule(const char *s, unsigned int len,
  1269. unsigned int i)
  1270. {
  1271. unsigned int j = i;
  1272. int depth;
  1273. while (j < len && s[j] != '{' && s[j] != ';')
  1274. j++;
  1275. if (j >= len)
  1276. return len;
  1277. if (s[j] == ';')
  1278. return j + 1;
  1279. depth = 1;
  1280. j++;
  1281. while (j < len && depth) {
  1282. if (s[j] == '{')
  1283. depth++;
  1284. else if (s[j] == '}')
  1285. depth--;
  1286. j++;
  1287. }
  1288. return j;
  1289. }
  1290. static unsigned int viewport_w = 1024;
  1291. static unsigned short theme_text = 0x0000;
  1292. static unsigned short theme_bg = 0xFFFF;
  1293. static unsigned short theme_panel = 0xEF5D;
  1294. static unsigned short theme_link = 0x001B;
  1295. void br_set_theme(unsigned short text, unsigned short bg,
  1296. unsigned short panel, unsigned short link)
  1297. {
  1298. theme_text = text;
  1299. theme_bg = bg;
  1300. theme_panel = panel;
  1301. theme_link = link;
  1302. }
  1303. void br_set_viewport(unsigned int width)
  1304. {
  1305. viewport_w = width ? width : 1024;
  1306. }
  1307. static unsigned int media_len_px(const char *s, unsigned int len)
  1308. {
  1309. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  1310. int em = 0;
  1311. while (i < len && is_space(s[i]))
  1312. i++;
  1313. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1314. whole = whole * 10 + (unsigned int)(s[i] - '0');
  1315. i++;
  1316. }
  1317. if (i < len && s[i] == '.') {
  1318. i++;
  1319. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  1320. frac = frac * 10 + (unsigned int)(s[i] - '0');
  1321. scale *= 10;
  1322. i++;
  1323. }
  1324. }
  1325. while (i < len && is_space(s[i]))
  1326. i++;
  1327. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1328. em = 1;
  1329. i += 2;
  1330. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1331. em = 1;
  1332. i += 3;
  1333. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  1334. i += 2;
  1335. }
  1336. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  1337. if (em)
  1338. return v * 16 / 100;
  1339. return v / 100;
  1340. }
  1341. /* One parenthesised media feature: min-width/max-width and the modern
  1342. * (width <= 600px) form. Unknown features are treated as true. */
  1343. static int media_feature_matches(const char *s, unsigned int len)
  1344. {
  1345. unsigned int i;
  1346. while (len && is_space(*s)) { s++; len--; }
  1347. while (len && is_space(s[len - 1])) len--;
  1348. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  1349. for (i = 9; i < len && s[i] != ':'; i++)
  1350. ;
  1351. if (i < len)
  1352. return viewport_w >= media_len_px(s + i + 1,
  1353. len - i - 1);
  1354. return 1;
  1355. }
  1356. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  1357. for (i = 9; i < len && s[i] != ':'; i++)
  1358. ;
  1359. if (i < len)
  1360. return viewport_w <= media_len_px(s + i + 1,
  1361. len - i - 1);
  1362. return 1;
  1363. }
  1364. for (i = 0; i < len; i++) {
  1365. if (s[i] == '<' || s[i] == '>') {
  1366. int max = s[i] == '<';
  1367. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  1368. if (i + 1 < len && s[i + 1] == '=')
  1369. v = media_len_px(s + i + 2, len - i - 2);
  1370. return max ? viewport_w <= v : viewport_w >= v;
  1371. }
  1372. }
  1373. return 1;
  1374. }
  1375. static int media_query_matches(const char *s, unsigned int len)
  1376. {
  1377. unsigned int i;
  1378. if (contains_ci_len(s, len, "print") &&
  1379. !contains_ci_len(s, len, "screen"))
  1380. return 0;
  1381. if (contains_ci_len(s, len, "not "))
  1382. return 0;
  1383. for (i = 0; i < len; i++) {
  1384. if (s[i] == '(') {
  1385. unsigned int st = ++i;
  1386. while (i < len && s[i] != ')')
  1387. i++;
  1388. if (!media_feature_matches(s + st, i - st))
  1389. return 0;
  1390. }
  1391. }
  1392. return 1;
  1393. }
  1394. static int media_list_matches(const char *s, unsigned int len)
  1395. {
  1396. unsigned int i = 0;
  1397. while (i < len) {
  1398. unsigned int st = i;
  1399. while (i < len && s[i] != ',')
  1400. i++;
  1401. if (media_query_matches(s + st, i - st))
  1402. return 1;
  1403. i++;
  1404. }
  1405. return 0;
  1406. }
  1407. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  1408. {
  1409. unsigned int i = 0;
  1410. while (i < len && d->nrules < BR_MAX_RULES) {
  1411. unsigned int sstart, send, bstart, bend, j;
  1412. struct br_decl decls[BR_DECLS];
  1413. unsigned int nd;
  1414. while (i < len && is_space(s[i]))
  1415. i++;
  1416. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  1417. i += 2;
  1418. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  1419. i++;
  1420. i += 2;
  1421. continue;
  1422. }
  1423. if (i >= len || s[i] == '}')
  1424. break;
  1425. if (s[i] == '@') {
  1426. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  1427. unsigned int q = i + 6;
  1428. while (q < len && s[q] != '{')
  1429. q++;
  1430. if (q < len &&
  1431. media_list_matches(s + i + 6,
  1432. q - (i + 6))) {
  1433. unsigned int b = q + 1, e = b;
  1434. int depth = 1;
  1435. while (e < len && depth) {
  1436. if (s[e] == '{')
  1437. depth++;
  1438. else if (s[e] == '}')
  1439. depth--;
  1440. e++;
  1441. }
  1442. if (depth == 0)
  1443. parse_css(d, s + b,
  1444. (e - 1) - b);
  1445. i = e;
  1446. } else {
  1447. i = skip_at_rule(s, len, i);
  1448. }
  1449. continue;
  1450. }
  1451. i = skip_at_rule(s, len, i);
  1452. continue;
  1453. }
  1454. sstart = i;
  1455. while (i < len && s[i] != '{')
  1456. i++;
  1457. if (i >= len)
  1458. break;
  1459. send = i;
  1460. i++;
  1461. bstart = i;
  1462. while (i < len && s[i] != '}')
  1463. i++;
  1464. bend = i;
  1465. if (i < len)
  1466. i++;
  1467. while (sstart < send && is_space(s[sstart]))
  1468. sstart++;
  1469. while (send > sstart && is_space(s[send - 1]))
  1470. send--;
  1471. if (sstart == send)
  1472. continue;
  1473. parse_decls(s + bstart, bend - bstart, decls, &nd);
  1474. if (!nd)
  1475. continue;
  1476. /* Split a comma-separated selector list into separate rules. */
  1477. j = sstart;
  1478. while (j <= send && d->nrules < BR_MAX_RULES) {
  1479. unsigned int q = j, e, k;
  1480. char sel[80];
  1481. unsigned int off;
  1482. while (q < send && s[q] != ',')
  1483. q++;
  1484. e = q;
  1485. while (j < e && is_space(s[j]))
  1486. j++;
  1487. while (e > j && is_space(s[e - 1]))
  1488. e--;
  1489. if (e > j) {
  1490. /* Ignore single selectors with sibling combinators,
  1491. * pseudo-classes or attribute tests: they need
  1492. * state/attributes we do not model. The other
  1493. * selectors in the list still apply. */
  1494. for (k = j; k < e; k++)
  1495. if (s[k] == '+' || s[k] == '~' ||
  1496. s[k] == ':' || s[k] == '[')
  1497. break;
  1498. if (k < e) {
  1499. j = q + 1;
  1500. if (q >= send)
  1501. break;
  1502. continue;
  1503. }
  1504. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  1505. sel[k] = s[j + k];
  1506. sel[k] = '\0';
  1507. off = store_attr(d, sel, k, 79);
  1508. if (off) {
  1509. struct br_rule *r = &d->rules[d->nrules++];
  1510. r->sel_off = (unsigned short)off;
  1511. r->nd = (unsigned char)nd;
  1512. for (k = 0; k < nd; k++)
  1513. r->d[k] = decls[k];
  1514. }
  1515. }
  1516. j = q + 1;
  1517. if (q >= send)
  1518. break;
  1519. }
  1520. }
  1521. }
  1522. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  1523. {
  1524. parse_css(d, css, len);
  1525. }
  1526. void br_parse_inline_css(struct br_doc *d)
  1527. {
  1528. if (d->inline_css_len)
  1529. parse_css(d, css_store, d->inline_css_len);
  1530. d->inline_css_len = 0;
  1531. }
  1532. void br_reset_styles(struct br_doc *d)
  1533. {
  1534. unsigned int i;
  1535. for (i = 0; i < d->nnode; i++)
  1536. d->nodes[i].style_valid = 0;
  1537. }
  1538. /* ---- selector matching ---- */
  1539. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  1540. unsigned int clen)
  1541. {
  1542. const char *s = attr_ptr(d, d->nodes[node].class_off);
  1543. unsigned int i = 0;
  1544. while (s[i]) {
  1545. unsigned int st, n;
  1546. while (s[i] && is_space(s[i]))
  1547. i++;
  1548. st = i;
  1549. while (s[i] && !is_space(s[i]))
  1550. i++;
  1551. n = i - st;
  1552. if (n == clen && ci_eq_n(s + st, cls, n))
  1553. return 1;
  1554. }
  1555. return 0;
  1556. }
  1557. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  1558. unsigned int len)
  1559. {
  1560. const char *s = attr_ptr(d, d->nodes[node].id_off);
  1561. unsigned int i;
  1562. for (i = 0; i < len; i++)
  1563. if (s[i] != id[i])
  1564. return 0;
  1565. return s[len] == '\0';
  1566. }
  1567. static int is_ident(char c)
  1568. {
  1569. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1570. (c >= '0' && c <= '9') || c == '-' || c == '_';
  1571. }
  1572. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  1573. unsigned int len)
  1574. {
  1575. unsigned int i = 0;
  1576. int got = 0;
  1577. if (i < len && s[i] == '*') {
  1578. i++;
  1579. got = 1;
  1580. } else if (i < len && is_ident(s[i])) {
  1581. char name[16];
  1582. unsigned int n = 0, tag;
  1583. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  1584. name[n++] = s[i++];
  1585. name[n] = '\0';
  1586. tag = br_tag_from_name(name);
  1587. if (tag != d->nodes[node].tag)
  1588. return 0;
  1589. got = 1;
  1590. }
  1591. while (i < len) {
  1592. if (s[i] == '.') {
  1593. unsigned int st;
  1594. i++;
  1595. st = i;
  1596. while (i < len && is_ident(s[i]))
  1597. i++;
  1598. if (!class_has(d, node, s + st, i - st))
  1599. return 0;
  1600. got = 1;
  1601. } else if (s[i] == '#') {
  1602. unsigned int st;
  1603. i++;
  1604. st = i;
  1605. while (i < len && is_ident(s[i]))
  1606. i++;
  1607. if (!id_eq(d, node, s + st, i - st))
  1608. return 0;
  1609. got = 1;
  1610. } else if (s[i] == ':') {
  1611. return 0; /* pseudo-classes are not modelled */
  1612. } else {
  1613. i++;
  1614. }
  1615. }
  1616. return got;
  1617. }
  1618. static unsigned int selector_specificity(const char *s, unsigned int len)
  1619. {
  1620. unsigned int i, spec = 0;
  1621. for (i = 0; i < len; i++) {
  1622. if (s[i] == '#')
  1623. spec += 100;
  1624. else if (s[i] == '.')
  1625. spec += 10;
  1626. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  1627. if (is_ident(s[i]))
  1628. spec += 1;
  1629. }
  1630. }
  1631. return spec;
  1632. }
  1633. /* Match one complex selector; returns specificity or -1. */
  1634. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  1635. unsigned int len)
  1636. {
  1637. struct {
  1638. unsigned int start, end;
  1639. int child;
  1640. } parts[8];
  1641. int np = 0;
  1642. unsigned int i = 0;
  1643. int right;
  1644. while (i < len) {
  1645. int child = 0;
  1646. unsigned int st, e;
  1647. while (i < len && is_space(s[i]))
  1648. i++;
  1649. if (i < len && s[i] == '>') {
  1650. child = 1;
  1651. i++;
  1652. while (i < len && is_space(s[i]))
  1653. i++;
  1654. }
  1655. if (i >= len)
  1656. break;
  1657. st = i;
  1658. while (i < len && !is_space(s[i]) && s[i] != '>')
  1659. i++;
  1660. e = i;
  1661. if (np < 8) {
  1662. parts[np].start = st;
  1663. parts[np].end = e;
  1664. parts[np].child = child;
  1665. np++;
  1666. }
  1667. }
  1668. if (!np)
  1669. return -1;
  1670. if (!compound_match(d, node, s + parts[np - 1].start,
  1671. parts[np - 1].end - parts[np - 1].start))
  1672. return -1;
  1673. right = np - 1;
  1674. while (right > 0) {
  1675. unsigned int p = d->nodes[node].parent;
  1676. if (!p)
  1677. return -1;
  1678. if (parts[right].child) {
  1679. if (!compound_match(d, p, s + parts[right - 1].start,
  1680. parts[right - 1].end -
  1681. parts[right - 1].start))
  1682. return -1;
  1683. node = p;
  1684. } else {
  1685. while (p && !compound_match(d, p,
  1686. s + parts[right - 1].start,
  1687. parts[right - 1].end -
  1688. parts[right - 1].start))
  1689. p = d->nodes[p].parent;
  1690. if (!p)
  1691. return -1;
  1692. node = p;
  1693. }
  1694. right--;
  1695. }
  1696. return (int)selector_specificity(s, len);
  1697. }
  1698. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  1699. {
  1700. switch (dcl->prop) {
  1701. case PROP_COLOR:
  1702. st->color = (unsigned short)dcl->val;
  1703. break;
  1704. case PROP_BG:
  1705. st->bg = (unsigned short)dcl->val;
  1706. st->has_bg = 1;
  1707. break;
  1708. case PROP_SCALE:
  1709. st->scale = (unsigned char)(dcl->val >= 2 ? 2 : 1);
  1710. break;
  1711. case PROP_BOLD:
  1712. st->bold = (unsigned char)(dcl->val != 0);
  1713. break;
  1714. case PROP_ALIGN:
  1715. st->align = (unsigned char)dcl->val;
  1716. break;
  1717. case PROP_UNDERLINE:
  1718. st->underline = (unsigned char)(dcl->val != 0);
  1719. break;
  1720. case PROP_DISPLAY:
  1721. st->display = (unsigned char)dcl->val;
  1722. break;
  1723. case PROP_MT: st->mt = dcl->val; break;
  1724. case PROP_MR: st->mr = dcl->val; break;
  1725. case PROP_MB: st->mb = dcl->val; break;
  1726. case PROP_ML: st->ml = dcl->val; break;
  1727. case PROP_PT: st->pt = dcl->val; break;
  1728. case PROP_PR: st->pr = dcl->val; break;
  1729. case PROP_PB: st->pb = dcl->val; break;
  1730. case PROP_PL: st->pl = dcl->val; break;
  1731. case PROP_BORDER_W:
  1732. st->border_w = (unsigned char)dcl->val;
  1733. if (st->border_w && !st->border_color)
  1734. st->border_color = st->color;
  1735. break;
  1736. case PROP_BORDER_COLOR:
  1737. st->border_color = (unsigned short)dcl->val;
  1738. break;
  1739. case PROP_LIST_NONE:
  1740. st->no_bullet = (unsigned char)(dcl->val != 0);
  1741. break;
  1742. case PROP_WIDTH:
  1743. st->width = (unsigned short)dcl->val;
  1744. break;
  1745. case PROP_WIDTH_KIND:
  1746. st->width_kind = (unsigned char)dcl->val;
  1747. break;
  1748. case PROP_MAXW:
  1749. st->max_width = (unsigned short)dcl->val;
  1750. break;
  1751. case PROP_MAXW_KIND:
  1752. st->max_width_kind = (unsigned char)dcl->val;
  1753. break;
  1754. case PROP_HEIGHT:
  1755. st->height = (unsigned short)dcl->val;
  1756. break;
  1757. case PROP_HEIGHT_KIND:
  1758. st->height_kind = (unsigned char)dcl->val;
  1759. break;
  1760. case PROP_LH_PCT:
  1761. st->line_h_pct = (unsigned char)dcl->val;
  1762. break;
  1763. case PROP_LH_PX:
  1764. st->line_h_px = (unsigned short)dcl->val;
  1765. break;
  1766. case PROP_UPPER:
  1767. st->uppercase = (unsigned char)(dcl->val != 0);
  1768. break;
  1769. case PROP_FLOAT:
  1770. st->float_left = (unsigned char)dcl->val;
  1771. break;
  1772. default:
  1773. break;
  1774. }
  1775. }
  1776. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  1777. int spec, short *best, short *best_imp)
  1778. {
  1779. unsigned char p = dcl->prop;
  1780. if (p >= PROP_COUNT)
  1781. return;
  1782. if (dcl->important) {
  1783. if (spec >= best_imp[p]) {
  1784. apply_decl(st, dcl);
  1785. best_imp[p] = (short)spec;
  1786. }
  1787. } else if (best_imp[p] < 0 && spec >= best[p]) {
  1788. apply_decl(st, dcl);
  1789. best[p] = (short)spec;
  1790. }
  1791. }
  1792. void br_style_for(struct br_doc *d, unsigned int node,
  1793. const struct br_style *parent, struct br_style *out)
  1794. {
  1795. unsigned int tag;
  1796. short best[PROP_COUNT], best_imp[PROP_COUNT];
  1797. unsigned int i;
  1798. if (d->nodes[node].style_valid) {
  1799. *out = d->nodes[node].style;
  1800. return;
  1801. }
  1802. *out = *parent;
  1803. out->has_bg = 0;
  1804. out->float_left = 0;
  1805. out->border_w = 0;
  1806. out->border_color = 0;
  1807. out->width = out->max_width = out->height = 0;
  1808. out->width_kind = out->max_width_kind = out->height_kind = 0;
  1809. out->mt = out->mr = out->mb = out->ml = 0;
  1810. out->pt = out->pr = out->pb = out->pl = 0;
  1811. tag = d->nodes[node].tag;
  1812. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  1813. BR_DISPLAY_INLINE;
  1814. for (i = 0; i < PROP_COUNT; i++) {
  1815. best[i] = -1;
  1816. best_imp[i] = -1;
  1817. }
  1818. switch (tag) {
  1819. case BR_TAG_HEAD:
  1820. case BR_TAG_TITLE:
  1821. case BR_TAG_META:
  1822. case BR_TAG_LINK:
  1823. case BR_TAG_BASE:
  1824. case BR_TAG_SCRIPT:
  1825. case BR_TAG_STYLE:
  1826. out->display = BR_DISPLAY_NONE;
  1827. break;
  1828. case BR_TAG_BODY:
  1829. out->mt = out->mb = out->ml = out->mr = 4;
  1830. break;
  1831. case BR_TAG_P:
  1832. out->mt = out->mb = 8;
  1833. break;
  1834. case BR_TAG_H1:
  1835. out->scale = 2; out->bold = 1;
  1836. out->mt = 10; out->mb = 6;
  1837. break;
  1838. case BR_TAG_H2:
  1839. out->scale = 2; out->bold = 1;
  1840. out->mt = 8; out->mb = 4;
  1841. break;
  1842. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  1843. out->bold = 1; out->mt = 6; out->mb = 3;
  1844. break;
  1845. case BR_TAG_UL: case BR_TAG_OL:
  1846. out->mt = out->mb = 6; out->pl = 20;
  1847. break;
  1848. case BR_TAG_LI:
  1849. out->mt = out->mb = 2;
  1850. break;
  1851. case BR_TAG_BLOCKQUOTE:
  1852. out->mt = out->mb = 6; out->ml = out->mr = 16;
  1853. break;
  1854. case BR_TAG_PRE:
  1855. out->mt = out->mb = 8; out->pl = 8;
  1856. out->has_bg = 1; out->bg = theme_panel;
  1857. break;
  1858. case BR_TAG_CENTER:
  1859. out->align = BR_ALIGN_CENTER;
  1860. break;
  1861. case BR_TAG_A:
  1862. out->color = theme_link; out->underline = 1;
  1863. break;
  1864. case BR_TAG_B: case BR_TAG_STRONG:
  1865. out->bold = 1;
  1866. break;
  1867. case BR_TAG_CODE:
  1868. out->color = 0x8010;
  1869. break;
  1870. case BR_TAG_TD: case BR_TAG_TH:
  1871. out->pr = 12;
  1872. break;
  1873. case BR_TAG_VIDEO:
  1874. out->display = BR_DISPLAY_BLOCK;
  1875. break;
  1876. default:
  1877. break;
  1878. }
  1879. /* presentational <font color>: author CSS below still wins */
  1880. if (d->nodes[node].color_off) {
  1881. unsigned short c;
  1882. const char *v = attr_ptr(d, d->nodes[node].color_off);
  1883. if (parse_color(v, s_len(v), &c))
  1884. out->color = c;
  1885. }
  1886. for (i = 0; i < d->nrules; i++) {
  1887. struct br_rule *r = &d->rules[i];
  1888. const char *sel = attr_ptr(d, r->sel_off);
  1889. int spec = selector_match(d, node, sel, s_len(sel));
  1890. unsigned int k;
  1891. if (spec < 0)
  1892. continue;
  1893. for (k = 0; k < r->nd; k++)
  1894. cascade_decl(out, &r->d[k], spec, best, best_imp);
  1895. }
  1896. if (d->nodes[node].style_off) {
  1897. struct br_decl decls[BR_DECLS];
  1898. unsigned int nd, k;
  1899. const char *v = attr_ptr(d, d->nodes[node].style_off);
  1900. parse_decls(v, s_len(v), decls, &nd);
  1901. for (k = 0; k < nd; k++)
  1902. cascade_decl(out, &decls[k], 1000, best, best_imp);
  1903. }
  1904. d->nodes[node].style = *out;
  1905. d->nodes[node].style_valid = 1;
  1906. }
  1907. /* ---- HTML parser ---- */
  1908. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  1909. unsigned int sp, int *pre)
  1910. {
  1911. unsigned int i;
  1912. *pre = 0;
  1913. for (i = 1; i <= sp; i++)
  1914. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  1915. (*pre)++;
  1916. }
  1917. static void implicit_close(struct br_doc *d, unsigned short *stack,
  1918. unsigned int *sp, unsigned int tag)
  1919. {
  1920. while (*sp > 0) {
  1921. unsigned int top = d->nodes[stack[*sp]].tag;
  1922. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  1923. (*sp)--;
  1924. continue;
  1925. }
  1926. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  1927. (*sp)--;
  1928. continue;
  1929. }
  1930. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  1931. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  1932. (*sp)--;
  1933. continue;
  1934. }
  1935. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  1936. (*sp)--;
  1937. continue;
  1938. }
  1939. break;
  1940. }
  1941. }
  1942. static int has_attr_token(const char *s, unsigned int len, const char *name)
  1943. {
  1944. unsigned int i, n = s_len(name);
  1945. for (i = 0; i + n <= len; i++) {
  1946. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  1947. continue;
  1948. if (!ci_eq_n(s + i, name, n))
  1949. continue;
  1950. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  1951. return 1;
  1952. }
  1953. return 0;
  1954. }
  1955. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  1956. {
  1957. unsigned short stack[64];
  1958. unsigned int sp = 0;
  1959. unsigned int i = 0;
  1960. int pre = 0;
  1961. unsigned int css_len = 0;
  1962. if (!d)
  1963. return -1;
  1964. if (!d->nodes)
  1965. br_doc_init(d);
  1966. doc_reset(d);
  1967. stack[0] = 0;
  1968. d->nodes[0].type = BR_NODE_ELEM;
  1969. d->nodes[0].tag = BR_TAG_UNKNOWN;
  1970. while (i < len) {
  1971. if (html[i] != '<') {
  1972. unsigned int st = i;
  1973. while (i < len && html[i] != '<')
  1974. i++;
  1975. if (sp <= 63)
  1976. append_text(d, stack[sp], html + st, i - st, pre);
  1977. continue;
  1978. }
  1979. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1980. html[i + 3] == '-') {
  1981. int p = find_ci(html, len, "-->", 3, i + 4);
  1982. i = p >= 0 ? (unsigned int)p + 3 : len;
  1983. continue;
  1984. }
  1985. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  1986. while (i < len && html[i] != '>')
  1987. i++;
  1988. if (i < len)
  1989. i++;
  1990. continue;
  1991. }
  1992. if (i + 1 < len && html[i + 1] == '/') {
  1993. unsigned int ns = i + 2, ne, tag;
  1994. while (ns < len && is_space(html[ns]))
  1995. ns++;
  1996. ne = ns;
  1997. while (ne < len && is_ident(html[ne]))
  1998. ne++;
  1999. if (ne == ns) {
  2000. i++;
  2001. continue;
  2002. }
  2003. {
  2004. char name[16];
  2005. unsigned int k, n = ne - ns;
  2006. if (n >= sizeof(name))
  2007. n = sizeof(name) - 1;
  2008. for (k = 0; k < n; k++)
  2009. name[k] = html[ns + k];
  2010. name[n] = '\0';
  2011. tag = br_tag_from_name(name);
  2012. }
  2013. i = ne;
  2014. while (i < len && html[i] != '>')
  2015. i++;
  2016. if (i < len)
  2017. i++;
  2018. {
  2019. unsigned int k;
  2020. for (k = sp; k > 0; k--) {
  2021. if (d->nodes[stack[k]].tag == tag) {
  2022. sp = k - 1;
  2023. break;
  2024. }
  2025. }
  2026. stack_recount_pre(d, stack, sp, &pre);
  2027. }
  2028. continue;
  2029. }
  2030. /* start tag */
  2031. {
  2032. unsigned int ns = i + 1, ne, tag, k, n;
  2033. char name[16];
  2034. unsigned int attr_start, attr_end;
  2035. int selfclose = 0;
  2036. while (ns < len && is_space(html[ns]))
  2037. ns++;
  2038. ne = ns;
  2039. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  2040. ne++;
  2041. if (ne == ns) {
  2042. /* stray '<' */
  2043. append_text(d, stack[sp], html + i, 1, pre);
  2044. i++;
  2045. continue;
  2046. }
  2047. n = ne - ns;
  2048. if (n >= sizeof(name))
  2049. n = sizeof(name) - 1;
  2050. for (k = 0; k < n; k++)
  2051. name[k] = html[ns + k];
  2052. name[n] = '\0';
  2053. tag = br_tag_from_name(name);
  2054. attr_start = ne;
  2055. {
  2056. unsigned int q = ne;
  2057. char quote = 0;
  2058. while (q < len) {
  2059. char c = html[q];
  2060. if (quote) {
  2061. if (c == quote)
  2062. quote = 0;
  2063. } else if (c == '"' || c == '\'') {
  2064. quote = c;
  2065. } else if (c == '>') {
  2066. break;
  2067. }
  2068. q++;
  2069. }
  2070. attr_end = q;
  2071. if (q > attr_start && html[q - 1] == '/')
  2072. selfclose = 1;
  2073. i = q < len ? q + 1 : len;
  2074. }
  2075. if (tag == BR_TAG_SCRIPT) {
  2076. int p = find_ci(html, len, "</script", 8, i);
  2077. i = p >= 0 ? (unsigned int)p + 8 : len;
  2078. while (i < len && html[i] != '>')
  2079. i++;
  2080. if (i < len)
  2081. i++;
  2082. continue;
  2083. }
  2084. if (tag == BR_TAG_SVG && !selfclose) {
  2085. /* Inline SVG is not rendered; skip its subtree so
  2086. * logos and icon sprites do not consume the node
  2087. * arena. */
  2088. int p = find_ci(html, len, "</svg>", 6, i);
  2089. i = p >= 0 ? (unsigned int)p + 6 : len;
  2090. continue;
  2091. }
  2092. if (tag == BR_TAG_DETAILS && !selfclose &&
  2093. !has_attr_token(html + attr_start,
  2094. attr_end - attr_start, "open")) {
  2095. /* Closed <details> is collapsed (and this browser has
  2096. * no script to open it); skipping the subtree keeps
  2097. * mobile mega-menus out of the node arena. */
  2098. int p = find_ci(html, len, "</details>", 10, i);
  2099. i = p >= 0 ? (unsigned int)p + 10 : len;
  2100. continue;
  2101. }
  2102. if (tag == BR_TAG_STYLE) {
  2103. int p = find_ci(html, len, "</style", 7, i);
  2104. unsigned int end = p >= 0 ? (unsigned int)p : len;
  2105. unsigned int cend = end;
  2106. while (cend > i && is_space(html[cend - 1]))
  2107. cend--;
  2108. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  2109. for (k = i; k < cend; k++)
  2110. css_store[css_len++] = html[k];
  2111. css_store[css_len++] = '\n';
  2112. }
  2113. i = p >= 0 ? (unsigned int)p + 7 : len;
  2114. while (i < len && html[i] != '>')
  2115. i++;
  2116. if (i < len)
  2117. i++;
  2118. continue;
  2119. }
  2120. if (tag == BR_TAG_TITLE) {
  2121. int p = find_ci(html, len, "</title", 7, i);
  2122. unsigned int end = p >= 0 ? (unsigned int)p : len;
  2123. unsigned int tn = 0;
  2124. char tmp[BR_TITLE_MAX];
  2125. tn = decode_text(html + i, end - i, tmp,
  2126. sizeof(tmp), 1);
  2127. (void)tn;
  2128. s_copy(d->title, tmp, sizeof(d->title));
  2129. i = p >= 0 ? (unsigned int)p + 7 : len;
  2130. while (i < len && html[i] != '>')
  2131. i++;
  2132. if (i < len)
  2133. i++;
  2134. continue;
  2135. }
  2136. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  2137. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  2138. unsigned int idx;
  2139. implicit_close(d, stack, &sp, tag);
  2140. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  2141. (unsigned char)tag);
  2142. if (idx)
  2143. parse_attrs(d, &d->nodes[idx],
  2144. html + attr_start,
  2145. attr_end - attr_start);
  2146. continue;
  2147. }
  2148. if (sp >= 63) {
  2149. d->truncated = 1;
  2150. continue;
  2151. }
  2152. implicit_close(d, stack, &sp, tag);
  2153. {
  2154. unsigned int idx = node_append(d, stack[sp],
  2155. BR_NODE_ELEM,
  2156. (unsigned char)tag);
  2157. if (!idx)
  2158. continue;
  2159. parse_attrs(d, &d->nodes[idx],
  2160. html + attr_start,
  2161. attr_end - attr_start);
  2162. if (!selfclose) {
  2163. sp++;
  2164. stack[sp] = (unsigned short)idx;
  2165. if (tag == BR_TAG_PRE)
  2166. pre++;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. d->inline_css_len = css_len;
  2172. return 0;
  2173. }
  2174. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  2175. {
  2176. if (!d->nodes)
  2177. br_doc_init(d);
  2178. doc_reset(d);
  2179. d->plain = 1;
  2180. d->nodes[0].type = BR_NODE_ELEM;
  2181. d->nodes[0].tag = BR_TAG_UNKNOWN;
  2182. append_text(d, 0, text, len, 1);
  2183. return 0;
  2184. }
  2185. /* A standalone image response: a synthetic document with one <img>. The
  2186. * caller decodes the already-fetched body into image slot 0 and sets the
  2187. * returned node's img_index. */
  2188. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  2189. {
  2190. unsigned int idx;
  2191. if (!d->nodes)
  2192. br_doc_init(d);
  2193. doc_reset(d);
  2194. d->nodes[0].type = BR_NODE_ELEM;
  2195. d->nodes[0].tag = BR_TAG_UNKNOWN;
  2196. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  2197. if (!idx)
  2198. return 0;
  2199. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  2200. return idx;
  2201. }