browser_html.c 218 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818
  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. static char bg_urls[BR_BG_URLS][192];
  22. static unsigned int bg_url_count;
  23. /* @font-face definitions, one document at a time. The URL is resolved by
  24. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  25. struct br_font_def {
  26. char family[BR_FACE_FAMILY];
  27. char url[BR_FACE_URL];
  28. char base[192];
  29. unsigned char used;
  30. };
  31. static struct br_font_def font_defs[BR_MAX_FACES];
  32. static int font_face_count;
  33. static char font_base[192];
  34. static void font_reset(void)
  35. {
  36. memset(font_defs, 0, sizeof(font_defs));
  37. font_face_count = 0;
  38. font_base[0] = '\0';
  39. }
  40. void br_set_font_base(const char *base)
  41. {
  42. unsigned int i = 0;
  43. if (!base)
  44. base = "";
  45. while (base[i] && i + 1 < sizeof(font_base)) {
  46. font_base[i] = base[i];
  47. i++;
  48. }
  49. font_base[i] = '\0';
  50. }
  51. int br_font_face_count(void)
  52. {
  53. return font_face_count;
  54. }
  55. const char *br_font_face_url(int id)
  56. {
  57. if (id < 1 || id > font_face_count)
  58. return "";
  59. return font_defs[id - 1].url;
  60. }
  61. const char *br_font_face_base(int id)
  62. {
  63. if (id < 1 || id > font_face_count)
  64. return "";
  65. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  66. font_base;
  67. }
  68. const char *br_font_face_family(int id)
  69. {
  70. if (id < 1 || id > font_face_count)
  71. return "";
  72. return font_defs[id - 1].family;
  73. }
  74. const char *br_bg_image_url(unsigned int idx)
  75. {
  76. if (idx >= bg_url_count)
  77. return "";
  78. return bg_urls[idx];
  79. }
  80. unsigned int br_root_font_px(void)
  81. {
  82. return (unsigned int)root_font_px;
  83. }
  84. /* ---- tag table ---- */
  85. struct tag_name {
  86. const char *name;
  87. unsigned char tag;
  88. };
  89. static const struct tag_name tag_names[] = {
  90. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  91. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  92. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  93. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  94. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  95. { "template", BR_TAG_TEMPLATE },
  96. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  97. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  98. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  99. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  100. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  101. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  102. { "picture", BR_TAG_PICTURE },
  103. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  104. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  105. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  106. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  107. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  108. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  109. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  110. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  111. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  112. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  113. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  114. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  115. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  116. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  117. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  118. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  119. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  120. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  121. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  122. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  123. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  124. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  125. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  126. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  127. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  128. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  129. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  130. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  131. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  132. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  133. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  134. { 0, 0 }
  135. };
  136. const char *br_tag_name(unsigned int tag)
  137. {
  138. unsigned int i;
  139. for (i = 0; tag_names[i].name; i++)
  140. if (tag_names[i].tag == tag)
  141. return tag_names[i].name;
  142. return "?";
  143. }
  144. static unsigned int s_len(const char *s);
  145. #define TAG_HASH_SIZE 128
  146. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  147. static int tag_hash_ready;
  148. static unsigned int tag_hash_key(const char *name, unsigned int len)
  149. {
  150. unsigned int i, h = 2166136261u;
  151. for (i = 0; i < len; i++) {
  152. h ^= (unsigned char)name[i];
  153. h *= 16777619u;
  154. }
  155. return h & (TAG_HASH_SIZE - 1);
  156. }
  157. static void tag_hash_build(void)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < TAG_HASH_SIZE; i++)
  161. tag_hash_idx[i] = 0xFF;
  162. for (i = 0; tag_names[i].name; i++) {
  163. unsigned int h = tag_hash_key(tag_names[i].name,
  164. s_len(tag_names[i].name));
  165. while (tag_hash_idx[h] != 0xFF)
  166. h = (h + 1) & (TAG_HASH_SIZE - 1);
  167. tag_hash_idx[h] = (unsigned char)i;
  168. }
  169. tag_hash_ready = 1;
  170. }
  171. unsigned int br_tag_from_name(const char *name)
  172. {
  173. char lower[16];
  174. unsigned int i, len, h;
  175. unsigned char idx;
  176. for (i = 0; i < 15 && name[i]; i++) {
  177. char c = name[i];
  178. if (c >= 'A' && c <= 'Z')
  179. c = (char)(c - 'A' + 'a');
  180. lower[i] = c;
  181. }
  182. lower[i] = '\0';
  183. len = i;
  184. if (!tag_hash_ready)
  185. tag_hash_build();
  186. h = tag_hash_key(lower, len);
  187. for (;;) {
  188. idx = tag_hash_idx[h];
  189. if (idx == 0xFF)
  190. return BR_TAG_UNKNOWN;
  191. {
  192. const char *n = tag_names[idx].name;
  193. unsigned int j;
  194. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  195. ;
  196. if (!lower[j] && !n[j])
  197. return tag_names[idx].tag;
  198. }
  199. h = (h + 1) & (TAG_HASH_SIZE - 1);
  200. }
  201. }
  202. int br_tag_is_void(unsigned int tag)
  203. {
  204. switch (tag) {
  205. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  206. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  207. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  208. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  209. case BR_TAG_AREA: case BR_TAG_COL:
  210. return 1;
  211. default:
  212. return 0;
  213. }
  214. }
  215. int br_tag_is_block(unsigned int tag)
  216. {
  217. switch (tag) {
  218. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  219. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  220. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  221. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  222. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  223. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  224. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  225. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  226. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  227. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  228. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  229. case BR_TAG_PICTURE:
  230. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  231. case BR_TAG_ADDRESS: case BR_TAG_HR:
  232. return 1;
  233. default:
  234. return 0;
  235. }
  236. }
  237. /* ---- string / entity helpers ---- */
  238. static unsigned int s_len(const char *s)
  239. {
  240. unsigned int n = 0;
  241. while (s && s[n])
  242. n++;
  243. return n;
  244. }
  245. static void s_copy(char *d, const char *s, unsigned int cap)
  246. {
  247. unsigned int i = 0;
  248. if (!cap)
  249. return;
  250. while (s && s[i] && i + 1 < cap) {
  251. d[i] = s[i];
  252. i++;
  253. }
  254. d[i] = '\0';
  255. }
  256. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  257. {
  258. unsigned int i;
  259. for (i = 0; i < n; i++) {
  260. char ca = a[i], cb = b[i];
  261. if (ca >= 'A' && ca <= 'Z')
  262. ca = (char)(ca - 'A' + 'a');
  263. if (cb >= 'A' && cb <= 'Z')
  264. cb = (char)(cb - 'A' + 'a');
  265. if (ca != cb)
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. static int find_ci(const char *hay, unsigned int len, const char *needle,
  271. unsigned int nlen, unsigned int from)
  272. {
  273. unsigned int i;
  274. if (nlen == 0 || from >= len)
  275. return -1;
  276. for (i = from; i + nlen <= len; i++)
  277. if (ci_eq_n(hay + i, needle, nlen))
  278. return (int)i;
  279. return -1;
  280. }
  281. static const struct {
  282. const char *name;
  283. const char *text;
  284. } entities[] = {
  285. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  286. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  287. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  288. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  289. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  290. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  291. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  292. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  293. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  294. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  295. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  296. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  297. { 0, 0 }
  298. };
  299. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  300. {
  301. if (*n + 1 < cap)
  302. out[(*n)++] = c;
  303. }
  304. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  305. char *out, unsigned int cap, unsigned int *n)
  306. {
  307. unsigned int start = *pos + 1, i = start;
  308. unsigned int code = 0;
  309. int ok = 0;
  310. while (i < len && s[i] != ';' && i - start < 12)
  311. i++;
  312. if (i >= len || s[i] != ';') {
  313. /* Not an entity: emit the '&' and advance past it. Leaving
  314. * *pos unchanged here used to spin forever on bare '&'. */
  315. emit(out, cap, n, '&');
  316. *pos = start;
  317. return;
  318. }
  319. if (s[start] == '#') {
  320. unsigned int j = start + 1;
  321. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  322. j++;
  323. for (; j < i; j++) {
  324. char c = s[j];
  325. int d = -1;
  326. if (c >= '0' && c <= '9') d = c - '0';
  327. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  328. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  329. if (d < 0) break;
  330. code = code * 16 + (unsigned int)d;
  331. ok = 1;
  332. }
  333. } else {
  334. for (; j < i; j++) {
  335. if (s[j] < '0' || s[j] > '9')
  336. break;
  337. code = code * 10 + (unsigned int)(s[j] - '0');
  338. ok = 1;
  339. }
  340. }
  341. } else {
  342. unsigned int k;
  343. for (k = 0; entities[k].name; k++) {
  344. unsigned int el = s_len(entities[k].name);
  345. unsigned int j;
  346. if (el != i - start)
  347. continue;
  348. for (j = 0; j < el; j++)
  349. if (s[start + j] != entities[k].name[j])
  350. break;
  351. if (j == el) {
  352. const char *t = entities[k].text;
  353. while (*t)
  354. emit(out, cap, n, *t++);
  355. *pos = i + 1;
  356. return;
  357. }
  358. }
  359. }
  360. if (ok && code) {
  361. if (code == 0x00A0)
  362. emit(out, cap, n, ' ');
  363. else if (code == 0x2013 || code == 0x2014)
  364. emit(out, cap, n, '-');
  365. else if (code == 0x2018 || code == 0x2019)
  366. emit(out, cap, n, '\'');
  367. else if (code == 0x201C || code == 0x201D)
  368. emit(out, cap, n, '"');
  369. else if (code == 0x2026) {
  370. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  371. emit(out, cap, n, '.');
  372. } else if (code <= 0xFF) {
  373. emit(out, cap, n, (char)code);
  374. } else {
  375. emit(out, cap, n, '?');
  376. }
  377. } else {
  378. emit(out, cap, n, '?');
  379. }
  380. *pos = i + 1;
  381. }
  382. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  383. * space and drops leading spaces. Returns the output length. */
  384. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  385. unsigned int cap, int collapse)
  386. {
  387. unsigned int i = 0, n = 0;
  388. while (i < len) {
  389. unsigned char c = (unsigned char)s[i];
  390. if (c == '&') {
  391. decode_entity(s, len, &i, out, cap, &n);
  392. continue;
  393. }
  394. if (c < 0x80) {
  395. i++;
  396. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  397. c == '\r' || c == '\f')) {
  398. if (n > 0 && out[n - 1] == ' ')
  399. continue;
  400. emit(out, cap, &n, ' ');
  401. continue;
  402. }
  403. if (!collapse && c == '\t') {
  404. emit(out, cap, &n, ' ');
  405. emit(out, cap, &n, ' ');
  406. emit(out, cap, &n, ' ');
  407. emit(out, cap, &n, ' ');
  408. continue;
  409. }
  410. emit(out, cap, &n, (char)c);
  411. continue;
  412. }
  413. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  414. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  415. unsigned int cp = ((c & 0x1F) << 6) |
  416. ((unsigned char)s[i + 1] & 0x3F);
  417. i += 2;
  418. if (cp == 0xA0)
  419. cp = ' ';
  420. else if (cp == 0xB7)
  421. cp = '-';
  422. else if (cp == 0xD7)
  423. cp = 'x';
  424. else if (cp == 0xB0)
  425. cp = 'o';
  426. if (cp <= 0xFF)
  427. emit(out, cap, &n, (char)cp);
  428. else
  429. emit(out, cap, &n, '?');
  430. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  431. unsigned int cp = ((c & 0x0F) << 12) |
  432. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  433. ((unsigned char)s[i + 2] & 0x3F);
  434. i += 3;
  435. if (cp == 0x2013 || cp == 0x2014) {
  436. emit(out, cap, &n, '-');
  437. } else if (cp == 0x2018 || cp == 0x2019) {
  438. emit(out, cap, &n, '\'');
  439. } else if (cp == 0x201C || cp == 0x201D) {
  440. emit(out, cap, &n, '"');
  441. } else if (cp == 0x2026) {
  442. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  443. emit(out, cap, &n, '.');
  444. } else if (cp == 0x2192) {
  445. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  446. } else if (cp == 0x2264) {
  447. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  448. } else {
  449. emit(out, cap, &n, '?');
  450. }
  451. } else {
  452. i++;
  453. emit(out, cap, &n, '?');
  454. }
  455. }
  456. out[n] = '\0';
  457. return n;
  458. }
  459. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  460. * space / empty. */
  461. /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
  462. * cap silently dropped every class past it (arstechnica's header lost its
  463. * background, alignment and all responsive variants). */
  464. #define BR_CLASS_MAX 2048
  465. #define BR_STYLE_ATTR_MAX 2048
  466. static unsigned int store_attr(struct br_doc *d, const char *s,
  467. unsigned int len, unsigned int limit)
  468. {
  469. unsigned int n;
  470. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  471. return 0;
  472. if (len > limit)
  473. len = limit;
  474. n = decode_text(s, len, d->attrs + d->attrs_used,
  475. BR_ATTR_SIZE - d->attrs_used, 0);
  476. if (n == 0)
  477. return 0;
  478. {
  479. unsigned int off = d->attrs_used;
  480. d->attrs_used += n + 1;
  481. return off;
  482. }
  483. }
  484. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  485. {
  486. return off ? d->attrs + off : "";
  487. }
  488. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  489. static unsigned int store_raw(struct br_doc *d, const char *s,
  490. unsigned int len, unsigned int limit)
  491. {
  492. unsigned int off;
  493. if (len > limit)
  494. len = limit;
  495. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  496. return 0;
  497. off = d->attrs_used;
  498. {
  499. unsigned int i;
  500. for (i = 0; i < len; i++)
  501. d->attrs[off + i] = s[i];
  502. }
  503. d->attrs[off + len] = '\0';
  504. d->attrs_used += len + 1;
  505. return off;
  506. }
  507. #define VAR_HASH_SIZE 1024
  508. static unsigned int var_hash(const char *name, unsigned int len)
  509. {
  510. unsigned int i, h = 2166136261u;
  511. for (i = 0; i < len; i++) {
  512. h ^= (unsigned char)name[i];
  513. h *= 16777619u;
  514. }
  515. return h & (VAR_HASH_SIZE - 1);
  516. }
  517. static unsigned int css_var_find(struct br_doc *d, const char *name,
  518. unsigned int nlen)
  519. {
  520. unsigned int h = var_hash(name, nlen);
  521. for (;;) {
  522. unsigned int i = d->var_slot[h];
  523. if (!i)
  524. return BR_MAX_VARS;
  525. i--;
  526. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  527. const char *n = attr_ptr(d, d->var_name[i]);
  528. unsigned int j;
  529. for (j = 0; j < nlen && n[j] == name[j]; j++)
  530. ;
  531. if (j == nlen)
  532. return i;
  533. }
  534. h = (h + 1) & (VAR_HASH_SIZE - 1);
  535. }
  536. }
  537. #define BR_VAR_ROOT 1
  538. #define BR_VAR_CONFLICT 2
  539. /* Custom properties live in one document-wide table (var() is expanded
  540. * when a sheet is parsed, not per element). Definitions on :root, html,
  541. * body or * are the theme and always apply, last one winning. A definition
  542. * on any other selector is a local override: it is used while it is the
  543. * only value seen for that name, and a second, different local value marks
  544. * the name ambiguous so var() falls back. Last-wins across components was
  545. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  546. * .fa-fw, and every icon became 100% wide. */
  547. static void css_var_set(struct br_doc *d, const char *name,
  548. unsigned int nlen, const char *val, unsigned int vlen,
  549. int root)
  550. {
  551. unsigned int i, noff, voff;
  552. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  553. return;
  554. i = css_var_find(d, name, nlen);
  555. if (i < BR_MAX_VARS && !root) {
  556. const char *old = attr_ptr(d, d->var_val[i]);
  557. unsigned int k = 0;
  558. if (d->var_flags[i] & BR_VAR_ROOT)
  559. return;
  560. while (k < vlen && old[k] == val[k])
  561. k++;
  562. if (!(k == vlen && old[k] == '\0'))
  563. d->var_flags[i] |= BR_VAR_CONFLICT;
  564. return;
  565. }
  566. noff = store_raw(d, name, nlen, 63);
  567. voff = store_raw(d, val, vlen, 199);
  568. if (!noff || !voff)
  569. return;
  570. if (i >= BR_MAX_VARS) {
  571. unsigned int h;
  572. if (d->nvars >= BR_MAX_VARS)
  573. return;
  574. i = d->nvars++;
  575. d->var_name[i] = noff;
  576. h = var_hash(name, nlen);
  577. while (d->var_slot[h])
  578. h = (h + 1) & (VAR_HASH_SIZE - 1);
  579. d->var_slot[h] = (unsigned short)(i + 1);
  580. d->var_flags[i] = 0;
  581. }
  582. d->var_val[i] = voff;
  583. if (root)
  584. d->var_flags[i] = BR_VAR_ROOT;
  585. }
  586. /* 1 when every selector of the list targets the document root (the theme
  587. * scope for custom properties): :root, html, body, * or :host, optionally
  588. * with classes/attributes but no combinator. */
  589. static int css_root_selector(const char *s, unsigned int len)
  590. {
  591. unsigned int i = 0;
  592. while (i < len) {
  593. unsigned int st, en;
  594. while (i < len && (is_space(s[i]) || s[i] == ','))
  595. i++;
  596. if (i >= len)
  597. break;
  598. st = i;
  599. while (i < len && s[i] != ',')
  600. i++;
  601. en = i;
  602. while (en > st && is_space(s[en - 1]))
  603. en--;
  604. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  605. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  606. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  607. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  608. s[st] == '*'))
  609. return 0;
  610. for (; st < en; st++)
  611. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  612. s[st] == '~')
  613. return 0;
  614. }
  615. return 1;
  616. }
  617. /* Find the end of a declaration value: the next ';' outside quotes and
  618. * parentheses. The caller passes the position just after the ':'. */
  619. static unsigned int css_value_end(const char *s, unsigned int len,
  620. unsigned int i)
  621. {
  622. int depth = 0;
  623. char quote = 0;
  624. while (i < len) {
  625. char c = s[i];
  626. if (quote) {
  627. if (c == quote)
  628. quote = 0;
  629. else if (c == '\\' && i + 1 < len)
  630. i++;
  631. } else if (c == '"' || c == '\'') {
  632. quote = c;
  633. } else if (c == '(') {
  634. depth++;
  635. } else if (c == ')' && depth > 0) {
  636. depth--;
  637. } else if (c == ';' && depth == 0) {
  638. break;
  639. }
  640. i++;
  641. }
  642. return i;
  643. }
  644. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  645. * second palette. We render the light page, so skip their custom-property
  646. * definitions; otherwise their last-wins values leak into the light rules. */
  647. static int css_dark_selector(const char *s, unsigned int len)
  648. {
  649. unsigned int i;
  650. for (i = 0; i + 4 <= len; i++) {
  651. if ((s[i] == 'n' || s[i] == 'N') &&
  652. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  653. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  654. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  655. (s[i + 4] == 't' || s[i + 4] == 'T'))
  656. return 1;
  657. if ((s[i] == 'd' || s[i] == 'D') &&
  658. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  659. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  660. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  661. return 1;
  662. if (i + 12 <= len &&
  663. (s[i] == 'c' || s[i] == 'C') &&
  664. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  665. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  666. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  667. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  668. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  669. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  670. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  671. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  672. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  673. s[i + 10] == '-' &&
  674. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  675. (s[i + 12] == 's' || s[i + 12] == 'S'))
  676. return 1;
  677. }
  678. return 0;
  679. }
  680. /* Collect --custom-property definitions from one declaration block. */
  681. static void css_collect_block(struct br_doc *d, const char *s,
  682. unsigned int len, int root)
  683. {
  684. unsigned int i = 0;
  685. while (i < len) {
  686. unsigned int pstart, pend, vstart, vend;
  687. while (i < len && (is_space(s[i]) || s[i] == ';'))
  688. i++;
  689. if (i >= len)
  690. break;
  691. pstart = i;
  692. while (i < len && s[i] != ':' && s[i] != ';')
  693. i++;
  694. pend = i;
  695. if (i >= len || s[i] != ':') {
  696. if (i < len)
  697. i++;
  698. continue;
  699. }
  700. i++;
  701. vstart = i;
  702. i = css_value_end(s, len, i);
  703. vend = i;
  704. if (i < len)
  705. i++;
  706. while (pstart < pend && is_space(s[pstart]))
  707. pstart++;
  708. while (pend > pstart && is_space(s[pend - 1]))
  709. pend--;
  710. while (vstart < vend && is_space(s[vstart]))
  711. vstart++;
  712. while (vend > vstart && is_space(s[vend - 1]))
  713. vend--;
  714. if (vend - vstart >= 10 &&
  715. ci_eq_n(s + vend - 10, "!important", 10)) {
  716. vend -= 10;
  717. while (vend > vstart && is_space(s[vend - 1]))
  718. vend--;
  719. }
  720. if (pend - pstart >= 3 && s[pstart] == '-' &&
  721. s[pstart + 1] == '-')
  722. css_var_set(d, s + pstart, pend - pstart,
  723. s + vstart, vend - vstart, root);
  724. }
  725. }
  726. const char *br_href(struct br_doc *d, unsigned int node)
  727. {
  728. return attr_ptr(d, d->nodes[node].href_off);
  729. }
  730. const char *br_src(struct br_doc *d, unsigned int node)
  731. {
  732. return attr_ptr(d, d->nodes[node].src_off);
  733. }
  734. const char *br_alt(struct br_doc *d, unsigned int node)
  735. {
  736. return attr_ptr(d, d->nodes[node].alt_off);
  737. }
  738. const char *br_rel(struct br_doc *d, unsigned int node)
  739. {
  740. return attr_ptr(d, d->nodes[node].rel_off);
  741. }
  742. const char *br_value(struct br_doc *d, unsigned int node)
  743. {
  744. return attr_ptr(d, d->nodes[node].src_off);
  745. }
  746. const char *br_placeholder(struct br_doc *d, unsigned int node)
  747. {
  748. return attr_ptr(d, d->nodes[node].alt_off);
  749. }
  750. const char *br_type(struct br_doc *d, unsigned int node)
  751. {
  752. return attr_ptr(d, d->nodes[node].rel_off);
  753. }
  754. /* ---- document setup / node append ---- */
  755. static void rule_index_reset(struct br_doc *d)
  756. {
  757. unsigned int i;
  758. for (i = 0; i < BR_MAX_RULES; i++)
  759. d->rule_next[i] = 0xFFFF;
  760. for (i = 0; i < BR_RULE_HASH; i++)
  761. d->rule_bucket[i] = 0xFFFF;
  762. for (i = 0; i < BR_TAG_MAX; i++)
  763. d->rule_tag[i] = 0xFFFF;
  764. d->rule_any = 0xFFFF;
  765. }
  766. void br_doc_init(struct br_doc *d)
  767. {
  768. unsigned int i;
  769. for (i = 0; i < sizeof(*d); i++)
  770. ((unsigned char *)d)[i] = 0;
  771. rule_index_reset(d);
  772. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  773. d->text = malloc(BR_TEXT_SIZE);
  774. d->attrs = malloc(BR_ATTR_SIZE);
  775. if (!d->nodes || !d->text || !d->attrs) {
  776. free(d->nodes);
  777. free(d->text);
  778. free(d->attrs);
  779. d->nodes = 0;
  780. d->text = 0;
  781. d->attrs = 0;
  782. return;
  783. }
  784. d->rules = rule_fallback;
  785. d->rule_cap = BR_RULE_FALLBACK;
  786. d->attrs_used = 1; /* offset 0 = "no attribute" */
  787. d->attrs[0] = '\0';
  788. d->nvars = 0;
  789. d->root = 0;
  790. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  791. d->var_slot[vi] = 0;
  792. root_font_px = 16;
  793. bg_url_count = 0;
  794. font_reset();
  795. }
  796. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  797. unsigned int cap)
  798. {
  799. if (!buf || !cap) {
  800. d->rules = rule_fallback;
  801. d->rule_cap = BR_RULE_FALLBACK;
  802. d->nrules = 0;
  803. return;
  804. }
  805. if (cap > BR_MAX_RULES)
  806. cap = BR_MAX_RULES;
  807. d->rules = buf;
  808. d->rule_cap = cap;
  809. d->nrules = 0;
  810. }
  811. static void doc_reset(struct br_doc *d)
  812. {
  813. unsigned int i;
  814. d->nnode = 0;
  815. d->text_used = 0;
  816. d->attrs_used = 1;
  817. d->attrs[0] = '\0';
  818. d->nrules = 0;
  819. d->ncontent = 0;
  820. d->npe_rules = 0;
  821. rule_index_reset(d);
  822. d->title[0] = '\0';
  823. d->inline_css_len = 0;
  824. d->nvars = 0;
  825. d->truncated = 0;
  826. d->plain = 0;
  827. d->script_src = 0;
  828. d->nscripts = 0;
  829. d->scripts_truncated = 0;
  830. font_reset();
  831. for (i = 0; i < BR_MAX_NODES; i++) {
  832. d->nodes[i].type = BR_NODE_ELEM;
  833. d->nodes[i].tag = BR_TAG_UNKNOWN;
  834. d->nodes[i].flags = 0;
  835. d->nodes[i].parent = 0;
  836. d->nodes[i].first = d->nodes[i].last = 0;
  837. d->nodes[i].next = 0;
  838. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  839. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  840. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  841. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  842. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  843. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  844. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  845. d->nodes[i].name_off = 0;
  846. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  847. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  848. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  849. d->nodes[i].svg_rx = 0;
  850. d->nodes[i].img_index = -1;
  851. d->nodes[i].bg_img_index = -1;
  852. d->nodes[i].style_valid = 0;
  853. d->nodes[i].pe_state = 0;
  854. }
  855. }
  856. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  857. unsigned char type, unsigned char tag)
  858. {
  859. unsigned int idx;
  860. struct br_node *n;
  861. if (d->nnode >= BR_MAX_NODES) {
  862. d->truncated = 1;
  863. return 0;
  864. }
  865. idx = d->nnode++;
  866. n = &d->nodes[idx];
  867. n->type = type;
  868. n->tag = tag;
  869. n->parent = (unsigned short)parent;
  870. n->img_index = -1;
  871. n->bg_img_index = -1;
  872. if (d->nodes[parent].last)
  873. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  874. else
  875. d->nodes[parent].first = (unsigned short)idx;
  876. d->nodes[parent].last = (unsigned short)idx;
  877. return idx;
  878. }
  879. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  880. const char *s, unsigned int len, int pre)
  881. {
  882. char *dst;
  883. unsigned int n;
  884. unsigned int idx;
  885. if (len == 0)
  886. return 0;
  887. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  888. d->truncated = 1;
  889. return 0;
  890. }
  891. dst = d->text + d->text_used;
  892. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  893. if (n == 0) {
  894. d->text[d->text_used] = '\0';
  895. return 0;
  896. }
  897. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  898. if (!idx)
  899. return 0;
  900. d->nodes[idx].text_off = d->text_used;
  901. d->nodes[idx].text_len = (unsigned short)n;
  902. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  903. d->text_used += n + 1;
  904. return idx;
  905. }
  906. /* Raw text child (no entity decoding, whitespace kept): the body of a
  907. * data-block <script> such as type="application/json", which scripts read
  908. * through textContent. Bodies longer than a text node holds are dropped. */
  909. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  910. const char *s, unsigned int len)
  911. {
  912. unsigned int idx, i;
  913. if (len == 0 || len > 0xFFFFu)
  914. return 0;
  915. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  916. d->truncated = 1;
  917. return 0;
  918. }
  919. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  920. if (!idx)
  921. return 0;
  922. for (i = 0; i < len; i++)
  923. d->text[d->text_used + i] = s[i];
  924. d->text[d->text_used + len] = '\0';
  925. d->nodes[idx].text_off = d->text_used;
  926. d->nodes[idx].text_len = (unsigned short)len;
  927. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  928. d->text_used += len + 1;
  929. return idx;
  930. }
  931. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  932. static int same_string(const char *a, const char *b)
  933. {
  934. while (*a && *a == *b) {
  935. a++;
  936. b++;
  937. }
  938. return *a == *b;
  939. }
  940. static int same_string_n(const char *a, const char *b, unsigned int n)
  941. {
  942. unsigned int i;
  943. for (i = 0; i < n; i++)
  944. if (a[i] != b[i])
  945. return 0;
  946. return 1;
  947. }
  948. static int node_detached(struct br_doc *d, unsigned int node)
  949. {
  950. unsigned int guard = 0;
  951. while (node && node < d->nnode && guard++ <= d->nnode) {
  952. if (d->nodes[node].flags & BR_NODE_DETACHED)
  953. return 1;
  954. node = d->nodes[node].parent;
  955. }
  956. return 0;
  957. }
  958. unsigned int br_find_id(struct br_doc *d, const char *id)
  959. {
  960. unsigned int i;
  961. if (!d || !id)
  962. return 0;
  963. for (i = 0; i < d->nnode; i++) {
  964. struct br_node *n = &d->nodes[i];
  965. if (n->type == BR_NODE_ELEM && n->id_off &&
  966. same_string(d->attrs + n->id_off, id) &&
  967. !node_detached(d, i))
  968. return i + 1;
  969. }
  970. return 0;
  971. }
  972. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  973. unsigned int cap, unsigned int *used)
  974. {
  975. unsigned int c;
  976. if (node >= d->nnode)
  977. return;
  978. if (d->nodes[node].type == BR_NODE_TEXT) {
  979. unsigned int len = d->nodes[node].text_len;
  980. unsigned int off = d->nodes[node].text_off;
  981. if (*used + len >= cap)
  982. len = *used + 1 < cap ? cap - *used - 1 : 0;
  983. for (c = 0; c < len; c++)
  984. out[*used + c] = d->text[off + c];
  985. *used += len;
  986. return;
  987. }
  988. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  989. text_walk(d, c, out, cap, used);
  990. }
  991. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  992. char *out, unsigned int cap)
  993. {
  994. unsigned int used = 0;
  995. if (!d || !out || cap == 0 || node >= d->nnode)
  996. return 0;
  997. text_walk(d, node, out, cap, &used);
  998. out[used] = '\0';
  999. return used;
  1000. }
  1001. int br_set_text_content(struct br_doc *d, unsigned int node,
  1002. const char *text, unsigned int len)
  1003. {
  1004. struct br_node *n;
  1005. unsigned int idx;
  1006. unsigned int i;
  1007. if (!d || node >= d->nnode)
  1008. return -1;
  1009. /* Detach the old subtree; its arena storage is reclaimed on the next
  1010. * navigation. */
  1011. {
  1012. unsigned int c;
  1013. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1014. d->nodes[c].flags |= BR_NODE_DETACHED;
  1015. }
  1016. d->nodes[node].first = d->nodes[node].last = 0;
  1017. d->nodes[node].style_valid = 0;
  1018. d->nodes[node].pe_state = 0;
  1019. if (len == 0)
  1020. return 0;
  1021. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1022. d->truncated = 1;
  1023. return -1;
  1024. }
  1025. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1026. if (!idx)
  1027. return -1;
  1028. for (i = 0; i < len; i++)
  1029. d->text[d->text_used + i] = text[i];
  1030. d->text[d->text_used + len] = '\0';
  1031. n = &d->nodes[idx];
  1032. n->text_off = d->text_used;
  1033. n->text_len = (unsigned short)len;
  1034. n->flags = 0;
  1035. d->text_used += len + 1;
  1036. return 0;
  1037. }
  1038. /* Replace the value of an existing text or comment node in place, keeping the
  1039. * node handle (and therefore the JS wrapper) valid. */
  1040. int br_set_text_node(struct br_doc *d, unsigned int node,
  1041. const char *text, unsigned int len)
  1042. {
  1043. struct br_node *n;
  1044. unsigned int i;
  1045. if (!d || node >= d->nnode)
  1046. return -1;
  1047. n = &d->nodes[node];
  1048. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1049. return -1;
  1050. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1051. d->truncated = 1;
  1052. return -1;
  1053. }
  1054. for (i = 0; i < len; i++)
  1055. d->text[d->text_used + i] = text[i];
  1056. d->text[d->text_used + len] = '\0';
  1057. n->text_off = d->text_used;
  1058. n->text_len = (unsigned short)len;
  1059. d->text_used += len + 1;
  1060. return 0;
  1061. }
  1062. /* ---- runtime DOM construction and mutation ---- */
  1063. static unsigned int text_len(const char *s)
  1064. {
  1065. unsigned int n = 0;
  1066. while (s && s[n])
  1067. n++;
  1068. return n;
  1069. }
  1070. static void node_zero(struct br_node *n)
  1071. {
  1072. unsigned int i;
  1073. for (i = 0; i < sizeof(*n); i++)
  1074. ((unsigned char *)n)[i] = 0;
  1075. n->img_index = -1;
  1076. n->bg_img_index = -1;
  1077. }
  1078. static void node_unlink(struct br_doc *d, unsigned int handle)
  1079. {
  1080. unsigned int node, parent, c, prev = 0;
  1081. if (!d || handle == 0 || handle > d->nnode)
  1082. return;
  1083. node = handle - 1;
  1084. parent = d->nodes[node].parent;
  1085. if (parent >= d->nnode) {
  1086. d->nodes[node].parent = 0;
  1087. d->nodes[node].next = 0;
  1088. return;
  1089. }
  1090. c = d->nodes[parent].first;
  1091. while (c && c != node) {
  1092. prev = c;
  1093. c = d->nodes[c].next;
  1094. }
  1095. if (c == node) {
  1096. if (prev)
  1097. d->nodes[prev].next = d->nodes[node].next;
  1098. else
  1099. d->nodes[parent].first = d->nodes[node].next;
  1100. if (d->nodes[parent].last == node)
  1101. d->nodes[parent].last = (unsigned short)prev;
  1102. }
  1103. d->nodes[node].parent = 0;
  1104. d->nodes[node].next = 0;
  1105. d->nodes[node].flags |= BR_NODE_DETACHED;
  1106. }
  1107. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1108. const char *s, unsigned int len);
  1109. static unsigned int find_tag_end(const char *s, unsigned int i,
  1110. unsigned int len)
  1111. {
  1112. char quote = 0;
  1113. while (i < len) {
  1114. char c = s[i];
  1115. if (quote) {
  1116. if (c == quote)
  1117. quote = 0;
  1118. } else if (c == '"' || c == '\'') {
  1119. quote = c;
  1120. } else if (c == '>') {
  1121. return i;
  1122. }
  1123. i++;
  1124. }
  1125. return len;
  1126. }
  1127. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1128. const char *html, unsigned int len)
  1129. {
  1130. unsigned int stack[64];
  1131. unsigned int sp = 1, i = 0;
  1132. if (!d || node >= d->nnode)
  1133. return -1;
  1134. stack[0] = node;
  1135. {
  1136. unsigned int c;
  1137. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1138. d->nodes[c].flags |= BR_NODE_DETACHED;
  1139. }
  1140. d->nodes[node].first = d->nodes[node].last = 0;
  1141. d->nodes[node].style_valid = 0;
  1142. d->nodes[node].pe_state = 0;
  1143. while (i < len) {
  1144. if (html[i] != '<') {
  1145. unsigned int start = i, end;
  1146. while (i < len && html[i] != '<')
  1147. i++;
  1148. end = i;
  1149. if (end > start) {
  1150. /* Entities decoded like the page parser does
  1151. * (createTextNode stays literal). */
  1152. char *tb = (char *)malloc(end - start + 1);
  1153. unsigned int tl = 0, h = 0;
  1154. if (tb) {
  1155. tl = decode_text(html + start, end - start,
  1156. tb, end - start + 1, 1);
  1157. h = br_create_text(d, tb, tl);
  1158. free(tb);
  1159. }
  1160. if (h)
  1161. br_append_child(d, stack[sp - 1], h);
  1162. }
  1163. continue;
  1164. }
  1165. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1166. html[i + 3] == '-') {
  1167. int p = find_ci(html, len, "-->", 3, i + 4);
  1168. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1169. unsigned int h = br_create_comment(d, html + i + 4,
  1170. end > i + 4 ?
  1171. end - i - 4 : 0);
  1172. if (h)
  1173. br_append_child(d, stack[sp - 1], h);
  1174. i = p >= 0 ? (unsigned int)p + 3 : len;
  1175. continue;
  1176. }
  1177. if (html[i + 1] == '/') {
  1178. unsigned int p = find_tag_end(html, i + 2, len);
  1179. i = p < len ? p + 1 : len;
  1180. if (sp > 1)
  1181. sp--;
  1182. continue;
  1183. }
  1184. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1185. unsigned int p = find_tag_end(html, i + 2, len);
  1186. i = p < len ? p + 1 : len;
  1187. continue;
  1188. }
  1189. {
  1190. unsigned int p = find_tag_end(html, i + 1, len);
  1191. unsigned int ns = i + 1, ne, as, ae;
  1192. unsigned int h, k;
  1193. char name[32];
  1194. int selfclose = 0;
  1195. while (ns < p && is_space(html[ns]))
  1196. ns++;
  1197. ne = ns;
  1198. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1199. ne++;
  1200. as = ne;
  1201. ae = p;
  1202. if (ae > as && html[ae - 1] == '/') {
  1203. selfclose = 1;
  1204. ae--;
  1205. }
  1206. if (ne == ns) {
  1207. i = p < len ? p + 1 : len;
  1208. continue;
  1209. }
  1210. if (ne - ns >= sizeof(name)) {
  1211. i = p < len ? p + 1 : len;
  1212. continue;
  1213. }
  1214. for (k = 0; k < ne - ns; k++)
  1215. name[k] = html[ns + k];
  1216. name[ne - ns] = '\0';
  1217. h = br_create_element(d, name, ne - ns);
  1218. if (!h) {
  1219. i = p < len ? p + 1 : len;
  1220. continue;
  1221. }
  1222. if (ae > as)
  1223. parse_attrs(d, &d->nodes[h - 1], html + as,
  1224. ae - as);
  1225. br_append_child(d, stack[sp - 1], h);
  1226. {
  1227. unsigned int tag = d->nodes[h - 1].tag;
  1228. if (!selfclose && !br_tag_is_void(tag) &&
  1229. sp < sizeof(stack) / sizeof(stack[0]))
  1230. stack[sp++] = h - 1;
  1231. }
  1232. i = p < len ? p + 1 : len;
  1233. }
  1234. }
  1235. return 0;
  1236. }
  1237. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1238. {
  1239. unsigned int i;
  1240. if (!d)
  1241. return 0;
  1242. for (i = 0; i < d->nnode; i++)
  1243. if (d->nodes[i].type == BR_NODE_ELEM &&
  1244. d->nodes[i].tag == (unsigned char)tag &&
  1245. !node_detached(d, i))
  1246. return i + 1;
  1247. return 0;
  1248. }
  1249. unsigned int br_create_element(struct br_doc *d, const char *name,
  1250. unsigned int len)
  1251. {
  1252. struct br_node *n;
  1253. unsigned int idx;
  1254. if (!d || !name)
  1255. return 0;
  1256. if (d->nnode >= BR_MAX_NODES) {
  1257. d->truncated = 1;
  1258. return 0;
  1259. }
  1260. idx = d->nnode++;
  1261. n = &d->nodes[idx];
  1262. node_zero(n);
  1263. n->type = BR_NODE_ELEM;
  1264. n->flags = BR_NODE_DETACHED;
  1265. n->tag = (unsigned char)br_tag_from_name(name);
  1266. if (n->tag == BR_TAG_UNKNOWN && len)
  1267. n->name_off = store_attr(d, name, len, 31);
  1268. return idx + 1;
  1269. }
  1270. unsigned int br_create_text(struct br_doc *d, const char *text,
  1271. unsigned int len)
  1272. {
  1273. struct br_node *n;
  1274. unsigned int idx, i;
  1275. if (!d)
  1276. return 0;
  1277. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1278. d->truncated = 1;
  1279. return 0;
  1280. }
  1281. idx = d->nnode++;
  1282. n = &d->nodes[idx];
  1283. node_zero(n);
  1284. n->type = BR_NODE_TEXT;
  1285. n->flags = BR_NODE_DETACHED;
  1286. n->text_off = d->text_used;
  1287. n->text_len = (unsigned short)len;
  1288. for (i = 0; i < len; i++)
  1289. d->text[d->text_used + i] = text[i];
  1290. d->text[d->text_used + len] = '\0';
  1291. d->text_used += len + 1;
  1292. return idx + 1;
  1293. }
  1294. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1295. unsigned int len)
  1296. {
  1297. unsigned int h = br_create_text(d, text, len);
  1298. if (h)
  1299. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1300. return h;
  1301. }
  1302. unsigned int br_create_fragment(struct br_doc *d)
  1303. {
  1304. struct br_node *n;
  1305. unsigned int idx;
  1306. if (!d)
  1307. return 0;
  1308. if (d->nnode >= BR_MAX_NODES) {
  1309. d->truncated = 1;
  1310. return 0;
  1311. }
  1312. idx = d->nnode++;
  1313. n = &d->nodes[idx];
  1314. node_zero(n);
  1315. n->type = BR_NODE_FRAGMENT;
  1316. n->flags = BR_NODE_DETACHED;
  1317. return idx + 1;
  1318. }
  1319. int br_append_child(struct br_doc *d, unsigned int parent,
  1320. unsigned int child)
  1321. {
  1322. unsigned int p;
  1323. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1324. parent == child - 1)
  1325. return -1;
  1326. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1327. /* Appending a fragment inserts its children, not the fragment. */
  1328. unsigned int c = d->nodes[child - 1].first;
  1329. while (c) {
  1330. unsigned int next = d->nodes[c].next;
  1331. br_append_child(d, parent, c + 1);
  1332. c = next;
  1333. }
  1334. return 0;
  1335. }
  1336. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1337. if (p == child - 1)
  1338. return -1;
  1339. node_unlink(d, child);
  1340. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1341. d->nodes[child - 1].parent = (unsigned short)parent;
  1342. d->nodes[child - 1].next = 0;
  1343. if (d->nodes[parent].last)
  1344. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1345. else
  1346. d->nodes[parent].first = (unsigned short)(child - 1);
  1347. d->nodes[parent].last = (unsigned short)(child - 1);
  1348. return 0;
  1349. }
  1350. int br_insert_before(struct br_doc *d, unsigned int parent,
  1351. unsigned int child, unsigned int before)
  1352. {
  1353. unsigned int prev = 0, c;
  1354. if (before == 0)
  1355. return br_append_child(d, parent, child);
  1356. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1357. before > d->nnode || parent == child - 1)
  1358. return -1;
  1359. if (d->nodes[before - 1].parent != parent)
  1360. return -1;
  1361. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1362. unsigned int c = d->nodes[child - 1].first;
  1363. while (c) {
  1364. unsigned int next = d->nodes[c].next;
  1365. br_insert_before(d, parent, c + 1, before);
  1366. c = next;
  1367. }
  1368. return 0;
  1369. }
  1370. {
  1371. unsigned int p;
  1372. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1373. if (p == child - 1)
  1374. return -1;
  1375. }
  1376. node_unlink(d, child);
  1377. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1378. c = d->nodes[parent].first;
  1379. while (c && c != before - 1) {
  1380. prev = c;
  1381. c = d->nodes[c].next;
  1382. }
  1383. if (c != before - 1)
  1384. return -1;
  1385. d->nodes[child - 1].parent = (unsigned short)parent;
  1386. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1387. if (prev)
  1388. d->nodes[prev].next = (unsigned short)(child - 1);
  1389. else
  1390. d->nodes[parent].first = (unsigned short)(child - 1);
  1391. return 0;
  1392. }
  1393. int br_remove_child(struct br_doc *d, unsigned int parent,
  1394. unsigned int child)
  1395. {
  1396. if (!d || child == 0 || child > d->nnode ||
  1397. d->nodes[child - 1].parent != parent)
  1398. return -1;
  1399. node_unlink(d, child);
  1400. return 0;
  1401. }
  1402. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1403. unsigned int child)
  1404. {
  1405. unsigned int c;
  1406. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1407. return 0;
  1408. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1409. if (c == child - 1)
  1410. return child;
  1411. return 0;
  1412. }
  1413. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1414. const char *name)
  1415. {
  1416. struct br_node *n;
  1417. const char *p, *end, *found = 0;
  1418. if (!d || node >= d->nnode || !name)
  1419. return 0;
  1420. n = &d->nodes[node];
  1421. p = d->attrs + n->extra_off;
  1422. end = p + n->extra_len;
  1423. while (p + 1 < end) {
  1424. const char *an = p;
  1425. const char *av;
  1426. p += text_len(p) + 1;
  1427. if (p >= end)
  1428. break;
  1429. av = p;
  1430. p += text_len(p) + 1;
  1431. if (same_string(an, name))
  1432. found = av;
  1433. }
  1434. if (found)
  1435. return found;
  1436. if (same_string(name, "id") && n->id_off)
  1437. return d->attrs + n->id_off;
  1438. if (same_string(name, "class") && n->class_off)
  1439. return d->attrs + n->class_off;
  1440. if (same_string(name, "style") && n->style_off)
  1441. return d->attrs + n->style_off;
  1442. if (same_string(name, "href") && n->href_off)
  1443. return d->attrs + n->href_off;
  1444. if (same_string(name, "src") && n->src_off)
  1445. return d->attrs + n->src_off;
  1446. if (same_string(name, "alt") && n->alt_off)
  1447. return d->attrs + n->alt_off;
  1448. if (same_string(name, "rel") && n->rel_off)
  1449. return d->attrs + n->rel_off;
  1450. return 0;
  1451. }
  1452. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1453. const char *name, const char *value,
  1454. unsigned int len)
  1455. {
  1456. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1457. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1458. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1459. * exactly like parse_attrs(); script-built inputs (Vue sets
  1460. * type="password" after createElement) must land there too. */
  1461. if (control && same_string(name, "type")) {
  1462. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1463. return;
  1464. }
  1465. if (control && same_string(name, "value")) {
  1466. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1467. return;
  1468. }
  1469. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1470. same_string(name, "placeholder")) {
  1471. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1472. return;
  1473. }
  1474. if (same_string(name, "id"))
  1475. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1476. else if (same_string(name, "class"))
  1477. n->class_off = len ? store_attr(d, value, len,
  1478. BR_CLASS_MAX) : 0;
  1479. else if (same_string(name, "style"))
  1480. n->style_off = len ? store_attr(d, value, len,
  1481. BR_STYLE_ATTR_MAX) : 0;
  1482. else if (same_string(name, "href"))
  1483. n->href_off = len ? store_attr(d, value, len, 480) : 0;
  1484. else if (same_string(name, "src"))
  1485. n->src_off = len ? store_attr(d, value, len, 400) : 0;
  1486. else if (same_string(name, "alt"))
  1487. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1488. else if (same_string(name, "rel"))
  1489. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1490. }
  1491. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1492. const char *value, unsigned int len)
  1493. {
  1494. struct br_node *n;
  1495. unsigned int start, nl, need;
  1496. const char *p, *old, *oldend;
  1497. if (!d || node >= d->nnode || !name)
  1498. return -1;
  1499. n = &d->nodes[node];
  1500. nl = text_len(name);
  1501. need = nl + 1 + len + 1;
  1502. start = d->attrs_used;
  1503. old = d->attrs + n->extra_off;
  1504. oldend = old + n->extra_len;
  1505. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1506. d->truncated = 1;
  1507. return -1;
  1508. }
  1509. p = old;
  1510. while (p + 1 < oldend) {
  1511. const char *an = p;
  1512. const char *av;
  1513. unsigned int anl, avl, k;
  1514. anl = text_len(an);
  1515. p += anl + 1;
  1516. if (p >= oldend)
  1517. break;
  1518. av = p;
  1519. avl = text_len(av);
  1520. p += avl + 1;
  1521. if (same_string(an, name))
  1522. continue;
  1523. for (k = 0; k < anl; k++)
  1524. d->attrs[d->attrs_used++] = an[k];
  1525. d->attrs[d->attrs_used++] = '\0';
  1526. for (k = 0; k < avl; k++)
  1527. d->attrs[d->attrs_used++] = av[k];
  1528. d->attrs[d->attrs_used++] = '\0';
  1529. }
  1530. {
  1531. unsigned int k;
  1532. for (k = 0; k < nl; k++)
  1533. d->attrs[d->attrs_used++] = name[k];
  1534. d->attrs[d->attrs_used++] = '\0';
  1535. for (k = 0; k < len; k++)
  1536. d->attrs[d->attrs_used++] = value[k];
  1537. d->attrs[d->attrs_used++] = '\0';
  1538. }
  1539. n = &d->nodes[node];
  1540. n->extra_off = start;
  1541. n->extra_len = (unsigned short)(d->attrs_used - start);
  1542. n->style_valid = 0;
  1543. n->pe_state = 0;
  1544. attr_note_known(d, n, name, value, len);
  1545. /* A script that sets a style/class (Angular's ng-style) or a new src
  1546. * after the first image scan re-arms the node for the loader; -2
  1547. * ("nothing to load") would otherwise be final. */
  1548. if ((same_string(name, "style") || same_string(name, "class")) &&
  1549. n->bg_img_index == -2)
  1550. n->bg_img_index = -1;
  1551. if (same_string(name, "src") && n->img_index == -2)
  1552. n->img_index = -1;
  1553. return 0;
  1554. }
  1555. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1556. {
  1557. struct br_node *n;
  1558. unsigned int start;
  1559. const char *p, *oldend;
  1560. int found = 0;
  1561. if (!d || node >= d->nnode || !name)
  1562. return -1;
  1563. n = &d->nodes[node];
  1564. start = d->attrs_used;
  1565. p = d->attrs + n->extra_off;
  1566. oldend = p + n->extra_len;
  1567. if (start + n->extra_len > BR_ATTR_SIZE)
  1568. return -1;
  1569. while (p + 1 < oldend) {
  1570. const char *an = p;
  1571. const char *av;
  1572. unsigned int anl, avl, k;
  1573. anl = text_len(an);
  1574. p += anl + 1;
  1575. if (p >= oldend)
  1576. break;
  1577. av = p;
  1578. avl = text_len(av);
  1579. p += avl + 1;
  1580. if (same_string(an, name)) {
  1581. found = 1;
  1582. continue;
  1583. }
  1584. for (k = 0; k < anl; k++)
  1585. d->attrs[d->attrs_used++] = an[k];
  1586. d->attrs[d->attrs_used++] = '\0';
  1587. for (k = 0; k < avl; k++)
  1588. d->attrs[d->attrs_used++] = av[k];
  1589. d->attrs[d->attrs_used++] = '\0';
  1590. }
  1591. n = &d->nodes[node];
  1592. n->extra_off = start;
  1593. n->extra_len = (unsigned short)(d->attrs_used - start);
  1594. n->style_valid = 0;
  1595. n->pe_state = 0;
  1596. attr_note_known(d, n, name, "", 0);
  1597. return found ? 0 : -1;
  1598. }
  1599. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1600. {
  1601. struct br_node *src;
  1602. unsigned int h;
  1603. const char *name;
  1604. if (!d || node == 0 || node > d->nnode)
  1605. return 0;
  1606. src = &d->nodes[node - 1];
  1607. if (src->type == BR_NODE_TEXT)
  1608. return br_create_text(d, d->text + src->text_off, src->text_len);
  1609. if (src->type == BR_NODE_COMMENT)
  1610. return br_create_comment(d, d->text + src->text_off,
  1611. src->text_len);
  1612. if (src->type == BR_NODE_FRAGMENT)
  1613. h = br_create_fragment(d);
  1614. else {
  1615. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1616. name = d->attrs + src->name_off;
  1617. else
  1618. name = br_tag_name(src->tag);
  1619. h = br_create_element(d, name, text_len(name));
  1620. }
  1621. if (!h)
  1622. return 0;
  1623. {
  1624. const char *p = d->attrs + src->extra_off;
  1625. const char *end = p + src->extra_len;
  1626. while (p + 1 < end) {
  1627. const char *an = p;
  1628. const char *av;
  1629. unsigned int anl, avl;
  1630. anl = text_len(an);
  1631. p += anl + 1;
  1632. if (p >= end)
  1633. break;
  1634. av = p;
  1635. avl = text_len(av);
  1636. p += avl + 1;
  1637. /* h is a 1-based handle; br_set_attr takes the
  1638. * node index (the old call stamped the copy's
  1639. * attributes onto the previous node). */
  1640. br_set_attr(d, h - 1, an, av, avl);
  1641. }
  1642. }
  1643. if (deep) {
  1644. unsigned int c;
  1645. for (c = src->first; c; c = d->nodes[c].next) {
  1646. unsigned int ch = br_clone_node(d, c + 1, 1);
  1647. if (ch)
  1648. br_append_child(d, h - 1, ch);
  1649. }
  1650. }
  1651. return h;
  1652. }
  1653. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1654. unsigned int len);
  1655. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1656. unsigned int *st, unsigned int *en);
  1657. /* querySelector(All) and Element.matches use the cascade's selector engine
  1658. * (sel_match(), below). */
  1659. static int match_selector(struct br_doc *d, unsigned int node,
  1660. const char *sel, unsigned int len)
  1661. {
  1662. while (len > 0 && sel[0] == ' ') {
  1663. sel++;
  1664. len--;
  1665. }
  1666. while (len > 0 && sel[len - 1] == ' ')
  1667. len--;
  1668. return len && sel_match(d, node, sel, len);
  1669. }
  1670. static int query_add(unsigned int *out, unsigned int max,
  1671. unsigned int *count, unsigned int node)
  1672. {
  1673. unsigned int i;
  1674. for (i = 0; i < *count; i++)
  1675. if (out[i] == node + 1)
  1676. return 0;
  1677. if (*count < max)
  1678. out[(*count)++] = node + 1;
  1679. return 0;
  1680. }
  1681. static void query_walk(struct br_doc *d, unsigned int node,
  1682. const char *sel, unsigned int len, unsigned int *out,
  1683. unsigned int max, unsigned int *count)
  1684. {
  1685. unsigned int c;
  1686. if (d->nodes[node].type == BR_NODE_ELEM &&
  1687. match_selector(d, node, sel, len))
  1688. query_add(out, max, count, node);
  1689. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1690. query_walk(d, c, sel, len, out, max, count);
  1691. }
  1692. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1693. {
  1694. unsigned int len;
  1695. if (!d || !selector || node == 0 || node > d->nnode)
  1696. return 0;
  1697. len = text_len(selector);
  1698. while (len > 0 && selector[0] == ' ') {
  1699. selector++;
  1700. len--;
  1701. }
  1702. while (len > 0 && selector[len - 1] == ' ')
  1703. len--;
  1704. {
  1705. unsigned int pos = 0, st, en;
  1706. while (sel_list_next(selector, len, &pos, &st, &en))
  1707. if (match_selector(d, node - 1, selector + st, en - st))
  1708. return 1;
  1709. }
  1710. return 0;
  1711. }
  1712. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1713. const char *selector, unsigned int *out,
  1714. unsigned int max)
  1715. {
  1716. unsigned int count = 0, i = 0, len, start, end;
  1717. if (!d || !selector || !out || max == 0)
  1718. return 0;
  1719. len = text_len(selector);
  1720. while (sel_list_next(selector, len, &i, &start, &end)) {
  1721. if (root == 0) {
  1722. unsigned int n;
  1723. for (n = 1; n < d->nnode; n++)
  1724. if (d->nodes[n].type == BR_NODE_ELEM &&
  1725. !node_detached(d, n) &&
  1726. match_selector(d, n, selector + start,
  1727. end - start))
  1728. query_add(out, max, &count, n);
  1729. } else if (root <= d->nnode) {
  1730. unsigned int c;
  1731. for (c = d->nodes[root - 1].first; c;
  1732. c = d->nodes[c].next)
  1733. query_walk(d, c, selector + start,
  1734. end - start, out, max, &count);
  1735. }
  1736. }
  1737. return count;
  1738. }
  1739. /* ---- attribute parsing ---- */
  1740. static int is_space(char c)
  1741. {
  1742. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1743. }
  1744. static int str_eq(const char *a, const char *b);
  1745. static unsigned int parse_uint(const char *s, unsigned int len)
  1746. {
  1747. unsigned int i = 0, v = 0;
  1748. while (i < len && is_space(s[i]))
  1749. i++;
  1750. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1751. v = v * 10 + (unsigned int)(s[i] - '0');
  1752. if (v > 4096)
  1753. v = 4096;
  1754. i++;
  1755. }
  1756. return v;
  1757. }
  1758. static int contains_ci_len(const char *s, unsigned int len,
  1759. const char *needle);
  1760. static int parse_svg_viewbox(const char *s, unsigned int len,
  1761. unsigned short *w, unsigned short *h)
  1762. {
  1763. unsigned int i = 0, v[4], n = 0;
  1764. while (i < len && n < 4) {
  1765. unsigned int x = 0;
  1766. int any = 0;
  1767. while (i < len && (is_space(s[i]) || s[i] == ','))
  1768. i++;
  1769. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1770. x = x * 10 + (unsigned int)(s[i] - '0');
  1771. i++;
  1772. any = 1;
  1773. }
  1774. /* viewBox values are commonly fractional ("0 0 340.16
  1775. * 340.16"). Consume the fraction so it is not mistaken for
  1776. * the next number; the integer part is enough for the aspect
  1777. * ratio used by the renderer. */
  1778. if (i < len && s[i] == '.') {
  1779. i++;
  1780. while (i < len && s[i] >= '0' && s[i] <= '9')
  1781. i++;
  1782. }
  1783. if (!any) {
  1784. if (i < len)
  1785. i++;
  1786. continue;
  1787. }
  1788. v[n++] = x;
  1789. }
  1790. if (n < 4)
  1791. return 0;
  1792. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1793. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1794. return *w && *h;
  1795. }
  1796. static void note_attr_flag(struct br_node *n, const char *name)
  1797. {
  1798. if (str_eq(name, "data-collapse-target"))
  1799. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1800. else if (str_eq(name, "data-collapse-trigger"))
  1801. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1802. else if (str_eq(name, "is-open"))
  1803. n->attr_flags |= BR_ATTR_IS_OPEN;
  1804. else if (str_eq(name, "disabled"))
  1805. n->attr_flags |= BR_ATTR_DISABLED;
  1806. else if (str_eq(name, "open"))
  1807. n->attr_flags |= BR_ATTR_OPEN;
  1808. else if (str_eq(name, "inert"))
  1809. n->attr_flags |= BR_ATTR_INERT;
  1810. }
  1811. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1812. * second pass so the block stays contiguous even though the known attributes
  1813. * above also use the shared attr arena. */
  1814. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1815. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1816. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1817. * written. */
  1818. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1819. {
  1820. static const struct { const char *e; char c; } ents[] = {
  1821. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1822. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1823. { 0, 0 }
  1824. };
  1825. unsigned int i = 0, n = 0;
  1826. while (i < len) {
  1827. int k, done = 0;
  1828. if (s[i] == '&') {
  1829. for (k = 0; ents[k].e; k++) {
  1830. unsigned int el = (unsigned int)strlen(ents[k].e);
  1831. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1832. out[n++] = ents[k].c;
  1833. i += el;
  1834. done = 1;
  1835. break;
  1836. }
  1837. }
  1838. if (!done && i + 3 < len && s[i + 1] == '#') {
  1839. unsigned int j = i + 2, v = 0;
  1840. int hex = s[j] == 'x' || s[j] == 'X';
  1841. if (hex)
  1842. j++;
  1843. while (j < len && j < i + 10 &&
  1844. ((s[j] >= '0' && s[j] <= '9') ||
  1845. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1846. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1847. char c = s[j++];
  1848. v = v * (hex ? 16 : 10) +
  1849. (unsigned int)(c <= '9' ? c - '0' :
  1850. (c | 0x20) - 'a' + 10);
  1851. }
  1852. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1853. out[n++] = (char)v;
  1854. i = j + 1;
  1855. done = 1;
  1856. }
  1857. }
  1858. }
  1859. if (!done)
  1860. out[n++] = s[i++];
  1861. }
  1862. return n;
  1863. }
  1864. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1865. const char *s, unsigned int len)
  1866. {
  1867. unsigned int i = 0;
  1868. unsigned int start = d->attrs_used;
  1869. while (i < len) {
  1870. char name[32];
  1871. unsigned int nl = 0, vstart, vlen;
  1872. char quote = 0;
  1873. while (i < len && is_space(s[i]))
  1874. i++;
  1875. if (i >= len || s[i] == '/')
  1876. break;
  1877. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1878. char c = s[i++];
  1879. if (c >= 'A' && c <= 'Z')
  1880. c = (char)(c - 'A' + 'a');
  1881. if (nl + 1 < sizeof(name))
  1882. name[nl++] = c;
  1883. }
  1884. name[nl] = '\0';
  1885. while (i < len && is_space(s[i]))
  1886. i++;
  1887. if (i < len && s[i] == '=') {
  1888. i++;
  1889. while (i < len && is_space(s[i]))
  1890. i++;
  1891. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1892. quote = s[i++];
  1893. vstart = i;
  1894. while (i < len && s[i] != quote)
  1895. i++;
  1896. vlen = i - vstart;
  1897. if (i < len)
  1898. i++;
  1899. } else {
  1900. vstart = i;
  1901. while (i < len && !is_space(s[i]) && s[i] != '>')
  1902. i++;
  1903. vlen = i - vstart;
  1904. }
  1905. } else {
  1906. /* Boolean attribute: expose it with an empty value. */
  1907. vstart = 0;
  1908. vlen = 0;
  1909. }
  1910. if (!nl)
  1911. continue;
  1912. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1913. d->truncated = 1;
  1914. break;
  1915. }
  1916. {
  1917. unsigned int k;
  1918. for (k = 0; k < nl; k++)
  1919. d->attrs[d->attrs_used++] = name[k];
  1920. d->attrs[d->attrs_used++] = '\0';
  1921. d->attrs_used += attr_unescape(s + vstart, vlen,
  1922. d->attrs + d->attrs_used);
  1923. d->attrs[d->attrs_used++] = '\0';
  1924. }
  1925. }
  1926. if (d->attrs_used > start) {
  1927. n->extra_off = start;
  1928. n->extra_len = (unsigned short)(d->attrs_used - start);
  1929. }
  1930. }
  1931. /* Parse attributes from `s` (between the tag name and '>'). */
  1932. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1933. const char *s, unsigned int len)
  1934. {
  1935. unsigned int i = 0;
  1936. while (i < len) {
  1937. char name[32];
  1938. unsigned int nl = 0, vstart, vlen;
  1939. char quote = 0;
  1940. while (i < len && is_space(s[i]))
  1941. i++;
  1942. if (i >= len || s[i] == '/')
  1943. break;
  1944. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1945. char c = s[i++];
  1946. if (c >= 'A' && c <= 'Z')
  1947. c = (char)(c - 'A' + 'a');
  1948. if (nl + 1 < sizeof(name))
  1949. name[nl++] = c;
  1950. }
  1951. name[nl] = '\0';
  1952. while (i < len && is_space(s[i]))
  1953. i++;
  1954. if (i >= len || s[i] != '=') {
  1955. if (str_eq(name, "hidden"))
  1956. n->flags |= BR_NODE_HIDDEN;
  1957. note_attr_flag(n, name);
  1958. continue;
  1959. }
  1960. i++;
  1961. while (i < len && is_space(s[i]))
  1962. i++;
  1963. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1964. quote = s[i++];
  1965. vstart = i;
  1966. while (i < len && s[i] != quote)
  1967. i++;
  1968. vlen = i - vstart;
  1969. if (i < len)
  1970. i++;
  1971. } else {
  1972. vstart = i;
  1973. while (i < len && !is_space(s[i]) && s[i] != '>')
  1974. i++;
  1975. vlen = i - vstart;
  1976. }
  1977. if (!nl)
  1978. continue;
  1979. if (str_eq(name, "hidden")) {
  1980. n->flags |= BR_NODE_HIDDEN;
  1981. continue;
  1982. }
  1983. note_attr_flag(n, name);
  1984. if (str_eq(name, "href"))
  1985. n->href_off = store_attr(d, s + vstart, vlen, 480);
  1986. else if (str_eq(name, "class"))
  1987. n->class_off = store_attr(d, s + vstart, vlen,
  1988. BR_CLASS_MAX);
  1989. else if (str_eq(name, "id"))
  1990. n->id_off = store_attr(d, s + vstart, vlen, 64);
  1991. else if (str_eq(name, "style"))
  1992. n->style_off = store_attr(d, s + vstart, vlen,
  1993. BR_STYLE_ATTR_MAX);
  1994. else if (str_eq(name, "src"))
  1995. n->src_off = store_attr(d, s + vstart, vlen, 400);
  1996. else if (str_eq(name, "alt"))
  1997. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  1998. else if (str_eq(name, "color"))
  1999. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2000. else if (str_eq(name, "rel"))
  2001. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2002. else if (str_eq(name, "value"))
  2003. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2004. else if (str_eq(name, "placeholder") &&
  2005. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2006. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2007. else if (str_eq(name, "type") &&
  2008. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2009. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2010. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2011. else if (str_eq(name, "width")) {
  2012. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2013. if (vlen && s[vstart + vlen - 1] == '%')
  2014. n->width_pct = 1;
  2015. } else if (str_eq(name, "height")) {
  2016. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2017. if (vlen && s[vstart + vlen - 1] == '%')
  2018. n->height_pct = 1;
  2019. } else if (str_eq(name, "colspan")) {
  2020. unsigned int v = parse_uint(s + vstart, vlen);
  2021. if (v < 1)
  2022. v = 1;
  2023. if (v > 16)
  2024. v = 16;
  2025. n->colspan = (unsigned char)v;
  2026. } else if (str_eq(name, "rowspan")) {
  2027. unsigned int v = parse_uint(s + vstart, vlen);
  2028. if (v < 1)
  2029. v = 1;
  2030. if (v > 16)
  2031. v = 16;
  2032. n->rowspan = (unsigned char)v;
  2033. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2034. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2035. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2036. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2037. } else if (str_eq(name, "x2")) {
  2038. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2039. } else if (str_eq(name, "y2")) {
  2040. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2041. } else if (str_eq(name, "rx")) {
  2042. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2043. } else if (str_eq(name, "d")) {
  2044. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2045. } else if (str_eq(name, "viewbox")) {
  2046. unsigned short vw = 0, vh = 0;
  2047. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2048. n->width_attr = vw;
  2049. n->height_attr = vh;
  2050. n->flags |= BR_NODE_VIEWBOX;
  2051. }
  2052. } else if (str_eq(name, "text-anchor")) {
  2053. if (contains_ci_len(s + vstart, vlen, "middle"))
  2054. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2055. else if (contains_ci_len(s + vstart, vlen, "end"))
  2056. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2057. }
  2058. }
  2059. parse_attrs_generic(d, n, s, len);
  2060. }
  2061. static int str_eq(const char *a, const char *b)
  2062. {
  2063. unsigned int i = 0;
  2064. while (a[i] && b[i] && a[i] == b[i])
  2065. i++;
  2066. return a[i] == '\0' && b[i] == '\0';
  2067. }
  2068. /* ---- CSS ---- */
  2069. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2070. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2071. {
  2072. return parse_color(s, len, out);
  2073. }
  2074. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2075. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2076. {
  2077. while (len && is_space(*s)) {
  2078. s++;
  2079. len--;
  2080. }
  2081. while (len && is_space(s[len - 1]))
  2082. len--;
  2083. if (!len)
  2084. return 0;
  2085. if (len == 4 && ci_eq_n(s, "none", 4))
  2086. return BR_PAINT_NONE;
  2087. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2088. return BR_PAINT_CURRENT;
  2089. if (len > 4 && ci_eq_n(s, "url(", 4))
  2090. return BR_PAINT_URL;
  2091. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2092. return BR_PAINT_NONE;
  2093. if (parse_color(s, len, c))
  2094. return BR_PAINT_COLOR;
  2095. return 0;
  2096. }
  2097. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2098. static int svg_len16(const char *s, unsigned int len)
  2099. {
  2100. unsigned int i = 0;
  2101. int v = 0, frac = 0, digits = 0;
  2102. while (i < len && is_space(s[i]))
  2103. i++;
  2104. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2105. if (v < 4000)
  2106. v = v * 10 + (s[i] - '0');
  2107. i++;
  2108. digits++;
  2109. }
  2110. if (i < len && s[i] == '.') {
  2111. int scale = 1;
  2112. i++;
  2113. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2114. if (scale < 10000) {
  2115. frac = frac * 10 + (s[i] - '0');
  2116. scale *= 10;
  2117. }
  2118. i++;
  2119. digits++;
  2120. }
  2121. frac = frac * 16 / scale;
  2122. }
  2123. if (!digits)
  2124. return -1;
  2125. return v * 16 + frac;
  2126. }
  2127. /* Inside an <svg> subtree (including the <svg> element itself). */
  2128. static int in_svg(const struct br_doc *d, unsigned int node)
  2129. {
  2130. unsigned int k;
  2131. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2132. if (d->nodes[node].tag == BR_TAG_SVG)
  2133. return 1;
  2134. node = d->nodes[node].parent;
  2135. }
  2136. return 0;
  2137. }
  2138. enum {
  2139. PROP_COLOR, PROP_BG, PROP_BG_IMAGE, PROP_BG_REPEAT,
  2140. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2141. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2142. PROP_FONT_PX, PROP_FONT_UNIT,
  2143. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2144. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2145. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2146. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2147. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2148. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2149. PROP_BORDER_W, PROP_BORDER_COLOR,
  2150. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2151. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2152. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2153. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2154. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2155. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2156. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2157. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2158. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2159. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2160. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2161. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2162. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2163. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2164. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2165. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2166. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2167. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2168. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2169. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2170. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2171. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2172. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2173. PROP_ZINDEX, PROP_ZSET,
  2174. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2175. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2176. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2177. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2178. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2179. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2180. PROP_ASPECT_W, PROP_ASPECT_H,
  2181. PROP_COUNT
  2182. };
  2183. struct color_name {
  2184. const char *name;
  2185. unsigned short rgb;
  2186. };
  2187. static const struct color_name color_names[] = {
  2188. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2189. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2190. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2191. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2192. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2193. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2194. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2195. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2196. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2197. { "darkgrey", 0x528A }, { 0, 0 }
  2198. };
  2199. static int hexval(char c)
  2200. {
  2201. if (c >= '0' && c <= '9')
  2202. return c - '0';
  2203. if (c >= 'a' && c <= 'f')
  2204. return c - 'a' + 10;
  2205. if (c >= 'A' && c <= 'F')
  2206. return c - 'A' + 10;
  2207. return -1;
  2208. }
  2209. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2210. {
  2211. unsigned int i;
  2212. for (i = 0; color_names[i].name; i++) {
  2213. unsigned int j, n = s_len(color_names[i].name);
  2214. if (n != len)
  2215. continue;
  2216. for (j = 0; j < n; j++) {
  2217. char c = s[j];
  2218. if (c >= 'A' && c <= 'Z')
  2219. c = (char)(c - 'A' + 'a');
  2220. if (c != color_names[i].name[j])
  2221. break;
  2222. }
  2223. if (j == n) {
  2224. *out = color_names[i].rgb;
  2225. return 1;
  2226. }
  2227. }
  2228. return 0;
  2229. }
  2230. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2231. * over white; the render target is a white page. */
  2232. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2233. {
  2234. unsigned int i = 0;
  2235. while (i < len && is_space(s[i]))
  2236. i++;
  2237. if (i < len && s[i] == '#') {
  2238. int d[6], nd = 0;
  2239. i++;
  2240. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2241. d[nd++] = hexval(s[i++]);
  2242. if (nd == 3) {
  2243. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2244. unsigned int b = (unsigned int)d[2];
  2245. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2246. ((g * 17 >> 2) << 5) |
  2247. (b * 17 >> 3));
  2248. return 1;
  2249. }
  2250. if (nd >= 6) {
  2251. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2252. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2253. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2254. *out = (unsigned short)(((r >> 3) << 11) |
  2255. ((g >> 2) << 5) | (b >> 3));
  2256. return 1;
  2257. }
  2258. return 0;
  2259. }
  2260. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2261. unsigned int v[3], k = 0, a = 255;
  2262. while (i < len && s[i] != '(')
  2263. i++;
  2264. if (i >= len)
  2265. return 0;
  2266. i++;
  2267. while (k < 3 && i < len) {
  2268. unsigned int x = 0;
  2269. int percent = 0;
  2270. while (i < len && (is_space(s[i]) || s[i] == ','))
  2271. i++;
  2272. if (i >= len || s[i] < '0' || s[i] > '9')
  2273. return 0;
  2274. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2275. x = x * 10 + (unsigned int)(s[i] - '0');
  2276. i++;
  2277. }
  2278. if (i < len && s[i] == '%') {
  2279. percent = 1;
  2280. i++;
  2281. }
  2282. if (percent)
  2283. x = x * 255 / 100;
  2284. if (x > 255)
  2285. x = 255;
  2286. v[k++] = x;
  2287. }
  2288. /* Optional alpha in both syntaxes:
  2289. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2290. {
  2291. unsigned int whole = 0, frac = 0, scale = 1;
  2292. int percent = 0;
  2293. while (i < len && (is_space(s[i]) || s[i] == ','))
  2294. i++;
  2295. if (i < len && s[i] == '/') {
  2296. i++;
  2297. while (i < len && is_space(s[i]))
  2298. i++;
  2299. }
  2300. if (i < len && s[i] >= '0' && s[i] <= '9') {
  2301. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2302. whole = whole * 10 +
  2303. (unsigned int)(s[i] - '0');
  2304. i++;
  2305. }
  2306. if (i < len && s[i] == '.') {
  2307. i++;
  2308. while (i < len && s[i] >= '0' &&
  2309. s[i] <= '9') {
  2310. if (scale < 1000) {
  2311. frac = frac * 10 +
  2312. (unsigned int)(s[i] - '0');
  2313. scale *= 10;
  2314. }
  2315. i++;
  2316. }
  2317. }
  2318. while (i < len && is_space(s[i]))
  2319. i++;
  2320. if (i < len && s[i] == '%')
  2321. percent = 1;
  2322. if (percent)
  2323. a = whole > 100 ? 255 :
  2324. whole * 255 / 100;
  2325. else if (whole >= 1)
  2326. a = 255;
  2327. else
  2328. a = frac * 255 / scale;
  2329. }
  2330. }
  2331. if (k == 3) {
  2332. unsigned int r = v[0], g = v[1], b = v[2];
  2333. if (a < 255) {
  2334. r = (r * a + 255u * (255u - a)) / 255u;
  2335. g = (g * a + 255u * (255u - a)) / 255u;
  2336. b = (b * a + 255u * (255u - a)) / 255u;
  2337. }
  2338. *out = (unsigned short)(((r >> 3) << 11) |
  2339. ((g >> 2) << 5) | (b >> 3));
  2340. return 1;
  2341. }
  2342. return 0;
  2343. }
  2344. {
  2345. unsigned int j = i;
  2346. while (i < len && !is_space(s[i]))
  2347. i++;
  2348. return named_color(s + j, i - j, out);
  2349. }
  2350. }
  2351. /* Fully transparent colours have no RGB565 representation. They are common
  2352. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2353. * only on hover; painting them black would draw an outline around every
  2354. * pill and card. */
  2355. static int color_is_transparent(const char *s, unsigned int len)
  2356. {
  2357. unsigned int i = 0, n = 0;
  2358. int last = 0, prev = 0, d;
  2359. while (i < len && is_space(s[i]))
  2360. i++;
  2361. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2362. return 1;
  2363. if (i >= len || s[i] != '#')
  2364. return 0;
  2365. i++;
  2366. while (i < len && hexval(s[i]) >= 0) {
  2367. d = hexval(s[i]);
  2368. prev = last;
  2369. last = d;
  2370. n++;
  2371. i++;
  2372. }
  2373. while (i < len && is_space(s[i]))
  2374. i++;
  2375. if (i != len)
  2376. return 0;
  2377. if (n == 4)
  2378. return last == 0;
  2379. if (n == 8)
  2380. return prev == 0 && last == 0;
  2381. return 0;
  2382. }
  2383. static int register_bg_url(const char *s, unsigned int len)
  2384. {
  2385. unsigned int i, j, n;
  2386. while (len && is_space(*s)) {
  2387. s++;
  2388. len--;
  2389. }
  2390. while (len && is_space(s[len - 1]))
  2391. len--;
  2392. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2393. s[len - 1] == s[0]) {
  2394. s++;
  2395. len -= 2;
  2396. }
  2397. if (len > 191)
  2398. len = 191;
  2399. for (i = 0; i < bg_url_count; i++) {
  2400. for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
  2401. ;
  2402. if (j == len && bg_urls[i][j] == '\0')
  2403. return (int)i + 1;
  2404. }
  2405. if (bg_url_count >= BR_BG_URLS || len == 0)
  2406. return 0;
  2407. n = len < 191 ? len : 191;
  2408. for (i = 0; i < n; i++)
  2409. bg_urls[bg_url_count][i] = s[i];
  2410. bg_urls[bg_url_count][n] = '\0';
  2411. return (int)(++bg_url_count);
  2412. }
  2413. /* Length values for margins/padding: px, ignore percentages/units. */
  2414. static unsigned int viewport_w;
  2415. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2416. {
  2417. unsigned int i = *pos;
  2418. int sign = 1;
  2419. long v = 0; /* hundredths of a unit before conversion */
  2420. int digits = 0, frac = 0, unit = 0;
  2421. while (i < len && is_space(s[i]))
  2422. i++;
  2423. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2424. if (s[i] == '-')
  2425. sign = -1;
  2426. i++;
  2427. }
  2428. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2429. v = v * 10 + (s[i] - '0');
  2430. digits = 1;
  2431. i++;
  2432. }
  2433. if (i < len && s[i] == '.') {
  2434. i++;
  2435. /* Minified CSS writes ".5rem" without a leading zero. */
  2436. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2437. if (frac < 2) {
  2438. v = v * 10 + (s[i] - '0');
  2439. frac++;
  2440. digits = 1;
  2441. }
  2442. i++;
  2443. }
  2444. }
  2445. while (frac < 2) {
  2446. v *= 10;
  2447. frac++;
  2448. }
  2449. while (frac > 2) {
  2450. v /= 10;
  2451. frac--;
  2452. }
  2453. if (i < len && s[i] == '%')
  2454. unit = 2;
  2455. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2456. s[i + 2] == 'm')
  2457. unit = 1;
  2458. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2459. unit = 3;
  2460. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2461. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2462. s[i] == 'a' || s[i] == 'd'))
  2463. i++;
  2464. *pos = i;
  2465. if (!digits)
  2466. return 0;
  2467. if (unit == 1)
  2468. v *= root_font_px;
  2469. else if (unit == 3)
  2470. v *= root_font_px;
  2471. else if (unit == 2)
  2472. v = v * (long)viewport_w / 100;
  2473. v /= 100;
  2474. v *= sign;
  2475. if (v < -2048)
  2476. v = -2048;
  2477. if (v > 2048)
  2478. v = 2048;
  2479. return (int)v;
  2480. }
  2481. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2482. * treats it as a zero-length token; margins need to keep it so block layout
  2483. * can centre a box with a definite width. */
  2484. #define CSS_LEN_AUTO (-32768)
  2485. /* A box length with its unit preserved for layout-time resolution:
  2486. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2487. * rem and whole pixels for px. */
  2488. static int parse_css_len(const char *s, unsigned int len,
  2489. unsigned int *pos, int *unit)
  2490. {
  2491. unsigned int i = *pos;
  2492. int sign = 1;
  2493. long v = 0;
  2494. int digits = 0, frac = 0;
  2495. *unit = 0;
  2496. while (i < len && is_space(s[i]))
  2497. i++;
  2498. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2499. if (s[i] == '-')
  2500. sign = -1;
  2501. i++;
  2502. }
  2503. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2504. v = v * 10 + (s[i] - '0');
  2505. digits = 1;
  2506. i++;
  2507. }
  2508. if (i < len && s[i] == '.') {
  2509. i++;
  2510. /* Leading-dot fractions are common in minified CSS
  2511. * (".5rem", ".3rem"). */
  2512. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2513. if (frac < 2) {
  2514. v = v * 10 + (s[i] - '0');
  2515. frac++;
  2516. digits = 1;
  2517. }
  2518. i++;
  2519. }
  2520. }
  2521. while (frac < 2) {
  2522. v *= 10;
  2523. frac++;
  2524. }
  2525. if (!digits) {
  2526. *pos = i;
  2527. return 0;
  2528. }
  2529. if (i < len && s[i] == '%') {
  2530. *unit = 1;
  2531. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2532. s[i + 2] == 'm') {
  2533. *unit = 3;
  2534. i += 3;
  2535. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2536. *unit = 2;
  2537. i += 2;
  2538. } else {
  2539. v /= 100;
  2540. }
  2541. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2542. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2543. s[i] == 'a' || s[i] == 'd'))
  2544. i++;
  2545. *pos = i;
  2546. v *= sign;
  2547. if (v < -32768)
  2548. v = -32768;
  2549. if (v > 32767)
  2550. v = 32767;
  2551. return (int)v;
  2552. }
  2553. static int parse_margin_len(const char *s, unsigned int len,
  2554. unsigned int *pos, int *unit)
  2555. {
  2556. unsigned int i = *pos;
  2557. while (i < len && is_space(s[i]))
  2558. i++;
  2559. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2560. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2561. *pos = i + 4;
  2562. *unit = 4;
  2563. return CSS_LEN_AUTO;
  2564. }
  2565. return parse_css_len(s, len, pos, unit);
  2566. }
  2567. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2568. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2569. static int parse_margin_calc(const char *s, unsigned int len,
  2570. unsigned int *pos, int *pct, int *px)
  2571. {
  2572. unsigned int i = *pos, j;
  2573. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2574. return 0;
  2575. i += 5;
  2576. j = i;
  2577. while (j < len && s[j] != ')')
  2578. j++;
  2579. if (j >= len)
  2580. return 0;
  2581. if (!parse_calc(s + i, j - i, pct, px))
  2582. return 0;
  2583. *pos = j + 1;
  2584. return 1;
  2585. }
  2586. /* calc(P% +/- Npx) for the common width/margin forms. */
  2587. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2588. {
  2589. unsigned int i = 0;
  2590. int sign = 1;
  2591. int have = 0;
  2592. *pct = 0;
  2593. *px = 0;
  2594. while (i < len) {
  2595. unsigned int j;
  2596. long whole = 0, frac = 0, scale = 1;
  2597. int digits = 0;
  2598. while (i < len && is_space(s[i]))
  2599. i++;
  2600. if (i >= len)
  2601. break;
  2602. if (s[i] == '+' || s[i] == '-') {
  2603. sign = s[i] == '-' ? -1 : 1;
  2604. i++;
  2605. continue;
  2606. }
  2607. if (s[i] < '0' || s[i] > '9') {
  2608. if (s[i] == '.') {
  2609. /* fall through to the number parser */
  2610. } else {
  2611. i++;
  2612. continue;
  2613. }
  2614. }
  2615. j = i;
  2616. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2617. whole = whole * 10 + (s[j] - '0');
  2618. digits = 1;
  2619. j++;
  2620. }
  2621. if (j < len && s[j] == '.') {
  2622. j++;
  2623. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2624. frac = frac * 10 + (s[j] - '0');
  2625. scale *= 10;
  2626. j++;
  2627. }
  2628. }
  2629. if (!digits) {
  2630. i = j + 1;
  2631. continue;
  2632. }
  2633. while (j < len && is_space(s[j]))
  2634. j++;
  2635. if (j < len && s[j] == '%') {
  2636. *pct = sign * (int)(whole + (scale > 1 ?
  2637. frac * 100 / scale : 0));
  2638. have = 1;
  2639. j++;
  2640. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2641. *px = sign * (int)whole;
  2642. have = 1;
  2643. j += 2;
  2644. }
  2645. i = j;
  2646. while (i < len && !is_space(s[i]))
  2647. i++;
  2648. sign = 1;
  2649. }
  2650. return have;
  2651. }
  2652. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2653. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2654. unsigned char *kind)
  2655. {
  2656. unsigned int i = 0;
  2657. unsigned long v = 0;
  2658. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2659. *val = 0;
  2660. *kind = 0;
  2661. while (i < len && is_space(s[i]))
  2662. i++;
  2663. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2664. return 0;
  2665. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2666. return 0;
  2667. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2668. v = v * 10 + (unsigned int)(s[i] - '0');
  2669. digits = 1;
  2670. i++;
  2671. }
  2672. if (i < len && s[i] == '.') {
  2673. i++;
  2674. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2675. if (frac < 2) {
  2676. v = v * 10 + (unsigned int)(s[i] - '0');
  2677. frac++;
  2678. digits = 1;
  2679. }
  2680. i++;
  2681. }
  2682. }
  2683. while (frac < 2) {
  2684. v *= 10;
  2685. frac++;
  2686. }
  2687. while (frac > 2) {
  2688. v /= 10;
  2689. frac--;
  2690. }
  2691. if (!digits)
  2692. return 0;
  2693. while (i < len && is_space(s[i]))
  2694. i++;
  2695. if (i < len && s[i] == '%')
  2696. pct = 1;
  2697. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2698. s[i + 2] == 'm')
  2699. rem = 1;
  2700. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2701. rem = 2;
  2702. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2703. vw = 1;
  2704. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2705. ; /* px: already the default scale */
  2706. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2707. bad_unit = 1;
  2708. if (bad_unit)
  2709. return 0;
  2710. if (rem == 1 || rem == 2) {
  2711. if (v > 65535)
  2712. v = 65535;
  2713. *val = (unsigned short)v;
  2714. *kind = rem == 1 ? 5 : 4;
  2715. return 1;
  2716. }
  2717. v /= 100;
  2718. if (vw)
  2719. v = v * viewport_w / 100;
  2720. if (pct) {
  2721. if (v > 100)
  2722. v = 100;
  2723. *kind = 2;
  2724. } else {
  2725. if (v > 2048)
  2726. v = 2048;
  2727. *kind = 1;
  2728. }
  2729. *val = (unsigned short)v;
  2730. return 1;
  2731. }
  2732. static int parse_font_px(const char *s, unsigned int len)
  2733. {
  2734. unsigned int i = 0;
  2735. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2736. int v1000, px;
  2737. int is_px = 1, is_rem = 0, is_pct = 0;
  2738. while (i < len && is_space(s[i]))
  2739. i++;
  2740. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2741. return 24;
  2742. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2743. return 32;
  2744. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2745. return 14;
  2746. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2747. whole = whole * 10 + (s[i] - '0');
  2748. digits = 1;
  2749. i++;
  2750. }
  2751. if (i < len && s[i] == '.') {
  2752. i++;
  2753. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2754. if (fscale < 1000) {
  2755. frac = frac * 10 + (s[i] - '0');
  2756. fscale *= 10;
  2757. }
  2758. digits = 1;
  2759. i++;
  2760. }
  2761. }
  2762. if (!digits)
  2763. return 0;
  2764. while (i < len && is_space(s[i]))
  2765. i++;
  2766. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2767. is_rem = 1;
  2768. is_px = 0;
  2769. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2770. is_rem = 1;
  2771. is_px = 0;
  2772. } else if (i < len && s[i] == '%') {
  2773. is_pct = 1;
  2774. is_px = 0;
  2775. }
  2776. v1000 = whole * 1000 + frac * 1000 / fscale;
  2777. if (is_rem)
  2778. px = v1000 * root_font_px / 1000;
  2779. else if (is_pct)
  2780. px = v1000 * root_font_px / 100000;
  2781. else if (is_px)
  2782. px = v1000 / 1000;
  2783. else
  2784. px = v1000 / 1000;
  2785. if (px < 8)
  2786. px = 8;
  2787. if (px > 72)
  2788. px = 72;
  2789. return px;
  2790. }
  2791. /* font-size keeping the unit so em/% resolve against the parent face. */
  2792. static int parse_font_size(const char *s, unsigned int len,
  2793. unsigned short *raw, unsigned char *unit)
  2794. {
  2795. unsigned int i = 0;
  2796. int whole = 0, frac = 0, scale = 1, digits = 0;
  2797. int v100;
  2798. *raw = 0;
  2799. *unit = 0;
  2800. while (i < len && is_space(s[i]))
  2801. i++;
  2802. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2803. *raw = 24;
  2804. *unit = 1;
  2805. return 1;
  2806. }
  2807. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2808. *raw = 32;
  2809. *unit = 1;
  2810. return 1;
  2811. }
  2812. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2813. *raw = 14;
  2814. *unit = 1;
  2815. return 1;
  2816. }
  2817. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2818. whole = whole * 10 + (s[i] - '0');
  2819. digits = 1;
  2820. i++;
  2821. }
  2822. if (i < len && s[i] == '.') {
  2823. i++;
  2824. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2825. frac = frac * 10 + (s[i] - '0');
  2826. scale *= 10;
  2827. i++;
  2828. }
  2829. }
  2830. if (!digits && !frac)
  2831. return 0;
  2832. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2833. while (i < len && is_space(s[i]))
  2834. i++;
  2835. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2836. *raw = (unsigned short)v100;
  2837. *unit = 3;
  2838. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2839. *raw = (unsigned short)v100;
  2840. *unit = 2;
  2841. } else if (i < len && s[i] == '%') {
  2842. /* Keep hundredths: 62.5% is a common root size and truncating
  2843. * it to 62% makes every rem 1px too small. */
  2844. *raw = (unsigned short)v100;
  2845. *unit = 4;
  2846. } else {
  2847. *raw = (unsigned short)(v100 / 100);
  2848. *unit = 1;
  2849. }
  2850. return 1;
  2851. }
  2852. static int parse_line_height(const char *s, unsigned int len,
  2853. unsigned short *px, unsigned char *pct)
  2854. {
  2855. unsigned int i = 0;
  2856. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2857. int v1000;
  2858. int is_px = 0, is_pct = 0;
  2859. *px = 0;
  2860. *pct = 0;
  2861. while (i < len && is_space(s[i]))
  2862. i++;
  2863. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2864. return 0;
  2865. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2866. whole = whole * 10 + (s[i] - '0');
  2867. digits = 1;
  2868. i++;
  2869. }
  2870. if (i < len && s[i] == '.') {
  2871. i++;
  2872. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2873. frac = frac * 10 + (s[i] - '0');
  2874. fscale *= 10;
  2875. digits = 1;
  2876. i++;
  2877. }
  2878. }
  2879. if (!digits)
  2880. return 0;
  2881. while (i < len && is_space(s[i]))
  2882. i++;
  2883. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2884. is_px = 1;
  2885. else if (i < len && s[i] == '%')
  2886. is_pct = 1;
  2887. v1000 = whole * 1000 + frac * 1000 / fscale;
  2888. /* rem is an absolute length (the root font is known by the time
  2889. * rules are parsed, as for font-size); em behaves like a multiplier. */
  2890. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2891. v1000 = v1000 * root_font_px;
  2892. is_px = 1;
  2893. }
  2894. if (is_px) {
  2895. int v = v1000 / 1000;
  2896. if (v < 8)
  2897. v = 8;
  2898. if (v > 160)
  2899. v = 160;
  2900. *px = (unsigned short)v;
  2901. } else {
  2902. int v = v1000 / 10; /* unitless multiplier -> percent */
  2903. if (is_pct)
  2904. v = v1000 / 1000;
  2905. if (v < 50)
  2906. v = 50;
  2907. if (v > 255) /* stored in an unsigned char */
  2908. v = 255;
  2909. *pct = (unsigned char)v;
  2910. }
  2911. return 1;
  2912. }
  2913. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2914. {
  2915. unsigned int i, j, n = s_len(needle);
  2916. for (i = 0; i + n <= len; i++) {
  2917. for (j = 0; j < n; j++) {
  2918. char a = s[i + j];
  2919. if (a >= 'A' && a <= 'Z')
  2920. a = (char)(a - 'A' + 'a');
  2921. if (a != needle[j])
  2922. break;
  2923. }
  2924. if (j == n)
  2925. return 1;
  2926. }
  2927. return 0;
  2928. }
  2929. /* Pull a colour out of a border/background shorthand. */
  2930. /* Background shorthands can hold several layers before the final solid
  2931. * colour. The colour is conventionally the last token (e.g. a stack of
  2932. * gradients followed by var(--bg)), so take the rightmost colour rather
  2933. * than the first gradient stop. */
  2934. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2935. {
  2936. int last = -1;
  2937. unsigned int i;
  2938. for (i = 0; i < len; i++) {
  2939. if (s[i] == '#')
  2940. last = (int)i;
  2941. else if ((s[i] == 'r' || s[i] == 'R') &&
  2942. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2943. last = (int)i;
  2944. }
  2945. if (last >= 0)
  2946. return parse_color(s + last, len - (unsigned int)last, c);
  2947. while (i > 0) {
  2948. unsigned int st, e;
  2949. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2950. s[i - 1] == ')' || s[i - 1] == '('))
  2951. i--;
  2952. e = i;
  2953. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  2954. s[i - 1] != ')' && s[i - 1] != '(')
  2955. i--;
  2956. st = i;
  2957. if (e > st && parse_color(s + st, e - st, c))
  2958. return 1;
  2959. }
  2960. return 0;
  2961. }
  2962. static int parse_display(const char *s, unsigned int len)
  2963. {
  2964. unsigned int i;
  2965. while (len && is_space(s[0])) { s++; len--; }
  2966. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  2967. ;
  2968. if (i == 4 && ci_eq_n(s, "none", 4))
  2969. return BR_DISPLAY_NONE;
  2970. if (i == 5 && ci_eq_n(s, "block", 5))
  2971. return BR_DISPLAY_BLOCK;
  2972. if (i == 6 && ci_eq_n(s, "inline", 6))
  2973. return BR_DISPLAY_INLINE;
  2974. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  2975. return BR_DISPLAY_INLINE_BLOCK;
  2976. if (i == 10 && ci_eq_n(s, "list-item", 10))
  2977. return BR_DISPLAY_BLOCK;
  2978. if (i == 4 && ci_eq_n(s, "flex", 4))
  2979. return BR_DISPLAY_FLEX;
  2980. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  2981. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  2982. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  2983. * block instead of an inline box. */
  2984. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  2985. return BR_DISPLAY_BLOCK;
  2986. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  2987. return BR_DISPLAY_BLOCK;
  2988. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  2989. return BR_DISPLAY_FLEX;
  2990. if (i == 5 && ci_eq_n(s, "table", 5))
  2991. return BR_DISPLAY_BLOCK;
  2992. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  2993. return BR_DISPLAY_BLOCK;
  2994. /* Grid shells are common on news/wiki pages, but the two-column
  2995. * heuristic is not reliable for their nested area templates yet.
  2996. * Collapse them to block flow so the content stays readable. */
  2997. if (i == 4 && ci_eq_n(s, "grid", 4))
  2998. return BR_DISPLAY_BLOCK;
  2999. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3000. return BR_DISPLAY_BLOCK;
  3001. return BR_DISPLAY_INLINE;
  3002. }
  3003. static void decl_add(struct br_decl *out, unsigned int *nout,
  3004. unsigned char prop, int val, int important)
  3005. {
  3006. if (*nout >= BR_DECLS)
  3007. return;
  3008. out[*nout].prop = prop;
  3009. out[*nout].val = (short)val;
  3010. out[*nout].important = (unsigned char)(important != 0);
  3011. (*nout)++;
  3012. }
  3013. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3014. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3015. unsigned int *start, unsigned int *end)
  3016. {
  3017. unsigned int i = *pos;
  3018. while (i < len && is_space(s[i]))
  3019. i++;
  3020. if (i >= len)
  3021. return 0;
  3022. *start = i;
  3023. while (i < len && !is_space(s[i]))
  3024. i++;
  3025. *end = i;
  3026. *pos = i;
  3027. return 1;
  3028. }
  3029. static int bg_number(const char *s, unsigned int len, signed short *val,
  3030. unsigned char *kind)
  3031. {
  3032. unsigned int i = 0;
  3033. long v = 0;
  3034. int neg = 0, digits = 0, pct = 0;
  3035. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3036. neg = s[i] == '-';
  3037. i++;
  3038. }
  3039. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3040. v = v * 10 + (s[i] - '0');
  3041. digits = 1;
  3042. i++;
  3043. }
  3044. if (i < len && s[i] == '.') {
  3045. i++;
  3046. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3047. digits = 1;
  3048. i++;
  3049. }
  3050. }
  3051. if (!digits)
  3052. return 0;
  3053. if (i < len && s[i] == '%') {
  3054. pct = 1;
  3055. i++;
  3056. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3057. i += 2;
  3058. }
  3059. if (i != len)
  3060. return 0;
  3061. if (neg)
  3062. v = -v;
  3063. if (v < -4096)
  3064. v = -4096;
  3065. if (v > 4096)
  3066. v = 4096;
  3067. *val = (signed short)v;
  3068. *kind = (unsigned char)(pct ? 1 : 0);
  3069. return 1;
  3070. }
  3071. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3072. int *vertical)
  3073. {
  3074. *vertical = 0;
  3075. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3076. *val = 0;
  3077. return 1;
  3078. }
  3079. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3080. *val = 100;
  3081. return 1;
  3082. }
  3083. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3084. *val = 0;
  3085. *vertical = 1;
  3086. return 1;
  3087. }
  3088. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3089. *val = 100;
  3090. *vertical = 1;
  3091. return 1;
  3092. }
  3093. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3094. *val = 50;
  3095. return 1;
  3096. }
  3097. return 0;
  3098. }
  3099. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3100. const char *s, unsigned int len, int important)
  3101. {
  3102. unsigned int pos = 0, ts, te;
  3103. signed short val[2] = { 50, 50 };
  3104. unsigned char kind[2] = { 1, 1 };
  3105. int vert[2] = { 0, 0 }, n = 0;
  3106. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3107. signed short v;
  3108. int is_vert;
  3109. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3110. val[n] = v;
  3111. kind[n] = 1;
  3112. vert[n] = is_vert;
  3113. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3114. val[n] = v;
  3115. vert[n] = 0;
  3116. } else {
  3117. n++;
  3118. continue;
  3119. }
  3120. n++;
  3121. }
  3122. if (n == 1) {
  3123. if (vert[0]) {
  3124. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3125. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3126. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3127. important);
  3128. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3129. } else {
  3130. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3131. important);
  3132. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3133. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3134. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3135. }
  3136. return;
  3137. }
  3138. if (n >= 2) {
  3139. int x = vert[0] && !vert[1] ? 1 : 0;
  3140. int y = 1 - x;
  3141. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3142. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3143. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3144. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3145. }
  3146. }
  3147. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3148. const char *s, unsigned int len, int important)
  3149. {
  3150. unsigned int pos = 0, ts, te;
  3151. signed short w = 0, h = 0;
  3152. unsigned char kw = 0, kh = 0;
  3153. int n = 0;
  3154. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3155. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3156. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3157. return;
  3158. }
  3159. if (n == 0 && te - ts == 7 &&
  3160. ci_eq_n(s + ts, "contain", 7)) {
  3161. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3162. return;
  3163. }
  3164. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3165. n++;
  3166. continue;
  3167. }
  3168. {
  3169. signed short v;
  3170. unsigned char k;
  3171. if (bg_number(s + ts, te - ts, &v, &k)) {
  3172. if (n == 0) {
  3173. w = v;
  3174. kw = k;
  3175. } else {
  3176. h = v;
  3177. kh = k;
  3178. }
  3179. }
  3180. }
  3181. n++;
  3182. }
  3183. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3184. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3185. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3186. (void)kh;
  3187. }
  3188. /* Parse the `position / size` part of the background shorthand. The image
  3189. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3190. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3191. const char *v, unsigned int len,
  3192. int important)
  3193. {
  3194. unsigned int i, slash = 0, depth = 0, p, first, count;
  3195. char q = 0;
  3196. signed short nv;
  3197. int vert;
  3198. unsigned char kind;
  3199. for (i = 0; i < len; i++) {
  3200. char c = v[i];
  3201. if (q) {
  3202. if (c == q)
  3203. q = 0;
  3204. continue;
  3205. }
  3206. if (c == '\'' || c == '"') {
  3207. q = c;
  3208. continue;
  3209. }
  3210. if (c == '(') {
  3211. depth++;
  3212. continue;
  3213. }
  3214. if (c == ')') {
  3215. if (depth)
  3216. depth--;
  3217. continue;
  3218. }
  3219. if (c == '/' && !depth) {
  3220. slash = i;
  3221. break;
  3222. }
  3223. }
  3224. if (!slash)
  3225. return;
  3226. /* Up to two position tokens immediately before the slash. */
  3227. p = slash;
  3228. first = slash;
  3229. count = 0;
  3230. while (count < 2) {
  3231. unsigned int e;
  3232. while (p > 0 && is_space(v[p - 1]))
  3233. p--;
  3234. e = p;
  3235. while (p > 0 && !is_space(v[p - 1]))
  3236. p--;
  3237. if (e == p)
  3238. break;
  3239. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3240. bg_number(v + p, e - p, &nv, &kind)) {
  3241. first = p;
  3242. count++;
  3243. } else {
  3244. break;
  3245. }
  3246. }
  3247. if (count)
  3248. add_bg_position(out, nout, v + first, slash - first,
  3249. important);
  3250. /* Up to two size tokens after the slash. */
  3251. p = slash + 1;
  3252. while (p < len && is_space(v[p]))
  3253. p++;
  3254. first = p;
  3255. count = 0;
  3256. while (count < 2) {
  3257. unsigned int ts = p;
  3258. while (p < len && !is_space(v[p]))
  3259. p++;
  3260. if (p == ts)
  3261. break;
  3262. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3263. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3264. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3265. bg_number(v + ts, p - ts, &nv, &kind)) {
  3266. count++;
  3267. while (p < len && is_space(v[p]))
  3268. p++;
  3269. } else {
  3270. break;
  3271. }
  3272. }
  3273. if (count)
  3274. add_bg_size(out, nout, v + first, p - first, important);
  3275. }
  3276. static int parse_fixed_100(const char *s, unsigned int len,
  3277. unsigned int *pos, signed short *out)
  3278. {
  3279. unsigned int i = *pos;
  3280. long whole = 0, frac = 0;
  3281. int neg = 0, digits = 0, fd = 0;
  3282. while (i < len && is_space(s[i]))
  3283. i++;
  3284. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3285. neg = s[i] == '-';
  3286. i++;
  3287. }
  3288. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3289. whole = whole * 10 + (s[i] - '0');
  3290. if (whole > 327)
  3291. whole = 327;
  3292. digits = 1;
  3293. i++;
  3294. }
  3295. if (i < len && s[i] == '.') {
  3296. i++;
  3297. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3298. if (fd < 2) {
  3299. frac = frac * 10 + (s[i] - '0');
  3300. fd++;
  3301. digits = 1;
  3302. }
  3303. i++;
  3304. }
  3305. }
  3306. if (!digits)
  3307. return 0;
  3308. while (fd++ < 2)
  3309. frac *= 10;
  3310. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3311. *pos = i;
  3312. return 1;
  3313. }
  3314. static int find_fn(const char *v, unsigned int len, const char *name,
  3315. unsigned int *arg)
  3316. {
  3317. unsigned int n = s_len(name), i;
  3318. for (i = 0; i + n <= len; i++) {
  3319. unsigned int j;
  3320. if (!ci_eq_n(v + i, name, n))
  3321. continue;
  3322. j = i + n;
  3323. while (j < len && is_space(v[j]))
  3324. j++;
  3325. if (j < len && v[j] == '(') {
  3326. *arg = j + 1;
  3327. return 1;
  3328. }
  3329. }
  3330. return 0;
  3331. }
  3332. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3333. const char *s, unsigned int len,
  3334. int important)
  3335. {
  3336. unsigned int pos = 0, ts, te;
  3337. signed short val[2] = { 50, 50 };
  3338. unsigned char kind[2] = { 1, 1 };
  3339. int vert[2] = { 0, 0 }, n = 0;
  3340. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3341. signed short v;
  3342. int is_vert;
  3343. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3344. val[n] = v;
  3345. kind[n] = 1;
  3346. vert[n] = is_vert;
  3347. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3348. val[n] = v;
  3349. vert[n] = 0;
  3350. } else {
  3351. n++;
  3352. continue;
  3353. }
  3354. n++;
  3355. }
  3356. if (n == 1) {
  3357. if (vert[0]) {
  3358. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3359. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3360. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3361. important);
  3362. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3363. } else {
  3364. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3365. important);
  3366. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3367. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3368. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3369. }
  3370. return;
  3371. }
  3372. if (n >= 2) {
  3373. int x = vert[0] && !vert[1] ? 1 : 0;
  3374. int y = 1 - x;
  3375. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3376. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3377. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3378. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3379. }
  3380. }
  3381. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3382. const char *v, unsigned int vl, int important)
  3383. {
  3384. unsigned int i;
  3385. for (i = 0; i + 4 <= vl; i++) {
  3386. if (ci_eq_n(v + i, "url(", 4)) {
  3387. unsigned int st = i + 4, en = st;
  3388. int idx;
  3389. while (en < vl && v[en] != ')')
  3390. en++;
  3391. idx = register_bg_url(v + st, en - st);
  3392. if (idx)
  3393. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3394. important);
  3395. return;
  3396. }
  3397. }
  3398. }
  3399. static int parse_len_token(const char *s, unsigned int len, int *out)
  3400. {
  3401. unsigned int pos = 0;
  3402. int v;
  3403. if (!len)
  3404. return 0;
  3405. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3406. return 0;
  3407. v = parse_len(s, len, &pos);
  3408. if (pos == 0)
  3409. return 0;
  3410. *out = v;
  3411. return 1;
  3412. }
  3413. static void expand_four(int v[4], int n)
  3414. {
  3415. if (n == 1) {
  3416. v[1] = v[2] = v[3] = v[0];
  3417. } else if (n == 2) {
  3418. v[2] = v[0];
  3419. v[3] = v[1];
  3420. } else if (n == 3) {
  3421. v[3] = v[1];
  3422. }
  3423. }
  3424. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3425. * the input is copied verbatim when it contains no var(). */
  3426. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3427. unsigned int len, char *out,
  3428. unsigned int cap, int depth)
  3429. {
  3430. unsigned int i = 0, n = 0;
  3431. if (depth > 8)
  3432. return 0;
  3433. while (i < len && n + 1 < cap) {
  3434. unsigned int p, inner, close, comma, nlen, fstart;
  3435. char quote;
  3436. int parens;
  3437. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3438. out[n++] = s[i++];
  3439. continue;
  3440. }
  3441. p = i + 4;
  3442. quote = 0;
  3443. parens = 1;
  3444. while (p < len && parens) {
  3445. char c = s[p];
  3446. if (quote) {
  3447. if (c == quote)
  3448. quote = 0;
  3449. else if (c == '\\' && p + 1 < len)
  3450. p++;
  3451. } else if (c == '"' || c == '\'') {
  3452. quote = c;
  3453. } else if (c == '(') {
  3454. parens++;
  3455. } else if (c == ')') {
  3456. parens--;
  3457. }
  3458. p++;
  3459. }
  3460. if (parens) {
  3461. out[n++] = s[i++];
  3462. continue;
  3463. }
  3464. close = p - 1;
  3465. inner = i + 4;
  3466. while (inner < close && is_space(s[inner]))
  3467. inner++;
  3468. comma = inner;
  3469. parens = 0;
  3470. quote = 0;
  3471. while (comma < close) {
  3472. char c = s[comma];
  3473. if (quote) {
  3474. if (c == quote)
  3475. quote = 0;
  3476. } else if (c == '"' || c == '\'') {
  3477. quote = c;
  3478. } else if (c == '(') {
  3479. parens++;
  3480. } else if (c == ')' && parens > 0) {
  3481. parens--;
  3482. } else if (c == ',' && parens == 0) {
  3483. break;
  3484. }
  3485. comma++;
  3486. }
  3487. nlen = comma - inner;
  3488. while (nlen && is_space(s[inner + nlen - 1]))
  3489. nlen--;
  3490. fstart = comma < close ? comma + 1 : close;
  3491. while (fstart < close && is_space(s[fstart]))
  3492. fstart++;
  3493. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3494. unsigned int vi = css_var_find(d, s + inner, nlen);
  3495. if (vi < BR_MAX_VARS &&
  3496. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3497. vi = BR_MAX_VARS;
  3498. if (vi < BR_MAX_VARS) {
  3499. const char *v = attr_ptr(d, d->var_val[vi]);
  3500. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3501. out + n,
  3502. cap - n, depth + 1);
  3503. n += vn;
  3504. } else if (fstart < close) {
  3505. unsigned int vn = css_expand_vars(d, s + fstart,
  3506. close - fstart,
  3507. out + n, cap - n,
  3508. depth + 1);
  3509. n += vn;
  3510. }
  3511. } else if (fstart < close) {
  3512. unsigned int vn = css_expand_vars(d, s + fstart,
  3513. close - fstart,
  3514. out + n, cap - n,
  3515. depth + 1);
  3516. n += vn;
  3517. }
  3518. i = p;
  3519. }
  3520. out[n] = '\0';
  3521. return n;
  3522. }
  3523. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3524. unsigned int i)
  3525. {
  3526. while (i < len && (is_space(s[i]) || s[i] == ','))
  3527. i++;
  3528. return i;
  3529. }
  3530. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3531. * argument). Returns 1 on success. */
  3532. static int parse_grid_track(const char *s, unsigned int len,
  3533. unsigned int *pp, unsigned short *val,
  3534. unsigned char *kind)
  3535. {
  3536. unsigned int i = grid_skip_sep(s, len, *pp);
  3537. unsigned int whole = 0, frac = 0, scale = 1;
  3538. int any = 0;
  3539. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3540. unsigned int j = i + 7;
  3541. while (j < len && s[j] != ',')
  3542. j++;
  3543. if (j >= len)
  3544. return 0;
  3545. i = j + 1;
  3546. while (i < len && is_space(s[i]))
  3547. i++;
  3548. }
  3549. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3550. int depth = 1;
  3551. i += 7;
  3552. while (i < len && depth) {
  3553. if (s[i] == '(')
  3554. depth++;
  3555. else if (s[i] == ')')
  3556. depth--;
  3557. i++;
  3558. }
  3559. *pp = i;
  3560. return 0;
  3561. }
  3562. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3563. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3564. i++;
  3565. any = 1;
  3566. }
  3567. if (i < len && s[i] == '.') {
  3568. i++;
  3569. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3570. if (scale < 1000) {
  3571. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3572. scale *= 10;
  3573. }
  3574. i++;
  3575. any = 1;
  3576. }
  3577. }
  3578. if (!any) {
  3579. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3580. *val = 1;
  3581. *kind = 3;
  3582. *pp = i + 4;
  3583. return 1;
  3584. }
  3585. return 0;
  3586. }
  3587. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3588. *val = (unsigned short)(whole ? whole : 1);
  3589. *kind = 3;
  3590. i += 2;
  3591. } else if (i < len && s[i] == '%') {
  3592. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3593. *kind = 2;
  3594. i++;
  3595. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3596. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3597. *kind = 1;
  3598. i += 3;
  3599. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3600. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3601. *kind = 1;
  3602. i += 2;
  3603. } else {
  3604. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3605. *kind = 1;
  3606. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3607. i += 2;
  3608. }
  3609. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3610. i++;
  3611. *pp = i;
  3612. return 1;
  3613. }
  3614. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3615. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3616. * keep the two-column path). */
  3617. static int grid_repeat_count(const char *s, unsigned int len)
  3618. {
  3619. unsigned int i = 0;
  3620. int n = 0;
  3621. while (i < len && is_space(s[i]))
  3622. i++;
  3623. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3624. i += 7;
  3625. while (i < len && is_space(s[i]))
  3626. i++;
  3627. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3628. n = n * 10 + (s[i] - '0');
  3629. i++;
  3630. }
  3631. if (n < 1 || n > 12)
  3632. return 0;
  3633. while (i < len && (is_space(s[i]) || s[i] == ','))
  3634. i++;
  3635. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3636. return n;
  3637. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3638. i += 7;
  3639. while (i < len && is_space(s[i]))
  3640. i++;
  3641. if (i < len && s[i] == '0') {
  3642. i++;
  3643. while (i < len && (is_space(s[i]) ||
  3644. s[i] == ',' || s[i] == 'p' ||
  3645. s[i] == 'x'))
  3646. i++;
  3647. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3648. return n;
  3649. }
  3650. }
  3651. return 0;
  3652. }
  3653. /* "1fr 1fr 1fr" */
  3654. for (;;) {
  3655. while (i < len && is_space(s[i]))
  3656. i++;
  3657. if (i >= len)
  3658. break;
  3659. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3660. (i + 3 == len || is_space(s[i + 3]))) {
  3661. n++;
  3662. i += 3;
  3663. } else {
  3664. return 0;
  3665. }
  3666. }
  3667. return n >= 3 ? n : 0;
  3668. }
  3669. static int parse_grid_cols(const char *s, unsigned int len,
  3670. unsigned short val[2], unsigned char kind[2])
  3671. {
  3672. unsigned int pos = 0;
  3673. int n = 0;
  3674. while (n < 2) {
  3675. unsigned int before = pos;
  3676. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3677. break;
  3678. n++;
  3679. if (pos <= before)
  3680. break;
  3681. while (pos < len && is_space(s[pos]))
  3682. pos++;
  3683. if (pos >= len)
  3684. break;
  3685. }
  3686. return n;
  3687. }
  3688. static int parse_grid_slot(const char *s, unsigned int len)
  3689. {
  3690. if (contains_ci_len(s, len, "footer") ||
  3691. contains_ci_len(s, len, "header") ||
  3692. contains_ci_len(s, len, "sitenotice") ||
  3693. contains_ci_len(s, len, "navbar"))
  3694. return 1;
  3695. if (contains_ci_len(s, len, "columnstart") ||
  3696. contains_ci_len(s, len, "sidebar") ||
  3697. contains_ci_len(s, len, "left") ||
  3698. contains_ci_len(s, len, "start"))
  3699. return 2;
  3700. if (contains_ci_len(s, len, "pagecontent") ||
  3701. contains_ci_len(s, len, "content") ||
  3702. contains_ci_len(s, len, "main") ||
  3703. contains_ci_len(s, len, "right") ||
  3704. contains_ci_len(s, len, "end"))
  3705. return 3;
  3706. return 0;
  3707. }
  3708. static int css_has_var(const char *s, unsigned int len)
  3709. {
  3710. unsigned int i;
  3711. for (i = 0; i + 4 <= len; i++)
  3712. if ((s[i] == 'v' || s[i] == 'V') &&
  3713. ci_eq_n(s + i, "var(", 4))
  3714. return 1;
  3715. return 0;
  3716. }
  3717. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3718. unsigned int i)
  3719. {
  3720. i += 2;
  3721. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3722. i++;
  3723. if (i + 1 < len)
  3724. i += 2;
  3725. else
  3726. i = len;
  3727. return i;
  3728. }
  3729. static unsigned int css_skip_string(const char *s, unsigned int len,
  3730. unsigned int i)
  3731. {
  3732. char q = s[i++];
  3733. while (i < len && s[i] != q) {
  3734. if (s[i] == '\\' && i + 1 < len)
  3735. i++;
  3736. i++;
  3737. }
  3738. if (i < len)
  3739. i++;
  3740. return i;
  3741. }
  3742. /* Find the '}' that closes a declaration block. i points just past the
  3743. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3744. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3745. * selectors such as "Highlight-Stories {" without a closing brace). */
  3746. static unsigned int css_block_end(const char *s, unsigned int len,
  3747. unsigned int i)
  3748. {
  3749. int depth = 0;
  3750. while (i < len) {
  3751. char c = s[i];
  3752. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3753. i = css_skip_comment(s, len, i);
  3754. continue;
  3755. }
  3756. if (c == '"' || c == '\'') {
  3757. i = css_skip_string(s, len, i);
  3758. continue;
  3759. }
  3760. if (c == '{') {
  3761. depth++;
  3762. } else if (c == '}') {
  3763. if (depth == 0)
  3764. return i;
  3765. depth--;
  3766. }
  3767. i++;
  3768. }
  3769. return len;
  3770. }
  3771. /* Skip a block whose opening '{' has already been consumed. */
  3772. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3773. unsigned int i)
  3774. {
  3775. int depth = 1;
  3776. while (i < len && depth) {
  3777. char c = s[i];
  3778. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3779. i = css_skip_comment(s, len, i);
  3780. continue;
  3781. }
  3782. if (c == '"' || c == '\'') {
  3783. i = css_skip_string(s, len, i);
  3784. continue;
  3785. }
  3786. if (c == '{')
  3787. depth++;
  3788. else if (c == '}')
  3789. depth--;
  3790. i++;
  3791. }
  3792. return i;
  3793. }
  3794. /* ---- @font-face ------------------------------------------------------- */
  3795. static void font_trim(const char **sp, unsigned int *len)
  3796. {
  3797. const char *s = *sp;
  3798. unsigned int n = *len;
  3799. while (n && is_space(*s)) {
  3800. s++;
  3801. n--;
  3802. }
  3803. while (n && is_space(s[n - 1]))
  3804. n--;
  3805. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3806. s++;
  3807. n -= 2;
  3808. }
  3809. *sp = s;
  3810. *len = n;
  3811. }
  3812. static void font_add(const char *family, unsigned int flen,
  3813. const char *url, unsigned int ulen)
  3814. {
  3815. unsigned int i, n = 0, k;
  3816. font_trim(&family, &flen);
  3817. font_trim(&url, &ulen);
  3818. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3819. return;
  3820. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3821. struct br_font_def *f = &font_defs[i];
  3822. if (s_len(f->family) != flen)
  3823. continue;
  3824. for (k = 0; k < flen; k++) {
  3825. char a = f->family[k], b = family[k];
  3826. if (a >= 'A' && a <= 'Z')
  3827. a = (char)(a - 'A' + 'a');
  3828. if (b >= 'A' && b <= 'Z')
  3829. b = (char)(b - 'A' + 'a');
  3830. if (a != b)
  3831. break;
  3832. }
  3833. if (k != flen)
  3834. continue;
  3835. n = 0;
  3836. while (url[n] && n + 1 < sizeof(f->url)) {
  3837. f->url[n] = url[n];
  3838. n++;
  3839. }
  3840. f->url[n] = '\0';
  3841. n = 0;
  3842. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3843. f->base[n] = font_base[n];
  3844. n++;
  3845. }
  3846. f->base[n] = '\0';
  3847. return;
  3848. }
  3849. if (font_face_count >= BR_MAX_FACES)
  3850. return;
  3851. {
  3852. struct br_font_def *f = &font_defs[font_face_count];
  3853. for (n = 0; n < flen; n++)
  3854. f->family[n] = family[n];
  3855. f->family[n] = '\0';
  3856. for (n = 0; n < ulen; n++)
  3857. f->url[n] = url[n];
  3858. f->url[n] = '\0';
  3859. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  3860. f->base[n] = font_base[n];
  3861. f->base[n] = '\0';
  3862. f->used = 1;
  3863. font_face_count++;
  3864. }
  3865. }
  3866. /* Look up the first name in a font-family list that has an @font-face. */
  3867. static int font_family_id(const char *s, unsigned int len)
  3868. {
  3869. unsigned int i = 0;
  3870. while (i < len) {
  3871. unsigned int start, end, q = 0;
  3872. while (i < len && (is_space(s[i]) || s[i] == ','))
  3873. i++;
  3874. if (i >= len)
  3875. break;
  3876. if (s[i] == '"' || s[i] == '\'') {
  3877. char quote = s[i++];
  3878. start = i;
  3879. while (i < len && s[i] != quote)
  3880. i++;
  3881. end = i;
  3882. if (i < len)
  3883. i++;
  3884. } else {
  3885. start = i;
  3886. while (i < len && s[i] != ',')
  3887. i++;
  3888. end = i;
  3889. while (end > start && is_space(s[end - 1]))
  3890. end--;
  3891. }
  3892. if (end > start) {
  3893. for (q = 0; q < (unsigned int)font_face_count; q++) {
  3894. const char *f = font_defs[q].family;
  3895. unsigned int fl = s_len(f), k;
  3896. if (fl != end - start)
  3897. continue;
  3898. for (k = 0; k < fl; k++) {
  3899. char a = f[k], b = s[start + k];
  3900. if (a >= 'A' && a <= 'Z')
  3901. a = (char)(a - 'A' + 'a');
  3902. if (b >= 'A' && b <= 'Z')
  3903. b = (char)(b - 'A' + 'a');
  3904. if (a != b)
  3905. break;
  3906. }
  3907. if (k == fl)
  3908. return (int)q + 1;
  3909. }
  3910. }
  3911. }
  3912. return 0;
  3913. }
  3914. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  3915. * at-rules, so faces must be registered before any font-family declaration
  3916. * in the same sheet is resolved. */
  3917. static void scan_font_faces(const char *s, unsigned int len)
  3918. {
  3919. unsigned int i = 0;
  3920. while (i + 10 <= len) {
  3921. unsigned int j, end;
  3922. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  3923. unsigned int flen = 0, ulen = 0, p;
  3924. int have_family = 0, have_url = 0;
  3925. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  3926. i++;
  3927. continue;
  3928. }
  3929. j = i + 10;
  3930. while (j < len && s[j] != '{' && s[j] != ';')
  3931. j++;
  3932. if (j >= len || s[j] == ';') {
  3933. i = j + 1;
  3934. continue;
  3935. }
  3936. end = css_skip_nested(s, len, j + 1);
  3937. p = j + 1;
  3938. while (p < end) {
  3939. unsigned int ds, de, vs, ve;
  3940. while (p < end && (is_space(s[p]) || s[p] == ';'))
  3941. p++;
  3942. if (p >= end)
  3943. break;
  3944. ds = p;
  3945. while (p < end && s[p] != ':' && s[p] != ';')
  3946. p++;
  3947. de = p;
  3948. if (p >= end || s[p] != ':') {
  3949. while (p < end && s[p] != ';')
  3950. p++;
  3951. continue;
  3952. }
  3953. p++;
  3954. vs = p;
  3955. while (p < end && s[p] != ';')
  3956. p++;
  3957. ve = p;
  3958. while (ds < de && is_space(s[ds]))
  3959. ds++;
  3960. while (de > ds && is_space(s[de - 1]))
  3961. de--;
  3962. while (vs < ve && is_space(s[vs]))
  3963. vs++;
  3964. while (ve > vs && is_space(s[ve - 1]))
  3965. ve--;
  3966. if (de - ds == 11 &&
  3967. ci_eq_n(s + ds, "font-family", 11)) {
  3968. const char *fp = s + vs;
  3969. unsigned int fn = ve - vs;
  3970. font_trim(&fp, &fn);
  3971. if (fn && fn < sizeof(family)) {
  3972. unsigned int k;
  3973. for (k = 0; k < fn; k++)
  3974. family[k] = fp[k];
  3975. family[fn] = '\0';
  3976. flen = fn;
  3977. have_family = 1;
  3978. }
  3979. } else if (de - ds == 3 &&
  3980. ci_eq_n(s + ds, "src", 3)) {
  3981. unsigned int q = vs;
  3982. while (q + 4 <= ve) {
  3983. unsigned int us, ue;
  3984. if (!ci_eq_n(s + q, "url(", 4)) {
  3985. q++;
  3986. continue;
  3987. }
  3988. q += 4;
  3989. us = q;
  3990. while (q < ve && s[q] != ')')
  3991. q++;
  3992. ue = q;
  3993. {
  3994. const char *up = s + us;
  3995. unsigned int un = ue - us;
  3996. font_trim(&up, &un);
  3997. if (un && un < sizeof(url) &&
  3998. !contains_ci_len(up, un,
  3999. "woff")) {
  4000. unsigned int k;
  4001. for (k = 0; k < un; k++)
  4002. url[k] = up[k];
  4003. url[un] = '\0';
  4004. ulen = un;
  4005. have_url = 1;
  4006. break;
  4007. }
  4008. }
  4009. }
  4010. }
  4011. }
  4012. if (have_family && have_url)
  4013. font_add(family, flen, url, ulen);
  4014. i = end;
  4015. }
  4016. }
  4017. /* One border-radius length. Unlike parse_size(), percentages keep their
  4018. * kind: the drawer scales them to the box, not the viewport. */
  4019. static int parse_radius_value(const char *s, unsigned int len,
  4020. unsigned int *pos, unsigned short *val,
  4021. unsigned char *kind)
  4022. {
  4023. unsigned int i = *pos;
  4024. unsigned long v = 0;
  4025. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4026. while (i < len && is_space(s[i]))
  4027. i++;
  4028. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4029. v = v * 10 + (unsigned int)(s[i] - '0');
  4030. digits = 1;
  4031. i++;
  4032. }
  4033. if (i < len && s[i] == '.') {
  4034. i++;
  4035. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4036. fdigits = 1;
  4037. if (frac < 2) {
  4038. v = v * 10 + (unsigned int)(s[i] - '0');
  4039. frac++;
  4040. }
  4041. i++;
  4042. }
  4043. }
  4044. while (frac < 2) {
  4045. v *= 10;
  4046. frac++;
  4047. }
  4048. while (i < len && is_space(s[i]))
  4049. i++;
  4050. if (i < len && s[i] == '%') {
  4051. pct = 1;
  4052. i++;
  4053. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4054. s[i + 2] == 'm') {
  4055. rem = 1;
  4056. i += 3;
  4057. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4058. rem = 2;
  4059. i += 2;
  4060. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4061. i += 2;
  4062. } else if (i < len && is_ident(s[i])) {
  4063. return 0;
  4064. }
  4065. if (!digits && !fdigits)
  4066. return 0;
  4067. if (rem == 1)
  4068. v *= root_font_px;
  4069. else if (rem == 2)
  4070. v *= root_font_px;
  4071. v /= 100;
  4072. if (v > 2048)
  4073. v = 2048;
  4074. *val = (unsigned short)v;
  4075. *kind = pct ? 2 : 1;
  4076. *pos = i;
  4077. return 1;
  4078. }
  4079. /* font: [style] [variant] [weight] size[/line-height] family */
  4080. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4081. const char *v, unsigned int len, int important)
  4082. {
  4083. unsigned int pos = 0, ts, te;
  4084. unsigned short size_raw = 0;
  4085. unsigned char size_unit = 0;
  4086. int have_size = 0;
  4087. int bold = 0, italic = 0;
  4088. int lh_px = 0, lh_pct = 0, mono = 0;
  4089. while (next_token(v, len, &pos, &ts, &te)) {
  4090. unsigned int tl = te - ts;
  4091. unsigned int slash = 0;
  4092. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4093. bold = 1;
  4094. continue;
  4095. }
  4096. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4097. continue;
  4098. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4099. italic = 1;
  4100. continue;
  4101. }
  4102. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4103. italic = 1;
  4104. continue;
  4105. }
  4106. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4107. continue;
  4108. for (slash = 0; slash < tl; slash++)
  4109. if (v[ts + slash] == '/')
  4110. break;
  4111. if (slash == tl) {
  4112. unsigned int q;
  4113. int weight = 0;
  4114. for (q = 0; q < tl; q++) {
  4115. if (v[ts + q] < '0' || v[ts + q] > '9')
  4116. break;
  4117. weight = weight * 10 + (v[ts + q] - '0');
  4118. }
  4119. if (q == tl && weight >= 100 && weight <= 900) {
  4120. bold = weight >= 500;
  4121. continue;
  4122. }
  4123. }
  4124. {
  4125. unsigned short raw;
  4126. unsigned char unit;
  4127. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4128. &raw, &unit))
  4129. continue;
  4130. size_raw = raw;
  4131. size_unit = unit;
  4132. }
  4133. have_size = 1;
  4134. if (slash < tl) {
  4135. unsigned int lstart = ts + slash + 1;
  4136. unsigned short lhp = 0;
  4137. unsigned char lhpct = 0;
  4138. if (!parse_line_height(v + lstart, te - lstart,
  4139. &lhp, &lhpct)) {
  4140. lh_px = 0;
  4141. lh_pct = 0;
  4142. } else {
  4143. lh_px = lhp;
  4144. lh_pct = lhpct;
  4145. }
  4146. }
  4147. }
  4148. if (!have_size)
  4149. return;
  4150. mono = contains_ci_len(v, len, "mono");
  4151. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4152. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4153. decl_add(out, nout, PROP_BOLD, bold, important);
  4154. decl_add(out, nout, PROP_ITALIC, italic, important);
  4155. decl_add(out, nout, PROP_MONO, mono, important);
  4156. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  4157. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4158. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4159. }
  4160. static int css_collect_only;
  4161. /* Append one code point of generated content as Latin-1. Typography the
  4162. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4163. * and anything else outside Latin-1 are dropped. */
  4164. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4165. unsigned int cp)
  4166. {
  4167. const char *sub = 0;
  4168. if (cp == 0x2013 || cp == 0x2014 || cp == 0x2212)
  4169. sub = "-";
  4170. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4171. sub = "'";
  4172. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4173. sub = "\"";
  4174. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4175. sub = "\xB7";
  4176. else if (cp == 0x2026)
  4177. sub = "...";
  4178. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4179. sub = ">";
  4180. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4181. sub = "<";
  4182. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4183. cp == 0x2003 || cp == 0x202F)
  4184. sub = " ";
  4185. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4186. sub = "\xD7";
  4187. else if (cp == 0x2713 || cp == 0x2714)
  4188. sub = "v";
  4189. if (sub) {
  4190. while (*sub && *n + 1 < cap)
  4191. out[(*n)++] = *sub++;
  4192. return;
  4193. }
  4194. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4195. out[(*n)++] = (char)cp;
  4196. else if (cp == '\n' && *n + 1 < cap)
  4197. out[(*n)++] = ' ';
  4198. }
  4199. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4200. * content index + 1, or 0 for none/empty (counters and url() are not
  4201. * generated). */
  4202. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4203. {
  4204. char out[160];
  4205. unsigned int i = 0, n = 0, off;
  4206. while (i < len) {
  4207. char c = s[i];
  4208. if (c == '"' || c == '\'') {
  4209. char q = c;
  4210. i++;
  4211. while (i < len && s[i] != q) {
  4212. unsigned int cp;
  4213. if (s[i] == '\\' && i + 1 < len) {
  4214. unsigned int h = 0, k = 0;
  4215. i++;
  4216. while (k < 6 && i < len &&
  4217. ((s[i] >= '0' && s[i] <= '9') ||
  4218. ((s[i] | 32) >= 'a' &&
  4219. (s[i] | 32) <= 'f'))) {
  4220. char x = (char)(s[i] | 32);
  4221. h = h * 16 + (unsigned int)(x <= '9' ?
  4222. x - '0' : x - 'a' + 10);
  4223. i++;
  4224. k++;
  4225. }
  4226. if (k) {
  4227. if (i < len && s[i] == ' ')
  4228. i++;
  4229. cp = h;
  4230. } else {
  4231. cp = (unsigned char)s[i++];
  4232. }
  4233. } else if ((unsigned char)s[i] >= 0xC0) {
  4234. unsigned char b = (unsigned char)s[i++];
  4235. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4236. cp = b & (0x3F >> more);
  4237. while (more-- && i < len &&
  4238. ((unsigned char)s[i] & 0xC0) == 0x80)
  4239. cp = (cp << 6) | (s[i++] & 0x3F);
  4240. } else {
  4241. cp = (unsigned char)s[i++];
  4242. }
  4243. content_put(out, &n, sizeof(out), cp);
  4244. }
  4245. i++;
  4246. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4247. unsigned int st = i + 5, e = st;
  4248. while (e < len && s[e] != ')')
  4249. e++;
  4250. while (st < e && is_space(s[st]))
  4251. st++;
  4252. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4253. out[n++] = '\001';
  4254. while (st < e && !is_space(s[st]))
  4255. out[n++] = s[st++];
  4256. out[n++] = '\002';
  4257. }
  4258. i = e + 1;
  4259. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4260. ci_eq_n(s + i, "close-quot", 10))) {
  4261. content_put(out, &n, sizeof(out), '"');
  4262. i += 10;
  4263. } else if (c == '(') {
  4264. unsigned int depth = 0;
  4265. /* counter(), url(), ...: skip the arguments */
  4266. for (; i < len; i++) {
  4267. if (s[i] == '(')
  4268. depth++;
  4269. else if (s[i] == ')' && --depth == 0)
  4270. break;
  4271. }
  4272. i++;
  4273. } else {
  4274. i++;
  4275. }
  4276. }
  4277. if (!n || d->ncontent >= BR_MAX_CONTENT)
  4278. return 0;
  4279. off = store_raw(d, out, n, sizeof(out));
  4280. if (!off)
  4281. return 0;
  4282. d->content_off[d->ncontent++] = off;
  4283. return d->ncontent;
  4284. }
  4285. static void parse_decls(struct br_doc *d, const char *s,
  4286. unsigned int len, struct br_decl *out,
  4287. unsigned int *nout)
  4288. {
  4289. static char expanded[4096];
  4290. if (css_has_var(s, len)) {
  4291. unsigned int elen = css_expand_vars(d, s, len, expanded,
  4292. sizeof(expanded), 0);
  4293. if (elen) {
  4294. s = expanded;
  4295. len = elen;
  4296. }
  4297. }
  4298. unsigned int i = 0;
  4299. *nout = 0;
  4300. while (i < len && *nout < BR_DECLS) {
  4301. unsigned int pstart, pend, vstart, vend, k;
  4302. char prop[24];
  4303. int important = 0;
  4304. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4305. i++;
  4306. if (i >= len)
  4307. break;
  4308. /* A nested at-rule (@media/@supports) inside the declaration
  4309. * block carries its own selector context; skip it rather than
  4310. * reading its prelude as a declaration. */
  4311. if (s[i] == '@') {
  4312. while (i < len && s[i] != '{' && s[i] != ';')
  4313. i++;
  4314. if (i < len && s[i] == '{')
  4315. i = css_skip_nested(s, len, i + 1);
  4316. else if (i < len)
  4317. i++;
  4318. continue;
  4319. }
  4320. pstart = i;
  4321. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4322. i++;
  4323. if (i < len && s[i] == '{') {
  4324. /* Nested style rule: skip its block. */
  4325. i = css_skip_nested(s, len, i + 1);
  4326. continue;
  4327. }
  4328. pend = i;
  4329. if (i >= len || s[i] != ':')
  4330. continue;
  4331. i++;
  4332. vstart = i;
  4333. while (i < len && s[i] != ';')
  4334. i++;
  4335. vend = i;
  4336. while (pstart < pend && is_space(s[pstart]))
  4337. pstart++;
  4338. while (pend > pstart && is_space(s[pend - 1]))
  4339. pend--;
  4340. while (vstart < vend && is_space(s[vstart]))
  4341. vstart++;
  4342. while (vend > vstart && is_space(s[vend - 1]))
  4343. vend--;
  4344. if (vend - vstart >= 10 &&
  4345. ci_eq_n(s + vend - 10, "!important", 10)) {
  4346. important = 1;
  4347. vend -= 10;
  4348. while (vend > vstart && is_space(s[vend - 1]))
  4349. vend--;
  4350. }
  4351. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4352. char c = s[pstart + k];
  4353. if (c >= 'A' && c <= 'Z')
  4354. c = (char)(c - 'A' + 'a');
  4355. prop[k] = c;
  4356. }
  4357. prop[k] = '\0';
  4358. if (str_eq(prop, "color")) {
  4359. unsigned short c;
  4360. if (parse_color(s + vstart, vend - vstart, &c))
  4361. decl_add(out, nout, PROP_COLOR, c, important);
  4362. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  4363. /* SVG paint only: an HTML element's fill never paints
  4364. * its background (tagesschau sets fill on buttons). */
  4365. int is_fill = prop[0] == 'f';
  4366. unsigned short c;
  4367. int mode = paint_mode(s + vstart, vend - vstart, &c);
  4368. if (mode) {
  4369. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  4370. PROP_STROKE_MODE, mode, important);
  4371. if (mode == BR_PAINT_COLOR)
  4372. decl_add(out, nout, is_fill ? PROP_FILL :
  4373. PROP_STROKE, (short)c,
  4374. important);
  4375. }
  4376. } else if (str_eq(prop, "stroke-width")) {
  4377. int sw = svg_len16(s + vstart, vend - vstart);
  4378. if (sw >= 0)
  4379. decl_add(out, nout, PROP_STROKE_W, sw,
  4380. important);
  4381. } else if (str_eq(prop, "fill-rule")) {
  4382. decl_add(out, nout, PROP_FILL_RULE,
  4383. contains_ci_len(s + vstart, vend - vstart,
  4384. "evenodd") ? 1 : 0,
  4385. important);
  4386. } else if (str_eq(prop, "text-anchor")) {
  4387. int a = BR_ALIGN_LEFT;
  4388. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4389. a = BR_ALIGN_CENTER;
  4390. else if (contains_ci_len(s + vstart, vend - vstart,
  4391. "end"))
  4392. a = BR_ALIGN_RIGHT;
  4393. decl_add(out, nout, PROP_ALIGN, a, important);
  4394. } else if (str_eq(prop, "background-image")) {
  4395. add_bg_image_decl(out, nout, s + vstart,
  4396. vend - vstart, important);
  4397. } else if (str_eq(prop, "background-repeat")) {
  4398. int r = 0;
  4399. if (contains_ci_len(s + vstart, vend - vstart,
  4400. "no-repeat"))
  4401. r = 1;
  4402. else if (contains_ci_len(s + vstart, vend - vstart,
  4403. "repeat-x"))
  4404. r = 2;
  4405. else if (contains_ci_len(s + vstart, vend - vstart,
  4406. "repeat-y"))
  4407. r = 3;
  4408. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  4409. } else if (str_eq(prop, "background-position")) {
  4410. add_bg_position(out, nout, s + vstart, vend - vstart,
  4411. important);
  4412. } else if (str_eq(prop, "background-size")) {
  4413. add_bg_size(out, nout, s + vstart, vend - vstart,
  4414. important);
  4415. } else if (str_eq(prop, "background-color") ||
  4416. str_eq(prop, "background")) {
  4417. unsigned short c;
  4418. int is_bg = str_eq(prop, "background");
  4419. if (is_bg) {
  4420. add_bg_image_decl(out, nout, s + vstart,
  4421. vend - vstart, important);
  4422. add_bg_shorthand_extra(out, nout,
  4423. s + vstart,
  4424. vend - vstart,
  4425. important);
  4426. if (contains_ci_len(s + vstart, vend - vstart,
  4427. "no-repeat"))
  4428. decl_add(out, nout, PROP_BG_REPEAT, 1,
  4429. important);
  4430. else if (contains_ci_len(s + vstart,
  4431. vend - vstart,
  4432. "repeat-x"))
  4433. decl_add(out, nout, PROP_BG_REPEAT, 2,
  4434. important);
  4435. else if (contains_ci_len(s + vstart,
  4436. vend - vstart,
  4437. "repeat-y"))
  4438. decl_add(out, nout, PROP_BG_REPEAT, 3,
  4439. important);
  4440. }
  4441. if (parse_color(s + vstart, vend - vstart, &c))
  4442. decl_add(out, nout, PROP_BG, c, important);
  4443. else if (is_bg &&
  4444. find_color_last(s + vstart, vend - vstart, &c))
  4445. decl_add(out, nout, PROP_BG, c, important);
  4446. } else if (str_eq(prop, "font-size")) {
  4447. unsigned short raw;
  4448. unsigned char unit;
  4449. if (parse_font_size(s + vstart, vend - vstart,
  4450. &raw, &unit)) {
  4451. decl_add(out, nout, PROP_FONT_PX, raw,
  4452. important);
  4453. decl_add(out, nout, PROP_FONT_UNIT, unit,
  4454. important);
  4455. }
  4456. } else if (str_eq(prop, "font-weight")) {
  4457. int bold = 0;
  4458. unsigned int j;
  4459. if (vend - vstart >= 4 &&
  4460. ci_eq_n(s + vstart, "bold", 4))
  4461. bold = 1;
  4462. else {
  4463. int w = 0;
  4464. for (j = vstart; j < vend; j++)
  4465. if (s[j] >= '0' && s[j] <= '9')
  4466. w = w * 10 + (s[j] - '0');
  4467. if (w >= 500)
  4468. bold = 1;
  4469. }
  4470. decl_add(out, nout, PROP_BOLD, bold, important);
  4471. } else if (str_eq(prop, "font-style")) {
  4472. int italic = (vend - vstart >= 6 &&
  4473. ci_eq_n(s + vstart, "italic", 6));
  4474. decl_add(out, nout, PROP_ITALIC, italic, important);
  4475. } else if (str_eq(prop, "font-family")) {
  4476. unsigned int j;
  4477. int mono = 0;
  4478. int face = font_family_id(s + vstart, vend - vstart);
  4479. for (j = vstart; j + 4 <= vend; j++)
  4480. if (ci_eq_n(s + j, "mono", 4))
  4481. mono = 1;
  4482. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4483. decl_add(out, nout, PROP_MONO, mono, important);
  4484. } else if (str_eq(prop, "font")) {
  4485. add_font_shorthand(out, nout, s + vstart,
  4486. vend - vstart, important);
  4487. } else if (str_eq(prop, "text-align")) {
  4488. int a = BR_ALIGN_LEFT;
  4489. if (vend - vstart >= 6 &&
  4490. ci_eq_n(s + vstart, "center", 6))
  4491. a = BR_ALIGN_CENTER;
  4492. else if (vend - vstart >= 5 &&
  4493. ci_eq_n(s + vstart, "right", 5))
  4494. a = BR_ALIGN_RIGHT;
  4495. decl_add(out, nout, PROP_ALIGN, a, important);
  4496. } else if (str_eq(prop, "text-decoration")) {
  4497. int u = 0, stv = 0, ov = 0;
  4498. if (contains_ci_len(s + vstart, vend - vstart,
  4499. "underline"))
  4500. u = 1;
  4501. if (contains_ci_len(s + vstart, vend - vstart,
  4502. "line-through"))
  4503. stv = 1;
  4504. if (contains_ci_len(s + vstart, vend - vstart,
  4505. "overline"))
  4506. ov = 1;
  4507. decl_add(out, nout, PROP_UNDERLINE, u, important);
  4508. decl_add(out, nout, PROP_STRIKE, stv, important);
  4509. decl_add(out, nout, PROP_OVERLINE, ov, important);
  4510. } else if (str_eq(prop, "text-transform")) {
  4511. int u = (vend - vstart >= 9 &&
  4512. ci_eq_n(s + vstart, "uppercase", 9));
  4513. decl_add(out, nout, PROP_UPPER, u, important);
  4514. } else if (str_eq(prop, "white-space")) {
  4515. int nw = contains_ci_len(s + vstart, vend - vstart,
  4516. "nowrap") ||
  4517. contains_ci_len(s + vstart, vend - vstart,
  4518. "pre");
  4519. decl_add(out, nout, PROP_NOWRAP, nw, important);
  4520. } else if (str_eq(prop, "display")) {
  4521. decl_add(out, nout, PROP_DISPLAY,
  4522. parse_display(s + vstart, vend - vstart),
  4523. important);
  4524. } else if (str_eq(prop, "visibility")) {
  4525. int h = contains_ci_len(s + vstart, vend - vstart,
  4526. "hidden") ||
  4527. contains_ci_len(s + vstart, vend - vstart,
  4528. "collapse");
  4529. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  4530. } else if (str_eq(prop, "opacity")) {
  4531. unsigned int q;
  4532. int h = 1, any = 0;
  4533. for (q = vstart; q < vend; q++) {
  4534. if (s[q] >= '1' && s[q] <= '9') {
  4535. h = 0;
  4536. break;
  4537. }
  4538. if (s[q] >= '0' && s[q] <= '9')
  4539. any = 1;
  4540. }
  4541. if (!any || contains_ci_len(s + vstart, vend - vstart,
  4542. "inherit"))
  4543. h = 0;
  4544. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  4545. } else if (str_eq(prop, "overflow") ||
  4546. str_eq(prop, "overflow-x") ||
  4547. str_eq(prop, "overflow-y")) {
  4548. int h = contains_ci_len(s + vstart, vend - vstart,
  4549. "hidden") ||
  4550. contains_ci_len(s + vstart, vend - vstart,
  4551. "clip");
  4552. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  4553. } else if (str_eq(prop, "float")) {
  4554. int f = 0;
  4555. if (vend - vstart >= 4 &&
  4556. ci_eq_n(s + vstart, "left", 4))
  4557. f = 1;
  4558. else if (vend - vstart >= 5 &&
  4559. ci_eq_n(s + vstart, "right", 5))
  4560. f = 2;
  4561. decl_add(out, nout, PROP_FLOAT, f, important);
  4562. } else if (str_eq(prop, "clear")) {
  4563. int c = 0;
  4564. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  4565. c = 3;
  4566. else if (contains_ci_len(s + vstart, vend - vstart,
  4567. "left"))
  4568. c = 1;
  4569. else if (contains_ci_len(s + vstart, vend - vstart,
  4570. "right"))
  4571. c = 2;
  4572. decl_add(out, nout, PROP_CLEAR, c, important);
  4573. } else if (str_eq(prop, "flex-grow") ||
  4574. str_eq(prop, "flex")) {
  4575. unsigned int q = vstart;
  4576. int g = 0;
  4577. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  4578. q++;
  4579. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4580. g = g * 10 + (s[q] - '0');
  4581. q++;
  4582. }
  4583. if (g > 100)
  4584. g = 100;
  4585. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  4586. } else if (str_eq(prop, "flex-direction")) {
  4587. decl_add(out, nout, PROP_FLEX_COL,
  4588. contains_ci_len(s + vstart, vend - vstart,
  4589. "column"), important);
  4590. } else if (str_eq(prop, "flex-wrap")) {
  4591. int wrap = 0;
  4592. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  4593. wrap = 2;
  4594. else if (contains_ci_len(s + vstart, vend - vstart,
  4595. "wrap"))
  4596. wrap = 1;
  4597. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  4598. } else if (str_eq(prop, "justify-content")) {
  4599. int j = 0;
  4600. if (contains_ci_len(s + vstart, vend - vstart,
  4601. "space-between"))
  4602. j = 3;
  4603. else if (contains_ci_len(s + vstart, vend - vstart,
  4604. "space-around"))
  4605. j = 4;
  4606. else if (contains_ci_len(s + vstart, vend - vstart,
  4607. "space-evenly"))
  4608. j = 5;
  4609. else if (contains_ci_len(s + vstart, vend - vstart,
  4610. "center"))
  4611. j = 1;
  4612. else if (contains_ci_len(s + vstart, vend - vstart,
  4613. "flex-end") ||
  4614. contains_ci_len(s + vstart, vend - vstart,
  4615. "end"))
  4616. j = 2;
  4617. decl_add(out, nout, PROP_JUSTIFY, j, important);
  4618. } else if (str_eq(prop, "align-items")) {
  4619. int a = 0;
  4620. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  4621. a = 1;
  4622. else if (contains_ci_len(s + vstart, vend - vstart,
  4623. "flex-end") ||
  4624. contains_ci_len(s + vstart, vend - vstart,
  4625. "end"))
  4626. a = 2;
  4627. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  4628. } else if (str_eq(prop, "position")) {
  4629. int p = 0;
  4630. if (contains_ci_len(s + vstart, vend - vstart,
  4631. "absolute"))
  4632. p = 1;
  4633. else if (contains_ci_len(s + vstart, vend - vstart,
  4634. "fixed"))
  4635. p = 2;
  4636. else if (contains_ci_len(s + vstart, vend - vstart,
  4637. "relative"))
  4638. p = 3;
  4639. else if (contains_ci_len(s + vstart, vend - vstart,
  4640. "sticky"))
  4641. p = 4;
  4642. decl_add(out, nout, PROP_POS, p, important);
  4643. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  4644. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  4645. int which = str_eq(prop, "left") ? 0 :
  4646. str_eq(prop, "top") ? 1 :
  4647. str_eq(prop, "right") ? 2 : 3;
  4648. unsigned int pos = 0;
  4649. if (!(vend - vstart >= 4 &&
  4650. ci_eq_n(s + vstart, "auto", 4))) {
  4651. int v = parse_len(s + vstart, vend - vstart, &pos);
  4652. if (v > 32000)
  4653. v = 32000;
  4654. else if (v < -32000)
  4655. v = -32000;
  4656. decl_add(out, nout,
  4657. (unsigned char)(which == 0 ? PROP_LEFT :
  4658. which == 1 ? PROP_TOP :
  4659. which == 2 ? PROP_RIGHT :
  4660. PROP_BOTTOM),
  4661. v, important);
  4662. decl_add(out, nout,
  4663. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  4664. which == 1 ? PROP_TOP_SET :
  4665. which == 2 ? PROP_RIGHT_SET :
  4666. PROP_BOTTOM_SET),
  4667. 1, important);
  4668. }
  4669. } else if (str_eq(prop, "z-index")) {
  4670. unsigned int q = vstart;
  4671. int sign = 1;
  4672. int v = 0;
  4673. int any = 0;
  4674. if (q < vend && s[q] == '-') {
  4675. sign = -1;
  4676. q++;
  4677. }
  4678. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4679. v = v * 10 + (s[q] - '0');
  4680. any = 1;
  4681. q++;
  4682. }
  4683. if (any) {
  4684. v *= sign;
  4685. if (v > 32000)
  4686. v = 32000;
  4687. else if (v < -32000)
  4688. v = -32000;
  4689. decl_add(out, nout, PROP_ZINDEX, v, important);
  4690. decl_add(out, nout, PROP_ZSET, 1, important);
  4691. } else if (vend - vstart >= 4 &&
  4692. ci_eq_n(s + vstart, "auto", 4)) {
  4693. decl_add(out, nout, PROP_ZSET, 0, important);
  4694. }
  4695. } else if (str_eq(prop, "transform")) {
  4696. const char *v = s + vstart;
  4697. unsigned int vl = vend - vstart;
  4698. int tx = 0, ty = 0;
  4699. int is_x = contains_ci_len(v, vl, "translatex");
  4700. int is_y = contains_ci_len(v, vl, "translatey");
  4701. unsigned int off = 0, arg;
  4702. if (contains_ci_len(v, vl, "translate")) {
  4703. while (off < vl && v[off] != '(')
  4704. off++;
  4705. if (off < vl)
  4706. off++;
  4707. if (!is_y)
  4708. tx = parse_len(v, vl, &off);
  4709. if (!is_x) {
  4710. while (off < vl &&
  4711. (is_space(v[off]) || v[off] == ','))
  4712. off++;
  4713. ty = parse_len(v, vl, &off);
  4714. }
  4715. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  4716. important);
  4717. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  4718. important);
  4719. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  4720. important);
  4721. }
  4722. if (find_fn(v, vl, "scale", &arg)) {
  4723. unsigned int q = arg;
  4724. signed short sx = 100, sy = 100;
  4725. if (parse_fixed_100(v, vl, &q, &sx)) {
  4726. unsigned int r = q;
  4727. while (r < vl &&
  4728. (is_space(v[r]) || v[r] == ','))
  4729. r++;
  4730. if (!parse_fixed_100(v, vl, &r, &sy))
  4731. sy = sx;
  4732. decl_add(out, nout, PROP_SCALE_X, sx,
  4733. important);
  4734. decl_add(out, nout, PROP_SCALE_Y, sy,
  4735. important);
  4736. decl_add(out, nout,
  4737. PROP_TRANSFORM_PAINT, 1,
  4738. important);
  4739. decl_add(out, nout,
  4740. PROP_TRANSFORM_SET, 1,
  4741. important);
  4742. }
  4743. }
  4744. if (find_fn(v, vl, "rotate", &arg)) {
  4745. unsigned int q = arg;
  4746. signed short deg;
  4747. if (parse_fixed_100(v, vl, &q, &deg)) {
  4748. decl_add(out, nout, PROP_ROTATE, deg,
  4749. important);
  4750. decl_add(out, nout,
  4751. PROP_TRANSFORM_PAINT, 1,
  4752. important);
  4753. decl_add(out, nout,
  4754. PROP_TRANSFORM_SET, 1,
  4755. important);
  4756. }
  4757. }
  4758. } else if (str_eq(prop, "transform-origin")) {
  4759. add_transform_origin(out, nout, s + vstart,
  4760. vend - vstart, important);
  4761. } else if (str_eq(prop, "grid-template-columns") ||
  4762. str_eq(prop, "grid-template")) {
  4763. const char *v = s + vstart;
  4764. unsigned int vl = vend - vstart;
  4765. unsigned short val[2];
  4766. unsigned char kind[2];
  4767. int n, q;
  4768. if (str_eq(prop, "grid-template")) {
  4769. for (q = 0; q < (int)vl; q++)
  4770. if (v[q] == '/') {
  4771. q++;
  4772. while (q < (int)vl && is_space(v[q]))
  4773. q++;
  4774. v += q;
  4775. vl -= (unsigned int)q;
  4776. break;
  4777. }
  4778. }
  4779. if (grid_repeat_count(v, vl) >= 1) {
  4780. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  4781. * Tailwind's grid-cols-N. N equal columns
  4782. * (GRID_FIXED + N); one column is plain block
  4783. * flow, which also resets a smaller breakpoint's
  4784. * count. */
  4785. int cnt = grid_repeat_count(v, vl);
  4786. decl_add(out, nout, PROP_GRID_AUTO,
  4787. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  4788. important);
  4789. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  4790. important);
  4791. } else if (contains_ci_len(v, vl, "auto-fit") ||
  4792. contains_ci_len(v, vl, "auto-fill")) {
  4793. unsigned int mp = 0;
  4794. int found = 0;
  4795. unsigned short mw = 200;
  4796. unsigned char mk = 1;
  4797. for (q = 0; q + 7 <= (int)vl; q++)
  4798. if (ci_eq_n(v + q, "minmax(", 7)) {
  4799. mp = (unsigned int)q + 7;
  4800. found = 1;
  4801. break;
  4802. }
  4803. if (found)
  4804. parse_grid_track(v, vl, &mp, &mw, &mk);
  4805. if (mk != 1 || mw < 80)
  4806. mw = 200;
  4807. if (mw > 480)
  4808. mw = 480;
  4809. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  4810. decl_add(out, nout, PROP_WIDTH, mw, important);
  4811. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  4812. } else {
  4813. n = parse_grid_cols(v, vl, val, kind);
  4814. if (n > 0) {
  4815. decl_add(out, nout, PROP_WIDTH, val[0],
  4816. important);
  4817. decl_add(out, nout, PROP_WIDTH_KIND,
  4818. kind[0], important);
  4819. decl_add(out, nout, PROP_GRID_NCOLS, n,
  4820. important);
  4821. }
  4822. if (n > 1) {
  4823. decl_add(out, nout, PROP_MAXW, val[1],
  4824. important);
  4825. decl_add(out, nout, PROP_MAXW_KIND,
  4826. kind[1], important);
  4827. }
  4828. }
  4829. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  4830. /* From the value, not the block start: gap was
  4831. * ignored on nearly every site. */
  4832. unsigned int gp = vstart;
  4833. unsigned short gv = 0;
  4834. unsigned char gk = 1;
  4835. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  4836. if (gv > 120)
  4837. gv = 120;
  4838. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  4839. }
  4840. } else if (str_eq(prop, "grid-area")) {
  4841. int slot = parse_grid_slot(s + vstart, vend - vstart);
  4842. if (slot)
  4843. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  4844. } else if (str_eq(prop, "width") ||
  4845. str_eq(prop, "max-width") ||
  4846. str_eq(prop, "min-width") ||
  4847. str_eq(prop, "height") ||
  4848. str_eq(prop, "max-height") ||
  4849. str_eq(prop, "min-height")) {
  4850. unsigned short v;
  4851. unsigned char kind;
  4852. if (vend - vstart >= 5 &&
  4853. ci_eq_n(s + vstart, "calc(", 5)) {
  4854. int pct, px;
  4855. if (parse_calc(s + vstart + 5,
  4856. vend - vstart - 5, &pct, &px)) {
  4857. if (str_eq(prop, "width")) {
  4858. decl_add(out, nout, PROP_WIDTH,
  4859. 0, important);
  4860. decl_add(out, nout,
  4861. PROP_WIDTH_KIND, 3,
  4862. important);
  4863. decl_add(out, nout,
  4864. PROP_WIDTH_CALC_PCT,
  4865. pct, important);
  4866. decl_add(out, nout,
  4867. PROP_WIDTH_CALC_PX,
  4868. px, important);
  4869. } else if (str_eq(prop, "max-width")) {
  4870. decl_add(out, nout, PROP_MAXW,
  4871. 0, important);
  4872. decl_add(out, nout,
  4873. PROP_MAXW_KIND, 3,
  4874. important);
  4875. decl_add(out, nout,
  4876. PROP_MAXW_CALC_PCT,
  4877. pct, important);
  4878. decl_add(out, nout,
  4879. PROP_MAXW_CALC_PX,
  4880. px, important);
  4881. } else if (str_eq(prop, "min-width")) {
  4882. decl_add(out, nout, PROP_MINW,
  4883. 0, important);
  4884. decl_add(out, nout,
  4885. PROP_MINW_KIND, 3,
  4886. important);
  4887. decl_add(out, nout,
  4888. PROP_MINW_CALC_PCT,
  4889. pct, important);
  4890. decl_add(out, nout,
  4891. PROP_MINW_CALC_PX,
  4892. px, important);
  4893. } else if (str_eq(prop, "height")) {
  4894. decl_add(out, nout, PROP_HEIGHT,
  4895. 0, important);
  4896. decl_add(out, nout,
  4897. PROP_HEIGHT_KIND, 3,
  4898. important);
  4899. decl_add(out, nout,
  4900. PROP_HEIGHT_CALC_PCT,
  4901. pct, important);
  4902. decl_add(out, nout,
  4903. PROP_HEIGHT_CALC_PX,
  4904. px, important);
  4905. } else if (str_eq(prop, "max-height")) {
  4906. decl_add(out, nout, PROP_MAXH,
  4907. 0, important);
  4908. decl_add(out, nout,
  4909. PROP_MAXH_KIND, 3,
  4910. important);
  4911. decl_add(out, nout,
  4912. PROP_MAXH_CALC_PCT,
  4913. pct, important);
  4914. decl_add(out, nout,
  4915. PROP_MAXH_CALC_PX,
  4916. px, important);
  4917. } else {
  4918. decl_add(out, nout, PROP_MINH,
  4919. 0, important);
  4920. decl_add(out, nout,
  4921. PROP_MINH_KIND, 3,
  4922. important);
  4923. decl_add(out, nout,
  4924. PROP_MINH_CALC_PCT,
  4925. pct, important);
  4926. decl_add(out, nout,
  4927. PROP_MINH_CALC_PX,
  4928. px, important);
  4929. }
  4930. }
  4931. continue;
  4932. }
  4933. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  4934. if (str_eq(prop, "width")) {
  4935. decl_add(out, nout, PROP_WIDTH, v, important);
  4936. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  4937. important);
  4938. } else if (str_eq(prop, "max-width")) {
  4939. decl_add(out, nout, PROP_MAXW, v, important);
  4940. decl_add(out, nout, PROP_MAXW_KIND, kind,
  4941. important);
  4942. } else if (str_eq(prop, "min-width")) {
  4943. decl_add(out, nout, PROP_MINW, v, important);
  4944. decl_add(out, nout, PROP_MINW_KIND, kind,
  4945. important);
  4946. } else if (str_eq(prop, "max-height")) {
  4947. decl_add(out, nout, PROP_MAXH, v, important);
  4948. decl_add(out, nout, PROP_MAXH_KIND, kind,
  4949. important);
  4950. } else if (str_eq(prop, "min-height")) {
  4951. decl_add(out, nout, PROP_MINH, v, important);
  4952. decl_add(out, nout, PROP_MINH_KIND, kind,
  4953. important);
  4954. } else {
  4955. decl_add(out, nout, PROP_HEIGHT, v, important);
  4956. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  4957. important);
  4958. }
  4959. }
  4960. } else if (str_eq(prop, "content")) {
  4961. decl_add(out, nout, PROP_CONTENT,
  4962. css_collect_only ? 0 :
  4963. parse_content(d, s + vstart, vend - vstart),
  4964. important);
  4965. } else if (str_eq(prop, "aspect-ratio")) {
  4966. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  4967. * preferred height of an auto-height box. */
  4968. unsigned int q = vstart;
  4969. unsigned int a[2] = { 0, 1 }, k;
  4970. for (k = 0; k < 2; k++) {
  4971. unsigned int whole = 0, frac = 0, sc = 1;
  4972. while (q < vend && is_space(s[q]))
  4973. q++;
  4974. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  4975. s[q] != '.'))
  4976. break;
  4977. while (q < vend && s[q] >= '0' && s[q] <= '9')
  4978. whole = whole * 10 +
  4979. (unsigned int)(s[q++] - '0');
  4980. if (q < vend && s[q] == '.') {
  4981. q++;
  4982. while (q < vend && s[q] >= '0' &&
  4983. s[q] <= '9') {
  4984. if (sc < 100) {
  4985. frac = frac * 10 +
  4986. (unsigned int)(s[q] - '0');
  4987. sc *= 10;
  4988. }
  4989. q++;
  4990. }
  4991. }
  4992. a[k] = whole * 100 + frac * (100 / sc);
  4993. while (q < vend && is_space(s[q]))
  4994. q++;
  4995. if (k == 0 && (q >= vend || s[q] != '/')) {
  4996. a[1] = 100;
  4997. break;
  4998. }
  4999. q++;
  5000. }
  5001. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5002. a[0] = a[1] = 0;
  5003. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5004. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5005. } else if (str_eq(prop, "box-sizing")) {
  5006. int bb = (vend - vstart >= 10 &&
  5007. ci_eq_n(s + vstart, "border-box", 10));
  5008. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5009. } else if (str_eq(prop, "line-height")) {
  5010. unsigned short px;
  5011. unsigned char pct;
  5012. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5013. if (px) {
  5014. decl_add(out, nout, PROP_LH_PX, px, important);
  5015. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5016. } else {
  5017. decl_add(out, nout, PROP_LH_PX, 0, important);
  5018. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5019. }
  5020. }
  5021. } else if (str_eq(prop, "list-style") ||
  5022. str_eq(prop, "list-style-type")) {
  5023. int ls = BR_LIST_AUTO;
  5024. if (contains_ci_len(s + vstart, vend - vstart,
  5025. "none"))
  5026. ls = BR_LIST_NONE;
  5027. else if (contains_ci_len(s + vstart, vend - vstart,
  5028. "circle"))
  5029. ls = BR_LIST_CIRCLE;
  5030. else if (contains_ci_len(s + vstart, vend - vstart,
  5031. "square"))
  5032. ls = BR_LIST_SQUARE;
  5033. else if (contains_ci_len(s + vstart, vend - vstart,
  5034. "lower-alpha") ||
  5035. contains_ci_len(s + vstart, vend - vstart,
  5036. "lower-latin"))
  5037. ls = BR_LIST_LOWER_ALPHA;
  5038. else if (contains_ci_len(s + vstart, vend - vstart,
  5039. "upper-alpha") ||
  5040. contains_ci_len(s + vstart, vend - vstart,
  5041. "upper-latin"))
  5042. ls = BR_LIST_UPPER_ALPHA;
  5043. else if (contains_ci_len(s + vstart, vend - vstart,
  5044. "lower-roman"))
  5045. ls = BR_LIST_LOWER_ROMAN;
  5046. else if (contains_ci_len(s + vstart, vend - vstart,
  5047. "upper-roman"))
  5048. ls = BR_LIST_UPPER_ROMAN;
  5049. else if (contains_ci_len(s + vstart, vend - vstart,
  5050. "decimal"))
  5051. ls = BR_LIST_DECIMAL;
  5052. else if (contains_ci_len(s + vstart, vend - vstart,
  5053. "disc"))
  5054. ls = BR_LIST_DISC;
  5055. decl_add(out, nout, PROP_LIST_NONE,
  5056. ls == BR_LIST_NONE, important);
  5057. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5058. } else if (str_eq(prop, "border-radius") ||
  5059. str_eq(prop, "border-top-left-radius") ||
  5060. str_eq(prop, "border-top-right-radius") ||
  5061. str_eq(prop, "border-bottom-right-radius") ||
  5062. str_eq(prop, "border-bottom-left-radius")) {
  5063. unsigned short v[4];
  5064. unsigned char k[4];
  5065. unsigned int pos = 0;
  5066. int n = 0;
  5067. while (pos < vend - vstart && n < 4) {
  5068. while (pos < vend - vstart &&
  5069. is_space(s[vstart + pos]))
  5070. pos++;
  5071. if (pos >= vend - vstart ||
  5072. s[vstart + pos] == '/')
  5073. break;
  5074. if (!parse_radius_value(s + vstart,
  5075. vend - vstart, &pos,
  5076. &v[n], &k[n]))
  5077. break;
  5078. n++;
  5079. }
  5080. if (n) {
  5081. unsigned short r[4];
  5082. unsigned char rk[4];
  5083. int i;
  5084. if (n == 1) {
  5085. for (i = 0; i < 4; i++) {
  5086. r[i] = v[0];
  5087. rk[i] = k[0];
  5088. }
  5089. } else if (n == 2) {
  5090. r[0] = r[2] = v[0];
  5091. r[1] = r[3] = v[1];
  5092. rk[0] = rk[2] = k[0];
  5093. rk[1] = rk[3] = k[1];
  5094. } else if (n == 3) {
  5095. r[0] = v[0];
  5096. r[1] = r[3] = v[1];
  5097. r[2] = v[2];
  5098. rk[0] = k[0];
  5099. rk[1] = rk[3] = k[1];
  5100. rk[2] = k[2];
  5101. } else {
  5102. for (i = 0; i < 4; i++) {
  5103. r[i] = v[i];
  5104. rk[i] = k[i];
  5105. }
  5106. }
  5107. for (i = 0; i < 4; i++)
  5108. r[i] = rk[i] == 2 ? 255 :
  5109. (r[i] > 255 ? 255 : r[i]);
  5110. if (str_eq(prop, "border-top-left-radius")) {
  5111. decl_add(out, nout, PROP_RADIUS_TL,
  5112. (short)r[0], important);
  5113. } else if (str_eq(prop,
  5114. "border-top-right-radius")) {
  5115. decl_add(out, nout, PROP_RADIUS_TR,
  5116. (short)r[0], important);
  5117. } else if (str_eq(prop,
  5118. "border-bottom-right-radius")) {
  5119. decl_add(out, nout, PROP_RADIUS_BR,
  5120. (short)r[0], important);
  5121. } else if (str_eq(prop,
  5122. "border-bottom-left-radius")) {
  5123. decl_add(out, nout, PROP_RADIUS_BL,
  5124. (short)r[0], important);
  5125. } else {
  5126. decl_add(out, nout, PROP_RADIUS_TL,
  5127. (short)r[0], important);
  5128. decl_add(out, nout, PROP_RADIUS_TR,
  5129. (short)r[1], important);
  5130. decl_add(out, nout, PROP_RADIUS_BR,
  5131. (short)r[2], important);
  5132. decl_add(out, nout, PROP_RADIUS_BL,
  5133. (short)r[3], important);
  5134. }
  5135. }
  5136. } else if (str_eq(prop, "border") ||
  5137. str_eq(prop, "border-top") ||
  5138. str_eq(prop, "border-right") ||
  5139. str_eq(prop, "border-bottom") ||
  5140. str_eq(prop, "border-left") ||
  5141. str_eq(prop, "border-color") ||
  5142. str_eq(prop, "border-width") ||
  5143. str_eq(prop, "border-style") ||
  5144. str_eq(prop, "border-top-width") ||
  5145. str_eq(prop, "border-right-width") ||
  5146. str_eq(prop, "border-bottom-width") ||
  5147. str_eq(prop, "border-left-width") ||
  5148. str_eq(prop, "border-top-color") ||
  5149. str_eq(prop, "border-right-color") ||
  5150. str_eq(prop, "border-bottom-color") ||
  5151. str_eq(prop, "border-left-color") ||
  5152. str_eq(prop, "border-top-style") ||
  5153. str_eq(prop, "border-right-style") ||
  5154. str_eq(prop, "border-bottom-style") ||
  5155. str_eq(prop, "border-left-style")) {
  5156. const char *v = s + vstart;
  5157. unsigned int vl = vend - vstart;
  5158. unsigned int pos = 0, ts, te;
  5159. int widths[4], colors[4], styles[4], trans[4];
  5160. int nw = 0, nc = 0, ns = 0;
  5161. int side = -1;
  5162. int is_wl = str_eq(prop, "border-width");
  5163. int is_cl = str_eq(prop, "border-color");
  5164. int is_sl = str_eq(prop, "border-style");
  5165. int i;
  5166. if (str_eq(prop, "border-top")) side = 0;
  5167. else if (str_eq(prop, "border-right")) side = 1;
  5168. else if (str_eq(prop, "border-bottom")) side = 2;
  5169. else if (str_eq(prop, "border-left")) side = 3;
  5170. else if (str_eq(prop, "border-top-width")) {
  5171. side = 0; is_wl = 2;
  5172. } else if (str_eq(prop, "border-right-width")) {
  5173. side = 1; is_wl = 2;
  5174. } else if (str_eq(prop, "border-bottom-width")) {
  5175. side = 2; is_wl = 2;
  5176. } else if (str_eq(prop, "border-left-width")) {
  5177. side = 3; is_wl = 2;
  5178. } else if (str_eq(prop, "border-top-color")) {
  5179. side = 0; is_cl = 2;
  5180. } else if (str_eq(prop, "border-right-color")) {
  5181. side = 1; is_cl = 2;
  5182. } else if (str_eq(prop, "border-bottom-color")) {
  5183. side = 2; is_cl = 2;
  5184. } else if (str_eq(prop, "border-left-color")) {
  5185. side = 3; is_cl = 2;
  5186. } else if (str_eq(prop, "border-top-style")) {
  5187. side = 0; is_sl = 2;
  5188. } else if (str_eq(prop, "border-right-style")) {
  5189. side = 1; is_sl = 2;
  5190. } else if (str_eq(prop, "border-bottom-style")) {
  5191. side = 2; is_sl = 2;
  5192. } else if (str_eq(prop, "border-left-style")) {
  5193. side = 3; is_sl = 2;
  5194. }
  5195. if (is_wl) {
  5196. while (nw < 4 && next_token(v, vl, &pos,
  5197. &ts, &te)) {
  5198. int x;
  5199. if (!parse_len_token(v + ts, te - ts,
  5200. &x))
  5201. break;
  5202. widths[nw++] = x;
  5203. }
  5204. if (nw) {
  5205. if (is_wl == 1)
  5206. expand_four(widths, nw);
  5207. if (side >= 0 && is_wl == 2) {
  5208. decl_add(out, nout,
  5209. PROP_BW_T + side,
  5210. widths[0], important);
  5211. } else {
  5212. for (i = 0; i < 4; i++)
  5213. decl_add(out, nout,
  5214. PROP_BW_T + i,
  5215. widths[i],
  5216. important);
  5217. }
  5218. }
  5219. } else if (is_cl) {
  5220. unsigned short c;
  5221. int t;
  5222. while (nc < 4 && next_token(v, vl, &pos,
  5223. &ts, &te)) {
  5224. t = color_is_transparent(v + ts,
  5225. te - ts);
  5226. if (!t && !parse_color(v + ts,
  5227. te - ts, &c))
  5228. break;
  5229. colors[nc] = t ? 0 : c;
  5230. trans[nc] = t;
  5231. nc++;
  5232. }
  5233. if (nc) {
  5234. if (is_cl == 1) {
  5235. expand_four(colors, nc);
  5236. expand_four(trans, nc);
  5237. }
  5238. if (side >= 0 && is_cl == 2) {
  5239. decl_add(out, nout,
  5240. PROP_BC_T + side,
  5241. colors[0], important);
  5242. decl_add(out, nout,
  5243. PROP_BT_T + side,
  5244. trans[0], important);
  5245. } else {
  5246. for (i = 0; i < 4; i++) {
  5247. decl_add(out, nout,
  5248. PROP_BC_T + i,
  5249. colors[i],
  5250. important);
  5251. decl_add(out, nout,
  5252. PROP_BT_T + i,
  5253. trans[i],
  5254. important);
  5255. }
  5256. }
  5257. }
  5258. } else if (is_sl) {
  5259. while (ns < 4 && next_token(v, vl, &pos,
  5260. &ts, &te)) {
  5261. int stv;
  5262. if ((te - ts == 4 &&
  5263. ci_eq_n(v + ts, "none", 4)) ||
  5264. (te - ts == 6 &&
  5265. ci_eq_n(v + ts, "hidden", 6)))
  5266. stv = 0;
  5267. else if (te - ts == 6 &&
  5268. ci_eq_n(v + ts,
  5269. "dashed", 6))
  5270. stv = 2;
  5271. else if (te - ts == 6 &&
  5272. ci_eq_n(v + ts,
  5273. "dotted", 6))
  5274. stv = 3;
  5275. else
  5276. stv = 1;
  5277. styles[ns++] = stv;
  5278. }
  5279. if (ns) {
  5280. if (is_sl == 1)
  5281. expand_four(styles, ns);
  5282. if (is_sl == 2 && side >= 0) {
  5283. if (!styles[0])
  5284. decl_add(out, nout,
  5285. PROP_BW_T + side,
  5286. 0, important);
  5287. else
  5288. decl_add(out, nout,
  5289. PROP_BS_T + side,
  5290. styles[0] - 1,
  5291. important);
  5292. } else {
  5293. for (i = 0; i < 4; i++) {
  5294. if (!styles[i])
  5295. decl_add(out, nout,
  5296. PROP_BW_T + i,
  5297. 0,
  5298. important);
  5299. else
  5300. decl_add(out, nout,
  5301. PROP_BS_T + i,
  5302. styles[i] - 1,
  5303. important);
  5304. }
  5305. }
  5306. }
  5307. } else {
  5308. int bw = -1, bc = -1, style_none = 0;
  5309. int bstyle = -1, btrans = 0;
  5310. while (next_token(v, vl, &pos, &ts, &te)) {
  5311. unsigned short c;
  5312. int x;
  5313. if ((te - ts == 4 &&
  5314. ci_eq_n(v + ts, "none", 4)) ||
  5315. (te - ts == 6 &&
  5316. ci_eq_n(v + ts, "hidden", 6))) {
  5317. style_none = 1;
  5318. } else if (te - ts == 6 &&
  5319. ci_eq_n(v + ts, "dashed", 6)) {
  5320. bstyle = BR_BORDER_DASHED;
  5321. } else if (te - ts == 6 &&
  5322. ci_eq_n(v + ts, "dotted", 6)) {
  5323. bstyle = BR_BORDER_DOTTED;
  5324. } else if (te - ts == 5 &&
  5325. ci_eq_n(v + ts, "solid", 5)) {
  5326. bstyle = BR_BORDER_SOLID;
  5327. } else if (parse_len_token(v + ts,
  5328. te - ts, &x)) {
  5329. bw = x;
  5330. } else if (color_is_transparent(v + ts,
  5331. te - ts)) {
  5332. bc = 0;
  5333. btrans = 1;
  5334. } else if (parse_color(v + ts,
  5335. te - ts, &c)) {
  5336. bc = c;
  5337. btrans = 0;
  5338. }
  5339. }
  5340. if (style_none)
  5341. bw = 0;
  5342. if (bc >= 0 && bw < 0)
  5343. bw = 1;
  5344. if (side < 0) {
  5345. if (bw >= 0)
  5346. decl_add(out, nout,
  5347. PROP_BORDER_W, bw,
  5348. important);
  5349. if (bc >= 0)
  5350. decl_add(out, nout,
  5351. PROP_BORDER_COLOR, bc,
  5352. important);
  5353. if (bc >= 0)
  5354. for (i = 0; i < 4; i++)
  5355. decl_add(out, nout,
  5356. PROP_BT_T + i,
  5357. btrans,
  5358. important);
  5359. if (bstyle >= 0)
  5360. for (i = 0; i < 4; i++)
  5361. decl_add(out, nout,
  5362. PROP_BS_T + i,
  5363. bstyle,
  5364. important);
  5365. } else {
  5366. if (bw >= 0)
  5367. decl_add(out, nout,
  5368. PROP_BW_T + side, bw,
  5369. important);
  5370. if (bc >= 0)
  5371. decl_add(out, nout,
  5372. PROP_BC_T + side, bc,
  5373. important);
  5374. if (bc >= 0)
  5375. decl_add(out, nout,
  5376. PROP_BT_T + side,
  5377. btrans, important);
  5378. if (bstyle >= 0)
  5379. decl_add(out, nout,
  5380. PROP_BS_T + side,
  5381. bstyle, important);
  5382. }
  5383. }
  5384. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  5385. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  5386. unsigned int pos = 0;
  5387. int is_pad = prop[0] == 'p';
  5388. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  5389. while (pos < vend - vstart && nv < 4) {
  5390. units[nv] = 0;
  5391. if (parse_margin_calc(s + vstart,
  5392. vend - vstart, &pos,
  5393. &vals[nv], &cpx[nv])) {
  5394. units[nv] = 5;
  5395. } else {
  5396. vals[nv] = parse_margin_len(
  5397. s + vstart, vend - vstart,
  5398. &pos, &units[nv]);
  5399. }
  5400. nv++;
  5401. while (pos < vend - vstart &&
  5402. is_space(s[vstart + pos]))
  5403. pos++;
  5404. }
  5405. if (nv == 1) {
  5406. vals[1] = vals[2] = vals[3] = vals[0];
  5407. units[1] = units[2] = units[3] = units[0];
  5408. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  5409. } else if (nv == 2) {
  5410. vals[2] = vals[0];
  5411. vals[3] = vals[1];
  5412. units[2] = units[0];
  5413. units[3] = units[1];
  5414. cpx[2] = cpx[0];
  5415. cpx[3] = cpx[1];
  5416. } else if (nv == 3) {
  5417. vals[3] = vals[1];
  5418. units[3] = units[1];
  5419. cpx[3] = cpx[1];
  5420. }
  5421. if (nv > 0) {
  5422. int k;
  5423. for (k = 0; k < 4; k++) {
  5424. int v = vals[k];
  5425. int u = units[k];
  5426. int au = u == 4;
  5427. if (au)
  5428. v = 0;
  5429. if (is_pad && au)
  5430. u = 0;
  5431. decl_add(out, nout,
  5432. (unsigned char)(base + k), v,
  5433. important);
  5434. decl_add(out, nout,
  5435. (unsigned char)
  5436. ((is_pad ? PROP_PT_U :
  5437. PROP_MT_U) + k),
  5438. u, important);
  5439. if (u == 5)
  5440. decl_add(out, nout,
  5441. (unsigned char)
  5442. ((is_pad ? PROP_PT_CPX :
  5443. PROP_MT_CPX) + k),
  5444. cpx[k], important);
  5445. if (!is_pad && (k == 1 || k == 3))
  5446. decl_add(out, nout,
  5447. k == 1 ? PROP_MR_AUTO :
  5448. PROP_ML_AUTO,
  5449. au, important);
  5450. }
  5451. }
  5452. } else if (str_eq(prop, "margin-top") ||
  5453. str_eq(prop, "margin-right") ||
  5454. str_eq(prop, "margin-bottom") ||
  5455. str_eq(prop, "margin-left") ||
  5456. str_eq(prop, "padding-top") ||
  5457. str_eq(prop, "padding-right") ||
  5458. str_eq(prop, "padding-bottom") ||
  5459. str_eq(prop, "padding-left")) {
  5460. unsigned int pos = 0;
  5461. int unit = 0;
  5462. int v = 0, cpx = 0;
  5463. unsigned char p = PROP_MT;
  5464. const char *side;
  5465. int au;
  5466. if (parse_margin_calc(s + vstart, vend - vstart,
  5467. &pos, &v, &cpx))
  5468. unit = 5;
  5469. else
  5470. v = parse_margin_len(s + vstart,
  5471. vend - vstart, &pos,
  5472. &unit);
  5473. au = unit == 4;
  5474. if (au)
  5475. v = 0;
  5476. if (prop[0] == 'p') {
  5477. p = PROP_PT;
  5478. if (au)
  5479. unit = 0;
  5480. }
  5481. side = prop + (prop[0] == 'p' ? 8 : 7);
  5482. if (str_eq(side, "right"))
  5483. p = (unsigned char)(p + 1);
  5484. else if (str_eq(side, "bottom"))
  5485. p = (unsigned char)(p + 2);
  5486. else if (str_eq(side, "left"))
  5487. p = (unsigned char)(p + 3);
  5488. decl_add(out, nout, p, v, important);
  5489. decl_add(out, nout,
  5490. (unsigned char)((prop[0] == 'p' ?
  5491. PROP_PT_U : PROP_MT_U) +
  5492. (p - (prop[0] == 'p' ?
  5493. PROP_PT : PROP_MT))),
  5494. unit, important);
  5495. if (unit == 5)
  5496. decl_add(out, nout,
  5497. (unsigned char)((prop[0] == 'p' ?
  5498. PROP_PT_CPX : PROP_MT_CPX) +
  5499. (p - (prop[0] == 'p' ?
  5500. PROP_PT : PROP_MT))),
  5501. cpx, important);
  5502. if (str_eq(side, "right"))
  5503. decl_add(out, nout, PROP_MR_AUTO, au, important);
  5504. else if (str_eq(side, "left"))
  5505. decl_add(out, nout, PROP_ML_AUTO, au, important);
  5506. }
  5507. }
  5508. }
  5509. /* Skip @media/@font-face/@keyframes/@import without descending into their
  5510. * nested blocks. This is the mobile/base rules path: media enhancements are
  5511. * deliberately ignored for now. */
  5512. static unsigned int skip_at_rule(const char *s, unsigned int len,
  5513. unsigned int i)
  5514. {
  5515. unsigned int j = i;
  5516. while (j < len && s[j] != '{' && s[j] != ';')
  5517. j++;
  5518. if (j >= len)
  5519. return len;
  5520. if (s[j] == ';')
  5521. return j + 1;
  5522. j++;
  5523. return css_skip_nested(s, len, j);
  5524. }
  5525. static unsigned int viewport_w = 1024;
  5526. static unsigned short theme_text = 0x0000;
  5527. static unsigned short theme_bg = 0xFFFF;
  5528. static unsigned short theme_panel = 0xEF5D;
  5529. static unsigned short theme_link = 0x001B;
  5530. void br_set_theme(unsigned short text, unsigned short bg,
  5531. unsigned short panel, unsigned short link)
  5532. {
  5533. theme_text = text;
  5534. theme_bg = bg;
  5535. theme_panel = panel;
  5536. theme_link = link;
  5537. }
  5538. void br_set_viewport(unsigned int width)
  5539. {
  5540. viewport_w = width ? width : 1024;
  5541. }
  5542. static unsigned int media_len_px(const char *s, unsigned int len)
  5543. {
  5544. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  5545. int em = 0;
  5546. while (i < len && is_space(s[i]))
  5547. i++;
  5548. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5549. whole = whole * 10 + (unsigned int)(s[i] - '0');
  5550. i++;
  5551. }
  5552. if (i < len && s[i] == '.') {
  5553. i++;
  5554. /* Consume every fractional digit even when there are more
  5555. * than the three we keep: leaving the tail behind made the
  5556. * unit check miss "em" (74.9375em became 74px). */
  5557. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5558. if (scale < 1000) {
  5559. frac = frac * 10 + (unsigned int)(s[i] - '0');
  5560. scale *= 10;
  5561. }
  5562. i++;
  5563. }
  5564. }
  5565. while (i < len && is_space(s[i]))
  5566. i++;
  5567. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  5568. em = 1;
  5569. i += 2;
  5570. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  5571. em = 1;
  5572. i += 3;
  5573. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  5574. i += 2;
  5575. }
  5576. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  5577. if (em)
  5578. return v * 16 / 100;
  5579. return v / 100;
  5580. }
  5581. /* One parenthesised media feature: min-width/max-width and the modern
  5582. * (width <= 600px) form. Unknown features are treated as true. */
  5583. static int media_feature_matches(const char *s, unsigned int len)
  5584. {
  5585. unsigned int i;
  5586. while (len && is_space(*s)) { s++; len--; }
  5587. while (len && is_space(s[len - 1])) len--;
  5588. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  5589. /* There is no user-selectable colour scheme. Ignore both
  5590. * variants so a page's base palette is used; sites whose base
  5591. * is dark (like the project docs) then stay dark instead of
  5592. * flipping to their light media override. */
  5593. return 0;
  5594. }
  5595. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  5596. for (i = 9; i < len && s[i] != ':'; i++)
  5597. ;
  5598. if (i < len)
  5599. return viewport_w >= media_len_px(s + i + 1,
  5600. len - i - 1);
  5601. return 1;
  5602. }
  5603. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  5604. for (i = 9; i < len && s[i] != ':'; i++)
  5605. ;
  5606. if (i < len)
  5607. return viewport_w <= media_len_px(s + i + 1,
  5608. len - i - 1);
  5609. return 1;
  5610. }
  5611. for (i = 0; i < len; i++) {
  5612. if (s[i] == '<' || s[i] == '>') {
  5613. int max = s[i] == '<';
  5614. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  5615. if (i + 1 < len && s[i + 1] == '=')
  5616. v = media_len_px(s + i + 2, len - i - 2);
  5617. return max ? viewport_w <= v : viewport_w >= v;
  5618. }
  5619. }
  5620. return 1;
  5621. }
  5622. static int media_query_matches(const char *s, unsigned int len)
  5623. {
  5624. unsigned int i;
  5625. if (contains_ci_len(s, len, "print") &&
  5626. !contains_ci_len(s, len, "screen"))
  5627. return 0;
  5628. if (contains_ci_len(s, len, "not "))
  5629. return 0;
  5630. for (i = 0; i < len; i++) {
  5631. if (s[i] == '(') {
  5632. unsigned int st = ++i;
  5633. while (i < len && s[i] != ')')
  5634. i++;
  5635. if (!media_feature_matches(s + st, i - st))
  5636. return 0;
  5637. }
  5638. }
  5639. return 1;
  5640. }
  5641. static int media_list_matches(const char *s, unsigned int len)
  5642. {
  5643. unsigned int i = 0;
  5644. while (i < len) {
  5645. unsigned int st = i;
  5646. while (i < len && s[i] != ',')
  5647. i++;
  5648. if (media_query_matches(s + st, i - st))
  5649. return 1;
  5650. i++;
  5651. }
  5652. return 0;
  5653. }
  5654. static unsigned int rule_hash(const char *s, unsigned int len)
  5655. {
  5656. unsigned int h = 2166136261u, i;
  5657. for (i = 0; i < len; i++) {
  5658. char c = s[i];
  5659. if (c >= 'A' && c <= 'Z')
  5660. c = (char)(c - 'A' + 'a');
  5661. h ^= (unsigned char)c;
  5662. h *= 16777619u;
  5663. }
  5664. return h % BR_RULE_HASH;
  5665. }
  5666. /* Index a parsed rule by the most selective component of its rightmost
  5667. * compound selector. br_style_for() then only visits rules that can
  5668. * possibly match a node instead of scanning every rule. */
  5669. static unsigned int sel_skip_group(const char *s, unsigned int i,
  5670. unsigned int len);
  5671. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  5672. char *out, unsigned int cap, unsigned int *olen);
  5673. static void rule_index_add(struct br_doc *d, unsigned int idx,
  5674. const char *key, unsigned int len)
  5675. {
  5676. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  5677. * "md:flex", the class the element carries, not under "md". */
  5678. char cname[96], iname[96];
  5679. unsigned int i, clen = 0, ilen = 0;
  5680. unsigned short *head;
  5681. for (i = 0; i < len; i++) {
  5682. if (key[i] == '\\') {
  5683. i++;
  5684. continue;
  5685. }
  5686. if (key[i] == '(' || key[i] == '[') {
  5687. /* :not(.x) / [href="a.b"] must not pick a bucket */
  5688. i = sel_skip_group(key, i, len) - 1;
  5689. continue;
  5690. }
  5691. if (key[i] == '.' || key[i] == '#') {
  5692. char tmp[96];
  5693. unsigned int n, e;
  5694. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  5695. if (n && n + 1 < sizeof(tmp)) {
  5696. if (key[i] == '#') {
  5697. memcpy(iname, tmp, n);
  5698. ilen = n;
  5699. } else {
  5700. memcpy(cname, tmp, n);
  5701. clen = n;
  5702. }
  5703. }
  5704. i = e - 1;
  5705. }
  5706. }
  5707. if (ilen)
  5708. head = &d->rule_bucket[rule_hash(iname, ilen)];
  5709. else if (clen)
  5710. head = &d->rule_bucket[rule_hash(cname, clen)];
  5711. else if (len == 5 && ci_eq_n(key, ":root", 5))
  5712. head = &d->rule_tag[BR_TAG_HTML];
  5713. else if (len && is_ident(key[0])) {
  5714. char name[16];
  5715. unsigned int n = 0, tag;
  5716. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  5717. name[n] = key[n];
  5718. n++;
  5719. }
  5720. name[n] = '\0';
  5721. tag = br_tag_from_name(name);
  5722. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  5723. head = &d->rule_tag[tag];
  5724. else
  5725. head = &d->rule_any;
  5726. } else {
  5727. head = &d->rule_any;
  5728. }
  5729. d->rule_next[idx] = *head;
  5730. *head = (unsigned short)idx;
  5731. }
  5732. #define SEL_PARTS 16
  5733. struct sel_part {
  5734. unsigned short st, en;
  5735. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  5736. };
  5737. static unsigned int selector_specificity(const char *s, unsigned int len);
  5738. static int css_selector_supported(const char *s, unsigned int len);
  5739. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  5740. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  5741. unsigned int *st, unsigned int *en);
  5742. static int css_collect_only;
  5743. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  5744. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  5745. * selector with any other pseudo-element. */
  5746. static int css_pseudo_element(char *sel, unsigned int *len)
  5747. {
  5748. static const struct {
  5749. const char *s;
  5750. unsigned char l, pe;
  5751. } pe[] = {
  5752. { "::before", 8, 1 }, { "::after", 7, 2 },
  5753. { ":before", 7, 1 }, { ":after", 6, 2 },
  5754. };
  5755. unsigned int k, n = *len;
  5756. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  5757. unsigned int l = pe[k].l;
  5758. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  5759. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  5760. continue; /* matched the tail of "::x" */
  5761. n -= l;
  5762. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  5763. sel[n - 1] == '+' || sel[n - 1] == '~')
  5764. sel[n++] = '*';
  5765. sel[n] = '\0';
  5766. *len = n;
  5767. return pe[k].pe;
  5768. }
  5769. }
  5770. for (k = 0; k + 1 < n; k++)
  5771. if (sel[k] == ':' && sel[k + 1] == ':')
  5772. return -1;
  5773. return 0;
  5774. }
  5775. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  5776. {
  5777. unsigned int i = 0;
  5778. while (i < len && d->nrules < d->rule_cap) {
  5779. unsigned int sstart, send, bstart, bend;
  5780. struct br_decl decls[BR_DECLS];
  5781. unsigned int nd;
  5782. while (i < len && is_space(s[i]))
  5783. i++;
  5784. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  5785. i += 2;
  5786. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  5787. i++;
  5788. i += 2;
  5789. continue;
  5790. }
  5791. if (i >= len || s[i] == '}')
  5792. break;
  5793. if (s[i] == '@') {
  5794. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  5795. unsigned int q = i + 6;
  5796. while (q < len && s[q] != '{')
  5797. q++;
  5798. if (q < len &&
  5799. media_list_matches(s + i + 6,
  5800. q - (i + 6))) {
  5801. unsigned int b = q + 1;
  5802. unsigned int e = css_skip_nested(s, len, b);
  5803. if (e > b)
  5804. parse_css(d, s + b,
  5805. (e - 1) - b);
  5806. i = e;
  5807. } else {
  5808. i = skip_at_rule(s, len, i);
  5809. }
  5810. continue;
  5811. }
  5812. i = skip_at_rule(s, len, i);
  5813. continue;
  5814. }
  5815. sstart = i;
  5816. while (i < len && s[i] != '{')
  5817. i++;
  5818. if (i >= len)
  5819. break;
  5820. send = i;
  5821. i++;
  5822. bstart = i;
  5823. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  5824. * to the matching close brace; stopping at the first '}' left
  5825. * the parser out of sync and dropped the rest of the sheet
  5826. * (nd-aktuell lost its desktop rules that way). */
  5827. bend = css_block_end(s, len, i);
  5828. i = bend < len ? bend + 1 : len;
  5829. while (sstart < send && is_space(s[sstart]))
  5830. sstart++;
  5831. while (send > sstart && is_space(s[send - 1]))
  5832. send--;
  5833. if (sstart == send)
  5834. continue;
  5835. if (!css_dark_selector(s + sstart, send - sstart))
  5836. css_collect_block(d, s + bstart, bend - bstart,
  5837. css_root_selector(s + sstart,
  5838. send - sstart));
  5839. if (css_collect_only)
  5840. continue;
  5841. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  5842. if (!nd)
  5843. continue;
  5844. /* Split a comma-separated selector list into separate rules.
  5845. * A selector the engine cannot match is dropped; the others
  5846. * in the list still apply. */
  5847. {
  5848. const char *list = s + sstart;
  5849. unsigned int ll = send - sstart, pos = 0, st, en;
  5850. while (d->nrules < d->rule_cap &&
  5851. sel_list_next(list, ll, &pos, &st, &en)) {
  5852. struct sel_part parts[SEL_PARTS];
  5853. struct br_rule *r;
  5854. char sel[160];
  5855. unsigned int k, off;
  5856. int np, pe;
  5857. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  5858. k++)
  5859. sel[k] = is_space(list[st + k]) ? ' ' :
  5860. list[st + k];
  5861. if (st + k < en)
  5862. continue; /* too long */
  5863. sel[k] = '\0';
  5864. pe = css_pseudo_element(sel, &k);
  5865. if (pe < 0 || !css_selector_supported(sel, k))
  5866. continue;
  5867. np = sel_split(sel, k, parts);
  5868. off = store_attr(d, sel, k, 159);
  5869. if (np <= 0 || !off)
  5870. continue;
  5871. r = &d->rules[d->nrules++];
  5872. r->sel_off = off;
  5873. r->key_off = store_attr(d, sel + parts[np - 1].st,
  5874. parts[np - 1].en -
  5875. parts[np - 1].st, 159);
  5876. r->spec = (unsigned short)selector_specificity(sel, k);
  5877. r->single = np == 1;
  5878. r->pe = (unsigned char)pe;
  5879. if (pe)
  5880. d->npe_rules++;
  5881. r->nd = (unsigned char)nd;
  5882. for (k = 0; k < nd; k++)
  5883. r->d[k] = decls[k];
  5884. rule_index_add(d, d->nrules - 1,
  5885. attr_ptr(d, r->key_off),
  5886. s_len(attr_ptr(d, r->key_off)));
  5887. }
  5888. }
  5889. }
  5890. }
  5891. /* Sites often set html { font-size: 62.5% } and then size everything in
  5892. * rem. Detect that root size before parsing the sheet so a rem resolves to
  5893. * the same pixels a real browser would use. */
  5894. static void detect_root_font(const char *s, unsigned int len)
  5895. {
  5896. unsigned int i;
  5897. for (i = 0; i + 4 < len; i++) {
  5898. unsigned int j, end, body, sel, q, h;
  5899. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  5900. int exact;
  5901. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  5902. s[i + 3] == 'l'))
  5903. continue;
  5904. h = i + 4;
  5905. while (h < len && is_space(s[h]))
  5906. h++;
  5907. if (h < len && s[h] != '{' && s[h] != ',' &&
  5908. s[h] != '.' && s[h] != ':' && s[h] != '[')
  5909. continue;
  5910. sel = i;
  5911. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  5912. s[sel - 1] != ';' && s[sel - 1] != ',')
  5913. sel--;
  5914. q = sel;
  5915. while (q < i && is_space(s[q]))
  5916. q++;
  5917. exact = (q == i);
  5918. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  5919. continue;
  5920. j = h;
  5921. if (j >= len || s[j] != '{')
  5922. continue;
  5923. while (j < len && s[j] != '{')
  5924. j++;
  5925. if (j >= len)
  5926. break;
  5927. end = j + 1;
  5928. body = end;
  5929. while (end < len && s[end] != '}')
  5930. end++;
  5931. for (; j + 9 < end; j++) {
  5932. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  5933. s[j + 3] == 't' && s[j + 4] == '-' &&
  5934. s[j + 5] == 's' && s[j + 6] == 'i' &&
  5935. s[j + 7] == 'z' && s[j + 8] == 'e'))
  5936. continue;
  5937. j += 9;
  5938. while (j < end && s[j] != ':')
  5939. j++;
  5940. if (j >= end)
  5941. break;
  5942. j++;
  5943. while (j < end && is_space(s[j]))
  5944. j++;
  5945. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5946. v = v * 10 + (s[j] - '0');
  5947. digits = 1;
  5948. j++;
  5949. }
  5950. if (j < end && s[j] == '.') {
  5951. j++;
  5952. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5953. v = v * 10 + (s[j] - '0');
  5954. frac++;
  5955. j++;
  5956. }
  5957. }
  5958. while (frac < 2) {
  5959. v *= 10;
  5960. frac++;
  5961. }
  5962. while (frac > 2) {
  5963. v /= 10;
  5964. frac--;
  5965. }
  5966. if (!digits)
  5967. break;
  5968. if (j < end && s[j] == '%')
  5969. pct = 1;
  5970. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  5971. px = 1;
  5972. if (pct)
  5973. v = (v * 16 + 5000) / 10000;
  5974. else if (px)
  5975. v /= 100;
  5976. else
  5977. v = v * 16 / 100;
  5978. if (exact && v >= 8 && v <= 24)
  5979. root_font_px = v;
  5980. if (exact)
  5981. return;
  5982. }
  5983. /* html { font: 10px/1 Verdana } also sets the root size. */
  5984. for (j = body; j + 5 <= end; j++) {
  5985. unsigned int p, ts, te;
  5986. if (!ci_eq_n(s + j, "font:", 5))
  5987. continue;
  5988. p = j + 5;
  5989. while (next_token(s, end, &p, &ts, &te)) {
  5990. int fv = parse_font_px(s + ts, te - ts);
  5991. if (fv >= 8 && fv <= 24) {
  5992. if (exact)
  5993. root_font_px = fv;
  5994. break;
  5995. }
  5996. }
  5997. if (exact)
  5998. return;
  5999. break;
  6000. }
  6001. }
  6002. }
  6003. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  6004. {
  6005. scan_font_faces(css, len);
  6006. detect_root_font(css, len);
  6007. css_collect_only = 1;
  6008. parse_css(d, css, len);
  6009. css_collect_only = 0;
  6010. parse_css(d, css, len);
  6011. }
  6012. void br_parse_inline_css(struct br_doc *d)
  6013. {
  6014. if (d->inline_css_len) {
  6015. scan_font_faces(css_store, d->inline_css_len);
  6016. detect_root_font(css_store, d->inline_css_len);
  6017. css_collect_only = 1;
  6018. parse_css(d, css_store, d->inline_css_len);
  6019. css_collect_only = 0;
  6020. parse_css(d, css_store, d->inline_css_len);
  6021. }
  6022. d->inline_css_len = 0;
  6023. }
  6024. void br_reset_styles(struct br_doc *d)
  6025. {
  6026. unsigned int i;
  6027. for (i = 0; i < d->nnode; i++) {
  6028. d->nodes[i].style_valid = 0;
  6029. d->nodes[i].pe_state = 0;
  6030. }
  6031. }
  6032. /* ---- selector matching ---- */
  6033. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  6034. unsigned int clen)
  6035. {
  6036. const char *s = attr_ptr(d, d->nodes[node].class_off);
  6037. unsigned int i = 0;
  6038. while (s[i]) {
  6039. unsigned int st, n;
  6040. while (s[i] && is_space(s[i]))
  6041. i++;
  6042. st = i;
  6043. while (s[i] && !is_space(s[i]))
  6044. i++;
  6045. n = i - st;
  6046. if (n == clen && ci_eq_n(s + st, cls, n))
  6047. return 1;
  6048. }
  6049. return 0;
  6050. }
  6051. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  6052. unsigned int len)
  6053. {
  6054. const char *s = attr_ptr(d, d->nodes[node].id_off);
  6055. unsigned int i;
  6056. for (i = 0; i < len; i++)
  6057. if (s[i] != id[i])
  6058. return 0;
  6059. return s[len] == '\0';
  6060. }
  6061. static int is_ident(char c)
  6062. {
  6063. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  6064. (c >= '0' && c <= '9') || c == '-' || c == '_';
  6065. }
  6066. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  6067. {
  6068. unsigned int n = 0;
  6069. while (lit[n])
  6070. n++;
  6071. return n == nlen && ci_eq_n(name, lit, nlen);
  6072. }
  6073. static int attr_present(struct br_doc *d, unsigned int node,
  6074. const char *name, unsigned int nlen)
  6075. {
  6076. struct br_node *n = &d->nodes[node];
  6077. if (attr_is(name, nlen, "data-collapse-target"))
  6078. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  6079. if (attr_is(name, nlen, "data-collapse-trigger"))
  6080. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  6081. if (attr_is(name, nlen, "is-open"))
  6082. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  6083. if (attr_is(name, nlen, "disabled"))
  6084. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  6085. if (attr_is(name, nlen, "open"))
  6086. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  6087. if (attr_is(name, nlen, "inert"))
  6088. return (n->attr_flags & BR_ATTR_INERT) != 0;
  6089. if (attr_is(name, nlen, "hidden"))
  6090. return (n->flags & BR_NODE_HIDDEN) != 0;
  6091. if (attr_is(name, nlen, "href"))
  6092. return attr_ptr(d, n->href_off)[0] != 0;
  6093. if (attr_is(name, nlen, "class"))
  6094. return attr_ptr(d, n->class_off)[0] != 0;
  6095. if (attr_is(name, nlen, "id"))
  6096. return attr_ptr(d, n->id_off)[0] != 0;
  6097. if (attr_is(name, nlen, "src"))
  6098. return attr_ptr(d, n->src_off)[0] != 0;
  6099. if (attr_is(name, nlen, "alt"))
  6100. return attr_ptr(d, n->alt_off)[0] != 0;
  6101. if (attr_is(name, nlen, "rel"))
  6102. return attr_ptr(d, n->rel_off)[0] != 0;
  6103. return 0;
  6104. }
  6105. /* ---- Selector engine ----
  6106. *
  6107. * One matcher serves the cascade (rule_try), querySelector(All) and
  6108. * Element.matches. A compound is an optional type/universal selector
  6109. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  6110. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  6111. * '~' combinators and is matched right to left with backtracking.
  6112. *
  6113. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  6114. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  6115. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  6116. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  6117. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  6118. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  6119. * and pseudo-elements never match an element; the cascade strips
  6120. * ::before/::after before it gets here. */
  6121. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  6122. unsigned int len);
  6123. /* i is at '(' or '['; returns the index just past the matching close. */
  6124. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6125. unsigned int len)
  6126. {
  6127. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  6128. int depth = 0;
  6129. for (; i < len; i++) {
  6130. char c = s[i];
  6131. if (quote) {
  6132. if (c == '\\')
  6133. i++;
  6134. else if (c == quote)
  6135. quote = 0;
  6136. continue;
  6137. }
  6138. if (c == '\\')
  6139. i++;
  6140. else if (c == '"' || c == '\'')
  6141. quote = c;
  6142. else if (c == open)
  6143. depth++;
  6144. else if (c == close && --depth == 0)
  6145. return i + 1;
  6146. }
  6147. return len;
  6148. }
  6149. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  6150. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6151. char *out, unsigned int cap, unsigned int *olen)
  6152. {
  6153. unsigned int n = 0;
  6154. while (i < len) {
  6155. char c = s[i];
  6156. if (c == '\\' && i + 1 < len) {
  6157. c = s[i + 1];
  6158. i += 2;
  6159. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  6160. i++;
  6161. } else {
  6162. break;
  6163. }
  6164. if (n + 1 < cap)
  6165. out[n++] = c;
  6166. }
  6167. out[n] = '\0';
  6168. *olen = n;
  6169. return i;
  6170. }
  6171. /* Next top-level item of a comma-separated selector list. */
  6172. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6173. unsigned int *st, unsigned int *en)
  6174. {
  6175. unsigned int i = *pos;
  6176. while (i < len && (is_space(s[i]) || s[i] == ','))
  6177. i++;
  6178. if (i >= len)
  6179. return 0;
  6180. *st = i;
  6181. while (i < len && s[i] != ',') {
  6182. if (s[i] == '(' || s[i] == '[')
  6183. i = sel_skip_group(s, i, len);
  6184. else if (s[i] == '\\' && i + 1 < len)
  6185. i += 2;
  6186. else if (s[i] == '"' || s[i] == '\'') {
  6187. char q = s[i++];
  6188. while (i < len && s[i] != q)
  6189. i++;
  6190. i++;
  6191. } else
  6192. i++;
  6193. }
  6194. if (i > len)
  6195. i = len;
  6196. *en = i;
  6197. while (*en > *st && is_space(s[*en - 1]))
  6198. (*en)--;
  6199. *pos = i;
  6200. return 1;
  6201. }
  6202. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  6203. unsigned int len)
  6204. {
  6205. unsigned int pos = 0, st, en;
  6206. while (sel_list_next(s, len, &pos, &st, &en))
  6207. if (sel_match(d, node, s + st, en - st))
  6208. return 1;
  6209. return 0;
  6210. }
  6211. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  6212. {
  6213. if (d->nodes[a].tag != d->nodes[b].tag)
  6214. return 0;
  6215. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  6216. return 1;
  6217. return same_string(attr_ptr(d, d->nodes[a].name_off),
  6218. attr_ptr(d, d->nodes[b].name_off));
  6219. }
  6220. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  6221. {
  6222. unsigned int c, prev = 0;
  6223. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  6224. c = d->nodes[c].next)
  6225. if (d->nodes[c].type == BR_NODE_ELEM)
  6226. prev = c;
  6227. return c == node ? prev : 0;
  6228. }
  6229. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  6230. {
  6231. unsigned int c;
  6232. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  6233. if (d->nodes[c].type == BR_NODE_ELEM)
  6234. return c;
  6235. return 0;
  6236. }
  6237. /* 1-based position among the element siblings (of the same type), counted
  6238. * from the start or, with from_end, from the end. */
  6239. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  6240. int from_end)
  6241. {
  6242. unsigned int c;
  6243. int pos = 0, total = 0;
  6244. for (c = d->nodes[d->nodes[node].parent].first; c;
  6245. c = d->nodes[c].next) {
  6246. if (d->nodes[c].type != BR_NODE_ELEM)
  6247. continue;
  6248. if (of_type && !sel_same_type(d, c, node))
  6249. continue;
  6250. total++;
  6251. if (c == node)
  6252. pos = total;
  6253. }
  6254. if (!pos)
  6255. return 0;
  6256. return from_end ? total - pos + 1 : pos;
  6257. }
  6258. /* an+b, odd, even. "of S" is not supported (returns 0). */
  6259. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  6260. {
  6261. unsigned int i = 0;
  6262. int sign = 1, num = 0, digits = 0;
  6263. while (len && is_space(s[len - 1]))
  6264. len--;
  6265. while (i < len && is_space(s[i]))
  6266. i++;
  6267. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  6268. *a = 2;
  6269. *b = 1;
  6270. return 1;
  6271. }
  6272. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  6273. *a = 2;
  6274. *b = 0;
  6275. return 1;
  6276. }
  6277. if (i < len && (s[i] == '+' || s[i] == '-'))
  6278. sign = s[i++] == '-' ? -1 : 1;
  6279. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6280. num = num * 10 + (s[i++] - '0');
  6281. digits = 1;
  6282. }
  6283. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  6284. *a = sign * (digits ? num : 1);
  6285. *b = 0;
  6286. i++;
  6287. while (i < len && is_space(s[i]))
  6288. i++;
  6289. if (i < len && (s[i] == '+' || s[i] == '-')) {
  6290. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  6291. while (i < len && is_space(s[i]))
  6292. i++;
  6293. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6294. bv = bv * 10 + (s[i++] - '0');
  6295. bd = 1;
  6296. }
  6297. if (!bd)
  6298. return 0;
  6299. *b = bs * bv;
  6300. }
  6301. } else {
  6302. if (!digits)
  6303. return 0;
  6304. *a = 0;
  6305. *b = sign * num;
  6306. }
  6307. return i == len;
  6308. }
  6309. static int sel_nth_ok(int a, int b, int pos)
  6310. {
  6311. if (pos <= 0)
  6312. return 0;
  6313. if (!a)
  6314. return pos == b;
  6315. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  6316. }
  6317. static int sel_attr_test(struct br_doc *d, unsigned int node,
  6318. const char *name, int op, const char *want,
  6319. unsigned int wl, int icase)
  6320. {
  6321. const char *have = br_find_attr(d, node, name);
  6322. unsigned int n, i;
  6323. if (!have) {
  6324. /* Boolean attributes the parser keeps as flags. */
  6325. if (!attr_present(d, node, name, text_len(name)))
  6326. return 0;
  6327. have = "";
  6328. }
  6329. if (!op)
  6330. return 1;
  6331. n = text_len(have);
  6332. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  6333. switch (op) {
  6334. case '=':
  6335. return n == wl && SEL_EQ(have, want, wl);
  6336. case '^':
  6337. return wl && n >= wl && SEL_EQ(have, want, wl);
  6338. case '$':
  6339. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  6340. case '*':
  6341. if (!wl)
  6342. return 0;
  6343. for (i = 0; i + wl <= n; i++)
  6344. if (SEL_EQ(have + i, want, wl))
  6345. return 1;
  6346. return 0;
  6347. case '|':
  6348. return n >= wl && SEL_EQ(have, want, wl) &&
  6349. (n == wl || have[wl] == '-');
  6350. case '~':
  6351. if (!wl)
  6352. return 0;
  6353. for (i = 0; i < n; ) {
  6354. unsigned int st;
  6355. while (i < n && is_space(have[i]))
  6356. i++;
  6357. st = i;
  6358. while (i < n && !is_space(have[i]))
  6359. i++;
  6360. if (i - st == wl && SEL_EQ(have + st, want, wl))
  6361. return 1;
  6362. }
  6363. return 0;
  6364. }
  6365. #undef SEL_EQ
  6366. return 0;
  6367. }
  6368. static int sel_empty(struct br_doc *d, unsigned int node)
  6369. {
  6370. unsigned int c;
  6371. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  6372. if (d->nodes[c].type == BR_NODE_ELEM)
  6373. return 0;
  6374. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  6375. return 0;
  6376. }
  6377. return 1;
  6378. }
  6379. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  6380. unsigned int len)
  6381. {
  6382. unsigned int c;
  6383. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  6384. if (d->nodes[c].type != BR_NODE_ELEM)
  6385. continue;
  6386. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  6387. return 1;
  6388. }
  6389. return 0;
  6390. }
  6391. /* :has() with an optional leading relative combinator per list item. */
  6392. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  6393. unsigned int len)
  6394. {
  6395. unsigned int pos = 0, st, en, c;
  6396. while (sel_list_next(s, len, &pos, &st, &en)) {
  6397. char comb = s[st];
  6398. if (comb == '>' || comb == '+' || comb == '~') {
  6399. st++;
  6400. while (st < en && is_space(s[st]))
  6401. st++;
  6402. } else {
  6403. comb = ' ';
  6404. }
  6405. if (st >= en)
  6406. continue;
  6407. if (comb == '>') {
  6408. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  6409. if (d->nodes[c].type == BR_NODE_ELEM &&
  6410. sel_match(d, c, s + st, en - st))
  6411. return 1;
  6412. } else if (comb == '+') {
  6413. c = sel_next_elem(d, node);
  6414. if (c && sel_match(d, c, s + st, en - st))
  6415. return 1;
  6416. } else if (comb == '~') {
  6417. for (c = sel_next_elem(d, node); c;
  6418. c = sel_next_elem(d, c))
  6419. if (sel_match(d, c, s + st, en - st))
  6420. return 1;
  6421. } else if (sel_has_desc(d, node, s + st, en - st)) {
  6422. return 1;
  6423. }
  6424. }
  6425. return 0;
  6426. }
  6427. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  6428. {
  6429. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  6430. }
  6431. /* Pseudo-class names the engine models; anything else never matches and
  6432. * makes a top-level rule unsupported (see css_selector_supported()). */
  6433. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  6434. {
  6435. static const char *const plain[] = {
  6436. "root", "link", "any-link", "visited", "first-child",
  6437. "last-child", "only-child", "first-of-type", "last-of-type",
  6438. "only-of-type", "empty", "checked", "disabled", "enabled",
  6439. "required", "optional",
  6440. };
  6441. static const char *const func[] = {
  6442. "not", "is", "where", "matches", "-webkit-any", "has",
  6443. "nth-child", "nth-last-child", "nth-of-type",
  6444. "nth-last-of-type",
  6445. };
  6446. unsigned int k;
  6447. if (fn) {
  6448. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  6449. if (sel_name_is(name, nl, func[k]))
  6450. return 1;
  6451. return 0;
  6452. }
  6453. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  6454. if (sel_name_is(name, nl, plain[k]))
  6455. return 1;
  6456. return 0;
  6457. }
  6458. static int sel_form_control(struct br_doc *d, unsigned int node)
  6459. {
  6460. unsigned int t = d->nodes[node].tag;
  6461. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  6462. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  6463. t == BR_TAG_OPTION;
  6464. }
  6465. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  6466. unsigned int nl, const char *arg, unsigned int al,
  6467. int fn)
  6468. {
  6469. struct br_node *n = &d->nodes[node];
  6470. int a, b;
  6471. if (fn) {
  6472. if (sel_name_is(name, nl, "not"))
  6473. return !sel_list_match(d, node, arg, al);
  6474. if (sel_name_is(name, nl, "is") ||
  6475. sel_name_is(name, nl, "where") ||
  6476. sel_name_is(name, nl, "matches") ||
  6477. sel_name_is(name, nl, "-webkit-any"))
  6478. return sel_list_match(d, node, arg, al);
  6479. if (sel_name_is(name, nl, "has"))
  6480. return sel_has(d, node, arg, al);
  6481. if (!sel_nth_parse(arg, al, &a, &b))
  6482. return 0;
  6483. if (sel_name_is(name, nl, "nth-child"))
  6484. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  6485. if (sel_name_is(name, nl, "nth-last-child"))
  6486. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  6487. if (sel_name_is(name, nl, "nth-of-type"))
  6488. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  6489. if (sel_name_is(name, nl, "nth-last-of-type"))
  6490. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  6491. return 0;
  6492. }
  6493. if (sel_name_is(name, nl, "root"))
  6494. return n->tag == BR_TAG_HTML;
  6495. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  6496. /* No history state: every link is unvisited. */
  6497. return n->tag == BR_TAG_A && n->href_off;
  6498. if (sel_name_is(name, nl, "first-child"))
  6499. return sel_sibling_pos(d, node, 0, 0) == 1;
  6500. if (sel_name_is(name, nl, "last-child"))
  6501. return sel_sibling_pos(d, node, 0, 1) == 1;
  6502. if (sel_name_is(name, nl, "only-child"))
  6503. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  6504. sel_sibling_pos(d, node, 0, 1) == 1;
  6505. if (sel_name_is(name, nl, "first-of-type"))
  6506. return sel_sibling_pos(d, node, 1, 0) == 1;
  6507. if (sel_name_is(name, nl, "last-of-type"))
  6508. return sel_sibling_pos(d, node, 1, 1) == 1;
  6509. if (sel_name_is(name, nl, "only-of-type"))
  6510. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  6511. sel_sibling_pos(d, node, 1, 1) == 1;
  6512. if (sel_name_is(name, nl, "empty"))
  6513. return sel_empty(d, node);
  6514. if (sel_name_is(name, nl, "checked"))
  6515. return br_find_attr(d, node, "checked") != 0 ||
  6516. (n->tag == BR_TAG_OPTION &&
  6517. br_find_attr(d, node, "selected") != 0);
  6518. if (sel_name_is(name, nl, "disabled"))
  6519. return sel_form_control(d, node) &&
  6520. (n->attr_flags & BR_ATTR_DISABLED);
  6521. if (sel_name_is(name, nl, "enabled"))
  6522. return sel_form_control(d, node) &&
  6523. !(n->attr_flags & BR_ATTR_DISABLED);
  6524. if (sel_name_is(name, nl, "required"))
  6525. return sel_form_control(d, node) &&
  6526. br_find_attr(d, node, "required") != 0;
  6527. if (sel_name_is(name, nl, "optional"))
  6528. return sel_form_control(d, node) &&
  6529. !br_find_attr(d, node, "required");
  6530. return 0;
  6531. }
  6532. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  6533. unsigned int len)
  6534. {
  6535. char buf[64];
  6536. unsigned int i = 0, bl;
  6537. if (d->nodes[node].type != BR_NODE_ELEM)
  6538. return 0;
  6539. if (i < len && s[i] == '*') {
  6540. i++;
  6541. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  6542. unsigned int tag;
  6543. i = sel_ident(s, i, len, buf, 32, &bl);
  6544. tag = br_tag_from_name(buf);
  6545. if (tag == BR_TAG_UNKNOWN) {
  6546. const char *raw = attr_ptr(d,
  6547. d->nodes[node].name_off);
  6548. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  6549. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  6550. return 0;
  6551. } else if (tag != d->nodes[node].tag) {
  6552. return 0;
  6553. }
  6554. }
  6555. if (i >= len)
  6556. return i > 0;
  6557. while (i < len) {
  6558. char c = s[i];
  6559. if (c == '.' || c == '#') {
  6560. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  6561. if (!bl)
  6562. return 0;
  6563. if (c == '.' ? !class_has(d, node, buf, bl) :
  6564. !id_eq(d, node, buf, bl))
  6565. return 0;
  6566. } else if (c == '[') {
  6567. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  6568. unsigned int vs = 0, vl = 0, k;
  6569. char name[32];
  6570. int op = 0, icase = 0;
  6571. while (j < e && is_space(s[j]))
  6572. j++;
  6573. k = 0;
  6574. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  6575. k + 1 < sizeof(name)) {
  6576. char ch = s[j++];
  6577. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  6578. (char)(ch + 32) : ch;
  6579. }
  6580. name[k] = '\0';
  6581. if (!k)
  6582. return 0;
  6583. while (j < e && is_space(s[j]))
  6584. j++;
  6585. if (j < e && s[j] == '=') {
  6586. op = '=';
  6587. j++;
  6588. } else if (j + 1 < e && s[j + 1] == '=' &&
  6589. (s[j] == '~' || s[j] == '|' ||
  6590. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  6591. op = s[j];
  6592. j += 2;
  6593. }
  6594. if (op) {
  6595. while (j < e && is_space(s[j]))
  6596. j++;
  6597. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  6598. char q = s[j++];
  6599. vs = j;
  6600. while (j < e && s[j] != q)
  6601. j++;
  6602. vl = j - vs;
  6603. j++;
  6604. } else {
  6605. vs = j;
  6606. while (j < e && s[j] != ']' &&
  6607. !is_space(s[j]))
  6608. j++;
  6609. vl = j - vs;
  6610. }
  6611. while (j < e && is_space(s[j]))
  6612. j++;
  6613. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  6614. icase = 1;
  6615. }
  6616. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  6617. icase))
  6618. return 0;
  6619. i = e;
  6620. } else if (c == ':') {
  6621. unsigned int st, nl, as = 0, al = 0;
  6622. int fn = 0;
  6623. if (i + 1 < len && s[i + 1] == ':')
  6624. return 0; /* pseudo-element */
  6625. st = ++i;
  6626. while (i < len && is_ident(s[i]))
  6627. i++;
  6628. nl = i - st;
  6629. if (i < len && s[i] == '(') {
  6630. unsigned int e = sel_skip_group(s, i, len);
  6631. fn = 1;
  6632. as = i + 1;
  6633. al = e > as ? e - as - 1 : 0;
  6634. i = e;
  6635. }
  6636. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  6637. return 0;
  6638. } else {
  6639. return 0;
  6640. }
  6641. }
  6642. return 1;
  6643. }
  6644. /* Split a complex selector into compounds; returns the count or -1. */
  6645. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  6646. {
  6647. unsigned int i = 0;
  6648. int n = 0;
  6649. char comb = 0;
  6650. while (i < len && is_space(s[i]))
  6651. i++;
  6652. while (i < len) {
  6653. unsigned int st = i;
  6654. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  6655. s[i] != '+' && s[i] != '~') {
  6656. if (s[i] == '(' || s[i] == '[')
  6657. i = sel_skip_group(s, i, len);
  6658. else if (s[i] == '\\' && i + 1 < len)
  6659. i += 2;
  6660. else
  6661. i++;
  6662. }
  6663. if (i == st || n == SEL_PARTS)
  6664. return -1;
  6665. p[n].st = (unsigned short)st;
  6666. p[n].en = (unsigned short)(i > len ? len : i);
  6667. p[n].comb = comb;
  6668. n++;
  6669. comb = 0;
  6670. while (i < len && is_space(s[i])) {
  6671. comb = ' ';
  6672. i++;
  6673. }
  6674. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  6675. comb = s[i++];
  6676. while (i < len && is_space(s[i]))
  6677. i++;
  6678. if (i >= len)
  6679. return -1;
  6680. }
  6681. }
  6682. return n;
  6683. }
  6684. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  6685. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  6686. const struct sel_part *p, int k)
  6687. {
  6688. unsigned int c, pl;
  6689. const char *ps;
  6690. if (k == 0)
  6691. return 1;
  6692. ps = s + p[k - 1].st;
  6693. pl = p[k - 1].en - p[k - 1].st;
  6694. switch (p[k].comb) {
  6695. case '>':
  6696. c = d->nodes[node].parent;
  6697. return c && compound_match(d, c, ps, pl) &&
  6698. sel_from(d, c, s, p, k - 1);
  6699. case '+':
  6700. c = sel_prev_elem(d, node);
  6701. return c && compound_match(d, c, ps, pl) &&
  6702. sel_from(d, c, s, p, k - 1);
  6703. case '~':
  6704. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  6705. if (compound_match(d, c, ps, pl) &&
  6706. sel_from(d, c, s, p, k - 1))
  6707. return 1;
  6708. return 0;
  6709. default:
  6710. for (c = d->nodes[node].parent; c; c = d->nodes[c].parent)
  6711. if (compound_match(d, c, ps, pl) &&
  6712. sel_from(d, c, s, p, k - 1))
  6713. return 1;
  6714. return 0;
  6715. }
  6716. }
  6717. /* Match one complex selector. right_done: the caller already matched the
  6718. * rightmost compound (the rule index key). */
  6719. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  6720. unsigned int len, int right_done)
  6721. {
  6722. struct sel_part parts[SEL_PARTS];
  6723. int np = sel_split(s, len, parts);
  6724. if (np <= 0)
  6725. return 0;
  6726. if (!right_done &&
  6727. !compound_match(d, node, s + parts[np - 1].st,
  6728. parts[np - 1].en - parts[np - 1].st))
  6729. return 0;
  6730. return sel_from(d, node, s, parts, np - 1);
  6731. }
  6732. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  6733. unsigned int len)
  6734. {
  6735. return selector_match(d, node, s, len, 0);
  6736. }
  6737. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  6738. * types/pseudo-elements); :is/:not/:has take their most specific argument
  6739. * and :where counts nothing. */
  6740. static unsigned int selector_specificity(const char *s, unsigned int len)
  6741. {
  6742. unsigned int i = 0, spec = 0, bl;
  6743. char buf[64];
  6744. while (i < len) {
  6745. char c = s[i];
  6746. if (c == '#' || c == '.') {
  6747. spec += c == '#' ? 100 : 10;
  6748. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  6749. } else if (c == '[') {
  6750. spec += 10;
  6751. i = sel_skip_group(s, i, len);
  6752. } else if (c == ':') {
  6753. unsigned int st, nl;
  6754. if (i + 1 < len && s[i + 1] == ':') {
  6755. spec += 1;
  6756. i += 2;
  6757. while (i < len && is_ident(s[i]))
  6758. i++;
  6759. continue;
  6760. }
  6761. st = ++i;
  6762. while (i < len && is_ident(s[i]))
  6763. i++;
  6764. nl = i - st;
  6765. if (i < len && s[i] == '(') {
  6766. unsigned int e = sel_skip_group(s, i, len);
  6767. if (sel_name_is(s + st, nl, "where")) {
  6768. /* zero */
  6769. } else if (sel_name_is(s + st, nl, "not") ||
  6770. sel_name_is(s + st, nl, "is") ||
  6771. sel_name_is(s + st, nl, "has") ||
  6772. sel_name_is(s + st, nl, "matches") ||
  6773. sel_name_is(s + st, nl,
  6774. "-webkit-any")) {
  6775. unsigned int pos = 0, as, ae, best = 0;
  6776. unsigned int al = e > i + 1 ?
  6777. e - i - 2 : 0;
  6778. while (sel_list_next(s + i + 1, al,
  6779. &pos, &as, &ae)) {
  6780. unsigned int v =
  6781. selector_specificity(
  6782. s + i + 1 + as,
  6783. ae - as);
  6784. if (v > best)
  6785. best = v;
  6786. }
  6787. spec += best;
  6788. } else {
  6789. spec += 10;
  6790. }
  6791. i = e;
  6792. } else if (sel_name_is(s + st, nl, "before") ||
  6793. sel_name_is(s + st, nl, "after")) {
  6794. spec += 1;
  6795. } else {
  6796. spec += 10;
  6797. }
  6798. } else if (is_ident(c) || c == '\\') {
  6799. spec += 1;
  6800. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  6801. } else {
  6802. i++;
  6803. }
  6804. }
  6805. return spec;
  6806. }
  6807. /* Cascade filter: drop a selector the engine cannot match (user-state
  6808. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  6809. * ones the caller handles), so it does not waste a rule slot. Arguments of
  6810. * functional pseudo-classes are not checked: an unknown pseudo-class there
  6811. * is simply false, which is right for :not(:hover). */
  6812. static int css_selector_supported(const char *s, unsigned int len)
  6813. {
  6814. struct sel_part parts[SEL_PARTS];
  6815. unsigned int i = 0;
  6816. if (sel_split(s, len, parts) <= 0)
  6817. return 0;
  6818. while (i < len) {
  6819. if (s[i] == '(' || s[i] == '[') {
  6820. i = sel_skip_group(s, i, len);
  6821. } else if (s[i] == '\\') {
  6822. i += 2;
  6823. } else if (s[i] == ':') {
  6824. unsigned int st, nl;
  6825. int fn;
  6826. if (i + 1 < len && s[i + 1] == ':')
  6827. return 0;
  6828. st = ++i;
  6829. while (i < len && is_ident(s[i]))
  6830. i++;
  6831. nl = i - st;
  6832. fn = i < len && s[i] == '(';
  6833. if (!sel_pseudo_known(s + st, nl, fn))
  6834. return 0;
  6835. } else {
  6836. i++;
  6837. }
  6838. }
  6839. return 1;
  6840. }
  6841. #define RULE_MATCH_MAX 512
  6842. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  6843. static int cascade_pe;
  6844. struct rule_match {
  6845. unsigned short idx;
  6846. short spec;
  6847. };
  6848. static struct rule_match rule_matches[RULE_MATCH_MAX];
  6849. static unsigned int nrule_matches;
  6850. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  6851. {
  6852. struct br_rule *r = &d->rules[idx];
  6853. const char *key = attr_ptr(d, r->key_off);
  6854. int spec, key_ok = 0;
  6855. if (r->pe != cascade_pe)
  6856. return;
  6857. if (key[0]) {
  6858. if (!compound_match(d, node, key, s_len(key)))
  6859. return;
  6860. key_ok = 1;
  6861. }
  6862. if (!(r->single && key_ok) &&
  6863. !selector_match(d, node, attr_ptr(d, r->sel_off),
  6864. s_len(attr_ptr(d, r->sel_off)), key_ok))
  6865. return;
  6866. spec = (int)r->spec;
  6867. if (nrule_matches < RULE_MATCH_MAX) {
  6868. rule_matches[nrule_matches].idx = (unsigned short)idx;
  6869. rule_matches[nrule_matches].spec = (short)spec;
  6870. nrule_matches++;
  6871. }
  6872. }
  6873. static void rule_scan_list(struct br_doc *d, unsigned int node,
  6874. unsigned short head)
  6875. {
  6876. while (head != 0xFFFF) {
  6877. rule_try(d, node, head);
  6878. head = d->rule_next[head];
  6879. }
  6880. }
  6881. static void rule_scan_class(struct br_doc *d, unsigned int node,
  6882. const char *cls, unsigned int len)
  6883. {
  6884. if (len)
  6885. rule_scan_list(d, node,
  6886. d->rule_bucket[rule_hash(cls, len)]);
  6887. }
  6888. static void rule_collect(struct br_doc *d, unsigned int node)
  6889. {
  6890. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  6891. const char *id = attr_ptr(d, d->nodes[node].id_off);
  6892. unsigned int tag = d->nodes[node].tag;
  6893. unsigned int i;
  6894. nrule_matches = 0;
  6895. rule_scan_list(d, node, d->rule_any);
  6896. if (tag < BR_TAG_MAX)
  6897. rule_scan_list(d, node, d->rule_tag[tag]);
  6898. if (id[0])
  6899. rule_scan_list(d, node,
  6900. d->rule_bucket[rule_hash(id, s_len(id))]);
  6901. i = 0;
  6902. while (cls[i]) {
  6903. unsigned int st, n;
  6904. while (cls[i] && is_space(cls[i]))
  6905. i++;
  6906. st = i;
  6907. while (cls[i] && !is_space(cls[i]))
  6908. i++;
  6909. n = i - st;
  6910. if (n)
  6911. rule_scan_class(d, node, cls + st, n);
  6912. }
  6913. /* Rules must cascade in document order even though the index visits
  6914. * them in bucket order. */
  6915. for (i = 1; i < nrule_matches; i++) {
  6916. struct rule_match m = rule_matches[i];
  6917. unsigned int j = i;
  6918. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  6919. rule_matches[j] = rule_matches[j - 1];
  6920. j--;
  6921. }
  6922. rule_matches[j] = m;
  6923. }
  6924. }
  6925. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  6926. int font_px)
  6927. {
  6928. if (*kind == 4) {
  6929. *val = (unsigned short)(*val * font_px / 100);
  6930. *kind = 1;
  6931. } else if (*kind == 5) {
  6932. *val = (unsigned short)(*val * root_font_px / 100);
  6933. *kind = 1;
  6934. }
  6935. }
  6936. static void resolve_size_units(struct br_style *st)
  6937. {
  6938. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  6939. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  6940. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  6941. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  6942. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  6943. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  6944. }
  6945. static void set_border_side_w(struct br_style *st, int side, int v)
  6946. {
  6947. if (v < 0)
  6948. v = 0;
  6949. if (v > 32)
  6950. v = 32;
  6951. st->border_w[side] = (unsigned char)v;
  6952. if (st->border_w[side] && !st->border_c[side])
  6953. st->border_c[side] = st->color;
  6954. }
  6955. static void set_border_side_c(struct br_style *st, int side, int v)
  6956. {
  6957. st->border_c[side] = (unsigned short)v;
  6958. }
  6959. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  6960. {
  6961. switch (dcl->prop) {
  6962. case PROP_COLOR:
  6963. st->color = (unsigned short)dcl->val;
  6964. break;
  6965. case PROP_BG:
  6966. st->bg = (unsigned short)dcl->val;
  6967. st->has_bg = 1;
  6968. break;
  6969. case PROP_BG_IMAGE:
  6970. st->bg_image = (unsigned short)dcl->val;
  6971. break;
  6972. case PROP_BG_REPEAT:
  6973. st->bg_repeat = (unsigned char)dcl->val;
  6974. break;
  6975. case PROP_BG_POS_X_KIND:
  6976. st->bg_pos_x_kind = (unsigned char)dcl->val;
  6977. break;
  6978. case PROP_BG_POS_X:
  6979. st->bg_pos_x = (signed short)dcl->val;
  6980. break;
  6981. case PROP_BG_POS_Y_KIND:
  6982. st->bg_pos_y_kind = (unsigned char)dcl->val;
  6983. break;
  6984. case PROP_BG_POS_Y:
  6985. st->bg_pos_y = (signed short)dcl->val;
  6986. break;
  6987. case PROP_BG_SIZE_KIND:
  6988. st->bg_size_kind = (unsigned char)dcl->val;
  6989. break;
  6990. case PROP_BG_SIZE_W:
  6991. st->bg_size_w = (unsigned short)dcl->val;
  6992. break;
  6993. case PROP_BG_SIZE_H:
  6994. st->bg_size_h = (unsigned short)dcl->val;
  6995. break;
  6996. case PROP_FONT_PX:
  6997. st->font_raw = (unsigned short)dcl->val;
  6998. break;
  6999. case PROP_FONT_UNIT:
  7000. st->font_unit = (unsigned char)dcl->val;
  7001. break;
  7002. case PROP_BOLD:
  7003. st->bold = (unsigned char)(dcl->val != 0);
  7004. break;
  7005. case PROP_ITALIC:
  7006. st->italic = (unsigned char)(dcl->val != 0);
  7007. break;
  7008. case PROP_MONO:
  7009. st->mono = (unsigned char)(dcl->val != 0);
  7010. break;
  7011. case PROP_FONT_FACE:
  7012. st->font_face = (unsigned char)dcl->val;
  7013. break;
  7014. case PROP_ALIGN:
  7015. st->align = (unsigned char)dcl->val;
  7016. break;
  7017. case PROP_UNDERLINE:
  7018. st->underline = (unsigned char)(dcl->val != 0);
  7019. break;
  7020. case PROP_STRIKE:
  7021. st->strike = (unsigned char)(dcl->val != 0);
  7022. break;
  7023. case PROP_OVERLINE:
  7024. st->overline = (unsigned char)(dcl->val != 0);
  7025. break;
  7026. case PROP_DISPLAY:
  7027. st->display = (unsigned char)(dcl->val &
  7028. ~BR_DISPLAY_INLINE_FLAG);
  7029. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  7030. st->list_item = 0;
  7031. break;
  7032. case PROP_VIS_HIDDEN:
  7033. st->vis_hidden = (unsigned char)(dcl->val != 0);
  7034. break;
  7035. case PROP_OPACITY_HIDDEN:
  7036. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  7037. break;
  7038. case PROP_FILL_MODE:
  7039. st->fill_mode = (unsigned char)dcl->val;
  7040. break;
  7041. case PROP_FILL:
  7042. st->fill = (unsigned short)dcl->val;
  7043. break;
  7044. case PROP_STROKE_MODE:
  7045. st->stroke_mode = (unsigned char)dcl->val;
  7046. break;
  7047. case PROP_STROKE:
  7048. st->stroke = (unsigned short)dcl->val;
  7049. break;
  7050. case PROP_STROKE_W:
  7051. st->stroke_w = (unsigned short)dcl->val;
  7052. break;
  7053. case PROP_FILL_RULE:
  7054. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  7055. break;
  7056. case PROP_OVERFLOW_HIDDEN:
  7057. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  7058. break;
  7059. case PROP_MT: st->mt = dcl->val; break;
  7060. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  7061. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  7062. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  7063. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  7064. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  7065. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  7066. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  7067. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  7068. case PROP_MR: st->mr = dcl->val; break;
  7069. case PROP_MB: st->mb = dcl->val; break;
  7070. case PROP_ML: st->ml = dcl->val; break;
  7071. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  7072. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  7073. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  7074. break;
  7075. case PROP_PT: st->pt = dcl->val; break;
  7076. case PROP_PR: st->pr = dcl->val; break;
  7077. case PROP_PB: st->pb = dcl->val; break;
  7078. case PROP_PL: st->pl = dcl->val; break;
  7079. case PROP_MT_U:
  7080. case PROP_MR_U:
  7081. case PROP_MB_U:
  7082. case PROP_ML_U:
  7083. case PROP_PT_U:
  7084. case PROP_PR_U:
  7085. case PROP_PB_U:
  7086. case PROP_PL_U:
  7087. st->mp_unit[dcl->prop - PROP_MT_U] =
  7088. (unsigned char)dcl->val;
  7089. break;
  7090. case PROP_BORDER_W:
  7091. set_border_side_w(st, 0, dcl->val);
  7092. set_border_side_w(st, 1, dcl->val);
  7093. set_border_side_w(st, 2, dcl->val);
  7094. set_border_side_w(st, 3, dcl->val);
  7095. break;
  7096. case PROP_BORDER_COLOR:
  7097. set_border_side_c(st, 0, dcl->val);
  7098. set_border_side_c(st, 1, dcl->val);
  7099. set_border_side_c(st, 2, dcl->val);
  7100. set_border_side_c(st, 3, dcl->val);
  7101. break;
  7102. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  7103. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  7104. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  7105. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  7106. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  7107. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  7108. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  7109. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  7110. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  7111. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  7112. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  7113. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  7114. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  7115. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  7116. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  7117. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  7118. case PROP_RADIUS_TL:
  7119. case PROP_RADIUS_TR:
  7120. case PROP_RADIUS_BR:
  7121. case PROP_RADIUS_BL:
  7122. st->radius[dcl->prop - PROP_RADIUS_TL] =
  7123. (unsigned char)dcl->val;
  7124. break;
  7125. case PROP_LIST_NONE:
  7126. st->no_bullet = (unsigned char)(dcl->val != 0);
  7127. break;
  7128. case PROP_LIST_STYLE:
  7129. st->list_style = (unsigned char)dcl->val;
  7130. break;
  7131. case PROP_CONTENT:
  7132. st->content = (unsigned short)dcl->val;
  7133. break;
  7134. case PROP_ASPECT_W:
  7135. st->aspect_w = (unsigned short)dcl->val;
  7136. break;
  7137. case PROP_ASPECT_H:
  7138. st->aspect_h = (unsigned short)dcl->val;
  7139. break;
  7140. case PROP_NOWRAP:
  7141. st->nowrap = (unsigned char)(dcl->val != 0);
  7142. break;
  7143. case PROP_WIDTH:
  7144. st->width = (unsigned short)dcl->val;
  7145. break;
  7146. case PROP_WIDTH_KIND:
  7147. st->width_kind = (unsigned char)dcl->val;
  7148. break;
  7149. case PROP_WIDTH_CALC_PCT:
  7150. st->width_calc_pct = dcl->val;
  7151. break;
  7152. case PROP_WIDTH_CALC_PX:
  7153. st->width_calc_px = dcl->val;
  7154. break;
  7155. case PROP_MAXW:
  7156. st->max_width = (unsigned short)dcl->val;
  7157. break;
  7158. case PROP_MAXW_KIND:
  7159. st->max_width_kind = (unsigned char)dcl->val;
  7160. break;
  7161. case PROP_MAXW_CALC_PCT:
  7162. st->max_width_calc_pct = (signed short)dcl->val;
  7163. break;
  7164. case PROP_MAXW_CALC_PX:
  7165. st->max_width_calc_px = (signed short)dcl->val;
  7166. break;
  7167. case PROP_MINW:
  7168. st->min_width = (unsigned short)dcl->val;
  7169. break;
  7170. case PROP_MINW_KIND:
  7171. st->min_width_kind = (unsigned char)dcl->val;
  7172. break;
  7173. case PROP_MINW_CALC_PCT:
  7174. st->min_width_calc_pct = (signed short)dcl->val;
  7175. break;
  7176. case PROP_MINW_CALC_PX:
  7177. st->min_width_calc_px = (signed short)dcl->val;
  7178. break;
  7179. case PROP_HEIGHT:
  7180. st->height = (unsigned short)dcl->val;
  7181. break;
  7182. case PROP_HEIGHT_KIND:
  7183. st->height_kind = (unsigned char)dcl->val;
  7184. break;
  7185. case PROP_HEIGHT_CALC_PCT:
  7186. st->height_calc_pct = (signed short)dcl->val;
  7187. break;
  7188. case PROP_HEIGHT_CALC_PX:
  7189. st->height_calc_px = (signed short)dcl->val;
  7190. break;
  7191. case PROP_MINH:
  7192. st->min_height = (unsigned short)dcl->val;
  7193. break;
  7194. case PROP_MINH_KIND:
  7195. st->min_height_kind = (unsigned char)dcl->val;
  7196. break;
  7197. case PROP_MINH_CALC_PCT:
  7198. st->min_height_calc_pct = (signed short)dcl->val;
  7199. break;
  7200. case PROP_MINH_CALC_PX:
  7201. st->min_height_calc_px = (signed short)dcl->val;
  7202. break;
  7203. case PROP_MAXH:
  7204. st->max_height = (unsigned short)dcl->val;
  7205. break;
  7206. case PROP_MAXH_KIND:
  7207. st->max_height_kind = (unsigned char)dcl->val;
  7208. break;
  7209. case PROP_MAXH_CALC_PCT:
  7210. st->max_height_calc_pct = (signed short)dcl->val;
  7211. break;
  7212. case PROP_MAXH_CALC_PX:
  7213. st->max_height_calc_px = (signed short)dcl->val;
  7214. break;
  7215. case PROP_LH_PCT:
  7216. st->line_h_pct = (unsigned char)dcl->val;
  7217. break;
  7218. case PROP_LH_PX:
  7219. st->line_h_px = (unsigned short)dcl->val;
  7220. break;
  7221. case PROP_UPPER:
  7222. st->uppercase = (unsigned char)(dcl->val != 0);
  7223. break;
  7224. case PROP_FLOAT:
  7225. st->float_left = (unsigned char)dcl->val;
  7226. break;
  7227. case PROP_CLEAR:
  7228. st->clear = (unsigned char)dcl->val;
  7229. break;
  7230. case PROP_FLEX_GROW:
  7231. st->flex_grow = (unsigned char)dcl->val;
  7232. break;
  7233. case PROP_FLEX_COL:
  7234. st->flex_col = (unsigned char)(dcl->val != 0);
  7235. break;
  7236. case PROP_FLEX_WRAP:
  7237. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  7238. break;
  7239. case PROP_JUSTIFY:
  7240. st->justify = (unsigned char)dcl->val;
  7241. break;
  7242. case PROP_ALIGN_ITEMS:
  7243. st->align_items = (unsigned char)dcl->val;
  7244. break;
  7245. case PROP_GRID_NCOLS:
  7246. st->grid_ncols = (unsigned char)dcl->val;
  7247. break;
  7248. case PROP_GRID_GAP:
  7249. st->grid_gap = (unsigned char)dcl->val;
  7250. break;
  7251. case PROP_GRID_SLOT:
  7252. st->grid_slot = (unsigned char)dcl->val;
  7253. break;
  7254. case PROP_GRID_AUTO:
  7255. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  7256. break;
  7257. case PROP_POS:
  7258. st->pos = (unsigned char)dcl->val;
  7259. break;
  7260. case PROP_LEFT:
  7261. st->left = dcl->val;
  7262. break;
  7263. case PROP_TOP:
  7264. st->top = dcl->val;
  7265. break;
  7266. case PROP_RIGHT:
  7267. st->right = dcl->val;
  7268. break;
  7269. case PROP_BOTTOM:
  7270. st->bottom = dcl->val;
  7271. break;
  7272. case PROP_LEFT_SET:
  7273. st->left_set = (unsigned char)(dcl->val != 0);
  7274. break;
  7275. case PROP_TOP_SET:
  7276. st->top_set = (unsigned char)(dcl->val != 0);
  7277. break;
  7278. case PROP_RIGHT_SET:
  7279. st->right_set = (unsigned char)(dcl->val != 0);
  7280. break;
  7281. case PROP_BOTTOM_SET:
  7282. st->bottom_set = (unsigned char)(dcl->val != 0);
  7283. break;
  7284. case PROP_ZINDEX:
  7285. st->z_index = dcl->val;
  7286. break;
  7287. case PROP_ZSET:
  7288. st->z_set = (unsigned char)(dcl->val != 0);
  7289. break;
  7290. case PROP_TRANSFORM_X:
  7291. st->tx = dcl->val;
  7292. break;
  7293. case PROP_TRANSFORM_Y:
  7294. st->ty = dcl->val;
  7295. break;
  7296. case PROP_TRANSFORM_SET:
  7297. st->transform_set = (unsigned char)(dcl->val != 0);
  7298. break;
  7299. case PROP_TRANSFORM_PAINT:
  7300. st->transform_scale = (unsigned char)(dcl->val != 0);
  7301. break;
  7302. case PROP_SCALE_X:
  7303. st->scale_x = (signed short)dcl->val;
  7304. break;
  7305. case PROP_SCALE_Y:
  7306. st->scale_y = (signed short)dcl->val;
  7307. break;
  7308. case PROP_ROTATE:
  7309. st->rotate_x100 = (signed short)dcl->val;
  7310. break;
  7311. case PROP_ORIGIN_X_KIND:
  7312. st->origin_x_kind = (unsigned char)dcl->val;
  7313. break;
  7314. case PROP_ORIGIN_X:
  7315. st->origin_x = (signed short)dcl->val;
  7316. break;
  7317. case PROP_ORIGIN_Y_KIND:
  7318. st->origin_y_kind = (unsigned char)dcl->val;
  7319. break;
  7320. case PROP_ORIGIN_Y:
  7321. st->origin_y = (signed short)dcl->val;
  7322. break;
  7323. default:
  7324. break;
  7325. }
  7326. }
  7327. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  7328. int spec, short *best, short *best_imp)
  7329. {
  7330. unsigned char p = dcl->prop;
  7331. if (p >= PROP_COUNT)
  7332. return;
  7333. if (dcl->important) {
  7334. if (spec >= best_imp[p]) {
  7335. apply_decl(st, dcl);
  7336. best_imp[p] = (short)spec;
  7337. }
  7338. } else if (best_imp[p] < 0 && spec >= best[p]) {
  7339. apply_decl(st, dcl);
  7340. best[p] = (short)spec;
  7341. }
  7342. }
  7343. void br_style_for(struct br_doc *d, unsigned int node,
  7344. const struct br_style *parent, struct br_style *out)
  7345. {
  7346. unsigned int tag;
  7347. short best[PROP_COUNT], best_imp[PROP_COUNT];
  7348. unsigned int i;
  7349. if (!cascade_pe && d->nodes[node].style_valid) {
  7350. *out = d->nodes[node].style;
  7351. return;
  7352. }
  7353. *out = *parent;
  7354. out->has_bg = 0;
  7355. out->bg_image = 0;
  7356. out->bg_repeat = 0;
  7357. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  7358. out->bg_pos_x = out->bg_pos_y = 0;
  7359. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  7360. out->float_left = 0;
  7361. out->clear = 0;
  7362. out->list_item = 0;
  7363. out->font_unit = 0;
  7364. for (i = 0; i < 4; i++) {
  7365. out->border_w[i] = 0;
  7366. out->border_c[i] = 0;
  7367. out->border_s[i] = BR_BORDER_SOLID;
  7368. out->radius[i] = 0;
  7369. }
  7370. out->width = out->max_width = out->min_width = out->height = 0;
  7371. out->min_height = out->max_height = 0;
  7372. out->width_calc_pct = out->width_calc_px = 0;
  7373. out->height_calc_pct = out->height_calc_px = 0;
  7374. out->min_height_calc_pct = out->min_height_calc_px = 0;
  7375. out->max_height_calc_pct = out->max_height_calc_px = 0;
  7376. out->min_width_calc_pct = out->min_width_calc_px = 0;
  7377. out->max_width_calc_pct = out->max_width_calc_px = 0;
  7378. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  7379. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  7380. out->box_sizing = 0;
  7381. out->ml_auto = out->mr_auto = 0;
  7382. out->mt = out->mr = out->mb = out->ml = 0;
  7383. out->pt = out->pr = out->pb = out->pl = 0;
  7384. for (i = 0; i < 8; i++) {
  7385. out->mp_unit[i] = 0;
  7386. out->mp_calc_px[i] = 0;
  7387. }
  7388. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  7389. out->justify = out->align_items = 0;
  7390. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  7391. out->grid_auto = 0;
  7392. out->aspect_w = out->aspect_h = 0;
  7393. out->pos = out->left_set = out->top_set = 0;
  7394. out->right_set = out->bottom_set = 0;
  7395. out->left = out->top = out->right = out->bottom = 0;
  7396. out->z_index = 0;
  7397. out->z_set = 0;
  7398. out->tx = out->ty = 0;
  7399. out->transform_set = 0;
  7400. out->transform_scale = 0;
  7401. out->scale_x = out->scale_y = 100;
  7402. out->rotate_x100 = 0;
  7403. out->origin_x = out->origin_y = 50;
  7404. out->origin_x_kind = out->origin_y_kind = 1;
  7405. out->vis_hidden = out->opacity_hidden = 0;
  7406. out->inline_flex = 0;
  7407. out->overflow_hidden = 0;
  7408. out->content = 0;
  7409. /* A pseudo-element is an anonymous inline box: none of the
  7410. * element's UA defaults or presentational hints apply to it. */
  7411. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  7412. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  7413. BR_DISPLAY_INLINE;
  7414. for (i = 0; i < PROP_COUNT; i++) {
  7415. best[i] = -1;
  7416. best_imp[i] = -1;
  7417. }
  7418. if (!cascade_pe)
  7419. switch (tag) {
  7420. case BR_TAG_HEAD:
  7421. case BR_TAG_TITLE:
  7422. case BR_TAG_META:
  7423. case BR_TAG_LINK:
  7424. case BR_TAG_BASE:
  7425. case BR_TAG_SCRIPT:
  7426. case BR_TAG_STYLE:
  7427. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  7428. out->display = BR_DISPLAY_NONE;
  7429. break;
  7430. case BR_TAG_BODY:
  7431. out->mt = out->mb = out->ml = out->mr = 4;
  7432. break;
  7433. case BR_TAG_P:
  7434. out->mt = out->mb = 8;
  7435. break;
  7436. case BR_TAG_H1:
  7437. out->font_px = 32; out->bold = 1;
  7438. out->mt = 10; out->mb = 6;
  7439. break;
  7440. case BR_TAG_H2:
  7441. out->font_px = 24; out->bold = 1;
  7442. out->mt = 8; out->mb = 4;
  7443. break;
  7444. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  7445. out->bold = 1; out->mt = 6; out->mb = 3;
  7446. if (tag == BR_TAG_H3)
  7447. out->font_px = 19;
  7448. else if (tag == BR_TAG_H4)
  7449. out->font_px = 17;
  7450. else if (tag == BR_TAG_H5)
  7451. out->font_px = 15;
  7452. else
  7453. out->font_px = 13;
  7454. break;
  7455. case BR_TAG_UL: case BR_TAG_OL:
  7456. out->mt = out->mb = 6; out->pl = 20;
  7457. break;
  7458. case BR_TAG_LI:
  7459. out->mt = out->mb = 2;
  7460. out->list_item = 1;
  7461. break;
  7462. case BR_TAG_BLOCKQUOTE:
  7463. out->mt = out->mb = 6; out->ml = out->mr = 16;
  7464. break;
  7465. case BR_TAG_PRE:
  7466. out->mt = out->mb = 8; out->pl = 8;
  7467. out->has_bg = 1; out->bg = theme_panel;
  7468. out->mono = 1;
  7469. break;
  7470. case BR_TAG_CENTER:
  7471. out->align = BR_ALIGN_CENTER;
  7472. break;
  7473. case BR_TAG_A:
  7474. out->color = theme_link; out->underline = 1;
  7475. break;
  7476. case BR_TAG_B: case BR_TAG_STRONG:
  7477. out->bold = 1;
  7478. break;
  7479. case BR_TAG_I: case BR_TAG_EM:
  7480. out->italic = 1;
  7481. break;
  7482. case BR_TAG_U:
  7483. out->underline = 1;
  7484. break;
  7485. case BR_TAG_S: case BR_TAG_STRIKE:
  7486. out->strike = 1;
  7487. break;
  7488. case BR_TAG_CODE:
  7489. out->color = 0x8010;
  7490. out->mono = 1;
  7491. break;
  7492. case BR_TAG_SMALL:
  7493. out->font_px = 13;
  7494. break;
  7495. case BR_TAG_BIG:
  7496. out->font_px = 20;
  7497. break;
  7498. case BR_TAG_TD: case BR_TAG_TH:
  7499. out->pr = 12;
  7500. /* A cell establishes left-aligned content even inside a legacy
  7501. * <center> (HN's outer page wrapper); author CSS/align below
  7502. * can still override this. */
  7503. out->align = BR_ALIGN_LEFT;
  7504. break;
  7505. case BR_TAG_VIDEO:
  7506. out->display = BR_DISPLAY_BLOCK;
  7507. break;
  7508. case BR_TAG_SVG:
  7509. /* The renderer treats an SVG as one replaced box; keeping it
  7510. * block-level also gives it a usable intrinsic width inside
  7511. * flex items such as a header logo. */
  7512. out->display = BR_DISPLAY_BLOCK;
  7513. break;
  7514. default:
  7515. break;
  7516. }
  7517. /* Legacy bgcolor/align attributes (tables/cells/body) are
  7518. * presentational hints; author CSS still wins because it is applied
  7519. * below. */
  7520. if (!cascade_pe) {
  7521. unsigned short bgc;
  7522. const char *bga = br_find_attr(d, node, "bgcolor");
  7523. const char *ala = br_find_attr(d, node, "align");
  7524. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  7525. out->bg = bgc;
  7526. out->has_bg = 1;
  7527. }
  7528. if (ala) {
  7529. if (same_string(ala, "right"))
  7530. out->align = BR_ALIGN_RIGHT;
  7531. else if (same_string(ala, "center"))
  7532. out->align = BR_ALIGN_CENTER;
  7533. else if (same_string(ala, "left"))
  7534. out->align = BR_ALIGN_LEFT;
  7535. }
  7536. }
  7537. /* presentational <font color>: author CSS below still wins */
  7538. if (!cascade_pe && d->nodes[node].color_off) {
  7539. unsigned short c;
  7540. const char *v = attr_ptr(d, d->nodes[node].color_off);
  7541. if (parse_color(v, s_len(v), &c))
  7542. out->color = c;
  7543. }
  7544. /* SVG presentation attributes; CSS declarations still win below. */
  7545. if (!cascade_pe && in_svg(d, node)) {
  7546. const char *fa = br_find_attr(d, node, "fill");
  7547. const char *sa = br_find_attr(d, node, "stroke");
  7548. const char *wa = br_find_attr(d, node, "stroke-width");
  7549. const char *ra = br_find_attr(d, node, "fill-rule");
  7550. const char *ca = br_find_attr(d, node, "color");
  7551. unsigned short c;
  7552. int mode;
  7553. if (ca && parse_color(ca, s_len(ca), &c))
  7554. out->color = c;
  7555. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  7556. out->fill_mode = (unsigned char)mode;
  7557. if (mode == BR_PAINT_COLOR)
  7558. out->fill = c;
  7559. }
  7560. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  7561. out->stroke_mode = (unsigned char)mode;
  7562. if (mode == BR_PAINT_COLOR)
  7563. out->stroke = c;
  7564. }
  7565. if (wa) {
  7566. int sw = svg_len16(wa, s_len(wa));
  7567. if (sw >= 0)
  7568. out->stroke_w = (unsigned short)sw;
  7569. }
  7570. if (ra)
  7571. out->fill_evenodd = same_string(ra, "evenodd");
  7572. }
  7573. /* UA behaviour for a closed <details>: only its <summary> is shown.
  7574. * Author CSS below can still override this. */
  7575. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  7576. unsigned int p = d->nodes[node].parent;
  7577. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  7578. !br_find_attr(d, p, "open"))
  7579. out->display = BR_DISPLAY_NONE;
  7580. }
  7581. rule_collect(d, node);
  7582. for (i = 0; i < nrule_matches; i++) {
  7583. struct br_rule *r = &d->rules[rule_matches[i].idx];
  7584. int spec = rule_matches[i].spec;
  7585. unsigned int k;
  7586. for (k = 0; k < r->nd; k++)
  7587. cascade_decl(out, &r->d[k], spec, best, best_imp);
  7588. }
  7589. if (!cascade_pe && d->nodes[node].style_off) {
  7590. struct br_decl decls[BR_DECLS];
  7591. unsigned int nd, k;
  7592. const char *v = attr_ptr(d, d->nodes[node].style_off);
  7593. parse_decls(d, v, s_len(v), decls, &nd);
  7594. for (k = 0; k < nd; k++)
  7595. cascade_decl(out, &decls[k], 1000, best, best_imp);
  7596. }
  7597. if (out->font_unit) {
  7598. int px = out->font_raw;
  7599. if (out->font_unit == 2)
  7600. px = parent->font_px * out->font_raw / 100;
  7601. else if (out->font_unit == 3)
  7602. px = root_font_px * out->font_raw / 100;
  7603. else if (out->font_unit == 4)
  7604. px = parent->font_px * out->font_raw / 10000;
  7605. if (px < 8)
  7606. px = 8;
  7607. if (px > 72)
  7608. px = 72;
  7609. out->font_px = (unsigned char)px;
  7610. }
  7611. resolve_size_units(out);
  7612. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  7613. out->display = BR_DISPLAY_NONE;
  7614. if (out->vis_hidden || out->opacity_hidden ||
  7615. (out->overflow_hidden && out->height_kind == 1 &&
  7616. out->height == 0))
  7617. out->display = BR_DISPLAY_NONE;
  7618. if (cascade_pe)
  7619. return;
  7620. d->nodes[node].style = *out;
  7621. d->nodes[node].style_valid = 1;
  7622. }
  7623. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  7624. const struct br_style *elem, struct br_style *out,
  7625. char *buf, unsigned int cap)
  7626. {
  7627. struct br_node *n;
  7628. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  7629. const char *c;
  7630. unsigned int len = 0;
  7631. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  7632. (pe != 1 && pe != 2))
  7633. return 0;
  7634. n = &d->nodes[node];
  7635. if (n->type != BR_NODE_ELEM ||
  7636. ((n->pe_state & chk) && !(n->pe_state & has)))
  7637. return 0;
  7638. cascade_pe = pe;
  7639. br_style_for(d, node, elem, out);
  7640. cascade_pe = 0;
  7641. if (out->content && out->display != BR_DISPLAY_NONE &&
  7642. out->pos != 1 && out->pos != 2) {
  7643. c = attr_ptr(d, d->content_off[out->content - 1]);
  7644. while (*c && len + 1 < cap) {
  7645. if (*c == '\001') {
  7646. char name[32];
  7647. unsigned int k = 0;
  7648. const char *v;
  7649. c++;
  7650. while (*c && *c != '\002' && k + 1 < sizeof(name))
  7651. name[k++] = *c++;
  7652. name[k] = '\0';
  7653. if (*c == '\002')
  7654. c++;
  7655. v = br_find_attr(d, node, name);
  7656. while (v && *v && len + 1 < cap)
  7657. buf[len++] = *v++;
  7658. continue;
  7659. }
  7660. buf[len++] = *c++;
  7661. }
  7662. }
  7663. buf[len] = '\0';
  7664. n->pe_state |= chk;
  7665. if (len)
  7666. n->pe_state |= has;
  7667. return len;
  7668. }
  7669. /* ---- HTML parser ---- */
  7670. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  7671. unsigned int sp, int *pre)
  7672. {
  7673. unsigned int i;
  7674. *pre = 0;
  7675. for (i = 1; i <= sp; i++)
  7676. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  7677. (*pre)++;
  7678. }
  7679. static void implicit_close(struct br_doc *d, unsigned short *stack,
  7680. unsigned int *sp, unsigned int tag)
  7681. {
  7682. while (*sp > 0) {
  7683. unsigned int top = d->nodes[stack[*sp]].tag;
  7684. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  7685. (*sp)--;
  7686. continue;
  7687. }
  7688. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  7689. (*sp)--;
  7690. continue;
  7691. }
  7692. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  7693. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  7694. (*sp)--;
  7695. continue;
  7696. }
  7697. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  7698. (*sp)--;
  7699. continue;
  7700. }
  7701. break;
  7702. }
  7703. }
  7704. static int has_attr_token(const char *s, unsigned int len, const char *name)
  7705. {
  7706. unsigned int i, n = s_len(name);
  7707. for (i = 0; i + n <= len; i++) {
  7708. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  7709. continue;
  7710. if (!ci_eq_n(s + i, name, n))
  7711. continue;
  7712. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  7713. return 1;
  7714. }
  7715. return 0;
  7716. }
  7717. static int stack_has_svg(const struct br_doc *d,
  7718. const unsigned short *stack, unsigned int sp)
  7719. {
  7720. unsigned int k;
  7721. for (k = 0; k <= sp && k < 64; k++)
  7722. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  7723. return 1;
  7724. return 0;
  7725. }
  7726. /* Skip an element subtree by finding its closing tag. Used for SVG
  7727. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  7728. * overwrite the document title. */
  7729. static unsigned int skip_element(const char *html, unsigned int len,
  7730. unsigned int i, const char *name,
  7731. unsigned int n)
  7732. {
  7733. char close[40];
  7734. unsigned int k = 0, p;
  7735. close[k++] = '<';
  7736. close[k++] = '/';
  7737. while (k < sizeof(close) - 1 && n--)
  7738. close[k++] = *name++;
  7739. p = (unsigned int)find_ci(html, len, close, k, i);
  7740. if ((int)p < 0)
  7741. return len;
  7742. p += k;
  7743. while (p < len && html[p] != '>')
  7744. p++;
  7745. if (p < len)
  7746. p++;
  7747. return p;
  7748. }
  7749. /* Store a <script src=...> value in the attribute arena. Kept separate from
  7750. * parse_attrs() so script-only attributes cannot be confused with the
  7751. * value/d attributes that also use src_off on other elements. */
  7752. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  7753. unsigned int len)
  7754. {
  7755. unsigned int i = 0;
  7756. while (i < len) {
  7757. unsigned int ns, ne, vs, ve;
  7758. char quote = 0;
  7759. while (i < len && is_space(s[i]))
  7760. i++;
  7761. if (i >= len || s[i] == '/')
  7762. break;
  7763. ns = i;
  7764. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  7765. i++;
  7766. ne = i;
  7767. while (i < len && is_space(s[i]))
  7768. i++;
  7769. if (i >= len || s[i] != '=')
  7770. continue;
  7771. i++;
  7772. while (i < len && is_space(s[i]))
  7773. i++;
  7774. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  7775. quote = s[i++];
  7776. vs = i;
  7777. while (i < len && s[i] != quote)
  7778. i++;
  7779. ve = i;
  7780. if (i < len)
  7781. i++;
  7782. } else {
  7783. vs = i;
  7784. while (i < len && !is_space(s[i]) && s[i] != '>')
  7785. i++;
  7786. ve = i;
  7787. }
  7788. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  7789. return store_attr(d, s + vs, ve - vs, 400);
  7790. }
  7791. return 0;
  7792. }
  7793. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  7794. * application/json, text/template, ...) and non-JS type values. External
  7795. * src scripts are allowed; the browser fetches them separately. Returns 1
  7796. * for a classic script, 2 for type="module", 0 to skip (also for a
  7797. * `nomodule` fallback, since module scripts run). */
  7798. static int script_is_javascript(const char *s, unsigned int len)
  7799. {
  7800. int kind = 1;
  7801. unsigned int i = 0;
  7802. while (i < len) {
  7803. unsigned int ns, ne, vs = 0, ve = 0;
  7804. char quote = 0;
  7805. int has_val = 0;
  7806. while (i < len && is_space(s[i]))
  7807. i++;
  7808. if (i >= len)
  7809. break;
  7810. ns = i;
  7811. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  7812. i++;
  7813. ne = i;
  7814. while (i < len && is_space(s[i]))
  7815. i++;
  7816. if (i < len && s[i] == '=') {
  7817. i++;
  7818. while (i < len && is_space(s[i]))
  7819. i++;
  7820. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  7821. quote = s[i++];
  7822. vs = i;
  7823. while (i < len && s[i] != quote)
  7824. i++;
  7825. ve = i;
  7826. if (i < len)
  7827. i++;
  7828. } else {
  7829. vs = i;
  7830. while (i < len && !is_space(s[i]) && s[i] != '/')
  7831. i++;
  7832. ve = i;
  7833. }
  7834. has_val = 1;
  7835. } else if (i == ne) {
  7836. i++;
  7837. }
  7838. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  7839. return 0;
  7840. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  7841. unsigned int vn = has_val ? ve - vs : 0;
  7842. if (vn == 0)
  7843. continue;
  7844. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  7845. kind = 2;
  7846. continue;
  7847. }
  7848. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  7849. (vn == 22 &&
  7850. ci_eq_n(s + vs, "application/javascript", 22)) ||
  7851. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  7852. (vn == 22 &&
  7853. ci_eq_n(s + vs, "application/ecmascript", 22)))
  7854. continue;
  7855. return 0;
  7856. }
  7857. }
  7858. return kind;
  7859. }
  7860. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  7861. {
  7862. unsigned short stack[64];
  7863. unsigned int sp = 0;
  7864. unsigned int i = 0;
  7865. int pre = 0;
  7866. unsigned int css_len = 0;
  7867. if (!d)
  7868. return -1;
  7869. if (!d->nodes)
  7870. br_doc_init(d);
  7871. if (!d->nodes || !d->text || !d->attrs)
  7872. return -1;
  7873. doc_reset(d);
  7874. d->script_src = html;
  7875. stack[0] = 0;
  7876. d->nodes[0].type = BR_NODE_ELEM;
  7877. d->nodes[0].tag = BR_TAG_UNKNOWN;
  7878. while (i < len) {
  7879. if (html[i] != '<') {
  7880. unsigned int st = i;
  7881. while (i < len && html[i] != '<')
  7882. i++;
  7883. if (sp <= 63)
  7884. append_text(d, stack[sp], html + st, i - st, pre);
  7885. continue;
  7886. }
  7887. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  7888. html[i + 3] == '-') {
  7889. int p = find_ci(html, len, "-->", 3, i + 4);
  7890. i = p >= 0 ? (unsigned int)p + 3 : len;
  7891. continue;
  7892. }
  7893. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  7894. while (i < len && html[i] != '>')
  7895. i++;
  7896. if (i < len)
  7897. i++;
  7898. continue;
  7899. }
  7900. if (i + 1 < len && html[i + 1] == '/') {
  7901. unsigned int ns = i + 2, ne, tag, k, n;
  7902. char name[32];
  7903. while (ns < len && is_space(html[ns]))
  7904. ns++;
  7905. ne = ns;
  7906. while (ne < len && is_ident(html[ne]))
  7907. ne++;
  7908. if (ne == ns) {
  7909. i++;
  7910. continue;
  7911. }
  7912. n = ne - ns;
  7913. if (n >= sizeof(name))
  7914. n = sizeof(name) - 1;
  7915. for (k = 0; k < n; k++)
  7916. name[k] = html[ns + k];
  7917. name[n] = '\0';
  7918. tag = br_tag_from_name(name);
  7919. i = ne;
  7920. while (i < len && html[i] != '>')
  7921. i++;
  7922. if (i < len)
  7923. i++;
  7924. for (k = sp; k > 0; k--) {
  7925. struct br_node *nd = &d->nodes[stack[k]];
  7926. if (tag == BR_TAG_UNKNOWN) {
  7927. const char *raw = attr_ptr(d, nd->name_off);
  7928. if (nd->tag == BR_TAG_UNKNOWN &&
  7929. ci_eq_n(raw, name, n) &&
  7930. raw[n] == '\0') {
  7931. sp = k - 1;
  7932. break;
  7933. }
  7934. } else if (nd->tag == tag) {
  7935. sp = k - 1;
  7936. break;
  7937. }
  7938. }
  7939. stack_recount_pre(d, stack, sp, &pre);
  7940. continue;
  7941. }
  7942. /* start tag */
  7943. {
  7944. unsigned int ns = i + 1, ne, tag, k, n;
  7945. char name[16];
  7946. unsigned int attr_start, attr_end;
  7947. int selfclose = 0;
  7948. while (ns < len && is_space(html[ns]))
  7949. ns++;
  7950. ne = ns;
  7951. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  7952. ne++;
  7953. if (ne == ns) {
  7954. /* stray '<' */
  7955. append_text(d, stack[sp], html + i, 1, pre);
  7956. i++;
  7957. continue;
  7958. }
  7959. n = ne - ns;
  7960. if (n >= sizeof(name))
  7961. n = sizeof(name) - 1;
  7962. for (k = 0; k < n; k++)
  7963. name[k] = html[ns + k];
  7964. name[n] = '\0';
  7965. tag = br_tag_from_name(name);
  7966. attr_start = ne;
  7967. {
  7968. unsigned int q = ne;
  7969. char quote = 0;
  7970. while (q < len) {
  7971. char c = html[q];
  7972. if (quote) {
  7973. if (c == quote)
  7974. quote = 0;
  7975. } else if (c == '"' || c == '\'') {
  7976. quote = c;
  7977. } else if (c == '>') {
  7978. break;
  7979. }
  7980. q++;
  7981. }
  7982. attr_end = q;
  7983. if (q > attr_start && html[q - 1] == '/')
  7984. selfclose = 1;
  7985. i = q < len ? q + 1 : len;
  7986. }
  7987. if (tag == BR_TAG_SCRIPT) {
  7988. int p = find_ci(html, len, "</script", 8, i);
  7989. unsigned int end = p >= 0 ? (unsigned int)p : len;
  7990. unsigned int idx;
  7991. struct br_script *script = 0;
  7992. int kind = script_is_javascript(html + attr_start,
  7993. attr_end - attr_start);
  7994. if (kind) {
  7995. if (d->nscripts < BR_MAX_SCRIPTS) {
  7996. script = &d->scripts[d->nscripts++];
  7997. script->module = kind == 2;
  7998. script->node = 0;
  7999. script->off = i;
  8000. script->len = end > i ? end - i : 0;
  8001. script->src_off = script_src_attr(
  8002. d, html + attr_start,
  8003. attr_end - attr_start);
  8004. } else {
  8005. d->scripts_truncated = 1;
  8006. }
  8007. }
  8008. /* Keep the element in the DOM even though its body is
  8009. * executed separately: scripts (Piwik, analytics)
  8010. * insert a sibling after getElementsByTagName('script')
  8011. * [0], and the element must exist for that to work. */
  8012. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8013. (unsigned char)tag);
  8014. if (idx)
  8015. parse_attrs(d, &d->nodes[idx],
  8016. html + attr_start,
  8017. attr_end - attr_start);
  8018. if (script)
  8019. script->node = idx;
  8020. if (idx && !kind && end > i)
  8021. append_raw_text(d, idx, html + i, end - i);
  8022. i = p >= 0 ? (unsigned int)p + 8 : len;
  8023. while (i < len && html[i] != '>')
  8024. i++;
  8025. if (i < len)
  8026. i++;
  8027. continue;
  8028. }
  8029. if (stack_has_svg(d, stack, sp) &&
  8030. (tag == BR_TAG_TITLE ||
  8031. (tag == BR_TAG_UNKNOWN &&
  8032. (ci_eq_n(name, "desc", 4) ||
  8033. ci_eq_n(name, "metadata", 8))))) {
  8034. i = skip_element(html, len, i, name, n);
  8035. continue;
  8036. }
  8037. /* A closed <details> is collapsed at style time, not
  8038. * parse time: the <summary> (the hamburger button on
  8039. * tagesschau and many mobile menus) must stay in the
  8040. * DOM. */
  8041. if (tag == BR_TAG_STYLE) {
  8042. int p = find_ci(html, len, "</style", 7, i);
  8043. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8044. unsigned int cend = end;
  8045. unsigned int idx;
  8046. while (cend > i && is_space(html[cend - 1]))
  8047. cend--;
  8048. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  8049. for (k = i; k < cend; k++)
  8050. css_store[css_len++] = html[k];
  8051. css_store[css_len++] = '\n';
  8052. }
  8053. /* The CSS is collected above, but keep a display:none
  8054. * element so getElementsByTagName('style')/parentNode
  8055. * behave like a real document. */
  8056. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8057. (unsigned char)tag);
  8058. if (idx)
  8059. parse_attrs(d, &d->nodes[idx],
  8060. html + attr_start,
  8061. attr_end - attr_start);
  8062. i = p >= 0 ? (unsigned int)p + 7 : len;
  8063. while (i < len && html[i] != '>')
  8064. i++;
  8065. if (i < len)
  8066. i++;
  8067. continue;
  8068. }
  8069. if (tag == BR_TAG_TITLE) {
  8070. int p = find_ci(html, len, "</title", 7, i);
  8071. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8072. unsigned int tn = 0;
  8073. char tmp[BR_TITLE_MAX];
  8074. tn = decode_text(html + i, end - i, tmp,
  8075. sizeof(tmp), 1);
  8076. (void)tn;
  8077. s_copy(d->title, tmp, sizeof(d->title));
  8078. i = p >= 0 ? (unsigned int)p + 7 : len;
  8079. while (i < len && html[i] != '>')
  8080. i++;
  8081. if (i < len)
  8082. i++;
  8083. continue;
  8084. }
  8085. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  8086. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  8087. unsigned int idx;
  8088. implicit_close(d, stack, &sp, tag);
  8089. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8090. (unsigned char)tag);
  8091. if (idx)
  8092. parse_attrs(d, &d->nodes[idx],
  8093. html + attr_start,
  8094. attr_end - attr_start);
  8095. continue;
  8096. }
  8097. if (sp >= 63) {
  8098. d->truncated = 1;
  8099. continue;
  8100. }
  8101. implicit_close(d, stack, &sp, tag);
  8102. {
  8103. unsigned int idx = node_append(d, stack[sp],
  8104. BR_NODE_ELEM,
  8105. (unsigned char)tag);
  8106. if (!idx)
  8107. continue;
  8108. parse_attrs(d, &d->nodes[idx],
  8109. html + attr_start,
  8110. attr_end - attr_start);
  8111. if (tag == BR_TAG_UNKNOWN)
  8112. d->nodes[idx].name_off =
  8113. store_attr(d, name, n, 31);
  8114. if (!selfclose) {
  8115. sp++;
  8116. stack[sp] = (unsigned short)idx;
  8117. if (tag == BR_TAG_PRE)
  8118. pre++;
  8119. }
  8120. }
  8121. }
  8122. }
  8123. d->inline_css_len = css_len;
  8124. return 0;
  8125. }
  8126. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  8127. {
  8128. if (!d->nodes)
  8129. br_doc_init(d);
  8130. if (!d->nodes || !d->text || !d->attrs)
  8131. return -1;
  8132. doc_reset(d);
  8133. d->plain = 1;
  8134. d->nodes[0].type = BR_NODE_ELEM;
  8135. d->nodes[0].tag = BR_TAG_UNKNOWN;
  8136. append_text(d, 0, text, len, 1);
  8137. return 0;
  8138. }
  8139. /* A standalone image response: a synthetic document with one <img>. The
  8140. * caller decodes the already-fetched body into image slot 0 and sets the
  8141. * returned node's img_index. */
  8142. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  8143. {
  8144. unsigned int idx;
  8145. if (!d->nodes)
  8146. br_doc_init(d);
  8147. if (!d->nodes || !d->text || !d->attrs)
  8148. return 0;
  8149. doc_reset(d);
  8150. d->nodes[0].type = BR_NODE_ELEM;
  8151. d->nodes[0].tag = BR_TAG_UNKNOWN;
  8152. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  8153. if (!idx)
  8154. return 0;
  8155. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  8156. return idx;
  8157. }