browser_html.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791
  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].name_off = 0;
  686. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  687. d->nodes[i].img_index = -1;
  688. d->nodes[i].style_valid = 0;
  689. }
  690. }
  691. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  692. unsigned char type, unsigned char tag)
  693. {
  694. unsigned int idx;
  695. struct br_node *n;
  696. if (d->nnode >= BR_MAX_NODES) {
  697. d->truncated = 1;
  698. return 0;
  699. }
  700. idx = d->nnode++;
  701. n = &d->nodes[idx];
  702. n->type = type;
  703. n->tag = tag;
  704. n->parent = (unsigned short)parent;
  705. n->img_index = -1;
  706. if (d->nodes[parent].last)
  707. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  708. else
  709. d->nodes[parent].first = (unsigned short)idx;
  710. d->nodes[parent].last = (unsigned short)idx;
  711. return idx;
  712. }
  713. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  714. const char *s, unsigned int len, int pre)
  715. {
  716. char *dst;
  717. unsigned int n;
  718. unsigned int idx;
  719. if (len == 0)
  720. return 0;
  721. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  722. d->truncated = 1;
  723. return 0;
  724. }
  725. dst = d->text + d->text_used;
  726. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  727. if (n == 0) {
  728. d->text[d->text_used] = '\0';
  729. return 0;
  730. }
  731. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  732. if (!idx)
  733. return 0;
  734. d->nodes[idx].text_off = (unsigned short)d->text_used;
  735. d->nodes[idx].text_len = (unsigned short)n;
  736. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  737. d->text_used += n + 1;
  738. return idx;
  739. }
  740. /* ---- attribute parsing ---- */
  741. static int is_space(char c)
  742. {
  743. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  744. }
  745. static int str_eq(const char *a, const char *b);
  746. static unsigned int parse_uint(const char *s, unsigned int len)
  747. {
  748. unsigned int i = 0, v = 0;
  749. while (i < len && is_space(s[i]))
  750. i++;
  751. while (i < len && s[i] >= '0' && s[i] <= '9') {
  752. v = v * 10 + (unsigned int)(s[i] - '0');
  753. if (v > 4096)
  754. v = 4096;
  755. i++;
  756. }
  757. return v;
  758. }
  759. static void note_attr_flag(struct br_node *n, const char *name)
  760. {
  761. if (str_eq(name, "data-collapse-target"))
  762. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  763. else if (str_eq(name, "data-collapse-trigger"))
  764. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  765. else if (str_eq(name, "is-open"))
  766. n->attr_flags |= BR_ATTR_IS_OPEN;
  767. else if (str_eq(name, "disabled"))
  768. n->attr_flags |= BR_ATTR_DISABLED;
  769. else if (str_eq(name, "open"))
  770. n->attr_flags |= BR_ATTR_OPEN;
  771. else if (str_eq(name, "inert"))
  772. n->attr_flags |= BR_ATTR_INERT;
  773. }
  774. /* Parse attributes from `s` (between the tag name and '>'). */
  775. static void parse_attrs(struct br_doc *d, struct br_node *n,
  776. const char *s, unsigned int len)
  777. {
  778. unsigned int i = 0;
  779. while (i < len) {
  780. char name[32];
  781. unsigned int nl = 0, vstart, vlen;
  782. char quote = 0;
  783. while (i < len && is_space(s[i]))
  784. i++;
  785. if (i >= len || s[i] == '/')
  786. break;
  787. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  788. char c = s[i++];
  789. if (c >= 'A' && c <= 'Z')
  790. c = (char)(c - 'A' + 'a');
  791. if (nl + 1 < sizeof(name))
  792. name[nl++] = c;
  793. }
  794. name[nl] = '\0';
  795. while (i < len && is_space(s[i]))
  796. i++;
  797. if (i >= len || s[i] != '=') {
  798. if (str_eq(name, "hidden"))
  799. n->flags |= BR_NODE_HIDDEN;
  800. note_attr_flag(n, name);
  801. continue;
  802. }
  803. i++;
  804. while (i < len && is_space(s[i]))
  805. i++;
  806. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  807. quote = s[i++];
  808. vstart = i;
  809. while (i < len && s[i] != quote)
  810. i++;
  811. vlen = i - vstart;
  812. if (i < len)
  813. i++;
  814. } else {
  815. vstart = i;
  816. while (i < len && !is_space(s[i]) && s[i] != '>')
  817. i++;
  818. vlen = i - vstart;
  819. }
  820. if (!nl)
  821. continue;
  822. if (str_eq(name, "hidden")) {
  823. n->flags |= BR_NODE_HIDDEN;
  824. continue;
  825. }
  826. note_attr_flag(n, name);
  827. if (str_eq(name, "href"))
  828. n->href_off = store_attr(d, s + vstart, vlen, 480);
  829. else if (str_eq(name, "class"))
  830. n->class_off = store_attr(d, s + vstart, vlen, 120);
  831. else if (str_eq(name, "id"))
  832. n->id_off = store_attr(d, s + vstart, vlen, 64);
  833. else if (str_eq(name, "style"))
  834. n->style_off = store_attr(d, s + vstart, vlen, 240);
  835. else if (str_eq(name, "src"))
  836. n->src_off = store_attr(d, s + vstart, vlen, 400);
  837. else if (str_eq(name, "alt"))
  838. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  839. else if (str_eq(name, "color"))
  840. n->color_off = store_attr(d, s + vstart, vlen, 32);
  841. else if (str_eq(name, "rel"))
  842. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  843. else if (str_eq(name, "value"))
  844. n->src_off = store_attr(d, s + vstart, vlen, 220);
  845. else if (str_eq(name, "placeholder") &&
  846. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  847. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  848. else if (str_eq(name, "type") &&
  849. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  850. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  851. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  852. else if (str_eq(name, "width")) {
  853. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  854. if (vlen && s[vstart + vlen - 1] == '%')
  855. n->width_pct = 1;
  856. } else if (str_eq(name, "height")) {
  857. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  858. if (vlen && s[vstart + vlen - 1] == '%')
  859. n->height_pct = 1;
  860. }
  861. }
  862. }
  863. static int str_eq(const char *a, const char *b)
  864. {
  865. unsigned int i = 0;
  866. while (a[i] && b[i] && a[i] == b[i])
  867. i++;
  868. return a[i] == '\0' && b[i] == '\0';
  869. }
  870. /* ---- CSS ---- */
  871. enum {
  872. PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
  873. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  874. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  875. PROP_BORDER_W, PROP_BORDER_COLOR, PROP_LIST_NONE,
  876. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  877. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  878. PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  879. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT,
  880. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  881. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  882. PROP_COUNT
  883. };
  884. struct color_name {
  885. const char *name;
  886. unsigned short rgb;
  887. };
  888. static const struct color_name color_names[] = {
  889. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  890. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  891. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  892. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  893. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  894. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  895. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  896. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  897. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  898. { "darkgrey", 0x528A }, { 0, 0 }
  899. };
  900. static int hexval(char c)
  901. {
  902. if (c >= '0' && c <= '9')
  903. return c - '0';
  904. if (c >= 'a' && c <= 'f')
  905. return c - 'a' + 10;
  906. if (c >= 'A' && c <= 'F')
  907. return c - 'A' + 10;
  908. return -1;
  909. }
  910. static int named_color(const char *s, unsigned int len, unsigned short *out)
  911. {
  912. unsigned int i;
  913. for (i = 0; color_names[i].name; i++) {
  914. unsigned int j, n = s_len(color_names[i].name);
  915. if (n != len)
  916. continue;
  917. for (j = 0; j < n; j++) {
  918. char c = s[j];
  919. if (c >= 'A' && c <= 'Z')
  920. c = (char)(c - 'A' + 'a');
  921. if (c != color_names[i].name[j])
  922. break;
  923. }
  924. if (j == n) {
  925. *out = color_names[i].rgb;
  926. return 1;
  927. }
  928. }
  929. return 0;
  930. }
  931. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  932. * over white; the render target is a white page. */
  933. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  934. {
  935. unsigned int i = 0;
  936. while (i < len && is_space(s[i]))
  937. i++;
  938. if (i < len && s[i] == '#') {
  939. int d[6], nd = 0;
  940. i++;
  941. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  942. d[nd++] = hexval(s[i++]);
  943. if (nd == 3) {
  944. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  945. unsigned int b = (unsigned int)d[2];
  946. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  947. ((g * 17 >> 2) << 5) |
  948. (b * 17 >> 3));
  949. return 1;
  950. }
  951. if (nd >= 6) {
  952. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  953. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  954. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  955. *out = (unsigned short)(((r >> 3) << 11) |
  956. ((g >> 2) << 5) | (b >> 3));
  957. return 1;
  958. }
  959. return 0;
  960. }
  961. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  962. unsigned int v[3], k = 0, a = 255;
  963. while (i < len && s[i] != '(')
  964. i++;
  965. if (i >= len)
  966. return 0;
  967. i++;
  968. while (k < 3 && i < len) {
  969. unsigned int x = 0;
  970. int percent = 0;
  971. while (i < len && (is_space(s[i]) || s[i] == ','))
  972. i++;
  973. if (i >= len || s[i] < '0' || s[i] > '9')
  974. return 0;
  975. while (i < len && s[i] >= '0' && s[i] <= '9') {
  976. x = x * 10 + (unsigned int)(s[i] - '0');
  977. i++;
  978. }
  979. if (i < len && s[i] == '%') {
  980. percent = 1;
  981. i++;
  982. }
  983. if (percent)
  984. x = x * 255 / 100;
  985. if (x > 255)
  986. x = 255;
  987. v[k++] = x;
  988. }
  989. /* Optional alpha in both syntaxes:
  990. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  991. {
  992. unsigned int whole = 0, frac = 0, scale = 1;
  993. int percent = 0;
  994. while (i < len && (is_space(s[i]) || s[i] == ','))
  995. i++;
  996. if (i < len && s[i] == '/') {
  997. i++;
  998. while (i < len && is_space(s[i]))
  999. i++;
  1000. }
  1001. if (i < len && s[i] >= '0' && s[i] <= '9') {
  1002. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1003. whole = whole * 10 +
  1004. (unsigned int)(s[i] - '0');
  1005. i++;
  1006. }
  1007. if (i < len && s[i] == '.') {
  1008. i++;
  1009. while (i < len && s[i] >= '0' &&
  1010. s[i] <= '9') {
  1011. if (scale < 1000) {
  1012. frac = frac * 10 +
  1013. (unsigned int)(s[i] - '0');
  1014. scale *= 10;
  1015. }
  1016. i++;
  1017. }
  1018. }
  1019. while (i < len && is_space(s[i]))
  1020. i++;
  1021. if (i < len && s[i] == '%')
  1022. percent = 1;
  1023. if (percent)
  1024. a = whole > 100 ? 255 :
  1025. whole * 255 / 100;
  1026. else if (whole >= 1)
  1027. a = 255;
  1028. else
  1029. a = frac * 255 / scale;
  1030. }
  1031. }
  1032. if (k == 3) {
  1033. unsigned int r = v[0], g = v[1], b = v[2];
  1034. if (a < 255) {
  1035. r = (r * a + 255u * (255u - a)) / 255u;
  1036. g = (g * a + 255u * (255u - a)) / 255u;
  1037. b = (b * a + 255u * (255u - a)) / 255u;
  1038. }
  1039. *out = (unsigned short)(((r >> 3) << 11) |
  1040. ((g >> 2) << 5) | (b >> 3));
  1041. return 1;
  1042. }
  1043. return 0;
  1044. }
  1045. {
  1046. unsigned int j = i;
  1047. while (i < len && !is_space(s[i]))
  1048. i++;
  1049. return named_color(s + j, i - j, out);
  1050. }
  1051. }
  1052. /* Length values for margins/padding: px, ignore percentages/units. */
  1053. static unsigned int viewport_w;
  1054. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  1055. {
  1056. unsigned int i = *pos;
  1057. int sign = 1;
  1058. long v = 0; /* hundredths of a unit before conversion */
  1059. int digits = 0, frac = 0, unit = 0;
  1060. while (i < len && is_space(s[i]))
  1061. i++;
  1062. if (i < len && (s[i] == '-' || s[i] == '+')) {
  1063. if (s[i] == '-')
  1064. sign = -1;
  1065. i++;
  1066. }
  1067. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1068. v = v * 10 + (s[i] - '0');
  1069. digits = 1;
  1070. i++;
  1071. }
  1072. if (i < len && s[i] == '.') {
  1073. i++;
  1074. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1075. if (frac < 2) {
  1076. v = v * 10 + (s[i] - '0');
  1077. frac++;
  1078. }
  1079. i++;
  1080. }
  1081. }
  1082. while (frac < 2) {
  1083. v *= 10;
  1084. frac++;
  1085. }
  1086. while (frac > 2) {
  1087. v /= 10;
  1088. frac--;
  1089. }
  1090. if (i < len && s[i] == '%')
  1091. unit = 2;
  1092. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1093. s[i + 2] == 'm')
  1094. unit = 1;
  1095. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1096. unit = 3;
  1097. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  1098. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  1099. s[i] == 'a' || s[i] == 'd'))
  1100. i++;
  1101. *pos = i;
  1102. if (!digits)
  1103. return 0;
  1104. if (unit == 1)
  1105. v *= root_font_px;
  1106. else if (unit == 3)
  1107. v *= 16;
  1108. else if (unit == 2)
  1109. v = v * (long)viewport_w / 100;
  1110. v /= 100;
  1111. v *= sign;
  1112. if (v < -2048)
  1113. v = -2048;
  1114. if (v > 2048)
  1115. v = 2048;
  1116. return (int)v;
  1117. }
  1118. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  1119. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  1120. unsigned char *kind)
  1121. {
  1122. unsigned int i = 0;
  1123. unsigned long v = 0;
  1124. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  1125. *val = 0;
  1126. *kind = 0;
  1127. while (i < len && is_space(s[i]))
  1128. i++;
  1129. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  1130. return 0;
  1131. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  1132. return 0;
  1133. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1134. v = v * 10 + (unsigned int)(s[i] - '0');
  1135. digits = 1;
  1136. i++;
  1137. }
  1138. if (i < len && s[i] == '.') {
  1139. i++;
  1140. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1141. if (frac < 2) {
  1142. v = v * 10 + (unsigned int)(s[i] - '0');
  1143. frac++;
  1144. }
  1145. i++;
  1146. }
  1147. }
  1148. while (frac < 2) {
  1149. v *= 10;
  1150. frac++;
  1151. }
  1152. while (frac > 2) {
  1153. v /= 10;
  1154. frac--;
  1155. }
  1156. if (!digits)
  1157. return 0;
  1158. while (i < len && is_space(s[i]))
  1159. i++;
  1160. if (i < len && s[i] == '%')
  1161. pct = 1;
  1162. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  1163. s[i + 2] == 'm')
  1164. rem = 1;
  1165. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  1166. rem = 2;
  1167. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  1168. vw = 1;
  1169. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  1170. ; /* px: already the default scale */
  1171. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  1172. bad_unit = 1;
  1173. if (bad_unit)
  1174. return 0;
  1175. if (rem == 1)
  1176. v *= root_font_px;
  1177. else if (rem == 2)
  1178. v *= 16;
  1179. v /= 100;
  1180. if (vw)
  1181. v = v * viewport_w / 100;
  1182. if (pct) {
  1183. if (v > 100)
  1184. v = 100;
  1185. *kind = 2;
  1186. } else {
  1187. if (v > 2048)
  1188. v = 2048;
  1189. *kind = 1;
  1190. }
  1191. *val = (unsigned short)v;
  1192. return 1;
  1193. }
  1194. static int parse_font_px(const char *s, unsigned int len)
  1195. {
  1196. unsigned int i = 0;
  1197. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1198. int v1000, px;
  1199. int is_px = 1, is_rem = 0, is_pct = 0;
  1200. while (i < len && is_space(s[i]))
  1201. i++;
  1202. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  1203. return 24;
  1204. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  1205. return 32;
  1206. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  1207. return 14;
  1208. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1209. whole = whole * 10 + (s[i] - '0');
  1210. digits = 1;
  1211. i++;
  1212. }
  1213. if (i < len && s[i] == '.') {
  1214. i++;
  1215. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1216. if (fscale < 1000) {
  1217. frac = frac * 10 + (s[i] - '0');
  1218. fscale *= 10;
  1219. }
  1220. i++;
  1221. }
  1222. }
  1223. if (!digits)
  1224. return 0;
  1225. while (i < len && is_space(s[i]))
  1226. i++;
  1227. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1228. is_rem = 1;
  1229. is_px = 0;
  1230. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1231. is_rem = 1;
  1232. is_px = 0;
  1233. } else if (i < len && s[i] == '%') {
  1234. is_pct = 1;
  1235. is_px = 0;
  1236. }
  1237. v1000 = whole * 1000 + frac * 1000 / fscale;
  1238. if (is_rem)
  1239. px = v1000 * root_font_px / 1000;
  1240. else if (is_pct)
  1241. px = v1000 * root_font_px / 100000;
  1242. else if (is_px)
  1243. px = v1000 / 1000;
  1244. else
  1245. px = v1000 / 1000;
  1246. if (px < 8)
  1247. px = 8;
  1248. if (px > 72)
  1249. px = 72;
  1250. return px;
  1251. }
  1252. static int parse_line_height(const char *s, unsigned int len,
  1253. unsigned short *px, unsigned char *pct)
  1254. {
  1255. unsigned int i = 0;
  1256. int whole = 0, frac = 0, fscale = 1, digits = 0;
  1257. int v1000;
  1258. int is_px = 0, is_pct = 0;
  1259. *px = 0;
  1260. *pct = 0;
  1261. while (i < len && is_space(s[i]))
  1262. i++;
  1263. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  1264. return 0;
  1265. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1266. whole = whole * 10 + (s[i] - '0');
  1267. digits = 1;
  1268. i++;
  1269. }
  1270. if (i < len && s[i] == '.') {
  1271. i++;
  1272. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  1273. frac = frac * 10 + (s[i] - '0');
  1274. fscale *= 10;
  1275. i++;
  1276. }
  1277. }
  1278. if (!digits)
  1279. return 0;
  1280. while (i < len && is_space(s[i]))
  1281. i++;
  1282. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1283. is_px = 1;
  1284. else if (i < len && s[i] == '%')
  1285. is_pct = 1;
  1286. v1000 = whole * 1000 + frac * 1000 / fscale;
  1287. if (is_px) {
  1288. int v = v1000 / 1000;
  1289. if (v < 8)
  1290. v = 8;
  1291. if (v > 160)
  1292. v = 160;
  1293. *px = (unsigned short)v;
  1294. } else {
  1295. int v = v1000 / 10; /* unitless multiplier -> percent */
  1296. if (is_pct)
  1297. v = v1000 / 1000;
  1298. if (v < 50)
  1299. v = 50;
  1300. if (v > 400)
  1301. v = 400;
  1302. *pct = (unsigned char)v;
  1303. }
  1304. return 1;
  1305. }
  1306. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  1307. {
  1308. unsigned int i, j, n = s_len(needle);
  1309. for (i = 0; i + n <= len; i++) {
  1310. for (j = 0; j < n; j++) {
  1311. char a = s[i + j];
  1312. if (a >= 'A' && a <= 'Z')
  1313. a = (char)(a - 'A' + 'a');
  1314. if (a != needle[j])
  1315. break;
  1316. }
  1317. if (j == n)
  1318. return 1;
  1319. }
  1320. return 0;
  1321. }
  1322. /* Pull a colour out of a border/background shorthand. */
  1323. static int find_color(const char *s, unsigned int len, unsigned short *c)
  1324. {
  1325. unsigned int i;
  1326. for (i = 0; i < len; i++) {
  1327. if (s[i] == '#')
  1328. return parse_color(s + i, len - i, c);
  1329. if ((s[i] == 'r' || s[i] == 'R') &&
  1330. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  1331. return parse_color(s + i, len - i, c);
  1332. }
  1333. for (i = 0; i < len; ) {
  1334. unsigned int st, e;
  1335. while (i < len && (is_space(s[i]) || s[i] == ','))
  1336. i++;
  1337. st = i;
  1338. while (i < len && !is_space(s[i]) && s[i] != ',')
  1339. i++;
  1340. e = i;
  1341. if (e > st && parse_color(s + st, e - st, c))
  1342. return 1;
  1343. }
  1344. return 0;
  1345. }
  1346. static int parse_display(const char *s, unsigned int len)
  1347. {
  1348. unsigned int i;
  1349. while (len && is_space(s[0])) { s++; len--; }
  1350. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  1351. ;
  1352. if (i == 4 && ci_eq_n(s, "none", 4))
  1353. return BR_DISPLAY_NONE;
  1354. if (i == 5 && ci_eq_n(s, "block", 5))
  1355. return BR_DISPLAY_BLOCK;
  1356. if (i == 6 && ci_eq_n(s, "inline", 6))
  1357. return BR_DISPLAY_INLINE;
  1358. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  1359. return BR_DISPLAY_BLOCK;
  1360. if (i == 10 && ci_eq_n(s, "list-item", 10))
  1361. return BR_DISPLAY_BLOCK;
  1362. if (i == 4 && ci_eq_n(s, "flex", 4))
  1363. return BR_DISPLAY_FLEX;
  1364. if (i == 10 && ci_eq_n(s, "inline-flex", 10))
  1365. return BR_DISPLAY_FLEX;
  1366. if (i == 5 && ci_eq_n(s, "table", 5))
  1367. return BR_DISPLAY_BLOCK;
  1368. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  1369. return BR_DISPLAY_BLOCK;
  1370. /* Grid shells are common on news/wiki pages, but the two-column
  1371. * heuristic is not reliable for their nested area templates yet.
  1372. * Collapse them to block flow so the content stays readable. */
  1373. if (i == 4 && ci_eq_n(s, "grid", 4))
  1374. return BR_DISPLAY_BLOCK;
  1375. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  1376. return BR_DISPLAY_BLOCK;
  1377. return BR_DISPLAY_INLINE;
  1378. }
  1379. static void decl_add(struct br_decl *out, unsigned int *nout,
  1380. unsigned char prop, int val, int important)
  1381. {
  1382. if (*nout >= BR_DECLS)
  1383. return;
  1384. out[*nout].prop = prop;
  1385. out[*nout].val = (short)val;
  1386. out[*nout].important = (unsigned char)(important != 0);
  1387. (*nout)++;
  1388. }
  1389. static int parse_border_width(const char *s, unsigned int len)
  1390. {
  1391. unsigned int i = 0;
  1392. while (i < len) {
  1393. while (i < len && is_space(s[i]))
  1394. i++;
  1395. if (i >= len)
  1396. break;
  1397. if (s[i] == '#') {
  1398. while (i < len && !is_space(s[i]))
  1399. i++;
  1400. continue;
  1401. }
  1402. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  1403. while (i < len && s[i] != ')')
  1404. i++;
  1405. if (i < len)
  1406. i++;
  1407. continue;
  1408. }
  1409. if (s[i] >= '0' && s[i] <= '9') {
  1410. int v = 0;
  1411. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1412. v = v * 10 + (s[i] - '0');
  1413. i++;
  1414. }
  1415. if (v < 0)
  1416. v = 0;
  1417. if (v > 3)
  1418. v = 3;
  1419. return v;
  1420. }
  1421. while (i < len && !is_space(s[i]))
  1422. i++;
  1423. }
  1424. return -1;
  1425. }
  1426. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  1427. * the input is copied verbatim when it contains no var(). */
  1428. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  1429. unsigned int len, char *out,
  1430. unsigned int cap, int depth)
  1431. {
  1432. unsigned int i = 0, n = 0;
  1433. if (depth > 8)
  1434. return 0;
  1435. while (i < len && n + 1 < cap) {
  1436. unsigned int p, inner, close, comma, nlen, fstart;
  1437. char quote;
  1438. int parens;
  1439. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  1440. out[n++] = s[i++];
  1441. continue;
  1442. }
  1443. p = i + 4;
  1444. quote = 0;
  1445. parens = 1;
  1446. while (p < len && parens) {
  1447. char c = s[p];
  1448. if (quote) {
  1449. if (c == quote)
  1450. quote = 0;
  1451. else if (c == '\\' && p + 1 < len)
  1452. p++;
  1453. } else if (c == '"' || c == '\'') {
  1454. quote = c;
  1455. } else if (c == '(') {
  1456. parens++;
  1457. } else if (c == ')') {
  1458. parens--;
  1459. }
  1460. p++;
  1461. }
  1462. if (parens) {
  1463. out[n++] = s[i++];
  1464. continue;
  1465. }
  1466. close = p - 1;
  1467. inner = i + 4;
  1468. while (inner < close && is_space(s[inner]))
  1469. inner++;
  1470. comma = inner;
  1471. parens = 0;
  1472. quote = 0;
  1473. while (comma < close) {
  1474. char c = s[comma];
  1475. if (quote) {
  1476. if (c == quote)
  1477. quote = 0;
  1478. } else if (c == '"' || c == '\'') {
  1479. quote = c;
  1480. } else if (c == '(') {
  1481. parens++;
  1482. } else if (c == ')' && parens > 0) {
  1483. parens--;
  1484. } else if (c == ',' && parens == 0) {
  1485. break;
  1486. }
  1487. comma++;
  1488. }
  1489. nlen = comma - inner;
  1490. while (nlen && is_space(s[inner + nlen - 1]))
  1491. nlen--;
  1492. fstart = comma < close ? comma + 1 : close;
  1493. while (fstart < close && is_space(s[fstart]))
  1494. fstart++;
  1495. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  1496. unsigned int vi = css_var_find(d, s + inner, nlen);
  1497. if (vi < BR_MAX_VARS) {
  1498. const char *v = attr_ptr(d, d->var_val[vi]);
  1499. unsigned int vn = css_expand_vars(d, v, s_len(v),
  1500. out + n,
  1501. cap - n, depth + 1);
  1502. n += vn;
  1503. } else if (fstart < close) {
  1504. unsigned int vn = css_expand_vars(d, s + fstart,
  1505. close - fstart,
  1506. out + n, cap - n,
  1507. depth + 1);
  1508. n += vn;
  1509. }
  1510. } else if (fstart < close) {
  1511. unsigned int vn = css_expand_vars(d, s + fstart,
  1512. close - fstart,
  1513. out + n, cap - n,
  1514. depth + 1);
  1515. n += vn;
  1516. }
  1517. i = p;
  1518. }
  1519. out[n] = '\0';
  1520. return n;
  1521. }
  1522. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  1523. unsigned int i)
  1524. {
  1525. while (i < len && (is_space(s[i]) || s[i] == ','))
  1526. i++;
  1527. return i;
  1528. }
  1529. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  1530. * argument). Returns 1 on success. */
  1531. static int parse_grid_track(const char *s, unsigned int len,
  1532. unsigned int *pp, unsigned short *val,
  1533. unsigned char *kind)
  1534. {
  1535. unsigned int i = grid_skip_sep(s, len, *pp);
  1536. unsigned int whole = 0, frac = 0, scale = 1;
  1537. int any = 0;
  1538. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  1539. unsigned int j = i + 7;
  1540. while (j < len && s[j] != ',')
  1541. j++;
  1542. if (j >= len)
  1543. return 0;
  1544. i = j + 1;
  1545. while (i < len && is_space(s[i]))
  1546. i++;
  1547. }
  1548. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  1549. int depth = 1;
  1550. i += 7;
  1551. while (i < len && depth) {
  1552. if (s[i] == '(')
  1553. depth++;
  1554. else if (s[i] == ')')
  1555. depth--;
  1556. i++;
  1557. }
  1558. *pp = i;
  1559. return 0;
  1560. }
  1561. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1562. whole = whole * 10 + (unsigned int)(s[i] - '0');
  1563. i++;
  1564. any = 1;
  1565. }
  1566. if (i < len && s[i] == '.') {
  1567. i++;
  1568. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1569. if (scale < 1000) {
  1570. frac = frac * 10 + (unsigned int)(s[i] - '0');
  1571. scale *= 10;
  1572. }
  1573. i++;
  1574. any = 1;
  1575. }
  1576. }
  1577. if (!any) {
  1578. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  1579. *val = 1;
  1580. *kind = 3;
  1581. *pp = i + 4;
  1582. return 1;
  1583. }
  1584. return 0;
  1585. }
  1586. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  1587. *val = (unsigned short)(whole ? whole : 1);
  1588. *kind = 3;
  1589. i += 2;
  1590. } else if (i < len && s[i] == '%') {
  1591. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1592. *kind = 2;
  1593. i++;
  1594. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  1595. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1596. *kind = 1;
  1597. i += 3;
  1598. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  1599. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  1600. *kind = 1;
  1601. i += 2;
  1602. } else {
  1603. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  1604. *kind = 1;
  1605. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  1606. i += 2;
  1607. }
  1608. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  1609. i++;
  1610. *pp = i;
  1611. return 1;
  1612. }
  1613. static int parse_grid_cols(const char *s, unsigned int len,
  1614. unsigned short val[2], unsigned char kind[2])
  1615. {
  1616. unsigned int pos = 0;
  1617. int n = 0;
  1618. while (n < 2) {
  1619. unsigned int before = pos;
  1620. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  1621. break;
  1622. n++;
  1623. if (pos <= before)
  1624. break;
  1625. while (pos < len && is_space(s[pos]))
  1626. pos++;
  1627. if (pos >= len)
  1628. break;
  1629. }
  1630. return n;
  1631. }
  1632. static int parse_grid_slot(const char *s, unsigned int len)
  1633. {
  1634. if (contains_ci_len(s, len, "footer") ||
  1635. contains_ci_len(s, len, "header") ||
  1636. contains_ci_len(s, len, "sitenotice") ||
  1637. contains_ci_len(s, len, "navbar"))
  1638. return 1;
  1639. if (contains_ci_len(s, len, "columnstart") ||
  1640. contains_ci_len(s, len, "sidebar") ||
  1641. contains_ci_len(s, len, "left") ||
  1642. contains_ci_len(s, len, "start"))
  1643. return 2;
  1644. if (contains_ci_len(s, len, "pagecontent") ||
  1645. contains_ci_len(s, len, "content") ||
  1646. contains_ci_len(s, len, "main") ||
  1647. contains_ci_len(s, len, "right") ||
  1648. contains_ci_len(s, len, "end"))
  1649. return 3;
  1650. return 0;
  1651. }
  1652. static int css_has_var(const char *s, unsigned int len)
  1653. {
  1654. unsigned int i;
  1655. for (i = 0; i + 4 <= len; i++)
  1656. if ((s[i] == 'v' || s[i] == 'V') &&
  1657. ci_eq_n(s + i, "var(", 4))
  1658. return 1;
  1659. return 0;
  1660. }
  1661. static unsigned int css_skip_comment(const char *s, unsigned int len,
  1662. unsigned int i)
  1663. {
  1664. i += 2;
  1665. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  1666. i++;
  1667. if (i + 1 < len)
  1668. i += 2;
  1669. else
  1670. i = len;
  1671. return i;
  1672. }
  1673. static unsigned int css_skip_string(const char *s, unsigned int len,
  1674. unsigned int i)
  1675. {
  1676. char q = s[i++];
  1677. while (i < len && s[i] != q) {
  1678. if (s[i] == '\\' && i + 1 < len)
  1679. i++;
  1680. i++;
  1681. }
  1682. if (i < len)
  1683. i++;
  1684. return i;
  1685. }
  1686. /* Find the '}' that closes a declaration block. i points just past the
  1687. * opening '{'. Comments and quoted strings are skipped: a brace inside
  1688. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  1689. * selectors such as "Highlight-Stories {" without a closing brace). */
  1690. static unsigned int css_block_end(const char *s, unsigned int len,
  1691. unsigned int i)
  1692. {
  1693. int depth = 0;
  1694. while (i < len) {
  1695. char c = s[i];
  1696. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  1697. i = css_skip_comment(s, len, i);
  1698. continue;
  1699. }
  1700. if (c == '"' || c == '\'') {
  1701. i = css_skip_string(s, len, i);
  1702. continue;
  1703. }
  1704. if (c == '{') {
  1705. depth++;
  1706. } else if (c == '}') {
  1707. if (depth == 0)
  1708. return i;
  1709. depth--;
  1710. }
  1711. i++;
  1712. }
  1713. return len;
  1714. }
  1715. /* Skip a block whose opening '{' has already been consumed. */
  1716. static unsigned int css_skip_nested(const char *s, unsigned int len,
  1717. unsigned int i)
  1718. {
  1719. int depth = 1;
  1720. while (i < len && depth) {
  1721. char c = s[i];
  1722. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  1723. i = css_skip_comment(s, len, i);
  1724. continue;
  1725. }
  1726. if (c == '"' || c == '\'') {
  1727. i = css_skip_string(s, len, i);
  1728. continue;
  1729. }
  1730. if (c == '{')
  1731. depth++;
  1732. else if (c == '}')
  1733. depth--;
  1734. i++;
  1735. }
  1736. return i;
  1737. }
  1738. static void parse_decls(struct br_doc *d, const char *s,
  1739. unsigned int len, struct br_decl *out,
  1740. unsigned int *nout)
  1741. {
  1742. static char expanded[4096];
  1743. if (css_has_var(s, len)) {
  1744. unsigned int elen = css_expand_vars(d, s, len, expanded,
  1745. sizeof(expanded), 0);
  1746. if (elen) {
  1747. s = expanded;
  1748. len = elen;
  1749. }
  1750. }
  1751. unsigned int i = 0;
  1752. *nout = 0;
  1753. while (i < len && *nout < BR_DECLS) {
  1754. unsigned int pstart, pend, vstart, vend, k;
  1755. char prop[24];
  1756. int important = 0;
  1757. while (i < len && (is_space(s[i]) || s[i] == ';'))
  1758. i++;
  1759. if (i >= len)
  1760. break;
  1761. /* A nested at-rule (@media/@supports) inside the declaration
  1762. * block carries its own selector context; skip it rather than
  1763. * reading its prelude as a declaration. */
  1764. if (s[i] == '@') {
  1765. while (i < len && s[i] != '{' && s[i] != ';')
  1766. i++;
  1767. if (i < len && s[i] == '{')
  1768. i = css_skip_nested(s, len, i + 1);
  1769. else if (i < len)
  1770. i++;
  1771. continue;
  1772. }
  1773. pstart = i;
  1774. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  1775. i++;
  1776. if (i < len && s[i] == '{') {
  1777. /* Nested style rule: skip its block. */
  1778. i = css_skip_nested(s, len, i + 1);
  1779. continue;
  1780. }
  1781. pend = i;
  1782. if (i >= len || s[i] != ':')
  1783. continue;
  1784. i++;
  1785. vstart = i;
  1786. while (i < len && s[i] != ';')
  1787. i++;
  1788. vend = i;
  1789. while (pstart < pend && is_space(s[pstart]))
  1790. pstart++;
  1791. while (pend > pstart && is_space(s[pend - 1]))
  1792. pend--;
  1793. while (vstart < vend && is_space(s[vstart]))
  1794. vstart++;
  1795. while (vend > vstart && is_space(s[vend - 1]))
  1796. vend--;
  1797. if (vend - vstart >= 10 &&
  1798. ci_eq_n(s + vend - 10, "!important", 10)) {
  1799. important = 1;
  1800. vend -= 10;
  1801. while (vend > vstart && is_space(s[vend - 1]))
  1802. vend--;
  1803. }
  1804. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  1805. char c = s[pstart + k];
  1806. if (c >= 'A' && c <= 'Z')
  1807. c = (char)(c - 'A' + 'a');
  1808. prop[k] = c;
  1809. }
  1810. prop[k] = '\0';
  1811. if (str_eq(prop, "color")) {
  1812. unsigned short c;
  1813. if (parse_color(s + vstart, vend - vstart, &c))
  1814. decl_add(out, nout, PROP_COLOR, c, important);
  1815. } else if (str_eq(prop, "background-color") ||
  1816. str_eq(prop, "background")) {
  1817. unsigned short c;
  1818. if (parse_color(s + vstart, vend - vstart, &c))
  1819. decl_add(out, nout, PROP_BG, c, important);
  1820. else if (str_eq(prop, "background") &&
  1821. find_color(s + vstart, vend - vstart, &c))
  1822. decl_add(out, nout, PROP_BG, c, important);
  1823. } else if (str_eq(prop, "font-size")) {
  1824. int px = parse_font_px(s + vstart, vend - vstart);
  1825. if (px > 0)
  1826. decl_add(out, nout, PROP_SCALE,
  1827. px >= 24 ? 2 : 1, important);
  1828. } else if (str_eq(prop, "font-weight")) {
  1829. int bold = 0;
  1830. unsigned int j;
  1831. if (vend - vstart >= 4 &&
  1832. ci_eq_n(s + vstart, "bold", 4))
  1833. bold = 1;
  1834. else {
  1835. int w = 0;
  1836. for (j = vstart; j < vend; j++)
  1837. if (s[j] >= '0' && s[j] <= '9')
  1838. w = w * 10 + (s[j] - '0');
  1839. if (w >= 500)
  1840. bold = 1;
  1841. }
  1842. decl_add(out, nout, PROP_BOLD, bold, important);
  1843. } else if (str_eq(prop, "text-align")) {
  1844. int a = BR_ALIGN_LEFT;
  1845. if (vend - vstart >= 6 &&
  1846. ci_eq_n(s + vstart, "center", 6))
  1847. a = BR_ALIGN_CENTER;
  1848. else if (vend - vstart >= 5 &&
  1849. ci_eq_n(s + vstart, "right", 5))
  1850. a = BR_ALIGN_RIGHT;
  1851. decl_add(out, nout, PROP_ALIGN, a, important);
  1852. } else if (str_eq(prop, "text-decoration")) {
  1853. int u = 0;
  1854. if (vend - vstart >= 9 &&
  1855. ci_eq_n(s + vstart, "underline", 9))
  1856. u = 1;
  1857. decl_add(out, nout, PROP_UNDERLINE, u, important);
  1858. } else if (str_eq(prop, "text-transform")) {
  1859. int u = (vend - vstart >= 9 &&
  1860. ci_eq_n(s + vstart, "uppercase", 9));
  1861. decl_add(out, nout, PROP_UPPER, u, important);
  1862. } else if (str_eq(prop, "display")) {
  1863. decl_add(out, nout, PROP_DISPLAY,
  1864. parse_display(s + vstart, vend - vstart),
  1865. important);
  1866. } else if (str_eq(prop, "visibility")) {
  1867. int h = contains_ci_len(s + vstart, vend - vstart,
  1868. "hidden") ||
  1869. contains_ci_len(s + vstart, vend - vstart,
  1870. "collapse");
  1871. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  1872. } else if (str_eq(prop, "opacity")) {
  1873. unsigned int q;
  1874. int h = 1, any = 0;
  1875. for (q = vstart; q < vend; q++) {
  1876. if (s[q] >= '1' && s[q] <= '9') {
  1877. h = 0;
  1878. break;
  1879. }
  1880. if (s[q] >= '0' && s[q] <= '9')
  1881. any = 1;
  1882. }
  1883. if (!any || contains_ci_len(s + vstart, vend - vstart,
  1884. "inherit"))
  1885. h = 0;
  1886. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  1887. } else if (str_eq(prop, "overflow") ||
  1888. str_eq(prop, "overflow-x") ||
  1889. str_eq(prop, "overflow-y")) {
  1890. int h = contains_ci_len(s + vstart, vend - vstart,
  1891. "hidden") ||
  1892. contains_ci_len(s + vstart, vend - vstart,
  1893. "clip");
  1894. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  1895. } else if (str_eq(prop, "float")) {
  1896. int f = 0;
  1897. if (vend - vstart >= 4 &&
  1898. ci_eq_n(s + vstart, "left", 4))
  1899. f = 1;
  1900. else if (vend - vstart >= 5 &&
  1901. ci_eq_n(s + vstart, "right", 5))
  1902. f = 2;
  1903. decl_add(out, nout, PROP_FLOAT, f, important);
  1904. } else if (str_eq(prop, "flex-grow") ||
  1905. str_eq(prop, "flex")) {
  1906. unsigned int q = vstart;
  1907. int g = 0;
  1908. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  1909. q++;
  1910. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  1911. g = g * 10 + (s[q] - '0');
  1912. q++;
  1913. }
  1914. if (g > 100)
  1915. g = 100;
  1916. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  1917. } else if (str_eq(prop, "flex-direction")) {
  1918. decl_add(out, nout, PROP_FLEX_COL,
  1919. contains_ci_len(s + vstart, vend - vstart,
  1920. "column"), important);
  1921. } else if (str_eq(prop, "flex-wrap")) {
  1922. int wrap = 0;
  1923. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  1924. wrap = 2;
  1925. else if (contains_ci_len(s + vstart, vend - vstart,
  1926. "wrap"))
  1927. wrap = 1;
  1928. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  1929. } else if (str_eq(prop, "position")) {
  1930. int p = 0;
  1931. if (contains_ci_len(s + vstart, vend - vstart,
  1932. "absolute"))
  1933. p = 1;
  1934. else if (contains_ci_len(s + vstart, vend - vstart,
  1935. "fixed") ||
  1936. contains_ci_len(s + vstart, vend - vstart,
  1937. "sticky"))
  1938. p = 2;
  1939. decl_add(out, nout, PROP_POS, p, important);
  1940. } else if (str_eq(prop, "left") || str_eq(prop, "top")) {
  1941. int is_left = prop[0] == 'l';
  1942. unsigned int pos = 0;
  1943. if (!(vend - vstart >= 4 &&
  1944. ci_eq_n(s + vstart, "auto", 4))) {
  1945. int v = parse_len(s + vstart, vend - vstart, &pos);
  1946. if (v > 32000)
  1947. v = 32000;
  1948. else if (v < -32000)
  1949. v = -32000;
  1950. decl_add(out, nout,
  1951. is_left ? PROP_LEFT : PROP_TOP, v, important);
  1952. decl_add(out, nout,
  1953. is_left ? PROP_LEFT_SET : PROP_TOP_SET, 1,
  1954. important);
  1955. }
  1956. } else if (str_eq(prop, "grid-template-columns") ||
  1957. str_eq(prop, "grid-template")) {
  1958. const char *v = s + vstart;
  1959. unsigned int vl = vend - vstart;
  1960. unsigned short val[2];
  1961. unsigned char kind[2];
  1962. int n, q;
  1963. if (str_eq(prop, "grid-template")) {
  1964. for (q = 0; q < (int)vl; q++)
  1965. if (v[q] == '/') {
  1966. q++;
  1967. while (q < (int)vl && is_space(v[q]))
  1968. q++;
  1969. v += q;
  1970. vl -= (unsigned int)q;
  1971. break;
  1972. }
  1973. }
  1974. n = parse_grid_cols(v, vl, val, kind);
  1975. if (n > 0) {
  1976. decl_add(out, nout, PROP_WIDTH, val[0], important);
  1977. decl_add(out, nout, PROP_WIDTH_KIND, kind[0], important);
  1978. decl_add(out, nout, PROP_GRID_NCOLS, n, important);
  1979. }
  1980. if (n > 1) {
  1981. decl_add(out, nout, PROP_MAXW, val[1], important);
  1982. decl_add(out, nout, PROP_MAXW_KIND, kind[1], important);
  1983. }
  1984. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  1985. unsigned int gp = 0;
  1986. unsigned short gv = 0;
  1987. unsigned char gk = 1;
  1988. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  1989. if (gv > 120)
  1990. gv = 120;
  1991. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  1992. }
  1993. } else if (str_eq(prop, "grid-area")) {
  1994. int slot = parse_grid_slot(s + vstart, vend - vstart);
  1995. if (slot)
  1996. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  1997. } else if (str_eq(prop, "width") ||
  1998. str_eq(prop, "max-width") ||
  1999. str_eq(prop, "height")) {
  2000. unsigned short v;
  2001. unsigned char kind;
  2002. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  2003. if (str_eq(prop, "width")) {
  2004. decl_add(out, nout, PROP_WIDTH, v, important);
  2005. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  2006. important);
  2007. } else if (str_eq(prop, "max-width")) {
  2008. decl_add(out, nout, PROP_MAXW, v, important);
  2009. decl_add(out, nout, PROP_MAXW_KIND, kind,
  2010. important);
  2011. } else {
  2012. decl_add(out, nout, PROP_HEIGHT, v, important);
  2013. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  2014. important);
  2015. }
  2016. }
  2017. } else if (str_eq(prop, "line-height")) {
  2018. unsigned short px;
  2019. unsigned char pct;
  2020. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  2021. if (px) {
  2022. decl_add(out, nout, PROP_LH_PX, px, important);
  2023. decl_add(out, nout, PROP_LH_PCT, 0, important);
  2024. } else {
  2025. decl_add(out, nout, PROP_LH_PX, 0, important);
  2026. decl_add(out, nout, PROP_LH_PCT, pct, important);
  2027. }
  2028. }
  2029. } else if (str_eq(prop, "list-style") ||
  2030. str_eq(prop, "list-style-type")) {
  2031. int none = (vend - vstart >= 4 &&
  2032. contains_ci_len(s + vstart, vend - vstart,
  2033. "none"));
  2034. decl_add(out, nout, PROP_LIST_NONE, none, important);
  2035. } else if (str_eq(prop, "border") ||
  2036. str_eq(prop, "border-top") ||
  2037. str_eq(prop, "border-right") ||
  2038. str_eq(prop, "border-bottom") ||
  2039. str_eq(prop, "border-left") ||
  2040. str_eq(prop, "border-color") ||
  2041. str_eq(prop, "border-width")) {
  2042. unsigned short c;
  2043. int bw;
  2044. int have_color;
  2045. if (vend - vstart >= 4 &&
  2046. ci_eq_n(s + vstart, "none", 4)) {
  2047. decl_add(out, nout, PROP_BORDER_W, 0, important);
  2048. } else {
  2049. bw = parse_border_width(s + vstart,
  2050. vend - vstart);
  2051. have_color = find_color(s + vstart,
  2052. vend - vstart, &c);
  2053. if (have_color) {
  2054. decl_add(out, nout, PROP_BORDER_COLOR, c,
  2055. important);
  2056. if (bw < 0)
  2057. bw = 1;
  2058. }
  2059. if (bw >= 0) {
  2060. if (bw > 3)
  2061. bw = 3;
  2062. decl_add(out, nout, PROP_BORDER_W, bw,
  2063. important);
  2064. }
  2065. }
  2066. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  2067. int vals[4], nv = 0;
  2068. unsigned int pos = 0;
  2069. int is_pad = prop[0] == 'p';
  2070. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  2071. while (pos < vend - vstart && nv < 4) {
  2072. vals[nv++] = parse_len(s + vstart,
  2073. vend - vstart, &pos);
  2074. while (pos < vend - vstart &&
  2075. is_space(s[vstart + pos]))
  2076. pos++;
  2077. }
  2078. if (nv == 1) {
  2079. vals[1] = vals[2] = vals[3] = vals[0];
  2080. } else if (nv == 2) {
  2081. vals[2] = vals[0];
  2082. vals[3] = vals[1];
  2083. } else if (nv == 3) {
  2084. vals[3] = vals[1];
  2085. }
  2086. if (nv > 0) {
  2087. decl_add(out, nout, base, vals[0], important);
  2088. decl_add(out, nout, (unsigned char)(base + 1),
  2089. vals[1], important);
  2090. decl_add(out, nout, (unsigned char)(base + 2),
  2091. vals[2], important);
  2092. decl_add(out, nout, (unsigned char)(base + 3),
  2093. vals[3], important);
  2094. }
  2095. } else if (str_eq(prop, "margin-top") ||
  2096. str_eq(prop, "margin-right") ||
  2097. str_eq(prop, "margin-bottom") ||
  2098. str_eq(prop, "margin-left") ||
  2099. str_eq(prop, "padding-top") ||
  2100. str_eq(prop, "padding-right") ||
  2101. str_eq(prop, "padding-bottom") ||
  2102. str_eq(prop, "padding-left")) {
  2103. unsigned int pos = 0;
  2104. int v = parse_len(s + vstart, vend - vstart, &pos);
  2105. unsigned char p = PROP_MT;
  2106. const char *side;
  2107. if (prop[0] == 'p')
  2108. p = PROP_PT;
  2109. side = prop + (prop[0] == 'p' ? 8 : 7);
  2110. if (str_eq(side, "right"))
  2111. p = (unsigned char)(p + 1);
  2112. else if (str_eq(side, "bottom"))
  2113. p = (unsigned char)(p + 2);
  2114. else if (str_eq(side, "left"))
  2115. p = (unsigned char)(p + 3);
  2116. decl_add(out, nout, p, v, important);
  2117. }
  2118. }
  2119. }
  2120. /* Skip @media/@font-face/@keyframes/@import without descending into their
  2121. * nested blocks. This is the mobile/base rules path: media enhancements are
  2122. * deliberately ignored for now. */
  2123. static unsigned int skip_at_rule(const char *s, unsigned int len,
  2124. unsigned int i)
  2125. {
  2126. unsigned int j = i;
  2127. while (j < len && s[j] != '{' && s[j] != ';')
  2128. j++;
  2129. if (j >= len)
  2130. return len;
  2131. if (s[j] == ';')
  2132. return j + 1;
  2133. j++;
  2134. return css_skip_nested(s, len, j);
  2135. }
  2136. static unsigned int viewport_w = 1024;
  2137. static unsigned short theme_text = 0x0000;
  2138. static unsigned short theme_bg = 0xFFFF;
  2139. static unsigned short theme_panel = 0xEF5D;
  2140. static unsigned short theme_link = 0x001B;
  2141. void br_set_theme(unsigned short text, unsigned short bg,
  2142. unsigned short panel, unsigned short link)
  2143. {
  2144. theme_text = text;
  2145. theme_bg = bg;
  2146. theme_panel = panel;
  2147. theme_link = link;
  2148. }
  2149. void br_set_viewport(unsigned int width)
  2150. {
  2151. viewport_w = width ? width : 1024;
  2152. }
  2153. static unsigned int media_len_px(const char *s, unsigned int len)
  2154. {
  2155. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  2156. int em = 0;
  2157. while (i < len && is_space(s[i]))
  2158. i++;
  2159. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2160. whole = whole * 10 + (unsigned int)(s[i] - '0');
  2161. i++;
  2162. }
  2163. if (i < len && s[i] == '.') {
  2164. i++;
  2165. /* Consume every fractional digit even when there are more
  2166. * than the three we keep: leaving the tail behind made the
  2167. * unit check miss "em" (74.9375em became 74px). */
  2168. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2169. if (scale < 1000) {
  2170. frac = frac * 10 + (unsigned int)(s[i] - '0');
  2171. scale *= 10;
  2172. }
  2173. i++;
  2174. }
  2175. }
  2176. while (i < len && is_space(s[i]))
  2177. i++;
  2178. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2179. em = 1;
  2180. i += 2;
  2181. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2182. em = 1;
  2183. i += 3;
  2184. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  2185. i += 2;
  2186. }
  2187. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2188. if (em)
  2189. return v * 16 / 100;
  2190. return v / 100;
  2191. }
  2192. /* One parenthesised media feature: min-width/max-width and the modern
  2193. * (width <= 600px) form. Unknown features are treated as true. */
  2194. static int media_feature_matches(const char *s, unsigned int len)
  2195. {
  2196. unsigned int i;
  2197. while (len && is_space(*s)) { s++; len--; }
  2198. while (len && is_space(s[len - 1])) len--;
  2199. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  2200. /* The browser canvas is dark, but sites express their light
  2201. * palette in the base rules; the dark overrides are usually
  2202. * invisible against our default surfaces. Pick the light set. */
  2203. return !contains_ci_len(s, len, "dark");
  2204. }
  2205. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  2206. for (i = 9; i < len && s[i] != ':'; i++)
  2207. ;
  2208. if (i < len)
  2209. return viewport_w >= media_len_px(s + i + 1,
  2210. len - i - 1);
  2211. return 1;
  2212. }
  2213. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  2214. for (i = 9; i < len && s[i] != ':'; i++)
  2215. ;
  2216. if (i < len)
  2217. return viewport_w <= media_len_px(s + i + 1,
  2218. len - i - 1);
  2219. return 1;
  2220. }
  2221. for (i = 0; i < len; i++) {
  2222. if (s[i] == '<' || s[i] == '>') {
  2223. int max = s[i] == '<';
  2224. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  2225. if (i + 1 < len && s[i + 1] == '=')
  2226. v = media_len_px(s + i + 2, len - i - 2);
  2227. return max ? viewport_w <= v : viewport_w >= v;
  2228. }
  2229. }
  2230. return 1;
  2231. }
  2232. static int media_query_matches(const char *s, unsigned int len)
  2233. {
  2234. unsigned int i;
  2235. if (contains_ci_len(s, len, "print") &&
  2236. !contains_ci_len(s, len, "screen"))
  2237. return 0;
  2238. if (contains_ci_len(s, len, "not "))
  2239. return 0;
  2240. for (i = 0; i < len; i++) {
  2241. if (s[i] == '(') {
  2242. unsigned int st = ++i;
  2243. while (i < len && s[i] != ')')
  2244. i++;
  2245. if (!media_feature_matches(s + st, i - st))
  2246. return 0;
  2247. }
  2248. }
  2249. return 1;
  2250. }
  2251. static int media_list_matches(const char *s, unsigned int len)
  2252. {
  2253. unsigned int i = 0;
  2254. while (i < len) {
  2255. unsigned int st = i;
  2256. while (i < len && s[i] != ',')
  2257. i++;
  2258. if (media_query_matches(s + st, i - st))
  2259. return 1;
  2260. i++;
  2261. }
  2262. return 0;
  2263. }
  2264. static unsigned int rule_hash(const char *s, unsigned int len)
  2265. {
  2266. unsigned int h = 2166136261u, i;
  2267. for (i = 0; i < len; i++) {
  2268. char c = s[i];
  2269. if (c >= 'A' && c <= 'Z')
  2270. c = (char)(c - 'A' + 'a');
  2271. h ^= (unsigned char)c;
  2272. h *= 16777619u;
  2273. }
  2274. return h % BR_RULE_HASH;
  2275. }
  2276. /* Index a parsed rule by the most selective component of its rightmost
  2277. * compound selector. br_style_for() then only visits rules that can
  2278. * possibly match a node instead of scanning every rule. */
  2279. static void rule_index_add(struct br_doc *d, unsigned int idx,
  2280. const char *key, unsigned int len)
  2281. {
  2282. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  2283. unsigned short *head;
  2284. for (i = 0; i < len; i++) {
  2285. if (key[i] == '.' || key[i] == '#') {
  2286. unsigned int st = i + 1, n = 0;
  2287. while (st + n < len && is_ident(key[st + n]))
  2288. n++;
  2289. if (key[i] == '#') {
  2290. ipos = st;
  2291. ilen = n;
  2292. } else if (n) {
  2293. cpos = st;
  2294. clen = n;
  2295. }
  2296. i = st + n - 1;
  2297. }
  2298. }
  2299. if (ilen)
  2300. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  2301. else if (clen)
  2302. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  2303. else if (len == 5 && ci_eq_n(key, ":root", 5))
  2304. head = &d->rule_tag[BR_TAG_HTML];
  2305. else if (len && is_ident(key[0])) {
  2306. char name[16];
  2307. unsigned int n = 0, tag;
  2308. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  2309. name[n] = key[n];
  2310. n++;
  2311. }
  2312. name[n] = '\0';
  2313. tag = br_tag_from_name(name);
  2314. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  2315. head = &d->rule_tag[tag];
  2316. else
  2317. head = &d->rule_any;
  2318. } else {
  2319. head = &d->rule_any;
  2320. }
  2321. d->rule_next[idx] = *head;
  2322. *head = (unsigned short)idx;
  2323. }
  2324. static unsigned int selector_specificity(const char *s, unsigned int len);
  2325. static int css_collect_only;
  2326. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  2327. {
  2328. unsigned int i = 0;
  2329. while (i < len && d->nrules < d->rule_cap) {
  2330. unsigned int sstart, send, bstart, bend, j;
  2331. struct br_decl decls[BR_DECLS];
  2332. unsigned int nd;
  2333. while (i < len && is_space(s[i]))
  2334. i++;
  2335. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  2336. i += 2;
  2337. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  2338. i++;
  2339. i += 2;
  2340. continue;
  2341. }
  2342. if (i >= len || s[i] == '}')
  2343. break;
  2344. if (s[i] == '@') {
  2345. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  2346. unsigned int q = i + 6;
  2347. while (q < len && s[q] != '{')
  2348. q++;
  2349. if (q < len &&
  2350. media_list_matches(s + i + 6,
  2351. q - (i + 6))) {
  2352. unsigned int b = q + 1;
  2353. unsigned int e = css_skip_nested(s, len, b);
  2354. if (e > b)
  2355. parse_css(d, s + b,
  2356. (e - 1) - b);
  2357. i = e;
  2358. } else {
  2359. i = skip_at_rule(s, len, i);
  2360. }
  2361. continue;
  2362. }
  2363. i = skip_at_rule(s, len, i);
  2364. continue;
  2365. }
  2366. sstart = i;
  2367. while (i < len && s[i] != '{')
  2368. i++;
  2369. if (i >= len)
  2370. break;
  2371. send = i;
  2372. i++;
  2373. bstart = i;
  2374. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  2375. * to the matching close brace; stopping at the first '}' left
  2376. * the parser out of sync and dropped the rest of the sheet
  2377. * (nd-aktuell lost its desktop rules that way). */
  2378. bend = css_block_end(s, len, i);
  2379. i = bend < len ? bend + 1 : len;
  2380. while (sstart < send && is_space(s[sstart]))
  2381. sstart++;
  2382. while (send > sstart && is_space(s[send - 1]))
  2383. send--;
  2384. if (sstart == send)
  2385. continue;
  2386. if (!css_dark_selector(s + sstart, send - sstart))
  2387. css_collect_block(d, s + bstart, bend - bstart);
  2388. if (css_collect_only)
  2389. continue;
  2390. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  2391. if (!nd)
  2392. continue;
  2393. /* Split a comma-separated selector list into separate rules. */
  2394. j = sstart;
  2395. while (j <= send && d->nrules < d->rule_cap) {
  2396. unsigned int q = j, e, k;
  2397. char sel[160];
  2398. unsigned int off;
  2399. while (q < send && s[q] != ',')
  2400. q++;
  2401. e = q;
  2402. while (j < e && is_space(s[j]))
  2403. j++;
  2404. while (e > j && is_space(s[e - 1]))
  2405. e--;
  2406. if (e > j) {
  2407. /* Ignore single selectors with sibling combinators,
  2408. * pseudo-classes or attribute tests: they need
  2409. * state/attributes we do not model. The other
  2410. * selectors in the list still apply. */
  2411. for (k = j; k < e; ) {
  2412. if (s[k] == '+' || s[k] == '~')
  2413. break;
  2414. if (s[k] == ':') {
  2415. if (k + 5 <= e &&
  2416. ci_eq_n(s + k, ":root", 5)) {
  2417. k += 5;
  2418. continue;
  2419. }
  2420. if (k + 5 <= e &&
  2421. ci_eq_n(s + k, ":not(", 5)) {
  2422. unsigned int n = k + 5;
  2423. int nd = 1;
  2424. while (n < e && nd) {
  2425. if (s[n] == '(')
  2426. nd++;
  2427. else if (s[n] == ')')
  2428. nd--;
  2429. n++;
  2430. }
  2431. if (nd)
  2432. break;
  2433. k = n;
  2434. continue;
  2435. }
  2436. break;
  2437. }
  2438. k++;
  2439. }
  2440. if (k < e) {
  2441. j = q + 1;
  2442. if (q >= send)
  2443. break;
  2444. continue;
  2445. }
  2446. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  2447. sel[k] = s[j + k];
  2448. sel[k] = '\0';
  2449. off = store_attr(d, sel, k, 159);
  2450. if (off) {
  2451. struct br_rule *r = &d->rules[d->nrules++];
  2452. unsigned int ks = 0, q;
  2453. for (q = 0; q < k; q++)
  2454. if (sel[q] == ' ' || sel[q] == '>')
  2455. ks = q + 1;
  2456. r->sel_off = off;
  2457. r->key_off = store_attr(d, sel + ks,
  2458. k - ks, 159);
  2459. r->spec = (unsigned short)selector_specificity(sel, k);
  2460. r->single = 1;
  2461. {
  2462. unsigned int z;
  2463. for (z = 0; z < k; z++)
  2464. if (is_space(sel[z]) || sel[z] == '>') {
  2465. r->single = 0;
  2466. break;
  2467. }
  2468. }
  2469. r->nd = (unsigned char)nd;
  2470. for (k = 0; k < nd; k++)
  2471. r->d[k] = decls[k];
  2472. rule_index_add(d, d->nrules - 1,
  2473. attr_ptr(d, r->key_off),
  2474. s_len(attr_ptr(d, r->key_off)));
  2475. }
  2476. }
  2477. j = q + 1;
  2478. if (q >= send)
  2479. break;
  2480. }
  2481. }
  2482. }
  2483. /* Sites often set html { font-size: 62.5% } and then size everything in
  2484. * rem. Detect that root size before parsing the sheet so a rem resolves to
  2485. * the same pixels a real browser would use. */
  2486. static void detect_root_font(const char *s, unsigned int len)
  2487. {
  2488. unsigned int i;
  2489. for (i = 0; i + 4 < len; i++) {
  2490. unsigned int j, end, sel, q;
  2491. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  2492. int exact;
  2493. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  2494. s[i + 3] == 'l'))
  2495. continue;
  2496. if (i + 4 < len && s[i + 4] != '{' && s[i + 4] != ',' &&
  2497. s[i + 4] != '.' && s[i + 4] != ':' && s[i + 4] != '[')
  2498. continue;
  2499. sel = i;
  2500. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  2501. s[sel - 1] != ';' && s[sel - 1] != ',')
  2502. sel--;
  2503. q = sel;
  2504. while (q < i && is_space(s[q]))
  2505. q++;
  2506. exact = (q == i);
  2507. if (!exact && i + 4 < len && s[i + 4] != ',' &&
  2508. s[i + 4] != '.')
  2509. continue;
  2510. j = i + 4;
  2511. while (j < len && is_space(s[j]))
  2512. j++;
  2513. if (j >= len || s[j] != '{')
  2514. continue;
  2515. while (j < len && s[j] != '{')
  2516. j++;
  2517. if (j >= len)
  2518. break;
  2519. end = j + 1;
  2520. while (end < len && s[end] != '}')
  2521. end++;
  2522. for (; j + 9 < end; j++) {
  2523. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  2524. s[j + 3] == 't' && s[j + 4] == '-' &&
  2525. s[j + 5] == 's' && s[j + 6] == 'i' &&
  2526. s[j + 7] == 'z' && s[j + 8] == 'e'))
  2527. continue;
  2528. j += 9;
  2529. while (j < end && s[j] != ':')
  2530. j++;
  2531. if (j >= end)
  2532. break;
  2533. j++;
  2534. while (j < end && is_space(s[j]))
  2535. j++;
  2536. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2537. v = v * 10 + (s[j] - '0');
  2538. digits = 1;
  2539. j++;
  2540. }
  2541. if (j < end && s[j] == '.') {
  2542. j++;
  2543. while (j < end && s[j] >= '0' && s[j] <= '9') {
  2544. v = v * 10 + (s[j] - '0');
  2545. frac++;
  2546. j++;
  2547. }
  2548. }
  2549. while (frac < 2) {
  2550. v *= 10;
  2551. frac++;
  2552. }
  2553. while (frac > 2) {
  2554. v /= 10;
  2555. frac--;
  2556. }
  2557. if (!digits)
  2558. break;
  2559. if (j < end && s[j] == '%')
  2560. pct = 1;
  2561. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  2562. px = 1;
  2563. if (pct)
  2564. v = (v * 16 + 5000) / 10000;
  2565. else if (px)
  2566. v /= 100;
  2567. else
  2568. v = v * 16 / 100;
  2569. if (exact && v >= 8 && v <= 24)
  2570. root_font_px = v;
  2571. if (exact)
  2572. return;
  2573. }
  2574. }
  2575. }
  2576. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  2577. {
  2578. detect_root_font(css, len);
  2579. css_collect_only = 1;
  2580. parse_css(d, css, len);
  2581. css_collect_only = 0;
  2582. parse_css(d, css, len);
  2583. }
  2584. void br_parse_inline_css(struct br_doc *d)
  2585. {
  2586. if (d->inline_css_len) {
  2587. css_collect_only = 1;
  2588. parse_css(d, css_store, d->inline_css_len);
  2589. css_collect_only = 0;
  2590. parse_css(d, css_store, d->inline_css_len);
  2591. }
  2592. d->inline_css_len = 0;
  2593. }
  2594. void br_reset_styles(struct br_doc *d)
  2595. {
  2596. unsigned int i;
  2597. for (i = 0; i < d->nnode; i++)
  2598. d->nodes[i].style_valid = 0;
  2599. }
  2600. /* ---- selector matching ---- */
  2601. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  2602. unsigned int clen)
  2603. {
  2604. const char *s = attr_ptr(d, d->nodes[node].class_off);
  2605. unsigned int i = 0;
  2606. while (s[i]) {
  2607. unsigned int st, n;
  2608. while (s[i] && is_space(s[i]))
  2609. i++;
  2610. st = i;
  2611. while (s[i] && !is_space(s[i]))
  2612. i++;
  2613. n = i - st;
  2614. if (n == clen && ci_eq_n(s + st, cls, n))
  2615. return 1;
  2616. }
  2617. return 0;
  2618. }
  2619. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  2620. unsigned int len)
  2621. {
  2622. const char *s = attr_ptr(d, d->nodes[node].id_off);
  2623. unsigned int i;
  2624. for (i = 0; i < len; i++)
  2625. if (s[i] != id[i])
  2626. return 0;
  2627. return s[len] == '\0';
  2628. }
  2629. static int is_ident(char c)
  2630. {
  2631. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  2632. (c >= '0' && c <= '9') || c == '-' || c == '_';
  2633. }
  2634. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  2635. {
  2636. unsigned int n = 0;
  2637. while (lit[n])
  2638. n++;
  2639. return n == nlen && ci_eq_n(name, lit, nlen);
  2640. }
  2641. static int attr_present(struct br_doc *d, unsigned int node,
  2642. const char *name, unsigned int nlen)
  2643. {
  2644. struct br_node *n = &d->nodes[node];
  2645. if (attr_is(name, nlen, "data-collapse-target"))
  2646. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  2647. if (attr_is(name, nlen, "data-collapse-trigger"))
  2648. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  2649. if (attr_is(name, nlen, "is-open"))
  2650. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  2651. if (attr_is(name, nlen, "disabled"))
  2652. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  2653. if (attr_is(name, nlen, "open"))
  2654. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  2655. if (attr_is(name, nlen, "inert"))
  2656. return (n->attr_flags & BR_ATTR_INERT) != 0;
  2657. if (attr_is(name, nlen, "hidden"))
  2658. return (n->flags & BR_NODE_HIDDEN) != 0;
  2659. if (attr_is(name, nlen, "href"))
  2660. return attr_ptr(d, n->href_off)[0] != 0;
  2661. if (attr_is(name, nlen, "class"))
  2662. return attr_ptr(d, n->class_off)[0] != 0;
  2663. if (attr_is(name, nlen, "id"))
  2664. return attr_ptr(d, n->id_off)[0] != 0;
  2665. if (attr_is(name, nlen, "src"))
  2666. return attr_ptr(d, n->src_off)[0] != 0;
  2667. if (attr_is(name, nlen, "alt"))
  2668. return attr_ptr(d, n->alt_off)[0] != 0;
  2669. if (attr_is(name, nlen, "rel"))
  2670. return attr_ptr(d, n->rel_off)[0] != 0;
  2671. return 0;
  2672. }
  2673. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  2674. unsigned int len)
  2675. {
  2676. unsigned int i = 0;
  2677. int got = 0;
  2678. if (i < len && s[i] == '*') {
  2679. i++;
  2680. got = 1;
  2681. } else if (i < len && is_ident(s[i])) {
  2682. char name[16];
  2683. unsigned int n = 0, tag;
  2684. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  2685. name[n++] = s[i++];
  2686. name[n] = '\0';
  2687. tag = br_tag_from_name(name);
  2688. if (tag == BR_TAG_UNKNOWN) {
  2689. const char *raw = attr_ptr(d,
  2690. d->nodes[node].name_off);
  2691. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  2692. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  2693. return 0;
  2694. } else if (tag != d->nodes[node].tag) {
  2695. return 0;
  2696. }
  2697. got = 1;
  2698. }
  2699. while (i < len) {
  2700. if (s[i] == '.') {
  2701. unsigned int st;
  2702. i++;
  2703. st = i;
  2704. while (i < len && is_ident(s[i]))
  2705. i++;
  2706. if (!class_has(d, node, s + st, i - st))
  2707. return 0;
  2708. got = 1;
  2709. } else if (s[i] == '#') {
  2710. unsigned int st;
  2711. i++;
  2712. st = i;
  2713. while (i < len && is_ident(s[i]))
  2714. i++;
  2715. if (!id_eq(d, node, s + st, i - st))
  2716. return 0;
  2717. got = 1;
  2718. } else if (s[i] == '[') {
  2719. unsigned int st, e;
  2720. i++;
  2721. st = e = i;
  2722. while (e < len && s[e] != ']' && s[e] != '=' &&
  2723. !is_space(s[e]))
  2724. e++;
  2725. if (e == st || !attr_present(d, node, s + st, e - st))
  2726. return 0;
  2727. while (i < len && s[i] != ']')
  2728. i++;
  2729. if (i < len)
  2730. i++;
  2731. got = 1;
  2732. } else if (s[i] == ':') {
  2733. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  2734. (i + 5 == len || !is_ident(s[i + 5]))) {
  2735. if (d->nodes[node].tag != BR_TAG_HTML)
  2736. return 0;
  2737. i += 5;
  2738. got = 1;
  2739. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  2740. unsigned int st = i + 5, e = st;
  2741. int depth = 1;
  2742. while (e < len && depth) {
  2743. if (s[e] == '(')
  2744. depth++;
  2745. else if (s[e] == ')')
  2746. depth--;
  2747. if (depth)
  2748. e++;
  2749. }
  2750. if (e >= len)
  2751. return 0;
  2752. for (unsigned int z = st; z < e; z++)
  2753. if (s[z] == ':')
  2754. return 0;
  2755. if (compound_match(d, node, s + st, e - st))
  2756. return 0;
  2757. i = e + 1;
  2758. got = 1;
  2759. } else {
  2760. return 0; /* other pseudo-classes are not modelled */
  2761. }
  2762. } else {
  2763. i++;
  2764. }
  2765. }
  2766. return got;
  2767. }
  2768. static unsigned int selector_specificity(const char *s, unsigned int len)
  2769. {
  2770. unsigned int i, spec = 0;
  2771. for (i = 0; i < len; i++) {
  2772. if (s[i] == '#')
  2773. spec += 100;
  2774. else if (s[i] == '.' || s[i] == '[' ||
  2775. (s[i] == ':' && !(i + 5 <= len &&
  2776. ci_eq_n(s + i, ":root", 5))))
  2777. spec += 10;
  2778. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  2779. if (is_ident(s[i]))
  2780. spec += 1;
  2781. }
  2782. }
  2783. return spec;
  2784. }
  2785. /* Match one complex selector; returns specificity or -1. */
  2786. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  2787. unsigned int len, int right_done)
  2788. {
  2789. struct {
  2790. unsigned int start, end;
  2791. int child;
  2792. } parts[8];
  2793. int np = 0;
  2794. unsigned int i = 0;
  2795. int right;
  2796. while (i < len) {
  2797. int child = 0;
  2798. unsigned int st, e;
  2799. while (i < len && is_space(s[i]))
  2800. i++;
  2801. if (i < len && s[i] == '>') {
  2802. child = 1;
  2803. i++;
  2804. while (i < len && is_space(s[i]))
  2805. i++;
  2806. }
  2807. if (i >= len)
  2808. break;
  2809. st = i;
  2810. while (i < len && !is_space(s[i]) && s[i] != '>')
  2811. i++;
  2812. e = i;
  2813. if (np < 8) {
  2814. parts[np].start = st;
  2815. parts[np].end = e;
  2816. parts[np].child = child;
  2817. np++;
  2818. }
  2819. }
  2820. if (!np)
  2821. return -1;
  2822. if (!right_done &&
  2823. !compound_match(d, node, s + parts[np - 1].start,
  2824. parts[np - 1].end - parts[np - 1].start))
  2825. return -1;
  2826. right = np - 1;
  2827. while (right > 0) {
  2828. unsigned int p = d->nodes[node].parent;
  2829. if (!p)
  2830. return -1;
  2831. if (parts[right].child) {
  2832. if (!compound_match(d, p, s + parts[right - 1].start,
  2833. parts[right - 1].end -
  2834. parts[right - 1].start))
  2835. return -1;
  2836. node = p;
  2837. } else {
  2838. while (p && !compound_match(d, p,
  2839. s + parts[right - 1].start,
  2840. parts[right - 1].end -
  2841. parts[right - 1].start))
  2842. p = d->nodes[p].parent;
  2843. if (!p)
  2844. return -1;
  2845. node = p;
  2846. }
  2847. right--;
  2848. }
  2849. return (int)selector_specificity(s, len);
  2850. }
  2851. #define RULE_MATCH_MAX 512
  2852. struct rule_match {
  2853. unsigned short idx;
  2854. short spec;
  2855. };
  2856. static struct rule_match rule_matches[RULE_MATCH_MAX];
  2857. static unsigned int nrule_matches;
  2858. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  2859. {
  2860. struct br_rule *r = &d->rules[idx];
  2861. const char *key = attr_ptr(d, r->key_off);
  2862. int spec, key_ok = 0;
  2863. if (key[0]) {
  2864. if (!compound_match(d, node, key, s_len(key)))
  2865. return;
  2866. key_ok = 1;
  2867. }
  2868. spec = (r->single && key_ok) ? (int)r->spec :
  2869. selector_match(d, node, attr_ptr(d, r->sel_off),
  2870. s_len(attr_ptr(d, r->sel_off)), key_ok);
  2871. if (spec < 0)
  2872. return;
  2873. if (nrule_matches < RULE_MATCH_MAX) {
  2874. rule_matches[nrule_matches].idx = (unsigned short)idx;
  2875. rule_matches[nrule_matches].spec = (short)spec;
  2876. nrule_matches++;
  2877. }
  2878. }
  2879. static void rule_scan_list(struct br_doc *d, unsigned int node,
  2880. unsigned short head)
  2881. {
  2882. while (head != 0xFFFF) {
  2883. rule_try(d, node, head);
  2884. head = d->rule_next[head];
  2885. }
  2886. }
  2887. static void rule_scan_class(struct br_doc *d, unsigned int node,
  2888. const char *cls, unsigned int len)
  2889. {
  2890. if (len)
  2891. rule_scan_list(d, node,
  2892. d->rule_bucket[rule_hash(cls, len)]);
  2893. }
  2894. static void rule_collect(struct br_doc *d, unsigned int node)
  2895. {
  2896. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  2897. const char *id = attr_ptr(d, d->nodes[node].id_off);
  2898. unsigned int tag = d->nodes[node].tag;
  2899. unsigned int i;
  2900. nrule_matches = 0;
  2901. rule_scan_list(d, node, d->rule_any);
  2902. if (tag < BR_TAG_MAX)
  2903. rule_scan_list(d, node, d->rule_tag[tag]);
  2904. if (id[0])
  2905. rule_scan_list(d, node,
  2906. d->rule_bucket[rule_hash(id, s_len(id))]);
  2907. i = 0;
  2908. while (cls[i]) {
  2909. unsigned int st, n;
  2910. while (cls[i] && is_space(cls[i]))
  2911. i++;
  2912. st = i;
  2913. while (cls[i] && !is_space(cls[i]))
  2914. i++;
  2915. n = i - st;
  2916. if (n)
  2917. rule_scan_class(d, node, cls + st, n);
  2918. }
  2919. /* Rules must cascade in document order even though the index visits
  2920. * them in bucket order. */
  2921. for (i = 1; i < nrule_matches; i++) {
  2922. struct rule_match m = rule_matches[i];
  2923. unsigned int j = i;
  2924. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  2925. rule_matches[j] = rule_matches[j - 1];
  2926. j--;
  2927. }
  2928. rule_matches[j] = m;
  2929. }
  2930. }
  2931. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  2932. {
  2933. switch (dcl->prop) {
  2934. case PROP_COLOR:
  2935. st->color = (unsigned short)dcl->val;
  2936. break;
  2937. case PROP_BG:
  2938. st->bg = (unsigned short)dcl->val;
  2939. st->has_bg = 1;
  2940. break;
  2941. case PROP_SCALE:
  2942. st->scale = (unsigned char)(dcl->val >= 2 ? 2 : 1);
  2943. break;
  2944. case PROP_BOLD:
  2945. st->bold = (unsigned char)(dcl->val != 0);
  2946. break;
  2947. case PROP_ALIGN:
  2948. st->align = (unsigned char)dcl->val;
  2949. break;
  2950. case PROP_UNDERLINE:
  2951. st->underline = (unsigned char)(dcl->val != 0);
  2952. break;
  2953. case PROP_DISPLAY:
  2954. st->display = (unsigned char)dcl->val;
  2955. break;
  2956. case PROP_VIS_HIDDEN:
  2957. st->vis_hidden = (unsigned char)(dcl->val != 0);
  2958. break;
  2959. case PROP_OPACITY_HIDDEN:
  2960. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  2961. break;
  2962. case PROP_OVERFLOW_HIDDEN:
  2963. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  2964. break;
  2965. case PROP_MT: st->mt = dcl->val; break;
  2966. case PROP_MR: st->mr = dcl->val; break;
  2967. case PROP_MB: st->mb = dcl->val; break;
  2968. case PROP_ML: st->ml = dcl->val; break;
  2969. case PROP_PT: st->pt = dcl->val; break;
  2970. case PROP_PR: st->pr = dcl->val; break;
  2971. case PROP_PB: st->pb = dcl->val; break;
  2972. case PROP_PL: st->pl = dcl->val; break;
  2973. case PROP_BORDER_W:
  2974. st->border_w = (unsigned char)dcl->val;
  2975. if (st->border_w && !st->border_color)
  2976. st->border_color = st->color;
  2977. break;
  2978. case PROP_BORDER_COLOR:
  2979. st->border_color = (unsigned short)dcl->val;
  2980. break;
  2981. case PROP_LIST_NONE:
  2982. st->no_bullet = (unsigned char)(dcl->val != 0);
  2983. break;
  2984. case PROP_WIDTH:
  2985. st->width = (unsigned short)dcl->val;
  2986. break;
  2987. case PROP_WIDTH_KIND:
  2988. st->width_kind = (unsigned char)dcl->val;
  2989. break;
  2990. case PROP_MAXW:
  2991. st->max_width = (unsigned short)dcl->val;
  2992. break;
  2993. case PROP_MAXW_KIND:
  2994. st->max_width_kind = (unsigned char)dcl->val;
  2995. break;
  2996. case PROP_HEIGHT:
  2997. st->height = (unsigned short)dcl->val;
  2998. break;
  2999. case PROP_HEIGHT_KIND:
  3000. st->height_kind = (unsigned char)dcl->val;
  3001. break;
  3002. case PROP_LH_PCT:
  3003. st->line_h_pct = (unsigned char)dcl->val;
  3004. break;
  3005. case PROP_LH_PX:
  3006. st->line_h_px = (unsigned short)dcl->val;
  3007. break;
  3008. case PROP_UPPER:
  3009. st->uppercase = (unsigned char)(dcl->val != 0);
  3010. break;
  3011. case PROP_FLOAT:
  3012. st->float_left = (unsigned char)dcl->val;
  3013. break;
  3014. case PROP_FLEX_GROW:
  3015. st->flex_grow = (unsigned char)dcl->val;
  3016. break;
  3017. case PROP_FLEX_COL:
  3018. st->flex_col = (unsigned char)(dcl->val != 0);
  3019. break;
  3020. case PROP_FLEX_WRAP:
  3021. st->flex_wrap = (unsigned char)(dcl->val != 0);
  3022. break;
  3023. case PROP_GRID_NCOLS:
  3024. st->grid_ncols = (unsigned char)dcl->val;
  3025. break;
  3026. case PROP_GRID_GAP:
  3027. st->grid_gap = (unsigned char)dcl->val;
  3028. break;
  3029. case PROP_GRID_SLOT:
  3030. st->grid_slot = (unsigned char)dcl->val;
  3031. break;
  3032. case PROP_POS:
  3033. st->pos = (unsigned char)dcl->val;
  3034. break;
  3035. case PROP_LEFT:
  3036. st->left = dcl->val;
  3037. break;
  3038. case PROP_TOP:
  3039. st->top = dcl->val;
  3040. break;
  3041. case PROP_LEFT_SET:
  3042. st->left_set = (unsigned char)(dcl->val != 0);
  3043. break;
  3044. case PROP_TOP_SET:
  3045. st->top_set = (unsigned char)(dcl->val != 0);
  3046. break;
  3047. default:
  3048. break;
  3049. }
  3050. }
  3051. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  3052. int spec, short *best, short *best_imp)
  3053. {
  3054. unsigned char p = dcl->prop;
  3055. if (p >= PROP_COUNT)
  3056. return;
  3057. if (dcl->important) {
  3058. if (spec >= best_imp[p]) {
  3059. apply_decl(st, dcl);
  3060. best_imp[p] = (short)spec;
  3061. }
  3062. } else if (best_imp[p] < 0 && spec >= best[p]) {
  3063. apply_decl(st, dcl);
  3064. best[p] = (short)spec;
  3065. }
  3066. }
  3067. void br_style_for(struct br_doc *d, unsigned int node,
  3068. const struct br_style *parent, struct br_style *out)
  3069. {
  3070. unsigned int tag;
  3071. short best[PROP_COUNT], best_imp[PROP_COUNT];
  3072. unsigned int i;
  3073. if (d->nodes[node].style_valid) {
  3074. *out = d->nodes[node].style;
  3075. return;
  3076. }
  3077. *out = *parent;
  3078. out->has_bg = 0;
  3079. out->float_left = 0;
  3080. out->border_w = 0;
  3081. out->border_color = 0;
  3082. out->width = out->max_width = out->height = 0;
  3083. out->width_kind = out->max_width_kind = out->height_kind = 0;
  3084. out->mt = out->mr = out->mb = out->ml = 0;
  3085. out->pt = out->pr = out->pb = out->pl = 0;
  3086. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  3087. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  3088. out->pos = out->left_set = out->top_set = 0;
  3089. out->left = out->top = 0;
  3090. out->vis_hidden = out->opacity_hidden = 0;
  3091. out->overflow_hidden = 0;
  3092. tag = d->nodes[node].tag;
  3093. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  3094. BR_DISPLAY_INLINE;
  3095. for (i = 0; i < PROP_COUNT; i++) {
  3096. best[i] = -1;
  3097. best_imp[i] = -1;
  3098. }
  3099. switch (tag) {
  3100. case BR_TAG_HEAD:
  3101. case BR_TAG_TITLE:
  3102. case BR_TAG_META:
  3103. case BR_TAG_LINK:
  3104. case BR_TAG_BASE:
  3105. case BR_TAG_SCRIPT:
  3106. case BR_TAG_STYLE:
  3107. out->display = BR_DISPLAY_NONE;
  3108. break;
  3109. case BR_TAG_BODY:
  3110. out->mt = out->mb = out->ml = out->mr = 4;
  3111. break;
  3112. case BR_TAG_P:
  3113. out->mt = out->mb = 8;
  3114. break;
  3115. case BR_TAG_H1:
  3116. out->scale = 2; out->bold = 1;
  3117. out->mt = 10; out->mb = 6;
  3118. break;
  3119. case BR_TAG_H2:
  3120. out->scale = 2; out->bold = 1;
  3121. out->mt = 8; out->mb = 4;
  3122. break;
  3123. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  3124. out->bold = 1; out->mt = 6; out->mb = 3;
  3125. break;
  3126. case BR_TAG_UL: case BR_TAG_OL:
  3127. out->mt = out->mb = 6; out->pl = 20;
  3128. break;
  3129. case BR_TAG_LI:
  3130. out->mt = out->mb = 2;
  3131. break;
  3132. case BR_TAG_BLOCKQUOTE:
  3133. out->mt = out->mb = 6; out->ml = out->mr = 16;
  3134. break;
  3135. case BR_TAG_PRE:
  3136. out->mt = out->mb = 8; out->pl = 8;
  3137. out->has_bg = 1; out->bg = theme_panel;
  3138. break;
  3139. case BR_TAG_CENTER:
  3140. out->align = BR_ALIGN_CENTER;
  3141. break;
  3142. case BR_TAG_A:
  3143. out->color = theme_link; out->underline = 1;
  3144. break;
  3145. case BR_TAG_B: case BR_TAG_STRONG:
  3146. out->bold = 1;
  3147. break;
  3148. case BR_TAG_CODE:
  3149. out->color = 0x8010;
  3150. break;
  3151. case BR_TAG_TD: case BR_TAG_TH:
  3152. out->pr = 12;
  3153. break;
  3154. case BR_TAG_VIDEO:
  3155. out->display = BR_DISPLAY_BLOCK;
  3156. break;
  3157. default:
  3158. break;
  3159. }
  3160. /* presentational <font color>: author CSS below still wins */
  3161. if (d->nodes[node].color_off) {
  3162. unsigned short c;
  3163. const char *v = attr_ptr(d, d->nodes[node].color_off);
  3164. if (parse_color(v, s_len(v), &c))
  3165. out->color = c;
  3166. }
  3167. rule_collect(d, node);
  3168. for (i = 0; i < nrule_matches; i++) {
  3169. struct br_rule *r = &d->rules[rule_matches[i].idx];
  3170. int spec = rule_matches[i].spec;
  3171. unsigned int k;
  3172. for (k = 0; k < r->nd; k++)
  3173. cascade_decl(out, &r->d[k], spec, best, best_imp);
  3174. }
  3175. if (d->nodes[node].style_off) {
  3176. struct br_decl decls[BR_DECLS];
  3177. unsigned int nd, k;
  3178. const char *v = attr_ptr(d, d->nodes[node].style_off);
  3179. parse_decls(d, v, s_len(v), decls, &nd);
  3180. for (k = 0; k < nd; k++)
  3181. cascade_decl(out, &decls[k], 1000, best, best_imp);
  3182. }
  3183. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  3184. out->display = BR_DISPLAY_NONE;
  3185. if (out->vis_hidden || out->opacity_hidden ||
  3186. (out->overflow_hidden && out->height_kind == 1 &&
  3187. out->height == 0))
  3188. out->display = BR_DISPLAY_NONE;
  3189. d->nodes[node].style = *out;
  3190. d->nodes[node].style_valid = 1;
  3191. }
  3192. /* ---- HTML parser ---- */
  3193. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  3194. unsigned int sp, int *pre)
  3195. {
  3196. unsigned int i;
  3197. *pre = 0;
  3198. for (i = 1; i <= sp; i++)
  3199. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  3200. (*pre)++;
  3201. }
  3202. static void implicit_close(struct br_doc *d, unsigned short *stack,
  3203. unsigned int *sp, unsigned int tag)
  3204. {
  3205. while (*sp > 0) {
  3206. unsigned int top = d->nodes[stack[*sp]].tag;
  3207. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  3208. (*sp)--;
  3209. continue;
  3210. }
  3211. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  3212. (*sp)--;
  3213. continue;
  3214. }
  3215. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  3216. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  3217. (*sp)--;
  3218. continue;
  3219. }
  3220. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  3221. (*sp)--;
  3222. continue;
  3223. }
  3224. break;
  3225. }
  3226. }
  3227. static int has_attr_token(const char *s, unsigned int len, const char *name)
  3228. {
  3229. unsigned int i, n = s_len(name);
  3230. for (i = 0; i + n <= len; i++) {
  3231. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  3232. continue;
  3233. if (!ci_eq_n(s + i, name, n))
  3234. continue;
  3235. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  3236. return 1;
  3237. }
  3238. return 0;
  3239. }
  3240. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  3241. {
  3242. unsigned short stack[64];
  3243. unsigned int sp = 0;
  3244. unsigned int i = 0;
  3245. int pre = 0;
  3246. unsigned int css_len = 0;
  3247. if (!d)
  3248. return -1;
  3249. if (!d->nodes)
  3250. br_doc_init(d);
  3251. doc_reset(d);
  3252. stack[0] = 0;
  3253. d->nodes[0].type = BR_NODE_ELEM;
  3254. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3255. while (i < len) {
  3256. if (html[i] != '<') {
  3257. unsigned int st = i;
  3258. while (i < len && html[i] != '<')
  3259. i++;
  3260. if (sp <= 63)
  3261. append_text(d, stack[sp], html + st, i - st, pre);
  3262. continue;
  3263. }
  3264. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  3265. html[i + 3] == '-') {
  3266. int p = find_ci(html, len, "-->", 3, i + 4);
  3267. i = p >= 0 ? (unsigned int)p + 3 : len;
  3268. continue;
  3269. }
  3270. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  3271. while (i < len && html[i] != '>')
  3272. i++;
  3273. if (i < len)
  3274. i++;
  3275. continue;
  3276. }
  3277. if (i + 1 < len && html[i + 1] == '/') {
  3278. unsigned int ns = i + 2, ne, tag, k, n;
  3279. char name[32];
  3280. while (ns < len && is_space(html[ns]))
  3281. ns++;
  3282. ne = ns;
  3283. while (ne < len && is_ident(html[ne]))
  3284. ne++;
  3285. if (ne == ns) {
  3286. i++;
  3287. continue;
  3288. }
  3289. n = ne - ns;
  3290. if (n >= sizeof(name))
  3291. n = sizeof(name) - 1;
  3292. for (k = 0; k < n; k++)
  3293. name[k] = html[ns + k];
  3294. name[n] = '\0';
  3295. tag = br_tag_from_name(name);
  3296. i = ne;
  3297. while (i < len && html[i] != '>')
  3298. i++;
  3299. if (i < len)
  3300. i++;
  3301. for (k = sp; k > 0; k--) {
  3302. struct br_node *nd = &d->nodes[stack[k]];
  3303. if (tag == BR_TAG_UNKNOWN) {
  3304. const char *raw = attr_ptr(d, nd->name_off);
  3305. if (nd->tag == BR_TAG_UNKNOWN &&
  3306. ci_eq_n(raw, name, n) &&
  3307. raw[n] == '\0') {
  3308. sp = k - 1;
  3309. break;
  3310. }
  3311. } else if (nd->tag == tag) {
  3312. sp = k - 1;
  3313. break;
  3314. }
  3315. }
  3316. stack_recount_pre(d, stack, sp, &pre);
  3317. continue;
  3318. }
  3319. /* start tag */
  3320. {
  3321. unsigned int ns = i + 1, ne, tag, k, n;
  3322. char name[16];
  3323. unsigned int attr_start, attr_end;
  3324. int selfclose = 0;
  3325. while (ns < len && is_space(html[ns]))
  3326. ns++;
  3327. ne = ns;
  3328. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  3329. ne++;
  3330. if (ne == ns) {
  3331. /* stray '<' */
  3332. append_text(d, stack[sp], html + i, 1, pre);
  3333. i++;
  3334. continue;
  3335. }
  3336. n = ne - ns;
  3337. if (n >= sizeof(name))
  3338. n = sizeof(name) - 1;
  3339. for (k = 0; k < n; k++)
  3340. name[k] = html[ns + k];
  3341. name[n] = '\0';
  3342. tag = br_tag_from_name(name);
  3343. attr_start = ne;
  3344. {
  3345. unsigned int q = ne;
  3346. char quote = 0;
  3347. while (q < len) {
  3348. char c = html[q];
  3349. if (quote) {
  3350. if (c == quote)
  3351. quote = 0;
  3352. } else if (c == '"' || c == '\'') {
  3353. quote = c;
  3354. } else if (c == '>') {
  3355. break;
  3356. }
  3357. q++;
  3358. }
  3359. attr_end = q;
  3360. if (q > attr_start && html[q - 1] == '/')
  3361. selfclose = 1;
  3362. i = q < len ? q + 1 : len;
  3363. }
  3364. if (tag == BR_TAG_SCRIPT) {
  3365. int p = find_ci(html, len, "</script", 8, i);
  3366. i = p >= 0 ? (unsigned int)p + 8 : len;
  3367. while (i < len && html[i] != '>')
  3368. i++;
  3369. if (i < len)
  3370. i++;
  3371. continue;
  3372. }
  3373. if (tag == BR_TAG_SVG && !selfclose) {
  3374. /* Inline SVG is not rendered; skip its subtree so
  3375. * logos and icon sprites do not consume the node
  3376. * arena. */
  3377. int p = find_ci(html, len, "</svg>", 6, i);
  3378. i = p >= 0 ? (unsigned int)p + 6 : len;
  3379. continue;
  3380. }
  3381. if (tag == BR_TAG_DETAILS && !selfclose &&
  3382. !has_attr_token(html + attr_start,
  3383. attr_end - attr_start, "open")) {
  3384. /* Closed <details> is collapsed (and this browser has
  3385. * no script to open it); skipping the subtree keeps
  3386. * mobile mega-menus out of the node arena. */
  3387. int p = find_ci(html, len, "</details>", 10, i);
  3388. i = p >= 0 ? (unsigned int)p + 10 : len;
  3389. continue;
  3390. }
  3391. if (tag == BR_TAG_STYLE) {
  3392. int p = find_ci(html, len, "</style", 7, i);
  3393. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3394. unsigned int cend = end;
  3395. while (cend > i && is_space(html[cend - 1]))
  3396. cend--;
  3397. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  3398. for (k = i; k < cend; k++)
  3399. css_store[css_len++] = html[k];
  3400. css_store[css_len++] = '\n';
  3401. }
  3402. i = p >= 0 ? (unsigned int)p + 7 : len;
  3403. while (i < len && html[i] != '>')
  3404. i++;
  3405. if (i < len)
  3406. i++;
  3407. continue;
  3408. }
  3409. if (tag == BR_TAG_TITLE) {
  3410. int p = find_ci(html, len, "</title", 7, i);
  3411. unsigned int end = p >= 0 ? (unsigned int)p : len;
  3412. unsigned int tn = 0;
  3413. char tmp[BR_TITLE_MAX];
  3414. tn = decode_text(html + i, end - i, tmp,
  3415. sizeof(tmp), 1);
  3416. (void)tn;
  3417. s_copy(d->title, tmp, sizeof(d->title));
  3418. i = p >= 0 ? (unsigned int)p + 7 : len;
  3419. while (i < len && html[i] != '>')
  3420. i++;
  3421. if (i < len)
  3422. i++;
  3423. continue;
  3424. }
  3425. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  3426. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  3427. unsigned int idx;
  3428. implicit_close(d, stack, &sp, tag);
  3429. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  3430. (unsigned char)tag);
  3431. if (idx)
  3432. parse_attrs(d, &d->nodes[idx],
  3433. html + attr_start,
  3434. attr_end - attr_start);
  3435. continue;
  3436. }
  3437. if (sp >= 63) {
  3438. d->truncated = 1;
  3439. continue;
  3440. }
  3441. implicit_close(d, stack, &sp, tag);
  3442. {
  3443. unsigned int idx = node_append(d, stack[sp],
  3444. BR_NODE_ELEM,
  3445. (unsigned char)tag);
  3446. if (!idx)
  3447. continue;
  3448. parse_attrs(d, &d->nodes[idx],
  3449. html + attr_start,
  3450. attr_end - attr_start);
  3451. if (tag == BR_TAG_UNKNOWN)
  3452. d->nodes[idx].name_off =
  3453. store_attr(d, name, n, 31);
  3454. if (!selfclose) {
  3455. sp++;
  3456. stack[sp] = (unsigned short)idx;
  3457. if (tag == BR_TAG_PRE)
  3458. pre++;
  3459. }
  3460. }
  3461. }
  3462. }
  3463. d->inline_css_len = css_len;
  3464. return 0;
  3465. }
  3466. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  3467. {
  3468. if (!d->nodes)
  3469. br_doc_init(d);
  3470. doc_reset(d);
  3471. d->plain = 1;
  3472. d->nodes[0].type = BR_NODE_ELEM;
  3473. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3474. append_text(d, 0, text, len, 1);
  3475. return 0;
  3476. }
  3477. /* A standalone image response: a synthetic document with one <img>. The
  3478. * caller decodes the already-fetched body into image slot 0 and sets the
  3479. * returned node's img_index. */
  3480. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  3481. {
  3482. unsigned int idx;
  3483. if (!d->nodes)
  3484. br_doc_init(d);
  3485. doc_reset(d);
  3486. d->nodes[0].type = BR_NODE_ELEM;
  3487. d->nodes[0].tag = BR_TAG_UNKNOWN;
  3488. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  3489. if (!idx)
  3490. return 0;
  3491. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  3492. return idx;
  3493. }