browser_html.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  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_FLOAT,
  572. PROP_COUNT
  573. };
  574. struct color_name {
  575. const char *name;
  576. unsigned short rgb;
  577. };
  578. static const struct color_name color_names[] = {
  579. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  580. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  581. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  582. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  583. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  584. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  585. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  586. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  587. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  588. { "darkgrey", 0x528A }, { 0, 0 }
  589. };
  590. static int hexval(char c)
  591. {
  592. if (c >= '0' && c <= '9')
  593. return c - '0';
  594. if (c >= 'a' && c <= 'f')
  595. return c - 'a' + 10;
  596. if (c >= 'A' && c <= 'F')
  597. return c - 'A' + 10;
  598. return -1;
  599. }
  600. static int named_color(const char *s, unsigned int len, unsigned short *out)
  601. {
  602. unsigned int i;
  603. for (i = 0; color_names[i].name; i++) {
  604. unsigned int j, n = s_len(color_names[i].name);
  605. if (n != len)
  606. continue;
  607. for (j = 0; j < n; j++) {
  608. char c = s[j];
  609. if (c >= 'A' && c <= 'Z')
  610. c = (char)(c - 'A' + 'a');
  611. if (c != color_names[i].name[j])
  612. break;
  613. }
  614. if (j == n) {
  615. *out = color_names[i].rgb;
  616. return 1;
  617. }
  618. }
  619. return 0;
  620. }
  621. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  622. * over white; the render target is a white page. */
  623. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  624. {
  625. unsigned int i = 0;
  626. while (i < len && is_space(s[i]))
  627. i++;
  628. if (i < len && s[i] == '#') {
  629. int d[6], nd = 0;
  630. i++;
  631. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  632. d[nd++] = hexval(s[i++]);
  633. if (nd == 3) {
  634. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  635. unsigned int b = (unsigned int)d[2];
  636. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  637. ((g * 17 >> 2) << 5) |
  638. (b * 17 >> 3));
  639. return 1;
  640. }
  641. if (nd >= 6) {
  642. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  643. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  644. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  645. *out = (unsigned short)(((r >> 3) << 11) |
  646. ((g >> 2) << 5) | (b >> 3));
  647. return 1;
  648. }
  649. return 0;
  650. }
  651. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  652. unsigned int v[3], k = 0, a = 255;
  653. int is_rgba = (i + 4 <= len && ci_eq_n(s + i, "rgba", 4));
  654. while (i < len && s[i] != '(')
  655. i++;
  656. if (i >= len)
  657. return 0;
  658. i++;
  659. while (k < 3 && i < len) {
  660. unsigned int x = 0;
  661. while (i < len && is_space(s[i]))
  662. i++;
  663. while (i < len && s[i] >= '0' && s[i] <= '9') {
  664. x = x * 10 + (unsigned int)(s[i] - '0');
  665. i++;
  666. }
  667. if (i < len && s[i] == '%') {
  668. x = x * 255 / 100;
  669. i++;
  670. }
  671. if (x > 255)
  672. x = 255;
  673. v[k++] = x;
  674. while (i < len && s[i] != ',' && s[i] != ')')
  675. i++;
  676. if (i < len && s[i] == ',')
  677. i++;
  678. }
  679. if (is_rgba && i < len) {
  680. unsigned int whole = 0, frac = 0, scale = 1;
  681. int percent = 0;
  682. while (i < len && is_space(s[i]))
  683. i++;
  684. while (i < len && s[i] >= '0' && s[i] <= '9') {
  685. whole = whole * 10 + (unsigned int)(s[i] - '0');
  686. i++;
  687. }
  688. if (i < len && s[i] == '.') {
  689. i++;
  690. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  691. frac = frac * 10 + (unsigned int)(s[i] - '0');
  692. scale *= 10;
  693. i++;
  694. }
  695. }
  696. while (i < len && is_space(s[i]))
  697. i++;
  698. if (i < len && s[i] == '%')
  699. percent = 1;
  700. if (percent) {
  701. a = whole > 100 ? 255 : whole * 255 / 100;
  702. } else {
  703. a = whole >= 1 ? 255 : (frac * 255) / scale;
  704. }
  705. if (a > 255)
  706. a = 255;
  707. }
  708. if (k == 3) {
  709. unsigned int r = v[0], g = v[1], b = v[2];
  710. if (a < 255) {
  711. r = (r * a + 255u * (255u - a)) / 255u;
  712. g = (g * a + 255u * (255u - a)) / 255u;
  713. b = (b * a + 255u * (255u - a)) / 255u;
  714. }
  715. *out = (unsigned short)(((r >> 3) << 11) |
  716. ((g >> 2) << 5) | (b >> 3));
  717. return 1;
  718. }
  719. return 0;
  720. }
  721. {
  722. unsigned int j = i;
  723. while (i < len && !is_space(s[i]))
  724. i++;
  725. return named_color(s + j, i - j, out);
  726. }
  727. }
  728. /* Length values for margins/padding: px, ignore percentages/units. */
  729. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  730. {
  731. unsigned int i = *pos;
  732. int sign = 1;
  733. int v = 0;
  734. while (i < len && is_space(s[i]))
  735. i++;
  736. if (i < len && (s[i] == '-' || s[i] == '+')) {
  737. if (s[i] == '-')
  738. sign = -1;
  739. i++;
  740. }
  741. while (i < len && s[i] >= '0' && s[i] <= '9') {
  742. v = v * 10 + (s[i] - '0');
  743. i++;
  744. }
  745. if (i < len && s[i] == '.') {
  746. i++;
  747. while (i < len && s[i] >= '0' && s[i] <= '9')
  748. i++;
  749. }
  750. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  751. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  752. s[i] == 'a' || s[i] == 'd'))
  753. i++;
  754. *pos = i;
  755. v *= sign;
  756. if (v < -32)
  757. v = -32;
  758. if (v > 96)
  759. v = 96;
  760. return v;
  761. }
  762. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  763. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  764. unsigned char *kind)
  765. {
  766. unsigned int i = 0;
  767. unsigned int v = 0;
  768. int digits = 0;
  769. int pct = 0;
  770. *val = 0;
  771. *kind = 0;
  772. while (i < len && is_space(s[i]))
  773. i++;
  774. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  775. return 0;
  776. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  777. return 0;
  778. while (i < len && s[i] >= '0' && s[i] <= '9') {
  779. v = v * 10 + (unsigned int)(s[i] - '0');
  780. digits = 1;
  781. i++;
  782. }
  783. if (i < len && s[i] == '.') {
  784. i++;
  785. while (i < len && s[i] >= '0' && s[i] <= '9')
  786. i++;
  787. }
  788. if (!digits)
  789. return 0;
  790. while (i < len && is_space(s[i]))
  791. i++;
  792. if (i < len && s[i] == '%')
  793. pct = 1;
  794. if (pct) {
  795. if (v > 100)
  796. v = 100;
  797. *kind = 2;
  798. } else {
  799. if (v > 2048)
  800. v = 2048;
  801. *kind = 1;
  802. }
  803. *val = (unsigned short)v;
  804. return 1;
  805. }
  806. static int parse_font_px(const char *s, unsigned int len)
  807. {
  808. unsigned int i = 0;
  809. int whole = 0, frac = 0, fscale = 1, digits = 0;
  810. int v1000, px;
  811. int is_px = 1, is_rem = 0, is_pct = 0;
  812. while (i < len && is_space(s[i]))
  813. i++;
  814. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  815. return 24;
  816. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  817. return 32;
  818. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  819. return 14;
  820. while (i < len && s[i] >= '0' && s[i] <= '9') {
  821. whole = whole * 10 + (s[i] - '0');
  822. digits = 1;
  823. i++;
  824. }
  825. if (i < len && s[i] == '.') {
  826. i++;
  827. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  828. frac = frac * 10 + (s[i] - '0');
  829. fscale *= 10;
  830. i++;
  831. }
  832. }
  833. if (!digits)
  834. return 0;
  835. while (i < len && is_space(s[i]))
  836. i++;
  837. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  838. is_rem = 1;
  839. is_px = 0;
  840. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  841. is_rem = 1;
  842. is_px = 0;
  843. } else if (i < len && s[i] == '%') {
  844. is_pct = 1;
  845. is_px = 0;
  846. }
  847. v1000 = whole * 1000 + frac * 1000 / fscale;
  848. if (is_rem)
  849. px = v1000 * 16 / 1000;
  850. else if (is_pct)
  851. px = v1000 * 16 / 100000;
  852. else if (is_px)
  853. px = v1000 / 1000;
  854. else
  855. px = v1000 / 1000;
  856. if (px < 8)
  857. px = 8;
  858. if (px > 72)
  859. px = 72;
  860. return px;
  861. }
  862. static int parse_line_height(const char *s, unsigned int len,
  863. unsigned short *px, unsigned char *pct)
  864. {
  865. unsigned int i = 0;
  866. int whole = 0, frac = 0, fscale = 1, digits = 0;
  867. int v1000;
  868. int is_px = 0, is_pct = 0;
  869. *px = 0;
  870. *pct = 0;
  871. while (i < len && is_space(s[i]))
  872. i++;
  873. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  874. return 0;
  875. while (i < len && s[i] >= '0' && s[i] <= '9') {
  876. whole = whole * 10 + (s[i] - '0');
  877. digits = 1;
  878. i++;
  879. }
  880. if (i < len && s[i] == '.') {
  881. i++;
  882. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  883. frac = frac * 10 + (s[i] - '0');
  884. fscale *= 10;
  885. i++;
  886. }
  887. }
  888. if (!digits)
  889. return 0;
  890. while (i < len && is_space(s[i]))
  891. i++;
  892. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  893. is_px = 1;
  894. else if (i < len && s[i] == '%')
  895. is_pct = 1;
  896. v1000 = whole * 1000 + frac * 1000 / fscale;
  897. if (is_px) {
  898. int v = v1000 / 1000;
  899. if (v < 8)
  900. v = 8;
  901. if (v > 160)
  902. v = 160;
  903. *px = (unsigned short)v;
  904. } else {
  905. int v = v1000 / 10; /* unitless multiplier -> percent */
  906. if (is_pct)
  907. v = v1000 / 1000;
  908. if (v < 50)
  909. v = 50;
  910. if (v > 400)
  911. v = 400;
  912. *pct = (unsigned char)v;
  913. }
  914. return 1;
  915. }
  916. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  917. {
  918. unsigned int i, j, n = s_len(needle);
  919. for (i = 0; i + n <= len; i++) {
  920. for (j = 0; j < n; j++) {
  921. char a = s[i + j];
  922. if (a >= 'A' && a <= 'Z')
  923. a = (char)(a - 'A' + 'a');
  924. if (a != needle[j])
  925. break;
  926. }
  927. if (j == n)
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. /* Pull a colour out of a border/background shorthand. */
  933. static int find_color(const char *s, unsigned int len, unsigned short *c)
  934. {
  935. unsigned int i;
  936. for (i = 0; i < len; i++) {
  937. if (s[i] == '#')
  938. return parse_color(s + i, len - i, c);
  939. if ((s[i] == 'r' || s[i] == 'R') &&
  940. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  941. return parse_color(s + i, len - i, c);
  942. }
  943. for (i = 0; i < len; ) {
  944. unsigned int st, e;
  945. while (i < len && (is_space(s[i]) || s[i] == ','))
  946. i++;
  947. st = i;
  948. while (i < len && !is_space(s[i]) && s[i] != ',')
  949. i++;
  950. e = i;
  951. if (e > st && parse_color(s + st, e - st, c))
  952. return 1;
  953. }
  954. return 0;
  955. }
  956. static int parse_display(const char *s, unsigned int len)
  957. {
  958. unsigned int i;
  959. while (len && is_space(s[0])) { s++; len--; }
  960. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  961. ;
  962. if (i == 4 && ci_eq_n(s, "none", 4))
  963. return BR_DISPLAY_NONE;
  964. if (i == 5 && ci_eq_n(s, "block", 5))
  965. return BR_DISPLAY_BLOCK;
  966. if (i == 6 && ci_eq_n(s, "inline", 6))
  967. return BR_DISPLAY_INLINE;
  968. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  969. return BR_DISPLAY_INLINE;
  970. if (i == 10 && ci_eq_n(s, "list-item", 10))
  971. return BR_DISPLAY_BLOCK;
  972. if (i == 4 && ci_eq_n(s, "flex", 4))
  973. return BR_DISPLAY_BLOCK;
  974. if (i == 5 && ci_eq_n(s, "table", 5))
  975. return BR_DISPLAY_BLOCK;
  976. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  977. return BR_DISPLAY_INLINE;
  978. if (i == 5 && ci_eq_n(s, "grid", 5))
  979. return BR_DISPLAY_BLOCK;
  980. return BR_DISPLAY_INLINE;
  981. }
  982. static void decl_add(struct br_decl *out, unsigned int *nout,
  983. unsigned char prop, int val, int important)
  984. {
  985. if (*nout >= BR_DECLS)
  986. return;
  987. out[*nout].prop = prop;
  988. out[*nout].val = (short)val;
  989. out[*nout].important = (unsigned char)(important != 0);
  990. (*nout)++;
  991. }
  992. static int parse_border_width(const char *s, unsigned int len)
  993. {
  994. unsigned int i = 0;
  995. while (i < len) {
  996. while (i < len && is_space(s[i]))
  997. i++;
  998. if (i >= len)
  999. break;
  1000. if (s[i] == '#') {
  1001. while (i < len && !is_space(s[i]))
  1002. i++;
  1003. continue;
  1004. }
  1005. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1006. while (i < len && s[i] != ')')
  1007. i++;
  1008. if (i < len)
  1009. i++;
  1010. continue;
  1011. }
  1012. if (s[i] >= '0' && s[i] <= '9') {
  1013. int v = 0;
  1014. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1015. v = v * 10 + (s[i] - '0');
  1016. i++;
  1017. }
  1018. if (v < 0)
  1019. v = 0;
  1020. if (v > 3)
  1021. v = 3;
  1022. return v;
  1023. }
  1024. while (i < len && !is_space(s[i]))
  1025. i++;
  1026. }
  1027. return -1;
  1028. }
  1029. static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
  1030. unsigned int *nout)
  1031. {
  1032. unsigned int i = 0;
  1033. *nout = 0;
  1034. while (i < len && *nout < BR_DECLS) {
  1035. unsigned int pstart, pend, vstart, vend, k;
  1036. char prop[24];
  1037. int important = 0;
  1038. while (i < len && (is_space(s[i]) || s[i] == ';'))
  1039. i++;
  1040. if (i >= len)
  1041. break;
  1042. pstart = i;
  1043. while (i < len && s[i] != ':' && s[i] != ';')
  1044. i++;
  1045. pend = i;
  1046. if (i >= len || s[i] != ':')
  1047. continue;
  1048. i++;
  1049. vstart = i;
  1050. while (i < len && s[i] != ';')
  1051. i++;
  1052. vend = i;
  1053. while (pstart < pend && is_space(s[pstart]))
  1054. pstart++;
  1055. while (pend > pstart && is_space(s[pend - 1]))
  1056. pend--;
  1057. while (vstart < vend && is_space(s[vstart]))
  1058. vstart++;
  1059. while (vend > vstart && is_space(s[vend - 1]))
  1060. vend--;
  1061. if (vend - vstart >= 10 &&
  1062. ci_eq_n(s + vend - 10, "!important", 10)) {
  1063. important = 1;
  1064. vend -= 10;
  1065. while (vend > vstart && is_space(s[vend - 1]))
  1066. vend--;
  1067. }
  1068. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  1069. char c = s[pstart + k];
  1070. if (c >= 'A' && c <= 'Z')
  1071. c = (char)(c - 'A' + 'a');
  1072. prop[k] = c;
  1073. }
  1074. prop[k] = '\0';
  1075. if (str_eq(prop, "color")) {
  1076. unsigned short c;
  1077. if (parse_color(s + vstart, vend - vstart, &c))
  1078. decl_add(out, nout, PROP_COLOR, c, important);
  1079. } else if (str_eq(prop, "background-color") ||
  1080. str_eq(prop, "background")) {
  1081. unsigned short c;
  1082. if (parse_color(s + vstart, vend - vstart, &c))
  1083. decl_add(out, nout, PROP_BG, c, important);
  1084. else if (str_eq(prop, "background") &&
  1085. find_color(s + vstart, vend - vstart, &c))
  1086. decl_add(out, nout, PROP_BG, c, important);
  1087. } else if (str_eq(prop, "font-size")) {
  1088. int px = parse_font_px(s + vstart, vend - vstart);
  1089. if (px > 0)
  1090. decl_add(out, nout, PROP_SCALE,
  1091. px >= 24 ? 2 : 1, important);
  1092. } else if (str_eq(prop, "font-weight")) {
  1093. int bold = 0;
  1094. unsigned int j;
  1095. if (vend - vstart >= 4 &&
  1096. ci_eq_n(s + vstart, "bold", 4))
  1097. bold = 1;
  1098. else {
  1099. int w = 0;
  1100. for (j = vstart; j < vend; j++)
  1101. if (s[j] >= '0' && s[j] <= '9')
  1102. w = w * 10 + (s[j] - '0');
  1103. if (w >= 500)
  1104. bold = 1;
  1105. }
  1106. decl_add(out, nout, PROP_BOLD, bold, important);
  1107. } else if (str_eq(prop, "text-align")) {
  1108. int a = BR_ALIGN_LEFT;
  1109. if (vend - vstart >= 6 &&
  1110. ci_eq_n(s + vstart, "center", 6))
  1111. a = BR_ALIGN_CENTER;
  1112. else if (vend - vstart >= 5 &&
  1113. ci_eq_n(s + vstart, "right", 5))
  1114. a = BR_ALIGN_RIGHT;
  1115. decl_add(out, nout, PROP_ALIGN, a, important);
  1116. } else if (str_eq(prop, "text-decoration")) {
  1117. int u = 0;
  1118. if (vend - vstart >= 9 &&
  1119. ci_eq_n(s + vstart, "underline", 9))
  1120. u = 1;
  1121. decl_add(out, nout, PROP_UNDERLINE, u, important);
  1122. } else if (str_eq(prop, "text-transform")) {
  1123. int u = (vend - vstart >= 9 &&
  1124. ci_eq_n(s + vstart, "uppercase", 9));
  1125. decl_add(out, nout, PROP_UPPER, u, important);
  1126. } else if (str_eq(prop, "display")) {
  1127. decl_add(out, nout, PROP_DISPLAY,
  1128. parse_display(s + vstart, vend - vstart),
  1129. important);
  1130. } else if (str_eq(prop, "float")) {
  1131. int f = 0;
  1132. if (vend - vstart >= 4 &&
  1133. ci_eq_n(s + vstart, "left", 4))
  1134. f = 1;
  1135. else if (vend - vstart >= 5 &&
  1136. ci_eq_n(s + vstart, "right", 5))
  1137. f = 2;
  1138. decl_add(out, nout, PROP_FLOAT, f, important);
  1139. } else if (str_eq(prop, "width") ||
  1140. str_eq(prop, "max-width") ||
  1141. str_eq(prop, "height")) {
  1142. unsigned short v;
  1143. unsigned char kind;
  1144. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  1145. if (str_eq(prop, "width")) {
  1146. decl_add(out, nout, PROP_WIDTH, v, important);
  1147. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  1148. important);
  1149. } else if (str_eq(prop, "max-width")) {
  1150. decl_add(out, nout, PROP_MAXW, v, important);
  1151. decl_add(out, nout, PROP_MAXW_KIND, kind,
  1152. important);
  1153. } else {
  1154. decl_add(out, nout, PROP_HEIGHT, v, important);
  1155. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  1156. important);
  1157. }
  1158. }
  1159. } else if (str_eq(prop, "line-height")) {
  1160. unsigned short px;
  1161. unsigned char pct;
  1162. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  1163. if (px) {
  1164. decl_add(out, nout, PROP_LH_PX, px, important);
  1165. decl_add(out, nout, PROP_LH_PCT, 0, important);
  1166. } else {
  1167. decl_add(out, nout, PROP_LH_PX, 0, important);
  1168. decl_add(out, nout, PROP_LH_PCT, pct, important);
  1169. }
  1170. }
  1171. } else if (str_eq(prop, "list-style") ||
  1172. str_eq(prop, "list-style-type")) {
  1173. int none = (vend - vstart >= 4 &&
  1174. contains_ci_len(s + vstart, vend - vstart,
  1175. "none"));
  1176. decl_add(out, nout, PROP_LIST_NONE, none, important);
  1177. } else if (str_eq(prop, "border") ||
  1178. str_eq(prop, "border-top") ||
  1179. str_eq(prop, "border-right") ||
  1180. str_eq(prop, "border-bottom") ||
  1181. str_eq(prop, "border-left") ||
  1182. str_eq(prop, "border-color") ||
  1183. str_eq(prop, "border-width")) {
  1184. unsigned short c;
  1185. int bw;
  1186. int have_color;
  1187. if (vend - vstart >= 4 &&
  1188. ci_eq_n(s + vstart, "none", 4)) {
  1189. decl_add(out, nout, PROP_BORDER_W, 0, important);
  1190. } else {
  1191. bw = parse_border_width(s + vstart,
  1192. vend - vstart);
  1193. have_color = find_color(s + vstart,
  1194. vend - vstart, &c);
  1195. if (have_color) {
  1196. decl_add(out, nout, PROP_BORDER_COLOR, c,
  1197. important);
  1198. if (bw < 0)
  1199. bw = 1;
  1200. }
  1201. if (bw >= 0) {
  1202. if (bw > 3)
  1203. bw = 3;
  1204. decl_add(out, nout, PROP_BORDER_W, bw,
  1205. important);
  1206. }
  1207. }
  1208. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  1209. int vals[4], nv = 0;
  1210. unsigned int pos = 0;
  1211. int is_pad = prop[0] == 'p';
  1212. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  1213. while (pos < vend - vstart && nv < 4) {
  1214. vals[nv++] = parse_len(s + vstart,
  1215. vend - vstart, &pos);
  1216. while (pos < vend - vstart &&
  1217. is_space(s[vstart + pos]))
  1218. pos++;
  1219. }
  1220. if (nv == 1) {
  1221. vals[1] = vals[2] = vals[3] = vals[0];
  1222. } else if (nv == 2) {
  1223. vals[2] = vals[0];
  1224. vals[3] = vals[1];
  1225. } else if (nv == 3) {
  1226. vals[3] = vals[1];
  1227. }
  1228. if (nv > 0) {
  1229. decl_add(out, nout, base, vals[0], important);
  1230. decl_add(out, nout, (unsigned char)(base + 1),
  1231. vals[1], important);
  1232. decl_add(out, nout, (unsigned char)(base + 2),
  1233. vals[2], important);
  1234. decl_add(out, nout, (unsigned char)(base + 3),
  1235. vals[3], important);
  1236. }
  1237. } else if (str_eq(prop, "margin-top") ||
  1238. str_eq(prop, "margin-right") ||
  1239. str_eq(prop, "margin-bottom") ||
  1240. str_eq(prop, "margin-left") ||
  1241. str_eq(prop, "padding-top") ||
  1242. str_eq(prop, "padding-right") ||
  1243. str_eq(prop, "padding-bottom") ||
  1244. str_eq(prop, "padding-left")) {
  1245. unsigned int pos = 0;
  1246. int v = parse_len(s + vstart, vend - vstart, &pos);
  1247. unsigned char p = PROP_MT;
  1248. const char *side;
  1249. if (prop[0] == 'p')
  1250. p = PROP_PT;
  1251. side = prop + (prop[0] == 'p' ? 8 : 7);
  1252. if (str_eq(side, "right"))
  1253. p = (unsigned char)(p + 1);
  1254. else if (str_eq(side, "bottom"))
  1255. p = (unsigned char)(p + 2);
  1256. else if (str_eq(side, "left"))
  1257. p = (unsigned char)(p + 3);
  1258. decl_add(out, nout, p, v, important);
  1259. }
  1260. }
  1261. }
  1262. /* Skip @media/@font-face/@keyframes/@import without descending into their
  1263. * nested blocks. This is the mobile/base rules path: media enhancements are
  1264. * deliberately ignored for now. */
  1265. static unsigned int skip_at_rule(const char *s, unsigned int len,
  1266. unsigned int i)
  1267. {
  1268. unsigned int j = i;
  1269. int depth;
  1270. while (j < len && s[j] != '{' && s[j] != ';')
  1271. j++;
  1272. if (j >= len)
  1273. return len;
  1274. if (s[j] == ';')
  1275. return j + 1;
  1276. depth = 1;
  1277. j++;
  1278. while (j < len && depth) {
  1279. if (s[j] == '{')
  1280. depth++;
  1281. else if (s[j] == '}')
  1282. depth--;
  1283. j++;
  1284. }
  1285. return j;
  1286. }
  1287. static unsigned int viewport_w = 1024;
  1288. void br_set_viewport(unsigned int width)
  1289. {
  1290. viewport_w = width ? width : 1024;
  1291. }
  1292. static unsigned int media_len_px(const char *s, unsigned int len)
  1293. {
  1294. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  1295. int em = 0;
  1296. while (i < len && is_space(s[i]))
  1297. i++;
  1298. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1299. whole = whole * 10 + (unsigned int)(s[i] - '0');
  1300. i++;
  1301. }
  1302. if (i < len && s[i] == '.') {
  1303. i++;
  1304. while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
  1305. frac = frac * 10 + (unsigned int)(s[i] - '0');
  1306. scale *= 10;
  1307. i++;
  1308. }
  1309. }
  1310. while (i < len && is_space(s[i]))
  1311. i++;
  1312. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1313. em = 1;
  1314. i += 2;
  1315. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1316. em = 1;
  1317. i += 3;
  1318. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  1319. i += 2;
  1320. }
  1321. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  1322. if (em)
  1323. return v * 16 / 100;
  1324. return v / 100;
  1325. }
  1326. /* One parenthesised media feature: min-width/max-width and the modern
  1327. * (width <= 600px) form. Unknown features are treated as true. */
  1328. static int media_feature_matches(const char *s, unsigned int len)
  1329. {
  1330. unsigned int i;
  1331. while (len && is_space(*s)) { s++; len--; }
  1332. while (len && is_space(s[len - 1])) len--;
  1333. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  1334. for (i = 9; i < len && s[i] != ':'; i++)
  1335. ;
  1336. if (i < len)
  1337. return viewport_w >= media_len_px(s + i + 1,
  1338. len - i - 1);
  1339. return 1;
  1340. }
  1341. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  1342. for (i = 9; i < len && s[i] != ':'; i++)
  1343. ;
  1344. if (i < len)
  1345. return viewport_w <= media_len_px(s + i + 1,
  1346. len - i - 1);
  1347. return 1;
  1348. }
  1349. for (i = 0; i < len; i++) {
  1350. if (s[i] == '<' || s[i] == '>') {
  1351. int max = s[i] == '<';
  1352. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  1353. if (i + 1 < len && s[i + 1] == '=')
  1354. v = media_len_px(s + i + 2, len - i - 2);
  1355. return max ? viewport_w <= v : viewport_w >= v;
  1356. }
  1357. }
  1358. return 1;
  1359. }
  1360. static int media_query_matches(const char *s, unsigned int len)
  1361. {
  1362. unsigned int i;
  1363. if (contains_ci_len(s, len, "print") &&
  1364. !contains_ci_len(s, len, "screen"))
  1365. return 0;
  1366. if (contains_ci_len(s, len, "not "))
  1367. return 0;
  1368. for (i = 0; i < len; i++) {
  1369. if (s[i] == '(') {
  1370. unsigned int st = ++i;
  1371. while (i < len && s[i] != ')')
  1372. i++;
  1373. if (!media_feature_matches(s + st, i - st))
  1374. return 0;
  1375. }
  1376. }
  1377. return 1;
  1378. }
  1379. static int media_list_matches(const char *s, unsigned int len)
  1380. {
  1381. unsigned int i = 0;
  1382. while (i < len) {
  1383. unsigned int st = i;
  1384. while (i < len && s[i] != ',')
  1385. i++;
  1386. if (media_query_matches(s + st, i - st))
  1387. return 1;
  1388. i++;
  1389. }
  1390. return 0;
  1391. }
  1392. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  1393. {
  1394. unsigned int i = 0;
  1395. while (i < len && d->nrules < BR_MAX_RULES) {
  1396. unsigned int sstart, send, bstart, bend, j;
  1397. struct br_decl decls[BR_DECLS];
  1398. unsigned int nd;
  1399. while (i < len && is_space(s[i]))
  1400. i++;
  1401. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  1402. i += 2;
  1403. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  1404. i++;
  1405. i += 2;
  1406. continue;
  1407. }
  1408. if (i >= len || s[i] == '}')
  1409. break;
  1410. if (s[i] == '@') {
  1411. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  1412. unsigned int q = i + 6;
  1413. while (q < len && s[q] != '{')
  1414. q++;
  1415. if (q < len &&
  1416. media_list_matches(s + i + 6,
  1417. q - (i + 6))) {
  1418. unsigned int b = q + 1, e = b;
  1419. int depth = 1;
  1420. while (e < len && depth) {
  1421. if (s[e] == '{')
  1422. depth++;
  1423. else if (s[e] == '}')
  1424. depth--;
  1425. e++;
  1426. }
  1427. if (depth == 0)
  1428. parse_css(d, s + b,
  1429. (e - 1) - b);
  1430. i = e;
  1431. } else {
  1432. i = skip_at_rule(s, len, i);
  1433. }
  1434. continue;
  1435. }
  1436. i = skip_at_rule(s, len, i);
  1437. continue;
  1438. }
  1439. sstart = i;
  1440. while (i < len && s[i] != '{')
  1441. i++;
  1442. if (i >= len)
  1443. break;
  1444. send = i;
  1445. i++;
  1446. bstart = i;
  1447. while (i < len && s[i] != '}')
  1448. i++;
  1449. bend = i;
  1450. if (i < len)
  1451. i++;
  1452. while (sstart < send && is_space(s[sstart]))
  1453. sstart++;
  1454. while (send > sstart && is_space(s[send - 1]))
  1455. send--;
  1456. if (sstart == send)
  1457. continue;
  1458. parse_decls(s + bstart, bend - bstart, decls, &nd);
  1459. if (!nd)
  1460. continue;
  1461. /* Split a comma-separated selector list into separate rules. */
  1462. j = sstart;
  1463. while (j <= send && d->nrules < BR_MAX_RULES) {
  1464. unsigned int q = j, e, k;
  1465. char sel[80];
  1466. unsigned int off;
  1467. while (q < send && s[q] != ',')
  1468. q++;
  1469. e = q;
  1470. while (j < e && is_space(s[j]))
  1471. j++;
  1472. while (e > j && is_space(s[e - 1]))
  1473. e--;
  1474. if (e > j) {
  1475. /* Ignore single selectors with sibling combinators,
  1476. * pseudo-classes or attribute tests: they need
  1477. * state/attributes we do not model. The other
  1478. * selectors in the list still apply. */
  1479. for (k = j; k < e; k++)
  1480. if (s[k] == '+' || s[k] == '~' ||
  1481. s[k] == ':' || s[k] == '[')
  1482. break;
  1483. if (k < e) {
  1484. j = q + 1;
  1485. if (q >= send)
  1486. break;
  1487. continue;
  1488. }
  1489. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  1490. sel[k] = s[j + k];
  1491. sel[k] = '\0';
  1492. off = store_attr(d, sel, k, 79);
  1493. if (off) {
  1494. struct br_rule *r = &d->rules[d->nrules++];
  1495. r->sel_off = (unsigned short)off;
  1496. r->nd = (unsigned char)nd;
  1497. for (k = 0; k < nd; k++)
  1498. r->d[k] = decls[k];
  1499. }
  1500. }
  1501. j = q + 1;
  1502. if (q >= send)
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  1508. {
  1509. parse_css(d, css, len);
  1510. }
  1511. void br_parse_inline_css(struct br_doc *d)
  1512. {
  1513. if (d->inline_css_len)
  1514. parse_css(d, css_store, d->inline_css_len);
  1515. d->inline_css_len = 0;
  1516. }
  1517. void br_reset_styles(struct br_doc *d)
  1518. {
  1519. unsigned int i;
  1520. for (i = 0; i < d->nnode; i++)
  1521. d->nodes[i].style_valid = 0;
  1522. }
  1523. /* ---- selector matching ---- */
  1524. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  1525. unsigned int clen)
  1526. {
  1527. const char *s = attr_ptr(d, d->nodes[node].class_off);
  1528. unsigned int i = 0;
  1529. while (s[i]) {
  1530. unsigned int st, n;
  1531. while (s[i] && is_space(s[i]))
  1532. i++;
  1533. st = i;
  1534. while (s[i] && !is_space(s[i]))
  1535. i++;
  1536. n = i - st;
  1537. if (n == clen && ci_eq_n(s + st, cls, n))
  1538. return 1;
  1539. }
  1540. return 0;
  1541. }
  1542. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  1543. unsigned int len)
  1544. {
  1545. const char *s = attr_ptr(d, d->nodes[node].id_off);
  1546. unsigned int i;
  1547. for (i = 0; i < len; i++)
  1548. if (s[i] != id[i])
  1549. return 0;
  1550. return s[len] == '\0';
  1551. }
  1552. static int is_ident(char c)
  1553. {
  1554. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1555. (c >= '0' && c <= '9') || c == '-' || c == '_';
  1556. }
  1557. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  1558. unsigned int len)
  1559. {
  1560. unsigned int i = 0;
  1561. int got = 0;
  1562. if (i < len && s[i] == '*') {
  1563. i++;
  1564. got = 1;
  1565. } else if (i < len && is_ident(s[i])) {
  1566. char name[16];
  1567. unsigned int n = 0, tag;
  1568. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  1569. name[n++] = s[i++];
  1570. name[n] = '\0';
  1571. tag = br_tag_from_name(name);
  1572. if (tag != d->nodes[node].tag)
  1573. return 0;
  1574. got = 1;
  1575. }
  1576. while (i < len) {
  1577. if (s[i] == '.') {
  1578. unsigned int st;
  1579. i++;
  1580. st = i;
  1581. while (i < len && is_ident(s[i]))
  1582. i++;
  1583. if (!class_has(d, node, s + st, i - st))
  1584. return 0;
  1585. got = 1;
  1586. } else if (s[i] == '#') {
  1587. unsigned int st;
  1588. i++;
  1589. st = i;
  1590. while (i < len && is_ident(s[i]))
  1591. i++;
  1592. if (!id_eq(d, node, s + st, i - st))
  1593. return 0;
  1594. got = 1;
  1595. } else if (s[i] == ':') {
  1596. return 0; /* pseudo-classes are not modelled */
  1597. } else {
  1598. i++;
  1599. }
  1600. }
  1601. return got;
  1602. }
  1603. static unsigned int selector_specificity(const char *s, unsigned int len)
  1604. {
  1605. unsigned int i, spec = 0;
  1606. for (i = 0; i < len; i++) {
  1607. if (s[i] == '#')
  1608. spec += 100;
  1609. else if (s[i] == '.')
  1610. spec += 10;
  1611. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  1612. if (is_ident(s[i]))
  1613. spec += 1;
  1614. }
  1615. }
  1616. return spec;
  1617. }
  1618. /* Match one complex selector; returns specificity or -1. */
  1619. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  1620. unsigned int len)
  1621. {
  1622. struct {
  1623. unsigned int start, end;
  1624. int child;
  1625. } parts[8];
  1626. int np = 0;
  1627. unsigned int i = 0;
  1628. int right;
  1629. while (i < len) {
  1630. int child = 0;
  1631. unsigned int st, e;
  1632. while (i < len && is_space(s[i]))
  1633. i++;
  1634. if (i < len && s[i] == '>') {
  1635. child = 1;
  1636. i++;
  1637. while (i < len && is_space(s[i]))
  1638. i++;
  1639. }
  1640. if (i >= len)
  1641. break;
  1642. st = i;
  1643. while (i < len && !is_space(s[i]) && s[i] != '>')
  1644. i++;
  1645. e = i;
  1646. if (np < 8) {
  1647. parts[np].start = st;
  1648. parts[np].end = e;
  1649. parts[np].child = child;
  1650. np++;
  1651. }
  1652. }
  1653. if (!np)
  1654. return -1;
  1655. if (!compound_match(d, node, s + parts[np - 1].start,
  1656. parts[np - 1].end - parts[np - 1].start))
  1657. return -1;
  1658. right = np - 1;
  1659. while (right > 0) {
  1660. unsigned int p = d->nodes[node].parent;
  1661. if (!p)
  1662. return -1;
  1663. if (parts[right].child) {
  1664. if (!compound_match(d, p, s + parts[right - 1].start,
  1665. parts[right - 1].end -
  1666. parts[right - 1].start))
  1667. return -1;
  1668. node = p;
  1669. } else {
  1670. while (p && !compound_match(d, p,
  1671. s + parts[right - 1].start,
  1672. parts[right - 1].end -
  1673. parts[right - 1].start))
  1674. p = d->nodes[p].parent;
  1675. if (!p)
  1676. return -1;
  1677. node = p;
  1678. }
  1679. right--;
  1680. }
  1681. return (int)selector_specificity(s, len);
  1682. }
  1683. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  1684. {
  1685. switch (dcl->prop) {
  1686. case PROP_COLOR:
  1687. st->color = (unsigned short)dcl->val;
  1688. break;
  1689. case PROP_BG:
  1690. st->bg = (unsigned short)dcl->val;
  1691. st->has_bg = 1;
  1692. break;
  1693. case PROP_SCALE:
  1694. st->scale = (unsigned char)(dcl->val >= 2 ? 2 : 1);
  1695. break;
  1696. case PROP_BOLD:
  1697. st->bold = (unsigned char)(dcl->val != 0);
  1698. break;
  1699. case PROP_ALIGN:
  1700. st->align = (unsigned char)dcl->val;
  1701. break;
  1702. case PROP_UNDERLINE:
  1703. st->underline = (unsigned char)(dcl->val != 0);
  1704. break;
  1705. case PROP_DISPLAY:
  1706. st->display = (unsigned char)dcl->val;
  1707. break;
  1708. case PROP_MT: st->mt = dcl->val; break;
  1709. case PROP_MR: st->mr = dcl->val; break;
  1710. case PROP_MB: st->mb = dcl->val; break;
  1711. case PROP_ML: st->ml = dcl->val; break;
  1712. case PROP_PT: st->pt = dcl->val; break;
  1713. case PROP_PR: st->pr = dcl->val; break;
  1714. case PROP_PB: st->pb = dcl->val; break;
  1715. case PROP_PL: st->pl = dcl->val; break;
  1716. case PROP_BORDER_W:
  1717. st->border_w = (unsigned char)dcl->val;
  1718. if (st->border_w && !st->border_color)
  1719. st->border_color = st->color;
  1720. break;
  1721. case PROP_BORDER_COLOR:
  1722. st->border_color = (unsigned short)dcl->val;
  1723. break;
  1724. case PROP_LIST_NONE:
  1725. st->no_bullet = (unsigned char)(dcl->val != 0);
  1726. break;
  1727. case PROP_WIDTH:
  1728. st->width = (unsigned short)dcl->val;
  1729. break;
  1730. case PROP_WIDTH_KIND:
  1731. st->width_kind = (unsigned char)dcl->val;
  1732. break;
  1733. case PROP_MAXW:
  1734. st->max_width = (unsigned short)dcl->val;
  1735. break;
  1736. case PROP_MAXW_KIND:
  1737. st->max_width_kind = (unsigned char)dcl->val;
  1738. break;
  1739. case PROP_HEIGHT:
  1740. st->height = (unsigned short)dcl->val;
  1741. break;
  1742. case PROP_HEIGHT_KIND:
  1743. st->height_kind = (unsigned char)dcl->val;
  1744. break;
  1745. case PROP_LH_PCT:
  1746. st->line_h_pct = (unsigned char)dcl->val;
  1747. break;
  1748. case PROP_LH_PX:
  1749. st->line_h_px = (unsigned short)dcl->val;
  1750. break;
  1751. case PROP_UPPER:
  1752. st->uppercase = (unsigned char)(dcl->val != 0);
  1753. break;
  1754. case PROP_FLOAT:
  1755. st->float_left = (unsigned char)dcl->val;
  1756. break;
  1757. default:
  1758. break;
  1759. }
  1760. }
  1761. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  1762. int spec, short *best, short *best_imp)
  1763. {
  1764. unsigned char p = dcl->prop;
  1765. if (p >= PROP_COUNT)
  1766. return;
  1767. if (dcl->important) {
  1768. if (spec >= best_imp[p]) {
  1769. apply_decl(st, dcl);
  1770. best_imp[p] = (short)spec;
  1771. }
  1772. } else if (best_imp[p] < 0 && spec >= best[p]) {
  1773. apply_decl(st, dcl);
  1774. best[p] = (short)spec;
  1775. }
  1776. }
  1777. void br_style_for(struct br_doc *d, unsigned int node,
  1778. const struct br_style *parent, struct br_style *out)
  1779. {
  1780. unsigned int tag;
  1781. short best[PROP_COUNT], best_imp[PROP_COUNT];
  1782. unsigned int i;
  1783. if (d->nodes[node].style_valid) {
  1784. *out = d->nodes[node].style;
  1785. return;
  1786. }
  1787. *out = *parent;
  1788. out->has_bg = 0;
  1789. out->float_left = 0;
  1790. out->border_w = 0;
  1791. out->border_color = 0;
  1792. out->width = out->max_width = out->height = 0;
  1793. out->width_kind = out->max_width_kind = out->height_kind = 0;
  1794. out->mt = out->mr = out->mb = out->ml = 0;
  1795. out->pt = out->pr = out->pb = out->pl = 0;
  1796. tag = d->nodes[node].tag;
  1797. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  1798. BR_DISPLAY_INLINE;
  1799. for (i = 0; i < PROP_COUNT; i++) {
  1800. best[i] = -1;
  1801. best_imp[i] = -1;
  1802. }
  1803. switch (tag) {
  1804. case BR_TAG_HEAD:
  1805. case BR_TAG_TITLE:
  1806. case BR_TAG_META:
  1807. case BR_TAG_LINK:
  1808. case BR_TAG_BASE:
  1809. case BR_TAG_SCRIPT:
  1810. case BR_TAG_STYLE:
  1811. out->display = BR_DISPLAY_NONE;
  1812. break;
  1813. case BR_TAG_BODY:
  1814. out->mt = out->mb = out->ml = out->mr = 4;
  1815. break;
  1816. case BR_TAG_P:
  1817. out->mt = out->mb = 8;
  1818. break;
  1819. case BR_TAG_H1:
  1820. out->scale = 2; out->bold = 1;
  1821. out->mt = 10; out->mb = 6;
  1822. break;
  1823. case BR_TAG_H2:
  1824. out->scale = 2; out->bold = 1;
  1825. out->mt = 8; out->mb = 4;
  1826. break;
  1827. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  1828. out->bold = 1; out->mt = 6; out->mb = 3;
  1829. break;
  1830. case BR_TAG_UL: case BR_TAG_OL:
  1831. out->mt = out->mb = 6; out->pl = 20;
  1832. break;
  1833. case BR_TAG_LI:
  1834. out->mt = out->mb = 2;
  1835. break;
  1836. case BR_TAG_BLOCKQUOTE:
  1837. out->mt = out->mb = 6; out->ml = out->mr = 16;
  1838. break;
  1839. case BR_TAG_PRE:
  1840. out->mt = out->mb = 8; out->pl = 8;
  1841. out->has_bg = 1; out->bg = 0xEF5D;
  1842. break;
  1843. case BR_TAG_CENTER:
  1844. out->align = BR_ALIGN_CENTER;
  1845. break;
  1846. case BR_TAG_A:
  1847. out->color = 0x001B; out->underline = 1;
  1848. break;
  1849. case BR_TAG_B: case BR_TAG_STRONG:
  1850. out->bold = 1;
  1851. break;
  1852. case BR_TAG_CODE:
  1853. out->color = 0x8010;
  1854. break;
  1855. case BR_TAG_TD: case BR_TAG_TH:
  1856. out->pr = 12;
  1857. break;
  1858. case BR_TAG_VIDEO:
  1859. out->display = BR_DISPLAY_BLOCK;
  1860. break;
  1861. default:
  1862. break;
  1863. }
  1864. /* presentational <font color>: author CSS below still wins */
  1865. if (d->nodes[node].color_off) {
  1866. unsigned short c;
  1867. const char *v = attr_ptr(d, d->nodes[node].color_off);
  1868. if (parse_color(v, s_len(v), &c))
  1869. out->color = c;
  1870. }
  1871. for (i = 0; i < d->nrules; i++) {
  1872. struct br_rule *r = &d->rules[i];
  1873. const char *sel = attr_ptr(d, r->sel_off);
  1874. int spec = selector_match(d, node, sel, s_len(sel));
  1875. unsigned int k;
  1876. if (spec < 0)
  1877. continue;
  1878. for (k = 0; k < r->nd; k++)
  1879. cascade_decl(out, &r->d[k], spec, best, best_imp);
  1880. }
  1881. if (d->nodes[node].style_off) {
  1882. struct br_decl decls[BR_DECLS];
  1883. unsigned int nd, k;
  1884. const char *v = attr_ptr(d, d->nodes[node].style_off);
  1885. parse_decls(v, s_len(v), decls, &nd);
  1886. for (k = 0; k < nd; k++)
  1887. cascade_decl(out, &decls[k], 1000, best, best_imp);
  1888. }
  1889. d->nodes[node].style = *out;
  1890. d->nodes[node].style_valid = 1;
  1891. }
  1892. /* ---- HTML parser ---- */
  1893. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  1894. unsigned int sp, int *pre)
  1895. {
  1896. unsigned int i;
  1897. *pre = 0;
  1898. for (i = 1; i <= sp; i++)
  1899. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  1900. (*pre)++;
  1901. }
  1902. static void implicit_close(struct br_doc *d, unsigned short *stack,
  1903. unsigned int *sp, unsigned int tag)
  1904. {
  1905. while (*sp > 0) {
  1906. unsigned int top = d->nodes[stack[*sp]].tag;
  1907. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  1908. (*sp)--;
  1909. continue;
  1910. }
  1911. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  1912. (*sp)--;
  1913. continue;
  1914. }
  1915. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  1916. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  1917. (*sp)--;
  1918. continue;
  1919. }
  1920. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  1921. (*sp)--;
  1922. continue;
  1923. }
  1924. break;
  1925. }
  1926. }
  1927. static int has_attr_token(const char *s, unsigned int len, const char *name)
  1928. {
  1929. unsigned int i, n = s_len(name);
  1930. for (i = 0; i + n <= len; i++) {
  1931. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  1932. continue;
  1933. if (!ci_eq_n(s + i, name, n))
  1934. continue;
  1935. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  1936. return 1;
  1937. }
  1938. return 0;
  1939. }
  1940. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  1941. {
  1942. unsigned short stack[64];
  1943. unsigned int sp = 0;
  1944. unsigned int i = 0;
  1945. int pre = 0;
  1946. unsigned int css_len = 0;
  1947. if (!d)
  1948. return -1;
  1949. if (!d->nodes)
  1950. br_doc_init(d);
  1951. doc_reset(d);
  1952. stack[0] = 0;
  1953. d->nodes[0].type = BR_NODE_ELEM;
  1954. d->nodes[0].tag = BR_TAG_UNKNOWN;
  1955. while (i < len) {
  1956. if (html[i] != '<') {
  1957. unsigned int st = i;
  1958. while (i < len && html[i] != '<')
  1959. i++;
  1960. if (sp <= 63)
  1961. append_text(d, stack[sp], html + st, i - st, pre);
  1962. continue;
  1963. }
  1964. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1965. html[i + 3] == '-') {
  1966. int p = find_ci(html, len, "-->", 3, i + 4);
  1967. i = p >= 0 ? (unsigned int)p + 3 : len;
  1968. continue;
  1969. }
  1970. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  1971. while (i < len && html[i] != '>')
  1972. i++;
  1973. if (i < len)
  1974. i++;
  1975. continue;
  1976. }
  1977. if (i + 1 < len && html[i + 1] == '/') {
  1978. unsigned int ns = i + 2, ne, tag;
  1979. while (ns < len && is_space(html[ns]))
  1980. ns++;
  1981. ne = ns;
  1982. while (ne < len && is_ident(html[ne]))
  1983. ne++;
  1984. if (ne == ns) {
  1985. i++;
  1986. continue;
  1987. }
  1988. {
  1989. char name[16];
  1990. unsigned int k, n = ne - ns;
  1991. if (n >= sizeof(name))
  1992. n = sizeof(name) - 1;
  1993. for (k = 0; k < n; k++)
  1994. name[k] = html[ns + k];
  1995. name[n] = '\0';
  1996. tag = br_tag_from_name(name);
  1997. }
  1998. i = ne;
  1999. while (i < len && html[i] != '>')
  2000. i++;
  2001. if (i < len)
  2002. i++;
  2003. {
  2004. unsigned int k;
  2005. for (k = sp; k > 0; k--) {
  2006. if (d->nodes[stack[k]].tag == tag) {
  2007. sp = k - 1;
  2008. break;
  2009. }
  2010. }
  2011. stack_recount_pre(d, stack, sp, &pre);
  2012. }
  2013. continue;
  2014. }
  2015. /* start tag */
  2016. {
  2017. unsigned int ns = i + 1, ne, tag, k, n;
  2018. char name[16];
  2019. unsigned int attr_start, attr_end;
  2020. int selfclose = 0;
  2021. while (ns < len && is_space(html[ns]))
  2022. ns++;
  2023. ne = ns;
  2024. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  2025. ne++;
  2026. if (ne == ns) {
  2027. /* stray '<' */
  2028. append_text(d, stack[sp], html + i, 1, pre);
  2029. i++;
  2030. continue;
  2031. }
  2032. n = ne - ns;
  2033. if (n >= sizeof(name))
  2034. n = sizeof(name) - 1;
  2035. for (k = 0; k < n; k++)
  2036. name[k] = html[ns + k];
  2037. name[n] = '\0';
  2038. tag = br_tag_from_name(name);
  2039. attr_start = ne;
  2040. {
  2041. unsigned int q = ne;
  2042. char quote = 0;
  2043. while (q < len) {
  2044. char c = html[q];
  2045. if (quote) {
  2046. if (c == quote)
  2047. quote = 0;
  2048. } else if (c == '"' || c == '\'') {
  2049. quote = c;
  2050. } else if (c == '>') {
  2051. break;
  2052. }
  2053. q++;
  2054. }
  2055. attr_end = q;
  2056. if (q > attr_start && html[q - 1] == '/')
  2057. selfclose = 1;
  2058. i = q < len ? q + 1 : len;
  2059. }
  2060. if (tag == BR_TAG_SCRIPT) {
  2061. int p = find_ci(html, len, "</script", 8, i);
  2062. i = p >= 0 ? (unsigned int)p + 8 : len;
  2063. while (i < len && html[i] != '>')
  2064. i++;
  2065. if (i < len)
  2066. i++;
  2067. continue;
  2068. }
  2069. if (tag == BR_TAG_SVG && !selfclose) {
  2070. /* Inline SVG is not rendered; skip its subtree so
  2071. * logos and icon sprites do not consume the node
  2072. * arena. */
  2073. int p = find_ci(html, len, "</svg>", 6, i);
  2074. i = p >= 0 ? (unsigned int)p + 6 : len;
  2075. continue;
  2076. }
  2077. if (tag == BR_TAG_DETAILS && !selfclose &&
  2078. !has_attr_token(html + attr_start,
  2079. attr_end - attr_start, "open")) {
  2080. /* Closed <details> is collapsed (and this browser has
  2081. * no script to open it); skipping the subtree keeps
  2082. * mobile mega-menus out of the node arena. */
  2083. int p = find_ci(html, len, "</details>", 10, i);
  2084. i = p >= 0 ? (unsigned int)p + 10 : len;
  2085. continue;
  2086. }
  2087. if (tag == BR_TAG_STYLE) {
  2088. int p = find_ci(html, len, "</style", 7, i);
  2089. unsigned int end = p >= 0 ? (unsigned int)p : len;
  2090. unsigned int cend = end;
  2091. while (cend > i && is_space(html[cend - 1]))
  2092. cend--;
  2093. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  2094. for (k = i; k < cend; k++)
  2095. css_store[css_len++] = html[k];
  2096. css_store[css_len++] = '\n';
  2097. }
  2098. i = p >= 0 ? (unsigned int)p + 7 : len;
  2099. while (i < len && html[i] != '>')
  2100. i++;
  2101. if (i < len)
  2102. i++;
  2103. continue;
  2104. }
  2105. if (tag == BR_TAG_TITLE) {
  2106. int p = find_ci(html, len, "</title", 7, i);
  2107. unsigned int end = p >= 0 ? (unsigned int)p : len;
  2108. unsigned int tn = 0;
  2109. char tmp[BR_TITLE_MAX];
  2110. tn = decode_text(html + i, end - i, tmp,
  2111. sizeof(tmp), 1);
  2112. (void)tn;
  2113. s_copy(d->title, tmp, sizeof(d->title));
  2114. i = p >= 0 ? (unsigned int)p + 7 : len;
  2115. while (i < len && html[i] != '>')
  2116. i++;
  2117. if (i < len)
  2118. i++;
  2119. continue;
  2120. }
  2121. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  2122. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  2123. unsigned int idx;
  2124. implicit_close(d, stack, &sp, tag);
  2125. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  2126. (unsigned char)tag);
  2127. if (idx)
  2128. parse_attrs(d, &d->nodes[idx],
  2129. html + attr_start,
  2130. attr_end - attr_start);
  2131. continue;
  2132. }
  2133. if (sp >= 63) {
  2134. d->truncated = 1;
  2135. continue;
  2136. }
  2137. implicit_close(d, stack, &sp, tag);
  2138. {
  2139. unsigned int idx = node_append(d, stack[sp],
  2140. BR_NODE_ELEM,
  2141. (unsigned char)tag);
  2142. if (!idx)
  2143. continue;
  2144. parse_attrs(d, &d->nodes[idx],
  2145. html + attr_start,
  2146. attr_end - attr_start);
  2147. if (!selfclose) {
  2148. sp++;
  2149. stack[sp] = (unsigned short)idx;
  2150. if (tag == BR_TAG_PRE)
  2151. pre++;
  2152. }
  2153. }
  2154. }
  2155. }
  2156. d->inline_css_len = css_len;
  2157. return 0;
  2158. }
  2159. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  2160. {
  2161. if (!d->nodes)
  2162. br_doc_init(d);
  2163. doc_reset(d);
  2164. d->plain = 1;
  2165. d->nodes[0].type = BR_NODE_ELEM;
  2166. d->nodes[0].tag = BR_TAG_UNKNOWN;
  2167. append_text(d, 0, text, len, 1);
  2168. return 0;
  2169. }
  2170. /* A standalone image response: a synthetic document with one <img>. The
  2171. * caller decodes the already-fetched body into image slot 0 and sets the
  2172. * returned node's img_index. */
  2173. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  2174. {
  2175. unsigned int idx;
  2176. if (!d->nodes)
  2177. br_doc_init(d);
  2178. doc_reset(d);
  2179. d->nodes[0].type = BR_NODE_ELEM;
  2180. d->nodes[0].tag = BR_TAG_UNKNOWN;
  2181. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  2182. if (!idx)
  2183. return 0;
  2184. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  2185. return idx;
  2186. }