browser_html.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621
  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. /* Parse the `position / size` part of the background shorthand. The image
  3068. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3069. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3070. const char *v, unsigned int len,
  3071. int important)
  3072. {
  3073. unsigned int i, slash = 0, depth = 0, p, first, count;
  3074. char q = 0;
  3075. signed short nv;
  3076. int vert;
  3077. unsigned char kind;
  3078. for (i = 0; i < len; i++) {
  3079. char c = v[i];
  3080. if (q) {
  3081. if (c == q)
  3082. q = 0;
  3083. continue;
  3084. }
  3085. if (c == '\'' || c == '"') {
  3086. q = c;
  3087. continue;
  3088. }
  3089. if (c == '(') {
  3090. depth++;
  3091. continue;
  3092. }
  3093. if (c == ')') {
  3094. if (depth)
  3095. depth--;
  3096. continue;
  3097. }
  3098. if (c == '/' && !depth) {
  3099. slash = i;
  3100. break;
  3101. }
  3102. }
  3103. if (!slash)
  3104. return;
  3105. /* Up to two position tokens immediately before the slash. */
  3106. p = slash;
  3107. first = slash;
  3108. count = 0;
  3109. while (count < 2) {
  3110. unsigned int e;
  3111. while (p > 0 && is_space(v[p - 1]))
  3112. p--;
  3113. e = p;
  3114. while (p > 0 && !is_space(v[p - 1]))
  3115. p--;
  3116. if (e == p)
  3117. break;
  3118. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3119. bg_number(v + p, e - p, &nv, &kind)) {
  3120. first = p;
  3121. count++;
  3122. } else {
  3123. break;
  3124. }
  3125. }
  3126. if (count)
  3127. add_bg_position(out, nout, v + first, slash - first,
  3128. important);
  3129. /* Up to two size tokens after the slash. */
  3130. p = slash + 1;
  3131. while (p < len && is_space(v[p]))
  3132. p++;
  3133. first = p;
  3134. count = 0;
  3135. while (count < 2) {
  3136. unsigned int ts = p;
  3137. while (p < len && !is_space(v[p]))
  3138. p++;
  3139. if (p == ts)
  3140. break;
  3141. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3142. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3143. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3144. bg_number(v + ts, p - ts, &nv, &kind)) {
  3145. count++;
  3146. while (p < len && is_space(v[p]))
  3147. p++;
  3148. } else {
  3149. break;
  3150. }
  3151. }
  3152. if (count)
  3153. add_bg_size(out, nout, v + first, p - first, important);
  3154. }
  3155. static int parse_fixed_100(const char *s, unsigned int len,
  3156. unsigned int *pos, signed short *out)
  3157. {
  3158. unsigned int i = *pos;
  3159. long whole = 0, frac = 0;
  3160. int neg = 0, digits = 0, fd = 0;
  3161. while (i < len && is_space(s[i]))
  3162. i++;
  3163. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3164. neg = s[i] == '-';
  3165. i++;
  3166. }
  3167. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3168. whole = whole * 10 + (s[i] - '0');
  3169. if (whole > 327)
  3170. whole = 327;
  3171. digits = 1;
  3172. i++;
  3173. }
  3174. if (i < len && s[i] == '.') {
  3175. i++;
  3176. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3177. if (fd < 2) {
  3178. frac = frac * 10 + (s[i] - '0');
  3179. fd++;
  3180. }
  3181. i++;
  3182. }
  3183. }
  3184. if (!digits)
  3185. return 0;
  3186. while (fd++ < 2)
  3187. frac *= 10;
  3188. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3189. *pos = i;
  3190. return 1;
  3191. }
  3192. static int find_fn(const char *v, unsigned int len, const char *name,
  3193. unsigned int *arg)
  3194. {
  3195. unsigned int n = s_len(name), i;
  3196. for (i = 0; i + n <= len; i++) {
  3197. unsigned int j;
  3198. if (!ci_eq_n(v + i, name, n))
  3199. continue;
  3200. j = i + n;
  3201. while (j < len && is_space(v[j]))
  3202. j++;
  3203. if (j < len && v[j] == '(') {
  3204. *arg = j + 1;
  3205. return 1;
  3206. }
  3207. }
  3208. return 0;
  3209. }
  3210. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3211. const char *s, unsigned int len,
  3212. int important)
  3213. {
  3214. unsigned int pos = 0, ts, te;
  3215. signed short val[2] = { 50, 50 };
  3216. unsigned char kind[2] = { 1, 1 };
  3217. int vert[2] = { 0, 0 }, n = 0;
  3218. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3219. signed short v;
  3220. int is_vert;
  3221. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3222. val[n] = v;
  3223. kind[n] = 1;
  3224. vert[n] = is_vert;
  3225. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3226. val[n] = v;
  3227. vert[n] = 0;
  3228. } else {
  3229. n++;
  3230. continue;
  3231. }
  3232. n++;
  3233. }
  3234. if (n == 1) {
  3235. if (vert[0]) {
  3236. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3237. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3238. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3239. important);
  3240. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3241. } else {
  3242. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3243. important);
  3244. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3245. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3246. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3247. }
  3248. return;
  3249. }
  3250. if (n >= 2) {
  3251. int x = vert[0] && !vert[1] ? 1 : 0;
  3252. int y = 1 - x;
  3253. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3254. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3255. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3256. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3257. }
  3258. }
  3259. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3260. const char *v, unsigned int vl, int important)
  3261. {
  3262. unsigned int i;
  3263. for (i = 0; i + 4 <= vl; i++) {
  3264. if (ci_eq_n(v + i, "url(", 4)) {
  3265. unsigned int st = i + 4, en = st;
  3266. int idx;
  3267. while (en < vl && v[en] != ')')
  3268. en++;
  3269. idx = register_bg_url(v + st, en - st);
  3270. if (idx)
  3271. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3272. important);
  3273. return;
  3274. }
  3275. }
  3276. }
  3277. static int parse_len_token(const char *s, unsigned int len, int *out)
  3278. {
  3279. unsigned int pos = 0;
  3280. int v;
  3281. if (!len)
  3282. return 0;
  3283. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3284. return 0;
  3285. v = parse_len(s, len, &pos);
  3286. if (pos == 0)
  3287. return 0;
  3288. *out = v;
  3289. return 1;
  3290. }
  3291. static void expand_four(int v[4], int n)
  3292. {
  3293. if (n == 1) {
  3294. v[1] = v[2] = v[3] = v[0];
  3295. } else if (n == 2) {
  3296. v[2] = v[0];
  3297. v[3] = v[1];
  3298. } else if (n == 3) {
  3299. v[3] = v[1];
  3300. }
  3301. }
  3302. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3303. * the input is copied verbatim when it contains no var(). */
  3304. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3305. unsigned int len, char *out,
  3306. unsigned int cap, int depth)
  3307. {
  3308. unsigned int i = 0, n = 0;
  3309. if (depth > 8)
  3310. return 0;
  3311. while (i < len && n + 1 < cap) {
  3312. unsigned int p, inner, close, comma, nlen, fstart;
  3313. char quote;
  3314. int parens;
  3315. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3316. out[n++] = s[i++];
  3317. continue;
  3318. }
  3319. p = i + 4;
  3320. quote = 0;
  3321. parens = 1;
  3322. while (p < len && parens) {
  3323. char c = s[p];
  3324. if (quote) {
  3325. if (c == quote)
  3326. quote = 0;
  3327. else if (c == '\\' && p + 1 < len)
  3328. p++;
  3329. } else if (c == '"' || c == '\'') {
  3330. quote = c;
  3331. } else if (c == '(') {
  3332. parens++;
  3333. } else if (c == ')') {
  3334. parens--;
  3335. }
  3336. p++;
  3337. }
  3338. if (parens) {
  3339. out[n++] = s[i++];
  3340. continue;
  3341. }
  3342. close = p - 1;
  3343. inner = i + 4;
  3344. while (inner < close && is_space(s[inner]))
  3345. inner++;
  3346. comma = inner;
  3347. parens = 0;
  3348. quote = 0;
  3349. while (comma < close) {
  3350. char c = s[comma];
  3351. if (quote) {
  3352. if (c == quote)
  3353. quote = 0;
  3354. } else if (c == '"' || c == '\'') {
  3355. quote = c;
  3356. } else if (c == '(') {
  3357. parens++;
  3358. } else if (c == ')' && parens > 0) {
  3359. parens--;
  3360. } else if (c == ',' && parens == 0) {
  3361. break;
  3362. }
  3363. comma++;
  3364. }
  3365. nlen = comma - inner;
  3366. while (nlen && is_space(s[inner + nlen - 1]))
  3367. nlen--;
  3368. fstart = comma < close ? comma + 1 : close;
  3369. while (fstart < close && is_space(s[fstart]))
  3370. fstart++;
  3371. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3372. unsigned int vi = css_var_find(d, s + inner, nlen);
  3373. if (vi < BR_MAX_VARS) {
  3374. const char *v = attr_ptr(d, d->var_val[vi]);
  3375. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3376. out + n,
  3377. cap - n, depth + 1);
  3378. n += vn;
  3379. } else if (fstart < close) {
  3380. unsigned int vn = css_expand_vars(d, s + fstart,
  3381. close - fstart,
  3382. out + n, cap - n,
  3383. depth + 1);
  3384. n += vn;
  3385. }
  3386. } else if (fstart < close) {
  3387. unsigned int vn = css_expand_vars(d, s + fstart,
  3388. close - fstart,
  3389. out + n, cap - n,
  3390. depth + 1);
  3391. n += vn;
  3392. }
  3393. i = p;
  3394. }
  3395. out[n] = '\0';
  3396. return n;
  3397. }
  3398. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3399. unsigned int i)
  3400. {
  3401. while (i < len && (is_space(s[i]) || s[i] == ','))
  3402. i++;
  3403. return i;
  3404. }
  3405. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3406. * argument). Returns 1 on success. */
  3407. static int parse_grid_track(const char *s, unsigned int len,
  3408. unsigned int *pp, unsigned short *val,
  3409. unsigned char *kind)
  3410. {
  3411. unsigned int i = grid_skip_sep(s, len, *pp);
  3412. unsigned int whole = 0, frac = 0, scale = 1;
  3413. int any = 0;
  3414. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3415. unsigned int j = i + 7;
  3416. while (j < len && s[j] != ',')
  3417. j++;
  3418. if (j >= len)
  3419. return 0;
  3420. i = j + 1;
  3421. while (i < len && is_space(s[i]))
  3422. i++;
  3423. }
  3424. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3425. int depth = 1;
  3426. i += 7;
  3427. while (i < len && depth) {
  3428. if (s[i] == '(')
  3429. depth++;
  3430. else if (s[i] == ')')
  3431. depth--;
  3432. i++;
  3433. }
  3434. *pp = i;
  3435. return 0;
  3436. }
  3437. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3438. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3439. i++;
  3440. any = 1;
  3441. }
  3442. if (i < len && s[i] == '.') {
  3443. i++;
  3444. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3445. if (scale < 1000) {
  3446. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3447. scale *= 10;
  3448. }
  3449. i++;
  3450. any = 1;
  3451. }
  3452. }
  3453. if (!any) {
  3454. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3455. *val = 1;
  3456. *kind = 3;
  3457. *pp = i + 4;
  3458. return 1;
  3459. }
  3460. return 0;
  3461. }
  3462. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3463. *val = (unsigned short)(whole ? whole : 1);
  3464. *kind = 3;
  3465. i += 2;
  3466. } else if (i < len && s[i] == '%') {
  3467. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3468. *kind = 2;
  3469. i++;
  3470. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3471. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3472. *kind = 1;
  3473. i += 3;
  3474. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3475. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3476. *kind = 1;
  3477. i += 2;
  3478. } else {
  3479. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3480. *kind = 1;
  3481. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3482. i += 2;
  3483. }
  3484. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3485. i++;
  3486. *pp = i;
  3487. return 1;
  3488. }
  3489. static int parse_grid_cols(const char *s, unsigned int len,
  3490. unsigned short val[2], unsigned char kind[2])
  3491. {
  3492. unsigned int pos = 0;
  3493. int n = 0;
  3494. while (n < 2) {
  3495. unsigned int before = pos;
  3496. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3497. break;
  3498. n++;
  3499. if (pos <= before)
  3500. break;
  3501. while (pos < len && is_space(s[pos]))
  3502. pos++;
  3503. if (pos >= len)
  3504. break;
  3505. }
  3506. return n;
  3507. }
  3508. static int parse_grid_slot(const char *s, unsigned int len)
  3509. {
  3510. if (contains_ci_len(s, len, "footer") ||
  3511. contains_ci_len(s, len, "header") ||
  3512. contains_ci_len(s, len, "sitenotice") ||
  3513. contains_ci_len(s, len, "navbar"))
  3514. return 1;
  3515. if (contains_ci_len(s, len, "columnstart") ||
  3516. contains_ci_len(s, len, "sidebar") ||
  3517. contains_ci_len(s, len, "left") ||
  3518. contains_ci_len(s, len, "start"))
  3519. return 2;
  3520. if (contains_ci_len(s, len, "pagecontent") ||
  3521. contains_ci_len(s, len, "content") ||
  3522. contains_ci_len(s, len, "main") ||
  3523. contains_ci_len(s, len, "right") ||
  3524. contains_ci_len(s, len, "end"))
  3525. return 3;
  3526. return 0;
  3527. }
  3528. static int css_has_var(const char *s, unsigned int len)
  3529. {
  3530. unsigned int i;
  3531. for (i = 0; i + 4 <= len; i++)
  3532. if ((s[i] == 'v' || s[i] == 'V') &&
  3533. ci_eq_n(s + i, "var(", 4))
  3534. return 1;
  3535. return 0;
  3536. }
  3537. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3538. unsigned int i)
  3539. {
  3540. i += 2;
  3541. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3542. i++;
  3543. if (i + 1 < len)
  3544. i += 2;
  3545. else
  3546. i = len;
  3547. return i;
  3548. }
  3549. static unsigned int css_skip_string(const char *s, unsigned int len,
  3550. unsigned int i)
  3551. {
  3552. char q = s[i++];
  3553. while (i < len && s[i] != q) {
  3554. if (s[i] == '\\' && i + 1 < len)
  3555. i++;
  3556. i++;
  3557. }
  3558. if (i < len)
  3559. i++;
  3560. return i;
  3561. }
  3562. /* Find the '}' that closes a declaration block. i points just past the
  3563. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3564. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3565. * selectors such as "Highlight-Stories {" without a closing brace). */
  3566. static unsigned int css_block_end(const char *s, unsigned int len,
  3567. unsigned int i)
  3568. {
  3569. int depth = 0;
  3570. while (i < len) {
  3571. char c = s[i];
  3572. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3573. i = css_skip_comment(s, len, i);
  3574. continue;
  3575. }
  3576. if (c == '"' || c == '\'') {
  3577. i = css_skip_string(s, len, i);
  3578. continue;
  3579. }
  3580. if (c == '{') {
  3581. depth++;
  3582. } else if (c == '}') {
  3583. if (depth == 0)
  3584. return i;
  3585. depth--;
  3586. }
  3587. i++;
  3588. }
  3589. return len;
  3590. }
  3591. /* Skip a block whose opening '{' has already been consumed. */
  3592. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3593. unsigned int i)
  3594. {
  3595. int depth = 1;
  3596. while (i < len && depth) {
  3597. char c = s[i];
  3598. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3599. i = css_skip_comment(s, len, i);
  3600. continue;
  3601. }
  3602. if (c == '"' || c == '\'') {
  3603. i = css_skip_string(s, len, i);
  3604. continue;
  3605. }
  3606. if (c == '{')
  3607. depth++;
  3608. else if (c == '}')
  3609. depth--;
  3610. i++;
  3611. }
  3612. return i;
  3613. }
  3614. /* ---- @font-face ------------------------------------------------------- */
  3615. static void font_trim(const char **sp, unsigned int *len)
  3616. {
  3617. const char *s = *sp;
  3618. unsigned int n = *len;
  3619. while (n && is_space(*s)) {
  3620. s++;
  3621. n--;
  3622. }
  3623. while (n && is_space(s[n - 1]))
  3624. n--;
  3625. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3626. s++;
  3627. n -= 2;
  3628. }
  3629. *sp = s;
  3630. *len = n;
  3631. }
  3632. static void font_add(const char *family, unsigned int flen,
  3633. const char *url, unsigned int ulen)
  3634. {
  3635. unsigned int i, n = 0, k;
  3636. font_trim(&family, &flen);
  3637. font_trim(&url, &ulen);
  3638. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3639. return;
  3640. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3641. struct br_font_def *f = &font_defs[i];
  3642. if (s_len(f->family) != flen)
  3643. continue;
  3644. for (k = 0; k < flen; k++) {
  3645. char a = f->family[k], b = family[k];
  3646. if (a >= 'A' && a <= 'Z')
  3647. a = (char)(a - 'A' + 'a');
  3648. if (b >= 'A' && b <= 'Z')
  3649. b = (char)(b - 'A' + 'a');
  3650. if (a != b)
  3651. break;
  3652. }
  3653. if (k != flen)
  3654. continue;
  3655. n = 0;
  3656. while (url[n] && n + 1 < sizeof(f->url)) {
  3657. f->url[n] = url[n];
  3658. n++;
  3659. }
  3660. f->url[n] = '\0';
  3661. n = 0;
  3662. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3663. f->base[n] = font_base[n];
  3664. n++;
  3665. }
  3666. f->base[n] = '\0';
  3667. return;
  3668. }
  3669. if (font_face_count >= BR_MAX_FACES)
  3670. return;
  3671. {
  3672. struct br_font_def *f = &font_defs[font_face_count];
  3673. for (n = 0; n < flen; n++)
  3674. f->family[n] = family[n];
  3675. f->family[n] = '\0';
  3676. for (n = 0; n < ulen; n++)
  3677. f->url[n] = url[n];
  3678. f->url[n] = '\0';
  3679. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  3680. f->base[n] = font_base[n];
  3681. f->base[n] = '\0';
  3682. f->used = 1;
  3683. font_face_count++;
  3684. }
  3685. }
  3686. /* Look up the first name in a font-family list that has an @font-face. */
  3687. static int font_family_id(const char *s, unsigned int len)
  3688. {
  3689. unsigned int i = 0;
  3690. while (i < len) {
  3691. unsigned int start, end, q = 0;
  3692. while (i < len && (is_space(s[i]) || s[i] == ','))
  3693. i++;
  3694. if (i >= len)
  3695. break;
  3696. if (s[i] == '"' || s[i] == '\'') {
  3697. char quote = s[i++];
  3698. start = i;
  3699. while (i < len && s[i] != quote)
  3700. i++;
  3701. end = i;
  3702. if (i < len)
  3703. i++;
  3704. } else {
  3705. start = i;
  3706. while (i < len && s[i] != ',')
  3707. i++;
  3708. end = i;
  3709. while (end > start && is_space(s[end - 1]))
  3710. end--;
  3711. }
  3712. if (end > start) {
  3713. for (q = 0; q < (unsigned int)font_face_count; q++) {
  3714. const char *f = font_defs[q].family;
  3715. unsigned int fl = s_len(f), k;
  3716. if (fl != end - start)
  3717. continue;
  3718. for (k = 0; k < fl; k++) {
  3719. char a = f[k], b = s[start + k];
  3720. if (a >= 'A' && a <= 'Z')
  3721. a = (char)(a - 'A' + 'a');
  3722. if (b >= 'A' && b <= 'Z')
  3723. b = (char)(b - 'A' + 'a');
  3724. if (a != b)
  3725. break;
  3726. }
  3727. if (k == fl)
  3728. return (int)q + 1;
  3729. }
  3730. }
  3731. }
  3732. return 0;
  3733. }
  3734. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  3735. * at-rules, so faces must be registered before any font-family declaration
  3736. * in the same sheet is resolved. */
  3737. static void scan_font_faces(const char *s, unsigned int len)
  3738. {
  3739. unsigned int i = 0;
  3740. while (i + 10 <= len) {
  3741. unsigned int j, end;
  3742. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  3743. unsigned int flen = 0, ulen = 0, p;
  3744. int have_family = 0, have_url = 0;
  3745. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  3746. i++;
  3747. continue;
  3748. }
  3749. j = i + 10;
  3750. while (j < len && s[j] != '{' && s[j] != ';')
  3751. j++;
  3752. if (j >= len || s[j] == ';') {
  3753. i = j + 1;
  3754. continue;
  3755. }
  3756. end = css_skip_nested(s, len, j + 1);
  3757. p = j + 1;
  3758. while (p < end) {
  3759. unsigned int ds, de, vs, ve;
  3760. while (p < end && (is_space(s[p]) || s[p] == ';'))
  3761. p++;
  3762. if (p >= end)
  3763. break;
  3764. ds = p;
  3765. while (p < end && s[p] != ':' && s[p] != ';')
  3766. p++;
  3767. de = p;
  3768. if (p >= end || s[p] != ':') {
  3769. while (p < end && s[p] != ';')
  3770. p++;
  3771. continue;
  3772. }
  3773. p++;
  3774. vs = p;
  3775. while (p < end && s[p] != ';')
  3776. p++;
  3777. ve = p;
  3778. while (ds < de && is_space(s[ds]))
  3779. ds++;
  3780. while (de > ds && is_space(s[de - 1]))
  3781. de--;
  3782. while (vs < ve && is_space(s[vs]))
  3783. vs++;
  3784. while (ve > vs && is_space(s[ve - 1]))
  3785. ve--;
  3786. if (de - ds == 11 &&
  3787. ci_eq_n(s + ds, "font-family", 11)) {
  3788. const char *fp = s + vs;
  3789. unsigned int fn = ve - vs;
  3790. font_trim(&fp, &fn);
  3791. if (fn && fn < sizeof(family)) {
  3792. unsigned int k;
  3793. for (k = 0; k < fn; k++)
  3794. family[k] = fp[k];
  3795. family[fn] = '\0';
  3796. flen = fn;
  3797. have_family = 1;
  3798. }
  3799. } else if (de - ds == 3 &&
  3800. ci_eq_n(s + ds, "src", 3)) {
  3801. unsigned int q = vs;
  3802. while (q + 4 <= ve) {
  3803. unsigned int us, ue;
  3804. if (!ci_eq_n(s + q, "url(", 4)) {
  3805. q++;
  3806. continue;
  3807. }
  3808. q += 4;
  3809. us = q;
  3810. while (q < ve && s[q] != ')')
  3811. q++;
  3812. ue = q;
  3813. {
  3814. const char *up = s + us;
  3815. unsigned int un = ue - us;
  3816. font_trim(&up, &un);
  3817. if (un && un < sizeof(url) &&
  3818. !contains_ci_len(up, un,
  3819. "woff")) {
  3820. unsigned int k;
  3821. for (k = 0; k < un; k++)
  3822. url[k] = up[k];
  3823. url[un] = '\0';
  3824. ulen = un;
  3825. have_url = 1;
  3826. break;
  3827. }
  3828. }
  3829. }
  3830. }
  3831. }
  3832. if (have_family && have_url)
  3833. font_add(family, flen, url, ulen);
  3834. i = end;
  3835. }
  3836. }
  3837. /* One border-radius length. Unlike parse_size(), percentages keep their
  3838. * kind: the drawer scales them to the box, not the viewport. */
  3839. static int parse_radius_value(const char *s, unsigned int len,
  3840. unsigned int *pos, unsigned short *val,
  3841. unsigned char *kind)
  3842. {
  3843. unsigned int i = *pos;
  3844. unsigned long v = 0;
  3845. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  3846. while (i < len && is_space(s[i]))
  3847. i++;
  3848. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3849. v = v * 10 + (unsigned int)(s[i] - '0');
  3850. digits = 1;
  3851. i++;
  3852. }
  3853. if (i < len && s[i] == '.') {
  3854. i++;
  3855. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3856. fdigits = 1;
  3857. if (frac < 2) {
  3858. v = v * 10 + (unsigned int)(s[i] - '0');
  3859. frac++;
  3860. }
  3861. i++;
  3862. }
  3863. }
  3864. while (frac < 2) {
  3865. v *= 10;
  3866. frac++;
  3867. }
  3868. while (i < len && is_space(s[i]))
  3869. i++;
  3870. if (i < len && s[i] == '%') {
  3871. pct = 1;
  3872. i++;
  3873. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  3874. s[i + 2] == 'm') {
  3875. rem = 1;
  3876. i += 3;
  3877. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  3878. rem = 2;
  3879. i += 2;
  3880. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  3881. i += 2;
  3882. } else if (i < len && is_ident(s[i])) {
  3883. return 0;
  3884. }
  3885. if (!digits && !fdigits)
  3886. return 0;
  3887. if (rem == 1)
  3888. v *= root_font_px;
  3889. else if (rem == 2)
  3890. v *= root_font_px;
  3891. v /= 100;
  3892. if (v > 2048)
  3893. v = 2048;
  3894. *val = (unsigned short)v;
  3895. *kind = pct ? 2 : 1;
  3896. *pos = i;
  3897. return 1;
  3898. }
  3899. /* font: [style] [variant] [weight] size[/line-height] family */
  3900. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  3901. const char *v, unsigned int len, int important)
  3902. {
  3903. unsigned int pos = 0, ts, te;
  3904. unsigned short size_raw = 0;
  3905. unsigned char size_unit = 0;
  3906. int have_size = 0;
  3907. int bold = 0, italic = 0;
  3908. int lh_px = 0, lh_pct = 0, mono = 0;
  3909. while (next_token(v, len, &pos, &ts, &te)) {
  3910. unsigned int tl = te - ts;
  3911. unsigned int slash = 0;
  3912. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  3913. bold = 1;
  3914. continue;
  3915. }
  3916. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  3917. continue;
  3918. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  3919. italic = 1;
  3920. continue;
  3921. }
  3922. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  3923. italic = 1;
  3924. continue;
  3925. }
  3926. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  3927. continue;
  3928. for (slash = 0; slash < tl; slash++)
  3929. if (v[ts + slash] == '/')
  3930. break;
  3931. if (slash == tl) {
  3932. unsigned int q;
  3933. int weight = 0;
  3934. for (q = 0; q < tl; q++) {
  3935. if (v[ts + q] < '0' || v[ts + q] > '9')
  3936. break;
  3937. weight = weight * 10 + (v[ts + q] - '0');
  3938. }
  3939. if (q == tl && weight >= 100 && weight <= 900) {
  3940. bold = weight >= 500;
  3941. continue;
  3942. }
  3943. }
  3944. {
  3945. unsigned short raw;
  3946. unsigned char unit;
  3947. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  3948. &raw, &unit))
  3949. continue;
  3950. size_raw = raw;
  3951. size_unit = unit;
  3952. }
  3953. have_size = 1;
  3954. if (slash < tl) {
  3955. unsigned int lstart = ts + slash + 1;
  3956. unsigned short lhp = 0;
  3957. unsigned char lhpct = 0;
  3958. if (!parse_line_height(v + lstart, te - lstart,
  3959. &lhp, &lhpct)) {
  3960. lh_px = 0;
  3961. lh_pct = 0;
  3962. } else {
  3963. lh_px = lhp;
  3964. lh_pct = lhpct;
  3965. }
  3966. }
  3967. }
  3968. if (!have_size)
  3969. return;
  3970. mono = contains_ci_len(v, len, "mono");
  3971. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  3972. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  3973. decl_add(out, nout, PROP_BOLD, bold, important);
  3974. decl_add(out, nout, PROP_ITALIC, italic, important);
  3975. decl_add(out, nout, PROP_MONO, mono, important);
  3976. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  3977. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  3978. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  3979. }
  3980. static void parse_decls(struct br_doc *d, const char *s,
  3981. unsigned int len, struct br_decl *out,
  3982. unsigned int *nout)
  3983. {
  3984. static char expanded[4096];
  3985. if (css_has_var(s, len)) {
  3986. unsigned int elen = css_expand_vars(d, s, len, expanded,
  3987. sizeof(expanded), 0);
  3988. if (elen) {
  3989. s = expanded;
  3990. len = elen;
  3991. }
  3992. }
  3993. unsigned int i = 0;
  3994. *nout = 0;
  3995. while (i < len && *nout < BR_DECLS) {
  3996. unsigned int pstart, pend, vstart, vend, k;
  3997. char prop[24];
  3998. int important = 0;
  3999. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4000. i++;
  4001. if (i >= len)
  4002. break;
  4003. /* A nested at-rule (@media/@supports) inside the declaration
  4004. * block carries its own selector context; skip it rather than
  4005. * reading its prelude as a declaration. */
  4006. if (s[i] == '@') {
  4007. while (i < len && s[i] != '{' && s[i] != ';')
  4008. i++;
  4009. if (i < len && s[i] == '{')
  4010. i = css_skip_nested(s, len, i + 1);
  4011. else if (i < len)
  4012. i++;
  4013. continue;
  4014. }
  4015. pstart = i;
  4016. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4017. i++;
  4018. if (i < len && s[i] == '{') {
  4019. /* Nested style rule: skip its block. */
  4020. i = css_skip_nested(s, len, i + 1);
  4021. continue;
  4022. }
  4023. pend = i;
  4024. if (i >= len || s[i] != ':')
  4025. continue;
  4026. i++;
  4027. vstart = i;
  4028. while (i < len && s[i] != ';')
  4029. i++;
  4030. vend = i;
  4031. while (pstart < pend && is_space(s[pstart]))
  4032. pstart++;
  4033. while (pend > pstart && is_space(s[pend - 1]))
  4034. pend--;
  4035. while (vstart < vend && is_space(s[vstart]))
  4036. vstart++;
  4037. while (vend > vstart && is_space(s[vend - 1]))
  4038. vend--;
  4039. if (vend - vstart >= 10 &&
  4040. ci_eq_n(s + vend - 10, "!important", 10)) {
  4041. important = 1;
  4042. vend -= 10;
  4043. while (vend > vstart && is_space(s[vend - 1]))
  4044. vend--;
  4045. }
  4046. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4047. char c = s[pstart + k];
  4048. if (c >= 'A' && c <= 'Z')
  4049. c = (char)(c - 'A' + 'a');
  4050. prop[k] = c;
  4051. }
  4052. prop[k] = '\0';
  4053. if (str_eq(prop, "color")) {
  4054. unsigned short c;
  4055. if (parse_color(s + vstart, vend - vstart, &c))
  4056. decl_add(out, nout, PROP_COLOR, c, important);
  4057. } else if (str_eq(prop, "fill")) {
  4058. unsigned short c;
  4059. if (parse_color(s + vstart, vend - vstart, &c)) {
  4060. decl_add(out, nout, PROP_BG, c, important);
  4061. decl_add(out, nout, PROP_COLOR, c, important);
  4062. }
  4063. } else if (str_eq(prop, "stroke")) {
  4064. unsigned short c;
  4065. if (parse_color(s + vstart, vend - vstart, &c)) {
  4066. decl_add(out, nout, PROP_BORDER_COLOR, c,
  4067. important);
  4068. decl_add(out, nout, PROP_BORDER_W, 1, important);
  4069. }
  4070. } else if (str_eq(prop, "stroke-width")) {
  4071. unsigned int j = vstart;
  4072. int sw = 0;
  4073. while (j < vend && s[j] >= '0' && s[j] <= '9') {
  4074. sw = sw * 10 + (s[j] - '0');
  4075. j++;
  4076. }
  4077. if (sw < 1)
  4078. sw = 1;
  4079. if (sw > 3)
  4080. sw = 3;
  4081. decl_add(out, nout, PROP_BORDER_W, sw, important);
  4082. } else if (str_eq(prop, "text-anchor")) {
  4083. int a = BR_ALIGN_LEFT;
  4084. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4085. a = BR_ALIGN_CENTER;
  4086. else if (contains_ci_len(s + vstart, vend - vstart,
  4087. "end"))
  4088. a = BR_ALIGN_RIGHT;
  4089. decl_add(out, nout, PROP_ALIGN, a, important);
  4090. } else if (str_eq(prop, "background-image")) {
  4091. add_bg_image_decl(out, nout, s + vstart,
  4092. vend - vstart, important);
  4093. } else if (str_eq(prop, "background-repeat")) {
  4094. int r = 0;
  4095. if (contains_ci_len(s + vstart, vend - vstart,
  4096. "no-repeat"))
  4097. r = 1;
  4098. else if (contains_ci_len(s + vstart, vend - vstart,
  4099. "repeat-x"))
  4100. r = 2;
  4101. else if (contains_ci_len(s + vstart, vend - vstart,
  4102. "repeat-y"))
  4103. r = 3;
  4104. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  4105. } else if (str_eq(prop, "background-position")) {
  4106. add_bg_position(out, nout, s + vstart, vend - vstart,
  4107. important);
  4108. } else if (str_eq(prop, "background-size")) {
  4109. add_bg_size(out, nout, s + vstart, vend - vstart,
  4110. important);
  4111. } else if (str_eq(prop, "background-color") ||
  4112. str_eq(prop, "background")) {
  4113. unsigned short c;
  4114. int is_bg = str_eq(prop, "background");
  4115. if (is_bg) {
  4116. add_bg_image_decl(out, nout, s + vstart,
  4117. vend - vstart, important);
  4118. add_bg_shorthand_extra(out, nout,
  4119. s + vstart,
  4120. vend - vstart,
  4121. important);
  4122. if (contains_ci_len(s + vstart, vend - vstart,
  4123. "no-repeat"))
  4124. decl_add(out, nout, PROP_BG_REPEAT, 1,
  4125. important);
  4126. else if (contains_ci_len(s + vstart,
  4127. vend - vstart,
  4128. "repeat-x"))
  4129. decl_add(out, nout, PROP_BG_REPEAT, 2,
  4130. important);
  4131. else if (contains_ci_len(s + vstart,
  4132. vend - vstart,
  4133. "repeat-y"))
  4134. decl_add(out, nout, PROP_BG_REPEAT, 3,
  4135. important);
  4136. }
  4137. if (parse_color(s + vstart, vend - vstart, &c))
  4138. decl_add(out, nout, PROP_BG, c, important);
  4139. else if (is_bg &&
  4140. find_color_last(s + vstart, vend - vstart, &c))
  4141. decl_add(out, nout, PROP_BG, c, important);
  4142. } else if (str_eq(prop, "font-size")) {
  4143. unsigned short raw;
  4144. unsigned char unit;
  4145. if (parse_font_size(s + vstart, vend - vstart,
  4146. &raw, &unit)) {
  4147. decl_add(out, nout, PROP_FONT_PX, raw,
  4148. important);
  4149. decl_add(out, nout, PROP_FONT_UNIT, unit,
  4150. important);
  4151. }
  4152. } else if (str_eq(prop, "font-weight")) {
  4153. int bold = 0;
  4154. unsigned int j;
  4155. if (vend - vstart >= 4 &&
  4156. ci_eq_n(s + vstart, "bold", 4))
  4157. bold = 1;
  4158. else {
  4159. int w = 0;
  4160. for (j = vstart; j < vend; j++)
  4161. if (s[j] >= '0' && s[j] <= '9')
  4162. w = w * 10 + (s[j] - '0');
  4163. if (w >= 500)
  4164. bold = 1;
  4165. }
  4166. decl_add(out, nout, PROP_BOLD, bold, important);
  4167. } else if (str_eq(prop, "font-style")) {
  4168. int italic = (vend - vstart >= 6 &&
  4169. ci_eq_n(s + vstart, "italic", 6));
  4170. decl_add(out, nout, PROP_ITALIC, italic, important);
  4171. } else if (str_eq(prop, "font-family")) {
  4172. unsigned int j;
  4173. int mono = 0;
  4174. int face = font_family_id(s + vstart, vend - vstart);
  4175. for (j = vstart; j + 4 <= vend; j++)
  4176. if (ci_eq_n(s + j, "mono", 4))
  4177. mono = 1;
  4178. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4179. decl_add(out, nout, PROP_MONO, mono, important);
  4180. } else if (str_eq(prop, "font")) {
  4181. add_font_shorthand(out, nout, s + vstart,
  4182. vend - vstart, important);
  4183. } else if (str_eq(prop, "text-align")) {
  4184. int a = BR_ALIGN_LEFT;
  4185. if (vend - vstart >= 6 &&
  4186. ci_eq_n(s + vstart, "center", 6))
  4187. a = BR_ALIGN_CENTER;
  4188. else if (vend - vstart >= 5 &&
  4189. ci_eq_n(s + vstart, "right", 5))
  4190. a = BR_ALIGN_RIGHT;
  4191. decl_add(out, nout, PROP_ALIGN, a, important);
  4192. } else if (str_eq(prop, "text-decoration")) {
  4193. int u = 0, stv = 0, ov = 0;
  4194. if (contains_ci_len(s + vstart, vend - vstart,
  4195. "underline"))
  4196. u = 1;
  4197. if (contains_ci_len(s + vstart, vend - vstart,
  4198. "line-through"))
  4199. stv = 1;
  4200. if (contains_ci_len(s + vstart, vend - vstart,
  4201. "overline"))
  4202. ov = 1;
  4203. decl_add(out, nout, PROP_UNDERLINE, u, important);
  4204. decl_add(out, nout, PROP_STRIKE, stv, important);
  4205. decl_add(out, nout, PROP_OVERLINE, ov, important);
  4206. } else if (str_eq(prop, "text-transform")) {
  4207. int u = (vend - vstart >= 9 &&
  4208. ci_eq_n(s + vstart, "uppercase", 9));
  4209. decl_add(out, nout, PROP_UPPER, u, important);
  4210. } else if (str_eq(prop, "white-space")) {
  4211. int nw = contains_ci_len(s + vstart, vend - vstart,
  4212. "nowrap") ||
  4213. contains_ci_len(s + vstart, vend - vstart,
  4214. "pre");
  4215. decl_add(out, nout, PROP_NOWRAP, nw, important);
  4216. } else if (str_eq(prop, "display")) {
  4217. decl_add(out, nout, PROP_DISPLAY,
  4218. parse_display(s + vstart, vend - vstart),
  4219. important);
  4220. } else if (str_eq(prop, "visibility")) {
  4221. int h = contains_ci_len(s + vstart, vend - vstart,
  4222. "hidden") ||
  4223. contains_ci_len(s + vstart, vend - vstart,
  4224. "collapse");
  4225. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  4226. } else if (str_eq(prop, "opacity")) {
  4227. unsigned int q;
  4228. int h = 1, any = 0;
  4229. for (q = vstart; q < vend; q++) {
  4230. if (s[q] >= '1' && s[q] <= '9') {
  4231. h = 0;
  4232. break;
  4233. }
  4234. if (s[q] >= '0' && s[q] <= '9')
  4235. any = 1;
  4236. }
  4237. if (!any || contains_ci_len(s + vstart, vend - vstart,
  4238. "inherit"))
  4239. h = 0;
  4240. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  4241. } else if (str_eq(prop, "overflow") ||
  4242. str_eq(prop, "overflow-x") ||
  4243. str_eq(prop, "overflow-y")) {
  4244. int h = contains_ci_len(s + vstart, vend - vstart,
  4245. "hidden") ||
  4246. contains_ci_len(s + vstart, vend - vstart,
  4247. "clip");
  4248. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  4249. } else if (str_eq(prop, "float")) {
  4250. int f = 0;
  4251. if (vend - vstart >= 4 &&
  4252. ci_eq_n(s + vstart, "left", 4))
  4253. f = 1;
  4254. else if (vend - vstart >= 5 &&
  4255. ci_eq_n(s + vstart, "right", 5))
  4256. f = 2;
  4257. decl_add(out, nout, PROP_FLOAT, f, important);
  4258. } else if (str_eq(prop, "clear")) {
  4259. int c = 0;
  4260. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  4261. c = 3;
  4262. else if (contains_ci_len(s + vstart, vend - vstart,
  4263. "left"))
  4264. c = 1;
  4265. else if (contains_ci_len(s + vstart, vend - vstart,
  4266. "right"))
  4267. c = 2;
  4268. decl_add(out, nout, PROP_CLEAR, c, important);
  4269. } else if (str_eq(prop, "flex-grow") ||
  4270. str_eq(prop, "flex")) {
  4271. unsigned int q = vstart;
  4272. int g = 0;
  4273. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  4274. q++;
  4275. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4276. g = g * 10 + (s[q] - '0');
  4277. q++;
  4278. }
  4279. if (g > 100)
  4280. g = 100;
  4281. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  4282. } else if (str_eq(prop, "flex-direction")) {
  4283. decl_add(out, nout, PROP_FLEX_COL,
  4284. contains_ci_len(s + vstart, vend - vstart,
  4285. "column"), important);
  4286. } else if (str_eq(prop, "flex-wrap")) {
  4287. int wrap = 0;
  4288. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  4289. wrap = 2;
  4290. else if (contains_ci_len(s + vstart, vend - vstart,
  4291. "wrap"))
  4292. wrap = 1;
  4293. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  4294. } else if (str_eq(prop, "justify-content")) {
  4295. int j = 0;
  4296. if (contains_ci_len(s + vstart, vend - vstart,
  4297. "space-between"))
  4298. j = 3;
  4299. else if (contains_ci_len(s + vstart, vend - vstart,
  4300. "space-around"))
  4301. j = 4;
  4302. else if (contains_ci_len(s + vstart, vend - vstart,
  4303. "space-evenly"))
  4304. j = 5;
  4305. else if (contains_ci_len(s + vstart, vend - vstart,
  4306. "center"))
  4307. j = 1;
  4308. else if (contains_ci_len(s + vstart, vend - vstart,
  4309. "flex-end") ||
  4310. contains_ci_len(s + vstart, vend - vstart,
  4311. "end"))
  4312. j = 2;
  4313. decl_add(out, nout, PROP_JUSTIFY, j, important);
  4314. } else if (str_eq(prop, "align-items")) {
  4315. int a = 0;
  4316. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  4317. a = 1;
  4318. else if (contains_ci_len(s + vstart, vend - vstart,
  4319. "flex-end") ||
  4320. contains_ci_len(s + vstart, vend - vstart,
  4321. "end"))
  4322. a = 2;
  4323. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  4324. } else if (str_eq(prop, "position")) {
  4325. int p = 0;
  4326. if (contains_ci_len(s + vstart, vend - vstart,
  4327. "absolute"))
  4328. p = 1;
  4329. else if (contains_ci_len(s + vstart, vend - vstart,
  4330. "fixed"))
  4331. p = 2;
  4332. else if (contains_ci_len(s + vstart, vend - vstart,
  4333. "relative"))
  4334. p = 3;
  4335. else if (contains_ci_len(s + vstart, vend - vstart,
  4336. "sticky"))
  4337. p = 4;
  4338. decl_add(out, nout, PROP_POS, p, important);
  4339. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  4340. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  4341. int which = str_eq(prop, "left") ? 0 :
  4342. str_eq(prop, "top") ? 1 :
  4343. str_eq(prop, "right") ? 2 : 3;
  4344. unsigned int pos = 0;
  4345. if (!(vend - vstart >= 4 &&
  4346. ci_eq_n(s + vstart, "auto", 4))) {
  4347. int v = parse_len(s + vstart, vend - vstart, &pos);
  4348. if (v > 32000)
  4349. v = 32000;
  4350. else if (v < -32000)
  4351. v = -32000;
  4352. decl_add(out, nout,
  4353. (unsigned char)(which == 0 ? PROP_LEFT :
  4354. which == 1 ? PROP_TOP :
  4355. which == 2 ? PROP_RIGHT :
  4356. PROP_BOTTOM),
  4357. v, important);
  4358. decl_add(out, nout,
  4359. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  4360. which == 1 ? PROP_TOP_SET :
  4361. which == 2 ? PROP_RIGHT_SET :
  4362. PROP_BOTTOM_SET),
  4363. 1, important);
  4364. }
  4365. } else if (str_eq(prop, "z-index")) {
  4366. unsigned int q = vstart;
  4367. int sign = 1;
  4368. int v = 0;
  4369. int any = 0;
  4370. if (q < vend && s[q] == '-') {
  4371. sign = -1;
  4372. q++;
  4373. }
  4374. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  4375. v = v * 10 + (s[q] - '0');
  4376. any = 1;
  4377. q++;
  4378. }
  4379. if (any) {
  4380. v *= sign;
  4381. if (v > 32000)
  4382. v = 32000;
  4383. else if (v < -32000)
  4384. v = -32000;
  4385. decl_add(out, nout, PROP_ZINDEX, v, important);
  4386. decl_add(out, nout, PROP_ZSET, 1, important);
  4387. } else if (vend - vstart >= 4 &&
  4388. ci_eq_n(s + vstart, "auto", 4)) {
  4389. decl_add(out, nout, PROP_ZSET, 0, important);
  4390. }
  4391. } else if (str_eq(prop, "transform")) {
  4392. const char *v = s + vstart;
  4393. unsigned int vl = vend - vstart;
  4394. int tx = 0, ty = 0;
  4395. int is_x = contains_ci_len(v, vl, "translatex");
  4396. int is_y = contains_ci_len(v, vl, "translatey");
  4397. unsigned int off = 0, arg;
  4398. if (contains_ci_len(v, vl, "translate")) {
  4399. while (off < vl && v[off] != '(')
  4400. off++;
  4401. if (off < vl)
  4402. off++;
  4403. if (!is_y)
  4404. tx = parse_len(v, vl, &off);
  4405. if (!is_x) {
  4406. while (off < vl &&
  4407. (is_space(v[off]) || v[off] == ','))
  4408. off++;
  4409. ty = parse_len(v, vl, &off);
  4410. }
  4411. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  4412. important);
  4413. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  4414. important);
  4415. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  4416. important);
  4417. }
  4418. if (find_fn(v, vl, "scale", &arg)) {
  4419. unsigned int q = arg;
  4420. signed short sx = 100, sy = 100;
  4421. if (parse_fixed_100(v, vl, &q, &sx)) {
  4422. unsigned int r = q;
  4423. while (r < vl &&
  4424. (is_space(v[r]) || v[r] == ','))
  4425. r++;
  4426. if (!parse_fixed_100(v, vl, &r, &sy))
  4427. sy = sx;
  4428. decl_add(out, nout, PROP_SCALE_X, sx,
  4429. important);
  4430. decl_add(out, nout, PROP_SCALE_Y, sy,
  4431. important);
  4432. decl_add(out, nout,
  4433. PROP_TRANSFORM_PAINT, 1,
  4434. important);
  4435. decl_add(out, nout,
  4436. PROP_TRANSFORM_SET, 1,
  4437. important);
  4438. }
  4439. }
  4440. if (find_fn(v, vl, "rotate", &arg)) {
  4441. unsigned int q = arg;
  4442. signed short deg;
  4443. if (parse_fixed_100(v, vl, &q, &deg)) {
  4444. decl_add(out, nout, PROP_ROTATE, deg,
  4445. important);
  4446. decl_add(out, nout,
  4447. PROP_TRANSFORM_PAINT, 1,
  4448. important);
  4449. decl_add(out, nout,
  4450. PROP_TRANSFORM_SET, 1,
  4451. important);
  4452. }
  4453. }
  4454. } else if (str_eq(prop, "transform-origin")) {
  4455. add_transform_origin(out, nout, s + vstart,
  4456. vend - vstart, important);
  4457. } else if (str_eq(prop, "grid-template-columns") ||
  4458. str_eq(prop, "grid-template")) {
  4459. const char *v = s + vstart;
  4460. unsigned int vl = vend - vstart;
  4461. unsigned short val[2];
  4462. unsigned char kind[2];
  4463. int n, q;
  4464. if (str_eq(prop, "grid-template")) {
  4465. for (q = 0; q < (int)vl; q++)
  4466. if (v[q] == '/') {
  4467. q++;
  4468. while (q < (int)vl && is_space(v[q]))
  4469. q++;
  4470. v += q;
  4471. vl -= (unsigned int)q;
  4472. break;
  4473. }
  4474. }
  4475. if (contains_ci_len(v, vl, "auto-fit") ||
  4476. contains_ci_len(v, vl, "auto-fill")) {
  4477. unsigned int mp = 0;
  4478. int found = 0;
  4479. unsigned short mw = 200;
  4480. unsigned char mk = 1;
  4481. for (q = 0; q + 7 <= (int)vl; q++)
  4482. if (ci_eq_n(v + q, "minmax(", 7)) {
  4483. mp = (unsigned int)q + 7;
  4484. found = 1;
  4485. break;
  4486. }
  4487. if (found)
  4488. parse_grid_track(v, vl, &mp, &mw, &mk);
  4489. if (mk != 1 || mw < 80)
  4490. mw = 200;
  4491. if (mw > 480)
  4492. mw = 480;
  4493. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  4494. decl_add(out, nout, PROP_WIDTH, mw, important);
  4495. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  4496. } else {
  4497. n = parse_grid_cols(v, vl, val, kind);
  4498. if (n > 0) {
  4499. decl_add(out, nout, PROP_WIDTH, val[0],
  4500. important);
  4501. decl_add(out, nout, PROP_WIDTH_KIND,
  4502. kind[0], important);
  4503. decl_add(out, nout, PROP_GRID_NCOLS, n,
  4504. important);
  4505. }
  4506. if (n > 1) {
  4507. decl_add(out, nout, PROP_MAXW, val[1],
  4508. important);
  4509. decl_add(out, nout, PROP_MAXW_KIND,
  4510. kind[1], important);
  4511. }
  4512. }
  4513. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  4514. unsigned int gp = 0;
  4515. unsigned short gv = 0;
  4516. unsigned char gk = 1;
  4517. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  4518. if (gv > 120)
  4519. gv = 120;
  4520. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  4521. }
  4522. } else if (str_eq(prop, "grid-area")) {
  4523. int slot = parse_grid_slot(s + vstart, vend - vstart);
  4524. if (slot)
  4525. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  4526. } else if (str_eq(prop, "width") ||
  4527. str_eq(prop, "max-width") ||
  4528. str_eq(prop, "min-width") ||
  4529. str_eq(prop, "height") ||
  4530. str_eq(prop, "max-height") ||
  4531. str_eq(prop, "min-height")) {
  4532. unsigned short v;
  4533. unsigned char kind;
  4534. if (vend - vstart >= 5 &&
  4535. ci_eq_n(s + vstart, "calc(", 5)) {
  4536. int pct, px;
  4537. if (parse_calc(s + vstart + 5,
  4538. vend - vstart - 5, &pct, &px)) {
  4539. if (str_eq(prop, "width")) {
  4540. decl_add(out, nout, PROP_WIDTH,
  4541. 0, important);
  4542. decl_add(out, nout,
  4543. PROP_WIDTH_KIND, 3,
  4544. important);
  4545. decl_add(out, nout,
  4546. PROP_WIDTH_CALC_PCT,
  4547. pct, important);
  4548. decl_add(out, nout,
  4549. PROP_WIDTH_CALC_PX,
  4550. px, important);
  4551. } else if (str_eq(prop, "max-width")) {
  4552. decl_add(out, nout, PROP_MAXW,
  4553. 0, important);
  4554. decl_add(out, nout,
  4555. PROP_MAXW_KIND, 3,
  4556. important);
  4557. decl_add(out, nout,
  4558. PROP_MAXW_CALC_PCT,
  4559. pct, important);
  4560. decl_add(out, nout,
  4561. PROP_MAXW_CALC_PX,
  4562. px, important);
  4563. } else if (str_eq(prop, "min-width")) {
  4564. decl_add(out, nout, PROP_MINW,
  4565. 0, important);
  4566. decl_add(out, nout,
  4567. PROP_MINW_KIND, 3,
  4568. important);
  4569. decl_add(out, nout,
  4570. PROP_MINW_CALC_PCT,
  4571. pct, important);
  4572. decl_add(out, nout,
  4573. PROP_MINW_CALC_PX,
  4574. px, important);
  4575. } else if (str_eq(prop, "height")) {
  4576. decl_add(out, nout, PROP_HEIGHT,
  4577. 0, important);
  4578. decl_add(out, nout,
  4579. PROP_HEIGHT_KIND, 3,
  4580. important);
  4581. decl_add(out, nout,
  4582. PROP_HEIGHT_CALC_PCT,
  4583. pct, important);
  4584. decl_add(out, nout,
  4585. PROP_HEIGHT_CALC_PX,
  4586. px, important);
  4587. } else if (str_eq(prop, "max-height")) {
  4588. decl_add(out, nout, PROP_MAXH,
  4589. 0, important);
  4590. decl_add(out, nout,
  4591. PROP_MAXH_KIND, 3,
  4592. important);
  4593. decl_add(out, nout,
  4594. PROP_MAXH_CALC_PCT,
  4595. pct, important);
  4596. decl_add(out, nout,
  4597. PROP_MAXH_CALC_PX,
  4598. px, important);
  4599. } else {
  4600. decl_add(out, nout, PROP_MINH,
  4601. 0, important);
  4602. decl_add(out, nout,
  4603. PROP_MINH_KIND, 3,
  4604. important);
  4605. decl_add(out, nout,
  4606. PROP_MINH_CALC_PCT,
  4607. pct, important);
  4608. decl_add(out, nout,
  4609. PROP_MINH_CALC_PX,
  4610. px, important);
  4611. }
  4612. }
  4613. continue;
  4614. }
  4615. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  4616. if (str_eq(prop, "width")) {
  4617. decl_add(out, nout, PROP_WIDTH, v, important);
  4618. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  4619. important);
  4620. } else if (str_eq(prop, "max-width")) {
  4621. decl_add(out, nout, PROP_MAXW, v, important);
  4622. decl_add(out, nout, PROP_MAXW_KIND, kind,
  4623. important);
  4624. } else if (str_eq(prop, "min-width")) {
  4625. decl_add(out, nout, PROP_MINW, v, important);
  4626. decl_add(out, nout, PROP_MINW_KIND, kind,
  4627. important);
  4628. } else if (str_eq(prop, "max-height")) {
  4629. decl_add(out, nout, PROP_MAXH, v, important);
  4630. decl_add(out, nout, PROP_MAXH_KIND, kind,
  4631. important);
  4632. } else if (str_eq(prop, "min-height")) {
  4633. decl_add(out, nout, PROP_MINH, v, important);
  4634. decl_add(out, nout, PROP_MINH_KIND, kind,
  4635. important);
  4636. } else {
  4637. decl_add(out, nout, PROP_HEIGHT, v, important);
  4638. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  4639. important);
  4640. }
  4641. }
  4642. } else if (str_eq(prop, "box-sizing")) {
  4643. int bb = (vend - vstart >= 10 &&
  4644. ci_eq_n(s + vstart, "border-box", 10));
  4645. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  4646. } else if (str_eq(prop, "line-height")) {
  4647. unsigned short px;
  4648. unsigned char pct;
  4649. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  4650. if (px) {
  4651. decl_add(out, nout, PROP_LH_PX, px, important);
  4652. decl_add(out, nout, PROP_LH_PCT, 0, important);
  4653. } else {
  4654. decl_add(out, nout, PROP_LH_PX, 0, important);
  4655. decl_add(out, nout, PROP_LH_PCT, pct, important);
  4656. }
  4657. }
  4658. } else if (str_eq(prop, "list-style") ||
  4659. str_eq(prop, "list-style-type")) {
  4660. int ls = BR_LIST_AUTO;
  4661. if (contains_ci_len(s + vstart, vend - vstart,
  4662. "none"))
  4663. ls = BR_LIST_NONE;
  4664. else if (contains_ci_len(s + vstart, vend - vstart,
  4665. "circle"))
  4666. ls = BR_LIST_CIRCLE;
  4667. else if (contains_ci_len(s + vstart, vend - vstart,
  4668. "square"))
  4669. ls = BR_LIST_SQUARE;
  4670. else if (contains_ci_len(s + vstart, vend - vstart,
  4671. "lower-alpha") ||
  4672. contains_ci_len(s + vstart, vend - vstart,
  4673. "lower-latin"))
  4674. ls = BR_LIST_LOWER_ALPHA;
  4675. else if (contains_ci_len(s + vstart, vend - vstart,
  4676. "upper-alpha") ||
  4677. contains_ci_len(s + vstart, vend - vstart,
  4678. "upper-latin"))
  4679. ls = BR_LIST_UPPER_ALPHA;
  4680. else if (contains_ci_len(s + vstart, vend - vstart,
  4681. "lower-roman"))
  4682. ls = BR_LIST_LOWER_ROMAN;
  4683. else if (contains_ci_len(s + vstart, vend - vstart,
  4684. "upper-roman"))
  4685. ls = BR_LIST_UPPER_ROMAN;
  4686. else if (contains_ci_len(s + vstart, vend - vstart,
  4687. "decimal"))
  4688. ls = BR_LIST_DECIMAL;
  4689. else if (contains_ci_len(s + vstart, vend - vstart,
  4690. "disc"))
  4691. ls = BR_LIST_DISC;
  4692. decl_add(out, nout, PROP_LIST_NONE,
  4693. ls == BR_LIST_NONE, important);
  4694. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  4695. } else if (str_eq(prop, "border-radius") ||
  4696. str_eq(prop, "border-top-left-radius") ||
  4697. str_eq(prop, "border-top-right-radius") ||
  4698. str_eq(prop, "border-bottom-right-radius") ||
  4699. str_eq(prop, "border-bottom-left-radius")) {
  4700. unsigned short v[4];
  4701. unsigned char k[4];
  4702. unsigned int pos = 0;
  4703. int n = 0;
  4704. while (pos < vend - vstart && n < 4) {
  4705. while (pos < vend - vstart &&
  4706. is_space(s[vstart + pos]))
  4707. pos++;
  4708. if (pos >= vend - vstart ||
  4709. s[vstart + pos] == '/')
  4710. break;
  4711. if (!parse_radius_value(s + vstart,
  4712. vend - vstart, &pos,
  4713. &v[n], &k[n]))
  4714. break;
  4715. n++;
  4716. }
  4717. if (n) {
  4718. unsigned short r[4];
  4719. unsigned char rk[4];
  4720. int i;
  4721. if (n == 1) {
  4722. for (i = 0; i < 4; i++) {
  4723. r[i] = v[0];
  4724. rk[i] = k[0];
  4725. }
  4726. } else if (n == 2) {
  4727. r[0] = r[2] = v[0];
  4728. r[1] = r[3] = v[1];
  4729. rk[0] = rk[2] = k[0];
  4730. rk[1] = rk[3] = k[1];
  4731. } else if (n == 3) {
  4732. r[0] = v[0];
  4733. r[1] = r[3] = v[1];
  4734. r[2] = v[2];
  4735. rk[0] = k[0];
  4736. rk[1] = rk[3] = k[1];
  4737. rk[2] = k[2];
  4738. } else {
  4739. for (i = 0; i < 4; i++) {
  4740. r[i] = v[i];
  4741. rk[i] = k[i];
  4742. }
  4743. }
  4744. for (i = 0; i < 4; i++)
  4745. r[i] = rk[i] == 2 ? 255 :
  4746. (r[i] > 255 ? 255 : r[i]);
  4747. if (str_eq(prop, "border-top-left-radius")) {
  4748. decl_add(out, nout, PROP_RADIUS_TL,
  4749. (short)r[0], important);
  4750. } else if (str_eq(prop,
  4751. "border-top-right-radius")) {
  4752. decl_add(out, nout, PROP_RADIUS_TR,
  4753. (short)r[0], important);
  4754. } else if (str_eq(prop,
  4755. "border-bottom-right-radius")) {
  4756. decl_add(out, nout, PROP_RADIUS_BR,
  4757. (short)r[0], important);
  4758. } else if (str_eq(prop,
  4759. "border-bottom-left-radius")) {
  4760. decl_add(out, nout, PROP_RADIUS_BL,
  4761. (short)r[0], important);
  4762. } else {
  4763. decl_add(out, nout, PROP_RADIUS_TL,
  4764. (short)r[0], important);
  4765. decl_add(out, nout, PROP_RADIUS_TR,
  4766. (short)r[1], important);
  4767. decl_add(out, nout, PROP_RADIUS_BR,
  4768. (short)r[2], important);
  4769. decl_add(out, nout, PROP_RADIUS_BL,
  4770. (short)r[3], important);
  4771. }
  4772. }
  4773. } else if (str_eq(prop, "border") ||
  4774. str_eq(prop, "border-top") ||
  4775. str_eq(prop, "border-right") ||
  4776. str_eq(prop, "border-bottom") ||
  4777. str_eq(prop, "border-left") ||
  4778. str_eq(prop, "border-color") ||
  4779. str_eq(prop, "border-width") ||
  4780. str_eq(prop, "border-style") ||
  4781. str_eq(prop, "border-top-width") ||
  4782. str_eq(prop, "border-right-width") ||
  4783. str_eq(prop, "border-bottom-width") ||
  4784. str_eq(prop, "border-left-width") ||
  4785. str_eq(prop, "border-top-color") ||
  4786. str_eq(prop, "border-right-color") ||
  4787. str_eq(prop, "border-bottom-color") ||
  4788. str_eq(prop, "border-left-color") ||
  4789. str_eq(prop, "border-top-style") ||
  4790. str_eq(prop, "border-right-style") ||
  4791. str_eq(prop, "border-bottom-style") ||
  4792. str_eq(prop, "border-left-style")) {
  4793. const char *v = s + vstart;
  4794. unsigned int vl = vend - vstart;
  4795. unsigned int pos = 0, ts, te;
  4796. int widths[4], colors[4], styles[4];
  4797. int nw = 0, nc = 0, ns = 0;
  4798. int side = -1;
  4799. int is_wl = str_eq(prop, "border-width");
  4800. int is_cl = str_eq(prop, "border-color");
  4801. int is_sl = str_eq(prop, "border-style");
  4802. int i;
  4803. if (str_eq(prop, "border-top")) side = 0;
  4804. else if (str_eq(prop, "border-right")) side = 1;
  4805. else if (str_eq(prop, "border-bottom")) side = 2;
  4806. else if (str_eq(prop, "border-left")) side = 3;
  4807. else if (str_eq(prop, "border-top-width")) {
  4808. side = 0; is_wl = 2;
  4809. } else if (str_eq(prop, "border-right-width")) {
  4810. side = 1; is_wl = 2;
  4811. } else if (str_eq(prop, "border-bottom-width")) {
  4812. side = 2; is_wl = 2;
  4813. } else if (str_eq(prop, "border-left-width")) {
  4814. side = 3; is_wl = 2;
  4815. } else if (str_eq(prop, "border-top-color")) {
  4816. side = 0; is_cl = 2;
  4817. } else if (str_eq(prop, "border-right-color")) {
  4818. side = 1; is_cl = 2;
  4819. } else if (str_eq(prop, "border-bottom-color")) {
  4820. side = 2; is_cl = 2;
  4821. } else if (str_eq(prop, "border-left-color")) {
  4822. side = 3; is_cl = 2;
  4823. } else if (str_eq(prop, "border-top-style")) {
  4824. side = 0; is_sl = 2;
  4825. } else if (str_eq(prop, "border-right-style")) {
  4826. side = 1; is_sl = 2;
  4827. } else if (str_eq(prop, "border-bottom-style")) {
  4828. side = 2; is_sl = 2;
  4829. } else if (str_eq(prop, "border-left-style")) {
  4830. side = 3; is_sl = 2;
  4831. }
  4832. if (is_wl) {
  4833. while (nw < 4 && next_token(v, vl, &pos,
  4834. &ts, &te)) {
  4835. int x;
  4836. if (!parse_len_token(v + ts, te - ts,
  4837. &x))
  4838. break;
  4839. widths[nw++] = x;
  4840. }
  4841. if (nw) {
  4842. if (is_wl == 1)
  4843. expand_four(widths, nw);
  4844. if (side >= 0 && is_wl == 2) {
  4845. decl_add(out, nout,
  4846. PROP_BW_T + side,
  4847. widths[0], important);
  4848. } else {
  4849. for (i = 0; i < 4; i++)
  4850. decl_add(out, nout,
  4851. PROP_BW_T + i,
  4852. widths[i],
  4853. important);
  4854. }
  4855. }
  4856. } else if (is_cl) {
  4857. unsigned short c;
  4858. while (nc < 4 && next_token(v, vl, &pos,
  4859. &ts, &te)) {
  4860. if (!parse_color(v + ts, te - ts, &c))
  4861. break;
  4862. colors[nc++] = c;
  4863. }
  4864. if (nc) {
  4865. if (is_cl == 1)
  4866. expand_four(colors, nc);
  4867. if (side >= 0 && is_cl == 2) {
  4868. decl_add(out, nout,
  4869. PROP_BC_T + side,
  4870. colors[0], important);
  4871. } else {
  4872. for (i = 0; i < 4; i++)
  4873. decl_add(out, nout,
  4874. PROP_BC_T + i,
  4875. colors[i],
  4876. important);
  4877. }
  4878. }
  4879. } else if (is_sl) {
  4880. while (ns < 4 && next_token(v, vl, &pos,
  4881. &ts, &te)) {
  4882. int stv;
  4883. if ((te - ts == 4 &&
  4884. ci_eq_n(v + ts, "none", 4)) ||
  4885. (te - ts == 6 &&
  4886. ci_eq_n(v + ts, "hidden", 6)))
  4887. stv = 0;
  4888. else if (te - ts == 6 &&
  4889. ci_eq_n(v + ts,
  4890. "dashed", 6))
  4891. stv = 2;
  4892. else if (te - ts == 6 &&
  4893. ci_eq_n(v + ts,
  4894. "dotted", 6))
  4895. stv = 3;
  4896. else
  4897. stv = 1;
  4898. styles[ns++] = stv;
  4899. }
  4900. if (ns) {
  4901. if (is_sl == 1)
  4902. expand_four(styles, ns);
  4903. if (is_sl == 2 && side >= 0) {
  4904. if (!styles[0])
  4905. decl_add(out, nout,
  4906. PROP_BW_T + side,
  4907. 0, important);
  4908. else
  4909. decl_add(out, nout,
  4910. PROP_BS_T + side,
  4911. styles[0] - 1,
  4912. important);
  4913. } else {
  4914. for (i = 0; i < 4; i++) {
  4915. if (!styles[i])
  4916. decl_add(out, nout,
  4917. PROP_BW_T + i,
  4918. 0,
  4919. important);
  4920. else
  4921. decl_add(out, nout,
  4922. PROP_BS_T + i,
  4923. styles[i] - 1,
  4924. important);
  4925. }
  4926. }
  4927. }
  4928. } else {
  4929. int bw = -1, bc = -1, style_none = 0;
  4930. int bstyle = -1;
  4931. while (next_token(v, vl, &pos, &ts, &te)) {
  4932. unsigned short c;
  4933. int x;
  4934. if ((te - ts == 4 &&
  4935. ci_eq_n(v + ts, "none", 4)) ||
  4936. (te - ts == 6 &&
  4937. ci_eq_n(v + ts, "hidden", 6))) {
  4938. style_none = 1;
  4939. } else if (te - ts == 6 &&
  4940. ci_eq_n(v + ts, "dashed", 6)) {
  4941. bstyle = BR_BORDER_DASHED;
  4942. } else if (te - ts == 6 &&
  4943. ci_eq_n(v + ts, "dotted", 6)) {
  4944. bstyle = BR_BORDER_DOTTED;
  4945. } else if (te - ts == 5 &&
  4946. ci_eq_n(v + ts, "solid", 5)) {
  4947. bstyle = BR_BORDER_SOLID;
  4948. } else if (parse_len_token(v + ts,
  4949. te - ts, &x)) {
  4950. bw = x;
  4951. } else if (parse_color(v + ts,
  4952. te - ts, &c)) {
  4953. bc = c;
  4954. }
  4955. }
  4956. if (style_none)
  4957. bw = 0;
  4958. if (bc >= 0 && bw < 0)
  4959. bw = 1;
  4960. if (side < 0) {
  4961. if (bw >= 0)
  4962. decl_add(out, nout,
  4963. PROP_BORDER_W, bw,
  4964. important);
  4965. if (bc >= 0)
  4966. decl_add(out, nout,
  4967. PROP_BORDER_COLOR, bc,
  4968. important);
  4969. if (bstyle >= 0)
  4970. for (i = 0; i < 4; i++)
  4971. decl_add(out, nout,
  4972. PROP_BS_T + i,
  4973. bstyle,
  4974. important);
  4975. } else {
  4976. if (bw >= 0)
  4977. decl_add(out, nout,
  4978. PROP_BW_T + side, bw,
  4979. important);
  4980. if (bc >= 0)
  4981. decl_add(out, nout,
  4982. PROP_BC_T + side, bc,
  4983. important);
  4984. if (bstyle >= 0)
  4985. decl_add(out, nout,
  4986. PROP_BS_T + side,
  4987. bstyle, important);
  4988. }
  4989. }
  4990. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  4991. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  4992. unsigned int pos = 0;
  4993. int is_pad = prop[0] == 'p';
  4994. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  4995. while (pos < vend - vstart && nv < 4) {
  4996. units[nv] = 0;
  4997. if (parse_margin_calc(s + vstart,
  4998. vend - vstart, &pos,
  4999. &vals[nv], &cpx[nv])) {
  5000. units[nv] = 5;
  5001. } else {
  5002. vals[nv] = parse_margin_len(
  5003. s + vstart, vend - vstart,
  5004. &pos, &units[nv]);
  5005. }
  5006. nv++;
  5007. while (pos < vend - vstart &&
  5008. is_space(s[vstart + pos]))
  5009. pos++;
  5010. }
  5011. if (nv == 1) {
  5012. vals[1] = vals[2] = vals[3] = vals[0];
  5013. units[1] = units[2] = units[3] = units[0];
  5014. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  5015. } else if (nv == 2) {
  5016. vals[2] = vals[0];
  5017. vals[3] = vals[1];
  5018. units[2] = units[0];
  5019. units[3] = units[1];
  5020. cpx[2] = cpx[0];
  5021. cpx[3] = cpx[1];
  5022. } else if (nv == 3) {
  5023. vals[3] = vals[1];
  5024. units[3] = units[1];
  5025. cpx[3] = cpx[1];
  5026. }
  5027. if (nv > 0) {
  5028. int k;
  5029. for (k = 0; k < 4; k++) {
  5030. int v = vals[k];
  5031. int u = units[k];
  5032. int au = u == 4;
  5033. if (au)
  5034. v = 0;
  5035. if (is_pad && au)
  5036. u = 0;
  5037. decl_add(out, nout,
  5038. (unsigned char)(base + k), v,
  5039. important);
  5040. decl_add(out, nout,
  5041. (unsigned char)
  5042. ((is_pad ? PROP_PT_U :
  5043. PROP_MT_U) + k),
  5044. u, important);
  5045. if (u == 5)
  5046. decl_add(out, nout,
  5047. (unsigned char)
  5048. ((is_pad ? PROP_PT_CPX :
  5049. PROP_MT_CPX) + k),
  5050. cpx[k], important);
  5051. if (!is_pad && (k == 1 || k == 3))
  5052. decl_add(out, nout,
  5053. k == 1 ? PROP_MR_AUTO :
  5054. PROP_ML_AUTO,
  5055. au, important);
  5056. }
  5057. }
  5058. } else if (str_eq(prop, "margin-top") ||
  5059. str_eq(prop, "margin-right") ||
  5060. str_eq(prop, "margin-bottom") ||
  5061. str_eq(prop, "margin-left") ||
  5062. str_eq(prop, "padding-top") ||
  5063. str_eq(prop, "padding-right") ||
  5064. str_eq(prop, "padding-bottom") ||
  5065. str_eq(prop, "padding-left")) {
  5066. unsigned int pos = 0;
  5067. int unit = 0;
  5068. int v = 0, cpx = 0;
  5069. unsigned char p = PROP_MT;
  5070. const char *side;
  5071. int au;
  5072. if (parse_margin_calc(s + vstart, vend - vstart,
  5073. &pos, &v, &cpx))
  5074. unit = 5;
  5075. else
  5076. v = parse_margin_len(s + vstart,
  5077. vend - vstart, &pos,
  5078. &unit);
  5079. au = unit == 4;
  5080. if (au)
  5081. v = 0;
  5082. if (prop[0] == 'p') {
  5083. p = PROP_PT;
  5084. if (au)
  5085. unit = 0;
  5086. }
  5087. side = prop + (prop[0] == 'p' ? 8 : 7);
  5088. if (str_eq(side, "right"))
  5089. p = (unsigned char)(p + 1);
  5090. else if (str_eq(side, "bottom"))
  5091. p = (unsigned char)(p + 2);
  5092. else if (str_eq(side, "left"))
  5093. p = (unsigned char)(p + 3);
  5094. decl_add(out, nout, p, v, important);
  5095. decl_add(out, nout,
  5096. (unsigned char)((prop[0] == 'p' ?
  5097. PROP_PT_U : PROP_MT_U) +
  5098. (p - (prop[0] == 'p' ?
  5099. PROP_PT : PROP_MT))),
  5100. unit, important);
  5101. if (unit == 5)
  5102. decl_add(out, nout,
  5103. (unsigned char)((prop[0] == 'p' ?
  5104. PROP_PT_CPX : PROP_MT_CPX) +
  5105. (p - (prop[0] == 'p' ?
  5106. PROP_PT : PROP_MT))),
  5107. cpx, important);
  5108. if (str_eq(side, "right"))
  5109. decl_add(out, nout, PROP_MR_AUTO, au, important);
  5110. else if (str_eq(side, "left"))
  5111. decl_add(out, nout, PROP_ML_AUTO, au, important);
  5112. }
  5113. }
  5114. }
  5115. /* Skip @media/@font-face/@keyframes/@import without descending into their
  5116. * nested blocks. This is the mobile/base rules path: media enhancements are
  5117. * deliberately ignored for now. */
  5118. static unsigned int skip_at_rule(const char *s, unsigned int len,
  5119. unsigned int i)
  5120. {
  5121. unsigned int j = i;
  5122. while (j < len && s[j] != '{' && s[j] != ';')
  5123. j++;
  5124. if (j >= len)
  5125. return len;
  5126. if (s[j] == ';')
  5127. return j + 1;
  5128. j++;
  5129. return css_skip_nested(s, len, j);
  5130. }
  5131. static unsigned int viewport_w = 1024;
  5132. static unsigned short theme_text = 0x0000;
  5133. static unsigned short theme_bg = 0xFFFF;
  5134. static unsigned short theme_panel = 0xEF5D;
  5135. static unsigned short theme_link = 0x001B;
  5136. void br_set_theme(unsigned short text, unsigned short bg,
  5137. unsigned short panel, unsigned short link)
  5138. {
  5139. theme_text = text;
  5140. theme_bg = bg;
  5141. theme_panel = panel;
  5142. theme_link = link;
  5143. }
  5144. void br_set_viewport(unsigned int width)
  5145. {
  5146. viewport_w = width ? width : 1024;
  5147. }
  5148. static unsigned int media_len_px(const char *s, unsigned int len)
  5149. {
  5150. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  5151. int em = 0;
  5152. while (i < len && is_space(s[i]))
  5153. i++;
  5154. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5155. whole = whole * 10 + (unsigned int)(s[i] - '0');
  5156. i++;
  5157. }
  5158. if (i < len && s[i] == '.') {
  5159. i++;
  5160. /* Consume every fractional digit even when there are more
  5161. * than the three we keep: leaving the tail behind made the
  5162. * unit check miss "em" (74.9375em became 74px). */
  5163. while (i < len && s[i] >= '0' && s[i] <= '9') {
  5164. if (scale < 1000) {
  5165. frac = frac * 10 + (unsigned int)(s[i] - '0');
  5166. scale *= 10;
  5167. }
  5168. i++;
  5169. }
  5170. }
  5171. while (i < len && is_space(s[i]))
  5172. i++;
  5173. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  5174. em = 1;
  5175. i += 2;
  5176. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  5177. em = 1;
  5178. i += 3;
  5179. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  5180. i += 2;
  5181. }
  5182. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  5183. if (em)
  5184. return v * 16 / 100;
  5185. return v / 100;
  5186. }
  5187. /* One parenthesised media feature: min-width/max-width and the modern
  5188. * (width <= 600px) form. Unknown features are treated as true. */
  5189. static int media_feature_matches(const char *s, unsigned int len)
  5190. {
  5191. unsigned int i;
  5192. while (len && is_space(*s)) { s++; len--; }
  5193. while (len && is_space(s[len - 1])) len--;
  5194. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  5195. /* There is no user-selectable colour scheme. Ignore both
  5196. * variants so a page's base palette is used; sites whose base
  5197. * is dark (like the project docs) then stay dark instead of
  5198. * flipping to their light media override. */
  5199. return 0;
  5200. }
  5201. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  5202. for (i = 9; i < len && s[i] != ':'; i++)
  5203. ;
  5204. if (i < len)
  5205. return viewport_w >= media_len_px(s + i + 1,
  5206. len - i - 1);
  5207. return 1;
  5208. }
  5209. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  5210. for (i = 9; i < len && s[i] != ':'; i++)
  5211. ;
  5212. if (i < len)
  5213. return viewport_w <= media_len_px(s + i + 1,
  5214. len - i - 1);
  5215. return 1;
  5216. }
  5217. for (i = 0; i < len; i++) {
  5218. if (s[i] == '<' || s[i] == '>') {
  5219. int max = s[i] == '<';
  5220. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  5221. if (i + 1 < len && s[i + 1] == '=')
  5222. v = media_len_px(s + i + 2, len - i - 2);
  5223. return max ? viewport_w <= v : viewport_w >= v;
  5224. }
  5225. }
  5226. return 1;
  5227. }
  5228. static int media_query_matches(const char *s, unsigned int len)
  5229. {
  5230. unsigned int i;
  5231. if (contains_ci_len(s, len, "print") &&
  5232. !contains_ci_len(s, len, "screen"))
  5233. return 0;
  5234. if (contains_ci_len(s, len, "not "))
  5235. return 0;
  5236. for (i = 0; i < len; i++) {
  5237. if (s[i] == '(') {
  5238. unsigned int st = ++i;
  5239. while (i < len && s[i] != ')')
  5240. i++;
  5241. if (!media_feature_matches(s + st, i - st))
  5242. return 0;
  5243. }
  5244. }
  5245. return 1;
  5246. }
  5247. static int media_list_matches(const char *s, unsigned int len)
  5248. {
  5249. unsigned int i = 0;
  5250. while (i < len) {
  5251. unsigned int st = i;
  5252. while (i < len && s[i] != ',')
  5253. i++;
  5254. if (media_query_matches(s + st, i - st))
  5255. return 1;
  5256. i++;
  5257. }
  5258. return 0;
  5259. }
  5260. static unsigned int rule_hash(const char *s, unsigned int len)
  5261. {
  5262. unsigned int h = 2166136261u, i;
  5263. for (i = 0; i < len; i++) {
  5264. char c = s[i];
  5265. if (c >= 'A' && c <= 'Z')
  5266. c = (char)(c - 'A' + 'a');
  5267. h ^= (unsigned char)c;
  5268. h *= 16777619u;
  5269. }
  5270. return h % BR_RULE_HASH;
  5271. }
  5272. /* Index a parsed rule by the most selective component of its rightmost
  5273. * compound selector. br_style_for() then only visits rules that can
  5274. * possibly match a node instead of scanning every rule. */
  5275. static void rule_index_add(struct br_doc *d, unsigned int idx,
  5276. const char *key, unsigned int len)
  5277. {
  5278. unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
  5279. unsigned short *head;
  5280. for (i = 0; i < len; i++) {
  5281. if (key[i] == '.' || key[i] == '#') {
  5282. unsigned int st = i + 1, n = 0;
  5283. while (st + n < len && is_ident(key[st + n]))
  5284. n++;
  5285. if (key[i] == '#') {
  5286. ipos = st;
  5287. ilen = n;
  5288. } else if (n) {
  5289. cpos = st;
  5290. clen = n;
  5291. }
  5292. i = st + n - 1;
  5293. }
  5294. }
  5295. if (ilen)
  5296. head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
  5297. else if (clen)
  5298. head = &d->rule_bucket[rule_hash(key + cpos, clen)];
  5299. else if (len == 5 && ci_eq_n(key, ":root", 5))
  5300. head = &d->rule_tag[BR_TAG_HTML];
  5301. else if (len && is_ident(key[0])) {
  5302. char name[16];
  5303. unsigned int n = 0, tag;
  5304. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  5305. name[n] = key[n];
  5306. n++;
  5307. }
  5308. name[n] = '\0';
  5309. tag = br_tag_from_name(name);
  5310. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  5311. head = &d->rule_tag[tag];
  5312. else
  5313. head = &d->rule_any;
  5314. } else {
  5315. head = &d->rule_any;
  5316. }
  5317. d->rule_next[idx] = *head;
  5318. *head = (unsigned short)idx;
  5319. }
  5320. static unsigned int selector_specificity(const char *s, unsigned int len);
  5321. static int css_collect_only;
  5322. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  5323. {
  5324. unsigned int i = 0;
  5325. while (i < len && d->nrules < d->rule_cap) {
  5326. unsigned int sstart, send, bstart, bend, j;
  5327. struct br_decl decls[BR_DECLS];
  5328. unsigned int nd;
  5329. while (i < len && is_space(s[i]))
  5330. i++;
  5331. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  5332. i += 2;
  5333. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  5334. i++;
  5335. i += 2;
  5336. continue;
  5337. }
  5338. if (i >= len || s[i] == '}')
  5339. break;
  5340. if (s[i] == '@') {
  5341. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  5342. unsigned int q = i + 6;
  5343. while (q < len && s[q] != '{')
  5344. q++;
  5345. if (q < len &&
  5346. media_list_matches(s + i + 6,
  5347. q - (i + 6))) {
  5348. unsigned int b = q + 1;
  5349. unsigned int e = css_skip_nested(s, len, b);
  5350. if (e > b)
  5351. parse_css(d, s + b,
  5352. (e - 1) - b);
  5353. i = e;
  5354. } else {
  5355. i = skip_at_rule(s, len, i);
  5356. }
  5357. continue;
  5358. }
  5359. i = skip_at_rule(s, len, i);
  5360. continue;
  5361. }
  5362. sstart = i;
  5363. while (i < len && s[i] != '{')
  5364. i++;
  5365. if (i >= len)
  5366. break;
  5367. send = i;
  5368. i++;
  5369. bstart = i;
  5370. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  5371. * to the matching close brace; stopping at the first '}' left
  5372. * the parser out of sync and dropped the rest of the sheet
  5373. * (nd-aktuell lost its desktop rules that way). */
  5374. bend = css_block_end(s, len, i);
  5375. i = bend < len ? bend + 1 : len;
  5376. while (sstart < send && is_space(s[sstart]))
  5377. sstart++;
  5378. while (send > sstart && is_space(s[send - 1]))
  5379. send--;
  5380. if (sstart == send)
  5381. continue;
  5382. if (!css_dark_selector(s + sstart, send - sstart))
  5383. css_collect_block(d, s + bstart, bend - bstart);
  5384. if (css_collect_only)
  5385. continue;
  5386. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  5387. if (!nd)
  5388. continue;
  5389. /* Split a comma-separated selector list into separate rules. */
  5390. j = sstart;
  5391. while (j <= send && d->nrules < d->rule_cap) {
  5392. unsigned int q = j, e, k;
  5393. char sel[160];
  5394. unsigned int off;
  5395. while (q < send && s[q] != ',')
  5396. q++;
  5397. e = q;
  5398. while (j < e && is_space(s[j]))
  5399. j++;
  5400. while (e > j && is_space(s[e - 1]))
  5401. e--;
  5402. if (e > j) {
  5403. /* Ignore single selectors with sibling combinators,
  5404. * pseudo-classes or attribute tests: they need
  5405. * state/attributes we do not model. The other
  5406. * selectors in the list still apply. */
  5407. for (k = j; k < e; ) {
  5408. if (s[k] == '+' || s[k] == '~')
  5409. break;
  5410. if (s[k] == ':') {
  5411. if (k + 5 <= e &&
  5412. ci_eq_n(s + k, ":root", 5)) {
  5413. k += 5;
  5414. continue;
  5415. }
  5416. if (k + 5 <= e &&
  5417. ci_eq_n(s + k, ":link", 5) &&
  5418. (k + 5 == e ||
  5419. !is_ident(s[k + 5]))) {
  5420. k += 5;
  5421. continue;
  5422. }
  5423. if (k + 5 <= e &&
  5424. ci_eq_n(s + k, ":not(", 5)) {
  5425. unsigned int n = k + 5;
  5426. int nd = 1;
  5427. while (n < e && nd) {
  5428. if (s[n] == '(')
  5429. nd++;
  5430. else if (s[n] == ')')
  5431. nd--;
  5432. n++;
  5433. }
  5434. if (nd)
  5435. break;
  5436. k = n;
  5437. continue;
  5438. }
  5439. break;
  5440. }
  5441. k++;
  5442. }
  5443. if (k < e) {
  5444. j = q + 1;
  5445. if (q >= send)
  5446. break;
  5447. continue;
  5448. }
  5449. for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
  5450. sel[k] = s[j + k];
  5451. sel[k] = '\0';
  5452. off = store_attr(d, sel, k, 159);
  5453. if (off) {
  5454. struct br_rule *r = &d->rules[d->nrules++];
  5455. unsigned int ks = 0, q;
  5456. for (q = 0; q < k; q++)
  5457. if (sel[q] == ' ' || sel[q] == '>')
  5458. ks = q + 1;
  5459. r->sel_off = off;
  5460. r->key_off = store_attr(d, sel + ks,
  5461. k - ks, 159);
  5462. r->spec = (unsigned short)selector_specificity(sel, k);
  5463. r->single = 1;
  5464. {
  5465. unsigned int z;
  5466. for (z = 0; z < k; z++)
  5467. if (is_space(sel[z]) || sel[z] == '>') {
  5468. r->single = 0;
  5469. break;
  5470. }
  5471. }
  5472. r->nd = (unsigned char)nd;
  5473. for (k = 0; k < nd; k++)
  5474. r->d[k] = decls[k];
  5475. rule_index_add(d, d->nrules - 1,
  5476. attr_ptr(d, r->key_off),
  5477. s_len(attr_ptr(d, r->key_off)));
  5478. }
  5479. }
  5480. j = q + 1;
  5481. if (q >= send)
  5482. break;
  5483. }
  5484. }
  5485. }
  5486. /* Sites often set html { font-size: 62.5% } and then size everything in
  5487. * rem. Detect that root size before parsing the sheet so a rem resolves to
  5488. * the same pixels a real browser would use. */
  5489. static void detect_root_font(const char *s, unsigned int len)
  5490. {
  5491. unsigned int i;
  5492. for (i = 0; i + 4 < len; i++) {
  5493. unsigned int j, end, body, sel, q, h;
  5494. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  5495. int exact;
  5496. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  5497. s[i + 3] == 'l'))
  5498. continue;
  5499. h = i + 4;
  5500. while (h < len && is_space(s[h]))
  5501. h++;
  5502. if (h < len && s[h] != '{' && s[h] != ',' &&
  5503. s[h] != '.' && s[h] != ':' && s[h] != '[')
  5504. continue;
  5505. sel = i;
  5506. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  5507. s[sel - 1] != ';' && s[sel - 1] != ',')
  5508. sel--;
  5509. q = sel;
  5510. while (q < i && is_space(s[q]))
  5511. q++;
  5512. exact = (q == i);
  5513. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  5514. continue;
  5515. j = h;
  5516. if (j >= len || s[j] != '{')
  5517. continue;
  5518. while (j < len && s[j] != '{')
  5519. j++;
  5520. if (j >= len)
  5521. break;
  5522. end = j + 1;
  5523. body = end;
  5524. while (end < len && s[end] != '}')
  5525. end++;
  5526. for (; j + 9 < end; j++) {
  5527. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  5528. s[j + 3] == 't' && s[j + 4] == '-' &&
  5529. s[j + 5] == 's' && s[j + 6] == 'i' &&
  5530. s[j + 7] == 'z' && s[j + 8] == 'e'))
  5531. continue;
  5532. j += 9;
  5533. while (j < end && s[j] != ':')
  5534. j++;
  5535. if (j >= end)
  5536. break;
  5537. j++;
  5538. while (j < end && is_space(s[j]))
  5539. j++;
  5540. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5541. v = v * 10 + (s[j] - '0');
  5542. digits = 1;
  5543. j++;
  5544. }
  5545. if (j < end && s[j] == '.') {
  5546. j++;
  5547. while (j < end && s[j] >= '0' && s[j] <= '9') {
  5548. v = v * 10 + (s[j] - '0');
  5549. frac++;
  5550. j++;
  5551. }
  5552. }
  5553. while (frac < 2) {
  5554. v *= 10;
  5555. frac++;
  5556. }
  5557. while (frac > 2) {
  5558. v /= 10;
  5559. frac--;
  5560. }
  5561. if (!digits)
  5562. break;
  5563. if (j < end && s[j] == '%')
  5564. pct = 1;
  5565. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  5566. px = 1;
  5567. if (pct)
  5568. v = (v * 16 + 5000) / 10000;
  5569. else if (px)
  5570. v /= 100;
  5571. else
  5572. v = v * 16 / 100;
  5573. if (exact && v >= 8 && v <= 24)
  5574. root_font_px = v;
  5575. if (exact)
  5576. return;
  5577. }
  5578. /* html { font: 10px/1 Verdana } also sets the root size. */
  5579. for (j = body; j + 5 <= end; j++) {
  5580. unsigned int p, ts, te;
  5581. if (!ci_eq_n(s + j, "font:", 5))
  5582. continue;
  5583. p = j + 5;
  5584. while (next_token(s, end, &p, &ts, &te)) {
  5585. int fv = parse_font_px(s + ts, te - ts);
  5586. if (fv >= 8 && fv <= 24) {
  5587. if (exact)
  5588. root_font_px = fv;
  5589. break;
  5590. }
  5591. }
  5592. if (exact)
  5593. return;
  5594. break;
  5595. }
  5596. }
  5597. }
  5598. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  5599. {
  5600. scan_font_faces(css, len);
  5601. detect_root_font(css, len);
  5602. css_collect_only = 1;
  5603. parse_css(d, css, len);
  5604. css_collect_only = 0;
  5605. parse_css(d, css, len);
  5606. }
  5607. void br_parse_inline_css(struct br_doc *d)
  5608. {
  5609. if (d->inline_css_len) {
  5610. scan_font_faces(css_store, d->inline_css_len);
  5611. detect_root_font(css_store, d->inline_css_len);
  5612. css_collect_only = 1;
  5613. parse_css(d, css_store, d->inline_css_len);
  5614. css_collect_only = 0;
  5615. parse_css(d, css_store, d->inline_css_len);
  5616. }
  5617. d->inline_css_len = 0;
  5618. }
  5619. void br_reset_styles(struct br_doc *d)
  5620. {
  5621. unsigned int i;
  5622. for (i = 0; i < d->nnode; i++)
  5623. d->nodes[i].style_valid = 0;
  5624. }
  5625. /* ---- selector matching ---- */
  5626. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  5627. unsigned int clen)
  5628. {
  5629. const char *s = attr_ptr(d, d->nodes[node].class_off);
  5630. unsigned int i = 0;
  5631. while (s[i]) {
  5632. unsigned int st, n;
  5633. while (s[i] && is_space(s[i]))
  5634. i++;
  5635. st = i;
  5636. while (s[i] && !is_space(s[i]))
  5637. i++;
  5638. n = i - st;
  5639. if (n == clen && ci_eq_n(s + st, cls, n))
  5640. return 1;
  5641. }
  5642. return 0;
  5643. }
  5644. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  5645. unsigned int len)
  5646. {
  5647. const char *s = attr_ptr(d, d->nodes[node].id_off);
  5648. unsigned int i;
  5649. for (i = 0; i < len; i++)
  5650. if (s[i] != id[i])
  5651. return 0;
  5652. return s[len] == '\0';
  5653. }
  5654. static int is_ident(char c)
  5655. {
  5656. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  5657. (c >= '0' && c <= '9') || c == '-' || c == '_';
  5658. }
  5659. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  5660. {
  5661. unsigned int n = 0;
  5662. while (lit[n])
  5663. n++;
  5664. return n == nlen && ci_eq_n(name, lit, nlen);
  5665. }
  5666. static int attr_present(struct br_doc *d, unsigned int node,
  5667. const char *name, unsigned int nlen)
  5668. {
  5669. struct br_node *n = &d->nodes[node];
  5670. if (attr_is(name, nlen, "data-collapse-target"))
  5671. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  5672. if (attr_is(name, nlen, "data-collapse-trigger"))
  5673. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  5674. if (attr_is(name, nlen, "is-open"))
  5675. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  5676. if (attr_is(name, nlen, "disabled"))
  5677. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  5678. if (attr_is(name, nlen, "open"))
  5679. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  5680. if (attr_is(name, nlen, "inert"))
  5681. return (n->attr_flags & BR_ATTR_INERT) != 0;
  5682. if (attr_is(name, nlen, "hidden"))
  5683. return (n->flags & BR_NODE_HIDDEN) != 0;
  5684. if (attr_is(name, nlen, "href"))
  5685. return attr_ptr(d, n->href_off)[0] != 0;
  5686. if (attr_is(name, nlen, "class"))
  5687. return attr_ptr(d, n->class_off)[0] != 0;
  5688. if (attr_is(name, nlen, "id"))
  5689. return attr_ptr(d, n->id_off)[0] != 0;
  5690. if (attr_is(name, nlen, "src"))
  5691. return attr_ptr(d, n->src_off)[0] != 0;
  5692. if (attr_is(name, nlen, "alt"))
  5693. return attr_ptr(d, n->alt_off)[0] != 0;
  5694. if (attr_is(name, nlen, "rel"))
  5695. return attr_ptr(d, n->rel_off)[0] != 0;
  5696. return 0;
  5697. }
  5698. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  5699. unsigned int len)
  5700. {
  5701. unsigned int i = 0;
  5702. int got = 0;
  5703. if (i < len && s[i] == '*') {
  5704. i++;
  5705. got = 1;
  5706. } else if (i < len && is_ident(s[i])) {
  5707. char name[16];
  5708. unsigned int n = 0, tag;
  5709. while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
  5710. name[n++] = s[i++];
  5711. name[n] = '\0';
  5712. tag = br_tag_from_name(name);
  5713. if (tag == BR_TAG_UNKNOWN) {
  5714. const char *raw = attr_ptr(d,
  5715. d->nodes[node].name_off);
  5716. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  5717. !ci_eq_n(raw, name, n) || raw[n] != '\0')
  5718. return 0;
  5719. } else if (tag != d->nodes[node].tag) {
  5720. return 0;
  5721. }
  5722. got = 1;
  5723. }
  5724. while (i < len) {
  5725. if (s[i] == '.') {
  5726. unsigned int st;
  5727. i++;
  5728. st = i;
  5729. while (i < len && is_ident(s[i]))
  5730. i++;
  5731. if (!class_has(d, node, s + st, i - st))
  5732. return 0;
  5733. got = 1;
  5734. } else if (s[i] == '#') {
  5735. unsigned int st;
  5736. i++;
  5737. st = i;
  5738. while (i < len && is_ident(s[i]))
  5739. i++;
  5740. if (!id_eq(d, node, s + st, i - st))
  5741. return 0;
  5742. got = 1;
  5743. } else if (s[i] == '[') {
  5744. unsigned int st, e;
  5745. i++;
  5746. st = e = i;
  5747. while (e < len && s[e] != ']' && s[e] != '=' &&
  5748. !is_space(s[e]))
  5749. e++;
  5750. if (e == st || !attr_present(d, node, s + st, e - st))
  5751. return 0;
  5752. while (i < len && s[i] != ']')
  5753. i++;
  5754. if (i < len)
  5755. i++;
  5756. got = 1;
  5757. } else if (s[i] == ':') {
  5758. if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
  5759. (i + 5 == len || !is_ident(s[i + 5]))) {
  5760. if (d->nodes[node].tag != BR_TAG_HTML)
  5761. return 0;
  5762. i += 5;
  5763. got = 1;
  5764. } else if (i + 5 <= len && ci_eq_n(s + i, ":link", 5) &&
  5765. (i + 5 == len || !is_ident(s[i + 5]))) {
  5766. /* No history state: every link is unvisited. */
  5767. if (d->nodes[node].tag != BR_TAG_A ||
  5768. !d->nodes[node].href_off)
  5769. return 0;
  5770. i += 5;
  5771. got = 1;
  5772. } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
  5773. unsigned int st = i + 5, e = st;
  5774. int depth = 1;
  5775. while (e < len && depth) {
  5776. if (s[e] == '(')
  5777. depth++;
  5778. else if (s[e] == ')')
  5779. depth--;
  5780. if (depth)
  5781. e++;
  5782. }
  5783. if (e >= len)
  5784. return 0;
  5785. for (unsigned int z = st; z < e; z++)
  5786. if (s[z] == ':')
  5787. return 0;
  5788. if (compound_match(d, node, s + st, e - st))
  5789. return 0;
  5790. i = e + 1;
  5791. got = 1;
  5792. } else {
  5793. return 0; /* other pseudo-classes are not modelled */
  5794. }
  5795. } else {
  5796. i++;
  5797. }
  5798. }
  5799. return got;
  5800. }
  5801. static unsigned int selector_specificity(const char *s, unsigned int len)
  5802. {
  5803. unsigned int i, spec = 0;
  5804. for (i = 0; i < len; i++) {
  5805. if (s[i] == '#')
  5806. spec += 100;
  5807. else if (s[i] == '.' || s[i] == '[' ||
  5808. (s[i] == ':' && !(i + 5 <= len &&
  5809. ci_eq_n(s + i, ":root", 5))))
  5810. spec += 10;
  5811. else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
  5812. if (is_ident(s[i]))
  5813. spec += 1;
  5814. }
  5815. }
  5816. return spec;
  5817. }
  5818. /* Match one complex selector; returns specificity or -1. */
  5819. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  5820. unsigned int len, int right_done)
  5821. {
  5822. struct {
  5823. unsigned int start, end;
  5824. int child;
  5825. } parts[8];
  5826. int np = 0;
  5827. unsigned int i = 0;
  5828. int right;
  5829. while (i < len) {
  5830. int child = 0;
  5831. unsigned int st, e;
  5832. while (i < len && is_space(s[i]))
  5833. i++;
  5834. if (i < len && s[i] == '>') {
  5835. child = 1;
  5836. i++;
  5837. while (i < len && is_space(s[i]))
  5838. i++;
  5839. }
  5840. if (i >= len)
  5841. break;
  5842. st = i;
  5843. while (i < len && !is_space(s[i]) && s[i] != '>')
  5844. i++;
  5845. e = i;
  5846. if (np < 8) {
  5847. parts[np].start = st;
  5848. parts[np].end = e;
  5849. parts[np].child = child;
  5850. np++;
  5851. }
  5852. }
  5853. if (!np)
  5854. return -1;
  5855. if (!right_done &&
  5856. !compound_match(d, node, s + parts[np - 1].start,
  5857. parts[np - 1].end - parts[np - 1].start))
  5858. return -1;
  5859. right = np - 1;
  5860. while (right > 0) {
  5861. unsigned int p = d->nodes[node].parent;
  5862. if (!p)
  5863. return -1;
  5864. if (parts[right].child) {
  5865. if (!compound_match(d, p, s + parts[right - 1].start,
  5866. parts[right - 1].end -
  5867. parts[right - 1].start))
  5868. return -1;
  5869. node = p;
  5870. } else {
  5871. while (p && !compound_match(d, p,
  5872. s + parts[right - 1].start,
  5873. parts[right - 1].end -
  5874. parts[right - 1].start))
  5875. p = d->nodes[p].parent;
  5876. if (!p)
  5877. return -1;
  5878. node = p;
  5879. }
  5880. right--;
  5881. }
  5882. return (int)selector_specificity(s, len);
  5883. }
  5884. #define RULE_MATCH_MAX 512
  5885. struct rule_match {
  5886. unsigned short idx;
  5887. short spec;
  5888. };
  5889. static struct rule_match rule_matches[RULE_MATCH_MAX];
  5890. static unsigned int nrule_matches;
  5891. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  5892. {
  5893. struct br_rule *r = &d->rules[idx];
  5894. const char *key = attr_ptr(d, r->key_off);
  5895. int spec, key_ok = 0;
  5896. if (key[0]) {
  5897. if (!compound_match(d, node, key, s_len(key)))
  5898. return;
  5899. key_ok = 1;
  5900. }
  5901. spec = (r->single && key_ok) ? (int)r->spec :
  5902. selector_match(d, node, attr_ptr(d, r->sel_off),
  5903. s_len(attr_ptr(d, r->sel_off)), key_ok);
  5904. if (spec < 0)
  5905. return;
  5906. if (nrule_matches < RULE_MATCH_MAX) {
  5907. rule_matches[nrule_matches].idx = (unsigned short)idx;
  5908. rule_matches[nrule_matches].spec = (short)spec;
  5909. nrule_matches++;
  5910. }
  5911. }
  5912. static void rule_scan_list(struct br_doc *d, unsigned int node,
  5913. unsigned short head)
  5914. {
  5915. while (head != 0xFFFF) {
  5916. rule_try(d, node, head);
  5917. head = d->rule_next[head];
  5918. }
  5919. }
  5920. static void rule_scan_class(struct br_doc *d, unsigned int node,
  5921. const char *cls, unsigned int len)
  5922. {
  5923. if (len)
  5924. rule_scan_list(d, node,
  5925. d->rule_bucket[rule_hash(cls, len)]);
  5926. }
  5927. static void rule_collect(struct br_doc *d, unsigned int node)
  5928. {
  5929. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  5930. const char *id = attr_ptr(d, d->nodes[node].id_off);
  5931. unsigned int tag = d->nodes[node].tag;
  5932. unsigned int i;
  5933. nrule_matches = 0;
  5934. rule_scan_list(d, node, d->rule_any);
  5935. if (tag < BR_TAG_MAX)
  5936. rule_scan_list(d, node, d->rule_tag[tag]);
  5937. if (id[0])
  5938. rule_scan_list(d, node,
  5939. d->rule_bucket[rule_hash(id, s_len(id))]);
  5940. i = 0;
  5941. while (cls[i]) {
  5942. unsigned int st, n;
  5943. while (cls[i] && is_space(cls[i]))
  5944. i++;
  5945. st = i;
  5946. while (cls[i] && !is_space(cls[i]))
  5947. i++;
  5948. n = i - st;
  5949. if (n)
  5950. rule_scan_class(d, node, cls + st, n);
  5951. }
  5952. /* Rules must cascade in document order even though the index visits
  5953. * them in bucket order. */
  5954. for (i = 1; i < nrule_matches; i++) {
  5955. struct rule_match m = rule_matches[i];
  5956. unsigned int j = i;
  5957. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  5958. rule_matches[j] = rule_matches[j - 1];
  5959. j--;
  5960. }
  5961. rule_matches[j] = m;
  5962. }
  5963. }
  5964. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  5965. int font_px)
  5966. {
  5967. if (*kind == 4) {
  5968. *val = (unsigned short)(*val * font_px / 100);
  5969. *kind = 1;
  5970. } else if (*kind == 5) {
  5971. *val = (unsigned short)(*val * root_font_px / 100);
  5972. *kind = 1;
  5973. }
  5974. }
  5975. static void resolve_size_units(struct br_style *st)
  5976. {
  5977. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  5978. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  5979. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  5980. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  5981. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  5982. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  5983. }
  5984. static void set_border_side_w(struct br_style *st, int side, int v)
  5985. {
  5986. if (v < 0)
  5987. v = 0;
  5988. if (v > 32)
  5989. v = 32;
  5990. st->border_w[side] = (unsigned char)v;
  5991. if (st->border_w[side] && !st->border_c[side])
  5992. st->border_c[side] = st->color;
  5993. }
  5994. static void set_border_side_c(struct br_style *st, int side, int v)
  5995. {
  5996. st->border_c[side] = (unsigned short)v;
  5997. }
  5998. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  5999. {
  6000. switch (dcl->prop) {
  6001. case PROP_COLOR:
  6002. st->color = (unsigned short)dcl->val;
  6003. break;
  6004. case PROP_BG:
  6005. st->bg = (unsigned short)dcl->val;
  6006. st->has_bg = 1;
  6007. break;
  6008. case PROP_BG_IMAGE:
  6009. st->bg_image = (unsigned short)dcl->val;
  6010. break;
  6011. case PROP_BG_REPEAT:
  6012. st->bg_repeat = (unsigned char)dcl->val;
  6013. break;
  6014. case PROP_BG_POS_X_KIND:
  6015. st->bg_pos_x_kind = (unsigned char)dcl->val;
  6016. break;
  6017. case PROP_BG_POS_X:
  6018. st->bg_pos_x = (signed short)dcl->val;
  6019. break;
  6020. case PROP_BG_POS_Y_KIND:
  6021. st->bg_pos_y_kind = (unsigned char)dcl->val;
  6022. break;
  6023. case PROP_BG_POS_Y:
  6024. st->bg_pos_y = (signed short)dcl->val;
  6025. break;
  6026. case PROP_BG_SIZE_KIND:
  6027. st->bg_size_kind = (unsigned char)dcl->val;
  6028. break;
  6029. case PROP_BG_SIZE_W:
  6030. st->bg_size_w = (unsigned short)dcl->val;
  6031. break;
  6032. case PROP_BG_SIZE_H:
  6033. st->bg_size_h = (unsigned short)dcl->val;
  6034. break;
  6035. case PROP_FONT_PX:
  6036. st->font_raw = (unsigned short)dcl->val;
  6037. break;
  6038. case PROP_FONT_UNIT:
  6039. st->font_unit = (unsigned char)dcl->val;
  6040. break;
  6041. case PROP_BOLD:
  6042. st->bold = (unsigned char)(dcl->val != 0);
  6043. break;
  6044. case PROP_ITALIC:
  6045. st->italic = (unsigned char)(dcl->val != 0);
  6046. break;
  6047. case PROP_MONO:
  6048. st->mono = (unsigned char)(dcl->val != 0);
  6049. break;
  6050. case PROP_FONT_FACE:
  6051. st->font_face = (unsigned char)dcl->val;
  6052. break;
  6053. case PROP_ALIGN:
  6054. st->align = (unsigned char)dcl->val;
  6055. break;
  6056. case PROP_UNDERLINE:
  6057. st->underline = (unsigned char)(dcl->val != 0);
  6058. break;
  6059. case PROP_STRIKE:
  6060. st->strike = (unsigned char)(dcl->val != 0);
  6061. break;
  6062. case PROP_OVERLINE:
  6063. st->overline = (unsigned char)(dcl->val != 0);
  6064. break;
  6065. case PROP_DISPLAY:
  6066. st->display = (unsigned char)dcl->val;
  6067. st->list_item = 0;
  6068. break;
  6069. case PROP_VIS_HIDDEN:
  6070. st->vis_hidden = (unsigned char)(dcl->val != 0);
  6071. break;
  6072. case PROP_OPACITY_HIDDEN:
  6073. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  6074. break;
  6075. case PROP_OVERFLOW_HIDDEN:
  6076. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  6077. break;
  6078. case PROP_MT: st->mt = dcl->val; break;
  6079. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  6080. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  6081. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  6082. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  6083. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  6084. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  6085. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  6086. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  6087. case PROP_MR: st->mr = dcl->val; break;
  6088. case PROP_MB: st->mb = dcl->val; break;
  6089. case PROP_ML: st->ml = dcl->val; break;
  6090. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  6091. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  6092. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  6093. break;
  6094. case PROP_PT: st->pt = dcl->val; break;
  6095. case PROP_PR: st->pr = dcl->val; break;
  6096. case PROP_PB: st->pb = dcl->val; break;
  6097. case PROP_PL: st->pl = dcl->val; break;
  6098. case PROP_MT_U:
  6099. case PROP_MR_U:
  6100. case PROP_MB_U:
  6101. case PROP_ML_U:
  6102. case PROP_PT_U:
  6103. case PROP_PR_U:
  6104. case PROP_PB_U:
  6105. case PROP_PL_U:
  6106. st->mp_unit[dcl->prop - PROP_MT_U] =
  6107. (unsigned char)dcl->val;
  6108. break;
  6109. case PROP_BORDER_W:
  6110. set_border_side_w(st, 0, dcl->val);
  6111. set_border_side_w(st, 1, dcl->val);
  6112. set_border_side_w(st, 2, dcl->val);
  6113. set_border_side_w(st, 3, dcl->val);
  6114. break;
  6115. case PROP_BORDER_COLOR:
  6116. set_border_side_c(st, 0, dcl->val);
  6117. set_border_side_c(st, 1, dcl->val);
  6118. set_border_side_c(st, 2, dcl->val);
  6119. set_border_side_c(st, 3, dcl->val);
  6120. break;
  6121. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  6122. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  6123. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  6124. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  6125. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  6126. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  6127. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  6128. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  6129. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  6130. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  6131. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  6132. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  6133. case PROP_RADIUS_TL:
  6134. case PROP_RADIUS_TR:
  6135. case PROP_RADIUS_BR:
  6136. case PROP_RADIUS_BL:
  6137. st->radius[dcl->prop - PROP_RADIUS_TL] =
  6138. (unsigned char)dcl->val;
  6139. break;
  6140. case PROP_LIST_NONE:
  6141. st->no_bullet = (unsigned char)(dcl->val != 0);
  6142. break;
  6143. case PROP_LIST_STYLE:
  6144. st->list_style = (unsigned char)dcl->val;
  6145. break;
  6146. case PROP_NOWRAP:
  6147. st->nowrap = (unsigned char)(dcl->val != 0);
  6148. break;
  6149. case PROP_WIDTH:
  6150. st->width = (unsigned short)dcl->val;
  6151. break;
  6152. case PROP_WIDTH_KIND:
  6153. st->width_kind = (unsigned char)dcl->val;
  6154. break;
  6155. case PROP_WIDTH_CALC_PCT:
  6156. st->width_calc_pct = dcl->val;
  6157. break;
  6158. case PROP_WIDTH_CALC_PX:
  6159. st->width_calc_px = dcl->val;
  6160. break;
  6161. case PROP_MAXW:
  6162. st->max_width = (unsigned short)dcl->val;
  6163. break;
  6164. case PROP_MAXW_KIND:
  6165. st->max_width_kind = (unsigned char)dcl->val;
  6166. break;
  6167. case PROP_MAXW_CALC_PCT:
  6168. st->max_width_calc_pct = (signed short)dcl->val;
  6169. break;
  6170. case PROP_MAXW_CALC_PX:
  6171. st->max_width_calc_px = (signed short)dcl->val;
  6172. break;
  6173. case PROP_MINW:
  6174. st->min_width = (unsigned short)dcl->val;
  6175. break;
  6176. case PROP_MINW_KIND:
  6177. st->min_width_kind = (unsigned char)dcl->val;
  6178. break;
  6179. case PROP_MINW_CALC_PCT:
  6180. st->min_width_calc_pct = (signed short)dcl->val;
  6181. break;
  6182. case PROP_MINW_CALC_PX:
  6183. st->min_width_calc_px = (signed short)dcl->val;
  6184. break;
  6185. case PROP_HEIGHT:
  6186. st->height = (unsigned short)dcl->val;
  6187. break;
  6188. case PROP_HEIGHT_KIND:
  6189. st->height_kind = (unsigned char)dcl->val;
  6190. break;
  6191. case PROP_HEIGHT_CALC_PCT:
  6192. st->height_calc_pct = (signed short)dcl->val;
  6193. break;
  6194. case PROP_HEIGHT_CALC_PX:
  6195. st->height_calc_px = (signed short)dcl->val;
  6196. break;
  6197. case PROP_MINH:
  6198. st->min_height = (unsigned short)dcl->val;
  6199. break;
  6200. case PROP_MINH_KIND:
  6201. st->min_height_kind = (unsigned char)dcl->val;
  6202. break;
  6203. case PROP_MINH_CALC_PCT:
  6204. st->min_height_calc_pct = (signed short)dcl->val;
  6205. break;
  6206. case PROP_MINH_CALC_PX:
  6207. st->min_height_calc_px = (signed short)dcl->val;
  6208. break;
  6209. case PROP_MAXH:
  6210. st->max_height = (unsigned short)dcl->val;
  6211. break;
  6212. case PROP_MAXH_KIND:
  6213. st->max_height_kind = (unsigned char)dcl->val;
  6214. break;
  6215. case PROP_MAXH_CALC_PCT:
  6216. st->max_height_calc_pct = (signed short)dcl->val;
  6217. break;
  6218. case PROP_MAXH_CALC_PX:
  6219. st->max_height_calc_px = (signed short)dcl->val;
  6220. break;
  6221. case PROP_LH_PCT:
  6222. st->line_h_pct = (unsigned char)dcl->val;
  6223. break;
  6224. case PROP_LH_PX:
  6225. st->line_h_px = (unsigned short)dcl->val;
  6226. break;
  6227. case PROP_UPPER:
  6228. st->uppercase = (unsigned char)(dcl->val != 0);
  6229. break;
  6230. case PROP_FLOAT:
  6231. st->float_left = (unsigned char)dcl->val;
  6232. break;
  6233. case PROP_CLEAR:
  6234. st->clear = (unsigned char)dcl->val;
  6235. break;
  6236. case PROP_FLEX_GROW:
  6237. st->flex_grow = (unsigned char)dcl->val;
  6238. break;
  6239. case PROP_FLEX_COL:
  6240. st->flex_col = (unsigned char)(dcl->val != 0);
  6241. break;
  6242. case PROP_FLEX_WRAP:
  6243. st->flex_wrap = (unsigned char)(dcl->val != 0);
  6244. break;
  6245. case PROP_JUSTIFY:
  6246. st->justify = (unsigned char)dcl->val;
  6247. break;
  6248. case PROP_ALIGN_ITEMS:
  6249. st->align_items = (unsigned char)dcl->val;
  6250. break;
  6251. case PROP_GRID_NCOLS:
  6252. st->grid_ncols = (unsigned char)dcl->val;
  6253. break;
  6254. case PROP_GRID_GAP:
  6255. st->grid_gap = (unsigned char)dcl->val;
  6256. break;
  6257. case PROP_GRID_SLOT:
  6258. st->grid_slot = (unsigned char)dcl->val;
  6259. break;
  6260. case PROP_GRID_AUTO:
  6261. st->grid_auto = (unsigned char)(dcl->val != 0);
  6262. break;
  6263. case PROP_POS:
  6264. st->pos = (unsigned char)dcl->val;
  6265. break;
  6266. case PROP_LEFT:
  6267. st->left = dcl->val;
  6268. break;
  6269. case PROP_TOP:
  6270. st->top = dcl->val;
  6271. break;
  6272. case PROP_RIGHT:
  6273. st->right = dcl->val;
  6274. break;
  6275. case PROP_BOTTOM:
  6276. st->bottom = dcl->val;
  6277. break;
  6278. case PROP_LEFT_SET:
  6279. st->left_set = (unsigned char)(dcl->val != 0);
  6280. break;
  6281. case PROP_TOP_SET:
  6282. st->top_set = (unsigned char)(dcl->val != 0);
  6283. break;
  6284. case PROP_RIGHT_SET:
  6285. st->right_set = (unsigned char)(dcl->val != 0);
  6286. break;
  6287. case PROP_BOTTOM_SET:
  6288. st->bottom_set = (unsigned char)(dcl->val != 0);
  6289. break;
  6290. case PROP_ZINDEX:
  6291. st->z_index = dcl->val;
  6292. break;
  6293. case PROP_ZSET:
  6294. st->z_set = (unsigned char)(dcl->val != 0);
  6295. break;
  6296. case PROP_TRANSFORM_X:
  6297. st->tx = dcl->val;
  6298. break;
  6299. case PROP_TRANSFORM_Y:
  6300. st->ty = dcl->val;
  6301. break;
  6302. case PROP_TRANSFORM_SET:
  6303. st->transform_set = (unsigned char)(dcl->val != 0);
  6304. break;
  6305. case PROP_TRANSFORM_PAINT:
  6306. st->transform_scale = (unsigned char)(dcl->val != 0);
  6307. break;
  6308. case PROP_SCALE_X:
  6309. st->scale_x = (signed short)dcl->val;
  6310. break;
  6311. case PROP_SCALE_Y:
  6312. st->scale_y = (signed short)dcl->val;
  6313. break;
  6314. case PROP_ROTATE:
  6315. st->rotate_x100 = (signed short)dcl->val;
  6316. break;
  6317. case PROP_ORIGIN_X_KIND:
  6318. st->origin_x_kind = (unsigned char)dcl->val;
  6319. break;
  6320. case PROP_ORIGIN_X:
  6321. st->origin_x = (signed short)dcl->val;
  6322. break;
  6323. case PROP_ORIGIN_Y_KIND:
  6324. st->origin_y_kind = (unsigned char)dcl->val;
  6325. break;
  6326. case PROP_ORIGIN_Y:
  6327. st->origin_y = (signed short)dcl->val;
  6328. break;
  6329. default:
  6330. break;
  6331. }
  6332. }
  6333. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  6334. int spec, short *best, short *best_imp)
  6335. {
  6336. unsigned char p = dcl->prop;
  6337. if (p >= PROP_COUNT)
  6338. return;
  6339. if (dcl->important) {
  6340. if (spec >= best_imp[p]) {
  6341. apply_decl(st, dcl);
  6342. best_imp[p] = (short)spec;
  6343. }
  6344. } else if (best_imp[p] < 0 && spec >= best[p]) {
  6345. apply_decl(st, dcl);
  6346. best[p] = (short)spec;
  6347. }
  6348. }
  6349. void br_style_for(struct br_doc *d, unsigned int node,
  6350. const struct br_style *parent, struct br_style *out)
  6351. {
  6352. unsigned int tag;
  6353. short best[PROP_COUNT], best_imp[PROP_COUNT];
  6354. unsigned int i;
  6355. if (d->nodes[node].style_valid) {
  6356. *out = d->nodes[node].style;
  6357. return;
  6358. }
  6359. *out = *parent;
  6360. out->has_bg = 0;
  6361. out->bg_image = 0;
  6362. out->bg_repeat = 0;
  6363. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  6364. out->bg_pos_x = out->bg_pos_y = 0;
  6365. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  6366. out->float_left = 0;
  6367. out->clear = 0;
  6368. out->list_item = 0;
  6369. out->font_unit = 0;
  6370. for (i = 0; i < 4; i++) {
  6371. out->border_w[i] = 0;
  6372. out->border_c[i] = 0;
  6373. out->border_s[i] = BR_BORDER_SOLID;
  6374. out->radius[i] = 0;
  6375. }
  6376. out->width = out->max_width = out->min_width = out->height = 0;
  6377. out->min_height = out->max_height = 0;
  6378. out->width_calc_pct = out->width_calc_px = 0;
  6379. out->height_calc_pct = out->height_calc_px = 0;
  6380. out->min_height_calc_pct = out->min_height_calc_px = 0;
  6381. out->max_height_calc_pct = out->max_height_calc_px = 0;
  6382. out->min_width_calc_pct = out->min_width_calc_px = 0;
  6383. out->max_width_calc_pct = out->max_width_calc_px = 0;
  6384. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  6385. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  6386. out->box_sizing = 0;
  6387. out->ml_auto = out->mr_auto = 0;
  6388. out->mt = out->mr = out->mb = out->ml = 0;
  6389. out->pt = out->pr = out->pb = out->pl = 0;
  6390. for (i = 0; i < 8; i++) {
  6391. out->mp_unit[i] = 0;
  6392. out->mp_calc_px[i] = 0;
  6393. }
  6394. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  6395. out->justify = out->align_items = 0;
  6396. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  6397. out->grid_auto = 0;
  6398. out->pos = out->left_set = out->top_set = 0;
  6399. out->right_set = out->bottom_set = 0;
  6400. out->left = out->top = out->right = out->bottom = 0;
  6401. out->z_index = 0;
  6402. out->z_set = 0;
  6403. out->tx = out->ty = 0;
  6404. out->transform_set = 0;
  6405. out->transform_scale = 0;
  6406. out->scale_x = out->scale_y = 100;
  6407. out->rotate_x100 = 0;
  6408. out->origin_x = out->origin_y = 50;
  6409. out->origin_x_kind = out->origin_y_kind = 1;
  6410. out->vis_hidden = out->opacity_hidden = 0;
  6411. out->overflow_hidden = 0;
  6412. tag = d->nodes[node].tag;
  6413. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  6414. BR_DISPLAY_INLINE;
  6415. for (i = 0; i < PROP_COUNT; i++) {
  6416. best[i] = -1;
  6417. best_imp[i] = -1;
  6418. }
  6419. switch (tag) {
  6420. case BR_TAG_HEAD:
  6421. case BR_TAG_TITLE:
  6422. case BR_TAG_META:
  6423. case BR_TAG_LINK:
  6424. case BR_TAG_BASE:
  6425. case BR_TAG_SCRIPT:
  6426. case BR_TAG_STYLE:
  6427. out->display = BR_DISPLAY_NONE;
  6428. break;
  6429. case BR_TAG_BODY:
  6430. out->mt = out->mb = out->ml = out->mr = 4;
  6431. break;
  6432. case BR_TAG_P:
  6433. out->mt = out->mb = 8;
  6434. break;
  6435. case BR_TAG_H1:
  6436. out->font_px = 32; out->bold = 1;
  6437. out->mt = 10; out->mb = 6;
  6438. break;
  6439. case BR_TAG_H2:
  6440. out->font_px = 24; out->bold = 1;
  6441. out->mt = 8; out->mb = 4;
  6442. break;
  6443. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  6444. out->bold = 1; out->mt = 6; out->mb = 3;
  6445. if (tag == BR_TAG_H3)
  6446. out->font_px = 19;
  6447. else if (tag == BR_TAG_H4)
  6448. out->font_px = 17;
  6449. else if (tag == BR_TAG_H5)
  6450. out->font_px = 15;
  6451. else
  6452. out->font_px = 13;
  6453. break;
  6454. case BR_TAG_UL: case BR_TAG_OL:
  6455. out->mt = out->mb = 6; out->pl = 20;
  6456. break;
  6457. case BR_TAG_LI:
  6458. out->mt = out->mb = 2;
  6459. out->list_item = 1;
  6460. break;
  6461. case BR_TAG_BLOCKQUOTE:
  6462. out->mt = out->mb = 6; out->ml = out->mr = 16;
  6463. break;
  6464. case BR_TAG_PRE:
  6465. out->mt = out->mb = 8; out->pl = 8;
  6466. out->has_bg = 1; out->bg = theme_panel;
  6467. out->mono = 1;
  6468. break;
  6469. case BR_TAG_CENTER:
  6470. out->align = BR_ALIGN_CENTER;
  6471. break;
  6472. case BR_TAG_A:
  6473. out->color = theme_link; out->underline = 1;
  6474. break;
  6475. case BR_TAG_B: case BR_TAG_STRONG:
  6476. out->bold = 1;
  6477. break;
  6478. case BR_TAG_I: case BR_TAG_EM:
  6479. out->italic = 1;
  6480. break;
  6481. case BR_TAG_U:
  6482. out->underline = 1;
  6483. break;
  6484. case BR_TAG_S: case BR_TAG_STRIKE:
  6485. out->strike = 1;
  6486. break;
  6487. case BR_TAG_CODE:
  6488. out->color = 0x8010;
  6489. out->mono = 1;
  6490. break;
  6491. case BR_TAG_SMALL:
  6492. out->font_px = 13;
  6493. break;
  6494. case BR_TAG_BIG:
  6495. out->font_px = 20;
  6496. break;
  6497. case BR_TAG_TD: case BR_TAG_TH:
  6498. out->pr = 12;
  6499. /* A cell establishes left-aligned content even inside a legacy
  6500. * <center> (HN's outer page wrapper); author CSS/align below
  6501. * can still override this. */
  6502. out->align = BR_ALIGN_LEFT;
  6503. break;
  6504. case BR_TAG_VIDEO:
  6505. out->display = BR_DISPLAY_BLOCK;
  6506. break;
  6507. default:
  6508. break;
  6509. }
  6510. /* Legacy bgcolor/align attributes (tables/cells/body) are
  6511. * presentational hints; author CSS still wins because it is applied
  6512. * below. */
  6513. {
  6514. unsigned short bgc;
  6515. const char *bga = br_find_attr(d, node, "bgcolor");
  6516. const char *ala = br_find_attr(d, node, "align");
  6517. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  6518. out->bg = bgc;
  6519. out->has_bg = 1;
  6520. }
  6521. if (ala) {
  6522. if (same_string(ala, "right"))
  6523. out->align = BR_ALIGN_RIGHT;
  6524. else if (same_string(ala, "center"))
  6525. out->align = BR_ALIGN_CENTER;
  6526. else if (same_string(ala, "left"))
  6527. out->align = BR_ALIGN_LEFT;
  6528. }
  6529. }
  6530. /* presentational <font color>: author CSS below still wins */
  6531. if (d->nodes[node].color_off) {
  6532. unsigned short c;
  6533. const char *v = attr_ptr(d, d->nodes[node].color_off);
  6534. if (parse_color(v, s_len(v), &c))
  6535. out->color = c;
  6536. }
  6537. rule_collect(d, node);
  6538. for (i = 0; i < nrule_matches; i++) {
  6539. struct br_rule *r = &d->rules[rule_matches[i].idx];
  6540. int spec = rule_matches[i].spec;
  6541. unsigned int k;
  6542. for (k = 0; k < r->nd; k++)
  6543. cascade_decl(out, &r->d[k], spec, best, best_imp);
  6544. }
  6545. if (d->nodes[node].style_off) {
  6546. struct br_decl decls[BR_DECLS];
  6547. unsigned int nd, k;
  6548. const char *v = attr_ptr(d, d->nodes[node].style_off);
  6549. parse_decls(d, v, s_len(v), decls, &nd);
  6550. for (k = 0; k < nd; k++)
  6551. cascade_decl(out, &decls[k], 1000, best, best_imp);
  6552. }
  6553. if (out->font_unit) {
  6554. int px = out->font_raw;
  6555. if (out->font_unit == 2)
  6556. px = parent->font_px * out->font_raw / 100;
  6557. else if (out->font_unit == 3)
  6558. px = root_font_px * out->font_raw / 100;
  6559. else if (out->font_unit == 4)
  6560. px = parent->font_px * out->font_raw / 100;
  6561. if (px < 8)
  6562. px = 8;
  6563. if (px > 72)
  6564. px = 72;
  6565. out->font_px = (unsigned char)px;
  6566. }
  6567. resolve_size_units(out);
  6568. if (d->nodes[node].flags & BR_NODE_HIDDEN)
  6569. out->display = BR_DISPLAY_NONE;
  6570. if (out->vis_hidden || out->opacity_hidden ||
  6571. (out->overflow_hidden && out->height_kind == 1 &&
  6572. out->height == 0))
  6573. out->display = BR_DISPLAY_NONE;
  6574. d->nodes[node].style = *out;
  6575. d->nodes[node].style_valid = 1;
  6576. }
  6577. /* ---- HTML parser ---- */
  6578. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  6579. unsigned int sp, int *pre)
  6580. {
  6581. unsigned int i;
  6582. *pre = 0;
  6583. for (i = 1; i <= sp; i++)
  6584. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  6585. (*pre)++;
  6586. }
  6587. static void implicit_close(struct br_doc *d, unsigned short *stack,
  6588. unsigned int *sp, unsigned int tag)
  6589. {
  6590. while (*sp > 0) {
  6591. unsigned int top = d->nodes[stack[*sp]].tag;
  6592. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  6593. (*sp)--;
  6594. continue;
  6595. }
  6596. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  6597. (*sp)--;
  6598. continue;
  6599. }
  6600. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  6601. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  6602. (*sp)--;
  6603. continue;
  6604. }
  6605. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  6606. (*sp)--;
  6607. continue;
  6608. }
  6609. break;
  6610. }
  6611. }
  6612. static int has_attr_token(const char *s, unsigned int len, const char *name)
  6613. {
  6614. unsigned int i, n = s_len(name);
  6615. for (i = 0; i + n <= len; i++) {
  6616. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  6617. continue;
  6618. if (!ci_eq_n(s + i, name, n))
  6619. continue;
  6620. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  6621. return 1;
  6622. }
  6623. return 0;
  6624. }
  6625. static int stack_has_svg(const struct br_doc *d,
  6626. const unsigned short *stack, unsigned int sp)
  6627. {
  6628. unsigned int k;
  6629. for (k = 0; k <= sp && k < 64; k++)
  6630. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  6631. return 1;
  6632. return 0;
  6633. }
  6634. /* Skip an element subtree by finding its closing tag. Used for SVG
  6635. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  6636. * overwrite the document title. */
  6637. static unsigned int skip_element(const char *html, unsigned int len,
  6638. unsigned int i, const char *name,
  6639. unsigned int n)
  6640. {
  6641. char close[40];
  6642. unsigned int k = 0, p;
  6643. close[k++] = '<';
  6644. close[k++] = '/';
  6645. while (k < sizeof(close) - 1 && n--)
  6646. close[k++] = *name++;
  6647. p = (unsigned int)find_ci(html, len, close, k, i);
  6648. if ((int)p < 0)
  6649. return len;
  6650. p += k;
  6651. while (p < len && html[p] != '>')
  6652. p++;
  6653. if (p < len)
  6654. p++;
  6655. return p;
  6656. }
  6657. /* Store a <script src=...> value in the attribute arena. Kept separate from
  6658. * parse_attrs() so script-only attributes cannot be confused with the
  6659. * value/d attributes that also use src_off on other elements. */
  6660. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  6661. unsigned int len)
  6662. {
  6663. unsigned int i = 0;
  6664. while (i < len) {
  6665. unsigned int ns, ne, vs, ve;
  6666. char quote = 0;
  6667. while (i < len && is_space(s[i]))
  6668. i++;
  6669. if (i >= len || s[i] == '/')
  6670. break;
  6671. ns = i;
  6672. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  6673. i++;
  6674. ne = i;
  6675. while (i < len && is_space(s[i]))
  6676. i++;
  6677. if (i >= len || s[i] != '=')
  6678. continue;
  6679. i++;
  6680. while (i < len && is_space(s[i]))
  6681. i++;
  6682. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6683. quote = s[i++];
  6684. vs = i;
  6685. while (i < len && s[i] != quote)
  6686. i++;
  6687. ve = i;
  6688. if (i < len)
  6689. i++;
  6690. } else {
  6691. vs = i;
  6692. while (i < len && !is_space(s[i]) && s[i] != '>')
  6693. i++;
  6694. ve = i;
  6695. }
  6696. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  6697. return store_attr(d, s + vs, ve - vs, 400);
  6698. }
  6699. return 0;
  6700. }
  6701. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  6702. * application/json, text/template, ...) and non-JS type values. External
  6703. * src scripts are allowed; the browser fetches them separately. */
  6704. static int script_is_javascript(const char *s, unsigned int len)
  6705. {
  6706. unsigned int i = 0;
  6707. while (i < len) {
  6708. unsigned int ns, ne, vs = 0, ve = 0;
  6709. char quote = 0;
  6710. int has_val = 0;
  6711. while (i < len && is_space(s[i]))
  6712. i++;
  6713. if (i >= len)
  6714. break;
  6715. ns = i;
  6716. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  6717. i++;
  6718. ne = i;
  6719. while (i < len && is_space(s[i]))
  6720. i++;
  6721. if (i < len && s[i] == '=') {
  6722. i++;
  6723. while (i < len && is_space(s[i]))
  6724. i++;
  6725. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  6726. quote = s[i++];
  6727. vs = i;
  6728. while (i < len && s[i] != quote)
  6729. i++;
  6730. ve = i;
  6731. if (i < len)
  6732. i++;
  6733. } else {
  6734. vs = i;
  6735. while (i < len && !is_space(s[i]) && s[i] != '/')
  6736. i++;
  6737. ve = i;
  6738. }
  6739. has_val = 1;
  6740. } else if (i == ne) {
  6741. i++;
  6742. }
  6743. if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
  6744. unsigned int vn = has_val ? ve - vs : 0;
  6745. if (vn == 0)
  6746. return 1;
  6747. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  6748. (vn == 22 &&
  6749. ci_eq_n(s + vs, "application/javascript", 22)) ||
  6750. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  6751. (vn == 22 &&
  6752. ci_eq_n(s + vs, "application/ecmascript", 22)))
  6753. return 1;
  6754. return 0;
  6755. }
  6756. }
  6757. return 1;
  6758. }
  6759. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  6760. {
  6761. unsigned short stack[64];
  6762. unsigned int sp = 0;
  6763. unsigned int i = 0;
  6764. int pre = 0;
  6765. unsigned int css_len = 0;
  6766. if (!d)
  6767. return -1;
  6768. if (!d->nodes)
  6769. br_doc_init(d);
  6770. if (!d->nodes || !d->text || !d->attrs)
  6771. return -1;
  6772. doc_reset(d);
  6773. d->script_src = html;
  6774. stack[0] = 0;
  6775. d->nodes[0].type = BR_NODE_ELEM;
  6776. d->nodes[0].tag = BR_TAG_UNKNOWN;
  6777. while (i < len) {
  6778. if (html[i] != '<') {
  6779. unsigned int st = i;
  6780. while (i < len && html[i] != '<')
  6781. i++;
  6782. if (sp <= 63)
  6783. append_text(d, stack[sp], html + st, i - st, pre);
  6784. continue;
  6785. }
  6786. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  6787. html[i + 3] == '-') {
  6788. int p = find_ci(html, len, "-->", 3, i + 4);
  6789. i = p >= 0 ? (unsigned int)p + 3 : len;
  6790. continue;
  6791. }
  6792. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  6793. while (i < len && html[i] != '>')
  6794. i++;
  6795. if (i < len)
  6796. i++;
  6797. continue;
  6798. }
  6799. if (i + 1 < len && html[i + 1] == '/') {
  6800. unsigned int ns = i + 2, ne, tag, k, n;
  6801. char name[32];
  6802. while (ns < len && is_space(html[ns]))
  6803. ns++;
  6804. ne = ns;
  6805. while (ne < len && is_ident(html[ne]))
  6806. ne++;
  6807. if (ne == ns) {
  6808. i++;
  6809. continue;
  6810. }
  6811. n = ne - ns;
  6812. if (n >= sizeof(name))
  6813. n = sizeof(name) - 1;
  6814. for (k = 0; k < n; k++)
  6815. name[k] = html[ns + k];
  6816. name[n] = '\0';
  6817. tag = br_tag_from_name(name);
  6818. i = ne;
  6819. while (i < len && html[i] != '>')
  6820. i++;
  6821. if (i < len)
  6822. i++;
  6823. for (k = sp; k > 0; k--) {
  6824. struct br_node *nd = &d->nodes[stack[k]];
  6825. if (tag == BR_TAG_UNKNOWN) {
  6826. const char *raw = attr_ptr(d, nd->name_off);
  6827. if (nd->tag == BR_TAG_UNKNOWN &&
  6828. ci_eq_n(raw, name, n) &&
  6829. raw[n] == '\0') {
  6830. sp = k - 1;
  6831. break;
  6832. }
  6833. } else if (nd->tag == tag) {
  6834. sp = k - 1;
  6835. break;
  6836. }
  6837. }
  6838. stack_recount_pre(d, stack, sp, &pre);
  6839. continue;
  6840. }
  6841. /* start tag */
  6842. {
  6843. unsigned int ns = i + 1, ne, tag, k, n;
  6844. char name[16];
  6845. unsigned int attr_start, attr_end;
  6846. int selfclose = 0;
  6847. while (ns < len && is_space(html[ns]))
  6848. ns++;
  6849. ne = ns;
  6850. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  6851. ne++;
  6852. if (ne == ns) {
  6853. /* stray '<' */
  6854. append_text(d, stack[sp], html + i, 1, pre);
  6855. i++;
  6856. continue;
  6857. }
  6858. n = ne - ns;
  6859. if (n >= sizeof(name))
  6860. n = sizeof(name) - 1;
  6861. for (k = 0; k < n; k++)
  6862. name[k] = html[ns + k];
  6863. name[n] = '\0';
  6864. tag = br_tag_from_name(name);
  6865. attr_start = ne;
  6866. {
  6867. unsigned int q = ne;
  6868. char quote = 0;
  6869. while (q < len) {
  6870. char c = html[q];
  6871. if (quote) {
  6872. if (c == quote)
  6873. quote = 0;
  6874. } else if (c == '"' || c == '\'') {
  6875. quote = c;
  6876. } else if (c == '>') {
  6877. break;
  6878. }
  6879. q++;
  6880. }
  6881. attr_end = q;
  6882. if (q > attr_start && html[q - 1] == '/')
  6883. selfclose = 1;
  6884. i = q < len ? q + 1 : len;
  6885. }
  6886. if (tag == BR_TAG_SCRIPT) {
  6887. int p = find_ci(html, len, "</script", 8, i);
  6888. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6889. unsigned int idx;
  6890. if (script_is_javascript(html + attr_start,
  6891. attr_end - attr_start)) {
  6892. if (d->nscripts < BR_MAX_SCRIPTS) {
  6893. struct br_script *script =
  6894. &d->scripts[d->nscripts++];
  6895. script->off = i;
  6896. script->len = end > i ? end - i : 0;
  6897. script->src_off = script_src_attr(
  6898. d, html + attr_start,
  6899. attr_end - attr_start);
  6900. } else {
  6901. d->scripts_truncated = 1;
  6902. }
  6903. }
  6904. /* Keep the element in the DOM even though its body is
  6905. * executed separately: scripts (Piwik, analytics)
  6906. * insert a sibling after getElementsByTagName('script')
  6907. * [0], and the element must exist for that to work. */
  6908. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  6909. (unsigned char)tag);
  6910. if (idx)
  6911. parse_attrs(d, &d->nodes[idx],
  6912. html + attr_start,
  6913. attr_end - attr_start);
  6914. i = p >= 0 ? (unsigned int)p + 8 : len;
  6915. while (i < len && html[i] != '>')
  6916. i++;
  6917. if (i < len)
  6918. i++;
  6919. continue;
  6920. }
  6921. if (stack_has_svg(d, stack, sp) &&
  6922. (tag == BR_TAG_TITLE ||
  6923. (tag == BR_TAG_UNKNOWN &&
  6924. (ci_eq_n(name, "desc", 4) ||
  6925. ci_eq_n(name, "metadata", 8))))) {
  6926. i = skip_element(html, len, i, name, n);
  6927. continue;
  6928. }
  6929. if (tag == BR_TAG_DETAILS && !selfclose &&
  6930. !has_attr_token(html + attr_start,
  6931. attr_end - attr_start, "open")) {
  6932. /* Closed <details> is collapsed (and this browser has
  6933. * no script to open it); skipping the subtree keeps
  6934. * mobile mega-menus out of the node arena. */
  6935. int p = find_ci(html, len, "</details>", 10, i);
  6936. i = p >= 0 ? (unsigned int)p + 10 : len;
  6937. continue;
  6938. }
  6939. if (tag == BR_TAG_STYLE) {
  6940. int p = find_ci(html, len, "</style", 7, i);
  6941. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6942. unsigned int cend = end;
  6943. unsigned int idx;
  6944. while (cend > i && is_space(html[cend - 1]))
  6945. cend--;
  6946. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  6947. for (k = i; k < cend; k++)
  6948. css_store[css_len++] = html[k];
  6949. css_store[css_len++] = '\n';
  6950. }
  6951. /* The CSS is collected above, but keep a display:none
  6952. * element so getElementsByTagName('style')/parentNode
  6953. * behave like a real document. */
  6954. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  6955. (unsigned char)tag);
  6956. if (idx)
  6957. parse_attrs(d, &d->nodes[idx],
  6958. html + attr_start,
  6959. attr_end - attr_start);
  6960. i = p >= 0 ? (unsigned int)p + 7 : len;
  6961. while (i < len && html[i] != '>')
  6962. i++;
  6963. if (i < len)
  6964. i++;
  6965. continue;
  6966. }
  6967. if (tag == BR_TAG_TITLE) {
  6968. int p = find_ci(html, len, "</title", 7, i);
  6969. unsigned int end = p >= 0 ? (unsigned int)p : len;
  6970. unsigned int tn = 0;
  6971. char tmp[BR_TITLE_MAX];
  6972. tn = decode_text(html + i, end - i, tmp,
  6973. sizeof(tmp), 1);
  6974. (void)tn;
  6975. s_copy(d->title, tmp, sizeof(d->title));
  6976. i = p >= 0 ? (unsigned int)p + 7 : len;
  6977. while (i < len && html[i] != '>')
  6978. i++;
  6979. if (i < len)
  6980. i++;
  6981. continue;
  6982. }
  6983. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  6984. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  6985. unsigned int idx;
  6986. implicit_close(d, stack, &sp, tag);
  6987. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  6988. (unsigned char)tag);
  6989. if (idx)
  6990. parse_attrs(d, &d->nodes[idx],
  6991. html + attr_start,
  6992. attr_end - attr_start);
  6993. continue;
  6994. }
  6995. if (sp >= 63) {
  6996. d->truncated = 1;
  6997. continue;
  6998. }
  6999. implicit_close(d, stack, &sp, tag);
  7000. {
  7001. unsigned int idx = node_append(d, stack[sp],
  7002. BR_NODE_ELEM,
  7003. (unsigned char)tag);
  7004. if (!idx)
  7005. continue;
  7006. parse_attrs(d, &d->nodes[idx],
  7007. html + attr_start,
  7008. attr_end - attr_start);
  7009. if (tag == BR_TAG_UNKNOWN)
  7010. d->nodes[idx].name_off =
  7011. store_attr(d, name, n, 31);
  7012. if (!selfclose) {
  7013. sp++;
  7014. stack[sp] = (unsigned short)idx;
  7015. if (tag == BR_TAG_PRE)
  7016. pre++;
  7017. }
  7018. }
  7019. }
  7020. }
  7021. d->inline_css_len = css_len;
  7022. return 0;
  7023. }
  7024. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  7025. {
  7026. if (!d->nodes)
  7027. br_doc_init(d);
  7028. if (!d->nodes || !d->text || !d->attrs)
  7029. return -1;
  7030. doc_reset(d);
  7031. d->plain = 1;
  7032. d->nodes[0].type = BR_NODE_ELEM;
  7033. d->nodes[0].tag = BR_TAG_UNKNOWN;
  7034. append_text(d, 0, text, len, 1);
  7035. return 0;
  7036. }
  7037. /* A standalone image response: a synthetic document with one <img>. The
  7038. * caller decodes the already-fetched body into image slot 0 and sets the
  7039. * returned node's img_index. */
  7040. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  7041. {
  7042. unsigned int idx;
  7043. if (!d->nodes)
  7044. br_doc_init(d);
  7045. if (!d->nodes || !d->text || !d->attrs)
  7046. return 0;
  7047. doc_reset(d);
  7048. d->nodes[0].type = BR_NODE_ELEM;
  7049. d->nodes[0].tag = BR_TAG_UNKNOWN;
  7050. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  7051. if (!idx)
  7052. return 0;
  7053. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  7054. return idx;
  7055. }