browser_html.c 95 KB

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