browser_html.c 223 KB

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