browser_html.c 83 KB

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