browser_html.c 182 KB

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