browser_html.c 52 KB

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