browser_html.c 175 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312
  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_BG_REPEAT,
  2051. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2052. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2053. PROP_FONT_PX, PROP_FONT_UNIT,
  2054. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2055. PROP_ALIGN, PROP_UNDERLINE,
  2056. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2057. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2058. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2059. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2060. PROP_BORDER_W, PROP_BORDER_COLOR,
  2061. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2062. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2063. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2064. PROP_LIST_NONE, PROP_NOWRAP,
  2065. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2066. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2067. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2068. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2069. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2070. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2071. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2072. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2073. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2074. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2075. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2076. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2077. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2078. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2079. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2080. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2081. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2082. PROP_ZINDEX, PROP_ZSET,
  2083. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2084. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2085. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2086. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2087. PROP_COUNT
  2088. };
  2089. struct color_name {
  2090. const char *name;
  2091. unsigned short rgb;
  2092. };
  2093. static const struct color_name color_names[] = {
  2094. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2095. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2096. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2097. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2098. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2099. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2100. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2101. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2102. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2103. { "darkgrey", 0x528A }, { 0, 0 }
  2104. };
  2105. static int hexval(char c)
  2106. {
  2107. if (c >= '0' && c <= '9')
  2108. return c - '0';
  2109. if (c >= 'a' && c <= 'f')
  2110. return c - 'a' + 10;
  2111. if (c >= 'A' && c <= 'F')
  2112. return c - 'A' + 10;
  2113. return -1;
  2114. }
  2115. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2116. {
  2117. unsigned int i;
  2118. for (i = 0; color_names[i].name; i++) {
  2119. unsigned int j, n = s_len(color_names[i].name);
  2120. if (n != len)
  2121. continue;
  2122. for (j = 0; j < n; j++) {
  2123. char c = s[j];
  2124. if (c >= 'A' && c <= 'Z')
  2125. c = (char)(c - 'A' + 'a');
  2126. if (c != color_names[i].name[j])
  2127. break;
  2128. }
  2129. if (j == n) {
  2130. *out = color_names[i].rgb;
  2131. return 1;
  2132. }
  2133. }
  2134. return 0;
  2135. }
  2136. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2137. * over white; the render target is a white page. */
  2138. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2139. {
  2140. unsigned int i = 0;
  2141. while (i < len && is_space(s[i]))
  2142. i++;
  2143. if (i < len && s[i] == '#') {
  2144. int d[6], nd = 0;
  2145. i++;
  2146. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2147. d[nd++] = hexval(s[i++]);
  2148. if (nd == 3) {
  2149. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2150. unsigned int b = (unsigned int)d[2];
  2151. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2152. ((g * 17 >> 2) << 5) |
  2153. (b * 17 >> 3));
  2154. return 1;
  2155. }
  2156. if (nd >= 6) {
  2157. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2158. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2159. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2160. *out = (unsigned short)(((r >> 3) << 11) |
  2161. ((g >> 2) << 5) | (b >> 3));
  2162. return 1;
  2163. }
  2164. return 0;
  2165. }
  2166. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2167. unsigned int v[3], k = 0, a = 255;
  2168. while (i < len && s[i] != '(')
  2169. i++;
  2170. if (i >= len)
  2171. return 0;
  2172. i++;
  2173. while (k < 3 && i < len) {
  2174. unsigned int x = 0;
  2175. int percent = 0;
  2176. while (i < len && (is_space(s[i]) || s[i] == ','))
  2177. i++;
  2178. if (i >= len || s[i] < '0' || s[i] > '9')
  2179. return 0;
  2180. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2181. x = x * 10 + (unsigned int)(s[i] - '0');
  2182. i++;
  2183. }
  2184. if (i < len && s[i] == '%') {
  2185. percent = 1;
  2186. i++;
  2187. }
  2188. if (percent)
  2189. x = x * 255 / 100;
  2190. if (x > 255)
  2191. x = 255;
  2192. v[k++] = x;
  2193. }
  2194. /* Optional alpha in both syntaxes:
  2195. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2196. {
  2197. unsigned int whole = 0, frac = 0, scale = 1;
  2198. int percent = 0;
  2199. while (i < len && (is_space(s[i]) || s[i] == ','))
  2200. i++;
  2201. if (i < len && s[i] == '/') {
  2202. i++;
  2203. while (i < len && is_space(s[i]))
  2204. i++;
  2205. }
  2206. if (i < len && s[i] >= '0' && s[i] <= '9') {
  2207. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2208. whole = whole * 10 +
  2209. (unsigned int)(s[i] - '0');
  2210. i++;
  2211. }
  2212. if (i < len && s[i] == '.') {
  2213. i++;
  2214. while (i < len && s[i] >= '0' &&
  2215. s[i] <= '9') {
  2216. if (scale < 1000) {
  2217. frac = frac * 10 +
  2218. (unsigned int)(s[i] - '0');
  2219. scale *= 10;
  2220. }
  2221. i++;
  2222. }
  2223. }
  2224. while (i < len && is_space(s[i]))
  2225. i++;
  2226. if (i < len && s[i] == '%')
  2227. percent = 1;
  2228. if (percent)
  2229. a = whole > 100 ? 255 :
  2230. whole * 255 / 100;
  2231. else if (whole >= 1)
  2232. a = 255;
  2233. else
  2234. a = frac * 255 / scale;
  2235. }
  2236. }
  2237. if (k == 3) {
  2238. unsigned int r = v[0], g = v[1], b = v[2];
  2239. if (a < 255) {
  2240. r = (r * a + 255u * (255u - a)) / 255u;
  2241. g = (g * a + 255u * (255u - a)) / 255u;
  2242. b = (b * a + 255u * (255u - a)) / 255u;
  2243. }
  2244. *out = (unsigned short)(((r >> 3) << 11) |
  2245. ((g >> 2) << 5) | (b >> 3));
  2246. return 1;
  2247. }
  2248. return 0;
  2249. }
  2250. {
  2251. unsigned int j = i;
  2252. while (i < len && !is_space(s[i]))
  2253. i++;
  2254. return named_color(s + j, i - j, out);
  2255. }
  2256. }
  2257. static int register_bg_url(const char *s, unsigned int len)
  2258. {
  2259. unsigned int i, j, n;
  2260. while (len && is_space(*s)) {
  2261. s++;
  2262. len--;
  2263. }
  2264. while (len && is_space(s[len - 1]))
  2265. len--;
  2266. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2267. s[len - 1] == s[0]) {
  2268. s++;
  2269. len -= 2;
  2270. }
  2271. if (len > 191)
  2272. len = 191;
  2273. for (i = 0; i < bg_url_count; i++) {
  2274. for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
  2275. ;
  2276. if (j == len && bg_urls[i][j] == '\0')
  2277. return (int)i + 1;
  2278. }
  2279. if (bg_url_count >= BR_BG_URLS || len == 0)
  2280. return 0;
  2281. n = len < 191 ? len : 191;
  2282. for (i = 0; i < n; i++)
  2283. bg_urls[bg_url_count][i] = s[i];
  2284. bg_urls[bg_url_count][n] = '\0';
  2285. return (int)(++bg_url_count);
  2286. }
  2287. /* Length values for margins/padding: px, ignore percentages/units. */
  2288. static unsigned int viewport_w;
  2289. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2290. {
  2291. unsigned int i = *pos;
  2292. int sign = 1;
  2293. long v = 0; /* hundredths of a unit before conversion */
  2294. int digits = 0, frac = 0, unit = 0;
  2295. while (i < len && is_space(s[i]))
  2296. i++;
  2297. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2298. if (s[i] == '-')
  2299. sign = -1;
  2300. i++;
  2301. }
  2302. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2303. v = v * 10 + (s[i] - '0');
  2304. digits = 1;
  2305. i++;
  2306. }
  2307. if (i < len && s[i] == '.') {
  2308. i++;
  2309. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2310. if (frac < 2) {
  2311. v = v * 10 + (s[i] - '0');
  2312. frac++;
  2313. }
  2314. i++;
  2315. }
  2316. }
  2317. while (frac < 2) {
  2318. v *= 10;
  2319. frac++;
  2320. }
  2321. while (frac > 2) {
  2322. v /= 10;
  2323. frac--;
  2324. }
  2325. if (i < len && s[i] == '%')
  2326. unit = 2;
  2327. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2328. s[i + 2] == 'm')
  2329. unit = 1;
  2330. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2331. unit = 3;
  2332. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2333. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2334. s[i] == 'a' || s[i] == 'd'))
  2335. i++;
  2336. *pos = i;
  2337. if (!digits)
  2338. return 0;
  2339. if (unit == 1)
  2340. v *= root_font_px;
  2341. else if (unit == 3)
  2342. v *= root_font_px;
  2343. else if (unit == 2)
  2344. v = v * (long)viewport_w / 100;
  2345. v /= 100;
  2346. v *= sign;
  2347. if (v < -2048)
  2348. v = -2048;
  2349. if (v > 2048)
  2350. v = 2048;
  2351. return (int)v;
  2352. }
  2353. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2354. * treats it as a zero-length token; margins need to keep it so block layout
  2355. * can centre a box with a definite width. */
  2356. #define CSS_LEN_AUTO (-32768)
  2357. /* A box length with its unit preserved for layout-time resolution:
  2358. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2359. * rem and whole pixels for px. */
  2360. static int parse_css_len(const char *s, unsigned int len,
  2361. unsigned int *pos, int *unit)
  2362. {
  2363. unsigned int i = *pos;
  2364. int sign = 1;
  2365. long v = 0;
  2366. int digits = 0, frac = 0;
  2367. *unit = 0;
  2368. while (i < len && is_space(s[i]))
  2369. i++;
  2370. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2371. if (s[i] == '-')
  2372. sign = -1;
  2373. i++;
  2374. }
  2375. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2376. v = v * 10 + (s[i] - '0');
  2377. digits = 1;
  2378. i++;
  2379. }
  2380. if (i < len && s[i] == '.') {
  2381. i++;
  2382. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2383. if (frac < 2) {
  2384. v = v * 10 + (s[i] - '0');
  2385. frac++;
  2386. }
  2387. i++;
  2388. }
  2389. }
  2390. while (frac < 2) {
  2391. v *= 10;
  2392. frac++;
  2393. }
  2394. if (!digits) {
  2395. *pos = i;
  2396. return 0;
  2397. }
  2398. if (i < len && s[i] == '%') {
  2399. *unit = 1;
  2400. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2401. s[i + 2] == 'm') {
  2402. *unit = 3;
  2403. i += 3;
  2404. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2405. *unit = 2;
  2406. i += 2;
  2407. } else {
  2408. v /= 100;
  2409. }
  2410. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2411. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2412. s[i] == 'a' || s[i] == 'd'))
  2413. i++;
  2414. *pos = i;
  2415. v *= sign;
  2416. if (v < -32768)
  2417. v = -32768;
  2418. if (v > 32767)
  2419. v = 32767;
  2420. return (int)v;
  2421. }
  2422. static int parse_margin_len(const char *s, unsigned int len,
  2423. unsigned int *pos, int *unit)
  2424. {
  2425. unsigned int i = *pos;
  2426. while (i < len && is_space(s[i]))
  2427. i++;
  2428. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2429. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2430. *pos = i + 4;
  2431. *unit = 4;
  2432. return CSS_LEN_AUTO;
  2433. }
  2434. return parse_css_len(s, len, pos, unit);
  2435. }
  2436. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2437. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2438. static int parse_margin_calc(const char *s, unsigned int len,
  2439. unsigned int *pos, int *pct, int *px)
  2440. {
  2441. unsigned int i = *pos, j;
  2442. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2443. return 0;
  2444. i += 5;
  2445. j = i;
  2446. while (j < len && s[j] != ')')
  2447. j++;
  2448. if (j >= len)
  2449. return 0;
  2450. if (!parse_calc(s + i, j - i, pct, px))
  2451. return 0;
  2452. *pos = j + 1;
  2453. return 1;
  2454. }
  2455. /* calc(P% +/- Npx) for the common width/margin forms. */
  2456. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2457. {
  2458. unsigned int i = 0;
  2459. int sign = 1;
  2460. int have = 0;
  2461. *pct = 0;
  2462. *px = 0;
  2463. while (i < len) {
  2464. unsigned int j;
  2465. long whole = 0, frac = 0, scale = 1;
  2466. int digits = 0;
  2467. while (i < len && is_space(s[i]))
  2468. i++;
  2469. if (i >= len)
  2470. break;
  2471. if (s[i] == '+' || s[i] == '-') {
  2472. sign = s[i] == '-' ? -1 : 1;
  2473. i++;
  2474. continue;
  2475. }
  2476. if (s[i] < '0' || s[i] > '9') {
  2477. if (s[i] == '.') {
  2478. /* fall through to the number parser */
  2479. } else {
  2480. i++;
  2481. continue;
  2482. }
  2483. }
  2484. j = i;
  2485. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2486. whole = whole * 10 + (s[j] - '0');
  2487. digits = 1;
  2488. j++;
  2489. }
  2490. if (j < len && s[j] == '.') {
  2491. j++;
  2492. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2493. frac = frac * 10 + (s[j] - '0');
  2494. scale *= 10;
  2495. j++;
  2496. }
  2497. }
  2498. if (!digits) {
  2499. i = j + 1;
  2500. continue;
  2501. }
  2502. while (j < len && is_space(s[j]))
  2503. j++;
  2504. if (j < len && s[j] == '%') {
  2505. *pct = sign * (int)(whole + (scale > 1 ?
  2506. frac * 100 / scale : 0));
  2507. have = 1;
  2508. j++;
  2509. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2510. *px = sign * (int)whole;
  2511. have = 1;
  2512. j += 2;
  2513. }
  2514. i = j;
  2515. while (i < len && !is_space(s[i]))
  2516. i++;
  2517. sign = 1;
  2518. }
  2519. return have;
  2520. }
  2521. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2522. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2523. unsigned char *kind)
  2524. {
  2525. unsigned int i = 0;
  2526. unsigned long v = 0;
  2527. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2528. *val = 0;
  2529. *kind = 0;
  2530. while (i < len && is_space(s[i]))
  2531. i++;
  2532. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2533. return 0;
  2534. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2535. return 0;
  2536. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2537. v = v * 10 + (unsigned int)(s[i] - '0');
  2538. digits = 1;
  2539. i++;
  2540. }
  2541. if (i < len && s[i] == '.') {
  2542. i++;
  2543. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2544. if (frac < 2) {
  2545. v = v * 10 + (unsigned int)(s[i] - '0');
  2546. frac++;
  2547. }
  2548. i++;
  2549. }
  2550. }
  2551. while (frac < 2) {
  2552. v *= 10;
  2553. frac++;
  2554. }
  2555. while (frac > 2) {
  2556. v /= 10;
  2557. frac--;
  2558. }
  2559. if (!digits)
  2560. return 0;
  2561. while (i < len && is_space(s[i]))
  2562. i++;
  2563. if (i < len && s[i] == '%')
  2564. pct = 1;
  2565. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2566. s[i + 2] == 'm')
  2567. rem = 1;
  2568. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2569. rem = 2;
  2570. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2571. vw = 1;
  2572. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2573. ; /* px: already the default scale */
  2574. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2575. bad_unit = 1;
  2576. if (bad_unit)
  2577. return 0;
  2578. if (rem == 1 || rem == 2) {
  2579. if (v > 65535)
  2580. v = 65535;
  2581. *val = (unsigned short)v;
  2582. *kind = rem == 1 ? 5 : 4;
  2583. return 1;
  2584. }
  2585. v /= 100;
  2586. if (vw)
  2587. v = v * viewport_w / 100;
  2588. if (pct) {
  2589. if (v > 100)
  2590. v = 100;
  2591. *kind = 2;
  2592. } else {
  2593. if (v > 2048)
  2594. v = 2048;
  2595. *kind = 1;
  2596. }
  2597. *val = (unsigned short)v;
  2598. return 1;
  2599. }
  2600. static int parse_font_px(const char *s, unsigned int len)
  2601. {
  2602. unsigned int i = 0;
  2603. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2604. int v1000, px;
  2605. int is_px = 1, is_rem = 0, is_pct = 0;
  2606. while (i < len && is_space(s[i]))
  2607. i++;
  2608. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2609. return 24;
  2610. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2611. return 32;
  2612. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2613. return 14;
  2614. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2615. whole = whole * 10 + (s[i] - '0');
  2616. digits = 1;
  2617. i++;
  2618. }
  2619. if (i < len && s[i] == '.') {
  2620. i++;
  2621. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2622. if (fscale < 1000) {
  2623. frac = frac * 10 + (s[i] - '0');
  2624. fscale *= 10;
  2625. }
  2626. i++;
  2627. }
  2628. }
  2629. if (!digits)
  2630. return 0;
  2631. while (i < len && is_space(s[i]))
  2632. i++;
  2633. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2634. is_rem = 1;
  2635. is_px = 0;
  2636. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2637. is_rem = 1;
  2638. is_px = 0;
  2639. } else if (i < len && s[i] == '%') {
  2640. is_pct = 1;
  2641. is_px = 0;
  2642. }
  2643. v1000 = whole * 1000 + frac * 1000 / fscale;
  2644. if (is_rem)
  2645. px = v1000 * root_font_px / 1000;
  2646. else if (is_pct)
  2647. px = v1000 * root_font_px / 100000;
  2648. else if (is_px)
  2649. px = v1000 / 1000;
  2650. else
  2651. px = v1000 / 1000;
  2652. if (px < 8)
  2653. px = 8;
  2654. if (px > 72)
  2655. px = 72;
  2656. return px;
  2657. }
  2658. /* font-size keeping the unit so em/% resolve against the parent face. */
  2659. static int parse_font_size(const char *s, unsigned int len,
  2660. unsigned short *raw, unsigned char *unit)
  2661. {
  2662. unsigned int i = 0;
  2663. int whole = 0, frac = 0, scale = 1, digits = 0;
  2664. int v100;
  2665. *raw = 0;
  2666. *unit = 0;
  2667. while (i < len && is_space(s[i]))
  2668. i++;
  2669. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2670. *raw = 24;
  2671. *unit = 1;
  2672. return 1;
  2673. }
  2674. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2675. *raw = 32;
  2676. *unit = 1;
  2677. return 1;
  2678. }
  2679. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2680. *raw = 14;
  2681. *unit = 1;
  2682. return 1;
  2683. }
  2684. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2685. whole = whole * 10 + (s[i] - '0');
  2686. digits = 1;
  2687. i++;
  2688. }
  2689. if (i < len && s[i] == '.') {
  2690. i++;
  2691. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2692. frac = frac * 10 + (s[i] - '0');
  2693. scale *= 10;
  2694. i++;
  2695. }
  2696. }
  2697. if (!digits && !frac)
  2698. return 0;
  2699. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2700. while (i < len && is_space(s[i]))
  2701. i++;
  2702. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2703. *raw = (unsigned short)v100;
  2704. *unit = 3;
  2705. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2706. *raw = (unsigned short)v100;
  2707. *unit = 2;
  2708. } else if (i < len && s[i] == '%') {
  2709. *raw = (unsigned short)(v100 / 100);
  2710. *unit = 4;
  2711. } else {
  2712. *raw = (unsigned short)(v100 / 100);
  2713. *unit = 1;
  2714. }
  2715. return 1;
  2716. }
  2717. static int parse_line_height(const char *s, unsigned int len,
  2718. unsigned short *px, unsigned char *pct)
  2719. {
  2720. unsigned int i = 0;
  2721. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2722. int v1000;
  2723. int is_px = 0, is_pct = 0;
  2724. *px = 0;
  2725. *pct = 0;
  2726. while (i < len && is_space(s[i]))
  2727. i++;
  2728. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2729. return 0;
  2730. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2731. whole = whole * 10 + (s[i] - '0');
  2732. digits = 1;
  2733. i++;
  2734. }
  2735. if (i < len && s[i] == '.') {
  2736. i++;
  2737. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2738. frac = frac * 10 + (s[i] - '0');
  2739. fscale *= 10;
  2740. i++;
  2741. }
  2742. }
  2743. if (!digits)
  2744. return 0;
  2745. while (i < len && is_space(s[i]))
  2746. i++;
  2747. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2748. is_px = 1;
  2749. else if (i < len && s[i] == '%')
  2750. is_pct = 1;
  2751. v1000 = whole * 1000 + frac * 1000 / fscale;
  2752. if (is_px) {
  2753. int v = v1000 / 1000;
  2754. if (v < 8)
  2755. v = 8;
  2756. if (v > 160)
  2757. v = 160;
  2758. *px = (unsigned short)v;
  2759. } else {
  2760. int v = v1000 / 10; /* unitless multiplier -> percent */
  2761. if (is_pct)
  2762. v = v1000 / 1000;
  2763. if (v < 50)
  2764. v = 50;
  2765. if (v > 400)
  2766. v = 400;
  2767. *pct = (unsigned char)v;
  2768. }
  2769. return 1;
  2770. }
  2771. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2772. {
  2773. unsigned int i, j, n = s_len(needle);
  2774. for (i = 0; i + n <= len; i++) {
  2775. for (j = 0; j < n; j++) {
  2776. char a = s[i + j];
  2777. if (a >= 'A' && a <= 'Z')
  2778. a = (char)(a - 'A' + 'a');
  2779. if (a != needle[j])
  2780. break;
  2781. }
  2782. if (j == n)
  2783. return 1;
  2784. }
  2785. return 0;
  2786. }
  2787. /* Pull a colour out of a border/background shorthand. */
  2788. /* Background shorthands can hold several layers before the final solid
  2789. * colour. The colour is conventionally the last token (e.g. a stack of
  2790. * gradients followed by var(--bg)), so take the rightmost colour rather
  2791. * than the first gradient stop. */
  2792. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2793. {
  2794. int last = -1;
  2795. unsigned int i;
  2796. for (i = 0; i < len; i++) {
  2797. if (s[i] == '#')
  2798. last = (int)i;
  2799. else if ((s[i] == 'r' || s[i] == 'R') &&
  2800. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2801. last = (int)i;
  2802. }
  2803. if (last >= 0)
  2804. return parse_color(s + last, len - (unsigned int)last, c);
  2805. while (i > 0) {
  2806. unsigned int st, e;
  2807. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2808. s[i - 1] == ')' || s[i - 1] == '('))
  2809. i--;
  2810. e = i;
  2811. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  2812. s[i - 1] != ')' && s[i - 1] != '(')
  2813. i--;
  2814. st = i;
  2815. if (e > st && parse_color(s + st, e - st, c))
  2816. return 1;
  2817. }
  2818. return 0;
  2819. }
  2820. static int parse_display(const char *s, unsigned int len)
  2821. {
  2822. unsigned int i;
  2823. while (len && is_space(s[0])) { s++; len--; }
  2824. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  2825. ;
  2826. if (i == 4 && ci_eq_n(s, "none", 4))
  2827. return BR_DISPLAY_NONE;
  2828. if (i == 5 && ci_eq_n(s, "block", 5))
  2829. return BR_DISPLAY_BLOCK;
  2830. if (i == 6 && ci_eq_n(s, "inline", 6))
  2831. return BR_DISPLAY_INLINE;
  2832. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  2833. return BR_DISPLAY_INLINE_BLOCK;
  2834. if (i == 10 && ci_eq_n(s, "list-item", 10))
  2835. return BR_DISPLAY_BLOCK;
  2836. if (i == 4 && ci_eq_n(s, "flex", 4))
  2837. return BR_DISPLAY_FLEX;
  2838. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  2839. return BR_DISPLAY_FLEX;
  2840. if (i == 5 && ci_eq_n(s, "table", 5))
  2841. return BR_DISPLAY_BLOCK;
  2842. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  2843. return BR_DISPLAY_BLOCK;
  2844. /* Grid shells are common on news/wiki pages, but the two-column
  2845. * heuristic is not reliable for their nested area templates yet.
  2846. * Collapse them to block flow so the content stays readable. */
  2847. if (i == 4 && ci_eq_n(s, "grid", 4))
  2848. return BR_DISPLAY_BLOCK;
  2849. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  2850. return BR_DISPLAY_BLOCK;
  2851. return BR_DISPLAY_INLINE;
  2852. }
  2853. static void decl_add(struct br_decl *out, unsigned int *nout,
  2854. unsigned char prop, int val, int important)
  2855. {
  2856. if (*nout >= BR_DECLS)
  2857. return;
  2858. out[*nout].prop = prop;
  2859. out[*nout].val = (short)val;
  2860. out[*nout].important = (unsigned char)(important != 0);
  2861. (*nout)++;
  2862. }
  2863. /* One whitespace-separated token. Returns 0 at the end of the value. */
  2864. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  2865. unsigned int *start, unsigned int *end)
  2866. {
  2867. unsigned int i = *pos;
  2868. while (i < len && is_space(s[i]))
  2869. i++;
  2870. if (i >= len)
  2871. return 0;
  2872. *start = i;
  2873. while (i < len && !is_space(s[i]))
  2874. i++;
  2875. *end = i;
  2876. *pos = i;
  2877. return 1;
  2878. }
  2879. static int bg_number(const char *s, unsigned int len, signed short *val,
  2880. unsigned char *kind)
  2881. {
  2882. unsigned int i = 0;
  2883. long v = 0;
  2884. int neg = 0, digits = 0, pct = 0;
  2885. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2886. neg = s[i] == '-';
  2887. i++;
  2888. }
  2889. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2890. v = v * 10 + (s[i] - '0');
  2891. digits = 1;
  2892. i++;
  2893. }
  2894. if (i < len && s[i] == '.') {
  2895. i++;
  2896. while (i < len && s[i] >= '0' && s[i] <= '9')
  2897. i++;
  2898. }
  2899. if (!digits)
  2900. return 0;
  2901. if (i < len && s[i] == '%') {
  2902. pct = 1;
  2903. i++;
  2904. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  2905. i += 2;
  2906. }
  2907. if (i != len)
  2908. return 0;
  2909. if (neg)
  2910. v = -v;
  2911. if (v < -4096)
  2912. v = -4096;
  2913. if (v > 4096)
  2914. v = 4096;
  2915. *val = (signed short)v;
  2916. *kind = (unsigned char)(pct ? 1 : 0);
  2917. return 1;
  2918. }
  2919. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  2920. int *vertical)
  2921. {
  2922. *vertical = 0;
  2923. if (len == 4 && ci_eq_n(s, "left", 4)) {
  2924. *val = 0;
  2925. return 1;
  2926. }
  2927. if (len == 5 && ci_eq_n(s, "right", 5)) {
  2928. *val = 100;
  2929. return 1;
  2930. }
  2931. if (len == 3 && ci_eq_n(s, "top", 3)) {
  2932. *val = 0;
  2933. *vertical = 1;
  2934. return 1;
  2935. }
  2936. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  2937. *val = 100;
  2938. *vertical = 1;
  2939. return 1;
  2940. }
  2941. if (len == 6 && ci_eq_n(s, "center", 6)) {
  2942. *val = 50;
  2943. return 1;
  2944. }
  2945. return 0;
  2946. }
  2947. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  2948. const char *s, unsigned int len, int important)
  2949. {
  2950. unsigned int pos = 0, ts, te;
  2951. signed short val[2] = { 50, 50 };
  2952. unsigned char kind[2] = { 1, 1 };
  2953. int vert[2] = { 0, 0 }, n = 0;
  2954. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  2955. signed short v;
  2956. int is_vert;
  2957. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  2958. val[n] = v;
  2959. kind[n] = 1;
  2960. vert[n] = is_vert;
  2961. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  2962. val[n] = v;
  2963. vert[n] = 0;
  2964. } else {
  2965. n++;
  2966. continue;
  2967. }
  2968. n++;
  2969. }
  2970. if (n == 1) {
  2971. if (vert[0]) {
  2972. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  2973. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  2974. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  2975. important);
  2976. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  2977. } else {
  2978. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  2979. important);
  2980. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  2981. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  2982. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  2983. }
  2984. return;
  2985. }
  2986. if (n >= 2) {
  2987. int x = vert[0] && !vert[1] ? 1 : 0;
  2988. int y = 1 - x;
  2989. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  2990. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  2991. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  2992. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  2993. }
  2994. }
  2995. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  2996. const char *s, unsigned int len, int important)
  2997. {
  2998. unsigned int pos = 0, ts, te;
  2999. signed short w = 0, h = 0;
  3000. unsigned char kw = 0, kh = 0;
  3001. int n = 0;
  3002. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3003. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3004. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3005. return;
  3006. }
  3007. if (n == 0 && te - ts == 7 &&
  3008. ci_eq_n(s + ts, "contain", 7)) {
  3009. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3010. return;
  3011. }
  3012. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3013. n++;
  3014. continue;
  3015. }
  3016. {
  3017. signed short v;
  3018. unsigned char k;
  3019. if (bg_number(s + ts, te - ts, &v, &k)) {
  3020. if (n == 0) {
  3021. w = v;
  3022. kw = k;
  3023. } else {
  3024. h = v;
  3025. kh = k;
  3026. }
  3027. }
  3028. }
  3029. n++;
  3030. }
  3031. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3032. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3033. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3034. (void)kh;
  3035. }
  3036. static int parse_fixed_100(const char *s, unsigned int len,
  3037. unsigned int *pos, signed short *out)
  3038. {
  3039. unsigned int i = *pos;
  3040. long whole = 0, frac = 0;
  3041. int neg = 0, digits = 0, fd = 0;
  3042. while (i < len && is_space(s[i]))
  3043. i++;
  3044. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3045. neg = s[i] == '-';
  3046. i++;
  3047. }
  3048. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3049. whole = whole * 10 + (s[i] - '0');
  3050. if (whole > 327)
  3051. whole = 327;
  3052. digits = 1;
  3053. i++;
  3054. }
  3055. if (i < len && s[i] == '.') {
  3056. i++;
  3057. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3058. if (fd < 2) {
  3059. frac = frac * 10 + (s[i] - '0');
  3060. fd++;
  3061. }
  3062. i++;
  3063. }
  3064. }
  3065. if (!digits)
  3066. return 0;
  3067. while (fd++ < 2)
  3068. frac *= 10;
  3069. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3070. *pos = i;
  3071. return 1;
  3072. }
  3073. static int find_fn(const char *v, unsigned int len, const char *name,
  3074. unsigned int *arg)
  3075. {
  3076. unsigned int n = s_len(name), i;
  3077. for (i = 0; i + n <= len; i++) {
  3078. unsigned int j;
  3079. if (!ci_eq_n(v + i, name, n))
  3080. continue;
  3081. j = i + n;
  3082. while (j < len && is_space(v[j]))
  3083. j++;
  3084. if (j < len && v[j] == '(') {
  3085. *arg = j + 1;
  3086. return 1;
  3087. }
  3088. }
  3089. return 0;
  3090. }
  3091. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3092. const char *s, unsigned int len,
  3093. int important)
  3094. {
  3095. unsigned int pos = 0, ts, te;
  3096. signed short val[2] = { 50, 50 };
  3097. unsigned char kind[2] = { 1, 1 };
  3098. int vert[2] = { 0, 0 }, n = 0;
  3099. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3100. signed short v;
  3101. int is_vert;
  3102. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3103. val[n] = v;
  3104. kind[n] = 1;
  3105. vert[n] = is_vert;
  3106. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3107. val[n] = v;
  3108. vert[n] = 0;
  3109. } else {
  3110. n++;
  3111. continue;
  3112. }
  3113. n++;
  3114. }
  3115. if (n == 1) {
  3116. if (vert[0]) {
  3117. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3118. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3119. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3120. important);
  3121. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3122. } else {
  3123. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3124. important);
  3125. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3126. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3127. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3128. }
  3129. return;
  3130. }
  3131. if (n >= 2) {
  3132. int x = vert[0] && !vert[1] ? 1 : 0;
  3133. int y = 1 - x;
  3134. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3135. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3136. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3137. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3138. }
  3139. }
  3140. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3141. const char *v, unsigned int vl, int important)
  3142. {
  3143. unsigned int i;
  3144. for (i = 0; i + 4 <= vl; i++) {
  3145. if (ci_eq_n(v + i, "url(", 4)) {
  3146. unsigned int st = i + 4, en = st;
  3147. int idx;
  3148. while (en < vl && v[en] != ')')
  3149. en++;
  3150. idx = register_bg_url(v + st, en - st);
  3151. if (idx)
  3152. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3153. important);
  3154. return;
  3155. }
  3156. }
  3157. }
  3158. static int parse_len_token(const char *s, unsigned int len, int *out)
  3159. {
  3160. unsigned int pos = 0;
  3161. int v;
  3162. if (!len)
  3163. return 0;
  3164. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3165. return 0;
  3166. v = parse_len(s, len, &pos);
  3167. if (pos == 0)
  3168. return 0;
  3169. *out = v;
  3170. return 1;
  3171. }
  3172. static void expand_four(int v[4], int n)
  3173. {
  3174. if (n == 1) {
  3175. v[1] = v[2] = v[3] = v[0];
  3176. } else if (n == 2) {
  3177. v[2] = v[0];
  3178. v[3] = v[1];
  3179. } else if (n == 3) {
  3180. v[3] = v[1];
  3181. }
  3182. }
  3183. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3184. * the input is copied verbatim when it contains no var(). */
  3185. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3186. unsigned int len, char *out,
  3187. unsigned int cap, int depth)
  3188. {
  3189. unsigned int i = 0, n = 0;
  3190. if (depth > 8)
  3191. return 0;
  3192. while (i < len && n + 1 < cap) {
  3193. unsigned int p, inner, close, comma, nlen, fstart;
  3194. char quote;
  3195. int parens;
  3196. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3197. out[n++] = s[i++];
  3198. continue;
  3199. }
  3200. p = i + 4;
  3201. quote = 0;
  3202. parens = 1;
  3203. while (p < len && parens) {
  3204. char c = s[p];
  3205. if (quote) {
  3206. if (c == quote)
  3207. quote = 0;
  3208. else if (c == '\\' && p + 1 < len)
  3209. p++;
  3210. } else if (c == '"' || c == '\'') {
  3211. quote = c;
  3212. } else if (c == '(') {
  3213. parens++;
  3214. } else if (c == ')') {
  3215. parens--;
  3216. }
  3217. p++;
  3218. }
  3219. if (parens) {
  3220. out[n++] = s[i++];
  3221. continue;
  3222. }
  3223. close = p - 1;
  3224. inner = i + 4;
  3225. while (inner < close && is_space(s[inner]))
  3226. inner++;
  3227. comma = inner;
  3228. parens = 0;
  3229. quote = 0;
  3230. while (comma < close) {
  3231. char c = s[comma];
  3232. if (quote) {
  3233. if (c == quote)
  3234. quote = 0;
  3235. } else if (c == '"' || c == '\'') {
  3236. quote = c;
  3237. } else if (c == '(') {
  3238. parens++;
  3239. } else if (c == ')' && parens > 0) {
  3240. parens--;
  3241. } else if (c == ',' && parens == 0) {
  3242. break;
  3243. }
  3244. comma++;
  3245. }
  3246. nlen = comma - inner;
  3247. while (nlen && is_space(s[inner + nlen - 1]))
  3248. nlen--;
  3249. fstart = comma < close ? comma + 1 : close;
  3250. while (fstart < close && is_space(s[fstart]))
  3251. fstart++;
  3252. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3253. unsigned int vi = css_var_find(d, s + inner, nlen);
  3254. if (vi < BR_MAX_VARS) {
  3255. const char *v = attr_ptr(d, d->var_val[vi]);
  3256. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3257. out + n,
  3258. cap - n, depth + 1);
  3259. n += vn;
  3260. } else if (fstart < close) {
  3261. unsigned int vn = css_expand_vars(d, s + fstart,
  3262. close - fstart,
  3263. out + n, cap - n,
  3264. depth + 1);
  3265. n += vn;
  3266. }
  3267. } else if (fstart < close) {
  3268. unsigned int vn = css_expand_vars(d, s + fstart,
  3269. close - fstart,
  3270. out + n, cap - n,
  3271. depth + 1);
  3272. n += vn;
  3273. }
  3274. i = p;
  3275. }
  3276. out[n] = '\0';
  3277. return n;
  3278. }
  3279. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3280. unsigned int i)
  3281. {
  3282. while (i < len && (is_space(s[i]) || s[i] == ','))
  3283. i++;
  3284. return i;
  3285. }
  3286. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3287. * argument). Returns 1 on success. */
  3288. static int parse_grid_track(const char *s, unsigned int len,
  3289. unsigned int *pp, unsigned short *val,
  3290. unsigned char *kind)
  3291. {
  3292. unsigned int i = grid_skip_sep(s, len, *pp);
  3293. unsigned int whole = 0, frac = 0, scale = 1;
  3294. int any = 0;
  3295. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3296. unsigned int j = i + 7;
  3297. while (j < len && s[j] != ',')
  3298. j++;
  3299. if (j >= len)
  3300. return 0;
  3301. i = j + 1;
  3302. while (i < len && is_space(s[i]))
  3303. i++;
  3304. }
  3305. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3306. int depth = 1;
  3307. i += 7;
  3308. while (i < len && depth) {
  3309. if (s[i] == '(')
  3310. depth++;
  3311. else if (s[i] == ')')
  3312. depth--;
  3313. i++;
  3314. }
  3315. *pp = i;
  3316. return 0;
  3317. }
  3318. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3319. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3320. i++;
  3321. any = 1;
  3322. }
  3323. if (i < len && s[i] == '.') {
  3324. i++;
  3325. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3326. if (scale < 1000) {
  3327. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3328. scale *= 10;
  3329. }
  3330. i++;
  3331. any = 1;
  3332. }
  3333. }
  3334. if (!any) {
  3335. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3336. *val = 1;
  3337. *kind = 3;
  3338. *pp = i + 4;
  3339. return 1;
  3340. }
  3341. return 0;
  3342. }
  3343. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3344. *val = (unsigned short)(whole ? whole : 1);
  3345. *kind = 3;
  3346. i += 2;
  3347. } else if (i < len && s[i] == '%') {
  3348. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3349. *kind = 2;
  3350. i++;
  3351. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3352. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3353. *kind = 1;
  3354. i += 3;
  3355. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3356. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3357. *kind = 1;
  3358. i += 2;
  3359. } else {
  3360. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3361. *kind = 1;
  3362. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3363. i += 2;
  3364. }
  3365. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3366. i++;
  3367. *pp = i;
  3368. return 1;
  3369. }
  3370. static int parse_grid_cols(const char *s, unsigned int len,
  3371. unsigned short val[2], unsigned char kind[2])
  3372. {
  3373. unsigned int pos = 0;
  3374. int n = 0;
  3375. while (n < 2) {
  3376. unsigned int before = pos;
  3377. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3378. break;
  3379. n++;
  3380. if (pos <= before)
  3381. break;
  3382. while (pos < len && is_space(s[pos]))
  3383. pos++;
  3384. if (pos >= len)
  3385. break;
  3386. }
  3387. return n;
  3388. }
  3389. static int parse_grid_slot(const char *s, unsigned int len)
  3390. {
  3391. if (contains_ci_len(s, len, "footer") ||
  3392. contains_ci_len(s, len, "header") ||
  3393. contains_ci_len(s, len, "sitenotice") ||
  3394. contains_ci_len(s, len, "navbar"))
  3395. return 1;
  3396. if (contains_ci_len(s, len, "columnstart") ||
  3397. contains_ci_len(s, len, "sidebar") ||
  3398. contains_ci_len(s, len, "left") ||
  3399. contains_ci_len(s, len, "start"))
  3400. return 2;
  3401. if (contains_ci_len(s, len, "pagecontent") ||
  3402. contains_ci_len(s, len, "content") ||
  3403. contains_ci_len(s, len, "main") ||
  3404. contains_ci_len(s, len, "right") ||
  3405. contains_ci_len(s, len, "end"))
  3406. return 3;
  3407. return 0;
  3408. }
  3409. static int css_has_var(const char *s, unsigned int len)
  3410. {
  3411. unsigned int i;
  3412. for (i = 0; i + 4 <= len; i++)
  3413. if ((s[i] == 'v' || s[i] == 'V') &&
  3414. ci_eq_n(s + i, "var(", 4))
  3415. return 1;
  3416. return 0;
  3417. }
  3418. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3419. unsigned int i)
  3420. {
  3421. i += 2;
  3422. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3423. i++;
  3424. if (i + 1 < len)
  3425. i += 2;
  3426. else
  3427. i = len;
  3428. return i;
  3429. }
  3430. static unsigned int css_skip_string(const char *s, unsigned int len,
  3431. unsigned int i)
  3432. {
  3433. char q = s[i++];
  3434. while (i < len && s[i] != q) {
  3435. if (s[i] == '\\' && i + 1 < len)
  3436. i++;
  3437. i++;
  3438. }
  3439. if (i < len)
  3440. i++;
  3441. return i;
  3442. }
  3443. /* Find the '}' that closes a declaration block. i points just past the
  3444. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3445. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3446. * selectors such as "Highlight-Stories {" without a closing brace). */
  3447. static unsigned int css_block_end(const char *s, unsigned int len,
  3448. unsigned int i)
  3449. {
  3450. int depth = 0;
  3451. while (i < len) {
  3452. char c = s[i];
  3453. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3454. i = css_skip_comment(s, len, i);
  3455. continue;
  3456. }
  3457. if (c == '"' || c == '\'') {
  3458. i = css_skip_string(s, len, i);
  3459. continue;
  3460. }
  3461. if (c == '{') {
  3462. depth++;
  3463. } else if (c == '}') {
  3464. if (depth == 0)
  3465. return i;
  3466. depth--;
  3467. }
  3468. i++;
  3469. }
  3470. return len;
  3471. }
  3472. /* Skip a block whose opening '{' has already been consumed. */
  3473. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3474. unsigned int i)
  3475. {
  3476. int depth = 1;
  3477. while (i < len && depth) {
  3478. char c = s[i];
  3479. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3480. i = css_skip_comment(s, len, i);
  3481. continue;
  3482. }
  3483. if (c == '"' || c == '\'') {
  3484. i = css_skip_string(s, len, i);
  3485. continue;
  3486. }
  3487. if (c == '{')
  3488. depth++;
  3489. else if (c == '}')
  3490. depth--;
  3491. i++;
  3492. }
  3493. return i;
  3494. }
  3495. /* ---- @font-face ------------------------------------------------------- */
  3496. static void font_trim(const char **sp, unsigned int *len)
  3497. {
  3498. const char *s = *sp;
  3499. unsigned int n = *len;
  3500. while (n && is_space(*s)) {
  3501. s++;
  3502. n--;
  3503. }
  3504. while (n && is_space(s[n - 1]))
  3505. n--;
  3506. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3507. s++;
  3508. n -= 2;
  3509. }
  3510. *sp = s;
  3511. *len = n;
  3512. }
  3513. static void font_add(const char *family, unsigned int flen,
  3514. const char *url, unsigned int ulen)
  3515. {
  3516. unsigned int i, n = 0, k;
  3517. font_trim(&family, &flen);
  3518. font_trim(&url, &ulen);
  3519. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3520. return;
  3521. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3522. struct br_font_def *f = &font_defs[i];
  3523. if (s_len(f->family) != flen)
  3524. continue;
  3525. for (k = 0; k < flen; k++) {
  3526. char a = f->family[k], b = family[k];
  3527. if (a >= 'A' && a <= 'Z')
  3528. a = (char)(a - 'A' + 'a');
  3529. if (b >= 'A' && b <= 'Z')
  3530. b = (char)(b - 'A' + 'a');
  3531. if (a != b)
  3532. break;
  3533. }
  3534. if (k != flen)
  3535. continue;
  3536. n = 0;
  3537. while (url[n] && n + 1 < sizeof(f->url)) {
  3538. f->url[n] = url[n];
  3539. n++;
  3540. }
  3541. f->url[n] = '\0';
  3542. n = 0;
  3543. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3544. f->base[n] = font_base[n];
  3545. n++;
  3546. }
  3547. f->base[n] = '\0';
  3548. return;
  3549. }
  3550. if (font_face_count >= BR_MAX_FACES)
  3551. return;
  3552. {
  3553. struct br_font_def *f = &font_defs[font_face_count];
  3554. for (n = 0; n < flen; n++)
  3555. f->family[n] = family[n];
  3556. f->family[n] = '\0';
  3557. for (n = 0; n < ulen; n++)
  3558. f->url[n] = url[n];
  3559. f->url[n] = '\0';
  3560. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  3561. f->base[n] = font_base[n];
  3562. f->base[n] = '\0';
  3563. f->used = 1;
  3564. font_face_count++;
  3565. }
  3566. }
  3567. /* Look up the first name in a font-family list that has an @font-face. */
  3568. static int font_family_id(const char *s, unsigned int len)
  3569. {
  3570. unsigned int i = 0;
  3571. while (i < len) {
  3572. unsigned int start, end, q = 0;
  3573. while (i < len && (is_space(s[i]) || s[i] == ','))
  3574. i++;
  3575. if (i >= len)
  3576. break;
  3577. if (s[i] == '"' || s[i] == '\'') {
  3578. char quote = s[i++];
  3579. start = i;
  3580. while (i < len && s[i] != quote)
  3581. i++;
  3582. end = i;
  3583. if (i < len)
  3584. i++;
  3585. } else {
  3586. start = i;
  3587. while (i < len && s[i] != ',')
  3588. i++;
  3589. end = i;
  3590. while (end > start && is_space(s[end - 1]))
  3591. end--;
  3592. }
  3593. if (end > start) {
  3594. for (q = 0; q < (unsigned int)font_face_count; q++) {
  3595. const char *f = font_defs[q].family;
  3596. unsigned int fl = s_len(f), k;
  3597. if (fl != end - start)
  3598. continue;
  3599. for (k = 0; k < fl; k++) {
  3600. char a = f[k], b = s[start + k];
  3601. if (a >= 'A' && a <= 'Z')
  3602. a = (char)(a - 'A' + 'a');
  3603. if (b >= 'A' && b <= 'Z')
  3604. b = (char)(b - 'A' + 'a');
  3605. if (a != b)
  3606. break;
  3607. }
  3608. if (k == fl)
  3609. return (int)q + 1;
  3610. }
  3611. }
  3612. }
  3613. return 0;
  3614. }
  3615. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  3616. * at-rules, so faces must be registered before any font-family declaration
  3617. * in the same sheet is resolved. */
  3618. static void scan_font_faces(const char *s, unsigned int len)
  3619. {
  3620. unsigned int i = 0;
  3621. while (i + 10 <= len) {
  3622. unsigned int j, end;
  3623. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  3624. unsigned int flen = 0, ulen = 0, p;
  3625. int have_family = 0, have_url = 0;
  3626. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  3627. i++;
  3628. continue;
  3629. }
  3630. j = i + 10;
  3631. while (j < len && s[j] != '{' && s[j] != ';')
  3632. j++;
  3633. if (j >= len || s[j] == ';') {
  3634. i = j + 1;
  3635. continue;
  3636. }
  3637. end = css_skip_nested(s, len, j + 1);
  3638. p = j + 1;
  3639. while (p < end) {
  3640. unsigned int ds, de, vs, ve;
  3641. while (p < end && (is_space(s[p]) || s[p] == ';'))
  3642. p++;
  3643. if (p >= end)
  3644. break;
  3645. ds = p;
  3646. while (p < end && s[p] != ':' && s[p] != ';')
  3647. p++;
  3648. de = p;
  3649. if (p >= end || s[p] != ':') {
  3650. while (p < end && s[p] != ';')
  3651. p++;
  3652. continue;
  3653. }
  3654. p++;
  3655. vs = p;
  3656. while (p < end && s[p] != ';')
  3657. p++;
  3658. ve = p;
  3659. while (ds < de && is_space(s[ds]))
  3660. ds++;
  3661. while (de > ds && is_space(s[de - 1]))
  3662. de--;
  3663. while (vs < ve && is_space(s[vs]))
  3664. vs++;
  3665. while (ve > vs && is_space(s[ve - 1]))
  3666. ve--;
  3667. if (de - ds == 11 &&
  3668. ci_eq_n(s + ds, "font-family", 11)) {
  3669. const char *fp = s + vs;
  3670. unsigned int fn = ve - vs;
  3671. font_trim(&fp, &fn);
  3672. if (fn && fn < sizeof(family)) {
  3673. unsigned int k;
  3674. for (k = 0; k < fn; k++)
  3675. family[k] = fp[k];
  3676. family[fn] = '\0';
  3677. flen = fn;
  3678. have_family = 1;
  3679. }
  3680. } else if (de - ds == 3 &&
  3681. ci_eq_n(s + ds, "src", 3)) {
  3682. unsigned int q = vs;
  3683. while (q + 4 <= ve) {
  3684. unsigned int us, ue;
  3685. if (!ci_eq_n(s + q, "url(", 4)) {
  3686. q++;
  3687. continue;
  3688. }
  3689. q += 4;
  3690. us = q;
  3691. while (q < ve && s[q] != ')')
  3692. q++;
  3693. ue = q;
  3694. {
  3695. const char *up = s + us;
  3696. unsigned int un = ue - us;
  3697. font_trim(&up, &un);
  3698. if (un && un < sizeof(url) &&
  3699. !contains_ci_len(up, un,
  3700. "woff")) {
  3701. unsigned int k;
  3702. for (k = 0; k < un; k++)
  3703. url[k] = up[k];
  3704. url[un] = '\0';
  3705. ulen = un;
  3706. have_url = 1;
  3707. break;
  3708. }
  3709. }
  3710. }
  3711. }
  3712. }
  3713. if (have_family && have_url)
  3714. font_add(family, flen, url, ulen);
  3715. i = end;
  3716. }
  3717. }
  3718. /* One border-radius length. Unlike parse_size(), percentages keep their
  3719. * kind: the drawer scales them to the box, not the viewport. */
  3720. static int parse_radius_value(const char *s, unsigned int len,
  3721. unsigned int *pos, unsigned short *val,
  3722. unsigned char *kind)
  3723. {
  3724. unsigned int i = *pos;
  3725. unsigned long v = 0;
  3726. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  3727. while (i < len && is_space(s[i]))
  3728. i++;
  3729. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3730. v = v * 10 + (unsigned int)(s[i] - '0');
  3731. digits = 1;
  3732. i++;
  3733. }
  3734. if (i < len && s[i] == '.') {
  3735. i++;
  3736. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3737. fdigits = 1;
  3738. if (frac < 2) {
  3739. v = v * 10 + (unsigned int)(s[i] - '0');
  3740. frac++;
  3741. }
  3742. i++;
  3743. }
  3744. }
  3745. while (frac < 2) {
  3746. v *= 10;
  3747. frac++;
  3748. }
  3749. while (i < len && is_space(s[i]))
  3750. i++;
  3751. if (i < len && s[i] == '%') {
  3752. pct = 1;
  3753. i++;
  3754. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  3755. s[i + 2] == 'm') {
  3756. rem = 1;
  3757. i += 3;
  3758. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  3759. rem = 2;
  3760. i += 2;
  3761. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  3762. i += 2;
  3763. } else if (i < len && is_ident(s[i])) {
  3764. return 0;
  3765. }
  3766. if (!digits && !fdigits)
  3767. return 0;
  3768. if (rem == 1)
  3769. v *= root_font_px;
  3770. else if (rem == 2)
  3771. v *= root_font_px;
  3772. v /= 100;
  3773. if (v > 2048)
  3774. v = 2048;
  3775. *val = (unsigned short)v;
  3776. *kind = pct ? 2 : 1;
  3777. *pos = i;
  3778. return 1;
  3779. }
  3780. /* font: [style] [variant] [weight] size[/line-height] family */
  3781. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  3782. const char *v, unsigned int len, int important)
  3783. {
  3784. unsigned int pos = 0, ts, te;
  3785. unsigned short size_raw = 0;
  3786. unsigned char size_unit = 0;
  3787. int have_size = 0;
  3788. int bold = 0, italic = 0;
  3789. int lh_px = 0, lh_pct = 0, mono = 0;
  3790. while (next_token(v, len, &pos, &ts, &te)) {
  3791. unsigned int tl = te - ts;
  3792. unsigned int slash = 0;
  3793. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  3794. bold = 1;
  3795. continue;
  3796. }
  3797. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  3798. continue;
  3799. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  3800. italic = 1;
  3801. continue;
  3802. }
  3803. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  3804. italic = 1;
  3805. continue;
  3806. }
  3807. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  3808. continue;
  3809. for (slash = 0; slash < tl; slash++)
  3810. if (v[ts + slash] == '/')
  3811. break;
  3812. if (slash == tl) {
  3813. unsigned int q;
  3814. int weight = 0;
  3815. for (q = 0; q < tl; q++) {
  3816. if (v[ts + q] < '0' || v[ts + q] > '9')
  3817. break;
  3818. weight = weight * 10 + (v[ts + q] - '0');
  3819. }
  3820. if (q == tl && weight >= 100 && weight <= 900) {
  3821. bold = weight >= 500;
  3822. continue;
  3823. }
  3824. }
  3825. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  3826. &size_raw, &size_unit))
  3827. continue;
  3828. have_size = 1;
  3829. if (slash < tl) {
  3830. unsigned int lstart = ts + slash + 1;
  3831. unsigned short lhp = 0;
  3832. unsigned char lhpct = 0;
  3833. if (!parse_line_height(v + lstart, te - lstart,
  3834. &lhp, &lhpct)) {
  3835. lh_px = 0;
  3836. lh_pct = 0;
  3837. } else {
  3838. lh_px = lhp;
  3839. lh_pct = lhpct;
  3840. }
  3841. }
  3842. }
  3843. if (!have_size)
  3844. return;
  3845. mono = contains_ci_len(v, len, "mono");
  3846. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  3847. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  3848. decl_add(out, nout, PROP_BOLD, bold, important);
  3849. decl_add(out, nout, PROP_ITALIC, italic, important);
  3850. decl_add(out, nout, PROP_MONO, mono, important);
  3851. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  3852. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  3853. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  3854. }
  3855. static void parse_decls(struct br_doc *d, const char *s,
  3856. unsigned int len, struct br_decl *out,
  3857. unsigned int *nout)
  3858. {
  3859. static char expanded[4096];
  3860. if (css_has_var(s, len)) {
  3861. unsigned int elen = css_expand_vars(d, s, len, expanded,
  3862. sizeof(expanded), 0);
  3863. if (elen) {
  3864. s = expanded;
  3865. len = elen;
  3866. }
  3867. }
  3868. unsigned int i = 0;
  3869. *nout = 0;
  3870. while (i < len && *nout < BR_DECLS) {
  3871. unsigned int pstart, pend, vstart, vend, k;
  3872. char prop[24];
  3873. int important = 0;
  3874. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3875. i++;
  3876. if (i >= len)
  3877. break;
  3878. /* A nested at-rule (@media/@supports) inside the declaration
  3879. * block carries its own selector context; skip it rather than
  3880. * reading its prelude as a declaration. */
  3881. if (s[i] == '@') {
  3882. while (i < len && s[i] != '{' && s[i] != ';')
  3883. i++;
  3884. if (i < len && s[i] == '{')
  3885. i = css_skip_nested(s, len, i + 1);
  3886. else if (i < len)
  3887. i++;
  3888. continue;
  3889. }
  3890. pstart = i;
  3891. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3892. i++;
  3893. if (i < len && s[i] == '{') {
  3894. /* Nested style rule: skip its block. */
  3895. i = css_skip_nested(s, len, i + 1);
  3896. continue;
  3897. }
  3898. pend = i;
  3899. if (i >= len || s[i] != ':')
  3900. continue;
  3901. i++;
  3902. vstart = i;
  3903. while (i < len && s[i] != ';')
  3904. i++;
  3905. vend = i;
  3906. while (pstart < pend && is_space(s[pstart]))
  3907. pstart++;
  3908. while (pend > pstart && is_space(s[pend - 1]))
  3909. pend--;
  3910. while (vstart < vend && is_space(s[vstart]))
  3911. vstart++;
  3912. while (vend > vstart && is_space(s[vend - 1]))
  3913. vend--;
  3914. if (vend - vstart >= 10 &&
  3915. ci_eq_n(s + vend - 10, "!important", 10)) {
  3916. important = 1;
  3917. vend -= 10;
  3918. while (vend > vstart && is_space(s[vend - 1]))
  3919. vend--;
  3920. }
  3921. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  3922. char c = s[pstart + k];
  3923. if (c >= 'A' && c <= 'Z')
  3924. c = (char)(c - 'A' + 'a');
  3925. prop[k] = c;
  3926. }
  3927. prop[k] = '\0';
  3928. if (str_eq(prop, "color")) {
  3929. unsigned short c;
  3930. if (parse_color(s + vstart, vend - vstart, &c))
  3931. decl_add(out, nout, PROP_COLOR, c, important);
  3932. } else if (str_eq(prop, "fill")) {
  3933. unsigned short c;
  3934. if (parse_color(s + vstart, vend - vstart, &c)) {
  3935. decl_add(out, nout, PROP_BG, c, important);
  3936. decl_add(out, nout, PROP_COLOR, c, important);
  3937. }
  3938. } else if (str_eq(prop, "stroke")) {
  3939. unsigned short c;
  3940. if (parse_color(s + vstart, vend - vstart, &c)) {
  3941. decl_add(out, nout, PROP_BORDER_COLOR, c,
  3942. important);
  3943. decl_add(out, nout, PROP_BORDER_W, 1, important);
  3944. }
  3945. } else if (str_eq(prop, "stroke-width")) {
  3946. unsigned int j = vstart;
  3947. int sw = 0;
  3948. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  3949. sw = sw * 10 + (s[j] - '0');
  3950. j++;
  3951. }
  3952. if (sw < 1)
  3953. sw = 1;
  3954. if (sw > 3)
  3955. sw = 3;
  3956. decl_add(out, nout, PROP_BORDER_W, sw, important);
  3957. } else if (str_eq(prop, "text-anchor")) {
  3958. int a = BR_ALIGN_LEFT;
  3959. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  3960. a = BR_ALIGN_CENTER;
  3961. else if (contains_ci_len(s + vstart, vend - vstart,
  3962. "end"))
  3963. a = BR_ALIGN_RIGHT;
  3964. decl_add(out, nout, PROP_ALIGN, a, important);
  3965. } else if (str_eq(prop, "background-image")) {
  3966. add_bg_image_decl(out, nout, s + vstart,
  3967. vend - vstart, important);
  3968. } else if (str_eq(prop, "background-repeat")) {
  3969. int r = 0;
  3970. if (contains_ci_len(s + vstart, vend - vstart,
  3971. "no-repeat"))
  3972. r = 1;
  3973. else if (contains_ci_len(s + vstart, vend - vstart,
  3974. "repeat-x"))
  3975. r = 2;
  3976. else if (contains_ci_len(s + vstart, vend - vstart,
  3977. "repeat-y"))
  3978. r = 3;
  3979. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  3980. } else if (str_eq(prop, "background-position")) {
  3981. add_bg_position(out, nout, s + vstart, vend - vstart,
  3982. important);
  3983. } else if (str_eq(prop, "background-size")) {
  3984. add_bg_size(out, nout, s + vstart, vend - vstart,
  3985. important);
  3986. } else if (str_eq(prop, "background-color") ||
  3987. str_eq(prop, "background")) {
  3988. unsigned short c;
  3989. int is_bg = str_eq(prop, "background");
  3990. if (is_bg) {
  3991. add_bg_image_decl(out, nout, s + vstart,
  3992. vend - vstart, important);
  3993. if (contains_ci_len(s + vstart, vend - vstart,
  3994. "no-repeat"))
  3995. decl_add(out, nout, PROP_BG_REPEAT, 1,
  3996. important);
  3997. else if (contains_ci_len(s + vstart,
  3998. vend - vstart,
  3999. "repeat-x"))
  4000. decl_add(out, nout, PROP_BG_REPEAT, 2,
  4001. important);
  4002. else if (contains_ci_len(s + vstart,
  4003. vend - vstart,
  4004. "repeat-y"))
  4005. decl_add(out, nout, PROP_BG_REPEAT, 3,
  4006. important);
  4007. }
  4008. if (parse_color(s + vstart, vend - vstart, &c))
  4009. decl_add(out, nout, PROP_BG, c, important);
  4010. else if (is_bg &&
  4011. find_color_last(s + vstart, vend - vstart, &c))
  4012. decl_add(out, nout, PROP_BG, c, important);
  4013. } else if (str_eq(prop, "font-size")) {
  4014. unsigned short raw;
  4015. unsigned char unit;
  4016. if (parse_font_size(s + vstart, vend - vstart,
  4017. &raw, &unit)) {
  4018. decl_add(out, nout, PROP_FONT_PX, raw,
  4019. important);
  4020. decl_add(out, nout, PROP_FONT_UNIT, unit,
  4021. important);
  4022. }
  4023. } else if (str_eq(prop, "font-weight")) {
  4024. int bold = 0;
  4025. unsigned int j;
  4026. if (vend - vstart >= 4 &&
  4027. ci_eq_n(s + vstart, "bold", 4))
  4028. bold = 1;
  4029. else {
  4030. int w = 0;
  4031. for (j = vstart; j < vend; j++)
  4032. if (s[j] >= '0' && s[j] <= '9')
  4033. w = w * 10 + (s[j] - '0');
  4034. if (w >= 500)
  4035. bold = 1;
  4036. }
  4037. decl_add(out, nout, PROP_BOLD, bold, important);
  4038. } else if (str_eq(prop, "font-style")) {
  4039. int italic = (vend - vstart >= 6 &&
  4040. ci_eq_n(s + vstart, "italic", 6));
  4041. decl_add(out, nout, PROP_ITALIC, italic, important);
  4042. } else if (str_eq(prop, "font-family")) {
  4043. unsigned int j;
  4044. int mono = 0;
  4045. int face = font_family_id(s + vstart, vend - vstart);
  4046. for (j = vstart; j + 4 <= vend; j++)
  4047. if (ci_eq_n(s + j, "mono", 4))
  4048. mono = 1;
  4049. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4050. decl_add(out, nout, PROP_MONO, mono, important);
  4051. } else if (str_eq(prop, "font")) {
  4052. add_font_shorthand(out, nout, s + vstart,
  4053. vend - vstart, important);
  4054. } else if (str_eq(prop, "text-align")) {
  4055. int a = BR_ALIGN_LEFT;
  4056. if (vend - vstart >= 6 &&
  4057. ci_eq_n(s + vstart, "center", 6))
  4058. a = BR_ALIGN_CENTER;
  4059. else if (vend - vstart >= 5 &&
  4060. ci_eq_n(s + vstart, "right", 5))
  4061. a = BR_ALIGN_RIGHT;
  4062. decl_add(out, nout, PROP_ALIGN, a, important);
  4063. } else if (str_eq(prop, "text-decoration")) {
  4064. int u = 0;
  4065. if (vend - vstart >= 9 &&
  4066. ci_eq_n(s + vstart, "underline", 9))
  4067. u = 1;
  4068. decl_add(out, nout, PROP_UNDERLINE, u, important);
  4069. } else if (str_eq(prop, "text-transform")) {
  4070. int u = (vend - vstart >= 9 &&
  4071. ci_eq_n(s + vstart, "uppercase", 9));
  4072. decl_add(out, nout, PROP_UPPER, u, important);
  4073. } else if (str_eq(prop, "white-space")) {
  4074. int nw = contains_ci_len(s + vstart, vend - vstart,
  4075. "nowrap") ||
  4076. contains_ci_len(s + vstart, vend - vstart,
  4077. "pre");
  4078. decl_add(out, nout, PROP_NOWRAP, nw, important);
  4079. } else if (str_eq(prop, "display")) {
  4080. decl_add(out, nout, PROP_DISPLAY,
  4081. parse_display(s + vstart, vend - vstart),
  4082. important);
  4083. } else if (str_eq(prop, "visibility")) {
  4084. int h = contains_ci_len(s + vstart, vend - vstart,
  4085. "hidden") ||
  4086. contains_ci_len(s + vstart, vend - vstart,
  4087. "collapse");
  4088. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  4089. } else if (str_eq(prop, "opacity")) {
  4090. unsigned int q;
  4091. int h = 1, any = 0;
  4092. for (q = vstart; q < vend; q++) {
  4093. if (s[q] >= '1' && s[q] <= '9') {
  4094. h = 0;
  4095. break;
  4096. }
  4097. if (s[q] >= '0' && s[q] <= '9')
  4098. any = 1;
  4099. }
  4100. if (!any || contains_ci_len(s + vstart, vend - vstart,
  4101. "inherit"))
  4102. h = 0;
  4103. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  4104. } else if (str_eq(prop, "overflow") ||
  4105. str_eq(prop, "overflow-x") ||
  4106. str_eq(prop, "overflow-y")) {
  4107. int h = contains_ci_len(s + vstart, vend - vstart,
  4108. "hidden") ||
  4109. contains_ci_len(s + vstart, vend - vstart,
  4110. "clip");
  4111. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  4112. } else if (str_eq(prop, "float")) {
  4113. int f = 0;
  4114. if (vend - vstart >= 4 &&
  4115. ci_eq_n(s + vstart, "left", 4))
  4116. f = 1;
  4117. else if (vend - vstart >= 5 &&
  4118. ci_eq_n(s + vstart, "right", 5))
  4119. f = 2;
  4120. decl_add(out, nout, PROP_FLOAT, f, important);
  4121. } else if (str_eq(prop, "clear")) {
  4122. int c = 0;
  4123. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  4124. c = 3;
  4125. else if (contains_ci_len(s + vstart, vend - vstart,
  4126. "left"))
  4127. c = 1;
  4128. else if (contains_ci_len(s + vstart, vend - vstart,
  4129. "right"))
  4130. c = 2;
  4131. decl_add(out, nout, PROP_CLEAR, c, important);
  4132. } else if (str_eq(prop, "flex-grow") ||
  4133. str_eq(prop, "flex")) {
  4134. unsigned int q = vstart;
  4135. int g = 0;
  4136. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  4137. q++;
  4138. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4139. g = g * 10 + (s[q] - '0');
  4140. q++;
  4141. }
  4142. if (g > 100)
  4143. g = 100;
  4144. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  4145. } else if (str_eq(prop, "flex-direction")) {
  4146. decl_add(out, nout, PROP_FLEX_COL,
  4147. contains_ci_len(s + vstart, vend - vstart,
  4148. "column"), important);
  4149. } else if (str_eq(prop, "flex-wrap")) {
  4150. int wrap = 0;
  4151. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  4152. wrap = 2;
  4153. else if (contains_ci_len(s + vstart, vend - vstart,
  4154. "wrap"))
  4155. wrap = 1;
  4156. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  4157. } else if (str_eq(prop, "justify-content")) {
  4158. int j = 0;
  4159. if (contains_ci_len(s + vstart, vend - vstart,
  4160. "space-between"))
  4161. j = 3;
  4162. else if (contains_ci_len(s + vstart, vend - vstart,
  4163. "space-around"))
  4164. j = 4;
  4165. else if (contains_ci_len(s + vstart, vend - vstart,
  4166. "center"))
  4167. j = 1;
  4168. else if (contains_ci_len(s + vstart, vend - vstart,
  4169. "flex-end") ||
  4170. contains_ci_len(s + vstart, vend - vstart,
  4171. "end"))
  4172. j = 2;
  4173. decl_add(out, nout, PROP_JUSTIFY, j, important);
  4174. } else if (str_eq(prop, "align-items")) {
  4175. int a = 0;
  4176. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  4177. a = 1;
  4178. else if (contains_ci_len(s + vstart, vend - vstart,
  4179. "flex-end") ||
  4180. contains_ci_len(s + vstart, vend - vstart,
  4181. "end"))
  4182. a = 2;
  4183. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  4184. } else if (str_eq(prop, "position")) {
  4185. int p = 0;
  4186. if (contains_ci_len(s + vstart, vend - vstart,
  4187. "absolute"))
  4188. p = 1;
  4189. else if (contains_ci_len(s + vstart, vend - vstart,
  4190. "fixed"))
  4191. p = 2;
  4192. else if (contains_ci_len(s + vstart, vend - vstart,
  4193. "relative"))
  4194. p = 3;
  4195. else if (contains_ci_len(s + vstart, vend - vstart,
  4196. "sticky"))
  4197. p = 4;
  4198. decl_add(out, nout, PROP_POS, p, important);
  4199. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  4200. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  4201. int which = str_eq(prop, "left") ? 0 :
  4202. str_eq(prop, "top") ? 1 :
  4203. str_eq(prop, "right") ? 2 : 3;
  4204. unsigned int pos = 0;
  4205. if (!(vend - vstart >= 4 &&
  4206. ci_eq_n(s + vstart, "auto", 4))) {
  4207. int v = parse_len(s + vstart, vend - vstart, &pos);
  4208. if (v > 32000)
  4209. v = 32000;
  4210. else if (v < -32000)
  4211. v = -32000;
  4212. decl_add(out, nout,
  4213. (unsigned char)(which == 0 ? PROP_LEFT :
  4214. which == 1 ? PROP_TOP :
  4215. which == 2 ? PROP_RIGHT :
  4216. PROP_BOTTOM),
  4217. v, important);
  4218. decl_add(out, nout,
  4219. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  4220. which == 1 ? PROP_TOP_SET :
  4221. which == 2 ? PROP_RIGHT_SET :
  4222. PROP_BOTTOM_SET),
  4223. 1, important);
  4224. }
  4225. } else if (str_eq(prop, "z-index")) {
  4226. unsigned int q = vstart;
  4227. int sign = 1;
  4228. int v = 0;
  4229. int any = 0;
  4230. if (q < vend && s[q] == '-') {
  4231. sign = -1;
  4232. q++;
  4233. }
  4234. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4235. v = v * 10 + (s[q] - '0');
  4236. any = 1;
  4237. q++;
  4238. }
  4239. if (any) {
  4240. v *= sign;
  4241. if (v > 32000)
  4242. v = 32000;
  4243. else if (v < -32000)
  4244. v = -32000;
  4245. decl_add(out, nout, PROP_ZINDEX, v, important);
  4246. decl_add(out, nout, PROP_ZSET, 1, important);
  4247. } else if (vend - vstart >= 4 &&
  4248. ci_eq_n(s + vstart, "auto", 4)) {
  4249. decl_add(out, nout, PROP_ZSET, 0, important);
  4250. }
  4251. } else if (str_eq(prop, "transform")) {
  4252. const char *v = s + vstart;
  4253. unsigned int vl = vend - vstart;
  4254. int tx = 0, ty = 0;
  4255. int is_x = contains_ci_len(v, vl, "translatex");
  4256. int is_y = contains_ci_len(v, vl, "translatey");
  4257. unsigned int off = 0, arg;
  4258. if (contains_ci_len(v, vl, "translate")) {
  4259. while (off < vl && v[off] != '(')
  4260. off++;
  4261. if (off < vl)
  4262. off++;
  4263. if (!is_y)
  4264. tx = parse_len(v, vl, &off);
  4265. if (!is_x) {
  4266. while (off < vl &&
  4267. (is_space(v[off]) || v[off] == ','))
  4268. off++;
  4269. ty = parse_len(v, vl, &off);
  4270. }
  4271. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  4272. important);
  4273. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  4274. important);
  4275. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  4276. important);
  4277. }
  4278. if (find_fn(v, vl, "scale", &arg)) {
  4279. unsigned int q = arg;
  4280. signed short sx = 100, sy = 100;
  4281. if (parse_fixed_100(v, vl, &q, &sx)) {
  4282. unsigned int r = q;
  4283. while (r < vl &&
  4284. (is_space(v[r]) || v[r] == ','))
  4285. r++;
  4286. if (!parse_fixed_100(v, vl, &r, &sy))
  4287. sy = sx;
  4288. decl_add(out, nout, PROP_SCALE_X, sx,
  4289. important);
  4290. decl_add(out, nout, PROP_SCALE_Y, sy,
  4291. important);
  4292. decl_add(out, nout,
  4293. PROP_TRANSFORM_PAINT, 1,
  4294. important);
  4295. decl_add(out, nout,
  4296. PROP_TRANSFORM_SET, 1,
  4297. important);
  4298. }
  4299. }
  4300. if (find_fn(v, vl, "rotate", &arg)) {
  4301. unsigned int q = arg;
  4302. signed short deg;
  4303. if (parse_fixed_100(v, vl, &q, &deg)) {
  4304. decl_add(out, nout, PROP_ROTATE, deg,
  4305. important);
  4306. decl_add(out, nout,
  4307. PROP_TRANSFORM_PAINT, 1,
  4308. important);
  4309. decl_add(out, nout,
  4310. PROP_TRANSFORM_SET, 1,
  4311. important);
  4312. }
  4313. }
  4314. } else if (str_eq(prop, "transform-origin")) {
  4315. add_transform_origin(out, nout, s + vstart,
  4316. vend - vstart, important);
  4317. } else if (str_eq(prop, "grid-template-columns") ||
  4318. str_eq(prop, "grid-template")) {
  4319. const char *v = s + vstart;
  4320. unsigned int vl = vend - vstart;
  4321. unsigned short val[2];
  4322. unsigned char kind[2];
  4323. int n, q;
  4324. if (str_eq(prop, "grid-template")) {
  4325. for (q = 0; q < (int)vl; q++)
  4326. if (v[q] == '/') {
  4327. q++;
  4328. while (q < (int)vl && is_space(v[q]))
  4329. q++;
  4330. v += q;
  4331. vl -= (unsigned int)q;
  4332. break;
  4333. }
  4334. }
  4335. if (contains_ci_len(v, vl, "auto-fit") ||
  4336. contains_ci_len(v, vl, "auto-fill")) {
  4337. unsigned int mp = 0;
  4338. int found = 0;
  4339. unsigned short mw = 200;
  4340. unsigned char mk = 1;
  4341. for (q = 0; q + 7 <= (int)vl; q++)
  4342. if (ci_eq_n(v + q, "minmax(", 7)) {
  4343. mp = (unsigned int)q + 7;
  4344. found = 1;
  4345. break;
  4346. }
  4347. if (found)
  4348. parse_grid_track(v, vl, &mp, &mw, &mk);
  4349. if (mk != 1 || mw < 80)
  4350. mw = 200;
  4351. if (mw > 480)
  4352. mw = 480;
  4353. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  4354. decl_add(out, nout, PROP_WIDTH, mw, important);
  4355. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  4356. } else {
  4357. n = parse_grid_cols(v, vl, val, kind);
  4358. if (n > 0) {
  4359. decl_add(out, nout, PROP_WIDTH, val[0],
  4360. important);
  4361. decl_add(out, nout, PROP_WIDTH_KIND,
  4362. kind[0], important);
  4363. decl_add(out, nout, PROP_GRID_NCOLS, n,
  4364. important);
  4365. }
  4366. if (n > 1) {
  4367. decl_add(out, nout, PROP_MAXW, val[1],
  4368. important);
  4369. decl_add(out, nout, PROP_MAXW_KIND,
  4370. kind[1], important);
  4371. }
  4372. }
  4373. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  4374. unsigned int gp = 0;
  4375. unsigned short gv = 0;
  4376. unsigned char gk = 1;
  4377. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  4378. if (gv > 120)
  4379. gv = 120;
  4380. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  4381. }
  4382. } else if (str_eq(prop, "grid-area")) {
  4383. int slot = parse_grid_slot(s + vstart, vend - vstart);
  4384. if (slot)
  4385. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  4386. } else if (str_eq(prop, "width") ||
  4387. str_eq(prop, "max-width") ||
  4388. str_eq(prop, "min-width") ||
  4389. str_eq(prop, "height") ||
  4390. str_eq(prop, "max-height") ||
  4391. str_eq(prop, "min-height")) {
  4392. unsigned short v;
  4393. unsigned char kind;
  4394. if (vend - vstart >= 5 &&
  4395. ci_eq_n(s + vstart, "calc(", 5)) {
  4396. int pct, px;
  4397. if (parse_calc(s + vstart + 5,
  4398. vend - vstart - 5, &pct, &px)) {
  4399. if (str_eq(prop, "width")) {
  4400. decl_add(out, nout, PROP_WIDTH,
  4401. 0, important);
  4402. decl_add(out, nout,
  4403. PROP_WIDTH_KIND, 3,
  4404. important);
  4405. decl_add(out, nout,
  4406. PROP_WIDTH_CALC_PCT,
  4407. pct, important);
  4408. decl_add(out, nout,
  4409. PROP_WIDTH_CALC_PX,
  4410. px, important);
  4411. } else if (str_eq(prop, "max-width")) {
  4412. decl_add(out, nout, PROP_MAXW,
  4413. 0, important);
  4414. decl_add(out, nout,
  4415. PROP_MAXW_KIND, 3,
  4416. important);
  4417. decl_add(out, nout,
  4418. PROP_MAXW_CALC_PCT,
  4419. pct, important);
  4420. decl_add(out, nout,
  4421. PROP_MAXW_CALC_PX,
  4422. px, important);
  4423. } else if (str_eq(prop, "min-width")) {
  4424. decl_add(out, nout, PROP_MINW,
  4425. 0, important);
  4426. decl_add(out, nout,
  4427. PROP_MINW_KIND, 3,
  4428. important);
  4429. decl_add(out, nout,
  4430. PROP_MINW_CALC_PCT,
  4431. pct, important);
  4432. decl_add(out, nout,
  4433. PROP_MINW_CALC_PX,
  4434. px, important);
  4435. } else if (str_eq(prop, "height")) {
  4436. decl_add(out, nout, PROP_HEIGHT,
  4437. 0, important);
  4438. decl_add(out, nout,
  4439. PROP_HEIGHT_KIND, 3,
  4440. important);
  4441. decl_add(out, nout,
  4442. PROP_HEIGHT_CALC_PCT,
  4443. pct, important);
  4444. decl_add(out, nout,
  4445. PROP_HEIGHT_CALC_PX,
  4446. px, important);
  4447. } else if (str_eq(prop, "max-height")) {
  4448. decl_add(out, nout, PROP_MAXH,
  4449. 0, important);
  4450. decl_add(out, nout,
  4451. PROP_MAXH_KIND, 3,
  4452. important);
  4453. decl_add(out, nout,
  4454. PROP_MAXH_CALC_PCT,
  4455. pct, important);
  4456. decl_add(out, nout,
  4457. PROP_MAXH_CALC_PX,
  4458. px, important);
  4459. } else {
  4460. decl_add(out, nout, PROP_MINH,
  4461. 0, important);
  4462. decl_add(out, nout,
  4463. PROP_MINH_KIND, 3,
  4464. important);
  4465. decl_add(out, nout,
  4466. PROP_MINH_CALC_PCT,
  4467. pct, important);
  4468. decl_add(out, nout,
  4469. PROP_MINH_CALC_PX,
  4470. px, important);
  4471. }
  4472. }
  4473. continue;
  4474. }
  4475. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  4476. if (str_eq(prop, "width")) {
  4477. decl_add(out, nout, PROP_WIDTH, v, important);
  4478. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  4479. important);
  4480. } else if (str_eq(prop, "max-width")) {
  4481. decl_add(out, nout, PROP_MAXW, v, important);
  4482. decl_add(out, nout, PROP_MAXW_KIND, kind,
  4483. important);
  4484. } else if (str_eq(prop, "min-width")) {
  4485. decl_add(out, nout, PROP_MINW, v, important);
  4486. decl_add(out, nout, PROP_MINW_KIND, kind,
  4487. important);
  4488. } else if (str_eq(prop, "max-height")) {
  4489. decl_add(out, nout, PROP_MAXH, v, important);
  4490. decl_add(out, nout, PROP_MAXH_KIND, kind,
  4491. important);
  4492. } else if (str_eq(prop, "min-height")) {
  4493. decl_add(out, nout, PROP_MINH, v, important);
  4494. decl_add(out, nout, PROP_MINH_KIND, kind,
  4495. important);
  4496. } else {
  4497. decl_add(out, nout, PROP_HEIGHT, v, important);
  4498. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  4499. important);
  4500. }
  4501. }
  4502. } else if (str_eq(prop, "box-sizing")) {
  4503. int bb = (vend - vstart >= 10 &&
  4504. ci_eq_n(s + vstart, "border-box", 10));
  4505. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  4506. } else if (str_eq(prop, "line-height")) {
  4507. unsigned short px;
  4508. unsigned char pct;
  4509. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  4510. if (px) {
  4511. decl_add(out, nout, PROP_LH_PX, px, important);
  4512. decl_add(out, nout, PROP_LH_PCT, 0, important);
  4513. } else {
  4514. decl_add(out, nout, PROP_LH_PX, 0, important);
  4515. decl_add(out, nout, PROP_LH_PCT, pct, important);
  4516. }
  4517. }
  4518. } else if (str_eq(prop, "list-style") ||
  4519. str_eq(prop, "list-style-type")) {
  4520. int none = (vend - vstart >= 4 &&
  4521. contains_ci_len(s + vstart, vend - vstart,
  4522. "none"));
  4523. decl_add(out, nout, PROP_LIST_NONE, none, important);
  4524. } else if (str_eq(prop, "border-radius") ||
  4525. str_eq(prop, "border-top-left-radius") ||
  4526. str_eq(prop, "border-top-right-radius") ||
  4527. str_eq(prop, "border-bottom-right-radius") ||
  4528. str_eq(prop, "border-bottom-left-radius")) {
  4529. unsigned short v[4];
  4530. unsigned char k[4];
  4531. unsigned int pos = 0;
  4532. int n = 0;
  4533. while (pos < vend - vstart && n < 4) {
  4534. while (pos < vend - vstart &&
  4535. is_space(s[vstart + pos]))
  4536. pos++;
  4537. if (pos >= vend - vstart ||
  4538. s[vstart + pos] == '/')
  4539. break;
  4540. if (!parse_radius_value(s + vstart,
  4541. vend - vstart, &pos,
  4542. &v[n], &k[n]))
  4543. break;
  4544. n++;
  4545. }
  4546. if (n) {
  4547. unsigned short r[4];
  4548. unsigned char rk[4];
  4549. int i;
  4550. if (n == 1) {
  4551. for (i = 0; i < 4; i++) {
  4552. r[i] = v[0];
  4553. rk[i] = k[0];
  4554. }
  4555. } else if (n == 2) {
  4556. r[0] = r[2] = v[0];
  4557. r[1] = r[3] = v[1];
  4558. rk[0] = rk[2] = k[0];
  4559. rk[1] = rk[3] = k[1];
  4560. } else if (n == 3) {
  4561. r[0] = v[0];
  4562. r[1] = r[3] = v[1];
  4563. r[2] = v[2];
  4564. rk[0] = k[0];
  4565. rk[1] = rk[3] = k[1];
  4566. rk[2] = k[2];
  4567. } else {
  4568. for (i = 0; i < 4; i++) {
  4569. r[i] = v[i];
  4570. rk[i] = k[i];
  4571. }
  4572. }
  4573. for (i = 0; i < 4; i++)
  4574. r[i] = rk[i] == 2 ? 255 :
  4575. (r[i] > 255 ? 255 : r[i]);
  4576. if (str_eq(prop, "border-top-left-radius")) {
  4577. decl_add(out, nout, PROP_RADIUS_TL,
  4578. (short)r[0], important);
  4579. } else if (str_eq(prop,
  4580. "border-top-right-radius")) {
  4581. decl_add(out, nout, PROP_RADIUS_TR,
  4582. (short)r[0], important);
  4583. } else if (str_eq(prop,
  4584. "border-bottom-right-radius")) {
  4585. decl_add(out, nout, PROP_RADIUS_BR,
  4586. (short)r[0], important);
  4587. } else if (str_eq(prop,
  4588. "border-bottom-left-radius")) {
  4589. decl_add(out, nout, PROP_RADIUS_BL,
  4590. (short)r[0], important);
  4591. } else {
  4592. decl_add(out, nout, PROP_RADIUS_TL,
  4593. (short)r[0], important);
  4594. decl_add(out, nout, PROP_RADIUS_TR,
  4595. (short)r[1], important);
  4596. decl_add(out, nout, PROP_RADIUS_BR,
  4597. (short)r[2], important);
  4598. decl_add(out, nout, PROP_RADIUS_BL,
  4599. (short)r[3], important);
  4600. }
  4601. }
  4602. } else if (str_eq(prop, "border") ||
  4603. str_eq(prop, "border-top") ||
  4604. str_eq(prop, "border-right") ||
  4605. str_eq(prop, "border-bottom") ||
  4606. str_eq(prop, "border-left") ||
  4607. str_eq(prop, "border-color") ||
  4608. str_eq(prop, "border-width") ||
  4609. str_eq(prop, "border-style") ||
  4610. str_eq(prop, "border-top-width") ||
  4611. str_eq(prop, "border-right-width") ||
  4612. str_eq(prop, "border-bottom-width") ||
  4613. str_eq(prop, "border-left-width") ||
  4614. str_eq(prop, "border-top-color") ||
  4615. str_eq(prop, "border-right-color") ||
  4616. str_eq(prop, "border-bottom-color") ||
  4617. str_eq(prop, "border-left-color") ||
  4618. str_eq(prop, "border-top-style") ||
  4619. str_eq(prop, "border-right-style") ||
  4620. str_eq(prop, "border-bottom-style") ||
  4621. str_eq(prop, "border-left-style")) {
  4622. const char *v = s + vstart;
  4623. unsigned int vl = vend - vstart;
  4624. unsigned int pos = 0, ts, te;
  4625. int widths[4], colors[4], styles[4];
  4626. int nw = 0, nc = 0, ns = 0;
  4627. int side = -1;
  4628. int is_wl = str_eq(prop, "border-width");
  4629. int is_cl = str_eq(prop, "border-color");
  4630. int is_sl = str_eq(prop, "border-style");
  4631. int i;
  4632. if (str_eq(prop, "border-top")) side = 0;
  4633. else if (str_eq(prop, "border-right")) side = 1;
  4634. else if (str_eq(prop, "border-bottom")) side = 2;
  4635. else if (str_eq(prop, "border-left")) side = 3;
  4636. else if (str_eq(prop, "border-top-width")) {
  4637. side = 0; is_wl = 2;
  4638. } else if (str_eq(prop, "border-right-width")) {
  4639. side = 1; is_wl = 2;
  4640. } else if (str_eq(prop, "border-bottom-width")) {
  4641. side = 2; is_wl = 2;
  4642. } else if (str_eq(prop, "border-left-width")) {
  4643. side = 3; is_wl = 2;
  4644. } else if (str_eq(prop, "border-top-color")) {
  4645. side = 0; is_cl = 2;
  4646. } else if (str_eq(prop, "border-right-color")) {
  4647. side = 1; is_cl = 2;
  4648. } else if (str_eq(prop, "border-bottom-color")) {
  4649. side = 2; is_cl = 2;
  4650. } else if (str_eq(prop, "border-left-color")) {
  4651. side = 3; is_cl = 2;
  4652. } else if (str_eq(prop, "border-top-style")) {
  4653. side = 0; is_sl = 2;
  4654. } else if (str_eq(prop, "border-right-style")) {
  4655. side = 1; is_sl = 2;
  4656. } else if (str_eq(prop, "border-bottom-style")) {
  4657. side = 2; is_sl = 2;
  4658. } else if (str_eq(prop, "border-left-style")) {
  4659. side = 3; is_sl = 2;
  4660. }
  4661. if (is_wl) {
  4662. while (nw < 4 && next_token(v, vl, &pos,
  4663. &ts, &te)) {
  4664. int x;
  4665. if (!parse_len_token(v + ts, te - ts,
  4666. &x))
  4667. break;
  4668. widths[nw++] = x;
  4669. }
  4670. if (nw) {
  4671. if (is_wl == 1)
  4672. expand_four(widths, nw);
  4673. if (side >= 0 && is_wl == 2) {
  4674. decl_add(out, nout,
  4675. PROP_BW_T + side,
  4676. widths[0], important);
  4677. } else {
  4678. for (i = 0; i < 4; i++)
  4679. decl_add(out, nout,
  4680. PROP_BW_T + i,
  4681. widths[i],
  4682. important);
  4683. }
  4684. }
  4685. } else if (is_cl) {
  4686. unsigned short c;
  4687. while (nc < 4 && next_token(v, vl, &pos,
  4688. &ts, &te)) {
  4689. if (!parse_color(v + ts, te - ts, &c))
  4690. break;
  4691. colors[nc++] = c;
  4692. }
  4693. if (nc) {
  4694. if (is_cl == 1)
  4695. expand_four(colors, nc);
  4696. if (side >= 0 && is_cl == 2) {
  4697. decl_add(out, nout,
  4698. PROP_BC_T + side,
  4699. colors[0], important);
  4700. } else {
  4701. for (i = 0; i < 4; i++)
  4702. decl_add(out, nout,
  4703. PROP_BC_T + i,
  4704. colors[i],
  4705. important);
  4706. }
  4707. }
  4708. } else if (is_sl) {
  4709. while (ns < 4 && next_token(v, vl, &pos,
  4710. &ts, &te)) {
  4711. int stv;
  4712. if ((te - ts == 4 &&
  4713. ci_eq_n(v + ts, "none", 4)) ||
  4714. (te - ts == 6 &&
  4715. ci_eq_n(v + ts, "hidden", 6)))
  4716. stv = 0;
  4717. else
  4718. stv = 1;
  4719. styles[ns++] = stv;
  4720. }
  4721. if (ns) {
  4722. if (is_sl == 1)
  4723. expand_four(styles, ns);
  4724. if (is_sl == 2 && side >= 0) {
  4725. if (!styles[0])
  4726. decl_add(out, nout,
  4727. PROP_BW_T + side,
  4728. 0, important);
  4729. } else {
  4730. for (i = 0; i < 4; i++)
  4731. if (!styles[i])
  4732. decl_add(out, nout,
  4733. PROP_BW_T + i,
  4734. 0,
  4735. important);
  4736. }
  4737. }
  4738. } else {
  4739. int bw = -1, bc = -1, style_none = 0;
  4740. while (next_token(v, vl, &pos, &ts, &te)) {
  4741. unsigned short c;
  4742. int x;
  4743. if ((te - ts == 4 &&
  4744. ci_eq_n(v + ts, "none", 4)) ||
  4745. (te - ts == 6 &&
  4746. ci_eq_n(v + ts, "hidden", 6))) {
  4747. style_none = 1;
  4748. } else if (parse_len_token(v + ts,
  4749. te - ts, &x)) {
  4750. bw = x;
  4751. } else if (parse_color(v + ts,
  4752. te - ts, &c)) {
  4753. bc = c;
  4754. }
  4755. }
  4756. if (style_none)
  4757. bw = 0;
  4758. if (bc >= 0 && bw < 0)
  4759. bw = 1;
  4760. if (side < 0) {
  4761. if (bw >= 0)
  4762. decl_add(out, nout,
  4763. PROP_BORDER_W, bw,
  4764. important);
  4765. if (bc >= 0)
  4766. decl_add(out, nout,
  4767. PROP_BORDER_COLOR, bc,
  4768. important);
  4769. } else {
  4770. if (bw >= 0)
  4771. decl_add(out, nout,
  4772. PROP_BW_T + side, bw,
  4773. important);
  4774. if (bc >= 0)
  4775. decl_add(out, nout,
  4776. PROP_BC_T + side, bc,
  4777. important);
  4778. }
  4779. }
  4780. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  4781. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  4782. unsigned int pos = 0;
  4783. int is_pad = prop[0] == 'p';
  4784. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  4785. while (pos < vend - vstart && nv < 4) {
  4786. units[nv] = 0;
  4787. if (parse_margin_calc(s + vstart,
  4788. vend - vstart, &pos,
  4789. &vals[nv], &cpx[nv])) {
  4790. units[nv] = 5;
  4791. } else {
  4792. vals[nv] = parse_margin_len(
  4793. s + vstart, vend - vstart,
  4794. &pos, &units[nv]);
  4795. }
  4796. nv++;
  4797. while (pos < vend - vstart &&
  4798. is_space(s[vstart + pos]))
  4799. pos++;
  4800. }
  4801. if (nv == 1) {
  4802. vals[1] = vals[2] = vals[3] = vals[0];
  4803. units[1] = units[2] = units[3] = units[0];
  4804. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  4805. } else if (nv == 2) {
  4806. vals[2] = vals[0];
  4807. vals[3] = vals[1];
  4808. units[2] = units[0];
  4809. units[3] = units[1];
  4810. cpx[2] = cpx[0];
  4811. cpx[3] = cpx[1];
  4812. } else if (nv == 3) {
  4813. vals[3] = vals[1];
  4814. units[3] = units[1];
  4815. cpx[3] = cpx[1];
  4816. }
  4817. if (nv > 0) {
  4818. int k;
  4819. for (k = 0; k < 4; k++) {
  4820. int v = vals[k];
  4821. int u = units[k];
  4822. int au = u == 4;
  4823. if (au)
  4824. v = 0;
  4825. if (is_pad && au)
  4826. u = 0;
  4827. decl_add(out, nout,
  4828. (unsigned char)(base + k), v,
  4829. important);
  4830. decl_add(out, nout,
  4831. (unsigned char)
  4832. ((is_pad ? PROP_PT_U :
  4833. PROP_MT_U) + k),
  4834. u, important);
  4835. if (u == 5)
  4836. decl_add(out, nout,
  4837. (unsigned char)
  4838. ((is_pad ? PROP_PT_CPX :
  4839. PROP_MT_CPX) + k),
  4840. cpx[k], important);
  4841. if (!is_pad && (k == 1 || k == 3))
  4842. decl_add(out, nout,
  4843. k == 1 ? PROP_MR_AUTO :
  4844. PROP_ML_AUTO,
  4845. au, important);
  4846. }
  4847. }
  4848. } else if (str_eq(prop, "margin-top") ||
  4849. str_eq(prop, "margin-right") ||
  4850. str_eq(prop, "margin-bottom") ||
  4851. str_eq(prop, "margin-left") ||
  4852. str_eq(prop, "padding-top") ||
  4853. str_eq(prop, "padding-right") ||
  4854. str_eq(prop, "padding-bottom") ||
  4855. str_eq(prop, "padding-left")) {
  4856. unsigned int pos = 0;
  4857. int unit = 0;
  4858. int v = 0, cpx = 0;
  4859. unsigned char p = PROP_MT;
  4860. const char *side;
  4861. int au;
  4862. if (parse_margin_calc(s + vstart, vend - vstart,
  4863. &pos, &v, &cpx))
  4864. unit = 5;
  4865. else
  4866. v = parse_margin_len(s + vstart,
  4867. vend - vstart, &pos,
  4868. &unit);
  4869. au = unit == 4;
  4870. if (au)
  4871. v = 0;
  4872. if (prop[0] == 'p') {
  4873. p = PROP_PT;
  4874. if (au)
  4875. unit = 0;
  4876. }
  4877. side = prop + (prop[0] == 'p' ? 8 : 7);
  4878. if (str_eq(side, "right"))
  4879. p = (unsigned char)(p + 1);
  4880. else if (str_eq(side, "bottom"))
  4881. p = (unsigned char)(p + 2);
  4882. else if (str_eq(side, "left"))
  4883. p = (unsigned char)(p + 3);
  4884. decl_add(out, nout, p, v, important);
  4885. decl_add(out, nout,
  4886. (unsigned char)((prop[0] == 'p' ?
  4887. PROP_PT_U : PROP_MT_U) +
  4888. (p - (prop[0] == 'p' ?
  4889. PROP_PT : PROP_MT))),
  4890. unit, important);
  4891. if (unit == 5)
  4892. decl_add(out, nout,
  4893. (unsigned char)((prop[0] == 'p' ?
  4894. PROP_PT_CPX : PROP_MT_CPX) +
  4895. (p - (prop[0] == 'p' ?
  4896. PROP_PT : PROP_MT))),
  4897. cpx, important);
  4898. if (str_eq(side, "right"))
  4899. decl_add(out, nout, PROP_MR_AUTO, au, important);
  4900. else if (str_eq(side, "left"))
  4901. decl_add(out, nout, PROP_ML_AUTO, au, important);
  4902. }
  4903. }
  4904. }
  4905. /* Skip @media/@font-face/@keyframes/@import without descending into their
  4906. * nested blocks. This is the mobile/base rules path: media enhancements are
  4907. * deliberately ignored for now. */
  4908. static unsigned int skip_at_rule(const char *s, unsigned int len,
  4909. unsigned int i)
  4910. {
  4911. unsigned int j = i;
  4912. while (j < len && s[j] != '{' && s[j] != ';')
  4913. j++;
  4914. if (j >= len)
  4915. return len;
  4916. if (s[j] == ';')
  4917. return j + 1;
  4918. j++;
  4919. return css_skip_nested(s, len, j);
  4920. }
  4921. static unsigned int viewport_w = 1024;
  4922. static unsigned short theme_text = 0x0000;
  4923. static unsigned short theme_bg = 0xFFFF;
  4924. static unsigned short theme_panel = 0xEF5D;
  4925. static unsigned short theme_link = 0x001B;
  4926. void br_set_theme(unsigned short text, unsigned short bg,
  4927. unsigned short panel, unsigned short link)
  4928. {
  4929. theme_text = text;
  4930. theme_bg = bg;
  4931. theme_panel = panel;
  4932. theme_link = link;
  4933. }
  4934. void br_set_viewport(unsigned int width)
  4935. {
  4936. viewport_w = width ? width : 1024;
  4937. }
  4938. static unsigned int media_len_px(const char *s, unsigned int len)
  4939. {
  4940. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  4941. int em = 0;
  4942. while (i < len && is_space(s[i]))
  4943. i++;
  4944. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4945. whole = whole * 10 + (unsigned int)(s[i] - '0');
  4946. i++;
  4947. }
  4948. if (i < len && s[i] == '.') {
  4949. i++;
  4950. /* Consume every fractional digit even when there are more
  4951. * than the three we keep: leaving the tail behind made the
  4952. * unit check miss "em" (74.9375em became 74px). */
  4953. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4954. if (scale < 1000) {
  4955. frac = frac * 10 + (unsigned int)(s[i] - '0');
  4956. scale *= 10;
  4957. }
  4958. i++;
  4959. }
  4960. }
  4961. while (i < len && is_space(s[i]))
  4962. i++;
  4963. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  4964. em = 1;
  4965. i += 2;
  4966. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  4967. em = 1;
  4968. i += 3;
  4969. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  4970. i += 2;
  4971. }
  4972. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  4973. if (em)
  4974. return v * 16 / 100;
  4975. return v / 100;
  4976. }
  4977. /* One parenthesised media feature: min-width/max-width and the modern
  4978. * (width <= 600px) form. Unknown features are treated as true. */
  4979. static int media_feature_matches(const char *s, unsigned int len)
  4980. {
  4981. unsigned int i;
  4982. while (len && is_space(*s)) { s++; len--; }
  4983. while (len && is_space(s[len - 1])) len--;
  4984. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  4985. /* There is no user-selectable colour scheme. Ignore both
  4986. * variants so a page's base palette is used; sites whose base
  4987. * is dark (like the project docs) then stay dark instead of
  4988. * flipping to their light media override. */
  4989. return 0;
  4990. }
  4991. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  4992. for (i = 9; i < len && s[i] != ':'; i++)
  4993. ;
  4994. if (i < len)
  4995. return viewport_w >= media_len_px(s + i + 1,
  4996. len - i - 1);
  4997. return 1;
  4998. }
  4999. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  5000. for (i = 9; i < len && s[i] != ':'; i++)
  5001. ;
  5002. if (i < len)
  5003. return viewport_w <= media_len_px(s + i + 1,
  5004. len - i - 1);
  5005. return 1;
  5006. }
  5007. for (i = 0; i < len; i++) {
  5008. if (s[i] == '<' || s[i] == '>') {
  5009. int max = s[i] == '<';
  5010. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  5011. if (i + 1 < len && s[i + 1] == '=')
  5012. v = media_len_px(s + i + 2, len - i - 2);
  5013. return max ? viewport_w <= v : viewport_w >= v;
  5014. }
  5015. }
  5016. return 1;
  5017. }
  5018. static int media_query_matches(const char *s, unsigned int len)
  5019. {
  5020. unsigned int i;
  5021. if (contains_ci_len(s, len, "print") &&
  5022. !contains_ci_len(s, len, "screen"))
  5023. return 0;
  5024. if (contains_ci_len(s, len, "not "))
  5025. return 0;
  5026. for (i = 0; i < len; i++) {
  5027. if (s[i] == '(') {
  5028. unsigned int st = ++i;
  5029. while (i < len && s[i] != ')')
  5030. i++;
  5031. if (!media_feature_matches(s + st, i - st))
  5032. return 0;
  5033. }
  5034. }
  5035. return 1;
  5036. }
  5037. static int media_list_matches(const char *s, unsigned int len)
  5038. {
  5039. unsigned int i = 0;
  5040. while (i < len) {
  5041. unsigned int st = i;
  5042. while (i < len && s[i] != ',')
  5043. i++;
  5044. if (media_query_matches(s + st, i - st))
  5045. return 1;
  5046. i++;
  5047. }
  5048. return 0;
  5049. }
  5050. static unsigned int rule_hash(const char *s, unsigned int len)
  5051. {
  5052. unsigned int h = 2166136261u, i;
  5053. for (i = 0; i < len; i++) {
  5054. char c = s[i];
  5055. if (c >= 'A' && c <= 'Z')
  5056. c = (char)(c - 'A' + 'a');
  5057. h ^= (unsigned char)c;
  5058. h *= 16777619u;
  5059. }
  5060. return h % BR_RULE_HASH;
  5061. }
  5062. /* Index a parsed rule by the most selective component of its rightmost
  5063. * compound selector. br_style_for() then only visits rules that can
  5064. * possibly match a node instead of scanning every rule. */
  5065. static void rule_index_add(struct br_doc *d, unsigned int idx,
  5066. const char *key, unsigned int len)
  5067. {
  5068. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  5069. unsigned short *head;
  5070. for (i = 0; i < len; i++) {
  5071. if (key[i] == '.' || key[i] == '#') {
  5072. unsigned int st = i + 1, n = 0;
  5073. while (st + n < len && is_ident(key[st + n]))
  5074. n++;
  5075. if (key[i] == '#') {
  5076. ipos = st;
  5077. ilen = n;
  5078. } else if (n) {
  5079. cpos = st;
  5080. clen = n;
  5081. }
  5082. i = st + n - 1;
  5083. }
  5084. }
  5085. if (ilen)
  5086. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  5087. else if (clen)
  5088. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  5089. else if (len == 5 && ci_eq_n(key, ":root", 5))
  5090. head = &d->rule_tag[BR_TAG_HTML];
  5091. else if (len && is_ident(key[0])) {
  5092. char name[16];
  5093. unsigned int n = 0, tag;
  5094. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  5095. name[n] = key[n];
  5096. n++;
  5097. }
  5098. name[n] = '\0';
  5099. tag = br_tag_from_name(name);
  5100. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  5101. head = &d->rule_tag[tag];
  5102. else
  5103. head = &d->rule_any;
  5104. } else {
  5105. head = &d->rule_any;
  5106. }
  5107. d->rule_next[idx] = *head;
  5108. *head = (unsigned short)idx;
  5109. }
  5110. static unsigned int selector_specificity(const char *s, unsigned int len);
  5111. static int css_collect_only;
  5112. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  5113. {
  5114. unsigned int i = 0;
  5115. while (i < len && d->nrules < d->rule_cap) {
  5116. unsigned int sstart, send, bstart, bend, j;
  5117. struct br_decl decls[BR_DECLS];
  5118. unsigned int nd;
  5119. while (i < len && is_space(s[i]))
  5120. i++;
  5121. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  5122. i += 2;
  5123. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  5124. i++;
  5125. i += 2;
  5126. continue;
  5127. }
  5128. if (i >= len || s[i] == '}')
  5129. break;
  5130. if (s[i] == '@') {
  5131. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  5132. unsigned int q = i + 6;
  5133. while (q < len && s[q] != '{')
  5134. q++;
  5135. if (q < len &&
  5136. media_list_matches(s + i + 6,
  5137. q - (i + 6))) {
  5138. unsigned int b = q + 1;
  5139. unsigned int e = css_skip_nested(s, len, b);
  5140. if (e > b)
  5141. parse_css(d, s + b,
  5142. (e - 1) - b);
  5143. i = e;
  5144. } else {
  5145. i = skip_at_rule(s, len, i);
  5146. }
  5147. continue;
  5148. }
  5149. i = skip_at_rule(s, len, i);
  5150. continue;
  5151. }
  5152. sstart = i;
  5153. while (i < len && s[i] != '{')
  5154. i++;
  5155. if (i >= len)
  5156. break;
  5157. send = i;
  5158. i++;
  5159. bstart = i;
  5160. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  5161. * to the matching close brace; stopping at the first '}' left
  5162. * the parser out of sync and dropped the rest of the sheet
  5163. * (nd-aktuell lost its desktop rules that way). */
  5164. bend = css_block_end(s, len, i);
  5165. i = bend < len ? bend + 1 : len;
  5166. while (sstart < send && is_space(s[sstart]))
  5167. sstart++;
  5168. while (send > sstart && is_space(s[send - 1]))
  5169. send--;
  5170. if (sstart == send)
  5171. continue;
  5172. if (!css_dark_selector(s + sstart, send - sstart))
  5173. css_collect_block(d, s + bstart, bend - bstart);
  5174. if (css_collect_only)
  5175. continue;
  5176. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  5177. if (!nd)
  5178. continue;
  5179. /* Split a comma-separated selector list into separate rules. */
  5180. j = sstart;
  5181. while (j <= send && d->nrules < d->rule_cap) {
  5182. unsigned int q = j, e, k;
  5183. char sel[160];
  5184. unsigned int off;
  5185. while (q < send && s[q] != ',')
  5186. q++;
  5187. e = q;
  5188. while (j < e && is_space(s[j]))
  5189. j++;
  5190. while (e > j && is_space(s[e - 1]))
  5191. e--;
  5192. if (e > j) {
  5193. /* Ignore single selectors with sibling combinators,
  5194. * pseudo-classes or attribute tests: they need
  5195. * state/attributes we do not model. The other
  5196. * selectors in the list still apply. */
  5197. for (k = j; k < e; ) {
  5198. if (s[k] == '+' || s[k] == '~')
  5199. break;
  5200. if (s[k] == ':') {
  5201. if (k + 5 <= e &&
  5202. ci_eq_n(s + k, ":root", 5)) {
  5203. k += 5;
  5204. continue;
  5205. }
  5206. if (k + 5 <= e &&
  5207. ci_eq_n(s + k, ":not(", 5)) {
  5208. unsigned int n = k + 5;
  5209. int nd = 1;
  5210. while (n < e && nd) {
  5211. if (s[n] == '(')
  5212. nd++;
  5213. else if (s[n] == ')')
  5214. nd--;
  5215. n++;
  5216. }
  5217. if (nd)
  5218. break;
  5219. k = n;
  5220. continue;
  5221. }
  5222. break;
  5223. }
  5224. k++;
  5225. }
  5226. if (k < e) {
  5227. j = q + 1;
  5228. if (q >= send)
  5229. break;
  5230. continue;
  5231. }
  5232. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  5233. sel[k] = s[j + k];
  5234. sel[k] = '\0';
  5235. off = store_attr(d, sel, k, 159);
  5236. if (off) {
  5237. struct br_rule *r = &d->rules[d->nrules++];
  5238. unsigned int ks = 0, q;
  5239. for (q = 0; q < k; q++)
  5240. if (sel[q] == ' ' || sel[q] == '>')
  5241. ks = q + 1;
  5242. r->sel_off = off;
  5243. r->key_off = store_attr(d, sel + ks,
  5244. k - ks, 159);
  5245. r->spec = (unsigned short)selector_specificity(sel, k);
  5246. r->single = 1;
  5247. {
  5248. unsigned int z;
  5249. for (z = 0; z < k; z++)
  5250. if (is_space(sel[z]) || sel[z] == '>') {
  5251. r->single = 0;
  5252. break;
  5253. }
  5254. }
  5255. r->nd = (unsigned char)nd;
  5256. for (k = 0; k < nd; k++)
  5257. r->d[k] = decls[k];
  5258. rule_index_add(d, d->nrules - 1,
  5259. attr_ptr(d, r->key_off),
  5260. s_len(attr_ptr(d, r->key_off)));
  5261. }
  5262. }
  5263. j = q + 1;
  5264. if (q >= send)
  5265. break;
  5266. }
  5267. }
  5268. }
  5269. /* Sites often set html { font-size: 62.5% } and then size everything in
  5270. * rem. Detect that root size before parsing the sheet so a rem resolves to
  5271. * the same pixels a real browser would use. */
  5272. static void detect_root_font(const char *s, unsigned int len)
  5273. {
  5274. unsigned int i;
  5275. for (i = 0; i + 4 < len; i++) {
  5276. unsigned int j, end, body, sel, q, h;
  5277. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  5278. int exact;
  5279. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  5280. s[i + 3] == 'l'))
  5281. continue;
  5282. h = i + 4;
  5283. while (h < len && is_space(s[h]))
  5284. h++;
  5285. if (h < len && s[h] != '{' && s[h] != ',' &&
  5286. s[h] != '.' && s[h] != ':' && s[h] != '[')
  5287. continue;
  5288. sel = i;
  5289. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  5290. s[sel - 1] != ';' && s[sel - 1] != ',')
  5291. sel--;
  5292. q = sel;
  5293. while (q < i && is_space(s[q]))
  5294. q++;
  5295. exact = (q == i);
  5296. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  5297. continue;
  5298. j = h;
  5299. if (j >= len || s[j] != '{')
  5300. continue;
  5301. while (j < len && s[j] != '{')
  5302. j++;
  5303. if (j >= len)
  5304. break;
  5305. end = j + 1;
  5306. body = end;
  5307. while (end < len && s[end] != '}')
  5308. end++;
  5309. for (; j + 9 < end; j++) {
  5310. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  5311. s[j + 3] == 't' && s[j + 4] == '-' &&
  5312. s[j + 5] == 's' && s[j + 6] == 'i' &&
  5313. s[j + 7] == 'z' && s[j + 8] == 'e'))
  5314. continue;
  5315. j += 9;
  5316. while (j < end && s[j] != ':')
  5317. j++;
  5318. if (j >= end)
  5319. break;
  5320. j++;
  5321. while (j < end && is_space(s[j]))
  5322. j++;
  5323. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5324. v = v * 10 + (s[j] - '0');
  5325. digits = 1;
  5326. j++;
  5327. }
  5328. if (j < end && s[j] == '.') {
  5329. j++;
  5330. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5331. v = v * 10 + (s[j] - '0');
  5332. frac++;
  5333. j++;
  5334. }
  5335. }
  5336. while (frac < 2) {
  5337. v *= 10;
  5338. frac++;
  5339. }
  5340. while (frac > 2) {
  5341. v /= 10;
  5342. frac--;
  5343. }
  5344. if (!digits)
  5345. break;
  5346. if (j < end && s[j] == '%')
  5347. pct = 1;
  5348. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  5349. px = 1;
  5350. if (pct)
  5351. v = (v * 16 + 5000) / 10000;
  5352. else if (px)
  5353. v /= 100;
  5354. else
  5355. v = v * 16 / 100;
  5356. if (exact && v >= 8 && v <= 24)
  5357. root_font_px = v;
  5358. if (exact)
  5359. return;
  5360. }
  5361. /* html { font: 10px/1 Verdana } also sets the root size. */
  5362. for (j = body; j + 5 <= end; j++) {
  5363. unsigned int p, ts, te;
  5364. if (!ci_eq_n(s + j, "font:", 5))
  5365. continue;
  5366. p = j + 5;
  5367. while (next_token(s, end, &p, &ts, &te)) {
  5368. int fv = parse_font_px(s + ts, te - ts);
  5369. if (fv >= 8 && fv <= 24) {
  5370. if (exact)
  5371. root_font_px = fv;
  5372. break;
  5373. }
  5374. }
  5375. if (exact)
  5376. return;
  5377. break;
  5378. }
  5379. }
  5380. }
  5381. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  5382. {
  5383. scan_font_faces(css, len);
  5384. detect_root_font(css, len);
  5385. css_collect_only = 1;
  5386. parse_css(d, css, len);
  5387. css_collect_only = 0;
  5388. parse_css(d, css, len);
  5389. }
  5390. void br_parse_inline_css(struct br_doc *d)
  5391. {
  5392. if (d->inline_css_len) {
  5393. scan_font_faces(css_store, d->inline_css_len);
  5394. detect_root_font(css_store, d->inline_css_len);
  5395. css_collect_only = 1;
  5396. parse_css(d, css_store, d->inline_css_len);
  5397. css_collect_only = 0;
  5398. parse_css(d, css_store, d->inline_css_len);
  5399. }
  5400. d->inline_css_len = 0;
  5401. }
  5402. void br_reset_styles(struct br_doc *d)
  5403. {
  5404. unsigned int i;
  5405. for (i = 0; i < d->nnode; i++)
  5406. d->nodes[i].style_valid = 0;
  5407. }
  5408. /* ---- selector matching ---- */
  5409. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  5410. unsigned int clen)
  5411. {
  5412. const char *s = attr_ptr(d, d->nodes[node].class_off);
  5413. unsigned int i = 0;
  5414. while (s[i]) {
  5415. unsigned int st, n;
  5416. while (s[i] && is_space(s[i]))
  5417. i++;
  5418. st = i;
  5419. while (s[i] && !is_space(s[i]))
  5420. i++;
  5421. n = i - st;
  5422. if (n == clen && ci_eq_n(s + st, cls, n))
  5423. return 1;
  5424. }
  5425. return 0;
  5426. }
  5427. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  5428. unsigned int len)
  5429. {
  5430. const char *s = attr_ptr(d, d->nodes[node].id_off);
  5431. unsigned int i;
  5432. for (i = 0; i < len; i++)
  5433. if (s[i] != id[i])
  5434. return 0;
  5435. return s[len] == '\0';
  5436. }
  5437. static int is_ident(char c)
  5438. {
  5439. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  5440. (c >= '0' && c <= '9') || c == '-' || c == '_';
  5441. }
  5442. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  5443. {
  5444. unsigned int n = 0;
  5445. while (lit[n])
  5446. n++;
  5447. return n == nlen && ci_eq_n(name, lit, nlen);
  5448. }
  5449. static int attr_present(struct br_doc *d, unsigned int node,
  5450. const char *name, unsigned int nlen)
  5451. {
  5452. struct br_node *n = &d->nodes[node];
  5453. if (attr_is(name, nlen, "data-collapse-target"))
  5454. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  5455. if (attr_is(name, nlen, "data-collapse-trigger"))
  5456. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  5457. if (attr_is(name, nlen, "is-open"))
  5458. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  5459. if (attr_is(name, nlen, "disabled"))
  5460. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  5461. if (attr_is(name, nlen, "open"))
  5462. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  5463. if (attr_is(name, nlen, "inert"))
  5464. return (n->attr_flags & BR_ATTR_INERT) != 0;
  5465. if (attr_is(name, nlen, "hidden"))
  5466. return (n->flags & BR_NODE_HIDDEN) != 0;
  5467. if (attr_is(name, nlen, "href"))
  5468. return attr_ptr(d, n->href_off)[0] != 0;
  5469. if (attr_is(name, nlen, "class"))
  5470. return attr_ptr(d, n->class_off)[0] != 0;
  5471. if (attr_is(name, nlen, "id"))
  5472. return attr_ptr(d, n->id_off)[0] != 0;
  5473. if (attr_is(name, nlen, "src"))
  5474. return attr_ptr(d, n->src_off)[0] != 0;
  5475. if (attr_is(name, nlen, "alt"))
  5476. return attr_ptr(d, n->alt_off)[0] != 0;
  5477. if (attr_is(name, nlen, "rel"))
  5478. return attr_ptr(d, n->rel_off)[0] != 0;
  5479. return 0;
  5480. }
  5481. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  5482. unsigned int len)
  5483. {
  5484. unsigned int i = 0;
  5485. int got = 0;
  5486. if (i < len && s[i] == '*') {
  5487. i++;
  5488. got = 1;
  5489. } else if (i < len && is_ident(s[i])) {
  5490. char name[16];
  5491. unsigned int n = 0, tag;
  5492. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  5493. name[n++] = s[i++];
  5494. name[n] = '\0';
  5495. tag = br_tag_from_name(name);
  5496. if (tag == BR_TAG_UNKNOWN) {
  5497. const char *raw = attr_ptr(d,
  5498. d->nodes[node].name_off);
  5499. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  5500. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  5501. return 0;
  5502. } else if (tag != d->nodes[node].tag) {
  5503. return 0;
  5504. }
  5505. got = 1;
  5506. }
  5507. while (i < len) {
  5508. if (s[i] == '.') {
  5509. unsigned int st;
  5510. i++;
  5511. st = i;
  5512. while (i < len && is_ident(s[i]))
  5513. i++;
  5514. if (!class_has(d, node, s + st, i - st))
  5515. return 0;
  5516. got = 1;
  5517. } else if (s[i] == '#') {
  5518. unsigned int st;
  5519. i++;
  5520. st = i;
  5521. while (i < len && is_ident(s[i]))
  5522. i++;
  5523. if (!id_eq(d, node, s + st, i - st))
  5524. return 0;
  5525. got = 1;
  5526. } else if (s[i] == '[') {
  5527. unsigned int st, e;
  5528. i++;
  5529. st = e = i;
  5530. while (e < len && s[e] != ']' && s[e] != '=' &&
  5531. !is_space(s[e]))
  5532. e++;
  5533. if (e == st || !attr_present(d, node, s + st, e - st))
  5534. return 0;
  5535. while (i < len && s[i] != ']')
  5536. i++;
  5537. if (i < len)
  5538. i++;
  5539. got = 1;
  5540. } else if (s[i] == ':') {
  5541. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  5542. (i + 5 == len || !is_ident(s[i + 5]))) {
  5543. if (d->nodes[node].tag != BR_TAG_HTML)
  5544. return 0;
  5545. i += 5;
  5546. got = 1;
  5547. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  5548. unsigned int st = i + 5, e = st;
  5549. int depth = 1;
  5550. while (e < len && depth) {
  5551. if (s[e] == '(')
  5552. depth++;
  5553. else if (s[e] == ')')
  5554. depth--;
  5555. if (depth)
  5556. e++;
  5557. }
  5558. if (e >= len)
  5559. return 0;
  5560. for (unsigned int z = st; z < e; z++)
  5561. if (s[z] == ':')
  5562. return 0;
  5563. if (compound_match(d, node, s + st, e - st))
  5564. return 0;
  5565. i = e + 1;
  5566. got = 1;
  5567. } else {
  5568. return 0; /* other pseudo-classes are not modelled */
  5569. }
  5570. } else {
  5571. i++;
  5572. }
  5573. }
  5574. return got;
  5575. }
  5576. static unsigned int selector_specificity(const char *s, unsigned int len)
  5577. {
  5578. unsigned int i, spec = 0;
  5579. for (i = 0; i < len; i++) {
  5580. if (s[i] == '#')
  5581. spec += 100;
  5582. else if (s[i] == '.' || s[i] == '[' ||
  5583. (s[i] == ':' && !(i + 5 <= len &&
  5584. ci_eq_n(s + i, ":root", 5))))
  5585. spec += 10;
  5586. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  5587. if (is_ident(s[i]))
  5588. spec += 1;
  5589. }
  5590. }
  5591. return spec;
  5592. }
  5593. /* Match one complex selector; returns specificity or -1. */
  5594. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  5595. unsigned int len, int right_done)
  5596. {
  5597. struct {
  5598. unsigned int start, end;
  5599. int child;
  5600. } parts[8];
  5601. int np = 0;
  5602. unsigned int i = 0;
  5603. int right;
  5604. while (i < len) {
  5605. int child = 0;
  5606. unsigned int st, e;
  5607. while (i < len && is_space(s[i]))
  5608. i++;
  5609. if (i < len && s[i] == '>') {
  5610. child = 1;
  5611. i++;
  5612. while (i < len && is_space(s[i]))
  5613. i++;
  5614. }
  5615. if (i >= len)
  5616. break;
  5617. st = i;
  5618. while (i < len && !is_space(s[i]) && s[i] != '>')
  5619. i++;
  5620. e = i;
  5621. if (np < 8) {
  5622. parts[np].start = st;
  5623. parts[np].end = e;
  5624. parts[np].child = child;
  5625. np++;
  5626. }
  5627. }
  5628. if (!np)
  5629. return -1;
  5630. if (!right_done &&
  5631. !compound_match(d, node, s + parts[np - 1].start,
  5632. parts[np - 1].end - parts[np - 1].start))
  5633. return -1;
  5634. right = np - 1;
  5635. while (right > 0) {
  5636. unsigned int p = d->nodes[node].parent;
  5637. if (!p)
  5638. return -1;
  5639. if (parts[right].child) {
  5640. if (!compound_match(d, p, s + parts[right - 1].start,
  5641. parts[right - 1].end -
  5642. parts[right - 1].start))
  5643. return -1;
  5644. node = p;
  5645. } else {
  5646. while (p && !compound_match(d, p,
  5647. s + parts[right - 1].start,
  5648. parts[right - 1].end -
  5649. parts[right - 1].start))
  5650. p = d->nodes[p].parent;
  5651. if (!p)
  5652. return -1;
  5653. node = p;
  5654. }
  5655. right--;
  5656. }
  5657. return (int)selector_specificity(s, len);
  5658. }
  5659. #define RULE_MATCH_MAX 512
  5660. struct rule_match {
  5661. unsigned short idx;
  5662. short spec;
  5663. };
  5664. static struct rule_match rule_matches[RULE_MATCH_MAX];
  5665. static unsigned int nrule_matches;
  5666. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  5667. {
  5668. struct br_rule *r = &d->rules[idx];
  5669. const char *key = attr_ptr(d, r->key_off);
  5670. int spec, key_ok = 0;
  5671. if (key[0]) {
  5672. if (!compound_match(d, node, key, s_len(key)))
  5673. return;
  5674. key_ok = 1;
  5675. }
  5676. spec = (r->single && key_ok) ? (int)r->spec :
  5677. selector_match(d, node, attr_ptr(d, r->sel_off),
  5678. s_len(attr_ptr(d, r->sel_off)), key_ok);
  5679. if (spec < 0)
  5680. return;
  5681. if (nrule_matches < RULE_MATCH_MAX) {
  5682. rule_matches[nrule_matches].idx = (unsigned short)idx;
  5683. rule_matches[nrule_matches].spec = (short)spec;
  5684. nrule_matches++;
  5685. }
  5686. }
  5687. static void rule_scan_list(struct br_doc *d, unsigned int node,
  5688. unsigned short head)
  5689. {
  5690. while (head != 0xFFFF) {
  5691. rule_try(d, node, head);
  5692. head = d->rule_next[head];
  5693. }
  5694. }
  5695. static void rule_scan_class(struct br_doc *d, unsigned int node,
  5696. const char *cls, unsigned int len)
  5697. {
  5698. if (len)
  5699. rule_scan_list(d, node,
  5700. d->rule_bucket[rule_hash(cls, len)]);
  5701. }
  5702. static void rule_collect(struct br_doc *d, unsigned int node)
  5703. {
  5704. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  5705. const char *id = attr_ptr(d, d->nodes[node].id_off);
  5706. unsigned int tag = d->nodes[node].tag;
  5707. unsigned int i;
  5708. nrule_matches = 0;
  5709. rule_scan_list(d, node, d->rule_any);
  5710. if (tag < BR_TAG_MAX)
  5711. rule_scan_list(d, node, d->rule_tag[tag]);
  5712. if (id[0])
  5713. rule_scan_list(d, node,
  5714. d->rule_bucket[rule_hash(id, s_len(id))]);
  5715. i = 0;
  5716. while (cls[i]) {
  5717. unsigned int st, n;
  5718. while (cls[i] && is_space(cls[i]))
  5719. i++;
  5720. st = i;
  5721. while (cls[i] && !is_space(cls[i]))
  5722. i++;
  5723. n = i - st;
  5724. if (n)
  5725. rule_scan_class(d, node, cls + st, n);
  5726. }
  5727. /* Rules must cascade in document order even though the index visits
  5728. * them in bucket order. */
  5729. for (i = 1; i < nrule_matches; i++) {
  5730. struct rule_match m = rule_matches[i];
  5731. unsigned int j = i;
  5732. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  5733. rule_matches[j] = rule_matches[j - 1];
  5734. j--;
  5735. }
  5736. rule_matches[j] = m;
  5737. }
  5738. }
  5739. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  5740. int font_px)
  5741. {
  5742. if (*kind == 4) {
  5743. *val = (unsigned short)(*val * font_px / 100);
  5744. *kind = 1;
  5745. } else if (*kind == 5) {
  5746. *val = (unsigned short)(*val * root_font_px / 100);
  5747. *kind = 1;
  5748. }
  5749. }
  5750. static void resolve_size_units(struct br_style *st)
  5751. {
  5752. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  5753. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  5754. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  5755. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  5756. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  5757. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  5758. }
  5759. static void set_border_side_w(struct br_style *st, int side, int v)
  5760. {
  5761. if (v < 0)
  5762. v = 0;
  5763. if (v > 32)
  5764. v = 32;
  5765. st->border_w[side] = (unsigned char)v;
  5766. if (st->border_w[side] && !st->border_c[side])
  5767. st->border_c[side] = st->color;
  5768. }
  5769. static void set_border_side_c(struct br_style *st, int side, int v)
  5770. {
  5771. st->border_c[side] = (unsigned short)v;
  5772. }
  5773. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  5774. {
  5775. switch (dcl->prop) {
  5776. case PROP_COLOR:
  5777. st->color = (unsigned short)dcl->val;
  5778. break;
  5779. case PROP_BG:
  5780. st->bg = (unsigned short)dcl->val;
  5781. st->has_bg = 1;
  5782. break;
  5783. case PROP_BG_IMAGE:
  5784. st->bg_image = (unsigned short)dcl->val;
  5785. break;
  5786. case PROP_BG_REPEAT:
  5787. st->bg_repeat = (unsigned char)dcl->val;
  5788. break;
  5789. case PROP_BG_POS_X_KIND:
  5790. st->bg_pos_x_kind = (unsigned char)dcl->val;
  5791. break;
  5792. case PROP_BG_POS_X:
  5793. st->bg_pos_x = (signed short)dcl->val;
  5794. break;
  5795. case PROP_BG_POS_Y_KIND:
  5796. st->bg_pos_y_kind = (unsigned char)dcl->val;
  5797. break;
  5798. case PROP_BG_POS_Y:
  5799. st->bg_pos_y = (signed short)dcl->val;
  5800. break;
  5801. case PROP_BG_SIZE_KIND:
  5802. st->bg_size_kind = (unsigned char)dcl->val;
  5803. break;
  5804. case PROP_BG_SIZE_W:
  5805. st->bg_size_w = (unsigned short)dcl->val;
  5806. break;
  5807. case PROP_BG_SIZE_H:
  5808. st->bg_size_h = (unsigned short)dcl->val;
  5809. break;
  5810. case PROP_FONT_PX:
  5811. st->font_raw = (unsigned short)dcl->val;
  5812. break;
  5813. case PROP_FONT_UNIT:
  5814. st->font_unit = (unsigned char)dcl->val;
  5815. break;
  5816. case PROP_BOLD:
  5817. st->bold = (unsigned char)(dcl->val != 0);
  5818. break;
  5819. case PROP_ITALIC:
  5820. st->italic = (unsigned char)(dcl->val != 0);
  5821. break;
  5822. case PROP_MONO:
  5823. st->mono = (unsigned char)(dcl->val != 0);
  5824. break;
  5825. case PROP_FONT_FACE:
  5826. st->font_face = (unsigned char)dcl->val;
  5827. break;
  5828. case PROP_ALIGN:
  5829. st->align = (unsigned char)dcl->val;
  5830. break;
  5831. case PROP_UNDERLINE:
  5832. st->underline = (unsigned char)(dcl->val != 0);
  5833. break;
  5834. case PROP_DISPLAY:
  5835. st->display = (unsigned char)dcl->val;
  5836. st->list_item = 0;
  5837. break;
  5838. case PROP_VIS_HIDDEN:
  5839. st->vis_hidden = (unsigned char)(dcl->val != 0);
  5840. break;
  5841. case PROP_OPACITY_HIDDEN:
  5842. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  5843. break;
  5844. case PROP_OVERFLOW_HIDDEN:
  5845. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  5846. break;
  5847. case PROP_MT: st->mt = dcl->val; break;
  5848. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  5849. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  5850. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  5851. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  5852. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  5853. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  5854. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  5855. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  5856. case PROP_MR: st->mr = dcl->val; break;
  5857. case PROP_MB: st->mb = dcl->val; break;
  5858. case PROP_ML: st->ml = dcl->val; break;
  5859. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  5860. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  5861. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  5862. break;
  5863. case PROP_PT: st->pt = dcl->val; break;
  5864. case PROP_PR: st->pr = dcl->val; break;
  5865. case PROP_PB: st->pb = dcl->val; break;
  5866. case PROP_PL: st->pl = dcl->val; break;
  5867. case PROP_MT_U:
  5868. case PROP_MR_U:
  5869. case PROP_MB_U:
  5870. case PROP_ML_U:
  5871. case PROP_PT_U:
  5872. case PROP_PR_U:
  5873. case PROP_PB_U:
  5874. case PROP_PL_U:
  5875. st->mp_unit[dcl->prop - PROP_MT_U] =
  5876. (unsigned char)dcl->val;
  5877. break;
  5878. case PROP_BORDER_W:
  5879. set_border_side_w(st, 0, dcl->val);
  5880. set_border_side_w(st, 1, dcl->val);
  5881. set_border_side_w(st, 2, dcl->val);
  5882. set_border_side_w(st, 3, dcl->val);
  5883. break;
  5884. case PROP_BORDER_COLOR:
  5885. set_border_side_c(st, 0, dcl->val);
  5886. set_border_side_c(st, 1, dcl->val);
  5887. set_border_side_c(st, 2, dcl->val);
  5888. set_border_side_c(st, 3, dcl->val);
  5889. break;
  5890. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  5891. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  5892. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  5893. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  5894. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  5895. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  5896. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  5897. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  5898. case PROP_RADIUS_TL:
  5899. case PROP_RADIUS_TR:
  5900. case PROP_RADIUS_BR:
  5901. case PROP_RADIUS_BL:
  5902. st->radius[dcl->prop - PROP_RADIUS_TL] =
  5903. (unsigned char)dcl->val;
  5904. break;
  5905. case PROP_LIST_NONE:
  5906. st->no_bullet = (unsigned char)(dcl->val != 0);
  5907. break;
  5908. case PROP_NOWRAP:
  5909. st->nowrap = (unsigned char)(dcl->val != 0);
  5910. break;
  5911. case PROP_WIDTH:
  5912. st->width = (unsigned short)dcl->val;
  5913. break;
  5914. case PROP_WIDTH_KIND:
  5915. st->width_kind = (unsigned char)dcl->val;
  5916. break;
  5917. case PROP_WIDTH_CALC_PCT:
  5918. st->width_calc_pct = dcl->val;
  5919. break;
  5920. case PROP_WIDTH_CALC_PX:
  5921. st->width_calc_px = dcl->val;
  5922. break;
  5923. case PROP_MAXW:
  5924. st->max_width = (unsigned short)dcl->val;
  5925. break;
  5926. case PROP_MAXW_KIND:
  5927. st->max_width_kind = (unsigned char)dcl->val;
  5928. break;
  5929. case PROP_MAXW_CALC_PCT:
  5930. st->max_width_calc_pct = (signed short)dcl->val;
  5931. break;
  5932. case PROP_MAXW_CALC_PX:
  5933. st->max_width_calc_px = (signed short)dcl->val;
  5934. break;
  5935. case PROP_MINW:
  5936. st->min_width = (unsigned short)dcl->val;
  5937. break;
  5938. case PROP_MINW_KIND:
  5939. st->min_width_kind = (unsigned char)dcl->val;
  5940. break;
  5941. case PROP_MINW_CALC_PCT:
  5942. st->min_width_calc_pct = (signed short)dcl->val;
  5943. break;
  5944. case PROP_MINW_CALC_PX:
  5945. st->min_width_calc_px = (signed short)dcl->val;
  5946. break;
  5947. case PROP_HEIGHT:
  5948. st->height = (unsigned short)dcl->val;
  5949. break;
  5950. case PROP_HEIGHT_KIND:
  5951. st->height_kind = (unsigned char)dcl->val;
  5952. break;
  5953. case PROP_HEIGHT_CALC_PCT:
  5954. st->height_calc_pct = (signed short)dcl->val;
  5955. break;
  5956. case PROP_HEIGHT_CALC_PX:
  5957. st->height_calc_px = (signed short)dcl->val;
  5958. break;
  5959. case PROP_MINH:
  5960. st->min_height = (unsigned short)dcl->val;
  5961. break;
  5962. case PROP_MINH_KIND:
  5963. st->min_height_kind = (unsigned char)dcl->val;
  5964. break;
  5965. case PROP_MINH_CALC_PCT:
  5966. st->min_height_calc_pct = (signed short)dcl->val;
  5967. break;
  5968. case PROP_MINH_CALC_PX:
  5969. st->min_height_calc_px = (signed short)dcl->val;
  5970. break;
  5971. case PROP_MAXH:
  5972. st->max_height = (unsigned short)dcl->val;
  5973. break;
  5974. case PROP_MAXH_KIND:
  5975. st->max_height_kind = (unsigned char)dcl->val;
  5976. break;
  5977. case PROP_MAXH_CALC_PCT:
  5978. st->max_height_calc_pct = (signed short)dcl->val;
  5979. break;
  5980. case PROP_MAXH_CALC_PX:
  5981. st->max_height_calc_px = (signed short)dcl->val;
  5982. break;
  5983. case PROP_LH_PCT:
  5984. st->line_h_pct = (unsigned char)dcl->val;
  5985. break;
  5986. case PROP_LH_PX:
  5987. st->line_h_px = (unsigned short)dcl->val;
  5988. break;
  5989. case PROP_UPPER:
  5990. st->uppercase = (unsigned char)(dcl->val != 0);
  5991. break;
  5992. case PROP_FLOAT:
  5993. st->float_left = (unsigned char)dcl->val;
  5994. break;
  5995. case PROP_CLEAR:
  5996. st->clear = (unsigned char)dcl->val;
  5997. break;
  5998. case PROP_FLEX_GROW:
  5999. st->flex_grow = (unsigned char)dcl->val;
  6000. break;
  6001. case PROP_FLEX_COL:
  6002. st->flex_col = (unsigned char)(dcl->val != 0);
  6003. break;
  6004. case PROP_FLEX_WRAP:
  6005. st->flex_wrap = (unsigned char)(dcl->val != 0);
  6006. break;
  6007. case PROP_JUSTIFY:
  6008. st->justify = (unsigned char)dcl->val;
  6009. break;
  6010. case PROP_ALIGN_ITEMS:
  6011. st->align_items = (unsigned char)dcl->val;
  6012. break;
  6013. case PROP_GRID_NCOLS:
  6014. st->grid_ncols = (unsigned char)dcl->val;
  6015. break;
  6016. case PROP_GRID_GAP:
  6017. st->grid_gap = (unsigned char)dcl->val;
  6018. break;
  6019. case PROP_GRID_SLOT:
  6020. st->grid_slot = (unsigned char)dcl->val;
  6021. break;
  6022. case PROP_GRID_AUTO:
  6023. st->grid_auto = (unsigned char)(dcl->val != 0);
  6024. break;
  6025. case PROP_POS:
  6026. st->pos = (unsigned char)dcl->val;
  6027. break;
  6028. case PROP_LEFT:
  6029. st->left = dcl->val;
  6030. break;
  6031. case PROP_TOP:
  6032. st->top = dcl->val;
  6033. break;
  6034. case PROP_RIGHT:
  6035. st->right = dcl->val;
  6036. break;
  6037. case PROP_BOTTOM:
  6038. st->bottom = dcl->val;
  6039. break;
  6040. case PROP_LEFT_SET:
  6041. st->left_set = (unsigned char)(dcl->val != 0);
  6042. break;
  6043. case PROP_TOP_SET:
  6044. st->top_set = (unsigned char)(dcl->val != 0);
  6045. break;
  6046. case PROP_RIGHT_SET:
  6047. st->right_set = (unsigned char)(dcl->val != 0);
  6048. break;
  6049. case PROP_BOTTOM_SET:
  6050. st->bottom_set = (unsigned char)(dcl->val != 0);
  6051. break;
  6052. case PROP_ZINDEX:
  6053. st->z_index = dcl->val;
  6054. break;
  6055. case PROP_ZSET:
  6056. st->z_set = (unsigned char)(dcl->val != 0);
  6057. break;
  6058. case PROP_TRANSFORM_X:
  6059. st->tx = dcl->val;
  6060. break;
  6061. case PROP_TRANSFORM_Y:
  6062. st->ty = dcl->val;
  6063. break;
  6064. case PROP_TRANSFORM_SET:
  6065. st->transform_set = (unsigned char)(dcl->val != 0);
  6066. break;
  6067. case PROP_TRANSFORM_PAINT:
  6068. st->transform_scale = (unsigned char)(dcl->val != 0);
  6069. break;
  6070. case PROP_SCALE_X:
  6071. st->scale_x = (signed short)dcl->val;
  6072. break;
  6073. case PROP_SCALE_Y:
  6074. st->scale_y = (signed short)dcl->val;
  6075. break;
  6076. case PROP_ROTATE:
  6077. st->rotate_x100 = (signed short)dcl->val;
  6078. break;
  6079. case PROP_ORIGIN_X_KIND:
  6080. st->origin_x_kind = (unsigned char)dcl->val;
  6081. break;
  6082. case PROP_ORIGIN_X:
  6083. st->origin_x = (signed short)dcl->val;
  6084. break;
  6085. case PROP_ORIGIN_Y_KIND:
  6086. st->origin_y_kind = (unsigned char)dcl->val;
  6087. break;
  6088. case PROP_ORIGIN_Y:
  6089. st->origin_y = (signed short)dcl->val;
  6090. break;
  6091. default:
  6092. break;
  6093. }
  6094. }
  6095. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  6096. int spec, short *best, short *best_imp)
  6097. {
  6098. unsigned char p = dcl->prop;
  6099. if (p >= PROP_COUNT)
  6100. return;
  6101. if (dcl->important) {
  6102. if (spec >= best_imp[p]) {
  6103. apply_decl(st, dcl);
  6104. best_imp[p] = (short)spec;
  6105. }
  6106. } else if (best_imp[p] < 0 && spec >= best[p]) {
  6107. apply_decl(st, dcl);
  6108. best[p] = (short)spec;
  6109. }
  6110. }
  6111. void br_style_for(struct br_doc *d, unsigned int node,
  6112. const struct br_style *parent, struct br_style *out)
  6113. {
  6114. unsigned int tag;
  6115. short best[PROP_COUNT], best_imp[PROP_COUNT];
  6116. unsigned int i;
  6117. if (d->nodes[node].style_valid) {
  6118. *out = d->nodes[node].style;
  6119. return;
  6120. }
  6121. *out = *parent;
  6122. out->has_bg = 0;
  6123. out->bg_image = 0;
  6124. out->bg_repeat = 0;
  6125. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  6126. out->bg_pos_x = out->bg_pos_y = 0;
  6127. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  6128. out->float_left = 0;
  6129. out->clear = 0;
  6130. out->list_item = 0;
  6131. out->font_unit = 0;
  6132. for (i = 0; i < 4; i++) {
  6133. out->border_w[i] = 0;
  6134. out->border_c[i] = 0;
  6135. out->radius[i] = 0;
  6136. }
  6137. out->width = out->max_width = out->min_width = out->height = 0;
  6138. out->min_height = out->max_height = 0;
  6139. out->width_calc_pct = out->width_calc_px = 0;
  6140. out->height_calc_pct = out->height_calc_px = 0;
  6141. out->min_height_calc_pct = out->min_height_calc_px = 0;
  6142. out->max_height_calc_pct = out->max_height_calc_px = 0;
  6143. out->min_width_calc_pct = out->min_width_calc_px = 0;
  6144. out->max_width_calc_pct = out->max_width_calc_px = 0;
  6145. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  6146. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  6147. out->box_sizing = 0;
  6148. out->ml_auto = out->mr_auto = 0;
  6149. out->mt = out->mr = out->mb = out->ml = 0;
  6150. out->pt = out->pr = out->pb = out->pl = 0;
  6151. for (i = 0; i < 8; i++) {
  6152. out->mp_unit[i] = 0;
  6153. out->mp_calc_px[i] = 0;
  6154. }
  6155. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  6156. out->justify = out->align_items = 0;
  6157. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  6158. out->grid_auto = 0;
  6159. out->pos = out->left_set = out->top_set = 0;
  6160. out->right_set = out->bottom_set = 0;
  6161. out->left = out->top = out->right = out->bottom = 0;
  6162. out->z_index = 0;
  6163. out->z_set = 0;
  6164. out->tx = out->ty = 0;
  6165. out->transform_set = 0;
  6166. out->transform_scale = 0;
  6167. out->scale_x = out->scale_y = 100;
  6168. out->rotate_x100 = 0;
  6169. out->origin_x = out->origin_y = 50;
  6170. out->origin_x_kind = out->origin_y_kind = 1;
  6171. out->vis_hidden = out->opacity_hidden = 0;
  6172. out->overflow_hidden = 0;
  6173. tag = d->nodes[node].tag;
  6174. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  6175. BR_DISPLAY_INLINE;
  6176. for (i = 0; i < PROP_COUNT; i++) {
  6177. best[i] = -1;
  6178. best_imp[i] = -1;
  6179. }
  6180. switch (tag) {
  6181. case BR_TAG_HEAD:
  6182. case BR_TAG_TITLE:
  6183. case BR_TAG_META:
  6184. case BR_TAG_LINK:
  6185. case BR_TAG_BASE:
  6186. case BR_TAG_SCRIPT:
  6187. case BR_TAG_STYLE:
  6188. out->display = BR_DISPLAY_NONE;
  6189. break;
  6190. case BR_TAG_BODY:
  6191. out->mt = out->mb = out->ml = out->mr = 4;
  6192. break;
  6193. case BR_TAG_P:
  6194. out->mt = out->mb = 8;
  6195. break;
  6196. case BR_TAG_H1:
  6197. out->font_px = 32; out->bold = 1;
  6198. out->mt = 10; out->mb = 6;
  6199. break;
  6200. case BR_TAG_H2:
  6201. out->font_px = 24; out->bold = 1;
  6202. out->mt = 8; out->mb = 4;
  6203. break;
  6204. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  6205. out->bold = 1; out->mt = 6; out->mb = 3;
  6206. if (tag == BR_TAG_H3)
  6207. out->font_px = 19;
  6208. else if (tag == BR_TAG_H4)
  6209. out->font_px = 17;
  6210. else if (tag == BR_TAG_H5)
  6211. out->font_px = 15;
  6212. else
  6213. out->font_px = 13;
  6214. break;
  6215. case BR_TAG_UL: case BR_TAG_OL:
  6216. out->mt = out->mb = 6; out->pl = 20;
  6217. break;
  6218. case BR_TAG_LI:
  6219. out->mt = out->mb = 2;
  6220. out->list_item = 1;
  6221. break;
  6222. case BR_TAG_BLOCKQUOTE:
  6223. out->mt = out->mb = 6; out->ml = out->mr = 16;
  6224. break;
  6225. case BR_TAG_PRE:
  6226. out->mt = out->mb = 8; out->pl = 8;
  6227. out->has_bg = 1; out->bg = theme_panel;
  6228. out->mono = 1;
  6229. break;
  6230. case BR_TAG_CENTER:
  6231. out->align = BR_ALIGN_CENTER;
  6232. break;
  6233. case BR_TAG_A:
  6234. out->color = theme_link; out->underline = 1;
  6235. break;
  6236. case BR_TAG_B: case BR_TAG_STRONG:
  6237. out->bold = 1;
  6238. break;
  6239. case BR_TAG_I: case BR_TAG_EM:
  6240. out->italic = 1;
  6241. break;
  6242. case BR_TAG_CODE:
  6243. out->color = 0x8010;
  6244. out->mono = 1;
  6245. break;
  6246. case BR_TAG_SMALL:
  6247. out->font_px = 13;
  6248. break;
  6249. case BR_TAG_BIG:
  6250. out->font_px = 20;
  6251. break;
  6252. case BR_TAG_TD: case BR_TAG_TH:
  6253. out->pr = 12;
  6254. break;
  6255. case BR_TAG_VIDEO:
  6256. out->display = BR_DISPLAY_BLOCK;
  6257. break;
  6258. default:
  6259. break;
  6260. }
  6261. /* presentational <font color>: author CSS below still wins */
  6262. if (d->nodes[node].color_off) {
  6263. unsigned short c;
  6264. const char *v = attr_ptr(d, d->nodes[node].color_off);
  6265. if (parse_color(v, s_len(v), &c))
  6266. out->color = c;
  6267. }
  6268. rule_collect(d, node);
  6269. for (i = 0; i < nrule_matches; i++) {
  6270. struct br_rule *r = &d->rules[rule_matches[i].idx];
  6271. int spec = rule_matches[i].spec;
  6272. unsigned int k;
  6273. for (k = 0; k < r->nd; k++)
  6274. cascade_decl(out, &r->d[k], spec, best, best_imp);
  6275. }
  6276. if (d->nodes[node].style_off) {
  6277. struct br_decl decls[BR_DECLS];
  6278. unsigned int nd, k;
  6279. const char *v = attr_ptr(d, d->nodes[node].style_off);
  6280. parse_decls(d, v, s_len(v), decls, &nd);
  6281. for (k = 0; k < nd; k++)
  6282. cascade_decl(out, &decls[k], 1000, best, best_imp);
  6283. }
  6284. if (out->font_unit) {
  6285. int px = out->font_raw;
  6286. if (out->font_unit == 2)
  6287. px = parent->font_px * out->font_raw / 100;
  6288. else if (out->font_unit == 3)
  6289. px = root_font_px * out->font_raw / 100;
  6290. else if (out->font_unit == 4)
  6291. px = parent->font_px * out->font_raw / 100;
  6292. if (px < 8)
  6293. px = 8;
  6294. if (px > 72)
  6295. px = 72;
  6296. out->font_px = (unsigned char)px;
  6297. }
  6298. resolve_size_units(out);
  6299. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  6300. out->display = BR_DISPLAY_NONE;
  6301. if (out->vis_hidden || out->opacity_hidden ||
  6302. (out->overflow_hidden && out->height_kind == 1 &&
  6303. out->height == 0))
  6304. out->display = BR_DISPLAY_NONE;
  6305. d->nodes[node].style = *out;
  6306. d->nodes[node].style_valid = 1;
  6307. }
  6308. /* ---- HTML parser ---- */
  6309. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  6310. unsigned int sp, int *pre)
  6311. {
  6312. unsigned int i;
  6313. *pre = 0;
  6314. for (i = 1; i <= sp; i++)
  6315. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  6316. (*pre)++;
  6317. }
  6318. static void implicit_close(struct br_doc *d, unsigned short *stack,
  6319. unsigned int *sp, unsigned int tag)
  6320. {
  6321. while (*sp > 0) {
  6322. unsigned int top = d->nodes[stack[*sp]].tag;
  6323. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  6324. (*sp)--;
  6325. continue;
  6326. }
  6327. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  6328. (*sp)--;
  6329. continue;
  6330. }
  6331. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  6332. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  6333. (*sp)--;
  6334. continue;
  6335. }
  6336. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  6337. (*sp)--;
  6338. continue;
  6339. }
  6340. break;
  6341. }
  6342. }
  6343. static int has_attr_token(const char *s, unsigned int len, const char *name)
  6344. {
  6345. unsigned int i, n = s_len(name);
  6346. for (i = 0; i + n <= len; i++) {
  6347. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  6348. continue;
  6349. if (!ci_eq_n(s + i, name, n))
  6350. continue;
  6351. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  6352. return 1;
  6353. }
  6354. return 0;
  6355. }
  6356. static int stack_has_svg(const struct br_doc *d,
  6357. const unsigned short *stack, unsigned int sp)
  6358. {
  6359. unsigned int k;
  6360. for (k = 0; k <= sp && k < 64; k++)
  6361. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  6362. return 1;
  6363. return 0;
  6364. }
  6365. /* Skip an element subtree by finding its closing tag. Used for SVG
  6366. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  6367. * overwrite the document title. */
  6368. static unsigned int skip_element(const char *html, unsigned int len,
  6369. unsigned int i, const char *name,
  6370. unsigned int n)
  6371. {
  6372. char close[40];
  6373. unsigned int k = 0, p;
  6374. close[k++] = '<';
  6375. close[k++] = '/';
  6376. while (k < sizeof(close) - 1 && n--)
  6377. close[k++] = *name++;
  6378. p = (unsigned int)find_ci(html, len, close, k, i);
  6379. if ((int)p < 0)
  6380. return len;
  6381. p += k;
  6382. while (p < len && html[p] != '>')
  6383. p++;
  6384. if (p < len)
  6385. p++;
  6386. return p;
  6387. }
  6388. /* Store a <script src=...> value in the attribute arena. Kept separate from
  6389. * parse_attrs() so script-only attributes cannot be confused with the
  6390. * value/d attributes that also use src_off on other elements. */
  6391. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  6392. unsigned int len)
  6393. {
  6394. unsigned int i = 0;
  6395. while (i < len) {
  6396. unsigned int ns, ne, vs, ve;
  6397. char quote = 0;
  6398. while (i < len && is_space(s[i]))
  6399. i++;
  6400. if (i >= len || s[i] == '/')
  6401. break;
  6402. ns = i;
  6403. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  6404. i++;
  6405. ne = i;
  6406. while (i < len && is_space(s[i]))
  6407. i++;
  6408. if (i >= len || s[i] != '=')
  6409. continue;
  6410. i++;
  6411. while (i < len && is_space(s[i]))
  6412. i++;
  6413. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6414. quote = s[i++];
  6415. vs = i;
  6416. while (i < len && s[i] != quote)
  6417. i++;
  6418. ve = i;
  6419. if (i < len)
  6420. i++;
  6421. } else {
  6422. vs = i;
  6423. while (i < len && !is_space(s[i]) && s[i] != '>')
  6424. i++;
  6425. ve = i;
  6426. }
  6427. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  6428. return store_attr(d, s + vs, ve - vs, 400);
  6429. }
  6430. return 0;
  6431. }
  6432. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  6433. * application/json, text/template, ...) and non-JS type values. External
  6434. * src scripts are allowed; the browser fetches them separately. */
  6435. static int script_is_javascript(const char *s, unsigned int len)
  6436. {
  6437. unsigned int i = 0;
  6438. while (i < len) {
  6439. unsigned int ns, ne, vs = 0, ve = 0;
  6440. char quote = 0;
  6441. int has_val = 0;
  6442. while (i < len && is_space(s[i]))
  6443. i++;
  6444. if (i >= len)
  6445. break;
  6446. ns = i;
  6447. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  6448. i++;
  6449. ne = i;
  6450. while (i < len && is_space(s[i]))
  6451. i++;
  6452. if (i < len && s[i] == '=') {
  6453. i++;
  6454. while (i < len && is_space(s[i]))
  6455. i++;
  6456. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6457. quote = s[i++];
  6458. vs = i;
  6459. while (i < len && s[i] != quote)
  6460. i++;
  6461. ve = i;
  6462. if (i < len)
  6463. i++;
  6464. } else {
  6465. vs = i;
  6466. while (i < len && !is_space(s[i]) && s[i] != '/')
  6467. i++;
  6468. ve = i;
  6469. }
  6470. has_val = 1;
  6471. } else if (i == ne) {
  6472. i++;
  6473. }
  6474. if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
  6475. unsigned int vn = has_val ? ve - vs : 0;
  6476. if (vn == 0)
  6477. return 1;
  6478. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  6479. (vn == 22 &&
  6480. ci_eq_n(s + vs, "application/javascript", 22)) ||
  6481. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  6482. (vn == 22 &&
  6483. ci_eq_n(s + vs, "application/ecmascript", 22)))
  6484. return 1;
  6485. return 0;
  6486. }
  6487. }
  6488. return 1;
  6489. }
  6490. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  6491. {
  6492. unsigned short stack[64];
  6493. unsigned int sp = 0;
  6494. unsigned int i = 0;
  6495. int pre = 0;
  6496. unsigned int css_len = 0;
  6497. if (!d)
  6498. return -1;
  6499. if (!d->nodes)
  6500. br_doc_init(d);
  6501. doc_reset(d);
  6502. d->script_src = html;
  6503. stack[0] = 0;
  6504. d->nodes[0].type = BR_NODE_ELEM;
  6505. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6506. while (i < len) {
  6507. if (html[i] != '<') {
  6508. unsigned int st = i;
  6509. while (i < len && html[i] != '<')
  6510. i++;
  6511. if (sp <= 63)
  6512. append_text(d, stack[sp], html + st, i - st, pre);
  6513. continue;
  6514. }
  6515. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  6516. html[i + 3] == '-') {
  6517. int p = find_ci(html, len, "-->", 3, i + 4);
  6518. i = p >= 0 ? (unsigned int)p + 3 : len;
  6519. continue;
  6520. }
  6521. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  6522. while (i < len && html[i] != '>')
  6523. i++;
  6524. if (i < len)
  6525. i++;
  6526. continue;
  6527. }
  6528. if (i + 1 < len && html[i + 1] == '/') {
  6529. unsigned int ns = i + 2, ne, tag, k, n;
  6530. char name[32];
  6531. while (ns < len && is_space(html[ns]))
  6532. ns++;
  6533. ne = ns;
  6534. while (ne < len && is_ident(html[ne]))
  6535. ne++;
  6536. if (ne == ns) {
  6537. i++;
  6538. continue;
  6539. }
  6540. n = ne - ns;
  6541. if (n >= sizeof(name))
  6542. n = sizeof(name) - 1;
  6543. for (k = 0; k < n; k++)
  6544. name[k] = html[ns + k];
  6545. name[n] = '\0';
  6546. tag = br_tag_from_name(name);
  6547. i = ne;
  6548. while (i < len && html[i] != '>')
  6549. i++;
  6550. if (i < len)
  6551. i++;
  6552. for (k = sp; k > 0; k--) {
  6553. struct br_node *nd = &d->nodes[stack[k]];
  6554. if (tag == BR_TAG_UNKNOWN) {
  6555. const char *raw = attr_ptr(d, nd->name_off);
  6556. if (nd->tag == BR_TAG_UNKNOWN &&
  6557. ci_eq_n(raw, name, n) &&
  6558. raw[n] == '\0') {
  6559. sp = k - 1;
  6560. break;
  6561. }
  6562. } else if (nd->tag == tag) {
  6563. sp = k - 1;
  6564. break;
  6565. }
  6566. }
  6567. stack_recount_pre(d, stack, sp, &pre);
  6568. continue;
  6569. }
  6570. /* start tag */
  6571. {
  6572. unsigned int ns = i + 1, ne, tag, k, n;
  6573. char name[16];
  6574. unsigned int attr_start, attr_end;
  6575. int selfclose = 0;
  6576. while (ns < len && is_space(html[ns]))
  6577. ns++;
  6578. ne = ns;
  6579. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  6580. ne++;
  6581. if (ne == ns) {
  6582. /* stray '<' */
  6583. append_text(d, stack[sp], html + i, 1, pre);
  6584. i++;
  6585. continue;
  6586. }
  6587. n = ne - ns;
  6588. if (n >= sizeof(name))
  6589. n = sizeof(name) - 1;
  6590. for (k = 0; k < n; k++)
  6591. name[k] = html[ns + k];
  6592. name[n] = '\0';
  6593. tag = br_tag_from_name(name);
  6594. attr_start = ne;
  6595. {
  6596. unsigned int q = ne;
  6597. char quote = 0;
  6598. while (q < len) {
  6599. char c = html[q];
  6600. if (quote) {
  6601. if (c == quote)
  6602. quote = 0;
  6603. } else if (c == '"' || c == '\'') {
  6604. quote = c;
  6605. } else if (c == '>') {
  6606. break;
  6607. }
  6608. q++;
  6609. }
  6610. attr_end = q;
  6611. if (q > attr_start && html[q - 1] == '/')
  6612. selfclose = 1;
  6613. i = q < len ? q + 1 : len;
  6614. }
  6615. if (tag == BR_TAG_SCRIPT) {
  6616. int p = find_ci(html, len, "</script", 8, i);
  6617. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6618. if (script_is_javascript(html + attr_start,
  6619. attr_end - attr_start)) {
  6620. if (d->nscripts < BR_MAX_SCRIPTS) {
  6621. struct br_script *script =
  6622. &d->scripts[d->nscripts++];
  6623. script->off = i;
  6624. script->len = end > i ? end - i : 0;
  6625. script->src_off = script_src_attr(
  6626. d, html + attr_start,
  6627. attr_end - attr_start);
  6628. } else {
  6629. d->scripts_truncated = 1;
  6630. }
  6631. }
  6632. i = p >= 0 ? (unsigned int)p + 8 : len;
  6633. while (i < len && html[i] != '>')
  6634. i++;
  6635. if (i < len)
  6636. i++;
  6637. continue;
  6638. }
  6639. if (stack_has_svg(d, stack, sp) &&
  6640. (tag == BR_TAG_TITLE ||
  6641. (tag == BR_TAG_UNKNOWN &&
  6642. (ci_eq_n(name, "desc", 4) ||
  6643. ci_eq_n(name, "metadata", 8))))) {
  6644. i = skip_element(html, len, i, name, n);
  6645. continue;
  6646. }
  6647. if (tag == BR_TAG_DETAILS && !selfclose &&
  6648. !has_attr_token(html + attr_start,
  6649. attr_end - attr_start, "open")) {
  6650. /* Closed <details> is collapsed (and this browser has
  6651. * no script to open it); skipping the subtree keeps
  6652. * mobile mega-menus out of the node arena. */
  6653. int p = find_ci(html, len, "</details>", 10, i);
  6654. i = p >= 0 ? (unsigned int)p + 10 : len;
  6655. continue;
  6656. }
  6657. if (tag == BR_TAG_STYLE) {
  6658. int p = find_ci(html, len, "</style", 7, i);
  6659. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6660. unsigned int cend = end;
  6661. while (cend > i && is_space(html[cend - 1]))
  6662. cend--;
  6663. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  6664. for (k = i; k < cend; k++)
  6665. css_store[css_len++] = html[k];
  6666. css_store[css_len++] = '\n';
  6667. }
  6668. i = p >= 0 ? (unsigned int)p + 7 : len;
  6669. while (i < len && html[i] != '>')
  6670. i++;
  6671. if (i < len)
  6672. i++;
  6673. continue;
  6674. }
  6675. if (tag == BR_TAG_TITLE) {
  6676. int p = find_ci(html, len, "</title", 7, i);
  6677. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6678. unsigned int tn = 0;
  6679. char tmp[BR_TITLE_MAX];
  6680. tn = decode_text(html + i, end - i, tmp,
  6681. sizeof(tmp), 1);
  6682. (void)tn;
  6683. s_copy(d->title, tmp, sizeof(d->title));
  6684. i = p >= 0 ? (unsigned int)p + 7 : len;
  6685. while (i < len && html[i] != '>')
  6686. i++;
  6687. if (i < len)
  6688. i++;
  6689. continue;
  6690. }
  6691. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  6692. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  6693. unsigned int idx;
  6694. implicit_close(d, stack, &sp, tag);
  6695. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  6696. (unsigned char)tag);
  6697. if (idx)
  6698. parse_attrs(d, &d->nodes[idx],
  6699. html + attr_start,
  6700. attr_end - attr_start);
  6701. continue;
  6702. }
  6703. if (sp >= 63) {
  6704. d->truncated = 1;
  6705. continue;
  6706. }
  6707. implicit_close(d, stack, &sp, tag);
  6708. {
  6709. unsigned int idx = node_append(d, stack[sp],
  6710. BR_NODE_ELEM,
  6711. (unsigned char)tag);
  6712. if (!idx)
  6713. continue;
  6714. parse_attrs(d, &d->nodes[idx],
  6715. html + attr_start,
  6716. attr_end - attr_start);
  6717. if (tag == BR_TAG_UNKNOWN)
  6718. d->nodes[idx].name_off =
  6719. store_attr(d, name, n, 31);
  6720. if (!selfclose) {
  6721. sp++;
  6722. stack[sp] = (unsigned short)idx;
  6723. if (tag == BR_TAG_PRE)
  6724. pre++;
  6725. }
  6726. }
  6727. }
  6728. }
  6729. d->inline_css_len = css_len;
  6730. return 0;
  6731. }
  6732. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  6733. {
  6734. if (!d->nodes)
  6735. br_doc_init(d);
  6736. doc_reset(d);
  6737. d->plain = 1;
  6738. d->nodes[0].type = BR_NODE_ELEM;
  6739. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6740. append_text(d, 0, text, len, 1);
  6741. return 0;
  6742. }
  6743. /* A standalone image response: a synthetic document with one <img>. The
  6744. * caller decodes the already-fetched body into image slot 0 and sets the
  6745. * returned node's img_index. */
  6746. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  6747. {
  6748. unsigned int idx;
  6749. if (!d->nodes)
  6750. br_doc_init(d);
  6751. doc_reset(d);
  6752. d->nodes[0].type = BR_NODE_ELEM;
  6753. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6754. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  6755. if (!idx)
  6756. return 0;
  6757. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  6758. return idx;
  6759. }