browser_html.c 101 KB

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