browser_html.c 153 KB

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