browser_html.c 75 KB

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