browser_html.c 258 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196
  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. #include <stdlib.h>
  6. #include <string.h>
  7. static int is_space(char c);
  8. static int is_ident(char c);
  9. /* ---- storage (one document per process) ----
  10. *
  11. * The node/text/attribute arenas are allocated from the ring-3 heap, not
  12. * static BSS: a large article needs several MB and the process image lives
  13. * in the 4 MB user window. heap_reset() on navigation releases them with
  14. * the rest of the page heap. */
  15. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  16. static char css_store[BR_CSS_SIZE];
  17. static int root_font_px = 16;
  18. /* Distinct background-image URLs per page (app stores give every card an
  19. * icon through an inline style). */
  20. #define BR_BG_URLS 512
  21. /* Background URLs live in one pool: a data: URL (Infima's menu chevron is
  22. * an inline SVG) is far longer than a fixed slot. */
  23. #define BR_BG_URL_POOL (128 * 1024)
  24. #define BR_BG_URL_MAX 8191
  25. static const char *bg_urls[BR_BG_URLS];
  26. static char bg_url_pool[BR_BG_URL_POOL];
  27. static unsigned int bg_url_pool_used;
  28. static unsigned int bg_url_count;
  29. /* @font-face definitions, one document at a time. The URL is resolved by
  30. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  31. struct br_font_def {
  32. char family[BR_FACE_FAMILY];
  33. char url[BR_FACE_URL];
  34. char base[192];
  35. unsigned short wmin, wmax; /* font-weight descriptor (a range for
  36. * variable fonts) */
  37. unsigned char italic; /* font-style italic/oblique */
  38. unsigned char family_id; /* first entry of the same family */
  39. unsigned char used; /* a font-family declaration names it */
  40. };
  41. static struct br_font_def font_defs[BR_MAX_FACES];
  42. static int font_face_count;
  43. static char font_base[192];
  44. static void font_reset(void)
  45. {
  46. memset(font_defs, 0, sizeof(font_defs));
  47. font_face_count = 0;
  48. font_base[0] = '\0';
  49. }
  50. void br_set_font_base(const char *base)
  51. {
  52. unsigned int i = 0;
  53. if (!base)
  54. base = "";
  55. while (base[i] && i + 1 < sizeof(font_base)) {
  56. font_base[i] = base[i];
  57. i++;
  58. }
  59. font_base[i] = '\0';
  60. }
  61. int br_font_face_count(void)
  62. {
  63. return font_face_count;
  64. }
  65. const char *br_font_face_url(int id)
  66. {
  67. if (id < 1 || id > font_face_count)
  68. return "";
  69. return font_defs[id - 1].url;
  70. }
  71. const char *br_font_face_base(int id)
  72. {
  73. if (id < 1 || id > font_face_count)
  74. return "";
  75. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  76. font_base;
  77. }
  78. const char *br_font_face_family(int id)
  79. {
  80. if (id < 1 || id > font_face_count)
  81. return "";
  82. return font_defs[id - 1].family;
  83. }
  84. int br_font_face_info(int id, struct br_font_face_info *out)
  85. {
  86. const struct br_font_def *f;
  87. if (id < 1 || id > font_face_count || !out)
  88. return -1;
  89. f = &font_defs[id - 1];
  90. out->family_id = f->family_id;
  91. out->wmin = f->wmin;
  92. out->wmax = f->wmax;
  93. out->italic = f->italic;
  94. out->used = f->used;
  95. return 0;
  96. }
  97. const char *br_bg_image_url(unsigned int idx)
  98. {
  99. if (idx >= bg_url_count)
  100. return "";
  101. return bg_urls[idx];
  102. }
  103. unsigned int br_root_font_px(void)
  104. {
  105. return (unsigned int)root_font_px;
  106. }
  107. /* ---- tag table ---- */
  108. struct tag_name {
  109. const char *name;
  110. unsigned char tag;
  111. };
  112. static const struct tag_name tag_names[] = {
  113. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  114. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  115. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  116. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  117. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  118. { "template", BR_TAG_TEMPLATE },
  119. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  120. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  121. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  122. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  123. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  124. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  125. { "picture", BR_TAG_PICTURE },
  126. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  127. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  128. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  129. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  130. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  131. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  132. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  133. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  134. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  135. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  136. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  137. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  138. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  139. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  140. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  141. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  142. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  143. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  144. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  145. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  146. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  147. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  148. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  149. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  150. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  151. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  152. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  153. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  154. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  155. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  156. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  157. { 0, 0 }
  158. };
  159. const char *br_tag_name(unsigned int tag)
  160. {
  161. unsigned int i;
  162. for (i = 0; tag_names[i].name; i++)
  163. if (tag_names[i].tag == tag)
  164. return tag_names[i].name;
  165. return "?";
  166. }
  167. static unsigned int s_len(const char *s);
  168. #define TAG_HASH_SIZE 128
  169. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  170. static int tag_hash_ready;
  171. static unsigned int tag_hash_key(const char *name, unsigned int len)
  172. {
  173. unsigned int i, h = 2166136261u;
  174. for (i = 0; i < len; i++) {
  175. h ^= (unsigned char)name[i];
  176. h *= 16777619u;
  177. }
  178. return h & (TAG_HASH_SIZE - 1);
  179. }
  180. static void tag_hash_build(void)
  181. {
  182. unsigned int i;
  183. for (i = 0; i < TAG_HASH_SIZE; i++)
  184. tag_hash_idx[i] = 0xFF;
  185. for (i = 0; tag_names[i].name; i++) {
  186. unsigned int h = tag_hash_key(tag_names[i].name,
  187. s_len(tag_names[i].name));
  188. while (tag_hash_idx[h] != 0xFF)
  189. h = (h + 1) & (TAG_HASH_SIZE - 1);
  190. tag_hash_idx[h] = (unsigned char)i;
  191. }
  192. tag_hash_ready = 1;
  193. }
  194. unsigned int br_tag_from_name(const char *name)
  195. {
  196. char lower[16];
  197. unsigned int i, len, h;
  198. unsigned char idx;
  199. for (i = 0; i < 15 && name[i]; i++) {
  200. char c = name[i];
  201. if (c >= 'A' && c <= 'Z')
  202. c = (char)(c - 'A' + 'a');
  203. lower[i] = c;
  204. }
  205. lower[i] = '\0';
  206. len = i;
  207. if (!tag_hash_ready)
  208. tag_hash_build();
  209. h = tag_hash_key(lower, len);
  210. for (;;) {
  211. idx = tag_hash_idx[h];
  212. if (idx == 0xFF)
  213. return BR_TAG_UNKNOWN;
  214. {
  215. const char *n = tag_names[idx].name;
  216. unsigned int j;
  217. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  218. ;
  219. if (!lower[j] && !n[j])
  220. return tag_names[idx].tag;
  221. }
  222. h = (h + 1) & (TAG_HASH_SIZE - 1);
  223. }
  224. }
  225. int br_tag_is_void(unsigned int tag)
  226. {
  227. switch (tag) {
  228. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  229. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  230. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  231. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  232. case BR_TAG_AREA: case BR_TAG_COL:
  233. return 1;
  234. default:
  235. return 0;
  236. }
  237. }
  238. int br_tag_is_block(unsigned int tag)
  239. {
  240. switch (tag) {
  241. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  242. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  243. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  244. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  245. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  246. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  247. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  248. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  249. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  250. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  251. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  252. case BR_TAG_PICTURE:
  253. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  254. case BR_TAG_ADDRESS: case BR_TAG_HR:
  255. return 1;
  256. default:
  257. return 0;
  258. }
  259. }
  260. /* ---- string / entity helpers ---- */
  261. static unsigned int s_len(const char *s)
  262. {
  263. unsigned int n = 0;
  264. while (s && s[n])
  265. n++;
  266. return n;
  267. }
  268. static void s_copy(char *d, const char *s, unsigned int cap)
  269. {
  270. unsigned int i = 0;
  271. if (!cap)
  272. return;
  273. while (s && s[i] && i + 1 < cap) {
  274. d[i] = s[i];
  275. i++;
  276. }
  277. d[i] = '\0';
  278. }
  279. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  280. {
  281. unsigned int i;
  282. for (i = 0; i < n; i++) {
  283. char ca = a[i], cb = b[i];
  284. if (ca >= 'A' && ca <= 'Z')
  285. ca = (char)(ca - 'A' + 'a');
  286. if (cb >= 'A' && cb <= 'Z')
  287. cb = (char)(cb - 'A' + 'a');
  288. if (ca != cb)
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. #define CURSOR_CSS_(e, css, file) css,
  294. static const char *const css_cursor_names[CURSOR_COUNT] = {
  295. CURSOR_LIST(CURSOR_CSS_)
  296. };
  297. #undef CURSOR_CSS_
  298. /* CSS `cursor`: the keyword after the last comma (url() images before it
  299. * are not supported, so the fallback keyword is what counts). Returns
  300. * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
  301. * anything else (the declaration is then ignored). */
  302. static int parse_cursor(const char *v, unsigned int n)
  303. {
  304. unsigned int a = 0, b, i;
  305. for (i = 0; i < n; i++)
  306. if (v[i] == ',')
  307. a = i + 1;
  308. while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
  309. a++;
  310. b = a;
  311. while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
  312. (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
  313. b++;
  314. if (b == a)
  315. return -1;
  316. if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
  317. return BR_CURSOR_AUTO;
  318. if (b - a == 4 && ci_eq_n(v + a, "none", 4))
  319. return CURSOR_DEFAULT + 1;
  320. for (i = 0; i < CURSOR_COUNT; i++)
  321. if (strlen(css_cursor_names[i]) == b - a &&
  322. ci_eq_n(v + a, css_cursor_names[i], b - a))
  323. return (int)i + 1;
  324. return -1;
  325. }
  326. static int find_ci(const char *hay, unsigned int len, const char *needle,
  327. unsigned int nlen, unsigned int from)
  328. {
  329. unsigned int i;
  330. if (nlen == 0 || from >= len)
  331. return -1;
  332. for (i = from; i + nlen <= len; i++)
  333. if (ci_eq_n(hay + i, needle, nlen))
  334. return (int)i;
  335. return -1;
  336. }
  337. static const struct {
  338. const char *name;
  339. const char *text;
  340. } entities[] = {
  341. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  342. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  343. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  344. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  345. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  346. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  347. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  348. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  349. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  350. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  351. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  352. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  353. { 0, 0 }
  354. };
  355. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  356. {
  357. if (*n + 1 < cap)
  358. out[(*n)++] = c;
  359. }
  360. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  361. char *out, unsigned int cap, unsigned int *n)
  362. {
  363. unsigned int start = *pos + 1, i = start;
  364. unsigned int code = 0;
  365. int ok = 0;
  366. while (i < len && s[i] != ';' && i - start < 12)
  367. i++;
  368. if (i >= len || s[i] != ';') {
  369. /* Not an entity: emit the '&' and advance past it. Leaving
  370. * *pos unchanged here used to spin forever on bare '&'. */
  371. emit(out, cap, n, '&');
  372. *pos = start;
  373. return;
  374. }
  375. if (s[start] == '#') {
  376. unsigned int j = start + 1;
  377. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  378. j++;
  379. for (; j < i; j++) {
  380. char c = s[j];
  381. int d = -1;
  382. if (c >= '0' && c <= '9') d = c - '0';
  383. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  384. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  385. if (d < 0) break;
  386. code = code * 16 + (unsigned int)d;
  387. ok = 1;
  388. }
  389. } else {
  390. for (; j < i; j++) {
  391. if (s[j] < '0' || s[j] > '9')
  392. break;
  393. code = code * 10 + (unsigned int)(s[j] - '0');
  394. ok = 1;
  395. }
  396. }
  397. } else {
  398. unsigned int k;
  399. for (k = 0; entities[k].name; k++) {
  400. unsigned int el = s_len(entities[k].name);
  401. unsigned int j;
  402. if (el != i - start)
  403. continue;
  404. for (j = 0; j < el; j++)
  405. if (s[start + j] != entities[k].name[j])
  406. break;
  407. if (j == el) {
  408. const char *t = entities[k].text;
  409. while (*t)
  410. emit(out, cap, n, *t++);
  411. *pos = i + 1;
  412. return;
  413. }
  414. }
  415. }
  416. if (ok && code) {
  417. if (code == 0x00A0)
  418. emit(out, cap, n, ' ');
  419. else if ((code >= 0x2010 && code <= 0x2015) ||
  420. code == 0x2212) /* hyphens, dashes */
  421. emit(out, cap, n, '-');
  422. else if (code == 0x2018 || code == 0x2019)
  423. emit(out, cap, n, '\'');
  424. else if (code == 0x201C || code == 0x201D)
  425. emit(out, cap, n, '"');
  426. else if (code == 0x2026) {
  427. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  428. emit(out, cap, n, '.');
  429. } else if (code <= 0xFF) {
  430. emit(out, cap, n, (char)code);
  431. } else {
  432. emit(out, cap, n, '?');
  433. }
  434. } else {
  435. emit(out, cap, n, '?');
  436. }
  437. *pos = i + 1;
  438. }
  439. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  440. * space and drops leading spaces. Returns the output length. */
  441. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  442. unsigned int cap, int collapse)
  443. {
  444. unsigned int i = 0, n = 0;
  445. while (i < len) {
  446. unsigned char c = (unsigned char)s[i];
  447. if (c == '&') {
  448. decode_entity(s, len, &i, out, cap, &n);
  449. continue;
  450. }
  451. if (c < 0x80) {
  452. i++;
  453. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  454. c == '\r' || c == '\f')) {
  455. if (n > 0 && out[n - 1] == ' ')
  456. continue;
  457. emit(out, cap, &n, ' ');
  458. continue;
  459. }
  460. if (!collapse && c == '\t') {
  461. emit(out, cap, &n, ' ');
  462. emit(out, cap, &n, ' ');
  463. emit(out, cap, &n, ' ');
  464. emit(out, cap, &n, ' ');
  465. continue;
  466. }
  467. emit(out, cap, &n, (char)c);
  468. continue;
  469. }
  470. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  471. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  472. unsigned int cp = ((c & 0x1F) << 6) |
  473. ((unsigned char)s[i + 1] & 0x3F);
  474. i += 2;
  475. if (cp == 0xA0)
  476. cp = ' ';
  477. else if (cp == 0xB7)
  478. cp = '-';
  479. else if (cp == 0xD7)
  480. cp = 'x';
  481. else if (cp == 0xB0)
  482. cp = 'o';
  483. if (cp <= 0xFF)
  484. emit(out, cap, &n, (char)cp);
  485. else
  486. emit(out, cap, &n, '?');
  487. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  488. unsigned int cp = ((c & 0x0F) << 12) |
  489. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  490. ((unsigned char)s[i + 2] & 0x3F);
  491. i += 3;
  492. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212) {
  493. emit(out, cap, &n, '-');
  494. } else if (cp == 0x2018 || cp == 0x2019) {
  495. emit(out, cap, &n, '\'');
  496. } else if (cp == 0x201C || cp == 0x201D) {
  497. emit(out, cap, &n, '"');
  498. } else if (cp == 0x2026) {
  499. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  500. emit(out, cap, &n, '.');
  501. } else if (cp == 0x2192) {
  502. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  503. } else if (cp == 0x2264) {
  504. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  505. } else {
  506. emit(out, cap, &n, '?');
  507. }
  508. } else {
  509. i++;
  510. emit(out, cap, &n, '?');
  511. }
  512. }
  513. out[n] = '\0';
  514. return n;
  515. }
  516. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  517. * space / empty. */
  518. /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
  519. * cap silently dropped every class past it (arstechnica's header lost its
  520. * background, alignment and all responsive variants). */
  521. #define BR_CLASS_MAX 2048
  522. #define BR_STYLE_ATTR_MAX 2048
  523. static unsigned int store_attr(struct br_doc *d, const char *s,
  524. unsigned int len, unsigned int limit)
  525. {
  526. unsigned int n;
  527. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  528. return 0;
  529. if (len > limit)
  530. len = limit;
  531. n = decode_text(s, len, d->attrs + d->attrs_used,
  532. BR_ATTR_SIZE - d->attrs_used, 0);
  533. if (n == 0)
  534. return 0;
  535. {
  536. unsigned int off = d->attrs_used;
  537. d->attrs_used += n + 1;
  538. return off;
  539. }
  540. }
  541. /* href is stored even when empty: `<a href="">` is a link to the page
  542. * itself (a hand cursor, a click target; scripts usually cancel the
  543. * click, like Bootstrap's data-toggle="modal"). Offset 0 would mean "no
  544. * href", so an empty value gets its own NUL byte. */
  545. static unsigned int store_href(struct br_doc *d, const char *s,
  546. unsigned int len)
  547. {
  548. unsigned int off;
  549. if (len)
  550. return store_attr(d, s, len, BR_URL_ATTR_MAX);
  551. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  552. return 0;
  553. off = d->attrs_used;
  554. d->attrs[d->attrs_used++] = '\0';
  555. return off;
  556. }
  557. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  558. {
  559. return off ? d->attrs + off : "";
  560. }
  561. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  562. static unsigned int store_raw(struct br_doc *d, const char *s,
  563. unsigned int len, unsigned int limit)
  564. {
  565. unsigned int off;
  566. if (len > limit)
  567. len = limit;
  568. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  569. return 0;
  570. off = d->attrs_used;
  571. {
  572. unsigned int i;
  573. for (i = 0; i < len; i++)
  574. d->attrs[off + i] = s[i];
  575. }
  576. d->attrs[off + len] = '\0';
  577. d->attrs_used += len + 1;
  578. return off;
  579. }
  580. #define VAR_HASH_SIZE 1024
  581. static unsigned int var_hash(const char *name, unsigned int len)
  582. {
  583. unsigned int i, h = 2166136261u;
  584. for (i = 0; i < len; i++) {
  585. h ^= (unsigned char)name[i];
  586. h *= 16777619u;
  587. }
  588. return h & (VAR_HASH_SIZE - 1);
  589. }
  590. static unsigned int css_var_find(struct br_doc *d, const char *name,
  591. unsigned int nlen)
  592. {
  593. unsigned int h = var_hash(name, nlen);
  594. for (;;) {
  595. unsigned int i = d->var_slot[h];
  596. if (!i)
  597. return BR_MAX_VARS;
  598. i--;
  599. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  600. const char *n = attr_ptr(d, d->var_name[i]);
  601. unsigned int j;
  602. for (j = 0; j < nlen && n[j] == name[j]; j++)
  603. ;
  604. if (j == nlen)
  605. return i;
  606. }
  607. h = (h + 1) & (VAR_HASH_SIZE - 1);
  608. }
  609. }
  610. #define BR_VAR_ROOT 1
  611. #define BR_VAR_CONFLICT 2
  612. #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
  613. /* Custom properties live in one document-wide table (var() is expanded
  614. * when a sheet is parsed, not per element). Definitions on :root, html,
  615. * body or * are the theme and always apply, last one winning. A definition
  616. * on any other selector is a local override: it is used while it is the
  617. * only value seen for that name, and a second, different local value marks
  618. * the name ambiguous so var() falls back. Last-wins across components was
  619. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  620. * .fa-fw, and every icon became 100% wide. */
  621. static void css_var_set(struct br_doc *d, const char *name,
  622. unsigned int nlen, const char *val, unsigned int vlen,
  623. int root)
  624. {
  625. unsigned int i, noff, voff;
  626. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  627. return;
  628. i = css_var_find(d, name, nlen);
  629. if (i < BR_MAX_VARS && !root) {
  630. const char *old = attr_ptr(d, d->var_val[i]);
  631. unsigned int k = 0;
  632. d->var_flags[i] |= BR_VAR_SCOPED;
  633. if (d->var_flags[i] & BR_VAR_ROOT)
  634. return;
  635. while (k < vlen && old[k] == val[k])
  636. k++;
  637. if (!(k == vlen && old[k] == '\0'))
  638. d->var_flags[i] |= BR_VAR_CONFLICT;
  639. return;
  640. }
  641. noff = store_raw(d, name, nlen, 63);
  642. voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
  643. if (!noff || !voff)
  644. return;
  645. if (i >= BR_MAX_VARS) {
  646. unsigned int h;
  647. if (d->nvars >= BR_MAX_VARS)
  648. return;
  649. i = d->nvars++;
  650. d->var_name[i] = noff;
  651. h = var_hash(name, nlen);
  652. while (d->var_slot[h])
  653. h = (h + 1) & (VAR_HASH_SIZE - 1);
  654. d->var_slot[h] = (unsigned short)(i + 1);
  655. d->var_flags[i] = 0;
  656. }
  657. d->var_val[i] = voff;
  658. if (root)
  659. d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
  660. (d->var_flags[i] & BR_VAR_SCOPED));
  661. else
  662. d->var_flags[i] |= BR_VAR_SCOPED;
  663. }
  664. /* 1 when every selector of the list targets the document root (the theme
  665. * scope for custom properties): :root, html, body, * or :host, optionally
  666. * with classes/attributes but no combinator. */
  667. static int css_root_selector(const char *s, unsigned int len)
  668. {
  669. unsigned int i = 0;
  670. while (i < len) {
  671. unsigned int st, en;
  672. while (i < len && (is_space(s[i]) || s[i] == ','))
  673. i++;
  674. if (i >= len)
  675. break;
  676. st = i;
  677. while (i < len && s[i] != ',')
  678. i++;
  679. en = i;
  680. while (en > st && is_space(s[en - 1]))
  681. en--;
  682. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  683. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  684. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  685. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  686. s[st] == '*'))
  687. return 0;
  688. for (; st < en; st++)
  689. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  690. s[st] == '~')
  691. return 0;
  692. }
  693. return 1;
  694. }
  695. /* Find the end of a declaration value: the next ';' outside quotes and
  696. * parentheses. The caller passes the position just after the ':'. */
  697. static unsigned int css_value_end(const char *s, unsigned int len,
  698. unsigned int i)
  699. {
  700. int depth = 0;
  701. char quote = 0;
  702. while (i < len) {
  703. char c = s[i];
  704. if (quote) {
  705. if (c == quote)
  706. quote = 0;
  707. else if (c == '\\' && i + 1 < len)
  708. i++;
  709. } else if (c == '"' || c == '\'') {
  710. quote = c;
  711. } else if (c == '(') {
  712. depth++;
  713. } else if (c == ')' && depth > 0) {
  714. depth--;
  715. } else if (c == ';' && depth == 0) {
  716. break;
  717. }
  718. i++;
  719. }
  720. return i;
  721. }
  722. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  723. * second palette. We render the light page, so skip their custom-property
  724. * definitions; otherwise their last-wins values leak into the light rules. */
  725. static int css_dark_selector(const char *s, unsigned int len)
  726. {
  727. unsigned int i;
  728. for (i = 0; i + 4 <= len; i++) {
  729. if ((s[i] == 'n' || s[i] == 'N') &&
  730. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  731. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  732. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  733. (s[i + 4] == 't' || s[i + 4] == 'T'))
  734. return 1;
  735. if ((s[i] == 'd' || s[i] == 'D') &&
  736. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  737. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  738. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  739. return 1;
  740. if (i + 12 <= len &&
  741. (s[i] == 'c' || s[i] == 'C') &&
  742. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  743. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  744. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  745. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  746. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  747. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  748. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  749. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  750. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  751. s[i + 10] == '-' &&
  752. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  753. (s[i + 12] == 's' || s[i + 12] == 'S'))
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. /* Collect --custom-property definitions from one declaration block. */
  759. static void css_collect_block(struct br_doc *d, const char *s,
  760. unsigned int len, int root)
  761. {
  762. unsigned int i = 0;
  763. while (i < len) {
  764. unsigned int pstart, pend, vstart, vend;
  765. while (i < len && (is_space(s[i]) || s[i] == ';'))
  766. i++;
  767. if (i >= len)
  768. break;
  769. pstart = i;
  770. while (i < len && s[i] != ':' && s[i] != ';')
  771. i++;
  772. pend = i;
  773. if (i >= len || s[i] != ':') {
  774. if (i < len)
  775. i++;
  776. continue;
  777. }
  778. i++;
  779. vstart = i;
  780. i = css_value_end(s, len, i);
  781. vend = i;
  782. if (i < len)
  783. i++;
  784. while (pstart < pend && is_space(s[pstart]))
  785. pstart++;
  786. while (pend > pstart && is_space(s[pend - 1]))
  787. pend--;
  788. while (vstart < vend && is_space(s[vstart]))
  789. vstart++;
  790. while (vend > vstart && is_space(s[vend - 1]))
  791. vend--;
  792. if (vend - vstart >= 10 &&
  793. ci_eq_n(s + vend - 10, "!important", 10)) {
  794. vend -= 10;
  795. while (vend > vstart && is_space(s[vend - 1]))
  796. vend--;
  797. }
  798. if (pend - pstart >= 3 && s[pstart] == '-' &&
  799. s[pstart + 1] == '-')
  800. css_var_set(d, s + pstart, pend - pstart,
  801. s + vstart, vend - vstart, root);
  802. }
  803. }
  804. const char *br_href(struct br_doc *d, unsigned int node)
  805. {
  806. return attr_ptr(d, d->nodes[node].href_off);
  807. }
  808. const char *br_src(struct br_doc *d, unsigned int node)
  809. {
  810. return attr_ptr(d, d->nodes[node].src_off);
  811. }
  812. const char *br_alt(struct br_doc *d, unsigned int node)
  813. {
  814. return attr_ptr(d, d->nodes[node].alt_off);
  815. }
  816. const char *br_rel(struct br_doc *d, unsigned int node)
  817. {
  818. return attr_ptr(d, d->nodes[node].rel_off);
  819. }
  820. const char *br_value(struct br_doc *d, unsigned int node)
  821. {
  822. return attr_ptr(d, d->nodes[node].src_off);
  823. }
  824. const char *br_placeholder(struct br_doc *d, unsigned int node)
  825. {
  826. return attr_ptr(d, d->nodes[node].alt_off);
  827. }
  828. const char *br_type(struct br_doc *d, unsigned int node)
  829. {
  830. return attr_ptr(d, d->nodes[node].rel_off);
  831. }
  832. /* ---- document setup / node append ---- */
  833. static void rule_index_reset(struct br_doc *d)
  834. {
  835. unsigned int i;
  836. for (i = 0; i < BR_MAX_RULES; i++)
  837. d->rule_next[i] = 0xFFFF;
  838. for (i = 0; i < BR_RULE_HASH; i++)
  839. d->rule_bucket[i] = 0xFFFF;
  840. for (i = 0; i < BR_TAG_MAX; i++)
  841. d->rule_tag[i] = 0xFFFF;
  842. d->rule_any = 0xFFFF;
  843. }
  844. void br_doc_init(struct br_doc *d)
  845. {
  846. unsigned int i;
  847. for (i = 0; i < sizeof(*d); i++)
  848. ((unsigned char *)d)[i] = 0;
  849. rule_index_reset(d);
  850. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  851. d->text = malloc(BR_TEXT_SIZE);
  852. d->attrs = malloc(BR_ATTR_SIZE);
  853. if (!d->nodes || !d->text || !d->attrs) {
  854. free(d->nodes);
  855. free(d->text);
  856. free(d->attrs);
  857. d->nodes = 0;
  858. d->text = 0;
  859. d->attrs = 0;
  860. return;
  861. }
  862. d->rules = rule_fallback;
  863. d->rule_cap = BR_RULE_FALLBACK;
  864. d->attrs_used = 1; /* offset 0 = "no attribute" */
  865. d->attrs[0] = '\0';
  866. d->nvars = 0;
  867. d->nlayers = 0;
  868. d->root = 0;
  869. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  870. d->var_slot[vi] = 0;
  871. root_font_px = 16;
  872. bg_url_count = 0;
  873. bg_url_pool_used = 0;
  874. font_reset();
  875. }
  876. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  877. unsigned int cap)
  878. {
  879. if (!buf || !cap) {
  880. d->rules = rule_fallback;
  881. d->rule_cap = BR_RULE_FALLBACK;
  882. d->nrules = 0;
  883. return;
  884. }
  885. if (cap > BR_MAX_RULES)
  886. cap = BR_MAX_RULES;
  887. d->rules = buf;
  888. d->rule_cap = cap;
  889. d->nrules = 0;
  890. }
  891. static void doc_reset(struct br_doc *d)
  892. {
  893. unsigned int i;
  894. d->nnode = 0;
  895. d->text_used = 0;
  896. d->attrs_used = 1;
  897. d->attrs[0] = '\0';
  898. d->nrules = 0;
  899. d->ncontent = 0;
  900. d->npe_rules = 0;
  901. rule_index_reset(d);
  902. d->title[0] = '\0';
  903. d->inline_css_len = 0;
  904. d->nvars = 0;
  905. d->nlayers = 0;
  906. d->truncated = 0;
  907. d->plain = 0;
  908. d->script_src = 0;
  909. d->nscripts = 0;
  910. d->scripts_truncated = 0;
  911. font_reset();
  912. for (i = 0; i < BR_MAX_NODES; i++) {
  913. d->nodes[i].type = BR_NODE_ELEM;
  914. d->nodes[i].tag = BR_TAG_UNKNOWN;
  915. d->nodes[i].flags = 0;
  916. /* The cached disabled/open/inert bits must not survive into
  917. * the next page: a slot that held a disabled control made
  918. * Docusaurus' search field unfocusable after navigating (and
  919. * real hardware does not hand out zeroed memory). */
  920. d->nodes[i].attr_flags = 0;
  921. d->nodes[i].parent = 0;
  922. d->nodes[i].first = d->nodes[i].last = 0;
  923. d->nodes[i].next = 0;
  924. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  925. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  926. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  927. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  928. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  929. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  930. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  931. d->nodes[i].name_off = 0;
  932. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  933. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  934. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  935. d->nodes[i].svg_rx = 0;
  936. d->nodes[i].img_index = -1;
  937. d->nodes[i].bg_img_index = -1;
  938. d->nodes[i].style_valid = 0;
  939. d->nodes[i].pe_state = 0;
  940. }
  941. }
  942. static void node_zero(struct br_node *n);
  943. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  944. unsigned char type, unsigned char tag)
  945. {
  946. unsigned int idx;
  947. struct br_node *n;
  948. if (d->nnode >= BR_MAX_NODES) {
  949. d->truncated = 1;
  950. return 0;
  951. }
  952. idx = d->nnode++;
  953. n = &d->nodes[idx];
  954. node_zero(n); /* every field, attr_flags included */
  955. n->type = type;
  956. n->tag = tag;
  957. n->parent = (unsigned short)parent;
  958. if (d->nodes[parent].last)
  959. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  960. else
  961. d->nodes[parent].first = (unsigned short)idx;
  962. d->nodes[parent].last = (unsigned short)idx;
  963. return idx;
  964. }
  965. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  966. const char *s, unsigned int len, int pre)
  967. {
  968. char *dst;
  969. unsigned int n;
  970. unsigned int idx;
  971. if (len == 0)
  972. return 0;
  973. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  974. d->truncated = 1;
  975. return 0;
  976. }
  977. dst = d->text + d->text_used;
  978. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  979. if (n == 0) {
  980. d->text[d->text_used] = '\0';
  981. return 0;
  982. }
  983. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  984. if (!idx)
  985. return 0;
  986. d->nodes[idx].text_off = d->text_used;
  987. d->nodes[idx].text_len = (unsigned short)n;
  988. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  989. d->text_used += n + 1;
  990. return idx;
  991. }
  992. /* Raw text child (no entity decoding, whitespace kept): the body of a
  993. * data-block <script> such as type="application/json", which scripts read
  994. * through textContent. Bodies longer than a text node holds are dropped. */
  995. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  996. const char *s, unsigned int len)
  997. {
  998. unsigned int idx, i;
  999. if (len == 0 || len > 0xFFFFu)
  1000. return 0;
  1001. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  1002. d->truncated = 1;
  1003. return 0;
  1004. }
  1005. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  1006. if (!idx)
  1007. return 0;
  1008. for (i = 0; i < len; i++)
  1009. d->text[d->text_used + i] = s[i];
  1010. d->text[d->text_used + len] = '\0';
  1011. d->nodes[idx].text_off = d->text_used;
  1012. d->nodes[idx].text_len = (unsigned short)len;
  1013. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  1014. d->text_used += len + 1;
  1015. return idx;
  1016. }
  1017. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  1018. static int same_string(const char *a, const char *b)
  1019. {
  1020. while (*a && *a == *b) {
  1021. a++;
  1022. b++;
  1023. }
  1024. return *a == *b;
  1025. }
  1026. static int same_string_n(const char *a, const char *b, unsigned int n)
  1027. {
  1028. unsigned int i;
  1029. for (i = 0; i < n; i++)
  1030. if (a[i] != b[i])
  1031. return 0;
  1032. return 1;
  1033. }
  1034. static int node_detached(struct br_doc *d, unsigned int node)
  1035. {
  1036. unsigned int guard = 0;
  1037. while (node && node < d->nnode && guard++ <= d->nnode) {
  1038. if (d->nodes[node].flags & BR_NODE_DETACHED)
  1039. return 1;
  1040. node = d->nodes[node].parent;
  1041. }
  1042. return 0;
  1043. }
  1044. int br_node_detached(struct br_doc *d, unsigned int node)
  1045. {
  1046. return !d || node >= d->nnode || node_detached(d, node);
  1047. }
  1048. unsigned int br_find_id(struct br_doc *d, const char *id)
  1049. {
  1050. unsigned int i;
  1051. if (!d || !id)
  1052. return 0;
  1053. for (i = 0; i < d->nnode; i++) {
  1054. struct br_node *n = &d->nodes[i];
  1055. if (n->type == BR_NODE_ELEM && n->id_off &&
  1056. same_string(d->attrs + n->id_off, id) &&
  1057. !node_detached(d, i))
  1058. return i + 1;
  1059. }
  1060. return 0;
  1061. }
  1062. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1063. unsigned int cap, unsigned int *used)
  1064. {
  1065. unsigned int c;
  1066. if (node >= d->nnode)
  1067. return;
  1068. if (d->nodes[node].type == BR_NODE_TEXT) {
  1069. unsigned int len = d->nodes[node].text_len;
  1070. unsigned int off = d->nodes[node].text_off;
  1071. if (*used + len >= cap)
  1072. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1073. for (c = 0; c < len; c++)
  1074. out[*used + c] = d->text[off + c];
  1075. *used += len;
  1076. return;
  1077. }
  1078. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1079. text_walk(d, c, out, cap, used);
  1080. }
  1081. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1082. char *out, unsigned int cap)
  1083. {
  1084. unsigned int used = 0;
  1085. if (!d || !out || cap == 0 || node >= d->nnode)
  1086. return 0;
  1087. text_walk(d, node, out, cap, &used);
  1088. out[used] = '\0';
  1089. return used;
  1090. }
  1091. int br_set_text_content(struct br_doc *d, unsigned int node,
  1092. const char *text, unsigned int len)
  1093. {
  1094. struct br_node *n;
  1095. unsigned int idx;
  1096. unsigned int i;
  1097. if (!d || node >= d->nnode)
  1098. return -1;
  1099. /* Detach the old subtree; its arena storage is reclaimed on the next
  1100. * navigation. */
  1101. {
  1102. unsigned int c;
  1103. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1104. d->nodes[c].flags |= BR_NODE_DETACHED;
  1105. }
  1106. d->nodes[node].first = d->nodes[node].last = 0;
  1107. d->nodes[node].style_valid = 0;
  1108. d->nodes[node].pe_state = 0;
  1109. if (len == 0)
  1110. return 0;
  1111. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1112. d->truncated = 1;
  1113. return -1;
  1114. }
  1115. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1116. if (!idx)
  1117. return -1;
  1118. for (i = 0; i < len; i++)
  1119. d->text[d->text_used + i] = text[i];
  1120. d->text[d->text_used + len] = '\0';
  1121. n = &d->nodes[idx];
  1122. n->text_off = d->text_used;
  1123. n->text_len = (unsigned short)len;
  1124. n->flags = 0;
  1125. d->text_used += len + 1;
  1126. return 0;
  1127. }
  1128. /* Replace the value of an existing text or comment node in place, keeping the
  1129. * node handle (and therefore the JS wrapper) valid. */
  1130. int br_set_text_node(struct br_doc *d, unsigned int node,
  1131. const char *text, unsigned int len)
  1132. {
  1133. struct br_node *n;
  1134. unsigned int i;
  1135. if (!d || node >= d->nnode)
  1136. return -1;
  1137. n = &d->nodes[node];
  1138. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1139. return -1;
  1140. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1141. d->truncated = 1;
  1142. return -1;
  1143. }
  1144. for (i = 0; i < len; i++)
  1145. d->text[d->text_used + i] = text[i];
  1146. d->text[d->text_used + len] = '\0';
  1147. n->text_off = d->text_used;
  1148. n->text_len = (unsigned short)len;
  1149. d->text_used += len + 1;
  1150. return 0;
  1151. }
  1152. /* ---- runtime DOM construction and mutation ---- */
  1153. static unsigned int text_len(const char *s)
  1154. {
  1155. unsigned int n = 0;
  1156. while (s && s[n])
  1157. n++;
  1158. return n;
  1159. }
  1160. static void node_zero(struct br_node *n)
  1161. {
  1162. unsigned int i;
  1163. for (i = 0; i < sizeof(*n); i++)
  1164. ((unsigned char *)n)[i] = 0;
  1165. n->img_index = -1;
  1166. n->bg_img_index = -1;
  1167. }
  1168. static void node_unlink(struct br_doc *d, unsigned int handle)
  1169. {
  1170. unsigned int node, parent, c, prev = 0;
  1171. if (!d || handle == 0 || handle > d->nnode)
  1172. return;
  1173. node = handle - 1;
  1174. parent = d->nodes[node].parent;
  1175. if (parent >= d->nnode) {
  1176. d->nodes[node].parent = 0;
  1177. d->nodes[node].next = 0;
  1178. return;
  1179. }
  1180. c = d->nodes[parent].first;
  1181. while (c && c != node) {
  1182. prev = c;
  1183. c = d->nodes[c].next;
  1184. }
  1185. if (c == node) {
  1186. if (prev)
  1187. d->nodes[prev].next = d->nodes[node].next;
  1188. else
  1189. d->nodes[parent].first = d->nodes[node].next;
  1190. if (d->nodes[parent].last == node)
  1191. d->nodes[parent].last = (unsigned short)prev;
  1192. }
  1193. d->nodes[node].parent = 0;
  1194. d->nodes[node].next = 0;
  1195. d->nodes[node].flags |= BR_NODE_DETACHED;
  1196. }
  1197. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1198. const char *s, unsigned int len);
  1199. static unsigned int find_tag_end(const char *s, unsigned int i,
  1200. unsigned int len)
  1201. {
  1202. char quote = 0;
  1203. while (i < len) {
  1204. char c = s[i];
  1205. if (quote) {
  1206. if (c == quote)
  1207. quote = 0;
  1208. } else if (c == '"' || c == '\'') {
  1209. quote = c;
  1210. } else if (c == '>') {
  1211. return i;
  1212. }
  1213. i++;
  1214. }
  1215. return len;
  1216. }
  1217. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1218. const char *html, unsigned int len)
  1219. {
  1220. unsigned int stack[64];
  1221. unsigned int sp = 1, i = 0;
  1222. if (!d || node >= d->nnode)
  1223. return -1;
  1224. stack[0] = node;
  1225. {
  1226. unsigned int c;
  1227. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1228. d->nodes[c].flags |= BR_NODE_DETACHED;
  1229. }
  1230. d->nodes[node].first = d->nodes[node].last = 0;
  1231. d->nodes[node].style_valid = 0;
  1232. d->nodes[node].pe_state = 0;
  1233. while (i < len) {
  1234. if (html[i] != '<') {
  1235. unsigned int start = i, end;
  1236. while (i < len && html[i] != '<')
  1237. i++;
  1238. end = i;
  1239. if (end > start) {
  1240. /* Entities decoded like the page parser does
  1241. * (createTextNode stays literal). */
  1242. char *tb = (char *)malloc(end - start + 1);
  1243. unsigned int tl = 0, h = 0;
  1244. if (tb) {
  1245. tl = decode_text(html + start, end - start,
  1246. tb, end - start + 1, 1);
  1247. h = br_create_text(d, tb, tl);
  1248. free(tb);
  1249. }
  1250. if (h)
  1251. br_append_child(d, stack[sp - 1], h);
  1252. }
  1253. continue;
  1254. }
  1255. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1256. html[i + 3] == '-') {
  1257. int p = find_ci(html, len, "-->", 3, i + 4);
  1258. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1259. unsigned int h = br_create_comment(d, html + i + 4,
  1260. end > i + 4 ?
  1261. end - i - 4 : 0);
  1262. if (h)
  1263. br_append_child(d, stack[sp - 1], h);
  1264. i = p >= 0 ? (unsigned int)p + 3 : len;
  1265. continue;
  1266. }
  1267. if (html[i + 1] == '/') {
  1268. unsigned int p = find_tag_end(html, i + 2, len);
  1269. i = p < len ? p + 1 : len;
  1270. if (sp > 1)
  1271. sp--;
  1272. continue;
  1273. }
  1274. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1275. unsigned int p = find_tag_end(html, i + 2, len);
  1276. i = p < len ? p + 1 : len;
  1277. continue;
  1278. }
  1279. {
  1280. unsigned int p = find_tag_end(html, i + 1, len);
  1281. unsigned int ns = i + 1, ne, as, ae;
  1282. unsigned int h, k;
  1283. char name[32];
  1284. int selfclose = 0;
  1285. while (ns < p && is_space(html[ns]))
  1286. ns++;
  1287. ne = ns;
  1288. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1289. ne++;
  1290. as = ne;
  1291. ae = p;
  1292. if (ae > as && html[ae - 1] == '/') {
  1293. selfclose = 1;
  1294. ae--;
  1295. }
  1296. if (ne == ns) {
  1297. i = p < len ? p + 1 : len;
  1298. continue;
  1299. }
  1300. if (ne - ns >= sizeof(name)) {
  1301. i = p < len ? p + 1 : len;
  1302. continue;
  1303. }
  1304. for (k = 0; k < ne - ns; k++)
  1305. name[k] = html[ns + k];
  1306. name[ne - ns] = '\0';
  1307. h = br_create_element(d, name, ne - ns);
  1308. if (!h) {
  1309. i = p < len ? p + 1 : len;
  1310. continue;
  1311. }
  1312. if (ae > as)
  1313. parse_attrs(d, &d->nodes[h - 1], html + as,
  1314. ae - as);
  1315. br_append_child(d, stack[sp - 1], h);
  1316. {
  1317. unsigned int tag = d->nodes[h - 1].tag;
  1318. if (!selfclose && !br_tag_is_void(tag) &&
  1319. sp < sizeof(stack) / sizeof(stack[0]))
  1320. stack[sp++] = h - 1;
  1321. }
  1322. i = p < len ? p + 1 : len;
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1328. {
  1329. unsigned int i;
  1330. if (!d)
  1331. return 0;
  1332. for (i = 0; i < d->nnode; i++)
  1333. if (d->nodes[i].type == BR_NODE_ELEM &&
  1334. d->nodes[i].tag == (unsigned char)tag &&
  1335. !node_detached(d, i))
  1336. return i + 1;
  1337. return 0;
  1338. }
  1339. unsigned int br_create_element(struct br_doc *d, const char *name,
  1340. unsigned int len)
  1341. {
  1342. struct br_node *n;
  1343. unsigned int idx;
  1344. if (!d || !name)
  1345. return 0;
  1346. if (d->nnode >= BR_MAX_NODES) {
  1347. d->truncated = 1;
  1348. return 0;
  1349. }
  1350. idx = d->nnode++;
  1351. n = &d->nodes[idx];
  1352. node_zero(n);
  1353. n->type = BR_NODE_ELEM;
  1354. n->flags = BR_NODE_DETACHED;
  1355. n->tag = (unsigned char)br_tag_from_name(name);
  1356. if (n->tag == BR_TAG_UNKNOWN && len)
  1357. n->name_off = store_attr(d, name, len, 31);
  1358. return idx + 1;
  1359. }
  1360. unsigned int br_create_text(struct br_doc *d, const char *text,
  1361. unsigned int len)
  1362. {
  1363. struct br_node *n;
  1364. unsigned int idx, i;
  1365. if (!d)
  1366. return 0;
  1367. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1368. d->truncated = 1;
  1369. return 0;
  1370. }
  1371. idx = d->nnode++;
  1372. n = &d->nodes[idx];
  1373. node_zero(n);
  1374. n->type = BR_NODE_TEXT;
  1375. n->flags = BR_NODE_DETACHED;
  1376. n->text_off = d->text_used;
  1377. n->text_len = (unsigned short)len;
  1378. for (i = 0; i < len; i++)
  1379. d->text[d->text_used + i] = text[i];
  1380. d->text[d->text_used + len] = '\0';
  1381. d->text_used += len + 1;
  1382. return idx + 1;
  1383. }
  1384. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1385. unsigned int len)
  1386. {
  1387. unsigned int h = br_create_text(d, text, len);
  1388. if (h)
  1389. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1390. return h;
  1391. }
  1392. unsigned int br_create_fragment(struct br_doc *d)
  1393. {
  1394. struct br_node *n;
  1395. unsigned int idx;
  1396. if (!d)
  1397. return 0;
  1398. if (d->nnode >= BR_MAX_NODES) {
  1399. d->truncated = 1;
  1400. return 0;
  1401. }
  1402. idx = d->nnode++;
  1403. n = &d->nodes[idx];
  1404. node_zero(n);
  1405. n->type = BR_NODE_FRAGMENT;
  1406. n->flags = BR_NODE_DETACHED;
  1407. return idx + 1;
  1408. }
  1409. int br_append_child(struct br_doc *d, unsigned int parent,
  1410. unsigned int child)
  1411. {
  1412. unsigned int p;
  1413. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1414. parent == child - 1)
  1415. return -1;
  1416. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1417. /* Appending a fragment inserts its children, not the fragment. */
  1418. unsigned int c = d->nodes[child - 1].first;
  1419. while (c) {
  1420. unsigned int next = d->nodes[c].next;
  1421. br_append_child(d, parent, c + 1);
  1422. c = next;
  1423. }
  1424. return 0;
  1425. }
  1426. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1427. if (p == child - 1)
  1428. return -1;
  1429. node_unlink(d, child);
  1430. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1431. d->nodes[child - 1].parent = (unsigned short)parent;
  1432. d->nodes[child - 1].next = 0;
  1433. if (d->nodes[parent].last)
  1434. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1435. else
  1436. d->nodes[parent].first = (unsigned short)(child - 1);
  1437. d->nodes[parent].last = (unsigned short)(child - 1);
  1438. return 0;
  1439. }
  1440. int br_insert_before(struct br_doc *d, unsigned int parent,
  1441. unsigned int child, unsigned int before)
  1442. {
  1443. unsigned int prev = 0, c;
  1444. if (before == 0)
  1445. return br_append_child(d, parent, child);
  1446. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1447. before > d->nnode || parent == child - 1)
  1448. return -1;
  1449. if (d->nodes[before - 1].parent != parent)
  1450. return -1;
  1451. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1452. unsigned int c = d->nodes[child - 1].first;
  1453. while (c) {
  1454. unsigned int next = d->nodes[c].next;
  1455. br_insert_before(d, parent, c + 1, before);
  1456. c = next;
  1457. }
  1458. return 0;
  1459. }
  1460. {
  1461. unsigned int p;
  1462. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1463. if (p == child - 1)
  1464. return -1;
  1465. }
  1466. node_unlink(d, child);
  1467. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1468. c = d->nodes[parent].first;
  1469. while (c && c != before - 1) {
  1470. prev = c;
  1471. c = d->nodes[c].next;
  1472. }
  1473. if (c != before - 1)
  1474. return -1;
  1475. d->nodes[child - 1].parent = (unsigned short)parent;
  1476. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1477. if (prev)
  1478. d->nodes[prev].next = (unsigned short)(child - 1);
  1479. else
  1480. d->nodes[parent].first = (unsigned short)(child - 1);
  1481. return 0;
  1482. }
  1483. int br_remove_child(struct br_doc *d, unsigned int parent,
  1484. unsigned int child)
  1485. {
  1486. if (!d || child == 0 || child > d->nnode ||
  1487. d->nodes[child - 1].parent != parent)
  1488. return -1;
  1489. node_unlink(d, child);
  1490. return 0;
  1491. }
  1492. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1493. unsigned int child)
  1494. {
  1495. unsigned int c;
  1496. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1497. return 0;
  1498. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1499. if (c == child - 1)
  1500. return child;
  1501. return 0;
  1502. }
  1503. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1504. const char *name)
  1505. {
  1506. struct br_node *n;
  1507. const char *p, *end, *found = 0;
  1508. if (!d || node >= d->nnode || !name)
  1509. return 0;
  1510. n = &d->nodes[node];
  1511. p = d->attrs + n->extra_off;
  1512. end = p + n->extra_len;
  1513. while (p + 1 < end) {
  1514. const char *an = p;
  1515. const char *av;
  1516. p += text_len(p) + 1;
  1517. if (p >= end)
  1518. break;
  1519. av = p;
  1520. p += text_len(p) + 1;
  1521. if (same_string(an, name))
  1522. found = av;
  1523. }
  1524. if (found)
  1525. return found;
  1526. if (same_string(name, "id") && n->id_off)
  1527. return d->attrs + n->id_off;
  1528. if (same_string(name, "class") && n->class_off)
  1529. return d->attrs + n->class_off;
  1530. if (same_string(name, "style") && n->style_off)
  1531. return d->attrs + n->style_off;
  1532. if (same_string(name, "href") && n->href_off)
  1533. return d->attrs + n->href_off;
  1534. if (same_string(name, "src") && n->src_off)
  1535. return d->attrs + n->src_off;
  1536. if (same_string(name, "alt") && n->alt_off)
  1537. return d->attrs + n->alt_off;
  1538. if (same_string(name, "rel") && n->rel_off)
  1539. return d->attrs + n->rel_off;
  1540. return 0;
  1541. }
  1542. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1543. const char *name, const char *value,
  1544. unsigned int len)
  1545. {
  1546. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1547. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1548. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1549. * exactly like parse_attrs(); script-built inputs (Vue sets
  1550. * type="password" after createElement) must land there too. */
  1551. if (control && same_string(name, "type")) {
  1552. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1553. return;
  1554. }
  1555. if (control && same_string(name, "value")) {
  1556. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1557. return;
  1558. }
  1559. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1560. same_string(name, "placeholder")) {
  1561. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1562. return;
  1563. }
  1564. if (same_string(name, "id"))
  1565. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1566. else if (same_string(name, "class"))
  1567. n->class_off = len ? store_attr(d, value, len,
  1568. BR_CLASS_MAX) : 0;
  1569. else if (same_string(name, "style"))
  1570. n->style_off = len ? store_attr(d, value, len,
  1571. BR_STYLE_ATTR_MAX) : 0;
  1572. else if (same_string(name, "href"))
  1573. n->href_off = store_href(d, value, len);
  1574. else if (same_string(name, "src"))
  1575. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1576. else if (same_string(name, "alt"))
  1577. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1578. else if (same_string(name, "rel"))
  1579. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1580. }
  1581. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1582. const char *value, unsigned int len)
  1583. {
  1584. struct br_node *n;
  1585. unsigned int start, nl, need;
  1586. const char *p, *old, *oldend;
  1587. if (!d || node >= d->nnode || !name)
  1588. return -1;
  1589. n = &d->nodes[node];
  1590. nl = text_len(name);
  1591. need = nl + 1 + len + 1;
  1592. start = d->attrs_used;
  1593. old = d->attrs + n->extra_off;
  1594. oldend = old + n->extra_len;
  1595. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1596. d->truncated = 1;
  1597. return -1;
  1598. }
  1599. p = old;
  1600. while (p + 1 < oldend) {
  1601. const char *an = p;
  1602. const char *av;
  1603. unsigned int anl, avl, k;
  1604. anl = text_len(an);
  1605. p += anl + 1;
  1606. if (p >= oldend)
  1607. break;
  1608. av = p;
  1609. avl = text_len(av);
  1610. p += avl + 1;
  1611. if (same_string(an, name))
  1612. continue;
  1613. for (k = 0; k < anl; k++)
  1614. d->attrs[d->attrs_used++] = an[k];
  1615. d->attrs[d->attrs_used++] = '\0';
  1616. for (k = 0; k < avl; k++)
  1617. d->attrs[d->attrs_used++] = av[k];
  1618. d->attrs[d->attrs_used++] = '\0';
  1619. }
  1620. {
  1621. unsigned int k;
  1622. for (k = 0; k < nl; k++)
  1623. d->attrs[d->attrs_used++] = name[k];
  1624. d->attrs[d->attrs_used++] = '\0';
  1625. for (k = 0; k < len; k++)
  1626. d->attrs[d->attrs_used++] = value[k];
  1627. d->attrs[d->attrs_used++] = '\0';
  1628. }
  1629. n = &d->nodes[node];
  1630. n->extra_off = start;
  1631. n->extra_len = (unsigned short)(d->attrs_used - start);
  1632. n->style_valid = 0;
  1633. n->pe_state = 0;
  1634. attr_note_known(d, n, name, value, len);
  1635. /* A script that sets a style/class (Angular's ng-style) or a new src
  1636. * after the first image scan re-arms the node for the loader; -2
  1637. * ("nothing to load") would otherwise be final. */
  1638. if ((same_string(name, "style") || same_string(name, "class")) &&
  1639. n->bg_img_index == -2)
  1640. n->bg_img_index = -1;
  1641. if (same_string(name, "src") && n->img_index == -2)
  1642. n->img_index = -1;
  1643. return 0;
  1644. }
  1645. static void clear_attr_flag(struct br_node *n, const char *name);
  1646. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1647. {
  1648. struct br_node *n;
  1649. unsigned int start;
  1650. const char *p, *oldend;
  1651. int found = 0;
  1652. if (!d || node >= d->nnode || !name)
  1653. return -1;
  1654. n = &d->nodes[node];
  1655. start = d->attrs_used;
  1656. p = d->attrs + n->extra_off;
  1657. oldend = p + n->extra_len;
  1658. if (start + n->extra_len > BR_ATTR_SIZE)
  1659. return -1;
  1660. while (p + 1 < oldend) {
  1661. const char *an = p;
  1662. const char *av;
  1663. unsigned int anl, avl, k;
  1664. anl = text_len(an);
  1665. p += anl + 1;
  1666. if (p >= oldend)
  1667. break;
  1668. av = p;
  1669. avl = text_len(av);
  1670. p += avl + 1;
  1671. if (same_string(an, name)) {
  1672. found = 1;
  1673. continue;
  1674. }
  1675. for (k = 0; k < anl; k++)
  1676. d->attrs[d->attrs_used++] = an[k];
  1677. d->attrs[d->attrs_used++] = '\0';
  1678. for (k = 0; k < avl; k++)
  1679. d->attrs[d->attrs_used++] = av[k];
  1680. d->attrs[d->attrs_used++] = '\0';
  1681. }
  1682. n = &d->nodes[node];
  1683. n->extra_off = start;
  1684. n->extra_len = (unsigned short)(d->attrs_used - start);
  1685. n->style_valid = 0;
  1686. n->pe_state = 0;
  1687. attr_note_known(d, n, name, "", 0);
  1688. if (found)
  1689. clear_attr_flag(n, name);
  1690. return found ? 0 : -1;
  1691. }
  1692. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1693. {
  1694. struct br_node *src;
  1695. unsigned int h;
  1696. const char *name;
  1697. if (!d || node == 0 || node > d->nnode)
  1698. return 0;
  1699. src = &d->nodes[node - 1];
  1700. if (src->type == BR_NODE_TEXT)
  1701. return br_create_text(d, d->text + src->text_off, src->text_len);
  1702. if (src->type == BR_NODE_COMMENT)
  1703. return br_create_comment(d, d->text + src->text_off,
  1704. src->text_len);
  1705. if (src->type == BR_NODE_FRAGMENT)
  1706. h = br_create_fragment(d);
  1707. else {
  1708. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1709. name = d->attrs + src->name_off;
  1710. else
  1711. name = br_tag_name(src->tag);
  1712. h = br_create_element(d, name, text_len(name));
  1713. }
  1714. if (!h)
  1715. return 0;
  1716. {
  1717. const char *p = d->attrs + src->extra_off;
  1718. const char *end = p + src->extra_len;
  1719. while (p + 1 < end) {
  1720. const char *an = p;
  1721. const char *av;
  1722. unsigned int anl, avl;
  1723. anl = text_len(an);
  1724. p += anl + 1;
  1725. if (p >= end)
  1726. break;
  1727. av = p;
  1728. avl = text_len(av);
  1729. p += avl + 1;
  1730. /* h is a 1-based handle; br_set_attr takes the
  1731. * node index (the old call stamped the copy's
  1732. * attributes onto the previous node). */
  1733. br_set_attr(d, h - 1, an, av, avl);
  1734. }
  1735. }
  1736. if (deep) {
  1737. unsigned int c;
  1738. for (c = src->first; c; c = d->nodes[c].next) {
  1739. unsigned int ch = br_clone_node(d, c + 1, 1);
  1740. if (ch)
  1741. br_append_child(d, h - 1, ch);
  1742. }
  1743. }
  1744. return h;
  1745. }
  1746. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1747. unsigned int len);
  1748. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1749. unsigned int *st, unsigned int *en);
  1750. /* querySelector(All) and Element.matches use the cascade's selector engine
  1751. * (sel_match(), below). */
  1752. static int match_selector(struct br_doc *d, unsigned int node,
  1753. const char *sel, unsigned int len)
  1754. {
  1755. while (len > 0 && sel[0] == ' ') {
  1756. sel++;
  1757. len--;
  1758. }
  1759. while (len > 0 && sel[len - 1] == ' ')
  1760. len--;
  1761. return len && sel_match(d, node, sel, len);
  1762. }
  1763. static int query_add(unsigned int *out, unsigned int max,
  1764. unsigned int *count, unsigned int node)
  1765. {
  1766. unsigned int i;
  1767. for (i = 0; i < *count; i++)
  1768. if (out[i] == node + 1)
  1769. return 0;
  1770. if (*count < max)
  1771. out[(*count)++] = node + 1;
  1772. return 0;
  1773. }
  1774. static void query_walk(struct br_doc *d, unsigned int node,
  1775. const char *sel, unsigned int len, unsigned int *out,
  1776. unsigned int max, unsigned int *count)
  1777. {
  1778. unsigned int c;
  1779. if (d->nodes[node].type == BR_NODE_ELEM &&
  1780. match_selector(d, node, sel, len))
  1781. query_add(out, max, count, node);
  1782. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1783. query_walk(d, c, sel, len, out, max, count);
  1784. }
  1785. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1786. {
  1787. unsigned int len;
  1788. if (!d || !selector || node == 0 || node > d->nnode)
  1789. return 0;
  1790. len = text_len(selector);
  1791. while (len > 0 && selector[0] == ' ') {
  1792. selector++;
  1793. len--;
  1794. }
  1795. while (len > 0 && selector[len - 1] == ' ')
  1796. len--;
  1797. {
  1798. unsigned int pos = 0, st, en;
  1799. while (sel_list_next(selector, len, &pos, &st, &en))
  1800. if (match_selector(d, node - 1, selector + st, en - st))
  1801. return 1;
  1802. }
  1803. return 0;
  1804. }
  1805. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1806. const char *selector, unsigned int *out,
  1807. unsigned int max)
  1808. {
  1809. unsigned int count = 0, i = 0, len, start, end;
  1810. if (!d || !selector || !out || max == 0)
  1811. return 0;
  1812. len = text_len(selector);
  1813. while (sel_list_next(selector, len, &i, &start, &end)) {
  1814. if (root == 0) {
  1815. unsigned int n;
  1816. for (n = 1; n < d->nnode; n++)
  1817. if (d->nodes[n].type == BR_NODE_ELEM &&
  1818. !node_detached(d, n) &&
  1819. match_selector(d, n, selector + start,
  1820. end - start))
  1821. query_add(out, max, &count, n);
  1822. } else if (root <= d->nnode) {
  1823. unsigned int c;
  1824. for (c = d->nodes[root - 1].first; c;
  1825. c = d->nodes[c].next)
  1826. query_walk(d, c, selector + start,
  1827. end - start, out, max, &count);
  1828. }
  1829. }
  1830. return count;
  1831. }
  1832. /* ---- attribute parsing ---- */
  1833. static int is_space(char c)
  1834. {
  1835. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1836. }
  1837. static int str_eq(const char *a, const char *b);
  1838. static unsigned int parse_uint(const char *s, unsigned int len)
  1839. {
  1840. unsigned int i = 0, v = 0;
  1841. while (i < len && is_space(s[i]))
  1842. i++;
  1843. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1844. v = v * 10 + (unsigned int)(s[i] - '0');
  1845. if (v > 4096)
  1846. v = 4096;
  1847. i++;
  1848. }
  1849. return v;
  1850. }
  1851. static int contains_ci_len(const char *s, unsigned int len,
  1852. const char *needle);
  1853. static int parse_svg_viewbox(const char *s, unsigned int len,
  1854. unsigned short *w, unsigned short *h)
  1855. {
  1856. unsigned int i = 0, v[4], n = 0;
  1857. while (i < len && n < 4) {
  1858. unsigned int x = 0;
  1859. int any = 0;
  1860. while (i < len && (is_space(s[i]) || s[i] == ','))
  1861. i++;
  1862. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1863. x = x * 10 + (unsigned int)(s[i] - '0');
  1864. i++;
  1865. any = 1;
  1866. }
  1867. /* viewBox values are commonly fractional ("0 0 340.16
  1868. * 340.16"). Consume the fraction so it is not mistaken for
  1869. * the next number; the integer part is enough for the aspect
  1870. * ratio used by the renderer. */
  1871. if (i < len && s[i] == '.') {
  1872. i++;
  1873. while (i < len && s[i] >= '0' && s[i] <= '9')
  1874. i++;
  1875. }
  1876. if (!any) {
  1877. if (i < len)
  1878. i++;
  1879. continue;
  1880. }
  1881. v[n++] = x;
  1882. }
  1883. if (n < 4)
  1884. return 0;
  1885. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1886. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1887. return *w && *h;
  1888. }
  1889. /* The inverse of note_attr_flag() for a removed attribute. The cached
  1890. * bit used to outlive the attribute: `input.disabled = false` removed
  1891. * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
  1892. * once (Docusaurus' search box while its index loads) never took focus
  1893. * again. */
  1894. static void clear_attr_flag(struct br_node *n, const char *name)
  1895. {
  1896. if (str_eq(name, "data-collapse-target"))
  1897. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
  1898. else if (str_eq(name, "data-collapse-trigger"))
  1899. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
  1900. else if (str_eq(name, "is-open"))
  1901. n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
  1902. else if (str_eq(name, "disabled"))
  1903. n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
  1904. else if (str_eq(name, "open"))
  1905. n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
  1906. else if (str_eq(name, "inert"))
  1907. n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
  1908. }
  1909. static void note_attr_flag(struct br_node *n, const char *name)
  1910. {
  1911. if (str_eq(name, "data-collapse-target"))
  1912. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1913. else if (str_eq(name, "data-collapse-trigger"))
  1914. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1915. else if (str_eq(name, "is-open"))
  1916. n->attr_flags |= BR_ATTR_IS_OPEN;
  1917. else if (str_eq(name, "disabled"))
  1918. n->attr_flags |= BR_ATTR_DISABLED;
  1919. else if (str_eq(name, "open"))
  1920. n->attr_flags |= BR_ATTR_OPEN;
  1921. else if (str_eq(name, "inert"))
  1922. n->attr_flags |= BR_ATTR_INERT;
  1923. }
  1924. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1925. * second pass so the block stays contiguous even though the known attributes
  1926. * above also use the shared attr arena. */
  1927. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1928. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1929. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1930. * written. */
  1931. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1932. {
  1933. static const struct { const char *e; char c; } ents[] = {
  1934. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1935. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1936. { 0, 0 }
  1937. };
  1938. unsigned int i = 0, n = 0;
  1939. while (i < len) {
  1940. int k, done = 0;
  1941. if (s[i] == '&') {
  1942. for (k = 0; ents[k].e; k++) {
  1943. unsigned int el = (unsigned int)strlen(ents[k].e);
  1944. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1945. out[n++] = ents[k].c;
  1946. i += el;
  1947. done = 1;
  1948. break;
  1949. }
  1950. }
  1951. if (!done && i + 3 < len && s[i + 1] == '#') {
  1952. unsigned int j = i + 2, v = 0;
  1953. int hex = s[j] == 'x' || s[j] == 'X';
  1954. if (hex)
  1955. j++;
  1956. while (j < len && j < i + 10 &&
  1957. ((s[j] >= '0' && s[j] <= '9') ||
  1958. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1959. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1960. char c = s[j++];
  1961. v = v * (hex ? 16 : 10) +
  1962. (unsigned int)(c <= '9' ? c - '0' :
  1963. (c | 0x20) - 'a' + 10);
  1964. }
  1965. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1966. out[n++] = (char)v;
  1967. i = j + 1;
  1968. done = 1;
  1969. }
  1970. }
  1971. }
  1972. if (!done)
  1973. out[n++] = s[i++];
  1974. }
  1975. return n;
  1976. }
  1977. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1978. const char *s, unsigned int len)
  1979. {
  1980. unsigned int i = 0;
  1981. unsigned int start = d->attrs_used;
  1982. while (i < len) {
  1983. char name[32];
  1984. unsigned int nl = 0, vstart, vlen;
  1985. char quote = 0;
  1986. while (i < len && is_space(s[i]))
  1987. i++;
  1988. if (i >= len || s[i] == '/')
  1989. break;
  1990. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1991. char c = s[i++];
  1992. if (c >= 'A' && c <= 'Z')
  1993. c = (char)(c - 'A' + 'a');
  1994. if (nl + 1 < sizeof(name))
  1995. name[nl++] = c;
  1996. }
  1997. name[nl] = '\0';
  1998. while (i < len && is_space(s[i]))
  1999. i++;
  2000. if (i < len && s[i] == '=') {
  2001. i++;
  2002. while (i < len && is_space(s[i]))
  2003. i++;
  2004. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2005. quote = s[i++];
  2006. vstart = i;
  2007. while (i < len && s[i] != quote)
  2008. i++;
  2009. vlen = i - vstart;
  2010. if (i < len)
  2011. i++;
  2012. } else {
  2013. vstart = i;
  2014. while (i < len && !is_space(s[i]) && s[i] != '>')
  2015. i++;
  2016. vlen = i - vstart;
  2017. }
  2018. } else {
  2019. /* Boolean attribute: expose it with an empty value. */
  2020. vstart = 0;
  2021. vlen = 0;
  2022. }
  2023. if (!nl)
  2024. continue;
  2025. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  2026. d->truncated = 1;
  2027. break;
  2028. }
  2029. {
  2030. unsigned int k;
  2031. for (k = 0; k < nl; k++)
  2032. d->attrs[d->attrs_used++] = name[k];
  2033. d->attrs[d->attrs_used++] = '\0';
  2034. d->attrs_used += attr_unescape(s + vstart, vlen,
  2035. d->attrs + d->attrs_used);
  2036. d->attrs[d->attrs_used++] = '\0';
  2037. }
  2038. }
  2039. if (d->attrs_used > start) {
  2040. n->extra_off = start;
  2041. n->extra_len = (unsigned short)(d->attrs_used - start);
  2042. }
  2043. }
  2044. /* Parse attributes from `s` (between the tag name and '>'). */
  2045. static void parse_attrs(struct br_doc *d, struct br_node *n,
  2046. const char *s, unsigned int len)
  2047. {
  2048. unsigned int i = 0;
  2049. while (i < len) {
  2050. char name[32];
  2051. unsigned int nl = 0, vstart, vlen;
  2052. char quote = 0;
  2053. while (i < len && is_space(s[i]))
  2054. i++;
  2055. if (i >= len || s[i] == '/')
  2056. break;
  2057. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  2058. char c = s[i++];
  2059. if (c >= 'A' && c <= 'Z')
  2060. c = (char)(c - 'A' + 'a');
  2061. if (nl + 1 < sizeof(name))
  2062. name[nl++] = c;
  2063. }
  2064. name[nl] = '\0';
  2065. while (i < len && is_space(s[i]))
  2066. i++;
  2067. if (i >= len || s[i] != '=') {
  2068. if (str_eq(name, "hidden"))
  2069. n->flags |= BR_NODE_HIDDEN;
  2070. note_attr_flag(n, name);
  2071. continue;
  2072. }
  2073. i++;
  2074. while (i < len && is_space(s[i]))
  2075. i++;
  2076. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2077. quote = s[i++];
  2078. vstart = i;
  2079. while (i < len && s[i] != quote)
  2080. i++;
  2081. vlen = i - vstart;
  2082. if (i < len)
  2083. i++;
  2084. } else {
  2085. vstart = i;
  2086. while (i < len && !is_space(s[i]) && s[i] != '>')
  2087. i++;
  2088. vlen = i - vstart;
  2089. }
  2090. if (!nl)
  2091. continue;
  2092. if (str_eq(name, "hidden")) {
  2093. n->flags |= BR_NODE_HIDDEN;
  2094. continue;
  2095. }
  2096. note_attr_flag(n, name);
  2097. if (str_eq(name, "href"))
  2098. n->href_off = store_href(d, s + vstart, vlen);
  2099. else if (str_eq(name, "class"))
  2100. n->class_off = store_attr(d, s + vstart, vlen,
  2101. BR_CLASS_MAX);
  2102. else if (str_eq(name, "id"))
  2103. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2104. else if (str_eq(name, "style"))
  2105. n->style_off = store_attr(d, s + vstart, vlen,
  2106. BR_STYLE_ATTR_MAX);
  2107. else if (str_eq(name, "src"))
  2108. n->src_off = store_attr(d, s + vstart, vlen,
  2109. BR_URL_ATTR_MAX);
  2110. else if (str_eq(name, "alt"))
  2111. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2112. else if (str_eq(name, "color"))
  2113. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2114. else if (str_eq(name, "rel"))
  2115. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2116. else if (str_eq(name, "value"))
  2117. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2118. else if (str_eq(name, "placeholder") &&
  2119. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2120. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2121. else if (str_eq(name, "type") &&
  2122. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2123. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2124. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2125. else if (str_eq(name, "width")) {
  2126. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2127. if (vlen && s[vstart + vlen - 1] == '%')
  2128. n->width_pct = 1;
  2129. } else if (str_eq(name, "height")) {
  2130. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2131. if (vlen && s[vstart + vlen - 1] == '%')
  2132. n->height_pct = 1;
  2133. } else if (str_eq(name, "colspan")) {
  2134. unsigned int v = parse_uint(s + vstart, vlen);
  2135. if (v < 1)
  2136. v = 1;
  2137. if (v > 16)
  2138. v = 16;
  2139. n->colspan = (unsigned char)v;
  2140. } else if (str_eq(name, "rowspan")) {
  2141. unsigned int v = parse_uint(s + vstart, vlen);
  2142. if (v < 1)
  2143. v = 1;
  2144. if (v > 16)
  2145. v = 16;
  2146. n->rowspan = (unsigned char)v;
  2147. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2148. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2149. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2150. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2151. } else if (str_eq(name, "x2")) {
  2152. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2153. } else if (str_eq(name, "y2")) {
  2154. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2155. } else if (str_eq(name, "rx")) {
  2156. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2157. } else if (str_eq(name, "d")) {
  2158. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2159. } else if (str_eq(name, "viewbox")) {
  2160. unsigned short vw = 0, vh = 0;
  2161. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2162. n->width_attr = vw;
  2163. n->height_attr = vh;
  2164. n->flags |= BR_NODE_VIEWBOX;
  2165. }
  2166. } else if (str_eq(name, "text-anchor")) {
  2167. if (contains_ci_len(s + vstart, vlen, "middle"))
  2168. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2169. else if (contains_ci_len(s + vstart, vlen, "end"))
  2170. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2171. }
  2172. }
  2173. parse_attrs_generic(d, n, s, len);
  2174. }
  2175. static int str_eq(const char *a, const char *b)
  2176. {
  2177. unsigned int i = 0;
  2178. while (a[i] && b[i] && a[i] == b[i])
  2179. i++;
  2180. return a[i] == '\0' && b[i] == '\0';
  2181. }
  2182. /* ---- CSS ---- */
  2183. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2184. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2185. {
  2186. return parse_color(s, len, out);
  2187. }
  2188. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2189. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2190. {
  2191. while (len && is_space(*s)) {
  2192. s++;
  2193. len--;
  2194. }
  2195. while (len && is_space(s[len - 1]))
  2196. len--;
  2197. if (!len)
  2198. return 0;
  2199. if (len == 4 && ci_eq_n(s, "none", 4))
  2200. return BR_PAINT_NONE;
  2201. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2202. return BR_PAINT_CURRENT;
  2203. if (len > 4 && ci_eq_n(s, "url(", 4))
  2204. return BR_PAINT_URL;
  2205. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2206. return BR_PAINT_NONE;
  2207. if (parse_color(s, len, c))
  2208. return BR_PAINT_COLOR;
  2209. return 0;
  2210. }
  2211. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2212. static int svg_len16(const char *s, unsigned int len)
  2213. {
  2214. unsigned int i = 0;
  2215. int v = 0, frac = 0, digits = 0;
  2216. while (i < len && is_space(s[i]))
  2217. i++;
  2218. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2219. if (v < 4000)
  2220. v = v * 10 + (s[i] - '0');
  2221. i++;
  2222. digits++;
  2223. }
  2224. if (i < len && s[i] == '.') {
  2225. int scale = 1;
  2226. i++;
  2227. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2228. if (scale < 10000) {
  2229. frac = frac * 10 + (s[i] - '0');
  2230. scale *= 10;
  2231. }
  2232. i++;
  2233. digits++;
  2234. }
  2235. frac = frac * 16 / scale;
  2236. }
  2237. if (!digits)
  2238. return -1;
  2239. return v * 16 + frac;
  2240. }
  2241. /* Inside an <svg> subtree (including the <svg> element itself). */
  2242. static int in_svg(const struct br_doc *d, unsigned int node)
  2243. {
  2244. unsigned int k;
  2245. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2246. if (d->nodes[node].tag == BR_TAG_SVG)
  2247. return 1;
  2248. node = d->nodes[node].parent;
  2249. }
  2250. return 0;
  2251. }
  2252. enum {
  2253. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2254. PROP_BG_REPEAT,
  2255. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2256. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2257. PROP_FONT_PX, PROP_FONT_UNIT,
  2258. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2259. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2260. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2261. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2262. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2263. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2264. PROP_BORDER_W, PROP_BORDER_COLOR,
  2265. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2266. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2267. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2268. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2269. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2270. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2271. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2272. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2273. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2274. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2275. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2276. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2277. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2278. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2279. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2280. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2281. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2282. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2283. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2284. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2285. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2286. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2287. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2288. PROP_ZINDEX, PROP_ZSET,
  2289. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2290. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2291. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2292. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2293. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2294. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2295. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2296. PROP_COUNT
  2297. };
  2298. struct color_name {
  2299. const char *name;
  2300. unsigned short rgb;
  2301. };
  2302. static const struct color_name color_names[] = {
  2303. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2304. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2305. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2306. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2307. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2308. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2309. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2310. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2311. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2312. { "darkgrey", 0x528A }, { 0, 0 }
  2313. };
  2314. static int hexval(char c)
  2315. {
  2316. if (c >= '0' && c <= '9')
  2317. return c - '0';
  2318. if (c >= 'a' && c <= 'f')
  2319. return c - 'a' + 10;
  2320. if (c >= 'A' && c <= 'F')
  2321. return c - 'A' + 10;
  2322. return -1;
  2323. }
  2324. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2325. {
  2326. unsigned int i;
  2327. for (i = 0; color_names[i].name; i++) {
  2328. unsigned int j, n = s_len(color_names[i].name);
  2329. if (n != len)
  2330. continue;
  2331. for (j = 0; j < n; j++) {
  2332. char c = s[j];
  2333. if (c >= 'A' && c <= 'Z')
  2334. c = (char)(c - 'A' + 'a');
  2335. if (c != color_names[i].name[j])
  2336. break;
  2337. }
  2338. if (j == n) {
  2339. *out = color_names[i].rgb;
  2340. return 1;
  2341. }
  2342. }
  2343. return 0;
  2344. }
  2345. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2346. * over white; the render target is a white page. */
  2347. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2348. {
  2349. unsigned int i = 0;
  2350. while (i < len && is_space(s[i]))
  2351. i++;
  2352. if (i < len && s[i] == '#') {
  2353. int d[6], nd = 0;
  2354. i++;
  2355. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2356. d[nd++] = hexval(s[i++]);
  2357. if (nd == 3) {
  2358. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2359. unsigned int b = (unsigned int)d[2];
  2360. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2361. ((g * 17 >> 2) << 5) |
  2362. (b * 17 >> 3));
  2363. return 1;
  2364. }
  2365. if (nd >= 6) {
  2366. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2367. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2368. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2369. *out = (unsigned short)(((r >> 3) << 11) |
  2370. ((g >> 2) << 5) | (b >> 3));
  2371. return 1;
  2372. }
  2373. return 0;
  2374. }
  2375. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2376. unsigned int v[3], k = 0, a = 255;
  2377. while (i < len && s[i] != '(')
  2378. i++;
  2379. if (i >= len)
  2380. return 0;
  2381. i++;
  2382. while (k < 3 && i < len) {
  2383. unsigned int x = 0;
  2384. int percent = 0;
  2385. while (i < len && (is_space(s[i]) || s[i] == ','))
  2386. i++;
  2387. if (i >= len || s[i] < '0' || s[i] > '9')
  2388. return 0;
  2389. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2390. x = x * 10 + (unsigned int)(s[i] - '0');
  2391. i++;
  2392. }
  2393. if (i < len && s[i] == '%') {
  2394. percent = 1;
  2395. i++;
  2396. }
  2397. if (percent)
  2398. x = x * 255 / 100;
  2399. if (x > 255)
  2400. x = 255;
  2401. v[k++] = x;
  2402. }
  2403. /* Optional alpha in both syntaxes:
  2404. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2405. {
  2406. unsigned int whole = 0, frac = 0, scale = 1;
  2407. int percent = 0;
  2408. while (i < len && (is_space(s[i]) || s[i] == ','))
  2409. i++;
  2410. if (i < len && s[i] == '/') {
  2411. i++;
  2412. while (i < len && is_space(s[i]))
  2413. i++;
  2414. }
  2415. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2416. * used to read as opaque black. */
  2417. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2418. (s[i] == '.' && i + 1 < len &&
  2419. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2420. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2421. whole = whole * 10 +
  2422. (unsigned int)(s[i] - '0');
  2423. i++;
  2424. }
  2425. if (i < len && s[i] == '.') {
  2426. i++;
  2427. while (i < len && s[i] >= '0' &&
  2428. s[i] <= '9') {
  2429. if (scale < 1000) {
  2430. frac = frac * 10 +
  2431. (unsigned int)(s[i] - '0');
  2432. scale *= 10;
  2433. }
  2434. i++;
  2435. }
  2436. }
  2437. while (i < len && is_space(s[i]))
  2438. i++;
  2439. if (i < len && s[i] == '%')
  2440. percent = 1;
  2441. if (percent)
  2442. a = whole > 100 ? 255 :
  2443. whole * 255 / 100;
  2444. else if (whole >= 1)
  2445. a = 255;
  2446. else
  2447. a = frac * 255 / scale;
  2448. }
  2449. }
  2450. if (k == 3) {
  2451. unsigned int r = v[0], g = v[1], b = v[2];
  2452. if (a < 255) {
  2453. r = (r * a + 255u * (255u - a)) / 255u;
  2454. g = (g * a + 255u * (255u - a)) / 255u;
  2455. b = (b * a + 255u * (255u - a)) / 255u;
  2456. }
  2457. *out = (unsigned short)(((r >> 3) << 11) |
  2458. ((g >> 2) << 5) | (b >> 3));
  2459. return 1;
  2460. }
  2461. return 0;
  2462. }
  2463. {
  2464. unsigned int j = i;
  2465. while (i < len && !is_space(s[i]))
  2466. i++;
  2467. return named_color(s + j, i - j, out);
  2468. }
  2469. }
  2470. /* Fully transparent colours have no RGB565 representation. They are common
  2471. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2472. * only on hover; painting them black would draw an outline around every
  2473. * pill and card. */
  2474. static int color_is_transparent(const char *s, unsigned int len)
  2475. {
  2476. unsigned int i = 0, n = 0;
  2477. int last = 0, prev = 0, d;
  2478. while (i < len && is_space(s[i]))
  2479. i++;
  2480. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2481. return 1;
  2482. if (i >= len || s[i] != '#')
  2483. return 0;
  2484. i++;
  2485. while (i < len && hexval(s[i]) >= 0) {
  2486. d = hexval(s[i]);
  2487. prev = last;
  2488. last = d;
  2489. n++;
  2490. i++;
  2491. }
  2492. while (i < len && is_space(s[i]))
  2493. i++;
  2494. if (i != len)
  2495. return 0;
  2496. if (n == 4)
  2497. return last == 0;
  2498. if (n == 8)
  2499. return prev == 0 && last == 0;
  2500. return 0;
  2501. }
  2502. static int register_bg_url(const char *s, unsigned int len)
  2503. {
  2504. char tmp[BR_BG_URL_MAX + 1];
  2505. unsigned int i, n = 0;
  2506. char q = 0;
  2507. while (len && is_space(*s)) {
  2508. s++;
  2509. len--;
  2510. }
  2511. while (len && is_space(s[len - 1]))
  2512. len--;
  2513. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2514. s[len - 1] == s[0]) {
  2515. q = s[0];
  2516. s++;
  2517. len -= 2;
  2518. }
  2519. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2520. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2521. if (q && s[i] == '\\' && i + 1 < len) {
  2522. i++;
  2523. if (s[i] == '\n')
  2524. continue;
  2525. }
  2526. tmp[n++] = s[i];
  2527. }
  2528. tmp[n] = '\0';
  2529. if (!n)
  2530. return 0;
  2531. for (i = 0; i < bg_url_count; i++)
  2532. if (!strcmp(bg_urls[i], tmp))
  2533. return (int)i + 1;
  2534. if (bg_url_count >= BR_BG_URLS ||
  2535. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2536. return 0;
  2537. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2538. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2539. bg_url_pool_used += n + 1;
  2540. return (int)(++bg_url_count);
  2541. }
  2542. /* Length values for margins/padding: px, ignore percentages/units. */
  2543. static unsigned int viewport_w;
  2544. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2545. {
  2546. unsigned int i = *pos;
  2547. int sign = 1;
  2548. long v = 0; /* hundredths of a unit before conversion */
  2549. int digits = 0, frac = 0, unit = 0;
  2550. while (i < len && is_space(s[i]))
  2551. i++;
  2552. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2553. if (s[i] == '-')
  2554. sign = -1;
  2555. i++;
  2556. }
  2557. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2558. v = v * 10 + (s[i] - '0');
  2559. digits = 1;
  2560. i++;
  2561. }
  2562. if (i < len && s[i] == '.') {
  2563. i++;
  2564. /* Minified CSS writes ".5rem" without a leading zero. */
  2565. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2566. if (frac < 2) {
  2567. v = v * 10 + (s[i] - '0');
  2568. frac++;
  2569. digits = 1;
  2570. }
  2571. i++;
  2572. }
  2573. }
  2574. while (frac < 2) {
  2575. v *= 10;
  2576. frac++;
  2577. }
  2578. while (frac > 2) {
  2579. v /= 10;
  2580. frac--;
  2581. }
  2582. if (i < len && s[i] == '%')
  2583. unit = 2;
  2584. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2585. s[i + 2] == 'm')
  2586. unit = 1;
  2587. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2588. unit = 3;
  2589. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2590. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2591. s[i] == 'a' || s[i] == 'd'))
  2592. i++;
  2593. *pos = i;
  2594. if (!digits)
  2595. return 0;
  2596. if (unit == 1)
  2597. v *= root_font_px;
  2598. else if (unit == 3)
  2599. v *= root_font_px;
  2600. else if (unit == 2)
  2601. v = v * (long)viewport_w / 100;
  2602. v /= 100;
  2603. v *= sign;
  2604. if (v < -2048)
  2605. v = -2048;
  2606. if (v > 2048)
  2607. v = 2048;
  2608. return (int)v;
  2609. }
  2610. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2611. * treats it as a zero-length token; margins need to keep it so block layout
  2612. * can centre a box with a definite width. */
  2613. #define CSS_LEN_AUTO (-32768)
  2614. /* A box length with its unit preserved for layout-time resolution:
  2615. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2616. * rem and whole pixels for px. */
  2617. static int parse_css_len(const char *s, unsigned int len,
  2618. unsigned int *pos, int *unit)
  2619. {
  2620. unsigned int i = *pos;
  2621. int sign = 1;
  2622. long v = 0;
  2623. int digits = 0, frac = 0;
  2624. *unit = 0;
  2625. while (i < len && is_space(s[i]))
  2626. i++;
  2627. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2628. if (s[i] == '-')
  2629. sign = -1;
  2630. i++;
  2631. }
  2632. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2633. v = v * 10 + (s[i] - '0');
  2634. digits = 1;
  2635. i++;
  2636. }
  2637. if (i < len && s[i] == '.') {
  2638. i++;
  2639. /* Leading-dot fractions are common in minified CSS
  2640. * (".5rem", ".3rem"). */
  2641. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2642. if (frac < 2) {
  2643. v = v * 10 + (s[i] - '0');
  2644. frac++;
  2645. digits = 1;
  2646. }
  2647. i++;
  2648. }
  2649. }
  2650. while (frac < 2) {
  2651. v *= 10;
  2652. frac++;
  2653. }
  2654. if (!digits) {
  2655. *pos = i;
  2656. return 0;
  2657. }
  2658. if (i < len && s[i] == '%') {
  2659. *unit = 1;
  2660. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2661. s[i + 2] == 'm') {
  2662. *unit = 3;
  2663. i += 3;
  2664. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2665. *unit = 2;
  2666. i += 2;
  2667. } else {
  2668. v /= 100;
  2669. }
  2670. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2671. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2672. s[i] == 'a' || s[i] == 'd'))
  2673. i++;
  2674. *pos = i;
  2675. v *= sign;
  2676. if (v < -32768)
  2677. v = -32768;
  2678. if (v > 32767)
  2679. v = 32767;
  2680. return (int)v;
  2681. }
  2682. static int parse_margin_len(const char *s, unsigned int len,
  2683. unsigned int *pos, int *unit)
  2684. {
  2685. unsigned int i = *pos;
  2686. while (i < len && is_space(s[i]))
  2687. i++;
  2688. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2689. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2690. *pos = i + 4;
  2691. *unit = 4;
  2692. return CSS_LEN_AUTO;
  2693. }
  2694. return parse_css_len(s, len, pos, unit);
  2695. }
  2696. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2697. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2698. static int parse_margin_calc(const char *s, unsigned int len,
  2699. unsigned int *pos, int *pct, int *px)
  2700. {
  2701. unsigned int i = *pos, j;
  2702. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2703. return 0;
  2704. i += 5;
  2705. j = i;
  2706. while (j < len && s[j] != ')')
  2707. j++;
  2708. if (j >= len)
  2709. return 0;
  2710. if (!parse_calc(s + i, j - i, pct, px))
  2711. return 0;
  2712. *pos = j + 1;
  2713. return 1;
  2714. }
  2715. /* calc(P% +/- Npx) for the common width/margin forms. */
  2716. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2717. {
  2718. unsigned int i = 0;
  2719. int sign = 1;
  2720. int have = 0;
  2721. *pct = 0;
  2722. *px = 0;
  2723. while (i < len) {
  2724. unsigned int j;
  2725. long whole = 0, frac = 0, scale = 1;
  2726. int digits = 0;
  2727. while (i < len && is_space(s[i]))
  2728. i++;
  2729. if (i >= len)
  2730. break;
  2731. if (s[i] == '+' || s[i] == '-') {
  2732. sign = s[i] == '-' ? -1 : 1;
  2733. i++;
  2734. continue;
  2735. }
  2736. if (s[i] < '0' || s[i] > '9') {
  2737. if (s[i] == '.') {
  2738. /* fall through to the number parser */
  2739. } else {
  2740. i++;
  2741. continue;
  2742. }
  2743. }
  2744. j = i;
  2745. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2746. whole = whole * 10 + (s[j] - '0');
  2747. digits = 1;
  2748. j++;
  2749. }
  2750. if (j < len && s[j] == '.') {
  2751. j++;
  2752. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2753. frac = frac * 10 + (s[j] - '0');
  2754. scale *= 10;
  2755. j++;
  2756. }
  2757. }
  2758. if (!digits) {
  2759. i = j + 1;
  2760. continue;
  2761. }
  2762. while (j < len && is_space(s[j]))
  2763. j++;
  2764. if (j < len && s[j] == '%') {
  2765. *pct = sign * (int)(whole + (scale > 1 ?
  2766. frac * 100 / scale : 0));
  2767. have = 1;
  2768. j++;
  2769. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2770. *px = sign * (int)whole;
  2771. have = 1;
  2772. j += 2;
  2773. }
  2774. i = j;
  2775. while (i < len && !is_space(s[i]))
  2776. i++;
  2777. sign = 1;
  2778. }
  2779. return have;
  2780. }
  2781. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2782. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2783. unsigned char *kind)
  2784. {
  2785. unsigned int i = 0;
  2786. unsigned long v = 0;
  2787. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2788. *val = 0;
  2789. *kind = 0;
  2790. while (i < len && is_space(s[i]))
  2791. i++;
  2792. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2793. return 0;
  2794. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2795. return 0;
  2796. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2797. v = v * 10 + (unsigned int)(s[i] - '0');
  2798. digits = 1;
  2799. i++;
  2800. }
  2801. if (i < len && s[i] == '.') {
  2802. i++;
  2803. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2804. if (frac < 2) {
  2805. v = v * 10 + (unsigned int)(s[i] - '0');
  2806. frac++;
  2807. digits = 1;
  2808. }
  2809. i++;
  2810. }
  2811. }
  2812. while (frac < 2) {
  2813. v *= 10;
  2814. frac++;
  2815. }
  2816. while (frac > 2) {
  2817. v /= 10;
  2818. frac--;
  2819. }
  2820. if (!digits)
  2821. return 0;
  2822. while (i < len && is_space(s[i]))
  2823. i++;
  2824. if (i < len && s[i] == '%')
  2825. pct = 1;
  2826. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2827. s[i + 2] == 'm')
  2828. rem = 1;
  2829. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2830. rem = 2;
  2831. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2832. vw = 1;
  2833. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2834. ; /* px: already the default scale */
  2835. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2836. bad_unit = 1;
  2837. if (bad_unit)
  2838. return 0;
  2839. if (rem == 1 || rem == 2) {
  2840. if (v > 65535)
  2841. v = 65535;
  2842. *val = (unsigned short)v;
  2843. *kind = rem == 1 ? 5 : 4;
  2844. return 1;
  2845. }
  2846. v /= 100;
  2847. if (vw)
  2848. v = v * viewport_w / 100;
  2849. if (pct) {
  2850. if (v > 100)
  2851. v = 100;
  2852. *kind = 2;
  2853. } else {
  2854. if (v > 2048)
  2855. v = 2048;
  2856. *kind = 1;
  2857. }
  2858. *val = (unsigned short)v;
  2859. return 1;
  2860. }
  2861. static int parse_font_px(const char *s, unsigned int len)
  2862. {
  2863. unsigned int i = 0;
  2864. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2865. int v1000, px;
  2866. int is_px = 1, is_rem = 0, is_pct = 0;
  2867. while (i < len && is_space(s[i]))
  2868. i++;
  2869. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2870. return 24;
  2871. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2872. return 32;
  2873. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2874. return 14;
  2875. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2876. whole = whole * 10 + (s[i] - '0');
  2877. digits = 1;
  2878. i++;
  2879. }
  2880. if (i < len && s[i] == '.') {
  2881. i++;
  2882. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2883. if (fscale < 1000) {
  2884. frac = frac * 10 + (s[i] - '0');
  2885. fscale *= 10;
  2886. }
  2887. digits = 1;
  2888. i++;
  2889. }
  2890. }
  2891. if (!digits)
  2892. return 0;
  2893. while (i < len && is_space(s[i]))
  2894. i++;
  2895. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2896. is_rem = 1;
  2897. is_px = 0;
  2898. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2899. is_rem = 1;
  2900. is_px = 0;
  2901. } else if (i < len && s[i] == '%') {
  2902. is_pct = 1;
  2903. is_px = 0;
  2904. }
  2905. v1000 = whole * 1000 + frac * 1000 / fscale;
  2906. if (is_rem)
  2907. px = v1000 * root_font_px / 1000;
  2908. else if (is_pct)
  2909. px = v1000 * root_font_px / 100000;
  2910. else if (is_px)
  2911. px = v1000 / 1000;
  2912. else
  2913. px = v1000 / 1000;
  2914. if (px < 8)
  2915. px = 8;
  2916. if (px > 72)
  2917. px = 72;
  2918. return px;
  2919. }
  2920. /* font-size keeping the unit so em/% resolve against the parent face. */
  2921. static int parse_font_size(const char *s, unsigned int len,
  2922. unsigned short *raw, unsigned char *unit)
  2923. {
  2924. unsigned int i = 0;
  2925. int whole = 0, frac = 0, scale = 1, digits = 0;
  2926. int v100;
  2927. *raw = 0;
  2928. *unit = 0;
  2929. while (i < len && is_space(s[i]))
  2930. i++;
  2931. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2932. *raw = 24;
  2933. *unit = 1;
  2934. return 1;
  2935. }
  2936. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2937. *raw = 32;
  2938. *unit = 1;
  2939. return 1;
  2940. }
  2941. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2942. *raw = 14;
  2943. *unit = 1;
  2944. return 1;
  2945. }
  2946. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2947. whole = whole * 10 + (s[i] - '0');
  2948. digits = 1;
  2949. i++;
  2950. }
  2951. if (i < len && s[i] == '.') {
  2952. i++;
  2953. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2954. frac = frac * 10 + (s[i] - '0');
  2955. scale *= 10;
  2956. i++;
  2957. }
  2958. }
  2959. if (!digits && !frac)
  2960. return 0;
  2961. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2962. while (i < len && is_space(s[i]))
  2963. i++;
  2964. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2965. *raw = (unsigned short)v100;
  2966. *unit = 3;
  2967. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2968. *raw = (unsigned short)v100;
  2969. *unit = 2;
  2970. } else if (i < len && s[i] == '%') {
  2971. /* Keep hundredths: 62.5% is a common root size and truncating
  2972. * it to 62% makes every rem 1px too small. */
  2973. *raw = (unsigned short)v100;
  2974. *unit = 4;
  2975. } else {
  2976. *raw = (unsigned short)(v100 / 100);
  2977. *unit = 1;
  2978. }
  2979. return 1;
  2980. }
  2981. static int parse_line_height(const char *s, unsigned int len,
  2982. unsigned short *px, unsigned char *pct)
  2983. {
  2984. unsigned int i = 0;
  2985. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2986. int v1000;
  2987. int is_px = 0, is_pct = 0;
  2988. *px = 0;
  2989. *pct = 0;
  2990. while (i < len && is_space(s[i]))
  2991. i++;
  2992. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2993. return 0;
  2994. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2995. whole = whole * 10 + (s[i] - '0');
  2996. digits = 1;
  2997. i++;
  2998. }
  2999. if (i < len && s[i] == '.') {
  3000. i++;
  3001. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  3002. frac = frac * 10 + (s[i] - '0');
  3003. fscale *= 10;
  3004. digits = 1;
  3005. i++;
  3006. }
  3007. }
  3008. if (!digits)
  3009. return 0;
  3010. while (i < len && is_space(s[i]))
  3011. i++;
  3012. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3013. is_px = 1;
  3014. else if (i < len && s[i] == '%')
  3015. is_pct = 1;
  3016. v1000 = whole * 1000 + frac * 1000 / fscale;
  3017. /* rem is an absolute length (the root font is known by the time
  3018. * rules are parsed, as for font-size); em behaves like a multiplier. */
  3019. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3020. v1000 = v1000 * root_font_px;
  3021. is_px = 1;
  3022. }
  3023. if (is_px) {
  3024. int v = v1000 / 1000;
  3025. if (v < 8)
  3026. v = 8;
  3027. if (v > 160)
  3028. v = 160;
  3029. *px = (unsigned short)v;
  3030. } else {
  3031. int v = v1000 / 10; /* unitless multiplier -> percent */
  3032. if (is_pct)
  3033. v = v1000 / 1000;
  3034. if (v < 50)
  3035. v = 50;
  3036. if (v > 255) /* stored in an unsigned char */
  3037. v = 255;
  3038. *pct = (unsigned char)v;
  3039. }
  3040. return 1;
  3041. }
  3042. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  3043. {
  3044. unsigned int i, j, n = s_len(needle);
  3045. for (i = 0; i + n <= len; i++) {
  3046. for (j = 0; j < n; j++) {
  3047. char a = s[i + j];
  3048. if (a >= 'A' && a <= 'Z')
  3049. a = (char)(a - 'A' + 'a');
  3050. if (a != needle[j])
  3051. break;
  3052. }
  3053. if (j == n)
  3054. return 1;
  3055. }
  3056. return 0;
  3057. }
  3058. /* Pull a colour out of a border/background shorthand. */
  3059. /* Background shorthands can hold several layers before the final solid
  3060. * colour. The colour is conventionally the last token (e.g. a stack of
  3061. * gradients followed by var(--bg)), so take the rightmost colour rather
  3062. * than the first gradient stop. */
  3063. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3064. {
  3065. int last = -1;
  3066. unsigned int i;
  3067. for (i = 0; i < len; i++) {
  3068. if (s[i] == '#')
  3069. last = (int)i;
  3070. else if ((s[i] == 'r' || s[i] == 'R') &&
  3071. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3072. last = (int)i;
  3073. }
  3074. if (last >= 0)
  3075. return parse_color(s + last, len - (unsigned int)last, c);
  3076. while (i > 0) {
  3077. unsigned int st, e;
  3078. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3079. s[i - 1] == ')' || s[i - 1] == '('))
  3080. i--;
  3081. e = i;
  3082. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3083. s[i - 1] != ')' && s[i - 1] != '(')
  3084. i--;
  3085. st = i;
  3086. if (e > st && parse_color(s + st, e - st, c))
  3087. return 1;
  3088. }
  3089. return 0;
  3090. }
  3091. static int parse_display(const char *s, unsigned int len)
  3092. {
  3093. unsigned int i;
  3094. while (len && is_space(s[0])) { s++; len--; }
  3095. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3096. ;
  3097. if (i == 4 && ci_eq_n(s, "none", 4))
  3098. return BR_DISPLAY_NONE;
  3099. if (i == 5 && ci_eq_n(s, "block", 5))
  3100. return BR_DISPLAY_BLOCK;
  3101. if (i == 6 && ci_eq_n(s, "inline", 6))
  3102. return BR_DISPLAY_INLINE;
  3103. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3104. return BR_DISPLAY_INLINE_BLOCK;
  3105. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3106. return BR_DISPLAY_BLOCK;
  3107. if (i == 4 && ci_eq_n(s, "flex", 4))
  3108. return BR_DISPLAY_FLEX;
  3109. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3110. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3111. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3112. * block instead of an inline box. */
  3113. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3114. return BR_DISPLAY_BLOCK;
  3115. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3116. return BR_DISPLAY_BLOCK;
  3117. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3118. return BR_DISPLAY_FLEX;
  3119. if (i == 5 && ci_eq_n(s, "table", 5))
  3120. return BR_DISPLAY_BLOCK;
  3121. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3122. return BR_DISPLAY_BLOCK;
  3123. /* Grid shells are common on news/wiki pages, but the two-column
  3124. * heuristic is not reliable for their nested area templates yet.
  3125. * Collapse them to block flow so the content stays readable. */
  3126. if (i == 4 && ci_eq_n(s, "grid", 4))
  3127. return BR_DISPLAY_BLOCK;
  3128. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3129. return BR_DISPLAY_BLOCK;
  3130. return BR_DISPLAY_INLINE;
  3131. }
  3132. static void decl_add(struct br_decl *out, unsigned int *nout,
  3133. unsigned char prop, int val, int important)
  3134. {
  3135. if (*nout >= BR_DECLS)
  3136. return;
  3137. out[*nout].prop = prop;
  3138. out[*nout].val = (short)val;
  3139. out[*nout].important = (unsigned char)(important != 0);
  3140. (*nout)++;
  3141. }
  3142. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3143. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3144. unsigned int *start, unsigned int *end)
  3145. {
  3146. unsigned int i = *pos;
  3147. while (i < len && is_space(s[i]))
  3148. i++;
  3149. if (i >= len)
  3150. return 0;
  3151. *start = i;
  3152. while (i < len && !is_space(s[i]))
  3153. i++;
  3154. *end = i;
  3155. *pos = i;
  3156. return 1;
  3157. }
  3158. static int bg_number(const char *s, unsigned int len, signed short *val,
  3159. unsigned char *kind)
  3160. {
  3161. unsigned int i = 0;
  3162. long v = 0;
  3163. int neg = 0, digits = 0, pct = 0;
  3164. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3165. neg = s[i] == '-';
  3166. i++;
  3167. }
  3168. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3169. v = v * 10 + (s[i] - '0');
  3170. digits = 1;
  3171. i++;
  3172. }
  3173. if (i < len && s[i] == '.') {
  3174. i++;
  3175. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3176. digits = 1;
  3177. i++;
  3178. }
  3179. }
  3180. if (!digits)
  3181. return 0;
  3182. if (i < len && s[i] == '%') {
  3183. pct = 1;
  3184. i++;
  3185. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3186. i += 2;
  3187. }
  3188. if (i != len)
  3189. return 0;
  3190. if (neg)
  3191. v = -v;
  3192. if (v < -4096)
  3193. v = -4096;
  3194. if (v > 4096)
  3195. v = 4096;
  3196. *val = (signed short)v;
  3197. *kind = (unsigned char)(pct ? 1 : 0);
  3198. return 1;
  3199. }
  3200. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3201. int *vertical)
  3202. {
  3203. *vertical = 0;
  3204. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3205. *val = 0;
  3206. return 1;
  3207. }
  3208. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3209. *val = 100;
  3210. return 1;
  3211. }
  3212. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3213. *val = 0;
  3214. *vertical = 1;
  3215. return 1;
  3216. }
  3217. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3218. *val = 100;
  3219. *vertical = 1;
  3220. return 1;
  3221. }
  3222. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3223. *val = 50;
  3224. return 1;
  3225. }
  3226. return 0;
  3227. }
  3228. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3229. const char *s, unsigned int len, int important)
  3230. {
  3231. unsigned int pos = 0, ts, te;
  3232. signed short val[2] = { 50, 50 };
  3233. unsigned char kind[2] = { 1, 1 };
  3234. int vert[2] = { 0, 0 }, n = 0;
  3235. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3236. signed short v;
  3237. int is_vert;
  3238. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3239. val[n] = v;
  3240. kind[n] = 1;
  3241. vert[n] = is_vert;
  3242. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3243. val[n] = v;
  3244. vert[n] = 0;
  3245. } else {
  3246. n++;
  3247. continue;
  3248. }
  3249. n++;
  3250. }
  3251. if (n == 1) {
  3252. if (vert[0]) {
  3253. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3254. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3255. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3256. important);
  3257. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3258. } else {
  3259. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3260. important);
  3261. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3262. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3263. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3264. }
  3265. return;
  3266. }
  3267. if (n >= 2) {
  3268. int x = vert[0] && !vert[1] ? 1 : 0;
  3269. int y = 1 - x;
  3270. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3271. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3272. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3273. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3274. }
  3275. }
  3276. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3277. const char *s, unsigned int len, int important)
  3278. {
  3279. unsigned int pos = 0, ts, te;
  3280. signed short w = 0, h = 0;
  3281. unsigned char kw = 0, kh = 0;
  3282. int n = 0;
  3283. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3284. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3285. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3286. return;
  3287. }
  3288. if (n == 0 && te - ts == 7 &&
  3289. ci_eq_n(s + ts, "contain", 7)) {
  3290. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3291. return;
  3292. }
  3293. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3294. n++;
  3295. continue;
  3296. }
  3297. {
  3298. signed short v;
  3299. unsigned char k;
  3300. if (bg_number(s + ts, te - ts, &v, &k)) {
  3301. if (n == 0) {
  3302. w = v;
  3303. kw = k;
  3304. } else {
  3305. h = v;
  3306. kh = k;
  3307. }
  3308. }
  3309. }
  3310. n++;
  3311. }
  3312. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3313. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3314. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3315. (void)kh;
  3316. }
  3317. /* Parse the `position / size` part of the background shorthand. The image
  3318. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3319. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3320. const char *v, unsigned int len,
  3321. int important)
  3322. {
  3323. unsigned int i, slash = 0, depth = 0, p, first, count;
  3324. char q = 0;
  3325. signed short nv;
  3326. int vert;
  3327. unsigned char kind;
  3328. for (i = 0; i < len; i++) {
  3329. char c = v[i];
  3330. if (q) {
  3331. if (c == q)
  3332. q = 0;
  3333. continue;
  3334. }
  3335. if (c == '\'' || c == '"') {
  3336. q = c;
  3337. continue;
  3338. }
  3339. if (c == '(') {
  3340. depth++;
  3341. continue;
  3342. }
  3343. if (c == ')') {
  3344. if (depth)
  3345. depth--;
  3346. continue;
  3347. }
  3348. if (c == '/' && !depth) {
  3349. slash = i;
  3350. break;
  3351. }
  3352. }
  3353. if (!slash)
  3354. return;
  3355. /* Up to two position tokens immediately before the slash. */
  3356. p = slash;
  3357. first = slash;
  3358. count = 0;
  3359. while (count < 2) {
  3360. unsigned int e;
  3361. while (p > 0 && is_space(v[p - 1]))
  3362. p--;
  3363. e = p;
  3364. while (p > 0 && !is_space(v[p - 1]))
  3365. p--;
  3366. if (e == p)
  3367. break;
  3368. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3369. bg_number(v + p, e - p, &nv, &kind)) {
  3370. first = p;
  3371. count++;
  3372. } else {
  3373. break;
  3374. }
  3375. }
  3376. if (count)
  3377. add_bg_position(out, nout, v + first, slash - first,
  3378. important);
  3379. /* Up to two size tokens after the slash. */
  3380. p = slash + 1;
  3381. while (p < len && is_space(v[p]))
  3382. p++;
  3383. first = p;
  3384. count = 0;
  3385. while (count < 2) {
  3386. unsigned int ts = p;
  3387. while (p < len && !is_space(v[p]))
  3388. p++;
  3389. if (p == ts)
  3390. break;
  3391. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3392. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3393. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3394. bg_number(v + ts, p - ts, &nv, &kind)) {
  3395. count++;
  3396. while (p < len && is_space(v[p]))
  3397. p++;
  3398. } else {
  3399. break;
  3400. }
  3401. }
  3402. if (count)
  3403. add_bg_size(out, nout, v + first, p - first, important);
  3404. }
  3405. static int parse_fixed_100(const char *s, unsigned int len,
  3406. unsigned int *pos, signed short *out)
  3407. {
  3408. unsigned int i = *pos;
  3409. long whole = 0, frac = 0;
  3410. int neg = 0, digits = 0, fd = 0;
  3411. while (i < len && is_space(s[i]))
  3412. i++;
  3413. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3414. neg = s[i] == '-';
  3415. i++;
  3416. }
  3417. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3418. whole = whole * 10 + (s[i] - '0');
  3419. if (whole > 327)
  3420. whole = 327;
  3421. digits = 1;
  3422. i++;
  3423. }
  3424. if (i < len && s[i] == '.') {
  3425. i++;
  3426. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3427. if (fd < 2) {
  3428. frac = frac * 10 + (s[i] - '0');
  3429. fd++;
  3430. digits = 1;
  3431. }
  3432. i++;
  3433. }
  3434. }
  3435. if (!digits)
  3436. return 0;
  3437. while (fd++ < 2)
  3438. frac *= 10;
  3439. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3440. *pos = i;
  3441. return 1;
  3442. }
  3443. static int find_fn(const char *v, unsigned int len, const char *name,
  3444. unsigned int *arg)
  3445. {
  3446. unsigned int n = s_len(name), i;
  3447. for (i = 0; i + n <= len; i++) {
  3448. unsigned int j;
  3449. if (!ci_eq_n(v + i, name, n))
  3450. continue;
  3451. j = i + n;
  3452. while (j < len && is_space(v[j]))
  3453. j++;
  3454. if (j < len && v[j] == '(') {
  3455. *arg = j + 1;
  3456. return 1;
  3457. }
  3458. }
  3459. return 0;
  3460. }
  3461. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3462. const char *s, unsigned int len,
  3463. int important)
  3464. {
  3465. unsigned int pos = 0, ts, te;
  3466. signed short val[2] = { 50, 50 };
  3467. unsigned char kind[2] = { 1, 1 };
  3468. int vert[2] = { 0, 0 }, n = 0;
  3469. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3470. signed short v;
  3471. int is_vert;
  3472. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3473. val[n] = v;
  3474. kind[n] = 1;
  3475. vert[n] = is_vert;
  3476. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3477. val[n] = v;
  3478. vert[n] = 0;
  3479. } else {
  3480. n++;
  3481. continue;
  3482. }
  3483. n++;
  3484. }
  3485. if (n == 1) {
  3486. if (vert[0]) {
  3487. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3488. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3489. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3490. important);
  3491. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3492. } else {
  3493. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3494. important);
  3495. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3496. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3497. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3498. }
  3499. return;
  3500. }
  3501. if (n >= 2) {
  3502. int x = vert[0] && !vert[1] ? 1 : 0;
  3503. int y = 1 - x;
  3504. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3505. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3506. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3507. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3508. }
  3509. }
  3510. /* Copy a background value with every url(...) body blanked (kept as
  3511. * `url()`), for the keyword and colour scans of the shorthand. */
  3512. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3513. unsigned int cap)
  3514. {
  3515. unsigned int i = 0, n = 0;
  3516. while (i < len && n + 1 < cap) {
  3517. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3518. char q = 0;
  3519. memcpy(out + n, "url(", 4);
  3520. n += 4;
  3521. i += 4;
  3522. while (i < len) {
  3523. if (q) {
  3524. if (s[i] == '\\' && i + 1 < len)
  3525. i++;
  3526. else if (s[i] == q)
  3527. q = 0;
  3528. } else if (s[i] == '"' || s[i] == '\'') {
  3529. q = s[i];
  3530. } else if (s[i] == ')') {
  3531. break;
  3532. }
  3533. i++;
  3534. }
  3535. continue; /* the ')' is copied below */
  3536. }
  3537. out[n++] = s[i++];
  3538. }
  3539. out[n] = '\0';
  3540. return n;
  3541. }
  3542. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3543. const char *v, unsigned int vl, int important)
  3544. {
  3545. unsigned int i;
  3546. for (i = 0; i + 4 <= vl; i++) {
  3547. if (ci_eq_n(v + i, "url(", 4)) {
  3548. unsigned int st = i + 4, en = st;
  3549. char q = 0;
  3550. int idx;
  3551. /* `)` inside a quoted data: URL (an SVG's
  3552. * rgba(...)) does not end it. */
  3553. while (en < vl) {
  3554. if (q) {
  3555. if (v[en] == '\\' && en + 1 < vl)
  3556. en++;
  3557. else if (v[en] == q)
  3558. q = 0;
  3559. } else if (v[en] == '"' || v[en] == '\'') {
  3560. q = v[en];
  3561. } else if (v[en] == ')') {
  3562. break;
  3563. }
  3564. en++;
  3565. }
  3566. idx = register_bg_url(v + st, en - st);
  3567. if (idx)
  3568. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3569. important);
  3570. return;
  3571. }
  3572. }
  3573. }
  3574. static int parse_len_token(const char *s, unsigned int len, int *out)
  3575. {
  3576. unsigned int pos = 0;
  3577. int v;
  3578. if (!len)
  3579. return 0;
  3580. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3581. return 0;
  3582. v = parse_len(s, len, &pos);
  3583. if (pos == 0)
  3584. return 0;
  3585. *out = v;
  3586. return 1;
  3587. }
  3588. static void expand_four(int v[4], int n)
  3589. {
  3590. if (n == 1) {
  3591. v[1] = v[2] = v[3] = v[0];
  3592. } else if (n == 2) {
  3593. v[2] = v[0];
  3594. v[3] = v[1];
  3595. } else if (n == 3) {
  3596. v[3] = v[1];
  3597. }
  3598. }
  3599. /* Custom properties defined in the declaration block being expanded. A
  3600. * component that sets a variable and uses it in the same rule (Infima's
  3601. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3602. * var(--ifm-col-width) }`) means its own value: the element matching the
  3603. * rule gets it. The document-wide table cannot know that, because
  3604. * `.col` and `.col--N` give the name a dozen different values. */
  3605. static int css_env_active; /* css_local holds an element's variables */
  3606. #define CSS_LOCAL_VARS 48
  3607. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3608. unsigned int i);
  3609. static struct {
  3610. const char *name, *val;
  3611. unsigned short nlen, vlen;
  3612. } css_local[CSS_LOCAL_VARS];
  3613. static unsigned int css_nlocal;
  3614. static int css_local_find(const char *name, unsigned int nlen)
  3615. {
  3616. unsigned int k;
  3617. for (k = 0; k < css_nlocal; k++)
  3618. if (css_local[k].nlen == nlen &&
  3619. !memcmp(css_local[k].name, name, nlen))
  3620. return (int)k;
  3621. return -1;
  3622. }
  3623. static int css_local_find_before(const char *name, unsigned int nlen,
  3624. unsigned int end)
  3625. {
  3626. unsigned int k;
  3627. for (k = 0; k < end; k++)
  3628. if (css_local[k].nlen == nlen &&
  3629. !memcmp(css_local[k].name, name, nlen))
  3630. return (int)k;
  3631. return -1;
  3632. }
  3633. static void css_local_append(const char *s, unsigned int len);
  3634. /* Collect the top-level `--name: value` declarations of a block. */
  3635. static void css_local_collect(const char *s, unsigned int len)
  3636. {
  3637. css_nlocal = 0;
  3638. css_local_append(s, len);
  3639. }
  3640. static void css_local_append(const char *s, unsigned int len)
  3641. {
  3642. unsigned int i = 0;
  3643. while (i < len) {
  3644. unsigned int ns, ne, vs, ve;
  3645. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3646. i++;
  3647. ns = i;
  3648. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3649. i++;
  3650. if (i >= len || s[i] != ':') {
  3651. if (i < len && s[i] == '{')
  3652. i = css_skip_nested(s, len, i + 1);
  3653. continue;
  3654. }
  3655. ne = i;
  3656. while (ne > ns && is_space(s[ne - 1]))
  3657. ne--;
  3658. vs = i + 1;
  3659. ve = css_value_end(s, len, vs);
  3660. i = ve;
  3661. while (vs < ve && is_space(s[vs]))
  3662. vs++;
  3663. while (ve > vs && is_space(s[ve - 1]))
  3664. ve--;
  3665. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3666. ve -= 10;
  3667. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3668. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3669. continue;
  3670. css_local[css_nlocal].name = s + ns;
  3671. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3672. css_local[css_nlocal].val = s + vs;
  3673. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3674. css_nlocal++;
  3675. }
  3676. }
  3677. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3678. * the input is copied verbatim when it contains no var(). */
  3679. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3680. unsigned int len, char *out,
  3681. unsigned int cap, int depth)
  3682. {
  3683. unsigned int i = 0, n = 0;
  3684. if (depth > 8)
  3685. return 0;
  3686. while (i < len && n + 1 < cap) {
  3687. unsigned int p, inner, close, comma, nlen, fstart;
  3688. char quote;
  3689. int parens, glue;
  3690. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3691. out[n++] = s[i++];
  3692. continue;
  3693. }
  3694. p = i + 4;
  3695. quote = 0;
  3696. parens = 1;
  3697. while (p < len && parens) {
  3698. char c = s[p];
  3699. if (quote) {
  3700. if (c == quote)
  3701. quote = 0;
  3702. else if (c == '\\' && p + 1 < len)
  3703. p++;
  3704. } else if (c == '"' || c == '\'') {
  3705. quote = c;
  3706. } else if (c == '(') {
  3707. parens++;
  3708. } else if (c == ')') {
  3709. parens--;
  3710. }
  3711. p++;
  3712. }
  3713. if (parens) {
  3714. out[n++] = s[i++];
  3715. continue;
  3716. }
  3717. close = p - 1;
  3718. inner = i + 4;
  3719. /* var() substitutes tokens: minified Infima writes
  3720. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3721. * must not become the one token `700.875rem`. Separate the
  3722. * value from a glued neighbour on both sides. */
  3723. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3724. out[n - 1] != '(' && out[n - 1] != ',' &&
  3725. out[n - 1] != ':' && out[n - 1] != '/')
  3726. out[n++] = ' ';
  3727. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3728. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3729. s[p] != '!' && s[p] != '}';
  3730. while (inner < close && is_space(s[inner]))
  3731. inner++;
  3732. comma = inner;
  3733. parens = 0;
  3734. quote = 0;
  3735. while (comma < close) {
  3736. char c = s[comma];
  3737. if (quote) {
  3738. if (c == quote)
  3739. quote = 0;
  3740. } else if (c == '"' || c == '\'') {
  3741. quote = c;
  3742. } else if (c == '(') {
  3743. parens++;
  3744. } else if (c == ')' && parens > 0) {
  3745. parens--;
  3746. } else if (c == ',' && parens == 0) {
  3747. break;
  3748. }
  3749. comma++;
  3750. }
  3751. nlen = comma - inner;
  3752. while (nlen && is_space(s[inner + nlen - 1]))
  3753. nlen--;
  3754. fstart = comma < close ? comma + 1 : close;
  3755. while (fstart < close && is_space(s[fstart]))
  3756. fstart++;
  3757. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3758. unsigned int vi = css_var_find(d, s + inner, nlen);
  3759. int li = css_local_find(s + inner, nlen);
  3760. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3761. s + close + 1) {
  3762. /* (not the definition's own value) */
  3763. n += css_expand_vars(d, css_local[li].val,
  3764. css_local[li].vlen,
  3765. out + n, cap - n,
  3766. depth + 1);
  3767. i = p;
  3768. if (glue && n + 1 < cap)
  3769. out[n++] = ' ';
  3770. continue;
  3771. }
  3772. if (vi < BR_MAX_VARS &&
  3773. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3774. vi = BR_MAX_VARS;
  3775. if (vi < BR_MAX_VARS) {
  3776. const char *v = attr_ptr(d, d->var_val[vi]);
  3777. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3778. out + n,
  3779. cap - n, depth + 1);
  3780. n += vn;
  3781. } else if (fstart < close) {
  3782. unsigned int vn = css_expand_vars(d, s + fstart,
  3783. close - fstart,
  3784. out + n, cap - n,
  3785. depth + 1);
  3786. n += vn;
  3787. }
  3788. } else if (fstart < close) {
  3789. unsigned int vn = css_expand_vars(d, s + fstart,
  3790. close - fstart,
  3791. out + n, cap - n,
  3792. depth + 1);
  3793. n += vn;
  3794. }
  3795. i = p;
  3796. if (glue && n + 1 < cap)
  3797. out[n++] = ' ';
  3798. }
  3799. out[n] = '\0';
  3800. return n;
  3801. }
  3802. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3803. unsigned int i)
  3804. {
  3805. while (i < len && (is_space(s[i]) || s[i] == ','))
  3806. i++;
  3807. return i;
  3808. }
  3809. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3810. * argument). Returns 1 on success. */
  3811. static int parse_grid_track(const char *s, unsigned int len,
  3812. unsigned int *pp, unsigned short *val,
  3813. unsigned char *kind)
  3814. {
  3815. unsigned int i = grid_skip_sep(s, len, *pp);
  3816. unsigned int whole = 0, frac = 0, scale = 1;
  3817. int any = 0;
  3818. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3819. unsigned int j = i + 7;
  3820. while (j < len && s[j] != ',')
  3821. j++;
  3822. if (j >= len)
  3823. return 0;
  3824. i = j + 1;
  3825. while (i < len && is_space(s[i]))
  3826. i++;
  3827. }
  3828. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3829. int depth = 1;
  3830. i += 7;
  3831. while (i < len && depth) {
  3832. if (s[i] == '(')
  3833. depth++;
  3834. else if (s[i] == ')')
  3835. depth--;
  3836. i++;
  3837. }
  3838. *pp = i;
  3839. return 0;
  3840. }
  3841. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3842. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3843. i++;
  3844. any = 1;
  3845. }
  3846. if (i < len && s[i] == '.') {
  3847. i++;
  3848. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3849. if (scale < 1000) {
  3850. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3851. scale *= 10;
  3852. }
  3853. i++;
  3854. any = 1;
  3855. }
  3856. }
  3857. if (!any) {
  3858. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3859. *val = 1;
  3860. *kind = 3;
  3861. *pp = i + 4;
  3862. return 1;
  3863. }
  3864. return 0;
  3865. }
  3866. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3867. *val = (unsigned short)(whole ? whole : 1);
  3868. *kind = 3;
  3869. i += 2;
  3870. } else if (i < len && s[i] == '%') {
  3871. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3872. *kind = 2;
  3873. i++;
  3874. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3875. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3876. *kind = 1;
  3877. i += 3;
  3878. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3879. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3880. *kind = 1;
  3881. i += 2;
  3882. } else {
  3883. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3884. *kind = 1;
  3885. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3886. i += 2;
  3887. }
  3888. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3889. i++;
  3890. *pp = i;
  3891. return 1;
  3892. }
  3893. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3894. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3895. * keep the two-column path). */
  3896. static int grid_repeat_count(const char *s, unsigned int len)
  3897. {
  3898. unsigned int i = 0;
  3899. int n = 0;
  3900. while (i < len && is_space(s[i]))
  3901. i++;
  3902. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3903. i += 7;
  3904. while (i < len && is_space(s[i]))
  3905. i++;
  3906. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3907. n = n * 10 + (s[i] - '0');
  3908. i++;
  3909. }
  3910. if (n < 1 || n > 12)
  3911. return 0;
  3912. while (i < len && (is_space(s[i]) || s[i] == ','))
  3913. i++;
  3914. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3915. return n;
  3916. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3917. i += 7;
  3918. while (i < len && is_space(s[i]))
  3919. i++;
  3920. if (i < len && s[i] == '0') {
  3921. i++;
  3922. while (i < len && (is_space(s[i]) ||
  3923. s[i] == ',' || s[i] == 'p' ||
  3924. s[i] == 'x'))
  3925. i++;
  3926. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3927. return n;
  3928. }
  3929. }
  3930. return 0;
  3931. }
  3932. /* "1fr 1fr 1fr" */
  3933. for (;;) {
  3934. while (i < len && is_space(s[i]))
  3935. i++;
  3936. if (i >= len)
  3937. break;
  3938. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3939. (i + 3 == len || is_space(s[i + 3]))) {
  3940. n++;
  3941. i += 3;
  3942. } else {
  3943. return 0;
  3944. }
  3945. }
  3946. return n >= 3 ? n : 0;
  3947. }
  3948. static int parse_grid_cols(const char *s, unsigned int len,
  3949. unsigned short val[2], unsigned char kind[2])
  3950. {
  3951. unsigned int pos = 0;
  3952. int n = 0;
  3953. while (n < 2) {
  3954. unsigned int before = pos;
  3955. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3956. break;
  3957. n++;
  3958. if (pos <= before)
  3959. break;
  3960. while (pos < len && is_space(s[pos]))
  3961. pos++;
  3962. if (pos >= len)
  3963. break;
  3964. }
  3965. return n;
  3966. }
  3967. static int parse_grid_slot(const char *s, unsigned int len)
  3968. {
  3969. if (contains_ci_len(s, len, "footer") ||
  3970. contains_ci_len(s, len, "header") ||
  3971. contains_ci_len(s, len, "sitenotice") ||
  3972. contains_ci_len(s, len, "navbar"))
  3973. return 1;
  3974. if (contains_ci_len(s, len, "columnstart") ||
  3975. contains_ci_len(s, len, "sidebar") ||
  3976. contains_ci_len(s, len, "left") ||
  3977. contains_ci_len(s, len, "start"))
  3978. return 2;
  3979. if (contains_ci_len(s, len, "pagecontent") ||
  3980. contains_ci_len(s, len, "content") ||
  3981. contains_ci_len(s, len, "main") ||
  3982. contains_ci_len(s, len, "right") ||
  3983. contains_ci_len(s, len, "end"))
  3984. return 3;
  3985. return 0;
  3986. }
  3987. static int css_has_var(const char *s, unsigned int len)
  3988. {
  3989. unsigned int i;
  3990. for (i = 0; i + 4 <= len; i++)
  3991. if ((s[i] == 'v' || s[i] == 'V') &&
  3992. ci_eq_n(s + i, "var(", 4))
  3993. return 1;
  3994. return 0;
  3995. }
  3996. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3997. unsigned int i)
  3998. {
  3999. i += 2;
  4000. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  4001. i++;
  4002. if (i + 1 < len)
  4003. i += 2;
  4004. else
  4005. i = len;
  4006. return i;
  4007. }
  4008. static unsigned int css_skip_string(const char *s, unsigned int len,
  4009. unsigned int i)
  4010. {
  4011. char q = s[i++];
  4012. while (i < len && s[i] != q) {
  4013. if (s[i] == '\\' && i + 1 < len)
  4014. i++;
  4015. i++;
  4016. }
  4017. if (i < len)
  4018. i++;
  4019. return i;
  4020. }
  4021. /* Find the '}' that closes a declaration block. i points just past the
  4022. * opening '{'. Comments and quoted strings are skipped: a brace inside
  4023. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  4024. * selectors such as "Highlight-Stories {" without a closing brace). */
  4025. static unsigned int css_block_end(const char *s, unsigned int len,
  4026. unsigned int i)
  4027. {
  4028. int depth = 0;
  4029. while (i < len) {
  4030. char c = s[i];
  4031. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4032. i = css_skip_comment(s, len, i);
  4033. continue;
  4034. }
  4035. if (c == '"' || c == '\'') {
  4036. i = css_skip_string(s, len, i);
  4037. continue;
  4038. }
  4039. if (c == '{') {
  4040. depth++;
  4041. } else if (c == '}') {
  4042. if (depth == 0)
  4043. return i;
  4044. depth--;
  4045. }
  4046. i++;
  4047. }
  4048. return len;
  4049. }
  4050. /* Skip a block whose opening '{' has already been consumed. */
  4051. static unsigned int css_skip_nested(const char *s, unsigned int len,
  4052. unsigned int i)
  4053. {
  4054. int depth = 1;
  4055. while (i < len && depth) {
  4056. char c = s[i];
  4057. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4058. i = css_skip_comment(s, len, i);
  4059. continue;
  4060. }
  4061. if (c == '"' || c == '\'') {
  4062. i = css_skip_string(s, len, i);
  4063. continue;
  4064. }
  4065. if (c == '{')
  4066. depth++;
  4067. else if (c == '}')
  4068. depth--;
  4069. i++;
  4070. }
  4071. return i;
  4072. }
  4073. /* ---- @font-face ------------------------------------------------------- */
  4074. static void font_trim(const char **sp, unsigned int *len)
  4075. {
  4076. const char *s = *sp;
  4077. unsigned int n = *len;
  4078. while (n && is_space(*s)) {
  4079. s++;
  4080. n--;
  4081. }
  4082. while (n && is_space(s[n - 1]))
  4083. n--;
  4084. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4085. s++;
  4086. n -= 2;
  4087. }
  4088. *sp = s;
  4089. *len = n;
  4090. }
  4091. static int font_name_eq(const char *a, const char *b, unsigned int blen)
  4092. {
  4093. unsigned int k;
  4094. if (s_len(a) != blen)
  4095. return 0;
  4096. for (k = 0; k < blen; k++) {
  4097. char x = a[k], y = b[k];
  4098. if (x >= 'A' && x <= 'Z')
  4099. x = (char)(x - 'A' + 'a');
  4100. if (y >= 'A' && y <= 'Z')
  4101. y = (char)(y - 'A' + 'a');
  4102. if (x != y)
  4103. return 0;
  4104. }
  4105. return 1;
  4106. }
  4107. static void font_copy(char *dst, unsigned int cap, const char *src,
  4108. unsigned int n)
  4109. {
  4110. unsigned int k;
  4111. for (k = 0; k < n && src[k] && k + 1 < cap; k++)
  4112. dst[k] = src[k];
  4113. dst[k] = '\0';
  4114. }
  4115. /* Register one @font-face. Every (family, weight range, style) is its own
  4116. * entry; a later rule with the same triple replaces the URL, as the CSS
  4117. * font-face collection does. family_id is the 1-based id of the family's
  4118. * first entry, which is what font-family declarations store. */
  4119. static void font_add(const char *family, unsigned int flen,
  4120. const char *url, unsigned int ulen,
  4121. unsigned int wmin, unsigned int wmax, int italic)
  4122. {
  4123. unsigned int i;
  4124. int fam = 0;
  4125. struct br_font_def *f;
  4126. font_trim(&family, &flen);
  4127. font_trim(&url, &ulen);
  4128. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4129. return;
  4130. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4131. f = &font_defs[i];
  4132. if (!font_name_eq(f->family, family, flen))
  4133. continue;
  4134. if (!fam)
  4135. fam = f->family_id;
  4136. if (f->wmin != wmin || f->wmax != wmax ||
  4137. f->italic != (unsigned char)italic)
  4138. continue;
  4139. font_copy(f->url, sizeof(f->url), url, ulen);
  4140. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4141. return;
  4142. }
  4143. if (font_face_count >= BR_MAX_FACES)
  4144. return;
  4145. f = &font_defs[font_face_count];
  4146. font_copy(f->family, sizeof(f->family), family, flen);
  4147. font_copy(f->url, sizeof(f->url), url, ulen);
  4148. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4149. f->wmin = (unsigned short)wmin;
  4150. f->wmax = (unsigned short)wmax;
  4151. f->italic = (unsigned char)italic;
  4152. f->used = 0;
  4153. font_face_count++;
  4154. f->family_id = (unsigned char)(fam ? fam : font_face_count);
  4155. /* A family that a font-family declaration already resolved (an
  4156. * earlier @font-face of it) stays wanted for its new variants. */
  4157. if (fam && font_defs[fam - 1].used)
  4158. f->used = 1;
  4159. }
  4160. /* Look up the first name in a font-family list that has an @font-face and
  4161. * mark that family as used, so the browser only downloads faces the page
  4162. * refers to. */
  4163. static int font_family_id(const char *s, unsigned int len)
  4164. {
  4165. unsigned int i = 0;
  4166. while (i < len) {
  4167. unsigned int start, end, q;
  4168. while (i < len && (is_space(s[i]) || s[i] == ','))
  4169. i++;
  4170. if (i >= len)
  4171. break;
  4172. if (s[i] == '"' || s[i] == '\'') {
  4173. char quote = s[i++];
  4174. start = i;
  4175. while (i < len && s[i] != quote)
  4176. i++;
  4177. end = i;
  4178. if (i < len)
  4179. i++;
  4180. } else {
  4181. start = i;
  4182. while (i < len && s[i] != ',')
  4183. i++;
  4184. end = i;
  4185. while (end > start && is_space(s[end - 1]))
  4186. end--;
  4187. }
  4188. if (end <= start)
  4189. continue;
  4190. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4191. int fam;
  4192. unsigned int k;
  4193. if (!font_name_eq(font_defs[q].family, s + start,
  4194. end - start))
  4195. continue;
  4196. fam = font_defs[q].family_id;
  4197. for (k = 0; k < (unsigned int)font_face_count; k++)
  4198. if (font_defs[k].family_id == fam)
  4199. font_defs[k].used = 1;
  4200. return fam;
  4201. }
  4202. }
  4203. return 0;
  4204. }
  4205. /* font-weight descriptor: `normal`, `bold`, a number, or a variable font's
  4206. * `100 900` range. */
  4207. static void font_parse_weight(const char *s, unsigned int len,
  4208. unsigned int *wmin, unsigned int *wmax)
  4209. {
  4210. unsigned int i = 0, n = 0, w[2] = { 400, 400 };
  4211. while (i < len && n < 2) {
  4212. unsigned int v = 0, st;
  4213. while (i < len && is_space(s[i]))
  4214. i++;
  4215. st = i;
  4216. while (i < len && !is_space(s[i]))
  4217. i++;
  4218. if (i == st)
  4219. break;
  4220. if (i - st == 4 && ci_eq_n(s + st, "bold", 4)) {
  4221. v = 700;
  4222. } else if (i - st == 6 && ci_eq_n(s + st, "bolder", 6)) {
  4223. v = 700;
  4224. } else if (i - st == 7 && ci_eq_n(s + st, "lighter", 7)) {
  4225. v = 300;
  4226. } else if (s[st] >= '0' && s[st] <= '9') {
  4227. unsigned int k;
  4228. for (k = st; k < i && s[k] >= '0' && s[k] <= '9'; k++)
  4229. v = v * 10 + (unsigned int)(s[k] - '0');
  4230. } else {
  4231. v = 400;
  4232. }
  4233. if (v < 1)
  4234. v = 1;
  4235. if (v > 1000)
  4236. v = 1000;
  4237. w[n++] = v;
  4238. }
  4239. if (n == 1)
  4240. w[1] = w[0];
  4241. if (w[0] > w[1]) {
  4242. unsigned int t = w[0];
  4243. w[0] = w[1];
  4244. w[1] = t;
  4245. }
  4246. *wmin = w[0];
  4247. *wmax = w[1];
  4248. }
  4249. static int font_hex(char c)
  4250. {
  4251. if (c >= '0' && c <= '9')
  4252. return c - '0';
  4253. if (c >= 'a' && c <= 'f')
  4254. return c - 'a' + 10;
  4255. if (c >= 'A' && c <= 'F')
  4256. return c - 'A' + 10;
  4257. return -1;
  4258. }
  4259. /* Whether a unicode-range list covers basic Latin. Google Fonts splits a
  4260. * family into per-script subsets with the same family/weight/style; only
  4261. * the one holding ASCII is useful here (the text path is Latin-1). */
  4262. static int font_range_has_latin(const char *s, unsigned int len)
  4263. {
  4264. unsigned int i = 0;
  4265. while (i < len) {
  4266. unsigned int lo = 0, hi, digits = 0, wild = 0;
  4267. while (i < len && (is_space(s[i]) || s[i] == ','))
  4268. i++;
  4269. if (i + 2 > len || (s[i] != 'U' && s[i] != 'u') ||
  4270. s[i + 1] != '+') {
  4271. while (i < len && s[i] != ',')
  4272. i++;
  4273. continue;
  4274. }
  4275. i += 2;
  4276. while (i < len && digits < 6 &&
  4277. (font_hex(s[i]) >= 0 || s[i] == '?')) {
  4278. if (s[i] == '?') {
  4279. lo <<= 4;
  4280. wild++;
  4281. } else {
  4282. lo = (lo << 4) | (unsigned int)font_hex(s[i]);
  4283. }
  4284. digits++;
  4285. i++;
  4286. }
  4287. hi = lo | ((1u << (wild * 4)) - 1u);
  4288. if (!wild && i < len && s[i] == '-') {
  4289. hi = 0;
  4290. i++;
  4291. while (i < len && font_hex(s[i]) >= 0) {
  4292. hi = (hi << 4) | (unsigned int)font_hex(s[i]);
  4293. i++;
  4294. }
  4295. }
  4296. if (digits && lo <= 'a' && hi >= 'a')
  4297. return 1;
  4298. while (i < len && s[i] != ',')
  4299. i++;
  4300. }
  4301. return 0;
  4302. }
  4303. /* Pick the first `src` entry the text engine can load: a TrueType/OpenType
  4304. * file or WOFF 1.0, by its format() hint or, without one, by its
  4305. * extension. local() names, WOFF2, EOT, SVG fonts and data: URLs (they do
  4306. * not fit BR_FACE_URL) are skipped. */
  4307. static int font_pick_src(const char *s, unsigned int len, char *url,
  4308. unsigned int cap, unsigned int *ulen)
  4309. {
  4310. unsigned int i = 0;
  4311. while (i < len) {
  4312. unsigned int is_ = i, ie, us = 0, ue = 0, q, depth = 0;
  4313. int quote = 0, ok;
  4314. const char *up;
  4315. unsigned int un;
  4316. /* One comma-separated item, commas inside url()/quotes kept. */
  4317. while (i < len) {
  4318. char c = s[i];
  4319. if (quote) {
  4320. if (c == quote)
  4321. quote = 0;
  4322. } else if (c == '"' || c == '\'') {
  4323. quote = c;
  4324. } else if (c == '(') {
  4325. depth++;
  4326. } else if (c == ')') {
  4327. if (depth)
  4328. depth--;
  4329. } else if (c == ',' && !depth) {
  4330. break;
  4331. }
  4332. i++;
  4333. }
  4334. ie = i;
  4335. i++;
  4336. for (q = is_; q + 4 <= ie; q++)
  4337. if (ci_eq_n(s + q, "url(", 4))
  4338. break;
  4339. if (q + 4 > ie)
  4340. continue;
  4341. us = q + 4;
  4342. for (ue = us; ue < ie && s[ue] != ')'; ue++)
  4343. if (s[ue] == '"' || s[ue] == '\'') {
  4344. char qc = s[ue++];
  4345. while (ue < ie && s[ue] != qc)
  4346. ue++;
  4347. }
  4348. up = s + us;
  4349. un = ue - us;
  4350. font_trim(&up, &un);
  4351. if (!un || un >= cap || (un >= 5 && ci_eq_n(up, "data:", 5)))
  4352. continue;
  4353. ok = 1;
  4354. if (contains_ci_len(s + ue, ie - ue, "format(")) {
  4355. const char *fp = s + ue;
  4356. unsigned int fn = ie - ue;
  4357. ok = (contains_ci_len(fp, fn, "truetype") ||
  4358. contains_ci_len(fp, fn, "opentype") ||
  4359. contains_ci_len(fp, fn, "woff")) &&
  4360. !contains_ci_len(fp, fn, "woff2") &&
  4361. !contains_ci_len(fp, fn, "embedded");
  4362. } else {
  4363. unsigned int e = 0;
  4364. while (e < un && up[e] != '?' && up[e] != '#')
  4365. e++;
  4366. if ((e >= 6 && ci_eq_n(up + e - 6, ".woff2", 6)) ||
  4367. (e >= 4 && ci_eq_n(up + e - 4, ".eot", 4)) ||
  4368. (e >= 4 && ci_eq_n(up + e - 4, ".svg", 4)))
  4369. ok = 0;
  4370. }
  4371. if (!ok)
  4372. continue;
  4373. for (q = 0; q < un; q++)
  4374. url[q] = up[q];
  4375. url[un] = '\0';
  4376. *ulen = un;
  4377. return 1;
  4378. }
  4379. return 0;
  4380. }
  4381. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4382. * at-rules, so faces must be registered before any font-family declaration
  4383. * in the same sheet is resolved. */
  4384. static void scan_font_faces(const char *s, unsigned int len)
  4385. {
  4386. unsigned int i = 0;
  4387. while (i + 10 <= len) {
  4388. unsigned int j, end;
  4389. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4390. unsigned int flen = 0, ulen = 0, p;
  4391. unsigned int wmin = 400, wmax = 400;
  4392. int have_family = 0, have_url = 0, italic = 0, latin = 1;
  4393. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4394. i++;
  4395. continue;
  4396. }
  4397. j = i + 10;
  4398. while (j < len && s[j] != '{' && s[j] != ';')
  4399. j++;
  4400. if (j >= len || s[j] == ';') {
  4401. i = j + 1;
  4402. continue;
  4403. }
  4404. end = css_skip_nested(s, len, j + 1);
  4405. p = j + 1;
  4406. while (p < end) {
  4407. unsigned int ds, de, vs, ve, quote = 0;
  4408. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4409. p++;
  4410. if (p >= end)
  4411. break;
  4412. ds = p;
  4413. while (p < end && s[p] != ':' && s[p] != ';' &&
  4414. s[p] != '}')
  4415. p++;
  4416. de = p;
  4417. if (p >= end || s[p] != ':') {
  4418. while (p < end && s[p] != ';')
  4419. p++;
  4420. continue;
  4421. }
  4422. p++;
  4423. vs = p;
  4424. /* A quoted URL may contain ';' (data: URLs do). */
  4425. while (p < end && (quote || (s[p] != ';' &&
  4426. s[p] != '}'))) {
  4427. if (quote && s[p] == (char)quote)
  4428. quote = 0;
  4429. else if (!quote && (s[p] == '"' || s[p] == '\''))
  4430. quote = (unsigned char)s[p];
  4431. p++;
  4432. }
  4433. ve = p;
  4434. while (ds < de && is_space(s[ds]))
  4435. ds++;
  4436. while (de > ds && is_space(s[de - 1]))
  4437. de--;
  4438. while (vs < ve && is_space(s[vs]))
  4439. vs++;
  4440. while (ve > vs && is_space(s[ve - 1]))
  4441. ve--;
  4442. if (de - ds == 11 &&
  4443. ci_eq_n(s + ds, "font-family", 11)) {
  4444. const char *fp = s + vs;
  4445. unsigned int fn = ve - vs;
  4446. font_trim(&fp, &fn);
  4447. if (fn && fn < sizeof(family)) {
  4448. unsigned int k;
  4449. for (k = 0; k < fn; k++)
  4450. family[k] = fp[k];
  4451. family[fn] = '\0';
  4452. flen = fn;
  4453. have_family = 1;
  4454. }
  4455. } else if (de - ds == 3 &&
  4456. ci_eq_n(s + ds, "src", 3)) {
  4457. /* A later src (the old IE `src: eot;` hack
  4458. * is followed by the real list) wins when
  4459. * it has a usable entry. */
  4460. if (font_pick_src(s + vs, ve - vs, url,
  4461. sizeof(url), &ulen))
  4462. have_url = 1;
  4463. } else if (de - ds == 11 &&
  4464. ci_eq_n(s + ds, "font-weight", 11)) {
  4465. font_parse_weight(s + vs, ve - vs, &wmin,
  4466. &wmax);
  4467. } else if (de - ds == 10 &&
  4468. ci_eq_n(s + ds, "font-style", 10)) {
  4469. italic = contains_ci_len(s + vs, ve - vs,
  4470. "italic") ||
  4471. contains_ci_len(s + vs, ve - vs,
  4472. "oblique");
  4473. } else if (de - ds == 13 &&
  4474. ci_eq_n(s + ds, "unicode-range", 13)) {
  4475. latin = font_range_has_latin(s + vs, ve - vs);
  4476. }
  4477. }
  4478. if (have_family && have_url && latin)
  4479. font_add(family, flen, url, ulen, wmin, wmax, italic);
  4480. i = end;
  4481. }
  4482. }
  4483. /* One border-radius length. Unlike parse_size(), percentages keep their
  4484. * kind: the drawer scales them to the box, not the viewport. */
  4485. static int parse_radius_value(const char *s, unsigned int len,
  4486. unsigned int *pos, unsigned short *val,
  4487. unsigned char *kind)
  4488. {
  4489. unsigned int i = *pos;
  4490. unsigned long v = 0;
  4491. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4492. while (i < len && is_space(s[i]))
  4493. i++;
  4494. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4495. v = v * 10 + (unsigned int)(s[i] - '0');
  4496. digits = 1;
  4497. i++;
  4498. }
  4499. if (i < len && s[i] == '.') {
  4500. i++;
  4501. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4502. fdigits = 1;
  4503. if (frac < 2) {
  4504. v = v * 10 + (unsigned int)(s[i] - '0');
  4505. frac++;
  4506. }
  4507. i++;
  4508. }
  4509. }
  4510. while (frac < 2) {
  4511. v *= 10;
  4512. frac++;
  4513. }
  4514. while (i < len && is_space(s[i]))
  4515. i++;
  4516. if (i < len && s[i] == '%') {
  4517. pct = 1;
  4518. i++;
  4519. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4520. s[i + 2] == 'm') {
  4521. rem = 1;
  4522. i += 3;
  4523. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4524. rem = 2;
  4525. i += 2;
  4526. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4527. i += 2;
  4528. } else if (i < len && is_ident(s[i])) {
  4529. return 0;
  4530. }
  4531. if (!digits && !fdigits)
  4532. return 0;
  4533. if (rem == 1)
  4534. v *= root_font_px;
  4535. else if (rem == 2)
  4536. v *= root_font_px;
  4537. v /= 100;
  4538. if (v > 2048)
  4539. v = 2048;
  4540. *val = (unsigned short)v;
  4541. *kind = pct ? 2 : 1;
  4542. *pos = i;
  4543. return 1;
  4544. }
  4545. /* font: [style] [variant] [weight] size[/line-height] family */
  4546. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4547. const char *v, unsigned int len, int important)
  4548. {
  4549. unsigned int pos = 0, ts, te;
  4550. unsigned short size_raw = 0;
  4551. unsigned char size_unit = 0;
  4552. int have_size = 0;
  4553. int bold = 0, italic = 0;
  4554. int lh_px = 0, lh_pct = 0, mono = 0, face;
  4555. unsigned int fam_at = len;
  4556. while (next_token(v, len, &pos, &ts, &te)) {
  4557. unsigned int tl = te - ts;
  4558. unsigned int slash = 0;
  4559. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4560. bold = 1;
  4561. continue;
  4562. }
  4563. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4564. continue;
  4565. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4566. italic = 1;
  4567. continue;
  4568. }
  4569. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4570. italic = 1;
  4571. continue;
  4572. }
  4573. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4574. continue;
  4575. for (slash = 0; slash < tl; slash++)
  4576. if (v[ts + slash] == '/')
  4577. break;
  4578. if (slash == tl) {
  4579. unsigned int q;
  4580. int weight = 0;
  4581. for (q = 0; q < tl; q++) {
  4582. if (v[ts + q] < '0' || v[ts + q] > '9')
  4583. break;
  4584. weight = weight * 10 + (v[ts + q] - '0');
  4585. }
  4586. if (q == tl && weight >= 100 && weight <= 900) {
  4587. bold = weight >= 500;
  4588. continue;
  4589. }
  4590. }
  4591. {
  4592. unsigned short raw;
  4593. unsigned char unit;
  4594. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4595. &raw, &unit))
  4596. continue;
  4597. size_raw = raw;
  4598. size_unit = unit;
  4599. }
  4600. if (!have_size)
  4601. fam_at = te; /* the family list follows the size */
  4602. have_size = 1;
  4603. if (slash < tl) {
  4604. unsigned int lstart = ts + slash + 1;
  4605. unsigned short lhp = 0;
  4606. unsigned char lhpct = 0;
  4607. if (!parse_line_height(v + lstart, te - lstart,
  4608. &lhp, &lhpct)) {
  4609. lh_px = 0;
  4610. lh_pct = 0;
  4611. } else {
  4612. lh_px = lhp;
  4613. lh_pct = lhpct;
  4614. }
  4615. }
  4616. }
  4617. if (!have_size)
  4618. return;
  4619. mono = contains_ci_len(v, len, "mono");
  4620. face = font_family_id(v + fam_at, len - fam_at);
  4621. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4622. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4623. decl_add(out, nout, PROP_BOLD, bold, important);
  4624. decl_add(out, nout, PROP_ITALIC, italic, important);
  4625. decl_add(out, nout, PROP_MONO, mono, important);
  4626. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4627. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4628. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4629. }
  4630. static int css_collect_only;
  4631. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4632. * index into d->layer_hash. css_layer_name is the dotted path of the
  4633. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4634. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4635. * layer's own rules therefore lose to its sublayers (the spec says the
  4636. * reverse), which no page we render depends on. */
  4637. static unsigned char css_cur_layer;
  4638. static char css_layer_name[128];
  4639. static unsigned int css_anon_layers;
  4640. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4641. {
  4642. unsigned int h = 2166136261u, i;
  4643. for (i = 0; i < len; i++) {
  4644. h ^= (unsigned char)s[i];
  4645. h *= 16777619u;
  4646. }
  4647. return h ? h : 1;
  4648. }
  4649. /* Register (or look up) the layer `name` nested under the current path and
  4650. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4651. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4652. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4653. unsigned int nlen, char *path,
  4654. unsigned int cap)
  4655. {
  4656. unsigned int pl = s_len(css_layer_name), i, h;
  4657. char anon[16];
  4658. while (nlen && is_space(*name)) { name++; nlen--; }
  4659. while (nlen && is_space(name[nlen - 1])) nlen--;
  4660. if (!nlen) {
  4661. /* An anonymous layer is unique; it can never be reopened.
  4662. * The collect-only (custom property) pass does not need a
  4663. * rank, so it must not burn a table slot. */
  4664. unsigned int n = ++css_anon_layers, k = 0;
  4665. char rev[10];
  4666. if (css_collect_only)
  4667. return 0;
  4668. anon[0] = '#';
  4669. while (n && k < sizeof(rev)) {
  4670. rev[k++] = (char)('0' + n % 10);
  4671. n /= 10;
  4672. }
  4673. for (i = 0; i < k; i++)
  4674. anon[1 + i] = rev[k - 1 - i];
  4675. name = anon;
  4676. nlen = k + 1;
  4677. }
  4678. if (pl + 1 + nlen >= cap)
  4679. return 0;
  4680. memcpy(path, css_layer_name, pl);
  4681. if (pl)
  4682. path[pl++] = '.';
  4683. memcpy(path + pl, name, nlen);
  4684. path[pl + nlen] = '\0';
  4685. h = layer_name_hash(path, pl + nlen);
  4686. for (i = 0; i < d->nlayers; i++)
  4687. if (d->layer_hash[i] == h)
  4688. return (unsigned char)(i + 1);
  4689. if (d->nlayers >= BR_MAX_LAYERS)
  4690. return 0;
  4691. d->layer_hash[d->nlayers++] = h;
  4692. return (unsigned char)d->nlayers;
  4693. }
  4694. /* @supports: the engine cannot answer feature queries, so the block
  4695. * applies unless it is negated (`not (...)` usually guards a fallback for
  4696. * old browsers) or asks for selector() support. */
  4697. static int supports_matches(const char *s, unsigned int len)
  4698. {
  4699. while (len && is_space(*s)) { s++; len--; }
  4700. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4701. (len == 3 || is_space(s[3]) || s[3] == '('))
  4702. return 0;
  4703. if (contains_ci_len(s, len, "selector("))
  4704. return 0;
  4705. return 1;
  4706. }
  4707. /* Append one code point of generated content as Latin-1. Typography the
  4708. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4709. * and anything else outside Latin-1 are dropped. */
  4710. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4711. unsigned int cp)
  4712. {
  4713. const char *sub = 0;
  4714. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4715. sub = "-";
  4716. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4717. sub = "'";
  4718. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4719. sub = "\"";
  4720. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4721. sub = "\xB7";
  4722. else if (cp == 0x2026)
  4723. sub = "...";
  4724. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4725. sub = ">";
  4726. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4727. sub = "<";
  4728. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4729. cp == 0x2003 || cp == 0x202F)
  4730. sub = " ";
  4731. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4732. sub = "\xD7";
  4733. else if (cp == 0x2713 || cp == 0x2714)
  4734. sub = "v";
  4735. if (sub) {
  4736. while (*sub && *n + 1 < cap)
  4737. out[(*n)++] = *sub++;
  4738. return;
  4739. }
  4740. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4741. out[(*n)++] = (char)cp;
  4742. else if (cp == '\n' && *n + 1 < cap)
  4743. out[(*n)++] = ' ';
  4744. }
  4745. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4746. * content index + 1, or 0 for none/empty (counters and url() are not
  4747. * generated). */
  4748. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4749. {
  4750. char out[160];
  4751. unsigned int i = 0, n = 0, off;
  4752. while (i < len) {
  4753. char c = s[i];
  4754. if (c == '"' || c == '\'') {
  4755. char q = c;
  4756. i++;
  4757. while (i < len && s[i] != q) {
  4758. unsigned int cp;
  4759. if (s[i] == '\\' && i + 1 < len) {
  4760. unsigned int h = 0, k = 0;
  4761. i++;
  4762. while (k < 6 && i < len &&
  4763. ((s[i] >= '0' && s[i] <= '9') ||
  4764. ((s[i] | 32) >= 'a' &&
  4765. (s[i] | 32) <= 'f'))) {
  4766. char x = (char)(s[i] | 32);
  4767. h = h * 16 + (unsigned int)(x <= '9' ?
  4768. x - '0' : x - 'a' + 10);
  4769. i++;
  4770. k++;
  4771. }
  4772. if (k) {
  4773. if (i < len && s[i] == ' ')
  4774. i++;
  4775. cp = h;
  4776. } else {
  4777. cp = (unsigned char)s[i++];
  4778. }
  4779. } else if ((unsigned char)s[i] >= 0xC0) {
  4780. unsigned char b = (unsigned char)s[i++];
  4781. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4782. cp = b & (0x3F >> more);
  4783. while (more-- && i < len &&
  4784. ((unsigned char)s[i] & 0xC0) == 0x80)
  4785. cp = (cp << 6) | (s[i++] & 0x3F);
  4786. } else {
  4787. cp = (unsigned char)s[i++];
  4788. }
  4789. content_put(out, &n, sizeof(out), cp);
  4790. }
  4791. i++;
  4792. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4793. unsigned int st = i + 5, e = st;
  4794. while (e < len && s[e] != ')')
  4795. e++;
  4796. while (st < e && is_space(s[st]))
  4797. st++;
  4798. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4799. out[n++] = '\001';
  4800. while (st < e && !is_space(s[st]))
  4801. out[n++] = s[st++];
  4802. out[n++] = '\002';
  4803. }
  4804. i = e + 1;
  4805. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4806. ci_eq_n(s + i, "close-quot", 10))) {
  4807. content_put(out, &n, sizeof(out), '"');
  4808. i += 10;
  4809. } else if (c == '(') {
  4810. unsigned int depth = 0;
  4811. /* counter(), url(), ...: skip the arguments */
  4812. for (; i < len; i++) {
  4813. if (s[i] == '(')
  4814. depth++;
  4815. else if (s[i] == ')' && --depth == 0)
  4816. break;
  4817. }
  4818. i++;
  4819. } else {
  4820. i++;
  4821. }
  4822. }
  4823. if (!n) {
  4824. /* `content: ""` still creates the box: icon
  4825. * pseudo-elements are empty strings with a size and a
  4826. * background image. */
  4827. for (i = 0; i < len; i++)
  4828. if (s[i] == '"' || s[i] == '\'')
  4829. return BR_CONTENT_EMPTY;
  4830. return 0;
  4831. }
  4832. if (d->ncontent >= BR_MAX_CONTENT)
  4833. return 0;
  4834. off = store_raw(d, out, n, sizeof(out));
  4835. if (!off)
  4836. return 0;
  4837. d->content_off[d->ncontent++] = off;
  4838. return d->ncontent;
  4839. }
  4840. /* ---- calc() folding ---------------------------------------------------
  4841. *
  4842. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4843. * Frameworks build their scales from arithmetic on custom properties
  4844. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4845. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4846. * calc() in a declaration block is folded into that canonical form:
  4847. * a linear combination of %, px (rem/em at the root font size, vw against
  4848. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4849. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4850. * the calc() text unchanged. Values are hundredths. */
  4851. static unsigned int viewport_w; /* defined with the media code */
  4852. struct calc_val {
  4853. long px, pct, num;
  4854. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  4855. };
  4856. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4857. struct calc_val *v, int depth);
  4858. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  4859. {
  4860. while (*i < len && is_space(s[*i]))
  4861. (*i)++;
  4862. }
  4863. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  4864. struct calc_val *v, int depth)
  4865. {
  4866. long whole = 0, frac = 0, scale = 1, n;
  4867. int sign = 1, digits = 0;
  4868. calc_space(s, len, i);
  4869. if (*i < len && (s[*i] == '(' ||
  4870. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  4871. *i += s[*i] == '(' ? 1 : 5;
  4872. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  4873. return 0;
  4874. calc_space(s, len, i);
  4875. if (*i >= len || s[*i] != ')')
  4876. return 0;
  4877. (*i)++;
  4878. return 1;
  4879. }
  4880. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  4881. sign = s[*i] == '-' ? -1 : 1;
  4882. (*i)++;
  4883. }
  4884. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4885. whole = whole * 10 + (s[*i] - '0');
  4886. digits = 1;
  4887. (*i)++;
  4888. if (whole > 1000000)
  4889. return 0;
  4890. }
  4891. if (*i < len && s[*i] == '.') {
  4892. (*i)++;
  4893. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4894. if (scale < 10000) {
  4895. frac = frac * 10 + (s[*i] - '0');
  4896. scale *= 10;
  4897. }
  4898. digits = 1;
  4899. (*i)++;
  4900. }
  4901. }
  4902. if (!digits)
  4903. return 0;
  4904. n = sign * (whole * 100 + frac * 100 / scale);
  4905. v->px = v->pct = v->num = 0;
  4906. if (*i < len && s[*i] == '%') {
  4907. (*i)++;
  4908. v->pct = n;
  4909. v->kind = 2;
  4910. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  4911. *i += 3;
  4912. v->px = n * root_font_px;
  4913. v->kind = 1;
  4914. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  4915. *i += 2;
  4916. v->px = n * root_font_px;
  4917. v->kind = 1;
  4918. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  4919. *i += 2;
  4920. v->px = n;
  4921. v->kind = 1;
  4922. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  4923. *i += 2;
  4924. v->px = n * (long)viewport_w / 100;
  4925. v->kind = 1;
  4926. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  4927. return 0; /* vh, deg, fr, ch, ... */
  4928. } else {
  4929. v->num = n;
  4930. v->kind = 4;
  4931. }
  4932. return 1;
  4933. }
  4934. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  4935. struct calc_val *v, int depth)
  4936. {
  4937. if (!calc_unary(s, len, i, v, depth))
  4938. return 0;
  4939. for (;;) {
  4940. struct calc_val r;
  4941. char op;
  4942. calc_space(s, len, i);
  4943. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  4944. return 1;
  4945. op = s[(*i)++];
  4946. if (!calc_unary(s, len, i, &r, depth))
  4947. return 0;
  4948. if (op == '/') {
  4949. if (r.kind != 4 || !r.num)
  4950. return 0;
  4951. v->px = v->px * 100 / r.num;
  4952. v->pct = v->pct * 100 / r.num;
  4953. v->num = v->num * 100 / r.num;
  4954. } else if (r.kind == 4) {
  4955. v->px = v->px * r.num / 100;
  4956. v->pct = v->pct * r.num / 100;
  4957. v->num = v->num * r.num / 100;
  4958. } else if (v->kind == 4) {
  4959. long k = v->num;
  4960. *v = r;
  4961. v->px = v->px * k / 100;
  4962. v->pct = v->pct * k / 100;
  4963. } else {
  4964. return 0; /* length * length */
  4965. }
  4966. }
  4967. }
  4968. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4969. struct calc_val *v, int depth)
  4970. {
  4971. if (!calc_product(s, len, i, v, depth))
  4972. return 0;
  4973. for (;;) {
  4974. struct calc_val r;
  4975. int neg;
  4976. calc_space(s, len, i);
  4977. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  4978. return 1;
  4979. neg = s[(*i)++] == '-';
  4980. if (!calc_product(s, len, i, &r, depth))
  4981. return 0;
  4982. if ((v->kind == 4) != (r.kind == 4))
  4983. return 0; /* number + length */
  4984. v->px += neg ? -r.px : r.px;
  4985. v->pct += neg ? -r.pct : r.pct;
  4986. v->num += neg ? -r.num : r.num;
  4987. v->kind |= r.kind;
  4988. }
  4989. }
  4990. /* Hundredths -> "12", "-0.5", "33.33". */
  4991. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  4992. {
  4993. char tmp[24];
  4994. unsigned int n = 0, k = 0;
  4995. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  4996. unsigned long w = a / 100, f = a % 100;
  4997. if (v < 0)
  4998. tmp[n++] = '-';
  4999. {
  5000. char rev[12];
  5001. unsigned int r = 0;
  5002. do {
  5003. rev[r++] = (char)('0' + w % 10);
  5004. w /= 10;
  5005. } while (w && r < sizeof(rev));
  5006. while (r)
  5007. tmp[n++] = rev[--r];
  5008. }
  5009. if (f) {
  5010. tmp[n++] = '.';
  5011. tmp[n++] = (char)('0' + f / 10);
  5012. if (f % 10)
  5013. tmp[n++] = (char)('0' + f % 10);
  5014. }
  5015. for (k = 0; k < n && k < cap; k++)
  5016. o[k] = tmp[k];
  5017. return k;
  5018. }
  5019. /* Copy s to out with every foldable calc() replaced; returns the length,
  5020. * or 0 when nothing was folded (the caller keeps the original). */
  5021. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  5022. unsigned int cap)
  5023. {
  5024. unsigned int i = 0, n = 0;
  5025. int changed = 0;
  5026. while (i < len && n + 40 < cap) {
  5027. struct calc_val v;
  5028. unsigned int j = i + 5;
  5029. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  5030. (i == 0 || !is_ident(s[i - 1])))) {
  5031. out[n++] = s[i++];
  5032. continue;
  5033. }
  5034. if (!calc_sum(s, len, &j, &v, 0) ||
  5035. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  5036. out[n++] = s[i++];
  5037. continue;
  5038. }
  5039. j++;
  5040. /* Minified CSS glues tokens to the function's parentheses
  5041. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  5042. * a folded `10px16px` would be one token. */
  5043. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  5044. out[n - 1] != '(' && out[n - 1] != ',')
  5045. out[n++] = ' ';
  5046. if (v.kind == 4) {
  5047. n += calc_fmt(out + n, cap - n, v.num);
  5048. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  5049. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  5050. -50 : 50)) /
  5051. 100 * 100);
  5052. out[n++] = 'p';
  5053. out[n++] = 'x';
  5054. } else if (!v.px) {
  5055. n += calc_fmt(out + n, cap - n, v.pct);
  5056. out[n++] = '%';
  5057. } else {
  5058. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  5059. memcpy(out + n, "calc(", 5);
  5060. n += 5;
  5061. n += calc_fmt(out + n, cap - n, v.pct);
  5062. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  5063. n += 4;
  5064. n += calc_fmt(out + n, cap - n, px * 100);
  5065. memcpy(out + n, "px)", 3);
  5066. n += 3;
  5067. }
  5068. i = j;
  5069. changed = 1;
  5070. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  5071. s[i] != ',' && s[i] != '!' && s[i] != '}')
  5072. out[n++] = ' ';
  5073. }
  5074. if (!changed || i < len)
  5075. return 0;
  5076. out[n] = '\0';
  5077. return n;
  5078. }
  5079. static int css_has_calc(const char *s, unsigned int len)
  5080. {
  5081. unsigned int i;
  5082. for (i = 0; i + 5 <= len; i++)
  5083. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  5084. return 1;
  5085. return 0;
  5086. }
  5087. static void parse_decls(struct br_doc *d, const char *s,
  5088. unsigned int len, struct br_decl *out,
  5089. unsigned int *nout)
  5090. {
  5091. static char expanded[4096];
  5092. static char folded[4096];
  5093. if (css_has_var(s, len)) {
  5094. unsigned int elen;
  5095. if (!css_env_active)
  5096. css_local_collect(s, len);
  5097. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  5098. 0);
  5099. if (!css_env_active)
  5100. css_nlocal = 0;
  5101. if (elen) {
  5102. s = expanded;
  5103. len = elen;
  5104. }
  5105. }
  5106. if (css_has_calc(s, len)) {
  5107. unsigned int flen = css_fold_calc(s, len, folded,
  5108. sizeof(folded));
  5109. if (flen) {
  5110. s = folded;
  5111. len = flen;
  5112. }
  5113. }
  5114. unsigned int i = 0;
  5115. *nout = 0;
  5116. while (i < len && *nout < BR_DECLS) {
  5117. unsigned int pstart, pend, vstart, vend, k;
  5118. char prop[24];
  5119. int important = 0;
  5120. while (i < len && (is_space(s[i]) || s[i] == ';'))
  5121. i++;
  5122. if (i >= len)
  5123. break;
  5124. /* A nested at-rule (@media/@supports) inside the declaration
  5125. * block carries its own selector context; skip it rather than
  5126. * reading its prelude as a declaration. */
  5127. if (s[i] == '@') {
  5128. while (i < len && s[i] != '{' && s[i] != ';')
  5129. i++;
  5130. if (i < len && s[i] == '{')
  5131. i = css_skip_nested(s, len, i + 1);
  5132. else if (i < len)
  5133. i++;
  5134. continue;
  5135. }
  5136. pstart = i;
  5137. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  5138. i++;
  5139. if (i < len && s[i] == '{') {
  5140. /* Nested style rule: skip its block. */
  5141. i = css_skip_nested(s, len, i + 1);
  5142. continue;
  5143. }
  5144. pend = i;
  5145. if (i >= len || s[i] != ':')
  5146. continue;
  5147. i++;
  5148. vstart = i;
  5149. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  5150. * does not end the value. */
  5151. i = css_value_end(s, len, i);
  5152. vend = i;
  5153. while (pstart < pend && is_space(s[pstart]))
  5154. pstart++;
  5155. while (pend > pstart && is_space(s[pend - 1]))
  5156. pend--;
  5157. while (vstart < vend && is_space(s[vstart]))
  5158. vstart++;
  5159. while (vend > vstart && is_space(s[vend - 1]))
  5160. vend--;
  5161. if (vend - vstart >= 10 &&
  5162. ci_eq_n(s + vend - 10, "!important", 10)) {
  5163. important = 1;
  5164. vend -= 10;
  5165. while (vend > vstart && is_space(s[vend - 1]))
  5166. vend--;
  5167. }
  5168. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  5169. char c = s[pstart + k];
  5170. if (c >= 'A' && c <= 'Z')
  5171. c = (char)(c - 'A' + 'a');
  5172. prop[k] = c;
  5173. }
  5174. prop[k] = '\0';
  5175. if (str_eq(prop, "color")) {
  5176. unsigned short c;
  5177. if (parse_color(s + vstart, vend - vstart, &c))
  5178. decl_add(out, nout, PROP_COLOR, c, important);
  5179. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  5180. /* SVG paint only: an HTML element's fill never paints
  5181. * its background (tagesschau sets fill on buttons). */
  5182. int is_fill = prop[0] == 'f';
  5183. unsigned short c;
  5184. int mode = paint_mode(s + vstart, vend - vstart, &c);
  5185. if (mode) {
  5186. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  5187. PROP_STROKE_MODE, mode, important);
  5188. if (mode == BR_PAINT_COLOR)
  5189. decl_add(out, nout, is_fill ? PROP_FILL :
  5190. PROP_STROKE, (short)c,
  5191. important);
  5192. }
  5193. } else if (str_eq(prop, "stroke-width")) {
  5194. int sw = svg_len16(s + vstart, vend - vstart);
  5195. if (sw >= 0)
  5196. decl_add(out, nout, PROP_STROKE_W, sw,
  5197. important);
  5198. } else if (str_eq(prop, "fill-rule")) {
  5199. decl_add(out, nout, PROP_FILL_RULE,
  5200. contains_ci_len(s + vstart, vend - vstart,
  5201. "evenodd") ? 1 : 0,
  5202. important);
  5203. } else if (str_eq(prop, "text-anchor")) {
  5204. int a = BR_ALIGN_LEFT;
  5205. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  5206. a = BR_ALIGN_CENTER;
  5207. else if (contains_ci_len(s + vstart, vend - vstart,
  5208. "end"))
  5209. a = BR_ALIGN_RIGHT;
  5210. decl_add(out, nout, PROP_ALIGN, a, important);
  5211. } else if (str_eq(prop, "background-image")) {
  5212. add_bg_image_decl(out, nout, s + vstart,
  5213. vend - vstart, important);
  5214. } else if (str_eq(prop, "background-repeat")) {
  5215. int r = 0;
  5216. if (contains_ci_len(s + vstart, vend - vstart,
  5217. "no-repeat"))
  5218. r = 1;
  5219. else if (contains_ci_len(s + vstart, vend - vstart,
  5220. "repeat-x"))
  5221. r = 2;
  5222. else if (contains_ci_len(s + vstart, vend - vstart,
  5223. "repeat-y"))
  5224. r = 3;
  5225. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  5226. } else if (str_eq(prop, "background-position")) {
  5227. add_bg_position(out, nout, s + vstart, vend - vstart,
  5228. important);
  5229. } else if (str_eq(prop, "background-size")) {
  5230. add_bg_size(out, nout, s + vstart, vend - vstart,
  5231. important);
  5232. } else if (str_eq(prop, "background-color") ||
  5233. str_eq(prop, "background")) {
  5234. unsigned short c;
  5235. int is_bg = str_eq(prop, "background");
  5236. static char bgv[1024];
  5237. const char *vs = s + vstart;
  5238. unsigned int vl = vend - vstart;
  5239. if (is_bg) {
  5240. add_bg_image_decl(out, nout, s + vstart,
  5241. vend - vstart, important);
  5242. /* Keywords and colours are read with url()
  5243. * contents blanked: a data: SVG carries its
  5244. * own fill="rgba(...)". */
  5245. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  5246. vs = bgv;
  5247. add_bg_shorthand_extra(out, nout,
  5248. vs, vl,
  5249. important);
  5250. if (contains_ci_len(vs, vl,
  5251. "no-repeat"))
  5252. decl_add(out, nout, PROP_BG_REPEAT, 1,
  5253. important);
  5254. else if (contains_ci_len(vs, vl,
  5255. "repeat-x"))
  5256. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5257. important);
  5258. else if (contains_ci_len(vs, vl,
  5259. "repeat-y"))
  5260. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5261. important);
  5262. }
  5263. /* `transparent`/`none` are real values: they must
  5264. * beat a lower-specificity colour (and tell form
  5265. * controls the author styled them). */
  5266. if (color_is_transparent(vs, vl) ||
  5267. (vl == 4 &&
  5268. ci_eq_n(vs, "none", 4)))
  5269. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5270. else if (parse_color(vs, vl, &c))
  5271. decl_add(out, nout, PROP_BG, c, important);
  5272. else if (is_bg &&
  5273. find_color_last(vs, vl, &c))
  5274. decl_add(out, nout, PROP_BG, c, important);
  5275. } else if (str_eq(prop, "font-size")) {
  5276. unsigned short raw;
  5277. unsigned char unit;
  5278. if (parse_font_size(s + vstart, vend - vstart,
  5279. &raw, &unit)) {
  5280. decl_add(out, nout, PROP_FONT_PX, raw,
  5281. important);
  5282. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5283. important);
  5284. }
  5285. } else if (str_eq(prop, "font-weight")) {
  5286. int bold = 0;
  5287. unsigned int j;
  5288. if (vend - vstart >= 4 &&
  5289. (ci_eq_n(s + vstart, "bold", 4) ||
  5290. (vend - vstart >= 6 &&
  5291. ci_eq_n(s + vstart, "bolder", 6))))
  5292. bold = 1;
  5293. else {
  5294. int w = 0;
  5295. for (j = vstart; j < vend; j++)
  5296. if (s[j] >= '0' && s[j] <= '9')
  5297. w = w * 10 + (s[j] - '0');
  5298. /* CSS font matching: with only a 400 and a
  5299. * 700 face, 500 (Infima's "semibold" menus)
  5300. * resolves to the regular face; 600 and up
  5301. * to the bold one. */
  5302. if (w >= 600)
  5303. bold = 1;
  5304. }
  5305. decl_add(out, nout, PROP_BOLD, bold, important);
  5306. } else if (str_eq(prop, "font-style")) {
  5307. int italic = (vend - vstart >= 6 &&
  5308. ci_eq_n(s + vstart, "italic", 6));
  5309. decl_add(out, nout, PROP_ITALIC, italic, important);
  5310. } else if (str_eq(prop, "font-family")) {
  5311. unsigned int j;
  5312. int mono = 0;
  5313. int face = font_family_id(s + vstart, vend - vstart);
  5314. for (j = vstart; j + 4 <= vend; j++)
  5315. if (ci_eq_n(s + j, "mono", 4))
  5316. mono = 1;
  5317. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5318. decl_add(out, nout, PROP_MONO, mono, important);
  5319. } else if (str_eq(prop, "font")) {
  5320. add_font_shorthand(out, nout, s + vstart,
  5321. vend - vstart, important);
  5322. } else if (str_eq(prop, "text-align")) {
  5323. int a = BR_ALIGN_LEFT;
  5324. if (vend - vstart >= 6 &&
  5325. ci_eq_n(s + vstart, "center", 6))
  5326. a = BR_ALIGN_CENTER;
  5327. else if (vend - vstart >= 5 &&
  5328. ci_eq_n(s + vstart, "right", 5))
  5329. a = BR_ALIGN_RIGHT;
  5330. decl_add(out, nout, PROP_ALIGN, a, important);
  5331. } else if (str_eq(prop, "text-decoration")) {
  5332. int u = 0, stv = 0, ov = 0;
  5333. if (contains_ci_len(s + vstart, vend - vstart,
  5334. "underline"))
  5335. u = 1;
  5336. if (contains_ci_len(s + vstart, vend - vstart,
  5337. "line-through"))
  5338. stv = 1;
  5339. if (contains_ci_len(s + vstart, vend - vstart,
  5340. "overline"))
  5341. ov = 1;
  5342. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5343. decl_add(out, nout, PROP_STRIKE, stv, important);
  5344. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5345. } else if (str_eq(prop, "cursor")) {
  5346. int c = parse_cursor(s + vstart, vend - vstart);
  5347. if (c >= 0)
  5348. decl_add(out, nout, PROP_CURSOR, c, important);
  5349. } else if (str_eq(prop, "text-transform")) {
  5350. int u = (vend - vstart >= 9 &&
  5351. ci_eq_n(s + vstart, "uppercase", 9));
  5352. decl_add(out, nout, PROP_UPPER, u, important);
  5353. } else if (str_eq(prop, "white-space")) {
  5354. int nw = contains_ci_len(s + vstart, vend - vstart,
  5355. "nowrap") ||
  5356. contains_ci_len(s + vstart, vend - vstart,
  5357. "pre");
  5358. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5359. } else if (str_eq(prop, "display")) {
  5360. decl_add(out, nout, PROP_DISPLAY,
  5361. parse_display(s + vstart, vend - vstart),
  5362. important);
  5363. } else if (str_eq(prop, "visibility")) {
  5364. int h = contains_ci_len(s + vstart, vend - vstart,
  5365. "hidden") ||
  5366. contains_ci_len(s + vstart, vend - vstart,
  5367. "collapse");
  5368. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5369. } else if (str_eq(prop, "opacity")) {
  5370. unsigned int q;
  5371. int h = 1, any = 0;
  5372. for (q = vstart; q < vend; q++) {
  5373. if (s[q] >= '1' && s[q] <= '9') {
  5374. h = 0;
  5375. break;
  5376. }
  5377. if (s[q] >= '0' && s[q] <= '9')
  5378. any = 1;
  5379. }
  5380. if (!any || contains_ci_len(s + vstart, vend - vstart,
  5381. "inherit"))
  5382. h = 0;
  5383. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  5384. } else if (str_eq(prop, "overflow") ||
  5385. str_eq(prop, "overflow-x") ||
  5386. str_eq(prop, "overflow-y")) {
  5387. int h = contains_ci_len(s + vstart, vend - vstart,
  5388. "hidden") ||
  5389. contains_ci_len(s + vstart, vend - vstart,
  5390. "clip");
  5391. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5392. } else if (str_eq(prop, "float")) {
  5393. int f = 0;
  5394. if (vend - vstart >= 4 &&
  5395. ci_eq_n(s + vstart, "left", 4))
  5396. f = 1;
  5397. else if (vend - vstart >= 5 &&
  5398. ci_eq_n(s + vstart, "right", 5))
  5399. f = 2;
  5400. decl_add(out, nout, PROP_FLOAT, f, important);
  5401. } else if (str_eq(prop, "clear")) {
  5402. int c = 0;
  5403. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5404. c = 3;
  5405. else if (contains_ci_len(s + vstart, vend - vstart,
  5406. "left"))
  5407. c = 1;
  5408. else if (contains_ci_len(s + vstart, vend - vstart,
  5409. "right"))
  5410. c = 2;
  5411. decl_add(out, nout, PROP_CLEAR, c, important);
  5412. } else if (str_eq(prop, "flex-grow") ||
  5413. str_eq(prop, "flex")) {
  5414. unsigned int q = vstart;
  5415. int g = 0;
  5416. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5417. q++;
  5418. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5419. ci_eq_n(s + q, "auto", 4))
  5420. g = 1; /* flex: auto = 1 1 auto */
  5421. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5422. g = g * 10 + (s[q] - '0');
  5423. q++;
  5424. }
  5425. if (g > 100)
  5426. g = 100;
  5427. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5428. if (str_eq(prop, "flex")) {
  5429. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5430. * basis is the item's main size, which our flex
  5431. * row reads from width (Infima's grid:
  5432. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5433. * of the grow number, then any unitless shrink. */
  5434. unsigned int t;
  5435. while (q < vend && (s[q] == '.' ||
  5436. (s[q] >= '0' && s[q] <= '9')))
  5437. q++;
  5438. for (t = 0; t < 2 && q < vend; t++) {
  5439. unsigned int st, e, k2;
  5440. unsigned short bv;
  5441. unsigned char bk;
  5442. while (q < vend && is_space(s[q]))
  5443. q++;
  5444. st = q;
  5445. while (q < vend && !is_space(s[q]))
  5446. q++;
  5447. e = q;
  5448. for (k2 = st; k2 < e; k2++)
  5449. if (!(s[k2] == '.' ||
  5450. (s[k2] >= '0' &&
  5451. s[k2] <= '9')))
  5452. break;
  5453. if (k2 == e)
  5454. continue; /* shrink */
  5455. if (parse_size(s + st, e - st, &bv,
  5456. &bk) && bk && bv) {
  5457. decl_add(out, nout, PROP_WIDTH,
  5458. bv, important);
  5459. decl_add(out, nout,
  5460. PROP_WIDTH_KIND, bk,
  5461. important);
  5462. }
  5463. break;
  5464. }
  5465. }
  5466. } else if (str_eq(prop, "flex-basis")) {
  5467. unsigned short bv;
  5468. unsigned char bk;
  5469. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5470. bk && bv) {
  5471. decl_add(out, nout, PROP_WIDTH, bv, important);
  5472. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5473. important);
  5474. }
  5475. } else if (str_eq(prop, "flex-direction")) {
  5476. decl_add(out, nout, PROP_FLEX_COL,
  5477. contains_ci_len(s + vstart, vend - vstart,
  5478. "column"), important);
  5479. } else if (str_eq(prop, "flex-wrap")) {
  5480. int wrap = 0;
  5481. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5482. wrap = 2;
  5483. else if (contains_ci_len(s + vstart, vend - vstart,
  5484. "wrap"))
  5485. wrap = 1;
  5486. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5487. } else if (str_eq(prop, "justify-content")) {
  5488. int j = 0;
  5489. if (contains_ci_len(s + vstart, vend - vstart,
  5490. "space-between"))
  5491. j = 3;
  5492. else if (contains_ci_len(s + vstart, vend - vstart,
  5493. "space-around"))
  5494. j = 4;
  5495. else if (contains_ci_len(s + vstart, vend - vstart,
  5496. "space-evenly"))
  5497. j = 5;
  5498. else if (contains_ci_len(s + vstart, vend - vstart,
  5499. "center"))
  5500. j = 1;
  5501. else if (contains_ci_len(s + vstart, vend - vstart,
  5502. "flex-end") ||
  5503. contains_ci_len(s + vstart, vend - vstart,
  5504. "end"))
  5505. j = 2;
  5506. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5507. } else if (str_eq(prop, "align-self")) {
  5508. /* 0 auto (the container's align-items), else
  5509. * align-items + 1. */
  5510. int a = 1;
  5511. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5512. a = 0;
  5513. else if (contains_ci_len(s + vstart, vend - vstart,
  5514. "center"))
  5515. a = 2;
  5516. else if (contains_ci_len(s + vstart, vend - vstart,
  5517. "flex-end") ||
  5518. contains_ci_len(s + vstart, vend - vstart,
  5519. "end"))
  5520. a = 3;
  5521. else if (contains_ci_len(s + vstart, vend - vstart,
  5522. "start") ||
  5523. contains_ci_len(s + vstart, vend - vstart,
  5524. "baseline"))
  5525. a = 4;
  5526. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5527. } else if (str_eq(prop, "align-items")) {
  5528. int a = 0;
  5529. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5530. a = 1;
  5531. else if (contains_ci_len(s + vstart, vend - vstart,
  5532. "flex-end") ||
  5533. contains_ci_len(s + vstart, vend - vstart,
  5534. "end"))
  5535. a = 2;
  5536. else if (contains_ci_len(s + vstart, vend - vstart,
  5537. "start") ||
  5538. contains_ci_len(s + vstart, vend - vstart,
  5539. "baseline"))
  5540. a = 3; /* not stretched */
  5541. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5542. } else if (str_eq(prop, "position")) {
  5543. int p = 0;
  5544. if (contains_ci_len(s + vstart, vend - vstart,
  5545. "absolute"))
  5546. p = 1;
  5547. else if (contains_ci_len(s + vstart, vend - vstart,
  5548. "fixed"))
  5549. p = 2;
  5550. else if (contains_ci_len(s + vstart, vend - vstart,
  5551. "relative"))
  5552. p = 3;
  5553. else if (contains_ci_len(s + vstart, vend - vstart,
  5554. "sticky"))
  5555. p = 4;
  5556. decl_add(out, nout, PROP_POS, p, important);
  5557. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5558. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5559. int which = str_eq(prop, "left") ? 0 :
  5560. str_eq(prop, "top") ? 1 :
  5561. str_eq(prop, "right") ? 2 : 3;
  5562. unsigned int pos = 0;
  5563. if (!(vend - vstart >= 4 &&
  5564. ci_eq_n(s + vstart, "auto", 4))) {
  5565. int v = parse_len(s + vstart, vend - vstart, &pos);
  5566. if (v > 32000)
  5567. v = 32000;
  5568. else if (v < -32000)
  5569. v = -32000;
  5570. decl_add(out, nout,
  5571. (unsigned char)(which == 0 ? PROP_LEFT :
  5572. which == 1 ? PROP_TOP :
  5573. which == 2 ? PROP_RIGHT :
  5574. PROP_BOTTOM),
  5575. v, important);
  5576. decl_add(out, nout,
  5577. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5578. which == 1 ? PROP_TOP_SET :
  5579. which == 2 ? PROP_RIGHT_SET :
  5580. PROP_BOTTOM_SET),
  5581. 1, important);
  5582. }
  5583. } else if (str_eq(prop, "z-index")) {
  5584. unsigned int q = vstart;
  5585. int sign = 1;
  5586. int v = 0;
  5587. int any = 0;
  5588. if (q < vend && s[q] == '-') {
  5589. sign = -1;
  5590. q++;
  5591. }
  5592. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5593. v = v * 10 + (s[q] - '0');
  5594. any = 1;
  5595. q++;
  5596. }
  5597. if (any) {
  5598. v *= sign;
  5599. if (v > 32000)
  5600. v = 32000;
  5601. else if (v < -32000)
  5602. v = -32000;
  5603. decl_add(out, nout, PROP_ZINDEX, v, important);
  5604. decl_add(out, nout, PROP_ZSET, 1, important);
  5605. } else if (vend - vstart >= 4 &&
  5606. ci_eq_n(s + vstart, "auto", 4)) {
  5607. decl_add(out, nout, PROP_ZSET, 0, important);
  5608. }
  5609. } else if (str_eq(prop, "transform")) {
  5610. const char *v = s + vstart;
  5611. unsigned int vl = vend - vstart;
  5612. int tx = 0, ty = 0;
  5613. int is_x = contains_ci_len(v, vl, "translatex");
  5614. int is_y = contains_ci_len(v, vl, "translatey");
  5615. unsigned int off = 0, arg;
  5616. if (contains_ci_len(v, vl, "translate")) {
  5617. while (off < vl && v[off] != '(')
  5618. off++;
  5619. if (off < vl)
  5620. off++;
  5621. if (!is_y)
  5622. tx = parse_len(v, vl, &off);
  5623. if (!is_x) {
  5624. while (off < vl &&
  5625. (is_space(v[off]) || v[off] == ','))
  5626. off++;
  5627. ty = parse_len(v, vl, &off);
  5628. }
  5629. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5630. important);
  5631. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5632. important);
  5633. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5634. important);
  5635. }
  5636. if (find_fn(v, vl, "scale", &arg)) {
  5637. unsigned int q = arg;
  5638. signed short sx = 100, sy = 100;
  5639. if (parse_fixed_100(v, vl, &q, &sx)) {
  5640. unsigned int r = q;
  5641. while (r < vl &&
  5642. (is_space(v[r]) || v[r] == ','))
  5643. r++;
  5644. if (!parse_fixed_100(v, vl, &r, &sy))
  5645. sy = sx;
  5646. decl_add(out, nout, PROP_SCALE_X, sx,
  5647. important);
  5648. decl_add(out, nout, PROP_SCALE_Y, sy,
  5649. important);
  5650. decl_add(out, nout,
  5651. PROP_TRANSFORM_PAINT, 1,
  5652. important);
  5653. decl_add(out, nout,
  5654. PROP_TRANSFORM_SET, 1,
  5655. important);
  5656. }
  5657. }
  5658. if (find_fn(v, vl, "rotate", &arg)) {
  5659. unsigned int q = arg;
  5660. signed short deg;
  5661. if (parse_fixed_100(v, vl, &q, &deg)) {
  5662. decl_add(out, nout, PROP_ROTATE, deg,
  5663. important);
  5664. decl_add(out, nout,
  5665. PROP_TRANSFORM_PAINT, 1,
  5666. important);
  5667. decl_add(out, nout,
  5668. PROP_TRANSFORM_SET, 1,
  5669. important);
  5670. }
  5671. }
  5672. } else if (str_eq(prop, "transform-origin")) {
  5673. add_transform_origin(out, nout, s + vstart,
  5674. vend - vstart, important);
  5675. } else if (str_eq(prop, "grid-template-columns") ||
  5676. str_eq(prop, "grid-template")) {
  5677. const char *v = s + vstart;
  5678. unsigned int vl = vend - vstart;
  5679. unsigned short val[2];
  5680. unsigned char kind[2];
  5681. int n, q;
  5682. if (str_eq(prop, "grid-template")) {
  5683. for (q = 0; q < (int)vl; q++)
  5684. if (v[q] == '/') {
  5685. q++;
  5686. while (q < (int)vl && is_space(v[q]))
  5687. q++;
  5688. v += q;
  5689. vl -= (unsigned int)q;
  5690. break;
  5691. }
  5692. }
  5693. if (grid_repeat_count(v, vl) >= 1) {
  5694. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5695. * Tailwind's grid-cols-N. N equal columns
  5696. * (GRID_FIXED + N); one column is plain block
  5697. * flow, which also resets a smaller breakpoint's
  5698. * count. */
  5699. int cnt = grid_repeat_count(v, vl);
  5700. decl_add(out, nout, PROP_GRID_AUTO,
  5701. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5702. important);
  5703. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5704. important);
  5705. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5706. contains_ci_len(v, vl, "auto-fill")) {
  5707. unsigned int mp = 0;
  5708. int found = 0;
  5709. unsigned short mw = 200;
  5710. unsigned char mk = 1;
  5711. for (q = 0; q + 7 <= (int)vl; q++)
  5712. if (ci_eq_n(v + q, "minmax(", 7)) {
  5713. mp = (unsigned int)q + 7;
  5714. found = 1;
  5715. break;
  5716. }
  5717. if (found)
  5718. parse_grid_track(v, vl, &mp, &mw, &mk);
  5719. if (mk != 1 || mw < 80)
  5720. mw = 200;
  5721. if (mw > 480)
  5722. mw = 480;
  5723. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5724. decl_add(out, nout, PROP_WIDTH, mw, important);
  5725. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5726. } else {
  5727. n = parse_grid_cols(v, vl, val, kind);
  5728. if (n > 0) {
  5729. decl_add(out, nout, PROP_WIDTH, val[0],
  5730. important);
  5731. decl_add(out, nout, PROP_WIDTH_KIND,
  5732. kind[0], important);
  5733. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5734. important);
  5735. }
  5736. if (n > 1) {
  5737. decl_add(out, nout, PROP_MAXW, val[1],
  5738. important);
  5739. decl_add(out, nout, PROP_MAXW_KIND,
  5740. kind[1], important);
  5741. }
  5742. }
  5743. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5744. /* From the value, not the block start: gap was
  5745. * ignored on nearly every site. */
  5746. unsigned int gp = vstart;
  5747. unsigned short gv = 0;
  5748. unsigned char gk = 1;
  5749. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5750. if (gv > 120)
  5751. gv = 120;
  5752. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5753. }
  5754. } else if (str_eq(prop, "grid-area")) {
  5755. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5756. if (slot)
  5757. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5758. } else if (str_eq(prop, "width") ||
  5759. str_eq(prop, "max-width") ||
  5760. str_eq(prop, "min-width") ||
  5761. str_eq(prop, "height") ||
  5762. str_eq(prop, "max-height") ||
  5763. str_eq(prop, "min-height")) {
  5764. unsigned short v;
  5765. unsigned char kind;
  5766. if (vend - vstart >= 5 &&
  5767. ci_eq_n(s + vstart, "calc(", 5)) {
  5768. int pct, px;
  5769. if (parse_calc(s + vstart + 5,
  5770. vend - vstart - 5, &pct, &px)) {
  5771. if (str_eq(prop, "width")) {
  5772. decl_add(out, nout, PROP_WIDTH,
  5773. 0, important);
  5774. decl_add(out, nout,
  5775. PROP_WIDTH_KIND, 3,
  5776. important);
  5777. decl_add(out, nout,
  5778. PROP_WIDTH_CALC_PCT,
  5779. pct, important);
  5780. decl_add(out, nout,
  5781. PROP_WIDTH_CALC_PX,
  5782. px, important);
  5783. } else if (str_eq(prop, "max-width")) {
  5784. decl_add(out, nout, PROP_MAXW,
  5785. 0, important);
  5786. decl_add(out, nout,
  5787. PROP_MAXW_KIND, 3,
  5788. important);
  5789. decl_add(out, nout,
  5790. PROP_MAXW_CALC_PCT,
  5791. pct, important);
  5792. decl_add(out, nout,
  5793. PROP_MAXW_CALC_PX,
  5794. px, important);
  5795. } else if (str_eq(prop, "min-width")) {
  5796. decl_add(out, nout, PROP_MINW,
  5797. 0, important);
  5798. decl_add(out, nout,
  5799. PROP_MINW_KIND, 3,
  5800. important);
  5801. decl_add(out, nout,
  5802. PROP_MINW_CALC_PCT,
  5803. pct, important);
  5804. decl_add(out, nout,
  5805. PROP_MINW_CALC_PX,
  5806. px, important);
  5807. } else if (str_eq(prop, "height")) {
  5808. decl_add(out, nout, PROP_HEIGHT,
  5809. 0, important);
  5810. decl_add(out, nout,
  5811. PROP_HEIGHT_KIND, 3,
  5812. important);
  5813. decl_add(out, nout,
  5814. PROP_HEIGHT_CALC_PCT,
  5815. pct, important);
  5816. decl_add(out, nout,
  5817. PROP_HEIGHT_CALC_PX,
  5818. px, important);
  5819. } else if (str_eq(prop, "max-height")) {
  5820. decl_add(out, nout, PROP_MAXH,
  5821. 0, important);
  5822. decl_add(out, nout,
  5823. PROP_MAXH_KIND, 3,
  5824. important);
  5825. decl_add(out, nout,
  5826. PROP_MAXH_CALC_PCT,
  5827. pct, important);
  5828. decl_add(out, nout,
  5829. PROP_MAXH_CALC_PX,
  5830. px, important);
  5831. } else {
  5832. decl_add(out, nout, PROP_MINH,
  5833. 0, important);
  5834. decl_add(out, nout,
  5835. PROP_MINH_KIND, 3,
  5836. important);
  5837. decl_add(out, nout,
  5838. PROP_MINH_CALC_PCT,
  5839. pct, important);
  5840. decl_add(out, nout,
  5841. PROP_MINH_CALC_PX,
  5842. px, important);
  5843. }
  5844. }
  5845. continue;
  5846. }
  5847. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  5848. if (str_eq(prop, "width")) {
  5849. decl_add(out, nout, PROP_WIDTH, v, important);
  5850. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  5851. important);
  5852. } else if (str_eq(prop, "max-width")) {
  5853. decl_add(out, nout, PROP_MAXW, v, important);
  5854. decl_add(out, nout, PROP_MAXW_KIND, kind,
  5855. important);
  5856. } else if (str_eq(prop, "min-width")) {
  5857. decl_add(out, nout, PROP_MINW, v, important);
  5858. decl_add(out, nout, PROP_MINW_KIND, kind,
  5859. important);
  5860. } else if (str_eq(prop, "max-height")) {
  5861. decl_add(out, nout, PROP_MAXH, v, important);
  5862. decl_add(out, nout, PROP_MAXH_KIND, kind,
  5863. important);
  5864. } else if (str_eq(prop, "min-height")) {
  5865. decl_add(out, nout, PROP_MINH, v, important);
  5866. decl_add(out, nout, PROP_MINH_KIND, kind,
  5867. important);
  5868. } else {
  5869. decl_add(out, nout, PROP_HEIGHT, v, important);
  5870. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  5871. important);
  5872. }
  5873. }
  5874. } else if (str_eq(prop, "content")) {
  5875. decl_add(out, nout, PROP_CONTENT,
  5876. css_collect_only ? 0 :
  5877. parse_content(d, s + vstart, vend - vstart),
  5878. important);
  5879. } else if (str_eq(prop, "aspect-ratio")) {
  5880. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  5881. * preferred height of an auto-height box. */
  5882. unsigned int q = vstart;
  5883. unsigned int a[2] = { 0, 1 }, k;
  5884. for (k = 0; k < 2; k++) {
  5885. unsigned int whole = 0, frac = 0, sc = 1;
  5886. while (q < vend && is_space(s[q]))
  5887. q++;
  5888. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  5889. s[q] != '.'))
  5890. break;
  5891. while (q < vend && s[q] >= '0' && s[q] <= '9')
  5892. whole = whole * 10 +
  5893. (unsigned int)(s[q++] - '0');
  5894. if (q < vend && s[q] == '.') {
  5895. q++;
  5896. while (q < vend && s[q] >= '0' &&
  5897. s[q] <= '9') {
  5898. if (sc < 100) {
  5899. frac = frac * 10 +
  5900. (unsigned int)(s[q] - '0');
  5901. sc *= 10;
  5902. }
  5903. q++;
  5904. }
  5905. }
  5906. a[k] = whole * 100 + frac * (100 / sc);
  5907. while (q < vend && is_space(s[q]))
  5908. q++;
  5909. if (k == 0 && (q >= vend || s[q] != '/')) {
  5910. a[1] = 100;
  5911. break;
  5912. }
  5913. q++;
  5914. }
  5915. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5916. a[0] = a[1] = 0;
  5917. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5918. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5919. } else if (str_eq(prop, "box-sizing")) {
  5920. int bb = (vend - vstart >= 10 &&
  5921. ci_eq_n(s + vstart, "border-box", 10));
  5922. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5923. } else if (str_eq(prop, "line-height")) {
  5924. unsigned short px;
  5925. unsigned char pct;
  5926. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5927. if (px) {
  5928. decl_add(out, nout, PROP_LH_PX, px, important);
  5929. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5930. } else {
  5931. decl_add(out, nout, PROP_LH_PX, 0, important);
  5932. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5933. }
  5934. }
  5935. } else if (str_eq(prop, "list-style") ||
  5936. str_eq(prop, "list-style-type")) {
  5937. int ls = BR_LIST_AUTO;
  5938. if (contains_ci_len(s + vstart, vend - vstart,
  5939. "none"))
  5940. ls = BR_LIST_NONE;
  5941. else if (contains_ci_len(s + vstart, vend - vstart,
  5942. "circle"))
  5943. ls = BR_LIST_CIRCLE;
  5944. else if (contains_ci_len(s + vstart, vend - vstart,
  5945. "square"))
  5946. ls = BR_LIST_SQUARE;
  5947. else if (contains_ci_len(s + vstart, vend - vstart,
  5948. "lower-alpha") ||
  5949. contains_ci_len(s + vstart, vend - vstart,
  5950. "lower-latin"))
  5951. ls = BR_LIST_LOWER_ALPHA;
  5952. else if (contains_ci_len(s + vstart, vend - vstart,
  5953. "upper-alpha") ||
  5954. contains_ci_len(s + vstart, vend - vstart,
  5955. "upper-latin"))
  5956. ls = BR_LIST_UPPER_ALPHA;
  5957. else if (contains_ci_len(s + vstart, vend - vstart,
  5958. "lower-roman"))
  5959. ls = BR_LIST_LOWER_ROMAN;
  5960. else if (contains_ci_len(s + vstart, vend - vstart,
  5961. "upper-roman"))
  5962. ls = BR_LIST_UPPER_ROMAN;
  5963. else if (contains_ci_len(s + vstart, vend - vstart,
  5964. "decimal"))
  5965. ls = BR_LIST_DECIMAL;
  5966. else if (contains_ci_len(s + vstart, vend - vstart,
  5967. "disc"))
  5968. ls = BR_LIST_DISC;
  5969. decl_add(out, nout, PROP_LIST_NONE,
  5970. ls == BR_LIST_NONE, important);
  5971. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5972. } else if (str_eq(prop, "border-radius") ||
  5973. str_eq(prop, "border-top-left-radius") ||
  5974. str_eq(prop, "border-top-right-radius") ||
  5975. str_eq(prop, "border-bottom-right-radius") ||
  5976. str_eq(prop, "border-bottom-left-radius")) {
  5977. unsigned short v[4];
  5978. unsigned char k[4];
  5979. unsigned int pos = 0;
  5980. int n = 0;
  5981. while (pos < vend - vstart && n < 4) {
  5982. while (pos < vend - vstart &&
  5983. is_space(s[vstart + pos]))
  5984. pos++;
  5985. if (pos >= vend - vstart ||
  5986. s[vstart + pos] == '/')
  5987. break;
  5988. if (!parse_radius_value(s + vstart,
  5989. vend - vstart, &pos,
  5990. &v[n], &k[n]))
  5991. break;
  5992. n++;
  5993. }
  5994. if (n) {
  5995. unsigned short r[4];
  5996. unsigned char rk[4];
  5997. int i;
  5998. if (n == 1) {
  5999. for (i = 0; i < 4; i++) {
  6000. r[i] = v[0];
  6001. rk[i] = k[0];
  6002. }
  6003. } else if (n == 2) {
  6004. r[0] = r[2] = v[0];
  6005. r[1] = r[3] = v[1];
  6006. rk[0] = rk[2] = k[0];
  6007. rk[1] = rk[3] = k[1];
  6008. } else if (n == 3) {
  6009. r[0] = v[0];
  6010. r[1] = r[3] = v[1];
  6011. r[2] = v[2];
  6012. rk[0] = k[0];
  6013. rk[1] = rk[3] = k[1];
  6014. rk[2] = k[2];
  6015. } else {
  6016. for (i = 0; i < 4; i++) {
  6017. r[i] = v[i];
  6018. rk[i] = k[i];
  6019. }
  6020. }
  6021. for (i = 0; i < 4; i++)
  6022. r[i] = rk[i] == 2 ? 255 :
  6023. (r[i] > 255 ? 255 : r[i]);
  6024. if (str_eq(prop, "border-top-left-radius")) {
  6025. decl_add(out, nout, PROP_RADIUS_TL,
  6026. (short)r[0], important);
  6027. } else if (str_eq(prop,
  6028. "border-top-right-radius")) {
  6029. decl_add(out, nout, PROP_RADIUS_TR,
  6030. (short)r[0], important);
  6031. } else if (str_eq(prop,
  6032. "border-bottom-right-radius")) {
  6033. decl_add(out, nout, PROP_RADIUS_BR,
  6034. (short)r[0], important);
  6035. } else if (str_eq(prop,
  6036. "border-bottom-left-radius")) {
  6037. decl_add(out, nout, PROP_RADIUS_BL,
  6038. (short)r[0], important);
  6039. } else {
  6040. decl_add(out, nout, PROP_RADIUS_TL,
  6041. (short)r[0], important);
  6042. decl_add(out, nout, PROP_RADIUS_TR,
  6043. (short)r[1], important);
  6044. decl_add(out, nout, PROP_RADIUS_BR,
  6045. (short)r[2], important);
  6046. decl_add(out, nout, PROP_RADIUS_BL,
  6047. (short)r[3], important);
  6048. }
  6049. }
  6050. } else if (str_eq(prop, "border") ||
  6051. str_eq(prop, "border-top") ||
  6052. str_eq(prop, "border-right") ||
  6053. str_eq(prop, "border-bottom") ||
  6054. str_eq(prop, "border-left") ||
  6055. str_eq(prop, "border-color") ||
  6056. str_eq(prop, "border-width") ||
  6057. str_eq(prop, "border-style") ||
  6058. str_eq(prop, "border-top-width") ||
  6059. str_eq(prop, "border-right-width") ||
  6060. str_eq(prop, "border-bottom-width") ||
  6061. str_eq(prop, "border-left-width") ||
  6062. str_eq(prop, "border-top-color") ||
  6063. str_eq(prop, "border-right-color") ||
  6064. str_eq(prop, "border-bottom-color") ||
  6065. str_eq(prop, "border-left-color") ||
  6066. str_eq(prop, "border-top-style") ||
  6067. str_eq(prop, "border-right-style") ||
  6068. str_eq(prop, "border-bottom-style") ||
  6069. str_eq(prop, "border-left-style")) {
  6070. const char *v = s + vstart;
  6071. unsigned int vl = vend - vstart;
  6072. unsigned int pos = 0, ts, te;
  6073. int widths[4], colors[4], styles[4], trans[4];
  6074. int nw = 0, nc = 0, ns = 0;
  6075. int side = -1;
  6076. int is_wl = str_eq(prop, "border-width");
  6077. int is_cl = str_eq(prop, "border-color");
  6078. int is_sl = str_eq(prop, "border-style");
  6079. int i;
  6080. if (str_eq(prop, "border-top")) side = 0;
  6081. else if (str_eq(prop, "border-right")) side = 1;
  6082. else if (str_eq(prop, "border-bottom")) side = 2;
  6083. else if (str_eq(prop, "border-left")) side = 3;
  6084. else if (str_eq(prop, "border-top-width")) {
  6085. side = 0; is_wl = 2;
  6086. } else if (str_eq(prop, "border-right-width")) {
  6087. side = 1; is_wl = 2;
  6088. } else if (str_eq(prop, "border-bottom-width")) {
  6089. side = 2; is_wl = 2;
  6090. } else if (str_eq(prop, "border-left-width")) {
  6091. side = 3; is_wl = 2;
  6092. } else if (str_eq(prop, "border-top-color")) {
  6093. side = 0; is_cl = 2;
  6094. } else if (str_eq(prop, "border-right-color")) {
  6095. side = 1; is_cl = 2;
  6096. } else if (str_eq(prop, "border-bottom-color")) {
  6097. side = 2; is_cl = 2;
  6098. } else if (str_eq(prop, "border-left-color")) {
  6099. side = 3; is_cl = 2;
  6100. } else if (str_eq(prop, "border-top-style")) {
  6101. side = 0; is_sl = 2;
  6102. } else if (str_eq(prop, "border-right-style")) {
  6103. side = 1; is_sl = 2;
  6104. } else if (str_eq(prop, "border-bottom-style")) {
  6105. side = 2; is_sl = 2;
  6106. } else if (str_eq(prop, "border-left-style")) {
  6107. side = 3; is_sl = 2;
  6108. }
  6109. if (is_wl) {
  6110. while (nw < 4 && next_token(v, vl, &pos,
  6111. &ts, &te)) {
  6112. int x;
  6113. if (!parse_len_token(v + ts, te - ts,
  6114. &x))
  6115. break;
  6116. widths[nw++] = x;
  6117. }
  6118. if (nw) {
  6119. if (is_wl == 1)
  6120. expand_four(widths, nw);
  6121. if (side >= 0 && is_wl == 2) {
  6122. decl_add(out, nout,
  6123. PROP_BW_T + side,
  6124. widths[0], important);
  6125. } else {
  6126. for (i = 0; i < 4; i++)
  6127. decl_add(out, nout,
  6128. PROP_BW_T + i,
  6129. widths[i],
  6130. important);
  6131. }
  6132. }
  6133. } else if (is_cl) {
  6134. unsigned short c;
  6135. int t;
  6136. while (nc < 4 && next_token(v, vl, &pos,
  6137. &ts, &te)) {
  6138. t = color_is_transparent(v + ts,
  6139. te - ts);
  6140. if (!t && !parse_color(v + ts,
  6141. te - ts, &c))
  6142. break;
  6143. colors[nc] = t ? 0 : c;
  6144. trans[nc] = t;
  6145. nc++;
  6146. }
  6147. if (nc) {
  6148. if (is_cl == 1) {
  6149. expand_four(colors, nc);
  6150. expand_four(trans, nc);
  6151. }
  6152. if (side >= 0 && is_cl == 2) {
  6153. decl_add(out, nout,
  6154. PROP_BC_T + side,
  6155. colors[0], important);
  6156. decl_add(out, nout,
  6157. PROP_BT_T + side,
  6158. trans[0], important);
  6159. } else {
  6160. for (i = 0; i < 4; i++) {
  6161. decl_add(out, nout,
  6162. PROP_BC_T + i,
  6163. colors[i],
  6164. important);
  6165. decl_add(out, nout,
  6166. PROP_BT_T + i,
  6167. trans[i],
  6168. important);
  6169. }
  6170. }
  6171. }
  6172. } else if (is_sl) {
  6173. while (ns < 4 && next_token(v, vl, &pos,
  6174. &ts, &te)) {
  6175. int stv;
  6176. if ((te - ts == 4 &&
  6177. ci_eq_n(v + ts, "none", 4)) ||
  6178. (te - ts == 6 &&
  6179. ci_eq_n(v + ts, "hidden", 6)))
  6180. stv = 0;
  6181. else if (te - ts == 6 &&
  6182. ci_eq_n(v + ts,
  6183. "dashed", 6))
  6184. stv = 2;
  6185. else if (te - ts == 6 &&
  6186. ci_eq_n(v + ts,
  6187. "dotted", 6))
  6188. stv = 3;
  6189. else
  6190. stv = 1;
  6191. styles[ns++] = stv;
  6192. }
  6193. if (ns) {
  6194. if (is_sl == 1)
  6195. expand_four(styles, ns);
  6196. if (is_sl == 2 && side >= 0) {
  6197. if (!styles[0])
  6198. decl_add(out, nout,
  6199. PROP_BW_T + side,
  6200. 0, important);
  6201. else
  6202. decl_add(out, nout,
  6203. PROP_BS_T + side,
  6204. styles[0] - 1,
  6205. important);
  6206. } else {
  6207. for (i = 0; i < 4; i++) {
  6208. if (!styles[i])
  6209. decl_add(out, nout,
  6210. PROP_BW_T + i,
  6211. 0,
  6212. important);
  6213. else
  6214. decl_add(out, nout,
  6215. PROP_BS_T + i,
  6216. styles[i] - 1,
  6217. important);
  6218. }
  6219. }
  6220. }
  6221. } else {
  6222. int bw = -1, bc = -1, style_none = 0;
  6223. int bstyle = -1, btrans = 0;
  6224. while (next_token(v, vl, &pos, &ts, &te)) {
  6225. unsigned short c;
  6226. int x;
  6227. if ((te - ts == 4 &&
  6228. ci_eq_n(v + ts, "none", 4)) ||
  6229. (te - ts == 6 &&
  6230. ci_eq_n(v + ts, "hidden", 6))) {
  6231. style_none = 1;
  6232. } else if (te - ts == 6 &&
  6233. ci_eq_n(v + ts, "dashed", 6)) {
  6234. bstyle = BR_BORDER_DASHED;
  6235. } else if (te - ts == 6 &&
  6236. ci_eq_n(v + ts, "dotted", 6)) {
  6237. bstyle = BR_BORDER_DOTTED;
  6238. } else if (te - ts == 5 &&
  6239. ci_eq_n(v + ts, "solid", 5)) {
  6240. bstyle = BR_BORDER_SOLID;
  6241. } else if (parse_len_token(v + ts,
  6242. te - ts, &x)) {
  6243. bw = x;
  6244. } else if (color_is_transparent(v + ts,
  6245. te - ts)) {
  6246. bc = 0;
  6247. btrans = 1;
  6248. } else if (parse_color(v + ts,
  6249. te - ts, &c)) {
  6250. bc = c;
  6251. btrans = 0;
  6252. }
  6253. }
  6254. if (style_none)
  6255. bw = 0;
  6256. if (bc >= 0 && bw < 0)
  6257. bw = 1;
  6258. if (side < 0) {
  6259. if (bw >= 0)
  6260. decl_add(out, nout,
  6261. PROP_BORDER_W, bw,
  6262. important);
  6263. if (bc >= 0)
  6264. decl_add(out, nout,
  6265. PROP_BORDER_COLOR, bc,
  6266. important);
  6267. if (bc >= 0)
  6268. for (i = 0; i < 4; i++)
  6269. decl_add(out, nout,
  6270. PROP_BT_T + i,
  6271. btrans,
  6272. important);
  6273. if (bstyle >= 0)
  6274. for (i = 0; i < 4; i++)
  6275. decl_add(out, nout,
  6276. PROP_BS_T + i,
  6277. bstyle,
  6278. important);
  6279. } else {
  6280. if (bw >= 0)
  6281. decl_add(out, nout,
  6282. PROP_BW_T + side, bw,
  6283. important);
  6284. if (bc >= 0)
  6285. decl_add(out, nout,
  6286. PROP_BC_T + side, bc,
  6287. important);
  6288. if (bc >= 0)
  6289. decl_add(out, nout,
  6290. PROP_BT_T + side,
  6291. btrans, important);
  6292. if (bstyle >= 0)
  6293. decl_add(out, nout,
  6294. PROP_BS_T + side,
  6295. bstyle, important);
  6296. }
  6297. }
  6298. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6299. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6300. unsigned int pos = 0;
  6301. int is_pad = prop[0] == 'p';
  6302. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6303. while (pos < vend - vstart && nv < 4) {
  6304. units[nv] = 0;
  6305. if (parse_margin_calc(s + vstart,
  6306. vend - vstart, &pos,
  6307. &vals[nv], &cpx[nv])) {
  6308. units[nv] = 5;
  6309. } else {
  6310. vals[nv] = parse_margin_len(
  6311. s + vstart, vend - vstart,
  6312. &pos, &units[nv]);
  6313. }
  6314. nv++;
  6315. while (pos < vend - vstart &&
  6316. is_space(s[vstart + pos]))
  6317. pos++;
  6318. }
  6319. if (nv == 1) {
  6320. vals[1] = vals[2] = vals[3] = vals[0];
  6321. units[1] = units[2] = units[3] = units[0];
  6322. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6323. } else if (nv == 2) {
  6324. vals[2] = vals[0];
  6325. vals[3] = vals[1];
  6326. units[2] = units[0];
  6327. units[3] = units[1];
  6328. cpx[2] = cpx[0];
  6329. cpx[3] = cpx[1];
  6330. } else if (nv == 3) {
  6331. vals[3] = vals[1];
  6332. units[3] = units[1];
  6333. cpx[3] = cpx[1];
  6334. }
  6335. if (nv > 0) {
  6336. int k;
  6337. for (k = 0; k < 4; k++) {
  6338. int v = vals[k];
  6339. int u = units[k];
  6340. int au = u == 4;
  6341. if (au)
  6342. v = 0;
  6343. if (is_pad && au)
  6344. u = 0;
  6345. decl_add(out, nout,
  6346. (unsigned char)(base + k), v,
  6347. important);
  6348. decl_add(out, nout,
  6349. (unsigned char)
  6350. ((is_pad ? PROP_PT_U :
  6351. PROP_MT_U) + k),
  6352. u, important);
  6353. if (u == 5)
  6354. decl_add(out, nout,
  6355. (unsigned char)
  6356. ((is_pad ? PROP_PT_CPX :
  6357. PROP_MT_CPX) + k),
  6358. cpx[k], important);
  6359. if (!is_pad && (k == 1 || k == 3))
  6360. decl_add(out, nout,
  6361. k == 1 ? PROP_MR_AUTO :
  6362. PROP_ML_AUTO,
  6363. au, important);
  6364. }
  6365. }
  6366. } else if (str_eq(prop, "margin-top") ||
  6367. str_eq(prop, "margin-right") ||
  6368. str_eq(prop, "margin-bottom") ||
  6369. str_eq(prop, "margin-left") ||
  6370. str_eq(prop, "padding-top") ||
  6371. str_eq(prop, "padding-right") ||
  6372. str_eq(prop, "padding-bottom") ||
  6373. str_eq(prop, "padding-left")) {
  6374. unsigned int pos = 0;
  6375. int unit = 0;
  6376. int v = 0, cpx = 0;
  6377. unsigned char p = PROP_MT;
  6378. const char *side;
  6379. int au;
  6380. if (parse_margin_calc(s + vstart, vend - vstart,
  6381. &pos, &v, &cpx))
  6382. unit = 5;
  6383. else
  6384. v = parse_margin_len(s + vstart,
  6385. vend - vstart, &pos,
  6386. &unit);
  6387. au = unit == 4;
  6388. if (au)
  6389. v = 0;
  6390. if (prop[0] == 'p') {
  6391. p = PROP_PT;
  6392. if (au)
  6393. unit = 0;
  6394. }
  6395. side = prop + (prop[0] == 'p' ? 8 : 7);
  6396. if (str_eq(side, "right"))
  6397. p = (unsigned char)(p + 1);
  6398. else if (str_eq(side, "bottom"))
  6399. p = (unsigned char)(p + 2);
  6400. else if (str_eq(side, "left"))
  6401. p = (unsigned char)(p + 3);
  6402. decl_add(out, nout, p, v, important);
  6403. decl_add(out, nout,
  6404. (unsigned char)((prop[0] == 'p' ?
  6405. PROP_PT_U : PROP_MT_U) +
  6406. (p - (prop[0] == 'p' ?
  6407. PROP_PT : PROP_MT))),
  6408. unit, important);
  6409. if (unit == 5)
  6410. decl_add(out, nout,
  6411. (unsigned char)((prop[0] == 'p' ?
  6412. PROP_PT_CPX : PROP_MT_CPX) +
  6413. (p - (prop[0] == 'p' ?
  6414. PROP_PT : PROP_MT))),
  6415. cpx, important);
  6416. if (str_eq(side, "right"))
  6417. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6418. else if (str_eq(side, "left"))
  6419. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6420. }
  6421. }
  6422. }
  6423. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6424. * nested blocks. This is the mobile/base rules path: media enhancements are
  6425. * deliberately ignored for now. */
  6426. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6427. unsigned int i)
  6428. {
  6429. unsigned int j = i;
  6430. while (j < len && s[j] != '{' && s[j] != ';')
  6431. j++;
  6432. if (j >= len)
  6433. return len;
  6434. if (s[j] == ';')
  6435. return j + 1;
  6436. j++;
  6437. return css_skip_nested(s, len, j);
  6438. }
  6439. static unsigned int viewport_w = 1024;
  6440. static unsigned short theme_text = 0x0000;
  6441. static unsigned short theme_bg = 0xFFFF;
  6442. static unsigned short theme_panel = 0xEF5D;
  6443. static unsigned short theme_link = 0x001B;
  6444. void br_set_theme(unsigned short text, unsigned short bg,
  6445. unsigned short panel, unsigned short link)
  6446. {
  6447. theme_text = text;
  6448. theme_bg = bg;
  6449. theme_panel = panel;
  6450. theme_link = link;
  6451. }
  6452. void br_set_viewport(unsigned int width)
  6453. {
  6454. viewport_w = width ? width : 1024;
  6455. }
  6456. static unsigned int media_len_px(const char *s, unsigned int len)
  6457. {
  6458. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6459. int em = 0;
  6460. while (i < len && is_space(s[i]))
  6461. i++;
  6462. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6463. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6464. i++;
  6465. }
  6466. if (i < len && s[i] == '.') {
  6467. i++;
  6468. /* Consume every fractional digit even when there are more
  6469. * than the three we keep: leaving the tail behind made the
  6470. * unit check miss "em" (74.9375em became 74px). */
  6471. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6472. if (scale < 1000) {
  6473. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6474. scale *= 10;
  6475. }
  6476. i++;
  6477. }
  6478. }
  6479. while (i < len && is_space(s[i]))
  6480. i++;
  6481. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6482. em = 1;
  6483. i += 2;
  6484. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6485. em = 1;
  6486. i += 3;
  6487. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6488. i += 2;
  6489. }
  6490. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6491. if (em)
  6492. return v * 16 / 100;
  6493. return v / 100;
  6494. }
  6495. /* One parenthesised media feature: min-width/max-width and the modern
  6496. * (width <= 600px) form. Unknown features are treated as true. */
  6497. static int media_feature_matches(const char *s, unsigned int len)
  6498. {
  6499. unsigned int i;
  6500. while (len && is_space(*s)) { s++; len--; }
  6501. while (len && is_space(s[len - 1])) len--;
  6502. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6503. /* There is no user-selectable colour scheme. Ignore both
  6504. * variants so a page's base palette is used; sites whose base
  6505. * is dark (like the project docs) then stay dark instead of
  6506. * flipping to their light media override. */
  6507. return 0;
  6508. }
  6509. {
  6510. /* Environment features with a known answer: a mouse-driven
  6511. * screen with normal contrast and no forced colours. Google
  6512. * puts `border:1px solid CanvasText` on its buttons inside
  6513. * `(forced-colors:active)`. */
  6514. static const struct { const char *name, *yes; } env[] = {
  6515. { "forced-colors", "none" },
  6516. { "-ms-high-contrast", "" },
  6517. { "prefers-contrast", "no-preference" },
  6518. { "inverted-colors", "none" },
  6519. { "any-hover", "hover" },
  6520. { "hover", "hover" },
  6521. { "any-pointer", "fine" },
  6522. { "pointer", "fine" },
  6523. { "prefers-reduced-motion", "no-preference" },
  6524. { "prefers-reduced-transparency", "no-preference" },
  6525. { "display-mode", "browser" },
  6526. };
  6527. unsigned int k;
  6528. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6529. unsigned int nl = s_len(env[k].name), j;
  6530. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6531. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6532. continue;
  6533. for (j = nl; j < len && s[j] != ':'; j++)
  6534. ;
  6535. if (j >= len) /* boolean form: (hover) */
  6536. return env[k].yes[0] &&
  6537. !ci_eq_n(env[k].yes, "none", 4);
  6538. j++;
  6539. while (j < len && is_space(s[j]))
  6540. j++;
  6541. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6542. ci_eq_n(s + j, env[k].yes, len - j);
  6543. }
  6544. }
  6545. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6546. for (i = 9; i < len && s[i] != ':'; i++)
  6547. ;
  6548. if (i < len)
  6549. return viewport_w >= media_len_px(s + i + 1,
  6550. len - i - 1);
  6551. return 1;
  6552. }
  6553. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6554. for (i = 9; i < len && s[i] != ':'; i++)
  6555. ;
  6556. if (i < len)
  6557. return viewport_w <= media_len_px(s + i + 1,
  6558. len - i - 1);
  6559. return 1;
  6560. }
  6561. for (i = 0; i < len; i++) {
  6562. if (s[i] == '<' || s[i] == '>') {
  6563. int max = s[i] == '<';
  6564. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6565. if (i + 1 < len && s[i + 1] == '=')
  6566. v = media_len_px(s + i + 2, len - i - 2);
  6567. return max ? viewport_w <= v : viewport_w >= v;
  6568. }
  6569. }
  6570. return 1;
  6571. }
  6572. static int media_query_matches(const char *s, unsigned int len)
  6573. {
  6574. unsigned int i;
  6575. if (contains_ci_len(s, len, "print") &&
  6576. !contains_ci_len(s, len, "screen"))
  6577. return 0;
  6578. if (contains_ci_len(s, len, "not "))
  6579. return 0;
  6580. for (i = 0; i < len; i++) {
  6581. if (s[i] == '(') {
  6582. unsigned int st = ++i;
  6583. while (i < len && s[i] != ')')
  6584. i++;
  6585. if (!media_feature_matches(s + st, i - st))
  6586. return 0;
  6587. }
  6588. }
  6589. return 1;
  6590. }
  6591. static int media_list_matches(const char *s, unsigned int len)
  6592. {
  6593. unsigned int i = 0;
  6594. while (i < len) {
  6595. unsigned int st = i;
  6596. while (i < len && s[i] != ',')
  6597. i++;
  6598. if (media_query_matches(s + st, i - st))
  6599. return 1;
  6600. i++;
  6601. }
  6602. return 0;
  6603. }
  6604. int br_media_matches(const char *query)
  6605. {
  6606. return media_list_matches(query, s_len(query));
  6607. }
  6608. static unsigned int rule_hash(const char *s, unsigned int len)
  6609. {
  6610. unsigned int h = 2166136261u, i;
  6611. for (i = 0; i < len; i++) {
  6612. char c = s[i];
  6613. if (c >= 'A' && c <= 'Z')
  6614. c = (char)(c - 'A' + 'a');
  6615. h ^= (unsigned char)c;
  6616. h *= 16777619u;
  6617. }
  6618. return h % BR_RULE_HASH;
  6619. }
  6620. /* Index a parsed rule by the most selective component of its rightmost
  6621. * compound selector. br_style_for() then only visits rules that can
  6622. * possibly match a node instead of scanning every rule. */
  6623. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6624. unsigned int len);
  6625. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6626. char *out, unsigned int cap, unsigned int *olen);
  6627. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6628. const char *key, unsigned int len)
  6629. {
  6630. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6631. * "md:flex", the class the element carries, not under "md". */
  6632. char cname[96], iname[96];
  6633. unsigned int i, clen = 0, ilen = 0;
  6634. unsigned short *head;
  6635. for (i = 0; i < len; i++) {
  6636. if (key[i] == '\\') {
  6637. i++;
  6638. continue;
  6639. }
  6640. if (key[i] == '(' || key[i] == '[') {
  6641. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6642. i = sel_skip_group(key, i, len) - 1;
  6643. continue;
  6644. }
  6645. if (key[i] == '.' || key[i] == '#') {
  6646. char tmp[96];
  6647. unsigned int n, e;
  6648. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6649. if (n && n + 1 < sizeof(tmp)) {
  6650. if (key[i] == '#') {
  6651. memcpy(iname, tmp, n);
  6652. ilen = n;
  6653. } else {
  6654. memcpy(cname, tmp, n);
  6655. clen = n;
  6656. }
  6657. }
  6658. i = e - 1;
  6659. }
  6660. }
  6661. if (ilen)
  6662. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6663. else if (clen)
  6664. head = &d->rule_bucket[rule_hash(cname, clen)];
  6665. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6666. head = &d->rule_tag[BR_TAG_HTML];
  6667. else if (len && is_ident(key[0])) {
  6668. char name[16];
  6669. unsigned int n = 0, tag;
  6670. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6671. name[n] = key[n];
  6672. n++;
  6673. }
  6674. name[n] = '\0';
  6675. tag = br_tag_from_name(name);
  6676. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6677. head = &d->rule_tag[tag];
  6678. else
  6679. head = &d->rule_any;
  6680. } else {
  6681. head = &d->rule_any;
  6682. }
  6683. d->rule_next[idx] = *head;
  6684. *head = (unsigned short)idx;
  6685. }
  6686. #define SEL_PARTS 16
  6687. struct sel_part {
  6688. unsigned short st, en;
  6689. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6690. };
  6691. static unsigned int selector_specificity(const char *s, unsigned int len);
  6692. static int css_selector_supported(const char *s, unsigned int len);
  6693. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6694. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6695. unsigned int *st, unsigned int *en);
  6696. static int css_collect_only;
  6697. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6698. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6699. * selector with any other pseudo-element. */
  6700. static int css_pseudo_element(char *sel, unsigned int *len)
  6701. {
  6702. static const struct {
  6703. const char *s;
  6704. unsigned char l, pe;
  6705. } pe[] = {
  6706. { "::before", 8, 1 }, { "::after", 7, 2 },
  6707. { ":before", 7, 1 }, { ":after", 6, 2 },
  6708. };
  6709. unsigned int k, n = *len;
  6710. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6711. unsigned int l = pe[k].l;
  6712. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6713. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6714. continue; /* matched the tail of "::x" */
  6715. n -= l;
  6716. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6717. sel[n - 1] == '+' || sel[n - 1] == '~')
  6718. sel[n++] = '*';
  6719. sel[n] = '\0';
  6720. *len = n;
  6721. return pe[k].pe;
  6722. }
  6723. }
  6724. for (k = 0; k + 1 < n; k++)
  6725. if (sel[k] == ':' && sel[k + 1] == ':')
  6726. return -1;
  6727. return 0;
  6728. }
  6729. /* Does a declaration block use or define a custom property whose value
  6730. * depends on the element (defined by some non-root rule)? Such rules are
  6731. * re-expanded per element at cascade time. */
  6732. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6733. unsigned int len)
  6734. {
  6735. unsigned char f = 0;
  6736. unsigned int i, k;
  6737. for (i = 0; i + 6 < len; i++) {
  6738. unsigned int st, e, vi;
  6739. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6740. continue;
  6741. st = i + 4;
  6742. while (st < len && is_space(s[st]))
  6743. st++;
  6744. e = st;
  6745. while (e < len && s[e] != ',' && s[e] != ')' &&
  6746. !is_space(s[e]))
  6747. e++;
  6748. vi = css_var_find(d, s + st, e - st);
  6749. if (vi < BR_MAX_VARS &&
  6750. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6751. f |= BR_RULE_USES_SCOPED;
  6752. break;
  6753. }
  6754. }
  6755. css_local_collect(s, len);
  6756. for (k = 0; k < css_nlocal; k++) {
  6757. unsigned int vi = css_var_find(d, css_local[k].name,
  6758. css_local[k].nlen);
  6759. if (vi < BR_MAX_VARS &&
  6760. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6761. f |= BR_RULE_DEFINES_SCOPED;
  6762. }
  6763. css_nlocal = 0;
  6764. return f;
  6765. }
  6766. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6767. {
  6768. unsigned int i = 0;
  6769. while (i < len && d->nrules < d->rule_cap) {
  6770. unsigned int sstart, send, bstart, bend;
  6771. struct br_decl decls[BR_DECLS];
  6772. unsigned int nd, scope_off = 0;
  6773. unsigned char scope_flags = 0;
  6774. while (i < len && is_space(s[i]))
  6775. i++;
  6776. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6777. i += 2;
  6778. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6779. i++;
  6780. i += 2;
  6781. continue;
  6782. }
  6783. if (i >= len || s[i] == '}')
  6784. break;
  6785. if (s[i] == '@') {
  6786. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6787. unsigned int q = i + 6;
  6788. while (q < len && s[q] != '{')
  6789. q++;
  6790. if (q < len &&
  6791. media_list_matches(s + i + 6,
  6792. q - (i + 6))) {
  6793. unsigned int b = q + 1;
  6794. unsigned int e = css_skip_nested(s, len, b);
  6795. if (e > b)
  6796. parse_css(d, s + b,
  6797. (e - 1) - b);
  6798. i = e;
  6799. } else {
  6800. i = skip_at_rule(s, len, i);
  6801. }
  6802. continue;
  6803. }
  6804. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6805. (i + 6 == len || is_space(s[i + 6]) ||
  6806. s[i + 6] == '{' || s[i + 6] == ';')) {
  6807. unsigned int q = i + 6, nst = q;
  6808. while (q < len && s[q] != '{' && s[q] != ';')
  6809. q++;
  6810. if (q >= len) {
  6811. i = len;
  6812. continue;
  6813. }
  6814. if (s[q] == ';') {
  6815. /* `@layer a, b;` only fixes the order. */
  6816. unsigned int st = nst;
  6817. char path[128];
  6818. while (st < q) {
  6819. unsigned int en = st;
  6820. while (en < q && s[en] != ',')
  6821. en++;
  6822. if (en > st)
  6823. css_layer_register(d, s + st,
  6824. en - st,
  6825. path,
  6826. sizeof(path));
  6827. st = en + 1;
  6828. }
  6829. i = q + 1;
  6830. continue;
  6831. }
  6832. {
  6833. unsigned int b = q + 1;
  6834. unsigned int e = css_skip_nested(s, len, b);
  6835. unsigned char saved = css_cur_layer;
  6836. char saved_name[128], path[128];
  6837. unsigned char rank;
  6838. memcpy(saved_name, css_layer_name,
  6839. sizeof(saved_name));
  6840. rank = css_layer_register(d, s + nst, q - nst,
  6841. path,
  6842. sizeof(path));
  6843. if (rank) {
  6844. css_cur_layer = rank;
  6845. memcpy(css_layer_name, path,
  6846. sizeof(css_layer_name));
  6847. }
  6848. if (e > b)
  6849. parse_css(d, s + b, (e - 1) - b);
  6850. css_cur_layer = saved;
  6851. memcpy(css_layer_name, saved_name,
  6852. sizeof(css_layer_name));
  6853. i = e;
  6854. }
  6855. continue;
  6856. }
  6857. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  6858. unsigned int q = i + 9;
  6859. while (q < len && s[q] != '{' && s[q] != ';')
  6860. q++;
  6861. if (q < len && s[q] == '{' &&
  6862. supports_matches(s + i + 9, q - (i + 9))) {
  6863. unsigned int b = q + 1;
  6864. unsigned int e = css_skip_nested(s, len, b);
  6865. if (e > b)
  6866. parse_css(d, s + b, (e - 1) - b);
  6867. i = e;
  6868. } else {
  6869. i = skip_at_rule(s, len, i);
  6870. }
  6871. continue;
  6872. }
  6873. i = skip_at_rule(s, len, i);
  6874. continue;
  6875. }
  6876. sstart = i;
  6877. while (i < len && s[i] != '{')
  6878. i++;
  6879. if (i >= len)
  6880. break;
  6881. send = i;
  6882. i++;
  6883. bstart = i;
  6884. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  6885. * to the matching close brace; stopping at the first '}' left
  6886. * the parser out of sync and dropped the rest of the sheet
  6887. * (nd-aktuell lost its desktop rules that way). */
  6888. bend = css_block_end(s, len, i);
  6889. i = bend < len ? bend + 1 : len;
  6890. while (sstart < send && is_space(s[sstart]))
  6891. sstart++;
  6892. while (send > sstart && is_space(s[send - 1]))
  6893. send--;
  6894. if (sstart == send)
  6895. continue;
  6896. if (!css_dark_selector(s + sstart, send - sstart))
  6897. css_collect_block(d, s + bstart, bend - bstart,
  6898. css_root_selector(s + sstart,
  6899. send - sstart));
  6900. if (css_collect_only)
  6901. continue;
  6902. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  6903. {
  6904. unsigned char sc = bend - bstart <= 4000 ?
  6905. css_block_scope(d, s + bstart, bend - bstart) :
  6906. 0;
  6907. scope_flags = 0;
  6908. scope_off = 0;
  6909. if (sc) {
  6910. scope_off = store_raw(d, s + bstart,
  6911. bend - bstart,
  6912. bend - bstart);
  6913. if (scope_off)
  6914. scope_flags = sc;
  6915. }
  6916. }
  6917. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  6918. continue;
  6919. /* Split a comma-separated selector list into separate rules.
  6920. * A selector the engine cannot match is dropped; the others
  6921. * in the list still apply. */
  6922. {
  6923. const char *list = s + sstart;
  6924. unsigned int ll = send - sstart, pos = 0, st, en;
  6925. while (d->nrules < d->rule_cap &&
  6926. sel_list_next(list, ll, &pos, &st, &en)) {
  6927. struct sel_part parts[SEL_PARTS];
  6928. struct br_rule *r;
  6929. char sel[160];
  6930. unsigned int k, off;
  6931. int np, pe;
  6932. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  6933. k++)
  6934. sel[k] = is_space(list[st + k]) ? ' ' :
  6935. list[st + k];
  6936. if (st + k < en)
  6937. continue; /* too long */
  6938. sel[k] = '\0';
  6939. pe = css_pseudo_element(sel, &k);
  6940. if (pe < 0 || !css_selector_supported(sel, k))
  6941. continue;
  6942. np = sel_split(sel, k, parts);
  6943. off = store_attr(d, sel, k, 159);
  6944. if (np <= 0 || !off)
  6945. continue;
  6946. r = &d->rules[d->nrules++];
  6947. r->sel_off = off;
  6948. r->key_off = store_attr(d, sel + parts[np - 1].st,
  6949. parts[np - 1].en -
  6950. parts[np - 1].st, 159);
  6951. r->spec = (unsigned short)selector_specificity(sel, k);
  6952. r->single = np == 1;
  6953. r->pe = (unsigned char)pe;
  6954. r->layer = css_cur_layer;
  6955. r->var_scope = scope_flags;
  6956. r->raw_off = scope_off;
  6957. r->raw_len = (unsigned short)(bend - bstart);
  6958. if (pe)
  6959. d->npe_rules++;
  6960. r->nd = (unsigned char)nd;
  6961. for (k = 0; k < nd; k++)
  6962. r->d[k] = decls[k];
  6963. rule_index_add(d, d->nrules - 1,
  6964. attr_ptr(d, r->key_off),
  6965. s_len(attr_ptr(d, r->key_off)));
  6966. }
  6967. }
  6968. }
  6969. }
  6970. /* Sites often set html { font-size: 62.5% } and then size everything in
  6971. * rem. Detect that root size before parsing the sheet so a rem resolves to
  6972. * the same pixels a real browser would use. */
  6973. static void detect_root_font(const char *s, unsigned int len)
  6974. {
  6975. unsigned int i;
  6976. for (i = 0; i + 4 < len; i++) {
  6977. unsigned int j, end, body, sel, q, h;
  6978. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  6979. int exact;
  6980. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  6981. s[i + 3] == 'l'))
  6982. continue;
  6983. h = i + 4;
  6984. while (h < len && is_space(s[h]))
  6985. h++;
  6986. if (h < len && s[h] != '{' && s[h] != ',' &&
  6987. s[h] != '.' && s[h] != ':' && s[h] != '[')
  6988. continue;
  6989. sel = i;
  6990. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  6991. s[sel - 1] != ';' && s[sel - 1] != ',')
  6992. sel--;
  6993. q = sel;
  6994. while (q < i && is_space(s[q]))
  6995. q++;
  6996. exact = (q == i);
  6997. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  6998. continue;
  6999. j = h;
  7000. if (j >= len || s[j] != '{')
  7001. continue;
  7002. while (j < len && s[j] != '{')
  7003. j++;
  7004. if (j >= len)
  7005. break;
  7006. end = j + 1;
  7007. body = end;
  7008. while (end < len && s[end] != '}')
  7009. end++;
  7010. for (; j + 9 < end; j++) {
  7011. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  7012. s[j + 3] == 't' && s[j + 4] == '-' &&
  7013. s[j + 5] == 's' && s[j + 6] == 'i' &&
  7014. s[j + 7] == 'z' && s[j + 8] == 'e'))
  7015. continue;
  7016. j += 9;
  7017. while (j < end && s[j] != ':')
  7018. j++;
  7019. if (j >= end)
  7020. break;
  7021. j++;
  7022. while (j < end && is_space(s[j]))
  7023. j++;
  7024. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7025. v = v * 10 + (s[j] - '0');
  7026. digits = 1;
  7027. j++;
  7028. }
  7029. if (j < end && s[j] == '.') {
  7030. j++;
  7031. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7032. v = v * 10 + (s[j] - '0');
  7033. frac++;
  7034. j++;
  7035. }
  7036. }
  7037. while (frac < 2) {
  7038. v *= 10;
  7039. frac++;
  7040. }
  7041. while (frac > 2) {
  7042. v /= 10;
  7043. frac--;
  7044. }
  7045. if (!digits)
  7046. break;
  7047. if (j < end && s[j] == '%')
  7048. pct = 1;
  7049. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  7050. px = 1;
  7051. if (pct)
  7052. v = (v * 16 + 5000) / 10000;
  7053. else if (px)
  7054. v /= 100;
  7055. else
  7056. v = v * 16 / 100;
  7057. if (exact && v >= 8 && v <= 24)
  7058. root_font_px = v;
  7059. if (exact)
  7060. return;
  7061. }
  7062. /* html { font: 10px/1 Verdana } also sets the root size. */
  7063. for (j = body; j + 5 <= end; j++) {
  7064. unsigned int p, ts, te;
  7065. if (!ci_eq_n(s + j, "font:", 5))
  7066. continue;
  7067. p = j + 5;
  7068. while (next_token(s, end, &p, &ts, &te)) {
  7069. int fv = parse_font_px(s + ts, te - ts);
  7070. if (fv >= 8 && fv <= 24) {
  7071. if (exact)
  7072. root_font_px = fv;
  7073. break;
  7074. }
  7075. }
  7076. if (exact)
  7077. return;
  7078. break;
  7079. }
  7080. }
  7081. }
  7082. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  7083. {
  7084. scan_font_faces(css, len);
  7085. detect_root_font(css, len);
  7086. css_collect_only = 1;
  7087. parse_css(d, css, len);
  7088. css_collect_only = 0;
  7089. parse_css(d, css, len);
  7090. }
  7091. void br_parse_inline_css(struct br_doc *d)
  7092. {
  7093. if (d->inline_css_len) {
  7094. scan_font_faces(css_store, d->inline_css_len);
  7095. detect_root_font(css_store, d->inline_css_len);
  7096. css_collect_only = 1;
  7097. parse_css(d, css_store, d->inline_css_len);
  7098. css_collect_only = 0;
  7099. parse_css(d, css_store, d->inline_css_len);
  7100. }
  7101. d->inline_css_len = 0;
  7102. }
  7103. void br_reset_styles(struct br_doc *d)
  7104. {
  7105. unsigned int i;
  7106. for (i = 0; i < d->nnode; i++) {
  7107. d->nodes[i].style_valid = 0;
  7108. d->nodes[i].pe_state = 0;
  7109. }
  7110. }
  7111. /* ---- selector matching ---- */
  7112. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  7113. unsigned int clen)
  7114. {
  7115. const char *s = attr_ptr(d, d->nodes[node].class_off);
  7116. unsigned int i = 0;
  7117. while (s[i]) {
  7118. unsigned int st, n;
  7119. while (s[i] && is_space(s[i]))
  7120. i++;
  7121. st = i;
  7122. while (s[i] && !is_space(s[i]))
  7123. i++;
  7124. n = i - st;
  7125. if (n == clen && ci_eq_n(s + st, cls, n))
  7126. return 1;
  7127. }
  7128. return 0;
  7129. }
  7130. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  7131. unsigned int len)
  7132. {
  7133. const char *s = attr_ptr(d, d->nodes[node].id_off);
  7134. unsigned int i;
  7135. for (i = 0; i < len; i++)
  7136. if (s[i] != id[i])
  7137. return 0;
  7138. return s[len] == '\0';
  7139. }
  7140. static int is_ident(char c)
  7141. {
  7142. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  7143. (c >= '0' && c <= '9') || c == '-' || c == '_';
  7144. }
  7145. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  7146. {
  7147. unsigned int n = 0;
  7148. while (lit[n])
  7149. n++;
  7150. return n == nlen && ci_eq_n(name, lit, nlen);
  7151. }
  7152. static int attr_present(struct br_doc *d, unsigned int node,
  7153. const char *name, unsigned int nlen)
  7154. {
  7155. struct br_node *n = &d->nodes[node];
  7156. if (attr_is(name, nlen, "data-collapse-target"))
  7157. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  7158. if (attr_is(name, nlen, "data-collapse-trigger"))
  7159. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  7160. if (attr_is(name, nlen, "is-open"))
  7161. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  7162. if (attr_is(name, nlen, "disabled"))
  7163. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  7164. if (attr_is(name, nlen, "open"))
  7165. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  7166. if (attr_is(name, nlen, "inert"))
  7167. return (n->attr_flags & BR_ATTR_INERT) != 0;
  7168. if (attr_is(name, nlen, "hidden"))
  7169. return (n->flags & BR_NODE_HIDDEN) != 0;
  7170. if (attr_is(name, nlen, "href"))
  7171. return attr_ptr(d, n->href_off)[0] != 0;
  7172. if (attr_is(name, nlen, "class"))
  7173. return attr_ptr(d, n->class_off)[0] != 0;
  7174. if (attr_is(name, nlen, "id"))
  7175. return attr_ptr(d, n->id_off)[0] != 0;
  7176. if (attr_is(name, nlen, "src"))
  7177. return attr_ptr(d, n->src_off)[0] != 0;
  7178. if (attr_is(name, nlen, "alt"))
  7179. return attr_ptr(d, n->alt_off)[0] != 0;
  7180. if (attr_is(name, nlen, "rel"))
  7181. return attr_ptr(d, n->rel_off)[0] != 0;
  7182. return 0;
  7183. }
  7184. /* ---- Selector engine ----
  7185. *
  7186. * One matcher serves the cascade (rule_try), querySelector(All) and
  7187. * Element.matches. A compound is an optional type/universal selector
  7188. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  7189. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  7190. * '~' combinators and is matched right to left with backtracking.
  7191. *
  7192. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  7193. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  7194. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  7195. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  7196. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  7197. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  7198. * and pseudo-elements never match an element; the cascade strips
  7199. * ::before/::after before it gets here. */
  7200. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7201. unsigned int len);
  7202. /* i is at '(' or '['; returns the index just past the matching close. */
  7203. static unsigned int sel_skip_group(const char *s, unsigned int i,
  7204. unsigned int len)
  7205. {
  7206. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  7207. int depth = 0;
  7208. for (; i < len; i++) {
  7209. char c = s[i];
  7210. if (quote) {
  7211. if (c == '\\')
  7212. i++;
  7213. else if (c == quote)
  7214. quote = 0;
  7215. continue;
  7216. }
  7217. if (c == '\\')
  7218. i++;
  7219. else if (c == '"' || c == '\'')
  7220. quote = c;
  7221. else if (c == open)
  7222. depth++;
  7223. else if (c == close && --depth == 0)
  7224. return i + 1;
  7225. }
  7226. return len;
  7227. }
  7228. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  7229. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  7230. char *out, unsigned int cap, unsigned int *olen)
  7231. {
  7232. unsigned int n = 0;
  7233. while (i < len) {
  7234. char c = s[i];
  7235. if (c == '\\' && i + 1 < len) {
  7236. c = s[i + 1];
  7237. i += 2;
  7238. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  7239. i++;
  7240. } else {
  7241. break;
  7242. }
  7243. if (n + 1 < cap)
  7244. out[n++] = c;
  7245. }
  7246. out[n] = '\0';
  7247. *olen = n;
  7248. return i;
  7249. }
  7250. /* Next top-level item of a comma-separated selector list. */
  7251. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  7252. unsigned int *st, unsigned int *en)
  7253. {
  7254. unsigned int i = *pos;
  7255. while (i < len && (is_space(s[i]) || s[i] == ','))
  7256. i++;
  7257. if (i >= len)
  7258. return 0;
  7259. *st = i;
  7260. while (i < len && s[i] != ',') {
  7261. if (s[i] == '(' || s[i] == '[')
  7262. i = sel_skip_group(s, i, len);
  7263. else if (s[i] == '\\' && i + 1 < len)
  7264. i += 2;
  7265. else if (s[i] == '"' || s[i] == '\'') {
  7266. char q = s[i++];
  7267. while (i < len && s[i] != q)
  7268. i++;
  7269. i++;
  7270. } else
  7271. i++;
  7272. }
  7273. if (i > len)
  7274. i = len;
  7275. *en = i;
  7276. while (*en > *st && is_space(s[*en - 1]))
  7277. (*en)--;
  7278. *pos = i;
  7279. return 1;
  7280. }
  7281. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7282. unsigned int len)
  7283. {
  7284. unsigned int pos = 0, st, en;
  7285. while (sel_list_next(s, len, &pos, &st, &en))
  7286. if (sel_match(d, node, s + st, en - st))
  7287. return 1;
  7288. return 0;
  7289. }
  7290. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7291. {
  7292. if (d->nodes[a].tag != d->nodes[b].tag)
  7293. return 0;
  7294. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7295. return 1;
  7296. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7297. attr_ptr(d, d->nodes[b].name_off));
  7298. }
  7299. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7300. {
  7301. unsigned int c, prev = 0;
  7302. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7303. c = d->nodes[c].next)
  7304. if (d->nodes[c].type == BR_NODE_ELEM)
  7305. prev = c;
  7306. return c == node ? prev : 0;
  7307. }
  7308. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7309. {
  7310. unsigned int c;
  7311. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7312. if (d->nodes[c].type == BR_NODE_ELEM)
  7313. return c;
  7314. return 0;
  7315. }
  7316. /* 1-based position among the element siblings (of the same type), counted
  7317. * from the start or, with from_end, from the end. */
  7318. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7319. int from_end)
  7320. {
  7321. unsigned int c;
  7322. int pos = 0, total = 0;
  7323. for (c = d->nodes[d->nodes[node].parent].first; c;
  7324. c = d->nodes[c].next) {
  7325. if (d->nodes[c].type != BR_NODE_ELEM)
  7326. continue;
  7327. if (of_type && !sel_same_type(d, c, node))
  7328. continue;
  7329. total++;
  7330. if (c == node)
  7331. pos = total;
  7332. }
  7333. if (!pos)
  7334. return 0;
  7335. return from_end ? total - pos + 1 : pos;
  7336. }
  7337. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7338. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7339. {
  7340. unsigned int i = 0;
  7341. int sign = 1, num = 0, digits = 0;
  7342. while (len && is_space(s[len - 1]))
  7343. len--;
  7344. while (i < len && is_space(s[i]))
  7345. i++;
  7346. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7347. *a = 2;
  7348. *b = 1;
  7349. return 1;
  7350. }
  7351. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7352. *a = 2;
  7353. *b = 0;
  7354. return 1;
  7355. }
  7356. if (i < len && (s[i] == '+' || s[i] == '-'))
  7357. sign = s[i++] == '-' ? -1 : 1;
  7358. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7359. num = num * 10 + (s[i++] - '0');
  7360. digits = 1;
  7361. }
  7362. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7363. *a = sign * (digits ? num : 1);
  7364. *b = 0;
  7365. i++;
  7366. while (i < len && is_space(s[i]))
  7367. i++;
  7368. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7369. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7370. while (i < len && is_space(s[i]))
  7371. i++;
  7372. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7373. bv = bv * 10 + (s[i++] - '0');
  7374. bd = 1;
  7375. }
  7376. if (!bd)
  7377. return 0;
  7378. *b = bs * bv;
  7379. }
  7380. } else {
  7381. if (!digits)
  7382. return 0;
  7383. *a = 0;
  7384. *b = sign * num;
  7385. }
  7386. return i == len;
  7387. }
  7388. static int sel_nth_ok(int a, int b, int pos)
  7389. {
  7390. if (pos <= 0)
  7391. return 0;
  7392. if (!a)
  7393. return pos == b;
  7394. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7395. }
  7396. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7397. const char *name, int op, const char *want,
  7398. unsigned int wl, int icase)
  7399. {
  7400. const char *have = br_find_attr(d, node, name);
  7401. unsigned int n, i;
  7402. if (!have) {
  7403. /* Boolean attributes the parser keeps as flags. */
  7404. if (!attr_present(d, node, name, text_len(name)))
  7405. return 0;
  7406. have = "";
  7407. }
  7408. if (!op)
  7409. return 1;
  7410. n = text_len(have);
  7411. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7412. switch (op) {
  7413. case '=':
  7414. return n == wl && SEL_EQ(have, want, wl);
  7415. case '^':
  7416. return wl && n >= wl && SEL_EQ(have, want, wl);
  7417. case '$':
  7418. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7419. case '*':
  7420. if (!wl)
  7421. return 0;
  7422. for (i = 0; i + wl <= n; i++)
  7423. if (SEL_EQ(have + i, want, wl))
  7424. return 1;
  7425. return 0;
  7426. case '|':
  7427. return n >= wl && SEL_EQ(have, want, wl) &&
  7428. (n == wl || have[wl] == '-');
  7429. case '~':
  7430. if (!wl)
  7431. return 0;
  7432. for (i = 0; i < n; ) {
  7433. unsigned int st;
  7434. while (i < n && is_space(have[i]))
  7435. i++;
  7436. st = i;
  7437. while (i < n && !is_space(have[i]))
  7438. i++;
  7439. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7440. return 1;
  7441. }
  7442. return 0;
  7443. }
  7444. #undef SEL_EQ
  7445. return 0;
  7446. }
  7447. static int sel_empty(struct br_doc *d, unsigned int node)
  7448. {
  7449. unsigned int c;
  7450. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7451. if (d->nodes[c].type == BR_NODE_ELEM)
  7452. return 0;
  7453. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7454. return 0;
  7455. }
  7456. return 1;
  7457. }
  7458. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7459. unsigned int len)
  7460. {
  7461. unsigned int c;
  7462. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7463. if (d->nodes[c].type != BR_NODE_ELEM)
  7464. continue;
  7465. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7466. return 1;
  7467. }
  7468. return 0;
  7469. }
  7470. /* :has() with an optional leading relative combinator per list item. */
  7471. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7472. unsigned int len)
  7473. {
  7474. unsigned int pos = 0, st, en, c;
  7475. while (sel_list_next(s, len, &pos, &st, &en)) {
  7476. char comb = s[st];
  7477. if (comb == '>' || comb == '+' || comb == '~') {
  7478. st++;
  7479. while (st < en && is_space(s[st]))
  7480. st++;
  7481. } else {
  7482. comb = ' ';
  7483. }
  7484. if (st >= en)
  7485. continue;
  7486. if (comb == '>') {
  7487. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7488. if (d->nodes[c].type == BR_NODE_ELEM &&
  7489. sel_match(d, c, s + st, en - st))
  7490. return 1;
  7491. } else if (comb == '+') {
  7492. c = sel_next_elem(d, node);
  7493. if (c && sel_match(d, c, s + st, en - st))
  7494. return 1;
  7495. } else if (comb == '~') {
  7496. for (c = sel_next_elem(d, node); c;
  7497. c = sel_next_elem(d, c))
  7498. if (sel_match(d, c, s + st, en - st))
  7499. return 1;
  7500. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7501. return 1;
  7502. }
  7503. }
  7504. return 0;
  7505. }
  7506. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7507. {
  7508. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7509. }
  7510. /* Pseudo-class names the engine models; anything else never matches and
  7511. * makes a top-level rule unsupported (see css_selector_supported()). */
  7512. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7513. {
  7514. static const char *const plain[] = {
  7515. "root", "link", "any-link", "visited", "first-child",
  7516. "last-child", "only-child", "first-of-type", "last-of-type",
  7517. "only-of-type", "empty", "checked", "disabled", "enabled",
  7518. "required", "optional",
  7519. };
  7520. static const char *const func[] = {
  7521. "not", "is", "where", "matches", "-webkit-any", "has",
  7522. "nth-child", "nth-last-child", "nth-of-type",
  7523. "nth-last-of-type",
  7524. };
  7525. unsigned int k;
  7526. if (fn) {
  7527. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7528. if (sel_name_is(name, nl, func[k]))
  7529. return 1;
  7530. return 0;
  7531. }
  7532. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7533. if (sel_name_is(name, nl, plain[k]))
  7534. return 1;
  7535. return 0;
  7536. }
  7537. static int sel_form_control(struct br_doc *d, unsigned int node)
  7538. {
  7539. unsigned int t = d->nodes[node].tag;
  7540. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7541. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7542. t == BR_TAG_OPTION;
  7543. }
  7544. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7545. unsigned int nl, const char *arg, unsigned int al,
  7546. int fn)
  7547. {
  7548. struct br_node *n = &d->nodes[node];
  7549. int a, b;
  7550. if (fn) {
  7551. if (sel_name_is(name, nl, "not"))
  7552. return !sel_list_match(d, node, arg, al);
  7553. if (sel_name_is(name, nl, "is") ||
  7554. sel_name_is(name, nl, "where") ||
  7555. sel_name_is(name, nl, "matches") ||
  7556. sel_name_is(name, nl, "-webkit-any"))
  7557. return sel_list_match(d, node, arg, al);
  7558. if (sel_name_is(name, nl, "has"))
  7559. return sel_has(d, node, arg, al);
  7560. if (!sel_nth_parse(arg, al, &a, &b))
  7561. return 0;
  7562. if (sel_name_is(name, nl, "nth-child"))
  7563. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7564. if (sel_name_is(name, nl, "nth-last-child"))
  7565. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7566. if (sel_name_is(name, nl, "nth-of-type"))
  7567. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7568. if (sel_name_is(name, nl, "nth-last-of-type"))
  7569. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7570. return 0;
  7571. }
  7572. if (sel_name_is(name, nl, "root"))
  7573. return n->tag == BR_TAG_HTML;
  7574. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7575. /* No history state: every link is unvisited. */
  7576. return n->tag == BR_TAG_A && n->href_off;
  7577. if (sel_name_is(name, nl, "first-child"))
  7578. return sel_sibling_pos(d, node, 0, 0) == 1;
  7579. if (sel_name_is(name, nl, "last-child"))
  7580. return sel_sibling_pos(d, node, 0, 1) == 1;
  7581. if (sel_name_is(name, nl, "only-child"))
  7582. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7583. sel_sibling_pos(d, node, 0, 1) == 1;
  7584. if (sel_name_is(name, nl, "first-of-type"))
  7585. return sel_sibling_pos(d, node, 1, 0) == 1;
  7586. if (sel_name_is(name, nl, "last-of-type"))
  7587. return sel_sibling_pos(d, node, 1, 1) == 1;
  7588. if (sel_name_is(name, nl, "only-of-type"))
  7589. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7590. sel_sibling_pos(d, node, 1, 1) == 1;
  7591. if (sel_name_is(name, nl, "empty"))
  7592. return sel_empty(d, node);
  7593. if (sel_name_is(name, nl, "checked"))
  7594. return br_find_attr(d, node, "checked") != 0 ||
  7595. (n->tag == BR_TAG_OPTION &&
  7596. br_find_attr(d, node, "selected") != 0);
  7597. if (sel_name_is(name, nl, "disabled"))
  7598. return sel_form_control(d, node) &&
  7599. (n->attr_flags & BR_ATTR_DISABLED);
  7600. if (sel_name_is(name, nl, "enabled"))
  7601. return sel_form_control(d, node) &&
  7602. !(n->attr_flags & BR_ATTR_DISABLED);
  7603. if (sel_name_is(name, nl, "required"))
  7604. return sel_form_control(d, node) &&
  7605. br_find_attr(d, node, "required") != 0;
  7606. if (sel_name_is(name, nl, "optional"))
  7607. return sel_form_control(d, node) &&
  7608. !br_find_attr(d, node, "required");
  7609. return 0;
  7610. }
  7611. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7612. unsigned int len)
  7613. {
  7614. char buf[64];
  7615. unsigned int i = 0, bl;
  7616. if (d->nodes[node].type != BR_NODE_ELEM)
  7617. return 0;
  7618. if (i < len && s[i] == '*') {
  7619. i++;
  7620. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7621. unsigned int tag;
  7622. i = sel_ident(s, i, len, buf, 32, &bl);
  7623. tag = br_tag_from_name(buf);
  7624. if (tag == BR_TAG_UNKNOWN) {
  7625. const char *raw = attr_ptr(d,
  7626. d->nodes[node].name_off);
  7627. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7628. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7629. return 0;
  7630. } else if (tag != d->nodes[node].tag) {
  7631. return 0;
  7632. }
  7633. }
  7634. if (i >= len)
  7635. return i > 0;
  7636. while (i < len) {
  7637. char c = s[i];
  7638. if (c == '.' || c == '#') {
  7639. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7640. if (!bl)
  7641. return 0;
  7642. if (c == '.' ? !class_has(d, node, buf, bl) :
  7643. !id_eq(d, node, buf, bl))
  7644. return 0;
  7645. } else if (c == '[') {
  7646. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7647. unsigned int vs = 0, vl = 0, k;
  7648. char name[32];
  7649. int op = 0, icase = 0;
  7650. while (j < e && is_space(s[j]))
  7651. j++;
  7652. k = 0;
  7653. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7654. k + 1 < sizeof(name)) {
  7655. char ch = s[j++];
  7656. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7657. (char)(ch + 32) : ch;
  7658. }
  7659. name[k] = '\0';
  7660. if (!k)
  7661. return 0;
  7662. while (j < e && is_space(s[j]))
  7663. j++;
  7664. if (j < e && s[j] == '=') {
  7665. op = '=';
  7666. j++;
  7667. } else if (j + 1 < e && s[j + 1] == '=' &&
  7668. (s[j] == '~' || s[j] == '|' ||
  7669. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7670. op = s[j];
  7671. j += 2;
  7672. }
  7673. if (op) {
  7674. while (j < e && is_space(s[j]))
  7675. j++;
  7676. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7677. char q = s[j++];
  7678. vs = j;
  7679. while (j < e && s[j] != q)
  7680. j++;
  7681. vl = j - vs;
  7682. j++;
  7683. } else {
  7684. vs = j;
  7685. while (j < e && s[j] != ']' &&
  7686. !is_space(s[j]))
  7687. j++;
  7688. vl = j - vs;
  7689. }
  7690. while (j < e && is_space(s[j]))
  7691. j++;
  7692. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7693. icase = 1;
  7694. }
  7695. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7696. icase))
  7697. return 0;
  7698. i = e;
  7699. } else if (c == ':') {
  7700. unsigned int st, nl, as = 0, al = 0;
  7701. int fn = 0;
  7702. if (i + 1 < len && s[i + 1] == ':')
  7703. return 0; /* pseudo-element */
  7704. st = ++i;
  7705. while (i < len && is_ident(s[i]))
  7706. i++;
  7707. nl = i - st;
  7708. if (i < len && s[i] == '(') {
  7709. unsigned int e = sel_skip_group(s, i, len);
  7710. fn = 1;
  7711. as = i + 1;
  7712. al = e > as ? e - as - 1 : 0;
  7713. i = e;
  7714. }
  7715. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7716. return 0;
  7717. } else {
  7718. return 0;
  7719. }
  7720. }
  7721. return 1;
  7722. }
  7723. /* Split a complex selector into compounds; returns the count or -1. */
  7724. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7725. {
  7726. unsigned int i = 0;
  7727. int n = 0;
  7728. char comb = 0;
  7729. while (i < len && is_space(s[i]))
  7730. i++;
  7731. while (i < len) {
  7732. unsigned int st = i;
  7733. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7734. s[i] != '+' && s[i] != '~') {
  7735. if (s[i] == '(' || s[i] == '[')
  7736. i = sel_skip_group(s, i, len);
  7737. else if (s[i] == '\\' && i + 1 < len)
  7738. i += 2;
  7739. else
  7740. i++;
  7741. }
  7742. if (i == st || n == SEL_PARTS)
  7743. return -1;
  7744. p[n].st = (unsigned short)st;
  7745. p[n].en = (unsigned short)(i > len ? len : i);
  7746. p[n].comb = comb;
  7747. n++;
  7748. comb = 0;
  7749. while (i < len && is_space(s[i])) {
  7750. comb = ' ';
  7751. i++;
  7752. }
  7753. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7754. comb = s[i++];
  7755. while (i < len && is_space(s[i]))
  7756. i++;
  7757. if (i >= len)
  7758. return -1;
  7759. }
  7760. }
  7761. return n;
  7762. }
  7763. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7764. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7765. * parent when the page starts with <html> (node 0 is that element; see
  7766. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7767. static int sel_parent(struct br_doc *d, unsigned int node)
  7768. {
  7769. unsigned int p = d->nodes[node].parent;
  7770. if (p)
  7771. return (int)p;
  7772. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7773. d->nodes[0].tag == BR_TAG_HTML &&
  7774. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7775. return 0;
  7776. return -1;
  7777. }
  7778. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7779. const struct sel_part *p, int k)
  7780. {
  7781. unsigned int c, pl;
  7782. const char *ps;
  7783. if (k == 0)
  7784. return 1;
  7785. ps = s + p[k - 1].st;
  7786. pl = p[k - 1].en - p[k - 1].st;
  7787. switch (p[k].comb) {
  7788. case '>': {
  7789. int pc = sel_parent(d, node);
  7790. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7791. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7792. }
  7793. case '+':
  7794. c = sel_prev_elem(d, node);
  7795. return c && compound_match(d, c, ps, pl) &&
  7796. sel_from(d, c, s, p, k - 1);
  7797. case '~':
  7798. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7799. if (compound_match(d, c, ps, pl) &&
  7800. sel_from(d, c, s, p, k - 1))
  7801. return 1;
  7802. return 0;
  7803. default: {
  7804. int pc;
  7805. for (pc = sel_parent(d, node); pc >= 0;
  7806. pc = sel_parent(d, (unsigned int)pc))
  7807. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7808. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7809. return 1;
  7810. return 0;
  7811. }
  7812. }
  7813. }
  7814. /* Match one complex selector. right_done: the caller already matched the
  7815. * rightmost compound (the rule index key). */
  7816. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7817. unsigned int len, int right_done)
  7818. {
  7819. struct sel_part parts[SEL_PARTS];
  7820. int np = sel_split(s, len, parts);
  7821. if (np <= 0)
  7822. return 0;
  7823. if (!right_done &&
  7824. !compound_match(d, node, s + parts[np - 1].st,
  7825. parts[np - 1].en - parts[np - 1].st))
  7826. return 0;
  7827. return sel_from(d, node, s, parts, np - 1);
  7828. }
  7829. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7830. unsigned int len)
  7831. {
  7832. return selector_match(d, node, s, len, 0);
  7833. }
  7834. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7835. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7836. * and :where counts nothing. */
  7837. static unsigned int selector_specificity(const char *s, unsigned int len)
  7838. {
  7839. unsigned int i = 0, spec = 0, bl;
  7840. char buf[64];
  7841. while (i < len) {
  7842. char c = s[i];
  7843. if (c == '#' || c == '.') {
  7844. spec += c == '#' ? 100 : 10;
  7845. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7846. } else if (c == '[') {
  7847. spec += 10;
  7848. i = sel_skip_group(s, i, len);
  7849. } else if (c == ':') {
  7850. unsigned int st, nl;
  7851. if (i + 1 < len && s[i + 1] == ':') {
  7852. spec += 1;
  7853. i += 2;
  7854. while (i < len && is_ident(s[i]))
  7855. i++;
  7856. continue;
  7857. }
  7858. st = ++i;
  7859. while (i < len && is_ident(s[i]))
  7860. i++;
  7861. nl = i - st;
  7862. if (i < len && s[i] == '(') {
  7863. unsigned int e = sel_skip_group(s, i, len);
  7864. if (sel_name_is(s + st, nl, "where")) {
  7865. /* zero */
  7866. } else if (sel_name_is(s + st, nl, "not") ||
  7867. sel_name_is(s + st, nl, "is") ||
  7868. sel_name_is(s + st, nl, "has") ||
  7869. sel_name_is(s + st, nl, "matches") ||
  7870. sel_name_is(s + st, nl,
  7871. "-webkit-any")) {
  7872. unsigned int pos = 0, as, ae, best = 0;
  7873. unsigned int al = e > i + 1 ?
  7874. e - i - 2 : 0;
  7875. while (sel_list_next(s + i + 1, al,
  7876. &pos, &as, &ae)) {
  7877. unsigned int v =
  7878. selector_specificity(
  7879. s + i + 1 + as,
  7880. ae - as);
  7881. if (v > best)
  7882. best = v;
  7883. }
  7884. spec += best;
  7885. } else {
  7886. spec += 10;
  7887. }
  7888. i = e;
  7889. } else if (sel_name_is(s + st, nl, "before") ||
  7890. sel_name_is(s + st, nl, "after")) {
  7891. spec += 1;
  7892. } else {
  7893. spec += 10;
  7894. }
  7895. } else if (is_ident(c) || c == '\\') {
  7896. spec += 1;
  7897. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  7898. } else {
  7899. i++;
  7900. }
  7901. }
  7902. return spec;
  7903. }
  7904. /* Cascade filter: drop a selector the engine cannot match (user-state
  7905. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  7906. * ones the caller handles), so it does not waste a rule slot. Arguments of
  7907. * functional pseudo-classes are not checked: an unknown pseudo-class there
  7908. * is simply false, which is right for :not(:hover). */
  7909. static int css_selector_supported(const char *s, unsigned int len)
  7910. {
  7911. struct sel_part parts[SEL_PARTS];
  7912. unsigned int i = 0;
  7913. if (sel_split(s, len, parts) <= 0)
  7914. return 0;
  7915. while (i < len) {
  7916. if (s[i] == '(' || s[i] == '[') {
  7917. i = sel_skip_group(s, i, len);
  7918. } else if (s[i] == '\\') {
  7919. i += 2;
  7920. } else if (s[i] == ':') {
  7921. unsigned int st, nl;
  7922. int fn;
  7923. if (i + 1 < len && s[i + 1] == ':')
  7924. return 0;
  7925. st = ++i;
  7926. while (i < len && is_ident(s[i]))
  7927. i++;
  7928. nl = i - st;
  7929. fn = i < len && s[i] == '(';
  7930. if (!sel_pseudo_known(s + st, nl, fn))
  7931. return 0;
  7932. } else {
  7933. i++;
  7934. }
  7935. }
  7936. return 1;
  7937. }
  7938. #define RULE_MATCH_MAX 512
  7939. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  7940. static int cascade_pe;
  7941. struct rule_match {
  7942. unsigned short idx;
  7943. short spec; /* precedence of normal declarations */
  7944. short spec_imp; /* precedence of !important declarations */
  7945. };
  7946. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  7947. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  7948. * unlayered rules, then the style attribute. !important reverses the
  7949. * layer order: unlayered lowest, the first layer highest, the style
  7950. * attribute above all. Both fit a short. */
  7951. #define CASCADE_BAND 512
  7952. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  7953. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  7954. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  7955. {
  7956. unsigned int band;
  7957. if (spec >= CASCADE_BAND)
  7958. spec = CASCADE_BAND - 1;
  7959. if (important)
  7960. band = layer ? CASCADE_UNLAYERED - layer : 0;
  7961. else
  7962. band = layer ? layer : CASCADE_UNLAYERED;
  7963. return (int)(band * CASCADE_BAND + spec);
  7964. }
  7965. static struct rule_match rule_matches[RULE_MATCH_MAX];
  7966. static unsigned int nrule_matches;
  7967. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  7968. {
  7969. struct br_rule *r = &d->rules[idx];
  7970. const char *key = attr_ptr(d, r->key_off);
  7971. int spec, key_ok = 0;
  7972. if (r->pe != cascade_pe)
  7973. return;
  7974. if (key[0]) {
  7975. if (!compound_match(d, node, key, s_len(key)))
  7976. return;
  7977. key_ok = 1;
  7978. }
  7979. if (!(r->single && key_ok) &&
  7980. !selector_match(d, node, attr_ptr(d, r->sel_off),
  7981. s_len(attr_ptr(d, r->sel_off)), key_ok))
  7982. return;
  7983. spec = (int)r->spec;
  7984. if (nrule_matches < RULE_MATCH_MAX) {
  7985. rule_matches[nrule_matches].idx = (unsigned short)idx;
  7986. rule_matches[nrule_matches].spec =
  7987. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  7988. rule_matches[nrule_matches].spec_imp =
  7989. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  7990. nrule_matches++;
  7991. }
  7992. }
  7993. static void rule_scan_list(struct br_doc *d, unsigned int node,
  7994. unsigned short head)
  7995. {
  7996. while (head != 0xFFFF) {
  7997. rule_try(d, node, head);
  7998. head = d->rule_next[head];
  7999. }
  8000. }
  8001. static void rule_scan_class(struct br_doc *d, unsigned int node,
  8002. const char *cls, unsigned int len)
  8003. {
  8004. if (len)
  8005. rule_scan_list(d, node,
  8006. d->rule_bucket[rule_hash(cls, len)]);
  8007. }
  8008. static void rule_collect(struct br_doc *d, unsigned int node)
  8009. {
  8010. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  8011. const char *id = attr_ptr(d, d->nodes[node].id_off);
  8012. unsigned int tag = d->nodes[node].tag;
  8013. unsigned int i;
  8014. nrule_matches = 0;
  8015. rule_scan_list(d, node, d->rule_any);
  8016. if (tag < BR_TAG_MAX)
  8017. rule_scan_list(d, node, d->rule_tag[tag]);
  8018. if (id[0])
  8019. rule_scan_list(d, node,
  8020. d->rule_bucket[rule_hash(id, s_len(id))]);
  8021. i = 0;
  8022. while (cls[i]) {
  8023. unsigned int st, n;
  8024. while (cls[i] && is_space(cls[i]))
  8025. i++;
  8026. st = i;
  8027. while (cls[i] && !is_space(cls[i]))
  8028. i++;
  8029. n = i - st;
  8030. if (n)
  8031. rule_scan_class(d, node, cls + st, n);
  8032. }
  8033. /* Rules must cascade in document order even though the index visits
  8034. * them in bucket order. */
  8035. for (i = 1; i < nrule_matches; i++) {
  8036. struct rule_match m = rule_matches[i];
  8037. unsigned int j = i;
  8038. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  8039. rule_matches[j] = rule_matches[j - 1];
  8040. j--;
  8041. }
  8042. rule_matches[j] = m;
  8043. }
  8044. }
  8045. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  8046. int font_px)
  8047. {
  8048. if (*kind == 4) {
  8049. *val = (unsigned short)(*val * font_px / 100);
  8050. *kind = 1;
  8051. } else if (*kind == 5) {
  8052. *val = (unsigned short)(*val * root_font_px / 100);
  8053. *kind = 1;
  8054. }
  8055. }
  8056. static void resolve_size_units(struct br_style *st)
  8057. {
  8058. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  8059. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  8060. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  8061. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  8062. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  8063. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  8064. }
  8065. static void set_border_side_w(struct br_style *st, int side, int v)
  8066. {
  8067. if (v < 0)
  8068. v = 0;
  8069. if (v > 32)
  8070. v = 32;
  8071. st->border_w[side] = (unsigned char)v;
  8072. if (st->border_w[side] && !st->border_c[side])
  8073. st->border_c[side] = st->color;
  8074. }
  8075. static void set_border_side_c(struct br_style *st, int side, int v)
  8076. {
  8077. st->border_c[side] = (unsigned short)v;
  8078. }
  8079. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  8080. {
  8081. switch (dcl->prop) {
  8082. case PROP_COLOR:
  8083. st->color = (unsigned short)dcl->val;
  8084. break;
  8085. case PROP_BG:
  8086. st->bg = (unsigned short)dcl->val;
  8087. st->has_bg = 1;
  8088. st->bg_set = 1;
  8089. break;
  8090. case PROP_BG_NONE:
  8091. st->has_bg = 0;
  8092. st->bg_set = 1;
  8093. break;
  8094. case PROP_BG_IMAGE:
  8095. st->bg_image = (unsigned short)dcl->val;
  8096. break;
  8097. case PROP_BG_REPEAT:
  8098. st->bg_repeat = (unsigned char)dcl->val;
  8099. break;
  8100. case PROP_BG_POS_X_KIND:
  8101. st->bg_pos_x_kind = (unsigned char)dcl->val;
  8102. break;
  8103. case PROP_BG_POS_X:
  8104. st->bg_pos_x = (signed short)dcl->val;
  8105. break;
  8106. case PROP_BG_POS_Y_KIND:
  8107. st->bg_pos_y_kind = (unsigned char)dcl->val;
  8108. break;
  8109. case PROP_BG_POS_Y:
  8110. st->bg_pos_y = (signed short)dcl->val;
  8111. break;
  8112. case PROP_BG_SIZE_KIND:
  8113. st->bg_size_kind = (unsigned char)dcl->val;
  8114. break;
  8115. case PROP_BG_SIZE_W:
  8116. st->bg_size_w = (unsigned short)dcl->val;
  8117. break;
  8118. case PROP_BG_SIZE_H:
  8119. st->bg_size_h = (unsigned short)dcl->val;
  8120. break;
  8121. case PROP_FONT_PX:
  8122. st->font_raw = (unsigned short)dcl->val;
  8123. break;
  8124. case PROP_FONT_UNIT:
  8125. st->font_unit = (unsigned char)dcl->val;
  8126. break;
  8127. case PROP_BOLD:
  8128. st->bold = (unsigned char)(dcl->val != 0);
  8129. break;
  8130. case PROP_ITALIC:
  8131. st->italic = (unsigned char)(dcl->val != 0);
  8132. break;
  8133. case PROP_MONO:
  8134. st->mono = (unsigned char)(dcl->val != 0);
  8135. break;
  8136. case PROP_FONT_FACE:
  8137. st->font_face = (unsigned char)dcl->val;
  8138. break;
  8139. case PROP_ALIGN:
  8140. st->align = (unsigned char)dcl->val;
  8141. break;
  8142. case PROP_UNDERLINE:
  8143. st->underline = (unsigned char)(dcl->val != 0);
  8144. break;
  8145. case PROP_STRIKE:
  8146. st->strike = (unsigned char)(dcl->val != 0);
  8147. break;
  8148. case PROP_OVERLINE:
  8149. st->overline = (unsigned char)(dcl->val != 0);
  8150. break;
  8151. case PROP_DISPLAY:
  8152. st->display = (unsigned char)(dcl->val &
  8153. ~BR_DISPLAY_INLINE_FLAG);
  8154. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  8155. st->list_item = 0;
  8156. break;
  8157. case PROP_VIS_HIDDEN:
  8158. st->vis_hidden = (unsigned char)(dcl->val != 0);
  8159. break;
  8160. case PROP_OPACITY_HIDDEN:
  8161. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  8162. break;
  8163. case PROP_FILL_MODE:
  8164. st->fill_mode = (unsigned char)dcl->val;
  8165. break;
  8166. case PROP_FILL:
  8167. st->fill = (unsigned short)dcl->val;
  8168. break;
  8169. case PROP_STROKE_MODE:
  8170. st->stroke_mode = (unsigned char)dcl->val;
  8171. break;
  8172. case PROP_STROKE:
  8173. st->stroke = (unsigned short)dcl->val;
  8174. break;
  8175. case PROP_STROKE_W:
  8176. st->stroke_w = (unsigned short)dcl->val;
  8177. break;
  8178. case PROP_FILL_RULE:
  8179. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  8180. break;
  8181. case PROP_OVERFLOW_HIDDEN:
  8182. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  8183. break;
  8184. case PROP_MT: st->mt = dcl->val; break;
  8185. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  8186. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  8187. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  8188. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  8189. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  8190. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  8191. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  8192. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  8193. case PROP_MR: st->mr = dcl->val; break;
  8194. case PROP_MB: st->mb = dcl->val; break;
  8195. case PROP_ML: st->ml = dcl->val; break;
  8196. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  8197. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  8198. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  8199. break;
  8200. case PROP_PT: st->pt = dcl->val; break;
  8201. case PROP_PR: st->pr = dcl->val; break;
  8202. case PROP_PB: st->pb = dcl->val; break;
  8203. case PROP_PL: st->pl = dcl->val; break;
  8204. case PROP_MT_U:
  8205. case PROP_MR_U:
  8206. case PROP_MB_U:
  8207. case PROP_ML_U:
  8208. case PROP_PT_U:
  8209. case PROP_PR_U:
  8210. case PROP_PB_U:
  8211. case PROP_PL_U:
  8212. st->mp_unit[dcl->prop - PROP_MT_U] =
  8213. (unsigned char)dcl->val;
  8214. break;
  8215. case PROP_BORDER_W:
  8216. set_border_side_w(st, 0, dcl->val);
  8217. set_border_side_w(st, 1, dcl->val);
  8218. set_border_side_w(st, 2, dcl->val);
  8219. set_border_side_w(st, 3, dcl->val);
  8220. break;
  8221. case PROP_BORDER_COLOR:
  8222. set_border_side_c(st, 0, dcl->val);
  8223. set_border_side_c(st, 1, dcl->val);
  8224. set_border_side_c(st, 2, dcl->val);
  8225. set_border_side_c(st, 3, dcl->val);
  8226. break;
  8227. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  8228. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  8229. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  8230. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  8231. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  8232. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  8233. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  8234. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  8235. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  8236. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  8237. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  8238. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  8239. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  8240. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  8241. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  8242. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  8243. case PROP_RADIUS_TL:
  8244. case PROP_RADIUS_TR:
  8245. case PROP_RADIUS_BR:
  8246. case PROP_RADIUS_BL:
  8247. st->radius[dcl->prop - PROP_RADIUS_TL] =
  8248. (unsigned char)dcl->val;
  8249. break;
  8250. case PROP_LIST_NONE:
  8251. st->no_bullet = (unsigned char)(dcl->val != 0);
  8252. break;
  8253. case PROP_LIST_STYLE:
  8254. st->list_style = (unsigned char)dcl->val;
  8255. break;
  8256. case PROP_CONTENT:
  8257. st->content = (unsigned short)dcl->val;
  8258. break;
  8259. case PROP_ASPECT_W:
  8260. st->aspect_w = (unsigned short)dcl->val;
  8261. break;
  8262. case PROP_ASPECT_H:
  8263. st->aspect_h = (unsigned short)dcl->val;
  8264. break;
  8265. case PROP_NOWRAP:
  8266. st->nowrap = (unsigned char)(dcl->val != 0);
  8267. break;
  8268. case PROP_WIDTH:
  8269. st->width = (unsigned short)dcl->val;
  8270. break;
  8271. case PROP_WIDTH_KIND:
  8272. st->width_kind = (unsigned char)dcl->val;
  8273. break;
  8274. case PROP_WIDTH_CALC_PCT:
  8275. st->width_calc_pct = dcl->val;
  8276. break;
  8277. case PROP_WIDTH_CALC_PX:
  8278. st->width_calc_px = dcl->val;
  8279. break;
  8280. case PROP_MAXW:
  8281. st->max_width = (unsigned short)dcl->val;
  8282. break;
  8283. case PROP_MAXW_KIND:
  8284. st->max_width_kind = (unsigned char)dcl->val;
  8285. break;
  8286. case PROP_MAXW_CALC_PCT:
  8287. st->max_width_calc_pct = (signed short)dcl->val;
  8288. break;
  8289. case PROP_MAXW_CALC_PX:
  8290. st->max_width_calc_px = (signed short)dcl->val;
  8291. break;
  8292. case PROP_MINW:
  8293. st->min_width = (unsigned short)dcl->val;
  8294. break;
  8295. case PROP_MINW_KIND:
  8296. st->min_width_kind = (unsigned char)dcl->val;
  8297. break;
  8298. case PROP_MINW_CALC_PCT:
  8299. st->min_width_calc_pct = (signed short)dcl->val;
  8300. break;
  8301. case PROP_MINW_CALC_PX:
  8302. st->min_width_calc_px = (signed short)dcl->val;
  8303. break;
  8304. case PROP_HEIGHT:
  8305. st->height = (unsigned short)dcl->val;
  8306. break;
  8307. case PROP_HEIGHT_KIND:
  8308. st->height_kind = (unsigned char)dcl->val;
  8309. break;
  8310. case PROP_HEIGHT_CALC_PCT:
  8311. st->height_calc_pct = (signed short)dcl->val;
  8312. break;
  8313. case PROP_HEIGHT_CALC_PX:
  8314. st->height_calc_px = (signed short)dcl->val;
  8315. break;
  8316. case PROP_MINH:
  8317. st->min_height = (unsigned short)dcl->val;
  8318. break;
  8319. case PROP_MINH_KIND:
  8320. st->min_height_kind = (unsigned char)dcl->val;
  8321. break;
  8322. case PROP_MINH_CALC_PCT:
  8323. st->min_height_calc_pct = (signed short)dcl->val;
  8324. break;
  8325. case PROP_MINH_CALC_PX:
  8326. st->min_height_calc_px = (signed short)dcl->val;
  8327. break;
  8328. case PROP_MAXH:
  8329. st->max_height = (unsigned short)dcl->val;
  8330. break;
  8331. case PROP_MAXH_KIND:
  8332. st->max_height_kind = (unsigned char)dcl->val;
  8333. break;
  8334. case PROP_MAXH_CALC_PCT:
  8335. st->max_height_calc_pct = (signed short)dcl->val;
  8336. break;
  8337. case PROP_MAXH_CALC_PX:
  8338. st->max_height_calc_px = (signed short)dcl->val;
  8339. break;
  8340. case PROP_LH_PCT:
  8341. st->line_h_pct = (unsigned char)dcl->val;
  8342. break;
  8343. case PROP_LH_PX:
  8344. st->line_h_px = (unsigned short)dcl->val;
  8345. break;
  8346. case PROP_UPPER:
  8347. st->uppercase = (unsigned char)(dcl->val != 0);
  8348. break;
  8349. case PROP_CURSOR:
  8350. st->cursor = (unsigned char)dcl->val;
  8351. break;
  8352. case PROP_FLOAT:
  8353. st->float_left = (unsigned char)dcl->val;
  8354. break;
  8355. case PROP_CLEAR:
  8356. st->clear = (unsigned char)dcl->val;
  8357. break;
  8358. case PROP_FLEX_GROW:
  8359. st->flex_grow = (unsigned char)dcl->val;
  8360. break;
  8361. case PROP_FLEX_COL:
  8362. st->flex_col = (unsigned char)(dcl->val != 0);
  8363. break;
  8364. case PROP_FLEX_WRAP:
  8365. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8366. break;
  8367. case PROP_JUSTIFY:
  8368. st->justify = (unsigned char)dcl->val;
  8369. break;
  8370. case PROP_ALIGN_ITEMS:
  8371. st->align_items = (unsigned char)dcl->val;
  8372. break;
  8373. case PROP_ALIGN_SELF:
  8374. st->align_self = (unsigned char)dcl->val;
  8375. break;
  8376. case PROP_GRID_NCOLS:
  8377. st->grid_ncols = (unsigned char)dcl->val;
  8378. break;
  8379. case PROP_GRID_GAP:
  8380. st->grid_gap = (unsigned char)dcl->val;
  8381. break;
  8382. case PROP_GRID_SLOT:
  8383. st->grid_slot = (unsigned char)dcl->val;
  8384. break;
  8385. case PROP_GRID_AUTO:
  8386. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8387. break;
  8388. case PROP_POS:
  8389. st->pos = (unsigned char)dcl->val;
  8390. break;
  8391. case PROP_LEFT:
  8392. st->left = dcl->val;
  8393. break;
  8394. case PROP_TOP:
  8395. st->top = dcl->val;
  8396. break;
  8397. case PROP_RIGHT:
  8398. st->right = dcl->val;
  8399. break;
  8400. case PROP_BOTTOM:
  8401. st->bottom = dcl->val;
  8402. break;
  8403. case PROP_LEFT_SET:
  8404. st->left_set = (unsigned char)(dcl->val != 0);
  8405. break;
  8406. case PROP_TOP_SET:
  8407. st->top_set = (unsigned char)(dcl->val != 0);
  8408. break;
  8409. case PROP_RIGHT_SET:
  8410. st->right_set = (unsigned char)(dcl->val != 0);
  8411. break;
  8412. case PROP_BOTTOM_SET:
  8413. st->bottom_set = (unsigned char)(dcl->val != 0);
  8414. break;
  8415. case PROP_ZINDEX:
  8416. st->z_index = dcl->val;
  8417. break;
  8418. case PROP_ZSET:
  8419. st->z_set = (unsigned char)(dcl->val != 0);
  8420. break;
  8421. case PROP_TRANSFORM_X:
  8422. st->tx = dcl->val;
  8423. break;
  8424. case PROP_TRANSFORM_Y:
  8425. st->ty = dcl->val;
  8426. break;
  8427. case PROP_TRANSFORM_SET:
  8428. st->transform_set = (unsigned char)(dcl->val != 0);
  8429. break;
  8430. case PROP_TRANSFORM_PAINT:
  8431. st->transform_scale = (unsigned char)(dcl->val != 0);
  8432. break;
  8433. case PROP_SCALE_X:
  8434. st->scale_x = (signed short)dcl->val;
  8435. break;
  8436. case PROP_SCALE_Y:
  8437. st->scale_y = (signed short)dcl->val;
  8438. break;
  8439. case PROP_ROTATE:
  8440. st->rotate_x100 = (signed short)dcl->val;
  8441. break;
  8442. case PROP_ORIGIN_X_KIND:
  8443. st->origin_x_kind = (unsigned char)dcl->val;
  8444. break;
  8445. case PROP_ORIGIN_X:
  8446. st->origin_x = (signed short)dcl->val;
  8447. break;
  8448. case PROP_ORIGIN_Y_KIND:
  8449. st->origin_y_kind = (unsigned char)dcl->val;
  8450. break;
  8451. case PROP_ORIGIN_Y:
  8452. st->origin_y = (signed short)dcl->val;
  8453. break;
  8454. default:
  8455. break;
  8456. }
  8457. }
  8458. /* The element's custom properties for rules flagged USES_SCOPED: the
  8459. * definitions of its matched rules in cascade order (precedence first,
  8460. * then document order), over the document-wide table. Inheritance from
  8461. * ancestors is not modelled; a name no matched rule defines falls back to
  8462. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8463. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8464. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8465. static void css_env_build(struct br_doc *d)
  8466. {
  8467. static unsigned short order[RULE_MATCH_MAX];
  8468. unsigned int i, need = 0, n = 0;
  8469. css_env_active = 0;
  8470. css_nlocal = 0;
  8471. for (i = 0; i < nrule_matches; i++)
  8472. if (d->rules[rule_matches[i].idx].var_scope &
  8473. BR_RULE_USES_SCOPED)
  8474. need = 1;
  8475. if (!need)
  8476. return;
  8477. /* Definitions apply lowest precedence first so the winner is
  8478. * written last (stable: document order within a precedence). */
  8479. for (i = 0; i < nrule_matches; i++) {
  8480. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8481. unsigned int j = n;
  8482. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8483. continue;
  8484. while (j > 0 && rule_matches[order[j - 1]].spec >
  8485. rule_matches[i].spec) {
  8486. order[j] = order[j - 1];
  8487. j--;
  8488. }
  8489. order[j] = i;
  8490. n++;
  8491. }
  8492. for (i = 0; i < n; i++) {
  8493. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8494. const char *raw = attr_ptr(d, r->raw_off);
  8495. unsigned int save = css_nlocal, k;
  8496. /* Collect this block's definitions after the earlier ones,
  8497. * replacing a name already present. */
  8498. {
  8499. unsigned int base = save;
  8500. css_local_append(raw, r->raw_len);
  8501. for (k = base; k < css_nlocal; k++) {
  8502. int prev = css_local_find_before(
  8503. css_local[k].name, css_local[k].nlen, base);
  8504. if (prev >= 0) {
  8505. css_local[prev] = css_local[k];
  8506. css_local[k].nlen = 0;
  8507. }
  8508. }
  8509. }
  8510. }
  8511. css_env_active = 1;
  8512. }
  8513. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8514. int spec, int spec_imp, short *best, short *best_imp)
  8515. {
  8516. unsigned char p = dcl->prop;
  8517. if (p >= PROP_COUNT)
  8518. return;
  8519. /* A transparent background competes with a colour for one slot. */
  8520. if (p == PROP_BG_NONE)
  8521. p = PROP_BG;
  8522. if (dcl->important) {
  8523. if (spec_imp >= best_imp[p]) {
  8524. apply_decl(st, dcl);
  8525. best_imp[p] = (short)spec_imp;
  8526. }
  8527. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8528. apply_decl(st, dcl);
  8529. best[p] = (short)spec;
  8530. }
  8531. }
  8532. void br_style_for(struct br_doc *d, unsigned int node,
  8533. const struct br_style *parent, struct br_style *out)
  8534. {
  8535. unsigned int tag;
  8536. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8537. unsigned int i;
  8538. if (!cascade_pe && d->nodes[node].style_valid) {
  8539. *out = d->nodes[node].style;
  8540. return;
  8541. }
  8542. *out = *parent;
  8543. out->has_bg = 0;
  8544. out->bg_set = 0;
  8545. out->bg_image = 0;
  8546. out->bg_repeat = 0;
  8547. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8548. out->bg_pos_x = out->bg_pos_y = 0;
  8549. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8550. out->float_left = 0;
  8551. out->clear = 0;
  8552. out->list_item = 0;
  8553. out->font_unit = 0;
  8554. for (i = 0; i < 4; i++) {
  8555. out->border_w[i] = 0;
  8556. out->border_c[i] = 0;
  8557. out->border_s[i] = BR_BORDER_SOLID;
  8558. out->radius[i] = 0;
  8559. }
  8560. out->width = out->max_width = out->min_width = out->height = 0;
  8561. out->min_height = out->max_height = 0;
  8562. out->width_calc_pct = out->width_calc_px = 0;
  8563. out->height_calc_pct = out->height_calc_px = 0;
  8564. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8565. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8566. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8567. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8568. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8569. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8570. out->box_sizing = 0;
  8571. out->ml_auto = out->mr_auto = 0;
  8572. out->mt = out->mr = out->mb = out->ml = 0;
  8573. out->pt = out->pr = out->pb = out->pl = 0;
  8574. for (i = 0; i < 8; i++) {
  8575. out->mp_unit[i] = 0;
  8576. out->mp_calc_px[i] = 0;
  8577. }
  8578. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8579. out->justify = out->align_items = out->align_self = 0;
  8580. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8581. out->grid_auto = 0;
  8582. out->aspect_w = out->aspect_h = 0;
  8583. out->pos = out->left_set = out->top_set = 0;
  8584. out->right_set = out->bottom_set = 0;
  8585. out->left = out->top = out->right = out->bottom = 0;
  8586. out->z_index = 0;
  8587. out->z_set = 0;
  8588. out->tx = out->ty = 0;
  8589. out->transform_set = 0;
  8590. out->transform_scale = 0;
  8591. out->scale_x = out->scale_y = 100;
  8592. out->rotate_x100 = 0;
  8593. out->origin_x = out->origin_y = 50;
  8594. out->origin_x_kind = out->origin_y_kind = 1;
  8595. out->vis_hidden = out->opacity_hidden = 0;
  8596. out->inline_flex = 0;
  8597. out->overflow_hidden = 0;
  8598. out->content = 0;
  8599. /* A pseudo-element is an anonymous inline box: none of the
  8600. * element's UA defaults or presentational hints apply to it. */
  8601. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8602. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8603. BR_DISPLAY_INLINE;
  8604. for (i = 0; i < PROP_COUNT; i++) {
  8605. best[i] = -1;
  8606. best_imp[i] = -1;
  8607. }
  8608. if (!cascade_pe)
  8609. switch (tag) {
  8610. case BR_TAG_HEAD:
  8611. case BR_TAG_TITLE:
  8612. case BR_TAG_META:
  8613. case BR_TAG_LINK:
  8614. case BR_TAG_BASE:
  8615. case BR_TAG_SCRIPT:
  8616. case BR_TAG_STYLE:
  8617. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8618. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8619. out->display = BR_DISPLAY_NONE;
  8620. break;
  8621. case BR_TAG_BODY:
  8622. /* The UA default: the page itself has no gutter, so a site's
  8623. * `body { margin: 0 }` really starts at the canvas edge. */
  8624. out->mt = out->mb = out->ml = out->mr = 8;
  8625. break;
  8626. case BR_TAG_P:
  8627. out->mt = out->mb = 8;
  8628. break;
  8629. case BR_TAG_H1:
  8630. out->font_px = 32; out->bold = 1;
  8631. out->mt = 10; out->mb = 6;
  8632. break;
  8633. case BR_TAG_H2:
  8634. out->font_px = 24; out->bold = 1;
  8635. out->mt = 8; out->mb = 4;
  8636. break;
  8637. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8638. out->bold = 1; out->mt = 6; out->mb = 3;
  8639. if (tag == BR_TAG_H3)
  8640. out->font_px = 19;
  8641. else if (tag == BR_TAG_H4)
  8642. out->font_px = 17;
  8643. else if (tag == BR_TAG_H5)
  8644. out->font_px = 15;
  8645. else
  8646. out->font_px = 13;
  8647. break;
  8648. case BR_TAG_UL: case BR_TAG_OL:
  8649. out->mt = out->mb = 6; out->pl = 20;
  8650. break;
  8651. case BR_TAG_LI:
  8652. out->mt = out->mb = 2;
  8653. out->list_item = 1;
  8654. break;
  8655. case BR_TAG_BLOCKQUOTE:
  8656. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8657. break;
  8658. case BR_TAG_PRE:
  8659. out->mt = out->mb = 8; out->pl = 8;
  8660. out->has_bg = 1; out->bg = theme_panel;
  8661. out->mono = 1;
  8662. break;
  8663. case BR_TAG_CENTER:
  8664. out->align = BR_ALIGN_CENTER;
  8665. break;
  8666. case BR_TAG_A:
  8667. out->color = theme_link; out->underline = 1;
  8668. break;
  8669. case BR_TAG_B: case BR_TAG_STRONG:
  8670. out->bold = 1;
  8671. break;
  8672. case BR_TAG_I: case BR_TAG_EM:
  8673. out->italic = 1;
  8674. break;
  8675. case BR_TAG_U:
  8676. out->underline = 1;
  8677. break;
  8678. case BR_TAG_S: case BR_TAG_STRIKE:
  8679. out->strike = 1;
  8680. break;
  8681. case BR_TAG_CODE:
  8682. out->color = 0x8010;
  8683. out->mono = 1;
  8684. break;
  8685. case BR_TAG_SMALL:
  8686. out->font_px = 13;
  8687. break;
  8688. case BR_TAG_BIG:
  8689. out->font_px = 20;
  8690. break;
  8691. case BR_TAG_TD: case BR_TAG_TH:
  8692. out->pr = 12;
  8693. /* A cell establishes left-aligned content even inside a legacy
  8694. * <center> (HN's outer page wrapper); author CSS/align below
  8695. * can still override this. */
  8696. out->align = BR_ALIGN_LEFT;
  8697. break;
  8698. case BR_TAG_VIDEO:
  8699. out->display = BR_DISPLAY_BLOCK;
  8700. break;
  8701. case BR_TAG_SVG:
  8702. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8703. * external-link icon follows its link text on the same line.
  8704. * Flex rows still treat it as an item (flex_wants_row). */
  8705. out->display = BR_DISPLAY_INLINE;
  8706. break;
  8707. default:
  8708. break;
  8709. }
  8710. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8711. * presentational hints; author CSS still wins because it is applied
  8712. * below. */
  8713. if (!cascade_pe) {
  8714. unsigned short bgc;
  8715. const char *bga = br_find_attr(d, node, "bgcolor");
  8716. const char *ala = br_find_attr(d, node, "align");
  8717. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8718. out->bg = bgc;
  8719. out->has_bg = 1;
  8720. }
  8721. if (ala) {
  8722. if (same_string(ala, "right"))
  8723. out->align = BR_ALIGN_RIGHT;
  8724. else if (same_string(ala, "center"))
  8725. out->align = BR_ALIGN_CENTER;
  8726. else if (same_string(ala, "left"))
  8727. out->align = BR_ALIGN_LEFT;
  8728. }
  8729. }
  8730. /* presentational <font color>: author CSS below still wins */
  8731. if (!cascade_pe && d->nodes[node].color_off) {
  8732. unsigned short c;
  8733. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8734. if (parse_color(v, s_len(v), &c))
  8735. out->color = c;
  8736. }
  8737. /* SVG presentation attributes; CSS declarations still win below. */
  8738. if (!cascade_pe && in_svg(d, node)) {
  8739. const char *fa = br_find_attr(d, node, "fill");
  8740. const char *sa = br_find_attr(d, node, "stroke");
  8741. const char *wa = br_find_attr(d, node, "stroke-width");
  8742. const char *ra = br_find_attr(d, node, "fill-rule");
  8743. const char *ca = br_find_attr(d, node, "color");
  8744. unsigned short c;
  8745. int mode;
  8746. if (ca && parse_color(ca, s_len(ca), &c))
  8747. out->color = c;
  8748. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8749. out->fill_mode = (unsigned char)mode;
  8750. if (mode == BR_PAINT_COLOR)
  8751. out->fill = c;
  8752. }
  8753. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8754. out->stroke_mode = (unsigned char)mode;
  8755. if (mode == BR_PAINT_COLOR)
  8756. out->stroke = c;
  8757. }
  8758. if (wa) {
  8759. int sw = svg_len16(wa, s_len(wa));
  8760. if (sw >= 0)
  8761. out->stroke_w = (unsigned short)sw;
  8762. }
  8763. if (ra)
  8764. out->fill_evenodd = same_string(ra, "evenodd");
  8765. }
  8766. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8767. * Author CSS below can still override this. */
  8768. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8769. unsigned int p = d->nodes[node].parent;
  8770. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8771. !br_find_attr(d, p, "open"))
  8772. out->display = BR_DISPLAY_NONE;
  8773. }
  8774. rule_collect(d, node);
  8775. css_env_build(d);
  8776. for (i = 0; i < nrule_matches; i++) {
  8777. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8778. int spec = rule_matches[i].spec;
  8779. int spec_imp = rule_matches[i].spec_imp;
  8780. unsigned int k;
  8781. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8782. /* Re-expand against this element's variables. */
  8783. struct br_decl decls[BR_DECLS];
  8784. unsigned int nd;
  8785. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8786. decls, &nd);
  8787. for (k = 0; k < nd; k++)
  8788. cascade_decl(out, &decls[k], spec, spec_imp,
  8789. best, best_imp);
  8790. continue;
  8791. }
  8792. for (k = 0; k < r->nd; k++)
  8793. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8794. best_imp);
  8795. }
  8796. css_env_active = 0;
  8797. css_nlocal = 0;
  8798. if (!cascade_pe && d->nodes[node].style_off) {
  8799. struct br_decl decls[BR_DECLS];
  8800. unsigned int nd, k;
  8801. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8802. parse_decls(d, v, s_len(v), decls, &nd);
  8803. for (k = 0; k < nd; k++)
  8804. cascade_decl(out, &decls[k],
  8805. CASCADE_INLINE * CASCADE_BAND,
  8806. CASCADE_INLINE * CASCADE_BAND, best,
  8807. best_imp);
  8808. }
  8809. if (out->font_unit) {
  8810. int px = out->font_raw;
  8811. if (out->font_unit == 2)
  8812. px = parent->font_px * out->font_raw / 100;
  8813. else if (out->font_unit == 3)
  8814. px = root_font_px * out->font_raw / 100;
  8815. else if (out->font_unit == 4)
  8816. px = parent->font_px * out->font_raw / 10000;
  8817. if (px < 8)
  8818. px = 8;
  8819. if (px > 72)
  8820. px = 72;
  8821. out->font_px = (unsigned char)px;
  8822. }
  8823. resolve_size_units(out);
  8824. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  8825. out->display = BR_DISPLAY_NONE;
  8826. /* Invisible in-flow content is dropped (it would otherwise take
  8827. * space this engine cannot hide well). An absolute/fixed box keeps
  8828. * its display: it moves nothing, and scripts measure it while it is
  8829. * still transparent (Bootstrap's tooltip is `opacity: 0` until it
  8830. * gets `.in`). The layout lays it out without painting it. */
  8831. if (((out->vis_hidden || out->opacity_hidden) &&
  8832. out->pos != 1 && out->pos != 2) ||
  8833. (out->overflow_hidden && out->height_kind == 1 &&
  8834. out->height == 0))
  8835. out->display = BR_DISPLAY_NONE;
  8836. if (cascade_pe)
  8837. return;
  8838. d->nodes[node].style = *out;
  8839. d->nodes[node].style_valid = 1;
  8840. }
  8841. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  8842. const struct br_style *elem, struct br_style *out,
  8843. char *buf, unsigned int cap)
  8844. {
  8845. struct br_node *n;
  8846. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  8847. const char *c;
  8848. unsigned int len = 0;
  8849. if (out) {
  8850. out->content = 0;
  8851. out->display = BR_DISPLAY_NONE;
  8852. }
  8853. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  8854. (pe != 1 && pe != 2))
  8855. return 0;
  8856. n = &d->nodes[node];
  8857. if (n->type != BR_NODE_ELEM ||
  8858. ((n->pe_state & chk) && !(n->pe_state & has)))
  8859. return 0;
  8860. cascade_pe = pe;
  8861. br_style_for(d, node, elem, out);
  8862. cascade_pe = 0;
  8863. if (out->content == BR_CONTENT_EMPTY) {
  8864. /* No text; the caller may still draw the box (an icon
  8865. * with a size and a background image). */
  8866. buf[0] = '\0';
  8867. n->pe_state |= chk;
  8868. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  8869. out->pos != 2 && (out->width_kind || out->height_kind) &&
  8870. (out->has_bg || out->bg_image))
  8871. n->pe_state |= has;
  8872. else
  8873. out->display = BR_DISPLAY_NONE;
  8874. return 0;
  8875. }
  8876. if (out->content && out->display != BR_DISPLAY_NONE &&
  8877. out->pos != 1 && out->pos != 2) {
  8878. c = attr_ptr(d, d->content_off[out->content - 1]);
  8879. while (*c && len + 1 < cap) {
  8880. if (*c == '\001') {
  8881. char name[32];
  8882. unsigned int k = 0;
  8883. const char *v;
  8884. c++;
  8885. while (*c && *c != '\002' && k + 1 < sizeof(name))
  8886. name[k++] = *c++;
  8887. name[k] = '\0';
  8888. if (*c == '\002')
  8889. c++;
  8890. v = br_find_attr(d, node, name);
  8891. while (v && *v && len + 1 < cap)
  8892. buf[len++] = *v++;
  8893. continue;
  8894. }
  8895. buf[len++] = *c++;
  8896. }
  8897. }
  8898. buf[len] = '\0';
  8899. n->pe_state |= chk;
  8900. if (len)
  8901. n->pe_state |= has;
  8902. return len;
  8903. }
  8904. /* ---- HTML parser ---- */
  8905. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  8906. unsigned int sp, int *pre)
  8907. {
  8908. unsigned int i;
  8909. *pre = 0;
  8910. for (i = 1; i <= sp; i++)
  8911. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  8912. (*pre)++;
  8913. }
  8914. static void implicit_close(struct br_doc *d, unsigned short *stack,
  8915. unsigned int *sp, unsigned int tag)
  8916. {
  8917. while (*sp > 0) {
  8918. unsigned int top = d->nodes[stack[*sp]].tag;
  8919. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  8920. (*sp)--;
  8921. continue;
  8922. }
  8923. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  8924. (*sp)--;
  8925. continue;
  8926. }
  8927. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  8928. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  8929. (*sp)--;
  8930. continue;
  8931. }
  8932. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  8933. (*sp)--;
  8934. continue;
  8935. }
  8936. break;
  8937. }
  8938. }
  8939. static int has_attr_token(const char *s, unsigned int len, const char *name)
  8940. {
  8941. unsigned int i, n = s_len(name);
  8942. for (i = 0; i + n <= len; i++) {
  8943. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  8944. continue;
  8945. if (!ci_eq_n(s + i, name, n))
  8946. continue;
  8947. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  8948. return 1;
  8949. }
  8950. return 0;
  8951. }
  8952. static int stack_has_svg(const struct br_doc *d,
  8953. const unsigned short *stack, unsigned int sp)
  8954. {
  8955. unsigned int k;
  8956. for (k = 0; k <= sp && k < 64; k++)
  8957. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  8958. return 1;
  8959. return 0;
  8960. }
  8961. /* Skip an element subtree by finding its closing tag. Used for SVG
  8962. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  8963. * overwrite the document title. */
  8964. static unsigned int skip_element(const char *html, unsigned int len,
  8965. unsigned int i, const char *name,
  8966. unsigned int n)
  8967. {
  8968. char close[40];
  8969. unsigned int k = 0, p;
  8970. close[k++] = '<';
  8971. close[k++] = '/';
  8972. while (k < sizeof(close) - 1 && n--)
  8973. close[k++] = *name++;
  8974. p = (unsigned int)find_ci(html, len, close, k, i);
  8975. if ((int)p < 0)
  8976. return len;
  8977. p += k;
  8978. while (p < len && html[p] != '>')
  8979. p++;
  8980. if (p < len)
  8981. p++;
  8982. return p;
  8983. }
  8984. /* Store a <script src=...> value in the attribute arena. Kept separate from
  8985. * parse_attrs() so script-only attributes cannot be confused with the
  8986. * value/d attributes that also use src_off on other elements. */
  8987. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  8988. unsigned int len)
  8989. {
  8990. unsigned int i = 0;
  8991. while (i < len) {
  8992. unsigned int ns, ne, vs, ve;
  8993. char quote = 0;
  8994. while (i < len && is_space(s[i]))
  8995. i++;
  8996. if (i >= len || s[i] == '/')
  8997. break;
  8998. ns = i;
  8999. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  9000. i++;
  9001. ne = i;
  9002. while (i < len && is_space(s[i]))
  9003. i++;
  9004. if (i >= len || s[i] != '=')
  9005. continue;
  9006. i++;
  9007. while (i < len && is_space(s[i]))
  9008. i++;
  9009. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9010. quote = s[i++];
  9011. vs = i;
  9012. while (i < len && s[i] != quote)
  9013. i++;
  9014. ve = i;
  9015. if (i < len)
  9016. i++;
  9017. } else {
  9018. vs = i;
  9019. while (i < len && !is_space(s[i]) && s[i] != '>')
  9020. i++;
  9021. ve = i;
  9022. }
  9023. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  9024. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  9025. }
  9026. return 0;
  9027. }
  9028. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  9029. * application/json, text/template, ...) and non-JS type values. External
  9030. * src scripts are allowed; the browser fetches them separately. Returns 1
  9031. * for a classic script, 2 for type="module", 0 to skip (also for a
  9032. * `nomodule` fallback, since module scripts run). */
  9033. static int script_is_javascript(const char *s, unsigned int len)
  9034. {
  9035. int kind = 1;
  9036. unsigned int i = 0;
  9037. while (i < len) {
  9038. unsigned int ns, ne, vs = 0, ve = 0;
  9039. char quote = 0;
  9040. int has_val = 0;
  9041. while (i < len && is_space(s[i]))
  9042. i++;
  9043. if (i >= len)
  9044. break;
  9045. ns = i;
  9046. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  9047. i++;
  9048. ne = i;
  9049. while (i < len && is_space(s[i]))
  9050. i++;
  9051. if (i < len && s[i] == '=') {
  9052. i++;
  9053. while (i < len && is_space(s[i]))
  9054. i++;
  9055. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9056. quote = s[i++];
  9057. vs = i;
  9058. while (i < len && s[i] != quote)
  9059. i++;
  9060. ve = i;
  9061. if (i < len)
  9062. i++;
  9063. } else {
  9064. vs = i;
  9065. while (i < len && !is_space(s[i]) && s[i] != '/')
  9066. i++;
  9067. ve = i;
  9068. }
  9069. has_val = 1;
  9070. } else if (i == ne) {
  9071. i++;
  9072. }
  9073. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  9074. return 0;
  9075. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  9076. unsigned int vn = has_val ? ve - vs : 0;
  9077. if (vn == 0)
  9078. continue;
  9079. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  9080. kind = 2;
  9081. continue;
  9082. }
  9083. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  9084. (vn == 22 &&
  9085. ci_eq_n(s + vs, "application/javascript", 22)) ||
  9086. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  9087. (vn == 22 &&
  9088. ci_eq_n(s + vs, "application/ecmascript", 22)))
  9089. continue;
  9090. return 0;
  9091. }
  9092. }
  9093. return kind;
  9094. }
  9095. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  9096. {
  9097. unsigned short stack[64];
  9098. unsigned int sp = 0;
  9099. unsigned int i = 0;
  9100. int pre = 0;
  9101. unsigned int css_len = 0;
  9102. if (!d)
  9103. return -1;
  9104. if (!d->nodes)
  9105. br_doc_init(d);
  9106. if (!d->nodes || !d->text || !d->attrs)
  9107. return -1;
  9108. doc_reset(d);
  9109. d->script_src = html;
  9110. stack[0] = 0;
  9111. d->nodes[0].type = BR_NODE_ELEM;
  9112. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9113. while (i < len) {
  9114. if (html[i] != '<') {
  9115. unsigned int st = i;
  9116. while (i < len && html[i] != '<')
  9117. i++;
  9118. if (sp <= 63)
  9119. append_text(d, stack[sp], html + st, i - st, pre);
  9120. continue;
  9121. }
  9122. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  9123. html[i + 3] == '-') {
  9124. int p = find_ci(html, len, "-->", 3, i + 4);
  9125. i = p >= 0 ? (unsigned int)p + 3 : len;
  9126. continue;
  9127. }
  9128. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  9129. while (i < len && html[i] != '>')
  9130. i++;
  9131. if (i < len)
  9132. i++;
  9133. continue;
  9134. }
  9135. if (i + 1 < len && html[i + 1] == '/') {
  9136. unsigned int ns = i + 2, ne, tag, k, n;
  9137. char name[32];
  9138. while (ns < len && is_space(html[ns]))
  9139. ns++;
  9140. ne = ns;
  9141. while (ne < len && is_ident(html[ne]))
  9142. ne++;
  9143. if (ne == ns) {
  9144. i++;
  9145. continue;
  9146. }
  9147. n = ne - ns;
  9148. if (n >= sizeof(name))
  9149. n = sizeof(name) - 1;
  9150. for (k = 0; k < n; k++)
  9151. name[k] = html[ns + k];
  9152. name[n] = '\0';
  9153. tag = br_tag_from_name(name);
  9154. i = ne;
  9155. while (i < len && html[i] != '>')
  9156. i++;
  9157. if (i < len)
  9158. i++;
  9159. for (k = sp; k > 0; k--) {
  9160. struct br_node *nd = &d->nodes[stack[k]];
  9161. if (tag == BR_TAG_UNKNOWN) {
  9162. const char *raw = attr_ptr(d, nd->name_off);
  9163. if (nd->tag == BR_TAG_UNKNOWN &&
  9164. ci_eq_n(raw, name, n) &&
  9165. raw[n] == '\0') {
  9166. sp = k - 1;
  9167. break;
  9168. }
  9169. } else if (nd->tag == tag) {
  9170. sp = k - 1;
  9171. break;
  9172. }
  9173. }
  9174. stack_recount_pre(d, stack, sp, &pre);
  9175. continue;
  9176. }
  9177. /* start tag */
  9178. {
  9179. unsigned int ns = i + 1, ne, tag, k, n;
  9180. char name[16];
  9181. unsigned int attr_start, attr_end;
  9182. int selfclose = 0;
  9183. while (ns < len && is_space(html[ns]))
  9184. ns++;
  9185. ne = ns;
  9186. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  9187. ne++;
  9188. if (ne == ns) {
  9189. /* stray '<' */
  9190. append_text(d, stack[sp], html + i, 1, pre);
  9191. i++;
  9192. continue;
  9193. }
  9194. n = ne - ns;
  9195. if (n >= sizeof(name))
  9196. n = sizeof(name) - 1;
  9197. for (k = 0; k < n; k++)
  9198. name[k] = html[ns + k];
  9199. name[n] = '\0';
  9200. tag = br_tag_from_name(name);
  9201. attr_start = ne;
  9202. {
  9203. unsigned int q = ne;
  9204. char quote = 0;
  9205. while (q < len) {
  9206. char c = html[q];
  9207. if (quote) {
  9208. if (c == quote)
  9209. quote = 0;
  9210. } else if (c == '"' || c == '\'') {
  9211. quote = c;
  9212. } else if (c == '>') {
  9213. break;
  9214. }
  9215. q++;
  9216. }
  9217. attr_end = q;
  9218. /* `/>` closes the tag only when the slash is
  9219. * not the end of an unquoted attribute value:
  9220. * Docusaurus writes `<a href=/installation/>`,
  9221. * which used to self-close the link and leave
  9222. * its label outside it. */
  9223. if (q > attr_start && html[q - 1] == '/' &&
  9224. (q - 1 == attr_start ||
  9225. is_space(html[q - 2]) ||
  9226. html[q - 2] == '"' || html[q - 2] == '\''))
  9227. selfclose = 1;
  9228. i = q < len ? q + 1 : len;
  9229. }
  9230. if (tag == BR_TAG_SCRIPT) {
  9231. int p = find_ci(html, len, "</script", 8, i);
  9232. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9233. unsigned int idx;
  9234. struct br_script *script = 0;
  9235. int kind = script_is_javascript(html + attr_start,
  9236. attr_end - attr_start);
  9237. if (kind) {
  9238. if (d->nscripts < BR_MAX_SCRIPTS) {
  9239. script = &d->scripts[d->nscripts++];
  9240. script->module = kind == 2;
  9241. script->node = 0;
  9242. script->off = i;
  9243. script->len = end > i ? end - i : 0;
  9244. script->src_off = script_src_attr(
  9245. d, html + attr_start,
  9246. attr_end - attr_start);
  9247. } else {
  9248. d->scripts_truncated = 1;
  9249. }
  9250. }
  9251. /* Keep the element in the DOM even though its body is
  9252. * executed separately: scripts (Piwik, analytics)
  9253. * insert a sibling after getElementsByTagName('script')
  9254. * [0], and the element must exist for that to work. */
  9255. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9256. (unsigned char)tag);
  9257. if (idx)
  9258. parse_attrs(d, &d->nodes[idx],
  9259. html + attr_start,
  9260. attr_end - attr_start);
  9261. if (script)
  9262. script->node = idx;
  9263. if (idx && !kind && end > i)
  9264. append_raw_text(d, idx, html + i, end - i);
  9265. i = p >= 0 ? (unsigned int)p + 8 : len;
  9266. while (i < len && html[i] != '>')
  9267. i++;
  9268. if (i < len)
  9269. i++;
  9270. continue;
  9271. }
  9272. /* Scripting is always enabled, so <noscript> is a
  9273. * raw-text element (HTML "in head/in body, scripting
  9274. * flag set"): its markup must not reach the DOM or the
  9275. * cascade. google.com puts a meta refresh and
  9276. * `table,div,span,p{display:none}` in one, which hid
  9277. * the whole page and left only the "click here" link. */
  9278. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9279. int p = find_ci(html, len, "</noscript", 10, i);
  9280. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9281. unsigned int idx;
  9282. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9283. (unsigned char)tag);
  9284. if (idx) {
  9285. parse_attrs(d, &d->nodes[idx],
  9286. html + attr_start,
  9287. attr_end - attr_start);
  9288. if (end > i)
  9289. append_raw_text(d, idx, html + i,
  9290. end - i);
  9291. }
  9292. i = p >= 0 ? (unsigned int)p + 10 : len;
  9293. while (i < len && html[i] != '>')
  9294. i++;
  9295. if (i < len)
  9296. i++;
  9297. continue;
  9298. }
  9299. if (stack_has_svg(d, stack, sp) &&
  9300. (tag == BR_TAG_TITLE ||
  9301. (tag == BR_TAG_UNKNOWN &&
  9302. (ci_eq_n(name, "desc", 4) ||
  9303. ci_eq_n(name, "metadata", 8))))) {
  9304. i = skip_element(html, len, i, name, n);
  9305. continue;
  9306. }
  9307. /* A closed <details> is collapsed at style time, not
  9308. * parse time: the <summary> (the hamburger button on
  9309. * tagesschau and many mobile menus) must stay in the
  9310. * DOM. */
  9311. if (tag == BR_TAG_STYLE) {
  9312. int p = find_ci(html, len, "</style", 7, i);
  9313. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9314. unsigned int cend = end;
  9315. unsigned int idx;
  9316. while (cend > i && is_space(html[cend - 1]))
  9317. cend--;
  9318. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9319. for (k = i; k < cend; k++)
  9320. css_store[css_len++] = html[k];
  9321. css_store[css_len++] = '\n';
  9322. }
  9323. /* The CSS is collected above, but keep a display:none
  9324. * element so getElementsByTagName('style')/parentNode
  9325. * behave like a real document. */
  9326. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9327. (unsigned char)tag);
  9328. if (idx)
  9329. parse_attrs(d, &d->nodes[idx],
  9330. html + attr_start,
  9331. attr_end - attr_start);
  9332. i = p >= 0 ? (unsigned int)p + 7 : len;
  9333. while (i < len && html[i] != '>')
  9334. i++;
  9335. if (i < len)
  9336. i++;
  9337. continue;
  9338. }
  9339. if (tag == BR_TAG_TITLE) {
  9340. int p = find_ci(html, len, "</title", 7, i);
  9341. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9342. unsigned int tn = 0;
  9343. char tmp[BR_TITLE_MAX];
  9344. tn = decode_text(html + i, end - i, tmp,
  9345. sizeof(tmp), 1);
  9346. (void)tn;
  9347. s_copy(d->title, tmp, sizeof(d->title));
  9348. i = p >= 0 ? (unsigned int)p + 7 : len;
  9349. while (i < len && html[i] != '>')
  9350. i++;
  9351. if (i < len)
  9352. i++;
  9353. continue;
  9354. }
  9355. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9356. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9357. unsigned int idx;
  9358. implicit_close(d, stack, &sp, tag);
  9359. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9360. (unsigned char)tag);
  9361. if (idx)
  9362. parse_attrs(d, &d->nodes[idx],
  9363. html + attr_start,
  9364. attr_end - attr_start);
  9365. continue;
  9366. }
  9367. if (sp >= 63) {
  9368. d->truncated = 1;
  9369. continue;
  9370. }
  9371. implicit_close(d, stack, &sp, tag);
  9372. {
  9373. unsigned int idx = node_append(d, stack[sp],
  9374. BR_NODE_ELEM,
  9375. (unsigned char)tag);
  9376. if (!idx)
  9377. continue;
  9378. parse_attrs(d, &d->nodes[idx],
  9379. html + attr_start,
  9380. attr_end - attr_start);
  9381. if (tag == BR_TAG_UNKNOWN)
  9382. d->nodes[idx].name_off =
  9383. store_attr(d, name, n, 31);
  9384. /* HTML ignores the self-closing flag on a
  9385. * normal element (`<div/>` opens a div); only
  9386. * SVG content (and unknown/custom tags, kept
  9387. * as before) honours it. */
  9388. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9389. tag != BR_TAG_SVG &&
  9390. !stack_has_svg(d, stack, sp))
  9391. selfclose = 0;
  9392. if (!selfclose) {
  9393. sp++;
  9394. stack[sp] = (unsigned short)idx;
  9395. if (tag == BR_TAG_PRE)
  9396. pre++;
  9397. }
  9398. }
  9399. }
  9400. }
  9401. d->inline_css_len = css_len;
  9402. return 0;
  9403. }
  9404. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9405. {
  9406. if (!d->nodes)
  9407. br_doc_init(d);
  9408. if (!d->nodes || !d->text || !d->attrs)
  9409. return -1;
  9410. doc_reset(d);
  9411. d->plain = 1;
  9412. d->nodes[0].type = BR_NODE_ELEM;
  9413. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9414. append_text(d, 0, text, len, 1);
  9415. return 0;
  9416. }
  9417. /* A standalone image response: a synthetic document with one <img>. The
  9418. * caller decodes the already-fetched body into image slot 0 and sets the
  9419. * returned node's img_index. */
  9420. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9421. {
  9422. unsigned int idx;
  9423. if (!d->nodes)
  9424. br_doc_init(d);
  9425. if (!d->nodes || !d->text || !d->attrs)
  9426. return 0;
  9427. doc_reset(d);
  9428. d->nodes[0].type = BR_NODE_ELEM;
  9429. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9430. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9431. if (!idx)
  9432. return 0;
  9433. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9434. return idx;
  9435. }