browser_html.c 70 KB

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