browser_html.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. /* ---- document setup / node append ---- */
  368. void br_doc_init(struct br_doc *d)
  369. {
  370. unsigned int i;
  371. for (i = 0; i < sizeof(*d); i++)
  372. ((unsigned char *)d)[i] = 0;
  373. d->nodes = nodes_store;
  374. d->text = text_store;
  375. d->attrs = attr_store;
  376. d->rules = rule_store;
  377. d->attrs_used = 1; /* offset 0 = "no attribute" */
  378. d->attrs[0] = '\0';
  379. d->root = 0;
  380. }
  381. static void doc_reset(struct br_doc *d)
  382. {
  383. unsigned int i;
  384. d->nnode = 0;
  385. d->text_used = 0;
  386. d->attrs_used = 1;
  387. d->attrs[0] = '\0';
  388. d->nrules = 0;
  389. d->title[0] = '\0';
  390. d->truncated = 0;
  391. d->plain = 0;
  392. for (i = 0; i < BR_MAX_NODES; i++) {
  393. d->nodes[i].type = BR_NODE_ELEM;
  394. d->nodes[i].tag = BR_TAG_UNKNOWN;
  395. d->nodes[i].flags = 0;
  396. d->nodes[i].parent = 0;
  397. d->nodes[i].first = d->nodes[i].last = 0;
  398. d->nodes[i].next = 0;
  399. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  400. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  401. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  402. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  403. d->nodes[i].color_off = 0;
  404. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  405. d->nodes[i].img_index = -1;
  406. }
  407. }
  408. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  409. unsigned char type, unsigned char tag)
  410. {
  411. unsigned int idx;
  412. struct br_node *n;
  413. if (d->nnode >= BR_MAX_NODES) {
  414. d->truncated = 1;
  415. return 0;
  416. }
  417. idx = d->nnode++;
  418. n = &d->nodes[idx];
  419. n->type = type;
  420. n->tag = tag;
  421. n->parent = (unsigned short)parent;
  422. n->img_index = -1;
  423. if (d->nodes[parent].last)
  424. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  425. else
  426. d->nodes[parent].first = (unsigned short)idx;
  427. d->nodes[parent].last = (unsigned short)idx;
  428. return idx;
  429. }
  430. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  431. const char *s, unsigned int len, int pre)
  432. {
  433. char *dst;
  434. unsigned int n;
  435. unsigned int idx;
  436. if (len == 0)
  437. return 0;
  438. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  439. d->truncated = 1;
  440. return 0;
  441. }
  442. dst = d->text + d->text_used;
  443. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  444. if (n == 0) {
  445. d->text[d->text_used] = '\0';
  446. return 0;
  447. }
  448. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  449. if (!idx)
  450. return 0;
  451. d->nodes[idx].text_off = (unsigned short)d->text_used;
  452. d->nodes[idx].text_len = (unsigned short)n;
  453. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  454. d->text_used += n + 1;
  455. return idx;
  456. }
  457. /* ---- attribute parsing ---- */
  458. static int is_space(char c)
  459. {
  460. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  461. }
  462. static int str_eq(const char *a, const char *b);
  463. static unsigned int parse_uint(const char *s, unsigned int len)
  464. {
  465. unsigned int i = 0, v = 0;
  466. while (i < len && is_space(s[i]))
  467. i++;
  468. while (i < len && s[i] >= '0' && s[i] <= '9') {
  469. v = v * 10 + (unsigned int)(s[i] - '0');
  470. if (v > 4096)
  471. v = 4096;
  472. i++;
  473. }
  474. return v;
  475. }
  476. /* Parse attributes from `s` (between the tag name and '>'). */
  477. static void parse_attrs(struct br_doc *d, struct br_node *n,
  478. const char *s, unsigned int len)
  479. {
  480. unsigned int i = 0;
  481. while (i < len) {
  482. char name[16];
  483. unsigned int nl = 0, vstart, vlen;
  484. char quote = 0;
  485. while (i < len && is_space(s[i]))
  486. i++;
  487. if (i >= len || s[i] == '/')
  488. break;
  489. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  490. char c = s[i++];
  491. if (c >= 'A' && c <= 'Z')
  492. c = (char)(c - 'A' + 'a');
  493. if (nl + 1 < sizeof(name))
  494. name[nl++] = c;
  495. }
  496. name[nl] = '\0';
  497. while (i < len && is_space(s[i]))
  498. i++;
  499. if (i >= len || s[i] != '=') {
  500. /* boolean attribute; nothing we use needs it */
  501. continue;
  502. }
  503. i++;
  504. while (i < len && is_space(s[i]))
  505. i++;
  506. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  507. quote = s[i++];
  508. vstart = i;
  509. while (i < len && s[i] != quote)
  510. i++;
  511. vlen = i - vstart;
  512. if (i < len)
  513. i++;
  514. } else {
  515. vstart = i;
  516. while (i < len && !is_space(s[i]) && s[i] != '>')
  517. i++;
  518. vlen = i - vstart;
  519. }
  520. if (!nl)
  521. continue;
  522. if (str_eq(name, "href"))
  523. n->href_off = store_attr(d, s + vstart, vlen, 220);
  524. else if (str_eq(name, "class"))
  525. n->class_off = store_attr(d, s + vstart, vlen, 120);
  526. else if (str_eq(name, "id"))
  527. n->id_off = store_attr(d, s + vstart, vlen, 64);
  528. else if (str_eq(name, "style"))
  529. n->style_off = store_attr(d, s + vstart, vlen, 240);
  530. else if (str_eq(name, "src"))
  531. n->src_off = store_attr(d, s + vstart, vlen, 220);
  532. else if (str_eq(name, "alt"))
  533. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  534. else if (str_eq(name, "color"))
  535. n->color_off = store_attr(d, s + vstart, vlen, 32);
  536. else if (str_eq(name, "width"))
  537. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  538. else if (str_eq(name, "height"))
  539. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  540. }
  541. }
  542. static int str_eq(const char *a, const char *b)
  543. {
  544. unsigned int i = 0;
  545. while (a[i] && b[i] && a[i] == b[i])
  546. i++;
  547. return a[i] == '\0' && b[i] == '\0';
  548. }
  549. /* ---- CSS ---- */
  550. enum {
  551. PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
  552. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  553. PROP_PT, PROP_PR, PROP_PB, PROP_PL, PROP_COUNT
  554. };
  555. struct color_name {
  556. const char *name;
  557. unsigned short rgb;
  558. };
  559. static const struct color_name color_names[] = {
  560. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  561. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  562. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  563. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  564. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  565. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  566. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  567. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  568. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  569. { "darkgrey", 0x528A }, { 0, 0 }
  570. };
  571. static int hexval(char c)
  572. {
  573. if (c >= '0' && c <= '9')
  574. return c - '0';
  575. if (c >= 'a' && c <= 'f')
  576. return c - 'a' + 10;
  577. if (c >= 'A' && c <= 'F')
  578. return c - 'A' + 10;
  579. return -1;
  580. }
  581. static int named_color(const char *s, unsigned int len, unsigned short *out)
  582. {
  583. unsigned int i;
  584. for (i = 0; color_names[i].name; i++) {
  585. unsigned int j, n = s_len(color_names[i].name);
  586. if (n != len)
  587. continue;
  588. for (j = 0; j < n; j++) {
  589. char c = s[j];
  590. if (c >= 'A' && c <= 'Z')
  591. c = (char)(c - 'A' + 'a');
  592. if (c != color_names[i].name[j])
  593. break;
  594. }
  595. if (j == n) {
  596. *out = color_names[i].rgb;
  597. return 1;
  598. }
  599. }
  600. return 0;
  601. }
  602. /* Parse #rgb/#rrggbb/rgb(r,g,b)/a named colour. */
  603. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  604. {
  605. unsigned int i = 0;
  606. while (i < len && is_space(s[i]))
  607. i++;
  608. if (i < len && s[i] == '#') {
  609. int d[6], nd = 0;
  610. i++;
  611. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  612. d[nd++] = hexval(s[i++]);
  613. if (nd == 3) {
  614. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  615. unsigned int b = (unsigned int)d[2];
  616. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  617. ((g * 17 >> 2) << 5) |
  618. (b * 17 >> 3));
  619. return 1;
  620. }
  621. if (nd >= 6) {
  622. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  623. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  624. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  625. *out = (unsigned short)(((r >> 3) << 11) |
  626. ((g >> 2) << 5) | (b >> 3));
  627. return 1;
  628. }
  629. return 0;
  630. }
  631. if (i + 3 < len && ci_eq_n(s + i, "rgb", 3)) {
  632. unsigned int v[3], k = 0;
  633. while (i < len && s[i] != '(')
  634. i++;
  635. if (i >= len)
  636. return 0;
  637. i++;
  638. while (k < 3 && i < len) {
  639. unsigned int x = 0;
  640. while (i < len && is_space(s[i]))
  641. i++;
  642. while (i < len && s[i] >= '0' && s[i] <= '9') {
  643. x = x * 10 + (unsigned int)(s[i] - '0');
  644. i++;
  645. }
  646. if (x > 255)
  647. x = 255;
  648. v[k++] = x;
  649. while (i < len && s[i] != ',' && s[i] != ')')
  650. i++;
  651. if (i < len && s[i] == ',')
  652. i++;
  653. }
  654. if (k == 3) {
  655. *out = (unsigned short)(((v[0] >> 3) << 11) |
  656. ((v[1] >> 2) << 5) | (v[2] >> 3));
  657. return 1;
  658. }
  659. return 0;
  660. }
  661. {
  662. unsigned int j = i;
  663. while (i < len && !is_space(s[i]))
  664. i++;
  665. return named_color(s + j, i - j, out);
  666. }
  667. }
  668. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  669. {
  670. unsigned int i = *pos;
  671. int sign = 1;
  672. int v = 0;
  673. while (i < len && is_space(s[i]))
  674. i++;
  675. if (i < len && (s[i] == '-' || s[i] == '+')) {
  676. if (s[i] == '-')
  677. sign = -1;
  678. i++;
  679. }
  680. while (i < len && s[i] >= '0' && s[i] <= '9') {
  681. v = v * 10 + (s[i] - '0');
  682. i++;
  683. }
  684. if (i < len && s[i] == '.') {
  685. i++;
  686. while (i < len && s[i] >= '0' && s[i] <= '9')
  687. i++;
  688. }
  689. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  690. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  691. s[i] == 'a' || s[i] == 'd'))
  692. i++;
  693. *pos = i;
  694. v *= sign;
  695. if (v < -32)
  696. v = -32;
  697. if (v > 96)
  698. v = 96;
  699. return v;
  700. }
  701. static int parse_display(const char *s, unsigned int len)
  702. {
  703. unsigned int i;
  704. while (len && is_space(s[0])) { s++; len--; }
  705. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  706. ;
  707. if (i == 4 && ci_eq_n(s, "none", 4))
  708. return BR_DISPLAY_NONE;
  709. if (i == 5 && ci_eq_n(s, "block", 5))
  710. return BR_DISPLAY_BLOCK;
  711. if (i == 6 && ci_eq_n(s, "inline", 6))
  712. return BR_DISPLAY_INLINE;
  713. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  714. return BR_DISPLAY_INLINE;
  715. if (i == 10 && ci_eq_n(s, "list-item", 10))
  716. return BR_DISPLAY_BLOCK;
  717. return BR_DISPLAY_INLINE;
  718. }
  719. static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
  720. unsigned int *nout)
  721. {
  722. unsigned int i = 0;
  723. *nout = 0;
  724. while (i < len && *nout < 8) {
  725. unsigned int pstart, pend, vstart, vend, k;
  726. char prop[24];
  727. while (i < len && (is_space(s[i]) || s[i] == ';'))
  728. i++;
  729. if (i >= len)
  730. break;
  731. pstart = i;
  732. while (i < len && s[i] != ':' && s[i] != ';')
  733. i++;
  734. pend = i;
  735. if (i >= len || s[i] != ':')
  736. continue;
  737. i++;
  738. vstart = i;
  739. while (i < len && s[i] != ';')
  740. i++;
  741. vend = i;
  742. while (pstart < pend && is_space(s[pstart]))
  743. pstart++;
  744. while (pend > pstart && is_space(s[pend - 1]))
  745. pend--;
  746. while (vstart < vend && is_space(s[vstart]))
  747. vstart++;
  748. while (vend > vstart && is_space(s[vend - 1]))
  749. vend--;
  750. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  751. char c = s[pstart + k];
  752. if (c >= 'A' && c <= 'Z')
  753. c = (char)(c - 'A' + 'a');
  754. prop[k] = c;
  755. }
  756. prop[k] = '\0';
  757. if (str_eq(prop, "color")) {
  758. unsigned short c;
  759. if (parse_color(s + vstart, vend - vstart, &c)) {
  760. out[*nout].prop = PROP_COLOR;
  761. out[*nout].val = (short)c;
  762. (*nout)++;
  763. }
  764. } else if (str_eq(prop, "background-color") ||
  765. str_eq(prop, "background")) {
  766. unsigned short c;
  767. if (parse_color(s + vstart, vend - vstart, &c)) {
  768. out[*nout].prop = PROP_BG;
  769. out[*nout].val = (short)c;
  770. (*nout)++;
  771. }
  772. } else if (str_eq(prop, "font-size")) {
  773. unsigned int pos = 0;
  774. int v = parse_len(s + vstart, vend - vstart, &pos);
  775. int scale = 1;
  776. if (v >= 20)
  777. scale = 2;
  778. if (vend - vstart >= 5 &&
  779. ci_eq_n(s + vstart, "large", 5))
  780. scale = 2;
  781. if (vend - vstart >= 6 &&
  782. ci_eq_n(s + vstart, "x-large", 7))
  783. scale = 2;
  784. out[*nout].prop = PROP_SCALE;
  785. out[*nout].val = (short)scale;
  786. (*nout)++;
  787. } else if (str_eq(prop, "font-weight")) {
  788. int bold = 0;
  789. if ((vend - vstart >= 4 &&
  790. ci_eq_n(s + vstart, "bold", 4)) ||
  791. (vend - vstart >= 3 &&
  792. ci_eq_n(s + vstart, "700", 3)) ||
  793. (vend - vstart >= 3 &&
  794. ci_eq_n(s + vstart, "800", 3)) ||
  795. (vend - vstart >= 3 &&
  796. ci_eq_n(s + vstart, "900", 3)))
  797. bold = 1;
  798. out[*nout].prop = PROP_BOLD;
  799. out[*nout].val = (short)bold;
  800. (*nout)++;
  801. } else if (str_eq(prop, "text-align")) {
  802. int a = BR_ALIGN_LEFT;
  803. if ((vend - vstart >= 6 &&
  804. ci_eq_n(s + vstart, "center", 6)))
  805. a = BR_ALIGN_CENTER;
  806. else if ((vend - vstart >= 5 &&
  807. ci_eq_n(s + vstart, "right", 5)))
  808. a = BR_ALIGN_RIGHT;
  809. out[*nout].prop = PROP_ALIGN;
  810. out[*nout].val = (short)a;
  811. (*nout)++;
  812. } else if (str_eq(prop, "text-decoration")) {
  813. int u = 0;
  814. if (vend - vstart >= 9 &&
  815. ci_eq_n(s + vstart, "underline", 9))
  816. u = 1;
  817. out[*nout].prop = PROP_UNDERLINE;
  818. out[*nout].val = (short)u;
  819. (*nout)++;
  820. } else if (str_eq(prop, "display")) {
  821. out[*nout].prop = PROP_DISPLAY;
  822. out[*nout].val = (short)parse_display(s + vstart,
  823. vend - vstart);
  824. (*nout)++;
  825. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  826. int vals[4], nv = 0;
  827. unsigned int pos = 0;
  828. int is_pad = prop[0] == 'p';
  829. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  830. while (pos < vend - vstart && nv < 4) {
  831. vals[nv++] = parse_len(s + vstart,
  832. vend - vstart, &pos);
  833. while (pos < vend - vstart &&
  834. is_space(s[vstart + pos]))
  835. pos++;
  836. }
  837. if (nv == 1) {
  838. vals[1] = vals[2] = vals[3] = vals[0];
  839. } else if (nv == 2) {
  840. vals[2] = vals[0];
  841. vals[3] = vals[1];
  842. } else if (nv == 3) {
  843. vals[3] = vals[1];
  844. }
  845. if (nv > 0) {
  846. /* props are T, R, B, L adjacent */
  847. out[*nout].prop = base;
  848. out[*nout].val = (short)vals[0];
  849. (*nout)++;
  850. out[*nout].prop = (unsigned char)(base + 1);
  851. out[*nout].val = (short)vals[1];
  852. (*nout)++;
  853. out[*nout].prop = (unsigned char)(base + 2);
  854. out[*nout].val = (short)vals[2];
  855. (*nout)++;
  856. out[*nout].prop = (unsigned char)(base + 3);
  857. out[*nout].val = (short)vals[3];
  858. (*nout)++;
  859. }
  860. } else if (str_eq(prop, "margin-top") ||
  861. str_eq(prop, "margin-right") ||
  862. str_eq(prop, "margin-bottom") ||
  863. str_eq(prop, "margin-left") ||
  864. str_eq(prop, "padding-top") ||
  865. str_eq(prop, "padding-right") ||
  866. str_eq(prop, "padding-bottom") ||
  867. str_eq(prop, "padding-left")) {
  868. unsigned int pos = 0;
  869. int v = parse_len(s + vstart, vend - vstart, &pos);
  870. unsigned char p = PROP_MT;
  871. if (prop[0] == 'p')
  872. p = PROP_PT;
  873. if (str_eq(prop + 7, "right"))
  874. p = (unsigned char)(p + 1);
  875. else if (str_eq(prop + 7, "bottom"))
  876. p = (unsigned char)(p + 2);
  877. else if (str_eq(prop + 7, "left"))
  878. p = (unsigned char)(p + 3);
  879. out[*nout].prop = p;
  880. out[*nout].val = (short)v;
  881. (*nout)++;
  882. }
  883. }
  884. }
  885. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  886. {
  887. unsigned int i = 0;
  888. while (i < len && d->nrules < BR_MAX_RULES) {
  889. unsigned int sstart, send, bstart, bend, j;
  890. struct br_decl decls[8];
  891. unsigned int nd;
  892. while (i < len && is_space(s[i]))
  893. i++;
  894. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  895. i += 2;
  896. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  897. i++;
  898. i += 2;
  899. continue;
  900. }
  901. if (i >= len || s[i] == '}')
  902. break;
  903. sstart = i;
  904. while (i < len && s[i] != '{')
  905. i++;
  906. if (i >= len)
  907. break;
  908. send = i;
  909. i++;
  910. bstart = i;
  911. while (i < len && s[i] != '}')
  912. i++;
  913. bend = i;
  914. if (i < len)
  915. i++;
  916. while (sstart < send && is_space(s[sstart]))
  917. sstart++;
  918. while (send > sstart && is_space(s[send - 1]))
  919. send--;
  920. if (sstart == send)
  921. continue;
  922. if (s[sstart] == '@')
  923. continue;
  924. /* Ignore sibling combinators and pseudo-classes: they need
  925. * state we do not model. */
  926. for (j = sstart; j < send; j++)
  927. if (s[j] == '+' || s[j] == '~' || s[j] == ':')
  928. break;
  929. if (j < send)
  930. continue;
  931. parse_decls(s + bstart, bend - bstart, decls, &nd);
  932. if (!nd)
  933. continue;
  934. /* Split a comma-separated selector list into separate rules. */
  935. j = sstart;
  936. while (j <= send && d->nrules < BR_MAX_RULES) {
  937. unsigned int q = j, e, k;
  938. char sel[80];
  939. unsigned int off;
  940. while (q < send && s[q] != ',')
  941. q++;
  942. e = q;
  943. while (j < e && is_space(s[j]))
  944. j++;
  945. while (e > j && is_space(s[e - 1]))
  946. e--;
  947. if (e > j) {
  948. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  949. sel[k] = s[j + k];
  950. sel[k] = '\0';
  951. off = store_attr(d, sel, k, 79);
  952. if (off) {
  953. struct br_rule *r = &d->rules[d->nrules++];
  954. r->sel_off = (unsigned short)off;
  955. r->nd = (unsigned char)nd;
  956. for (k = 0; k < nd; k++)
  957. r->d[k] = decls[k];
  958. }
  959. }
  960. j = q + 1;
  961. if (q >= send)
  962. break;
  963. }
  964. }
  965. }
  966. /* ---- selector matching ---- */
  967. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  968. unsigned int clen)
  969. {
  970. const char *s = attr_ptr(d, d->nodes[node].class_off);
  971. unsigned int i = 0;
  972. while (s[i]) {
  973. unsigned int st, n;
  974. while (s[i] && is_space(s[i]))
  975. i++;
  976. st = i;
  977. while (s[i] && !is_space(s[i]))
  978. i++;
  979. n = i - st;
  980. if (n == clen && ci_eq_n(s + st, cls, n))
  981. return 1;
  982. }
  983. return 0;
  984. }
  985. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  986. unsigned int len)
  987. {
  988. const char *s = attr_ptr(d, d->nodes[node].id_off);
  989. unsigned int i;
  990. for (i = 0; i < len; i++)
  991. if (s[i] != id[i])
  992. return 0;
  993. return s[len] == '\0';
  994. }
  995. static int is_ident(char c)
  996. {
  997. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  998. (c >= '0' && c <= '9') || c == '-' || c == '_';
  999. }
  1000. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  1001. unsigned int len)
  1002. {
  1003. unsigned int i = 0;
  1004. int got = 0;
  1005. if (i < len && s[i] == '*') {
  1006. i++;
  1007. got = 1;
  1008. } else if (i < len && is_ident(s[i])) {
  1009. char name[16];
  1010. unsigned int n = 0, tag;
  1011. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  1012. name[n++] = s[i++];
  1013. name[n] = '\0';
  1014. tag = br_tag_from_name(name);
  1015. if (tag != d->nodes[node].tag)
  1016. return 0;
  1017. got = 1;
  1018. }
  1019. while (i < len) {
  1020. if (s[i] == '.') {
  1021. unsigned int st;
  1022. i++;
  1023. st = i;
  1024. while (i < len && is_ident(s[i]))
  1025. i++;
  1026. if (!class_has(d, node, s + st, i - st))
  1027. return 0;
  1028. got = 1;
  1029. } else if (s[i] == '#') {
  1030. unsigned int st;
  1031. i++;
  1032. st = i;
  1033. while (i < len && is_ident(s[i]))
  1034. i++;
  1035. if (!id_eq(d, node, s + st, i - st))
  1036. return 0;
  1037. got = 1;
  1038. } else if (s[i] == ':') {
  1039. return 0; /* pseudo-classes are not modelled */
  1040. } else {
  1041. i++;
  1042. }
  1043. }
  1044. return got;
  1045. }
  1046. static unsigned int selector_specificity(const char *s, unsigned int len)
  1047. {
  1048. unsigned int i, spec = 0;
  1049. for (i = 0; i < len; i++) {
  1050. if (s[i] == '#')
  1051. spec += 100;
  1052. else if (s[i] == '.')
  1053. spec += 10;
  1054. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  1055. if (is_ident(s[i]))
  1056. spec += 1;
  1057. }
  1058. }
  1059. return spec;
  1060. }
  1061. /* Match one complex selector; returns specificity or -1. */
  1062. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  1063. unsigned int len)
  1064. {
  1065. struct {
  1066. unsigned int start, end;
  1067. int child;
  1068. } parts[8];
  1069. int np = 0;
  1070. unsigned int i = 0;
  1071. int right;
  1072. while (i < len) {
  1073. int child = 0;
  1074. unsigned int st, e;
  1075. while (i < len && is_space(s[i]))
  1076. i++;
  1077. if (i < len && s[i] == '>') {
  1078. child = 1;
  1079. i++;
  1080. while (i < len && is_space(s[i]))
  1081. i++;
  1082. }
  1083. if (i >= len)
  1084. break;
  1085. st = i;
  1086. while (i < len && !is_space(s[i]) && s[i] != '>')
  1087. i++;
  1088. e = i;
  1089. if (np < 8) {
  1090. parts[np].start = st;
  1091. parts[np].end = e;
  1092. parts[np].child = child;
  1093. np++;
  1094. }
  1095. }
  1096. if (!np)
  1097. return -1;
  1098. if (!compound_match(d, node, s + parts[np - 1].start,
  1099. parts[np - 1].end - parts[np - 1].start))
  1100. return -1;
  1101. right = np - 1;
  1102. while (right > 0) {
  1103. unsigned int p = d->nodes[node].parent;
  1104. if (!p)
  1105. return -1;
  1106. if (parts[right].child) {
  1107. if (!compound_match(d, p, s + parts[right - 1].start,
  1108. parts[right - 1].end -
  1109. parts[right - 1].start))
  1110. return -1;
  1111. node = p;
  1112. } else {
  1113. while (p && !compound_match(d, p,
  1114. s + parts[right - 1].start,
  1115. parts[right - 1].end -
  1116. parts[right - 1].start))
  1117. p = d->nodes[p].parent;
  1118. if (!p)
  1119. return -1;
  1120. node = p;
  1121. }
  1122. right--;
  1123. }
  1124. return (int)selector_specificity(s, len);
  1125. }
  1126. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  1127. {
  1128. switch (dcl->prop) {
  1129. case PROP_COLOR:
  1130. st->color = (unsigned short)dcl->val;
  1131. break;
  1132. case PROP_BG:
  1133. st->bg = (unsigned short)dcl->val;
  1134. st->has_bg = 1;
  1135. break;
  1136. case PROP_SCALE:
  1137. st->scale = (unsigned char)(dcl->val >= 2 ? 2 : 1);
  1138. break;
  1139. case PROP_BOLD:
  1140. st->bold = (unsigned char)(dcl->val != 0);
  1141. break;
  1142. case PROP_ALIGN:
  1143. st->align = (unsigned char)dcl->val;
  1144. break;
  1145. case PROP_UNDERLINE:
  1146. st->underline = (unsigned char)(dcl->val != 0);
  1147. break;
  1148. case PROP_DISPLAY:
  1149. st->display = (unsigned char)dcl->val;
  1150. break;
  1151. case PROP_MT: st->mt = dcl->val; break;
  1152. case PROP_MR: st->mr = dcl->val; break;
  1153. case PROP_MB: st->mb = dcl->val; break;
  1154. case PROP_ML: st->ml = dcl->val; break;
  1155. case PROP_PT: st->pt = dcl->val; break;
  1156. case PROP_PR: st->pr = dcl->val; break;
  1157. case PROP_PB: st->pb = dcl->val; break;
  1158. case PROP_PL: st->pl = dcl->val; break;
  1159. default: break;
  1160. }
  1161. }
  1162. void br_style_for(struct br_doc *d, unsigned int node,
  1163. const struct br_style *parent, struct br_style *out)
  1164. {
  1165. unsigned int tag;
  1166. short best[PROP_COUNT];
  1167. unsigned int i;
  1168. *out = *parent;
  1169. out->has_bg = 0;
  1170. out->mt = out->mr = out->mb = out->ml = 0;
  1171. out->pt = out->pr = out->pb = out->pl = 0;
  1172. tag = d->nodes[node].tag;
  1173. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  1174. BR_DISPLAY_INLINE;
  1175. for (i = 0; i < PROP_COUNT; i++)
  1176. best[i] = -1;
  1177. switch (tag) {
  1178. case BR_TAG_HEAD:
  1179. case BR_TAG_TITLE:
  1180. case BR_TAG_META:
  1181. case BR_TAG_LINK:
  1182. case BR_TAG_BASE:
  1183. case BR_TAG_SCRIPT:
  1184. case BR_TAG_STYLE:
  1185. out->display = BR_DISPLAY_NONE;
  1186. break;
  1187. case BR_TAG_BODY:
  1188. out->mt = out->mb = out->ml = out->mr = 4;
  1189. break;
  1190. case BR_TAG_P:
  1191. out->mt = out->mb = 8;
  1192. break;
  1193. case BR_TAG_H1:
  1194. out->scale = 2; out->bold = 1;
  1195. out->mt = 10; out->mb = 6;
  1196. break;
  1197. case BR_TAG_H2:
  1198. out->scale = 2; out->bold = 1;
  1199. out->mt = 8; out->mb = 4;
  1200. break;
  1201. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  1202. out->bold = 1; out->mt = 6; out->mb = 3;
  1203. break;
  1204. case BR_TAG_UL: case BR_TAG_OL:
  1205. out->mt = out->mb = 6; out->pl = 20;
  1206. break;
  1207. case BR_TAG_LI:
  1208. out->mt = out->mb = 2;
  1209. break;
  1210. case BR_TAG_BLOCKQUOTE:
  1211. out->mt = out->mb = 6; out->ml = out->mr = 16;
  1212. break;
  1213. case BR_TAG_PRE:
  1214. out->mt = out->mb = 8; out->pl = 8;
  1215. out->has_bg = 1; out->bg = 0xEF5D;
  1216. break;
  1217. case BR_TAG_CENTER:
  1218. out->align = BR_ALIGN_CENTER;
  1219. break;
  1220. case BR_TAG_A:
  1221. out->color = 0x001B; out->underline = 1;
  1222. break;
  1223. case BR_TAG_B: case BR_TAG_STRONG:
  1224. out->bold = 1;
  1225. break;
  1226. case BR_TAG_CODE:
  1227. out->color = 0x8010;
  1228. break;
  1229. case BR_TAG_TD: case BR_TAG_TH:
  1230. out->pr = 12;
  1231. break;
  1232. default:
  1233. break;
  1234. }
  1235. /* presentational <font color>: author CSS below still wins */
  1236. if (d->nodes[node].color_off) {
  1237. unsigned short c;
  1238. const char *v = attr_ptr(d, d->nodes[node].color_off);
  1239. if (parse_color(v, s_len(v), &c))
  1240. out->color = c;
  1241. }
  1242. for (i = 0; i < d->nrules; i++) {
  1243. struct br_rule *r = &d->rules[i];
  1244. const char *sel = attr_ptr(d, r->sel_off);
  1245. int spec = selector_match(d, node, sel, s_len(sel));
  1246. unsigned int k;
  1247. if (spec < 0)
  1248. continue;
  1249. for (k = 0; k < r->nd; k++) {
  1250. unsigned char p = r->d[k].prop;
  1251. if (spec >= best[p]) {
  1252. apply_decl(out, &r->d[k]);
  1253. best[p] = (short)spec;
  1254. }
  1255. }
  1256. }
  1257. if (d->nodes[node].style_off) {
  1258. struct br_decl decls[8];
  1259. unsigned int nd, k;
  1260. const char *v = attr_ptr(d, d->nodes[node].style_off);
  1261. parse_decls(v, s_len(v), decls, &nd);
  1262. for (k = 0; k < nd; k++)
  1263. apply_decl(out, &decls[k]);
  1264. }
  1265. }
  1266. /* ---- HTML parser ---- */
  1267. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  1268. unsigned int sp, int *pre)
  1269. {
  1270. unsigned int i;
  1271. *pre = 0;
  1272. for (i = 1; i <= sp; i++)
  1273. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  1274. (*pre)++;
  1275. }
  1276. static void implicit_close(struct br_doc *d, unsigned short *stack,
  1277. unsigned int *sp, unsigned int tag)
  1278. {
  1279. while (*sp > 0) {
  1280. unsigned int top = d->nodes[stack[*sp]].tag;
  1281. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  1282. (*sp)--;
  1283. continue;
  1284. }
  1285. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  1286. (*sp)--;
  1287. continue;
  1288. }
  1289. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  1290. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  1291. (*sp)--;
  1292. continue;
  1293. }
  1294. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  1295. (*sp)--;
  1296. continue;
  1297. }
  1298. break;
  1299. }
  1300. }
  1301. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  1302. {
  1303. unsigned short stack[64];
  1304. unsigned int sp = 0;
  1305. unsigned int i = 0;
  1306. int pre = 0;
  1307. unsigned int css_len = 0;
  1308. if (!d)
  1309. return -1;
  1310. if (!d->nodes)
  1311. br_doc_init(d);
  1312. doc_reset(d);
  1313. stack[0] = 0;
  1314. d->nodes[0].type = BR_NODE_ELEM;
  1315. d->nodes[0].tag = BR_TAG_UNKNOWN;
  1316. while (i < len) {
  1317. if (html[i] != '<') {
  1318. unsigned int st = i;
  1319. while (i < len && html[i] != '<')
  1320. i++;
  1321. if (sp <= 63)
  1322. append_text(d, stack[sp], html + st, i - st, pre);
  1323. continue;
  1324. }
  1325. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1326. html[i + 3] == '-') {
  1327. int p = find_ci(html, len, "-->", 3, i + 4);
  1328. i = p >= 0 ? (unsigned int)p + 3 : len;
  1329. continue;
  1330. }
  1331. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  1332. while (i < len && html[i] != '>')
  1333. i++;
  1334. if (i < len)
  1335. i++;
  1336. continue;
  1337. }
  1338. if (i + 1 < len && html[i + 1] == '/') {
  1339. unsigned int ns = i + 2, ne, tag;
  1340. while (ns < len && is_space(html[ns]))
  1341. ns++;
  1342. ne = ns;
  1343. while (ne < len && is_ident(html[ne]))
  1344. ne++;
  1345. if (ne == ns) {
  1346. i++;
  1347. continue;
  1348. }
  1349. {
  1350. char name[16];
  1351. unsigned int k, n = ne - ns;
  1352. if (n >= sizeof(name))
  1353. n = sizeof(name) - 1;
  1354. for (k = 0; k < n; k++)
  1355. name[k] = html[ns + k];
  1356. name[n] = '\0';
  1357. tag = br_tag_from_name(name);
  1358. }
  1359. i = ne;
  1360. while (i < len && html[i] != '>')
  1361. i++;
  1362. if (i < len)
  1363. i++;
  1364. {
  1365. unsigned int k;
  1366. for (k = sp; k > 0; k--) {
  1367. if (d->nodes[stack[k]].tag == tag) {
  1368. sp = k - 1;
  1369. break;
  1370. }
  1371. }
  1372. stack_recount_pre(d, stack, sp, &pre);
  1373. }
  1374. continue;
  1375. }
  1376. /* start tag */
  1377. {
  1378. unsigned int ns = i + 1, ne, tag, k, n;
  1379. char name[16];
  1380. unsigned int attr_start, attr_end;
  1381. int selfclose = 0;
  1382. while (ns < len && is_space(html[ns]))
  1383. ns++;
  1384. ne = ns;
  1385. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  1386. ne++;
  1387. if (ne == ns) {
  1388. /* stray '<' */
  1389. append_text(d, stack[sp], html + i, 1, pre);
  1390. i++;
  1391. continue;
  1392. }
  1393. n = ne - ns;
  1394. if (n >= sizeof(name))
  1395. n = sizeof(name) - 1;
  1396. for (k = 0; k < n; k++)
  1397. name[k] = html[ns + k];
  1398. name[n] = '\0';
  1399. tag = br_tag_from_name(name);
  1400. attr_start = ne;
  1401. {
  1402. unsigned int q = ne;
  1403. char quote = 0;
  1404. while (q < len) {
  1405. char c = html[q];
  1406. if (quote) {
  1407. if (c == quote)
  1408. quote = 0;
  1409. } else if (c == '"' || c == '\'') {
  1410. quote = c;
  1411. } else if (c == '>') {
  1412. break;
  1413. }
  1414. q++;
  1415. }
  1416. attr_end = q;
  1417. if (q > attr_start && html[q - 1] == '/')
  1418. selfclose = 1;
  1419. i = q < len ? q + 1 : len;
  1420. }
  1421. if (tag == BR_TAG_SCRIPT) {
  1422. int p = find_ci(html, len, "</script", 8, i);
  1423. i = p >= 0 ? (unsigned int)p + 8 : len;
  1424. while (i < len && html[i] != '>')
  1425. i++;
  1426. if (i < len)
  1427. i++;
  1428. continue;
  1429. }
  1430. if (tag == BR_TAG_STYLE) {
  1431. int p = find_ci(html, len, "</style", 7, i);
  1432. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1433. unsigned int cend = end;
  1434. while (cend > i && is_space(html[cend - 1]))
  1435. cend--;
  1436. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  1437. for (k = i; k < cend; k++)
  1438. css_store[css_len++] = html[k];
  1439. css_store[css_len++] = '\n';
  1440. }
  1441. i = p >= 0 ? (unsigned int)p + 7 : len;
  1442. while (i < len && html[i] != '>')
  1443. i++;
  1444. if (i < len)
  1445. i++;
  1446. continue;
  1447. }
  1448. if (tag == BR_TAG_TITLE) {
  1449. int p = find_ci(html, len, "</title", 7, i);
  1450. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1451. unsigned int tn = 0;
  1452. char tmp[BR_TITLE_MAX];
  1453. tn = decode_text(html + i, end - i, tmp,
  1454. sizeof(tmp), 1);
  1455. (void)tn;
  1456. s_copy(d->title, tmp, sizeof(d->title));
  1457. i = p >= 0 ? (unsigned int)p + 7 : len;
  1458. while (i < len && html[i] != '>')
  1459. i++;
  1460. if (i < len)
  1461. i++;
  1462. continue;
  1463. }
  1464. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  1465. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  1466. unsigned int idx;
  1467. implicit_close(d, stack, &sp, tag);
  1468. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  1469. (unsigned char)tag);
  1470. if (idx)
  1471. parse_attrs(d, &d->nodes[idx],
  1472. html + attr_start,
  1473. attr_end - attr_start);
  1474. continue;
  1475. }
  1476. if (sp >= 63) {
  1477. d->truncated = 1;
  1478. continue;
  1479. }
  1480. implicit_close(d, stack, &sp, tag);
  1481. {
  1482. unsigned int idx = node_append(d, stack[sp],
  1483. BR_NODE_ELEM,
  1484. (unsigned char)tag);
  1485. if (!idx)
  1486. continue;
  1487. parse_attrs(d, &d->nodes[idx],
  1488. html + attr_start,
  1489. attr_end - attr_start);
  1490. if (!selfclose) {
  1491. sp++;
  1492. stack[sp] = (unsigned short)idx;
  1493. if (tag == BR_TAG_PRE)
  1494. pre++;
  1495. }
  1496. }
  1497. }
  1498. }
  1499. if (css_len)
  1500. parse_css(d, css_store, css_len);
  1501. return 0;
  1502. }
  1503. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  1504. {
  1505. if (!d->nodes)
  1506. br_doc_init(d);
  1507. doc_reset(d);
  1508. d->plain = 1;
  1509. d->nodes[0].type = BR_NODE_ELEM;
  1510. d->nodes[0].tag = BR_TAG_UNKNOWN;
  1511. append_text(d, 0, text, len, 1);
  1512. return 0;
  1513. }
  1514. /* A standalone image response: a synthetic document with one <img>. The
  1515. * caller decodes the already-fetched body into image slot 0 and sets the
  1516. * returned node's img_index. */
  1517. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  1518. {
  1519. unsigned int idx;
  1520. if (!d->nodes)
  1521. br_doc_init(d);
  1522. doc_reset(d);
  1523. d->nodes[0].type = BR_NODE_ELEM;
  1524. d->nodes[0].tag = BR_TAG_UNKNOWN;
  1525. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  1526. if (!idx)
  1527. return 0;
  1528. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  1529. return idx;
  1530. }