browser_html.c 85 KB

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