browser_html.c 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721
  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 <string.h>
  6. static int is_space(char c);
  7. static int is_ident(char c);
  8. /* ---- storage (one document per process) ---- */
  9. static struct br_node nodes_store[BR_MAX_NODES];
  10. static char text_store[BR_TEXT_SIZE];
  11. static char attr_store[BR_ATTR_SIZE];
  12. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  13. static char css_store[BR_CSS_SIZE];
  14. static int root_font_px = 16;
  15. #define BR_BG_URLS 16
  16. static char bg_urls[BR_BG_URLS][192];
  17. static unsigned int bg_url_count;
  18. /* @font-face definitions, one document at a time. The URL is resolved by
  19. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  20. struct br_font_def {
  21. char family[BR_FACE_FAMILY];
  22. char url[BR_FACE_URL];
  23. char base[192];
  24. unsigned char used;
  25. };
  26. static struct br_font_def font_defs[BR_MAX_FACES];
  27. static int font_face_count;
  28. static char font_base[192];
  29. static void font_reset(void)
  30. {
  31. memset(font_defs, 0, sizeof(font_defs));
  32. font_face_count = 0;
  33. font_base[0] = '\0';
  34. }
  35. void br_set_font_base(const char *base)
  36. {
  37. unsigned int i = 0;
  38. if (!base)
  39. base = "";
  40. while (base[i] && i + 1 < sizeof(font_base)) {
  41. font_base[i] = base[i];
  42. i++;
  43. }
  44. font_base[i] = '\0';
  45. }
  46. int br_font_face_count(void)
  47. {
  48. return font_face_count;
  49. }
  50. const char *br_font_face_url(int id)
  51. {
  52. if (id < 1 || id > font_face_count)
  53. return "";
  54. return font_defs[id - 1].url;
  55. }
  56. const char *br_font_face_base(int id)
  57. {
  58. if (id < 1 || id > font_face_count)
  59. return "";
  60. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  61. font_base;
  62. }
  63. const char *br_font_face_family(int id)
  64. {
  65. if (id < 1 || id > font_face_count)
  66. return "";
  67. return font_defs[id - 1].family;
  68. }
  69. const char *br_bg_image_url(unsigned int idx)
  70. {
  71. if (idx >= bg_url_count)
  72. return "";
  73. return bg_urls[idx];
  74. }
  75. unsigned int br_root_font_px(void)
  76. {
  77. return (unsigned int)root_font_px;
  78. }
  79. /* ---- tag table ---- */
  80. struct tag_name {
  81. const char *name;
  82. unsigned char tag;
  83. };
  84. static const struct tag_name tag_names[] = {
  85. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  86. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  87. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  88. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  89. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  90. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  91. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  92. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  93. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  94. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  95. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  96. { "picture", BR_TAG_PICTURE },
  97. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  98. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  99. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  100. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  101. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  102. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  103. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  104. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  105. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  106. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  107. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  108. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  109. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  110. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  111. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  112. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  113. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  114. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  115. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  116. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  117. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  118. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  119. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  120. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  121. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  122. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  123. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  124. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  125. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  126. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  127. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  128. { 0, 0 }
  129. };
  130. const char *br_tag_name(unsigned int tag)
  131. {
  132. unsigned int i;
  133. for (i = 0; tag_names[i].name; i++)
  134. if (tag_names[i].tag == tag)
  135. return tag_names[i].name;
  136. return "?";
  137. }
  138. static unsigned int s_len(const char *s);
  139. #define TAG_HASH_SIZE 128
  140. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  141. static int tag_hash_ready;
  142. static unsigned int tag_hash_key(const char *name, unsigned int len)
  143. {
  144. unsigned int i, h = 2166136261u;
  145. for (i = 0; i < len; i++) {
  146. h ^= (unsigned char)name[i];
  147. h *= 16777619u;
  148. }
  149. return h & (TAG_HASH_SIZE - 1);
  150. }
  151. static void tag_hash_build(void)
  152. {
  153. unsigned int i;
  154. for (i = 0; i < TAG_HASH_SIZE; i++)
  155. tag_hash_idx[i] = 0xFF;
  156. for (i = 0; tag_names[i].name; i++) {
  157. unsigned int h = tag_hash_key(tag_names[i].name,
  158. s_len(tag_names[i].name));
  159. while (tag_hash_idx[h] != 0xFF)
  160. h = (h + 1) & (TAG_HASH_SIZE - 1);
  161. tag_hash_idx[h] = (unsigned char)i;
  162. }
  163. tag_hash_ready = 1;
  164. }
  165. unsigned int br_tag_from_name(const char *name)
  166. {
  167. char lower[16];
  168. unsigned int i, len, h;
  169. unsigned char idx;
  170. for (i = 0; i < 15 && name[i]; i++) {
  171. char c = name[i];
  172. if (c >= 'A' && c <= 'Z')
  173. c = (char)(c - 'A' + 'a');
  174. lower[i] = c;
  175. }
  176. lower[i] = '\0';
  177. len = i;
  178. if (!tag_hash_ready)
  179. tag_hash_build();
  180. h = tag_hash_key(lower, len);
  181. for (;;) {
  182. idx = tag_hash_idx[h];
  183. if (idx == 0xFF)
  184. return BR_TAG_UNKNOWN;
  185. {
  186. const char *n = tag_names[idx].name;
  187. unsigned int j;
  188. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  189. ;
  190. if (!lower[j] && !n[j])
  191. return tag_names[idx].tag;
  192. }
  193. h = (h + 1) & (TAG_HASH_SIZE - 1);
  194. }
  195. }
  196. int br_tag_is_void(unsigned int tag)
  197. {
  198. switch (tag) {
  199. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  200. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  201. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  202. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  203. case BR_TAG_AREA: case BR_TAG_COL:
  204. return 1;
  205. default:
  206. return 0;
  207. }
  208. }
  209. int br_tag_is_block(unsigned int tag)
  210. {
  211. switch (tag) {
  212. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  213. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  214. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  215. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  216. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  217. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  218. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  219. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  220. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  221. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  222. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  223. case BR_TAG_PICTURE:
  224. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  225. case BR_TAG_ADDRESS: case BR_TAG_HR:
  226. return 1;
  227. default:
  228. return 0;
  229. }
  230. }
  231. /* ---- string / entity helpers ---- */
  232. static unsigned int s_len(const char *s)
  233. {
  234. unsigned int n = 0;
  235. while (s && s[n])
  236. n++;
  237. return n;
  238. }
  239. static void s_copy(char *d, const char *s, unsigned int cap)
  240. {
  241. unsigned int i = 0;
  242. if (!cap)
  243. return;
  244. while (s && s[i] && i + 1 < cap) {
  245. d[i] = s[i];
  246. i++;
  247. }
  248. d[i] = '\0';
  249. }
  250. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  251. {
  252. unsigned int i;
  253. for (i = 0; i < n; i++) {
  254. char ca = a[i], cb = b[i];
  255. if (ca >= 'A' && ca <= 'Z')
  256. ca = (char)(ca - 'A' + 'a');
  257. if (cb >= 'A' && cb <= 'Z')
  258. cb = (char)(cb - 'A' + 'a');
  259. if (ca != cb)
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. static int find_ci(const char *hay, unsigned int len, const char *needle,
  265. unsigned int nlen, unsigned int from)
  266. {
  267. unsigned int i;
  268. if (nlen == 0 || from >= len)
  269. return -1;
  270. for (i = from; i + nlen <= len; i++)
  271. if (ci_eq_n(hay + i, needle, nlen))
  272. return (int)i;
  273. return -1;
  274. }
  275. static const struct {
  276. const char *name;
  277. const char *text;
  278. } entities[] = {
  279. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  280. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  281. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  282. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  283. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  284. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  285. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  286. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  287. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  288. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  289. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  290. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  291. { 0, 0 }
  292. };
  293. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  294. {
  295. if (*n + 1 < cap)
  296. out[(*n)++] = c;
  297. }
  298. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  299. char *out, unsigned int cap, unsigned int *n)
  300. {
  301. unsigned int start = *pos + 1, i = start;
  302. unsigned int code = 0;
  303. int ok = 0;
  304. while (i < len && s[i] != ';' && i - start < 12)
  305. i++;
  306. if (i >= len || s[i] != ';') {
  307. /* Not an entity: emit the '&' and advance past it. Leaving
  308. * *pos unchanged here used to spin forever on bare '&'. */
  309. emit(out, cap, n, '&');
  310. *pos = start;
  311. return;
  312. }
  313. if (s[start] == '#') {
  314. unsigned int j = start + 1;
  315. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  316. j++;
  317. for (; j < i; j++) {
  318. char c = s[j];
  319. int d = -1;
  320. if (c >= '0' && c <= '9') d = c - '0';
  321. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  322. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  323. if (d < 0) break;
  324. code = code * 16 + (unsigned int)d;
  325. ok = 1;
  326. }
  327. } else {
  328. for (; j < i; j++) {
  329. if (s[j] < '0' || s[j] > '9')
  330. break;
  331. code = code * 10 + (unsigned int)(s[j] - '0');
  332. ok = 1;
  333. }
  334. }
  335. } else {
  336. unsigned int k;
  337. for (k = 0; entities[k].name; k++) {
  338. unsigned int el = s_len(entities[k].name);
  339. unsigned int j;
  340. if (el != i - start)
  341. continue;
  342. for (j = 0; j < el; j++)
  343. if (s[start + j] != entities[k].name[j])
  344. break;
  345. if (j == el) {
  346. const char *t = entities[k].text;
  347. while (*t)
  348. emit(out, cap, n, *t++);
  349. *pos = i + 1;
  350. return;
  351. }
  352. }
  353. }
  354. if (ok && code) {
  355. if (code == 0x00A0)
  356. emit(out, cap, n, ' ');
  357. else if (code == 0x2013 || code == 0x2014)
  358. emit(out, cap, n, '-');
  359. else if (code == 0x2018 || code == 0x2019)
  360. emit(out, cap, n, '\'');
  361. else if (code == 0x201C || code == 0x201D)
  362. emit(out, cap, n, '"');
  363. else if (code == 0x2026) {
  364. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  365. emit(out, cap, n, '.');
  366. } else if (code <= 0xFF) {
  367. emit(out, cap, n, (char)code);
  368. } else {
  369. emit(out, cap, n, '?');
  370. }
  371. } else {
  372. emit(out, cap, n, '?');
  373. }
  374. *pos = i + 1;
  375. }
  376. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  377. * space and drops leading spaces. Returns the output length. */
  378. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  379. unsigned int cap, int collapse)
  380. {
  381. unsigned int i = 0, n = 0;
  382. while (i < len) {
  383. unsigned char c = (unsigned char)s[i];
  384. if (c == '&') {
  385. decode_entity(s, len, &i, out, cap, &n);
  386. continue;
  387. }
  388. if (c < 0x80) {
  389. i++;
  390. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  391. c == '\r' || c == '\f')) {
  392. if (n > 0 && out[n - 1] == ' ')
  393. continue;
  394. emit(out, cap, &n, ' ');
  395. continue;
  396. }
  397. if (!collapse && c == '\t') {
  398. emit(out, cap, &n, ' ');
  399. emit(out, cap, &n, ' ');
  400. emit(out, cap, &n, ' ');
  401. emit(out, cap, &n, ' ');
  402. continue;
  403. }
  404. emit(out, cap, &n, (char)c);
  405. continue;
  406. }
  407. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  408. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  409. unsigned int cp = ((c & 0x1F) << 6) |
  410. ((unsigned char)s[i + 1] & 0x3F);
  411. i += 2;
  412. if (cp == 0xA0)
  413. cp = ' ';
  414. else if (cp == 0xB7)
  415. cp = '-';
  416. else if (cp == 0xD7)
  417. cp = 'x';
  418. else if (cp == 0xB0)
  419. cp = 'o';
  420. if (cp <= 0xFF)
  421. emit(out, cap, &n, (char)cp);
  422. else
  423. emit(out, cap, &n, '?');
  424. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  425. unsigned int cp = ((c & 0x0F) << 12) |
  426. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  427. ((unsigned char)s[i + 2] & 0x3F);
  428. i += 3;
  429. if (cp == 0x2013 || cp == 0x2014) {
  430. emit(out, cap, &n, '-');
  431. } else if (cp == 0x2018 || cp == 0x2019) {
  432. emit(out, cap, &n, '\'');
  433. } else if (cp == 0x201C || cp == 0x201D) {
  434. emit(out, cap, &n, '"');
  435. } else if (cp == 0x2026) {
  436. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  437. emit(out, cap, &n, '.');
  438. } else if (cp == 0x2192) {
  439. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  440. } else if (cp == 0x2264) {
  441. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  442. } else {
  443. emit(out, cap, &n, '?');
  444. }
  445. } else {
  446. i++;
  447. emit(out, cap, &n, '?');
  448. }
  449. }
  450. out[n] = '\0';
  451. return n;
  452. }
  453. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  454. * space / empty. */
  455. static unsigned int store_attr(struct br_doc *d, const char *s,
  456. unsigned int len, unsigned int limit)
  457. {
  458. unsigned int n;
  459. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  460. return 0;
  461. if (len > limit)
  462. len = limit;
  463. n = decode_text(s, len, d->attrs + d->attrs_used,
  464. BR_ATTR_SIZE - d->attrs_used, 0);
  465. if (n == 0)
  466. return 0;
  467. {
  468. unsigned int off = d->attrs_used;
  469. d->attrs_used += n + 1;
  470. return off;
  471. }
  472. }
  473. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  474. {
  475. return off ? d->attrs + off : "";
  476. }
  477. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  478. static unsigned int store_raw(struct br_doc *d, const char *s,
  479. unsigned int len, unsigned int limit)
  480. {
  481. unsigned int off;
  482. if (len > limit)
  483. len = limit;
  484. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  485. return 0;
  486. off = d->attrs_used;
  487. {
  488. unsigned int i;
  489. for (i = 0; i < len; i++)
  490. d->attrs[off + i] = s[i];
  491. }
  492. d->attrs[off + len] = '\0';
  493. d->attrs_used += len + 1;
  494. return off;
  495. }
  496. #define VAR_HASH_SIZE 1024
  497. static unsigned int var_hash(const char *name, unsigned int len)
  498. {
  499. unsigned int i, h = 2166136261u;
  500. for (i = 0; i < len; i++) {
  501. h ^= (unsigned char)name[i];
  502. h *= 16777619u;
  503. }
  504. return h & (VAR_HASH_SIZE - 1);
  505. }
  506. static unsigned int css_var_find(struct br_doc *d, const char *name,
  507. unsigned int nlen)
  508. {
  509. unsigned int h = var_hash(name, nlen);
  510. for (;;) {
  511. unsigned int i = d->var_slot[h];
  512. if (!i)
  513. return BR_MAX_VARS;
  514. i--;
  515. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  516. const char *n = attr_ptr(d, d->var_name[i]);
  517. unsigned int j;
  518. for (j = 0; j < nlen && n[j] == name[j]; j++)
  519. ;
  520. if (j == nlen)
  521. return i;
  522. }
  523. h = (h + 1) & (VAR_HASH_SIZE - 1);
  524. }
  525. }
  526. static void css_var_set(struct br_doc *d, const char *name,
  527. unsigned int nlen, const char *val, unsigned int vlen)
  528. {
  529. unsigned int i, noff, voff;
  530. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  531. return;
  532. noff = store_raw(d, name, nlen, 63);
  533. voff = store_raw(d, val, vlen, 199);
  534. if (!noff || !voff)
  535. return;
  536. i = css_var_find(d, name, nlen);
  537. if (i >= BR_MAX_VARS) {
  538. unsigned int h;
  539. if (d->nvars >= BR_MAX_VARS)
  540. return;
  541. i = d->nvars++;
  542. d->var_name[i] = noff;
  543. h = var_hash(name, nlen);
  544. while (d->var_slot[h])
  545. h = (h + 1) & (VAR_HASH_SIZE - 1);
  546. d->var_slot[h] = (unsigned short)(i + 1);
  547. }
  548. d->var_val[i] = voff;
  549. }
  550. /* Find the end of a declaration value: the next ';' outside quotes and
  551. * parentheses. The caller passes the position just after the ':'. */
  552. static unsigned int css_value_end(const char *s, unsigned int len,
  553. unsigned int i)
  554. {
  555. int depth = 0;
  556. char quote = 0;
  557. while (i < len) {
  558. char c = s[i];
  559. if (quote) {
  560. if (c == quote)
  561. quote = 0;
  562. else if (c == '\\' && i + 1 < len)
  563. i++;
  564. } else if (c == '"' || c == '\'') {
  565. quote = c;
  566. } else if (c == '(') {
  567. depth++;
  568. } else if (c == ')' && depth > 0) {
  569. depth--;
  570. } else if (c == ';' && depth == 0) {
  571. break;
  572. }
  573. i++;
  574. }
  575. return i;
  576. }
  577. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  578. * second palette. We render the light page, so skip their custom-property
  579. * definitions; otherwise their last-wins values leak into the light rules. */
  580. static int css_dark_selector(const char *s, unsigned int len)
  581. {
  582. unsigned int i;
  583. for (i = 0; i + 4 <= len; i++) {
  584. if ((s[i] == 'n' || s[i] == 'N') &&
  585. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  586. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  587. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  588. (s[i + 4] == 't' || s[i + 4] == 'T'))
  589. return 1;
  590. if ((s[i] == 'd' || s[i] == 'D') &&
  591. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  592. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  593. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  594. return 1;
  595. if (i + 12 <= len &&
  596. (s[i] == 'c' || s[i] == 'C') &&
  597. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  598. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  599. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  600. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  601. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  602. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  603. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  604. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  605. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  606. s[i + 10] == '-' &&
  607. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  608. (s[i + 12] == 's' || s[i + 12] == 'S'))
  609. return 1;
  610. }
  611. return 0;
  612. }
  613. /* Collect --custom-property definitions from one declaration block. */
  614. static void css_collect_block(struct br_doc *d, const char *s,
  615. unsigned int len)
  616. {
  617. unsigned int i = 0;
  618. while (i < len) {
  619. unsigned int pstart, pend, vstart, vend;
  620. while (i < len && (is_space(s[i]) || s[i] == ';'))
  621. i++;
  622. if (i >= len)
  623. break;
  624. pstart = i;
  625. while (i < len && s[i] != ':' && s[i] != ';')
  626. i++;
  627. pend = i;
  628. if (i >= len || s[i] != ':') {
  629. if (i < len)
  630. i++;
  631. continue;
  632. }
  633. i++;
  634. vstart = i;
  635. i = css_value_end(s, len, i);
  636. vend = i;
  637. if (i < len)
  638. i++;
  639. while (pstart < pend && is_space(s[pstart]))
  640. pstart++;
  641. while (pend > pstart && is_space(s[pend - 1]))
  642. pend--;
  643. while (vstart < vend && is_space(s[vstart]))
  644. vstart++;
  645. while (vend > vstart && is_space(s[vend - 1]))
  646. vend--;
  647. if (vend - vstart >= 10 &&
  648. ci_eq_n(s + vend - 10, "!important", 10)) {
  649. vend -= 10;
  650. while (vend > vstart && is_space(s[vend - 1]))
  651. vend--;
  652. }
  653. if (pend - pstart >= 3 && s[pstart] == '-' &&
  654. s[pstart + 1] == '-')
  655. css_var_set(d, s + pstart, pend - pstart,
  656. s + vstart, vend - vstart);
  657. }
  658. }
  659. const char *br_href(struct br_doc *d, unsigned int node)
  660. {
  661. return attr_ptr(d, d->nodes[node].href_off);
  662. }
  663. const char *br_src(struct br_doc *d, unsigned int node)
  664. {
  665. return attr_ptr(d, d->nodes[node].src_off);
  666. }
  667. const char *br_alt(struct br_doc *d, unsigned int node)
  668. {
  669. return attr_ptr(d, d->nodes[node].alt_off);
  670. }
  671. const char *br_rel(struct br_doc *d, unsigned int node)
  672. {
  673. return attr_ptr(d, d->nodes[node].rel_off);
  674. }
  675. const char *br_value(struct br_doc *d, unsigned int node)
  676. {
  677. return attr_ptr(d, d->nodes[node].src_off);
  678. }
  679. const char *br_placeholder(struct br_doc *d, unsigned int node)
  680. {
  681. return attr_ptr(d, d->nodes[node].alt_off);
  682. }
  683. const char *br_type(struct br_doc *d, unsigned int node)
  684. {
  685. return attr_ptr(d, d->nodes[node].rel_off);
  686. }
  687. /* ---- document setup / node append ---- */
  688. static void rule_index_reset(struct br_doc *d)
  689. {
  690. unsigned int i;
  691. for (i = 0; i < BR_MAX_RULES; i++)
  692. d->rule_next[i] = 0xFFFF;
  693. for (i = 0; i < BR_RULE_HASH; i++)
  694. d->rule_bucket[i] = 0xFFFF;
  695. for (i = 0; i < BR_TAG_MAX; i++)
  696. d->rule_tag[i] = 0xFFFF;
  697. d->rule_any = 0xFFFF;
  698. }
  699. void br_doc_init(struct br_doc *d)
  700. {
  701. unsigned int i;
  702. for (i = 0; i < sizeof(*d); i++)
  703. ((unsigned char *)d)[i] = 0;
  704. rule_index_reset(d);
  705. d->nodes = nodes_store;
  706. d->text = text_store;
  707. d->attrs = attr_store;
  708. d->rules = rule_fallback;
  709. d->rule_cap = BR_RULE_FALLBACK;
  710. d->attrs_used = 1; /* offset 0 = "no attribute" */
  711. d->attrs[0] = '\0';
  712. d->nvars = 0;
  713. d->root = 0;
  714. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  715. d->var_slot[vi] = 0;
  716. root_font_px = 16;
  717. bg_url_count = 0;
  718. font_reset();
  719. }
  720. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  721. unsigned int cap)
  722. {
  723. if (!buf || !cap) {
  724. d->rules = rule_fallback;
  725. d->rule_cap = BR_RULE_FALLBACK;
  726. d->nrules = 0;
  727. return;
  728. }
  729. if (cap > BR_MAX_RULES)
  730. cap = BR_MAX_RULES;
  731. d->rules = buf;
  732. d->rule_cap = cap;
  733. d->nrules = 0;
  734. }
  735. static void doc_reset(struct br_doc *d)
  736. {
  737. unsigned int i;
  738. d->nnode = 0;
  739. d->text_used = 0;
  740. d->attrs_used = 1;
  741. d->attrs[0] = '\0';
  742. d->nrules = 0;
  743. rule_index_reset(d);
  744. d->title[0] = '\0';
  745. d->inline_css_len = 0;
  746. d->nvars = 0;
  747. d->truncated = 0;
  748. d->plain = 0;
  749. d->script_src = 0;
  750. d->nscripts = 0;
  751. d->scripts_truncated = 0;
  752. font_reset();
  753. for (i = 0; i < BR_MAX_NODES; i++) {
  754. d->nodes[i].type = BR_NODE_ELEM;
  755. d->nodes[i].tag = BR_TAG_UNKNOWN;
  756. d->nodes[i].flags = 0;
  757. d->nodes[i].parent = 0;
  758. d->nodes[i].first = d->nodes[i].last = 0;
  759. d->nodes[i].next = 0;
  760. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  761. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  762. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  763. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  764. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  765. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  766. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  767. d->nodes[i].name_off = 0;
  768. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  769. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  770. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  771. d->nodes[i].svg_rx = 0;
  772. d->nodes[i].img_index = -1;
  773. d->nodes[i].bg_img_index = -1;
  774. d->nodes[i].style_valid = 0;
  775. }
  776. }
  777. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  778. unsigned char type, unsigned char tag)
  779. {
  780. unsigned int idx;
  781. struct br_node *n;
  782. if (d->nnode >= BR_MAX_NODES) {
  783. d->truncated = 1;
  784. return 0;
  785. }
  786. idx = d->nnode++;
  787. n = &d->nodes[idx];
  788. n->type = type;
  789. n->tag = tag;
  790. n->parent = (unsigned short)parent;
  791. n->img_index = -1;
  792. n->bg_img_index = -1;
  793. if (d->nodes[parent].last)
  794. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  795. else
  796. d->nodes[parent].first = (unsigned short)idx;
  797. d->nodes[parent].last = (unsigned short)idx;
  798. return idx;
  799. }
  800. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  801. const char *s, unsigned int len, int pre)
  802. {
  803. char *dst;
  804. unsigned int n;
  805. unsigned int idx;
  806. if (len == 0)
  807. return 0;
  808. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  809. d->truncated = 1;
  810. return 0;
  811. }
  812. dst = d->text + d->text_used;
  813. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  814. if (n == 0) {
  815. d->text[d->text_used] = '\0';
  816. return 0;
  817. }
  818. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  819. if (!idx)
  820. return 0;
  821. d->nodes[idx].text_off = (unsigned short)d->text_used;
  822. d->nodes[idx].text_len = (unsigned short)n;
  823. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  824. d->text_used += n + 1;
  825. return idx;
  826. }
  827. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  828. static int same_string(const char *a, const char *b)
  829. {
  830. while (*a && *a == *b) {
  831. a++;
  832. b++;
  833. }
  834. return *a == *b;
  835. }
  836. static int same_string_n(const char *a, const char *b, unsigned int n)
  837. {
  838. unsigned int i;
  839. for (i = 0; i < n; i++)
  840. if (a[i] != b[i])
  841. return 0;
  842. return 1;
  843. }
  844. static int node_detached(struct br_doc *d, unsigned int node)
  845. {
  846. unsigned int guard = 0;
  847. while (node && node < d->nnode && guard++ <= d->nnode) {
  848. if (d->nodes[node].flags & BR_NODE_DETACHED)
  849. return 1;
  850. node = d->nodes[node].parent;
  851. }
  852. return 0;
  853. }
  854. unsigned int br_find_id(struct br_doc *d, const char *id)
  855. {
  856. unsigned int i;
  857. if (!d || !id)
  858. return 0;
  859. for (i = 0; i < d->nnode; i++) {
  860. struct br_node *n = &d->nodes[i];
  861. if (n->type == BR_NODE_ELEM && n->id_off &&
  862. same_string(d->attrs + n->id_off, id) &&
  863. !node_detached(d, i))
  864. return i + 1;
  865. }
  866. return 0;
  867. }
  868. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  869. unsigned int cap, unsigned int *used)
  870. {
  871. unsigned int c;
  872. if (node >= d->nnode)
  873. return;
  874. if (d->nodes[node].type == BR_NODE_TEXT) {
  875. unsigned int len = d->nodes[node].text_len;
  876. unsigned int off = d->nodes[node].text_off;
  877. if (*used + len >= cap)
  878. len = *used + 1 < cap ? cap - *used - 1 : 0;
  879. for (c = 0; c < len; c++)
  880. out[*used + c] = d->text[off + c];
  881. *used += len;
  882. return;
  883. }
  884. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  885. text_walk(d, c, out, cap, used);
  886. }
  887. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  888. char *out, unsigned int cap)
  889. {
  890. unsigned int used = 0;
  891. if (!d || !out || cap == 0 || node >= d->nnode)
  892. return 0;
  893. text_walk(d, node, out, cap, &used);
  894. out[used] = '\0';
  895. return used;
  896. }
  897. int br_set_text_content(struct br_doc *d, unsigned int node,
  898. const char *text, unsigned int len)
  899. {
  900. struct br_node *n;
  901. unsigned int idx;
  902. unsigned int i;
  903. if (!d || node >= d->nnode)
  904. return -1;
  905. /* Detach the old subtree; its arena storage is reclaimed on the next
  906. * navigation. */
  907. {
  908. unsigned int c;
  909. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  910. d->nodes[c].flags |= BR_NODE_DETACHED;
  911. }
  912. d->nodes[node].first = d->nodes[node].last = 0;
  913. d->nodes[node].style_valid = 0;
  914. if (len == 0)
  915. return 0;
  916. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  917. d->truncated = 1;
  918. return -1;
  919. }
  920. idx = node_append(d, node, BR_NODE_TEXT, 0);
  921. if (!idx)
  922. return -1;
  923. for (i = 0; i < len; i++)
  924. d->text[d->text_used + i] = text[i];
  925. d->text[d->text_used + len] = '\0';
  926. n = &d->nodes[idx];
  927. n->text_off = (unsigned short)d->text_used;
  928. n->text_len = (unsigned short)len;
  929. n->flags = 0;
  930. d->text_used += len + 1;
  931. return 0;
  932. }
  933. /* Replace the value of an existing text or comment node in place, keeping the
  934. * node handle (and therefore the JS wrapper) valid. */
  935. int br_set_text_node(struct br_doc *d, unsigned int node,
  936. const char *text, unsigned int len)
  937. {
  938. struct br_node *n;
  939. unsigned int i;
  940. if (!d || node >= d->nnode)
  941. return -1;
  942. n = &d->nodes[node];
  943. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  944. return -1;
  945. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  946. d->truncated = 1;
  947. return -1;
  948. }
  949. for (i = 0; i < len; i++)
  950. d->text[d->text_used + i] = text[i];
  951. d->text[d->text_used + len] = '\0';
  952. n->text_off = (unsigned short)d->text_used;
  953. n->text_len = (unsigned short)len;
  954. d->text_used += len + 1;
  955. return 0;
  956. }
  957. /* ---- runtime DOM construction and mutation ---- */
  958. static unsigned int text_len(const char *s)
  959. {
  960. unsigned int n = 0;
  961. while (s && s[n])
  962. n++;
  963. return n;
  964. }
  965. static void node_zero(struct br_node *n)
  966. {
  967. unsigned int i;
  968. for (i = 0; i < sizeof(*n); i++)
  969. ((unsigned char *)n)[i] = 0;
  970. n->img_index = -1;
  971. n->bg_img_index = -1;
  972. }
  973. static void node_unlink(struct br_doc *d, unsigned int handle)
  974. {
  975. unsigned int node, parent, c, prev = 0;
  976. if (!d || handle == 0 || handle > d->nnode)
  977. return;
  978. node = handle - 1;
  979. parent = d->nodes[node].parent;
  980. if (parent >= d->nnode) {
  981. d->nodes[node].parent = 0;
  982. d->nodes[node].next = 0;
  983. return;
  984. }
  985. c = d->nodes[parent].first;
  986. while (c && c != node) {
  987. prev = c;
  988. c = d->nodes[c].next;
  989. }
  990. if (c == node) {
  991. if (prev)
  992. d->nodes[prev].next = d->nodes[node].next;
  993. else
  994. d->nodes[parent].first = d->nodes[node].next;
  995. if (d->nodes[parent].last == node)
  996. d->nodes[parent].last = (unsigned short)prev;
  997. }
  998. d->nodes[node].parent = 0;
  999. d->nodes[node].next = 0;
  1000. d->nodes[node].flags |= BR_NODE_DETACHED;
  1001. }
  1002. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1003. const char *s, unsigned int len);
  1004. static unsigned int find_tag_end(const char *s, unsigned int i,
  1005. unsigned int len)
  1006. {
  1007. char quote = 0;
  1008. while (i < len) {
  1009. char c = s[i];
  1010. if (quote) {
  1011. if (c == quote)
  1012. quote = 0;
  1013. } else if (c == '"' || c == '\'') {
  1014. quote = c;
  1015. } else if (c == '>') {
  1016. return i;
  1017. }
  1018. i++;
  1019. }
  1020. return len;
  1021. }
  1022. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1023. const char *html, unsigned int len)
  1024. {
  1025. unsigned int stack[64];
  1026. unsigned int sp = 1, i = 0;
  1027. if (!d || node >= d->nnode)
  1028. return -1;
  1029. stack[0] = node;
  1030. {
  1031. unsigned int c;
  1032. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1033. d->nodes[c].flags |= BR_NODE_DETACHED;
  1034. }
  1035. d->nodes[node].first = d->nodes[node].last = 0;
  1036. d->nodes[node].style_valid = 0;
  1037. while (i < len) {
  1038. if (html[i] != '<') {
  1039. unsigned int start = i, end;
  1040. while (i < len && html[i] != '<')
  1041. i++;
  1042. end = i;
  1043. if (end > start) {
  1044. unsigned int h = br_create_text(d, html + start,
  1045. end - start);
  1046. if (h)
  1047. br_append_child(d, stack[sp - 1], h);
  1048. }
  1049. continue;
  1050. }
  1051. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1052. html[i + 3] == '-') {
  1053. int p = find_ci(html, len, "-->", 3, i + 4);
  1054. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1055. unsigned int h = br_create_comment(d, html + i + 4,
  1056. end > i + 4 ?
  1057. end - i - 4 : 0);
  1058. if (h)
  1059. br_append_child(d, stack[sp - 1], h);
  1060. i = p >= 0 ? (unsigned int)p + 3 : len;
  1061. continue;
  1062. }
  1063. if (html[i + 1] == '/') {
  1064. unsigned int p = find_tag_end(html, i + 2, len);
  1065. i = p < len ? p + 1 : len;
  1066. if (sp > 1)
  1067. sp--;
  1068. continue;
  1069. }
  1070. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1071. unsigned int p = find_tag_end(html, i + 2, len);
  1072. i = p < len ? p + 1 : len;
  1073. continue;
  1074. }
  1075. {
  1076. unsigned int p = find_tag_end(html, i + 1, len);
  1077. unsigned int ns = i + 1, ne, as, ae;
  1078. unsigned int h, k;
  1079. char name[32];
  1080. int selfclose = 0;
  1081. while (ns < p && is_space(html[ns]))
  1082. ns++;
  1083. ne = ns;
  1084. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1085. ne++;
  1086. as = ne;
  1087. ae = p;
  1088. if (ae > as && html[ae - 1] == '/') {
  1089. selfclose = 1;
  1090. ae--;
  1091. }
  1092. if (ne == ns) {
  1093. i = p < len ? p + 1 : len;
  1094. continue;
  1095. }
  1096. if (ne - ns >= sizeof(name)) {
  1097. i = p < len ? p + 1 : len;
  1098. continue;
  1099. }
  1100. for (k = 0; k < ne - ns; k++)
  1101. name[k] = html[ns + k];
  1102. name[ne - ns] = '\0';
  1103. h = br_create_element(d, name, ne - ns);
  1104. if (!h) {
  1105. i = p < len ? p + 1 : len;
  1106. continue;
  1107. }
  1108. if (ae > as)
  1109. parse_attrs(d, &d->nodes[h - 1], html + as,
  1110. ae - as);
  1111. br_append_child(d, stack[sp - 1], h);
  1112. {
  1113. unsigned int tag = d->nodes[h - 1].tag;
  1114. if (!selfclose && !br_tag_is_void(tag) &&
  1115. sp < sizeof(stack) / sizeof(stack[0]))
  1116. stack[sp++] = h - 1;
  1117. }
  1118. i = p < len ? p + 1 : len;
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1124. {
  1125. unsigned int i;
  1126. if (!d)
  1127. return 0;
  1128. for (i = 0; i < d->nnode; i++)
  1129. if (d->nodes[i].type == BR_NODE_ELEM &&
  1130. d->nodes[i].tag == (unsigned char)tag &&
  1131. !node_detached(d, i))
  1132. return i + 1;
  1133. return 0;
  1134. }
  1135. unsigned int br_create_element(struct br_doc *d, const char *name,
  1136. unsigned int len)
  1137. {
  1138. struct br_node *n;
  1139. unsigned int idx;
  1140. if (!d || !name)
  1141. return 0;
  1142. if (d->nnode >= BR_MAX_NODES) {
  1143. d->truncated = 1;
  1144. return 0;
  1145. }
  1146. idx = d->nnode++;
  1147. n = &d->nodes[idx];
  1148. node_zero(n);
  1149. n->type = BR_NODE_ELEM;
  1150. n->flags = BR_NODE_DETACHED;
  1151. n->tag = (unsigned char)br_tag_from_name(name);
  1152. if (n->tag == BR_TAG_UNKNOWN && len)
  1153. n->name_off = store_attr(d, name, len, 31);
  1154. return idx + 1;
  1155. }
  1156. unsigned int br_create_text(struct br_doc *d, const char *text,
  1157. unsigned int len)
  1158. {
  1159. struct br_node *n;
  1160. unsigned int idx, i;
  1161. if (!d)
  1162. return 0;
  1163. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1164. d->truncated = 1;
  1165. return 0;
  1166. }
  1167. idx = d->nnode++;
  1168. n = &d->nodes[idx];
  1169. node_zero(n);
  1170. n->type = BR_NODE_TEXT;
  1171. n->flags = BR_NODE_DETACHED;
  1172. n->text_off = (unsigned short)d->text_used;
  1173. n->text_len = (unsigned short)len;
  1174. for (i = 0; i < len; i++)
  1175. d->text[d->text_used + i] = text[i];
  1176. d->text[d->text_used + len] = '\0';
  1177. d->text_used += len + 1;
  1178. return idx + 1;
  1179. }
  1180. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1181. unsigned int len)
  1182. {
  1183. unsigned int h = br_create_text(d, text, len);
  1184. if (h)
  1185. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1186. return h;
  1187. }
  1188. unsigned int br_create_fragment(struct br_doc *d)
  1189. {
  1190. struct br_node *n;
  1191. unsigned int idx;
  1192. if (!d)
  1193. return 0;
  1194. if (d->nnode >= BR_MAX_NODES) {
  1195. d->truncated = 1;
  1196. return 0;
  1197. }
  1198. idx = d->nnode++;
  1199. n = &d->nodes[idx];
  1200. node_zero(n);
  1201. n->type = BR_NODE_FRAGMENT;
  1202. n->flags = BR_NODE_DETACHED;
  1203. return idx + 1;
  1204. }
  1205. int br_append_child(struct br_doc *d, unsigned int parent,
  1206. unsigned int child)
  1207. {
  1208. unsigned int p;
  1209. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1210. parent == child - 1)
  1211. return -1;
  1212. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1213. /* Appending a fragment inserts its children, not the fragment. */
  1214. unsigned int c = d->nodes[child - 1].first;
  1215. while (c) {
  1216. unsigned int next = d->nodes[c].next;
  1217. br_append_child(d, parent, c + 1);
  1218. c = next;
  1219. }
  1220. return 0;
  1221. }
  1222. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1223. if (p == child - 1)
  1224. return -1;
  1225. node_unlink(d, child);
  1226. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1227. d->nodes[child - 1].parent = (unsigned short)parent;
  1228. d->nodes[child - 1].next = 0;
  1229. if (d->nodes[parent].last)
  1230. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1231. else
  1232. d->nodes[parent].first = (unsigned short)(child - 1);
  1233. d->nodes[parent].last = (unsigned short)(child - 1);
  1234. return 0;
  1235. }
  1236. int br_insert_before(struct br_doc *d, unsigned int parent,
  1237. unsigned int child, unsigned int before)
  1238. {
  1239. unsigned int prev = 0, c;
  1240. if (before == 0)
  1241. return br_append_child(d, parent, child);
  1242. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1243. before > d->nnode || parent == child - 1)
  1244. return -1;
  1245. if (d->nodes[before - 1].parent != parent)
  1246. return -1;
  1247. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1248. unsigned int c = d->nodes[child - 1].first;
  1249. while (c) {
  1250. unsigned int next = d->nodes[c].next;
  1251. br_insert_before(d, parent, c + 1, before);
  1252. c = next;
  1253. }
  1254. return 0;
  1255. }
  1256. {
  1257. unsigned int p;
  1258. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1259. if (p == child - 1)
  1260. return -1;
  1261. }
  1262. node_unlink(d, child);
  1263. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1264. c = d->nodes[parent].first;
  1265. while (c && c != before - 1) {
  1266. prev = c;
  1267. c = d->nodes[c].next;
  1268. }
  1269. if (c != before - 1)
  1270. return -1;
  1271. d->nodes[child - 1].parent = (unsigned short)parent;
  1272. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1273. if (prev)
  1274. d->nodes[prev].next = (unsigned short)(child - 1);
  1275. else
  1276. d->nodes[parent].first = (unsigned short)(child - 1);
  1277. return 0;
  1278. }
  1279. int br_remove_child(struct br_doc *d, unsigned int parent,
  1280. unsigned int child)
  1281. {
  1282. if (!d || child == 0 || child > d->nnode ||
  1283. d->nodes[child - 1].parent != parent)
  1284. return -1;
  1285. node_unlink(d, child);
  1286. return 0;
  1287. }
  1288. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1289. unsigned int child)
  1290. {
  1291. unsigned int c;
  1292. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1293. return 0;
  1294. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1295. if (c == child - 1)
  1296. return child;
  1297. return 0;
  1298. }
  1299. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1300. const char *name)
  1301. {
  1302. struct br_node *n;
  1303. const char *p, *end, *found = 0;
  1304. if (!d || node >= d->nnode || !name)
  1305. return 0;
  1306. n = &d->nodes[node];
  1307. p = d->attrs + n->extra_off;
  1308. end = p + n->extra_len;
  1309. while (p + 1 < end) {
  1310. const char *an = p;
  1311. const char *av;
  1312. p += text_len(p) + 1;
  1313. if (p >= end)
  1314. break;
  1315. av = p;
  1316. p += text_len(p) + 1;
  1317. if (same_string(an, name))
  1318. found = av;
  1319. }
  1320. if (found)
  1321. return found;
  1322. if (same_string(name, "id") && n->id_off)
  1323. return d->attrs + n->id_off;
  1324. if (same_string(name, "class") && n->class_off)
  1325. return d->attrs + n->class_off;
  1326. if (same_string(name, "style") && n->style_off)
  1327. return d->attrs + n->style_off;
  1328. if (same_string(name, "href") && n->href_off)
  1329. return d->attrs + n->href_off;
  1330. if (same_string(name, "src") && n->src_off)
  1331. return d->attrs + n->src_off;
  1332. if (same_string(name, "alt") && n->alt_off)
  1333. return d->attrs + n->alt_off;
  1334. if (same_string(name, "rel") && n->rel_off)
  1335. return d->attrs + n->rel_off;
  1336. return 0;
  1337. }
  1338. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1339. const char *name, const char *value,
  1340. unsigned int len)
  1341. {
  1342. if (same_string(name, "id"))
  1343. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1344. else if (same_string(name, "class"))
  1345. n->class_off = len ? store_attr(d, value, len, 120) : 0;
  1346. else if (same_string(name, "style"))
  1347. n->style_off = len ? store_attr(d, value, len, 240) : 0;
  1348. else if (same_string(name, "href"))
  1349. n->href_off = len ? store_attr(d, value, len, 480) : 0;
  1350. else if (same_string(name, "src"))
  1351. n->src_off = len ? store_attr(d, value, len, 400) : 0;
  1352. else if (same_string(name, "alt"))
  1353. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1354. else if (same_string(name, "rel"))
  1355. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1356. }
  1357. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1358. const char *value, unsigned int len)
  1359. {
  1360. struct br_node *n;
  1361. unsigned int start, nl, need;
  1362. const char *p, *old, *oldend;
  1363. if (!d || node >= d->nnode || !name)
  1364. return -1;
  1365. n = &d->nodes[node];
  1366. nl = text_len(name);
  1367. need = nl + 1 + len + 1;
  1368. start = d->attrs_used;
  1369. old = d->attrs + n->extra_off;
  1370. oldend = old + n->extra_len;
  1371. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1372. d->truncated = 1;
  1373. return -1;
  1374. }
  1375. p = old;
  1376. while (p + 1 < oldend) {
  1377. const char *an = p;
  1378. const char *av;
  1379. unsigned int anl, avl, k;
  1380. anl = text_len(an);
  1381. p += anl + 1;
  1382. if (p >= oldend)
  1383. break;
  1384. av = p;
  1385. avl = text_len(av);
  1386. p += avl + 1;
  1387. if (same_string(an, name))
  1388. continue;
  1389. for (k = 0; k < anl; k++)
  1390. d->attrs[d->attrs_used++] = an[k];
  1391. d->attrs[d->attrs_used++] = '\0';
  1392. for (k = 0; k < avl; k++)
  1393. d->attrs[d->attrs_used++] = av[k];
  1394. d->attrs[d->attrs_used++] = '\0';
  1395. }
  1396. {
  1397. unsigned int k;
  1398. for (k = 0; k < nl; k++)
  1399. d->attrs[d->attrs_used++] = name[k];
  1400. d->attrs[d->attrs_used++] = '\0';
  1401. for (k = 0; k < len; k++)
  1402. d->attrs[d->attrs_used++] = value[k];
  1403. d->attrs[d->attrs_used++] = '\0';
  1404. }
  1405. n = &d->nodes[node];
  1406. n->extra_off = start;
  1407. n->extra_len = (unsigned short)(d->attrs_used - start);
  1408. n->style_valid = 0;
  1409. attr_note_known(d, n, name, value, len);
  1410. return 0;
  1411. }
  1412. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1413. {
  1414. struct br_node *n;
  1415. unsigned int start;
  1416. const char *p, *oldend;
  1417. int found = 0;
  1418. if (!d || node >= d->nnode || !name)
  1419. return -1;
  1420. n = &d->nodes[node];
  1421. start = d->attrs_used;
  1422. p = d->attrs + n->extra_off;
  1423. oldend = p + n->extra_len;
  1424. if (start + n->extra_len > BR_ATTR_SIZE)
  1425. return -1;
  1426. while (p + 1 < oldend) {
  1427. const char *an = p;
  1428. const char *av;
  1429. unsigned int anl, avl, k;
  1430. anl = text_len(an);
  1431. p += anl + 1;
  1432. if (p >= oldend)
  1433. break;
  1434. av = p;
  1435. avl = text_len(av);
  1436. p += avl + 1;
  1437. if (same_string(an, name)) {
  1438. found = 1;
  1439. continue;
  1440. }
  1441. for (k = 0; k < anl; k++)
  1442. d->attrs[d->attrs_used++] = an[k];
  1443. d->attrs[d->attrs_used++] = '\0';
  1444. for (k = 0; k < avl; k++)
  1445. d->attrs[d->attrs_used++] = av[k];
  1446. d->attrs[d->attrs_used++] = '\0';
  1447. }
  1448. n = &d->nodes[node];
  1449. n->extra_off = start;
  1450. n->extra_len = (unsigned short)(d->attrs_used - start);
  1451. n->style_valid = 0;
  1452. attr_note_known(d, n, name, "", 0);
  1453. return found ? 0 : -1;
  1454. }
  1455. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1456. {
  1457. struct br_node *src;
  1458. unsigned int h;
  1459. const char *name;
  1460. if (!d || node == 0 || node > d->nnode)
  1461. return 0;
  1462. src = &d->nodes[node - 1];
  1463. if (src->type == BR_NODE_TEXT)
  1464. return br_create_text(d, d->text + src->text_off, src->text_len);
  1465. if (src->type == BR_NODE_COMMENT)
  1466. return br_create_comment(d, d->text + src->text_off,
  1467. src->text_len);
  1468. if (src->type == BR_NODE_FRAGMENT)
  1469. h = br_create_fragment(d);
  1470. else {
  1471. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1472. name = d->attrs + src->name_off;
  1473. else
  1474. name = br_tag_name(src->tag);
  1475. h = br_create_element(d, name, text_len(name));
  1476. }
  1477. if (!h)
  1478. return 0;
  1479. {
  1480. const char *p = d->attrs + src->extra_off;
  1481. const char *end = p + src->extra_len;
  1482. while (p + 1 < end) {
  1483. const char *an = p;
  1484. const char *av;
  1485. unsigned int anl, avl;
  1486. anl = text_len(an);
  1487. p += anl + 1;
  1488. if (p >= end)
  1489. break;
  1490. av = p;
  1491. avl = text_len(av);
  1492. p += avl + 1;
  1493. br_set_attr(d, h, an, av, avl);
  1494. }
  1495. }
  1496. if (deep) {
  1497. unsigned int c;
  1498. for (c = src->first; c; c = d->nodes[c].next) {
  1499. unsigned int ch = br_clone_node(d, c + 1, 1);
  1500. if (ch)
  1501. br_append_child(d, h - 1, ch);
  1502. }
  1503. }
  1504. return h;
  1505. }
  1506. static int attr_value_match(const char *have, const char *want,
  1507. unsigned int want_len, int op)
  1508. {
  1509. unsigned int n = text_len(have);
  1510. unsigned int i;
  1511. if (!op)
  1512. return have != 0;
  1513. if (!have)
  1514. return 0;
  1515. if (op == '=')
  1516. return n == want_len && same_string_n(have, want, want_len);
  1517. if (op == '^')
  1518. return n >= want_len && same_string_n(have, want, want_len);
  1519. if (op == '$') {
  1520. if (n < want_len)
  1521. return 0;
  1522. have += n - want_len;
  1523. for (i = 0; i < want_len; i++)
  1524. if (have[i] != want[i])
  1525. return 0;
  1526. return 1;
  1527. }
  1528. if (op == '*') {
  1529. for (i = 0; i + want_len <= n; i++)
  1530. if (same_string_n(have + i, want, want_len))
  1531. return 1;
  1532. return 0;
  1533. }
  1534. return 0;
  1535. }
  1536. static int class_contains(const char *classes, const char *want,
  1537. unsigned int want_len)
  1538. {
  1539. unsigned int i = 0;
  1540. if (!classes)
  1541. return 0;
  1542. while (classes[i]) {
  1543. unsigned int start;
  1544. while (classes[i] == ' ' || classes[i] == '\t' ||
  1545. classes[i] == '\r' || classes[i] == '\n')
  1546. i++;
  1547. start = i;
  1548. while (classes[i] && classes[i] != ' ' && classes[i] != '\t' &&
  1549. classes[i] != '\r' && classes[i] != '\n')
  1550. i++;
  1551. if (i - start == want_len &&
  1552. same_string_n(classes + start, want, want_len))
  1553. return 1;
  1554. }
  1555. return 0;
  1556. }
  1557. static int match_compound(struct br_doc *d, unsigned int node,
  1558. const char *sel, unsigned int len)
  1559. {
  1560. struct br_node *n = &d->nodes[node];
  1561. unsigned int i = 0;
  1562. while (i < len && sel[i] != '.' && sel[i] != '#' && sel[i] != '[')
  1563. i++;
  1564. if (i > 0 && !(i == 1 && sel[0] == '*')) {
  1565. char tag[32];
  1566. unsigned int tl = i < sizeof(tag) - 1 ? i : sizeof(tag) - 1;
  1567. unsigned int k;
  1568. for (k = 0; k < tl; k++)
  1569. tag[k] = sel[k];
  1570. tag[tl] = '\0';
  1571. if (n->tag == BR_TAG_UNKNOWN) {
  1572. if (!n->name_off ||
  1573. !same_string(d->attrs + n->name_off, tag))
  1574. return 0;
  1575. } else if (n->tag != br_tag_from_name(tag)) {
  1576. return 0;
  1577. }
  1578. }
  1579. while (i < len) {
  1580. if (sel[i] == '.') {
  1581. unsigned int start;
  1582. i++;
  1583. start = i;
  1584. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1585. sel[i] != '[' && sel[i] != ' ')
  1586. i++;
  1587. if (!class_contains(br_find_attr(d, node, "class"),
  1588. sel + start, i - start))
  1589. return 0;
  1590. } else if (sel[i] == '#') {
  1591. unsigned int start;
  1592. i++;
  1593. start = i;
  1594. while (i < len && sel[i] != '.' && sel[i] != '#' &&
  1595. sel[i] != '[' && sel[i] != ' ')
  1596. i++;
  1597. {
  1598. const char *id = br_find_attr(d, node, "id");
  1599. if (!id || !same_string_n(id, sel + start,
  1600. i - start) ||
  1601. text_len(id) != i - start)
  1602. return 0;
  1603. }
  1604. } else if (sel[i] == '[') {
  1605. unsigned int start, nl, op = 0, vs, ve;
  1606. char quote = 0;
  1607. i++;
  1608. start = i;
  1609. while (i < len && sel[i] != '=' && sel[i] != ']' &&
  1610. sel[i] != '^' && sel[i] != '$' && sel[i] != '*')
  1611. i++;
  1612. nl = i - start;
  1613. if (i < len && sel[i] != ']') {
  1614. op = sel[i++];
  1615. if (i < len && sel[i] == '=')
  1616. i++;
  1617. }
  1618. while (i < len && sel[i] == ' ')
  1619. i++;
  1620. if (i < len && (sel[i] == '"' || sel[i] == '\'')) {
  1621. quote = sel[i++];
  1622. vs = i;
  1623. while (i < len && sel[i] != quote)
  1624. i++;
  1625. ve = i;
  1626. if (i < len)
  1627. i++;
  1628. } else {
  1629. vs = i;
  1630. while (i < len && sel[i] != ']')
  1631. i++;
  1632. ve = i;
  1633. }
  1634. if (i < len && sel[i] == ']')
  1635. i++;
  1636. {
  1637. char name[32];
  1638. unsigned int k;
  1639. if (nl >= sizeof(name))
  1640. return 0;
  1641. for (k = 0; k < nl; k++)
  1642. name[k] = sel[start + k];
  1643. name[nl] = '\0';
  1644. if (!attr_value_match(br_find_attr(d, node, name),
  1645. sel + vs, ve - vs, op))
  1646. return 0;
  1647. }
  1648. } else {
  1649. return 0;
  1650. }
  1651. }
  1652. return 1;
  1653. }
  1654. static int match_selector(struct br_doc *d, unsigned int node,
  1655. const char *sel, unsigned int len)
  1656. {
  1657. unsigned int i, split = 0, depth = 0;
  1658. char quote = 0;
  1659. while (len > 0 && sel[0] == ' ') {
  1660. sel++;
  1661. len--;
  1662. }
  1663. while (len > 0 && sel[len - 1] == ' ')
  1664. len--;
  1665. if (len == 0)
  1666. return 0;
  1667. for (i = 0; i < len; i++) {
  1668. char c = sel[i];
  1669. if (quote) {
  1670. if (c == quote)
  1671. quote = 0;
  1672. } else if (c == '"' || c == '\'') {
  1673. quote = c;
  1674. } else if (c == '[') {
  1675. depth++;
  1676. } else if (c == ']' && depth) {
  1677. depth--;
  1678. } else if (!depth && c == ' ') {
  1679. split = i;
  1680. }
  1681. }
  1682. if (split) {
  1683. unsigned int p = d->nodes[node].parent;
  1684. if (!match_compound(d, node, sel + split + 1,
  1685. len - split - 1))
  1686. return 0;
  1687. while (p) {
  1688. unsigned int next = d->nodes[p].parent;
  1689. if (match_selector(d, p, sel, split))
  1690. return 1;
  1691. p = next;
  1692. }
  1693. return 0;
  1694. }
  1695. return match_compound(d, node, sel, len);
  1696. }
  1697. static int query_add(unsigned int *out, unsigned int max,
  1698. unsigned int *count, unsigned int node)
  1699. {
  1700. unsigned int i;
  1701. for (i = 0; i < *count; i++)
  1702. if (out[i] == node + 1)
  1703. return 0;
  1704. if (*count < max)
  1705. out[(*count)++] = node + 1;
  1706. return 0;
  1707. }
  1708. static void query_walk(struct br_doc *d, unsigned int node,
  1709. const char *sel, unsigned int len, unsigned int *out,
  1710. unsigned int max, unsigned int *count)
  1711. {
  1712. unsigned int c;
  1713. if (match_selector(d, node, sel, len))
  1714. query_add(out, max, count, node + 1);
  1715. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1716. query_walk(d, c, sel, len, out, max, count);
  1717. }
  1718. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1719. {
  1720. unsigned int len;
  1721. if (!d || !selector || node == 0 || node > d->nnode)
  1722. return 0;
  1723. len = text_len(selector);
  1724. while (len > 0 && selector[0] == ' ') {
  1725. selector++;
  1726. len--;
  1727. }
  1728. while (len > 0 && selector[len - 1] == ' ')
  1729. len--;
  1730. return match_selector(d, node - 1, selector, len);
  1731. }
  1732. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1733. const char *selector, unsigned int *out,
  1734. unsigned int max)
  1735. {
  1736. unsigned int count = 0, i = 0;
  1737. if (!d || !selector || !out || max == 0)
  1738. return 0;
  1739. while (selector[i]) {
  1740. unsigned int start = i, depth = 0;
  1741. char quote = 0;
  1742. while (selector[i]) {
  1743. char c = selector[i];
  1744. if (quote) {
  1745. if (c == quote)
  1746. quote = 0;
  1747. } else if (c == '"' || c == '\'') {
  1748. quote = c;
  1749. } else if (c == '[') {
  1750. depth++;
  1751. } else if (c == ']' && depth) {
  1752. depth--;
  1753. } else if (!depth && c == ',') {
  1754. break;
  1755. }
  1756. i++;
  1757. }
  1758. if (i > start) {
  1759. if (root == 0) {
  1760. unsigned int n;
  1761. for (n = 1; n < d->nnode; n++)
  1762. if (!node_detached(d, n) &&
  1763. match_selector(d, n, selector + start,
  1764. i - start))
  1765. query_add(out, max, &count,
  1766. n + 1);
  1767. } else if (root <= d->nnode) {
  1768. unsigned int c;
  1769. for (c = d->nodes[root - 1].first; c;
  1770. c = d->nodes[c].next)
  1771. query_walk(d, c, selector + start,
  1772. i - start, out, max, &count);
  1773. }
  1774. }
  1775. if (selector[i] == ',')
  1776. i++;
  1777. }
  1778. return count;
  1779. }
  1780. /* ---- attribute parsing ---- */
  1781. static int is_space(char c)
  1782. {
  1783. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1784. }
  1785. static int str_eq(const char *a, const char *b);
  1786. static unsigned int parse_uint(const char *s, unsigned int len)
  1787. {
  1788. unsigned int i = 0, v = 0;
  1789. while (i < len && is_space(s[i]))
  1790. i++;
  1791. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1792. v = v * 10 + (unsigned int)(s[i] - '0');
  1793. if (v > 4096)
  1794. v = 4096;
  1795. i++;
  1796. }
  1797. return v;
  1798. }
  1799. static int contains_ci_len(const char *s, unsigned int len,
  1800. const char *needle);
  1801. static int parse_svg_viewbox(const char *s, unsigned int len,
  1802. unsigned short *w, unsigned short *h)
  1803. {
  1804. unsigned int i = 0, v[4], n = 0;
  1805. while (i < len && n < 4) {
  1806. unsigned int x = 0;
  1807. int any = 0;
  1808. while (i < len && (is_space(s[i]) || s[i] == ','))
  1809. i++;
  1810. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1811. x = x * 10 + (unsigned int)(s[i] - '0');
  1812. i++;
  1813. any = 1;
  1814. }
  1815. if (!any) {
  1816. if (i < len)
  1817. i++;
  1818. continue;
  1819. }
  1820. v[n++] = x;
  1821. }
  1822. if (n < 4)
  1823. return 0;
  1824. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1825. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1826. return *w && *h;
  1827. }
  1828. static void note_attr_flag(struct br_node *n, const char *name)
  1829. {
  1830. if (str_eq(name, "data-collapse-target"))
  1831. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1832. else if (str_eq(name, "data-collapse-trigger"))
  1833. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1834. else if (str_eq(name, "is-open"))
  1835. n->attr_flags |= BR_ATTR_IS_OPEN;
  1836. else if (str_eq(name, "disabled"))
  1837. n->attr_flags |= BR_ATTR_DISABLED;
  1838. else if (str_eq(name, "open"))
  1839. n->attr_flags |= BR_ATTR_OPEN;
  1840. else if (str_eq(name, "inert"))
  1841. n->attr_flags |= BR_ATTR_INERT;
  1842. }
  1843. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1844. * second pass so the block stays contiguous even though the known attributes
  1845. * above also use the shared attr arena. */
  1846. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1847. const char *s, unsigned int len)
  1848. {
  1849. unsigned int i = 0;
  1850. unsigned int start = d->attrs_used;
  1851. while (i < len) {
  1852. char name[32];
  1853. unsigned int nl = 0, vstart, vlen;
  1854. char quote = 0;
  1855. while (i < len && is_space(s[i]))
  1856. i++;
  1857. if (i >= len || s[i] == '/')
  1858. break;
  1859. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1860. char c = s[i++];
  1861. if (c >= 'A' && c <= 'Z')
  1862. c = (char)(c - 'A' + 'a');
  1863. if (nl + 1 < sizeof(name))
  1864. name[nl++] = c;
  1865. }
  1866. name[nl] = '\0';
  1867. while (i < len && is_space(s[i]))
  1868. i++;
  1869. if (i < len && s[i] == '=') {
  1870. i++;
  1871. while (i < len && is_space(s[i]))
  1872. i++;
  1873. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1874. quote = s[i++];
  1875. vstart = i;
  1876. while (i < len && s[i] != quote)
  1877. i++;
  1878. vlen = i - vstart;
  1879. if (i < len)
  1880. i++;
  1881. } else {
  1882. vstart = i;
  1883. while (i < len && !is_space(s[i]) && s[i] != '>')
  1884. i++;
  1885. vlen = i - vstart;
  1886. }
  1887. } else {
  1888. /* Boolean attribute: expose it with an empty value. */
  1889. vstart = 0;
  1890. vlen = 0;
  1891. }
  1892. if (!nl)
  1893. continue;
  1894. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1895. d->truncated = 1;
  1896. break;
  1897. }
  1898. {
  1899. unsigned int k;
  1900. for (k = 0; k < nl; k++)
  1901. d->attrs[d->attrs_used++] = name[k];
  1902. d->attrs[d->attrs_used++] = '\0';
  1903. for (k = 0; k < vlen; k++)
  1904. d->attrs[d->attrs_used++] = s[vstart + k];
  1905. d->attrs[d->attrs_used++] = '\0';
  1906. }
  1907. }
  1908. if (d->attrs_used > start) {
  1909. n->extra_off = start;
  1910. n->extra_len = (unsigned short)(d->attrs_used - start);
  1911. }
  1912. }
  1913. /* Parse attributes from `s` (between the tag name and '>'). */
  1914. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1915. const char *s, unsigned int len)
  1916. {
  1917. unsigned int i = 0;
  1918. while (i < len) {
  1919. char name[32];
  1920. unsigned int nl = 0, vstart, vlen;
  1921. char quote = 0;
  1922. while (i < len && is_space(s[i]))
  1923. i++;
  1924. if (i >= len || s[i] == '/')
  1925. break;
  1926. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1927. char c = s[i++];
  1928. if (c >= 'A' && c <= 'Z')
  1929. c = (char)(c - 'A' + 'a');
  1930. if (nl + 1 < sizeof(name))
  1931. name[nl++] = c;
  1932. }
  1933. name[nl] = '\0';
  1934. while (i < len && is_space(s[i]))
  1935. i++;
  1936. if (i >= len || s[i] != '=') {
  1937. if (str_eq(name, "hidden"))
  1938. n->flags |= BR_NODE_HIDDEN;
  1939. note_attr_flag(n, name);
  1940. continue;
  1941. }
  1942. i++;
  1943. while (i < len && is_space(s[i]))
  1944. i++;
  1945. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1946. quote = s[i++];
  1947. vstart = i;
  1948. while (i < len && s[i] != quote)
  1949. i++;
  1950. vlen = i - vstart;
  1951. if (i < len)
  1952. i++;
  1953. } else {
  1954. vstart = i;
  1955. while (i < len && !is_space(s[i]) && s[i] != '>')
  1956. i++;
  1957. vlen = i - vstart;
  1958. }
  1959. if (!nl)
  1960. continue;
  1961. if (str_eq(name, "hidden")) {
  1962. n->flags |= BR_NODE_HIDDEN;
  1963. continue;
  1964. }
  1965. note_attr_flag(n, name);
  1966. if (str_eq(name, "href"))
  1967. n->href_off = store_attr(d, s + vstart, vlen, 480);
  1968. else if (str_eq(name, "class"))
  1969. n->class_off = store_attr(d, s + vstart, vlen, 120);
  1970. else if (str_eq(name, "id"))
  1971. n->id_off = store_attr(d, s + vstart, vlen, 64);
  1972. else if (str_eq(name, "style"))
  1973. n->style_off = store_attr(d, s + vstart, vlen, 240);
  1974. else if (str_eq(name, "src"))
  1975. n->src_off = store_attr(d, s + vstart, vlen, 400);
  1976. else if (str_eq(name, "alt"))
  1977. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  1978. else if (str_eq(name, "color"))
  1979. n->color_off = store_attr(d, s + vstart, vlen, 32);
  1980. else if (str_eq(name, "rel"))
  1981. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  1982. else if (str_eq(name, "value"))
  1983. n->src_off = store_attr(d, s + vstart, vlen, 220);
  1984. else if (str_eq(name, "placeholder") &&
  1985. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  1986. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  1987. else if (str_eq(name, "type") &&
  1988. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1989. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  1990. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  1991. else if (str_eq(name, "width")) {
  1992. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1993. if (vlen && s[vstart + vlen - 1] == '%')
  1994. n->width_pct = 1;
  1995. } else if (str_eq(name, "height")) {
  1996. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  1997. if (vlen && s[vstart + vlen - 1] == '%')
  1998. n->height_pct = 1;
  1999. } else if (str_eq(name, "colspan")) {
  2000. unsigned int v = parse_uint(s + vstart, vlen);
  2001. if (v < 1)
  2002. v = 1;
  2003. if (v > 16)
  2004. v = 16;
  2005. n->colspan = (unsigned char)v;
  2006. } else if (str_eq(name, "rowspan")) {
  2007. unsigned int v = parse_uint(s + vstart, vlen);
  2008. if (v < 1)
  2009. v = 1;
  2010. if (v > 16)
  2011. v = 16;
  2012. n->rowspan = (unsigned char)v;
  2013. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2014. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2015. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2016. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2017. } else if (str_eq(name, "x2")) {
  2018. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2019. } else if (str_eq(name, "y2")) {
  2020. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2021. } else if (str_eq(name, "rx")) {
  2022. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2023. } else if (str_eq(name, "d")) {
  2024. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2025. } else if (str_eq(name, "viewbox")) {
  2026. unsigned short vw = 0, vh = 0;
  2027. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2028. n->width_attr = vw;
  2029. n->height_attr = vh;
  2030. n->flags |= BR_NODE_VIEWBOX;
  2031. }
  2032. } else if (str_eq(name, "text-anchor")) {
  2033. if (contains_ci_len(s + vstart, vlen, "middle"))
  2034. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2035. else if (contains_ci_len(s + vstart, vlen, "end"))
  2036. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2037. }
  2038. }
  2039. parse_attrs_generic(d, n, s, len);
  2040. }
  2041. static int str_eq(const char *a, const char *b)
  2042. {
  2043. unsigned int i = 0;
  2044. while (a[i] && b[i] && a[i] == b[i])
  2045. i++;
  2046. return a[i] == '\0' && b[i] == '\0';
  2047. }
  2048. /* ---- CSS ---- */
  2049. enum {
  2050. PROP_COLOR, PROP_BG, PROP_BG_IMAGE, PROP_FONT_PX, PROP_FONT_UNIT,
  2051. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2052. PROP_ALIGN, PROP_UNDERLINE,
  2053. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2054. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2055. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2056. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2057. PROP_BORDER_W, PROP_BORDER_COLOR,
  2058. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2059. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2060. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2061. PROP_LIST_NONE, PROP_NOWRAP,
  2062. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2063. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2064. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2065. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2066. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2067. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2068. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2069. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2070. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2071. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2072. PROP_ZINDEX, PROP_ZSET,
  2073. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2074. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2075. PROP_COUNT
  2076. };
  2077. struct color_name {
  2078. const char *name;
  2079. unsigned short rgb;
  2080. };
  2081. static const struct color_name color_names[] = {
  2082. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2083. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2084. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2085. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2086. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2087. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2088. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2089. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2090. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2091. { "darkgrey", 0x528A }, { 0, 0 }
  2092. };
  2093. static int hexval(char c)
  2094. {
  2095. if (c >= '0' && c <= '9')
  2096. return c - '0';
  2097. if (c >= 'a' && c <= 'f')
  2098. return c - 'a' + 10;
  2099. if (c >= 'A' && c <= 'F')
  2100. return c - 'A' + 10;
  2101. return -1;
  2102. }
  2103. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2104. {
  2105. unsigned int i;
  2106. for (i = 0; color_names[i].name; i++) {
  2107. unsigned int j, n = s_len(color_names[i].name);
  2108. if (n != len)
  2109. continue;
  2110. for (j = 0; j < n; j++) {
  2111. char c = s[j];
  2112. if (c >= 'A' && c <= 'Z')
  2113. c = (char)(c - 'A' + 'a');
  2114. if (c != color_names[i].name[j])
  2115. break;
  2116. }
  2117. if (j == n) {
  2118. *out = color_names[i].rgb;
  2119. return 1;
  2120. }
  2121. }
  2122. return 0;
  2123. }
  2124. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2125. * over white; the render target is a white page. */
  2126. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2127. {
  2128. unsigned int i = 0;
  2129. while (i < len && is_space(s[i]))
  2130. i++;
  2131. if (i < len && s[i] == '#') {
  2132. int d[6], nd = 0;
  2133. i++;
  2134. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2135. d[nd++] = hexval(s[i++]);
  2136. if (nd == 3) {
  2137. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2138. unsigned int b = (unsigned int)d[2];
  2139. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2140. ((g * 17 >> 2) << 5) |
  2141. (b * 17 >> 3));
  2142. return 1;
  2143. }
  2144. if (nd >= 6) {
  2145. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2146. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2147. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2148. *out = (unsigned short)(((r >> 3) << 11) |
  2149. ((g >> 2) << 5) | (b >> 3));
  2150. return 1;
  2151. }
  2152. return 0;
  2153. }
  2154. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2155. unsigned int v[3], k = 0, a = 255;
  2156. while (i < len && s[i] != '(')
  2157. i++;
  2158. if (i >= len)
  2159. return 0;
  2160. i++;
  2161. while (k < 3 && i < len) {
  2162. unsigned int x = 0;
  2163. int percent = 0;
  2164. while (i < len && (is_space(s[i]) || s[i] == ','))
  2165. i++;
  2166. if (i >= len || s[i] < '0' || s[i] > '9')
  2167. return 0;
  2168. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2169. x = x * 10 + (unsigned int)(s[i] - '0');
  2170. i++;
  2171. }
  2172. if (i < len && s[i] == '%') {
  2173. percent = 1;
  2174. i++;
  2175. }
  2176. if (percent)
  2177. x = x * 255 / 100;
  2178. if (x > 255)
  2179. x = 255;
  2180. v[k++] = x;
  2181. }
  2182. /* Optional alpha in both syntaxes:
  2183. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2184. {
  2185. unsigned int whole = 0, frac = 0, scale = 1;
  2186. int percent = 0;
  2187. while (i < len && (is_space(s[i]) || s[i] == ','))
  2188. i++;
  2189. if (i < len && s[i] == '/') {
  2190. i++;
  2191. while (i < len && is_space(s[i]))
  2192. i++;
  2193. }
  2194. if (i < len && s[i] >= '0' && s[i] <= '9') {
  2195. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2196. whole = whole * 10 +
  2197. (unsigned int)(s[i] - '0');
  2198. i++;
  2199. }
  2200. if (i < len && s[i] == '.') {
  2201. i++;
  2202. while (i < len && s[i] >= '0' &&
  2203. s[i] <= '9') {
  2204. if (scale < 1000) {
  2205. frac = frac * 10 +
  2206. (unsigned int)(s[i] - '0');
  2207. scale *= 10;
  2208. }
  2209. i++;
  2210. }
  2211. }
  2212. while (i < len && is_space(s[i]))
  2213. i++;
  2214. if (i < len && s[i] == '%')
  2215. percent = 1;
  2216. if (percent)
  2217. a = whole > 100 ? 255 :
  2218. whole * 255 / 100;
  2219. else if (whole >= 1)
  2220. a = 255;
  2221. else
  2222. a = frac * 255 / scale;
  2223. }
  2224. }
  2225. if (k == 3) {
  2226. unsigned int r = v[0], g = v[1], b = v[2];
  2227. if (a < 255) {
  2228. r = (r * a + 255u * (255u - a)) / 255u;
  2229. g = (g * a + 255u * (255u - a)) / 255u;
  2230. b = (b * a + 255u * (255u - a)) / 255u;
  2231. }
  2232. *out = (unsigned short)(((r >> 3) << 11) |
  2233. ((g >> 2) << 5) | (b >> 3));
  2234. return 1;
  2235. }
  2236. return 0;
  2237. }
  2238. {
  2239. unsigned int j = i;
  2240. while (i < len && !is_space(s[i]))
  2241. i++;
  2242. return named_color(s + j, i - j, out);
  2243. }
  2244. }
  2245. static int register_bg_url(const char *s, unsigned int len)
  2246. {
  2247. unsigned int i, j, n;
  2248. while (len && is_space(*s)) {
  2249. s++;
  2250. len--;
  2251. }
  2252. while (len && is_space(s[len - 1]))
  2253. len--;
  2254. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2255. s[len - 1] == s[0]) {
  2256. s++;
  2257. len -= 2;
  2258. }
  2259. if (len > 191)
  2260. len = 191;
  2261. for (i = 0; i < bg_url_count; i++) {
  2262. for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
  2263. ;
  2264. if (j == len && bg_urls[i][j] == '\0')
  2265. return (int)i + 1;
  2266. }
  2267. if (bg_url_count >= BR_BG_URLS || len == 0)
  2268. return 0;
  2269. n = len < 191 ? len : 191;
  2270. for (i = 0; i < n; i++)
  2271. bg_urls[bg_url_count][i] = s[i];
  2272. bg_urls[bg_url_count][n] = '\0';
  2273. return (int)(++bg_url_count);
  2274. }
  2275. /* Length values for margins/padding: px, ignore percentages/units. */
  2276. static unsigned int viewport_w;
  2277. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2278. {
  2279. unsigned int i = *pos;
  2280. int sign = 1;
  2281. long v = 0; /* hundredths of a unit before conversion */
  2282. int digits = 0, frac = 0, unit = 0;
  2283. while (i < len && is_space(s[i]))
  2284. i++;
  2285. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2286. if (s[i] == '-')
  2287. sign = -1;
  2288. i++;
  2289. }
  2290. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2291. v = v * 10 + (s[i] - '0');
  2292. digits = 1;
  2293. i++;
  2294. }
  2295. if (i < len && s[i] == '.') {
  2296. i++;
  2297. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2298. if (frac < 2) {
  2299. v = v * 10 + (s[i] - '0');
  2300. frac++;
  2301. }
  2302. i++;
  2303. }
  2304. }
  2305. while (frac < 2) {
  2306. v *= 10;
  2307. frac++;
  2308. }
  2309. while (frac > 2) {
  2310. v /= 10;
  2311. frac--;
  2312. }
  2313. if (i < len && s[i] == '%')
  2314. unit = 2;
  2315. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2316. s[i + 2] == 'm')
  2317. unit = 1;
  2318. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2319. unit = 3;
  2320. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2321. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2322. s[i] == 'a' || s[i] == 'd'))
  2323. i++;
  2324. *pos = i;
  2325. if (!digits)
  2326. return 0;
  2327. if (unit == 1)
  2328. v *= root_font_px;
  2329. else if (unit == 3)
  2330. v *= root_font_px;
  2331. else if (unit == 2)
  2332. v = v * (long)viewport_w / 100;
  2333. v /= 100;
  2334. v *= sign;
  2335. if (v < -2048)
  2336. v = -2048;
  2337. if (v > 2048)
  2338. v = 2048;
  2339. return (int)v;
  2340. }
  2341. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2342. * treats it as a zero-length token; margins need to keep it so block layout
  2343. * can centre a box with a definite width. */
  2344. #define CSS_LEN_AUTO (-32768)
  2345. /* A box length with its unit preserved for layout-time resolution:
  2346. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2347. * rem and whole pixels for px. */
  2348. static int parse_css_len(const char *s, unsigned int len,
  2349. unsigned int *pos, int *unit)
  2350. {
  2351. unsigned int i = *pos;
  2352. int sign = 1;
  2353. long v = 0;
  2354. int digits = 0, frac = 0;
  2355. *unit = 0;
  2356. while (i < len && is_space(s[i]))
  2357. i++;
  2358. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2359. if (s[i] == '-')
  2360. sign = -1;
  2361. i++;
  2362. }
  2363. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2364. v = v * 10 + (s[i] - '0');
  2365. digits = 1;
  2366. i++;
  2367. }
  2368. if (i < len && s[i] == '.') {
  2369. i++;
  2370. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2371. if (frac < 2) {
  2372. v = v * 10 + (s[i] - '0');
  2373. frac++;
  2374. }
  2375. i++;
  2376. }
  2377. }
  2378. while (frac < 2) {
  2379. v *= 10;
  2380. frac++;
  2381. }
  2382. if (!digits) {
  2383. *pos = i;
  2384. return 0;
  2385. }
  2386. if (i < len && s[i] == '%') {
  2387. *unit = 1;
  2388. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2389. s[i + 2] == 'm') {
  2390. *unit = 3;
  2391. i += 3;
  2392. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2393. *unit = 2;
  2394. i += 2;
  2395. } else {
  2396. v /= 100;
  2397. }
  2398. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2399. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2400. s[i] == 'a' || s[i] == 'd'))
  2401. i++;
  2402. *pos = i;
  2403. v *= sign;
  2404. if (v < -32768)
  2405. v = -32768;
  2406. if (v > 32767)
  2407. v = 32767;
  2408. return (int)v;
  2409. }
  2410. static int parse_margin_len(const char *s, unsigned int len,
  2411. unsigned int *pos, int *unit)
  2412. {
  2413. unsigned int i = *pos;
  2414. while (i < len && is_space(s[i]))
  2415. i++;
  2416. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2417. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2418. *pos = i + 4;
  2419. *unit = 4;
  2420. return CSS_LEN_AUTO;
  2421. }
  2422. return parse_css_len(s, len, pos, unit);
  2423. }
  2424. /* calc(P% +/- Npx) for the common width/margin forms. */
  2425. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2426. {
  2427. unsigned int i = 0;
  2428. int sign = 1;
  2429. int have = 0;
  2430. *pct = 0;
  2431. *px = 0;
  2432. while (i < len) {
  2433. unsigned int j;
  2434. long whole = 0, frac = 0, scale = 1;
  2435. int digits = 0;
  2436. while (i < len && is_space(s[i]))
  2437. i++;
  2438. if (i >= len)
  2439. break;
  2440. if (s[i] == '+' || s[i] == '-') {
  2441. sign = s[i] == '-' ? -1 : 1;
  2442. i++;
  2443. continue;
  2444. }
  2445. if (s[i] < '0' || s[i] > '9') {
  2446. if (s[i] == '.') {
  2447. /* fall through to the number parser */
  2448. } else {
  2449. i++;
  2450. continue;
  2451. }
  2452. }
  2453. j = i;
  2454. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2455. whole = whole * 10 + (s[j] - '0');
  2456. digits = 1;
  2457. j++;
  2458. }
  2459. if (j < len && s[j] == '.') {
  2460. j++;
  2461. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2462. frac = frac * 10 + (s[j] - '0');
  2463. scale *= 10;
  2464. j++;
  2465. }
  2466. }
  2467. if (!digits) {
  2468. i = j + 1;
  2469. continue;
  2470. }
  2471. while (j < len && is_space(s[j]))
  2472. j++;
  2473. if (j < len && s[j] == '%') {
  2474. *pct = sign * (int)(whole + (scale > 1 ?
  2475. frac * 100 / scale : 0));
  2476. have = 1;
  2477. j++;
  2478. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2479. *px = sign * (int)whole;
  2480. have = 1;
  2481. j += 2;
  2482. }
  2483. i = j;
  2484. while (i < len && !is_space(s[i]))
  2485. i++;
  2486. sign = 1;
  2487. }
  2488. return have;
  2489. }
  2490. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2491. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2492. unsigned char *kind)
  2493. {
  2494. unsigned int i = 0;
  2495. unsigned long v = 0;
  2496. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2497. *val = 0;
  2498. *kind = 0;
  2499. while (i < len && is_space(s[i]))
  2500. i++;
  2501. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2502. return 0;
  2503. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2504. return 0;
  2505. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2506. v = v * 10 + (unsigned int)(s[i] - '0');
  2507. digits = 1;
  2508. i++;
  2509. }
  2510. if (i < len && s[i] == '.') {
  2511. i++;
  2512. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2513. if (frac < 2) {
  2514. v = v * 10 + (unsigned int)(s[i] - '0');
  2515. frac++;
  2516. }
  2517. i++;
  2518. }
  2519. }
  2520. while (frac < 2) {
  2521. v *= 10;
  2522. frac++;
  2523. }
  2524. while (frac > 2) {
  2525. v /= 10;
  2526. frac--;
  2527. }
  2528. if (!digits)
  2529. return 0;
  2530. while (i < len && is_space(s[i]))
  2531. i++;
  2532. if (i < len && s[i] == '%')
  2533. pct = 1;
  2534. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2535. s[i + 2] == 'm')
  2536. rem = 1;
  2537. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2538. rem = 2;
  2539. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2540. vw = 1;
  2541. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2542. ; /* px: already the default scale */
  2543. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2544. bad_unit = 1;
  2545. if (bad_unit)
  2546. return 0;
  2547. if (rem == 1 || rem == 2) {
  2548. if (v > 65535)
  2549. v = 65535;
  2550. *val = (unsigned short)v;
  2551. *kind = rem == 1 ? 5 : 4;
  2552. return 1;
  2553. }
  2554. v /= 100;
  2555. if (vw)
  2556. v = v * viewport_w / 100;
  2557. if (pct) {
  2558. if (v > 100)
  2559. v = 100;
  2560. *kind = 2;
  2561. } else {
  2562. if (v > 2048)
  2563. v = 2048;
  2564. *kind = 1;
  2565. }
  2566. *val = (unsigned short)v;
  2567. return 1;
  2568. }
  2569. static int parse_font_px(const char *s, unsigned int len)
  2570. {
  2571. unsigned int i = 0;
  2572. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2573. int v1000, px;
  2574. int is_px = 1, is_rem = 0, is_pct = 0;
  2575. while (i < len && is_space(s[i]))
  2576. i++;
  2577. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2578. return 24;
  2579. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2580. return 32;
  2581. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2582. return 14;
  2583. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2584. whole = whole * 10 + (s[i] - '0');
  2585. digits = 1;
  2586. i++;
  2587. }
  2588. if (i < len && s[i] == '.') {
  2589. i++;
  2590. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2591. if (fscale < 1000) {
  2592. frac = frac * 10 + (s[i] - '0');
  2593. fscale *= 10;
  2594. }
  2595. i++;
  2596. }
  2597. }
  2598. if (!digits)
  2599. return 0;
  2600. while (i < len && is_space(s[i]))
  2601. i++;
  2602. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2603. is_rem = 1;
  2604. is_px = 0;
  2605. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2606. is_rem = 1;
  2607. is_px = 0;
  2608. } else if (i < len && s[i] == '%') {
  2609. is_pct = 1;
  2610. is_px = 0;
  2611. }
  2612. v1000 = whole * 1000 + frac * 1000 / fscale;
  2613. if (is_rem)
  2614. px = v1000 * root_font_px / 1000;
  2615. else if (is_pct)
  2616. px = v1000 * root_font_px / 100000;
  2617. else if (is_px)
  2618. px = v1000 / 1000;
  2619. else
  2620. px = v1000 / 1000;
  2621. if (px < 8)
  2622. px = 8;
  2623. if (px > 72)
  2624. px = 72;
  2625. return px;
  2626. }
  2627. /* font-size keeping the unit so em/% resolve against the parent face. */
  2628. static int parse_font_size(const char *s, unsigned int len,
  2629. unsigned short *raw, unsigned char *unit)
  2630. {
  2631. unsigned int i = 0;
  2632. int whole = 0, frac = 0, scale = 1, digits = 0;
  2633. int v100;
  2634. *raw = 0;
  2635. *unit = 0;
  2636. while (i < len && is_space(s[i]))
  2637. i++;
  2638. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2639. *raw = 24;
  2640. *unit = 1;
  2641. return 1;
  2642. }
  2643. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2644. *raw = 32;
  2645. *unit = 1;
  2646. return 1;
  2647. }
  2648. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2649. *raw = 14;
  2650. *unit = 1;
  2651. return 1;
  2652. }
  2653. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2654. whole = whole * 10 + (s[i] - '0');
  2655. digits = 1;
  2656. i++;
  2657. }
  2658. if (i < len && s[i] == '.') {
  2659. i++;
  2660. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2661. frac = frac * 10 + (s[i] - '0');
  2662. scale *= 10;
  2663. i++;
  2664. }
  2665. }
  2666. if (!digits && !frac)
  2667. return 0;
  2668. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2669. while (i < len && is_space(s[i]))
  2670. i++;
  2671. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2672. *raw = (unsigned short)v100;
  2673. *unit = 3;
  2674. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2675. *raw = (unsigned short)v100;
  2676. *unit = 2;
  2677. } else if (i < len && s[i] == '%') {
  2678. *raw = (unsigned short)(v100 / 100);
  2679. *unit = 4;
  2680. } else {
  2681. *raw = (unsigned short)(v100 / 100);
  2682. *unit = 1;
  2683. }
  2684. return 1;
  2685. }
  2686. static int parse_line_height(const char *s, unsigned int len,
  2687. unsigned short *px, unsigned char *pct)
  2688. {
  2689. unsigned int i = 0;
  2690. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2691. int v1000;
  2692. int is_px = 0, is_pct = 0;
  2693. *px = 0;
  2694. *pct = 0;
  2695. while (i < len && is_space(s[i]))
  2696. i++;
  2697. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2698. return 0;
  2699. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2700. whole = whole * 10 + (s[i] - '0');
  2701. digits = 1;
  2702. i++;
  2703. }
  2704. if (i < len && s[i] == '.') {
  2705. i++;
  2706. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2707. frac = frac * 10 + (s[i] - '0');
  2708. fscale *= 10;
  2709. i++;
  2710. }
  2711. }
  2712. if (!digits)
  2713. return 0;
  2714. while (i < len && is_space(s[i]))
  2715. i++;
  2716. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2717. is_px = 1;
  2718. else if (i < len && s[i] == '%')
  2719. is_pct = 1;
  2720. v1000 = whole * 1000 + frac * 1000 / fscale;
  2721. if (is_px) {
  2722. int v = v1000 / 1000;
  2723. if (v < 8)
  2724. v = 8;
  2725. if (v > 160)
  2726. v = 160;
  2727. *px = (unsigned short)v;
  2728. } else {
  2729. int v = v1000 / 10; /* unitless multiplier -> percent */
  2730. if (is_pct)
  2731. v = v1000 / 1000;
  2732. if (v < 50)
  2733. v = 50;
  2734. if (v > 400)
  2735. v = 400;
  2736. *pct = (unsigned char)v;
  2737. }
  2738. return 1;
  2739. }
  2740. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2741. {
  2742. unsigned int i, j, n = s_len(needle);
  2743. for (i = 0; i + n <= len; i++) {
  2744. for (j = 0; j < n; j++) {
  2745. char a = s[i + j];
  2746. if (a >= 'A' && a <= 'Z')
  2747. a = (char)(a - 'A' + 'a');
  2748. if (a != needle[j])
  2749. break;
  2750. }
  2751. if (j == n)
  2752. return 1;
  2753. }
  2754. return 0;
  2755. }
  2756. /* Pull a colour out of a border/background shorthand. */
  2757. /* Background shorthands can hold several layers before the final solid
  2758. * colour. The colour is conventionally the last token (e.g. a stack of
  2759. * gradients followed by var(--bg)), so take the rightmost colour rather
  2760. * than the first gradient stop. */
  2761. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2762. {
  2763. int last = -1;
  2764. unsigned int i;
  2765. for (i = 0; i < len; i++) {
  2766. if (s[i] == '#')
  2767. last = (int)i;
  2768. else if ((s[i] == 'r' || s[i] == 'R') &&
  2769. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2770. last = (int)i;
  2771. }
  2772. if (last >= 0)
  2773. return parse_color(s + last, len - (unsigned int)last, c);
  2774. while (i > 0) {
  2775. unsigned int st, e;
  2776. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2777. s[i - 1] == ')' || s[i - 1] == '('))
  2778. i--;
  2779. e = i;
  2780. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  2781. s[i - 1] != ')' && s[i - 1] != '(')
  2782. i--;
  2783. st = i;
  2784. if (e > st && parse_color(s + st, e - st, c))
  2785. return 1;
  2786. }
  2787. return 0;
  2788. }
  2789. static int parse_display(const char *s, unsigned int len)
  2790. {
  2791. unsigned int i;
  2792. while (len && is_space(s[0])) { s++; len--; }
  2793. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  2794. ;
  2795. if (i == 4 && ci_eq_n(s, "none", 4))
  2796. return BR_DISPLAY_NONE;
  2797. if (i == 5 && ci_eq_n(s, "block", 5))
  2798. return BR_DISPLAY_BLOCK;
  2799. if (i == 6 && ci_eq_n(s, "inline", 6))
  2800. return BR_DISPLAY_INLINE;
  2801. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  2802. return BR_DISPLAY_INLINE_BLOCK;
  2803. if (i == 10 && ci_eq_n(s, "list-item", 10))
  2804. return BR_DISPLAY_BLOCK;
  2805. if (i == 4 && ci_eq_n(s, "flex", 4))
  2806. return BR_DISPLAY_FLEX;
  2807. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  2808. return BR_DISPLAY_FLEX;
  2809. if (i == 5 && ci_eq_n(s, "table", 5))
  2810. return BR_DISPLAY_BLOCK;
  2811. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  2812. return BR_DISPLAY_BLOCK;
  2813. /* Grid shells are common on news/wiki pages, but the two-column
  2814. * heuristic is not reliable for their nested area templates yet.
  2815. * Collapse them to block flow so the content stays readable. */
  2816. if (i == 4 && ci_eq_n(s, "grid", 4))
  2817. return BR_DISPLAY_BLOCK;
  2818. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  2819. return BR_DISPLAY_BLOCK;
  2820. return BR_DISPLAY_INLINE;
  2821. }
  2822. static void decl_add(struct br_decl *out, unsigned int *nout,
  2823. unsigned char prop, int val, int important)
  2824. {
  2825. if (*nout >= BR_DECLS)
  2826. return;
  2827. out[*nout].prop = prop;
  2828. out[*nout].val = (short)val;
  2829. out[*nout].important = (unsigned char)(important != 0);
  2830. (*nout)++;
  2831. }
  2832. /* One whitespace-separated token. Returns 0 at the end of the value. */
  2833. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  2834. unsigned int *start, unsigned int *end)
  2835. {
  2836. unsigned int i = *pos;
  2837. while (i < len && is_space(s[i]))
  2838. i++;
  2839. if (i >= len)
  2840. return 0;
  2841. *start = i;
  2842. while (i < len && !is_space(s[i]))
  2843. i++;
  2844. *end = i;
  2845. *pos = i;
  2846. return 1;
  2847. }
  2848. static int parse_len_token(const char *s, unsigned int len, int *out)
  2849. {
  2850. unsigned int pos = 0;
  2851. int v;
  2852. if (!len)
  2853. return 0;
  2854. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  2855. return 0;
  2856. v = parse_len(s, len, &pos);
  2857. if (pos == 0)
  2858. return 0;
  2859. *out = v;
  2860. return 1;
  2861. }
  2862. static void expand_four(int v[4], int n)
  2863. {
  2864. if (n == 1) {
  2865. v[1] = v[2] = v[3] = v[0];
  2866. } else if (n == 2) {
  2867. v[2] = v[0];
  2868. v[3] = v[1];
  2869. } else if (n == 3) {
  2870. v[3] = v[1];
  2871. }
  2872. }
  2873. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  2874. * the input is copied verbatim when it contains no var(). */
  2875. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  2876. unsigned int len, char *out,
  2877. unsigned int cap, int depth)
  2878. {
  2879. unsigned int i = 0, n = 0;
  2880. if (depth > 8)
  2881. return 0;
  2882. while (i < len && n + 1 < cap) {
  2883. unsigned int p, inner, close, comma, nlen, fstart;
  2884. char quote;
  2885. int parens;
  2886. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  2887. out[n++] = s[i++];
  2888. continue;
  2889. }
  2890. p = i + 4;
  2891. quote = 0;
  2892. parens = 1;
  2893. while (p < len && parens) {
  2894. char c = s[p];
  2895. if (quote) {
  2896. if (c == quote)
  2897. quote = 0;
  2898. else if (c == '\\' && p + 1 < len)
  2899. p++;
  2900. } else if (c == '"' || c == '\'') {
  2901. quote = c;
  2902. } else if (c == '(') {
  2903. parens++;
  2904. } else if (c == ')') {
  2905. parens--;
  2906. }
  2907. p++;
  2908. }
  2909. if (parens) {
  2910. out[n++] = s[i++];
  2911. continue;
  2912. }
  2913. close = p - 1;
  2914. inner = i + 4;
  2915. while (inner < close && is_space(s[inner]))
  2916. inner++;
  2917. comma = inner;
  2918. parens = 0;
  2919. quote = 0;
  2920. while (comma < close) {
  2921. char c = s[comma];
  2922. if (quote) {
  2923. if (c == quote)
  2924. quote = 0;
  2925. } else if (c == '"' || c == '\'') {
  2926. quote = c;
  2927. } else if (c == '(') {
  2928. parens++;
  2929. } else if (c == ')' && parens > 0) {
  2930. parens--;
  2931. } else if (c == ',' && parens == 0) {
  2932. break;
  2933. }
  2934. comma++;
  2935. }
  2936. nlen = comma - inner;
  2937. while (nlen && is_space(s[inner + nlen - 1]))
  2938. nlen--;
  2939. fstart = comma < close ? comma + 1 : close;
  2940. while (fstart < close && is_space(s[fstart]))
  2941. fstart++;
  2942. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  2943. unsigned int vi = css_var_find(d, s + inner, nlen);
  2944. if (vi < BR_MAX_VARS) {
  2945. const char *v = attr_ptr(d, d->var_val[vi]);
  2946. unsigned int vn = css_expand_vars(d, v, s_len(v),
  2947. out + n,
  2948. cap - n, depth + 1);
  2949. n += vn;
  2950. } else if (fstart < close) {
  2951. unsigned int vn = css_expand_vars(d, s + fstart,
  2952. close - fstart,
  2953. out + n, cap - n,
  2954. depth + 1);
  2955. n += vn;
  2956. }
  2957. } else if (fstart < close) {
  2958. unsigned int vn = css_expand_vars(d, s + fstart,
  2959. close - fstart,
  2960. out + n, cap - n,
  2961. depth + 1);
  2962. n += vn;
  2963. }
  2964. i = p;
  2965. }
  2966. out[n] = '\0';
  2967. return n;
  2968. }
  2969. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  2970. unsigned int i)
  2971. {
  2972. while (i < len && (is_space(s[i]) || s[i] == ','))
  2973. i++;
  2974. return i;
  2975. }
  2976. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  2977. * argument). Returns 1 on success. */
  2978. static int parse_grid_track(const char *s, unsigned int len,
  2979. unsigned int *pp, unsigned short *val,
  2980. unsigned char *kind)
  2981. {
  2982. unsigned int i = grid_skip_sep(s, len, *pp);
  2983. unsigned int whole = 0, frac = 0, scale = 1;
  2984. int any = 0;
  2985. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  2986. unsigned int j = i + 7;
  2987. while (j < len && s[j] != ',')
  2988. j++;
  2989. if (j >= len)
  2990. return 0;
  2991. i = j + 1;
  2992. while (i < len && is_space(s[i]))
  2993. i++;
  2994. }
  2995. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  2996. int depth = 1;
  2997. i += 7;
  2998. while (i < len && depth) {
  2999. if (s[i] == '(')
  3000. depth++;
  3001. else if (s[i] == ')')
  3002. depth--;
  3003. i++;
  3004. }
  3005. *pp = i;
  3006. return 0;
  3007. }
  3008. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3009. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3010. i++;
  3011. any = 1;
  3012. }
  3013. if (i < len && s[i] == '.') {
  3014. i++;
  3015. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3016. if (scale < 1000) {
  3017. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3018. scale *= 10;
  3019. }
  3020. i++;
  3021. any = 1;
  3022. }
  3023. }
  3024. if (!any) {
  3025. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3026. *val = 1;
  3027. *kind = 3;
  3028. *pp = i + 4;
  3029. return 1;
  3030. }
  3031. return 0;
  3032. }
  3033. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3034. *val = (unsigned short)(whole ? whole : 1);
  3035. *kind = 3;
  3036. i += 2;
  3037. } else if (i < len && s[i] == '%') {
  3038. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3039. *kind = 2;
  3040. i++;
  3041. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3042. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3043. *kind = 1;
  3044. i += 3;
  3045. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3046. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3047. *kind = 1;
  3048. i += 2;
  3049. } else {
  3050. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3051. *kind = 1;
  3052. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3053. i += 2;
  3054. }
  3055. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3056. i++;
  3057. *pp = i;
  3058. return 1;
  3059. }
  3060. static int parse_grid_cols(const char *s, unsigned int len,
  3061. unsigned short val[2], unsigned char kind[2])
  3062. {
  3063. unsigned int pos = 0;
  3064. int n = 0;
  3065. while (n < 2) {
  3066. unsigned int before = pos;
  3067. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3068. break;
  3069. n++;
  3070. if (pos <= before)
  3071. break;
  3072. while (pos < len && is_space(s[pos]))
  3073. pos++;
  3074. if (pos >= len)
  3075. break;
  3076. }
  3077. return n;
  3078. }
  3079. static int parse_grid_slot(const char *s, unsigned int len)
  3080. {
  3081. if (contains_ci_len(s, len, "footer") ||
  3082. contains_ci_len(s, len, "header") ||
  3083. contains_ci_len(s, len, "sitenotice") ||
  3084. contains_ci_len(s, len, "navbar"))
  3085. return 1;
  3086. if (contains_ci_len(s, len, "columnstart") ||
  3087. contains_ci_len(s, len, "sidebar") ||
  3088. contains_ci_len(s, len, "left") ||
  3089. contains_ci_len(s, len, "start"))
  3090. return 2;
  3091. if (contains_ci_len(s, len, "pagecontent") ||
  3092. contains_ci_len(s, len, "content") ||
  3093. contains_ci_len(s, len, "main") ||
  3094. contains_ci_len(s, len, "right") ||
  3095. contains_ci_len(s, len, "end"))
  3096. return 3;
  3097. return 0;
  3098. }
  3099. static int css_has_var(const char *s, unsigned int len)
  3100. {
  3101. unsigned int i;
  3102. for (i = 0; i + 4 <= len; i++)
  3103. if ((s[i] == 'v' || s[i] == 'V') &&
  3104. ci_eq_n(s + i, "var(", 4))
  3105. return 1;
  3106. return 0;
  3107. }
  3108. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3109. unsigned int i)
  3110. {
  3111. i += 2;
  3112. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3113. i++;
  3114. if (i + 1 < len)
  3115. i += 2;
  3116. else
  3117. i = len;
  3118. return i;
  3119. }
  3120. static unsigned int css_skip_string(const char *s, unsigned int len,
  3121. unsigned int i)
  3122. {
  3123. char q = s[i++];
  3124. while (i < len && s[i] != q) {
  3125. if (s[i] == '\\' && i + 1 < len)
  3126. i++;
  3127. i++;
  3128. }
  3129. if (i < len)
  3130. i++;
  3131. return i;
  3132. }
  3133. /* Find the '}' that closes a declaration block. i points just past the
  3134. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3135. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3136. * selectors such as "Highlight-Stories {" without a closing brace). */
  3137. static unsigned int css_block_end(const char *s, unsigned int len,
  3138. unsigned int i)
  3139. {
  3140. int depth = 0;
  3141. while (i < len) {
  3142. char c = s[i];
  3143. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3144. i = css_skip_comment(s, len, i);
  3145. continue;
  3146. }
  3147. if (c == '"' || c == '\'') {
  3148. i = css_skip_string(s, len, i);
  3149. continue;
  3150. }
  3151. if (c == '{') {
  3152. depth++;
  3153. } else if (c == '}') {
  3154. if (depth == 0)
  3155. return i;
  3156. depth--;
  3157. }
  3158. i++;
  3159. }
  3160. return len;
  3161. }
  3162. /* Skip a block whose opening '{' has already been consumed. */
  3163. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3164. unsigned int i)
  3165. {
  3166. int depth = 1;
  3167. while (i < len && depth) {
  3168. char c = s[i];
  3169. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3170. i = css_skip_comment(s, len, i);
  3171. continue;
  3172. }
  3173. if (c == '"' || c == '\'') {
  3174. i = css_skip_string(s, len, i);
  3175. continue;
  3176. }
  3177. if (c == '{')
  3178. depth++;
  3179. else if (c == '}')
  3180. depth--;
  3181. i++;
  3182. }
  3183. return i;
  3184. }
  3185. /* ---- @font-face ------------------------------------------------------- */
  3186. static void font_trim(const char **sp, unsigned int *len)
  3187. {
  3188. const char *s = *sp;
  3189. unsigned int n = *len;
  3190. while (n && is_space(*s)) {
  3191. s++;
  3192. n--;
  3193. }
  3194. while (n && is_space(s[n - 1]))
  3195. n--;
  3196. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3197. s++;
  3198. n -= 2;
  3199. }
  3200. *sp = s;
  3201. *len = n;
  3202. }
  3203. static void font_add(const char *family, unsigned int flen,
  3204. const char *url, unsigned int ulen)
  3205. {
  3206. unsigned int i, n = 0, k;
  3207. font_trim(&family, &flen);
  3208. font_trim(&url, &ulen);
  3209. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3210. return;
  3211. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3212. struct br_font_def *f = &font_defs[i];
  3213. if (s_len(f->family) != flen)
  3214. continue;
  3215. for (k = 0; k < flen; k++) {
  3216. char a = f->family[k], b = family[k];
  3217. if (a >= 'A' && a <= 'Z')
  3218. a = (char)(a - 'A' + 'a');
  3219. if (b >= 'A' && b <= 'Z')
  3220. b = (char)(b - 'A' + 'a');
  3221. if (a != b)
  3222. break;
  3223. }
  3224. if (k != flen)
  3225. continue;
  3226. n = 0;
  3227. while (url[n] && n + 1 < sizeof(f->url)) {
  3228. f->url[n] = url[n];
  3229. n++;
  3230. }
  3231. f->url[n] = '\0';
  3232. n = 0;
  3233. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3234. f->base[n] = font_base[n];
  3235. n++;
  3236. }
  3237. f->base[n] = '\0';
  3238. return;
  3239. }
  3240. if (font_face_count >= BR_MAX_FACES)
  3241. return;
  3242. {
  3243. struct br_font_def *f = &font_defs[font_face_count];
  3244. for (n = 0; n < flen; n++)
  3245. f->family[n] = family[n];
  3246. f->family[n] = '\0';
  3247. for (n = 0; n < ulen; n++)
  3248. f->url[n] = url[n];
  3249. f->url[n] = '\0';
  3250. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  3251. f->base[n] = font_base[n];
  3252. f->base[n] = '\0';
  3253. f->used = 1;
  3254. font_face_count++;
  3255. }
  3256. }
  3257. /* Look up the first name in a font-family list that has an @font-face. */
  3258. static int font_family_id(const char *s, unsigned int len)
  3259. {
  3260. unsigned int i = 0;
  3261. while (i < len) {
  3262. unsigned int start, end, q = 0;
  3263. while (i < len && (is_space(s[i]) || s[i] == ','))
  3264. i++;
  3265. if (i >= len)
  3266. break;
  3267. if (s[i] == '"' || s[i] == '\'') {
  3268. char quote = s[i++];
  3269. start = i;
  3270. while (i < len && s[i] != quote)
  3271. i++;
  3272. end = i;
  3273. if (i < len)
  3274. i++;
  3275. } else {
  3276. start = i;
  3277. while (i < len && s[i] != ',')
  3278. i++;
  3279. end = i;
  3280. while (end > start && is_space(s[end - 1]))
  3281. end--;
  3282. }
  3283. if (end > start) {
  3284. for (q = 0; q < (unsigned int)font_face_count; q++) {
  3285. const char *f = font_defs[q].family;
  3286. unsigned int fl = s_len(f), k;
  3287. if (fl != end - start)
  3288. continue;
  3289. for (k = 0; k < fl; k++) {
  3290. char a = f[k], b = s[start + k];
  3291. if (a >= 'A' && a <= 'Z')
  3292. a = (char)(a - 'A' + 'a');
  3293. if (b >= 'A' && b <= 'Z')
  3294. b = (char)(b - 'A' + 'a');
  3295. if (a != b)
  3296. break;
  3297. }
  3298. if (k == fl)
  3299. return (int)q + 1;
  3300. }
  3301. }
  3302. }
  3303. return 0;
  3304. }
  3305. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  3306. * at-rules, so faces must be registered before any font-family declaration
  3307. * in the same sheet is resolved. */
  3308. static void scan_font_faces(const char *s, unsigned int len)
  3309. {
  3310. unsigned int i = 0;
  3311. while (i + 10 <= len) {
  3312. unsigned int j, end;
  3313. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  3314. unsigned int flen = 0, ulen = 0, p;
  3315. int have_family = 0, have_url = 0;
  3316. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  3317. i++;
  3318. continue;
  3319. }
  3320. j = i + 10;
  3321. while (j < len && s[j] != '{' && s[j] != ';')
  3322. j++;
  3323. if (j >= len || s[j] == ';') {
  3324. i = j + 1;
  3325. continue;
  3326. }
  3327. end = css_skip_nested(s, len, j + 1);
  3328. p = j + 1;
  3329. while (p < end) {
  3330. unsigned int ds, de, vs, ve;
  3331. while (p < end && (is_space(s[p]) || s[p] == ';'))
  3332. p++;
  3333. if (p >= end)
  3334. break;
  3335. ds = p;
  3336. while (p < end && s[p] != ':' && s[p] != ';')
  3337. p++;
  3338. de = p;
  3339. if (p >= end || s[p] != ':') {
  3340. while (p < end && s[p] != ';')
  3341. p++;
  3342. continue;
  3343. }
  3344. p++;
  3345. vs = p;
  3346. while (p < end && s[p] != ';')
  3347. p++;
  3348. ve = p;
  3349. while (ds < de && is_space(s[ds]))
  3350. ds++;
  3351. while (de > ds && is_space(s[de - 1]))
  3352. de--;
  3353. while (vs < ve && is_space(s[vs]))
  3354. vs++;
  3355. while (ve > vs && is_space(s[ve - 1]))
  3356. ve--;
  3357. if (de - ds == 11 &&
  3358. ci_eq_n(s + ds, "font-family", 11)) {
  3359. const char *fp = s + vs;
  3360. unsigned int fn = ve - vs;
  3361. font_trim(&fp, &fn);
  3362. if (fn && fn < sizeof(family)) {
  3363. unsigned int k;
  3364. for (k = 0; k < fn; k++)
  3365. family[k] = fp[k];
  3366. family[fn] = '\0';
  3367. flen = fn;
  3368. have_family = 1;
  3369. }
  3370. } else if (de - ds == 3 &&
  3371. ci_eq_n(s + ds, "src", 3)) {
  3372. unsigned int q = vs;
  3373. while (q + 4 <= ve) {
  3374. unsigned int us, ue;
  3375. if (!ci_eq_n(s + q, "url(", 4)) {
  3376. q++;
  3377. continue;
  3378. }
  3379. q += 4;
  3380. us = q;
  3381. while (q < ve && s[q] != ')')
  3382. q++;
  3383. ue = q;
  3384. {
  3385. const char *up = s + us;
  3386. unsigned int un = ue - us;
  3387. font_trim(&up, &un);
  3388. if (un && un < sizeof(url) &&
  3389. !contains_ci_len(up, un,
  3390. "woff")) {
  3391. unsigned int k;
  3392. for (k = 0; k < un; k++)
  3393. url[k] = up[k];
  3394. url[un] = '\0';
  3395. ulen = un;
  3396. have_url = 1;
  3397. break;
  3398. }
  3399. }
  3400. }
  3401. }
  3402. }
  3403. if (have_family && have_url)
  3404. font_add(family, flen, url, ulen);
  3405. i = end;
  3406. }
  3407. }
  3408. /* One border-radius length. Unlike parse_size(), percentages keep their
  3409. * kind: the drawer scales them to the box, not the viewport. */
  3410. static int parse_radius_value(const char *s, unsigned int len,
  3411. unsigned int *pos, unsigned short *val,
  3412. unsigned char *kind)
  3413. {
  3414. unsigned int i = *pos;
  3415. unsigned long v = 0;
  3416. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  3417. while (i < len && is_space(s[i]))
  3418. i++;
  3419. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3420. v = v * 10 + (unsigned int)(s[i] - '0');
  3421. digits = 1;
  3422. i++;
  3423. }
  3424. if (i < len && s[i] == '.') {
  3425. i++;
  3426. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3427. fdigits = 1;
  3428. if (frac < 2) {
  3429. v = v * 10 + (unsigned int)(s[i] - '0');
  3430. frac++;
  3431. }
  3432. i++;
  3433. }
  3434. }
  3435. while (frac < 2) {
  3436. v *= 10;
  3437. frac++;
  3438. }
  3439. while (i < len && is_space(s[i]))
  3440. i++;
  3441. if (i < len && s[i] == '%') {
  3442. pct = 1;
  3443. i++;
  3444. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  3445. s[i + 2] == 'm') {
  3446. rem = 1;
  3447. i += 3;
  3448. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  3449. rem = 2;
  3450. i += 2;
  3451. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  3452. i += 2;
  3453. } else if (i < len && is_ident(s[i])) {
  3454. return 0;
  3455. }
  3456. if (!digits && !fdigits)
  3457. return 0;
  3458. if (rem == 1)
  3459. v *= root_font_px;
  3460. else if (rem == 2)
  3461. v *= root_font_px;
  3462. v /= 100;
  3463. if (v > 2048)
  3464. v = 2048;
  3465. *val = (unsigned short)v;
  3466. *kind = pct ? 2 : 1;
  3467. *pos = i;
  3468. return 1;
  3469. }
  3470. /* font: [style] [variant] [weight] size[/line-height] family */
  3471. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  3472. const char *v, unsigned int len, int important)
  3473. {
  3474. unsigned int pos = 0, ts, te;
  3475. unsigned short size_raw = 0;
  3476. unsigned char size_unit = 0;
  3477. int have_size = 0;
  3478. int bold = 0, italic = 0;
  3479. int lh_px = 0, lh_pct = 0, mono = 0;
  3480. while (next_token(v, len, &pos, &ts, &te)) {
  3481. unsigned int tl = te - ts;
  3482. unsigned int slash = 0;
  3483. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  3484. bold = 1;
  3485. continue;
  3486. }
  3487. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  3488. continue;
  3489. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  3490. italic = 1;
  3491. continue;
  3492. }
  3493. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  3494. italic = 1;
  3495. continue;
  3496. }
  3497. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  3498. continue;
  3499. for (slash = 0; slash < tl; slash++)
  3500. if (v[ts + slash] == '/')
  3501. break;
  3502. if (slash == tl) {
  3503. unsigned int q;
  3504. int weight = 0;
  3505. for (q = 0; q < tl; q++) {
  3506. if (v[ts + q] < '0' || v[ts + q] > '9')
  3507. break;
  3508. weight = weight * 10 + (v[ts + q] - '0');
  3509. }
  3510. if (q == tl && weight >= 100 && weight <= 900) {
  3511. bold = weight >= 500;
  3512. continue;
  3513. }
  3514. }
  3515. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  3516. &size_raw, &size_unit))
  3517. continue;
  3518. have_size = 1;
  3519. if (slash < tl) {
  3520. unsigned int lstart = ts + slash + 1;
  3521. unsigned short lhp = 0;
  3522. unsigned char lhpct = 0;
  3523. if (!parse_line_height(v + lstart, te - lstart,
  3524. &lhp, &lhpct)) {
  3525. lh_px = 0;
  3526. lh_pct = 0;
  3527. } else {
  3528. lh_px = lhp;
  3529. lh_pct = lhpct;
  3530. }
  3531. }
  3532. }
  3533. if (!have_size)
  3534. return;
  3535. mono = contains_ci_len(v, len, "mono");
  3536. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  3537. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  3538. decl_add(out, nout, PROP_BOLD, bold, important);
  3539. decl_add(out, nout, PROP_ITALIC, italic, important);
  3540. decl_add(out, nout, PROP_MONO, mono, important);
  3541. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  3542. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  3543. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  3544. }
  3545. static void parse_decls(struct br_doc *d, const char *s,
  3546. unsigned int len, struct br_decl *out,
  3547. unsigned int *nout)
  3548. {
  3549. static char expanded[4096];
  3550. if (css_has_var(s, len)) {
  3551. unsigned int elen = css_expand_vars(d, s, len, expanded,
  3552. sizeof(expanded), 0);
  3553. if (elen) {
  3554. s = expanded;
  3555. len = elen;
  3556. }
  3557. }
  3558. unsigned int i = 0;
  3559. *nout = 0;
  3560. while (i < len && *nout < BR_DECLS) {
  3561. unsigned int pstart, pend, vstart, vend, k;
  3562. char prop[24];
  3563. int important = 0;
  3564. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3565. i++;
  3566. if (i >= len)
  3567. break;
  3568. /* A nested at-rule (@media/@supports) inside the declaration
  3569. * block carries its own selector context; skip it rather than
  3570. * reading its prelude as a declaration. */
  3571. if (s[i] == '@') {
  3572. while (i < len && s[i] != '{' && s[i] != ';')
  3573. i++;
  3574. if (i < len && s[i] == '{')
  3575. i = css_skip_nested(s, len, i + 1);
  3576. else if (i < len)
  3577. i++;
  3578. continue;
  3579. }
  3580. pstart = i;
  3581. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3582. i++;
  3583. if (i < len && s[i] == '{') {
  3584. /* Nested style rule: skip its block. */
  3585. i = css_skip_nested(s, len, i + 1);
  3586. continue;
  3587. }
  3588. pend = i;
  3589. if (i >= len || s[i] != ':')
  3590. continue;
  3591. i++;
  3592. vstart = i;
  3593. while (i < len && s[i] != ';')
  3594. i++;
  3595. vend = i;
  3596. while (pstart < pend && is_space(s[pstart]))
  3597. pstart++;
  3598. while (pend > pstart && is_space(s[pend - 1]))
  3599. pend--;
  3600. while (vstart < vend && is_space(s[vstart]))
  3601. vstart++;
  3602. while (vend > vstart && is_space(s[vend - 1]))
  3603. vend--;
  3604. if (vend - vstart >= 10 &&
  3605. ci_eq_n(s + vend - 10, "!important", 10)) {
  3606. important = 1;
  3607. vend -= 10;
  3608. while (vend > vstart && is_space(s[vend - 1]))
  3609. vend--;
  3610. }
  3611. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  3612. char c = s[pstart + k];
  3613. if (c >= 'A' && c <= 'Z')
  3614. c = (char)(c - 'A' + 'a');
  3615. prop[k] = c;
  3616. }
  3617. prop[k] = '\0';
  3618. if (str_eq(prop, "color")) {
  3619. unsigned short c;
  3620. if (parse_color(s + vstart, vend - vstart, &c))
  3621. decl_add(out, nout, PROP_COLOR, c, important);
  3622. } else if (str_eq(prop, "fill")) {
  3623. unsigned short c;
  3624. if (parse_color(s + vstart, vend - vstart, &c)) {
  3625. decl_add(out, nout, PROP_BG, c, important);
  3626. decl_add(out, nout, PROP_COLOR, c, important);
  3627. }
  3628. } else if (str_eq(prop, "stroke")) {
  3629. unsigned short c;
  3630. if (parse_color(s + vstart, vend - vstart, &c)) {
  3631. decl_add(out, nout, PROP_BORDER_COLOR, c,
  3632. important);
  3633. decl_add(out, nout, PROP_BORDER_W, 1, important);
  3634. }
  3635. } else if (str_eq(prop, "stroke-width")) {
  3636. unsigned int j = vstart;
  3637. int sw = 0;
  3638. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  3639. sw = sw * 10 + (s[j] - '0');
  3640. j++;
  3641. }
  3642. if (sw < 1)
  3643. sw = 1;
  3644. if (sw > 3)
  3645. sw = 3;
  3646. decl_add(out, nout, PROP_BORDER_W, sw, important);
  3647. } else if (str_eq(prop, "text-anchor")) {
  3648. int a = BR_ALIGN_LEFT;
  3649. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  3650. a = BR_ALIGN_CENTER;
  3651. else if (contains_ci_len(s + vstart, vend - vstart,
  3652. "end"))
  3653. a = BR_ALIGN_RIGHT;
  3654. decl_add(out, nout, PROP_ALIGN, a, important);
  3655. } else if (str_eq(prop, "background-image")) {
  3656. const char *v = s + vstart;
  3657. unsigned int vl = vend - vstart, i;
  3658. for (i = 0; i + 4 <= vl; i++)
  3659. if (ci_eq_n(v + i, "url(", 4))
  3660. break;
  3661. if (i + 4 < vl) {
  3662. unsigned int st = i + 4, en = st;
  3663. int idx;
  3664. while (en < vl && v[en] != ')')
  3665. en++;
  3666. idx = register_bg_url(v + st, en - st);
  3667. if (idx)
  3668. decl_add(out, nout, PROP_BG_IMAGE,
  3669. idx, important);
  3670. }
  3671. } else if (str_eq(prop, "background-color") ||
  3672. str_eq(prop, "background")) {
  3673. unsigned short c;
  3674. if (parse_color(s + vstart, vend - vstart, &c))
  3675. decl_add(out, nout, PROP_BG, c, important);
  3676. else if (str_eq(prop, "background") &&
  3677. find_color_last(s + vstart, vend - vstart, &c))
  3678. decl_add(out, nout, PROP_BG, c, important);
  3679. } else if (str_eq(prop, "font-size")) {
  3680. unsigned short raw;
  3681. unsigned char unit;
  3682. if (parse_font_size(s + vstart, vend - vstart,
  3683. &raw, &unit)) {
  3684. decl_add(out, nout, PROP_FONT_PX, raw,
  3685. important);
  3686. decl_add(out, nout, PROP_FONT_UNIT, unit,
  3687. important);
  3688. }
  3689. } else if (str_eq(prop, "font-weight")) {
  3690. int bold = 0;
  3691. unsigned int j;
  3692. if (vend - vstart >= 4 &&
  3693. ci_eq_n(s + vstart, "bold", 4))
  3694. bold = 1;
  3695. else {
  3696. int w = 0;
  3697. for (j = vstart; j < vend; j++)
  3698. if (s[j] >= '0' && s[j] <= '9')
  3699. w = w * 10 + (s[j] - '0');
  3700. if (w >= 500)
  3701. bold = 1;
  3702. }
  3703. decl_add(out, nout, PROP_BOLD, bold, important);
  3704. } else if (str_eq(prop, "font-style")) {
  3705. int italic = (vend - vstart >= 6 &&
  3706. ci_eq_n(s + vstart, "italic", 6));
  3707. decl_add(out, nout, PROP_ITALIC, italic, important);
  3708. } else if (str_eq(prop, "font-family")) {
  3709. unsigned int j;
  3710. int mono = 0;
  3711. int face = font_family_id(s + vstart, vend - vstart);
  3712. for (j = vstart; j + 4 <= vend; j++)
  3713. if (ci_eq_n(s + j, "mono", 4))
  3714. mono = 1;
  3715. decl_add(out, nout, PROP_FONT_FACE, face, important);
  3716. decl_add(out, nout, PROP_MONO, mono, important);
  3717. } else if (str_eq(prop, "font")) {
  3718. add_font_shorthand(out, nout, s + vstart,
  3719. vend - vstart, important);
  3720. } else if (str_eq(prop, "text-align")) {
  3721. int a = BR_ALIGN_LEFT;
  3722. if (vend - vstart >= 6 &&
  3723. ci_eq_n(s + vstart, "center", 6))
  3724. a = BR_ALIGN_CENTER;
  3725. else if (vend - vstart >= 5 &&
  3726. ci_eq_n(s + vstart, "right", 5))
  3727. a = BR_ALIGN_RIGHT;
  3728. decl_add(out, nout, PROP_ALIGN, a, important);
  3729. } else if (str_eq(prop, "text-decoration")) {
  3730. int u = 0;
  3731. if (vend - vstart >= 9 &&
  3732. ci_eq_n(s + vstart, "underline", 9))
  3733. u = 1;
  3734. decl_add(out, nout, PROP_UNDERLINE, u, important);
  3735. } else if (str_eq(prop, "text-transform")) {
  3736. int u = (vend - vstart >= 9 &&
  3737. ci_eq_n(s + vstart, "uppercase", 9));
  3738. decl_add(out, nout, PROP_UPPER, u, important);
  3739. } else if (str_eq(prop, "white-space")) {
  3740. int nw = contains_ci_len(s + vstart, vend - vstart,
  3741. "nowrap") ||
  3742. contains_ci_len(s + vstart, vend - vstart,
  3743. "pre");
  3744. decl_add(out, nout, PROP_NOWRAP, nw, important);
  3745. } else if (str_eq(prop, "display")) {
  3746. decl_add(out, nout, PROP_DISPLAY,
  3747. parse_display(s + vstart, vend - vstart),
  3748. important);
  3749. } else if (str_eq(prop, "visibility")) {
  3750. int h = contains_ci_len(s + vstart, vend - vstart,
  3751. "hidden") ||
  3752. contains_ci_len(s + vstart, vend - vstart,
  3753. "collapse");
  3754. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  3755. } else if (str_eq(prop, "opacity")) {
  3756. unsigned int q;
  3757. int h = 1, any = 0;
  3758. for (q = vstart; q < vend; q++) {
  3759. if (s[q] >= '1' && s[q] <= '9') {
  3760. h = 0;
  3761. break;
  3762. }
  3763. if (s[q] >= '0' && s[q] <= '9')
  3764. any = 1;
  3765. }
  3766. if (!any || contains_ci_len(s + vstart, vend - vstart,
  3767. "inherit"))
  3768. h = 0;
  3769. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  3770. } else if (str_eq(prop, "overflow") ||
  3771. str_eq(prop, "overflow-x") ||
  3772. str_eq(prop, "overflow-y")) {
  3773. int h = contains_ci_len(s + vstart, vend - vstart,
  3774. "hidden") ||
  3775. contains_ci_len(s + vstart, vend - vstart,
  3776. "clip");
  3777. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  3778. } else if (str_eq(prop, "float")) {
  3779. int f = 0;
  3780. if (vend - vstart >= 4 &&
  3781. ci_eq_n(s + vstart, "left", 4))
  3782. f = 1;
  3783. else if (vend - vstart >= 5 &&
  3784. ci_eq_n(s + vstart, "right", 5))
  3785. f = 2;
  3786. decl_add(out, nout, PROP_FLOAT, f, important);
  3787. } else if (str_eq(prop, "clear")) {
  3788. int c = 0;
  3789. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  3790. c = 3;
  3791. else if (contains_ci_len(s + vstart, vend - vstart,
  3792. "left"))
  3793. c = 1;
  3794. else if (contains_ci_len(s + vstart, vend - vstart,
  3795. "right"))
  3796. c = 2;
  3797. decl_add(out, nout, PROP_CLEAR, c, important);
  3798. } else if (str_eq(prop, "flex-grow") ||
  3799. str_eq(prop, "flex")) {
  3800. unsigned int q = vstart;
  3801. int g = 0;
  3802. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  3803. q++;
  3804. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  3805. g = g * 10 + (s[q] - '0');
  3806. q++;
  3807. }
  3808. if (g > 100)
  3809. g = 100;
  3810. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  3811. } else if (str_eq(prop, "flex-direction")) {
  3812. decl_add(out, nout, PROP_FLEX_COL,
  3813. contains_ci_len(s + vstart, vend - vstart,
  3814. "column"), important);
  3815. } else if (str_eq(prop, "flex-wrap")) {
  3816. int wrap = 0;
  3817. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  3818. wrap = 2;
  3819. else if (contains_ci_len(s + vstart, vend - vstart,
  3820. "wrap"))
  3821. wrap = 1;
  3822. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  3823. } else if (str_eq(prop, "justify-content")) {
  3824. int j = 0;
  3825. if (contains_ci_len(s + vstart, vend - vstart,
  3826. "space-between"))
  3827. j = 3;
  3828. else if (contains_ci_len(s + vstart, vend - vstart,
  3829. "space-around"))
  3830. j = 4;
  3831. else if (contains_ci_len(s + vstart, vend - vstart,
  3832. "center"))
  3833. j = 1;
  3834. else if (contains_ci_len(s + vstart, vend - vstart,
  3835. "flex-end") ||
  3836. contains_ci_len(s + vstart, vend - vstart,
  3837. "end"))
  3838. j = 2;
  3839. decl_add(out, nout, PROP_JUSTIFY, j, important);
  3840. } else if (str_eq(prop, "align-items")) {
  3841. int a = 0;
  3842. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  3843. a = 1;
  3844. else if (contains_ci_len(s + vstart, vend - vstart,
  3845. "flex-end") ||
  3846. contains_ci_len(s + vstart, vend - vstart,
  3847. "end"))
  3848. a = 2;
  3849. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  3850. } else if (str_eq(prop, "position")) {
  3851. int p = 0;
  3852. if (contains_ci_len(s + vstart, vend - vstart,
  3853. "absolute"))
  3854. p = 1;
  3855. else if (contains_ci_len(s + vstart, vend - vstart,
  3856. "fixed"))
  3857. p = 2;
  3858. else if (contains_ci_len(s + vstart, vend - vstart,
  3859. "relative"))
  3860. p = 3;
  3861. else if (contains_ci_len(s + vstart, vend - vstart,
  3862. "sticky"))
  3863. p = 4;
  3864. decl_add(out, nout, PROP_POS, p, important);
  3865. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  3866. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  3867. int which = str_eq(prop, "left") ? 0 :
  3868. str_eq(prop, "top") ? 1 :
  3869. str_eq(prop, "right") ? 2 : 3;
  3870. unsigned int pos = 0;
  3871. if (!(vend - vstart >= 4 &&
  3872. ci_eq_n(s + vstart, "auto", 4))) {
  3873. int v = parse_len(s + vstart, vend - vstart, &pos);
  3874. if (v > 32000)
  3875. v = 32000;
  3876. else if (v < -32000)
  3877. v = -32000;
  3878. decl_add(out, nout,
  3879. (unsigned char)(which == 0 ? PROP_LEFT :
  3880. which == 1 ? PROP_TOP :
  3881. which == 2 ? PROP_RIGHT :
  3882. PROP_BOTTOM),
  3883. v, important);
  3884. decl_add(out, nout,
  3885. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  3886. which == 1 ? PROP_TOP_SET :
  3887. which == 2 ? PROP_RIGHT_SET :
  3888. PROP_BOTTOM_SET),
  3889. 1, important);
  3890. }
  3891. } else if (str_eq(prop, "z-index")) {
  3892. unsigned int q = vstart;
  3893. int sign = 1;
  3894. int v = 0;
  3895. int any = 0;
  3896. if (q < vend && s[q] == '-') {
  3897. sign = -1;
  3898. q++;
  3899. }
  3900. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  3901. v = v * 10 + (s[q] - '0');
  3902. any = 1;
  3903. q++;
  3904. }
  3905. if (any) {
  3906. v *= sign;
  3907. if (v > 32000)
  3908. v = 32000;
  3909. else if (v < -32000)
  3910. v = -32000;
  3911. decl_add(out, nout, PROP_ZINDEX, v, important);
  3912. decl_add(out, nout, PROP_ZSET, 1, important);
  3913. } else if (vend - vstart >= 4 &&
  3914. ci_eq_n(s + vstart, "auto", 4)) {
  3915. decl_add(out, nout, PROP_ZSET, 0, important);
  3916. }
  3917. } else if (str_eq(prop, "transform")) {
  3918. int tx = 0, ty = 0;
  3919. int is_x = contains_ci_len(s + vstart, vend - vstart,
  3920. "translatex");
  3921. int is_y = contains_ci_len(s + vstart, vend - vstart,
  3922. "translatey");
  3923. unsigned int off = 0;
  3924. if (contains_ci_len(s + vstart, vend - vstart,
  3925. "translate")) {
  3926. while (off < vend - vstart &&
  3927. s[vstart + off] != '(')
  3928. off++;
  3929. if (off < vend - vstart)
  3930. off++;
  3931. if (!is_y)
  3932. tx = parse_len(s + vstart,
  3933. vend - vstart, &off);
  3934. if (!is_x) {
  3935. while (off < vend - vstart &&
  3936. (is_space(s[vstart + off]) ||
  3937. s[vstart + off] == ','))
  3938. off++;
  3939. ty = parse_len(s + vstart,
  3940. vend - vstart, &off);
  3941. }
  3942. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  3943. important);
  3944. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  3945. important);
  3946. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  3947. important);
  3948. }
  3949. } else if (str_eq(prop, "grid-template-columns") ||
  3950. str_eq(prop, "grid-template")) {
  3951. const char *v = s + vstart;
  3952. unsigned int vl = vend - vstart;
  3953. unsigned short val[2];
  3954. unsigned char kind[2];
  3955. int n, q;
  3956. if (str_eq(prop, "grid-template")) {
  3957. for (q = 0; q < (int)vl; q++)
  3958. if (v[q] == '/') {
  3959. q++;
  3960. while (q < (int)vl && is_space(v[q]))
  3961. q++;
  3962. v += q;
  3963. vl -= (unsigned int)q;
  3964. break;
  3965. }
  3966. }
  3967. if (contains_ci_len(v, vl, "auto-fit") ||
  3968. contains_ci_len(v, vl, "auto-fill")) {
  3969. unsigned int mp = 0;
  3970. int found = 0;
  3971. unsigned short mw = 200;
  3972. unsigned char mk = 1;
  3973. for (q = 0; q + 7 <= (int)vl; q++)
  3974. if (ci_eq_n(v + q, "minmax(", 7)) {
  3975. mp = (unsigned int)q + 7;
  3976. found = 1;
  3977. break;
  3978. }
  3979. if (found)
  3980. parse_grid_track(v, vl, &mp, &mw, &mk);
  3981. if (mk != 1 || mw < 80)
  3982. mw = 200;
  3983. if (mw > 480)
  3984. mw = 480;
  3985. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  3986. decl_add(out, nout, PROP_WIDTH, mw, important);
  3987. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  3988. } else {
  3989. n = parse_grid_cols(v, vl, val, kind);
  3990. if (n > 0) {
  3991. decl_add(out, nout, PROP_WIDTH, val[0],
  3992. important);
  3993. decl_add(out, nout, PROP_WIDTH_KIND,
  3994. kind[0], important);
  3995. decl_add(out, nout, PROP_GRID_NCOLS, n,
  3996. important);
  3997. }
  3998. if (n > 1) {
  3999. decl_add(out, nout, PROP_MAXW, val[1],
  4000. important);
  4001. decl_add(out, nout, PROP_MAXW_KIND,
  4002. kind[1], important);
  4003. }
  4004. }
  4005. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  4006. unsigned int gp = 0;
  4007. unsigned short gv = 0;
  4008. unsigned char gk = 1;
  4009. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  4010. if (gv > 120)
  4011. gv = 120;
  4012. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  4013. }
  4014. } else if (str_eq(prop, "grid-area")) {
  4015. int slot = parse_grid_slot(s + vstart, vend - vstart);
  4016. if (slot)
  4017. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  4018. } else if (str_eq(prop, "width") ||
  4019. str_eq(prop, "max-width") ||
  4020. str_eq(prop, "min-width") ||
  4021. str_eq(prop, "height") ||
  4022. str_eq(prop, "max-height") ||
  4023. str_eq(prop, "min-height")) {
  4024. unsigned short v;
  4025. unsigned char kind;
  4026. if (str_eq(prop, "width") &&
  4027. vend - vstart >= 5 &&
  4028. ci_eq_n(s + vstart, "calc(", 5)) {
  4029. int pct, px;
  4030. if (parse_calc(s + vstart + 5,
  4031. vend - vstart - 5, &pct, &px)) {
  4032. decl_add(out, nout, PROP_WIDTH, 0,
  4033. important);
  4034. decl_add(out, nout, PROP_WIDTH_KIND, 3,
  4035. important);
  4036. decl_add(out, nout,
  4037. PROP_WIDTH_CALC_PCT, pct,
  4038. important);
  4039. decl_add(out, nout,
  4040. PROP_WIDTH_CALC_PX, px,
  4041. important);
  4042. }
  4043. continue;
  4044. }
  4045. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  4046. if (str_eq(prop, "width")) {
  4047. decl_add(out, nout, PROP_WIDTH, v, important);
  4048. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  4049. important);
  4050. } else if (str_eq(prop, "max-width")) {
  4051. decl_add(out, nout, PROP_MAXW, v, important);
  4052. decl_add(out, nout, PROP_MAXW_KIND, kind,
  4053. important);
  4054. } else if (str_eq(prop, "min-width")) {
  4055. decl_add(out, nout, PROP_MINW, v, important);
  4056. decl_add(out, nout, PROP_MINW_KIND, kind,
  4057. important);
  4058. } else if (str_eq(prop, "max-height")) {
  4059. decl_add(out, nout, PROP_MAXH, v, important);
  4060. decl_add(out, nout, PROP_MAXH_KIND, kind,
  4061. important);
  4062. } else if (str_eq(prop, "min-height")) {
  4063. decl_add(out, nout, PROP_MINH, v, important);
  4064. decl_add(out, nout, PROP_MINH_KIND, kind,
  4065. important);
  4066. } else {
  4067. decl_add(out, nout, PROP_HEIGHT, v, important);
  4068. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  4069. important);
  4070. }
  4071. }
  4072. } else if (str_eq(prop, "box-sizing")) {
  4073. int bb = (vend - vstart >= 10 &&
  4074. ci_eq_n(s + vstart, "border-box", 10));
  4075. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  4076. } else if (str_eq(prop, "line-height")) {
  4077. unsigned short px;
  4078. unsigned char pct;
  4079. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  4080. if (px) {
  4081. decl_add(out, nout, PROP_LH_PX, px, important);
  4082. decl_add(out, nout, PROP_LH_PCT, 0, important);
  4083. } else {
  4084. decl_add(out, nout, PROP_LH_PX, 0, important);
  4085. decl_add(out, nout, PROP_LH_PCT, pct, important);
  4086. }
  4087. }
  4088. } else if (str_eq(prop, "list-style") ||
  4089. str_eq(prop, "list-style-type")) {
  4090. int none = (vend - vstart >= 4 &&
  4091. contains_ci_len(s + vstart, vend - vstart,
  4092. "none"));
  4093. decl_add(out, nout, PROP_LIST_NONE, none, important);
  4094. } else if (str_eq(prop, "border-radius") ||
  4095. str_eq(prop, "border-top-left-radius") ||
  4096. str_eq(prop, "border-top-right-radius") ||
  4097. str_eq(prop, "border-bottom-right-radius") ||
  4098. str_eq(prop, "border-bottom-left-radius")) {
  4099. unsigned short v[4];
  4100. unsigned char k[4];
  4101. unsigned int pos = 0;
  4102. int n = 0;
  4103. while (pos < vend - vstart && n < 4) {
  4104. while (pos < vend - vstart &&
  4105. is_space(s[vstart + pos]))
  4106. pos++;
  4107. if (pos >= vend - vstart ||
  4108. s[vstart + pos] == '/')
  4109. break;
  4110. if (!parse_radius_value(s + vstart,
  4111. vend - vstart, &pos,
  4112. &v[n], &k[n]))
  4113. break;
  4114. n++;
  4115. }
  4116. if (n) {
  4117. unsigned short r[4];
  4118. unsigned char rk[4];
  4119. int i;
  4120. if (n == 1) {
  4121. for (i = 0; i < 4; i++) {
  4122. r[i] = v[0];
  4123. rk[i] = k[0];
  4124. }
  4125. } else if (n == 2) {
  4126. r[0] = r[2] = v[0];
  4127. r[1] = r[3] = v[1];
  4128. rk[0] = rk[2] = k[0];
  4129. rk[1] = rk[3] = k[1];
  4130. } else if (n == 3) {
  4131. r[0] = v[0];
  4132. r[1] = r[3] = v[1];
  4133. r[2] = v[2];
  4134. rk[0] = k[0];
  4135. rk[1] = rk[3] = k[1];
  4136. rk[2] = k[2];
  4137. } else {
  4138. for (i = 0; i < 4; i++) {
  4139. r[i] = v[i];
  4140. rk[i] = k[i];
  4141. }
  4142. }
  4143. for (i = 0; i < 4; i++)
  4144. r[i] = rk[i] == 2 ? 255 :
  4145. (r[i] > 255 ? 255 : r[i]);
  4146. if (str_eq(prop, "border-top-left-radius")) {
  4147. decl_add(out, nout, PROP_RADIUS_TL,
  4148. (short)r[0], important);
  4149. } else if (str_eq(prop,
  4150. "border-top-right-radius")) {
  4151. decl_add(out, nout, PROP_RADIUS_TR,
  4152. (short)r[0], important);
  4153. } else if (str_eq(prop,
  4154. "border-bottom-right-radius")) {
  4155. decl_add(out, nout, PROP_RADIUS_BR,
  4156. (short)r[0], important);
  4157. } else if (str_eq(prop,
  4158. "border-bottom-left-radius")) {
  4159. decl_add(out, nout, PROP_RADIUS_BL,
  4160. (short)r[0], important);
  4161. } else {
  4162. decl_add(out, nout, PROP_RADIUS_TL,
  4163. (short)r[0], important);
  4164. decl_add(out, nout, PROP_RADIUS_TR,
  4165. (short)r[1], important);
  4166. decl_add(out, nout, PROP_RADIUS_BR,
  4167. (short)r[2], important);
  4168. decl_add(out, nout, PROP_RADIUS_BL,
  4169. (short)r[3], important);
  4170. }
  4171. }
  4172. } else if (str_eq(prop, "border") ||
  4173. str_eq(prop, "border-top") ||
  4174. str_eq(prop, "border-right") ||
  4175. str_eq(prop, "border-bottom") ||
  4176. str_eq(prop, "border-left") ||
  4177. str_eq(prop, "border-color") ||
  4178. str_eq(prop, "border-width") ||
  4179. str_eq(prop, "border-style") ||
  4180. str_eq(prop, "border-top-width") ||
  4181. str_eq(prop, "border-right-width") ||
  4182. str_eq(prop, "border-bottom-width") ||
  4183. str_eq(prop, "border-left-width") ||
  4184. str_eq(prop, "border-top-color") ||
  4185. str_eq(prop, "border-right-color") ||
  4186. str_eq(prop, "border-bottom-color") ||
  4187. str_eq(prop, "border-left-color") ||
  4188. str_eq(prop, "border-top-style") ||
  4189. str_eq(prop, "border-right-style") ||
  4190. str_eq(prop, "border-bottom-style") ||
  4191. str_eq(prop, "border-left-style")) {
  4192. const char *v = s + vstart;
  4193. unsigned int vl = vend - vstart;
  4194. unsigned int pos = 0, ts, te;
  4195. int widths[4], colors[4], styles[4];
  4196. int nw = 0, nc = 0, ns = 0;
  4197. int side = -1;
  4198. int is_wl = str_eq(prop, "border-width");
  4199. int is_cl = str_eq(prop, "border-color");
  4200. int is_sl = str_eq(prop, "border-style");
  4201. int i;
  4202. if (str_eq(prop, "border-top")) side = 0;
  4203. else if (str_eq(prop, "border-right")) side = 1;
  4204. else if (str_eq(prop, "border-bottom")) side = 2;
  4205. else if (str_eq(prop, "border-left")) side = 3;
  4206. else if (str_eq(prop, "border-top-width")) {
  4207. side = 0; is_wl = 2;
  4208. } else if (str_eq(prop, "border-right-width")) {
  4209. side = 1; is_wl = 2;
  4210. } else if (str_eq(prop, "border-bottom-width")) {
  4211. side = 2; is_wl = 2;
  4212. } else if (str_eq(prop, "border-left-width")) {
  4213. side = 3; is_wl = 2;
  4214. } else if (str_eq(prop, "border-top-color")) {
  4215. side = 0; is_cl = 2;
  4216. } else if (str_eq(prop, "border-right-color")) {
  4217. side = 1; is_cl = 2;
  4218. } else if (str_eq(prop, "border-bottom-color")) {
  4219. side = 2; is_cl = 2;
  4220. } else if (str_eq(prop, "border-left-color")) {
  4221. side = 3; is_cl = 2;
  4222. } else if (str_eq(prop, "border-top-style")) {
  4223. side = 0; is_sl = 2;
  4224. } else if (str_eq(prop, "border-right-style")) {
  4225. side = 1; is_sl = 2;
  4226. } else if (str_eq(prop, "border-bottom-style")) {
  4227. side = 2; is_sl = 2;
  4228. } else if (str_eq(prop, "border-left-style")) {
  4229. side = 3; is_sl = 2;
  4230. }
  4231. if (is_wl) {
  4232. while (nw < 4 && next_token(v, vl, &pos,
  4233. &ts, &te)) {
  4234. int x;
  4235. if (!parse_len_token(v + ts, te - ts,
  4236. &x))
  4237. break;
  4238. widths[nw++] = x;
  4239. }
  4240. if (nw) {
  4241. if (is_wl == 1)
  4242. expand_four(widths, nw);
  4243. if (side >= 0 && is_wl == 2) {
  4244. decl_add(out, nout,
  4245. PROP_BW_T + side,
  4246. widths[0], important);
  4247. } else {
  4248. for (i = 0; i < 4; i++)
  4249. decl_add(out, nout,
  4250. PROP_BW_T + i,
  4251. widths[i],
  4252. important);
  4253. }
  4254. }
  4255. } else if (is_cl) {
  4256. unsigned short c;
  4257. while (nc < 4 && next_token(v, vl, &pos,
  4258. &ts, &te)) {
  4259. if (!parse_color(v + ts, te - ts, &c))
  4260. break;
  4261. colors[nc++] = c;
  4262. }
  4263. if (nc) {
  4264. if (is_cl == 1)
  4265. expand_four(colors, nc);
  4266. if (side >= 0 && is_cl == 2) {
  4267. decl_add(out, nout,
  4268. PROP_BC_T + side,
  4269. colors[0], important);
  4270. } else {
  4271. for (i = 0; i < 4; i++)
  4272. decl_add(out, nout,
  4273. PROP_BC_T + i,
  4274. colors[i],
  4275. important);
  4276. }
  4277. }
  4278. } else if (is_sl) {
  4279. while (ns < 4 && next_token(v, vl, &pos,
  4280. &ts, &te)) {
  4281. int stv;
  4282. if ((te - ts == 4 &&
  4283. ci_eq_n(v + ts, "none", 4)) ||
  4284. (te - ts == 6 &&
  4285. ci_eq_n(v + ts, "hidden", 6)))
  4286. stv = 0;
  4287. else
  4288. stv = 1;
  4289. styles[ns++] = stv;
  4290. }
  4291. if (ns) {
  4292. if (is_sl == 1)
  4293. expand_four(styles, ns);
  4294. if (is_sl == 2 && side >= 0) {
  4295. if (!styles[0])
  4296. decl_add(out, nout,
  4297. PROP_BW_T + side,
  4298. 0, important);
  4299. } else {
  4300. for (i = 0; i < 4; i++)
  4301. if (!styles[i])
  4302. decl_add(out, nout,
  4303. PROP_BW_T + i,
  4304. 0,
  4305. important);
  4306. }
  4307. }
  4308. } else {
  4309. int bw = -1, bc = -1, style_none = 0;
  4310. while (next_token(v, vl, &pos, &ts, &te)) {
  4311. unsigned short c;
  4312. int x;
  4313. if ((te - ts == 4 &&
  4314. ci_eq_n(v + ts, "none", 4)) ||
  4315. (te - ts == 6 &&
  4316. ci_eq_n(v + ts, "hidden", 6))) {
  4317. style_none = 1;
  4318. } else if (parse_len_token(v + ts,
  4319. te - ts, &x)) {
  4320. bw = x;
  4321. } else if (parse_color(v + ts,
  4322. te - ts, &c)) {
  4323. bc = c;
  4324. }
  4325. }
  4326. if (style_none)
  4327. bw = 0;
  4328. if (bc >= 0 && bw < 0)
  4329. bw = 1;
  4330. if (side < 0) {
  4331. if (bw >= 0)
  4332. decl_add(out, nout,
  4333. PROP_BORDER_W, bw,
  4334. important);
  4335. if (bc >= 0)
  4336. decl_add(out, nout,
  4337. PROP_BORDER_COLOR, bc,
  4338. important);
  4339. } else {
  4340. if (bw >= 0)
  4341. decl_add(out, nout,
  4342. PROP_BW_T + side, bw,
  4343. important);
  4344. if (bc >= 0)
  4345. decl_add(out, nout,
  4346. PROP_BC_T + side, bc,
  4347. important);
  4348. }
  4349. }
  4350. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  4351. int vals[4], units[4], nv = 0;
  4352. unsigned int pos = 0;
  4353. int is_pad = prop[0] == 'p';
  4354. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  4355. while (pos < vend - vstart && nv < 4) {
  4356. units[nv] = 0;
  4357. vals[nv] = parse_margin_len(s + vstart,
  4358. vend - vstart,
  4359. &pos, &units[nv]);
  4360. nv++;
  4361. while (pos < vend - vstart &&
  4362. is_space(s[vstart + pos]))
  4363. pos++;
  4364. }
  4365. if (nv == 1) {
  4366. vals[1] = vals[2] = vals[3] = vals[0];
  4367. units[1] = units[2] = units[3] = units[0];
  4368. } else if (nv == 2) {
  4369. vals[2] = vals[0];
  4370. vals[3] = vals[1];
  4371. units[2] = units[0];
  4372. units[3] = units[1];
  4373. } else if (nv == 3) {
  4374. vals[3] = vals[1];
  4375. units[3] = units[1];
  4376. }
  4377. if (nv > 0) {
  4378. int k;
  4379. for (k = 0; k < 4; k++) {
  4380. int v = vals[k];
  4381. int u = units[k];
  4382. int au = u == 4;
  4383. if (au)
  4384. v = 0;
  4385. if (is_pad && au)
  4386. u = 0;
  4387. decl_add(out, nout,
  4388. (unsigned char)(base + k), v,
  4389. important);
  4390. decl_add(out, nout,
  4391. (unsigned char)
  4392. ((is_pad ? PROP_PT_U :
  4393. PROP_MT_U) + k),
  4394. u, important);
  4395. if (!is_pad && (k == 1 || k == 3))
  4396. decl_add(out, nout,
  4397. k == 1 ? PROP_MR_AUTO :
  4398. PROP_ML_AUTO,
  4399. au, important);
  4400. }
  4401. }
  4402. } else if (str_eq(prop, "margin-top") ||
  4403. str_eq(prop, "margin-right") ||
  4404. str_eq(prop, "margin-bottom") ||
  4405. str_eq(prop, "margin-left") ||
  4406. str_eq(prop, "padding-top") ||
  4407. str_eq(prop, "padding-right") ||
  4408. str_eq(prop, "padding-bottom") ||
  4409. str_eq(prop, "padding-left")) {
  4410. unsigned int pos = 0;
  4411. int unit = 0;
  4412. int v = parse_margin_len(s + vstart, vend - vstart,
  4413. &pos, &unit);
  4414. unsigned char p = PROP_MT;
  4415. const char *side;
  4416. int au = unit == 4;
  4417. if (au)
  4418. v = 0;
  4419. if (prop[0] == 'p') {
  4420. p = PROP_PT;
  4421. if (au)
  4422. unit = 0;
  4423. }
  4424. side = prop + (prop[0] == 'p' ? 8 : 7);
  4425. if (str_eq(side, "right"))
  4426. p = (unsigned char)(p + 1);
  4427. else if (str_eq(side, "bottom"))
  4428. p = (unsigned char)(p + 2);
  4429. else if (str_eq(side, "left"))
  4430. p = (unsigned char)(p + 3);
  4431. decl_add(out, nout, p, v, important);
  4432. decl_add(out, nout,
  4433. (unsigned char)((prop[0] == 'p' ?
  4434. PROP_PT_U : PROP_MT_U) +
  4435. (p - (prop[0] == 'p' ?
  4436. PROP_PT : PROP_MT))),
  4437. unit, important);
  4438. if (str_eq(side, "right"))
  4439. decl_add(out, nout, PROP_MR_AUTO, au, important);
  4440. else if (str_eq(side, "left"))
  4441. decl_add(out, nout, PROP_ML_AUTO, au, important);
  4442. }
  4443. }
  4444. }
  4445. /* Skip @media/@font-face/@keyframes/@import without descending into their
  4446. * nested blocks. This is the mobile/base rules path: media enhancements are
  4447. * deliberately ignored for now. */
  4448. static unsigned int skip_at_rule(const char *s, unsigned int len,
  4449. unsigned int i)
  4450. {
  4451. unsigned int j = i;
  4452. while (j < len && s[j] != '{' && s[j] != ';')
  4453. j++;
  4454. if (j >= len)
  4455. return len;
  4456. if (s[j] == ';')
  4457. return j + 1;
  4458. j++;
  4459. return css_skip_nested(s, len, j);
  4460. }
  4461. static unsigned int viewport_w = 1024;
  4462. static unsigned short theme_text = 0x0000;
  4463. static unsigned short theme_bg = 0xFFFF;
  4464. static unsigned short theme_panel = 0xEF5D;
  4465. static unsigned short theme_link = 0x001B;
  4466. void br_set_theme(unsigned short text, unsigned short bg,
  4467. unsigned short panel, unsigned short link)
  4468. {
  4469. theme_text = text;
  4470. theme_bg = bg;
  4471. theme_panel = panel;
  4472. theme_link = link;
  4473. }
  4474. void br_set_viewport(unsigned int width)
  4475. {
  4476. viewport_w = width ? width : 1024;
  4477. }
  4478. static unsigned int media_len_px(const char *s, unsigned int len)
  4479. {
  4480. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  4481. int em = 0;
  4482. while (i < len && is_space(s[i]))
  4483. i++;
  4484. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4485. whole = whole * 10 + (unsigned int)(s[i] - '0');
  4486. i++;
  4487. }
  4488. if (i < len && s[i] == '.') {
  4489. i++;
  4490. /* Consume every fractional digit even when there are more
  4491. * than the three we keep: leaving the tail behind made the
  4492. * unit check miss "em" (74.9375em became 74px). */
  4493. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4494. if (scale < 1000) {
  4495. frac = frac * 10 + (unsigned int)(s[i] - '0');
  4496. scale *= 10;
  4497. }
  4498. i++;
  4499. }
  4500. }
  4501. while (i < len && is_space(s[i]))
  4502. i++;
  4503. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  4504. em = 1;
  4505. i += 2;
  4506. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  4507. em = 1;
  4508. i += 3;
  4509. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  4510. i += 2;
  4511. }
  4512. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  4513. if (em)
  4514. return v * 16 / 100;
  4515. return v / 100;
  4516. }
  4517. /* One parenthesised media feature: min-width/max-width and the modern
  4518. * (width <= 600px) form. Unknown features are treated as true. */
  4519. static int media_feature_matches(const char *s, unsigned int len)
  4520. {
  4521. unsigned int i;
  4522. while (len && is_space(*s)) { s++; len--; }
  4523. while (len && is_space(s[len - 1])) len--;
  4524. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  4525. /* There is no user-selectable colour scheme. Ignore both
  4526. * variants so a page's base palette is used; sites whose base
  4527. * is dark (like the project docs) then stay dark instead of
  4528. * flipping to their light media override. */
  4529. return 0;
  4530. }
  4531. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  4532. for (i = 9; i < len && s[i] != ':'; i++)
  4533. ;
  4534. if (i < len)
  4535. return viewport_w >= media_len_px(s + i + 1,
  4536. len - i - 1);
  4537. return 1;
  4538. }
  4539. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  4540. for (i = 9; i < len && s[i] != ':'; i++)
  4541. ;
  4542. if (i < len)
  4543. return viewport_w <= media_len_px(s + i + 1,
  4544. len - i - 1);
  4545. return 1;
  4546. }
  4547. for (i = 0; i < len; i++) {
  4548. if (s[i] == '<' || s[i] == '>') {
  4549. int max = s[i] == '<';
  4550. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  4551. if (i + 1 < len && s[i + 1] == '=')
  4552. v = media_len_px(s + i + 2, len - i - 2);
  4553. return max ? viewport_w <= v : viewport_w >= v;
  4554. }
  4555. }
  4556. return 1;
  4557. }
  4558. static int media_query_matches(const char *s, unsigned int len)
  4559. {
  4560. unsigned int i;
  4561. if (contains_ci_len(s, len, "print") &&
  4562. !contains_ci_len(s, len, "screen"))
  4563. return 0;
  4564. if (contains_ci_len(s, len, "not "))
  4565. return 0;
  4566. for (i = 0; i < len; i++) {
  4567. if (s[i] == '(') {
  4568. unsigned int st = ++i;
  4569. while (i < len && s[i] != ')')
  4570. i++;
  4571. if (!media_feature_matches(s + st, i - st))
  4572. return 0;
  4573. }
  4574. }
  4575. return 1;
  4576. }
  4577. static int media_list_matches(const char *s, unsigned int len)
  4578. {
  4579. unsigned int i = 0;
  4580. while (i < len) {
  4581. unsigned int st = i;
  4582. while (i < len && s[i] != ',')
  4583. i++;
  4584. if (media_query_matches(s + st, i - st))
  4585. return 1;
  4586. i++;
  4587. }
  4588. return 0;
  4589. }
  4590. static unsigned int rule_hash(const char *s, unsigned int len)
  4591. {
  4592. unsigned int h = 2166136261u, i;
  4593. for (i = 0; i < len; i++) {
  4594. char c = s[i];
  4595. if (c >= 'A' && c <= 'Z')
  4596. c = (char)(c - 'A' + 'a');
  4597. h ^= (unsigned char)c;
  4598. h *= 16777619u;
  4599. }
  4600. return h % BR_RULE_HASH;
  4601. }
  4602. /* Index a parsed rule by the most selective component of its rightmost
  4603. * compound selector. br_style_for() then only visits rules that can
  4604. * possibly match a node instead of scanning every rule. */
  4605. static void rule_index_add(struct br_doc *d, unsigned int idx,
  4606. const char *key, unsigned int len)
  4607. {
  4608. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  4609. unsigned short *head;
  4610. for (i = 0; i < len; i++) {
  4611. if (key[i] == '.' || key[i] == '#') {
  4612. unsigned int st = i + 1, n = 0;
  4613. while (st + n < len && is_ident(key[st + n]))
  4614. n++;
  4615. if (key[i] == '#') {
  4616. ipos = st;
  4617. ilen = n;
  4618. } else if (n) {
  4619. cpos = st;
  4620. clen = n;
  4621. }
  4622. i = st + n - 1;
  4623. }
  4624. }
  4625. if (ilen)
  4626. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  4627. else if (clen)
  4628. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  4629. else if (len == 5 && ci_eq_n(key, ":root", 5))
  4630. head = &d->rule_tag[BR_TAG_HTML];
  4631. else if (len && is_ident(key[0])) {
  4632. char name[16];
  4633. unsigned int n = 0, tag;
  4634. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  4635. name[n] = key[n];
  4636. n++;
  4637. }
  4638. name[n] = '\0';
  4639. tag = br_tag_from_name(name);
  4640. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  4641. head = &d->rule_tag[tag];
  4642. else
  4643. head = &d->rule_any;
  4644. } else {
  4645. head = &d->rule_any;
  4646. }
  4647. d->rule_next[idx] = *head;
  4648. *head = (unsigned short)idx;
  4649. }
  4650. static unsigned int selector_specificity(const char *s, unsigned int len);
  4651. static int css_collect_only;
  4652. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  4653. {
  4654. unsigned int i = 0;
  4655. while (i < len && d->nrules < d->rule_cap) {
  4656. unsigned int sstart, send, bstart, bend, j;
  4657. struct br_decl decls[BR_DECLS];
  4658. unsigned int nd;
  4659. while (i < len && is_space(s[i]))
  4660. i++;
  4661. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  4662. i += 2;
  4663. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  4664. i++;
  4665. i += 2;
  4666. continue;
  4667. }
  4668. if (i >= len || s[i] == '}')
  4669. break;
  4670. if (s[i] == '@') {
  4671. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  4672. unsigned int q = i + 6;
  4673. while (q < len && s[q] != '{')
  4674. q++;
  4675. if (q < len &&
  4676. media_list_matches(s + i + 6,
  4677. q - (i + 6))) {
  4678. unsigned int b = q + 1;
  4679. unsigned int e = css_skip_nested(s, len, b);
  4680. if (e > b)
  4681. parse_css(d, s + b,
  4682. (e - 1) - b);
  4683. i = e;
  4684. } else {
  4685. i = skip_at_rule(s, len, i);
  4686. }
  4687. continue;
  4688. }
  4689. i = skip_at_rule(s, len, i);
  4690. continue;
  4691. }
  4692. sstart = i;
  4693. while (i < len && s[i] != '{')
  4694. i++;
  4695. if (i >= len)
  4696. break;
  4697. send = i;
  4698. i++;
  4699. bstart = i;
  4700. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  4701. * to the matching close brace; stopping at the first '}' left
  4702. * the parser out of sync and dropped the rest of the sheet
  4703. * (nd-aktuell lost its desktop rules that way). */
  4704. bend = css_block_end(s, len, i);
  4705. i = bend < len ? bend + 1 : len;
  4706. while (sstart < send && is_space(s[sstart]))
  4707. sstart++;
  4708. while (send > sstart && is_space(s[send - 1]))
  4709. send--;
  4710. if (sstart == send)
  4711. continue;
  4712. if (!css_dark_selector(s + sstart, send - sstart))
  4713. css_collect_block(d, s + bstart, bend - bstart);
  4714. if (css_collect_only)
  4715. continue;
  4716. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  4717. if (!nd)
  4718. continue;
  4719. /* Split a comma-separated selector list into separate rules. */
  4720. j = sstart;
  4721. while (j <= send && d->nrules < d->rule_cap) {
  4722. unsigned int q = j, e, k;
  4723. char sel[160];
  4724. unsigned int off;
  4725. while (q < send && s[q] != ',')
  4726. q++;
  4727. e = q;
  4728. while (j < e && is_space(s[j]))
  4729. j++;
  4730. while (e > j && is_space(s[e - 1]))
  4731. e--;
  4732. if (e > j) {
  4733. /* Ignore single selectors with sibling combinators,
  4734. * pseudo-classes or attribute tests: they need
  4735. * state/attributes we do not model. The other
  4736. * selectors in the list still apply. */
  4737. for (k = j; k < e; ) {
  4738. if (s[k] == '+' || s[k] == '~')
  4739. break;
  4740. if (s[k] == ':') {
  4741. if (k + 5 <= e &&
  4742. ci_eq_n(s + k, ":root", 5)) {
  4743. k += 5;
  4744. continue;
  4745. }
  4746. if (k + 5 <= e &&
  4747. ci_eq_n(s + k, ":not(", 5)) {
  4748. unsigned int n = k + 5;
  4749. int nd = 1;
  4750. while (n < e && nd) {
  4751. if (s[n] == '(')
  4752. nd++;
  4753. else if (s[n] == ')')
  4754. nd--;
  4755. n++;
  4756. }
  4757. if (nd)
  4758. break;
  4759. k = n;
  4760. continue;
  4761. }
  4762. break;
  4763. }
  4764. k++;
  4765. }
  4766. if (k < e) {
  4767. j = q + 1;
  4768. if (q >= send)
  4769. break;
  4770. continue;
  4771. }
  4772. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  4773. sel[k] = s[j + k];
  4774. sel[k] = '\0';
  4775. off = store_attr(d, sel, k, 159);
  4776. if (off) {
  4777. struct br_rule *r = &d->rules[d->nrules++];
  4778. unsigned int ks = 0, q;
  4779. for (q = 0; q < k; q++)
  4780. if (sel[q] == ' ' || sel[q] == '>')
  4781. ks = q + 1;
  4782. r->sel_off = off;
  4783. r->key_off = store_attr(d, sel + ks,
  4784. k - ks, 159);
  4785. r->spec = (unsigned short)selector_specificity(sel, k);
  4786. r->single = 1;
  4787. {
  4788. unsigned int z;
  4789. for (z = 0; z < k; z++)
  4790. if (is_space(sel[z]) || sel[z] == '>') {
  4791. r->single = 0;
  4792. break;
  4793. }
  4794. }
  4795. r->nd = (unsigned char)nd;
  4796. for (k = 0; k < nd; k++)
  4797. r->d[k] = decls[k];
  4798. rule_index_add(d, d->nrules - 1,
  4799. attr_ptr(d, r->key_off),
  4800. s_len(attr_ptr(d, r->key_off)));
  4801. }
  4802. }
  4803. j = q + 1;
  4804. if (q >= send)
  4805. break;
  4806. }
  4807. }
  4808. }
  4809. /* Sites often set html { font-size: 62.5% } and then size everything in
  4810. * rem. Detect that root size before parsing the sheet so a rem resolves to
  4811. * the same pixels a real browser would use. */
  4812. static void detect_root_font(const char *s, unsigned int len)
  4813. {
  4814. unsigned int i;
  4815. for (i = 0; i + 4 < len; i++) {
  4816. unsigned int j, end, body, sel, q, h;
  4817. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  4818. int exact;
  4819. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  4820. s[i + 3] == 'l'))
  4821. continue;
  4822. h = i + 4;
  4823. while (h < len && is_space(s[h]))
  4824. h++;
  4825. if (h < len && s[h] != '{' && s[h] != ',' &&
  4826. s[h] != '.' && s[h] != ':' && s[h] != '[')
  4827. continue;
  4828. sel = i;
  4829. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  4830. s[sel - 1] != ';' && s[sel - 1] != ',')
  4831. sel--;
  4832. q = sel;
  4833. while (q < i && is_space(s[q]))
  4834. q++;
  4835. exact = (q == i);
  4836. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  4837. continue;
  4838. j = h;
  4839. if (j >= len || s[j] != '{')
  4840. continue;
  4841. while (j < len && s[j] != '{')
  4842. j++;
  4843. if (j >= len)
  4844. break;
  4845. end = j + 1;
  4846. body = end;
  4847. while (end < len && s[end] != '}')
  4848. end++;
  4849. for (; j + 9 < end; j++) {
  4850. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  4851. s[j + 3] == 't' && s[j + 4] == '-' &&
  4852. s[j + 5] == 's' && s[j + 6] == 'i' &&
  4853. s[j + 7] == 'z' && s[j + 8] == 'e'))
  4854. continue;
  4855. j += 9;
  4856. while (j < end && s[j] != ':')
  4857. j++;
  4858. if (j >= end)
  4859. break;
  4860. j++;
  4861. while (j < end && is_space(s[j]))
  4862. j++;
  4863. while (j < end && s[j] >= '0' && s[j] <= '9') {
  4864. v = v * 10 + (s[j] - '0');
  4865. digits = 1;
  4866. j++;
  4867. }
  4868. if (j < end && s[j] == '.') {
  4869. j++;
  4870. while (j < end && s[j] >= '0' && s[j] <= '9') {
  4871. v = v * 10 + (s[j] - '0');
  4872. frac++;
  4873. j++;
  4874. }
  4875. }
  4876. while (frac < 2) {
  4877. v *= 10;
  4878. frac++;
  4879. }
  4880. while (frac > 2) {
  4881. v /= 10;
  4882. frac--;
  4883. }
  4884. if (!digits)
  4885. break;
  4886. if (j < end && s[j] == '%')
  4887. pct = 1;
  4888. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  4889. px = 1;
  4890. if (pct)
  4891. v = (v * 16 + 5000) / 10000;
  4892. else if (px)
  4893. v /= 100;
  4894. else
  4895. v = v * 16 / 100;
  4896. if (exact && v >= 8 && v <= 24)
  4897. root_font_px = v;
  4898. if (exact)
  4899. return;
  4900. }
  4901. /* html { font: 10px/1 Verdana } also sets the root size. */
  4902. for (j = body; j + 5 <= end; j++) {
  4903. unsigned int p, ts, te;
  4904. if (!ci_eq_n(s + j, "font:", 5))
  4905. continue;
  4906. p = j + 5;
  4907. while (next_token(s, end, &p, &ts, &te)) {
  4908. int fv = parse_font_px(s + ts, te - ts);
  4909. if (fv >= 8 && fv <= 24) {
  4910. if (exact)
  4911. root_font_px = fv;
  4912. break;
  4913. }
  4914. }
  4915. if (exact)
  4916. return;
  4917. break;
  4918. }
  4919. }
  4920. }
  4921. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  4922. {
  4923. scan_font_faces(css, len);
  4924. detect_root_font(css, len);
  4925. css_collect_only = 1;
  4926. parse_css(d, css, len);
  4927. css_collect_only = 0;
  4928. parse_css(d, css, len);
  4929. }
  4930. void br_parse_inline_css(struct br_doc *d)
  4931. {
  4932. if (d->inline_css_len) {
  4933. scan_font_faces(css_store, d->inline_css_len);
  4934. detect_root_font(css_store, d->inline_css_len);
  4935. css_collect_only = 1;
  4936. parse_css(d, css_store, d->inline_css_len);
  4937. css_collect_only = 0;
  4938. parse_css(d, css_store, d->inline_css_len);
  4939. }
  4940. d->inline_css_len = 0;
  4941. }
  4942. void br_reset_styles(struct br_doc *d)
  4943. {
  4944. unsigned int i;
  4945. for (i = 0; i < d->nnode; i++)
  4946. d->nodes[i].style_valid = 0;
  4947. }
  4948. /* ---- selector matching ---- */
  4949. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  4950. unsigned int clen)
  4951. {
  4952. const char *s = attr_ptr(d, d->nodes[node].class_off);
  4953. unsigned int i = 0;
  4954. while (s[i]) {
  4955. unsigned int st, n;
  4956. while (s[i] && is_space(s[i]))
  4957. i++;
  4958. st = i;
  4959. while (s[i] && !is_space(s[i]))
  4960. i++;
  4961. n = i - st;
  4962. if (n == clen && ci_eq_n(s + st, cls, n))
  4963. return 1;
  4964. }
  4965. return 0;
  4966. }
  4967. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  4968. unsigned int len)
  4969. {
  4970. const char *s = attr_ptr(d, d->nodes[node].id_off);
  4971. unsigned int i;
  4972. for (i = 0; i < len; i++)
  4973. if (s[i] != id[i])
  4974. return 0;
  4975. return s[len] == '\0';
  4976. }
  4977. static int is_ident(char c)
  4978. {
  4979. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  4980. (c >= '0' && c <= '9') || c == '-' || c == '_';
  4981. }
  4982. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  4983. {
  4984. unsigned int n = 0;
  4985. while (lit[n])
  4986. n++;
  4987. return n == nlen && ci_eq_n(name, lit, nlen);
  4988. }
  4989. static int attr_present(struct br_doc *d, unsigned int node,
  4990. const char *name, unsigned int nlen)
  4991. {
  4992. struct br_node *n = &d->nodes[node];
  4993. if (attr_is(name, nlen, "data-collapse-target"))
  4994. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  4995. if (attr_is(name, nlen, "data-collapse-trigger"))
  4996. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  4997. if (attr_is(name, nlen, "is-open"))
  4998. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  4999. if (attr_is(name, nlen, "disabled"))
  5000. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  5001. if (attr_is(name, nlen, "open"))
  5002. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  5003. if (attr_is(name, nlen, "inert"))
  5004. return (n->attr_flags & BR_ATTR_INERT) != 0;
  5005. if (attr_is(name, nlen, "hidden"))
  5006. return (n->flags & BR_NODE_HIDDEN) != 0;
  5007. if (attr_is(name, nlen, "href"))
  5008. return attr_ptr(d, n->href_off)[0] != 0;
  5009. if (attr_is(name, nlen, "class"))
  5010. return attr_ptr(d, n->class_off)[0] != 0;
  5011. if (attr_is(name, nlen, "id"))
  5012. return attr_ptr(d, n->id_off)[0] != 0;
  5013. if (attr_is(name, nlen, "src"))
  5014. return attr_ptr(d, n->src_off)[0] != 0;
  5015. if (attr_is(name, nlen, "alt"))
  5016. return attr_ptr(d, n->alt_off)[0] != 0;
  5017. if (attr_is(name, nlen, "rel"))
  5018. return attr_ptr(d, n->rel_off)[0] != 0;
  5019. return 0;
  5020. }
  5021. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  5022. unsigned int len)
  5023. {
  5024. unsigned int i = 0;
  5025. int got = 0;
  5026. if (i < len && s[i] == '*') {
  5027. i++;
  5028. got = 1;
  5029. } else if (i < len && is_ident(s[i])) {
  5030. char name[16];
  5031. unsigned int n = 0, tag;
  5032. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  5033. name[n++] = s[i++];
  5034. name[n] = '\0';
  5035. tag = br_tag_from_name(name);
  5036. if (tag == BR_TAG_UNKNOWN) {
  5037. const char *raw = attr_ptr(d,
  5038. d->nodes[node].name_off);
  5039. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  5040. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  5041. return 0;
  5042. } else if (tag != d->nodes[node].tag) {
  5043. return 0;
  5044. }
  5045. got = 1;
  5046. }
  5047. while (i < len) {
  5048. if (s[i] == '.') {
  5049. unsigned int st;
  5050. i++;
  5051. st = i;
  5052. while (i < len && is_ident(s[i]))
  5053. i++;
  5054. if (!class_has(d, node, s + st, i - st))
  5055. return 0;
  5056. got = 1;
  5057. } else if (s[i] == '#') {
  5058. unsigned int st;
  5059. i++;
  5060. st = i;
  5061. while (i < len && is_ident(s[i]))
  5062. i++;
  5063. if (!id_eq(d, node, s + st, i - st))
  5064. return 0;
  5065. got = 1;
  5066. } else if (s[i] == '[') {
  5067. unsigned int st, e;
  5068. i++;
  5069. st = e = i;
  5070. while (e < len && s[e] != ']' && s[e] != '=' &&
  5071. !is_space(s[e]))
  5072. e++;
  5073. if (e == st || !attr_present(d, node, s + st, e - st))
  5074. return 0;
  5075. while (i < len && s[i] != ']')
  5076. i++;
  5077. if (i < len)
  5078. i++;
  5079. got = 1;
  5080. } else if (s[i] == ':') {
  5081. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  5082. (i + 5 == len || !is_ident(s[i + 5]))) {
  5083. if (d->nodes[node].tag != BR_TAG_HTML)
  5084. return 0;
  5085. i += 5;
  5086. got = 1;
  5087. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  5088. unsigned int st = i + 5, e = st;
  5089. int depth = 1;
  5090. while (e < len && depth) {
  5091. if (s[e] == '(')
  5092. depth++;
  5093. else if (s[e] == ')')
  5094. depth--;
  5095. if (depth)
  5096. e++;
  5097. }
  5098. if (e >= len)
  5099. return 0;
  5100. for (unsigned int z = st; z < e; z++)
  5101. if (s[z] == ':')
  5102. return 0;
  5103. if (compound_match(d, node, s + st, e - st))
  5104. return 0;
  5105. i = e + 1;
  5106. got = 1;
  5107. } else {
  5108. return 0; /* other pseudo-classes are not modelled */
  5109. }
  5110. } else {
  5111. i++;
  5112. }
  5113. }
  5114. return got;
  5115. }
  5116. static unsigned int selector_specificity(const char *s, unsigned int len)
  5117. {
  5118. unsigned int i, spec = 0;
  5119. for (i = 0; i < len; i++) {
  5120. if (s[i] == '#')
  5121. spec += 100;
  5122. else if (s[i] == '.' || s[i] == '[' ||
  5123. (s[i] == ':' && !(i + 5 <= len &&
  5124. ci_eq_n(s + i, ":root", 5))))
  5125. spec += 10;
  5126. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  5127. if (is_ident(s[i]))
  5128. spec += 1;
  5129. }
  5130. }
  5131. return spec;
  5132. }
  5133. /* Match one complex selector; returns specificity or -1. */
  5134. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  5135. unsigned int len, int right_done)
  5136. {
  5137. struct {
  5138. unsigned int start, end;
  5139. int child;
  5140. } parts[8];
  5141. int np = 0;
  5142. unsigned int i = 0;
  5143. int right;
  5144. while (i < len) {
  5145. int child = 0;
  5146. unsigned int st, e;
  5147. while (i < len && is_space(s[i]))
  5148. i++;
  5149. if (i < len && s[i] == '>') {
  5150. child = 1;
  5151. i++;
  5152. while (i < len && is_space(s[i]))
  5153. i++;
  5154. }
  5155. if (i >= len)
  5156. break;
  5157. st = i;
  5158. while (i < len && !is_space(s[i]) && s[i] != '>')
  5159. i++;
  5160. e = i;
  5161. if (np < 8) {
  5162. parts[np].start = st;
  5163. parts[np].end = e;
  5164. parts[np].child = child;
  5165. np++;
  5166. }
  5167. }
  5168. if (!np)
  5169. return -1;
  5170. if (!right_done &&
  5171. !compound_match(d, node, s + parts[np - 1].start,
  5172. parts[np - 1].end - parts[np - 1].start))
  5173. return -1;
  5174. right = np - 1;
  5175. while (right > 0) {
  5176. unsigned int p = d->nodes[node].parent;
  5177. if (!p)
  5178. return -1;
  5179. if (parts[right].child) {
  5180. if (!compound_match(d, p, s + parts[right - 1].start,
  5181. parts[right - 1].end -
  5182. parts[right - 1].start))
  5183. return -1;
  5184. node = p;
  5185. } else {
  5186. while (p && !compound_match(d, p,
  5187. s + parts[right - 1].start,
  5188. parts[right - 1].end -
  5189. parts[right - 1].start))
  5190. p = d->nodes[p].parent;
  5191. if (!p)
  5192. return -1;
  5193. node = p;
  5194. }
  5195. right--;
  5196. }
  5197. return (int)selector_specificity(s, len);
  5198. }
  5199. #define RULE_MATCH_MAX 512
  5200. struct rule_match {
  5201. unsigned short idx;
  5202. short spec;
  5203. };
  5204. static struct rule_match rule_matches[RULE_MATCH_MAX];
  5205. static unsigned int nrule_matches;
  5206. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  5207. {
  5208. struct br_rule *r = &d->rules[idx];
  5209. const char *key = attr_ptr(d, r->key_off);
  5210. int spec, key_ok = 0;
  5211. if (key[0]) {
  5212. if (!compound_match(d, node, key, s_len(key)))
  5213. return;
  5214. key_ok = 1;
  5215. }
  5216. spec = (r->single && key_ok) ? (int)r->spec :
  5217. selector_match(d, node, attr_ptr(d, r->sel_off),
  5218. s_len(attr_ptr(d, r->sel_off)), key_ok);
  5219. if (spec < 0)
  5220. return;
  5221. if (nrule_matches < RULE_MATCH_MAX) {
  5222. rule_matches[nrule_matches].idx = (unsigned short)idx;
  5223. rule_matches[nrule_matches].spec = (short)spec;
  5224. nrule_matches++;
  5225. }
  5226. }
  5227. static void rule_scan_list(struct br_doc *d, unsigned int node,
  5228. unsigned short head)
  5229. {
  5230. while (head != 0xFFFF) {
  5231. rule_try(d, node, head);
  5232. head = d->rule_next[head];
  5233. }
  5234. }
  5235. static void rule_scan_class(struct br_doc *d, unsigned int node,
  5236. const char *cls, unsigned int len)
  5237. {
  5238. if (len)
  5239. rule_scan_list(d, node,
  5240. d->rule_bucket[rule_hash(cls, len)]);
  5241. }
  5242. static void rule_collect(struct br_doc *d, unsigned int node)
  5243. {
  5244. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  5245. const char *id = attr_ptr(d, d->nodes[node].id_off);
  5246. unsigned int tag = d->nodes[node].tag;
  5247. unsigned int i;
  5248. nrule_matches = 0;
  5249. rule_scan_list(d, node, d->rule_any);
  5250. if (tag < BR_TAG_MAX)
  5251. rule_scan_list(d, node, d->rule_tag[tag]);
  5252. if (id[0])
  5253. rule_scan_list(d, node,
  5254. d->rule_bucket[rule_hash(id, s_len(id))]);
  5255. i = 0;
  5256. while (cls[i]) {
  5257. unsigned int st, n;
  5258. while (cls[i] && is_space(cls[i]))
  5259. i++;
  5260. st = i;
  5261. while (cls[i] && !is_space(cls[i]))
  5262. i++;
  5263. n = i - st;
  5264. if (n)
  5265. rule_scan_class(d, node, cls + st, n);
  5266. }
  5267. /* Rules must cascade in document order even though the index visits
  5268. * them in bucket order. */
  5269. for (i = 1; i < nrule_matches; i++) {
  5270. struct rule_match m = rule_matches[i];
  5271. unsigned int j = i;
  5272. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  5273. rule_matches[j] = rule_matches[j - 1];
  5274. j--;
  5275. }
  5276. rule_matches[j] = m;
  5277. }
  5278. }
  5279. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  5280. int font_px)
  5281. {
  5282. if (*kind == 4) {
  5283. *val = (unsigned short)(*val * font_px / 100);
  5284. *kind = 1;
  5285. } else if (*kind == 5) {
  5286. *val = (unsigned short)(*val * root_font_px / 100);
  5287. *kind = 1;
  5288. }
  5289. }
  5290. static void resolve_size_units(struct br_style *st)
  5291. {
  5292. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  5293. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  5294. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  5295. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  5296. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  5297. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  5298. }
  5299. static void set_border_side_w(struct br_style *st, int side, int v)
  5300. {
  5301. if (v < 0)
  5302. v = 0;
  5303. if (v > 32)
  5304. v = 32;
  5305. st->border_w[side] = (unsigned char)v;
  5306. if (st->border_w[side] && !st->border_c[side])
  5307. st->border_c[side] = st->color;
  5308. }
  5309. static void set_border_side_c(struct br_style *st, int side, int v)
  5310. {
  5311. st->border_c[side] = (unsigned short)v;
  5312. }
  5313. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  5314. {
  5315. switch (dcl->prop) {
  5316. case PROP_COLOR:
  5317. st->color = (unsigned short)dcl->val;
  5318. break;
  5319. case PROP_BG:
  5320. st->bg = (unsigned short)dcl->val;
  5321. st->has_bg = 1;
  5322. break;
  5323. case PROP_BG_IMAGE:
  5324. st->bg_image = (unsigned short)dcl->val;
  5325. break;
  5326. case PROP_FONT_PX:
  5327. st->font_raw = (unsigned short)dcl->val;
  5328. break;
  5329. case PROP_FONT_UNIT:
  5330. st->font_unit = (unsigned char)dcl->val;
  5331. break;
  5332. case PROP_BOLD:
  5333. st->bold = (unsigned char)(dcl->val != 0);
  5334. break;
  5335. case PROP_ITALIC:
  5336. st->italic = (unsigned char)(dcl->val != 0);
  5337. break;
  5338. case PROP_MONO:
  5339. st->mono = (unsigned char)(dcl->val != 0);
  5340. break;
  5341. case PROP_FONT_FACE:
  5342. st->font_face = (unsigned char)dcl->val;
  5343. break;
  5344. case PROP_ALIGN:
  5345. st->align = (unsigned char)dcl->val;
  5346. break;
  5347. case PROP_UNDERLINE:
  5348. st->underline = (unsigned char)(dcl->val != 0);
  5349. break;
  5350. case PROP_DISPLAY:
  5351. st->display = (unsigned char)dcl->val;
  5352. st->list_item = 0;
  5353. break;
  5354. case PROP_VIS_HIDDEN:
  5355. st->vis_hidden = (unsigned char)(dcl->val != 0);
  5356. break;
  5357. case PROP_OPACITY_HIDDEN:
  5358. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  5359. break;
  5360. case PROP_OVERFLOW_HIDDEN:
  5361. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  5362. break;
  5363. case PROP_MT: st->mt = dcl->val; break;
  5364. case PROP_MR: st->mr = dcl->val; break;
  5365. case PROP_MB: st->mb = dcl->val; break;
  5366. case PROP_ML: st->ml = dcl->val; break;
  5367. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  5368. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  5369. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  5370. break;
  5371. case PROP_PT: st->pt = dcl->val; break;
  5372. case PROP_PR: st->pr = dcl->val; break;
  5373. case PROP_PB: st->pb = dcl->val; break;
  5374. case PROP_PL: st->pl = dcl->val; break;
  5375. case PROP_MT_U:
  5376. case PROP_MR_U:
  5377. case PROP_MB_U:
  5378. case PROP_ML_U:
  5379. case PROP_PT_U:
  5380. case PROP_PR_U:
  5381. case PROP_PB_U:
  5382. case PROP_PL_U:
  5383. st->mp_unit[dcl->prop - PROP_MT_U] =
  5384. (unsigned char)dcl->val;
  5385. break;
  5386. case PROP_BORDER_W:
  5387. set_border_side_w(st, 0, dcl->val);
  5388. set_border_side_w(st, 1, dcl->val);
  5389. set_border_side_w(st, 2, dcl->val);
  5390. set_border_side_w(st, 3, dcl->val);
  5391. break;
  5392. case PROP_BORDER_COLOR:
  5393. set_border_side_c(st, 0, dcl->val);
  5394. set_border_side_c(st, 1, dcl->val);
  5395. set_border_side_c(st, 2, dcl->val);
  5396. set_border_side_c(st, 3, dcl->val);
  5397. break;
  5398. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  5399. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  5400. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  5401. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  5402. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  5403. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  5404. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  5405. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  5406. case PROP_RADIUS_TL:
  5407. case PROP_RADIUS_TR:
  5408. case PROP_RADIUS_BR:
  5409. case PROP_RADIUS_BL:
  5410. st->radius[dcl->prop - PROP_RADIUS_TL] =
  5411. (unsigned char)dcl->val;
  5412. break;
  5413. case PROP_LIST_NONE:
  5414. st->no_bullet = (unsigned char)(dcl->val != 0);
  5415. break;
  5416. case PROP_NOWRAP:
  5417. st->nowrap = (unsigned char)(dcl->val != 0);
  5418. break;
  5419. case PROP_WIDTH:
  5420. st->width = (unsigned short)dcl->val;
  5421. break;
  5422. case PROP_WIDTH_KIND:
  5423. st->width_kind = (unsigned char)dcl->val;
  5424. break;
  5425. case PROP_WIDTH_CALC_PCT:
  5426. st->width_calc_pct = dcl->val;
  5427. break;
  5428. case PROP_WIDTH_CALC_PX:
  5429. st->width_calc_px = dcl->val;
  5430. break;
  5431. case PROP_MAXW:
  5432. st->max_width = (unsigned short)dcl->val;
  5433. break;
  5434. case PROP_MAXW_KIND:
  5435. st->max_width_kind = (unsigned char)dcl->val;
  5436. break;
  5437. case PROP_MINW:
  5438. st->min_width = (unsigned short)dcl->val;
  5439. break;
  5440. case PROP_MINW_KIND:
  5441. st->min_width_kind = (unsigned char)dcl->val;
  5442. break;
  5443. case PROP_HEIGHT:
  5444. st->height = (unsigned short)dcl->val;
  5445. break;
  5446. case PROP_HEIGHT_KIND:
  5447. st->height_kind = (unsigned char)dcl->val;
  5448. break;
  5449. case PROP_MINH:
  5450. st->min_height = (unsigned short)dcl->val;
  5451. break;
  5452. case PROP_MINH_KIND:
  5453. st->min_height_kind = (unsigned char)dcl->val;
  5454. break;
  5455. case PROP_MAXH:
  5456. st->max_height = (unsigned short)dcl->val;
  5457. break;
  5458. case PROP_MAXH_KIND:
  5459. st->max_height_kind = (unsigned char)dcl->val;
  5460. break;
  5461. case PROP_LH_PCT:
  5462. st->line_h_pct = (unsigned char)dcl->val;
  5463. break;
  5464. case PROP_LH_PX:
  5465. st->line_h_px = (unsigned short)dcl->val;
  5466. break;
  5467. case PROP_UPPER:
  5468. st->uppercase = (unsigned char)(dcl->val != 0);
  5469. break;
  5470. case PROP_FLOAT:
  5471. st->float_left = (unsigned char)dcl->val;
  5472. break;
  5473. case PROP_CLEAR:
  5474. st->clear = (unsigned char)dcl->val;
  5475. break;
  5476. case PROP_FLEX_GROW:
  5477. st->flex_grow = (unsigned char)dcl->val;
  5478. break;
  5479. case PROP_FLEX_COL:
  5480. st->flex_col = (unsigned char)(dcl->val != 0);
  5481. break;
  5482. case PROP_FLEX_WRAP:
  5483. st->flex_wrap = (unsigned char)(dcl->val != 0);
  5484. break;
  5485. case PROP_JUSTIFY:
  5486. st->justify = (unsigned char)dcl->val;
  5487. break;
  5488. case PROP_ALIGN_ITEMS:
  5489. st->align_items = (unsigned char)dcl->val;
  5490. break;
  5491. case PROP_GRID_NCOLS:
  5492. st->grid_ncols = (unsigned char)dcl->val;
  5493. break;
  5494. case PROP_GRID_GAP:
  5495. st->grid_gap = (unsigned char)dcl->val;
  5496. break;
  5497. case PROP_GRID_SLOT:
  5498. st->grid_slot = (unsigned char)dcl->val;
  5499. break;
  5500. case PROP_GRID_AUTO:
  5501. st->grid_auto = (unsigned char)(dcl->val != 0);
  5502. break;
  5503. case PROP_POS:
  5504. st->pos = (unsigned char)dcl->val;
  5505. break;
  5506. case PROP_LEFT:
  5507. st->left = dcl->val;
  5508. break;
  5509. case PROP_TOP:
  5510. st->top = dcl->val;
  5511. break;
  5512. case PROP_RIGHT:
  5513. st->right = dcl->val;
  5514. break;
  5515. case PROP_BOTTOM:
  5516. st->bottom = dcl->val;
  5517. break;
  5518. case PROP_LEFT_SET:
  5519. st->left_set = (unsigned char)(dcl->val != 0);
  5520. break;
  5521. case PROP_TOP_SET:
  5522. st->top_set = (unsigned char)(dcl->val != 0);
  5523. break;
  5524. case PROP_RIGHT_SET:
  5525. st->right_set = (unsigned char)(dcl->val != 0);
  5526. break;
  5527. case PROP_BOTTOM_SET:
  5528. st->bottom_set = (unsigned char)(dcl->val != 0);
  5529. break;
  5530. case PROP_ZINDEX:
  5531. st->z_index = dcl->val;
  5532. break;
  5533. case PROP_ZSET:
  5534. st->z_set = (unsigned char)(dcl->val != 0);
  5535. break;
  5536. case PROP_TRANSFORM_X:
  5537. st->tx = dcl->val;
  5538. break;
  5539. case PROP_TRANSFORM_Y:
  5540. st->ty = dcl->val;
  5541. break;
  5542. case PROP_TRANSFORM_SET:
  5543. st->transform_set = (unsigned char)(dcl->val != 0);
  5544. break;
  5545. default:
  5546. break;
  5547. }
  5548. }
  5549. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  5550. int spec, short *best, short *best_imp)
  5551. {
  5552. unsigned char p = dcl->prop;
  5553. if (p >= PROP_COUNT)
  5554. return;
  5555. if (dcl->important) {
  5556. if (spec >= best_imp[p]) {
  5557. apply_decl(st, dcl);
  5558. best_imp[p] = (short)spec;
  5559. }
  5560. } else if (best_imp[p] < 0 && spec >= best[p]) {
  5561. apply_decl(st, dcl);
  5562. best[p] = (short)spec;
  5563. }
  5564. }
  5565. void br_style_for(struct br_doc *d, unsigned int node,
  5566. const struct br_style *parent, struct br_style *out)
  5567. {
  5568. unsigned int tag;
  5569. short best[PROP_COUNT], best_imp[PROP_COUNT];
  5570. unsigned int i;
  5571. if (d->nodes[node].style_valid) {
  5572. *out = d->nodes[node].style;
  5573. return;
  5574. }
  5575. *out = *parent;
  5576. out->has_bg = 0;
  5577. out->bg_image = 0;
  5578. out->float_left = 0;
  5579. out->clear = 0;
  5580. out->list_item = 0;
  5581. out->font_unit = 0;
  5582. for (i = 0; i < 4; i++) {
  5583. out->border_w[i] = 0;
  5584. out->border_c[i] = 0;
  5585. out->radius[i] = 0;
  5586. }
  5587. out->width = out->max_width = out->min_width = out->height = 0;
  5588. out->min_height = out->max_height = 0;
  5589. out->width_calc_pct = out->width_calc_px = 0;
  5590. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  5591. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  5592. out->box_sizing = 0;
  5593. out->ml_auto = out->mr_auto = 0;
  5594. out->mt = out->mr = out->mb = out->ml = 0;
  5595. out->pt = out->pr = out->pb = out->pl = 0;
  5596. for (i = 0; i < 8; i++)
  5597. out->mp_unit[i] = 0;
  5598. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  5599. out->justify = out->align_items = 0;
  5600. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  5601. out->grid_auto = 0;
  5602. out->pos = out->left_set = out->top_set = 0;
  5603. out->right_set = out->bottom_set = 0;
  5604. out->left = out->top = out->right = out->bottom = 0;
  5605. out->z_index = 0;
  5606. out->z_set = 0;
  5607. out->tx = out->ty = 0;
  5608. out->transform_set = 0;
  5609. out->vis_hidden = out->opacity_hidden = 0;
  5610. out->overflow_hidden = 0;
  5611. tag = d->nodes[node].tag;
  5612. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  5613. BR_DISPLAY_INLINE;
  5614. for (i = 0; i < PROP_COUNT; i++) {
  5615. best[i] = -1;
  5616. best_imp[i] = -1;
  5617. }
  5618. switch (tag) {
  5619. case BR_TAG_HEAD:
  5620. case BR_TAG_TITLE:
  5621. case BR_TAG_META:
  5622. case BR_TAG_LINK:
  5623. case BR_TAG_BASE:
  5624. case BR_TAG_SCRIPT:
  5625. case BR_TAG_STYLE:
  5626. out->display = BR_DISPLAY_NONE;
  5627. break;
  5628. case BR_TAG_BODY:
  5629. out->mt = out->mb = out->ml = out->mr = 4;
  5630. break;
  5631. case BR_TAG_P:
  5632. out->mt = out->mb = 8;
  5633. break;
  5634. case BR_TAG_H1:
  5635. out->font_px = 32; out->bold = 1;
  5636. out->mt = 10; out->mb = 6;
  5637. break;
  5638. case BR_TAG_H2:
  5639. out->font_px = 24; out->bold = 1;
  5640. out->mt = 8; out->mb = 4;
  5641. break;
  5642. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  5643. out->bold = 1; out->mt = 6; out->mb = 3;
  5644. if (tag == BR_TAG_H3)
  5645. out->font_px = 19;
  5646. else if (tag == BR_TAG_H4)
  5647. out->font_px = 17;
  5648. else if (tag == BR_TAG_H5)
  5649. out->font_px = 15;
  5650. else
  5651. out->font_px = 13;
  5652. break;
  5653. case BR_TAG_UL: case BR_TAG_OL:
  5654. out->mt = out->mb = 6; out->pl = 20;
  5655. break;
  5656. case BR_TAG_LI:
  5657. out->mt = out->mb = 2;
  5658. out->list_item = 1;
  5659. break;
  5660. case BR_TAG_BLOCKQUOTE:
  5661. out->mt = out->mb = 6; out->ml = out->mr = 16;
  5662. break;
  5663. case BR_TAG_PRE:
  5664. out->mt = out->mb = 8; out->pl = 8;
  5665. out->has_bg = 1; out->bg = theme_panel;
  5666. out->mono = 1;
  5667. break;
  5668. case BR_TAG_CENTER:
  5669. out->align = BR_ALIGN_CENTER;
  5670. break;
  5671. case BR_TAG_A:
  5672. out->color = theme_link; out->underline = 1;
  5673. break;
  5674. case BR_TAG_B: case BR_TAG_STRONG:
  5675. out->bold = 1;
  5676. break;
  5677. case BR_TAG_I: case BR_TAG_EM:
  5678. out->italic = 1;
  5679. break;
  5680. case BR_TAG_CODE:
  5681. out->color = 0x8010;
  5682. out->mono = 1;
  5683. break;
  5684. case BR_TAG_SMALL:
  5685. out->font_px = 13;
  5686. break;
  5687. case BR_TAG_BIG:
  5688. out->font_px = 20;
  5689. break;
  5690. case BR_TAG_TD: case BR_TAG_TH:
  5691. out->pr = 12;
  5692. break;
  5693. case BR_TAG_VIDEO:
  5694. out->display = BR_DISPLAY_BLOCK;
  5695. break;
  5696. default:
  5697. break;
  5698. }
  5699. /* presentational <font color>: author CSS below still wins */
  5700. if (d->nodes[node].color_off) {
  5701. unsigned short c;
  5702. const char *v = attr_ptr(d, d->nodes[node].color_off);
  5703. if (parse_color(v, s_len(v), &c))
  5704. out->color = c;
  5705. }
  5706. rule_collect(d, node);
  5707. for (i = 0; i < nrule_matches; i++) {
  5708. struct br_rule *r = &d->rules[rule_matches[i].idx];
  5709. int spec = rule_matches[i].spec;
  5710. unsigned int k;
  5711. for (k = 0; k < r->nd; k++)
  5712. cascade_decl(out, &r->d[k], spec, best, best_imp);
  5713. }
  5714. if (d->nodes[node].style_off) {
  5715. struct br_decl decls[BR_DECLS];
  5716. unsigned int nd, k;
  5717. const char *v = attr_ptr(d, d->nodes[node].style_off);
  5718. parse_decls(d, v, s_len(v), decls, &nd);
  5719. for (k = 0; k < nd; k++)
  5720. cascade_decl(out, &decls[k], 1000, best, best_imp);
  5721. }
  5722. if (out->font_unit) {
  5723. int px = out->font_raw;
  5724. if (out->font_unit == 2)
  5725. px = parent->font_px * out->font_raw / 100;
  5726. else if (out->font_unit == 3)
  5727. px = root_font_px * out->font_raw / 100;
  5728. else if (out->font_unit == 4)
  5729. px = parent->font_px * out->font_raw / 100;
  5730. if (px < 8)
  5731. px = 8;
  5732. if (px > 72)
  5733. px = 72;
  5734. out->font_px = (unsigned char)px;
  5735. }
  5736. resolve_size_units(out);
  5737. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  5738. out->display = BR_DISPLAY_NONE;
  5739. if (out->vis_hidden || out->opacity_hidden ||
  5740. (out->overflow_hidden && out->height_kind == 1 &&
  5741. out->height == 0))
  5742. out->display = BR_DISPLAY_NONE;
  5743. d->nodes[node].style = *out;
  5744. d->nodes[node].style_valid = 1;
  5745. }
  5746. /* ---- HTML parser ---- */
  5747. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  5748. unsigned int sp, int *pre)
  5749. {
  5750. unsigned int i;
  5751. *pre = 0;
  5752. for (i = 1; i <= sp; i++)
  5753. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  5754. (*pre)++;
  5755. }
  5756. static void implicit_close(struct br_doc *d, unsigned short *stack,
  5757. unsigned int *sp, unsigned int tag)
  5758. {
  5759. while (*sp > 0) {
  5760. unsigned int top = d->nodes[stack[*sp]].tag;
  5761. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  5762. (*sp)--;
  5763. continue;
  5764. }
  5765. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  5766. (*sp)--;
  5767. continue;
  5768. }
  5769. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  5770. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  5771. (*sp)--;
  5772. continue;
  5773. }
  5774. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  5775. (*sp)--;
  5776. continue;
  5777. }
  5778. break;
  5779. }
  5780. }
  5781. static int has_attr_token(const char *s, unsigned int len, const char *name)
  5782. {
  5783. unsigned int i, n = s_len(name);
  5784. for (i = 0; i + n <= len; i++) {
  5785. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  5786. continue;
  5787. if (!ci_eq_n(s + i, name, n))
  5788. continue;
  5789. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  5790. return 1;
  5791. }
  5792. return 0;
  5793. }
  5794. static int stack_has_svg(const struct br_doc *d,
  5795. const unsigned short *stack, unsigned int sp)
  5796. {
  5797. unsigned int k;
  5798. for (k = 0; k <= sp && k < 64; k++)
  5799. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  5800. return 1;
  5801. return 0;
  5802. }
  5803. /* Skip an element subtree by finding its closing tag. Used for SVG
  5804. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  5805. * overwrite the document title. */
  5806. static unsigned int skip_element(const char *html, unsigned int len,
  5807. unsigned int i, const char *name,
  5808. unsigned int n)
  5809. {
  5810. char close[40];
  5811. unsigned int k = 0, p;
  5812. close[k++] = '<';
  5813. close[k++] = '/';
  5814. while (k < sizeof(close) - 1 && n--)
  5815. close[k++] = *name++;
  5816. p = (unsigned int)find_ci(html, len, close, k, i);
  5817. if ((int)p < 0)
  5818. return len;
  5819. p += k;
  5820. while (p < len && html[p] != '>')
  5821. p++;
  5822. if (p < len)
  5823. p++;
  5824. return p;
  5825. }
  5826. /* Store a <script src=...> value in the attribute arena. Kept separate from
  5827. * parse_attrs() so script-only attributes cannot be confused with the
  5828. * value/d attributes that also use src_off on other elements. */
  5829. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  5830. unsigned int len)
  5831. {
  5832. unsigned int i = 0;
  5833. while (i < len) {
  5834. unsigned int ns, ne, vs, ve;
  5835. char quote = 0;
  5836. while (i < len && is_space(s[i]))
  5837. i++;
  5838. if (i >= len || s[i] == '/')
  5839. break;
  5840. ns = i;
  5841. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  5842. i++;
  5843. ne = i;
  5844. while (i < len && is_space(s[i]))
  5845. i++;
  5846. if (i >= len || s[i] != '=')
  5847. continue;
  5848. i++;
  5849. while (i < len && is_space(s[i]))
  5850. i++;
  5851. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  5852. quote = s[i++];
  5853. vs = i;
  5854. while (i < len && s[i] != quote)
  5855. i++;
  5856. ve = i;
  5857. if (i < len)
  5858. i++;
  5859. } else {
  5860. vs = i;
  5861. while (i < len && !is_space(s[i]) && s[i] != '>')
  5862. i++;
  5863. ve = i;
  5864. }
  5865. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  5866. return store_attr(d, s + vs, ve - vs, 400);
  5867. }
  5868. return 0;
  5869. }
  5870. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  5871. * application/json, text/template, ...) and non-JS type values. External
  5872. * src scripts are allowed; the browser fetches them separately. */
  5873. static int script_is_javascript(const char *s, unsigned int len)
  5874. {
  5875. unsigned int i = 0;
  5876. while (i < len) {
  5877. unsigned int ns, ne, vs = 0, ve = 0;
  5878. char quote = 0;
  5879. int has_val = 0;
  5880. while (i < len && is_space(s[i]))
  5881. i++;
  5882. if (i >= len)
  5883. break;
  5884. ns = i;
  5885. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  5886. i++;
  5887. ne = i;
  5888. while (i < len && is_space(s[i]))
  5889. i++;
  5890. if (i < len && s[i] == '=') {
  5891. i++;
  5892. while (i < len && is_space(s[i]))
  5893. i++;
  5894. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  5895. quote = s[i++];
  5896. vs = i;
  5897. while (i < len && s[i] != quote)
  5898. i++;
  5899. ve = i;
  5900. if (i < len)
  5901. i++;
  5902. } else {
  5903. vs = i;
  5904. while (i < len && !is_space(s[i]) && s[i] != '/')
  5905. i++;
  5906. ve = i;
  5907. }
  5908. has_val = 1;
  5909. } else if (i == ne) {
  5910. i++;
  5911. }
  5912. if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
  5913. unsigned int vn = has_val ? ve - vs : 0;
  5914. if (vn == 0)
  5915. return 1;
  5916. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  5917. (vn == 22 &&
  5918. ci_eq_n(s + vs, "application/javascript", 22)) ||
  5919. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  5920. (vn == 22 &&
  5921. ci_eq_n(s + vs, "application/ecmascript", 22)))
  5922. return 1;
  5923. return 0;
  5924. }
  5925. }
  5926. return 1;
  5927. }
  5928. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  5929. {
  5930. unsigned short stack[64];
  5931. unsigned int sp = 0;
  5932. unsigned int i = 0;
  5933. int pre = 0;
  5934. unsigned int css_len = 0;
  5935. if (!d)
  5936. return -1;
  5937. if (!d->nodes)
  5938. br_doc_init(d);
  5939. doc_reset(d);
  5940. d->script_src = html;
  5941. stack[0] = 0;
  5942. d->nodes[0].type = BR_NODE_ELEM;
  5943. d->nodes[0].tag = BR_TAG_UNKNOWN;
  5944. while (i < len) {
  5945. if (html[i] != '<') {
  5946. unsigned int st = i;
  5947. while (i < len && html[i] != '<')
  5948. i++;
  5949. if (sp <= 63)
  5950. append_text(d, stack[sp], html + st, i - st, pre);
  5951. continue;
  5952. }
  5953. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  5954. html[i + 3] == '-') {
  5955. int p = find_ci(html, len, "-->", 3, i + 4);
  5956. i = p >= 0 ? (unsigned int)p + 3 : len;
  5957. continue;
  5958. }
  5959. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  5960. while (i < len && html[i] != '>')
  5961. i++;
  5962. if (i < len)
  5963. i++;
  5964. continue;
  5965. }
  5966. if (i + 1 < len && html[i + 1] == '/') {
  5967. unsigned int ns = i + 2, ne, tag, k, n;
  5968. char name[32];
  5969. while (ns < len && is_space(html[ns]))
  5970. ns++;
  5971. ne = ns;
  5972. while (ne < len && is_ident(html[ne]))
  5973. ne++;
  5974. if (ne == ns) {
  5975. i++;
  5976. continue;
  5977. }
  5978. n = ne - ns;
  5979. if (n >= sizeof(name))
  5980. n = sizeof(name) - 1;
  5981. for (k = 0; k < n; k++)
  5982. name[k] = html[ns + k];
  5983. name[n] = '\0';
  5984. tag = br_tag_from_name(name);
  5985. i = ne;
  5986. while (i < len && html[i] != '>')
  5987. i++;
  5988. if (i < len)
  5989. i++;
  5990. for (k = sp; k > 0; k--) {
  5991. struct br_node *nd = &d->nodes[stack[k]];
  5992. if (tag == BR_TAG_UNKNOWN) {
  5993. const char *raw = attr_ptr(d, nd->name_off);
  5994. if (nd->tag == BR_TAG_UNKNOWN &&
  5995. ci_eq_n(raw, name, n) &&
  5996. raw[n] == '\0') {
  5997. sp = k - 1;
  5998. break;
  5999. }
  6000. } else if (nd->tag == tag) {
  6001. sp = k - 1;
  6002. break;
  6003. }
  6004. }
  6005. stack_recount_pre(d, stack, sp, &pre);
  6006. continue;
  6007. }
  6008. /* start tag */
  6009. {
  6010. unsigned int ns = i + 1, ne, tag, k, n;
  6011. char name[16];
  6012. unsigned int attr_start, attr_end;
  6013. int selfclose = 0;
  6014. while (ns < len && is_space(html[ns]))
  6015. ns++;
  6016. ne = ns;
  6017. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  6018. ne++;
  6019. if (ne == ns) {
  6020. /* stray '<' */
  6021. append_text(d, stack[sp], html + i, 1, pre);
  6022. i++;
  6023. continue;
  6024. }
  6025. n = ne - ns;
  6026. if (n >= sizeof(name))
  6027. n = sizeof(name) - 1;
  6028. for (k = 0; k < n; k++)
  6029. name[k] = html[ns + k];
  6030. name[n] = '\0';
  6031. tag = br_tag_from_name(name);
  6032. attr_start = ne;
  6033. {
  6034. unsigned int q = ne;
  6035. char quote = 0;
  6036. while (q < len) {
  6037. char c = html[q];
  6038. if (quote) {
  6039. if (c == quote)
  6040. quote = 0;
  6041. } else if (c == '"' || c == '\'') {
  6042. quote = c;
  6043. } else if (c == '>') {
  6044. break;
  6045. }
  6046. q++;
  6047. }
  6048. attr_end = q;
  6049. if (q > attr_start && html[q - 1] == '/')
  6050. selfclose = 1;
  6051. i = q < len ? q + 1 : len;
  6052. }
  6053. if (tag == BR_TAG_SCRIPT) {
  6054. int p = find_ci(html, len, "</script", 8, i);
  6055. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6056. if (script_is_javascript(html + attr_start,
  6057. attr_end - attr_start)) {
  6058. if (d->nscripts < BR_MAX_SCRIPTS) {
  6059. struct br_script *script =
  6060. &d->scripts[d->nscripts++];
  6061. script->off = i;
  6062. script->len = end > i ? end - i : 0;
  6063. script->src_off = script_src_attr(
  6064. d, html + attr_start,
  6065. attr_end - attr_start);
  6066. } else {
  6067. d->scripts_truncated = 1;
  6068. }
  6069. }
  6070. i = p >= 0 ? (unsigned int)p + 8 : len;
  6071. while (i < len && html[i] != '>')
  6072. i++;
  6073. if (i < len)
  6074. i++;
  6075. continue;
  6076. }
  6077. if (stack_has_svg(d, stack, sp) &&
  6078. (tag == BR_TAG_TITLE ||
  6079. (tag == BR_TAG_UNKNOWN &&
  6080. (ci_eq_n(name, "desc", 4) ||
  6081. ci_eq_n(name, "metadata", 8))))) {
  6082. i = skip_element(html, len, i, name, n);
  6083. continue;
  6084. }
  6085. if (tag == BR_TAG_DETAILS && !selfclose &&
  6086. !has_attr_token(html + attr_start,
  6087. attr_end - attr_start, "open")) {
  6088. /* Closed <details> is collapsed (and this browser has
  6089. * no script to open it); skipping the subtree keeps
  6090. * mobile mega-menus out of the node arena. */
  6091. int p = find_ci(html, len, "</details>", 10, i);
  6092. i = p >= 0 ? (unsigned int)p + 10 : len;
  6093. continue;
  6094. }
  6095. if (tag == BR_TAG_STYLE) {
  6096. int p = find_ci(html, len, "</style", 7, i);
  6097. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6098. unsigned int cend = end;
  6099. while (cend > i && is_space(html[cend - 1]))
  6100. cend--;
  6101. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  6102. for (k = i; k < cend; k++)
  6103. css_store[css_len++] = html[k];
  6104. css_store[css_len++] = '\n';
  6105. }
  6106. i = p >= 0 ? (unsigned int)p + 7 : len;
  6107. while (i < len && html[i] != '>')
  6108. i++;
  6109. if (i < len)
  6110. i++;
  6111. continue;
  6112. }
  6113. if (tag == BR_TAG_TITLE) {
  6114. int p = find_ci(html, len, "</title", 7, i);
  6115. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6116. unsigned int tn = 0;
  6117. char tmp[BR_TITLE_MAX];
  6118. tn = decode_text(html + i, end - i, tmp,
  6119. sizeof(tmp), 1);
  6120. (void)tn;
  6121. s_copy(d->title, tmp, sizeof(d->title));
  6122. i = p >= 0 ? (unsigned int)p + 7 : len;
  6123. while (i < len && html[i] != '>')
  6124. i++;
  6125. if (i < len)
  6126. i++;
  6127. continue;
  6128. }
  6129. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  6130. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  6131. unsigned int idx;
  6132. implicit_close(d, stack, &sp, tag);
  6133. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  6134. (unsigned char)tag);
  6135. if (idx)
  6136. parse_attrs(d, &d->nodes[idx],
  6137. html + attr_start,
  6138. attr_end - attr_start);
  6139. continue;
  6140. }
  6141. if (sp >= 63) {
  6142. d->truncated = 1;
  6143. continue;
  6144. }
  6145. implicit_close(d, stack, &sp, tag);
  6146. {
  6147. unsigned int idx = node_append(d, stack[sp],
  6148. BR_NODE_ELEM,
  6149. (unsigned char)tag);
  6150. if (!idx)
  6151. continue;
  6152. parse_attrs(d, &d->nodes[idx],
  6153. html + attr_start,
  6154. attr_end - attr_start);
  6155. if (tag == BR_TAG_UNKNOWN)
  6156. d->nodes[idx].name_off =
  6157. store_attr(d, name, n, 31);
  6158. if (!selfclose) {
  6159. sp++;
  6160. stack[sp] = (unsigned short)idx;
  6161. if (tag == BR_TAG_PRE)
  6162. pre++;
  6163. }
  6164. }
  6165. }
  6166. }
  6167. d->inline_css_len = css_len;
  6168. return 0;
  6169. }
  6170. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  6171. {
  6172. if (!d->nodes)
  6173. br_doc_init(d);
  6174. doc_reset(d);
  6175. d->plain = 1;
  6176. d->nodes[0].type = BR_NODE_ELEM;
  6177. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6178. append_text(d, 0, text, len, 1);
  6179. return 0;
  6180. }
  6181. /* A standalone image response: a synthetic document with one <img>. The
  6182. * caller decodes the already-fetched body into image slot 0 and sets the
  6183. * returned node's img_index. */
  6184. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  6185. {
  6186. unsigned int idx;
  6187. if (!d->nodes)
  6188. br_doc_init(d);
  6189. doc_reset(d);
  6190. d->nodes[0].type = BR_NODE_ELEM;
  6191. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6192. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  6193. if (!idx)
  6194. return 0;
  6195. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  6196. return idx;
  6197. }