browser_html.c 264 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400
  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_TX_PCT, PROP_TY_PCT, PROP_TR_MASK, PROP_TR_MS, PROP_TR_EASE,
  2297. PROP_PE_NONE,
  2298. PROP_COUNT
  2299. };
  2300. struct color_name {
  2301. const char *name;
  2302. unsigned short rgb;
  2303. };
  2304. static const struct color_name color_names[] = {
  2305. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2306. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2307. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2308. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2309. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2310. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2311. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2312. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2313. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2314. { "darkgrey", 0x528A }, { 0, 0 }
  2315. };
  2316. static int hexval(char c)
  2317. {
  2318. if (c >= '0' && c <= '9')
  2319. return c - '0';
  2320. if (c >= 'a' && c <= 'f')
  2321. return c - 'a' + 10;
  2322. if (c >= 'A' && c <= 'F')
  2323. return c - 'A' + 10;
  2324. return -1;
  2325. }
  2326. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2327. {
  2328. unsigned int i;
  2329. for (i = 0; color_names[i].name; i++) {
  2330. unsigned int j, n = s_len(color_names[i].name);
  2331. if (n != len)
  2332. continue;
  2333. for (j = 0; j < n; j++) {
  2334. char c = s[j];
  2335. if (c >= 'A' && c <= 'Z')
  2336. c = (char)(c - 'A' + 'a');
  2337. if (c != color_names[i].name[j])
  2338. break;
  2339. }
  2340. if (j == n) {
  2341. *out = color_names[i].rgb;
  2342. return 1;
  2343. }
  2344. }
  2345. return 0;
  2346. }
  2347. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2348. * over white; the render target is a white page. */
  2349. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2350. {
  2351. unsigned int i = 0;
  2352. while (i < len && is_space(s[i]))
  2353. i++;
  2354. if (i < len && s[i] == '#') {
  2355. int d[6], nd = 0;
  2356. i++;
  2357. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2358. d[nd++] = hexval(s[i++]);
  2359. if (nd == 3) {
  2360. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2361. unsigned int b = (unsigned int)d[2];
  2362. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2363. ((g * 17 >> 2) << 5) |
  2364. (b * 17 >> 3));
  2365. return 1;
  2366. }
  2367. if (nd >= 6) {
  2368. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2369. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2370. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2371. *out = (unsigned short)(((r >> 3) << 11) |
  2372. ((g >> 2) << 5) | (b >> 3));
  2373. return 1;
  2374. }
  2375. return 0;
  2376. }
  2377. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2378. unsigned int v[3], k = 0, a = 255;
  2379. while (i < len && s[i] != '(')
  2380. i++;
  2381. if (i >= len)
  2382. return 0;
  2383. i++;
  2384. while (k < 3 && i < len) {
  2385. unsigned int x = 0;
  2386. int percent = 0;
  2387. while (i < len && (is_space(s[i]) || s[i] == ','))
  2388. i++;
  2389. if (i >= len || s[i] < '0' || s[i] > '9')
  2390. return 0;
  2391. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2392. x = x * 10 + (unsigned int)(s[i] - '0');
  2393. i++;
  2394. }
  2395. if (i < len && s[i] == '%') {
  2396. percent = 1;
  2397. i++;
  2398. }
  2399. if (percent)
  2400. x = x * 255 / 100;
  2401. if (x > 255)
  2402. x = 255;
  2403. v[k++] = x;
  2404. }
  2405. /* Optional alpha in both syntaxes:
  2406. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2407. {
  2408. unsigned int whole = 0, frac = 0, scale = 1;
  2409. int percent = 0;
  2410. while (i < len && (is_space(s[i]) || s[i] == ','))
  2411. i++;
  2412. if (i < len && s[i] == '/') {
  2413. i++;
  2414. while (i < len && is_space(s[i]))
  2415. i++;
  2416. }
  2417. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2418. * used to read as opaque black. */
  2419. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2420. (s[i] == '.' && i + 1 < len &&
  2421. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2422. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2423. whole = whole * 10 +
  2424. (unsigned int)(s[i] - '0');
  2425. i++;
  2426. }
  2427. if (i < len && s[i] == '.') {
  2428. i++;
  2429. while (i < len && s[i] >= '0' &&
  2430. s[i] <= '9') {
  2431. if (scale < 1000) {
  2432. frac = frac * 10 +
  2433. (unsigned int)(s[i] - '0');
  2434. scale *= 10;
  2435. }
  2436. i++;
  2437. }
  2438. }
  2439. while (i < len && is_space(s[i]))
  2440. i++;
  2441. if (i < len && s[i] == '%')
  2442. percent = 1;
  2443. if (percent)
  2444. a = whole > 100 ? 255 :
  2445. whole * 255 / 100;
  2446. else if (whole >= 1)
  2447. a = 255;
  2448. else
  2449. a = frac * 255 / scale;
  2450. }
  2451. }
  2452. if (k == 3) {
  2453. unsigned int r = v[0], g = v[1], b = v[2];
  2454. if (a < 255) {
  2455. r = (r * a + 255u * (255u - a)) / 255u;
  2456. g = (g * a + 255u * (255u - a)) / 255u;
  2457. b = (b * a + 255u * (255u - a)) / 255u;
  2458. }
  2459. *out = (unsigned short)(((r >> 3) << 11) |
  2460. ((g >> 2) << 5) | (b >> 3));
  2461. return 1;
  2462. }
  2463. return 0;
  2464. }
  2465. {
  2466. unsigned int j = i;
  2467. while (i < len && !is_space(s[i]))
  2468. i++;
  2469. return named_color(s + j, i - j, out);
  2470. }
  2471. }
  2472. /* Fully transparent colours have no RGB565 representation. They are common
  2473. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2474. * only on hover; painting them black would draw an outline around every
  2475. * pill and card. */
  2476. static int color_is_transparent(const char *s, unsigned int len)
  2477. {
  2478. unsigned int i = 0, n = 0;
  2479. int last = 0, prev = 0, d;
  2480. while (i < len && is_space(s[i]))
  2481. i++;
  2482. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2483. return 1;
  2484. if (i >= len || s[i] != '#')
  2485. return 0;
  2486. i++;
  2487. while (i < len && hexval(s[i]) >= 0) {
  2488. d = hexval(s[i]);
  2489. prev = last;
  2490. last = d;
  2491. n++;
  2492. i++;
  2493. }
  2494. while (i < len && is_space(s[i]))
  2495. i++;
  2496. if (i != len)
  2497. return 0;
  2498. if (n == 4)
  2499. return last == 0;
  2500. if (n == 8)
  2501. return prev == 0 && last == 0;
  2502. return 0;
  2503. }
  2504. static int register_bg_url(const char *s, unsigned int len)
  2505. {
  2506. char tmp[BR_BG_URL_MAX + 1];
  2507. unsigned int i, n = 0;
  2508. char q = 0;
  2509. while (len && is_space(*s)) {
  2510. s++;
  2511. len--;
  2512. }
  2513. while (len && is_space(s[len - 1]))
  2514. len--;
  2515. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2516. s[len - 1] == s[0]) {
  2517. q = s[0];
  2518. s++;
  2519. len -= 2;
  2520. }
  2521. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2522. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2523. if (q && s[i] == '\\' && i + 1 < len) {
  2524. i++;
  2525. if (s[i] == '\n')
  2526. continue;
  2527. }
  2528. tmp[n++] = s[i];
  2529. }
  2530. tmp[n] = '\0';
  2531. if (!n)
  2532. return 0;
  2533. for (i = 0; i < bg_url_count; i++)
  2534. if (!strcmp(bg_urls[i], tmp))
  2535. return (int)i + 1;
  2536. if (bg_url_count >= BR_BG_URLS ||
  2537. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2538. return 0;
  2539. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2540. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2541. bg_url_pool_used += n + 1;
  2542. return (int)(++bg_url_count);
  2543. }
  2544. /* Length values for margins/padding: px, ignore percentages/units. */
  2545. static unsigned int viewport_w;
  2546. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2547. {
  2548. unsigned int i = *pos;
  2549. int sign = 1;
  2550. long v = 0; /* hundredths of a unit before conversion */
  2551. int digits = 0, frac = 0, unit = 0;
  2552. while (i < len && is_space(s[i]))
  2553. i++;
  2554. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2555. if (s[i] == '-')
  2556. sign = -1;
  2557. i++;
  2558. }
  2559. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2560. v = v * 10 + (s[i] - '0');
  2561. digits = 1;
  2562. i++;
  2563. }
  2564. if (i < len && s[i] == '.') {
  2565. i++;
  2566. /* Minified CSS writes ".5rem" without a leading zero. */
  2567. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2568. if (frac < 2) {
  2569. v = v * 10 + (s[i] - '0');
  2570. frac++;
  2571. digits = 1;
  2572. }
  2573. i++;
  2574. }
  2575. }
  2576. while (frac < 2) {
  2577. v *= 10;
  2578. frac++;
  2579. }
  2580. while (frac > 2) {
  2581. v /= 10;
  2582. frac--;
  2583. }
  2584. if (i < len && s[i] == '%')
  2585. unit = 2;
  2586. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2587. s[i + 2] == 'm')
  2588. unit = 1;
  2589. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2590. unit = 3;
  2591. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2592. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2593. s[i] == 'a' || s[i] == 'd'))
  2594. i++;
  2595. *pos = i;
  2596. if (!digits)
  2597. return 0;
  2598. if (unit == 1)
  2599. v *= root_font_px;
  2600. else if (unit == 3)
  2601. v *= root_font_px;
  2602. else if (unit == 2)
  2603. v = v * (long)viewport_w / 100;
  2604. v /= 100;
  2605. v *= sign;
  2606. if (v < -2048)
  2607. v = -2048;
  2608. if (v > 2048)
  2609. v = 2048;
  2610. return (int)v;
  2611. }
  2612. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2613. * treats it as a zero-length token; margins need to keep it so block layout
  2614. * can centre a box with a definite width. */
  2615. #define CSS_LEN_AUTO (-32768)
  2616. /* A box length with its unit preserved for layout-time resolution:
  2617. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2618. * rem and whole pixels for px. */
  2619. static int parse_css_len(const char *s, unsigned int len,
  2620. unsigned int *pos, int *unit)
  2621. {
  2622. unsigned int i = *pos;
  2623. int sign = 1;
  2624. long v = 0;
  2625. int digits = 0, frac = 0;
  2626. *unit = 0;
  2627. while (i < len && is_space(s[i]))
  2628. i++;
  2629. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2630. if (s[i] == '-')
  2631. sign = -1;
  2632. i++;
  2633. }
  2634. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2635. v = v * 10 + (s[i] - '0');
  2636. digits = 1;
  2637. i++;
  2638. }
  2639. if (i < len && s[i] == '.') {
  2640. i++;
  2641. /* Leading-dot fractions are common in minified CSS
  2642. * (".5rem", ".3rem"). */
  2643. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2644. if (frac < 2) {
  2645. v = v * 10 + (s[i] - '0');
  2646. frac++;
  2647. digits = 1;
  2648. }
  2649. i++;
  2650. }
  2651. }
  2652. while (frac < 2) {
  2653. v *= 10;
  2654. frac++;
  2655. }
  2656. if (!digits) {
  2657. *pos = i;
  2658. return 0;
  2659. }
  2660. if (i < len && s[i] == '%') {
  2661. *unit = 1;
  2662. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2663. s[i + 2] == 'm') {
  2664. *unit = 3;
  2665. i += 3;
  2666. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2667. *unit = 2;
  2668. i += 2;
  2669. } else {
  2670. v /= 100;
  2671. }
  2672. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2673. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2674. s[i] == 'a' || s[i] == 'd'))
  2675. i++;
  2676. *pos = i;
  2677. v *= sign;
  2678. if (v < -32768)
  2679. v = -32768;
  2680. if (v > 32767)
  2681. v = 32767;
  2682. return (int)v;
  2683. }
  2684. static int parse_margin_len(const char *s, unsigned int len,
  2685. unsigned int *pos, int *unit)
  2686. {
  2687. unsigned int i = *pos;
  2688. while (i < len && is_space(s[i]))
  2689. i++;
  2690. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2691. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2692. *pos = i + 4;
  2693. *unit = 4;
  2694. return CSS_LEN_AUTO;
  2695. }
  2696. return parse_css_len(s, len, pos, unit);
  2697. }
  2698. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2699. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2700. static int parse_margin_calc(const char *s, unsigned int len,
  2701. unsigned int *pos, int *pct, int *px)
  2702. {
  2703. unsigned int i = *pos, j;
  2704. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2705. return 0;
  2706. i += 5;
  2707. j = i;
  2708. while (j < len && s[j] != ')')
  2709. j++;
  2710. if (j >= len)
  2711. return 0;
  2712. if (!parse_calc(s + i, j - i, pct, px))
  2713. return 0;
  2714. *pos = j + 1;
  2715. return 1;
  2716. }
  2717. /* calc(P% +/- Npx) for the common width/margin forms. */
  2718. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2719. {
  2720. unsigned int i = 0;
  2721. int sign = 1;
  2722. int have = 0;
  2723. *pct = 0;
  2724. *px = 0;
  2725. while (i < len) {
  2726. unsigned int j;
  2727. long whole = 0, frac = 0, scale = 1;
  2728. int digits = 0;
  2729. while (i < len && is_space(s[i]))
  2730. i++;
  2731. if (i >= len)
  2732. break;
  2733. if (s[i] == '+' || s[i] == '-') {
  2734. sign = s[i] == '-' ? -1 : 1;
  2735. i++;
  2736. continue;
  2737. }
  2738. if (s[i] < '0' || s[i] > '9') {
  2739. if (s[i] == '.') {
  2740. /* fall through to the number parser */
  2741. } else {
  2742. i++;
  2743. continue;
  2744. }
  2745. }
  2746. j = i;
  2747. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2748. whole = whole * 10 + (s[j] - '0');
  2749. digits = 1;
  2750. j++;
  2751. }
  2752. if (j < len && s[j] == '.') {
  2753. j++;
  2754. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2755. frac = frac * 10 + (s[j] - '0');
  2756. scale *= 10;
  2757. j++;
  2758. }
  2759. }
  2760. if (!digits) {
  2761. i = j + 1;
  2762. continue;
  2763. }
  2764. while (j < len && is_space(s[j]))
  2765. j++;
  2766. if (j < len && s[j] == '%') {
  2767. *pct = sign * (int)(whole + (scale > 1 ?
  2768. frac * 100 / scale : 0));
  2769. have = 1;
  2770. j++;
  2771. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2772. *px = sign * (int)whole;
  2773. have = 1;
  2774. j += 2;
  2775. }
  2776. i = j;
  2777. while (i < len && !is_space(s[i]))
  2778. i++;
  2779. sign = 1;
  2780. }
  2781. return have;
  2782. }
  2783. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2784. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2785. unsigned char *kind)
  2786. {
  2787. unsigned int i = 0;
  2788. unsigned long v = 0;
  2789. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2790. *val = 0;
  2791. *kind = 0;
  2792. while (i < len && is_space(s[i]))
  2793. i++;
  2794. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2795. return 0;
  2796. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2797. return 0;
  2798. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2799. v = v * 10 + (unsigned int)(s[i] - '0');
  2800. digits = 1;
  2801. i++;
  2802. }
  2803. if (i < len && s[i] == '.') {
  2804. i++;
  2805. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2806. if (frac < 2) {
  2807. v = v * 10 + (unsigned int)(s[i] - '0');
  2808. frac++;
  2809. digits = 1;
  2810. }
  2811. i++;
  2812. }
  2813. }
  2814. while (frac < 2) {
  2815. v *= 10;
  2816. frac++;
  2817. }
  2818. while (frac > 2) {
  2819. v /= 10;
  2820. frac--;
  2821. }
  2822. if (!digits)
  2823. return 0;
  2824. while (i < len && is_space(s[i]))
  2825. i++;
  2826. if (i < len && s[i] == '%')
  2827. pct = 1;
  2828. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2829. s[i + 2] == 'm')
  2830. rem = 1;
  2831. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2832. rem = 2;
  2833. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2834. vw = 1;
  2835. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2836. ; /* px: already the default scale */
  2837. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2838. bad_unit = 1;
  2839. if (bad_unit)
  2840. return 0;
  2841. if (rem == 1 || rem == 2) {
  2842. if (v > 65535)
  2843. v = 65535;
  2844. *val = (unsigned short)v;
  2845. *kind = rem == 1 ? 5 : 4;
  2846. return 1;
  2847. }
  2848. v /= 100;
  2849. if (vw)
  2850. v = v * viewport_w / 100;
  2851. if (pct) {
  2852. if (v > 100)
  2853. v = 100;
  2854. *kind = 2;
  2855. } else {
  2856. if (v > 2048)
  2857. v = 2048;
  2858. *kind = 1;
  2859. }
  2860. *val = (unsigned short)v;
  2861. return 1;
  2862. }
  2863. static int parse_font_px(const char *s, unsigned int len)
  2864. {
  2865. unsigned int i = 0;
  2866. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2867. int v1000, px;
  2868. int is_px = 1, is_rem = 0, is_pct = 0;
  2869. while (i < len && is_space(s[i]))
  2870. i++;
  2871. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2872. return 24;
  2873. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2874. return 32;
  2875. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2876. return 14;
  2877. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2878. whole = whole * 10 + (s[i] - '0');
  2879. digits = 1;
  2880. i++;
  2881. }
  2882. if (i < len && s[i] == '.') {
  2883. i++;
  2884. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2885. if (fscale < 1000) {
  2886. frac = frac * 10 + (s[i] - '0');
  2887. fscale *= 10;
  2888. }
  2889. digits = 1;
  2890. i++;
  2891. }
  2892. }
  2893. if (!digits)
  2894. return 0;
  2895. while (i < len && is_space(s[i]))
  2896. i++;
  2897. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2898. is_rem = 1;
  2899. is_px = 0;
  2900. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2901. is_rem = 1;
  2902. is_px = 0;
  2903. } else if (i < len && s[i] == '%') {
  2904. is_pct = 1;
  2905. is_px = 0;
  2906. }
  2907. v1000 = whole * 1000 + frac * 1000 / fscale;
  2908. if (is_rem)
  2909. px = v1000 * root_font_px / 1000;
  2910. else if (is_pct)
  2911. px = v1000 * root_font_px / 100000;
  2912. else if (is_px)
  2913. px = v1000 / 1000;
  2914. else
  2915. px = v1000 / 1000;
  2916. if (px < 8)
  2917. px = 8;
  2918. if (px > 72)
  2919. px = 72;
  2920. return px;
  2921. }
  2922. /* font-size keeping the unit so em/% resolve against the parent face. */
  2923. static int parse_font_size(const char *s, unsigned int len,
  2924. unsigned short *raw, unsigned char *unit)
  2925. {
  2926. unsigned int i = 0;
  2927. int whole = 0, frac = 0, scale = 1, digits = 0;
  2928. int v100;
  2929. *raw = 0;
  2930. *unit = 0;
  2931. while (i < len && is_space(s[i]))
  2932. i++;
  2933. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2934. *raw = 24;
  2935. *unit = 1;
  2936. return 1;
  2937. }
  2938. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2939. *raw = 32;
  2940. *unit = 1;
  2941. return 1;
  2942. }
  2943. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2944. *raw = 14;
  2945. *unit = 1;
  2946. return 1;
  2947. }
  2948. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2949. whole = whole * 10 + (s[i] - '0');
  2950. digits = 1;
  2951. i++;
  2952. }
  2953. if (i < len && s[i] == '.') {
  2954. i++;
  2955. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2956. frac = frac * 10 + (s[i] - '0');
  2957. scale *= 10;
  2958. i++;
  2959. }
  2960. }
  2961. if (!digits && !frac)
  2962. return 0;
  2963. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2964. while (i < len && is_space(s[i]))
  2965. i++;
  2966. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2967. *raw = (unsigned short)v100;
  2968. *unit = 3;
  2969. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2970. *raw = (unsigned short)v100;
  2971. *unit = 2;
  2972. } else if (i + 2 <= len && ci_eq_n(s + i, "pt", 2)) {
  2973. /* 1pt = 4/3 px: Hacker News sets 10pt/7pt, which used to be
  2974. * read as 10/7 px. */
  2975. *raw = (unsigned short)((v100 * 4 / 3 + 50) / 100);
  2976. *unit = 1;
  2977. } else if (i < len && s[i] == '%') {
  2978. /* Keep hundredths: 62.5% is a common root size and truncating
  2979. * it to 62% makes every rem 1px too small. */
  2980. *raw = (unsigned short)v100;
  2981. *unit = 4;
  2982. } else {
  2983. *raw = (unsigned short)(v100 / 100);
  2984. *unit = 1;
  2985. }
  2986. return 1;
  2987. }
  2988. static int parse_line_height(const char *s, unsigned int len,
  2989. unsigned short *px, unsigned char *pct)
  2990. {
  2991. unsigned int i = 0;
  2992. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2993. int v1000;
  2994. int is_px = 0, is_pct = 0;
  2995. *px = 0;
  2996. *pct = 0;
  2997. while (i < len && is_space(s[i]))
  2998. i++;
  2999. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  3000. return 0;
  3001. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3002. whole = whole * 10 + (s[i] - '0');
  3003. digits = 1;
  3004. i++;
  3005. }
  3006. if (i < len && s[i] == '.') {
  3007. i++;
  3008. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  3009. frac = frac * 10 + (s[i] - '0');
  3010. fscale *= 10;
  3011. digits = 1;
  3012. i++;
  3013. }
  3014. }
  3015. if (!digits)
  3016. return 0;
  3017. while (i < len && is_space(s[i]))
  3018. i++;
  3019. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3020. is_px = 1;
  3021. else if (i < len && s[i] == '%')
  3022. is_pct = 1;
  3023. v1000 = whole * 1000 + frac * 1000 / fscale;
  3024. /* rem is an absolute length (the root font is known by the time
  3025. * rules are parsed, as for font-size); em behaves like a multiplier. */
  3026. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3027. v1000 = v1000 * root_font_px;
  3028. is_px = 1;
  3029. }
  3030. if (is_px) {
  3031. int v = v1000 / 1000;
  3032. if (v < 8)
  3033. v = 8;
  3034. if (v > 160)
  3035. v = 160;
  3036. *px = (unsigned short)v;
  3037. } else {
  3038. int v = v1000 / 10; /* unitless multiplier -> percent */
  3039. if (is_pct)
  3040. v = v1000 / 1000;
  3041. if (v < 50)
  3042. v = 50;
  3043. if (v > 255) /* stored in an unsigned char */
  3044. v = 255;
  3045. *pct = (unsigned char)v;
  3046. }
  3047. return 1;
  3048. }
  3049. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  3050. {
  3051. unsigned int i, j, n = s_len(needle);
  3052. for (i = 0; i + n <= len; i++) {
  3053. for (j = 0; j < n; j++) {
  3054. char a = s[i + j];
  3055. if (a >= 'A' && a <= 'Z')
  3056. a = (char)(a - 'A' + 'a');
  3057. if (a != needle[j])
  3058. break;
  3059. }
  3060. if (j == n)
  3061. return 1;
  3062. }
  3063. return 0;
  3064. }
  3065. /* Pull a colour out of a border/background shorthand. */
  3066. /* Background shorthands can hold several layers before the final solid
  3067. * colour. The colour is conventionally the last token (e.g. a stack of
  3068. * gradients followed by var(--bg)), so take the rightmost colour rather
  3069. * than the first gradient stop. */
  3070. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3071. {
  3072. int last = -1;
  3073. unsigned int i;
  3074. for (i = 0; i < len; i++) {
  3075. if (s[i] == '#')
  3076. last = (int)i;
  3077. else if ((s[i] == 'r' || s[i] == 'R') &&
  3078. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3079. last = (int)i;
  3080. }
  3081. if (last >= 0)
  3082. return parse_color(s + last, len - (unsigned int)last, c);
  3083. while (i > 0) {
  3084. unsigned int st, e;
  3085. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3086. s[i - 1] == ')' || s[i - 1] == '('))
  3087. i--;
  3088. e = i;
  3089. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3090. s[i - 1] != ')' && s[i - 1] != '(')
  3091. i--;
  3092. st = i;
  3093. if (e > st && parse_color(s + st, e - st, c))
  3094. return 1;
  3095. }
  3096. return 0;
  3097. }
  3098. static int parse_display(const char *s, unsigned int len)
  3099. {
  3100. unsigned int i;
  3101. while (len && is_space(s[0])) { s++; len--; }
  3102. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3103. ;
  3104. if (i == 4 && ci_eq_n(s, "none", 4))
  3105. return BR_DISPLAY_NONE;
  3106. if (i == 5 && ci_eq_n(s, "block", 5))
  3107. return BR_DISPLAY_BLOCK;
  3108. if (i == 6 && ci_eq_n(s, "inline", 6))
  3109. return BR_DISPLAY_INLINE;
  3110. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3111. return BR_DISPLAY_INLINE_BLOCK;
  3112. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3113. return BR_DISPLAY_BLOCK;
  3114. if (i == 4 && ci_eq_n(s, "flex", 4))
  3115. return BR_DISPLAY_FLEX;
  3116. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3117. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3118. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3119. * block instead of an inline box. */
  3120. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3121. return BR_DISPLAY_BLOCK;
  3122. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3123. return BR_DISPLAY_BLOCK;
  3124. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3125. return BR_DISPLAY_FLEX;
  3126. if (i == 5 && ci_eq_n(s, "table", 5))
  3127. return BR_DISPLAY_BLOCK;
  3128. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3129. return BR_DISPLAY_BLOCK;
  3130. /* Grid shells are common on news/wiki pages, but the two-column
  3131. * heuristic is not reliable for their nested area templates yet.
  3132. * Collapse them to block flow so the content stays readable. */
  3133. if (i == 4 && ci_eq_n(s, "grid", 4))
  3134. return BR_DISPLAY_BLOCK;
  3135. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3136. return BR_DISPLAY_BLOCK;
  3137. return BR_DISPLAY_INLINE;
  3138. }
  3139. static void decl_add(struct br_decl *out, unsigned int *nout,
  3140. unsigned char prop, int val, int important)
  3141. {
  3142. if (*nout >= BR_DECLS)
  3143. return;
  3144. out[*nout].prop = prop;
  3145. out[*nout].val = (short)val;
  3146. out[*nout].important = (unsigned char)(important != 0);
  3147. (*nout)++;
  3148. }
  3149. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3150. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3151. unsigned int *start, unsigned int *end)
  3152. {
  3153. unsigned int i = *pos;
  3154. while (i < len && is_space(s[i]))
  3155. i++;
  3156. if (i >= len)
  3157. return 0;
  3158. *start = i;
  3159. while (i < len && !is_space(s[i]))
  3160. i++;
  3161. *end = i;
  3162. *pos = i;
  3163. return 1;
  3164. }
  3165. static int bg_number(const char *s, unsigned int len, signed short *val,
  3166. unsigned char *kind)
  3167. {
  3168. unsigned int i = 0;
  3169. long v = 0;
  3170. int neg = 0, digits = 0, pct = 0;
  3171. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3172. neg = s[i] == '-';
  3173. i++;
  3174. }
  3175. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3176. v = v * 10 + (s[i] - '0');
  3177. digits = 1;
  3178. i++;
  3179. }
  3180. if (i < len && s[i] == '.') {
  3181. i++;
  3182. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3183. digits = 1;
  3184. i++;
  3185. }
  3186. }
  3187. if (!digits)
  3188. return 0;
  3189. if (i < len && s[i] == '%') {
  3190. pct = 1;
  3191. i++;
  3192. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3193. i += 2;
  3194. }
  3195. if (i != len)
  3196. return 0;
  3197. if (neg)
  3198. v = -v;
  3199. if (v < -4096)
  3200. v = -4096;
  3201. if (v > 4096)
  3202. v = 4096;
  3203. *val = (signed short)v;
  3204. *kind = (unsigned char)(pct ? 1 : 0);
  3205. return 1;
  3206. }
  3207. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3208. int *vertical)
  3209. {
  3210. *vertical = 0;
  3211. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3212. *val = 0;
  3213. return 1;
  3214. }
  3215. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3216. *val = 100;
  3217. return 1;
  3218. }
  3219. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3220. *val = 0;
  3221. *vertical = 1;
  3222. return 1;
  3223. }
  3224. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3225. *val = 100;
  3226. *vertical = 1;
  3227. return 1;
  3228. }
  3229. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3230. *val = 50;
  3231. return 1;
  3232. }
  3233. return 0;
  3234. }
  3235. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3236. const char *s, unsigned int len, int important)
  3237. {
  3238. unsigned int pos = 0, ts, te;
  3239. signed short val[2] = { 50, 50 };
  3240. unsigned char kind[2] = { 1, 1 };
  3241. int vert[2] = { 0, 0 }, n = 0;
  3242. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3243. signed short v;
  3244. int is_vert;
  3245. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3246. val[n] = v;
  3247. kind[n] = 1;
  3248. vert[n] = is_vert;
  3249. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3250. val[n] = v;
  3251. vert[n] = 0;
  3252. } else {
  3253. n++;
  3254. continue;
  3255. }
  3256. n++;
  3257. }
  3258. if (n == 1) {
  3259. if (vert[0]) {
  3260. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3261. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3262. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3263. important);
  3264. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3265. } else {
  3266. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3267. important);
  3268. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3269. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3270. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3271. }
  3272. return;
  3273. }
  3274. if (n >= 2) {
  3275. int x = vert[0] && !vert[1] ? 1 : 0;
  3276. int y = 1 - x;
  3277. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3278. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3279. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3280. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3281. }
  3282. }
  3283. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3284. const char *s, unsigned int len, int important)
  3285. {
  3286. unsigned int pos = 0, ts, te;
  3287. signed short w = 0, h = 0;
  3288. unsigned char kw = 0, kh = 0;
  3289. int n = 0;
  3290. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3291. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3292. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3293. return;
  3294. }
  3295. if (n == 0 && te - ts == 7 &&
  3296. ci_eq_n(s + ts, "contain", 7)) {
  3297. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3298. return;
  3299. }
  3300. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3301. n++;
  3302. continue;
  3303. }
  3304. {
  3305. signed short v;
  3306. unsigned char k;
  3307. if (bg_number(s + ts, te - ts, &v, &k)) {
  3308. if (n == 0) {
  3309. w = v;
  3310. kw = k;
  3311. } else {
  3312. h = v;
  3313. kh = k;
  3314. }
  3315. }
  3316. }
  3317. n++;
  3318. }
  3319. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3320. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3321. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3322. (void)kh;
  3323. }
  3324. /* Parse the `position / size` part of the background shorthand. The image
  3325. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3326. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3327. const char *v, unsigned int len,
  3328. int important)
  3329. {
  3330. unsigned int i, slash = 0, depth = 0, p, first, count;
  3331. char q = 0;
  3332. signed short nv;
  3333. int vert;
  3334. unsigned char kind;
  3335. for (i = 0; i < len; i++) {
  3336. char c = v[i];
  3337. if (q) {
  3338. if (c == q)
  3339. q = 0;
  3340. continue;
  3341. }
  3342. if (c == '\'' || c == '"') {
  3343. q = c;
  3344. continue;
  3345. }
  3346. if (c == '(') {
  3347. depth++;
  3348. continue;
  3349. }
  3350. if (c == ')') {
  3351. if (depth)
  3352. depth--;
  3353. continue;
  3354. }
  3355. if (c == '/' && !depth) {
  3356. slash = i;
  3357. break;
  3358. }
  3359. }
  3360. if (!slash)
  3361. return;
  3362. /* Up to two position tokens immediately before the slash. */
  3363. p = slash;
  3364. first = slash;
  3365. count = 0;
  3366. while (count < 2) {
  3367. unsigned int e;
  3368. while (p > 0 && is_space(v[p - 1]))
  3369. p--;
  3370. e = p;
  3371. while (p > 0 && !is_space(v[p - 1]))
  3372. p--;
  3373. if (e == p)
  3374. break;
  3375. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3376. bg_number(v + p, e - p, &nv, &kind)) {
  3377. first = p;
  3378. count++;
  3379. } else {
  3380. break;
  3381. }
  3382. }
  3383. if (count)
  3384. add_bg_position(out, nout, v + first, slash - first,
  3385. important);
  3386. /* Up to two size tokens after the slash. */
  3387. p = slash + 1;
  3388. while (p < len && is_space(v[p]))
  3389. p++;
  3390. first = p;
  3391. count = 0;
  3392. while (count < 2) {
  3393. unsigned int ts = p;
  3394. while (p < len && !is_space(v[p]))
  3395. p++;
  3396. if (p == ts)
  3397. break;
  3398. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3399. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3400. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3401. bg_number(v + ts, p - ts, &nv, &kind)) {
  3402. count++;
  3403. while (p < len && is_space(v[p]))
  3404. p++;
  3405. } else {
  3406. break;
  3407. }
  3408. }
  3409. if (count)
  3410. add_bg_size(out, nout, v + first, p - first, important);
  3411. }
  3412. static int parse_fixed_100(const char *s, unsigned int len,
  3413. unsigned int *pos, signed short *out)
  3414. {
  3415. unsigned int i = *pos;
  3416. long whole = 0, frac = 0;
  3417. int neg = 0, digits = 0, fd = 0;
  3418. while (i < len && is_space(s[i]))
  3419. i++;
  3420. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3421. neg = s[i] == '-';
  3422. i++;
  3423. }
  3424. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3425. whole = whole * 10 + (s[i] - '0');
  3426. if (whole > 327)
  3427. whole = 327;
  3428. digits = 1;
  3429. i++;
  3430. }
  3431. if (i < len && s[i] == '.') {
  3432. i++;
  3433. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3434. if (fd < 2) {
  3435. frac = frac * 10 + (s[i] - '0');
  3436. fd++;
  3437. digits = 1;
  3438. }
  3439. i++;
  3440. }
  3441. }
  3442. if (!digits)
  3443. return 0;
  3444. while (fd++ < 2)
  3445. frac *= 10;
  3446. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3447. *pos = i;
  3448. return 1;
  3449. }
  3450. /* One translate() argument: px (or rem/em/vw through parse_len), or a
  3451. * percentage of the element's own box, which only layout can resolve. */
  3452. static int translate_arg(const char *v, unsigned int vl, unsigned int *off,
  3453. int *pct)
  3454. {
  3455. unsigned int q = *off;
  3456. signed short h100;
  3457. *pct = 0;
  3458. while (q < vl && is_space(v[q]))
  3459. q++;
  3460. if (parse_fixed_100(v, vl, &q, &h100) && q < vl && v[q] == '%') {
  3461. *off = q + 1;
  3462. *pct = 1;
  3463. return h100 / 100;
  3464. }
  3465. return parse_len(v, vl, off);
  3466. }
  3467. /* "0.3s" / ".15s" / "300ms" -> milliseconds; -1 when not a time. */
  3468. static int parse_time_ms(const char *s, unsigned int len, unsigned int *pos)
  3469. {
  3470. unsigned int i = *pos;
  3471. long v = 0, scale = 1000;
  3472. int digits = 0;
  3473. while (i < len && is_space(s[i]))
  3474. i++;
  3475. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3476. v = v * 10 + (s[i] - '0');
  3477. digits = 1;
  3478. i++;
  3479. }
  3480. if (i < len && s[i] == '.') {
  3481. i++;
  3482. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3483. if (scale > 1) {
  3484. v = v * 10 + (s[i] - '0');
  3485. scale /= 10;
  3486. }
  3487. digits = 1;
  3488. i++;
  3489. }
  3490. }
  3491. if (!digits)
  3492. return -1;
  3493. if (i + 1 < len && s[i] == 'm' && s[i + 1] == 's') {
  3494. *pos = i + 2;
  3495. v = v * scale / 1000;
  3496. } else if (i < len && s[i] == 's') {
  3497. *pos = i + 1;
  3498. v = v * scale;
  3499. } else {
  3500. return -1;
  3501. }
  3502. if (v > 10000)
  3503. v = 10000;
  3504. return (int)v;
  3505. }
  3506. static int ease_code(const char *s, unsigned int n)
  3507. {
  3508. if (contains_ci_len(s, n, "ease-in-out"))
  3509. return BR_EASE_IN_OUT;
  3510. if (contains_ci_len(s, n, "ease-out"))
  3511. return BR_EASE_OUT;
  3512. if (contains_ci_len(s, n, "ease-in"))
  3513. return BR_EASE_IN;
  3514. if (contains_ci_len(s, n, "linear"))
  3515. return BR_EASE_LINEAR;
  3516. if (contains_ci_len(s, n, "cubic-bezier"))
  3517. return BR_EASE_OUT;
  3518. return BR_EASE_EASE;
  3519. }
  3520. static int tr_prop_mask(const char *s, unsigned int n)
  3521. {
  3522. int m = 0;
  3523. if (contains_ci_len(s, n, "all"))
  3524. m |= BR_TR_OPACITY | BR_TR_TRANSFORM;
  3525. if (contains_ci_len(s, n, "opacity"))
  3526. m |= BR_TR_OPACITY;
  3527. if (contains_ci_len(s, n, "transform"))
  3528. m |= BR_TR_TRANSFORM;
  3529. return m;
  3530. }
  3531. /* `transition` (shorthand, comma list) and its longhands, reduced to the
  3532. * set of animated properties the engine supports (opacity, transform),
  3533. * one duration (the longest listed) and the first easing. A shorthand
  3534. * item without a property name means `all`. */
  3535. static void add_transition(struct br_decl *out, unsigned int *nout,
  3536. const char *prop,
  3537. const char *v, unsigned int vl, int important)
  3538. {
  3539. unsigned int i = 0;
  3540. int mask = 0, ms = 0, ease = -1;
  3541. if (str_eq(prop, "transition-property")) {
  3542. decl_add(out, nout, PROP_TR_MASK, tr_prop_mask(v, vl),
  3543. important);
  3544. return;
  3545. }
  3546. if (str_eq(prop, "transition-timing-function")) {
  3547. decl_add(out, nout, PROP_TR_EASE, ease_code(v, vl), important);
  3548. return;
  3549. }
  3550. while (i < vl) {
  3551. unsigned int a = i, e, q;
  3552. int t, item_ms = -1;
  3553. while (i < vl && v[i] != ',') {
  3554. if (v[i] == '(')
  3555. while (i < vl && v[i] != ')')
  3556. i++;
  3557. i++;
  3558. }
  3559. e = i < vl ? i : vl;
  3560. for (q = a; q < e; q++) {
  3561. unsigned int p = q;
  3562. if ((q == a || is_space(v[q - 1])) &&
  3563. (t = parse_time_ms(v, e, &p)) >= 0) {
  3564. if (item_ms < 0)
  3565. item_ms = t; /* the second time is the delay */
  3566. q = p;
  3567. }
  3568. }
  3569. if (str_eq(prop, "transition-duration")) {
  3570. if (item_ms > ms)
  3571. ms = item_ms;
  3572. } else if (item_ms > 0) {
  3573. int m = tr_prop_mask(v + a, e - a);
  3574. if (!m && !contains_ci_len(v + a, e - a, "none"))
  3575. m = BR_TR_OPACITY | BR_TR_TRANSFORM;
  3576. if (m) {
  3577. mask |= m;
  3578. if (item_ms > ms)
  3579. ms = item_ms;
  3580. if (ease < 0)
  3581. ease = ease_code(v + a, e - a);
  3582. }
  3583. }
  3584. i = e + 1;
  3585. }
  3586. if (str_eq(prop, "transition-duration")) {
  3587. decl_add(out, nout, PROP_TR_MS, ms, important);
  3588. return;
  3589. }
  3590. decl_add(out, nout, PROP_TR_MASK, mask, important);
  3591. decl_add(out, nout, PROP_TR_MS, ms, important);
  3592. decl_add(out, nout, PROP_TR_EASE, ease < 0 ? BR_EASE_EASE : ease,
  3593. important);
  3594. }
  3595. static int find_fn(const char *v, unsigned int len, const char *name,
  3596. unsigned int *arg)
  3597. {
  3598. unsigned int n = s_len(name), i;
  3599. for (i = 0; i + n <= len; i++) {
  3600. unsigned int j;
  3601. if (!ci_eq_n(v + i, name, n))
  3602. continue;
  3603. j = i + n;
  3604. while (j < len && is_space(v[j]))
  3605. j++;
  3606. if (j < len && v[j] == '(') {
  3607. *arg = j + 1;
  3608. return 1;
  3609. }
  3610. }
  3611. return 0;
  3612. }
  3613. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3614. const char *s, unsigned int len,
  3615. int important)
  3616. {
  3617. unsigned int pos = 0, ts, te;
  3618. signed short val[2] = { 50, 50 };
  3619. unsigned char kind[2] = { 1, 1 };
  3620. int vert[2] = { 0, 0 }, n = 0;
  3621. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3622. signed short v;
  3623. int is_vert;
  3624. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3625. val[n] = v;
  3626. kind[n] = 1;
  3627. vert[n] = is_vert;
  3628. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3629. val[n] = v;
  3630. vert[n] = 0;
  3631. } else {
  3632. n++;
  3633. continue;
  3634. }
  3635. n++;
  3636. }
  3637. if (n == 1) {
  3638. if (vert[0]) {
  3639. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3640. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3641. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3642. important);
  3643. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3644. } else {
  3645. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3646. important);
  3647. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3648. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3649. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3650. }
  3651. return;
  3652. }
  3653. if (n >= 2) {
  3654. int x = vert[0] && !vert[1] ? 1 : 0;
  3655. int y = 1 - x;
  3656. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3657. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3658. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3659. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3660. }
  3661. }
  3662. /* Copy a background value with every url(...) body blanked (kept as
  3663. * `url()`), for the keyword and colour scans of the shorthand. */
  3664. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3665. unsigned int cap)
  3666. {
  3667. unsigned int i = 0, n = 0;
  3668. while (i < len && n + 1 < cap) {
  3669. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3670. char q = 0;
  3671. memcpy(out + n, "url(", 4);
  3672. n += 4;
  3673. i += 4;
  3674. while (i < len) {
  3675. if (q) {
  3676. if (s[i] == '\\' && i + 1 < len)
  3677. i++;
  3678. else if (s[i] == q)
  3679. q = 0;
  3680. } else if (s[i] == '"' || s[i] == '\'') {
  3681. q = s[i];
  3682. } else if (s[i] == ')') {
  3683. break;
  3684. }
  3685. i++;
  3686. }
  3687. continue; /* the ')' is copied below */
  3688. }
  3689. out[n++] = s[i++];
  3690. }
  3691. out[n] = '\0';
  3692. return n;
  3693. }
  3694. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3695. const char *v, unsigned int vl, int important)
  3696. {
  3697. unsigned int i;
  3698. for (i = 0; i + 4 <= vl; i++) {
  3699. if (ci_eq_n(v + i, "url(", 4)) {
  3700. unsigned int st = i + 4, en = st;
  3701. char q = 0;
  3702. int idx;
  3703. /* `)` inside a quoted data: URL (an SVG's
  3704. * rgba(...)) does not end it. */
  3705. while (en < vl) {
  3706. if (q) {
  3707. if (v[en] == '\\' && en + 1 < vl)
  3708. en++;
  3709. else if (v[en] == q)
  3710. q = 0;
  3711. } else if (v[en] == '"' || v[en] == '\'') {
  3712. q = v[en];
  3713. } else if (v[en] == ')') {
  3714. break;
  3715. }
  3716. en++;
  3717. }
  3718. idx = register_bg_url(v + st, en - st);
  3719. if (idx)
  3720. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3721. important);
  3722. return;
  3723. }
  3724. }
  3725. }
  3726. static int parse_len_token(const char *s, unsigned int len, int *out)
  3727. {
  3728. unsigned int pos = 0;
  3729. int v;
  3730. if (!len)
  3731. return 0;
  3732. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3733. return 0;
  3734. v = parse_len(s, len, &pos);
  3735. if (pos == 0)
  3736. return 0;
  3737. *out = v;
  3738. return 1;
  3739. }
  3740. static void expand_four(int v[4], int n)
  3741. {
  3742. if (n == 1) {
  3743. v[1] = v[2] = v[3] = v[0];
  3744. } else if (n == 2) {
  3745. v[2] = v[0];
  3746. v[3] = v[1];
  3747. } else if (n == 3) {
  3748. v[3] = v[1];
  3749. }
  3750. }
  3751. /* Custom properties defined in the declaration block being expanded. A
  3752. * component that sets a variable and uses it in the same rule (Infima's
  3753. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3754. * var(--ifm-col-width) }`) means its own value: the element matching the
  3755. * rule gets it. The document-wide table cannot know that, because
  3756. * `.col` and `.col--N` give the name a dozen different values. */
  3757. static int css_env_active; /* css_local holds an element's variables */
  3758. #define CSS_LOCAL_VARS 48
  3759. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3760. unsigned int i);
  3761. static struct {
  3762. const char *name, *val;
  3763. unsigned short nlen, vlen;
  3764. } css_local[CSS_LOCAL_VARS];
  3765. static unsigned int css_nlocal;
  3766. static int css_local_find(const char *name, unsigned int nlen)
  3767. {
  3768. unsigned int k;
  3769. for (k = 0; k < css_nlocal; k++)
  3770. if (css_local[k].nlen == nlen &&
  3771. !memcmp(css_local[k].name, name, nlen))
  3772. return (int)k;
  3773. return -1;
  3774. }
  3775. static int css_local_find_before(const char *name, unsigned int nlen,
  3776. unsigned int end)
  3777. {
  3778. unsigned int k;
  3779. for (k = 0; k < end; k++)
  3780. if (css_local[k].nlen == nlen &&
  3781. !memcmp(css_local[k].name, name, nlen))
  3782. return (int)k;
  3783. return -1;
  3784. }
  3785. static void css_local_append(const char *s, unsigned int len);
  3786. /* Collect the top-level `--name: value` declarations of a block. */
  3787. static void css_local_collect(const char *s, unsigned int len)
  3788. {
  3789. css_nlocal = 0;
  3790. css_local_append(s, len);
  3791. }
  3792. static void css_local_append(const char *s, unsigned int len)
  3793. {
  3794. unsigned int i = 0;
  3795. while (i < len) {
  3796. unsigned int ns, ne, vs, ve;
  3797. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3798. i++;
  3799. ns = i;
  3800. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3801. i++;
  3802. if (i >= len || s[i] != ':') {
  3803. if (i < len && s[i] == '{')
  3804. i = css_skip_nested(s, len, i + 1);
  3805. continue;
  3806. }
  3807. ne = i;
  3808. while (ne > ns && is_space(s[ne - 1]))
  3809. ne--;
  3810. vs = i + 1;
  3811. ve = css_value_end(s, len, vs);
  3812. i = ve;
  3813. while (vs < ve && is_space(s[vs]))
  3814. vs++;
  3815. while (ve > vs && is_space(s[ve - 1]))
  3816. ve--;
  3817. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3818. ve -= 10;
  3819. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3820. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3821. continue;
  3822. css_local[css_nlocal].name = s + ns;
  3823. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3824. css_local[css_nlocal].val = s + vs;
  3825. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3826. css_nlocal++;
  3827. }
  3828. }
  3829. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3830. * the input is copied verbatim when it contains no var(). */
  3831. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3832. unsigned int len, char *out,
  3833. unsigned int cap, int depth)
  3834. {
  3835. unsigned int i = 0, n = 0;
  3836. if (depth > 8)
  3837. return 0;
  3838. while (i < len && n + 1 < cap) {
  3839. unsigned int p, inner, close, comma, nlen, fstart;
  3840. char quote;
  3841. int parens, glue;
  3842. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3843. out[n++] = s[i++];
  3844. continue;
  3845. }
  3846. p = i + 4;
  3847. quote = 0;
  3848. parens = 1;
  3849. while (p < len && parens) {
  3850. char c = s[p];
  3851. if (quote) {
  3852. if (c == quote)
  3853. quote = 0;
  3854. else if (c == '\\' && p + 1 < len)
  3855. p++;
  3856. } else if (c == '"' || c == '\'') {
  3857. quote = c;
  3858. } else if (c == '(') {
  3859. parens++;
  3860. } else if (c == ')') {
  3861. parens--;
  3862. }
  3863. p++;
  3864. }
  3865. if (parens) {
  3866. out[n++] = s[i++];
  3867. continue;
  3868. }
  3869. close = p - 1;
  3870. inner = i + 4;
  3871. /* var() substitutes tokens: minified Infima writes
  3872. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3873. * must not become the one token `700.875rem`. Separate the
  3874. * value from a glued neighbour on both sides. */
  3875. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3876. out[n - 1] != '(' && out[n - 1] != ',' &&
  3877. out[n - 1] != ':' && out[n - 1] != '/')
  3878. out[n++] = ' ';
  3879. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3880. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3881. s[p] != '!' && s[p] != '}';
  3882. while (inner < close && is_space(s[inner]))
  3883. inner++;
  3884. comma = inner;
  3885. parens = 0;
  3886. quote = 0;
  3887. while (comma < close) {
  3888. char c = s[comma];
  3889. if (quote) {
  3890. if (c == quote)
  3891. quote = 0;
  3892. } else if (c == '"' || c == '\'') {
  3893. quote = c;
  3894. } else if (c == '(') {
  3895. parens++;
  3896. } else if (c == ')' && parens > 0) {
  3897. parens--;
  3898. } else if (c == ',' && parens == 0) {
  3899. break;
  3900. }
  3901. comma++;
  3902. }
  3903. nlen = comma - inner;
  3904. while (nlen && is_space(s[inner + nlen - 1]))
  3905. nlen--;
  3906. fstart = comma < close ? comma + 1 : close;
  3907. while (fstart < close && is_space(s[fstart]))
  3908. fstart++;
  3909. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3910. unsigned int vi = css_var_find(d, s + inner, nlen);
  3911. int li = css_local_find(s + inner, nlen);
  3912. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3913. s + close + 1) {
  3914. /* (not the definition's own value) */
  3915. n += css_expand_vars(d, css_local[li].val,
  3916. css_local[li].vlen,
  3917. out + n, cap - n,
  3918. depth + 1);
  3919. i = p;
  3920. if (glue && n + 1 < cap)
  3921. out[n++] = ' ';
  3922. continue;
  3923. }
  3924. if (vi < BR_MAX_VARS &&
  3925. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3926. vi = BR_MAX_VARS;
  3927. if (vi < BR_MAX_VARS) {
  3928. const char *v = attr_ptr(d, d->var_val[vi]);
  3929. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3930. out + n,
  3931. cap - n, depth + 1);
  3932. n += vn;
  3933. } else if (fstart < close) {
  3934. unsigned int vn = css_expand_vars(d, s + fstart,
  3935. close - fstart,
  3936. out + n, cap - n,
  3937. depth + 1);
  3938. n += vn;
  3939. }
  3940. } else if (fstart < close) {
  3941. unsigned int vn = css_expand_vars(d, s + fstart,
  3942. close - fstart,
  3943. out + n, cap - n,
  3944. depth + 1);
  3945. n += vn;
  3946. }
  3947. i = p;
  3948. if (glue && n + 1 < cap)
  3949. out[n++] = ' ';
  3950. }
  3951. out[n] = '\0';
  3952. return n;
  3953. }
  3954. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3955. unsigned int i)
  3956. {
  3957. while (i < len && (is_space(s[i]) || s[i] == ','))
  3958. i++;
  3959. return i;
  3960. }
  3961. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3962. * argument). Returns 1 on success. */
  3963. static int parse_grid_track(const char *s, unsigned int len,
  3964. unsigned int *pp, unsigned short *val,
  3965. unsigned char *kind)
  3966. {
  3967. unsigned int i = grid_skip_sep(s, len, *pp);
  3968. unsigned int whole = 0, frac = 0, scale = 1;
  3969. int any = 0;
  3970. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3971. unsigned int j = i + 7;
  3972. while (j < len && s[j] != ',')
  3973. j++;
  3974. if (j >= len)
  3975. return 0;
  3976. i = j + 1;
  3977. while (i < len && is_space(s[i]))
  3978. i++;
  3979. }
  3980. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3981. int depth = 1;
  3982. i += 7;
  3983. while (i < len && depth) {
  3984. if (s[i] == '(')
  3985. depth++;
  3986. else if (s[i] == ')')
  3987. depth--;
  3988. i++;
  3989. }
  3990. *pp = i;
  3991. return 0;
  3992. }
  3993. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3994. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3995. i++;
  3996. any = 1;
  3997. }
  3998. if (i < len && s[i] == '.') {
  3999. i++;
  4000. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4001. if (scale < 1000) {
  4002. frac = frac * 10 + (unsigned int)(s[i] - '0');
  4003. scale *= 10;
  4004. }
  4005. i++;
  4006. any = 1;
  4007. }
  4008. }
  4009. if (!any) {
  4010. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  4011. *val = 1;
  4012. *kind = 3;
  4013. *pp = i + 4;
  4014. return 1;
  4015. }
  4016. return 0;
  4017. }
  4018. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  4019. *val = (unsigned short)(whole ? whole : 1);
  4020. *kind = 3;
  4021. i += 2;
  4022. } else if (i < len && s[i] == '%') {
  4023. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  4024. *kind = 2;
  4025. i++;
  4026. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  4027. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  4028. *kind = 1;
  4029. i += 3;
  4030. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  4031. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  4032. *kind = 1;
  4033. i += 2;
  4034. } else {
  4035. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  4036. *kind = 1;
  4037. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  4038. i += 2;
  4039. }
  4040. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  4041. i++;
  4042. *pp = i;
  4043. return 1;
  4044. }
  4045. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  4046. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  4047. * keep the two-column path). */
  4048. static int grid_repeat_count(const char *s, unsigned int len)
  4049. {
  4050. unsigned int i = 0;
  4051. int n = 0;
  4052. while (i < len && is_space(s[i]))
  4053. i++;
  4054. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  4055. i += 7;
  4056. while (i < len && is_space(s[i]))
  4057. i++;
  4058. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4059. n = n * 10 + (s[i] - '0');
  4060. i++;
  4061. }
  4062. if (n < 1 || n > 12)
  4063. return 0;
  4064. while (i < len && (is_space(s[i]) || s[i] == ','))
  4065. i++;
  4066. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  4067. return n;
  4068. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  4069. i += 7;
  4070. while (i < len && is_space(s[i]))
  4071. i++;
  4072. if (i < len && s[i] == '0') {
  4073. i++;
  4074. while (i < len && (is_space(s[i]) ||
  4075. s[i] == ',' || s[i] == 'p' ||
  4076. s[i] == 'x'))
  4077. i++;
  4078. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  4079. return n;
  4080. }
  4081. }
  4082. return 0;
  4083. }
  4084. /* "1fr 1fr 1fr" */
  4085. for (;;) {
  4086. while (i < len && is_space(s[i]))
  4087. i++;
  4088. if (i >= len)
  4089. break;
  4090. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  4091. (i + 3 == len || is_space(s[i + 3]))) {
  4092. n++;
  4093. i += 3;
  4094. } else {
  4095. return 0;
  4096. }
  4097. }
  4098. return n >= 3 ? n : 0;
  4099. }
  4100. static int parse_grid_cols(const char *s, unsigned int len,
  4101. unsigned short val[2], unsigned char kind[2])
  4102. {
  4103. unsigned int pos = 0;
  4104. int n = 0;
  4105. while (n < 2) {
  4106. unsigned int before = pos;
  4107. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  4108. break;
  4109. n++;
  4110. if (pos <= before)
  4111. break;
  4112. while (pos < len && is_space(s[pos]))
  4113. pos++;
  4114. if (pos >= len)
  4115. break;
  4116. }
  4117. return n;
  4118. }
  4119. static int parse_grid_slot(const char *s, unsigned int len)
  4120. {
  4121. if (contains_ci_len(s, len, "footer") ||
  4122. contains_ci_len(s, len, "header") ||
  4123. contains_ci_len(s, len, "sitenotice") ||
  4124. contains_ci_len(s, len, "navbar"))
  4125. return 1;
  4126. if (contains_ci_len(s, len, "columnstart") ||
  4127. contains_ci_len(s, len, "sidebar") ||
  4128. contains_ci_len(s, len, "left") ||
  4129. contains_ci_len(s, len, "start"))
  4130. return 2;
  4131. if (contains_ci_len(s, len, "pagecontent") ||
  4132. contains_ci_len(s, len, "content") ||
  4133. contains_ci_len(s, len, "main") ||
  4134. contains_ci_len(s, len, "right") ||
  4135. contains_ci_len(s, len, "end"))
  4136. return 3;
  4137. return 0;
  4138. }
  4139. static int css_has_var(const char *s, unsigned int len)
  4140. {
  4141. unsigned int i;
  4142. for (i = 0; i + 4 <= len; i++)
  4143. if ((s[i] == 'v' || s[i] == 'V') &&
  4144. ci_eq_n(s + i, "var(", 4))
  4145. return 1;
  4146. return 0;
  4147. }
  4148. static unsigned int css_skip_comment(const char *s, unsigned int len,
  4149. unsigned int i)
  4150. {
  4151. i += 2;
  4152. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  4153. i++;
  4154. if (i + 1 < len)
  4155. i += 2;
  4156. else
  4157. i = len;
  4158. return i;
  4159. }
  4160. static unsigned int css_skip_string(const char *s, unsigned int len,
  4161. unsigned int i)
  4162. {
  4163. char q = s[i++];
  4164. while (i < len && s[i] != q) {
  4165. if (s[i] == '\\' && i + 1 < len)
  4166. i++;
  4167. i++;
  4168. }
  4169. if (i < len)
  4170. i++;
  4171. return i;
  4172. }
  4173. /* Find the '}' that closes a declaration block. i points just past the
  4174. * opening '{'. Comments and quoted strings are skipped: a brace inside
  4175. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  4176. * selectors such as "Highlight-Stories {" without a closing brace). */
  4177. static unsigned int css_block_end(const char *s, unsigned int len,
  4178. unsigned int i)
  4179. {
  4180. int depth = 0;
  4181. while (i < len) {
  4182. char c = s[i];
  4183. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4184. i = css_skip_comment(s, len, i);
  4185. continue;
  4186. }
  4187. if (c == '"' || c == '\'') {
  4188. i = css_skip_string(s, len, i);
  4189. continue;
  4190. }
  4191. if (c == '{') {
  4192. depth++;
  4193. } else if (c == '}') {
  4194. if (depth == 0)
  4195. return i;
  4196. depth--;
  4197. }
  4198. i++;
  4199. }
  4200. return len;
  4201. }
  4202. /* Skip a block whose opening '{' has already been consumed. */
  4203. static unsigned int css_skip_nested(const char *s, unsigned int len,
  4204. unsigned int i)
  4205. {
  4206. int depth = 1;
  4207. while (i < len && depth) {
  4208. char c = s[i];
  4209. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4210. i = css_skip_comment(s, len, i);
  4211. continue;
  4212. }
  4213. if (c == '"' || c == '\'') {
  4214. i = css_skip_string(s, len, i);
  4215. continue;
  4216. }
  4217. if (c == '{')
  4218. depth++;
  4219. else if (c == '}')
  4220. depth--;
  4221. i++;
  4222. }
  4223. return i;
  4224. }
  4225. /* ---- @font-face ------------------------------------------------------- */
  4226. static void font_trim(const char **sp, unsigned int *len)
  4227. {
  4228. const char *s = *sp;
  4229. unsigned int n = *len;
  4230. while (n && is_space(*s)) {
  4231. s++;
  4232. n--;
  4233. }
  4234. while (n && is_space(s[n - 1]))
  4235. n--;
  4236. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4237. s++;
  4238. n -= 2;
  4239. }
  4240. *sp = s;
  4241. *len = n;
  4242. }
  4243. static int font_name_eq(const char *a, const char *b, unsigned int blen)
  4244. {
  4245. unsigned int k;
  4246. if (s_len(a) != blen)
  4247. return 0;
  4248. for (k = 0; k < blen; k++) {
  4249. char x = a[k], y = b[k];
  4250. if (x >= 'A' && x <= 'Z')
  4251. x = (char)(x - 'A' + 'a');
  4252. if (y >= 'A' && y <= 'Z')
  4253. y = (char)(y - 'A' + 'a');
  4254. if (x != y)
  4255. return 0;
  4256. }
  4257. return 1;
  4258. }
  4259. static void font_copy(char *dst, unsigned int cap, const char *src,
  4260. unsigned int n)
  4261. {
  4262. unsigned int k;
  4263. for (k = 0; k < n && src[k] && k + 1 < cap; k++)
  4264. dst[k] = src[k];
  4265. dst[k] = '\0';
  4266. }
  4267. /* Register one @font-face. Every (family, weight range, style) is its own
  4268. * entry; a later rule with the same triple replaces the URL, as the CSS
  4269. * font-face collection does. family_id is the 1-based id of the family's
  4270. * first entry, which is what font-family declarations store. */
  4271. static void font_add(const char *family, unsigned int flen,
  4272. const char *url, unsigned int ulen,
  4273. unsigned int wmin, unsigned int wmax, int italic)
  4274. {
  4275. unsigned int i;
  4276. int fam = 0;
  4277. struct br_font_def *f;
  4278. font_trim(&family, &flen);
  4279. font_trim(&url, &ulen);
  4280. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4281. return;
  4282. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4283. f = &font_defs[i];
  4284. if (!font_name_eq(f->family, family, flen))
  4285. continue;
  4286. if (!fam)
  4287. fam = f->family_id;
  4288. if (f->wmin != wmin || f->wmax != wmax ||
  4289. f->italic != (unsigned char)italic)
  4290. continue;
  4291. font_copy(f->url, sizeof(f->url), url, ulen);
  4292. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4293. return;
  4294. }
  4295. if (font_face_count >= BR_MAX_FACES)
  4296. return;
  4297. f = &font_defs[font_face_count];
  4298. font_copy(f->family, sizeof(f->family), family, flen);
  4299. font_copy(f->url, sizeof(f->url), url, ulen);
  4300. font_copy(f->base, sizeof(f->base), font_base, s_len(font_base));
  4301. f->wmin = (unsigned short)wmin;
  4302. f->wmax = (unsigned short)wmax;
  4303. f->italic = (unsigned char)italic;
  4304. f->used = 0;
  4305. font_face_count++;
  4306. f->family_id = (unsigned char)(fam ? fam : font_face_count);
  4307. /* A family that a font-family declaration already resolved (an
  4308. * earlier @font-face of it) stays wanted for its new variants. */
  4309. if (fam && font_defs[fam - 1].used)
  4310. f->used = 1;
  4311. }
  4312. /* Look up the first name in a font-family list that has an @font-face and
  4313. * mark that family as used, so the browser only downloads faces the page
  4314. * refers to. */
  4315. static int font_family_id(const char *s, unsigned int len)
  4316. {
  4317. unsigned int i = 0;
  4318. while (i < len) {
  4319. unsigned int start, end, q;
  4320. while (i < len && (is_space(s[i]) || s[i] == ','))
  4321. i++;
  4322. if (i >= len)
  4323. break;
  4324. if (s[i] == '"' || s[i] == '\'') {
  4325. char quote = s[i++];
  4326. start = i;
  4327. while (i < len && s[i] != quote)
  4328. i++;
  4329. end = i;
  4330. if (i < len)
  4331. i++;
  4332. } else {
  4333. start = i;
  4334. while (i < len && s[i] != ',')
  4335. i++;
  4336. end = i;
  4337. while (end > start && is_space(s[end - 1]))
  4338. end--;
  4339. }
  4340. if (end <= start)
  4341. continue;
  4342. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4343. int fam;
  4344. unsigned int k;
  4345. if (!font_name_eq(font_defs[q].family, s + start,
  4346. end - start))
  4347. continue;
  4348. fam = font_defs[q].family_id;
  4349. for (k = 0; k < (unsigned int)font_face_count; k++)
  4350. if (font_defs[k].family_id == fam)
  4351. font_defs[k].used = 1;
  4352. return fam;
  4353. }
  4354. }
  4355. return 0;
  4356. }
  4357. /* font-weight descriptor: `normal`, `bold`, a number, or a variable font's
  4358. * `100 900` range. */
  4359. static void font_parse_weight(const char *s, unsigned int len,
  4360. unsigned int *wmin, unsigned int *wmax)
  4361. {
  4362. unsigned int i = 0, n = 0, w[2] = { 400, 400 };
  4363. while (i < len && n < 2) {
  4364. unsigned int v = 0, st;
  4365. while (i < len && is_space(s[i]))
  4366. i++;
  4367. st = i;
  4368. while (i < len && !is_space(s[i]))
  4369. i++;
  4370. if (i == st)
  4371. break;
  4372. if (i - st == 4 && ci_eq_n(s + st, "bold", 4)) {
  4373. v = 700;
  4374. } else if (i - st == 6 && ci_eq_n(s + st, "bolder", 6)) {
  4375. v = 700;
  4376. } else if (i - st == 7 && ci_eq_n(s + st, "lighter", 7)) {
  4377. v = 300;
  4378. } else if (s[st] >= '0' && s[st] <= '9') {
  4379. unsigned int k;
  4380. for (k = st; k < i && s[k] >= '0' && s[k] <= '9'; k++)
  4381. v = v * 10 + (unsigned int)(s[k] - '0');
  4382. } else {
  4383. v = 400;
  4384. }
  4385. if (v < 1)
  4386. v = 1;
  4387. if (v > 1000)
  4388. v = 1000;
  4389. w[n++] = v;
  4390. }
  4391. if (n == 1)
  4392. w[1] = w[0];
  4393. if (w[0] > w[1]) {
  4394. unsigned int t = w[0];
  4395. w[0] = w[1];
  4396. w[1] = t;
  4397. }
  4398. *wmin = w[0];
  4399. *wmax = w[1];
  4400. }
  4401. static int font_hex(char c)
  4402. {
  4403. if (c >= '0' && c <= '9')
  4404. return c - '0';
  4405. if (c >= 'a' && c <= 'f')
  4406. return c - 'a' + 10;
  4407. if (c >= 'A' && c <= 'F')
  4408. return c - 'A' + 10;
  4409. return -1;
  4410. }
  4411. /* Whether a unicode-range list covers basic Latin. Google Fonts splits a
  4412. * family into per-script subsets with the same family/weight/style; only
  4413. * the one holding ASCII is useful here (the text path is Latin-1). */
  4414. static int font_range_has_latin(const char *s, unsigned int len)
  4415. {
  4416. unsigned int i = 0;
  4417. while (i < len) {
  4418. unsigned int lo = 0, hi, digits = 0, wild = 0;
  4419. while (i < len && (is_space(s[i]) || s[i] == ','))
  4420. i++;
  4421. if (i + 2 > len || (s[i] != 'U' && s[i] != 'u') ||
  4422. s[i + 1] != '+') {
  4423. while (i < len && s[i] != ',')
  4424. i++;
  4425. continue;
  4426. }
  4427. i += 2;
  4428. while (i < len && digits < 6 &&
  4429. (font_hex(s[i]) >= 0 || s[i] == '?')) {
  4430. if (s[i] == '?') {
  4431. lo <<= 4;
  4432. wild++;
  4433. } else {
  4434. lo = (lo << 4) | (unsigned int)font_hex(s[i]);
  4435. }
  4436. digits++;
  4437. i++;
  4438. }
  4439. hi = lo | ((1u << (wild * 4)) - 1u);
  4440. if (!wild && i < len && s[i] == '-') {
  4441. hi = 0;
  4442. i++;
  4443. while (i < len && font_hex(s[i]) >= 0) {
  4444. hi = (hi << 4) | (unsigned int)font_hex(s[i]);
  4445. i++;
  4446. }
  4447. }
  4448. if (digits && lo <= 'a' && hi >= 'a')
  4449. return 1;
  4450. while (i < len && s[i] != ',')
  4451. i++;
  4452. }
  4453. return 0;
  4454. }
  4455. /* Pick the first `src` entry the text engine can load: a TrueType/OpenType
  4456. * file or WOFF 1.0, by its format() hint or, without one, by its
  4457. * extension. local() names, WOFF2, EOT, SVG fonts and data: URLs (they do
  4458. * not fit BR_FACE_URL) are skipped. */
  4459. static int font_pick_src(const char *s, unsigned int len, char *url,
  4460. unsigned int cap, unsigned int *ulen)
  4461. {
  4462. unsigned int i = 0;
  4463. while (i < len) {
  4464. unsigned int is_ = i, ie, us = 0, ue = 0, q, depth = 0;
  4465. int quote = 0, ok;
  4466. const char *up;
  4467. unsigned int un;
  4468. /* One comma-separated item, commas inside url()/quotes kept. */
  4469. while (i < len) {
  4470. char c = s[i];
  4471. if (quote) {
  4472. if (c == quote)
  4473. quote = 0;
  4474. } else if (c == '"' || c == '\'') {
  4475. quote = c;
  4476. } else if (c == '(') {
  4477. depth++;
  4478. } else if (c == ')') {
  4479. if (depth)
  4480. depth--;
  4481. } else if (c == ',' && !depth) {
  4482. break;
  4483. }
  4484. i++;
  4485. }
  4486. ie = i;
  4487. i++;
  4488. for (q = is_; q + 4 <= ie; q++)
  4489. if (ci_eq_n(s + q, "url(", 4))
  4490. break;
  4491. if (q + 4 > ie)
  4492. continue;
  4493. us = q + 4;
  4494. for (ue = us; ue < ie && s[ue] != ')'; ue++)
  4495. if (s[ue] == '"' || s[ue] == '\'') {
  4496. char qc = s[ue++];
  4497. while (ue < ie && s[ue] != qc)
  4498. ue++;
  4499. }
  4500. up = s + us;
  4501. un = ue - us;
  4502. font_trim(&up, &un);
  4503. if (!un || un >= cap || (un >= 5 && ci_eq_n(up, "data:", 5)))
  4504. continue;
  4505. ok = 1;
  4506. if (contains_ci_len(s + ue, ie - ue, "format(")) {
  4507. const char *fp = s + ue;
  4508. unsigned int fn = ie - ue;
  4509. ok = (contains_ci_len(fp, fn, "truetype") ||
  4510. contains_ci_len(fp, fn, "opentype") ||
  4511. contains_ci_len(fp, fn, "woff")) &&
  4512. !contains_ci_len(fp, fn, "woff2") &&
  4513. !contains_ci_len(fp, fn, "embedded");
  4514. } else {
  4515. unsigned int e = 0;
  4516. while (e < un && up[e] != '?' && up[e] != '#')
  4517. e++;
  4518. if ((e >= 6 && ci_eq_n(up + e - 6, ".woff2", 6)) ||
  4519. (e >= 4 && ci_eq_n(up + e - 4, ".eot", 4)) ||
  4520. (e >= 4 && ci_eq_n(up + e - 4, ".svg", 4)))
  4521. ok = 0;
  4522. }
  4523. if (!ok)
  4524. continue;
  4525. for (q = 0; q < un; q++)
  4526. url[q] = up[q];
  4527. url[un] = '\0';
  4528. *ulen = un;
  4529. return 1;
  4530. }
  4531. return 0;
  4532. }
  4533. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4534. * at-rules, so faces must be registered before any font-family declaration
  4535. * in the same sheet is resolved. */
  4536. static void scan_font_faces(const char *s, unsigned int len)
  4537. {
  4538. unsigned int i = 0;
  4539. while (i + 10 <= len) {
  4540. unsigned int j, end;
  4541. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4542. unsigned int flen = 0, ulen = 0, p;
  4543. unsigned int wmin = 400, wmax = 400;
  4544. int have_family = 0, have_url = 0, italic = 0, latin = 1;
  4545. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4546. i++;
  4547. continue;
  4548. }
  4549. j = i + 10;
  4550. while (j < len && s[j] != '{' && s[j] != ';')
  4551. j++;
  4552. if (j >= len || s[j] == ';') {
  4553. i = j + 1;
  4554. continue;
  4555. }
  4556. end = css_skip_nested(s, len, j + 1);
  4557. p = j + 1;
  4558. while (p < end) {
  4559. unsigned int ds, de, vs, ve, quote = 0;
  4560. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4561. p++;
  4562. if (p >= end)
  4563. break;
  4564. ds = p;
  4565. while (p < end && s[p] != ':' && s[p] != ';' &&
  4566. s[p] != '}')
  4567. p++;
  4568. de = p;
  4569. if (p >= end || s[p] != ':') {
  4570. while (p < end && s[p] != ';')
  4571. p++;
  4572. continue;
  4573. }
  4574. p++;
  4575. vs = p;
  4576. /* A quoted URL may contain ';' (data: URLs do). */
  4577. while (p < end && (quote || (s[p] != ';' &&
  4578. s[p] != '}'))) {
  4579. if (quote && s[p] == (char)quote)
  4580. quote = 0;
  4581. else if (!quote && (s[p] == '"' || s[p] == '\''))
  4582. quote = (unsigned char)s[p];
  4583. p++;
  4584. }
  4585. ve = p;
  4586. while (ds < de && is_space(s[ds]))
  4587. ds++;
  4588. while (de > ds && is_space(s[de - 1]))
  4589. de--;
  4590. while (vs < ve && is_space(s[vs]))
  4591. vs++;
  4592. while (ve > vs && is_space(s[ve - 1]))
  4593. ve--;
  4594. if (de - ds == 11 &&
  4595. ci_eq_n(s + ds, "font-family", 11)) {
  4596. const char *fp = s + vs;
  4597. unsigned int fn = ve - vs;
  4598. font_trim(&fp, &fn);
  4599. if (fn && fn < sizeof(family)) {
  4600. unsigned int k;
  4601. for (k = 0; k < fn; k++)
  4602. family[k] = fp[k];
  4603. family[fn] = '\0';
  4604. flen = fn;
  4605. have_family = 1;
  4606. }
  4607. } else if (de - ds == 3 &&
  4608. ci_eq_n(s + ds, "src", 3)) {
  4609. /* A later src (the old IE `src: eot;` hack
  4610. * is followed by the real list) wins when
  4611. * it has a usable entry. */
  4612. if (font_pick_src(s + vs, ve - vs, url,
  4613. sizeof(url), &ulen))
  4614. have_url = 1;
  4615. } else if (de - ds == 11 &&
  4616. ci_eq_n(s + ds, "font-weight", 11)) {
  4617. font_parse_weight(s + vs, ve - vs, &wmin,
  4618. &wmax);
  4619. } else if (de - ds == 10 &&
  4620. ci_eq_n(s + ds, "font-style", 10)) {
  4621. italic = contains_ci_len(s + vs, ve - vs,
  4622. "italic") ||
  4623. contains_ci_len(s + vs, ve - vs,
  4624. "oblique");
  4625. } else if (de - ds == 13 &&
  4626. ci_eq_n(s + ds, "unicode-range", 13)) {
  4627. latin = font_range_has_latin(s + vs, ve - vs);
  4628. }
  4629. }
  4630. if (have_family && have_url && latin)
  4631. font_add(family, flen, url, ulen, wmin, wmax, italic);
  4632. i = end;
  4633. }
  4634. }
  4635. /* One border-radius length. Unlike parse_size(), percentages keep their
  4636. * kind: the drawer scales them to the box, not the viewport. */
  4637. static int parse_radius_value(const char *s, unsigned int len,
  4638. unsigned int *pos, unsigned short *val,
  4639. unsigned char *kind)
  4640. {
  4641. unsigned int i = *pos;
  4642. unsigned long v = 0;
  4643. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4644. while (i < len && is_space(s[i]))
  4645. i++;
  4646. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4647. v = v * 10 + (unsigned int)(s[i] - '0');
  4648. digits = 1;
  4649. i++;
  4650. }
  4651. if (i < len && s[i] == '.') {
  4652. i++;
  4653. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4654. fdigits = 1;
  4655. if (frac < 2) {
  4656. v = v * 10 + (unsigned int)(s[i] - '0');
  4657. frac++;
  4658. }
  4659. i++;
  4660. }
  4661. }
  4662. while (frac < 2) {
  4663. v *= 10;
  4664. frac++;
  4665. }
  4666. while (i < len && is_space(s[i]))
  4667. i++;
  4668. if (i < len && s[i] == '%') {
  4669. pct = 1;
  4670. i++;
  4671. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4672. s[i + 2] == 'm') {
  4673. rem = 1;
  4674. i += 3;
  4675. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4676. rem = 2;
  4677. i += 2;
  4678. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4679. i += 2;
  4680. } else if (i < len && is_ident(s[i])) {
  4681. return 0;
  4682. }
  4683. if (!digits && !fdigits)
  4684. return 0;
  4685. if (rem == 1)
  4686. v *= root_font_px;
  4687. else if (rem == 2)
  4688. v *= root_font_px;
  4689. v /= 100;
  4690. if (v > 2048)
  4691. v = 2048;
  4692. *val = (unsigned short)v;
  4693. *kind = pct ? 2 : 1;
  4694. *pos = i;
  4695. return 1;
  4696. }
  4697. /* font: [style] [variant] [weight] size[/line-height] family */
  4698. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4699. const char *v, unsigned int len, int important)
  4700. {
  4701. unsigned int pos = 0, ts, te;
  4702. unsigned short size_raw = 0;
  4703. unsigned char size_unit = 0;
  4704. int have_size = 0;
  4705. int bold = 0, italic = 0;
  4706. int lh_px = 0, lh_pct = 0, mono = 0, face;
  4707. unsigned int fam_at = len;
  4708. while (next_token(v, len, &pos, &ts, &te)) {
  4709. unsigned int tl = te - ts;
  4710. unsigned int slash = 0;
  4711. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4712. bold = 1;
  4713. continue;
  4714. }
  4715. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4716. continue;
  4717. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4718. italic = 1;
  4719. continue;
  4720. }
  4721. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4722. italic = 1;
  4723. continue;
  4724. }
  4725. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4726. continue;
  4727. for (slash = 0; slash < tl; slash++)
  4728. if (v[ts + slash] == '/')
  4729. break;
  4730. if (slash == tl) {
  4731. unsigned int q;
  4732. int weight = 0;
  4733. for (q = 0; q < tl; q++) {
  4734. if (v[ts + q] < '0' || v[ts + q] > '9')
  4735. break;
  4736. weight = weight * 10 + (v[ts + q] - '0');
  4737. }
  4738. if (q == tl && weight >= 100 && weight <= 900) {
  4739. bold = weight >= 500;
  4740. continue;
  4741. }
  4742. }
  4743. {
  4744. unsigned short raw;
  4745. unsigned char unit;
  4746. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4747. &raw, &unit))
  4748. continue;
  4749. size_raw = raw;
  4750. size_unit = unit;
  4751. }
  4752. if (!have_size)
  4753. fam_at = te; /* the family list follows the size */
  4754. have_size = 1;
  4755. if (slash < tl) {
  4756. unsigned int lstart = ts + slash + 1;
  4757. unsigned short lhp = 0;
  4758. unsigned char lhpct = 0;
  4759. if (!parse_line_height(v + lstart, te - lstart,
  4760. &lhp, &lhpct)) {
  4761. lh_px = 0;
  4762. lh_pct = 0;
  4763. } else {
  4764. lh_px = lhp;
  4765. lh_pct = lhpct;
  4766. }
  4767. }
  4768. }
  4769. if (!have_size)
  4770. return;
  4771. mono = contains_ci_len(v, len, "mono");
  4772. face = font_family_id(v + fam_at, len - fam_at);
  4773. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4774. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4775. decl_add(out, nout, PROP_BOLD, bold, important);
  4776. decl_add(out, nout, PROP_ITALIC, italic, important);
  4777. decl_add(out, nout, PROP_MONO, mono, important);
  4778. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4779. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4780. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4781. }
  4782. static int css_collect_only;
  4783. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4784. * index into d->layer_hash. css_layer_name is the dotted path of the
  4785. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4786. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4787. * layer's own rules therefore lose to its sublayers (the spec says the
  4788. * reverse), which no page we render depends on. */
  4789. static unsigned char css_cur_layer;
  4790. static char css_layer_name[128];
  4791. static unsigned int css_anon_layers;
  4792. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4793. {
  4794. unsigned int h = 2166136261u, i;
  4795. for (i = 0; i < len; i++) {
  4796. h ^= (unsigned char)s[i];
  4797. h *= 16777619u;
  4798. }
  4799. return h ? h : 1;
  4800. }
  4801. /* Register (or look up) the layer `name` nested under the current path and
  4802. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4803. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4804. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4805. unsigned int nlen, char *path,
  4806. unsigned int cap)
  4807. {
  4808. unsigned int pl = s_len(css_layer_name), i, h;
  4809. char anon[16];
  4810. while (nlen && is_space(*name)) { name++; nlen--; }
  4811. while (nlen && is_space(name[nlen - 1])) nlen--;
  4812. if (!nlen) {
  4813. /* An anonymous layer is unique; it can never be reopened.
  4814. * The collect-only (custom property) pass does not need a
  4815. * rank, so it must not burn a table slot. */
  4816. unsigned int n = ++css_anon_layers, k = 0;
  4817. char rev[10];
  4818. if (css_collect_only)
  4819. return 0;
  4820. anon[0] = '#';
  4821. while (n && k < sizeof(rev)) {
  4822. rev[k++] = (char)('0' + n % 10);
  4823. n /= 10;
  4824. }
  4825. for (i = 0; i < k; i++)
  4826. anon[1 + i] = rev[k - 1 - i];
  4827. name = anon;
  4828. nlen = k + 1;
  4829. }
  4830. if (pl + 1 + nlen >= cap)
  4831. return 0;
  4832. memcpy(path, css_layer_name, pl);
  4833. if (pl)
  4834. path[pl++] = '.';
  4835. memcpy(path + pl, name, nlen);
  4836. path[pl + nlen] = '\0';
  4837. h = layer_name_hash(path, pl + nlen);
  4838. for (i = 0; i < d->nlayers; i++)
  4839. if (d->layer_hash[i] == h)
  4840. return (unsigned char)(i + 1);
  4841. if (d->nlayers >= BR_MAX_LAYERS)
  4842. return 0;
  4843. d->layer_hash[d->nlayers++] = h;
  4844. return (unsigned char)d->nlayers;
  4845. }
  4846. /* @supports: the engine cannot answer feature queries, so the block
  4847. * applies unless it is negated (`not (...)` usually guards a fallback for
  4848. * old browsers) or asks for selector() support. */
  4849. static int supports_matches(const char *s, unsigned int len)
  4850. {
  4851. while (len && is_space(*s)) { s++; len--; }
  4852. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4853. (len == 3 || is_space(s[3]) || s[3] == '('))
  4854. return 0;
  4855. if (contains_ci_len(s, len, "selector("))
  4856. return 0;
  4857. return 1;
  4858. }
  4859. /* Append one code point of generated content as Latin-1. Typography the
  4860. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4861. * and anything else outside Latin-1 are dropped. */
  4862. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4863. unsigned int cp)
  4864. {
  4865. const char *sub = 0;
  4866. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4867. sub = "-";
  4868. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4869. sub = "'";
  4870. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4871. sub = "\"";
  4872. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4873. sub = "\xB7";
  4874. else if (cp == 0x2026)
  4875. sub = "...";
  4876. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4877. sub = ">";
  4878. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4879. sub = "<";
  4880. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4881. cp == 0x2003 || cp == 0x202F)
  4882. sub = " ";
  4883. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4884. sub = "\xD7";
  4885. else if (cp == 0x2713 || cp == 0x2714)
  4886. sub = "v";
  4887. if (sub) {
  4888. while (*sub && *n + 1 < cap)
  4889. out[(*n)++] = *sub++;
  4890. return;
  4891. }
  4892. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4893. out[(*n)++] = (char)cp;
  4894. else if (cp == '\n' && *n + 1 < cap)
  4895. out[(*n)++] = ' ';
  4896. }
  4897. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4898. * content index + 1, or 0 for none/empty (counters and url() are not
  4899. * generated). */
  4900. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4901. {
  4902. char out[160];
  4903. unsigned int i = 0, n = 0, off;
  4904. while (i < len) {
  4905. char c = s[i];
  4906. if (c == '"' || c == '\'') {
  4907. char q = c;
  4908. i++;
  4909. while (i < len && s[i] != q) {
  4910. unsigned int cp;
  4911. if (s[i] == '\\' && i + 1 < len) {
  4912. unsigned int h = 0, k = 0;
  4913. i++;
  4914. while (k < 6 && i < len &&
  4915. ((s[i] >= '0' && s[i] <= '9') ||
  4916. ((s[i] | 32) >= 'a' &&
  4917. (s[i] | 32) <= 'f'))) {
  4918. char x = (char)(s[i] | 32);
  4919. h = h * 16 + (unsigned int)(x <= '9' ?
  4920. x - '0' : x - 'a' + 10);
  4921. i++;
  4922. k++;
  4923. }
  4924. if (k) {
  4925. if (i < len && s[i] == ' ')
  4926. i++;
  4927. cp = h;
  4928. } else {
  4929. cp = (unsigned char)s[i++];
  4930. }
  4931. } else if ((unsigned char)s[i] >= 0xC0) {
  4932. unsigned char b = (unsigned char)s[i++];
  4933. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4934. cp = b & (0x3F >> more);
  4935. while (more-- && i < len &&
  4936. ((unsigned char)s[i] & 0xC0) == 0x80)
  4937. cp = (cp << 6) | (s[i++] & 0x3F);
  4938. } else {
  4939. cp = (unsigned char)s[i++];
  4940. }
  4941. content_put(out, &n, sizeof(out), cp);
  4942. }
  4943. i++;
  4944. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4945. unsigned int st = i + 5, e = st;
  4946. while (e < len && s[e] != ')')
  4947. e++;
  4948. while (st < e && is_space(s[st]))
  4949. st++;
  4950. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4951. out[n++] = '\001';
  4952. while (st < e && !is_space(s[st]))
  4953. out[n++] = s[st++];
  4954. out[n++] = '\002';
  4955. }
  4956. i = e + 1;
  4957. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4958. ci_eq_n(s + i, "close-quot", 10))) {
  4959. content_put(out, &n, sizeof(out), '"');
  4960. i += 10;
  4961. } else if (c == '(') {
  4962. unsigned int depth = 0;
  4963. /* counter(), url(), ...: skip the arguments */
  4964. for (; i < len; i++) {
  4965. if (s[i] == '(')
  4966. depth++;
  4967. else if (s[i] == ')' && --depth == 0)
  4968. break;
  4969. }
  4970. i++;
  4971. } else {
  4972. i++;
  4973. }
  4974. }
  4975. if (!n) {
  4976. /* `content: ""` still creates the box: icon
  4977. * pseudo-elements are empty strings with a size and a
  4978. * background image. */
  4979. for (i = 0; i < len; i++)
  4980. if (s[i] == '"' || s[i] == '\'')
  4981. return BR_CONTENT_EMPTY;
  4982. return 0;
  4983. }
  4984. if (d->ncontent >= BR_MAX_CONTENT)
  4985. return 0;
  4986. off = store_raw(d, out, n, sizeof(out));
  4987. if (!off)
  4988. return 0;
  4989. d->content_off[d->ncontent++] = off;
  4990. return d->ncontent;
  4991. }
  4992. /* ---- calc() folding ---------------------------------------------------
  4993. *
  4994. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4995. * Frameworks build their scales from arithmetic on custom properties
  4996. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4997. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4998. * calc() in a declaration block is folded into that canonical form:
  4999. * a linear combination of %, px (rem/em at the root font size, vw against
  5000. * the viewport) and unitless numbers, with *, / and nested parentheses.
  5001. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  5002. * the calc() text unchanged. Values are hundredths. */
  5003. static unsigned int viewport_w; /* defined with the media code */
  5004. struct calc_val {
  5005. long px, pct, num;
  5006. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  5007. };
  5008. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  5009. struct calc_val *v, int depth);
  5010. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  5011. {
  5012. while (*i < len && is_space(s[*i]))
  5013. (*i)++;
  5014. }
  5015. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  5016. struct calc_val *v, int depth)
  5017. {
  5018. long whole = 0, frac = 0, scale = 1, n;
  5019. int sign = 1, digits = 0;
  5020. calc_space(s, len, i);
  5021. if (*i < len && (s[*i] == '(' ||
  5022. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  5023. *i += s[*i] == '(' ? 1 : 5;
  5024. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  5025. return 0;
  5026. calc_space(s, len, i);
  5027. if (*i >= len || s[*i] != ')')
  5028. return 0;
  5029. (*i)++;
  5030. return 1;
  5031. }
  5032. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  5033. sign = s[*i] == '-' ? -1 : 1;
  5034. (*i)++;
  5035. }
  5036. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  5037. whole = whole * 10 + (s[*i] - '0');
  5038. digits = 1;
  5039. (*i)++;
  5040. if (whole > 1000000)
  5041. return 0;
  5042. }
  5043. if (*i < len && s[*i] == '.') {
  5044. (*i)++;
  5045. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  5046. if (scale < 10000) {
  5047. frac = frac * 10 + (s[*i] - '0');
  5048. scale *= 10;
  5049. }
  5050. digits = 1;
  5051. (*i)++;
  5052. }
  5053. }
  5054. if (!digits)
  5055. return 0;
  5056. n = sign * (whole * 100 + frac * 100 / scale);
  5057. v->px = v->pct = v->num = 0;
  5058. if (*i < len && s[*i] == '%') {
  5059. (*i)++;
  5060. v->pct = n;
  5061. v->kind = 2;
  5062. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  5063. *i += 3;
  5064. v->px = n * root_font_px;
  5065. v->kind = 1;
  5066. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  5067. *i += 2;
  5068. v->px = n * root_font_px;
  5069. v->kind = 1;
  5070. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  5071. *i += 2;
  5072. v->px = n;
  5073. v->kind = 1;
  5074. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  5075. *i += 2;
  5076. v->px = n * (long)viewport_w / 100;
  5077. v->kind = 1;
  5078. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  5079. return 0; /* vh, deg, fr, ch, ... */
  5080. } else {
  5081. v->num = n;
  5082. v->kind = 4;
  5083. }
  5084. return 1;
  5085. }
  5086. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  5087. struct calc_val *v, int depth)
  5088. {
  5089. if (!calc_unary(s, len, i, v, depth))
  5090. return 0;
  5091. for (;;) {
  5092. struct calc_val r;
  5093. char op;
  5094. calc_space(s, len, i);
  5095. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  5096. return 1;
  5097. op = s[(*i)++];
  5098. if (!calc_unary(s, len, i, &r, depth))
  5099. return 0;
  5100. if (op == '/') {
  5101. if (r.kind != 4 || !r.num)
  5102. return 0;
  5103. v->px = v->px * 100 / r.num;
  5104. v->pct = v->pct * 100 / r.num;
  5105. v->num = v->num * 100 / r.num;
  5106. } else if (r.kind == 4) {
  5107. v->px = v->px * r.num / 100;
  5108. v->pct = v->pct * r.num / 100;
  5109. v->num = v->num * r.num / 100;
  5110. } else if (v->kind == 4) {
  5111. long k = v->num;
  5112. *v = r;
  5113. v->px = v->px * k / 100;
  5114. v->pct = v->pct * k / 100;
  5115. } else {
  5116. return 0; /* length * length */
  5117. }
  5118. }
  5119. }
  5120. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  5121. struct calc_val *v, int depth)
  5122. {
  5123. if (!calc_product(s, len, i, v, depth))
  5124. return 0;
  5125. for (;;) {
  5126. struct calc_val r;
  5127. int neg;
  5128. calc_space(s, len, i);
  5129. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  5130. return 1;
  5131. neg = s[(*i)++] == '-';
  5132. if (!calc_product(s, len, i, &r, depth))
  5133. return 0;
  5134. if ((v->kind == 4) != (r.kind == 4))
  5135. return 0; /* number + length */
  5136. v->px += neg ? -r.px : r.px;
  5137. v->pct += neg ? -r.pct : r.pct;
  5138. v->num += neg ? -r.num : r.num;
  5139. v->kind |= r.kind;
  5140. }
  5141. }
  5142. /* Hundredths -> "12", "-0.5", "33.33". */
  5143. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  5144. {
  5145. char tmp[24];
  5146. unsigned int n = 0, k = 0;
  5147. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  5148. unsigned long w = a / 100, f = a % 100;
  5149. if (v < 0)
  5150. tmp[n++] = '-';
  5151. {
  5152. char rev[12];
  5153. unsigned int r = 0;
  5154. do {
  5155. rev[r++] = (char)('0' + w % 10);
  5156. w /= 10;
  5157. } while (w && r < sizeof(rev));
  5158. while (r)
  5159. tmp[n++] = rev[--r];
  5160. }
  5161. if (f) {
  5162. tmp[n++] = '.';
  5163. tmp[n++] = (char)('0' + f / 10);
  5164. if (f % 10)
  5165. tmp[n++] = (char)('0' + f % 10);
  5166. }
  5167. for (k = 0; k < n && k < cap; k++)
  5168. o[k] = tmp[k];
  5169. return k;
  5170. }
  5171. /* Copy s to out with every foldable calc() replaced; returns the length,
  5172. * or 0 when nothing was folded (the caller keeps the original). */
  5173. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  5174. unsigned int cap)
  5175. {
  5176. unsigned int i = 0, n = 0;
  5177. int changed = 0;
  5178. while (i < len && n + 40 < cap) {
  5179. struct calc_val v;
  5180. unsigned int j = i + 5;
  5181. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  5182. (i == 0 || !is_ident(s[i - 1])))) {
  5183. out[n++] = s[i++];
  5184. continue;
  5185. }
  5186. if (!calc_sum(s, len, &j, &v, 0) ||
  5187. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  5188. out[n++] = s[i++];
  5189. continue;
  5190. }
  5191. j++;
  5192. /* Minified CSS glues tokens to the function's parentheses
  5193. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  5194. * a folded `10px16px` would be one token. */
  5195. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  5196. out[n - 1] != '(' && out[n - 1] != ',')
  5197. out[n++] = ' ';
  5198. if (v.kind == 4) {
  5199. n += calc_fmt(out + n, cap - n, v.num);
  5200. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  5201. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  5202. -50 : 50)) /
  5203. 100 * 100);
  5204. out[n++] = 'p';
  5205. out[n++] = 'x';
  5206. } else if (!v.px) {
  5207. n += calc_fmt(out + n, cap - n, v.pct);
  5208. out[n++] = '%';
  5209. } else {
  5210. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  5211. memcpy(out + n, "calc(", 5);
  5212. n += 5;
  5213. n += calc_fmt(out + n, cap - n, v.pct);
  5214. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  5215. n += 4;
  5216. n += calc_fmt(out + n, cap - n, px * 100);
  5217. memcpy(out + n, "px)", 3);
  5218. n += 3;
  5219. }
  5220. i = j;
  5221. changed = 1;
  5222. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  5223. s[i] != ',' && s[i] != '!' && s[i] != '}')
  5224. out[n++] = ' ';
  5225. }
  5226. if (!changed || i < len)
  5227. return 0;
  5228. out[n] = '\0';
  5229. return n;
  5230. }
  5231. static int css_has_calc(const char *s, unsigned int len)
  5232. {
  5233. unsigned int i;
  5234. for (i = 0; i + 5 <= len; i++)
  5235. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  5236. return 1;
  5237. return 0;
  5238. }
  5239. static void parse_decls(struct br_doc *d, const char *s,
  5240. unsigned int len, struct br_decl *out,
  5241. unsigned int *nout)
  5242. {
  5243. static char expanded[4096];
  5244. static char folded[4096];
  5245. if (css_has_var(s, len)) {
  5246. unsigned int elen;
  5247. if (!css_env_active)
  5248. css_local_collect(s, len);
  5249. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  5250. 0);
  5251. if (!css_env_active)
  5252. css_nlocal = 0;
  5253. if (elen) {
  5254. s = expanded;
  5255. len = elen;
  5256. }
  5257. }
  5258. if (css_has_calc(s, len)) {
  5259. unsigned int flen = css_fold_calc(s, len, folded,
  5260. sizeof(folded));
  5261. if (flen) {
  5262. s = folded;
  5263. len = flen;
  5264. }
  5265. }
  5266. unsigned int i = 0;
  5267. *nout = 0;
  5268. while (i < len && *nout < BR_DECLS) {
  5269. unsigned int pstart, pend, vstart, vend, k;
  5270. char prop[24];
  5271. int important = 0;
  5272. while (i < len && (is_space(s[i]) || s[i] == ';'))
  5273. i++;
  5274. if (i >= len)
  5275. break;
  5276. /* A nested at-rule (@media/@supports) inside the declaration
  5277. * block carries its own selector context; skip it rather than
  5278. * reading its prelude as a declaration. */
  5279. if (s[i] == '@') {
  5280. while (i < len && s[i] != '{' && s[i] != ';')
  5281. i++;
  5282. if (i < len && s[i] == '{')
  5283. i = css_skip_nested(s, len, i + 1);
  5284. else if (i < len)
  5285. i++;
  5286. continue;
  5287. }
  5288. pstart = i;
  5289. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  5290. i++;
  5291. if (i < len && s[i] == '{') {
  5292. /* Nested style rule: skip its block. */
  5293. i = css_skip_nested(s, len, i + 1);
  5294. continue;
  5295. }
  5296. pend = i;
  5297. if (i >= len || s[i] != ':')
  5298. continue;
  5299. i++;
  5300. vstart = i;
  5301. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  5302. * does not end the value. */
  5303. i = css_value_end(s, len, i);
  5304. vend = i;
  5305. while (pstart < pend && is_space(s[pstart]))
  5306. pstart++;
  5307. while (pend > pstart && is_space(s[pend - 1]))
  5308. pend--;
  5309. while (vstart < vend && is_space(s[vstart]))
  5310. vstart++;
  5311. while (vend > vstart && is_space(s[vend - 1]))
  5312. vend--;
  5313. if (vend - vstart >= 10 &&
  5314. ci_eq_n(s + vend - 10, "!important", 10)) {
  5315. important = 1;
  5316. vend -= 10;
  5317. while (vend > vstart && is_space(s[vend - 1]))
  5318. vend--;
  5319. }
  5320. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  5321. char c = s[pstart + k];
  5322. if (c >= 'A' && c <= 'Z')
  5323. c = (char)(c - 'A' + 'a');
  5324. prop[k] = c;
  5325. }
  5326. prop[k] = '\0';
  5327. if (str_eq(prop, "color")) {
  5328. unsigned short c;
  5329. if (parse_color(s + vstart, vend - vstart, &c))
  5330. decl_add(out, nout, PROP_COLOR, c, important);
  5331. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  5332. /* SVG paint only: an HTML element's fill never paints
  5333. * its background (tagesschau sets fill on buttons). */
  5334. int is_fill = prop[0] == 'f';
  5335. unsigned short c;
  5336. int mode = paint_mode(s + vstart, vend - vstart, &c);
  5337. if (mode) {
  5338. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  5339. PROP_STROKE_MODE, mode, important);
  5340. if (mode == BR_PAINT_COLOR)
  5341. decl_add(out, nout, is_fill ? PROP_FILL :
  5342. PROP_STROKE, (short)c,
  5343. important);
  5344. }
  5345. } else if (str_eq(prop, "stroke-width")) {
  5346. int sw = svg_len16(s + vstart, vend - vstart);
  5347. if (sw >= 0)
  5348. decl_add(out, nout, PROP_STROKE_W, sw,
  5349. important);
  5350. } else if (str_eq(prop, "fill-rule")) {
  5351. decl_add(out, nout, PROP_FILL_RULE,
  5352. contains_ci_len(s + vstart, vend - vstart,
  5353. "evenodd") ? 1 : 0,
  5354. important);
  5355. } else if (str_eq(prop, "text-anchor")) {
  5356. int a = BR_ALIGN_LEFT;
  5357. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  5358. a = BR_ALIGN_CENTER;
  5359. else if (contains_ci_len(s + vstart, vend - vstart,
  5360. "end"))
  5361. a = BR_ALIGN_RIGHT;
  5362. decl_add(out, nout, PROP_ALIGN, a, important);
  5363. } else if (str_eq(prop, "background-image")) {
  5364. add_bg_image_decl(out, nout, s + vstart,
  5365. vend - vstart, important);
  5366. } else if (str_eq(prop, "background-repeat")) {
  5367. int r = 0;
  5368. if (contains_ci_len(s + vstart, vend - vstart,
  5369. "no-repeat"))
  5370. r = 1;
  5371. else if (contains_ci_len(s + vstart, vend - vstart,
  5372. "repeat-x"))
  5373. r = 2;
  5374. else if (contains_ci_len(s + vstart, vend - vstart,
  5375. "repeat-y"))
  5376. r = 3;
  5377. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  5378. } else if (str_eq(prop, "background-position")) {
  5379. add_bg_position(out, nout, s + vstart, vend - vstart,
  5380. important);
  5381. } else if (str_eq(prop, "background-size")) {
  5382. add_bg_size(out, nout, s + vstart, vend - vstart,
  5383. important);
  5384. } else if (str_eq(prop, "background-color") ||
  5385. str_eq(prop, "background")) {
  5386. unsigned short c;
  5387. int is_bg = str_eq(prop, "background");
  5388. static char bgv[1024];
  5389. const char *vs = s + vstart;
  5390. unsigned int vl = vend - vstart;
  5391. if (is_bg) {
  5392. add_bg_image_decl(out, nout, s + vstart,
  5393. vend - vstart, important);
  5394. /* Keywords and colours are read with url()
  5395. * contents blanked: a data: SVG carries its
  5396. * own fill="rgba(...)". */
  5397. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  5398. vs = bgv;
  5399. add_bg_shorthand_extra(out, nout,
  5400. vs, vl,
  5401. important);
  5402. if (contains_ci_len(vs, vl,
  5403. "no-repeat"))
  5404. decl_add(out, nout, PROP_BG_REPEAT, 1,
  5405. important);
  5406. else if (contains_ci_len(vs, vl,
  5407. "repeat-x"))
  5408. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5409. important);
  5410. else if (contains_ci_len(vs, vl,
  5411. "repeat-y"))
  5412. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5413. important);
  5414. }
  5415. /* `transparent`/`none` are real values: they must
  5416. * beat a lower-specificity colour (and tell form
  5417. * controls the author styled them). */
  5418. if (color_is_transparent(vs, vl) ||
  5419. (vl == 4 &&
  5420. ci_eq_n(vs, "none", 4)))
  5421. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5422. else if (parse_color(vs, vl, &c))
  5423. decl_add(out, nout, PROP_BG, c, important);
  5424. else if (is_bg &&
  5425. find_color_last(vs, vl, &c))
  5426. decl_add(out, nout, PROP_BG, c, important);
  5427. } else if (str_eq(prop, "font-size")) {
  5428. unsigned short raw;
  5429. unsigned char unit;
  5430. if (parse_font_size(s + vstart, vend - vstart,
  5431. &raw, &unit)) {
  5432. decl_add(out, nout, PROP_FONT_PX, raw,
  5433. important);
  5434. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5435. important);
  5436. }
  5437. } else if (str_eq(prop, "font-weight")) {
  5438. int bold = 0;
  5439. unsigned int j;
  5440. if (vend - vstart >= 4 &&
  5441. (ci_eq_n(s + vstart, "bold", 4) ||
  5442. (vend - vstart >= 6 &&
  5443. ci_eq_n(s + vstart, "bolder", 6))))
  5444. bold = 1;
  5445. else {
  5446. int w = 0;
  5447. for (j = vstart; j < vend; j++)
  5448. if (s[j] >= '0' && s[j] <= '9')
  5449. w = w * 10 + (s[j] - '0');
  5450. /* CSS font matching: with only a 400 and a
  5451. * 700 face, 500 (Infima's "semibold" menus)
  5452. * resolves to the regular face; 600 and up
  5453. * to the bold one. */
  5454. if (w >= 600)
  5455. bold = 1;
  5456. }
  5457. decl_add(out, nout, PROP_BOLD, bold, important);
  5458. } else if (str_eq(prop, "font-style")) {
  5459. int italic = (vend - vstart >= 6 &&
  5460. ci_eq_n(s + vstart, "italic", 6));
  5461. decl_add(out, nout, PROP_ITALIC, italic, important);
  5462. } else if (str_eq(prop, "font-family")) {
  5463. unsigned int j;
  5464. int mono = 0;
  5465. int face = font_family_id(s + vstart, vend - vstart);
  5466. for (j = vstart; j + 4 <= vend; j++)
  5467. if (ci_eq_n(s + j, "mono", 4))
  5468. mono = 1;
  5469. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5470. decl_add(out, nout, PROP_MONO, mono, important);
  5471. } else if (str_eq(prop, "font")) {
  5472. add_font_shorthand(out, nout, s + vstart,
  5473. vend - vstart, important);
  5474. } else if (str_eq(prop, "text-align")) {
  5475. int a = BR_ALIGN_LEFT;
  5476. if (vend - vstart >= 6 &&
  5477. ci_eq_n(s + vstart, "center", 6))
  5478. a = BR_ALIGN_CENTER;
  5479. else if (vend - vstart >= 5 &&
  5480. ci_eq_n(s + vstart, "right", 5))
  5481. a = BR_ALIGN_RIGHT;
  5482. decl_add(out, nout, PROP_ALIGN, a, important);
  5483. } else if (str_eq(prop, "text-decoration")) {
  5484. int u = 0, stv = 0, ov = 0;
  5485. if (contains_ci_len(s + vstart, vend - vstart,
  5486. "underline"))
  5487. u = 1;
  5488. if (contains_ci_len(s + vstart, vend - vstart,
  5489. "line-through"))
  5490. stv = 1;
  5491. if (contains_ci_len(s + vstart, vend - vstart,
  5492. "overline"))
  5493. ov = 1;
  5494. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5495. decl_add(out, nout, PROP_STRIKE, stv, important);
  5496. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5497. } else if (str_eq(prop, "pointer-events")) {
  5498. decl_add(out, nout, PROP_PE_NONE,
  5499. vend - vstart >= 4 &&
  5500. ci_eq_n(s + vstart, "none", 4), important);
  5501. } else if (str_eq(prop, "cursor")) {
  5502. int c = parse_cursor(s + vstart, vend - vstart);
  5503. if (c >= 0)
  5504. decl_add(out, nout, PROP_CURSOR, c, important);
  5505. } else if (str_eq(prop, "text-transform")) {
  5506. int u = (vend - vstart >= 9 &&
  5507. ci_eq_n(s + vstart, "uppercase", 9));
  5508. decl_add(out, nout, PROP_UPPER, u, important);
  5509. } else if (str_eq(prop, "white-space")) {
  5510. int nw = contains_ci_len(s + vstart, vend - vstart,
  5511. "nowrap") ||
  5512. contains_ci_len(s + vstart, vend - vstart,
  5513. "pre");
  5514. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5515. } else if (str_eq(prop, "display")) {
  5516. decl_add(out, nout, PROP_DISPLAY,
  5517. parse_display(s + vstart, vend - vstart),
  5518. important);
  5519. } else if (str_eq(prop, "visibility")) {
  5520. int h = contains_ci_len(s + vstart, vend - vstart,
  5521. "hidden") ||
  5522. contains_ci_len(s + vstart, vend - vstart,
  5523. "collapse");
  5524. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5525. } else if (str_eq(prop, "opacity")) {
  5526. /* Stored as transparency (255 - alpha), so a missing
  5527. * declaration means opaque. `0`, `.5`, `0.9`, `50%`;
  5528. * anything unparsable (inherit, var leftovers) is 1. */
  5529. unsigned int q = vstart;
  5530. signed short h100;
  5531. int a = 255;
  5532. if (parse_fixed_100(s, vend, &q, &h100)) {
  5533. int pct = q < vend && s[q] == '%';
  5534. long v = pct ? h100 / 100 : h100;
  5535. if (v < 0)
  5536. v = 0;
  5537. if (v > 100)
  5538. v = 100;
  5539. a = (int)(v * 255 / 100);
  5540. }
  5541. decl_add(out, nout, PROP_OPACITY_HIDDEN, 255 - a,
  5542. important);
  5543. } else if (str_eq(prop, "overflow") ||
  5544. str_eq(prop, "overflow-x") ||
  5545. str_eq(prop, "overflow-y")) {
  5546. int h = contains_ci_len(s + vstart, vend - vstart,
  5547. "hidden") ||
  5548. contains_ci_len(s + vstart, vend - vstart,
  5549. "clip");
  5550. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5551. } else if (str_eq(prop, "float")) {
  5552. int f = 0;
  5553. if (vend - vstart >= 4 &&
  5554. ci_eq_n(s + vstart, "left", 4))
  5555. f = 1;
  5556. else if (vend - vstart >= 5 &&
  5557. ci_eq_n(s + vstart, "right", 5))
  5558. f = 2;
  5559. decl_add(out, nout, PROP_FLOAT, f, important);
  5560. } else if (str_eq(prop, "clear")) {
  5561. int c = 0;
  5562. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5563. c = 3;
  5564. else if (contains_ci_len(s + vstart, vend - vstart,
  5565. "left"))
  5566. c = 1;
  5567. else if (contains_ci_len(s + vstart, vend - vstart,
  5568. "right"))
  5569. c = 2;
  5570. decl_add(out, nout, PROP_CLEAR, c, important);
  5571. } else if (str_eq(prop, "flex-grow") ||
  5572. str_eq(prop, "flex")) {
  5573. unsigned int q = vstart;
  5574. int g = 0;
  5575. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5576. q++;
  5577. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5578. ci_eq_n(s + q, "auto", 4))
  5579. g = 1; /* flex: auto = 1 1 auto */
  5580. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5581. g = g * 10 + (s[q] - '0');
  5582. q++;
  5583. }
  5584. if (g > 100)
  5585. g = 100;
  5586. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5587. if (str_eq(prop, "flex")) {
  5588. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5589. * basis is the item's main size, which our flex
  5590. * row reads from width (Infima's grid:
  5591. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5592. * of the grow number, then any unitless shrink. */
  5593. unsigned int t;
  5594. while (q < vend && (s[q] == '.' ||
  5595. (s[q] >= '0' && s[q] <= '9')))
  5596. q++;
  5597. for (t = 0; t < 2 && q < vend; t++) {
  5598. unsigned int st, e, k2;
  5599. unsigned short bv;
  5600. unsigned char bk;
  5601. while (q < vend && is_space(s[q]))
  5602. q++;
  5603. st = q;
  5604. while (q < vend && !is_space(s[q]))
  5605. q++;
  5606. e = q;
  5607. for (k2 = st; k2 < e; k2++)
  5608. if (!(s[k2] == '.' ||
  5609. (s[k2] >= '0' &&
  5610. s[k2] <= '9')))
  5611. break;
  5612. if (k2 == e)
  5613. continue; /* shrink */
  5614. if (parse_size(s + st, e - st, &bv,
  5615. &bk) && bk && bv) {
  5616. decl_add(out, nout, PROP_WIDTH,
  5617. bv, important);
  5618. decl_add(out, nout,
  5619. PROP_WIDTH_KIND, bk,
  5620. important);
  5621. }
  5622. break;
  5623. }
  5624. }
  5625. } else if (str_eq(prop, "flex-basis")) {
  5626. unsigned short bv;
  5627. unsigned char bk;
  5628. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5629. bk && bv) {
  5630. decl_add(out, nout, PROP_WIDTH, bv, important);
  5631. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5632. important);
  5633. }
  5634. } else if (str_eq(prop, "flex-direction")) {
  5635. decl_add(out, nout, PROP_FLEX_COL,
  5636. contains_ci_len(s + vstart, vend - vstart,
  5637. "column"), important);
  5638. } else if (str_eq(prop, "flex-wrap")) {
  5639. int wrap = 0;
  5640. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5641. wrap = 2;
  5642. else if (contains_ci_len(s + vstart, vend - vstart,
  5643. "wrap"))
  5644. wrap = 1;
  5645. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5646. } else if (str_eq(prop, "justify-content")) {
  5647. int j = 0;
  5648. if (contains_ci_len(s + vstart, vend - vstart,
  5649. "space-between"))
  5650. j = 3;
  5651. else if (contains_ci_len(s + vstart, vend - vstart,
  5652. "space-around"))
  5653. j = 4;
  5654. else if (contains_ci_len(s + vstart, vend - vstart,
  5655. "space-evenly"))
  5656. j = 5;
  5657. else if (contains_ci_len(s + vstart, vend - vstart,
  5658. "center"))
  5659. j = 1;
  5660. else if (contains_ci_len(s + vstart, vend - vstart,
  5661. "flex-end") ||
  5662. contains_ci_len(s + vstart, vend - vstart,
  5663. "end"))
  5664. j = 2;
  5665. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5666. } else if (str_eq(prop, "align-self")) {
  5667. /* 0 auto (the container's align-items), else
  5668. * align-items + 1. */
  5669. int a = 1;
  5670. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5671. a = 0;
  5672. else if (contains_ci_len(s + vstart, vend - vstart,
  5673. "center"))
  5674. a = 2;
  5675. else if (contains_ci_len(s + vstart, vend - vstart,
  5676. "flex-end") ||
  5677. contains_ci_len(s + vstart, vend - vstart,
  5678. "end"))
  5679. a = 3;
  5680. else if (contains_ci_len(s + vstart, vend - vstart,
  5681. "start") ||
  5682. contains_ci_len(s + vstart, vend - vstart,
  5683. "baseline"))
  5684. a = 4;
  5685. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5686. } else if (str_eq(prop, "align-items")) {
  5687. int a = 0;
  5688. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5689. a = 1;
  5690. else if (contains_ci_len(s + vstart, vend - vstart,
  5691. "flex-end") ||
  5692. contains_ci_len(s + vstart, vend - vstart,
  5693. "end"))
  5694. a = 2;
  5695. else if (contains_ci_len(s + vstart, vend - vstart,
  5696. "start") ||
  5697. contains_ci_len(s + vstart, vend - vstart,
  5698. "baseline"))
  5699. a = 3; /* not stretched */
  5700. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5701. } else if (str_eq(prop, "position")) {
  5702. int p = 0;
  5703. if (contains_ci_len(s + vstart, vend - vstart,
  5704. "absolute"))
  5705. p = 1;
  5706. else if (contains_ci_len(s + vstart, vend - vstart,
  5707. "fixed"))
  5708. p = 2;
  5709. else if (contains_ci_len(s + vstart, vend - vstart,
  5710. "relative"))
  5711. p = 3;
  5712. else if (contains_ci_len(s + vstart, vend - vstart,
  5713. "sticky"))
  5714. p = 4;
  5715. decl_add(out, nout, PROP_POS, p, important);
  5716. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5717. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5718. int which = str_eq(prop, "left") ? 0 :
  5719. str_eq(prop, "top") ? 1 :
  5720. str_eq(prop, "right") ? 2 : 3;
  5721. unsigned int pos = 0;
  5722. if (!(vend - vstart >= 4 &&
  5723. ci_eq_n(s + vstart, "auto", 4))) {
  5724. int v = parse_len(s + vstart, vend - vstart, &pos);
  5725. if (v > 32000)
  5726. v = 32000;
  5727. else if (v < -32000)
  5728. v = -32000;
  5729. decl_add(out, nout,
  5730. (unsigned char)(which == 0 ? PROP_LEFT :
  5731. which == 1 ? PROP_TOP :
  5732. which == 2 ? PROP_RIGHT :
  5733. PROP_BOTTOM),
  5734. v, important);
  5735. decl_add(out, nout,
  5736. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5737. which == 1 ? PROP_TOP_SET :
  5738. which == 2 ? PROP_RIGHT_SET :
  5739. PROP_BOTTOM_SET),
  5740. 1, important);
  5741. }
  5742. } else if (str_eq(prop, "z-index")) {
  5743. unsigned int q = vstart;
  5744. int sign = 1;
  5745. int v = 0;
  5746. int any = 0;
  5747. if (q < vend && s[q] == '-') {
  5748. sign = -1;
  5749. q++;
  5750. }
  5751. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5752. v = v * 10 + (s[q] - '0');
  5753. any = 1;
  5754. q++;
  5755. }
  5756. if (any) {
  5757. v *= sign;
  5758. if (v > 32000)
  5759. v = 32000;
  5760. else if (v < -32000)
  5761. v = -32000;
  5762. decl_add(out, nout, PROP_ZINDEX, v, important);
  5763. decl_add(out, nout, PROP_ZSET, 1, important);
  5764. } else if (vend - vstart >= 4 &&
  5765. ci_eq_n(s + vstart, "auto", 4)) {
  5766. decl_add(out, nout, PROP_ZSET, 0, important);
  5767. }
  5768. } else if (str_eq(prop, "transform")) {
  5769. const char *v = s + vstart;
  5770. unsigned int vl = vend - vstart;
  5771. int tx = 0, ty = 0, txp = 0, typ = 0;
  5772. int is_x = contains_ci_len(v, vl, "translatex");
  5773. int is_y = contains_ci_len(v, vl, "translatey");
  5774. unsigned int off = 0, arg;
  5775. if (contains_ci_len(v, vl, "translate")) {
  5776. while (off < vl && v[off] != '(')
  5777. off++;
  5778. if (off < vl)
  5779. off++;
  5780. if (!is_y)
  5781. tx = translate_arg(v, vl, &off, &txp);
  5782. if (!is_x) {
  5783. while (off < vl &&
  5784. (is_space(v[off]) || v[off] == ','))
  5785. off++;
  5786. ty = translate_arg(v, vl, &off, &typ);
  5787. }
  5788. decl_add(out, nout, PROP_TX_PCT, txp, important);
  5789. decl_add(out, nout, PROP_TY_PCT, typ, important);
  5790. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5791. important);
  5792. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5793. important);
  5794. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5795. important);
  5796. }
  5797. if (find_fn(v, vl, "scale", &arg)) {
  5798. unsigned int q = arg;
  5799. signed short sx = 100, sy = 100;
  5800. if (parse_fixed_100(v, vl, &q, &sx)) {
  5801. unsigned int r = q;
  5802. while (r < vl &&
  5803. (is_space(v[r]) || v[r] == ','))
  5804. r++;
  5805. if (!parse_fixed_100(v, vl, &r, &sy))
  5806. sy = sx;
  5807. decl_add(out, nout, PROP_SCALE_X, sx,
  5808. important);
  5809. decl_add(out, nout, PROP_SCALE_Y, sy,
  5810. important);
  5811. decl_add(out, nout,
  5812. PROP_TRANSFORM_PAINT, 1,
  5813. important);
  5814. decl_add(out, nout,
  5815. PROP_TRANSFORM_SET, 1,
  5816. important);
  5817. }
  5818. }
  5819. if (find_fn(v, vl, "rotate", &arg)) {
  5820. unsigned int q = arg;
  5821. signed short deg;
  5822. if (parse_fixed_100(v, vl, &q, &deg)) {
  5823. decl_add(out, nout, PROP_ROTATE, deg,
  5824. important);
  5825. decl_add(out, nout,
  5826. PROP_TRANSFORM_PAINT, 1,
  5827. important);
  5828. decl_add(out, nout,
  5829. PROP_TRANSFORM_SET, 1,
  5830. important);
  5831. }
  5832. }
  5833. } else if (str_eq(prop, "transition") ||
  5834. str_eq(prop, "transition-property") ||
  5835. str_eq(prop, "transition-duration") ||
  5836. str_eq(prop, "transition-timing-function")) {
  5837. add_transition(out, nout, prop, s + vstart,
  5838. vend - vstart, important);
  5839. } else if (str_eq(prop, "transform-origin")) {
  5840. add_transform_origin(out, nout, s + vstart,
  5841. vend - vstart, important);
  5842. } else if (str_eq(prop, "grid-template-columns") ||
  5843. str_eq(prop, "grid-template")) {
  5844. const char *v = s + vstart;
  5845. unsigned int vl = vend - vstart;
  5846. unsigned short val[2];
  5847. unsigned char kind[2];
  5848. int n, q;
  5849. if (str_eq(prop, "grid-template")) {
  5850. for (q = 0; q < (int)vl; q++)
  5851. if (v[q] == '/') {
  5852. q++;
  5853. while (q < (int)vl && is_space(v[q]))
  5854. q++;
  5855. v += q;
  5856. vl -= (unsigned int)q;
  5857. break;
  5858. }
  5859. }
  5860. if (grid_repeat_count(v, vl) >= 1) {
  5861. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5862. * Tailwind's grid-cols-N. N equal columns
  5863. * (GRID_FIXED + N); one column is plain block
  5864. * flow, which also resets a smaller breakpoint's
  5865. * count. */
  5866. int cnt = grid_repeat_count(v, vl);
  5867. decl_add(out, nout, PROP_GRID_AUTO,
  5868. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5869. important);
  5870. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5871. important);
  5872. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5873. contains_ci_len(v, vl, "auto-fill")) {
  5874. unsigned int mp = 0;
  5875. int found = 0;
  5876. unsigned short mw = 200;
  5877. unsigned char mk = 1;
  5878. for (q = 0; q + 7 <= (int)vl; q++)
  5879. if (ci_eq_n(v + q, "minmax(", 7)) {
  5880. mp = (unsigned int)q + 7;
  5881. found = 1;
  5882. break;
  5883. }
  5884. if (found)
  5885. parse_grid_track(v, vl, &mp, &mw, &mk);
  5886. if (mk != 1 || mw < 80)
  5887. mw = 200;
  5888. if (mw > 480)
  5889. mw = 480;
  5890. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5891. decl_add(out, nout, PROP_WIDTH, mw, important);
  5892. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5893. } else {
  5894. n = parse_grid_cols(v, vl, val, kind);
  5895. if (n > 0) {
  5896. decl_add(out, nout, PROP_WIDTH, val[0],
  5897. important);
  5898. decl_add(out, nout, PROP_WIDTH_KIND,
  5899. kind[0], important);
  5900. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5901. important);
  5902. }
  5903. if (n > 1) {
  5904. decl_add(out, nout, PROP_MAXW, val[1],
  5905. important);
  5906. decl_add(out, nout, PROP_MAXW_KIND,
  5907. kind[1], important);
  5908. }
  5909. }
  5910. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5911. /* From the value, not the block start: gap was
  5912. * ignored on nearly every site. */
  5913. unsigned int gp = vstart;
  5914. unsigned short gv = 0;
  5915. unsigned char gk = 1;
  5916. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5917. if (gv > 120)
  5918. gv = 120;
  5919. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5920. }
  5921. } else if (str_eq(prop, "grid-area")) {
  5922. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5923. if (slot)
  5924. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5925. } else if (str_eq(prop, "width") ||
  5926. str_eq(prop, "max-width") ||
  5927. str_eq(prop, "min-width") ||
  5928. str_eq(prop, "height") ||
  5929. str_eq(prop, "max-height") ||
  5930. str_eq(prop, "min-height")) {
  5931. unsigned short v;
  5932. unsigned char kind;
  5933. if (vend - vstart >= 5 &&
  5934. ci_eq_n(s + vstart, "calc(", 5)) {
  5935. int pct, px;
  5936. if (parse_calc(s + vstart + 5,
  5937. vend - vstart - 5, &pct, &px)) {
  5938. if (str_eq(prop, "width")) {
  5939. decl_add(out, nout, PROP_WIDTH,
  5940. 0, important);
  5941. decl_add(out, nout,
  5942. PROP_WIDTH_KIND, 3,
  5943. important);
  5944. decl_add(out, nout,
  5945. PROP_WIDTH_CALC_PCT,
  5946. pct, important);
  5947. decl_add(out, nout,
  5948. PROP_WIDTH_CALC_PX,
  5949. px, important);
  5950. } else if (str_eq(prop, "max-width")) {
  5951. decl_add(out, nout, PROP_MAXW,
  5952. 0, important);
  5953. decl_add(out, nout,
  5954. PROP_MAXW_KIND, 3,
  5955. important);
  5956. decl_add(out, nout,
  5957. PROP_MAXW_CALC_PCT,
  5958. pct, important);
  5959. decl_add(out, nout,
  5960. PROP_MAXW_CALC_PX,
  5961. px, important);
  5962. } else if (str_eq(prop, "min-width")) {
  5963. decl_add(out, nout, PROP_MINW,
  5964. 0, important);
  5965. decl_add(out, nout,
  5966. PROP_MINW_KIND, 3,
  5967. important);
  5968. decl_add(out, nout,
  5969. PROP_MINW_CALC_PCT,
  5970. pct, important);
  5971. decl_add(out, nout,
  5972. PROP_MINW_CALC_PX,
  5973. px, important);
  5974. } else if (str_eq(prop, "height")) {
  5975. decl_add(out, nout, PROP_HEIGHT,
  5976. 0, important);
  5977. decl_add(out, nout,
  5978. PROP_HEIGHT_KIND, 3,
  5979. important);
  5980. decl_add(out, nout,
  5981. PROP_HEIGHT_CALC_PCT,
  5982. pct, important);
  5983. decl_add(out, nout,
  5984. PROP_HEIGHT_CALC_PX,
  5985. px, important);
  5986. } else if (str_eq(prop, "max-height")) {
  5987. decl_add(out, nout, PROP_MAXH,
  5988. 0, important);
  5989. decl_add(out, nout,
  5990. PROP_MAXH_KIND, 3,
  5991. important);
  5992. decl_add(out, nout,
  5993. PROP_MAXH_CALC_PCT,
  5994. pct, important);
  5995. decl_add(out, nout,
  5996. PROP_MAXH_CALC_PX,
  5997. px, important);
  5998. } else {
  5999. decl_add(out, nout, PROP_MINH,
  6000. 0, important);
  6001. decl_add(out, nout,
  6002. PROP_MINH_KIND, 3,
  6003. important);
  6004. decl_add(out, nout,
  6005. PROP_MINH_CALC_PCT,
  6006. pct, important);
  6007. decl_add(out, nout,
  6008. PROP_MINH_CALC_PX,
  6009. px, important);
  6010. }
  6011. }
  6012. continue;
  6013. }
  6014. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  6015. if (str_eq(prop, "width")) {
  6016. decl_add(out, nout, PROP_WIDTH, v, important);
  6017. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  6018. important);
  6019. } else if (str_eq(prop, "max-width")) {
  6020. decl_add(out, nout, PROP_MAXW, v, important);
  6021. decl_add(out, nout, PROP_MAXW_KIND, kind,
  6022. important);
  6023. } else if (str_eq(prop, "min-width")) {
  6024. decl_add(out, nout, PROP_MINW, v, important);
  6025. decl_add(out, nout, PROP_MINW_KIND, kind,
  6026. important);
  6027. } else if (str_eq(prop, "max-height")) {
  6028. decl_add(out, nout, PROP_MAXH, v, important);
  6029. decl_add(out, nout, PROP_MAXH_KIND, kind,
  6030. important);
  6031. } else if (str_eq(prop, "min-height")) {
  6032. decl_add(out, nout, PROP_MINH, v, important);
  6033. decl_add(out, nout, PROP_MINH_KIND, kind,
  6034. important);
  6035. } else {
  6036. decl_add(out, nout, PROP_HEIGHT, v, important);
  6037. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  6038. important);
  6039. }
  6040. }
  6041. } else if (str_eq(prop, "content")) {
  6042. decl_add(out, nout, PROP_CONTENT,
  6043. css_collect_only ? 0 :
  6044. parse_content(d, s + vstart, vend - vstart),
  6045. important);
  6046. } else if (str_eq(prop, "aspect-ratio")) {
  6047. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  6048. * preferred height of an auto-height box. */
  6049. unsigned int q = vstart;
  6050. unsigned int a[2] = { 0, 1 }, k;
  6051. for (k = 0; k < 2; k++) {
  6052. unsigned int whole = 0, frac = 0, sc = 1;
  6053. while (q < vend && is_space(s[q]))
  6054. q++;
  6055. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  6056. s[q] != '.'))
  6057. break;
  6058. while (q < vend && s[q] >= '0' && s[q] <= '9')
  6059. whole = whole * 10 +
  6060. (unsigned int)(s[q++] - '0');
  6061. if (q < vend && s[q] == '.') {
  6062. q++;
  6063. while (q < vend && s[q] >= '0' &&
  6064. s[q] <= '9') {
  6065. if (sc < 100) {
  6066. frac = frac * 10 +
  6067. (unsigned int)(s[q] - '0');
  6068. sc *= 10;
  6069. }
  6070. q++;
  6071. }
  6072. }
  6073. a[k] = whole * 100 + frac * (100 / sc);
  6074. while (q < vend && is_space(s[q]))
  6075. q++;
  6076. if (k == 0 && (q >= vend || s[q] != '/')) {
  6077. a[1] = 100;
  6078. break;
  6079. }
  6080. q++;
  6081. }
  6082. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  6083. a[0] = a[1] = 0;
  6084. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  6085. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  6086. } else if (str_eq(prop, "box-sizing")) {
  6087. int bb = (vend - vstart >= 10 &&
  6088. ci_eq_n(s + vstart, "border-box", 10));
  6089. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  6090. } else if (str_eq(prop, "line-height")) {
  6091. unsigned short px;
  6092. unsigned char pct;
  6093. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  6094. if (px) {
  6095. decl_add(out, nout, PROP_LH_PX, px, important);
  6096. decl_add(out, nout, PROP_LH_PCT, 0, important);
  6097. } else {
  6098. decl_add(out, nout, PROP_LH_PX, 0, important);
  6099. decl_add(out, nout, PROP_LH_PCT, pct, important);
  6100. }
  6101. }
  6102. } else if (str_eq(prop, "list-style") ||
  6103. str_eq(prop, "list-style-type")) {
  6104. int ls = BR_LIST_AUTO;
  6105. if (contains_ci_len(s + vstart, vend - vstart,
  6106. "none"))
  6107. ls = BR_LIST_NONE;
  6108. else if (contains_ci_len(s + vstart, vend - vstart,
  6109. "circle"))
  6110. ls = BR_LIST_CIRCLE;
  6111. else if (contains_ci_len(s + vstart, vend - vstart,
  6112. "square"))
  6113. ls = BR_LIST_SQUARE;
  6114. else if (contains_ci_len(s + vstart, vend - vstart,
  6115. "lower-alpha") ||
  6116. contains_ci_len(s + vstart, vend - vstart,
  6117. "lower-latin"))
  6118. ls = BR_LIST_LOWER_ALPHA;
  6119. else if (contains_ci_len(s + vstart, vend - vstart,
  6120. "upper-alpha") ||
  6121. contains_ci_len(s + vstart, vend - vstart,
  6122. "upper-latin"))
  6123. ls = BR_LIST_UPPER_ALPHA;
  6124. else if (contains_ci_len(s + vstart, vend - vstart,
  6125. "lower-roman"))
  6126. ls = BR_LIST_LOWER_ROMAN;
  6127. else if (contains_ci_len(s + vstart, vend - vstart,
  6128. "upper-roman"))
  6129. ls = BR_LIST_UPPER_ROMAN;
  6130. else if (contains_ci_len(s + vstart, vend - vstart,
  6131. "decimal"))
  6132. ls = BR_LIST_DECIMAL;
  6133. else if (contains_ci_len(s + vstart, vend - vstart,
  6134. "disc"))
  6135. ls = BR_LIST_DISC;
  6136. decl_add(out, nout, PROP_LIST_NONE,
  6137. ls == BR_LIST_NONE, important);
  6138. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  6139. } else if (str_eq(prop, "border-radius") ||
  6140. str_eq(prop, "border-top-left-radius") ||
  6141. str_eq(prop, "border-top-right-radius") ||
  6142. str_eq(prop, "border-bottom-right-radius") ||
  6143. str_eq(prop, "border-bottom-left-radius")) {
  6144. unsigned short v[4];
  6145. unsigned char k[4];
  6146. unsigned int pos = 0;
  6147. int n = 0;
  6148. while (pos < vend - vstart && n < 4) {
  6149. while (pos < vend - vstart &&
  6150. is_space(s[vstart + pos]))
  6151. pos++;
  6152. if (pos >= vend - vstart ||
  6153. s[vstart + pos] == '/')
  6154. break;
  6155. if (!parse_radius_value(s + vstart,
  6156. vend - vstart, &pos,
  6157. &v[n], &k[n]))
  6158. break;
  6159. n++;
  6160. }
  6161. if (n) {
  6162. unsigned short r[4];
  6163. unsigned char rk[4];
  6164. int i;
  6165. if (n == 1) {
  6166. for (i = 0; i < 4; i++) {
  6167. r[i] = v[0];
  6168. rk[i] = k[0];
  6169. }
  6170. } else if (n == 2) {
  6171. r[0] = r[2] = v[0];
  6172. r[1] = r[3] = v[1];
  6173. rk[0] = rk[2] = k[0];
  6174. rk[1] = rk[3] = k[1];
  6175. } else if (n == 3) {
  6176. r[0] = v[0];
  6177. r[1] = r[3] = v[1];
  6178. r[2] = v[2];
  6179. rk[0] = k[0];
  6180. rk[1] = rk[3] = k[1];
  6181. rk[2] = k[2];
  6182. } else {
  6183. for (i = 0; i < 4; i++) {
  6184. r[i] = v[i];
  6185. rk[i] = k[i];
  6186. }
  6187. }
  6188. for (i = 0; i < 4; i++)
  6189. r[i] = rk[i] == 2 ? 255 :
  6190. (r[i] > 255 ? 255 : r[i]);
  6191. if (str_eq(prop, "border-top-left-radius")) {
  6192. decl_add(out, nout, PROP_RADIUS_TL,
  6193. (short)r[0], important);
  6194. } else if (str_eq(prop,
  6195. "border-top-right-radius")) {
  6196. decl_add(out, nout, PROP_RADIUS_TR,
  6197. (short)r[0], important);
  6198. } else if (str_eq(prop,
  6199. "border-bottom-right-radius")) {
  6200. decl_add(out, nout, PROP_RADIUS_BR,
  6201. (short)r[0], important);
  6202. } else if (str_eq(prop,
  6203. "border-bottom-left-radius")) {
  6204. decl_add(out, nout, PROP_RADIUS_BL,
  6205. (short)r[0], important);
  6206. } else {
  6207. decl_add(out, nout, PROP_RADIUS_TL,
  6208. (short)r[0], important);
  6209. decl_add(out, nout, PROP_RADIUS_TR,
  6210. (short)r[1], important);
  6211. decl_add(out, nout, PROP_RADIUS_BR,
  6212. (short)r[2], important);
  6213. decl_add(out, nout, PROP_RADIUS_BL,
  6214. (short)r[3], important);
  6215. }
  6216. }
  6217. } else if (str_eq(prop, "border") ||
  6218. str_eq(prop, "border-top") ||
  6219. str_eq(prop, "border-right") ||
  6220. str_eq(prop, "border-bottom") ||
  6221. str_eq(prop, "border-left") ||
  6222. str_eq(prop, "border-color") ||
  6223. str_eq(prop, "border-width") ||
  6224. str_eq(prop, "border-style") ||
  6225. str_eq(prop, "border-top-width") ||
  6226. str_eq(prop, "border-right-width") ||
  6227. str_eq(prop, "border-bottom-width") ||
  6228. str_eq(prop, "border-left-width") ||
  6229. str_eq(prop, "border-top-color") ||
  6230. str_eq(prop, "border-right-color") ||
  6231. str_eq(prop, "border-bottom-color") ||
  6232. str_eq(prop, "border-left-color") ||
  6233. str_eq(prop, "border-top-style") ||
  6234. str_eq(prop, "border-right-style") ||
  6235. str_eq(prop, "border-bottom-style") ||
  6236. str_eq(prop, "border-left-style")) {
  6237. const char *v = s + vstart;
  6238. unsigned int vl = vend - vstart;
  6239. unsigned int pos = 0, ts, te;
  6240. int widths[4], colors[4], styles[4], trans[4];
  6241. int nw = 0, nc = 0, ns = 0;
  6242. int side = -1;
  6243. int is_wl = str_eq(prop, "border-width");
  6244. int is_cl = str_eq(prop, "border-color");
  6245. int is_sl = str_eq(prop, "border-style");
  6246. int i;
  6247. if (str_eq(prop, "border-top")) side = 0;
  6248. else if (str_eq(prop, "border-right")) side = 1;
  6249. else if (str_eq(prop, "border-bottom")) side = 2;
  6250. else if (str_eq(prop, "border-left")) side = 3;
  6251. else if (str_eq(prop, "border-top-width")) {
  6252. side = 0; is_wl = 2;
  6253. } else if (str_eq(prop, "border-right-width")) {
  6254. side = 1; is_wl = 2;
  6255. } else if (str_eq(prop, "border-bottom-width")) {
  6256. side = 2; is_wl = 2;
  6257. } else if (str_eq(prop, "border-left-width")) {
  6258. side = 3; is_wl = 2;
  6259. } else if (str_eq(prop, "border-top-color")) {
  6260. side = 0; is_cl = 2;
  6261. } else if (str_eq(prop, "border-right-color")) {
  6262. side = 1; is_cl = 2;
  6263. } else if (str_eq(prop, "border-bottom-color")) {
  6264. side = 2; is_cl = 2;
  6265. } else if (str_eq(prop, "border-left-color")) {
  6266. side = 3; is_cl = 2;
  6267. } else if (str_eq(prop, "border-top-style")) {
  6268. side = 0; is_sl = 2;
  6269. } else if (str_eq(prop, "border-right-style")) {
  6270. side = 1; is_sl = 2;
  6271. } else if (str_eq(prop, "border-bottom-style")) {
  6272. side = 2; is_sl = 2;
  6273. } else if (str_eq(prop, "border-left-style")) {
  6274. side = 3; is_sl = 2;
  6275. }
  6276. if (is_wl) {
  6277. while (nw < 4 && next_token(v, vl, &pos,
  6278. &ts, &te)) {
  6279. int x;
  6280. if (!parse_len_token(v + ts, te - ts,
  6281. &x))
  6282. break;
  6283. widths[nw++] = x;
  6284. }
  6285. if (nw) {
  6286. if (is_wl == 1)
  6287. expand_four(widths, nw);
  6288. if (side >= 0 && is_wl == 2) {
  6289. decl_add(out, nout,
  6290. PROP_BW_T + side,
  6291. widths[0], important);
  6292. } else {
  6293. for (i = 0; i < 4; i++)
  6294. decl_add(out, nout,
  6295. PROP_BW_T + i,
  6296. widths[i],
  6297. important);
  6298. }
  6299. }
  6300. } else if (is_cl) {
  6301. unsigned short c;
  6302. int t;
  6303. while (nc < 4 && next_token(v, vl, &pos,
  6304. &ts, &te)) {
  6305. t = color_is_transparent(v + ts,
  6306. te - ts);
  6307. if (!t && !parse_color(v + ts,
  6308. te - ts, &c))
  6309. break;
  6310. colors[nc] = t ? 0 : c;
  6311. trans[nc] = t;
  6312. nc++;
  6313. }
  6314. if (nc) {
  6315. if (is_cl == 1) {
  6316. expand_four(colors, nc);
  6317. expand_four(trans, nc);
  6318. }
  6319. if (side >= 0 && is_cl == 2) {
  6320. decl_add(out, nout,
  6321. PROP_BC_T + side,
  6322. colors[0], important);
  6323. decl_add(out, nout,
  6324. PROP_BT_T + side,
  6325. trans[0], important);
  6326. } else {
  6327. for (i = 0; i < 4; i++) {
  6328. decl_add(out, nout,
  6329. PROP_BC_T + i,
  6330. colors[i],
  6331. important);
  6332. decl_add(out, nout,
  6333. PROP_BT_T + i,
  6334. trans[i],
  6335. important);
  6336. }
  6337. }
  6338. }
  6339. } else if (is_sl) {
  6340. while (ns < 4 && next_token(v, vl, &pos,
  6341. &ts, &te)) {
  6342. int stv;
  6343. if ((te - ts == 4 &&
  6344. ci_eq_n(v + ts, "none", 4)) ||
  6345. (te - ts == 6 &&
  6346. ci_eq_n(v + ts, "hidden", 6)))
  6347. stv = 0;
  6348. else if (te - ts == 6 &&
  6349. ci_eq_n(v + ts,
  6350. "dashed", 6))
  6351. stv = 2;
  6352. else if (te - ts == 6 &&
  6353. ci_eq_n(v + ts,
  6354. "dotted", 6))
  6355. stv = 3;
  6356. else
  6357. stv = 1;
  6358. styles[ns++] = stv;
  6359. }
  6360. if (ns) {
  6361. if (is_sl == 1)
  6362. expand_four(styles, ns);
  6363. if (is_sl == 2 && side >= 0) {
  6364. if (!styles[0])
  6365. decl_add(out, nout,
  6366. PROP_BW_T + side,
  6367. 0, important);
  6368. else
  6369. decl_add(out, nout,
  6370. PROP_BS_T + side,
  6371. styles[0] - 1,
  6372. important);
  6373. } else {
  6374. for (i = 0; i < 4; i++) {
  6375. if (!styles[i])
  6376. decl_add(out, nout,
  6377. PROP_BW_T + i,
  6378. 0,
  6379. important);
  6380. else
  6381. decl_add(out, nout,
  6382. PROP_BS_T + i,
  6383. styles[i] - 1,
  6384. important);
  6385. }
  6386. }
  6387. }
  6388. } else {
  6389. int bw = -1, bc = -1, style_none = 0;
  6390. int bstyle = -1, btrans = 0;
  6391. while (next_token(v, vl, &pos, &ts, &te)) {
  6392. unsigned short c;
  6393. int x;
  6394. if ((te - ts == 4 &&
  6395. ci_eq_n(v + ts, "none", 4)) ||
  6396. (te - ts == 6 &&
  6397. ci_eq_n(v + ts, "hidden", 6))) {
  6398. style_none = 1;
  6399. } else if (te - ts == 6 &&
  6400. ci_eq_n(v + ts, "dashed", 6)) {
  6401. bstyle = BR_BORDER_DASHED;
  6402. } else if (te - ts == 6 &&
  6403. ci_eq_n(v + ts, "dotted", 6)) {
  6404. bstyle = BR_BORDER_DOTTED;
  6405. } else if (te - ts == 5 &&
  6406. ci_eq_n(v + ts, "solid", 5)) {
  6407. bstyle = BR_BORDER_SOLID;
  6408. } else if (parse_len_token(v + ts,
  6409. te - ts, &x)) {
  6410. bw = x;
  6411. } else if (color_is_transparent(v + ts,
  6412. te - ts)) {
  6413. bc = 0;
  6414. btrans = 1;
  6415. } else if (parse_color(v + ts,
  6416. te - ts, &c)) {
  6417. bc = c;
  6418. btrans = 0;
  6419. }
  6420. }
  6421. if (style_none)
  6422. bw = 0;
  6423. if (bc >= 0 && bw < 0)
  6424. bw = 1;
  6425. if (side < 0) {
  6426. if (bw >= 0)
  6427. decl_add(out, nout,
  6428. PROP_BORDER_W, bw,
  6429. important);
  6430. if (bc >= 0)
  6431. decl_add(out, nout,
  6432. PROP_BORDER_COLOR, bc,
  6433. important);
  6434. if (bc >= 0)
  6435. for (i = 0; i < 4; i++)
  6436. decl_add(out, nout,
  6437. PROP_BT_T + i,
  6438. btrans,
  6439. important);
  6440. if (bstyle >= 0)
  6441. for (i = 0; i < 4; i++)
  6442. decl_add(out, nout,
  6443. PROP_BS_T + i,
  6444. bstyle,
  6445. important);
  6446. } else {
  6447. if (bw >= 0)
  6448. decl_add(out, nout,
  6449. PROP_BW_T + side, bw,
  6450. important);
  6451. if (bc >= 0)
  6452. decl_add(out, nout,
  6453. PROP_BC_T + side, bc,
  6454. important);
  6455. if (bc >= 0)
  6456. decl_add(out, nout,
  6457. PROP_BT_T + side,
  6458. btrans, important);
  6459. if (bstyle >= 0)
  6460. decl_add(out, nout,
  6461. PROP_BS_T + side,
  6462. bstyle, important);
  6463. }
  6464. }
  6465. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6466. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6467. unsigned int pos = 0;
  6468. int is_pad = prop[0] == 'p';
  6469. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6470. while (pos < vend - vstart && nv < 4) {
  6471. units[nv] = 0;
  6472. if (parse_margin_calc(s + vstart,
  6473. vend - vstart, &pos,
  6474. &vals[nv], &cpx[nv])) {
  6475. units[nv] = 5;
  6476. } else {
  6477. vals[nv] = parse_margin_len(
  6478. s + vstart, vend - vstart,
  6479. &pos, &units[nv]);
  6480. }
  6481. nv++;
  6482. while (pos < vend - vstart &&
  6483. is_space(s[vstart + pos]))
  6484. pos++;
  6485. }
  6486. if (nv == 1) {
  6487. vals[1] = vals[2] = vals[3] = vals[0];
  6488. units[1] = units[2] = units[3] = units[0];
  6489. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6490. } else if (nv == 2) {
  6491. vals[2] = vals[0];
  6492. vals[3] = vals[1];
  6493. units[2] = units[0];
  6494. units[3] = units[1];
  6495. cpx[2] = cpx[0];
  6496. cpx[3] = cpx[1];
  6497. } else if (nv == 3) {
  6498. vals[3] = vals[1];
  6499. units[3] = units[1];
  6500. cpx[3] = cpx[1];
  6501. }
  6502. if (nv > 0) {
  6503. int k;
  6504. for (k = 0; k < 4; k++) {
  6505. int v = vals[k];
  6506. int u = units[k];
  6507. int au = u == 4;
  6508. if (au)
  6509. v = 0;
  6510. if (is_pad && au)
  6511. u = 0;
  6512. decl_add(out, nout,
  6513. (unsigned char)(base + k), v,
  6514. important);
  6515. decl_add(out, nout,
  6516. (unsigned char)
  6517. ((is_pad ? PROP_PT_U :
  6518. PROP_MT_U) + k),
  6519. u, important);
  6520. if (u == 5)
  6521. decl_add(out, nout,
  6522. (unsigned char)
  6523. ((is_pad ? PROP_PT_CPX :
  6524. PROP_MT_CPX) + k),
  6525. cpx[k], important);
  6526. if (!is_pad && (k == 1 || k == 3))
  6527. decl_add(out, nout,
  6528. k == 1 ? PROP_MR_AUTO :
  6529. PROP_ML_AUTO,
  6530. au, important);
  6531. }
  6532. }
  6533. } else if (str_eq(prop, "margin-top") ||
  6534. str_eq(prop, "margin-right") ||
  6535. str_eq(prop, "margin-bottom") ||
  6536. str_eq(prop, "margin-left") ||
  6537. str_eq(prop, "padding-top") ||
  6538. str_eq(prop, "padding-right") ||
  6539. str_eq(prop, "padding-bottom") ||
  6540. str_eq(prop, "padding-left")) {
  6541. unsigned int pos = 0;
  6542. int unit = 0;
  6543. int v = 0, cpx = 0;
  6544. unsigned char p = PROP_MT;
  6545. const char *side;
  6546. int au;
  6547. if (parse_margin_calc(s + vstart, vend - vstart,
  6548. &pos, &v, &cpx))
  6549. unit = 5;
  6550. else
  6551. v = parse_margin_len(s + vstart,
  6552. vend - vstart, &pos,
  6553. &unit);
  6554. au = unit == 4;
  6555. if (au)
  6556. v = 0;
  6557. if (prop[0] == 'p') {
  6558. p = PROP_PT;
  6559. if (au)
  6560. unit = 0;
  6561. }
  6562. side = prop + (prop[0] == 'p' ? 8 : 7);
  6563. if (str_eq(side, "right"))
  6564. p = (unsigned char)(p + 1);
  6565. else if (str_eq(side, "bottom"))
  6566. p = (unsigned char)(p + 2);
  6567. else if (str_eq(side, "left"))
  6568. p = (unsigned char)(p + 3);
  6569. decl_add(out, nout, p, v, important);
  6570. decl_add(out, nout,
  6571. (unsigned char)((prop[0] == 'p' ?
  6572. PROP_PT_U : PROP_MT_U) +
  6573. (p - (prop[0] == 'p' ?
  6574. PROP_PT : PROP_MT))),
  6575. unit, important);
  6576. if (unit == 5)
  6577. decl_add(out, nout,
  6578. (unsigned char)((prop[0] == 'p' ?
  6579. PROP_PT_CPX : PROP_MT_CPX) +
  6580. (p - (prop[0] == 'p' ?
  6581. PROP_PT : PROP_MT))),
  6582. cpx, important);
  6583. if (str_eq(side, "right"))
  6584. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6585. else if (str_eq(side, "left"))
  6586. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6587. }
  6588. }
  6589. }
  6590. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6591. * nested blocks. This is the mobile/base rules path: media enhancements are
  6592. * deliberately ignored for now. */
  6593. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6594. unsigned int i)
  6595. {
  6596. unsigned int j = i;
  6597. while (j < len && s[j] != '{' && s[j] != ';')
  6598. j++;
  6599. if (j >= len)
  6600. return len;
  6601. if (s[j] == ';')
  6602. return j + 1;
  6603. j++;
  6604. return css_skip_nested(s, len, j);
  6605. }
  6606. static unsigned int viewport_w = 1024;
  6607. static unsigned short theme_text = 0x0000;
  6608. static unsigned short theme_bg = 0xFFFF;
  6609. static unsigned short theme_panel = 0xEF5D;
  6610. static unsigned short theme_link = 0x001B;
  6611. void br_set_theme(unsigned short text, unsigned short bg,
  6612. unsigned short panel, unsigned short link)
  6613. {
  6614. theme_text = text;
  6615. theme_bg = bg;
  6616. theme_panel = panel;
  6617. theme_link = link;
  6618. }
  6619. void br_set_viewport(unsigned int width)
  6620. {
  6621. viewport_w = width ? width : 1024;
  6622. }
  6623. static unsigned int media_len_px(const char *s, unsigned int len)
  6624. {
  6625. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6626. int em = 0;
  6627. while (i < len && is_space(s[i]))
  6628. i++;
  6629. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6630. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6631. i++;
  6632. }
  6633. if (i < len && s[i] == '.') {
  6634. i++;
  6635. /* Consume every fractional digit even when there are more
  6636. * than the three we keep: leaving the tail behind made the
  6637. * unit check miss "em" (74.9375em became 74px). */
  6638. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6639. if (scale < 1000) {
  6640. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6641. scale *= 10;
  6642. }
  6643. i++;
  6644. }
  6645. }
  6646. while (i < len && is_space(s[i]))
  6647. i++;
  6648. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6649. em = 1;
  6650. i += 2;
  6651. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6652. em = 1;
  6653. i += 3;
  6654. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6655. i += 2;
  6656. }
  6657. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6658. if (em)
  6659. return v * 16 / 100;
  6660. return v / 100;
  6661. }
  6662. /* One parenthesised media feature: min-width/max-width and the modern
  6663. * (width <= 600px) form. Unknown features are treated as true. */
  6664. static int media_feature_matches(const char *s, unsigned int len)
  6665. {
  6666. unsigned int i;
  6667. while (len && is_space(*s)) { s++; len--; }
  6668. while (len && is_space(s[len - 1])) len--;
  6669. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6670. /* There is no user-selectable colour scheme. Ignore both
  6671. * variants so a page's base palette is used; sites whose base
  6672. * is dark (like the project docs) then stay dark instead of
  6673. * flipping to their light media override. */
  6674. return 0;
  6675. }
  6676. {
  6677. /* Environment features with a known answer: a mouse-driven
  6678. * screen with normal contrast and no forced colours. Google
  6679. * puts `border:1px solid CanvasText` on its buttons inside
  6680. * `(forced-colors:active)`. */
  6681. static const struct { const char *name, *yes; } env[] = {
  6682. { "forced-colors", "none" },
  6683. { "-ms-high-contrast", "" },
  6684. { "prefers-contrast", "no-preference" },
  6685. { "inverted-colors", "none" },
  6686. { "any-hover", "hover" },
  6687. { "hover", "hover" },
  6688. { "any-pointer", "fine" },
  6689. { "pointer", "fine" },
  6690. { "prefers-reduced-motion", "no-preference" },
  6691. { "prefers-reduced-transparency", "no-preference" },
  6692. { "display-mode", "browser" },
  6693. };
  6694. unsigned int k;
  6695. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6696. unsigned int nl = s_len(env[k].name), j;
  6697. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6698. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6699. continue;
  6700. for (j = nl; j < len && s[j] != ':'; j++)
  6701. ;
  6702. if (j >= len) /* boolean form: (hover) */
  6703. return env[k].yes[0] &&
  6704. !ci_eq_n(env[k].yes, "none", 4);
  6705. j++;
  6706. while (j < len && is_space(s[j]))
  6707. j++;
  6708. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6709. ci_eq_n(s + j, env[k].yes, len - j);
  6710. }
  6711. }
  6712. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6713. for (i = 9; i < len && s[i] != ':'; i++)
  6714. ;
  6715. if (i < len)
  6716. return viewport_w >= media_len_px(s + i + 1,
  6717. len - i - 1);
  6718. return 1;
  6719. }
  6720. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6721. for (i = 9; i < len && s[i] != ':'; i++)
  6722. ;
  6723. if (i < len)
  6724. return viewport_w <= media_len_px(s + i + 1,
  6725. len - i - 1);
  6726. return 1;
  6727. }
  6728. for (i = 0; i < len; i++) {
  6729. if (s[i] == '<' || s[i] == '>') {
  6730. int max = s[i] == '<';
  6731. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6732. if (i + 1 < len && s[i + 1] == '=')
  6733. v = media_len_px(s + i + 2, len - i - 2);
  6734. return max ? viewport_w <= v : viewport_w >= v;
  6735. }
  6736. }
  6737. return 1;
  6738. }
  6739. static int media_query_matches(const char *s, unsigned int len)
  6740. {
  6741. unsigned int i;
  6742. if (contains_ci_len(s, len, "print") &&
  6743. !contains_ci_len(s, len, "screen"))
  6744. return 0;
  6745. if (contains_ci_len(s, len, "not "))
  6746. return 0;
  6747. for (i = 0; i < len; i++) {
  6748. if (s[i] == '(') {
  6749. unsigned int st = ++i;
  6750. while (i < len && s[i] != ')')
  6751. i++;
  6752. if (!media_feature_matches(s + st, i - st))
  6753. return 0;
  6754. }
  6755. }
  6756. return 1;
  6757. }
  6758. static int media_list_matches(const char *s, unsigned int len)
  6759. {
  6760. unsigned int i = 0;
  6761. while (i < len) {
  6762. unsigned int st = i;
  6763. while (i < len && s[i] != ',')
  6764. i++;
  6765. if (media_query_matches(s + st, i - st))
  6766. return 1;
  6767. i++;
  6768. }
  6769. return 0;
  6770. }
  6771. int br_media_matches(const char *query)
  6772. {
  6773. return media_list_matches(query, s_len(query));
  6774. }
  6775. static unsigned int rule_hash(const char *s, unsigned int len)
  6776. {
  6777. unsigned int h = 2166136261u, i;
  6778. for (i = 0; i < len; i++) {
  6779. char c = s[i];
  6780. if (c >= 'A' && c <= 'Z')
  6781. c = (char)(c - 'A' + 'a');
  6782. h ^= (unsigned char)c;
  6783. h *= 16777619u;
  6784. }
  6785. return h % BR_RULE_HASH;
  6786. }
  6787. /* Index a parsed rule by the most selective component of its rightmost
  6788. * compound selector. br_style_for() then only visits rules that can
  6789. * possibly match a node instead of scanning every rule. */
  6790. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6791. unsigned int len);
  6792. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6793. char *out, unsigned int cap, unsigned int *olen);
  6794. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6795. const char *key, unsigned int len)
  6796. {
  6797. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6798. * "md:flex", the class the element carries, not under "md". */
  6799. char cname[96], iname[96];
  6800. unsigned int i, clen = 0, ilen = 0;
  6801. unsigned short *head;
  6802. for (i = 0; i < len; i++) {
  6803. if (key[i] == '\\') {
  6804. i++;
  6805. continue;
  6806. }
  6807. if (key[i] == '(' || key[i] == '[') {
  6808. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6809. i = sel_skip_group(key, i, len) - 1;
  6810. continue;
  6811. }
  6812. if (key[i] == '.' || key[i] == '#') {
  6813. char tmp[96];
  6814. unsigned int n, e;
  6815. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6816. if (n && n + 1 < sizeof(tmp)) {
  6817. if (key[i] == '#') {
  6818. memcpy(iname, tmp, n);
  6819. ilen = n;
  6820. } else {
  6821. memcpy(cname, tmp, n);
  6822. clen = n;
  6823. }
  6824. }
  6825. i = e - 1;
  6826. }
  6827. }
  6828. if (ilen)
  6829. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6830. else if (clen)
  6831. head = &d->rule_bucket[rule_hash(cname, clen)];
  6832. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6833. head = &d->rule_tag[BR_TAG_HTML];
  6834. else if (len && is_ident(key[0])) {
  6835. char name[16];
  6836. unsigned int n = 0, tag;
  6837. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6838. name[n] = key[n];
  6839. n++;
  6840. }
  6841. name[n] = '\0';
  6842. tag = br_tag_from_name(name);
  6843. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6844. head = &d->rule_tag[tag];
  6845. else
  6846. head = &d->rule_any;
  6847. } else {
  6848. head = &d->rule_any;
  6849. }
  6850. d->rule_next[idx] = *head;
  6851. *head = (unsigned short)idx;
  6852. }
  6853. #define SEL_PARTS 16
  6854. struct sel_part {
  6855. unsigned short st, en;
  6856. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6857. };
  6858. static unsigned int selector_specificity(const char *s, unsigned int len);
  6859. static int css_selector_supported(const char *s, unsigned int len);
  6860. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6861. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6862. unsigned int *st, unsigned int *en);
  6863. static int css_collect_only;
  6864. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6865. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6866. * selector with any other pseudo-element. */
  6867. static int css_pseudo_element(char *sel, unsigned int *len)
  6868. {
  6869. static const struct {
  6870. const char *s;
  6871. unsigned char l, pe;
  6872. } pe[] = {
  6873. { "::before", 8, 1 }, { "::after", 7, 2 },
  6874. { ":before", 7, 1 }, { ":after", 6, 2 },
  6875. };
  6876. unsigned int k, n = *len;
  6877. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6878. unsigned int l = pe[k].l;
  6879. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6880. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6881. continue; /* matched the tail of "::x" */
  6882. n -= l;
  6883. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6884. sel[n - 1] == '+' || sel[n - 1] == '~')
  6885. sel[n++] = '*';
  6886. sel[n] = '\0';
  6887. *len = n;
  6888. return pe[k].pe;
  6889. }
  6890. }
  6891. for (k = 0; k + 1 < n; k++)
  6892. if (sel[k] == ':' && sel[k + 1] == ':')
  6893. return -1;
  6894. return 0;
  6895. }
  6896. /* Does a declaration block use or define a custom property whose value
  6897. * depends on the element (defined by some non-root rule)? Such rules are
  6898. * re-expanded per element at cascade time. */
  6899. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6900. unsigned int len)
  6901. {
  6902. unsigned char f = 0;
  6903. unsigned int i, k;
  6904. for (i = 0; i + 6 < len; i++) {
  6905. unsigned int st, e, vi;
  6906. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6907. continue;
  6908. st = i + 4;
  6909. while (st < len && is_space(s[st]))
  6910. st++;
  6911. e = st;
  6912. while (e < len && s[e] != ',' && s[e] != ')' &&
  6913. !is_space(s[e]))
  6914. e++;
  6915. vi = css_var_find(d, s + st, e - st);
  6916. if (vi < BR_MAX_VARS &&
  6917. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6918. f |= BR_RULE_USES_SCOPED;
  6919. break;
  6920. }
  6921. }
  6922. css_local_collect(s, len);
  6923. for (k = 0; k < css_nlocal; k++) {
  6924. unsigned int vi = css_var_find(d, css_local[k].name,
  6925. css_local[k].nlen);
  6926. if (vi < BR_MAX_VARS &&
  6927. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6928. f |= BR_RULE_DEFINES_SCOPED;
  6929. }
  6930. css_nlocal = 0;
  6931. return f;
  6932. }
  6933. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6934. {
  6935. unsigned int i = 0;
  6936. while (i < len && d->nrules < d->rule_cap) {
  6937. unsigned int sstart, send, bstart, bend;
  6938. struct br_decl decls[BR_DECLS];
  6939. unsigned int nd, scope_off = 0;
  6940. unsigned char scope_flags = 0;
  6941. while (i < len && is_space(s[i]))
  6942. i++;
  6943. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6944. i += 2;
  6945. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6946. i++;
  6947. i += 2;
  6948. continue;
  6949. }
  6950. if (i >= len || s[i] == '}')
  6951. break;
  6952. if (s[i] == '@') {
  6953. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6954. unsigned int q = i + 6;
  6955. while (q < len && s[q] != '{')
  6956. q++;
  6957. if (q < len &&
  6958. media_list_matches(s + i + 6,
  6959. q - (i + 6))) {
  6960. unsigned int b = q + 1;
  6961. unsigned int e = css_skip_nested(s, len, b);
  6962. if (e > b)
  6963. parse_css(d, s + b,
  6964. (e - 1) - b);
  6965. i = e;
  6966. } else {
  6967. i = skip_at_rule(s, len, i);
  6968. }
  6969. continue;
  6970. }
  6971. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6972. (i + 6 == len || is_space(s[i + 6]) ||
  6973. s[i + 6] == '{' || s[i + 6] == ';')) {
  6974. unsigned int q = i + 6, nst = q;
  6975. while (q < len && s[q] != '{' && s[q] != ';')
  6976. q++;
  6977. if (q >= len) {
  6978. i = len;
  6979. continue;
  6980. }
  6981. if (s[q] == ';') {
  6982. /* `@layer a, b;` only fixes the order. */
  6983. unsigned int st = nst;
  6984. char path[128];
  6985. while (st < q) {
  6986. unsigned int en = st;
  6987. while (en < q && s[en] != ',')
  6988. en++;
  6989. if (en > st)
  6990. css_layer_register(d, s + st,
  6991. en - st,
  6992. path,
  6993. sizeof(path));
  6994. st = en + 1;
  6995. }
  6996. i = q + 1;
  6997. continue;
  6998. }
  6999. {
  7000. unsigned int b = q + 1;
  7001. unsigned int e = css_skip_nested(s, len, b);
  7002. unsigned char saved = css_cur_layer;
  7003. char saved_name[128], path[128];
  7004. unsigned char rank;
  7005. memcpy(saved_name, css_layer_name,
  7006. sizeof(saved_name));
  7007. rank = css_layer_register(d, s + nst, q - nst,
  7008. path,
  7009. sizeof(path));
  7010. if (rank) {
  7011. css_cur_layer = rank;
  7012. memcpy(css_layer_name, path,
  7013. sizeof(css_layer_name));
  7014. }
  7015. if (e > b)
  7016. parse_css(d, s + b, (e - 1) - b);
  7017. css_cur_layer = saved;
  7018. memcpy(css_layer_name, saved_name,
  7019. sizeof(css_layer_name));
  7020. i = e;
  7021. }
  7022. continue;
  7023. }
  7024. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  7025. unsigned int q = i + 9;
  7026. while (q < len && s[q] != '{' && s[q] != ';')
  7027. q++;
  7028. if (q < len && s[q] == '{' &&
  7029. supports_matches(s + i + 9, q - (i + 9))) {
  7030. unsigned int b = q + 1;
  7031. unsigned int e = css_skip_nested(s, len, b);
  7032. if (e > b)
  7033. parse_css(d, s + b, (e - 1) - b);
  7034. i = e;
  7035. } else {
  7036. i = skip_at_rule(s, len, i);
  7037. }
  7038. continue;
  7039. }
  7040. i = skip_at_rule(s, len, i);
  7041. continue;
  7042. }
  7043. sstart = i;
  7044. while (i < len && s[i] != '{')
  7045. i++;
  7046. if (i >= len)
  7047. break;
  7048. send = i;
  7049. i++;
  7050. bstart = i;
  7051. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  7052. * to the matching close brace; stopping at the first '}' left
  7053. * the parser out of sync and dropped the rest of the sheet
  7054. * (nd-aktuell lost its desktop rules that way). */
  7055. bend = css_block_end(s, len, i);
  7056. i = bend < len ? bend + 1 : len;
  7057. while (sstart < send && is_space(s[sstart]))
  7058. sstart++;
  7059. while (send > sstart && is_space(s[send - 1]))
  7060. send--;
  7061. if (sstart == send)
  7062. continue;
  7063. if (!css_dark_selector(s + sstart, send - sstart))
  7064. css_collect_block(d, s + bstart, bend - bstart,
  7065. css_root_selector(s + sstart,
  7066. send - sstart));
  7067. if (css_collect_only)
  7068. continue;
  7069. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  7070. {
  7071. unsigned char sc = bend - bstart <= 4000 ?
  7072. css_block_scope(d, s + bstart, bend - bstart) :
  7073. 0;
  7074. scope_flags = 0;
  7075. scope_off = 0;
  7076. if (sc) {
  7077. scope_off = store_raw(d, s + bstart,
  7078. bend - bstart,
  7079. bend - bstart);
  7080. if (scope_off)
  7081. scope_flags = sc;
  7082. }
  7083. }
  7084. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  7085. continue;
  7086. /* Split a comma-separated selector list into separate rules.
  7087. * A selector the engine cannot match is dropped; the others
  7088. * in the list still apply. */
  7089. {
  7090. const char *list = s + sstart;
  7091. unsigned int ll = send - sstart, pos = 0, st, en;
  7092. while (d->nrules < d->rule_cap &&
  7093. sel_list_next(list, ll, &pos, &st, &en)) {
  7094. struct sel_part parts[SEL_PARTS];
  7095. struct br_rule *r;
  7096. char sel[160];
  7097. unsigned int k, off;
  7098. int np, pe;
  7099. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  7100. k++)
  7101. sel[k] = is_space(list[st + k]) ? ' ' :
  7102. list[st + k];
  7103. if (st + k < en)
  7104. continue; /* too long */
  7105. sel[k] = '\0';
  7106. pe = css_pseudo_element(sel, &k);
  7107. if (pe < 0 || !css_selector_supported(sel, k))
  7108. continue;
  7109. np = sel_split(sel, k, parts);
  7110. off = store_attr(d, sel, k, 159);
  7111. if (np <= 0 || !off)
  7112. continue;
  7113. r = &d->rules[d->nrules++];
  7114. r->sel_off = off;
  7115. r->key_off = store_attr(d, sel + parts[np - 1].st,
  7116. parts[np - 1].en -
  7117. parts[np - 1].st, 159);
  7118. r->spec = (unsigned short)selector_specificity(sel, k);
  7119. r->single = np == 1;
  7120. r->pe = (unsigned char)pe;
  7121. r->layer = css_cur_layer;
  7122. r->var_scope = scope_flags;
  7123. r->raw_off = scope_off;
  7124. r->raw_len = (unsigned short)(bend - bstart);
  7125. if (pe)
  7126. d->npe_rules++;
  7127. r->nd = (unsigned char)nd;
  7128. for (k = 0; k < nd; k++)
  7129. r->d[k] = decls[k];
  7130. rule_index_add(d, d->nrules - 1,
  7131. attr_ptr(d, r->key_off),
  7132. s_len(attr_ptr(d, r->key_off)));
  7133. }
  7134. }
  7135. }
  7136. }
  7137. /* Sites often set html { font-size: 62.5% } and then size everything in
  7138. * rem. Detect that root size before parsing the sheet so a rem resolves to
  7139. * the same pixels a real browser would use. */
  7140. static void detect_root_font(const char *s, unsigned int len)
  7141. {
  7142. unsigned int i;
  7143. for (i = 0; i + 4 < len; i++) {
  7144. unsigned int j, end, body, sel, q, h;
  7145. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  7146. int exact;
  7147. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  7148. s[i + 3] == 'l'))
  7149. continue;
  7150. h = i + 4;
  7151. while (h < len && is_space(s[h]))
  7152. h++;
  7153. if (h < len && s[h] != '{' && s[h] != ',' &&
  7154. s[h] != '.' && s[h] != ':' && s[h] != '[')
  7155. continue;
  7156. sel = i;
  7157. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  7158. s[sel - 1] != ';' && s[sel - 1] != ',')
  7159. sel--;
  7160. q = sel;
  7161. while (q < i && is_space(s[q]))
  7162. q++;
  7163. exact = (q == i);
  7164. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  7165. continue;
  7166. j = h;
  7167. if (j >= len || s[j] != '{')
  7168. continue;
  7169. while (j < len && s[j] != '{')
  7170. j++;
  7171. if (j >= len)
  7172. break;
  7173. end = j + 1;
  7174. body = end;
  7175. while (end < len && s[end] != '}')
  7176. end++;
  7177. for (; j + 9 < end; j++) {
  7178. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  7179. s[j + 3] == 't' && s[j + 4] == '-' &&
  7180. s[j + 5] == 's' && s[j + 6] == 'i' &&
  7181. s[j + 7] == 'z' && s[j + 8] == 'e'))
  7182. continue;
  7183. j += 9;
  7184. while (j < end && s[j] != ':')
  7185. j++;
  7186. if (j >= end)
  7187. break;
  7188. j++;
  7189. while (j < end && is_space(s[j]))
  7190. j++;
  7191. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7192. v = v * 10 + (s[j] - '0');
  7193. digits = 1;
  7194. j++;
  7195. }
  7196. if (j < end && s[j] == '.') {
  7197. j++;
  7198. while (j < end && s[j] >= '0' && s[j] <= '9') {
  7199. v = v * 10 + (s[j] - '0');
  7200. frac++;
  7201. j++;
  7202. }
  7203. }
  7204. while (frac < 2) {
  7205. v *= 10;
  7206. frac++;
  7207. }
  7208. while (frac > 2) {
  7209. v /= 10;
  7210. frac--;
  7211. }
  7212. if (!digits)
  7213. break;
  7214. if (j < end && s[j] == '%')
  7215. pct = 1;
  7216. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  7217. px = 1;
  7218. if (pct)
  7219. v = (v * 16 + 5000) / 10000;
  7220. else if (px)
  7221. v /= 100;
  7222. else
  7223. v = v * 16 / 100;
  7224. if (exact && v >= 8 && v <= 24)
  7225. root_font_px = v;
  7226. if (exact)
  7227. return;
  7228. }
  7229. /* html { font: 10px/1 Verdana } also sets the root size. */
  7230. for (j = body; j + 5 <= end; j++) {
  7231. unsigned int p, ts, te;
  7232. if (!ci_eq_n(s + j, "font:", 5))
  7233. continue;
  7234. p = j + 5;
  7235. while (next_token(s, end, &p, &ts, &te)) {
  7236. int fv = parse_font_px(s + ts, te - ts);
  7237. if (fv >= 8 && fv <= 24) {
  7238. if (exact)
  7239. root_font_px = fv;
  7240. break;
  7241. }
  7242. }
  7243. if (exact)
  7244. return;
  7245. break;
  7246. }
  7247. }
  7248. }
  7249. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  7250. {
  7251. scan_font_faces(css, len);
  7252. detect_root_font(css, len);
  7253. css_collect_only = 1;
  7254. parse_css(d, css, len);
  7255. css_collect_only = 0;
  7256. parse_css(d, css, len);
  7257. }
  7258. void br_parse_inline_css(struct br_doc *d)
  7259. {
  7260. if (d->inline_css_len) {
  7261. scan_font_faces(css_store, d->inline_css_len);
  7262. detect_root_font(css_store, d->inline_css_len);
  7263. css_collect_only = 1;
  7264. parse_css(d, css_store, d->inline_css_len);
  7265. css_collect_only = 0;
  7266. parse_css(d, css_store, d->inline_css_len);
  7267. }
  7268. d->inline_css_len = 0;
  7269. }
  7270. void br_reset_styles(struct br_doc *d)
  7271. {
  7272. unsigned int i;
  7273. for (i = 0; i < d->nnode; i++) {
  7274. d->nodes[i].style_valid = 0;
  7275. d->nodes[i].pe_state = 0;
  7276. }
  7277. }
  7278. /* ---- selector matching ---- */
  7279. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  7280. unsigned int clen)
  7281. {
  7282. const char *s = attr_ptr(d, d->nodes[node].class_off);
  7283. unsigned int i = 0;
  7284. while (s[i]) {
  7285. unsigned int st, n;
  7286. while (s[i] && is_space(s[i]))
  7287. i++;
  7288. st = i;
  7289. while (s[i] && !is_space(s[i]))
  7290. i++;
  7291. n = i - st;
  7292. if (n == clen && ci_eq_n(s + st, cls, n))
  7293. return 1;
  7294. }
  7295. return 0;
  7296. }
  7297. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  7298. unsigned int len)
  7299. {
  7300. const char *s = attr_ptr(d, d->nodes[node].id_off);
  7301. unsigned int i;
  7302. for (i = 0; i < len; i++)
  7303. if (s[i] != id[i])
  7304. return 0;
  7305. return s[len] == '\0';
  7306. }
  7307. static int is_ident(char c)
  7308. {
  7309. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  7310. (c >= '0' && c <= '9') || c == '-' || c == '_';
  7311. }
  7312. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  7313. {
  7314. unsigned int n = 0;
  7315. while (lit[n])
  7316. n++;
  7317. return n == nlen && ci_eq_n(name, lit, nlen);
  7318. }
  7319. static int attr_present(struct br_doc *d, unsigned int node,
  7320. const char *name, unsigned int nlen)
  7321. {
  7322. struct br_node *n = &d->nodes[node];
  7323. if (attr_is(name, nlen, "data-collapse-target"))
  7324. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  7325. if (attr_is(name, nlen, "data-collapse-trigger"))
  7326. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  7327. if (attr_is(name, nlen, "is-open"))
  7328. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  7329. if (attr_is(name, nlen, "disabled"))
  7330. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  7331. if (attr_is(name, nlen, "open"))
  7332. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  7333. if (attr_is(name, nlen, "inert"))
  7334. return (n->attr_flags & BR_ATTR_INERT) != 0;
  7335. if (attr_is(name, nlen, "hidden"))
  7336. return (n->flags & BR_NODE_HIDDEN) != 0;
  7337. if (attr_is(name, nlen, "href"))
  7338. return attr_ptr(d, n->href_off)[0] != 0;
  7339. if (attr_is(name, nlen, "class"))
  7340. return attr_ptr(d, n->class_off)[0] != 0;
  7341. if (attr_is(name, nlen, "id"))
  7342. return attr_ptr(d, n->id_off)[0] != 0;
  7343. if (attr_is(name, nlen, "src"))
  7344. return attr_ptr(d, n->src_off)[0] != 0;
  7345. if (attr_is(name, nlen, "alt"))
  7346. return attr_ptr(d, n->alt_off)[0] != 0;
  7347. if (attr_is(name, nlen, "rel"))
  7348. return attr_ptr(d, n->rel_off)[0] != 0;
  7349. return 0;
  7350. }
  7351. /* ---- Selector engine ----
  7352. *
  7353. * One matcher serves the cascade (rule_try), querySelector(All) and
  7354. * Element.matches. A compound is an optional type/universal selector
  7355. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  7356. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  7357. * '~' combinators and is matched right to left with backtracking.
  7358. *
  7359. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  7360. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  7361. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  7362. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  7363. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  7364. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  7365. * and pseudo-elements never match an element; the cascade strips
  7366. * ::before/::after before it gets here. */
  7367. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7368. unsigned int len);
  7369. /* i is at '(' or '['; returns the index just past the matching close. */
  7370. static unsigned int sel_skip_group(const char *s, unsigned int i,
  7371. unsigned int len)
  7372. {
  7373. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  7374. int depth = 0;
  7375. for (; i < len; i++) {
  7376. char c = s[i];
  7377. if (quote) {
  7378. if (c == '\\')
  7379. i++;
  7380. else if (c == quote)
  7381. quote = 0;
  7382. continue;
  7383. }
  7384. if (c == '\\')
  7385. i++;
  7386. else if (c == '"' || c == '\'')
  7387. quote = c;
  7388. else if (c == open)
  7389. depth++;
  7390. else if (c == close && --depth == 0)
  7391. return i + 1;
  7392. }
  7393. return len;
  7394. }
  7395. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  7396. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  7397. char *out, unsigned int cap, unsigned int *olen)
  7398. {
  7399. unsigned int n = 0;
  7400. while (i < len) {
  7401. char c = s[i];
  7402. if (c == '\\' && i + 1 < len) {
  7403. c = s[i + 1];
  7404. i += 2;
  7405. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  7406. i++;
  7407. } else {
  7408. break;
  7409. }
  7410. if (n + 1 < cap)
  7411. out[n++] = c;
  7412. }
  7413. out[n] = '\0';
  7414. *olen = n;
  7415. return i;
  7416. }
  7417. /* Next top-level item of a comma-separated selector list. */
  7418. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  7419. unsigned int *st, unsigned int *en)
  7420. {
  7421. unsigned int i = *pos;
  7422. while (i < len && (is_space(s[i]) || s[i] == ','))
  7423. i++;
  7424. if (i >= len)
  7425. return 0;
  7426. *st = i;
  7427. while (i < len && s[i] != ',') {
  7428. if (s[i] == '(' || s[i] == '[')
  7429. i = sel_skip_group(s, i, len);
  7430. else if (s[i] == '\\' && i + 1 < len)
  7431. i += 2;
  7432. else if (s[i] == '"' || s[i] == '\'') {
  7433. char q = s[i++];
  7434. while (i < len && s[i] != q)
  7435. i++;
  7436. i++;
  7437. } else
  7438. i++;
  7439. }
  7440. if (i > len)
  7441. i = len;
  7442. *en = i;
  7443. while (*en > *st && is_space(s[*en - 1]))
  7444. (*en)--;
  7445. *pos = i;
  7446. return 1;
  7447. }
  7448. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7449. unsigned int len)
  7450. {
  7451. unsigned int pos = 0, st, en;
  7452. while (sel_list_next(s, len, &pos, &st, &en))
  7453. if (sel_match(d, node, s + st, en - st))
  7454. return 1;
  7455. return 0;
  7456. }
  7457. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7458. {
  7459. if (d->nodes[a].tag != d->nodes[b].tag)
  7460. return 0;
  7461. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7462. return 1;
  7463. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7464. attr_ptr(d, d->nodes[b].name_off));
  7465. }
  7466. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7467. {
  7468. unsigned int c, prev = 0;
  7469. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7470. c = d->nodes[c].next)
  7471. if (d->nodes[c].type == BR_NODE_ELEM)
  7472. prev = c;
  7473. return c == node ? prev : 0;
  7474. }
  7475. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7476. {
  7477. unsigned int c;
  7478. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7479. if (d->nodes[c].type == BR_NODE_ELEM)
  7480. return c;
  7481. return 0;
  7482. }
  7483. /* 1-based position among the element siblings (of the same type), counted
  7484. * from the start or, with from_end, from the end. */
  7485. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7486. int from_end)
  7487. {
  7488. unsigned int c;
  7489. int pos = 0, total = 0;
  7490. for (c = d->nodes[d->nodes[node].parent].first; c;
  7491. c = d->nodes[c].next) {
  7492. if (d->nodes[c].type != BR_NODE_ELEM)
  7493. continue;
  7494. if (of_type && !sel_same_type(d, c, node))
  7495. continue;
  7496. total++;
  7497. if (c == node)
  7498. pos = total;
  7499. }
  7500. if (!pos)
  7501. return 0;
  7502. return from_end ? total - pos + 1 : pos;
  7503. }
  7504. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7505. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7506. {
  7507. unsigned int i = 0;
  7508. int sign = 1, num = 0, digits = 0;
  7509. while (len && is_space(s[len - 1]))
  7510. len--;
  7511. while (i < len && is_space(s[i]))
  7512. i++;
  7513. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7514. *a = 2;
  7515. *b = 1;
  7516. return 1;
  7517. }
  7518. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7519. *a = 2;
  7520. *b = 0;
  7521. return 1;
  7522. }
  7523. if (i < len && (s[i] == '+' || s[i] == '-'))
  7524. sign = s[i++] == '-' ? -1 : 1;
  7525. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7526. num = num * 10 + (s[i++] - '0');
  7527. digits = 1;
  7528. }
  7529. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7530. *a = sign * (digits ? num : 1);
  7531. *b = 0;
  7532. i++;
  7533. while (i < len && is_space(s[i]))
  7534. i++;
  7535. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7536. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7537. while (i < len && is_space(s[i]))
  7538. i++;
  7539. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7540. bv = bv * 10 + (s[i++] - '0');
  7541. bd = 1;
  7542. }
  7543. if (!bd)
  7544. return 0;
  7545. *b = bs * bv;
  7546. }
  7547. } else {
  7548. if (!digits)
  7549. return 0;
  7550. *a = 0;
  7551. *b = sign * num;
  7552. }
  7553. return i == len;
  7554. }
  7555. static int sel_nth_ok(int a, int b, int pos)
  7556. {
  7557. if (pos <= 0)
  7558. return 0;
  7559. if (!a)
  7560. return pos == b;
  7561. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7562. }
  7563. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7564. const char *name, int op, const char *want,
  7565. unsigned int wl, int icase)
  7566. {
  7567. const char *have = br_find_attr(d, node, name);
  7568. unsigned int n, i;
  7569. if (!have) {
  7570. /* Boolean attributes the parser keeps as flags. */
  7571. if (!attr_present(d, node, name, text_len(name)))
  7572. return 0;
  7573. have = "";
  7574. }
  7575. if (!op)
  7576. return 1;
  7577. n = text_len(have);
  7578. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7579. switch (op) {
  7580. case '=':
  7581. return n == wl && SEL_EQ(have, want, wl);
  7582. case '^':
  7583. return wl && n >= wl && SEL_EQ(have, want, wl);
  7584. case '$':
  7585. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7586. case '*':
  7587. if (!wl)
  7588. return 0;
  7589. for (i = 0; i + wl <= n; i++)
  7590. if (SEL_EQ(have + i, want, wl))
  7591. return 1;
  7592. return 0;
  7593. case '|':
  7594. return n >= wl && SEL_EQ(have, want, wl) &&
  7595. (n == wl || have[wl] == '-');
  7596. case '~':
  7597. if (!wl)
  7598. return 0;
  7599. for (i = 0; i < n; ) {
  7600. unsigned int st;
  7601. while (i < n && is_space(have[i]))
  7602. i++;
  7603. st = i;
  7604. while (i < n && !is_space(have[i]))
  7605. i++;
  7606. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7607. return 1;
  7608. }
  7609. return 0;
  7610. }
  7611. #undef SEL_EQ
  7612. return 0;
  7613. }
  7614. static int sel_empty(struct br_doc *d, unsigned int node)
  7615. {
  7616. unsigned int c;
  7617. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7618. if (d->nodes[c].type == BR_NODE_ELEM)
  7619. return 0;
  7620. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7621. return 0;
  7622. }
  7623. return 1;
  7624. }
  7625. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7626. unsigned int len)
  7627. {
  7628. unsigned int c;
  7629. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7630. if (d->nodes[c].type != BR_NODE_ELEM)
  7631. continue;
  7632. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7633. return 1;
  7634. }
  7635. return 0;
  7636. }
  7637. /* :has() with an optional leading relative combinator per list item. */
  7638. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7639. unsigned int len)
  7640. {
  7641. unsigned int pos = 0, st, en, c;
  7642. while (sel_list_next(s, len, &pos, &st, &en)) {
  7643. char comb = s[st];
  7644. if (comb == '>' || comb == '+' || comb == '~') {
  7645. st++;
  7646. while (st < en && is_space(s[st]))
  7647. st++;
  7648. } else {
  7649. comb = ' ';
  7650. }
  7651. if (st >= en)
  7652. continue;
  7653. if (comb == '>') {
  7654. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7655. if (d->nodes[c].type == BR_NODE_ELEM &&
  7656. sel_match(d, c, s + st, en - st))
  7657. return 1;
  7658. } else if (comb == '+') {
  7659. c = sel_next_elem(d, node);
  7660. if (c && sel_match(d, c, s + st, en - st))
  7661. return 1;
  7662. } else if (comb == '~') {
  7663. for (c = sel_next_elem(d, node); c;
  7664. c = sel_next_elem(d, c))
  7665. if (sel_match(d, c, s + st, en - st))
  7666. return 1;
  7667. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7668. return 1;
  7669. }
  7670. }
  7671. return 0;
  7672. }
  7673. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7674. {
  7675. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7676. }
  7677. /* Pseudo-class names the engine models; anything else never matches and
  7678. * makes a top-level rule unsupported (see css_selector_supported()). */
  7679. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7680. {
  7681. static const char *const plain[] = {
  7682. "root", "link", "any-link", "visited", "first-child",
  7683. "last-child", "only-child", "first-of-type", "last-of-type",
  7684. "only-of-type", "empty", "checked", "disabled", "enabled",
  7685. "required", "optional",
  7686. };
  7687. static const char *const func[] = {
  7688. "not", "is", "where", "matches", "-webkit-any", "has",
  7689. "nth-child", "nth-last-child", "nth-of-type",
  7690. "nth-last-of-type",
  7691. };
  7692. unsigned int k;
  7693. if (fn) {
  7694. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7695. if (sel_name_is(name, nl, func[k]))
  7696. return 1;
  7697. return 0;
  7698. }
  7699. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7700. if (sel_name_is(name, nl, plain[k]))
  7701. return 1;
  7702. return 0;
  7703. }
  7704. static int sel_form_control(struct br_doc *d, unsigned int node)
  7705. {
  7706. unsigned int t = d->nodes[node].tag;
  7707. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7708. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7709. t == BR_TAG_OPTION;
  7710. }
  7711. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7712. unsigned int nl, const char *arg, unsigned int al,
  7713. int fn)
  7714. {
  7715. struct br_node *n = &d->nodes[node];
  7716. int a, b;
  7717. if (fn) {
  7718. if (sel_name_is(name, nl, "not"))
  7719. return !sel_list_match(d, node, arg, al);
  7720. if (sel_name_is(name, nl, "is") ||
  7721. sel_name_is(name, nl, "where") ||
  7722. sel_name_is(name, nl, "matches") ||
  7723. sel_name_is(name, nl, "-webkit-any"))
  7724. return sel_list_match(d, node, arg, al);
  7725. if (sel_name_is(name, nl, "has"))
  7726. return sel_has(d, node, arg, al);
  7727. if (!sel_nth_parse(arg, al, &a, &b))
  7728. return 0;
  7729. if (sel_name_is(name, nl, "nth-child"))
  7730. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7731. if (sel_name_is(name, nl, "nth-last-child"))
  7732. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7733. if (sel_name_is(name, nl, "nth-of-type"))
  7734. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7735. if (sel_name_is(name, nl, "nth-last-of-type"))
  7736. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7737. return 0;
  7738. }
  7739. if (sel_name_is(name, nl, "root"))
  7740. return n->tag == BR_TAG_HTML;
  7741. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7742. /* No history state: every link is unvisited. */
  7743. return n->tag == BR_TAG_A && n->href_off;
  7744. if (sel_name_is(name, nl, "first-child"))
  7745. return sel_sibling_pos(d, node, 0, 0) == 1;
  7746. if (sel_name_is(name, nl, "last-child"))
  7747. return sel_sibling_pos(d, node, 0, 1) == 1;
  7748. if (sel_name_is(name, nl, "only-child"))
  7749. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7750. sel_sibling_pos(d, node, 0, 1) == 1;
  7751. if (sel_name_is(name, nl, "first-of-type"))
  7752. return sel_sibling_pos(d, node, 1, 0) == 1;
  7753. if (sel_name_is(name, nl, "last-of-type"))
  7754. return sel_sibling_pos(d, node, 1, 1) == 1;
  7755. if (sel_name_is(name, nl, "only-of-type"))
  7756. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7757. sel_sibling_pos(d, node, 1, 1) == 1;
  7758. if (sel_name_is(name, nl, "empty"))
  7759. return sel_empty(d, node);
  7760. if (sel_name_is(name, nl, "checked"))
  7761. return br_find_attr(d, node, "checked") != 0 ||
  7762. (n->tag == BR_TAG_OPTION &&
  7763. br_find_attr(d, node, "selected") != 0);
  7764. if (sel_name_is(name, nl, "disabled"))
  7765. return sel_form_control(d, node) &&
  7766. (n->attr_flags & BR_ATTR_DISABLED);
  7767. if (sel_name_is(name, nl, "enabled"))
  7768. return sel_form_control(d, node) &&
  7769. !(n->attr_flags & BR_ATTR_DISABLED);
  7770. if (sel_name_is(name, nl, "required"))
  7771. return sel_form_control(d, node) &&
  7772. br_find_attr(d, node, "required") != 0;
  7773. if (sel_name_is(name, nl, "optional"))
  7774. return sel_form_control(d, node) &&
  7775. !br_find_attr(d, node, "required");
  7776. return 0;
  7777. }
  7778. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7779. unsigned int len)
  7780. {
  7781. char buf[64];
  7782. unsigned int i = 0, bl;
  7783. if (d->nodes[node].type != BR_NODE_ELEM)
  7784. return 0;
  7785. if (i < len && s[i] == '*') {
  7786. i++;
  7787. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7788. unsigned int tag;
  7789. i = sel_ident(s, i, len, buf, 32, &bl);
  7790. tag = br_tag_from_name(buf);
  7791. if (tag == BR_TAG_UNKNOWN) {
  7792. const char *raw = attr_ptr(d,
  7793. d->nodes[node].name_off);
  7794. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7795. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7796. return 0;
  7797. } else if (tag != d->nodes[node].tag) {
  7798. return 0;
  7799. }
  7800. }
  7801. if (i >= len)
  7802. return i > 0;
  7803. while (i < len) {
  7804. char c = s[i];
  7805. if (c == '.' || c == '#') {
  7806. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7807. if (!bl)
  7808. return 0;
  7809. if (c == '.' ? !class_has(d, node, buf, bl) :
  7810. !id_eq(d, node, buf, bl))
  7811. return 0;
  7812. } else if (c == '[') {
  7813. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7814. unsigned int vs = 0, vl = 0, k;
  7815. char name[32];
  7816. int op = 0, icase = 0;
  7817. while (j < e && is_space(s[j]))
  7818. j++;
  7819. k = 0;
  7820. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7821. k + 1 < sizeof(name)) {
  7822. char ch = s[j++];
  7823. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7824. (char)(ch + 32) : ch;
  7825. }
  7826. name[k] = '\0';
  7827. if (!k)
  7828. return 0;
  7829. while (j < e && is_space(s[j]))
  7830. j++;
  7831. if (j < e && s[j] == '=') {
  7832. op = '=';
  7833. j++;
  7834. } else if (j + 1 < e && s[j + 1] == '=' &&
  7835. (s[j] == '~' || s[j] == '|' ||
  7836. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7837. op = s[j];
  7838. j += 2;
  7839. }
  7840. if (op) {
  7841. while (j < e && is_space(s[j]))
  7842. j++;
  7843. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7844. char q = s[j++];
  7845. vs = j;
  7846. while (j < e && s[j] != q)
  7847. j++;
  7848. vl = j - vs;
  7849. j++;
  7850. } else {
  7851. vs = j;
  7852. while (j < e && s[j] != ']' &&
  7853. !is_space(s[j]))
  7854. j++;
  7855. vl = j - vs;
  7856. }
  7857. while (j < e && is_space(s[j]))
  7858. j++;
  7859. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7860. icase = 1;
  7861. }
  7862. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7863. icase))
  7864. return 0;
  7865. i = e;
  7866. } else if (c == ':') {
  7867. unsigned int st, nl, as = 0, al = 0;
  7868. int fn = 0;
  7869. if (i + 1 < len && s[i + 1] == ':')
  7870. return 0; /* pseudo-element */
  7871. st = ++i;
  7872. while (i < len && is_ident(s[i]))
  7873. i++;
  7874. nl = i - st;
  7875. if (i < len && s[i] == '(') {
  7876. unsigned int e = sel_skip_group(s, i, len);
  7877. fn = 1;
  7878. as = i + 1;
  7879. al = e > as ? e - as - 1 : 0;
  7880. i = e;
  7881. }
  7882. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7883. return 0;
  7884. } else {
  7885. return 0;
  7886. }
  7887. }
  7888. return 1;
  7889. }
  7890. /* Split a complex selector into compounds; returns the count or -1. */
  7891. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7892. {
  7893. unsigned int i = 0;
  7894. int n = 0;
  7895. char comb = 0;
  7896. while (i < len && is_space(s[i]))
  7897. i++;
  7898. while (i < len) {
  7899. unsigned int st = i;
  7900. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7901. s[i] != '+' && s[i] != '~') {
  7902. if (s[i] == '(' || s[i] == '[')
  7903. i = sel_skip_group(s, i, len);
  7904. else if (s[i] == '\\' && i + 1 < len)
  7905. i += 2;
  7906. else
  7907. i++;
  7908. }
  7909. if (i == st || n == SEL_PARTS)
  7910. return -1;
  7911. p[n].st = (unsigned short)st;
  7912. p[n].en = (unsigned short)(i > len ? len : i);
  7913. p[n].comb = comb;
  7914. n++;
  7915. comb = 0;
  7916. while (i < len && is_space(s[i])) {
  7917. comb = ' ';
  7918. i++;
  7919. }
  7920. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7921. comb = s[i++];
  7922. while (i < len && is_space(s[i]))
  7923. i++;
  7924. if (i >= len)
  7925. return -1;
  7926. }
  7927. }
  7928. return n;
  7929. }
  7930. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7931. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7932. * parent when the page starts with <html> (node 0 is that element; see
  7933. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7934. static int sel_parent(struct br_doc *d, unsigned int node)
  7935. {
  7936. unsigned int p = d->nodes[node].parent;
  7937. if (p)
  7938. return (int)p;
  7939. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7940. d->nodes[0].tag == BR_TAG_HTML &&
  7941. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7942. return 0;
  7943. return -1;
  7944. }
  7945. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7946. const struct sel_part *p, int k)
  7947. {
  7948. unsigned int c, pl;
  7949. const char *ps;
  7950. if (k == 0)
  7951. return 1;
  7952. ps = s + p[k - 1].st;
  7953. pl = p[k - 1].en - p[k - 1].st;
  7954. switch (p[k].comb) {
  7955. case '>': {
  7956. int pc = sel_parent(d, node);
  7957. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7958. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7959. }
  7960. case '+':
  7961. c = sel_prev_elem(d, node);
  7962. return c && compound_match(d, c, ps, pl) &&
  7963. sel_from(d, c, s, p, k - 1);
  7964. case '~':
  7965. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7966. if (compound_match(d, c, ps, pl) &&
  7967. sel_from(d, c, s, p, k - 1))
  7968. return 1;
  7969. return 0;
  7970. default: {
  7971. int pc;
  7972. for (pc = sel_parent(d, node); pc >= 0;
  7973. pc = sel_parent(d, (unsigned int)pc))
  7974. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7975. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7976. return 1;
  7977. return 0;
  7978. }
  7979. }
  7980. }
  7981. /* Match one complex selector. right_done: the caller already matched the
  7982. * rightmost compound (the rule index key). */
  7983. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7984. unsigned int len, int right_done)
  7985. {
  7986. struct sel_part parts[SEL_PARTS];
  7987. int np = sel_split(s, len, parts);
  7988. if (np <= 0)
  7989. return 0;
  7990. if (!right_done &&
  7991. !compound_match(d, node, s + parts[np - 1].st,
  7992. parts[np - 1].en - parts[np - 1].st))
  7993. return 0;
  7994. return sel_from(d, node, s, parts, np - 1);
  7995. }
  7996. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7997. unsigned int len)
  7998. {
  7999. return selector_match(d, node, s, len, 0);
  8000. }
  8001. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  8002. * types/pseudo-elements); :is/:not/:has take their most specific argument
  8003. * and :where counts nothing. */
  8004. static unsigned int selector_specificity(const char *s, unsigned int len)
  8005. {
  8006. unsigned int i = 0, spec = 0, bl;
  8007. char buf[64];
  8008. while (i < len) {
  8009. char c = s[i];
  8010. if (c == '#' || c == '.') {
  8011. spec += c == '#' ? 100 : 10;
  8012. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  8013. } else if (c == '[') {
  8014. spec += 10;
  8015. i = sel_skip_group(s, i, len);
  8016. } else if (c == ':') {
  8017. unsigned int st, nl;
  8018. if (i + 1 < len && s[i + 1] == ':') {
  8019. spec += 1;
  8020. i += 2;
  8021. while (i < len && is_ident(s[i]))
  8022. i++;
  8023. continue;
  8024. }
  8025. st = ++i;
  8026. while (i < len && is_ident(s[i]))
  8027. i++;
  8028. nl = i - st;
  8029. if (i < len && s[i] == '(') {
  8030. unsigned int e = sel_skip_group(s, i, len);
  8031. if (sel_name_is(s + st, nl, "where")) {
  8032. /* zero */
  8033. } else if (sel_name_is(s + st, nl, "not") ||
  8034. sel_name_is(s + st, nl, "is") ||
  8035. sel_name_is(s + st, nl, "has") ||
  8036. sel_name_is(s + st, nl, "matches") ||
  8037. sel_name_is(s + st, nl,
  8038. "-webkit-any")) {
  8039. unsigned int pos = 0, as, ae, best = 0;
  8040. unsigned int al = e > i + 1 ?
  8041. e - i - 2 : 0;
  8042. while (sel_list_next(s + i + 1, al,
  8043. &pos, &as, &ae)) {
  8044. unsigned int v =
  8045. selector_specificity(
  8046. s + i + 1 + as,
  8047. ae - as);
  8048. if (v > best)
  8049. best = v;
  8050. }
  8051. spec += best;
  8052. } else {
  8053. spec += 10;
  8054. }
  8055. i = e;
  8056. } else if (sel_name_is(s + st, nl, "before") ||
  8057. sel_name_is(s + st, nl, "after")) {
  8058. spec += 1;
  8059. } else {
  8060. spec += 10;
  8061. }
  8062. } else if (is_ident(c) || c == '\\') {
  8063. spec += 1;
  8064. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  8065. } else {
  8066. i++;
  8067. }
  8068. }
  8069. return spec;
  8070. }
  8071. /* Cascade filter: drop a selector the engine cannot match (user-state
  8072. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  8073. * ones the caller handles), so it does not waste a rule slot. Arguments of
  8074. * functional pseudo-classes are not checked: an unknown pseudo-class there
  8075. * is simply false, which is right for :not(:hover). */
  8076. static int css_selector_supported(const char *s, unsigned int len)
  8077. {
  8078. struct sel_part parts[SEL_PARTS];
  8079. unsigned int i = 0;
  8080. if (sel_split(s, len, parts) <= 0)
  8081. return 0;
  8082. while (i < len) {
  8083. if (s[i] == '(' || s[i] == '[') {
  8084. i = sel_skip_group(s, i, len);
  8085. } else if (s[i] == '\\') {
  8086. i += 2;
  8087. } else if (s[i] == ':') {
  8088. unsigned int st, nl;
  8089. int fn;
  8090. if (i + 1 < len && s[i + 1] == ':')
  8091. return 0;
  8092. st = ++i;
  8093. while (i < len && is_ident(s[i]))
  8094. i++;
  8095. nl = i - st;
  8096. fn = i < len && s[i] == '(';
  8097. if (!sel_pseudo_known(s + st, nl, fn))
  8098. return 0;
  8099. } else {
  8100. i++;
  8101. }
  8102. }
  8103. return 1;
  8104. }
  8105. #define RULE_MATCH_MAX 512
  8106. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  8107. static int cascade_pe;
  8108. struct rule_match {
  8109. unsigned short idx;
  8110. short spec; /* precedence of normal declarations */
  8111. short spec_imp; /* precedence of !important declarations */
  8112. };
  8113. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  8114. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  8115. * unlayered rules, then the style attribute. !important reverses the
  8116. * layer order: unlayered lowest, the first layer highest, the style
  8117. * attribute above all. Both fit a short. */
  8118. #define CASCADE_BAND 512
  8119. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  8120. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  8121. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  8122. {
  8123. unsigned int band;
  8124. if (spec >= CASCADE_BAND)
  8125. spec = CASCADE_BAND - 1;
  8126. if (important)
  8127. band = layer ? CASCADE_UNLAYERED - layer : 0;
  8128. else
  8129. band = layer ? layer : CASCADE_UNLAYERED;
  8130. return (int)(band * CASCADE_BAND + spec);
  8131. }
  8132. static struct rule_match rule_matches[RULE_MATCH_MAX];
  8133. static unsigned int nrule_matches;
  8134. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  8135. {
  8136. struct br_rule *r = &d->rules[idx];
  8137. const char *key = attr_ptr(d, r->key_off);
  8138. int spec, key_ok = 0;
  8139. if (r->pe != cascade_pe)
  8140. return;
  8141. if (key[0]) {
  8142. if (!compound_match(d, node, key, s_len(key)))
  8143. return;
  8144. key_ok = 1;
  8145. }
  8146. if (!(r->single && key_ok) &&
  8147. !selector_match(d, node, attr_ptr(d, r->sel_off),
  8148. s_len(attr_ptr(d, r->sel_off)), key_ok))
  8149. return;
  8150. spec = (int)r->spec;
  8151. if (nrule_matches < RULE_MATCH_MAX) {
  8152. rule_matches[nrule_matches].idx = (unsigned short)idx;
  8153. rule_matches[nrule_matches].spec =
  8154. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  8155. rule_matches[nrule_matches].spec_imp =
  8156. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  8157. nrule_matches++;
  8158. }
  8159. }
  8160. static void rule_scan_list(struct br_doc *d, unsigned int node,
  8161. unsigned short head)
  8162. {
  8163. while (head != 0xFFFF) {
  8164. rule_try(d, node, head);
  8165. head = d->rule_next[head];
  8166. }
  8167. }
  8168. static void rule_scan_class(struct br_doc *d, unsigned int node,
  8169. const char *cls, unsigned int len)
  8170. {
  8171. if (len)
  8172. rule_scan_list(d, node,
  8173. d->rule_bucket[rule_hash(cls, len)]);
  8174. }
  8175. static void rule_collect(struct br_doc *d, unsigned int node)
  8176. {
  8177. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  8178. const char *id = attr_ptr(d, d->nodes[node].id_off);
  8179. unsigned int tag = d->nodes[node].tag;
  8180. unsigned int i;
  8181. nrule_matches = 0;
  8182. rule_scan_list(d, node, d->rule_any);
  8183. if (tag < BR_TAG_MAX)
  8184. rule_scan_list(d, node, d->rule_tag[tag]);
  8185. if (id[0])
  8186. rule_scan_list(d, node,
  8187. d->rule_bucket[rule_hash(id, s_len(id))]);
  8188. i = 0;
  8189. while (cls[i]) {
  8190. unsigned int st, n;
  8191. while (cls[i] && is_space(cls[i]))
  8192. i++;
  8193. st = i;
  8194. while (cls[i] && !is_space(cls[i]))
  8195. i++;
  8196. n = i - st;
  8197. if (n)
  8198. rule_scan_class(d, node, cls + st, n);
  8199. }
  8200. /* Rules must cascade in document order even though the index visits
  8201. * them in bucket order. */
  8202. for (i = 1; i < nrule_matches; i++) {
  8203. struct rule_match m = rule_matches[i];
  8204. unsigned int j = i;
  8205. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  8206. rule_matches[j] = rule_matches[j - 1];
  8207. j--;
  8208. }
  8209. rule_matches[j] = m;
  8210. }
  8211. }
  8212. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  8213. int font_px)
  8214. {
  8215. if (*kind == 4) {
  8216. *val = (unsigned short)(*val * font_px / 100);
  8217. *kind = 1;
  8218. } else if (*kind == 5) {
  8219. *val = (unsigned short)(*val * root_font_px / 100);
  8220. *kind = 1;
  8221. }
  8222. }
  8223. static void resolve_size_units(struct br_style *st)
  8224. {
  8225. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  8226. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  8227. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  8228. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  8229. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  8230. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  8231. }
  8232. static void set_border_side_w(struct br_style *st, int side, int v)
  8233. {
  8234. if (v < 0)
  8235. v = 0;
  8236. if (v > 32)
  8237. v = 32;
  8238. st->border_w[side] = (unsigned char)v;
  8239. if (st->border_w[side] && !st->border_c[side])
  8240. st->border_c[side] = st->color;
  8241. }
  8242. static void set_border_side_c(struct br_style *st, int side, int v)
  8243. {
  8244. st->border_c[side] = (unsigned short)v;
  8245. }
  8246. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  8247. {
  8248. switch (dcl->prop) {
  8249. case PROP_COLOR:
  8250. st->color = (unsigned short)dcl->val;
  8251. break;
  8252. case PROP_BG:
  8253. st->bg = (unsigned short)dcl->val;
  8254. st->has_bg = 1;
  8255. st->bg_set = 1;
  8256. break;
  8257. case PROP_BG_NONE:
  8258. st->has_bg = 0;
  8259. st->bg_set = 1;
  8260. break;
  8261. case PROP_BG_IMAGE:
  8262. st->bg_image = (unsigned short)dcl->val;
  8263. break;
  8264. case PROP_BG_REPEAT:
  8265. st->bg_repeat = (unsigned char)dcl->val;
  8266. break;
  8267. case PROP_BG_POS_X_KIND:
  8268. st->bg_pos_x_kind = (unsigned char)dcl->val;
  8269. break;
  8270. case PROP_BG_POS_X:
  8271. st->bg_pos_x = (signed short)dcl->val;
  8272. break;
  8273. case PROP_BG_POS_Y_KIND:
  8274. st->bg_pos_y_kind = (unsigned char)dcl->val;
  8275. break;
  8276. case PROP_BG_POS_Y:
  8277. st->bg_pos_y = (signed short)dcl->val;
  8278. break;
  8279. case PROP_BG_SIZE_KIND:
  8280. st->bg_size_kind = (unsigned char)dcl->val;
  8281. break;
  8282. case PROP_BG_SIZE_W:
  8283. st->bg_size_w = (unsigned short)dcl->val;
  8284. break;
  8285. case PROP_BG_SIZE_H:
  8286. st->bg_size_h = (unsigned short)dcl->val;
  8287. break;
  8288. case PROP_FONT_PX:
  8289. st->font_raw = (unsigned short)dcl->val;
  8290. break;
  8291. case PROP_FONT_UNIT:
  8292. st->font_unit = (unsigned char)dcl->val;
  8293. break;
  8294. case PROP_BOLD:
  8295. st->bold = (unsigned char)(dcl->val != 0);
  8296. break;
  8297. case PROP_ITALIC:
  8298. st->italic = (unsigned char)(dcl->val != 0);
  8299. break;
  8300. case PROP_MONO:
  8301. st->mono = (unsigned char)(dcl->val != 0);
  8302. break;
  8303. case PROP_FONT_FACE:
  8304. st->font_face = (unsigned char)dcl->val;
  8305. break;
  8306. case PROP_ALIGN:
  8307. st->align = (unsigned char)dcl->val;
  8308. break;
  8309. case PROP_UNDERLINE:
  8310. st->underline = (unsigned char)(dcl->val != 0);
  8311. break;
  8312. case PROP_STRIKE:
  8313. st->strike = (unsigned char)(dcl->val != 0);
  8314. break;
  8315. case PROP_OVERLINE:
  8316. st->overline = (unsigned char)(dcl->val != 0);
  8317. break;
  8318. case PROP_DISPLAY:
  8319. st->display = (unsigned char)(dcl->val &
  8320. ~BR_DISPLAY_INLINE_FLAG);
  8321. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  8322. st->list_item = 0;
  8323. break;
  8324. case PROP_VIS_HIDDEN:
  8325. st->vis_hidden = (unsigned char)(dcl->val != 0);
  8326. break;
  8327. case PROP_OPACITY_HIDDEN:
  8328. st->opacity = (unsigned char)(255 - (dcl->val & 255));
  8329. st->opacity_hidden = (unsigned char)(st->opacity == 0);
  8330. break;
  8331. case PROP_FILL_MODE:
  8332. st->fill_mode = (unsigned char)dcl->val;
  8333. break;
  8334. case PROP_FILL:
  8335. st->fill = (unsigned short)dcl->val;
  8336. break;
  8337. case PROP_STROKE_MODE:
  8338. st->stroke_mode = (unsigned char)dcl->val;
  8339. break;
  8340. case PROP_STROKE:
  8341. st->stroke = (unsigned short)dcl->val;
  8342. break;
  8343. case PROP_STROKE_W:
  8344. st->stroke_w = (unsigned short)dcl->val;
  8345. break;
  8346. case PROP_FILL_RULE:
  8347. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  8348. break;
  8349. case PROP_OVERFLOW_HIDDEN:
  8350. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  8351. break;
  8352. case PROP_MT: st->mt = dcl->val; break;
  8353. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  8354. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  8355. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  8356. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  8357. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  8358. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  8359. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  8360. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  8361. case PROP_MR: st->mr = dcl->val; break;
  8362. case PROP_MB: st->mb = dcl->val; break;
  8363. case PROP_ML: st->ml = dcl->val; break;
  8364. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  8365. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  8366. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  8367. break;
  8368. case PROP_PT: st->pt = dcl->val; break;
  8369. case PROP_PR: st->pr = dcl->val; break;
  8370. case PROP_PB: st->pb = dcl->val; break;
  8371. case PROP_PL: st->pl = dcl->val; break;
  8372. case PROP_MT_U:
  8373. case PROP_MR_U:
  8374. case PROP_MB_U:
  8375. case PROP_ML_U:
  8376. case PROP_PT_U:
  8377. case PROP_PR_U:
  8378. case PROP_PB_U:
  8379. case PROP_PL_U:
  8380. st->mp_unit[dcl->prop - PROP_MT_U] =
  8381. (unsigned char)dcl->val;
  8382. break;
  8383. case PROP_BORDER_W:
  8384. set_border_side_w(st, 0, dcl->val);
  8385. set_border_side_w(st, 1, dcl->val);
  8386. set_border_side_w(st, 2, dcl->val);
  8387. set_border_side_w(st, 3, dcl->val);
  8388. break;
  8389. case PROP_BORDER_COLOR:
  8390. set_border_side_c(st, 0, dcl->val);
  8391. set_border_side_c(st, 1, dcl->val);
  8392. set_border_side_c(st, 2, dcl->val);
  8393. set_border_side_c(st, 3, dcl->val);
  8394. break;
  8395. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  8396. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  8397. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  8398. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  8399. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  8400. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  8401. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  8402. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  8403. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  8404. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  8405. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  8406. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  8407. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  8408. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  8409. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  8410. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  8411. case PROP_RADIUS_TL:
  8412. case PROP_RADIUS_TR:
  8413. case PROP_RADIUS_BR:
  8414. case PROP_RADIUS_BL:
  8415. st->radius[dcl->prop - PROP_RADIUS_TL] =
  8416. (unsigned char)dcl->val;
  8417. break;
  8418. case PROP_LIST_NONE:
  8419. st->no_bullet = (unsigned char)(dcl->val != 0);
  8420. break;
  8421. case PROP_LIST_STYLE:
  8422. st->list_style = (unsigned char)dcl->val;
  8423. break;
  8424. case PROP_CONTENT:
  8425. st->content = (unsigned short)dcl->val;
  8426. break;
  8427. case PROP_ASPECT_W:
  8428. st->aspect_w = (unsigned short)dcl->val;
  8429. break;
  8430. case PROP_ASPECT_H:
  8431. st->aspect_h = (unsigned short)dcl->val;
  8432. break;
  8433. case PROP_NOWRAP:
  8434. st->nowrap = (unsigned char)(dcl->val != 0);
  8435. break;
  8436. case PROP_WIDTH:
  8437. st->width = (unsigned short)dcl->val;
  8438. break;
  8439. case PROP_WIDTH_KIND:
  8440. st->width_kind = (unsigned char)dcl->val;
  8441. break;
  8442. case PROP_WIDTH_CALC_PCT:
  8443. st->width_calc_pct = dcl->val;
  8444. break;
  8445. case PROP_WIDTH_CALC_PX:
  8446. st->width_calc_px = dcl->val;
  8447. break;
  8448. case PROP_MAXW:
  8449. st->max_width = (unsigned short)dcl->val;
  8450. break;
  8451. case PROP_MAXW_KIND:
  8452. st->max_width_kind = (unsigned char)dcl->val;
  8453. break;
  8454. case PROP_MAXW_CALC_PCT:
  8455. st->max_width_calc_pct = (signed short)dcl->val;
  8456. break;
  8457. case PROP_MAXW_CALC_PX:
  8458. st->max_width_calc_px = (signed short)dcl->val;
  8459. break;
  8460. case PROP_MINW:
  8461. st->min_width = (unsigned short)dcl->val;
  8462. break;
  8463. case PROP_MINW_KIND:
  8464. st->min_width_kind = (unsigned char)dcl->val;
  8465. break;
  8466. case PROP_MINW_CALC_PCT:
  8467. st->min_width_calc_pct = (signed short)dcl->val;
  8468. break;
  8469. case PROP_MINW_CALC_PX:
  8470. st->min_width_calc_px = (signed short)dcl->val;
  8471. break;
  8472. case PROP_HEIGHT:
  8473. st->height = (unsigned short)dcl->val;
  8474. break;
  8475. case PROP_HEIGHT_KIND:
  8476. st->height_kind = (unsigned char)dcl->val;
  8477. break;
  8478. case PROP_HEIGHT_CALC_PCT:
  8479. st->height_calc_pct = (signed short)dcl->val;
  8480. break;
  8481. case PROP_HEIGHT_CALC_PX:
  8482. st->height_calc_px = (signed short)dcl->val;
  8483. break;
  8484. case PROP_MINH:
  8485. st->min_height = (unsigned short)dcl->val;
  8486. break;
  8487. case PROP_MINH_KIND:
  8488. st->min_height_kind = (unsigned char)dcl->val;
  8489. break;
  8490. case PROP_MINH_CALC_PCT:
  8491. st->min_height_calc_pct = (signed short)dcl->val;
  8492. break;
  8493. case PROP_MINH_CALC_PX:
  8494. st->min_height_calc_px = (signed short)dcl->val;
  8495. break;
  8496. case PROP_MAXH:
  8497. st->max_height = (unsigned short)dcl->val;
  8498. break;
  8499. case PROP_MAXH_KIND:
  8500. st->max_height_kind = (unsigned char)dcl->val;
  8501. break;
  8502. case PROP_MAXH_CALC_PCT:
  8503. st->max_height_calc_pct = (signed short)dcl->val;
  8504. break;
  8505. case PROP_MAXH_CALC_PX:
  8506. st->max_height_calc_px = (signed short)dcl->val;
  8507. break;
  8508. case PROP_LH_PCT:
  8509. st->line_h_pct = (unsigned char)dcl->val;
  8510. break;
  8511. case PROP_LH_PX:
  8512. st->line_h_px = (unsigned short)dcl->val;
  8513. break;
  8514. case PROP_UPPER:
  8515. st->uppercase = (unsigned char)(dcl->val != 0);
  8516. break;
  8517. case PROP_CURSOR:
  8518. st->cursor = (unsigned char)dcl->val;
  8519. break;
  8520. case PROP_PE_NONE:
  8521. st->pe_none = (unsigned char)(dcl->val != 0);
  8522. break;
  8523. case PROP_TX_PCT:
  8524. st->tx_pct = (unsigned char)(dcl->val != 0);
  8525. break;
  8526. case PROP_TY_PCT:
  8527. st->ty_pct = (unsigned char)(dcl->val != 0);
  8528. break;
  8529. case PROP_TR_MASK:
  8530. st->tr_mask = (unsigned char)dcl->val;
  8531. break;
  8532. case PROP_TR_MS:
  8533. st->tr_ms = (unsigned short)dcl->val;
  8534. break;
  8535. case PROP_TR_EASE:
  8536. st->tr_ease = (unsigned char)dcl->val;
  8537. break;
  8538. case PROP_FLOAT:
  8539. st->float_left = (unsigned char)dcl->val;
  8540. break;
  8541. case PROP_CLEAR:
  8542. st->clear = (unsigned char)dcl->val;
  8543. break;
  8544. case PROP_FLEX_GROW:
  8545. st->flex_grow = (unsigned char)dcl->val;
  8546. break;
  8547. case PROP_FLEX_COL:
  8548. st->flex_col = (unsigned char)(dcl->val != 0);
  8549. break;
  8550. case PROP_FLEX_WRAP:
  8551. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8552. break;
  8553. case PROP_JUSTIFY:
  8554. st->justify = (unsigned char)dcl->val;
  8555. break;
  8556. case PROP_ALIGN_ITEMS:
  8557. st->align_items = (unsigned char)dcl->val;
  8558. break;
  8559. case PROP_ALIGN_SELF:
  8560. st->align_self = (unsigned char)dcl->val;
  8561. break;
  8562. case PROP_GRID_NCOLS:
  8563. st->grid_ncols = (unsigned char)dcl->val;
  8564. break;
  8565. case PROP_GRID_GAP:
  8566. st->grid_gap = (unsigned char)dcl->val;
  8567. break;
  8568. case PROP_GRID_SLOT:
  8569. st->grid_slot = (unsigned char)dcl->val;
  8570. break;
  8571. case PROP_GRID_AUTO:
  8572. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8573. break;
  8574. case PROP_POS:
  8575. st->pos = (unsigned char)dcl->val;
  8576. break;
  8577. case PROP_LEFT:
  8578. st->left = dcl->val;
  8579. break;
  8580. case PROP_TOP:
  8581. st->top = dcl->val;
  8582. break;
  8583. case PROP_RIGHT:
  8584. st->right = dcl->val;
  8585. break;
  8586. case PROP_BOTTOM:
  8587. st->bottom = dcl->val;
  8588. break;
  8589. case PROP_LEFT_SET:
  8590. st->left_set = (unsigned char)(dcl->val != 0);
  8591. break;
  8592. case PROP_TOP_SET:
  8593. st->top_set = (unsigned char)(dcl->val != 0);
  8594. break;
  8595. case PROP_RIGHT_SET:
  8596. st->right_set = (unsigned char)(dcl->val != 0);
  8597. break;
  8598. case PROP_BOTTOM_SET:
  8599. st->bottom_set = (unsigned char)(dcl->val != 0);
  8600. break;
  8601. case PROP_ZINDEX:
  8602. st->z_index = dcl->val;
  8603. break;
  8604. case PROP_ZSET:
  8605. st->z_set = (unsigned char)(dcl->val != 0);
  8606. break;
  8607. case PROP_TRANSFORM_X:
  8608. st->tx = dcl->val;
  8609. break;
  8610. case PROP_TRANSFORM_Y:
  8611. st->ty = dcl->val;
  8612. break;
  8613. case PROP_TRANSFORM_SET:
  8614. st->transform_set = (unsigned char)(dcl->val != 0);
  8615. break;
  8616. case PROP_TRANSFORM_PAINT:
  8617. st->transform_scale = (unsigned char)(dcl->val != 0);
  8618. break;
  8619. case PROP_SCALE_X:
  8620. st->scale_x = (signed short)dcl->val;
  8621. break;
  8622. case PROP_SCALE_Y:
  8623. st->scale_y = (signed short)dcl->val;
  8624. break;
  8625. case PROP_ROTATE:
  8626. st->rotate_x100 = (signed short)dcl->val;
  8627. break;
  8628. case PROP_ORIGIN_X_KIND:
  8629. st->origin_x_kind = (unsigned char)dcl->val;
  8630. break;
  8631. case PROP_ORIGIN_X:
  8632. st->origin_x = (signed short)dcl->val;
  8633. break;
  8634. case PROP_ORIGIN_Y_KIND:
  8635. st->origin_y_kind = (unsigned char)dcl->val;
  8636. break;
  8637. case PROP_ORIGIN_Y:
  8638. st->origin_y = (signed short)dcl->val;
  8639. break;
  8640. default:
  8641. break;
  8642. }
  8643. }
  8644. /* The element's custom properties for rules flagged USES_SCOPED: the
  8645. * definitions of its matched rules in cascade order (precedence first,
  8646. * then document order), over the document-wide table. Inheritance from
  8647. * ancestors is not modelled; a name no matched rule defines falls back to
  8648. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8649. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8650. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8651. static void css_env_build(struct br_doc *d)
  8652. {
  8653. static unsigned short order[RULE_MATCH_MAX];
  8654. unsigned int i, need = 0, n = 0;
  8655. css_env_active = 0;
  8656. css_nlocal = 0;
  8657. for (i = 0; i < nrule_matches; i++)
  8658. if (d->rules[rule_matches[i].idx].var_scope &
  8659. BR_RULE_USES_SCOPED)
  8660. need = 1;
  8661. if (!need)
  8662. return;
  8663. /* Definitions apply lowest precedence first so the winner is
  8664. * written last (stable: document order within a precedence). */
  8665. for (i = 0; i < nrule_matches; i++) {
  8666. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8667. unsigned int j = n;
  8668. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8669. continue;
  8670. while (j > 0 && rule_matches[order[j - 1]].spec >
  8671. rule_matches[i].spec) {
  8672. order[j] = order[j - 1];
  8673. j--;
  8674. }
  8675. order[j] = i;
  8676. n++;
  8677. }
  8678. for (i = 0; i < n; i++) {
  8679. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8680. const char *raw = attr_ptr(d, r->raw_off);
  8681. unsigned int save = css_nlocal, k;
  8682. /* Collect this block's definitions after the earlier ones,
  8683. * replacing a name already present. */
  8684. {
  8685. unsigned int base = save;
  8686. css_local_append(raw, r->raw_len);
  8687. for (k = base; k < css_nlocal; k++) {
  8688. int prev = css_local_find_before(
  8689. css_local[k].name, css_local[k].nlen, base);
  8690. if (prev >= 0) {
  8691. css_local[prev] = css_local[k];
  8692. css_local[k].nlen = 0;
  8693. }
  8694. }
  8695. }
  8696. }
  8697. css_env_active = 1;
  8698. }
  8699. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8700. int spec, int spec_imp, short *best, short *best_imp)
  8701. {
  8702. unsigned char p = dcl->prop;
  8703. if (p >= PROP_COUNT)
  8704. return;
  8705. /* A transparent background competes with a colour for one slot. */
  8706. if (p == PROP_BG_NONE)
  8707. p = PROP_BG;
  8708. if (dcl->important) {
  8709. if (spec_imp >= best_imp[p]) {
  8710. apply_decl(st, dcl);
  8711. best_imp[p] = (short)spec_imp;
  8712. }
  8713. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8714. apply_decl(st, dcl);
  8715. best[p] = (short)spec;
  8716. }
  8717. }
  8718. void br_style_for(struct br_doc *d, unsigned int node,
  8719. const struct br_style *parent, struct br_style *out)
  8720. {
  8721. unsigned int tag;
  8722. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8723. unsigned int i;
  8724. if (!cascade_pe && d->nodes[node].style_valid) {
  8725. *out = d->nodes[node].style;
  8726. return;
  8727. }
  8728. *out = *parent;
  8729. out->has_bg = 0;
  8730. out->bg_set = 0;
  8731. out->bg_image = 0;
  8732. out->bg_repeat = 0;
  8733. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8734. out->bg_pos_x = out->bg_pos_y = 0;
  8735. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8736. out->float_left = 0;
  8737. out->clear = 0;
  8738. out->list_item = 0;
  8739. out->font_unit = 0;
  8740. for (i = 0; i < 4; i++) {
  8741. out->border_w[i] = 0;
  8742. out->border_c[i] = 0;
  8743. out->border_s[i] = BR_BORDER_SOLID;
  8744. out->radius[i] = 0;
  8745. }
  8746. out->width = out->max_width = out->min_width = out->height = 0;
  8747. out->min_height = out->max_height = 0;
  8748. out->width_calc_pct = out->width_calc_px = 0;
  8749. out->height_calc_pct = out->height_calc_px = 0;
  8750. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8751. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8752. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8753. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8754. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8755. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8756. out->box_sizing = 0;
  8757. out->ml_auto = out->mr_auto = 0;
  8758. out->mt = out->mr = out->mb = out->ml = 0;
  8759. out->pt = out->pr = out->pb = out->pl = 0;
  8760. for (i = 0; i < 8; i++) {
  8761. out->mp_unit[i] = 0;
  8762. out->mp_calc_px[i] = 0;
  8763. }
  8764. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8765. out->justify = out->align_items = out->align_self = 0;
  8766. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8767. out->grid_auto = 0;
  8768. out->aspect_w = out->aspect_h = 0;
  8769. out->pos = out->left_set = out->top_set = 0;
  8770. out->right_set = out->bottom_set = 0;
  8771. out->left = out->top = out->right = out->bottom = 0;
  8772. out->z_index = 0;
  8773. out->z_set = 0;
  8774. out->tx = out->ty = 0;
  8775. out->tx_pct = out->ty_pct = 0;
  8776. out->transform_set = 0;
  8777. out->opacity = 255;
  8778. out->tr_mask = 0;
  8779. out->tr_ms = 0;
  8780. out->tr_ease = BR_EASE_EASE;
  8781. out->transform_scale = 0;
  8782. out->scale_x = out->scale_y = 100;
  8783. out->rotate_x100 = 0;
  8784. out->origin_x = out->origin_y = 50;
  8785. out->origin_x_kind = out->origin_y_kind = 1;
  8786. out->vis_hidden = out->opacity_hidden = 0;
  8787. out->inline_flex = 0;
  8788. out->overflow_hidden = 0;
  8789. out->content = 0;
  8790. /* A pseudo-element is an anonymous inline box: none of the
  8791. * element's UA defaults or presentational hints apply to it. */
  8792. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8793. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8794. BR_DISPLAY_INLINE;
  8795. for (i = 0; i < PROP_COUNT; i++) {
  8796. best[i] = -1;
  8797. best_imp[i] = -1;
  8798. }
  8799. if (!cascade_pe)
  8800. switch (tag) {
  8801. case BR_TAG_HEAD:
  8802. case BR_TAG_TITLE:
  8803. case BR_TAG_META:
  8804. case BR_TAG_LINK:
  8805. case BR_TAG_BASE:
  8806. case BR_TAG_SCRIPT:
  8807. case BR_TAG_STYLE:
  8808. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8809. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8810. out->display = BR_DISPLAY_NONE;
  8811. break;
  8812. case BR_TAG_BODY:
  8813. /* The UA default: the page itself has no gutter, so a site's
  8814. * `body { margin: 0 }` really starts at the canvas edge. */
  8815. out->mt = out->mb = out->ml = out->mr = 8;
  8816. break;
  8817. case BR_TAG_P:
  8818. out->mt = out->mb = 8;
  8819. break;
  8820. case BR_TAG_H1:
  8821. out->font_px = 32; out->bold = 1;
  8822. out->mt = 10; out->mb = 6;
  8823. break;
  8824. case BR_TAG_H2:
  8825. out->font_px = 24; out->bold = 1;
  8826. out->mt = 8; out->mb = 4;
  8827. break;
  8828. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8829. out->bold = 1; out->mt = 6; out->mb = 3;
  8830. if (tag == BR_TAG_H3)
  8831. out->font_px = 19;
  8832. else if (tag == BR_TAG_H4)
  8833. out->font_px = 17;
  8834. else if (tag == BR_TAG_H5)
  8835. out->font_px = 15;
  8836. else
  8837. out->font_px = 13;
  8838. break;
  8839. case BR_TAG_UL: case BR_TAG_OL:
  8840. out->mt = out->mb = 6; out->pl = 20;
  8841. break;
  8842. case BR_TAG_LI:
  8843. out->mt = out->mb = 2;
  8844. out->list_item = 1;
  8845. break;
  8846. case BR_TAG_BLOCKQUOTE:
  8847. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8848. break;
  8849. case BR_TAG_PRE:
  8850. out->mt = out->mb = 8; out->pl = 8;
  8851. out->has_bg = 1; out->bg = theme_panel;
  8852. out->mono = 1;
  8853. break;
  8854. case BR_TAG_CENTER:
  8855. out->align = BR_ALIGN_CENTER;
  8856. break;
  8857. case BR_TAG_A:
  8858. out->color = theme_link; out->underline = 1;
  8859. break;
  8860. case BR_TAG_B: case BR_TAG_STRONG:
  8861. out->bold = 1;
  8862. break;
  8863. case BR_TAG_I: case BR_TAG_EM:
  8864. out->italic = 1;
  8865. break;
  8866. case BR_TAG_U:
  8867. out->underline = 1;
  8868. break;
  8869. case BR_TAG_S: case BR_TAG_STRIKE:
  8870. out->strike = 1;
  8871. break;
  8872. case BR_TAG_CODE:
  8873. out->color = 0x8010;
  8874. out->mono = 1;
  8875. break;
  8876. case BR_TAG_SMALL:
  8877. out->font_px = 13;
  8878. break;
  8879. case BR_TAG_BIG:
  8880. out->font_px = 20;
  8881. break;
  8882. case BR_TAG_TD: case BR_TAG_TH:
  8883. out->pr = 12;
  8884. /* A cell establishes left-aligned content even inside a legacy
  8885. * <center> (HN's outer page wrapper); author CSS/align below
  8886. * can still override this. */
  8887. out->align = BR_ALIGN_LEFT;
  8888. break;
  8889. case BR_TAG_VIDEO:
  8890. out->display = BR_DISPLAY_BLOCK;
  8891. break;
  8892. case BR_TAG_SVG:
  8893. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8894. * external-link icon follows its link text on the same line.
  8895. * Flex rows still treat it as an item (flex_wants_row). */
  8896. out->display = BR_DISPLAY_INLINE;
  8897. break;
  8898. default:
  8899. break;
  8900. }
  8901. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8902. * presentational hints; author CSS still wins because it is applied
  8903. * below. */
  8904. if (!cascade_pe) {
  8905. unsigned short bgc;
  8906. const char *bga = br_find_attr(d, node, "bgcolor");
  8907. const char *ala = br_find_attr(d, node, "align");
  8908. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8909. out->bg = bgc;
  8910. out->has_bg = 1;
  8911. }
  8912. if (ala) {
  8913. if (same_string(ala, "right"))
  8914. out->align = BR_ALIGN_RIGHT;
  8915. else if (same_string(ala, "center"))
  8916. out->align = BR_ALIGN_CENTER;
  8917. else if (same_string(ala, "left"))
  8918. out->align = BR_ALIGN_LEFT;
  8919. }
  8920. }
  8921. /* presentational <font color>: author CSS below still wins */
  8922. if (!cascade_pe && d->nodes[node].color_off) {
  8923. unsigned short c;
  8924. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8925. if (parse_color(v, s_len(v), &c))
  8926. out->color = c;
  8927. }
  8928. /* SVG presentation attributes; CSS declarations still win below. */
  8929. if (!cascade_pe && in_svg(d, node)) {
  8930. const char *fa = br_find_attr(d, node, "fill");
  8931. const char *sa = br_find_attr(d, node, "stroke");
  8932. const char *wa = br_find_attr(d, node, "stroke-width");
  8933. const char *ra = br_find_attr(d, node, "fill-rule");
  8934. const char *ca = br_find_attr(d, node, "color");
  8935. unsigned short c;
  8936. int mode;
  8937. if (ca && parse_color(ca, s_len(ca), &c))
  8938. out->color = c;
  8939. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8940. out->fill_mode = (unsigned char)mode;
  8941. if (mode == BR_PAINT_COLOR)
  8942. out->fill = c;
  8943. }
  8944. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8945. out->stroke_mode = (unsigned char)mode;
  8946. if (mode == BR_PAINT_COLOR)
  8947. out->stroke = c;
  8948. }
  8949. if (wa) {
  8950. int sw = svg_len16(wa, s_len(wa));
  8951. if (sw >= 0)
  8952. out->stroke_w = (unsigned short)sw;
  8953. }
  8954. if (ra)
  8955. out->fill_evenodd = same_string(ra, "evenodd");
  8956. }
  8957. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8958. * Author CSS below can still override this. */
  8959. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8960. unsigned int p = d->nodes[node].parent;
  8961. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8962. !br_find_attr(d, p, "open"))
  8963. out->display = BR_DISPLAY_NONE;
  8964. }
  8965. rule_collect(d, node);
  8966. css_env_build(d);
  8967. for (i = 0; i < nrule_matches; i++) {
  8968. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8969. int spec = rule_matches[i].spec;
  8970. int spec_imp = rule_matches[i].spec_imp;
  8971. unsigned int k;
  8972. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8973. /* Re-expand against this element's variables. */
  8974. struct br_decl decls[BR_DECLS];
  8975. unsigned int nd;
  8976. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8977. decls, &nd);
  8978. for (k = 0; k < nd; k++)
  8979. cascade_decl(out, &decls[k], spec, spec_imp,
  8980. best, best_imp);
  8981. continue;
  8982. }
  8983. for (k = 0; k < r->nd; k++)
  8984. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8985. best_imp);
  8986. }
  8987. css_env_active = 0;
  8988. css_nlocal = 0;
  8989. if (!cascade_pe && d->nodes[node].style_off) {
  8990. struct br_decl decls[BR_DECLS];
  8991. unsigned int nd, k;
  8992. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8993. parse_decls(d, v, s_len(v), decls, &nd);
  8994. for (k = 0; k < nd; k++)
  8995. cascade_decl(out, &decls[k],
  8996. CASCADE_INLINE * CASCADE_BAND,
  8997. CASCADE_INLINE * CASCADE_BAND, best,
  8998. best_imp);
  8999. }
  9000. if (out->font_unit) {
  9001. int px = out->font_raw;
  9002. if (out->font_unit == 2)
  9003. px = parent->font_px * out->font_raw / 100;
  9004. else if (out->font_unit == 3)
  9005. px = root_font_px * out->font_raw / 100;
  9006. else if (out->font_unit == 4)
  9007. px = parent->font_px * out->font_raw / 10000;
  9008. if (px < 8)
  9009. px = 8;
  9010. if (px > 72)
  9011. px = 72;
  9012. out->font_px = (unsigned char)px;
  9013. }
  9014. resolve_size_units(out);
  9015. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  9016. out->display = BR_DISPLAY_NONE;
  9017. /* Invisible in-flow content is dropped (it would otherwise take
  9018. * space this engine cannot hide well). An absolute/fixed box keeps
  9019. * its display: it moves nothing, and scripts measure it while it is
  9020. * still transparent (Bootstrap's tooltip is `opacity: 0` until it
  9021. * gets `.in`). The layout lays it out without painting it. */
  9022. if (((out->vis_hidden || out->opacity_hidden) &&
  9023. out->pos != 1 && out->pos != 2) ||
  9024. (out->overflow_hidden && out->height_kind == 1 &&
  9025. out->height == 0))
  9026. out->display = BR_DISPLAY_NONE;
  9027. if (cascade_pe)
  9028. return;
  9029. d->nodes[node].style = *out;
  9030. d->nodes[node].style_valid = 1;
  9031. }
  9032. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  9033. const struct br_style *elem, struct br_style *out,
  9034. char *buf, unsigned int cap)
  9035. {
  9036. struct br_node *n;
  9037. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  9038. const char *c;
  9039. unsigned int len = 0;
  9040. if (out) {
  9041. out->content = 0;
  9042. out->display = BR_DISPLAY_NONE;
  9043. }
  9044. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  9045. (pe != 1 && pe != 2))
  9046. return 0;
  9047. n = &d->nodes[node];
  9048. if (n->type != BR_NODE_ELEM ||
  9049. ((n->pe_state & chk) && !(n->pe_state & has)))
  9050. return 0;
  9051. cascade_pe = pe;
  9052. br_style_for(d, node, elem, out);
  9053. cascade_pe = 0;
  9054. if (out->content == BR_CONTENT_EMPTY) {
  9055. /* No text; the caller may still draw the box (an icon
  9056. * with a size and a background image). */
  9057. buf[0] = '\0';
  9058. n->pe_state |= chk;
  9059. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  9060. out->pos != 2 && (out->width_kind || out->height_kind) &&
  9061. (out->has_bg || out->bg_image))
  9062. n->pe_state |= has;
  9063. else
  9064. out->display = BR_DISPLAY_NONE;
  9065. return 0;
  9066. }
  9067. if (out->content && out->display != BR_DISPLAY_NONE &&
  9068. out->pos != 1 && out->pos != 2) {
  9069. c = attr_ptr(d, d->content_off[out->content - 1]);
  9070. while (*c && len + 1 < cap) {
  9071. if (*c == '\001') {
  9072. char name[32];
  9073. unsigned int k = 0;
  9074. const char *v;
  9075. c++;
  9076. while (*c && *c != '\002' && k + 1 < sizeof(name))
  9077. name[k++] = *c++;
  9078. name[k] = '\0';
  9079. if (*c == '\002')
  9080. c++;
  9081. v = br_find_attr(d, node, name);
  9082. while (v && *v && len + 1 < cap)
  9083. buf[len++] = *v++;
  9084. continue;
  9085. }
  9086. buf[len++] = *c++;
  9087. }
  9088. }
  9089. buf[len] = '\0';
  9090. n->pe_state |= chk;
  9091. if (len)
  9092. n->pe_state |= has;
  9093. return len;
  9094. }
  9095. /* ---- HTML parser ---- */
  9096. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  9097. unsigned int sp, int *pre)
  9098. {
  9099. unsigned int i;
  9100. *pre = 0;
  9101. for (i = 1; i <= sp; i++)
  9102. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  9103. (*pre)++;
  9104. }
  9105. static void implicit_close(struct br_doc *d, unsigned short *stack,
  9106. unsigned int *sp, unsigned int tag)
  9107. {
  9108. while (*sp > 0) {
  9109. unsigned int top = d->nodes[stack[*sp]].tag;
  9110. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  9111. (*sp)--;
  9112. continue;
  9113. }
  9114. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  9115. (*sp)--;
  9116. continue;
  9117. }
  9118. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  9119. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  9120. (*sp)--;
  9121. continue;
  9122. }
  9123. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  9124. (*sp)--;
  9125. continue;
  9126. }
  9127. break;
  9128. }
  9129. }
  9130. static int has_attr_token(const char *s, unsigned int len, const char *name)
  9131. {
  9132. unsigned int i, n = s_len(name);
  9133. for (i = 0; i + n <= len; i++) {
  9134. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  9135. continue;
  9136. if (!ci_eq_n(s + i, name, n))
  9137. continue;
  9138. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  9139. return 1;
  9140. }
  9141. return 0;
  9142. }
  9143. static int stack_has_svg(const struct br_doc *d,
  9144. const unsigned short *stack, unsigned int sp)
  9145. {
  9146. unsigned int k;
  9147. for (k = 0; k <= sp && k < 64; k++)
  9148. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  9149. return 1;
  9150. return 0;
  9151. }
  9152. /* Skip an element subtree by finding its closing tag. Used for SVG
  9153. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  9154. * overwrite the document title. */
  9155. static unsigned int skip_element(const char *html, unsigned int len,
  9156. unsigned int i, const char *name,
  9157. unsigned int n)
  9158. {
  9159. char close[40];
  9160. unsigned int k = 0, p;
  9161. close[k++] = '<';
  9162. close[k++] = '/';
  9163. while (k < sizeof(close) - 1 && n--)
  9164. close[k++] = *name++;
  9165. p = (unsigned int)find_ci(html, len, close, k, i);
  9166. if ((int)p < 0)
  9167. return len;
  9168. p += k;
  9169. while (p < len && html[p] != '>')
  9170. p++;
  9171. if (p < len)
  9172. p++;
  9173. return p;
  9174. }
  9175. /* Store a <script src=...> value in the attribute arena. Kept separate from
  9176. * parse_attrs() so script-only attributes cannot be confused with the
  9177. * value/d attributes that also use src_off on other elements. */
  9178. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  9179. unsigned int len)
  9180. {
  9181. unsigned int i = 0;
  9182. while (i < len) {
  9183. unsigned int ns, ne, vs, ve;
  9184. char quote = 0;
  9185. while (i < len && is_space(s[i]))
  9186. i++;
  9187. if (i >= len || s[i] == '/')
  9188. break;
  9189. ns = i;
  9190. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  9191. i++;
  9192. ne = i;
  9193. while (i < len && is_space(s[i]))
  9194. i++;
  9195. if (i >= len || s[i] != '=')
  9196. continue;
  9197. i++;
  9198. while (i < len && is_space(s[i]))
  9199. i++;
  9200. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9201. quote = s[i++];
  9202. vs = i;
  9203. while (i < len && s[i] != quote)
  9204. i++;
  9205. ve = i;
  9206. if (i < len)
  9207. i++;
  9208. } else {
  9209. vs = i;
  9210. while (i < len && !is_space(s[i]) && s[i] != '>')
  9211. i++;
  9212. ve = i;
  9213. }
  9214. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  9215. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  9216. }
  9217. return 0;
  9218. }
  9219. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  9220. * application/json, text/template, ...) and non-JS type values. External
  9221. * src scripts are allowed; the browser fetches them separately. Returns 1
  9222. * for a classic script, 2 for type="module", 0 to skip (also for a
  9223. * `nomodule` fallback, since module scripts run). */
  9224. static int script_is_javascript(const char *s, unsigned int len)
  9225. {
  9226. int kind = 1;
  9227. unsigned int i = 0;
  9228. while (i < len) {
  9229. unsigned int ns, ne, vs = 0, ve = 0;
  9230. char quote = 0;
  9231. int has_val = 0;
  9232. while (i < len && is_space(s[i]))
  9233. i++;
  9234. if (i >= len)
  9235. break;
  9236. ns = i;
  9237. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  9238. i++;
  9239. ne = i;
  9240. while (i < len && is_space(s[i]))
  9241. i++;
  9242. if (i < len && s[i] == '=') {
  9243. i++;
  9244. while (i < len && is_space(s[i]))
  9245. i++;
  9246. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  9247. quote = s[i++];
  9248. vs = i;
  9249. while (i < len && s[i] != quote)
  9250. i++;
  9251. ve = i;
  9252. if (i < len)
  9253. i++;
  9254. } else {
  9255. vs = i;
  9256. while (i < len && !is_space(s[i]) && s[i] != '/')
  9257. i++;
  9258. ve = i;
  9259. }
  9260. has_val = 1;
  9261. } else if (i == ne) {
  9262. i++;
  9263. }
  9264. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  9265. return 0;
  9266. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  9267. unsigned int vn = has_val ? ve - vs : 0;
  9268. if (vn == 0)
  9269. continue;
  9270. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  9271. kind = 2;
  9272. continue;
  9273. }
  9274. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  9275. (vn == 22 &&
  9276. ci_eq_n(s + vs, "application/javascript", 22)) ||
  9277. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  9278. (vn == 22 &&
  9279. ci_eq_n(s + vs, "application/ecmascript", 22)))
  9280. continue;
  9281. return 0;
  9282. }
  9283. }
  9284. return kind;
  9285. }
  9286. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  9287. {
  9288. unsigned short stack[64];
  9289. unsigned int sp = 0;
  9290. unsigned int i = 0;
  9291. int pre = 0;
  9292. unsigned int css_len = 0;
  9293. if (!d)
  9294. return -1;
  9295. if (!d->nodes)
  9296. br_doc_init(d);
  9297. if (!d->nodes || !d->text || !d->attrs)
  9298. return -1;
  9299. doc_reset(d);
  9300. d->script_src = html;
  9301. stack[0] = 0;
  9302. d->nodes[0].type = BR_NODE_ELEM;
  9303. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9304. while (i < len) {
  9305. if (html[i] != '<') {
  9306. unsigned int st = i;
  9307. while (i < len && html[i] != '<')
  9308. i++;
  9309. if (sp <= 63)
  9310. append_text(d, stack[sp], html + st, i - st, pre);
  9311. continue;
  9312. }
  9313. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  9314. html[i + 3] == '-') {
  9315. int p = find_ci(html, len, "-->", 3, i + 4);
  9316. i = p >= 0 ? (unsigned int)p + 3 : len;
  9317. continue;
  9318. }
  9319. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  9320. while (i < len && html[i] != '>')
  9321. i++;
  9322. if (i < len)
  9323. i++;
  9324. continue;
  9325. }
  9326. if (i + 1 < len && html[i + 1] == '/') {
  9327. unsigned int ns = i + 2, ne, tag, k, n;
  9328. char name[32];
  9329. while (ns < len && is_space(html[ns]))
  9330. ns++;
  9331. ne = ns;
  9332. while (ne < len && is_ident(html[ne]))
  9333. ne++;
  9334. if (ne == ns) {
  9335. i++;
  9336. continue;
  9337. }
  9338. n = ne - ns;
  9339. if (n >= sizeof(name))
  9340. n = sizeof(name) - 1;
  9341. for (k = 0; k < n; k++)
  9342. name[k] = html[ns + k];
  9343. name[n] = '\0';
  9344. tag = br_tag_from_name(name);
  9345. i = ne;
  9346. while (i < len && html[i] != '>')
  9347. i++;
  9348. if (i < len)
  9349. i++;
  9350. for (k = sp; k > 0; k--) {
  9351. struct br_node *nd = &d->nodes[stack[k]];
  9352. if (tag == BR_TAG_UNKNOWN) {
  9353. const char *raw = attr_ptr(d, nd->name_off);
  9354. if (nd->tag == BR_TAG_UNKNOWN &&
  9355. ci_eq_n(raw, name, n) &&
  9356. raw[n] == '\0') {
  9357. sp = k - 1;
  9358. break;
  9359. }
  9360. } else if (nd->tag == tag) {
  9361. sp = k - 1;
  9362. break;
  9363. }
  9364. }
  9365. stack_recount_pre(d, stack, sp, &pre);
  9366. continue;
  9367. }
  9368. /* start tag */
  9369. {
  9370. unsigned int ns = i + 1, ne, tag, k, n;
  9371. char name[16];
  9372. unsigned int attr_start, attr_end;
  9373. int selfclose = 0;
  9374. while (ns < len && is_space(html[ns]))
  9375. ns++;
  9376. ne = ns;
  9377. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  9378. ne++;
  9379. if (ne == ns) {
  9380. /* stray '<' */
  9381. append_text(d, stack[sp], html + i, 1, pre);
  9382. i++;
  9383. continue;
  9384. }
  9385. n = ne - ns;
  9386. if (n >= sizeof(name))
  9387. n = sizeof(name) - 1;
  9388. for (k = 0; k < n; k++)
  9389. name[k] = html[ns + k];
  9390. name[n] = '\0';
  9391. tag = br_tag_from_name(name);
  9392. attr_start = ne;
  9393. {
  9394. unsigned int q = ne;
  9395. char quote = 0;
  9396. while (q < len) {
  9397. char c = html[q];
  9398. if (quote) {
  9399. if (c == quote)
  9400. quote = 0;
  9401. } else if (c == '"' || c == '\'') {
  9402. quote = c;
  9403. } else if (c == '>') {
  9404. break;
  9405. }
  9406. q++;
  9407. }
  9408. attr_end = q;
  9409. /* `/>` closes the tag only when the slash is
  9410. * not the end of an unquoted attribute value:
  9411. * Docusaurus writes `<a href=/installation/>`,
  9412. * which used to self-close the link and leave
  9413. * its label outside it. */
  9414. if (q > attr_start && html[q - 1] == '/' &&
  9415. (q - 1 == attr_start ||
  9416. is_space(html[q - 2]) ||
  9417. html[q - 2] == '"' || html[q - 2] == '\''))
  9418. selfclose = 1;
  9419. i = q < len ? q + 1 : len;
  9420. }
  9421. if (tag == BR_TAG_SCRIPT) {
  9422. int p = find_ci(html, len, "</script", 8, i);
  9423. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9424. unsigned int idx;
  9425. struct br_script *script = 0;
  9426. int kind = script_is_javascript(html + attr_start,
  9427. attr_end - attr_start);
  9428. if (kind) {
  9429. if (d->nscripts < BR_MAX_SCRIPTS) {
  9430. script = &d->scripts[d->nscripts++];
  9431. script->module = kind == 2;
  9432. script->node = 0;
  9433. script->off = i;
  9434. script->len = end > i ? end - i : 0;
  9435. script->src_off = script_src_attr(
  9436. d, html + attr_start,
  9437. attr_end - attr_start);
  9438. } else {
  9439. d->scripts_truncated = 1;
  9440. }
  9441. }
  9442. /* Keep the element in the DOM even though its body is
  9443. * executed separately: scripts (Piwik, analytics)
  9444. * insert a sibling after getElementsByTagName('script')
  9445. * [0], and the element must exist for that to work. */
  9446. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9447. (unsigned char)tag);
  9448. if (idx)
  9449. parse_attrs(d, &d->nodes[idx],
  9450. html + attr_start,
  9451. attr_end - attr_start);
  9452. if (script)
  9453. script->node = idx;
  9454. if (idx && !kind && end > i)
  9455. append_raw_text(d, idx, html + i, end - i);
  9456. i = p >= 0 ? (unsigned int)p + 8 : len;
  9457. while (i < len && html[i] != '>')
  9458. i++;
  9459. if (i < len)
  9460. i++;
  9461. continue;
  9462. }
  9463. /* Scripting is always enabled, so <noscript> is a
  9464. * raw-text element (HTML "in head/in body, scripting
  9465. * flag set"): its markup must not reach the DOM or the
  9466. * cascade. google.com puts a meta refresh and
  9467. * `table,div,span,p{display:none}` in one, which hid
  9468. * the whole page and left only the "click here" link. */
  9469. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9470. int p = find_ci(html, len, "</noscript", 10, i);
  9471. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9472. unsigned int idx;
  9473. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9474. (unsigned char)tag);
  9475. if (idx) {
  9476. parse_attrs(d, &d->nodes[idx],
  9477. html + attr_start,
  9478. attr_end - attr_start);
  9479. if (end > i)
  9480. append_raw_text(d, idx, html + i,
  9481. end - i);
  9482. }
  9483. i = p >= 0 ? (unsigned int)p + 10 : len;
  9484. while (i < len && html[i] != '>')
  9485. i++;
  9486. if (i < len)
  9487. i++;
  9488. continue;
  9489. }
  9490. if (stack_has_svg(d, stack, sp) &&
  9491. (tag == BR_TAG_TITLE ||
  9492. (tag == BR_TAG_UNKNOWN &&
  9493. (ci_eq_n(name, "desc", 4) ||
  9494. ci_eq_n(name, "metadata", 8))))) {
  9495. i = skip_element(html, len, i, name, n);
  9496. continue;
  9497. }
  9498. /* A closed <details> is collapsed at style time, not
  9499. * parse time: the <summary> (the hamburger button on
  9500. * tagesschau and many mobile menus) must stay in the
  9501. * DOM. */
  9502. if (tag == BR_TAG_STYLE) {
  9503. int p = find_ci(html, len, "</style", 7, i);
  9504. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9505. unsigned int cend = end;
  9506. unsigned int idx;
  9507. while (cend > i && is_space(html[cend - 1]))
  9508. cend--;
  9509. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9510. for (k = i; k < cend; k++)
  9511. css_store[css_len++] = html[k];
  9512. css_store[css_len++] = '\n';
  9513. }
  9514. /* The CSS is collected above, but keep a display:none
  9515. * element so getElementsByTagName('style')/parentNode
  9516. * behave like a real document. */
  9517. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9518. (unsigned char)tag);
  9519. if (idx)
  9520. parse_attrs(d, &d->nodes[idx],
  9521. html + attr_start,
  9522. attr_end - attr_start);
  9523. i = p >= 0 ? (unsigned int)p + 7 : len;
  9524. while (i < len && html[i] != '>')
  9525. i++;
  9526. if (i < len)
  9527. i++;
  9528. continue;
  9529. }
  9530. if (tag == BR_TAG_TITLE) {
  9531. int p = find_ci(html, len, "</title", 7, i);
  9532. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9533. unsigned int tn = 0;
  9534. char tmp[BR_TITLE_MAX];
  9535. tn = decode_text(html + i, end - i, tmp,
  9536. sizeof(tmp), 1);
  9537. (void)tn;
  9538. s_copy(d->title, tmp, sizeof(d->title));
  9539. i = p >= 0 ? (unsigned int)p + 7 : len;
  9540. while (i < len && html[i] != '>')
  9541. i++;
  9542. if (i < len)
  9543. i++;
  9544. continue;
  9545. }
  9546. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9547. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9548. unsigned int idx;
  9549. implicit_close(d, stack, &sp, tag);
  9550. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9551. (unsigned char)tag);
  9552. if (idx)
  9553. parse_attrs(d, &d->nodes[idx],
  9554. html + attr_start,
  9555. attr_end - attr_start);
  9556. continue;
  9557. }
  9558. if (sp >= 63) {
  9559. d->truncated = 1;
  9560. continue;
  9561. }
  9562. implicit_close(d, stack, &sp, tag);
  9563. {
  9564. unsigned int idx = node_append(d, stack[sp],
  9565. BR_NODE_ELEM,
  9566. (unsigned char)tag);
  9567. if (!idx)
  9568. continue;
  9569. parse_attrs(d, &d->nodes[idx],
  9570. html + attr_start,
  9571. attr_end - attr_start);
  9572. if (tag == BR_TAG_UNKNOWN)
  9573. d->nodes[idx].name_off =
  9574. store_attr(d, name, n, 31);
  9575. /* HTML ignores the self-closing flag on a
  9576. * normal element (`<div/>` opens a div); only
  9577. * SVG content (and unknown/custom tags, kept
  9578. * as before) honours it. */
  9579. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9580. tag != BR_TAG_SVG &&
  9581. !stack_has_svg(d, stack, sp))
  9582. selfclose = 0;
  9583. if (!selfclose) {
  9584. sp++;
  9585. stack[sp] = (unsigned short)idx;
  9586. if (tag == BR_TAG_PRE)
  9587. pre++;
  9588. }
  9589. }
  9590. }
  9591. }
  9592. d->inline_css_len = css_len;
  9593. return 0;
  9594. }
  9595. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9596. {
  9597. if (!d->nodes)
  9598. br_doc_init(d);
  9599. if (!d->nodes || !d->text || !d->attrs)
  9600. return -1;
  9601. doc_reset(d);
  9602. d->plain = 1;
  9603. d->nodes[0].type = BR_NODE_ELEM;
  9604. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9605. append_text(d, 0, text, len, 1);
  9606. return 0;
  9607. }
  9608. /* A standalone image response: a synthetic document with one <img>. The
  9609. * caller decodes the already-fetched body into image slot 0 and sets the
  9610. * returned node's img_index. */
  9611. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9612. {
  9613. unsigned int idx;
  9614. if (!d->nodes)
  9615. br_doc_init(d);
  9616. if (!d->nodes || !d->text || !d->attrs)
  9617. return 0;
  9618. doc_reset(d);
  9619. d->nodes[0].type = BR_NODE_ELEM;
  9620. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9621. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9622. if (!idx)
  9623. return 0;
  9624. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9625. return idx;
  9626. }