browser_html.c 106 KB

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