browser_html.c 166 KB

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