browser_html.c 57 KB

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