browser_html.c 249 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902
  1. /* browser_html: see browser_html.h. A deliberately tolerant parser: bad
  2. * nesting is repaired, unknown tags are transparent, and the CSS engine only
  3. * implements the properties in the browser's documented subset. */
  4. #include "browser_html.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. static int is_space(char c);
  8. static int is_ident(char c);
  9. /* ---- storage (one document per process) ----
  10. *
  11. * The node/text/attribute arenas are allocated from the ring-3 heap, not
  12. * static BSS: a large article needs several MB and the process image lives
  13. * in the 4 MB user window. heap_reset() on navigation releases them with
  14. * the rest of the page heap. */
  15. static struct br_rule rule_fallback[BR_RULE_FALLBACK];
  16. static char css_store[BR_CSS_SIZE];
  17. static int root_font_px = 16;
  18. /* Distinct background-image URLs per page (app stores give every card an
  19. * icon through an inline style). */
  20. #define BR_BG_URLS 512
  21. /* Background URLs live in one pool: a data: URL (Infima's menu chevron is
  22. * an inline SVG) is far longer than a fixed slot. */
  23. #define BR_BG_URL_POOL (128 * 1024)
  24. #define BR_BG_URL_MAX 8191
  25. static const char *bg_urls[BR_BG_URLS];
  26. static char bg_url_pool[BR_BG_URL_POOL];
  27. static unsigned int bg_url_pool_used;
  28. static unsigned int bg_url_count;
  29. /* @font-face definitions, one document at a time. The URL is resolved by
  30. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  31. struct br_font_def {
  32. char family[BR_FACE_FAMILY];
  33. char url[BR_FACE_URL];
  34. char base[192];
  35. unsigned char used;
  36. };
  37. static struct br_font_def font_defs[BR_MAX_FACES];
  38. static int font_face_count;
  39. static char font_base[192];
  40. static void font_reset(void)
  41. {
  42. memset(font_defs, 0, sizeof(font_defs));
  43. font_face_count = 0;
  44. font_base[0] = '\0';
  45. }
  46. void br_set_font_base(const char *base)
  47. {
  48. unsigned int i = 0;
  49. if (!base)
  50. base = "";
  51. while (base[i] && i + 1 < sizeof(font_base)) {
  52. font_base[i] = base[i];
  53. i++;
  54. }
  55. font_base[i] = '\0';
  56. }
  57. int br_font_face_count(void)
  58. {
  59. return font_face_count;
  60. }
  61. const char *br_font_face_url(int id)
  62. {
  63. if (id < 1 || id > font_face_count)
  64. return "";
  65. return font_defs[id - 1].url;
  66. }
  67. const char *br_font_face_base(int id)
  68. {
  69. if (id < 1 || id > font_face_count)
  70. return "";
  71. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  72. font_base;
  73. }
  74. const char *br_font_face_family(int id)
  75. {
  76. if (id < 1 || id > font_face_count)
  77. return "";
  78. return font_defs[id - 1].family;
  79. }
  80. const char *br_bg_image_url(unsigned int idx)
  81. {
  82. if (idx >= bg_url_count)
  83. return "";
  84. return bg_urls[idx];
  85. }
  86. unsigned int br_root_font_px(void)
  87. {
  88. return (unsigned int)root_font_px;
  89. }
  90. /* ---- tag table ---- */
  91. struct tag_name {
  92. const char *name;
  93. unsigned char tag;
  94. };
  95. static const struct tag_name tag_names[] = {
  96. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  97. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  98. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  99. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  100. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  101. { "template", BR_TAG_TEMPLATE },
  102. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  103. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  104. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  105. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  106. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  107. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  108. { "picture", BR_TAG_PICTURE },
  109. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  110. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  111. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  112. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  113. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  114. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  115. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  116. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  117. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  118. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  119. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  120. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  121. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  122. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  123. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  124. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  125. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  126. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  127. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  128. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  129. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  130. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  131. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  132. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  133. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  134. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  135. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  136. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  137. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  138. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  139. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  140. { 0, 0 }
  141. };
  142. const char *br_tag_name(unsigned int tag)
  143. {
  144. unsigned int i;
  145. for (i = 0; tag_names[i].name; i++)
  146. if (tag_names[i].tag == tag)
  147. return tag_names[i].name;
  148. return "?";
  149. }
  150. static unsigned int s_len(const char *s);
  151. #define TAG_HASH_SIZE 128
  152. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  153. static int tag_hash_ready;
  154. static unsigned int tag_hash_key(const char *name, unsigned int len)
  155. {
  156. unsigned int i, h = 2166136261u;
  157. for (i = 0; i < len; i++) {
  158. h ^= (unsigned char)name[i];
  159. h *= 16777619u;
  160. }
  161. return h & (TAG_HASH_SIZE - 1);
  162. }
  163. static void tag_hash_build(void)
  164. {
  165. unsigned int i;
  166. for (i = 0; i < TAG_HASH_SIZE; i++)
  167. tag_hash_idx[i] = 0xFF;
  168. for (i = 0; tag_names[i].name; i++) {
  169. unsigned int h = tag_hash_key(tag_names[i].name,
  170. s_len(tag_names[i].name));
  171. while (tag_hash_idx[h] != 0xFF)
  172. h = (h + 1) & (TAG_HASH_SIZE - 1);
  173. tag_hash_idx[h] = (unsigned char)i;
  174. }
  175. tag_hash_ready = 1;
  176. }
  177. unsigned int br_tag_from_name(const char *name)
  178. {
  179. char lower[16];
  180. unsigned int i, len, h;
  181. unsigned char idx;
  182. for (i = 0; i < 15 && name[i]; i++) {
  183. char c = name[i];
  184. if (c >= 'A' && c <= 'Z')
  185. c = (char)(c - 'A' + 'a');
  186. lower[i] = c;
  187. }
  188. lower[i] = '\0';
  189. len = i;
  190. if (!tag_hash_ready)
  191. tag_hash_build();
  192. h = tag_hash_key(lower, len);
  193. for (;;) {
  194. idx = tag_hash_idx[h];
  195. if (idx == 0xFF)
  196. return BR_TAG_UNKNOWN;
  197. {
  198. const char *n = tag_names[idx].name;
  199. unsigned int j;
  200. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  201. ;
  202. if (!lower[j] && !n[j])
  203. return tag_names[idx].tag;
  204. }
  205. h = (h + 1) & (TAG_HASH_SIZE - 1);
  206. }
  207. }
  208. int br_tag_is_void(unsigned int tag)
  209. {
  210. switch (tag) {
  211. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  212. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  213. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  214. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  215. case BR_TAG_AREA: case BR_TAG_COL:
  216. return 1;
  217. default:
  218. return 0;
  219. }
  220. }
  221. int br_tag_is_block(unsigned int tag)
  222. {
  223. switch (tag) {
  224. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  225. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  226. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  227. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  228. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  229. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  230. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  231. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  232. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  233. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  234. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  235. case BR_TAG_PICTURE:
  236. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  237. case BR_TAG_ADDRESS: case BR_TAG_HR:
  238. return 1;
  239. default:
  240. return 0;
  241. }
  242. }
  243. /* ---- string / entity helpers ---- */
  244. static unsigned int s_len(const char *s)
  245. {
  246. unsigned int n = 0;
  247. while (s && s[n])
  248. n++;
  249. return n;
  250. }
  251. static void s_copy(char *d, const char *s, unsigned int cap)
  252. {
  253. unsigned int i = 0;
  254. if (!cap)
  255. return;
  256. while (s && s[i] && i + 1 < cap) {
  257. d[i] = s[i];
  258. i++;
  259. }
  260. d[i] = '\0';
  261. }
  262. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  263. {
  264. unsigned int i;
  265. for (i = 0; i < n; i++) {
  266. char ca = a[i], cb = b[i];
  267. if (ca >= 'A' && ca <= 'Z')
  268. ca = (char)(ca - 'A' + 'a');
  269. if (cb >= 'A' && cb <= 'Z')
  270. cb = (char)(cb - 'A' + 'a');
  271. if (ca != cb)
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. #define CURSOR_CSS_(e, css, file) css,
  277. static const char *const css_cursor_names[CURSOR_COUNT] = {
  278. CURSOR_LIST(CURSOR_CSS_)
  279. };
  280. #undef CURSOR_CSS_
  281. /* CSS `cursor`: the keyword after the last comma (url() images before it
  282. * are not supported, so the fallback keyword is what counts). Returns
  283. * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
  284. * anything else (the declaration is then ignored). */
  285. static int parse_cursor(const char *v, unsigned int n)
  286. {
  287. unsigned int a = 0, b, i;
  288. for (i = 0; i < n; i++)
  289. if (v[i] == ',')
  290. a = i + 1;
  291. while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
  292. a++;
  293. b = a;
  294. while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
  295. (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
  296. b++;
  297. if (b == a)
  298. return -1;
  299. if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
  300. return BR_CURSOR_AUTO;
  301. if (b - a == 4 && ci_eq_n(v + a, "none", 4))
  302. return CURSOR_DEFAULT + 1;
  303. for (i = 0; i < CURSOR_COUNT; i++)
  304. if (strlen(css_cursor_names[i]) == b - a &&
  305. ci_eq_n(v + a, css_cursor_names[i], b - a))
  306. return (int)i + 1;
  307. return -1;
  308. }
  309. static int find_ci(const char *hay, unsigned int len, const char *needle,
  310. unsigned int nlen, unsigned int from)
  311. {
  312. unsigned int i;
  313. if (nlen == 0 || from >= len)
  314. return -1;
  315. for (i = from; i + nlen <= len; i++)
  316. if (ci_eq_n(hay + i, needle, nlen))
  317. return (int)i;
  318. return -1;
  319. }
  320. static const struct {
  321. const char *name;
  322. const char *text;
  323. } entities[] = {
  324. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  325. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  326. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  327. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  328. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  329. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  330. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  331. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  332. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  333. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  334. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  335. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  336. { 0, 0 }
  337. };
  338. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  339. {
  340. if (*n + 1 < cap)
  341. out[(*n)++] = c;
  342. }
  343. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  344. char *out, unsigned int cap, unsigned int *n)
  345. {
  346. unsigned int start = *pos + 1, i = start;
  347. unsigned int code = 0;
  348. int ok = 0;
  349. while (i < len && s[i] != ';' && i - start < 12)
  350. i++;
  351. if (i >= len || s[i] != ';') {
  352. /* Not an entity: emit the '&' and advance past it. Leaving
  353. * *pos unchanged here used to spin forever on bare '&'. */
  354. emit(out, cap, n, '&');
  355. *pos = start;
  356. return;
  357. }
  358. if (s[start] == '#') {
  359. unsigned int j = start + 1;
  360. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  361. j++;
  362. for (; j < i; j++) {
  363. char c = s[j];
  364. int d = -1;
  365. if (c >= '0' && c <= '9') d = c - '0';
  366. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  367. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  368. if (d < 0) break;
  369. code = code * 16 + (unsigned int)d;
  370. ok = 1;
  371. }
  372. } else {
  373. for (; j < i; j++) {
  374. if (s[j] < '0' || s[j] > '9')
  375. break;
  376. code = code * 10 + (unsigned int)(s[j] - '0');
  377. ok = 1;
  378. }
  379. }
  380. } else {
  381. unsigned int k;
  382. for (k = 0; entities[k].name; k++) {
  383. unsigned int el = s_len(entities[k].name);
  384. unsigned int j;
  385. if (el != i - start)
  386. continue;
  387. for (j = 0; j < el; j++)
  388. if (s[start + j] != entities[k].name[j])
  389. break;
  390. if (j == el) {
  391. const char *t = entities[k].text;
  392. while (*t)
  393. emit(out, cap, n, *t++);
  394. *pos = i + 1;
  395. return;
  396. }
  397. }
  398. }
  399. if (ok && code) {
  400. if (code == 0x00A0)
  401. emit(out, cap, n, ' ');
  402. else if ((code >= 0x2010 && code <= 0x2015) ||
  403. code == 0x2212) /* hyphens, dashes */
  404. emit(out, cap, n, '-');
  405. else if (code == 0x2018 || code == 0x2019)
  406. emit(out, cap, n, '\'');
  407. else if (code == 0x201C || code == 0x201D)
  408. emit(out, cap, n, '"');
  409. else if (code == 0x2026) {
  410. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  411. emit(out, cap, n, '.');
  412. } else if (code <= 0xFF) {
  413. emit(out, cap, n, (char)code);
  414. } else {
  415. emit(out, cap, n, '?');
  416. }
  417. } else {
  418. emit(out, cap, n, '?');
  419. }
  420. *pos = i + 1;
  421. }
  422. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  423. * space and drops leading spaces. Returns the output length. */
  424. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  425. unsigned int cap, int collapse)
  426. {
  427. unsigned int i = 0, n = 0;
  428. while (i < len) {
  429. unsigned char c = (unsigned char)s[i];
  430. if (c == '&') {
  431. decode_entity(s, len, &i, out, cap, &n);
  432. continue;
  433. }
  434. if (c < 0x80) {
  435. i++;
  436. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  437. c == '\r' || c == '\f')) {
  438. if (n > 0 && out[n - 1] == ' ')
  439. continue;
  440. emit(out, cap, &n, ' ');
  441. continue;
  442. }
  443. if (!collapse && c == '\t') {
  444. emit(out, cap, &n, ' ');
  445. emit(out, cap, &n, ' ');
  446. emit(out, cap, &n, ' ');
  447. emit(out, cap, &n, ' ');
  448. continue;
  449. }
  450. emit(out, cap, &n, (char)c);
  451. continue;
  452. }
  453. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  454. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  455. unsigned int cp = ((c & 0x1F) << 6) |
  456. ((unsigned char)s[i + 1] & 0x3F);
  457. i += 2;
  458. if (cp == 0xA0)
  459. cp = ' ';
  460. else if (cp == 0xB7)
  461. cp = '-';
  462. else if (cp == 0xD7)
  463. cp = 'x';
  464. else if (cp == 0xB0)
  465. cp = 'o';
  466. if (cp <= 0xFF)
  467. emit(out, cap, &n, (char)cp);
  468. else
  469. emit(out, cap, &n, '?');
  470. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  471. unsigned int cp = ((c & 0x0F) << 12) |
  472. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  473. ((unsigned char)s[i + 2] & 0x3F);
  474. i += 3;
  475. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212) {
  476. emit(out, cap, &n, '-');
  477. } else if (cp == 0x2018 || cp == 0x2019) {
  478. emit(out, cap, &n, '\'');
  479. } else if (cp == 0x201C || cp == 0x201D) {
  480. emit(out, cap, &n, '"');
  481. } else if (cp == 0x2026) {
  482. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  483. emit(out, cap, &n, '.');
  484. } else if (cp == 0x2192) {
  485. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  486. } else if (cp == 0x2264) {
  487. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  488. } else {
  489. emit(out, cap, &n, '?');
  490. }
  491. } else {
  492. i++;
  493. emit(out, cap, &n, '?');
  494. }
  495. }
  496. out[n] = '\0';
  497. return n;
  498. }
  499. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  500. * space / empty. */
  501. /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
  502. * cap silently dropped every class past it (arstechnica's header lost its
  503. * background, alignment and all responsive variants). */
  504. #define BR_CLASS_MAX 2048
  505. #define BR_STYLE_ATTR_MAX 2048
  506. static unsigned int store_attr(struct br_doc *d, const char *s,
  507. unsigned int len, unsigned int limit)
  508. {
  509. unsigned int n;
  510. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  511. return 0;
  512. if (len > limit)
  513. len = limit;
  514. n = decode_text(s, len, d->attrs + d->attrs_used,
  515. BR_ATTR_SIZE - d->attrs_used, 0);
  516. if (n == 0)
  517. return 0;
  518. {
  519. unsigned int off = d->attrs_used;
  520. d->attrs_used += n + 1;
  521. return off;
  522. }
  523. }
  524. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  525. {
  526. return off ? d->attrs + off : "";
  527. }
  528. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  529. static unsigned int store_raw(struct br_doc *d, const char *s,
  530. unsigned int len, unsigned int limit)
  531. {
  532. unsigned int off;
  533. if (len > limit)
  534. len = limit;
  535. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  536. return 0;
  537. off = d->attrs_used;
  538. {
  539. unsigned int i;
  540. for (i = 0; i < len; i++)
  541. d->attrs[off + i] = s[i];
  542. }
  543. d->attrs[off + len] = '\0';
  544. d->attrs_used += len + 1;
  545. return off;
  546. }
  547. #define VAR_HASH_SIZE 1024
  548. static unsigned int var_hash(const char *name, unsigned int len)
  549. {
  550. unsigned int i, h = 2166136261u;
  551. for (i = 0; i < len; i++) {
  552. h ^= (unsigned char)name[i];
  553. h *= 16777619u;
  554. }
  555. return h & (VAR_HASH_SIZE - 1);
  556. }
  557. static unsigned int css_var_find(struct br_doc *d, const char *name,
  558. unsigned int nlen)
  559. {
  560. unsigned int h = var_hash(name, nlen);
  561. for (;;) {
  562. unsigned int i = d->var_slot[h];
  563. if (!i)
  564. return BR_MAX_VARS;
  565. i--;
  566. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  567. const char *n = attr_ptr(d, d->var_name[i]);
  568. unsigned int j;
  569. for (j = 0; j < nlen && n[j] == name[j]; j++)
  570. ;
  571. if (j == nlen)
  572. return i;
  573. }
  574. h = (h + 1) & (VAR_HASH_SIZE - 1);
  575. }
  576. }
  577. #define BR_VAR_ROOT 1
  578. #define BR_VAR_CONFLICT 2
  579. #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
  580. /* Custom properties live in one document-wide table (var() is expanded
  581. * when a sheet is parsed, not per element). Definitions on :root, html,
  582. * body or * are the theme and always apply, last one winning. A definition
  583. * on any other selector is a local override: it is used while it is the
  584. * only value seen for that name, and a second, different local value marks
  585. * the name ambiguous so var() falls back. Last-wins across components was
  586. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  587. * .fa-fw, and every icon became 100% wide. */
  588. static void css_var_set(struct br_doc *d, const char *name,
  589. unsigned int nlen, const char *val, unsigned int vlen,
  590. int root)
  591. {
  592. unsigned int i, noff, voff;
  593. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  594. return;
  595. i = css_var_find(d, name, nlen);
  596. if (i < BR_MAX_VARS && !root) {
  597. const char *old = attr_ptr(d, d->var_val[i]);
  598. unsigned int k = 0;
  599. d->var_flags[i] |= BR_VAR_SCOPED;
  600. if (d->var_flags[i] & BR_VAR_ROOT)
  601. return;
  602. while (k < vlen && old[k] == val[k])
  603. k++;
  604. if (!(k == vlen && old[k] == '\0'))
  605. d->var_flags[i] |= BR_VAR_CONFLICT;
  606. return;
  607. }
  608. noff = store_raw(d, name, nlen, 63);
  609. voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
  610. if (!noff || !voff)
  611. return;
  612. if (i >= BR_MAX_VARS) {
  613. unsigned int h;
  614. if (d->nvars >= BR_MAX_VARS)
  615. return;
  616. i = d->nvars++;
  617. d->var_name[i] = noff;
  618. h = var_hash(name, nlen);
  619. while (d->var_slot[h])
  620. h = (h + 1) & (VAR_HASH_SIZE - 1);
  621. d->var_slot[h] = (unsigned short)(i + 1);
  622. d->var_flags[i] = 0;
  623. }
  624. d->var_val[i] = voff;
  625. if (root)
  626. d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
  627. (d->var_flags[i] & BR_VAR_SCOPED));
  628. else
  629. d->var_flags[i] |= BR_VAR_SCOPED;
  630. }
  631. /* 1 when every selector of the list targets the document root (the theme
  632. * scope for custom properties): :root, html, body, * or :host, optionally
  633. * with classes/attributes but no combinator. */
  634. static int css_root_selector(const char *s, unsigned int len)
  635. {
  636. unsigned int i = 0;
  637. while (i < len) {
  638. unsigned int st, en;
  639. while (i < len && (is_space(s[i]) || s[i] == ','))
  640. i++;
  641. if (i >= len)
  642. break;
  643. st = i;
  644. while (i < len && s[i] != ',')
  645. i++;
  646. en = i;
  647. while (en > st && is_space(s[en - 1]))
  648. en--;
  649. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  650. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  651. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  652. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  653. s[st] == '*'))
  654. return 0;
  655. for (; st < en; st++)
  656. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  657. s[st] == '~')
  658. return 0;
  659. }
  660. return 1;
  661. }
  662. /* Find the end of a declaration value: the next ';' outside quotes and
  663. * parentheses. The caller passes the position just after the ':'. */
  664. static unsigned int css_value_end(const char *s, unsigned int len,
  665. unsigned int i)
  666. {
  667. int depth = 0;
  668. char quote = 0;
  669. while (i < len) {
  670. char c = s[i];
  671. if (quote) {
  672. if (c == quote)
  673. quote = 0;
  674. else if (c == '\\' && i + 1 < len)
  675. i++;
  676. } else if (c == '"' || c == '\'') {
  677. quote = c;
  678. } else if (c == '(') {
  679. depth++;
  680. } else if (c == ')' && depth > 0) {
  681. depth--;
  682. } else if (c == ';' && depth == 0) {
  683. break;
  684. }
  685. i++;
  686. }
  687. return i;
  688. }
  689. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  690. * second palette. We render the light page, so skip their custom-property
  691. * definitions; otherwise their last-wins values leak into the light rules. */
  692. static int css_dark_selector(const char *s, unsigned int len)
  693. {
  694. unsigned int i;
  695. for (i = 0; i + 4 <= len; i++) {
  696. if ((s[i] == 'n' || s[i] == 'N') &&
  697. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  698. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  699. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  700. (s[i + 4] == 't' || s[i + 4] == 'T'))
  701. return 1;
  702. if ((s[i] == 'd' || s[i] == 'D') &&
  703. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  704. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  705. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  706. return 1;
  707. if (i + 12 <= len &&
  708. (s[i] == 'c' || s[i] == 'C') &&
  709. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  710. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  711. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  712. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  713. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  714. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  715. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  716. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  717. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  718. s[i + 10] == '-' &&
  719. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  720. (s[i + 12] == 's' || s[i + 12] == 'S'))
  721. return 1;
  722. }
  723. return 0;
  724. }
  725. /* Collect --custom-property definitions from one declaration block. */
  726. static void css_collect_block(struct br_doc *d, const char *s,
  727. unsigned int len, int root)
  728. {
  729. unsigned int i = 0;
  730. while (i < len) {
  731. unsigned int pstart, pend, vstart, vend;
  732. while (i < len && (is_space(s[i]) || s[i] == ';'))
  733. i++;
  734. if (i >= len)
  735. break;
  736. pstart = i;
  737. while (i < len && s[i] != ':' && s[i] != ';')
  738. i++;
  739. pend = i;
  740. if (i >= len || s[i] != ':') {
  741. if (i < len)
  742. i++;
  743. continue;
  744. }
  745. i++;
  746. vstart = i;
  747. i = css_value_end(s, len, i);
  748. vend = i;
  749. if (i < len)
  750. i++;
  751. while (pstart < pend && is_space(s[pstart]))
  752. pstart++;
  753. while (pend > pstart && is_space(s[pend - 1]))
  754. pend--;
  755. while (vstart < vend && is_space(s[vstart]))
  756. vstart++;
  757. while (vend > vstart && is_space(s[vend - 1]))
  758. vend--;
  759. if (vend - vstart >= 10 &&
  760. ci_eq_n(s + vend - 10, "!important", 10)) {
  761. vend -= 10;
  762. while (vend > vstart && is_space(s[vend - 1]))
  763. vend--;
  764. }
  765. if (pend - pstart >= 3 && s[pstart] == '-' &&
  766. s[pstart + 1] == '-')
  767. css_var_set(d, s + pstart, pend - pstart,
  768. s + vstart, vend - vstart, root);
  769. }
  770. }
  771. const char *br_href(struct br_doc *d, unsigned int node)
  772. {
  773. return attr_ptr(d, d->nodes[node].href_off);
  774. }
  775. const char *br_src(struct br_doc *d, unsigned int node)
  776. {
  777. return attr_ptr(d, d->nodes[node].src_off);
  778. }
  779. const char *br_alt(struct br_doc *d, unsigned int node)
  780. {
  781. return attr_ptr(d, d->nodes[node].alt_off);
  782. }
  783. const char *br_rel(struct br_doc *d, unsigned int node)
  784. {
  785. return attr_ptr(d, d->nodes[node].rel_off);
  786. }
  787. const char *br_value(struct br_doc *d, unsigned int node)
  788. {
  789. return attr_ptr(d, d->nodes[node].src_off);
  790. }
  791. const char *br_placeholder(struct br_doc *d, unsigned int node)
  792. {
  793. return attr_ptr(d, d->nodes[node].alt_off);
  794. }
  795. const char *br_type(struct br_doc *d, unsigned int node)
  796. {
  797. return attr_ptr(d, d->nodes[node].rel_off);
  798. }
  799. /* ---- document setup / node append ---- */
  800. static void rule_index_reset(struct br_doc *d)
  801. {
  802. unsigned int i;
  803. for (i = 0; i < BR_MAX_RULES; i++)
  804. d->rule_next[i] = 0xFFFF;
  805. for (i = 0; i < BR_RULE_HASH; i++)
  806. d->rule_bucket[i] = 0xFFFF;
  807. for (i = 0; i < BR_TAG_MAX; i++)
  808. d->rule_tag[i] = 0xFFFF;
  809. d->rule_any = 0xFFFF;
  810. }
  811. void br_doc_init(struct br_doc *d)
  812. {
  813. unsigned int i;
  814. for (i = 0; i < sizeof(*d); i++)
  815. ((unsigned char *)d)[i] = 0;
  816. rule_index_reset(d);
  817. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  818. d->text = malloc(BR_TEXT_SIZE);
  819. d->attrs = malloc(BR_ATTR_SIZE);
  820. if (!d->nodes || !d->text || !d->attrs) {
  821. free(d->nodes);
  822. free(d->text);
  823. free(d->attrs);
  824. d->nodes = 0;
  825. d->text = 0;
  826. d->attrs = 0;
  827. return;
  828. }
  829. d->rules = rule_fallback;
  830. d->rule_cap = BR_RULE_FALLBACK;
  831. d->attrs_used = 1; /* offset 0 = "no attribute" */
  832. d->attrs[0] = '\0';
  833. d->nvars = 0;
  834. d->nlayers = 0;
  835. d->root = 0;
  836. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  837. d->var_slot[vi] = 0;
  838. root_font_px = 16;
  839. bg_url_count = 0;
  840. bg_url_pool_used = 0;
  841. font_reset();
  842. }
  843. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  844. unsigned int cap)
  845. {
  846. if (!buf || !cap) {
  847. d->rules = rule_fallback;
  848. d->rule_cap = BR_RULE_FALLBACK;
  849. d->nrules = 0;
  850. return;
  851. }
  852. if (cap > BR_MAX_RULES)
  853. cap = BR_MAX_RULES;
  854. d->rules = buf;
  855. d->rule_cap = cap;
  856. d->nrules = 0;
  857. }
  858. static void doc_reset(struct br_doc *d)
  859. {
  860. unsigned int i;
  861. d->nnode = 0;
  862. d->text_used = 0;
  863. d->attrs_used = 1;
  864. d->attrs[0] = '\0';
  865. d->nrules = 0;
  866. d->ncontent = 0;
  867. d->npe_rules = 0;
  868. rule_index_reset(d);
  869. d->title[0] = '\0';
  870. d->inline_css_len = 0;
  871. d->nvars = 0;
  872. d->nlayers = 0;
  873. d->truncated = 0;
  874. d->plain = 0;
  875. d->script_src = 0;
  876. d->nscripts = 0;
  877. d->scripts_truncated = 0;
  878. font_reset();
  879. for (i = 0; i < BR_MAX_NODES; i++) {
  880. d->nodes[i].type = BR_NODE_ELEM;
  881. d->nodes[i].tag = BR_TAG_UNKNOWN;
  882. d->nodes[i].flags = 0;
  883. d->nodes[i].parent = 0;
  884. d->nodes[i].first = d->nodes[i].last = 0;
  885. d->nodes[i].next = 0;
  886. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  887. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  888. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  889. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  890. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  891. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  892. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  893. d->nodes[i].name_off = 0;
  894. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  895. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  896. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  897. d->nodes[i].svg_rx = 0;
  898. d->nodes[i].img_index = -1;
  899. d->nodes[i].bg_img_index = -1;
  900. d->nodes[i].style_valid = 0;
  901. d->nodes[i].pe_state = 0;
  902. }
  903. }
  904. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  905. unsigned char type, unsigned char tag)
  906. {
  907. unsigned int idx;
  908. struct br_node *n;
  909. if (d->nnode >= BR_MAX_NODES) {
  910. d->truncated = 1;
  911. return 0;
  912. }
  913. idx = d->nnode++;
  914. n = &d->nodes[idx];
  915. n->type = type;
  916. n->tag = tag;
  917. n->parent = (unsigned short)parent;
  918. n->img_index = -1;
  919. n->bg_img_index = -1;
  920. if (d->nodes[parent].last)
  921. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  922. else
  923. d->nodes[parent].first = (unsigned short)idx;
  924. d->nodes[parent].last = (unsigned short)idx;
  925. return idx;
  926. }
  927. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  928. const char *s, unsigned int len, int pre)
  929. {
  930. char *dst;
  931. unsigned int n;
  932. unsigned int idx;
  933. if (len == 0)
  934. return 0;
  935. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  936. d->truncated = 1;
  937. return 0;
  938. }
  939. dst = d->text + d->text_used;
  940. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  941. if (n == 0) {
  942. d->text[d->text_used] = '\0';
  943. return 0;
  944. }
  945. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  946. if (!idx)
  947. return 0;
  948. d->nodes[idx].text_off = d->text_used;
  949. d->nodes[idx].text_len = (unsigned short)n;
  950. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  951. d->text_used += n + 1;
  952. return idx;
  953. }
  954. /* Raw text child (no entity decoding, whitespace kept): the body of a
  955. * data-block <script> such as type="application/json", which scripts read
  956. * through textContent. Bodies longer than a text node holds are dropped. */
  957. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  958. const char *s, unsigned int len)
  959. {
  960. unsigned int idx, i;
  961. if (len == 0 || len > 0xFFFFu)
  962. return 0;
  963. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  964. d->truncated = 1;
  965. return 0;
  966. }
  967. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  968. if (!idx)
  969. return 0;
  970. for (i = 0; i < len; i++)
  971. d->text[d->text_used + i] = s[i];
  972. d->text[d->text_used + len] = '\0';
  973. d->nodes[idx].text_off = d->text_used;
  974. d->nodes[idx].text_len = (unsigned short)len;
  975. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  976. d->text_used += len + 1;
  977. return idx;
  978. }
  979. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  980. static int same_string(const char *a, const char *b)
  981. {
  982. while (*a && *a == *b) {
  983. a++;
  984. b++;
  985. }
  986. return *a == *b;
  987. }
  988. static int same_string_n(const char *a, const char *b, unsigned int n)
  989. {
  990. unsigned int i;
  991. for (i = 0; i < n; i++)
  992. if (a[i] != b[i])
  993. return 0;
  994. return 1;
  995. }
  996. static int node_detached(struct br_doc *d, unsigned int node)
  997. {
  998. unsigned int guard = 0;
  999. while (node && node < d->nnode && guard++ <= d->nnode) {
  1000. if (d->nodes[node].flags & BR_NODE_DETACHED)
  1001. return 1;
  1002. node = d->nodes[node].parent;
  1003. }
  1004. return 0;
  1005. }
  1006. unsigned int br_find_id(struct br_doc *d, const char *id)
  1007. {
  1008. unsigned int i;
  1009. if (!d || !id)
  1010. return 0;
  1011. for (i = 0; i < d->nnode; i++) {
  1012. struct br_node *n = &d->nodes[i];
  1013. if (n->type == BR_NODE_ELEM && n->id_off &&
  1014. same_string(d->attrs + n->id_off, id) &&
  1015. !node_detached(d, i))
  1016. return i + 1;
  1017. }
  1018. return 0;
  1019. }
  1020. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1021. unsigned int cap, unsigned int *used)
  1022. {
  1023. unsigned int c;
  1024. if (node >= d->nnode)
  1025. return;
  1026. if (d->nodes[node].type == BR_NODE_TEXT) {
  1027. unsigned int len = d->nodes[node].text_len;
  1028. unsigned int off = d->nodes[node].text_off;
  1029. if (*used + len >= cap)
  1030. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1031. for (c = 0; c < len; c++)
  1032. out[*used + c] = d->text[off + c];
  1033. *used += len;
  1034. return;
  1035. }
  1036. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1037. text_walk(d, c, out, cap, used);
  1038. }
  1039. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1040. char *out, unsigned int cap)
  1041. {
  1042. unsigned int used = 0;
  1043. if (!d || !out || cap == 0 || node >= d->nnode)
  1044. return 0;
  1045. text_walk(d, node, out, cap, &used);
  1046. out[used] = '\0';
  1047. return used;
  1048. }
  1049. int br_set_text_content(struct br_doc *d, unsigned int node,
  1050. const char *text, unsigned int len)
  1051. {
  1052. struct br_node *n;
  1053. unsigned int idx;
  1054. unsigned int i;
  1055. if (!d || node >= d->nnode)
  1056. return -1;
  1057. /* Detach the old subtree; its arena storage is reclaimed on the next
  1058. * navigation. */
  1059. {
  1060. unsigned int c;
  1061. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1062. d->nodes[c].flags |= BR_NODE_DETACHED;
  1063. }
  1064. d->nodes[node].first = d->nodes[node].last = 0;
  1065. d->nodes[node].style_valid = 0;
  1066. d->nodes[node].pe_state = 0;
  1067. if (len == 0)
  1068. return 0;
  1069. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1070. d->truncated = 1;
  1071. return -1;
  1072. }
  1073. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1074. if (!idx)
  1075. return -1;
  1076. for (i = 0; i < len; i++)
  1077. d->text[d->text_used + i] = text[i];
  1078. d->text[d->text_used + len] = '\0';
  1079. n = &d->nodes[idx];
  1080. n->text_off = d->text_used;
  1081. n->text_len = (unsigned short)len;
  1082. n->flags = 0;
  1083. d->text_used += len + 1;
  1084. return 0;
  1085. }
  1086. /* Replace the value of an existing text or comment node in place, keeping the
  1087. * node handle (and therefore the JS wrapper) valid. */
  1088. int br_set_text_node(struct br_doc *d, unsigned int node,
  1089. const char *text, unsigned int len)
  1090. {
  1091. struct br_node *n;
  1092. unsigned int i;
  1093. if (!d || node >= d->nnode)
  1094. return -1;
  1095. n = &d->nodes[node];
  1096. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1097. return -1;
  1098. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1099. d->truncated = 1;
  1100. return -1;
  1101. }
  1102. for (i = 0; i < len; i++)
  1103. d->text[d->text_used + i] = text[i];
  1104. d->text[d->text_used + len] = '\0';
  1105. n->text_off = d->text_used;
  1106. n->text_len = (unsigned short)len;
  1107. d->text_used += len + 1;
  1108. return 0;
  1109. }
  1110. /* ---- runtime DOM construction and mutation ---- */
  1111. static unsigned int text_len(const char *s)
  1112. {
  1113. unsigned int n = 0;
  1114. while (s && s[n])
  1115. n++;
  1116. return n;
  1117. }
  1118. static void node_zero(struct br_node *n)
  1119. {
  1120. unsigned int i;
  1121. for (i = 0; i < sizeof(*n); i++)
  1122. ((unsigned char *)n)[i] = 0;
  1123. n->img_index = -1;
  1124. n->bg_img_index = -1;
  1125. }
  1126. static void node_unlink(struct br_doc *d, unsigned int handle)
  1127. {
  1128. unsigned int node, parent, c, prev = 0;
  1129. if (!d || handle == 0 || handle > d->nnode)
  1130. return;
  1131. node = handle - 1;
  1132. parent = d->nodes[node].parent;
  1133. if (parent >= d->nnode) {
  1134. d->nodes[node].parent = 0;
  1135. d->nodes[node].next = 0;
  1136. return;
  1137. }
  1138. c = d->nodes[parent].first;
  1139. while (c && c != node) {
  1140. prev = c;
  1141. c = d->nodes[c].next;
  1142. }
  1143. if (c == node) {
  1144. if (prev)
  1145. d->nodes[prev].next = d->nodes[node].next;
  1146. else
  1147. d->nodes[parent].first = d->nodes[node].next;
  1148. if (d->nodes[parent].last == node)
  1149. d->nodes[parent].last = (unsigned short)prev;
  1150. }
  1151. d->nodes[node].parent = 0;
  1152. d->nodes[node].next = 0;
  1153. d->nodes[node].flags |= BR_NODE_DETACHED;
  1154. }
  1155. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1156. const char *s, unsigned int len);
  1157. static unsigned int find_tag_end(const char *s, unsigned int i,
  1158. unsigned int len)
  1159. {
  1160. char quote = 0;
  1161. while (i < len) {
  1162. char c = s[i];
  1163. if (quote) {
  1164. if (c == quote)
  1165. quote = 0;
  1166. } else if (c == '"' || c == '\'') {
  1167. quote = c;
  1168. } else if (c == '>') {
  1169. return i;
  1170. }
  1171. i++;
  1172. }
  1173. return len;
  1174. }
  1175. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1176. const char *html, unsigned int len)
  1177. {
  1178. unsigned int stack[64];
  1179. unsigned int sp = 1, i = 0;
  1180. if (!d || node >= d->nnode)
  1181. return -1;
  1182. stack[0] = node;
  1183. {
  1184. unsigned int c;
  1185. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1186. d->nodes[c].flags |= BR_NODE_DETACHED;
  1187. }
  1188. d->nodes[node].first = d->nodes[node].last = 0;
  1189. d->nodes[node].style_valid = 0;
  1190. d->nodes[node].pe_state = 0;
  1191. while (i < len) {
  1192. if (html[i] != '<') {
  1193. unsigned int start = i, end;
  1194. while (i < len && html[i] != '<')
  1195. i++;
  1196. end = i;
  1197. if (end > start) {
  1198. /* Entities decoded like the page parser does
  1199. * (createTextNode stays literal). */
  1200. char *tb = (char *)malloc(end - start + 1);
  1201. unsigned int tl = 0, h = 0;
  1202. if (tb) {
  1203. tl = decode_text(html + start, end - start,
  1204. tb, end - start + 1, 1);
  1205. h = br_create_text(d, tb, tl);
  1206. free(tb);
  1207. }
  1208. if (h)
  1209. br_append_child(d, stack[sp - 1], h);
  1210. }
  1211. continue;
  1212. }
  1213. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1214. html[i + 3] == '-') {
  1215. int p = find_ci(html, len, "-->", 3, i + 4);
  1216. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1217. unsigned int h = br_create_comment(d, html + i + 4,
  1218. end > i + 4 ?
  1219. end - i - 4 : 0);
  1220. if (h)
  1221. br_append_child(d, stack[sp - 1], h);
  1222. i = p >= 0 ? (unsigned int)p + 3 : len;
  1223. continue;
  1224. }
  1225. if (html[i + 1] == '/') {
  1226. unsigned int p = find_tag_end(html, i + 2, len);
  1227. i = p < len ? p + 1 : len;
  1228. if (sp > 1)
  1229. sp--;
  1230. continue;
  1231. }
  1232. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1233. unsigned int p = find_tag_end(html, i + 2, len);
  1234. i = p < len ? p + 1 : len;
  1235. continue;
  1236. }
  1237. {
  1238. unsigned int p = find_tag_end(html, i + 1, len);
  1239. unsigned int ns = i + 1, ne, as, ae;
  1240. unsigned int h, k;
  1241. char name[32];
  1242. int selfclose = 0;
  1243. while (ns < p && is_space(html[ns]))
  1244. ns++;
  1245. ne = ns;
  1246. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1247. ne++;
  1248. as = ne;
  1249. ae = p;
  1250. if (ae > as && html[ae - 1] == '/') {
  1251. selfclose = 1;
  1252. ae--;
  1253. }
  1254. if (ne == ns) {
  1255. i = p < len ? p + 1 : len;
  1256. continue;
  1257. }
  1258. if (ne - ns >= sizeof(name)) {
  1259. i = p < len ? p + 1 : len;
  1260. continue;
  1261. }
  1262. for (k = 0; k < ne - ns; k++)
  1263. name[k] = html[ns + k];
  1264. name[ne - ns] = '\0';
  1265. h = br_create_element(d, name, ne - ns);
  1266. if (!h) {
  1267. i = p < len ? p + 1 : len;
  1268. continue;
  1269. }
  1270. if (ae > as)
  1271. parse_attrs(d, &d->nodes[h - 1], html + as,
  1272. ae - as);
  1273. br_append_child(d, stack[sp - 1], h);
  1274. {
  1275. unsigned int tag = d->nodes[h - 1].tag;
  1276. if (!selfclose && !br_tag_is_void(tag) &&
  1277. sp < sizeof(stack) / sizeof(stack[0]))
  1278. stack[sp++] = h - 1;
  1279. }
  1280. i = p < len ? p + 1 : len;
  1281. }
  1282. }
  1283. return 0;
  1284. }
  1285. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1286. {
  1287. unsigned int i;
  1288. if (!d)
  1289. return 0;
  1290. for (i = 0; i < d->nnode; i++)
  1291. if (d->nodes[i].type == BR_NODE_ELEM &&
  1292. d->nodes[i].tag == (unsigned char)tag &&
  1293. !node_detached(d, i))
  1294. return i + 1;
  1295. return 0;
  1296. }
  1297. unsigned int br_create_element(struct br_doc *d, const char *name,
  1298. unsigned int len)
  1299. {
  1300. struct br_node *n;
  1301. unsigned int idx;
  1302. if (!d || !name)
  1303. return 0;
  1304. if (d->nnode >= BR_MAX_NODES) {
  1305. d->truncated = 1;
  1306. return 0;
  1307. }
  1308. idx = d->nnode++;
  1309. n = &d->nodes[idx];
  1310. node_zero(n);
  1311. n->type = BR_NODE_ELEM;
  1312. n->flags = BR_NODE_DETACHED;
  1313. n->tag = (unsigned char)br_tag_from_name(name);
  1314. if (n->tag == BR_TAG_UNKNOWN && len)
  1315. n->name_off = store_attr(d, name, len, 31);
  1316. return idx + 1;
  1317. }
  1318. unsigned int br_create_text(struct br_doc *d, const char *text,
  1319. unsigned int len)
  1320. {
  1321. struct br_node *n;
  1322. unsigned int idx, i;
  1323. if (!d)
  1324. return 0;
  1325. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1326. d->truncated = 1;
  1327. return 0;
  1328. }
  1329. idx = d->nnode++;
  1330. n = &d->nodes[idx];
  1331. node_zero(n);
  1332. n->type = BR_NODE_TEXT;
  1333. n->flags = BR_NODE_DETACHED;
  1334. n->text_off = d->text_used;
  1335. n->text_len = (unsigned short)len;
  1336. for (i = 0; i < len; i++)
  1337. d->text[d->text_used + i] = text[i];
  1338. d->text[d->text_used + len] = '\0';
  1339. d->text_used += len + 1;
  1340. return idx + 1;
  1341. }
  1342. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1343. unsigned int len)
  1344. {
  1345. unsigned int h = br_create_text(d, text, len);
  1346. if (h)
  1347. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1348. return h;
  1349. }
  1350. unsigned int br_create_fragment(struct br_doc *d)
  1351. {
  1352. struct br_node *n;
  1353. unsigned int idx;
  1354. if (!d)
  1355. return 0;
  1356. if (d->nnode >= BR_MAX_NODES) {
  1357. d->truncated = 1;
  1358. return 0;
  1359. }
  1360. idx = d->nnode++;
  1361. n = &d->nodes[idx];
  1362. node_zero(n);
  1363. n->type = BR_NODE_FRAGMENT;
  1364. n->flags = BR_NODE_DETACHED;
  1365. return idx + 1;
  1366. }
  1367. int br_append_child(struct br_doc *d, unsigned int parent,
  1368. unsigned int child)
  1369. {
  1370. unsigned int p;
  1371. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1372. parent == child - 1)
  1373. return -1;
  1374. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1375. /* Appending a fragment inserts its children, not the fragment. */
  1376. unsigned int c = d->nodes[child - 1].first;
  1377. while (c) {
  1378. unsigned int next = d->nodes[c].next;
  1379. br_append_child(d, parent, c + 1);
  1380. c = next;
  1381. }
  1382. return 0;
  1383. }
  1384. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1385. if (p == child - 1)
  1386. return -1;
  1387. node_unlink(d, child);
  1388. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1389. d->nodes[child - 1].parent = (unsigned short)parent;
  1390. d->nodes[child - 1].next = 0;
  1391. if (d->nodes[parent].last)
  1392. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1393. else
  1394. d->nodes[parent].first = (unsigned short)(child - 1);
  1395. d->nodes[parent].last = (unsigned short)(child - 1);
  1396. return 0;
  1397. }
  1398. int br_insert_before(struct br_doc *d, unsigned int parent,
  1399. unsigned int child, unsigned int before)
  1400. {
  1401. unsigned int prev = 0, c;
  1402. if (before == 0)
  1403. return br_append_child(d, parent, child);
  1404. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1405. before > d->nnode || parent == child - 1)
  1406. return -1;
  1407. if (d->nodes[before - 1].parent != parent)
  1408. return -1;
  1409. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1410. unsigned int c = d->nodes[child - 1].first;
  1411. while (c) {
  1412. unsigned int next = d->nodes[c].next;
  1413. br_insert_before(d, parent, c + 1, before);
  1414. c = next;
  1415. }
  1416. return 0;
  1417. }
  1418. {
  1419. unsigned int p;
  1420. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1421. if (p == child - 1)
  1422. return -1;
  1423. }
  1424. node_unlink(d, child);
  1425. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1426. c = d->nodes[parent].first;
  1427. while (c && c != before - 1) {
  1428. prev = c;
  1429. c = d->nodes[c].next;
  1430. }
  1431. if (c != before - 1)
  1432. return -1;
  1433. d->nodes[child - 1].parent = (unsigned short)parent;
  1434. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1435. if (prev)
  1436. d->nodes[prev].next = (unsigned short)(child - 1);
  1437. else
  1438. d->nodes[parent].first = (unsigned short)(child - 1);
  1439. return 0;
  1440. }
  1441. int br_remove_child(struct br_doc *d, unsigned int parent,
  1442. unsigned int child)
  1443. {
  1444. if (!d || child == 0 || child > d->nnode ||
  1445. d->nodes[child - 1].parent != parent)
  1446. return -1;
  1447. node_unlink(d, child);
  1448. return 0;
  1449. }
  1450. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1451. unsigned int child)
  1452. {
  1453. unsigned int c;
  1454. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1455. return 0;
  1456. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1457. if (c == child - 1)
  1458. return child;
  1459. return 0;
  1460. }
  1461. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1462. const char *name)
  1463. {
  1464. struct br_node *n;
  1465. const char *p, *end, *found = 0;
  1466. if (!d || node >= d->nnode || !name)
  1467. return 0;
  1468. n = &d->nodes[node];
  1469. p = d->attrs + n->extra_off;
  1470. end = p + n->extra_len;
  1471. while (p + 1 < end) {
  1472. const char *an = p;
  1473. const char *av;
  1474. p += text_len(p) + 1;
  1475. if (p >= end)
  1476. break;
  1477. av = p;
  1478. p += text_len(p) + 1;
  1479. if (same_string(an, name))
  1480. found = av;
  1481. }
  1482. if (found)
  1483. return found;
  1484. if (same_string(name, "id") && n->id_off)
  1485. return d->attrs + n->id_off;
  1486. if (same_string(name, "class") && n->class_off)
  1487. return d->attrs + n->class_off;
  1488. if (same_string(name, "style") && n->style_off)
  1489. return d->attrs + n->style_off;
  1490. if (same_string(name, "href") && n->href_off)
  1491. return d->attrs + n->href_off;
  1492. if (same_string(name, "src") && n->src_off)
  1493. return d->attrs + n->src_off;
  1494. if (same_string(name, "alt") && n->alt_off)
  1495. return d->attrs + n->alt_off;
  1496. if (same_string(name, "rel") && n->rel_off)
  1497. return d->attrs + n->rel_off;
  1498. return 0;
  1499. }
  1500. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1501. const char *name, const char *value,
  1502. unsigned int len)
  1503. {
  1504. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1505. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1506. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1507. * exactly like parse_attrs(); script-built inputs (Vue sets
  1508. * type="password" after createElement) must land there too. */
  1509. if (control && same_string(name, "type")) {
  1510. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1511. return;
  1512. }
  1513. if (control && same_string(name, "value")) {
  1514. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1515. return;
  1516. }
  1517. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1518. same_string(name, "placeholder")) {
  1519. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1520. return;
  1521. }
  1522. if (same_string(name, "id"))
  1523. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1524. else if (same_string(name, "class"))
  1525. n->class_off = len ? store_attr(d, value, len,
  1526. BR_CLASS_MAX) : 0;
  1527. else if (same_string(name, "style"))
  1528. n->style_off = len ? store_attr(d, value, len,
  1529. BR_STYLE_ATTR_MAX) : 0;
  1530. else if (same_string(name, "href"))
  1531. n->href_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1532. else if (same_string(name, "src"))
  1533. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1534. else if (same_string(name, "alt"))
  1535. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1536. else if (same_string(name, "rel"))
  1537. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1538. }
  1539. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1540. const char *value, unsigned int len)
  1541. {
  1542. struct br_node *n;
  1543. unsigned int start, nl, need;
  1544. const char *p, *old, *oldend;
  1545. if (!d || node >= d->nnode || !name)
  1546. return -1;
  1547. n = &d->nodes[node];
  1548. nl = text_len(name);
  1549. need = nl + 1 + len + 1;
  1550. start = d->attrs_used;
  1551. old = d->attrs + n->extra_off;
  1552. oldend = old + n->extra_len;
  1553. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1554. d->truncated = 1;
  1555. return -1;
  1556. }
  1557. p = old;
  1558. while (p + 1 < oldend) {
  1559. const char *an = p;
  1560. const char *av;
  1561. unsigned int anl, avl, k;
  1562. anl = text_len(an);
  1563. p += anl + 1;
  1564. if (p >= oldend)
  1565. break;
  1566. av = p;
  1567. avl = text_len(av);
  1568. p += avl + 1;
  1569. if (same_string(an, name))
  1570. continue;
  1571. for (k = 0; k < anl; k++)
  1572. d->attrs[d->attrs_used++] = an[k];
  1573. d->attrs[d->attrs_used++] = '\0';
  1574. for (k = 0; k < avl; k++)
  1575. d->attrs[d->attrs_used++] = av[k];
  1576. d->attrs[d->attrs_used++] = '\0';
  1577. }
  1578. {
  1579. unsigned int k;
  1580. for (k = 0; k < nl; k++)
  1581. d->attrs[d->attrs_used++] = name[k];
  1582. d->attrs[d->attrs_used++] = '\0';
  1583. for (k = 0; k < len; k++)
  1584. d->attrs[d->attrs_used++] = value[k];
  1585. d->attrs[d->attrs_used++] = '\0';
  1586. }
  1587. n = &d->nodes[node];
  1588. n->extra_off = start;
  1589. n->extra_len = (unsigned short)(d->attrs_used - start);
  1590. n->style_valid = 0;
  1591. n->pe_state = 0;
  1592. attr_note_known(d, n, name, value, len);
  1593. /* A script that sets a style/class (Angular's ng-style) or a new src
  1594. * after the first image scan re-arms the node for the loader; -2
  1595. * ("nothing to load") would otherwise be final. */
  1596. if ((same_string(name, "style") || same_string(name, "class")) &&
  1597. n->bg_img_index == -2)
  1598. n->bg_img_index = -1;
  1599. if (same_string(name, "src") && n->img_index == -2)
  1600. n->img_index = -1;
  1601. return 0;
  1602. }
  1603. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1604. {
  1605. struct br_node *n;
  1606. unsigned int start;
  1607. const char *p, *oldend;
  1608. int found = 0;
  1609. if (!d || node >= d->nnode || !name)
  1610. return -1;
  1611. n = &d->nodes[node];
  1612. start = d->attrs_used;
  1613. p = d->attrs + n->extra_off;
  1614. oldend = p + n->extra_len;
  1615. if (start + n->extra_len > BR_ATTR_SIZE)
  1616. return -1;
  1617. while (p + 1 < oldend) {
  1618. const char *an = p;
  1619. const char *av;
  1620. unsigned int anl, avl, k;
  1621. anl = text_len(an);
  1622. p += anl + 1;
  1623. if (p >= oldend)
  1624. break;
  1625. av = p;
  1626. avl = text_len(av);
  1627. p += avl + 1;
  1628. if (same_string(an, name)) {
  1629. found = 1;
  1630. continue;
  1631. }
  1632. for (k = 0; k < anl; k++)
  1633. d->attrs[d->attrs_used++] = an[k];
  1634. d->attrs[d->attrs_used++] = '\0';
  1635. for (k = 0; k < avl; k++)
  1636. d->attrs[d->attrs_used++] = av[k];
  1637. d->attrs[d->attrs_used++] = '\0';
  1638. }
  1639. n = &d->nodes[node];
  1640. n->extra_off = start;
  1641. n->extra_len = (unsigned short)(d->attrs_used - start);
  1642. n->style_valid = 0;
  1643. n->pe_state = 0;
  1644. attr_note_known(d, n, name, "", 0);
  1645. return found ? 0 : -1;
  1646. }
  1647. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1648. {
  1649. struct br_node *src;
  1650. unsigned int h;
  1651. const char *name;
  1652. if (!d || node == 0 || node > d->nnode)
  1653. return 0;
  1654. src = &d->nodes[node - 1];
  1655. if (src->type == BR_NODE_TEXT)
  1656. return br_create_text(d, d->text + src->text_off, src->text_len);
  1657. if (src->type == BR_NODE_COMMENT)
  1658. return br_create_comment(d, d->text + src->text_off,
  1659. src->text_len);
  1660. if (src->type == BR_NODE_FRAGMENT)
  1661. h = br_create_fragment(d);
  1662. else {
  1663. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1664. name = d->attrs + src->name_off;
  1665. else
  1666. name = br_tag_name(src->tag);
  1667. h = br_create_element(d, name, text_len(name));
  1668. }
  1669. if (!h)
  1670. return 0;
  1671. {
  1672. const char *p = d->attrs + src->extra_off;
  1673. const char *end = p + src->extra_len;
  1674. while (p + 1 < end) {
  1675. const char *an = p;
  1676. const char *av;
  1677. unsigned int anl, avl;
  1678. anl = text_len(an);
  1679. p += anl + 1;
  1680. if (p >= end)
  1681. break;
  1682. av = p;
  1683. avl = text_len(av);
  1684. p += avl + 1;
  1685. /* h is a 1-based handle; br_set_attr takes the
  1686. * node index (the old call stamped the copy's
  1687. * attributes onto the previous node). */
  1688. br_set_attr(d, h - 1, an, av, avl);
  1689. }
  1690. }
  1691. if (deep) {
  1692. unsigned int c;
  1693. for (c = src->first; c; c = d->nodes[c].next) {
  1694. unsigned int ch = br_clone_node(d, c + 1, 1);
  1695. if (ch)
  1696. br_append_child(d, h - 1, ch);
  1697. }
  1698. }
  1699. return h;
  1700. }
  1701. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1702. unsigned int len);
  1703. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1704. unsigned int *st, unsigned int *en);
  1705. /* querySelector(All) and Element.matches use the cascade's selector engine
  1706. * (sel_match(), below). */
  1707. static int match_selector(struct br_doc *d, unsigned int node,
  1708. const char *sel, unsigned int len)
  1709. {
  1710. while (len > 0 && sel[0] == ' ') {
  1711. sel++;
  1712. len--;
  1713. }
  1714. while (len > 0 && sel[len - 1] == ' ')
  1715. len--;
  1716. return len && sel_match(d, node, sel, len);
  1717. }
  1718. static int query_add(unsigned int *out, unsigned int max,
  1719. unsigned int *count, unsigned int node)
  1720. {
  1721. unsigned int i;
  1722. for (i = 0; i < *count; i++)
  1723. if (out[i] == node + 1)
  1724. return 0;
  1725. if (*count < max)
  1726. out[(*count)++] = node + 1;
  1727. return 0;
  1728. }
  1729. static void query_walk(struct br_doc *d, unsigned int node,
  1730. const char *sel, unsigned int len, unsigned int *out,
  1731. unsigned int max, unsigned int *count)
  1732. {
  1733. unsigned int c;
  1734. if (d->nodes[node].type == BR_NODE_ELEM &&
  1735. match_selector(d, node, sel, len))
  1736. query_add(out, max, count, node);
  1737. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1738. query_walk(d, c, sel, len, out, max, count);
  1739. }
  1740. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1741. {
  1742. unsigned int len;
  1743. if (!d || !selector || node == 0 || node > d->nnode)
  1744. return 0;
  1745. len = text_len(selector);
  1746. while (len > 0 && selector[0] == ' ') {
  1747. selector++;
  1748. len--;
  1749. }
  1750. while (len > 0 && selector[len - 1] == ' ')
  1751. len--;
  1752. {
  1753. unsigned int pos = 0, st, en;
  1754. while (sel_list_next(selector, len, &pos, &st, &en))
  1755. if (match_selector(d, node - 1, selector + st, en - st))
  1756. return 1;
  1757. }
  1758. return 0;
  1759. }
  1760. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1761. const char *selector, unsigned int *out,
  1762. unsigned int max)
  1763. {
  1764. unsigned int count = 0, i = 0, len, start, end;
  1765. if (!d || !selector || !out || max == 0)
  1766. return 0;
  1767. len = text_len(selector);
  1768. while (sel_list_next(selector, len, &i, &start, &end)) {
  1769. if (root == 0) {
  1770. unsigned int n;
  1771. for (n = 1; n < d->nnode; n++)
  1772. if (d->nodes[n].type == BR_NODE_ELEM &&
  1773. !node_detached(d, n) &&
  1774. match_selector(d, n, selector + start,
  1775. end - start))
  1776. query_add(out, max, &count, n);
  1777. } else if (root <= d->nnode) {
  1778. unsigned int c;
  1779. for (c = d->nodes[root - 1].first; c;
  1780. c = d->nodes[c].next)
  1781. query_walk(d, c, selector + start,
  1782. end - start, out, max, &count);
  1783. }
  1784. }
  1785. return count;
  1786. }
  1787. /* ---- attribute parsing ---- */
  1788. static int is_space(char c)
  1789. {
  1790. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1791. }
  1792. static int str_eq(const char *a, const char *b);
  1793. static unsigned int parse_uint(const char *s, unsigned int len)
  1794. {
  1795. unsigned int i = 0, v = 0;
  1796. while (i < len && is_space(s[i]))
  1797. i++;
  1798. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1799. v = v * 10 + (unsigned int)(s[i] - '0');
  1800. if (v > 4096)
  1801. v = 4096;
  1802. i++;
  1803. }
  1804. return v;
  1805. }
  1806. static int contains_ci_len(const char *s, unsigned int len,
  1807. const char *needle);
  1808. static int parse_svg_viewbox(const char *s, unsigned int len,
  1809. unsigned short *w, unsigned short *h)
  1810. {
  1811. unsigned int i = 0, v[4], n = 0;
  1812. while (i < len && n < 4) {
  1813. unsigned int x = 0;
  1814. int any = 0;
  1815. while (i < len && (is_space(s[i]) || s[i] == ','))
  1816. i++;
  1817. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1818. x = x * 10 + (unsigned int)(s[i] - '0');
  1819. i++;
  1820. any = 1;
  1821. }
  1822. /* viewBox values are commonly fractional ("0 0 340.16
  1823. * 340.16"). Consume the fraction so it is not mistaken for
  1824. * the next number; the integer part is enough for the aspect
  1825. * ratio used by the renderer. */
  1826. if (i < len && s[i] == '.') {
  1827. i++;
  1828. while (i < len && s[i] >= '0' && s[i] <= '9')
  1829. i++;
  1830. }
  1831. if (!any) {
  1832. if (i < len)
  1833. i++;
  1834. continue;
  1835. }
  1836. v[n++] = x;
  1837. }
  1838. if (n < 4)
  1839. return 0;
  1840. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1841. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1842. return *w && *h;
  1843. }
  1844. static void note_attr_flag(struct br_node *n, const char *name)
  1845. {
  1846. if (str_eq(name, "data-collapse-target"))
  1847. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1848. else if (str_eq(name, "data-collapse-trigger"))
  1849. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1850. else if (str_eq(name, "is-open"))
  1851. n->attr_flags |= BR_ATTR_IS_OPEN;
  1852. else if (str_eq(name, "disabled"))
  1853. n->attr_flags |= BR_ATTR_DISABLED;
  1854. else if (str_eq(name, "open"))
  1855. n->attr_flags |= BR_ATTR_OPEN;
  1856. else if (str_eq(name, "inert"))
  1857. n->attr_flags |= BR_ATTR_INERT;
  1858. }
  1859. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1860. * second pass so the block stays contiguous even though the known attributes
  1861. * above also use the shared attr arena. */
  1862. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1863. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1864. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1865. * written. */
  1866. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1867. {
  1868. static const struct { const char *e; char c; } ents[] = {
  1869. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1870. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1871. { 0, 0 }
  1872. };
  1873. unsigned int i = 0, n = 0;
  1874. while (i < len) {
  1875. int k, done = 0;
  1876. if (s[i] == '&') {
  1877. for (k = 0; ents[k].e; k++) {
  1878. unsigned int el = (unsigned int)strlen(ents[k].e);
  1879. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1880. out[n++] = ents[k].c;
  1881. i += el;
  1882. done = 1;
  1883. break;
  1884. }
  1885. }
  1886. if (!done && i + 3 < len && s[i + 1] == '#') {
  1887. unsigned int j = i + 2, v = 0;
  1888. int hex = s[j] == 'x' || s[j] == 'X';
  1889. if (hex)
  1890. j++;
  1891. while (j < len && j < i + 10 &&
  1892. ((s[j] >= '0' && s[j] <= '9') ||
  1893. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1894. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1895. char c = s[j++];
  1896. v = v * (hex ? 16 : 10) +
  1897. (unsigned int)(c <= '9' ? c - '0' :
  1898. (c | 0x20) - 'a' + 10);
  1899. }
  1900. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1901. out[n++] = (char)v;
  1902. i = j + 1;
  1903. done = 1;
  1904. }
  1905. }
  1906. }
  1907. if (!done)
  1908. out[n++] = s[i++];
  1909. }
  1910. return n;
  1911. }
  1912. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1913. const char *s, unsigned int len)
  1914. {
  1915. unsigned int i = 0;
  1916. unsigned int start = d->attrs_used;
  1917. while (i < len) {
  1918. char name[32];
  1919. unsigned int nl = 0, vstart, vlen;
  1920. char quote = 0;
  1921. while (i < len && is_space(s[i]))
  1922. i++;
  1923. if (i >= len || s[i] == '/')
  1924. break;
  1925. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1926. char c = s[i++];
  1927. if (c >= 'A' && c <= 'Z')
  1928. c = (char)(c - 'A' + 'a');
  1929. if (nl + 1 < sizeof(name))
  1930. name[nl++] = c;
  1931. }
  1932. name[nl] = '\0';
  1933. while (i < len && is_space(s[i]))
  1934. i++;
  1935. if (i < len && s[i] == '=') {
  1936. i++;
  1937. while (i < len && is_space(s[i]))
  1938. i++;
  1939. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1940. quote = s[i++];
  1941. vstart = i;
  1942. while (i < len && s[i] != quote)
  1943. i++;
  1944. vlen = i - vstart;
  1945. if (i < len)
  1946. i++;
  1947. } else {
  1948. vstart = i;
  1949. while (i < len && !is_space(s[i]) && s[i] != '>')
  1950. i++;
  1951. vlen = i - vstart;
  1952. }
  1953. } else {
  1954. /* Boolean attribute: expose it with an empty value. */
  1955. vstart = 0;
  1956. vlen = 0;
  1957. }
  1958. if (!nl)
  1959. continue;
  1960. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1961. d->truncated = 1;
  1962. break;
  1963. }
  1964. {
  1965. unsigned int k;
  1966. for (k = 0; k < nl; k++)
  1967. d->attrs[d->attrs_used++] = name[k];
  1968. d->attrs[d->attrs_used++] = '\0';
  1969. d->attrs_used += attr_unescape(s + vstart, vlen,
  1970. d->attrs + d->attrs_used);
  1971. d->attrs[d->attrs_used++] = '\0';
  1972. }
  1973. }
  1974. if (d->attrs_used > start) {
  1975. n->extra_off = start;
  1976. n->extra_len = (unsigned short)(d->attrs_used - start);
  1977. }
  1978. }
  1979. /* Parse attributes from `s` (between the tag name and '>'). */
  1980. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1981. const char *s, unsigned int len)
  1982. {
  1983. unsigned int i = 0;
  1984. while (i < len) {
  1985. char name[32];
  1986. unsigned int nl = 0, vstart, vlen;
  1987. char quote = 0;
  1988. while (i < len && is_space(s[i]))
  1989. i++;
  1990. if (i >= len || s[i] == '/')
  1991. break;
  1992. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1993. char c = s[i++];
  1994. if (c >= 'A' && c <= 'Z')
  1995. c = (char)(c - 'A' + 'a');
  1996. if (nl + 1 < sizeof(name))
  1997. name[nl++] = c;
  1998. }
  1999. name[nl] = '\0';
  2000. while (i < len && is_space(s[i]))
  2001. i++;
  2002. if (i >= len || s[i] != '=') {
  2003. if (str_eq(name, "hidden"))
  2004. n->flags |= BR_NODE_HIDDEN;
  2005. note_attr_flag(n, name);
  2006. continue;
  2007. }
  2008. i++;
  2009. while (i < len && is_space(s[i]))
  2010. i++;
  2011. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2012. quote = s[i++];
  2013. vstart = i;
  2014. while (i < len && s[i] != quote)
  2015. i++;
  2016. vlen = i - vstart;
  2017. if (i < len)
  2018. i++;
  2019. } else {
  2020. vstart = i;
  2021. while (i < len && !is_space(s[i]) && s[i] != '>')
  2022. i++;
  2023. vlen = i - vstart;
  2024. }
  2025. if (!nl)
  2026. continue;
  2027. if (str_eq(name, "hidden")) {
  2028. n->flags |= BR_NODE_HIDDEN;
  2029. continue;
  2030. }
  2031. note_attr_flag(n, name);
  2032. if (str_eq(name, "href"))
  2033. n->href_off = store_attr(d, s + vstart, vlen,
  2034. BR_URL_ATTR_MAX);
  2035. else if (str_eq(name, "class"))
  2036. n->class_off = store_attr(d, s + vstart, vlen,
  2037. BR_CLASS_MAX);
  2038. else if (str_eq(name, "id"))
  2039. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2040. else if (str_eq(name, "style"))
  2041. n->style_off = store_attr(d, s + vstart, vlen,
  2042. BR_STYLE_ATTR_MAX);
  2043. else if (str_eq(name, "src"))
  2044. n->src_off = store_attr(d, s + vstart, vlen,
  2045. BR_URL_ATTR_MAX);
  2046. else if (str_eq(name, "alt"))
  2047. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2048. else if (str_eq(name, "color"))
  2049. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2050. else if (str_eq(name, "rel"))
  2051. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2052. else if (str_eq(name, "value"))
  2053. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2054. else if (str_eq(name, "placeholder") &&
  2055. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2056. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2057. else if (str_eq(name, "type") &&
  2058. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2059. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2060. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2061. else if (str_eq(name, "width")) {
  2062. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2063. if (vlen && s[vstart + vlen - 1] == '%')
  2064. n->width_pct = 1;
  2065. } else if (str_eq(name, "height")) {
  2066. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2067. if (vlen && s[vstart + vlen - 1] == '%')
  2068. n->height_pct = 1;
  2069. } else if (str_eq(name, "colspan")) {
  2070. unsigned int v = parse_uint(s + vstart, vlen);
  2071. if (v < 1)
  2072. v = 1;
  2073. if (v > 16)
  2074. v = 16;
  2075. n->colspan = (unsigned char)v;
  2076. } else if (str_eq(name, "rowspan")) {
  2077. unsigned int v = parse_uint(s + vstart, vlen);
  2078. if (v < 1)
  2079. v = 1;
  2080. if (v > 16)
  2081. v = 16;
  2082. n->rowspan = (unsigned char)v;
  2083. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2084. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2085. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2086. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2087. } else if (str_eq(name, "x2")) {
  2088. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2089. } else if (str_eq(name, "y2")) {
  2090. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2091. } else if (str_eq(name, "rx")) {
  2092. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2093. } else if (str_eq(name, "d")) {
  2094. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2095. } else if (str_eq(name, "viewbox")) {
  2096. unsigned short vw = 0, vh = 0;
  2097. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2098. n->width_attr = vw;
  2099. n->height_attr = vh;
  2100. n->flags |= BR_NODE_VIEWBOX;
  2101. }
  2102. } else if (str_eq(name, "text-anchor")) {
  2103. if (contains_ci_len(s + vstart, vlen, "middle"))
  2104. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2105. else if (contains_ci_len(s + vstart, vlen, "end"))
  2106. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2107. }
  2108. }
  2109. parse_attrs_generic(d, n, s, len);
  2110. }
  2111. static int str_eq(const char *a, const char *b)
  2112. {
  2113. unsigned int i = 0;
  2114. while (a[i] && b[i] && a[i] == b[i])
  2115. i++;
  2116. return a[i] == '\0' && b[i] == '\0';
  2117. }
  2118. /* ---- CSS ---- */
  2119. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2120. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2121. {
  2122. return parse_color(s, len, out);
  2123. }
  2124. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2125. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2126. {
  2127. while (len && is_space(*s)) {
  2128. s++;
  2129. len--;
  2130. }
  2131. while (len && is_space(s[len - 1]))
  2132. len--;
  2133. if (!len)
  2134. return 0;
  2135. if (len == 4 && ci_eq_n(s, "none", 4))
  2136. return BR_PAINT_NONE;
  2137. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2138. return BR_PAINT_CURRENT;
  2139. if (len > 4 && ci_eq_n(s, "url(", 4))
  2140. return BR_PAINT_URL;
  2141. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2142. return BR_PAINT_NONE;
  2143. if (parse_color(s, len, c))
  2144. return BR_PAINT_COLOR;
  2145. return 0;
  2146. }
  2147. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2148. static int svg_len16(const char *s, unsigned int len)
  2149. {
  2150. unsigned int i = 0;
  2151. int v = 0, frac = 0, digits = 0;
  2152. while (i < len && is_space(s[i]))
  2153. i++;
  2154. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2155. if (v < 4000)
  2156. v = v * 10 + (s[i] - '0');
  2157. i++;
  2158. digits++;
  2159. }
  2160. if (i < len && s[i] == '.') {
  2161. int scale = 1;
  2162. i++;
  2163. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2164. if (scale < 10000) {
  2165. frac = frac * 10 + (s[i] - '0');
  2166. scale *= 10;
  2167. }
  2168. i++;
  2169. digits++;
  2170. }
  2171. frac = frac * 16 / scale;
  2172. }
  2173. if (!digits)
  2174. return -1;
  2175. return v * 16 + frac;
  2176. }
  2177. /* Inside an <svg> subtree (including the <svg> element itself). */
  2178. static int in_svg(const struct br_doc *d, unsigned int node)
  2179. {
  2180. unsigned int k;
  2181. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2182. if (d->nodes[node].tag == BR_TAG_SVG)
  2183. return 1;
  2184. node = d->nodes[node].parent;
  2185. }
  2186. return 0;
  2187. }
  2188. enum {
  2189. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2190. PROP_BG_REPEAT,
  2191. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2192. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2193. PROP_FONT_PX, PROP_FONT_UNIT,
  2194. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2195. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2196. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2197. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2198. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2199. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2200. PROP_BORDER_W, PROP_BORDER_COLOR,
  2201. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2202. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2203. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2204. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2205. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2206. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2207. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2208. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2209. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2210. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2211. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2212. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2213. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2214. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2215. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2216. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2217. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2218. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2219. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2220. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2221. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2222. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2223. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2224. PROP_ZINDEX, PROP_ZSET,
  2225. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2226. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2227. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2228. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2229. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2230. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2231. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2232. PROP_COUNT
  2233. };
  2234. struct color_name {
  2235. const char *name;
  2236. unsigned short rgb;
  2237. };
  2238. static const struct color_name color_names[] = {
  2239. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2240. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2241. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2242. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2243. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2244. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2245. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2246. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2247. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2248. { "darkgrey", 0x528A }, { 0, 0 }
  2249. };
  2250. static int hexval(char c)
  2251. {
  2252. if (c >= '0' && c <= '9')
  2253. return c - '0';
  2254. if (c >= 'a' && c <= 'f')
  2255. return c - 'a' + 10;
  2256. if (c >= 'A' && c <= 'F')
  2257. return c - 'A' + 10;
  2258. return -1;
  2259. }
  2260. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2261. {
  2262. unsigned int i;
  2263. for (i = 0; color_names[i].name; i++) {
  2264. unsigned int j, n = s_len(color_names[i].name);
  2265. if (n != len)
  2266. continue;
  2267. for (j = 0; j < n; j++) {
  2268. char c = s[j];
  2269. if (c >= 'A' && c <= 'Z')
  2270. c = (char)(c - 'A' + 'a');
  2271. if (c != color_names[i].name[j])
  2272. break;
  2273. }
  2274. if (j == n) {
  2275. *out = color_names[i].rgb;
  2276. return 1;
  2277. }
  2278. }
  2279. return 0;
  2280. }
  2281. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2282. * over white; the render target is a white page. */
  2283. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2284. {
  2285. unsigned int i = 0;
  2286. while (i < len && is_space(s[i]))
  2287. i++;
  2288. if (i < len && s[i] == '#') {
  2289. int d[6], nd = 0;
  2290. i++;
  2291. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2292. d[nd++] = hexval(s[i++]);
  2293. if (nd == 3) {
  2294. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2295. unsigned int b = (unsigned int)d[2];
  2296. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2297. ((g * 17 >> 2) << 5) |
  2298. (b * 17 >> 3));
  2299. return 1;
  2300. }
  2301. if (nd >= 6) {
  2302. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2303. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2304. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2305. *out = (unsigned short)(((r >> 3) << 11) |
  2306. ((g >> 2) << 5) | (b >> 3));
  2307. return 1;
  2308. }
  2309. return 0;
  2310. }
  2311. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2312. unsigned int v[3], k = 0, a = 255;
  2313. while (i < len && s[i] != '(')
  2314. i++;
  2315. if (i >= len)
  2316. return 0;
  2317. i++;
  2318. while (k < 3 && i < len) {
  2319. unsigned int x = 0;
  2320. int percent = 0;
  2321. while (i < len && (is_space(s[i]) || s[i] == ','))
  2322. i++;
  2323. if (i >= len || s[i] < '0' || s[i] > '9')
  2324. return 0;
  2325. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2326. x = x * 10 + (unsigned int)(s[i] - '0');
  2327. i++;
  2328. }
  2329. if (i < len && s[i] == '%') {
  2330. percent = 1;
  2331. i++;
  2332. }
  2333. if (percent)
  2334. x = x * 255 / 100;
  2335. if (x > 255)
  2336. x = 255;
  2337. v[k++] = x;
  2338. }
  2339. /* Optional alpha in both syntaxes:
  2340. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2341. {
  2342. unsigned int whole = 0, frac = 0, scale = 1;
  2343. int percent = 0;
  2344. while (i < len && (is_space(s[i]) || s[i] == ','))
  2345. i++;
  2346. if (i < len && s[i] == '/') {
  2347. i++;
  2348. while (i < len && is_space(s[i]))
  2349. i++;
  2350. }
  2351. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2352. * used to read as opaque black. */
  2353. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2354. (s[i] == '.' && i + 1 < len &&
  2355. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2356. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2357. whole = whole * 10 +
  2358. (unsigned int)(s[i] - '0');
  2359. i++;
  2360. }
  2361. if (i < len && s[i] == '.') {
  2362. i++;
  2363. while (i < len && s[i] >= '0' &&
  2364. s[i] <= '9') {
  2365. if (scale < 1000) {
  2366. frac = frac * 10 +
  2367. (unsigned int)(s[i] - '0');
  2368. scale *= 10;
  2369. }
  2370. i++;
  2371. }
  2372. }
  2373. while (i < len && is_space(s[i]))
  2374. i++;
  2375. if (i < len && s[i] == '%')
  2376. percent = 1;
  2377. if (percent)
  2378. a = whole > 100 ? 255 :
  2379. whole * 255 / 100;
  2380. else if (whole >= 1)
  2381. a = 255;
  2382. else
  2383. a = frac * 255 / scale;
  2384. }
  2385. }
  2386. if (k == 3) {
  2387. unsigned int r = v[0], g = v[1], b = v[2];
  2388. if (a < 255) {
  2389. r = (r * a + 255u * (255u - a)) / 255u;
  2390. g = (g * a + 255u * (255u - a)) / 255u;
  2391. b = (b * a + 255u * (255u - a)) / 255u;
  2392. }
  2393. *out = (unsigned short)(((r >> 3) << 11) |
  2394. ((g >> 2) << 5) | (b >> 3));
  2395. return 1;
  2396. }
  2397. return 0;
  2398. }
  2399. {
  2400. unsigned int j = i;
  2401. while (i < len && !is_space(s[i]))
  2402. i++;
  2403. return named_color(s + j, i - j, out);
  2404. }
  2405. }
  2406. /* Fully transparent colours have no RGB565 representation. They are common
  2407. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2408. * only on hover; painting them black would draw an outline around every
  2409. * pill and card. */
  2410. static int color_is_transparent(const char *s, unsigned int len)
  2411. {
  2412. unsigned int i = 0, n = 0;
  2413. int last = 0, prev = 0, d;
  2414. while (i < len && is_space(s[i]))
  2415. i++;
  2416. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2417. return 1;
  2418. if (i >= len || s[i] != '#')
  2419. return 0;
  2420. i++;
  2421. while (i < len && hexval(s[i]) >= 0) {
  2422. d = hexval(s[i]);
  2423. prev = last;
  2424. last = d;
  2425. n++;
  2426. i++;
  2427. }
  2428. while (i < len && is_space(s[i]))
  2429. i++;
  2430. if (i != len)
  2431. return 0;
  2432. if (n == 4)
  2433. return last == 0;
  2434. if (n == 8)
  2435. return prev == 0 && last == 0;
  2436. return 0;
  2437. }
  2438. static int register_bg_url(const char *s, unsigned int len)
  2439. {
  2440. char tmp[BR_BG_URL_MAX + 1];
  2441. unsigned int i, n = 0;
  2442. char q = 0;
  2443. while (len && is_space(*s)) {
  2444. s++;
  2445. len--;
  2446. }
  2447. while (len && is_space(s[len - 1]))
  2448. len--;
  2449. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2450. s[len - 1] == s[0]) {
  2451. q = s[0];
  2452. s++;
  2453. len -= 2;
  2454. }
  2455. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2456. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2457. if (q && s[i] == '\\' && i + 1 < len) {
  2458. i++;
  2459. if (s[i] == '\n')
  2460. continue;
  2461. }
  2462. tmp[n++] = s[i];
  2463. }
  2464. tmp[n] = '\0';
  2465. if (!n)
  2466. return 0;
  2467. for (i = 0; i < bg_url_count; i++)
  2468. if (!strcmp(bg_urls[i], tmp))
  2469. return (int)i + 1;
  2470. if (bg_url_count >= BR_BG_URLS ||
  2471. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2472. return 0;
  2473. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2474. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2475. bg_url_pool_used += n + 1;
  2476. return (int)(++bg_url_count);
  2477. }
  2478. /* Length values for margins/padding: px, ignore percentages/units. */
  2479. static unsigned int viewport_w;
  2480. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2481. {
  2482. unsigned int i = *pos;
  2483. int sign = 1;
  2484. long v = 0; /* hundredths of a unit before conversion */
  2485. int digits = 0, frac = 0, unit = 0;
  2486. while (i < len && is_space(s[i]))
  2487. i++;
  2488. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2489. if (s[i] == '-')
  2490. sign = -1;
  2491. i++;
  2492. }
  2493. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2494. v = v * 10 + (s[i] - '0');
  2495. digits = 1;
  2496. i++;
  2497. }
  2498. if (i < len && s[i] == '.') {
  2499. i++;
  2500. /* Minified CSS writes ".5rem" without a leading zero. */
  2501. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2502. if (frac < 2) {
  2503. v = v * 10 + (s[i] - '0');
  2504. frac++;
  2505. digits = 1;
  2506. }
  2507. i++;
  2508. }
  2509. }
  2510. while (frac < 2) {
  2511. v *= 10;
  2512. frac++;
  2513. }
  2514. while (frac > 2) {
  2515. v /= 10;
  2516. frac--;
  2517. }
  2518. if (i < len && s[i] == '%')
  2519. unit = 2;
  2520. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2521. s[i + 2] == 'm')
  2522. unit = 1;
  2523. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2524. unit = 3;
  2525. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2526. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2527. s[i] == 'a' || s[i] == 'd'))
  2528. i++;
  2529. *pos = i;
  2530. if (!digits)
  2531. return 0;
  2532. if (unit == 1)
  2533. v *= root_font_px;
  2534. else if (unit == 3)
  2535. v *= root_font_px;
  2536. else if (unit == 2)
  2537. v = v * (long)viewport_w / 100;
  2538. v /= 100;
  2539. v *= sign;
  2540. if (v < -2048)
  2541. v = -2048;
  2542. if (v > 2048)
  2543. v = 2048;
  2544. return (int)v;
  2545. }
  2546. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2547. * treats it as a zero-length token; margins need to keep it so block layout
  2548. * can centre a box with a definite width. */
  2549. #define CSS_LEN_AUTO (-32768)
  2550. /* A box length with its unit preserved for layout-time resolution:
  2551. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2552. * rem and whole pixels for px. */
  2553. static int parse_css_len(const char *s, unsigned int len,
  2554. unsigned int *pos, int *unit)
  2555. {
  2556. unsigned int i = *pos;
  2557. int sign = 1;
  2558. long v = 0;
  2559. int digits = 0, frac = 0;
  2560. *unit = 0;
  2561. while (i < len && is_space(s[i]))
  2562. i++;
  2563. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2564. if (s[i] == '-')
  2565. sign = -1;
  2566. i++;
  2567. }
  2568. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2569. v = v * 10 + (s[i] - '0');
  2570. digits = 1;
  2571. i++;
  2572. }
  2573. if (i < len && s[i] == '.') {
  2574. i++;
  2575. /* Leading-dot fractions are common in minified CSS
  2576. * (".5rem", ".3rem"). */
  2577. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2578. if (frac < 2) {
  2579. v = v * 10 + (s[i] - '0');
  2580. frac++;
  2581. digits = 1;
  2582. }
  2583. i++;
  2584. }
  2585. }
  2586. while (frac < 2) {
  2587. v *= 10;
  2588. frac++;
  2589. }
  2590. if (!digits) {
  2591. *pos = i;
  2592. return 0;
  2593. }
  2594. if (i < len && s[i] == '%') {
  2595. *unit = 1;
  2596. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2597. s[i + 2] == 'm') {
  2598. *unit = 3;
  2599. i += 3;
  2600. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2601. *unit = 2;
  2602. i += 2;
  2603. } else {
  2604. v /= 100;
  2605. }
  2606. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2607. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2608. s[i] == 'a' || s[i] == 'd'))
  2609. i++;
  2610. *pos = i;
  2611. v *= sign;
  2612. if (v < -32768)
  2613. v = -32768;
  2614. if (v > 32767)
  2615. v = 32767;
  2616. return (int)v;
  2617. }
  2618. static int parse_margin_len(const char *s, unsigned int len,
  2619. unsigned int *pos, int *unit)
  2620. {
  2621. unsigned int i = *pos;
  2622. while (i < len && is_space(s[i]))
  2623. i++;
  2624. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2625. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2626. *pos = i + 4;
  2627. *unit = 4;
  2628. return CSS_LEN_AUTO;
  2629. }
  2630. return parse_css_len(s, len, pos, unit);
  2631. }
  2632. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2633. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2634. static int parse_margin_calc(const char *s, unsigned int len,
  2635. unsigned int *pos, int *pct, int *px)
  2636. {
  2637. unsigned int i = *pos, j;
  2638. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2639. return 0;
  2640. i += 5;
  2641. j = i;
  2642. while (j < len && s[j] != ')')
  2643. j++;
  2644. if (j >= len)
  2645. return 0;
  2646. if (!parse_calc(s + i, j - i, pct, px))
  2647. return 0;
  2648. *pos = j + 1;
  2649. return 1;
  2650. }
  2651. /* calc(P% +/- Npx) for the common width/margin forms. */
  2652. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2653. {
  2654. unsigned int i = 0;
  2655. int sign = 1;
  2656. int have = 0;
  2657. *pct = 0;
  2658. *px = 0;
  2659. while (i < len) {
  2660. unsigned int j;
  2661. long whole = 0, frac = 0, scale = 1;
  2662. int digits = 0;
  2663. while (i < len && is_space(s[i]))
  2664. i++;
  2665. if (i >= len)
  2666. break;
  2667. if (s[i] == '+' || s[i] == '-') {
  2668. sign = s[i] == '-' ? -1 : 1;
  2669. i++;
  2670. continue;
  2671. }
  2672. if (s[i] < '0' || s[i] > '9') {
  2673. if (s[i] == '.') {
  2674. /* fall through to the number parser */
  2675. } else {
  2676. i++;
  2677. continue;
  2678. }
  2679. }
  2680. j = i;
  2681. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2682. whole = whole * 10 + (s[j] - '0');
  2683. digits = 1;
  2684. j++;
  2685. }
  2686. if (j < len && s[j] == '.') {
  2687. j++;
  2688. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2689. frac = frac * 10 + (s[j] - '0');
  2690. scale *= 10;
  2691. j++;
  2692. }
  2693. }
  2694. if (!digits) {
  2695. i = j + 1;
  2696. continue;
  2697. }
  2698. while (j < len && is_space(s[j]))
  2699. j++;
  2700. if (j < len && s[j] == '%') {
  2701. *pct = sign * (int)(whole + (scale > 1 ?
  2702. frac * 100 / scale : 0));
  2703. have = 1;
  2704. j++;
  2705. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2706. *px = sign * (int)whole;
  2707. have = 1;
  2708. j += 2;
  2709. }
  2710. i = j;
  2711. while (i < len && !is_space(s[i]))
  2712. i++;
  2713. sign = 1;
  2714. }
  2715. return have;
  2716. }
  2717. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2718. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2719. unsigned char *kind)
  2720. {
  2721. unsigned int i = 0;
  2722. unsigned long v = 0;
  2723. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2724. *val = 0;
  2725. *kind = 0;
  2726. while (i < len && is_space(s[i]))
  2727. i++;
  2728. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2729. return 0;
  2730. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2731. return 0;
  2732. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2733. v = v * 10 + (unsigned int)(s[i] - '0');
  2734. digits = 1;
  2735. i++;
  2736. }
  2737. if (i < len && s[i] == '.') {
  2738. i++;
  2739. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2740. if (frac < 2) {
  2741. v = v * 10 + (unsigned int)(s[i] - '0');
  2742. frac++;
  2743. digits = 1;
  2744. }
  2745. i++;
  2746. }
  2747. }
  2748. while (frac < 2) {
  2749. v *= 10;
  2750. frac++;
  2751. }
  2752. while (frac > 2) {
  2753. v /= 10;
  2754. frac--;
  2755. }
  2756. if (!digits)
  2757. return 0;
  2758. while (i < len && is_space(s[i]))
  2759. i++;
  2760. if (i < len && s[i] == '%')
  2761. pct = 1;
  2762. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2763. s[i + 2] == 'm')
  2764. rem = 1;
  2765. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2766. rem = 2;
  2767. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2768. vw = 1;
  2769. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2770. ; /* px: already the default scale */
  2771. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2772. bad_unit = 1;
  2773. if (bad_unit)
  2774. return 0;
  2775. if (rem == 1 || rem == 2) {
  2776. if (v > 65535)
  2777. v = 65535;
  2778. *val = (unsigned short)v;
  2779. *kind = rem == 1 ? 5 : 4;
  2780. return 1;
  2781. }
  2782. v /= 100;
  2783. if (vw)
  2784. v = v * viewport_w / 100;
  2785. if (pct) {
  2786. if (v > 100)
  2787. v = 100;
  2788. *kind = 2;
  2789. } else {
  2790. if (v > 2048)
  2791. v = 2048;
  2792. *kind = 1;
  2793. }
  2794. *val = (unsigned short)v;
  2795. return 1;
  2796. }
  2797. static int parse_font_px(const char *s, unsigned int len)
  2798. {
  2799. unsigned int i = 0;
  2800. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2801. int v1000, px;
  2802. int is_px = 1, is_rem = 0, is_pct = 0;
  2803. while (i < len && is_space(s[i]))
  2804. i++;
  2805. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2806. return 24;
  2807. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2808. return 32;
  2809. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2810. return 14;
  2811. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2812. whole = whole * 10 + (s[i] - '0');
  2813. digits = 1;
  2814. i++;
  2815. }
  2816. if (i < len && s[i] == '.') {
  2817. i++;
  2818. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2819. if (fscale < 1000) {
  2820. frac = frac * 10 + (s[i] - '0');
  2821. fscale *= 10;
  2822. }
  2823. digits = 1;
  2824. i++;
  2825. }
  2826. }
  2827. if (!digits)
  2828. return 0;
  2829. while (i < len && is_space(s[i]))
  2830. i++;
  2831. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2832. is_rem = 1;
  2833. is_px = 0;
  2834. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2835. is_rem = 1;
  2836. is_px = 0;
  2837. } else if (i < len && s[i] == '%') {
  2838. is_pct = 1;
  2839. is_px = 0;
  2840. }
  2841. v1000 = whole * 1000 + frac * 1000 / fscale;
  2842. if (is_rem)
  2843. px = v1000 * root_font_px / 1000;
  2844. else if (is_pct)
  2845. px = v1000 * root_font_px / 100000;
  2846. else if (is_px)
  2847. px = v1000 / 1000;
  2848. else
  2849. px = v1000 / 1000;
  2850. if (px < 8)
  2851. px = 8;
  2852. if (px > 72)
  2853. px = 72;
  2854. return px;
  2855. }
  2856. /* font-size keeping the unit so em/% resolve against the parent face. */
  2857. static int parse_font_size(const char *s, unsigned int len,
  2858. unsigned short *raw, unsigned char *unit)
  2859. {
  2860. unsigned int i = 0;
  2861. int whole = 0, frac = 0, scale = 1, digits = 0;
  2862. int v100;
  2863. *raw = 0;
  2864. *unit = 0;
  2865. while (i < len && is_space(s[i]))
  2866. i++;
  2867. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2868. *raw = 24;
  2869. *unit = 1;
  2870. return 1;
  2871. }
  2872. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2873. *raw = 32;
  2874. *unit = 1;
  2875. return 1;
  2876. }
  2877. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2878. *raw = 14;
  2879. *unit = 1;
  2880. return 1;
  2881. }
  2882. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2883. whole = whole * 10 + (s[i] - '0');
  2884. digits = 1;
  2885. i++;
  2886. }
  2887. if (i < len && s[i] == '.') {
  2888. i++;
  2889. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2890. frac = frac * 10 + (s[i] - '0');
  2891. scale *= 10;
  2892. i++;
  2893. }
  2894. }
  2895. if (!digits && !frac)
  2896. return 0;
  2897. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2898. while (i < len && is_space(s[i]))
  2899. i++;
  2900. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2901. *raw = (unsigned short)v100;
  2902. *unit = 3;
  2903. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2904. *raw = (unsigned short)v100;
  2905. *unit = 2;
  2906. } else if (i < len && s[i] == '%') {
  2907. /* Keep hundredths: 62.5% is a common root size and truncating
  2908. * it to 62% makes every rem 1px too small. */
  2909. *raw = (unsigned short)v100;
  2910. *unit = 4;
  2911. } else {
  2912. *raw = (unsigned short)(v100 / 100);
  2913. *unit = 1;
  2914. }
  2915. return 1;
  2916. }
  2917. static int parse_line_height(const char *s, unsigned int len,
  2918. unsigned short *px, unsigned char *pct)
  2919. {
  2920. unsigned int i = 0;
  2921. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2922. int v1000;
  2923. int is_px = 0, is_pct = 0;
  2924. *px = 0;
  2925. *pct = 0;
  2926. while (i < len && is_space(s[i]))
  2927. i++;
  2928. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2929. return 0;
  2930. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2931. whole = whole * 10 + (s[i] - '0');
  2932. digits = 1;
  2933. i++;
  2934. }
  2935. if (i < len && s[i] == '.') {
  2936. i++;
  2937. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2938. frac = frac * 10 + (s[i] - '0');
  2939. fscale *= 10;
  2940. digits = 1;
  2941. i++;
  2942. }
  2943. }
  2944. if (!digits)
  2945. return 0;
  2946. while (i < len && is_space(s[i]))
  2947. i++;
  2948. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2949. is_px = 1;
  2950. else if (i < len && s[i] == '%')
  2951. is_pct = 1;
  2952. v1000 = whole * 1000 + frac * 1000 / fscale;
  2953. /* rem is an absolute length (the root font is known by the time
  2954. * rules are parsed, as for font-size); em behaves like a multiplier. */
  2955. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2956. v1000 = v1000 * root_font_px;
  2957. is_px = 1;
  2958. }
  2959. if (is_px) {
  2960. int v = v1000 / 1000;
  2961. if (v < 8)
  2962. v = 8;
  2963. if (v > 160)
  2964. v = 160;
  2965. *px = (unsigned short)v;
  2966. } else {
  2967. int v = v1000 / 10; /* unitless multiplier -> percent */
  2968. if (is_pct)
  2969. v = v1000 / 1000;
  2970. if (v < 50)
  2971. v = 50;
  2972. if (v > 255) /* stored in an unsigned char */
  2973. v = 255;
  2974. *pct = (unsigned char)v;
  2975. }
  2976. return 1;
  2977. }
  2978. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2979. {
  2980. unsigned int i, j, n = s_len(needle);
  2981. for (i = 0; i + n <= len; i++) {
  2982. for (j = 0; j < n; j++) {
  2983. char a = s[i + j];
  2984. if (a >= 'A' && a <= 'Z')
  2985. a = (char)(a - 'A' + 'a');
  2986. if (a != needle[j])
  2987. break;
  2988. }
  2989. if (j == n)
  2990. return 1;
  2991. }
  2992. return 0;
  2993. }
  2994. /* Pull a colour out of a border/background shorthand. */
  2995. /* Background shorthands can hold several layers before the final solid
  2996. * colour. The colour is conventionally the last token (e.g. a stack of
  2997. * gradients followed by var(--bg)), so take the rightmost colour rather
  2998. * than the first gradient stop. */
  2999. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3000. {
  3001. int last = -1;
  3002. unsigned int i;
  3003. for (i = 0; i < len; i++) {
  3004. if (s[i] == '#')
  3005. last = (int)i;
  3006. else if ((s[i] == 'r' || s[i] == 'R') &&
  3007. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3008. last = (int)i;
  3009. }
  3010. if (last >= 0)
  3011. return parse_color(s + last, len - (unsigned int)last, c);
  3012. while (i > 0) {
  3013. unsigned int st, e;
  3014. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3015. s[i - 1] == ')' || s[i - 1] == '('))
  3016. i--;
  3017. e = i;
  3018. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3019. s[i - 1] != ')' && s[i - 1] != '(')
  3020. i--;
  3021. st = i;
  3022. if (e > st && parse_color(s + st, e - st, c))
  3023. return 1;
  3024. }
  3025. return 0;
  3026. }
  3027. static int parse_display(const char *s, unsigned int len)
  3028. {
  3029. unsigned int i;
  3030. while (len && is_space(s[0])) { s++; len--; }
  3031. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3032. ;
  3033. if (i == 4 && ci_eq_n(s, "none", 4))
  3034. return BR_DISPLAY_NONE;
  3035. if (i == 5 && ci_eq_n(s, "block", 5))
  3036. return BR_DISPLAY_BLOCK;
  3037. if (i == 6 && ci_eq_n(s, "inline", 6))
  3038. return BR_DISPLAY_INLINE;
  3039. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3040. return BR_DISPLAY_INLINE_BLOCK;
  3041. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3042. return BR_DISPLAY_BLOCK;
  3043. if (i == 4 && ci_eq_n(s, "flex", 4))
  3044. return BR_DISPLAY_FLEX;
  3045. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3046. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3047. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3048. * block instead of an inline box. */
  3049. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3050. return BR_DISPLAY_BLOCK;
  3051. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3052. return BR_DISPLAY_BLOCK;
  3053. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3054. return BR_DISPLAY_FLEX;
  3055. if (i == 5 && ci_eq_n(s, "table", 5))
  3056. return BR_DISPLAY_BLOCK;
  3057. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3058. return BR_DISPLAY_BLOCK;
  3059. /* Grid shells are common on news/wiki pages, but the two-column
  3060. * heuristic is not reliable for their nested area templates yet.
  3061. * Collapse them to block flow so the content stays readable. */
  3062. if (i == 4 && ci_eq_n(s, "grid", 4))
  3063. return BR_DISPLAY_BLOCK;
  3064. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3065. return BR_DISPLAY_BLOCK;
  3066. return BR_DISPLAY_INLINE;
  3067. }
  3068. static void decl_add(struct br_decl *out, unsigned int *nout,
  3069. unsigned char prop, int val, int important)
  3070. {
  3071. if (*nout >= BR_DECLS)
  3072. return;
  3073. out[*nout].prop = prop;
  3074. out[*nout].val = (short)val;
  3075. out[*nout].important = (unsigned char)(important != 0);
  3076. (*nout)++;
  3077. }
  3078. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3079. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3080. unsigned int *start, unsigned int *end)
  3081. {
  3082. unsigned int i = *pos;
  3083. while (i < len && is_space(s[i]))
  3084. i++;
  3085. if (i >= len)
  3086. return 0;
  3087. *start = i;
  3088. while (i < len && !is_space(s[i]))
  3089. i++;
  3090. *end = i;
  3091. *pos = i;
  3092. return 1;
  3093. }
  3094. static int bg_number(const char *s, unsigned int len, signed short *val,
  3095. unsigned char *kind)
  3096. {
  3097. unsigned int i = 0;
  3098. long v = 0;
  3099. int neg = 0, digits = 0, pct = 0;
  3100. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3101. neg = s[i] == '-';
  3102. i++;
  3103. }
  3104. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3105. v = v * 10 + (s[i] - '0');
  3106. digits = 1;
  3107. i++;
  3108. }
  3109. if (i < len && s[i] == '.') {
  3110. i++;
  3111. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3112. digits = 1;
  3113. i++;
  3114. }
  3115. }
  3116. if (!digits)
  3117. return 0;
  3118. if (i < len && s[i] == '%') {
  3119. pct = 1;
  3120. i++;
  3121. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3122. i += 2;
  3123. }
  3124. if (i != len)
  3125. return 0;
  3126. if (neg)
  3127. v = -v;
  3128. if (v < -4096)
  3129. v = -4096;
  3130. if (v > 4096)
  3131. v = 4096;
  3132. *val = (signed short)v;
  3133. *kind = (unsigned char)(pct ? 1 : 0);
  3134. return 1;
  3135. }
  3136. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3137. int *vertical)
  3138. {
  3139. *vertical = 0;
  3140. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3141. *val = 0;
  3142. return 1;
  3143. }
  3144. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3145. *val = 100;
  3146. return 1;
  3147. }
  3148. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3149. *val = 0;
  3150. *vertical = 1;
  3151. return 1;
  3152. }
  3153. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3154. *val = 100;
  3155. *vertical = 1;
  3156. return 1;
  3157. }
  3158. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3159. *val = 50;
  3160. return 1;
  3161. }
  3162. return 0;
  3163. }
  3164. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3165. const char *s, unsigned int len, int important)
  3166. {
  3167. unsigned int pos = 0, ts, te;
  3168. signed short val[2] = { 50, 50 };
  3169. unsigned char kind[2] = { 1, 1 };
  3170. int vert[2] = { 0, 0 }, n = 0;
  3171. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3172. signed short v;
  3173. int is_vert;
  3174. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3175. val[n] = v;
  3176. kind[n] = 1;
  3177. vert[n] = is_vert;
  3178. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3179. val[n] = v;
  3180. vert[n] = 0;
  3181. } else {
  3182. n++;
  3183. continue;
  3184. }
  3185. n++;
  3186. }
  3187. if (n == 1) {
  3188. if (vert[0]) {
  3189. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3190. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3191. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3192. important);
  3193. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3194. } else {
  3195. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3196. important);
  3197. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3198. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3199. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3200. }
  3201. return;
  3202. }
  3203. if (n >= 2) {
  3204. int x = vert[0] && !vert[1] ? 1 : 0;
  3205. int y = 1 - x;
  3206. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3207. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3208. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3209. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3210. }
  3211. }
  3212. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3213. const char *s, unsigned int len, int important)
  3214. {
  3215. unsigned int pos = 0, ts, te;
  3216. signed short w = 0, h = 0;
  3217. unsigned char kw = 0, kh = 0;
  3218. int n = 0;
  3219. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3220. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3221. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3222. return;
  3223. }
  3224. if (n == 0 && te - ts == 7 &&
  3225. ci_eq_n(s + ts, "contain", 7)) {
  3226. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3227. return;
  3228. }
  3229. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3230. n++;
  3231. continue;
  3232. }
  3233. {
  3234. signed short v;
  3235. unsigned char k;
  3236. if (bg_number(s + ts, te - ts, &v, &k)) {
  3237. if (n == 0) {
  3238. w = v;
  3239. kw = k;
  3240. } else {
  3241. h = v;
  3242. kh = k;
  3243. }
  3244. }
  3245. }
  3246. n++;
  3247. }
  3248. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3249. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3250. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3251. (void)kh;
  3252. }
  3253. /* Parse the `position / size` part of the background shorthand. The image
  3254. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3255. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3256. const char *v, unsigned int len,
  3257. int important)
  3258. {
  3259. unsigned int i, slash = 0, depth = 0, p, first, count;
  3260. char q = 0;
  3261. signed short nv;
  3262. int vert;
  3263. unsigned char kind;
  3264. for (i = 0; i < len; i++) {
  3265. char c = v[i];
  3266. if (q) {
  3267. if (c == q)
  3268. q = 0;
  3269. continue;
  3270. }
  3271. if (c == '\'' || c == '"') {
  3272. q = c;
  3273. continue;
  3274. }
  3275. if (c == '(') {
  3276. depth++;
  3277. continue;
  3278. }
  3279. if (c == ')') {
  3280. if (depth)
  3281. depth--;
  3282. continue;
  3283. }
  3284. if (c == '/' && !depth) {
  3285. slash = i;
  3286. break;
  3287. }
  3288. }
  3289. if (!slash)
  3290. return;
  3291. /* Up to two position tokens immediately before the slash. */
  3292. p = slash;
  3293. first = slash;
  3294. count = 0;
  3295. while (count < 2) {
  3296. unsigned int e;
  3297. while (p > 0 && is_space(v[p - 1]))
  3298. p--;
  3299. e = p;
  3300. while (p > 0 && !is_space(v[p - 1]))
  3301. p--;
  3302. if (e == p)
  3303. break;
  3304. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3305. bg_number(v + p, e - p, &nv, &kind)) {
  3306. first = p;
  3307. count++;
  3308. } else {
  3309. break;
  3310. }
  3311. }
  3312. if (count)
  3313. add_bg_position(out, nout, v + first, slash - first,
  3314. important);
  3315. /* Up to two size tokens after the slash. */
  3316. p = slash + 1;
  3317. while (p < len && is_space(v[p]))
  3318. p++;
  3319. first = p;
  3320. count = 0;
  3321. while (count < 2) {
  3322. unsigned int ts = p;
  3323. while (p < len && !is_space(v[p]))
  3324. p++;
  3325. if (p == ts)
  3326. break;
  3327. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3328. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3329. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3330. bg_number(v + ts, p - ts, &nv, &kind)) {
  3331. count++;
  3332. while (p < len && is_space(v[p]))
  3333. p++;
  3334. } else {
  3335. break;
  3336. }
  3337. }
  3338. if (count)
  3339. add_bg_size(out, nout, v + first, p - first, important);
  3340. }
  3341. static int parse_fixed_100(const char *s, unsigned int len,
  3342. unsigned int *pos, signed short *out)
  3343. {
  3344. unsigned int i = *pos;
  3345. long whole = 0, frac = 0;
  3346. int neg = 0, digits = 0, fd = 0;
  3347. while (i < len && is_space(s[i]))
  3348. i++;
  3349. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3350. neg = s[i] == '-';
  3351. i++;
  3352. }
  3353. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3354. whole = whole * 10 + (s[i] - '0');
  3355. if (whole > 327)
  3356. whole = 327;
  3357. digits = 1;
  3358. i++;
  3359. }
  3360. if (i < len && s[i] == '.') {
  3361. i++;
  3362. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3363. if (fd < 2) {
  3364. frac = frac * 10 + (s[i] - '0');
  3365. fd++;
  3366. digits = 1;
  3367. }
  3368. i++;
  3369. }
  3370. }
  3371. if (!digits)
  3372. return 0;
  3373. while (fd++ < 2)
  3374. frac *= 10;
  3375. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3376. *pos = i;
  3377. return 1;
  3378. }
  3379. static int find_fn(const char *v, unsigned int len, const char *name,
  3380. unsigned int *arg)
  3381. {
  3382. unsigned int n = s_len(name), i;
  3383. for (i = 0; i + n <= len; i++) {
  3384. unsigned int j;
  3385. if (!ci_eq_n(v + i, name, n))
  3386. continue;
  3387. j = i + n;
  3388. while (j < len && is_space(v[j]))
  3389. j++;
  3390. if (j < len && v[j] == '(') {
  3391. *arg = j + 1;
  3392. return 1;
  3393. }
  3394. }
  3395. return 0;
  3396. }
  3397. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3398. const char *s, unsigned int len,
  3399. int important)
  3400. {
  3401. unsigned int pos = 0, ts, te;
  3402. signed short val[2] = { 50, 50 };
  3403. unsigned char kind[2] = { 1, 1 };
  3404. int vert[2] = { 0, 0 }, n = 0;
  3405. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3406. signed short v;
  3407. int is_vert;
  3408. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3409. val[n] = v;
  3410. kind[n] = 1;
  3411. vert[n] = is_vert;
  3412. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3413. val[n] = v;
  3414. vert[n] = 0;
  3415. } else {
  3416. n++;
  3417. continue;
  3418. }
  3419. n++;
  3420. }
  3421. if (n == 1) {
  3422. if (vert[0]) {
  3423. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3424. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3425. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3426. important);
  3427. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3428. } else {
  3429. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3430. important);
  3431. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3432. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3433. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3434. }
  3435. return;
  3436. }
  3437. if (n >= 2) {
  3438. int x = vert[0] && !vert[1] ? 1 : 0;
  3439. int y = 1 - x;
  3440. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3441. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3442. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3443. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3444. }
  3445. }
  3446. /* Copy a background value with every url(...) body blanked (kept as
  3447. * `url()`), for the keyword and colour scans of the shorthand. */
  3448. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3449. unsigned int cap)
  3450. {
  3451. unsigned int i = 0, n = 0;
  3452. while (i < len && n + 1 < cap) {
  3453. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3454. char q = 0;
  3455. memcpy(out + n, "url(", 4);
  3456. n += 4;
  3457. i += 4;
  3458. while (i < len) {
  3459. if (q) {
  3460. if (s[i] == '\\' && i + 1 < len)
  3461. i++;
  3462. else if (s[i] == q)
  3463. q = 0;
  3464. } else if (s[i] == '"' || s[i] == '\'') {
  3465. q = s[i];
  3466. } else if (s[i] == ')') {
  3467. break;
  3468. }
  3469. i++;
  3470. }
  3471. continue; /* the ')' is copied below */
  3472. }
  3473. out[n++] = s[i++];
  3474. }
  3475. out[n] = '\0';
  3476. return n;
  3477. }
  3478. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3479. const char *v, unsigned int vl, int important)
  3480. {
  3481. unsigned int i;
  3482. for (i = 0; i + 4 <= vl; i++) {
  3483. if (ci_eq_n(v + i, "url(", 4)) {
  3484. unsigned int st = i + 4, en = st;
  3485. char q = 0;
  3486. int idx;
  3487. /* `)` inside a quoted data: URL (an SVG's
  3488. * rgba(...)) does not end it. */
  3489. while (en < vl) {
  3490. if (q) {
  3491. if (v[en] == '\\' && en + 1 < vl)
  3492. en++;
  3493. else if (v[en] == q)
  3494. q = 0;
  3495. } else if (v[en] == '"' || v[en] == '\'') {
  3496. q = v[en];
  3497. } else if (v[en] == ')') {
  3498. break;
  3499. }
  3500. en++;
  3501. }
  3502. idx = register_bg_url(v + st, en - st);
  3503. if (idx)
  3504. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3505. important);
  3506. return;
  3507. }
  3508. }
  3509. }
  3510. static int parse_len_token(const char *s, unsigned int len, int *out)
  3511. {
  3512. unsigned int pos = 0;
  3513. int v;
  3514. if (!len)
  3515. return 0;
  3516. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3517. return 0;
  3518. v = parse_len(s, len, &pos);
  3519. if (pos == 0)
  3520. return 0;
  3521. *out = v;
  3522. return 1;
  3523. }
  3524. static void expand_four(int v[4], int n)
  3525. {
  3526. if (n == 1) {
  3527. v[1] = v[2] = v[3] = v[0];
  3528. } else if (n == 2) {
  3529. v[2] = v[0];
  3530. v[3] = v[1];
  3531. } else if (n == 3) {
  3532. v[3] = v[1];
  3533. }
  3534. }
  3535. /* Custom properties defined in the declaration block being expanded. A
  3536. * component that sets a variable and uses it in the same rule (Infima's
  3537. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3538. * var(--ifm-col-width) }`) means its own value: the element matching the
  3539. * rule gets it. The document-wide table cannot know that, because
  3540. * `.col` and `.col--N` give the name a dozen different values. */
  3541. static int css_env_active; /* css_local holds an element's variables */
  3542. #define CSS_LOCAL_VARS 48
  3543. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3544. unsigned int i);
  3545. static struct {
  3546. const char *name, *val;
  3547. unsigned short nlen, vlen;
  3548. } css_local[CSS_LOCAL_VARS];
  3549. static unsigned int css_nlocal;
  3550. static int css_local_find(const char *name, unsigned int nlen)
  3551. {
  3552. unsigned int k;
  3553. for (k = 0; k < css_nlocal; k++)
  3554. if (css_local[k].nlen == nlen &&
  3555. !memcmp(css_local[k].name, name, nlen))
  3556. return (int)k;
  3557. return -1;
  3558. }
  3559. static int css_local_find_before(const char *name, unsigned int nlen,
  3560. unsigned int end)
  3561. {
  3562. unsigned int k;
  3563. for (k = 0; k < end; k++)
  3564. if (css_local[k].nlen == nlen &&
  3565. !memcmp(css_local[k].name, name, nlen))
  3566. return (int)k;
  3567. return -1;
  3568. }
  3569. static void css_local_append(const char *s, unsigned int len);
  3570. /* Collect the top-level `--name: value` declarations of a block. */
  3571. static void css_local_collect(const char *s, unsigned int len)
  3572. {
  3573. css_nlocal = 0;
  3574. css_local_append(s, len);
  3575. }
  3576. static void css_local_append(const char *s, unsigned int len)
  3577. {
  3578. unsigned int i = 0;
  3579. while (i < len) {
  3580. unsigned int ns, ne, vs, ve;
  3581. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3582. i++;
  3583. ns = i;
  3584. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3585. i++;
  3586. if (i >= len || s[i] != ':') {
  3587. if (i < len && s[i] == '{')
  3588. i = css_skip_nested(s, len, i + 1);
  3589. continue;
  3590. }
  3591. ne = i;
  3592. while (ne > ns && is_space(s[ne - 1]))
  3593. ne--;
  3594. vs = i + 1;
  3595. ve = css_value_end(s, len, vs);
  3596. i = ve;
  3597. while (vs < ve && is_space(s[vs]))
  3598. vs++;
  3599. while (ve > vs && is_space(s[ve - 1]))
  3600. ve--;
  3601. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3602. ve -= 10;
  3603. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3604. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3605. continue;
  3606. css_local[css_nlocal].name = s + ns;
  3607. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3608. css_local[css_nlocal].val = s + vs;
  3609. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3610. css_nlocal++;
  3611. }
  3612. }
  3613. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3614. * the input is copied verbatim when it contains no var(). */
  3615. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3616. unsigned int len, char *out,
  3617. unsigned int cap, int depth)
  3618. {
  3619. unsigned int i = 0, n = 0;
  3620. if (depth > 8)
  3621. return 0;
  3622. while (i < len && n + 1 < cap) {
  3623. unsigned int p, inner, close, comma, nlen, fstart;
  3624. char quote;
  3625. int parens, glue;
  3626. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3627. out[n++] = s[i++];
  3628. continue;
  3629. }
  3630. p = i + 4;
  3631. quote = 0;
  3632. parens = 1;
  3633. while (p < len && parens) {
  3634. char c = s[p];
  3635. if (quote) {
  3636. if (c == quote)
  3637. quote = 0;
  3638. else if (c == '\\' && p + 1 < len)
  3639. p++;
  3640. } else if (c == '"' || c == '\'') {
  3641. quote = c;
  3642. } else if (c == '(') {
  3643. parens++;
  3644. } else if (c == ')') {
  3645. parens--;
  3646. }
  3647. p++;
  3648. }
  3649. if (parens) {
  3650. out[n++] = s[i++];
  3651. continue;
  3652. }
  3653. close = p - 1;
  3654. inner = i + 4;
  3655. /* var() substitutes tokens: minified Infima writes
  3656. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3657. * must not become the one token `700.875rem`. Separate the
  3658. * value from a glued neighbour on both sides. */
  3659. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3660. out[n - 1] != '(' && out[n - 1] != ',' &&
  3661. out[n - 1] != ':' && out[n - 1] != '/')
  3662. out[n++] = ' ';
  3663. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3664. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3665. s[p] != '!' && s[p] != '}';
  3666. while (inner < close && is_space(s[inner]))
  3667. inner++;
  3668. comma = inner;
  3669. parens = 0;
  3670. quote = 0;
  3671. while (comma < close) {
  3672. char c = s[comma];
  3673. if (quote) {
  3674. if (c == quote)
  3675. quote = 0;
  3676. } else if (c == '"' || c == '\'') {
  3677. quote = c;
  3678. } else if (c == '(') {
  3679. parens++;
  3680. } else if (c == ')' && parens > 0) {
  3681. parens--;
  3682. } else if (c == ',' && parens == 0) {
  3683. break;
  3684. }
  3685. comma++;
  3686. }
  3687. nlen = comma - inner;
  3688. while (nlen && is_space(s[inner + nlen - 1]))
  3689. nlen--;
  3690. fstart = comma < close ? comma + 1 : close;
  3691. while (fstart < close && is_space(s[fstart]))
  3692. fstart++;
  3693. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3694. unsigned int vi = css_var_find(d, s + inner, nlen);
  3695. int li = css_local_find(s + inner, nlen);
  3696. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3697. s + close + 1) {
  3698. /* (not the definition's own value) */
  3699. n += css_expand_vars(d, css_local[li].val,
  3700. css_local[li].vlen,
  3701. out + n, cap - n,
  3702. depth + 1);
  3703. i = p;
  3704. if (glue && n + 1 < cap)
  3705. out[n++] = ' ';
  3706. continue;
  3707. }
  3708. if (vi < BR_MAX_VARS &&
  3709. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3710. vi = BR_MAX_VARS;
  3711. if (vi < BR_MAX_VARS) {
  3712. const char *v = attr_ptr(d, d->var_val[vi]);
  3713. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3714. out + n,
  3715. cap - n, depth + 1);
  3716. n += vn;
  3717. } else if (fstart < close) {
  3718. unsigned int vn = css_expand_vars(d, s + fstart,
  3719. close - fstart,
  3720. out + n, cap - n,
  3721. depth + 1);
  3722. n += vn;
  3723. }
  3724. } else if (fstart < close) {
  3725. unsigned int vn = css_expand_vars(d, s + fstart,
  3726. close - fstart,
  3727. out + n, cap - n,
  3728. depth + 1);
  3729. n += vn;
  3730. }
  3731. i = p;
  3732. if (glue && n + 1 < cap)
  3733. out[n++] = ' ';
  3734. }
  3735. out[n] = '\0';
  3736. return n;
  3737. }
  3738. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3739. unsigned int i)
  3740. {
  3741. while (i < len && (is_space(s[i]) || s[i] == ','))
  3742. i++;
  3743. return i;
  3744. }
  3745. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3746. * argument). Returns 1 on success. */
  3747. static int parse_grid_track(const char *s, unsigned int len,
  3748. unsigned int *pp, unsigned short *val,
  3749. unsigned char *kind)
  3750. {
  3751. unsigned int i = grid_skip_sep(s, len, *pp);
  3752. unsigned int whole = 0, frac = 0, scale = 1;
  3753. int any = 0;
  3754. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3755. unsigned int j = i + 7;
  3756. while (j < len && s[j] != ',')
  3757. j++;
  3758. if (j >= len)
  3759. return 0;
  3760. i = j + 1;
  3761. while (i < len && is_space(s[i]))
  3762. i++;
  3763. }
  3764. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3765. int depth = 1;
  3766. i += 7;
  3767. while (i < len && depth) {
  3768. if (s[i] == '(')
  3769. depth++;
  3770. else if (s[i] == ')')
  3771. depth--;
  3772. i++;
  3773. }
  3774. *pp = i;
  3775. return 0;
  3776. }
  3777. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3778. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3779. i++;
  3780. any = 1;
  3781. }
  3782. if (i < len && s[i] == '.') {
  3783. i++;
  3784. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3785. if (scale < 1000) {
  3786. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3787. scale *= 10;
  3788. }
  3789. i++;
  3790. any = 1;
  3791. }
  3792. }
  3793. if (!any) {
  3794. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3795. *val = 1;
  3796. *kind = 3;
  3797. *pp = i + 4;
  3798. return 1;
  3799. }
  3800. return 0;
  3801. }
  3802. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3803. *val = (unsigned short)(whole ? whole : 1);
  3804. *kind = 3;
  3805. i += 2;
  3806. } else if (i < len && s[i] == '%') {
  3807. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3808. *kind = 2;
  3809. i++;
  3810. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3811. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3812. *kind = 1;
  3813. i += 3;
  3814. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3815. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3816. *kind = 1;
  3817. i += 2;
  3818. } else {
  3819. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3820. *kind = 1;
  3821. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3822. i += 2;
  3823. }
  3824. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3825. i++;
  3826. *pp = i;
  3827. return 1;
  3828. }
  3829. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3830. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3831. * keep the two-column path). */
  3832. static int grid_repeat_count(const char *s, unsigned int len)
  3833. {
  3834. unsigned int i = 0;
  3835. int n = 0;
  3836. while (i < len && is_space(s[i]))
  3837. i++;
  3838. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3839. i += 7;
  3840. while (i < len && is_space(s[i]))
  3841. i++;
  3842. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3843. n = n * 10 + (s[i] - '0');
  3844. i++;
  3845. }
  3846. if (n < 1 || n > 12)
  3847. return 0;
  3848. while (i < len && (is_space(s[i]) || s[i] == ','))
  3849. i++;
  3850. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3851. return n;
  3852. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3853. i += 7;
  3854. while (i < len && is_space(s[i]))
  3855. i++;
  3856. if (i < len && s[i] == '0') {
  3857. i++;
  3858. while (i < len && (is_space(s[i]) ||
  3859. s[i] == ',' || s[i] == 'p' ||
  3860. s[i] == 'x'))
  3861. i++;
  3862. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3863. return n;
  3864. }
  3865. }
  3866. return 0;
  3867. }
  3868. /* "1fr 1fr 1fr" */
  3869. for (;;) {
  3870. while (i < len && is_space(s[i]))
  3871. i++;
  3872. if (i >= len)
  3873. break;
  3874. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3875. (i + 3 == len || is_space(s[i + 3]))) {
  3876. n++;
  3877. i += 3;
  3878. } else {
  3879. return 0;
  3880. }
  3881. }
  3882. return n >= 3 ? n : 0;
  3883. }
  3884. static int parse_grid_cols(const char *s, unsigned int len,
  3885. unsigned short val[2], unsigned char kind[2])
  3886. {
  3887. unsigned int pos = 0;
  3888. int n = 0;
  3889. while (n < 2) {
  3890. unsigned int before = pos;
  3891. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3892. break;
  3893. n++;
  3894. if (pos <= before)
  3895. break;
  3896. while (pos < len && is_space(s[pos]))
  3897. pos++;
  3898. if (pos >= len)
  3899. break;
  3900. }
  3901. return n;
  3902. }
  3903. static int parse_grid_slot(const char *s, unsigned int len)
  3904. {
  3905. if (contains_ci_len(s, len, "footer") ||
  3906. contains_ci_len(s, len, "header") ||
  3907. contains_ci_len(s, len, "sitenotice") ||
  3908. contains_ci_len(s, len, "navbar"))
  3909. return 1;
  3910. if (contains_ci_len(s, len, "columnstart") ||
  3911. contains_ci_len(s, len, "sidebar") ||
  3912. contains_ci_len(s, len, "left") ||
  3913. contains_ci_len(s, len, "start"))
  3914. return 2;
  3915. if (contains_ci_len(s, len, "pagecontent") ||
  3916. contains_ci_len(s, len, "content") ||
  3917. contains_ci_len(s, len, "main") ||
  3918. contains_ci_len(s, len, "right") ||
  3919. contains_ci_len(s, len, "end"))
  3920. return 3;
  3921. return 0;
  3922. }
  3923. static int css_has_var(const char *s, unsigned int len)
  3924. {
  3925. unsigned int i;
  3926. for (i = 0; i + 4 <= len; i++)
  3927. if ((s[i] == 'v' || s[i] == 'V') &&
  3928. ci_eq_n(s + i, "var(", 4))
  3929. return 1;
  3930. return 0;
  3931. }
  3932. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3933. unsigned int i)
  3934. {
  3935. i += 2;
  3936. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3937. i++;
  3938. if (i + 1 < len)
  3939. i += 2;
  3940. else
  3941. i = len;
  3942. return i;
  3943. }
  3944. static unsigned int css_skip_string(const char *s, unsigned int len,
  3945. unsigned int i)
  3946. {
  3947. char q = s[i++];
  3948. while (i < len && s[i] != q) {
  3949. if (s[i] == '\\' && i + 1 < len)
  3950. i++;
  3951. i++;
  3952. }
  3953. if (i < len)
  3954. i++;
  3955. return i;
  3956. }
  3957. /* Find the '}' that closes a declaration block. i points just past the
  3958. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3959. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3960. * selectors such as "Highlight-Stories {" without a closing brace). */
  3961. static unsigned int css_block_end(const char *s, unsigned int len,
  3962. unsigned int i)
  3963. {
  3964. int depth = 0;
  3965. while (i < len) {
  3966. char c = s[i];
  3967. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3968. i = css_skip_comment(s, len, i);
  3969. continue;
  3970. }
  3971. if (c == '"' || c == '\'') {
  3972. i = css_skip_string(s, len, i);
  3973. continue;
  3974. }
  3975. if (c == '{') {
  3976. depth++;
  3977. } else if (c == '}') {
  3978. if (depth == 0)
  3979. return i;
  3980. depth--;
  3981. }
  3982. i++;
  3983. }
  3984. return len;
  3985. }
  3986. /* Skip a block whose opening '{' has already been consumed. */
  3987. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3988. unsigned int i)
  3989. {
  3990. int depth = 1;
  3991. while (i < len && depth) {
  3992. char c = s[i];
  3993. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3994. i = css_skip_comment(s, len, i);
  3995. continue;
  3996. }
  3997. if (c == '"' || c == '\'') {
  3998. i = css_skip_string(s, len, i);
  3999. continue;
  4000. }
  4001. if (c == '{')
  4002. depth++;
  4003. else if (c == '}')
  4004. depth--;
  4005. i++;
  4006. }
  4007. return i;
  4008. }
  4009. /* ---- @font-face ------------------------------------------------------- */
  4010. static void font_trim(const char **sp, unsigned int *len)
  4011. {
  4012. const char *s = *sp;
  4013. unsigned int n = *len;
  4014. while (n && is_space(*s)) {
  4015. s++;
  4016. n--;
  4017. }
  4018. while (n && is_space(s[n - 1]))
  4019. n--;
  4020. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4021. s++;
  4022. n -= 2;
  4023. }
  4024. *sp = s;
  4025. *len = n;
  4026. }
  4027. static void font_add(const char *family, unsigned int flen,
  4028. const char *url, unsigned int ulen)
  4029. {
  4030. unsigned int i, n = 0, k;
  4031. font_trim(&family, &flen);
  4032. font_trim(&url, &ulen);
  4033. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4034. return;
  4035. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4036. struct br_font_def *f = &font_defs[i];
  4037. if (s_len(f->family) != flen)
  4038. continue;
  4039. for (k = 0; k < flen; k++) {
  4040. char a = f->family[k], b = family[k];
  4041. if (a >= 'A' && a <= 'Z')
  4042. a = (char)(a - 'A' + 'a');
  4043. if (b >= 'A' && b <= 'Z')
  4044. b = (char)(b - 'A' + 'a');
  4045. if (a != b)
  4046. break;
  4047. }
  4048. if (k != flen)
  4049. continue;
  4050. n = 0;
  4051. while (url[n] && n + 1 < sizeof(f->url)) {
  4052. f->url[n] = url[n];
  4053. n++;
  4054. }
  4055. f->url[n] = '\0';
  4056. n = 0;
  4057. while (font_base[n] && n + 1 < sizeof(f->base)) {
  4058. f->base[n] = font_base[n];
  4059. n++;
  4060. }
  4061. f->base[n] = '\0';
  4062. return;
  4063. }
  4064. if (font_face_count >= BR_MAX_FACES)
  4065. return;
  4066. {
  4067. struct br_font_def *f = &font_defs[font_face_count];
  4068. for (n = 0; n < flen; n++)
  4069. f->family[n] = family[n];
  4070. f->family[n] = '\0';
  4071. for (n = 0; n < ulen; n++)
  4072. f->url[n] = url[n];
  4073. f->url[n] = '\0';
  4074. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  4075. f->base[n] = font_base[n];
  4076. f->base[n] = '\0';
  4077. f->used = 1;
  4078. font_face_count++;
  4079. }
  4080. }
  4081. /* Look up the first name in a font-family list that has an @font-face. */
  4082. static int font_family_id(const char *s, unsigned int len)
  4083. {
  4084. unsigned int i = 0;
  4085. while (i < len) {
  4086. unsigned int start, end, q = 0;
  4087. while (i < len && (is_space(s[i]) || s[i] == ','))
  4088. i++;
  4089. if (i >= len)
  4090. break;
  4091. if (s[i] == '"' || s[i] == '\'') {
  4092. char quote = s[i++];
  4093. start = i;
  4094. while (i < len && s[i] != quote)
  4095. i++;
  4096. end = i;
  4097. if (i < len)
  4098. i++;
  4099. } else {
  4100. start = i;
  4101. while (i < len && s[i] != ',')
  4102. i++;
  4103. end = i;
  4104. while (end > start && is_space(s[end - 1]))
  4105. end--;
  4106. }
  4107. if (end > start) {
  4108. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4109. const char *f = font_defs[q].family;
  4110. unsigned int fl = s_len(f), k;
  4111. if (fl != end - start)
  4112. continue;
  4113. for (k = 0; k < fl; k++) {
  4114. char a = f[k], b = s[start + k];
  4115. if (a >= 'A' && a <= 'Z')
  4116. a = (char)(a - 'A' + 'a');
  4117. if (b >= 'A' && b <= 'Z')
  4118. b = (char)(b - 'A' + 'a');
  4119. if (a != b)
  4120. break;
  4121. }
  4122. if (k == fl)
  4123. return (int)q + 1;
  4124. }
  4125. }
  4126. }
  4127. return 0;
  4128. }
  4129. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4130. * at-rules, so faces must be registered before any font-family declaration
  4131. * in the same sheet is resolved. */
  4132. static void scan_font_faces(const char *s, unsigned int len)
  4133. {
  4134. unsigned int i = 0;
  4135. while (i + 10 <= len) {
  4136. unsigned int j, end;
  4137. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4138. unsigned int flen = 0, ulen = 0, p;
  4139. int have_family = 0, have_url = 0;
  4140. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4141. i++;
  4142. continue;
  4143. }
  4144. j = i + 10;
  4145. while (j < len && s[j] != '{' && s[j] != ';')
  4146. j++;
  4147. if (j >= len || s[j] == ';') {
  4148. i = j + 1;
  4149. continue;
  4150. }
  4151. end = css_skip_nested(s, len, j + 1);
  4152. p = j + 1;
  4153. while (p < end) {
  4154. unsigned int ds, de, vs, ve;
  4155. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4156. p++;
  4157. if (p >= end)
  4158. break;
  4159. ds = p;
  4160. while (p < end && s[p] != ':' && s[p] != ';')
  4161. p++;
  4162. de = p;
  4163. if (p >= end || s[p] != ':') {
  4164. while (p < end && s[p] != ';')
  4165. p++;
  4166. continue;
  4167. }
  4168. p++;
  4169. vs = p;
  4170. while (p < end && s[p] != ';')
  4171. p++;
  4172. ve = p;
  4173. while (ds < de && is_space(s[ds]))
  4174. ds++;
  4175. while (de > ds && is_space(s[de - 1]))
  4176. de--;
  4177. while (vs < ve && is_space(s[vs]))
  4178. vs++;
  4179. while (ve > vs && is_space(s[ve - 1]))
  4180. ve--;
  4181. if (de - ds == 11 &&
  4182. ci_eq_n(s + ds, "font-family", 11)) {
  4183. const char *fp = s + vs;
  4184. unsigned int fn = ve - vs;
  4185. font_trim(&fp, &fn);
  4186. if (fn && fn < sizeof(family)) {
  4187. unsigned int k;
  4188. for (k = 0; k < fn; k++)
  4189. family[k] = fp[k];
  4190. family[fn] = '\0';
  4191. flen = fn;
  4192. have_family = 1;
  4193. }
  4194. } else if (de - ds == 3 &&
  4195. ci_eq_n(s + ds, "src", 3)) {
  4196. unsigned int q = vs;
  4197. while (q + 4 <= ve) {
  4198. unsigned int us, ue;
  4199. if (!ci_eq_n(s + q, "url(", 4)) {
  4200. q++;
  4201. continue;
  4202. }
  4203. q += 4;
  4204. us = q;
  4205. while (q < ve && s[q] != ')')
  4206. q++;
  4207. ue = q;
  4208. {
  4209. const char *up = s + us;
  4210. unsigned int un = ue - us;
  4211. font_trim(&up, &un);
  4212. if (un && un < sizeof(url) &&
  4213. !contains_ci_len(up, un,
  4214. "woff")) {
  4215. unsigned int k;
  4216. for (k = 0; k < un; k++)
  4217. url[k] = up[k];
  4218. url[un] = '\0';
  4219. ulen = un;
  4220. have_url = 1;
  4221. break;
  4222. }
  4223. }
  4224. }
  4225. }
  4226. }
  4227. if (have_family && have_url)
  4228. font_add(family, flen, url, ulen);
  4229. i = end;
  4230. }
  4231. }
  4232. /* One border-radius length. Unlike parse_size(), percentages keep their
  4233. * kind: the drawer scales them to the box, not the viewport. */
  4234. static int parse_radius_value(const char *s, unsigned int len,
  4235. unsigned int *pos, unsigned short *val,
  4236. unsigned char *kind)
  4237. {
  4238. unsigned int i = *pos;
  4239. unsigned long v = 0;
  4240. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4241. while (i < len && is_space(s[i]))
  4242. i++;
  4243. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4244. v = v * 10 + (unsigned int)(s[i] - '0');
  4245. digits = 1;
  4246. i++;
  4247. }
  4248. if (i < len && s[i] == '.') {
  4249. i++;
  4250. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4251. fdigits = 1;
  4252. if (frac < 2) {
  4253. v = v * 10 + (unsigned int)(s[i] - '0');
  4254. frac++;
  4255. }
  4256. i++;
  4257. }
  4258. }
  4259. while (frac < 2) {
  4260. v *= 10;
  4261. frac++;
  4262. }
  4263. while (i < len && is_space(s[i]))
  4264. i++;
  4265. if (i < len && s[i] == '%') {
  4266. pct = 1;
  4267. i++;
  4268. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4269. s[i + 2] == 'm') {
  4270. rem = 1;
  4271. i += 3;
  4272. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4273. rem = 2;
  4274. i += 2;
  4275. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4276. i += 2;
  4277. } else if (i < len && is_ident(s[i])) {
  4278. return 0;
  4279. }
  4280. if (!digits && !fdigits)
  4281. return 0;
  4282. if (rem == 1)
  4283. v *= root_font_px;
  4284. else if (rem == 2)
  4285. v *= root_font_px;
  4286. v /= 100;
  4287. if (v > 2048)
  4288. v = 2048;
  4289. *val = (unsigned short)v;
  4290. *kind = pct ? 2 : 1;
  4291. *pos = i;
  4292. return 1;
  4293. }
  4294. /* font: [style] [variant] [weight] size[/line-height] family */
  4295. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4296. const char *v, unsigned int len, int important)
  4297. {
  4298. unsigned int pos = 0, ts, te;
  4299. unsigned short size_raw = 0;
  4300. unsigned char size_unit = 0;
  4301. int have_size = 0;
  4302. int bold = 0, italic = 0;
  4303. int lh_px = 0, lh_pct = 0, mono = 0;
  4304. while (next_token(v, len, &pos, &ts, &te)) {
  4305. unsigned int tl = te - ts;
  4306. unsigned int slash = 0;
  4307. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4308. bold = 1;
  4309. continue;
  4310. }
  4311. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4312. continue;
  4313. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4314. italic = 1;
  4315. continue;
  4316. }
  4317. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4318. italic = 1;
  4319. continue;
  4320. }
  4321. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4322. continue;
  4323. for (slash = 0; slash < tl; slash++)
  4324. if (v[ts + slash] == '/')
  4325. break;
  4326. if (slash == tl) {
  4327. unsigned int q;
  4328. int weight = 0;
  4329. for (q = 0; q < tl; q++) {
  4330. if (v[ts + q] < '0' || v[ts + q] > '9')
  4331. break;
  4332. weight = weight * 10 + (v[ts + q] - '0');
  4333. }
  4334. if (q == tl && weight >= 100 && weight <= 900) {
  4335. bold = weight >= 500;
  4336. continue;
  4337. }
  4338. }
  4339. {
  4340. unsigned short raw;
  4341. unsigned char unit;
  4342. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4343. &raw, &unit))
  4344. continue;
  4345. size_raw = raw;
  4346. size_unit = unit;
  4347. }
  4348. have_size = 1;
  4349. if (slash < tl) {
  4350. unsigned int lstart = ts + slash + 1;
  4351. unsigned short lhp = 0;
  4352. unsigned char lhpct = 0;
  4353. if (!parse_line_height(v + lstart, te - lstart,
  4354. &lhp, &lhpct)) {
  4355. lh_px = 0;
  4356. lh_pct = 0;
  4357. } else {
  4358. lh_px = lhp;
  4359. lh_pct = lhpct;
  4360. }
  4361. }
  4362. }
  4363. if (!have_size)
  4364. return;
  4365. mono = contains_ci_len(v, len, "mono");
  4366. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4367. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4368. decl_add(out, nout, PROP_BOLD, bold, important);
  4369. decl_add(out, nout, PROP_ITALIC, italic, important);
  4370. decl_add(out, nout, PROP_MONO, mono, important);
  4371. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  4372. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4373. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4374. }
  4375. static int css_collect_only;
  4376. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4377. * index into d->layer_hash. css_layer_name is the dotted path of the
  4378. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4379. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4380. * layer's own rules therefore lose to its sublayers (the spec says the
  4381. * reverse), which no page we render depends on. */
  4382. static unsigned char css_cur_layer;
  4383. static char css_layer_name[128];
  4384. static unsigned int css_anon_layers;
  4385. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4386. {
  4387. unsigned int h = 2166136261u, i;
  4388. for (i = 0; i < len; i++) {
  4389. h ^= (unsigned char)s[i];
  4390. h *= 16777619u;
  4391. }
  4392. return h ? h : 1;
  4393. }
  4394. /* Register (or look up) the layer `name` nested under the current path and
  4395. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4396. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4397. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4398. unsigned int nlen, char *path,
  4399. unsigned int cap)
  4400. {
  4401. unsigned int pl = s_len(css_layer_name), i, h;
  4402. char anon[16];
  4403. while (nlen && is_space(*name)) { name++; nlen--; }
  4404. while (nlen && is_space(name[nlen - 1])) nlen--;
  4405. if (!nlen) {
  4406. /* An anonymous layer is unique; it can never be reopened.
  4407. * The collect-only (custom property) pass does not need a
  4408. * rank, so it must not burn a table slot. */
  4409. unsigned int n = ++css_anon_layers, k = 0;
  4410. char rev[10];
  4411. if (css_collect_only)
  4412. return 0;
  4413. anon[0] = '#';
  4414. while (n && k < sizeof(rev)) {
  4415. rev[k++] = (char)('0' + n % 10);
  4416. n /= 10;
  4417. }
  4418. for (i = 0; i < k; i++)
  4419. anon[1 + i] = rev[k - 1 - i];
  4420. name = anon;
  4421. nlen = k + 1;
  4422. }
  4423. if (pl + 1 + nlen >= cap)
  4424. return 0;
  4425. memcpy(path, css_layer_name, pl);
  4426. if (pl)
  4427. path[pl++] = '.';
  4428. memcpy(path + pl, name, nlen);
  4429. path[pl + nlen] = '\0';
  4430. h = layer_name_hash(path, pl + nlen);
  4431. for (i = 0; i < d->nlayers; i++)
  4432. if (d->layer_hash[i] == h)
  4433. return (unsigned char)(i + 1);
  4434. if (d->nlayers >= BR_MAX_LAYERS)
  4435. return 0;
  4436. d->layer_hash[d->nlayers++] = h;
  4437. return (unsigned char)d->nlayers;
  4438. }
  4439. /* @supports: the engine cannot answer feature queries, so the block
  4440. * applies unless it is negated (`not (...)` usually guards a fallback for
  4441. * old browsers) or asks for selector() support. */
  4442. static int supports_matches(const char *s, unsigned int len)
  4443. {
  4444. while (len && is_space(*s)) { s++; len--; }
  4445. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4446. (len == 3 || is_space(s[3]) || s[3] == '('))
  4447. return 0;
  4448. if (contains_ci_len(s, len, "selector("))
  4449. return 0;
  4450. return 1;
  4451. }
  4452. /* Append one code point of generated content as Latin-1. Typography the
  4453. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4454. * and anything else outside Latin-1 are dropped. */
  4455. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4456. unsigned int cp)
  4457. {
  4458. const char *sub = 0;
  4459. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4460. sub = "-";
  4461. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4462. sub = "'";
  4463. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4464. sub = "\"";
  4465. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4466. sub = "\xB7";
  4467. else if (cp == 0x2026)
  4468. sub = "...";
  4469. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4470. sub = ">";
  4471. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4472. sub = "<";
  4473. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4474. cp == 0x2003 || cp == 0x202F)
  4475. sub = " ";
  4476. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4477. sub = "\xD7";
  4478. else if (cp == 0x2713 || cp == 0x2714)
  4479. sub = "v";
  4480. if (sub) {
  4481. while (*sub && *n + 1 < cap)
  4482. out[(*n)++] = *sub++;
  4483. return;
  4484. }
  4485. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4486. out[(*n)++] = (char)cp;
  4487. else if (cp == '\n' && *n + 1 < cap)
  4488. out[(*n)++] = ' ';
  4489. }
  4490. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4491. * content index + 1, or 0 for none/empty (counters and url() are not
  4492. * generated). */
  4493. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4494. {
  4495. char out[160];
  4496. unsigned int i = 0, n = 0, off;
  4497. while (i < len) {
  4498. char c = s[i];
  4499. if (c == '"' || c == '\'') {
  4500. char q = c;
  4501. i++;
  4502. while (i < len && s[i] != q) {
  4503. unsigned int cp;
  4504. if (s[i] == '\\' && i + 1 < len) {
  4505. unsigned int h = 0, k = 0;
  4506. i++;
  4507. while (k < 6 && i < len &&
  4508. ((s[i] >= '0' && s[i] <= '9') ||
  4509. ((s[i] | 32) >= 'a' &&
  4510. (s[i] | 32) <= 'f'))) {
  4511. char x = (char)(s[i] | 32);
  4512. h = h * 16 + (unsigned int)(x <= '9' ?
  4513. x - '0' : x - 'a' + 10);
  4514. i++;
  4515. k++;
  4516. }
  4517. if (k) {
  4518. if (i < len && s[i] == ' ')
  4519. i++;
  4520. cp = h;
  4521. } else {
  4522. cp = (unsigned char)s[i++];
  4523. }
  4524. } else if ((unsigned char)s[i] >= 0xC0) {
  4525. unsigned char b = (unsigned char)s[i++];
  4526. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4527. cp = b & (0x3F >> more);
  4528. while (more-- && i < len &&
  4529. ((unsigned char)s[i] & 0xC0) == 0x80)
  4530. cp = (cp << 6) | (s[i++] & 0x3F);
  4531. } else {
  4532. cp = (unsigned char)s[i++];
  4533. }
  4534. content_put(out, &n, sizeof(out), cp);
  4535. }
  4536. i++;
  4537. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4538. unsigned int st = i + 5, e = st;
  4539. while (e < len && s[e] != ')')
  4540. e++;
  4541. while (st < e && is_space(s[st]))
  4542. st++;
  4543. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4544. out[n++] = '\001';
  4545. while (st < e && !is_space(s[st]))
  4546. out[n++] = s[st++];
  4547. out[n++] = '\002';
  4548. }
  4549. i = e + 1;
  4550. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4551. ci_eq_n(s + i, "close-quot", 10))) {
  4552. content_put(out, &n, sizeof(out), '"');
  4553. i += 10;
  4554. } else if (c == '(') {
  4555. unsigned int depth = 0;
  4556. /* counter(), url(), ...: skip the arguments */
  4557. for (; i < len; i++) {
  4558. if (s[i] == '(')
  4559. depth++;
  4560. else if (s[i] == ')' && --depth == 0)
  4561. break;
  4562. }
  4563. i++;
  4564. } else {
  4565. i++;
  4566. }
  4567. }
  4568. if (!n) {
  4569. /* `content: ""` still creates the box: icon
  4570. * pseudo-elements are empty strings with a size and a
  4571. * background image. */
  4572. for (i = 0; i < len; i++)
  4573. if (s[i] == '"' || s[i] == '\'')
  4574. return BR_CONTENT_EMPTY;
  4575. return 0;
  4576. }
  4577. if (d->ncontent >= BR_MAX_CONTENT)
  4578. return 0;
  4579. off = store_raw(d, out, n, sizeof(out));
  4580. if (!off)
  4581. return 0;
  4582. d->content_off[d->ncontent++] = off;
  4583. return d->ncontent;
  4584. }
  4585. /* ---- calc() folding ---------------------------------------------------
  4586. *
  4587. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4588. * Frameworks build their scales from arithmetic on custom properties
  4589. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4590. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4591. * calc() in a declaration block is folded into that canonical form:
  4592. * a linear combination of %, px (rem/em at the root font size, vw against
  4593. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4594. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4595. * the calc() text unchanged. Values are hundredths. */
  4596. static unsigned int viewport_w; /* defined with the media code */
  4597. struct calc_val {
  4598. long px, pct, num;
  4599. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  4600. };
  4601. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4602. struct calc_val *v, int depth);
  4603. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  4604. {
  4605. while (*i < len && is_space(s[*i]))
  4606. (*i)++;
  4607. }
  4608. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  4609. struct calc_val *v, int depth)
  4610. {
  4611. long whole = 0, frac = 0, scale = 1, n;
  4612. int sign = 1, digits = 0;
  4613. calc_space(s, len, i);
  4614. if (*i < len && (s[*i] == '(' ||
  4615. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  4616. *i += s[*i] == '(' ? 1 : 5;
  4617. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  4618. return 0;
  4619. calc_space(s, len, i);
  4620. if (*i >= len || s[*i] != ')')
  4621. return 0;
  4622. (*i)++;
  4623. return 1;
  4624. }
  4625. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  4626. sign = s[*i] == '-' ? -1 : 1;
  4627. (*i)++;
  4628. }
  4629. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4630. whole = whole * 10 + (s[*i] - '0');
  4631. digits = 1;
  4632. (*i)++;
  4633. if (whole > 1000000)
  4634. return 0;
  4635. }
  4636. if (*i < len && s[*i] == '.') {
  4637. (*i)++;
  4638. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4639. if (scale < 10000) {
  4640. frac = frac * 10 + (s[*i] - '0');
  4641. scale *= 10;
  4642. }
  4643. digits = 1;
  4644. (*i)++;
  4645. }
  4646. }
  4647. if (!digits)
  4648. return 0;
  4649. n = sign * (whole * 100 + frac * 100 / scale);
  4650. v->px = v->pct = v->num = 0;
  4651. if (*i < len && s[*i] == '%') {
  4652. (*i)++;
  4653. v->pct = n;
  4654. v->kind = 2;
  4655. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  4656. *i += 3;
  4657. v->px = n * root_font_px;
  4658. v->kind = 1;
  4659. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  4660. *i += 2;
  4661. v->px = n * root_font_px;
  4662. v->kind = 1;
  4663. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  4664. *i += 2;
  4665. v->px = n;
  4666. v->kind = 1;
  4667. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  4668. *i += 2;
  4669. v->px = n * (long)viewport_w / 100;
  4670. v->kind = 1;
  4671. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  4672. return 0; /* vh, deg, fr, ch, ... */
  4673. } else {
  4674. v->num = n;
  4675. v->kind = 4;
  4676. }
  4677. return 1;
  4678. }
  4679. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  4680. struct calc_val *v, int depth)
  4681. {
  4682. if (!calc_unary(s, len, i, v, depth))
  4683. return 0;
  4684. for (;;) {
  4685. struct calc_val r;
  4686. char op;
  4687. calc_space(s, len, i);
  4688. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  4689. return 1;
  4690. op = s[(*i)++];
  4691. if (!calc_unary(s, len, i, &r, depth))
  4692. return 0;
  4693. if (op == '/') {
  4694. if (r.kind != 4 || !r.num)
  4695. return 0;
  4696. v->px = v->px * 100 / r.num;
  4697. v->pct = v->pct * 100 / r.num;
  4698. v->num = v->num * 100 / r.num;
  4699. } else if (r.kind == 4) {
  4700. v->px = v->px * r.num / 100;
  4701. v->pct = v->pct * r.num / 100;
  4702. v->num = v->num * r.num / 100;
  4703. } else if (v->kind == 4) {
  4704. long k = v->num;
  4705. *v = r;
  4706. v->px = v->px * k / 100;
  4707. v->pct = v->pct * k / 100;
  4708. } else {
  4709. return 0; /* length * length */
  4710. }
  4711. }
  4712. }
  4713. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4714. struct calc_val *v, int depth)
  4715. {
  4716. if (!calc_product(s, len, i, v, depth))
  4717. return 0;
  4718. for (;;) {
  4719. struct calc_val r;
  4720. int neg;
  4721. calc_space(s, len, i);
  4722. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  4723. return 1;
  4724. neg = s[(*i)++] == '-';
  4725. if (!calc_product(s, len, i, &r, depth))
  4726. return 0;
  4727. if ((v->kind == 4) != (r.kind == 4))
  4728. return 0; /* number + length */
  4729. v->px += neg ? -r.px : r.px;
  4730. v->pct += neg ? -r.pct : r.pct;
  4731. v->num += neg ? -r.num : r.num;
  4732. v->kind |= r.kind;
  4733. }
  4734. }
  4735. /* Hundredths -> "12", "-0.5", "33.33". */
  4736. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  4737. {
  4738. char tmp[24];
  4739. unsigned int n = 0, k = 0;
  4740. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  4741. unsigned long w = a / 100, f = a % 100;
  4742. if (v < 0)
  4743. tmp[n++] = '-';
  4744. {
  4745. char rev[12];
  4746. unsigned int r = 0;
  4747. do {
  4748. rev[r++] = (char)('0' + w % 10);
  4749. w /= 10;
  4750. } while (w && r < sizeof(rev));
  4751. while (r)
  4752. tmp[n++] = rev[--r];
  4753. }
  4754. if (f) {
  4755. tmp[n++] = '.';
  4756. tmp[n++] = (char)('0' + f / 10);
  4757. if (f % 10)
  4758. tmp[n++] = (char)('0' + f % 10);
  4759. }
  4760. for (k = 0; k < n && k < cap; k++)
  4761. o[k] = tmp[k];
  4762. return k;
  4763. }
  4764. /* Copy s to out with every foldable calc() replaced; returns the length,
  4765. * or 0 when nothing was folded (the caller keeps the original). */
  4766. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  4767. unsigned int cap)
  4768. {
  4769. unsigned int i = 0, n = 0;
  4770. int changed = 0;
  4771. while (i < len && n + 40 < cap) {
  4772. struct calc_val v;
  4773. unsigned int j = i + 5;
  4774. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  4775. (i == 0 || !is_ident(s[i - 1])))) {
  4776. out[n++] = s[i++];
  4777. continue;
  4778. }
  4779. if (!calc_sum(s, len, &j, &v, 0) ||
  4780. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  4781. out[n++] = s[i++];
  4782. continue;
  4783. }
  4784. j++;
  4785. /* Minified CSS glues tokens to the function's parentheses
  4786. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  4787. * a folded `10px16px` would be one token. */
  4788. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  4789. out[n - 1] != '(' && out[n - 1] != ',')
  4790. out[n++] = ' ';
  4791. if (v.kind == 4) {
  4792. n += calc_fmt(out + n, cap - n, v.num);
  4793. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  4794. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  4795. -50 : 50)) /
  4796. 100 * 100);
  4797. out[n++] = 'p';
  4798. out[n++] = 'x';
  4799. } else if (!v.px) {
  4800. n += calc_fmt(out + n, cap - n, v.pct);
  4801. out[n++] = '%';
  4802. } else {
  4803. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  4804. memcpy(out + n, "calc(", 5);
  4805. n += 5;
  4806. n += calc_fmt(out + n, cap - n, v.pct);
  4807. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  4808. n += 4;
  4809. n += calc_fmt(out + n, cap - n, px * 100);
  4810. memcpy(out + n, "px)", 3);
  4811. n += 3;
  4812. }
  4813. i = j;
  4814. changed = 1;
  4815. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  4816. s[i] != ',' && s[i] != '!' && s[i] != '}')
  4817. out[n++] = ' ';
  4818. }
  4819. if (!changed || i < len)
  4820. return 0;
  4821. out[n] = '\0';
  4822. return n;
  4823. }
  4824. static int css_has_calc(const char *s, unsigned int len)
  4825. {
  4826. unsigned int i;
  4827. for (i = 0; i + 5 <= len; i++)
  4828. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  4829. return 1;
  4830. return 0;
  4831. }
  4832. static void parse_decls(struct br_doc *d, const char *s,
  4833. unsigned int len, struct br_decl *out,
  4834. unsigned int *nout)
  4835. {
  4836. static char expanded[4096];
  4837. static char folded[4096];
  4838. if (css_has_var(s, len)) {
  4839. unsigned int elen;
  4840. if (!css_env_active)
  4841. css_local_collect(s, len);
  4842. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  4843. 0);
  4844. if (!css_env_active)
  4845. css_nlocal = 0;
  4846. if (elen) {
  4847. s = expanded;
  4848. len = elen;
  4849. }
  4850. }
  4851. if (css_has_calc(s, len)) {
  4852. unsigned int flen = css_fold_calc(s, len, folded,
  4853. sizeof(folded));
  4854. if (flen) {
  4855. s = folded;
  4856. len = flen;
  4857. }
  4858. }
  4859. unsigned int i = 0;
  4860. *nout = 0;
  4861. while (i < len && *nout < BR_DECLS) {
  4862. unsigned int pstart, pend, vstart, vend, k;
  4863. char prop[24];
  4864. int important = 0;
  4865. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4866. i++;
  4867. if (i >= len)
  4868. break;
  4869. /* A nested at-rule (@media/@supports) inside the declaration
  4870. * block carries its own selector context; skip it rather than
  4871. * reading its prelude as a declaration. */
  4872. if (s[i] == '@') {
  4873. while (i < len && s[i] != '{' && s[i] != ';')
  4874. i++;
  4875. if (i < len && s[i] == '{')
  4876. i = css_skip_nested(s, len, i + 1);
  4877. else if (i < len)
  4878. i++;
  4879. continue;
  4880. }
  4881. pstart = i;
  4882. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4883. i++;
  4884. if (i < len && s[i] == '{') {
  4885. /* Nested style rule: skip its block. */
  4886. i = css_skip_nested(s, len, i + 1);
  4887. continue;
  4888. }
  4889. pend = i;
  4890. if (i >= len || s[i] != ':')
  4891. continue;
  4892. i++;
  4893. vstart = i;
  4894. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  4895. * does not end the value. */
  4896. i = css_value_end(s, len, i);
  4897. vend = i;
  4898. while (pstart < pend && is_space(s[pstart]))
  4899. pstart++;
  4900. while (pend > pstart && is_space(s[pend - 1]))
  4901. pend--;
  4902. while (vstart < vend && is_space(s[vstart]))
  4903. vstart++;
  4904. while (vend > vstart && is_space(s[vend - 1]))
  4905. vend--;
  4906. if (vend - vstart >= 10 &&
  4907. ci_eq_n(s + vend - 10, "!important", 10)) {
  4908. important = 1;
  4909. vend -= 10;
  4910. while (vend > vstart && is_space(s[vend - 1]))
  4911. vend--;
  4912. }
  4913. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4914. char c = s[pstart + k];
  4915. if (c >= 'A' && c <= 'Z')
  4916. c = (char)(c - 'A' + 'a');
  4917. prop[k] = c;
  4918. }
  4919. prop[k] = '\0';
  4920. if (str_eq(prop, "color")) {
  4921. unsigned short c;
  4922. if (parse_color(s + vstart, vend - vstart, &c))
  4923. decl_add(out, nout, PROP_COLOR, c, important);
  4924. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  4925. /* SVG paint only: an HTML element's fill never paints
  4926. * its background (tagesschau sets fill on buttons). */
  4927. int is_fill = prop[0] == 'f';
  4928. unsigned short c;
  4929. int mode = paint_mode(s + vstart, vend - vstart, &c);
  4930. if (mode) {
  4931. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  4932. PROP_STROKE_MODE, mode, important);
  4933. if (mode == BR_PAINT_COLOR)
  4934. decl_add(out, nout, is_fill ? PROP_FILL :
  4935. PROP_STROKE, (short)c,
  4936. important);
  4937. }
  4938. } else if (str_eq(prop, "stroke-width")) {
  4939. int sw = svg_len16(s + vstart, vend - vstart);
  4940. if (sw >= 0)
  4941. decl_add(out, nout, PROP_STROKE_W, sw,
  4942. important);
  4943. } else if (str_eq(prop, "fill-rule")) {
  4944. decl_add(out, nout, PROP_FILL_RULE,
  4945. contains_ci_len(s + vstart, vend - vstart,
  4946. "evenodd") ? 1 : 0,
  4947. important);
  4948. } else if (str_eq(prop, "text-anchor")) {
  4949. int a = BR_ALIGN_LEFT;
  4950. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4951. a = BR_ALIGN_CENTER;
  4952. else if (contains_ci_len(s + vstart, vend - vstart,
  4953. "end"))
  4954. a = BR_ALIGN_RIGHT;
  4955. decl_add(out, nout, PROP_ALIGN, a, important);
  4956. } else if (str_eq(prop, "background-image")) {
  4957. add_bg_image_decl(out, nout, s + vstart,
  4958. vend - vstart, important);
  4959. } else if (str_eq(prop, "background-repeat")) {
  4960. int r = 0;
  4961. if (contains_ci_len(s + vstart, vend - vstart,
  4962. "no-repeat"))
  4963. r = 1;
  4964. else if (contains_ci_len(s + vstart, vend - vstart,
  4965. "repeat-x"))
  4966. r = 2;
  4967. else if (contains_ci_len(s + vstart, vend - vstart,
  4968. "repeat-y"))
  4969. r = 3;
  4970. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  4971. } else if (str_eq(prop, "background-position")) {
  4972. add_bg_position(out, nout, s + vstart, vend - vstart,
  4973. important);
  4974. } else if (str_eq(prop, "background-size")) {
  4975. add_bg_size(out, nout, s + vstart, vend - vstart,
  4976. important);
  4977. } else if (str_eq(prop, "background-color") ||
  4978. str_eq(prop, "background")) {
  4979. unsigned short c;
  4980. int is_bg = str_eq(prop, "background");
  4981. static char bgv[1024];
  4982. const char *vs = s + vstart;
  4983. unsigned int vl = vend - vstart;
  4984. if (is_bg) {
  4985. add_bg_image_decl(out, nout, s + vstart,
  4986. vend - vstart, important);
  4987. /* Keywords and colours are read with url()
  4988. * contents blanked: a data: SVG carries its
  4989. * own fill="rgba(...)". */
  4990. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  4991. vs = bgv;
  4992. add_bg_shorthand_extra(out, nout,
  4993. vs, vl,
  4994. important);
  4995. if (contains_ci_len(vs, vl,
  4996. "no-repeat"))
  4997. decl_add(out, nout, PROP_BG_REPEAT, 1,
  4998. important);
  4999. else if (contains_ci_len(vs, vl,
  5000. "repeat-x"))
  5001. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5002. important);
  5003. else if (contains_ci_len(vs, vl,
  5004. "repeat-y"))
  5005. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5006. important);
  5007. }
  5008. /* `transparent`/`none` are real values: they must
  5009. * beat a lower-specificity colour (and tell form
  5010. * controls the author styled them). */
  5011. if (color_is_transparent(vs, vl) ||
  5012. (vl == 4 &&
  5013. ci_eq_n(vs, "none", 4)))
  5014. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5015. else if (parse_color(vs, vl, &c))
  5016. decl_add(out, nout, PROP_BG, c, important);
  5017. else if (is_bg &&
  5018. find_color_last(vs, vl, &c))
  5019. decl_add(out, nout, PROP_BG, c, important);
  5020. } else if (str_eq(prop, "font-size")) {
  5021. unsigned short raw;
  5022. unsigned char unit;
  5023. if (parse_font_size(s + vstart, vend - vstart,
  5024. &raw, &unit)) {
  5025. decl_add(out, nout, PROP_FONT_PX, raw,
  5026. important);
  5027. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5028. important);
  5029. }
  5030. } else if (str_eq(prop, "font-weight")) {
  5031. int bold = 0;
  5032. unsigned int j;
  5033. if (vend - vstart >= 4 &&
  5034. (ci_eq_n(s + vstart, "bold", 4) ||
  5035. (vend - vstart >= 6 &&
  5036. ci_eq_n(s + vstart, "bolder", 6))))
  5037. bold = 1;
  5038. else {
  5039. int w = 0;
  5040. for (j = vstart; j < vend; j++)
  5041. if (s[j] >= '0' && s[j] <= '9')
  5042. w = w * 10 + (s[j] - '0');
  5043. /* CSS font matching: with only a 400 and a
  5044. * 700 face, 500 (Infima's "semibold" menus)
  5045. * resolves to the regular face; 600 and up
  5046. * to the bold one. */
  5047. if (w >= 600)
  5048. bold = 1;
  5049. }
  5050. decl_add(out, nout, PROP_BOLD, bold, important);
  5051. } else if (str_eq(prop, "font-style")) {
  5052. int italic = (vend - vstart >= 6 &&
  5053. ci_eq_n(s + vstart, "italic", 6));
  5054. decl_add(out, nout, PROP_ITALIC, italic, important);
  5055. } else if (str_eq(prop, "font-family")) {
  5056. unsigned int j;
  5057. int mono = 0;
  5058. int face = font_family_id(s + vstart, vend - vstart);
  5059. for (j = vstart; j + 4 <= vend; j++)
  5060. if (ci_eq_n(s + j, "mono", 4))
  5061. mono = 1;
  5062. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5063. decl_add(out, nout, PROP_MONO, mono, important);
  5064. } else if (str_eq(prop, "font")) {
  5065. add_font_shorthand(out, nout, s + vstart,
  5066. vend - vstart, important);
  5067. } else if (str_eq(prop, "text-align")) {
  5068. int a = BR_ALIGN_LEFT;
  5069. if (vend - vstart >= 6 &&
  5070. ci_eq_n(s + vstart, "center", 6))
  5071. a = BR_ALIGN_CENTER;
  5072. else if (vend - vstart >= 5 &&
  5073. ci_eq_n(s + vstart, "right", 5))
  5074. a = BR_ALIGN_RIGHT;
  5075. decl_add(out, nout, PROP_ALIGN, a, important);
  5076. } else if (str_eq(prop, "text-decoration")) {
  5077. int u = 0, stv = 0, ov = 0;
  5078. if (contains_ci_len(s + vstart, vend - vstart,
  5079. "underline"))
  5080. u = 1;
  5081. if (contains_ci_len(s + vstart, vend - vstart,
  5082. "line-through"))
  5083. stv = 1;
  5084. if (contains_ci_len(s + vstart, vend - vstart,
  5085. "overline"))
  5086. ov = 1;
  5087. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5088. decl_add(out, nout, PROP_STRIKE, stv, important);
  5089. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5090. } else if (str_eq(prop, "cursor")) {
  5091. int c = parse_cursor(s + vstart, vend - vstart);
  5092. if (c >= 0)
  5093. decl_add(out, nout, PROP_CURSOR, c, important);
  5094. } else if (str_eq(prop, "text-transform")) {
  5095. int u = (vend - vstart >= 9 &&
  5096. ci_eq_n(s + vstart, "uppercase", 9));
  5097. decl_add(out, nout, PROP_UPPER, u, important);
  5098. } else if (str_eq(prop, "white-space")) {
  5099. int nw = contains_ci_len(s + vstart, vend - vstart,
  5100. "nowrap") ||
  5101. contains_ci_len(s + vstart, vend - vstart,
  5102. "pre");
  5103. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5104. } else if (str_eq(prop, "display")) {
  5105. decl_add(out, nout, PROP_DISPLAY,
  5106. parse_display(s + vstart, vend - vstart),
  5107. important);
  5108. } else if (str_eq(prop, "visibility")) {
  5109. int h = contains_ci_len(s + vstart, vend - vstart,
  5110. "hidden") ||
  5111. contains_ci_len(s + vstart, vend - vstart,
  5112. "collapse");
  5113. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5114. } else if (str_eq(prop, "opacity")) {
  5115. unsigned int q;
  5116. int h = 1, any = 0;
  5117. for (q = vstart; q < vend; q++) {
  5118. if (s[q] >= '1' && s[q] <= '9') {
  5119. h = 0;
  5120. break;
  5121. }
  5122. if (s[q] >= '0' && s[q] <= '9')
  5123. any = 1;
  5124. }
  5125. if (!any || contains_ci_len(s + vstart, vend - vstart,
  5126. "inherit"))
  5127. h = 0;
  5128. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  5129. } else if (str_eq(prop, "overflow") ||
  5130. str_eq(prop, "overflow-x") ||
  5131. str_eq(prop, "overflow-y")) {
  5132. int h = contains_ci_len(s + vstart, vend - vstart,
  5133. "hidden") ||
  5134. contains_ci_len(s + vstart, vend - vstart,
  5135. "clip");
  5136. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5137. } else if (str_eq(prop, "float")) {
  5138. int f = 0;
  5139. if (vend - vstart >= 4 &&
  5140. ci_eq_n(s + vstart, "left", 4))
  5141. f = 1;
  5142. else if (vend - vstart >= 5 &&
  5143. ci_eq_n(s + vstart, "right", 5))
  5144. f = 2;
  5145. decl_add(out, nout, PROP_FLOAT, f, important);
  5146. } else if (str_eq(prop, "clear")) {
  5147. int c = 0;
  5148. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5149. c = 3;
  5150. else if (contains_ci_len(s + vstart, vend - vstart,
  5151. "left"))
  5152. c = 1;
  5153. else if (contains_ci_len(s + vstart, vend - vstart,
  5154. "right"))
  5155. c = 2;
  5156. decl_add(out, nout, PROP_CLEAR, c, important);
  5157. } else if (str_eq(prop, "flex-grow") ||
  5158. str_eq(prop, "flex")) {
  5159. unsigned int q = vstart;
  5160. int g = 0;
  5161. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5162. q++;
  5163. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5164. ci_eq_n(s + q, "auto", 4))
  5165. g = 1; /* flex: auto = 1 1 auto */
  5166. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5167. g = g * 10 + (s[q] - '0');
  5168. q++;
  5169. }
  5170. if (g > 100)
  5171. g = 100;
  5172. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5173. if (str_eq(prop, "flex")) {
  5174. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5175. * basis is the item's main size, which our flex
  5176. * row reads from width (Infima's grid:
  5177. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5178. * of the grow number, then any unitless shrink. */
  5179. unsigned int t;
  5180. while (q < vend && (s[q] == '.' ||
  5181. (s[q] >= '0' && s[q] <= '9')))
  5182. q++;
  5183. for (t = 0; t < 2 && q < vend; t++) {
  5184. unsigned int st, e, k2;
  5185. unsigned short bv;
  5186. unsigned char bk;
  5187. while (q < vend && is_space(s[q]))
  5188. q++;
  5189. st = q;
  5190. while (q < vend && !is_space(s[q]))
  5191. q++;
  5192. e = q;
  5193. for (k2 = st; k2 < e; k2++)
  5194. if (!(s[k2] == '.' ||
  5195. (s[k2] >= '0' &&
  5196. s[k2] <= '9')))
  5197. break;
  5198. if (k2 == e)
  5199. continue; /* shrink */
  5200. if (parse_size(s + st, e - st, &bv,
  5201. &bk) && bk && bv) {
  5202. decl_add(out, nout, PROP_WIDTH,
  5203. bv, important);
  5204. decl_add(out, nout,
  5205. PROP_WIDTH_KIND, bk,
  5206. important);
  5207. }
  5208. break;
  5209. }
  5210. }
  5211. } else if (str_eq(prop, "flex-basis")) {
  5212. unsigned short bv;
  5213. unsigned char bk;
  5214. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5215. bk && bv) {
  5216. decl_add(out, nout, PROP_WIDTH, bv, important);
  5217. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5218. important);
  5219. }
  5220. } else if (str_eq(prop, "flex-direction")) {
  5221. decl_add(out, nout, PROP_FLEX_COL,
  5222. contains_ci_len(s + vstart, vend - vstart,
  5223. "column"), important);
  5224. } else if (str_eq(prop, "flex-wrap")) {
  5225. int wrap = 0;
  5226. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5227. wrap = 2;
  5228. else if (contains_ci_len(s + vstart, vend - vstart,
  5229. "wrap"))
  5230. wrap = 1;
  5231. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5232. } else if (str_eq(prop, "justify-content")) {
  5233. int j = 0;
  5234. if (contains_ci_len(s + vstart, vend - vstart,
  5235. "space-between"))
  5236. j = 3;
  5237. else if (contains_ci_len(s + vstart, vend - vstart,
  5238. "space-around"))
  5239. j = 4;
  5240. else if (contains_ci_len(s + vstart, vend - vstart,
  5241. "space-evenly"))
  5242. j = 5;
  5243. else if (contains_ci_len(s + vstart, vend - vstart,
  5244. "center"))
  5245. j = 1;
  5246. else if (contains_ci_len(s + vstart, vend - vstart,
  5247. "flex-end") ||
  5248. contains_ci_len(s + vstart, vend - vstart,
  5249. "end"))
  5250. j = 2;
  5251. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5252. } else if (str_eq(prop, "align-self")) {
  5253. /* 0 auto (the container's align-items), else
  5254. * align-items + 1. */
  5255. int a = 1;
  5256. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5257. a = 0;
  5258. else if (contains_ci_len(s + vstart, vend - vstart,
  5259. "center"))
  5260. a = 2;
  5261. else if (contains_ci_len(s + vstart, vend - vstart,
  5262. "flex-end") ||
  5263. contains_ci_len(s + vstart, vend - vstart,
  5264. "end"))
  5265. a = 3;
  5266. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5267. } else if (str_eq(prop, "align-items")) {
  5268. int a = 0;
  5269. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5270. a = 1;
  5271. else if (contains_ci_len(s + vstart, vend - vstart,
  5272. "flex-end") ||
  5273. contains_ci_len(s + vstart, vend - vstart,
  5274. "end"))
  5275. a = 2;
  5276. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5277. } else if (str_eq(prop, "position")) {
  5278. int p = 0;
  5279. if (contains_ci_len(s + vstart, vend - vstart,
  5280. "absolute"))
  5281. p = 1;
  5282. else if (contains_ci_len(s + vstart, vend - vstart,
  5283. "fixed"))
  5284. p = 2;
  5285. else if (contains_ci_len(s + vstart, vend - vstart,
  5286. "relative"))
  5287. p = 3;
  5288. else if (contains_ci_len(s + vstart, vend - vstart,
  5289. "sticky"))
  5290. p = 4;
  5291. decl_add(out, nout, PROP_POS, p, important);
  5292. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5293. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5294. int which = str_eq(prop, "left") ? 0 :
  5295. str_eq(prop, "top") ? 1 :
  5296. str_eq(prop, "right") ? 2 : 3;
  5297. unsigned int pos = 0;
  5298. if (!(vend - vstart >= 4 &&
  5299. ci_eq_n(s + vstart, "auto", 4))) {
  5300. int v = parse_len(s + vstart, vend - vstart, &pos);
  5301. if (v > 32000)
  5302. v = 32000;
  5303. else if (v < -32000)
  5304. v = -32000;
  5305. decl_add(out, nout,
  5306. (unsigned char)(which == 0 ? PROP_LEFT :
  5307. which == 1 ? PROP_TOP :
  5308. which == 2 ? PROP_RIGHT :
  5309. PROP_BOTTOM),
  5310. v, important);
  5311. decl_add(out, nout,
  5312. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5313. which == 1 ? PROP_TOP_SET :
  5314. which == 2 ? PROP_RIGHT_SET :
  5315. PROP_BOTTOM_SET),
  5316. 1, important);
  5317. }
  5318. } else if (str_eq(prop, "z-index")) {
  5319. unsigned int q = vstart;
  5320. int sign = 1;
  5321. int v = 0;
  5322. int any = 0;
  5323. if (q < vend && s[q] == '-') {
  5324. sign = -1;
  5325. q++;
  5326. }
  5327. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5328. v = v * 10 + (s[q] - '0');
  5329. any = 1;
  5330. q++;
  5331. }
  5332. if (any) {
  5333. v *= sign;
  5334. if (v > 32000)
  5335. v = 32000;
  5336. else if (v < -32000)
  5337. v = -32000;
  5338. decl_add(out, nout, PROP_ZINDEX, v, important);
  5339. decl_add(out, nout, PROP_ZSET, 1, important);
  5340. } else if (vend - vstart >= 4 &&
  5341. ci_eq_n(s + vstart, "auto", 4)) {
  5342. decl_add(out, nout, PROP_ZSET, 0, important);
  5343. }
  5344. } else if (str_eq(prop, "transform")) {
  5345. const char *v = s + vstart;
  5346. unsigned int vl = vend - vstart;
  5347. int tx = 0, ty = 0;
  5348. int is_x = contains_ci_len(v, vl, "translatex");
  5349. int is_y = contains_ci_len(v, vl, "translatey");
  5350. unsigned int off = 0, arg;
  5351. if (contains_ci_len(v, vl, "translate")) {
  5352. while (off < vl && v[off] != '(')
  5353. off++;
  5354. if (off < vl)
  5355. off++;
  5356. if (!is_y)
  5357. tx = parse_len(v, vl, &off);
  5358. if (!is_x) {
  5359. while (off < vl &&
  5360. (is_space(v[off]) || v[off] == ','))
  5361. off++;
  5362. ty = parse_len(v, vl, &off);
  5363. }
  5364. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5365. important);
  5366. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5367. important);
  5368. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5369. important);
  5370. }
  5371. if (find_fn(v, vl, "scale", &arg)) {
  5372. unsigned int q = arg;
  5373. signed short sx = 100, sy = 100;
  5374. if (parse_fixed_100(v, vl, &q, &sx)) {
  5375. unsigned int r = q;
  5376. while (r < vl &&
  5377. (is_space(v[r]) || v[r] == ','))
  5378. r++;
  5379. if (!parse_fixed_100(v, vl, &r, &sy))
  5380. sy = sx;
  5381. decl_add(out, nout, PROP_SCALE_X, sx,
  5382. important);
  5383. decl_add(out, nout, PROP_SCALE_Y, sy,
  5384. important);
  5385. decl_add(out, nout,
  5386. PROP_TRANSFORM_PAINT, 1,
  5387. important);
  5388. decl_add(out, nout,
  5389. PROP_TRANSFORM_SET, 1,
  5390. important);
  5391. }
  5392. }
  5393. if (find_fn(v, vl, "rotate", &arg)) {
  5394. unsigned int q = arg;
  5395. signed short deg;
  5396. if (parse_fixed_100(v, vl, &q, &deg)) {
  5397. decl_add(out, nout, PROP_ROTATE, deg,
  5398. important);
  5399. decl_add(out, nout,
  5400. PROP_TRANSFORM_PAINT, 1,
  5401. important);
  5402. decl_add(out, nout,
  5403. PROP_TRANSFORM_SET, 1,
  5404. important);
  5405. }
  5406. }
  5407. } else if (str_eq(prop, "transform-origin")) {
  5408. add_transform_origin(out, nout, s + vstart,
  5409. vend - vstart, important);
  5410. } else if (str_eq(prop, "grid-template-columns") ||
  5411. str_eq(prop, "grid-template")) {
  5412. const char *v = s + vstart;
  5413. unsigned int vl = vend - vstart;
  5414. unsigned short val[2];
  5415. unsigned char kind[2];
  5416. int n, q;
  5417. if (str_eq(prop, "grid-template")) {
  5418. for (q = 0; q < (int)vl; q++)
  5419. if (v[q] == '/') {
  5420. q++;
  5421. while (q < (int)vl && is_space(v[q]))
  5422. q++;
  5423. v += q;
  5424. vl -= (unsigned int)q;
  5425. break;
  5426. }
  5427. }
  5428. if (grid_repeat_count(v, vl) >= 1) {
  5429. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5430. * Tailwind's grid-cols-N. N equal columns
  5431. * (GRID_FIXED + N); one column is plain block
  5432. * flow, which also resets a smaller breakpoint's
  5433. * count. */
  5434. int cnt = grid_repeat_count(v, vl);
  5435. decl_add(out, nout, PROP_GRID_AUTO,
  5436. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5437. important);
  5438. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5439. important);
  5440. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5441. contains_ci_len(v, vl, "auto-fill")) {
  5442. unsigned int mp = 0;
  5443. int found = 0;
  5444. unsigned short mw = 200;
  5445. unsigned char mk = 1;
  5446. for (q = 0; q + 7 <= (int)vl; q++)
  5447. if (ci_eq_n(v + q, "minmax(", 7)) {
  5448. mp = (unsigned int)q + 7;
  5449. found = 1;
  5450. break;
  5451. }
  5452. if (found)
  5453. parse_grid_track(v, vl, &mp, &mw, &mk);
  5454. if (mk != 1 || mw < 80)
  5455. mw = 200;
  5456. if (mw > 480)
  5457. mw = 480;
  5458. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5459. decl_add(out, nout, PROP_WIDTH, mw, important);
  5460. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5461. } else {
  5462. n = parse_grid_cols(v, vl, val, kind);
  5463. if (n > 0) {
  5464. decl_add(out, nout, PROP_WIDTH, val[0],
  5465. important);
  5466. decl_add(out, nout, PROP_WIDTH_KIND,
  5467. kind[0], important);
  5468. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5469. important);
  5470. }
  5471. if (n > 1) {
  5472. decl_add(out, nout, PROP_MAXW, val[1],
  5473. important);
  5474. decl_add(out, nout, PROP_MAXW_KIND,
  5475. kind[1], important);
  5476. }
  5477. }
  5478. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5479. /* From the value, not the block start: gap was
  5480. * ignored on nearly every site. */
  5481. unsigned int gp = vstart;
  5482. unsigned short gv = 0;
  5483. unsigned char gk = 1;
  5484. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5485. if (gv > 120)
  5486. gv = 120;
  5487. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5488. }
  5489. } else if (str_eq(prop, "grid-area")) {
  5490. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5491. if (slot)
  5492. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5493. } else if (str_eq(prop, "width") ||
  5494. str_eq(prop, "max-width") ||
  5495. str_eq(prop, "min-width") ||
  5496. str_eq(prop, "height") ||
  5497. str_eq(prop, "max-height") ||
  5498. str_eq(prop, "min-height")) {
  5499. unsigned short v;
  5500. unsigned char kind;
  5501. if (vend - vstart >= 5 &&
  5502. ci_eq_n(s + vstart, "calc(", 5)) {
  5503. int pct, px;
  5504. if (parse_calc(s + vstart + 5,
  5505. vend - vstart - 5, &pct, &px)) {
  5506. if (str_eq(prop, "width")) {
  5507. decl_add(out, nout, PROP_WIDTH,
  5508. 0, important);
  5509. decl_add(out, nout,
  5510. PROP_WIDTH_KIND, 3,
  5511. important);
  5512. decl_add(out, nout,
  5513. PROP_WIDTH_CALC_PCT,
  5514. pct, important);
  5515. decl_add(out, nout,
  5516. PROP_WIDTH_CALC_PX,
  5517. px, important);
  5518. } else if (str_eq(prop, "max-width")) {
  5519. decl_add(out, nout, PROP_MAXW,
  5520. 0, important);
  5521. decl_add(out, nout,
  5522. PROP_MAXW_KIND, 3,
  5523. important);
  5524. decl_add(out, nout,
  5525. PROP_MAXW_CALC_PCT,
  5526. pct, important);
  5527. decl_add(out, nout,
  5528. PROP_MAXW_CALC_PX,
  5529. px, important);
  5530. } else if (str_eq(prop, "min-width")) {
  5531. decl_add(out, nout, PROP_MINW,
  5532. 0, important);
  5533. decl_add(out, nout,
  5534. PROP_MINW_KIND, 3,
  5535. important);
  5536. decl_add(out, nout,
  5537. PROP_MINW_CALC_PCT,
  5538. pct, important);
  5539. decl_add(out, nout,
  5540. PROP_MINW_CALC_PX,
  5541. px, important);
  5542. } else if (str_eq(prop, "height")) {
  5543. decl_add(out, nout, PROP_HEIGHT,
  5544. 0, important);
  5545. decl_add(out, nout,
  5546. PROP_HEIGHT_KIND, 3,
  5547. important);
  5548. decl_add(out, nout,
  5549. PROP_HEIGHT_CALC_PCT,
  5550. pct, important);
  5551. decl_add(out, nout,
  5552. PROP_HEIGHT_CALC_PX,
  5553. px, important);
  5554. } else if (str_eq(prop, "max-height")) {
  5555. decl_add(out, nout, PROP_MAXH,
  5556. 0, important);
  5557. decl_add(out, nout,
  5558. PROP_MAXH_KIND, 3,
  5559. important);
  5560. decl_add(out, nout,
  5561. PROP_MAXH_CALC_PCT,
  5562. pct, important);
  5563. decl_add(out, nout,
  5564. PROP_MAXH_CALC_PX,
  5565. px, important);
  5566. } else {
  5567. decl_add(out, nout, PROP_MINH,
  5568. 0, important);
  5569. decl_add(out, nout,
  5570. PROP_MINH_KIND, 3,
  5571. important);
  5572. decl_add(out, nout,
  5573. PROP_MINH_CALC_PCT,
  5574. pct, important);
  5575. decl_add(out, nout,
  5576. PROP_MINH_CALC_PX,
  5577. px, important);
  5578. }
  5579. }
  5580. continue;
  5581. }
  5582. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  5583. if (str_eq(prop, "width")) {
  5584. decl_add(out, nout, PROP_WIDTH, v, important);
  5585. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  5586. important);
  5587. } else if (str_eq(prop, "max-width")) {
  5588. decl_add(out, nout, PROP_MAXW, v, important);
  5589. decl_add(out, nout, PROP_MAXW_KIND, kind,
  5590. important);
  5591. } else if (str_eq(prop, "min-width")) {
  5592. decl_add(out, nout, PROP_MINW, v, important);
  5593. decl_add(out, nout, PROP_MINW_KIND, kind,
  5594. important);
  5595. } else if (str_eq(prop, "max-height")) {
  5596. decl_add(out, nout, PROP_MAXH, v, important);
  5597. decl_add(out, nout, PROP_MAXH_KIND, kind,
  5598. important);
  5599. } else if (str_eq(prop, "min-height")) {
  5600. decl_add(out, nout, PROP_MINH, v, important);
  5601. decl_add(out, nout, PROP_MINH_KIND, kind,
  5602. important);
  5603. } else {
  5604. decl_add(out, nout, PROP_HEIGHT, v, important);
  5605. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  5606. important);
  5607. }
  5608. }
  5609. } else if (str_eq(prop, "content")) {
  5610. decl_add(out, nout, PROP_CONTENT,
  5611. css_collect_only ? 0 :
  5612. parse_content(d, s + vstart, vend - vstart),
  5613. important);
  5614. } else if (str_eq(prop, "aspect-ratio")) {
  5615. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  5616. * preferred height of an auto-height box. */
  5617. unsigned int q = vstart;
  5618. unsigned int a[2] = { 0, 1 }, k;
  5619. for (k = 0; k < 2; k++) {
  5620. unsigned int whole = 0, frac = 0, sc = 1;
  5621. while (q < vend && is_space(s[q]))
  5622. q++;
  5623. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  5624. s[q] != '.'))
  5625. break;
  5626. while (q < vend && s[q] >= '0' && s[q] <= '9')
  5627. whole = whole * 10 +
  5628. (unsigned int)(s[q++] - '0');
  5629. if (q < vend && s[q] == '.') {
  5630. q++;
  5631. while (q < vend && s[q] >= '0' &&
  5632. s[q] <= '9') {
  5633. if (sc < 100) {
  5634. frac = frac * 10 +
  5635. (unsigned int)(s[q] - '0');
  5636. sc *= 10;
  5637. }
  5638. q++;
  5639. }
  5640. }
  5641. a[k] = whole * 100 + frac * (100 / sc);
  5642. while (q < vend && is_space(s[q]))
  5643. q++;
  5644. if (k == 0 && (q >= vend || s[q] != '/')) {
  5645. a[1] = 100;
  5646. break;
  5647. }
  5648. q++;
  5649. }
  5650. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5651. a[0] = a[1] = 0;
  5652. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5653. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5654. } else if (str_eq(prop, "box-sizing")) {
  5655. int bb = (vend - vstart >= 10 &&
  5656. ci_eq_n(s + vstart, "border-box", 10));
  5657. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5658. } else if (str_eq(prop, "line-height")) {
  5659. unsigned short px;
  5660. unsigned char pct;
  5661. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5662. if (px) {
  5663. decl_add(out, nout, PROP_LH_PX, px, important);
  5664. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5665. } else {
  5666. decl_add(out, nout, PROP_LH_PX, 0, important);
  5667. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5668. }
  5669. }
  5670. } else if (str_eq(prop, "list-style") ||
  5671. str_eq(prop, "list-style-type")) {
  5672. int ls = BR_LIST_AUTO;
  5673. if (contains_ci_len(s + vstart, vend - vstart,
  5674. "none"))
  5675. ls = BR_LIST_NONE;
  5676. else if (contains_ci_len(s + vstart, vend - vstart,
  5677. "circle"))
  5678. ls = BR_LIST_CIRCLE;
  5679. else if (contains_ci_len(s + vstart, vend - vstart,
  5680. "square"))
  5681. ls = BR_LIST_SQUARE;
  5682. else if (contains_ci_len(s + vstart, vend - vstart,
  5683. "lower-alpha") ||
  5684. contains_ci_len(s + vstart, vend - vstart,
  5685. "lower-latin"))
  5686. ls = BR_LIST_LOWER_ALPHA;
  5687. else if (contains_ci_len(s + vstart, vend - vstart,
  5688. "upper-alpha") ||
  5689. contains_ci_len(s + vstart, vend - vstart,
  5690. "upper-latin"))
  5691. ls = BR_LIST_UPPER_ALPHA;
  5692. else if (contains_ci_len(s + vstart, vend - vstart,
  5693. "lower-roman"))
  5694. ls = BR_LIST_LOWER_ROMAN;
  5695. else if (contains_ci_len(s + vstart, vend - vstart,
  5696. "upper-roman"))
  5697. ls = BR_LIST_UPPER_ROMAN;
  5698. else if (contains_ci_len(s + vstart, vend - vstart,
  5699. "decimal"))
  5700. ls = BR_LIST_DECIMAL;
  5701. else if (contains_ci_len(s + vstart, vend - vstart,
  5702. "disc"))
  5703. ls = BR_LIST_DISC;
  5704. decl_add(out, nout, PROP_LIST_NONE,
  5705. ls == BR_LIST_NONE, important);
  5706. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5707. } else if (str_eq(prop, "border-radius") ||
  5708. str_eq(prop, "border-top-left-radius") ||
  5709. str_eq(prop, "border-top-right-radius") ||
  5710. str_eq(prop, "border-bottom-right-radius") ||
  5711. str_eq(prop, "border-bottom-left-radius")) {
  5712. unsigned short v[4];
  5713. unsigned char k[4];
  5714. unsigned int pos = 0;
  5715. int n = 0;
  5716. while (pos < vend - vstart && n < 4) {
  5717. while (pos < vend - vstart &&
  5718. is_space(s[vstart + pos]))
  5719. pos++;
  5720. if (pos >= vend - vstart ||
  5721. s[vstart + pos] == '/')
  5722. break;
  5723. if (!parse_radius_value(s + vstart,
  5724. vend - vstart, &pos,
  5725. &v[n], &k[n]))
  5726. break;
  5727. n++;
  5728. }
  5729. if (n) {
  5730. unsigned short r[4];
  5731. unsigned char rk[4];
  5732. int i;
  5733. if (n == 1) {
  5734. for (i = 0; i < 4; i++) {
  5735. r[i] = v[0];
  5736. rk[i] = k[0];
  5737. }
  5738. } else if (n == 2) {
  5739. r[0] = r[2] = v[0];
  5740. r[1] = r[3] = v[1];
  5741. rk[0] = rk[2] = k[0];
  5742. rk[1] = rk[3] = k[1];
  5743. } else if (n == 3) {
  5744. r[0] = v[0];
  5745. r[1] = r[3] = v[1];
  5746. r[2] = v[2];
  5747. rk[0] = k[0];
  5748. rk[1] = rk[3] = k[1];
  5749. rk[2] = k[2];
  5750. } else {
  5751. for (i = 0; i < 4; i++) {
  5752. r[i] = v[i];
  5753. rk[i] = k[i];
  5754. }
  5755. }
  5756. for (i = 0; i < 4; i++)
  5757. r[i] = rk[i] == 2 ? 255 :
  5758. (r[i] > 255 ? 255 : r[i]);
  5759. if (str_eq(prop, "border-top-left-radius")) {
  5760. decl_add(out, nout, PROP_RADIUS_TL,
  5761. (short)r[0], important);
  5762. } else if (str_eq(prop,
  5763. "border-top-right-radius")) {
  5764. decl_add(out, nout, PROP_RADIUS_TR,
  5765. (short)r[0], important);
  5766. } else if (str_eq(prop,
  5767. "border-bottom-right-radius")) {
  5768. decl_add(out, nout, PROP_RADIUS_BR,
  5769. (short)r[0], important);
  5770. } else if (str_eq(prop,
  5771. "border-bottom-left-radius")) {
  5772. decl_add(out, nout, PROP_RADIUS_BL,
  5773. (short)r[0], important);
  5774. } else {
  5775. decl_add(out, nout, PROP_RADIUS_TL,
  5776. (short)r[0], important);
  5777. decl_add(out, nout, PROP_RADIUS_TR,
  5778. (short)r[1], important);
  5779. decl_add(out, nout, PROP_RADIUS_BR,
  5780. (short)r[2], important);
  5781. decl_add(out, nout, PROP_RADIUS_BL,
  5782. (short)r[3], important);
  5783. }
  5784. }
  5785. } else if (str_eq(prop, "border") ||
  5786. str_eq(prop, "border-top") ||
  5787. str_eq(prop, "border-right") ||
  5788. str_eq(prop, "border-bottom") ||
  5789. str_eq(prop, "border-left") ||
  5790. str_eq(prop, "border-color") ||
  5791. str_eq(prop, "border-width") ||
  5792. str_eq(prop, "border-style") ||
  5793. str_eq(prop, "border-top-width") ||
  5794. str_eq(prop, "border-right-width") ||
  5795. str_eq(prop, "border-bottom-width") ||
  5796. str_eq(prop, "border-left-width") ||
  5797. str_eq(prop, "border-top-color") ||
  5798. str_eq(prop, "border-right-color") ||
  5799. str_eq(prop, "border-bottom-color") ||
  5800. str_eq(prop, "border-left-color") ||
  5801. str_eq(prop, "border-top-style") ||
  5802. str_eq(prop, "border-right-style") ||
  5803. str_eq(prop, "border-bottom-style") ||
  5804. str_eq(prop, "border-left-style")) {
  5805. const char *v = s + vstart;
  5806. unsigned int vl = vend - vstart;
  5807. unsigned int pos = 0, ts, te;
  5808. int widths[4], colors[4], styles[4], trans[4];
  5809. int nw = 0, nc = 0, ns = 0;
  5810. int side = -1;
  5811. int is_wl = str_eq(prop, "border-width");
  5812. int is_cl = str_eq(prop, "border-color");
  5813. int is_sl = str_eq(prop, "border-style");
  5814. int i;
  5815. if (str_eq(prop, "border-top")) side = 0;
  5816. else if (str_eq(prop, "border-right")) side = 1;
  5817. else if (str_eq(prop, "border-bottom")) side = 2;
  5818. else if (str_eq(prop, "border-left")) side = 3;
  5819. else if (str_eq(prop, "border-top-width")) {
  5820. side = 0; is_wl = 2;
  5821. } else if (str_eq(prop, "border-right-width")) {
  5822. side = 1; is_wl = 2;
  5823. } else if (str_eq(prop, "border-bottom-width")) {
  5824. side = 2; is_wl = 2;
  5825. } else if (str_eq(prop, "border-left-width")) {
  5826. side = 3; is_wl = 2;
  5827. } else if (str_eq(prop, "border-top-color")) {
  5828. side = 0; is_cl = 2;
  5829. } else if (str_eq(prop, "border-right-color")) {
  5830. side = 1; is_cl = 2;
  5831. } else if (str_eq(prop, "border-bottom-color")) {
  5832. side = 2; is_cl = 2;
  5833. } else if (str_eq(prop, "border-left-color")) {
  5834. side = 3; is_cl = 2;
  5835. } else if (str_eq(prop, "border-top-style")) {
  5836. side = 0; is_sl = 2;
  5837. } else if (str_eq(prop, "border-right-style")) {
  5838. side = 1; is_sl = 2;
  5839. } else if (str_eq(prop, "border-bottom-style")) {
  5840. side = 2; is_sl = 2;
  5841. } else if (str_eq(prop, "border-left-style")) {
  5842. side = 3; is_sl = 2;
  5843. }
  5844. if (is_wl) {
  5845. while (nw < 4 && next_token(v, vl, &pos,
  5846. &ts, &te)) {
  5847. int x;
  5848. if (!parse_len_token(v + ts, te - ts,
  5849. &x))
  5850. break;
  5851. widths[nw++] = x;
  5852. }
  5853. if (nw) {
  5854. if (is_wl == 1)
  5855. expand_four(widths, nw);
  5856. if (side >= 0 && is_wl == 2) {
  5857. decl_add(out, nout,
  5858. PROP_BW_T + side,
  5859. widths[0], important);
  5860. } else {
  5861. for (i = 0; i < 4; i++)
  5862. decl_add(out, nout,
  5863. PROP_BW_T + i,
  5864. widths[i],
  5865. important);
  5866. }
  5867. }
  5868. } else if (is_cl) {
  5869. unsigned short c;
  5870. int t;
  5871. while (nc < 4 && next_token(v, vl, &pos,
  5872. &ts, &te)) {
  5873. t = color_is_transparent(v + ts,
  5874. te - ts);
  5875. if (!t && !parse_color(v + ts,
  5876. te - ts, &c))
  5877. break;
  5878. colors[nc] = t ? 0 : c;
  5879. trans[nc] = t;
  5880. nc++;
  5881. }
  5882. if (nc) {
  5883. if (is_cl == 1) {
  5884. expand_four(colors, nc);
  5885. expand_four(trans, nc);
  5886. }
  5887. if (side >= 0 && is_cl == 2) {
  5888. decl_add(out, nout,
  5889. PROP_BC_T + side,
  5890. colors[0], important);
  5891. decl_add(out, nout,
  5892. PROP_BT_T + side,
  5893. trans[0], important);
  5894. } else {
  5895. for (i = 0; i < 4; i++) {
  5896. decl_add(out, nout,
  5897. PROP_BC_T + i,
  5898. colors[i],
  5899. important);
  5900. decl_add(out, nout,
  5901. PROP_BT_T + i,
  5902. trans[i],
  5903. important);
  5904. }
  5905. }
  5906. }
  5907. } else if (is_sl) {
  5908. while (ns < 4 && next_token(v, vl, &pos,
  5909. &ts, &te)) {
  5910. int stv;
  5911. if ((te - ts == 4 &&
  5912. ci_eq_n(v + ts, "none", 4)) ||
  5913. (te - ts == 6 &&
  5914. ci_eq_n(v + ts, "hidden", 6)))
  5915. stv = 0;
  5916. else if (te - ts == 6 &&
  5917. ci_eq_n(v + ts,
  5918. "dashed", 6))
  5919. stv = 2;
  5920. else if (te - ts == 6 &&
  5921. ci_eq_n(v + ts,
  5922. "dotted", 6))
  5923. stv = 3;
  5924. else
  5925. stv = 1;
  5926. styles[ns++] = stv;
  5927. }
  5928. if (ns) {
  5929. if (is_sl == 1)
  5930. expand_four(styles, ns);
  5931. if (is_sl == 2 && side >= 0) {
  5932. if (!styles[0])
  5933. decl_add(out, nout,
  5934. PROP_BW_T + side,
  5935. 0, important);
  5936. else
  5937. decl_add(out, nout,
  5938. PROP_BS_T + side,
  5939. styles[0] - 1,
  5940. important);
  5941. } else {
  5942. for (i = 0; i < 4; i++) {
  5943. if (!styles[i])
  5944. decl_add(out, nout,
  5945. PROP_BW_T + i,
  5946. 0,
  5947. important);
  5948. else
  5949. decl_add(out, nout,
  5950. PROP_BS_T + i,
  5951. styles[i] - 1,
  5952. important);
  5953. }
  5954. }
  5955. }
  5956. } else {
  5957. int bw = -1, bc = -1, style_none = 0;
  5958. int bstyle = -1, btrans = 0;
  5959. while (next_token(v, vl, &pos, &ts, &te)) {
  5960. unsigned short c;
  5961. int x;
  5962. if ((te - ts == 4 &&
  5963. ci_eq_n(v + ts, "none", 4)) ||
  5964. (te - ts == 6 &&
  5965. ci_eq_n(v + ts, "hidden", 6))) {
  5966. style_none = 1;
  5967. } else if (te - ts == 6 &&
  5968. ci_eq_n(v + ts, "dashed", 6)) {
  5969. bstyle = BR_BORDER_DASHED;
  5970. } else if (te - ts == 6 &&
  5971. ci_eq_n(v + ts, "dotted", 6)) {
  5972. bstyle = BR_BORDER_DOTTED;
  5973. } else if (te - ts == 5 &&
  5974. ci_eq_n(v + ts, "solid", 5)) {
  5975. bstyle = BR_BORDER_SOLID;
  5976. } else if (parse_len_token(v + ts,
  5977. te - ts, &x)) {
  5978. bw = x;
  5979. } else if (color_is_transparent(v + ts,
  5980. te - ts)) {
  5981. bc = 0;
  5982. btrans = 1;
  5983. } else if (parse_color(v + ts,
  5984. te - ts, &c)) {
  5985. bc = c;
  5986. btrans = 0;
  5987. }
  5988. }
  5989. if (style_none)
  5990. bw = 0;
  5991. if (bc >= 0 && bw < 0)
  5992. bw = 1;
  5993. if (side < 0) {
  5994. if (bw >= 0)
  5995. decl_add(out, nout,
  5996. PROP_BORDER_W, bw,
  5997. important);
  5998. if (bc >= 0)
  5999. decl_add(out, nout,
  6000. PROP_BORDER_COLOR, bc,
  6001. important);
  6002. if (bc >= 0)
  6003. for (i = 0; i < 4; i++)
  6004. decl_add(out, nout,
  6005. PROP_BT_T + i,
  6006. btrans,
  6007. important);
  6008. if (bstyle >= 0)
  6009. for (i = 0; i < 4; i++)
  6010. decl_add(out, nout,
  6011. PROP_BS_T + i,
  6012. bstyle,
  6013. important);
  6014. } else {
  6015. if (bw >= 0)
  6016. decl_add(out, nout,
  6017. PROP_BW_T + side, bw,
  6018. important);
  6019. if (bc >= 0)
  6020. decl_add(out, nout,
  6021. PROP_BC_T + side, bc,
  6022. important);
  6023. if (bc >= 0)
  6024. decl_add(out, nout,
  6025. PROP_BT_T + side,
  6026. btrans, important);
  6027. if (bstyle >= 0)
  6028. decl_add(out, nout,
  6029. PROP_BS_T + side,
  6030. bstyle, important);
  6031. }
  6032. }
  6033. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6034. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6035. unsigned int pos = 0;
  6036. int is_pad = prop[0] == 'p';
  6037. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6038. while (pos < vend - vstart && nv < 4) {
  6039. units[nv] = 0;
  6040. if (parse_margin_calc(s + vstart,
  6041. vend - vstart, &pos,
  6042. &vals[nv], &cpx[nv])) {
  6043. units[nv] = 5;
  6044. } else {
  6045. vals[nv] = parse_margin_len(
  6046. s + vstart, vend - vstart,
  6047. &pos, &units[nv]);
  6048. }
  6049. nv++;
  6050. while (pos < vend - vstart &&
  6051. is_space(s[vstart + pos]))
  6052. pos++;
  6053. }
  6054. if (nv == 1) {
  6055. vals[1] = vals[2] = vals[3] = vals[0];
  6056. units[1] = units[2] = units[3] = units[0];
  6057. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6058. } else if (nv == 2) {
  6059. vals[2] = vals[0];
  6060. vals[3] = vals[1];
  6061. units[2] = units[0];
  6062. units[3] = units[1];
  6063. cpx[2] = cpx[0];
  6064. cpx[3] = cpx[1];
  6065. } else if (nv == 3) {
  6066. vals[3] = vals[1];
  6067. units[3] = units[1];
  6068. cpx[3] = cpx[1];
  6069. }
  6070. if (nv > 0) {
  6071. int k;
  6072. for (k = 0; k < 4; k++) {
  6073. int v = vals[k];
  6074. int u = units[k];
  6075. int au = u == 4;
  6076. if (au)
  6077. v = 0;
  6078. if (is_pad && au)
  6079. u = 0;
  6080. decl_add(out, nout,
  6081. (unsigned char)(base + k), v,
  6082. important);
  6083. decl_add(out, nout,
  6084. (unsigned char)
  6085. ((is_pad ? PROP_PT_U :
  6086. PROP_MT_U) + k),
  6087. u, important);
  6088. if (u == 5)
  6089. decl_add(out, nout,
  6090. (unsigned char)
  6091. ((is_pad ? PROP_PT_CPX :
  6092. PROP_MT_CPX) + k),
  6093. cpx[k], important);
  6094. if (!is_pad && (k == 1 || k == 3))
  6095. decl_add(out, nout,
  6096. k == 1 ? PROP_MR_AUTO :
  6097. PROP_ML_AUTO,
  6098. au, important);
  6099. }
  6100. }
  6101. } else if (str_eq(prop, "margin-top") ||
  6102. str_eq(prop, "margin-right") ||
  6103. str_eq(prop, "margin-bottom") ||
  6104. str_eq(prop, "margin-left") ||
  6105. str_eq(prop, "padding-top") ||
  6106. str_eq(prop, "padding-right") ||
  6107. str_eq(prop, "padding-bottom") ||
  6108. str_eq(prop, "padding-left")) {
  6109. unsigned int pos = 0;
  6110. int unit = 0;
  6111. int v = 0, cpx = 0;
  6112. unsigned char p = PROP_MT;
  6113. const char *side;
  6114. int au;
  6115. if (parse_margin_calc(s + vstart, vend - vstart,
  6116. &pos, &v, &cpx))
  6117. unit = 5;
  6118. else
  6119. v = parse_margin_len(s + vstart,
  6120. vend - vstart, &pos,
  6121. &unit);
  6122. au = unit == 4;
  6123. if (au)
  6124. v = 0;
  6125. if (prop[0] == 'p') {
  6126. p = PROP_PT;
  6127. if (au)
  6128. unit = 0;
  6129. }
  6130. side = prop + (prop[0] == 'p' ? 8 : 7);
  6131. if (str_eq(side, "right"))
  6132. p = (unsigned char)(p + 1);
  6133. else if (str_eq(side, "bottom"))
  6134. p = (unsigned char)(p + 2);
  6135. else if (str_eq(side, "left"))
  6136. p = (unsigned char)(p + 3);
  6137. decl_add(out, nout, p, v, important);
  6138. decl_add(out, nout,
  6139. (unsigned char)((prop[0] == 'p' ?
  6140. PROP_PT_U : PROP_MT_U) +
  6141. (p - (prop[0] == 'p' ?
  6142. PROP_PT : PROP_MT))),
  6143. unit, important);
  6144. if (unit == 5)
  6145. decl_add(out, nout,
  6146. (unsigned char)((prop[0] == 'p' ?
  6147. PROP_PT_CPX : PROP_MT_CPX) +
  6148. (p - (prop[0] == 'p' ?
  6149. PROP_PT : PROP_MT))),
  6150. cpx, important);
  6151. if (str_eq(side, "right"))
  6152. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6153. else if (str_eq(side, "left"))
  6154. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6155. }
  6156. }
  6157. }
  6158. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6159. * nested blocks. This is the mobile/base rules path: media enhancements are
  6160. * deliberately ignored for now. */
  6161. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6162. unsigned int i)
  6163. {
  6164. unsigned int j = i;
  6165. while (j < len && s[j] != '{' && s[j] != ';')
  6166. j++;
  6167. if (j >= len)
  6168. return len;
  6169. if (s[j] == ';')
  6170. return j + 1;
  6171. j++;
  6172. return css_skip_nested(s, len, j);
  6173. }
  6174. static unsigned int viewport_w = 1024;
  6175. static unsigned short theme_text = 0x0000;
  6176. static unsigned short theme_bg = 0xFFFF;
  6177. static unsigned short theme_panel = 0xEF5D;
  6178. static unsigned short theme_link = 0x001B;
  6179. void br_set_theme(unsigned short text, unsigned short bg,
  6180. unsigned short panel, unsigned short link)
  6181. {
  6182. theme_text = text;
  6183. theme_bg = bg;
  6184. theme_panel = panel;
  6185. theme_link = link;
  6186. }
  6187. void br_set_viewport(unsigned int width)
  6188. {
  6189. viewport_w = width ? width : 1024;
  6190. }
  6191. static unsigned int media_len_px(const char *s, unsigned int len)
  6192. {
  6193. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6194. int em = 0;
  6195. while (i < len && is_space(s[i]))
  6196. i++;
  6197. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6198. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6199. i++;
  6200. }
  6201. if (i < len && s[i] == '.') {
  6202. i++;
  6203. /* Consume every fractional digit even when there are more
  6204. * than the three we keep: leaving the tail behind made the
  6205. * unit check miss "em" (74.9375em became 74px). */
  6206. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6207. if (scale < 1000) {
  6208. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6209. scale *= 10;
  6210. }
  6211. i++;
  6212. }
  6213. }
  6214. while (i < len && is_space(s[i]))
  6215. i++;
  6216. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6217. em = 1;
  6218. i += 2;
  6219. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6220. em = 1;
  6221. i += 3;
  6222. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6223. i += 2;
  6224. }
  6225. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6226. if (em)
  6227. return v * 16 / 100;
  6228. return v / 100;
  6229. }
  6230. /* One parenthesised media feature: min-width/max-width and the modern
  6231. * (width <= 600px) form. Unknown features are treated as true. */
  6232. static int media_feature_matches(const char *s, unsigned int len)
  6233. {
  6234. unsigned int i;
  6235. while (len && is_space(*s)) { s++; len--; }
  6236. while (len && is_space(s[len - 1])) len--;
  6237. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6238. /* There is no user-selectable colour scheme. Ignore both
  6239. * variants so a page's base palette is used; sites whose base
  6240. * is dark (like the project docs) then stay dark instead of
  6241. * flipping to their light media override. */
  6242. return 0;
  6243. }
  6244. {
  6245. /* Environment features with a known answer: a mouse-driven
  6246. * screen with normal contrast and no forced colours. Google
  6247. * puts `border:1px solid CanvasText` on its buttons inside
  6248. * `(forced-colors:active)`. */
  6249. static const struct { const char *name, *yes; } env[] = {
  6250. { "forced-colors", "none" },
  6251. { "-ms-high-contrast", "" },
  6252. { "prefers-contrast", "no-preference" },
  6253. { "inverted-colors", "none" },
  6254. { "any-hover", "hover" },
  6255. { "hover", "hover" },
  6256. { "any-pointer", "fine" },
  6257. { "pointer", "fine" },
  6258. { "prefers-reduced-motion", "no-preference" },
  6259. { "prefers-reduced-transparency", "no-preference" },
  6260. { "display-mode", "browser" },
  6261. };
  6262. unsigned int k;
  6263. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6264. unsigned int nl = s_len(env[k].name), j;
  6265. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6266. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6267. continue;
  6268. for (j = nl; j < len && s[j] != ':'; j++)
  6269. ;
  6270. if (j >= len) /* boolean form: (hover) */
  6271. return env[k].yes[0] &&
  6272. !ci_eq_n(env[k].yes, "none", 4);
  6273. j++;
  6274. while (j < len && is_space(s[j]))
  6275. j++;
  6276. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6277. ci_eq_n(s + j, env[k].yes, len - j);
  6278. }
  6279. }
  6280. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6281. for (i = 9; i < len && s[i] != ':'; i++)
  6282. ;
  6283. if (i < len)
  6284. return viewport_w >= media_len_px(s + i + 1,
  6285. len - i - 1);
  6286. return 1;
  6287. }
  6288. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6289. for (i = 9; i < len && s[i] != ':'; i++)
  6290. ;
  6291. if (i < len)
  6292. return viewport_w <= media_len_px(s + i + 1,
  6293. len - i - 1);
  6294. return 1;
  6295. }
  6296. for (i = 0; i < len; i++) {
  6297. if (s[i] == '<' || s[i] == '>') {
  6298. int max = s[i] == '<';
  6299. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6300. if (i + 1 < len && s[i + 1] == '=')
  6301. v = media_len_px(s + i + 2, len - i - 2);
  6302. return max ? viewport_w <= v : viewport_w >= v;
  6303. }
  6304. }
  6305. return 1;
  6306. }
  6307. static int media_query_matches(const char *s, unsigned int len)
  6308. {
  6309. unsigned int i;
  6310. if (contains_ci_len(s, len, "print") &&
  6311. !contains_ci_len(s, len, "screen"))
  6312. return 0;
  6313. if (contains_ci_len(s, len, "not "))
  6314. return 0;
  6315. for (i = 0; i < len; i++) {
  6316. if (s[i] == '(') {
  6317. unsigned int st = ++i;
  6318. while (i < len && s[i] != ')')
  6319. i++;
  6320. if (!media_feature_matches(s + st, i - st))
  6321. return 0;
  6322. }
  6323. }
  6324. return 1;
  6325. }
  6326. static int media_list_matches(const char *s, unsigned int len)
  6327. {
  6328. unsigned int i = 0;
  6329. while (i < len) {
  6330. unsigned int st = i;
  6331. while (i < len && s[i] != ',')
  6332. i++;
  6333. if (media_query_matches(s + st, i - st))
  6334. return 1;
  6335. i++;
  6336. }
  6337. return 0;
  6338. }
  6339. int br_media_matches(const char *query)
  6340. {
  6341. return media_list_matches(query, s_len(query));
  6342. }
  6343. static unsigned int rule_hash(const char *s, unsigned int len)
  6344. {
  6345. unsigned int h = 2166136261u, i;
  6346. for (i = 0; i < len; i++) {
  6347. char c = s[i];
  6348. if (c >= 'A' && c <= 'Z')
  6349. c = (char)(c - 'A' + 'a');
  6350. h ^= (unsigned char)c;
  6351. h *= 16777619u;
  6352. }
  6353. return h % BR_RULE_HASH;
  6354. }
  6355. /* Index a parsed rule by the most selective component of its rightmost
  6356. * compound selector. br_style_for() then only visits rules that can
  6357. * possibly match a node instead of scanning every rule. */
  6358. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6359. unsigned int len);
  6360. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6361. char *out, unsigned int cap, unsigned int *olen);
  6362. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6363. const char *key, unsigned int len)
  6364. {
  6365. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6366. * "md:flex", the class the element carries, not under "md". */
  6367. char cname[96], iname[96];
  6368. unsigned int i, clen = 0, ilen = 0;
  6369. unsigned short *head;
  6370. for (i = 0; i < len; i++) {
  6371. if (key[i] == '\\') {
  6372. i++;
  6373. continue;
  6374. }
  6375. if (key[i] == '(' || key[i] == '[') {
  6376. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6377. i = sel_skip_group(key, i, len) - 1;
  6378. continue;
  6379. }
  6380. if (key[i] == '.' || key[i] == '#') {
  6381. char tmp[96];
  6382. unsigned int n, e;
  6383. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6384. if (n && n + 1 < sizeof(tmp)) {
  6385. if (key[i] == '#') {
  6386. memcpy(iname, tmp, n);
  6387. ilen = n;
  6388. } else {
  6389. memcpy(cname, tmp, n);
  6390. clen = n;
  6391. }
  6392. }
  6393. i = e - 1;
  6394. }
  6395. }
  6396. if (ilen)
  6397. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6398. else if (clen)
  6399. head = &d->rule_bucket[rule_hash(cname, clen)];
  6400. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6401. head = &d->rule_tag[BR_TAG_HTML];
  6402. else if (len && is_ident(key[0])) {
  6403. char name[16];
  6404. unsigned int n = 0, tag;
  6405. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6406. name[n] = key[n];
  6407. n++;
  6408. }
  6409. name[n] = '\0';
  6410. tag = br_tag_from_name(name);
  6411. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6412. head = &d->rule_tag[tag];
  6413. else
  6414. head = &d->rule_any;
  6415. } else {
  6416. head = &d->rule_any;
  6417. }
  6418. d->rule_next[idx] = *head;
  6419. *head = (unsigned short)idx;
  6420. }
  6421. #define SEL_PARTS 16
  6422. struct sel_part {
  6423. unsigned short st, en;
  6424. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6425. };
  6426. static unsigned int selector_specificity(const char *s, unsigned int len);
  6427. static int css_selector_supported(const char *s, unsigned int len);
  6428. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6429. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6430. unsigned int *st, unsigned int *en);
  6431. static int css_collect_only;
  6432. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6433. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6434. * selector with any other pseudo-element. */
  6435. static int css_pseudo_element(char *sel, unsigned int *len)
  6436. {
  6437. static const struct {
  6438. const char *s;
  6439. unsigned char l, pe;
  6440. } pe[] = {
  6441. { "::before", 8, 1 }, { "::after", 7, 2 },
  6442. { ":before", 7, 1 }, { ":after", 6, 2 },
  6443. };
  6444. unsigned int k, n = *len;
  6445. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6446. unsigned int l = pe[k].l;
  6447. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6448. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6449. continue; /* matched the tail of "::x" */
  6450. n -= l;
  6451. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6452. sel[n - 1] == '+' || sel[n - 1] == '~')
  6453. sel[n++] = '*';
  6454. sel[n] = '\0';
  6455. *len = n;
  6456. return pe[k].pe;
  6457. }
  6458. }
  6459. for (k = 0; k + 1 < n; k++)
  6460. if (sel[k] == ':' && sel[k + 1] == ':')
  6461. return -1;
  6462. return 0;
  6463. }
  6464. /* Does a declaration block use or define a custom property whose value
  6465. * depends on the element (defined by some non-root rule)? Such rules are
  6466. * re-expanded per element at cascade time. */
  6467. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6468. unsigned int len)
  6469. {
  6470. unsigned char f = 0;
  6471. unsigned int i, k;
  6472. for (i = 0; i + 6 < len; i++) {
  6473. unsigned int st, e, vi;
  6474. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6475. continue;
  6476. st = i + 4;
  6477. while (st < len && is_space(s[st]))
  6478. st++;
  6479. e = st;
  6480. while (e < len && s[e] != ',' && s[e] != ')' &&
  6481. !is_space(s[e]))
  6482. e++;
  6483. vi = css_var_find(d, s + st, e - st);
  6484. if (vi < BR_MAX_VARS &&
  6485. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6486. f |= BR_RULE_USES_SCOPED;
  6487. break;
  6488. }
  6489. }
  6490. css_local_collect(s, len);
  6491. for (k = 0; k < css_nlocal; k++) {
  6492. unsigned int vi = css_var_find(d, css_local[k].name,
  6493. css_local[k].nlen);
  6494. if (vi < BR_MAX_VARS &&
  6495. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6496. f |= BR_RULE_DEFINES_SCOPED;
  6497. }
  6498. css_nlocal = 0;
  6499. return f;
  6500. }
  6501. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6502. {
  6503. unsigned int i = 0;
  6504. while (i < len && d->nrules < d->rule_cap) {
  6505. unsigned int sstart, send, bstart, bend;
  6506. struct br_decl decls[BR_DECLS];
  6507. unsigned int nd, scope_off = 0;
  6508. unsigned char scope_flags = 0;
  6509. while (i < len && is_space(s[i]))
  6510. i++;
  6511. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6512. i += 2;
  6513. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6514. i++;
  6515. i += 2;
  6516. continue;
  6517. }
  6518. if (i >= len || s[i] == '}')
  6519. break;
  6520. if (s[i] == '@') {
  6521. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6522. unsigned int q = i + 6;
  6523. while (q < len && s[q] != '{')
  6524. q++;
  6525. if (q < len &&
  6526. media_list_matches(s + i + 6,
  6527. q - (i + 6))) {
  6528. unsigned int b = q + 1;
  6529. unsigned int e = css_skip_nested(s, len, b);
  6530. if (e > b)
  6531. parse_css(d, s + b,
  6532. (e - 1) - b);
  6533. i = e;
  6534. } else {
  6535. i = skip_at_rule(s, len, i);
  6536. }
  6537. continue;
  6538. }
  6539. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6540. (i + 6 == len || is_space(s[i + 6]) ||
  6541. s[i + 6] == '{' || s[i + 6] == ';')) {
  6542. unsigned int q = i + 6, nst = q;
  6543. while (q < len && s[q] != '{' && s[q] != ';')
  6544. q++;
  6545. if (q >= len) {
  6546. i = len;
  6547. continue;
  6548. }
  6549. if (s[q] == ';') {
  6550. /* `@layer a, b;` only fixes the order. */
  6551. unsigned int st = nst;
  6552. char path[128];
  6553. while (st < q) {
  6554. unsigned int en = st;
  6555. while (en < q && s[en] != ',')
  6556. en++;
  6557. if (en > st)
  6558. css_layer_register(d, s + st,
  6559. en - st,
  6560. path,
  6561. sizeof(path));
  6562. st = en + 1;
  6563. }
  6564. i = q + 1;
  6565. continue;
  6566. }
  6567. {
  6568. unsigned int b = q + 1;
  6569. unsigned int e = css_skip_nested(s, len, b);
  6570. unsigned char saved = css_cur_layer;
  6571. char saved_name[128], path[128];
  6572. unsigned char rank;
  6573. memcpy(saved_name, css_layer_name,
  6574. sizeof(saved_name));
  6575. rank = css_layer_register(d, s + nst, q - nst,
  6576. path,
  6577. sizeof(path));
  6578. if (rank) {
  6579. css_cur_layer = rank;
  6580. memcpy(css_layer_name, path,
  6581. sizeof(css_layer_name));
  6582. }
  6583. if (e > b)
  6584. parse_css(d, s + b, (e - 1) - b);
  6585. css_cur_layer = saved;
  6586. memcpy(css_layer_name, saved_name,
  6587. sizeof(css_layer_name));
  6588. i = e;
  6589. }
  6590. continue;
  6591. }
  6592. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  6593. unsigned int q = i + 9;
  6594. while (q < len && s[q] != '{' && s[q] != ';')
  6595. q++;
  6596. if (q < len && s[q] == '{' &&
  6597. supports_matches(s + i + 9, q - (i + 9))) {
  6598. unsigned int b = q + 1;
  6599. unsigned int e = css_skip_nested(s, len, b);
  6600. if (e > b)
  6601. parse_css(d, s + b, (e - 1) - b);
  6602. i = e;
  6603. } else {
  6604. i = skip_at_rule(s, len, i);
  6605. }
  6606. continue;
  6607. }
  6608. i = skip_at_rule(s, len, i);
  6609. continue;
  6610. }
  6611. sstart = i;
  6612. while (i < len && s[i] != '{')
  6613. i++;
  6614. if (i >= len)
  6615. break;
  6616. send = i;
  6617. i++;
  6618. bstart = i;
  6619. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  6620. * to the matching close brace; stopping at the first '}' left
  6621. * the parser out of sync and dropped the rest of the sheet
  6622. * (nd-aktuell lost its desktop rules that way). */
  6623. bend = css_block_end(s, len, i);
  6624. i = bend < len ? bend + 1 : len;
  6625. while (sstart < send && is_space(s[sstart]))
  6626. sstart++;
  6627. while (send > sstart && is_space(s[send - 1]))
  6628. send--;
  6629. if (sstart == send)
  6630. continue;
  6631. if (!css_dark_selector(s + sstart, send - sstart))
  6632. css_collect_block(d, s + bstart, bend - bstart,
  6633. css_root_selector(s + sstart,
  6634. send - sstart));
  6635. if (css_collect_only)
  6636. continue;
  6637. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  6638. {
  6639. unsigned char sc = bend - bstart <= 4000 ?
  6640. css_block_scope(d, s + bstart, bend - bstart) :
  6641. 0;
  6642. scope_flags = 0;
  6643. scope_off = 0;
  6644. if (sc) {
  6645. scope_off = store_raw(d, s + bstart,
  6646. bend - bstart,
  6647. bend - bstart);
  6648. if (scope_off)
  6649. scope_flags = sc;
  6650. }
  6651. }
  6652. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  6653. continue;
  6654. /* Split a comma-separated selector list into separate rules.
  6655. * A selector the engine cannot match is dropped; the others
  6656. * in the list still apply. */
  6657. {
  6658. const char *list = s + sstart;
  6659. unsigned int ll = send - sstart, pos = 0, st, en;
  6660. while (d->nrules < d->rule_cap &&
  6661. sel_list_next(list, ll, &pos, &st, &en)) {
  6662. struct sel_part parts[SEL_PARTS];
  6663. struct br_rule *r;
  6664. char sel[160];
  6665. unsigned int k, off;
  6666. int np, pe;
  6667. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  6668. k++)
  6669. sel[k] = is_space(list[st + k]) ? ' ' :
  6670. list[st + k];
  6671. if (st + k < en)
  6672. continue; /* too long */
  6673. sel[k] = '\0';
  6674. pe = css_pseudo_element(sel, &k);
  6675. if (pe < 0 || !css_selector_supported(sel, k))
  6676. continue;
  6677. np = sel_split(sel, k, parts);
  6678. off = store_attr(d, sel, k, 159);
  6679. if (np <= 0 || !off)
  6680. continue;
  6681. r = &d->rules[d->nrules++];
  6682. r->sel_off = off;
  6683. r->key_off = store_attr(d, sel + parts[np - 1].st,
  6684. parts[np - 1].en -
  6685. parts[np - 1].st, 159);
  6686. r->spec = (unsigned short)selector_specificity(sel, k);
  6687. r->single = np == 1;
  6688. r->pe = (unsigned char)pe;
  6689. r->layer = css_cur_layer;
  6690. r->var_scope = scope_flags;
  6691. r->raw_off = scope_off;
  6692. r->raw_len = (unsigned short)(bend - bstart);
  6693. if (pe)
  6694. d->npe_rules++;
  6695. r->nd = (unsigned char)nd;
  6696. for (k = 0; k < nd; k++)
  6697. r->d[k] = decls[k];
  6698. rule_index_add(d, d->nrules - 1,
  6699. attr_ptr(d, r->key_off),
  6700. s_len(attr_ptr(d, r->key_off)));
  6701. }
  6702. }
  6703. }
  6704. }
  6705. /* Sites often set html { font-size: 62.5% } and then size everything in
  6706. * rem. Detect that root size before parsing the sheet so a rem resolves to
  6707. * the same pixels a real browser would use. */
  6708. static void detect_root_font(const char *s, unsigned int len)
  6709. {
  6710. unsigned int i;
  6711. for (i = 0; i + 4 < len; i++) {
  6712. unsigned int j, end, body, sel, q, h;
  6713. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  6714. int exact;
  6715. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  6716. s[i + 3] == 'l'))
  6717. continue;
  6718. h = i + 4;
  6719. while (h < len && is_space(s[h]))
  6720. h++;
  6721. if (h < len && s[h] != '{' && s[h] != ',' &&
  6722. s[h] != '.' && s[h] != ':' && s[h] != '[')
  6723. continue;
  6724. sel = i;
  6725. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  6726. s[sel - 1] != ';' && s[sel - 1] != ',')
  6727. sel--;
  6728. q = sel;
  6729. while (q < i && is_space(s[q]))
  6730. q++;
  6731. exact = (q == i);
  6732. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  6733. continue;
  6734. j = h;
  6735. if (j >= len || s[j] != '{')
  6736. continue;
  6737. while (j < len && s[j] != '{')
  6738. j++;
  6739. if (j >= len)
  6740. break;
  6741. end = j + 1;
  6742. body = end;
  6743. while (end < len && s[end] != '}')
  6744. end++;
  6745. for (; j + 9 < end; j++) {
  6746. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  6747. s[j + 3] == 't' && s[j + 4] == '-' &&
  6748. s[j + 5] == 's' && s[j + 6] == 'i' &&
  6749. s[j + 7] == 'z' && s[j + 8] == 'e'))
  6750. continue;
  6751. j += 9;
  6752. while (j < end && s[j] != ':')
  6753. j++;
  6754. if (j >= end)
  6755. break;
  6756. j++;
  6757. while (j < end && is_space(s[j]))
  6758. j++;
  6759. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6760. v = v * 10 + (s[j] - '0');
  6761. digits = 1;
  6762. j++;
  6763. }
  6764. if (j < end && s[j] == '.') {
  6765. j++;
  6766. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6767. v = v * 10 + (s[j] - '0');
  6768. frac++;
  6769. j++;
  6770. }
  6771. }
  6772. while (frac < 2) {
  6773. v *= 10;
  6774. frac++;
  6775. }
  6776. while (frac > 2) {
  6777. v /= 10;
  6778. frac--;
  6779. }
  6780. if (!digits)
  6781. break;
  6782. if (j < end && s[j] == '%')
  6783. pct = 1;
  6784. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  6785. px = 1;
  6786. if (pct)
  6787. v = (v * 16 + 5000) / 10000;
  6788. else if (px)
  6789. v /= 100;
  6790. else
  6791. v = v * 16 / 100;
  6792. if (exact && v >= 8 && v <= 24)
  6793. root_font_px = v;
  6794. if (exact)
  6795. return;
  6796. }
  6797. /* html { font: 10px/1 Verdana } also sets the root size. */
  6798. for (j = body; j + 5 <= end; j++) {
  6799. unsigned int p, ts, te;
  6800. if (!ci_eq_n(s + j, "font:", 5))
  6801. continue;
  6802. p = j + 5;
  6803. while (next_token(s, end, &p, &ts, &te)) {
  6804. int fv = parse_font_px(s + ts, te - ts);
  6805. if (fv >= 8 && fv <= 24) {
  6806. if (exact)
  6807. root_font_px = fv;
  6808. break;
  6809. }
  6810. }
  6811. if (exact)
  6812. return;
  6813. break;
  6814. }
  6815. }
  6816. }
  6817. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  6818. {
  6819. scan_font_faces(css, len);
  6820. detect_root_font(css, len);
  6821. css_collect_only = 1;
  6822. parse_css(d, css, len);
  6823. css_collect_only = 0;
  6824. parse_css(d, css, len);
  6825. }
  6826. void br_parse_inline_css(struct br_doc *d)
  6827. {
  6828. if (d->inline_css_len) {
  6829. scan_font_faces(css_store, d->inline_css_len);
  6830. detect_root_font(css_store, d->inline_css_len);
  6831. css_collect_only = 1;
  6832. parse_css(d, css_store, d->inline_css_len);
  6833. css_collect_only = 0;
  6834. parse_css(d, css_store, d->inline_css_len);
  6835. }
  6836. d->inline_css_len = 0;
  6837. }
  6838. void br_reset_styles(struct br_doc *d)
  6839. {
  6840. unsigned int i;
  6841. for (i = 0; i < d->nnode; i++) {
  6842. d->nodes[i].style_valid = 0;
  6843. d->nodes[i].pe_state = 0;
  6844. }
  6845. }
  6846. /* ---- selector matching ---- */
  6847. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  6848. unsigned int clen)
  6849. {
  6850. const char *s = attr_ptr(d, d->nodes[node].class_off);
  6851. unsigned int i = 0;
  6852. while (s[i]) {
  6853. unsigned int st, n;
  6854. while (s[i] && is_space(s[i]))
  6855. i++;
  6856. st = i;
  6857. while (s[i] && !is_space(s[i]))
  6858. i++;
  6859. n = i - st;
  6860. if (n == clen && ci_eq_n(s + st, cls, n))
  6861. return 1;
  6862. }
  6863. return 0;
  6864. }
  6865. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  6866. unsigned int len)
  6867. {
  6868. const char *s = attr_ptr(d, d->nodes[node].id_off);
  6869. unsigned int i;
  6870. for (i = 0; i < len; i++)
  6871. if (s[i] != id[i])
  6872. return 0;
  6873. return s[len] == '\0';
  6874. }
  6875. static int is_ident(char c)
  6876. {
  6877. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  6878. (c >= '0' && c <= '9') || c == '-' || c == '_';
  6879. }
  6880. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  6881. {
  6882. unsigned int n = 0;
  6883. while (lit[n])
  6884. n++;
  6885. return n == nlen && ci_eq_n(name, lit, nlen);
  6886. }
  6887. static int attr_present(struct br_doc *d, unsigned int node,
  6888. const char *name, unsigned int nlen)
  6889. {
  6890. struct br_node *n = &d->nodes[node];
  6891. if (attr_is(name, nlen, "data-collapse-target"))
  6892. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  6893. if (attr_is(name, nlen, "data-collapse-trigger"))
  6894. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  6895. if (attr_is(name, nlen, "is-open"))
  6896. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  6897. if (attr_is(name, nlen, "disabled"))
  6898. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  6899. if (attr_is(name, nlen, "open"))
  6900. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  6901. if (attr_is(name, nlen, "inert"))
  6902. return (n->attr_flags & BR_ATTR_INERT) != 0;
  6903. if (attr_is(name, nlen, "hidden"))
  6904. return (n->flags & BR_NODE_HIDDEN) != 0;
  6905. if (attr_is(name, nlen, "href"))
  6906. return attr_ptr(d, n->href_off)[0] != 0;
  6907. if (attr_is(name, nlen, "class"))
  6908. return attr_ptr(d, n->class_off)[0] != 0;
  6909. if (attr_is(name, nlen, "id"))
  6910. return attr_ptr(d, n->id_off)[0] != 0;
  6911. if (attr_is(name, nlen, "src"))
  6912. return attr_ptr(d, n->src_off)[0] != 0;
  6913. if (attr_is(name, nlen, "alt"))
  6914. return attr_ptr(d, n->alt_off)[0] != 0;
  6915. if (attr_is(name, nlen, "rel"))
  6916. return attr_ptr(d, n->rel_off)[0] != 0;
  6917. return 0;
  6918. }
  6919. /* ---- Selector engine ----
  6920. *
  6921. * One matcher serves the cascade (rule_try), querySelector(All) and
  6922. * Element.matches. A compound is an optional type/universal selector
  6923. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  6924. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  6925. * '~' combinators and is matched right to left with backtracking.
  6926. *
  6927. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  6928. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  6929. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  6930. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  6931. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  6932. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  6933. * and pseudo-elements never match an element; the cascade strips
  6934. * ::before/::after before it gets here. */
  6935. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  6936. unsigned int len);
  6937. /* i is at '(' or '['; returns the index just past the matching close. */
  6938. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6939. unsigned int len)
  6940. {
  6941. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  6942. int depth = 0;
  6943. for (; i < len; i++) {
  6944. char c = s[i];
  6945. if (quote) {
  6946. if (c == '\\')
  6947. i++;
  6948. else if (c == quote)
  6949. quote = 0;
  6950. continue;
  6951. }
  6952. if (c == '\\')
  6953. i++;
  6954. else if (c == '"' || c == '\'')
  6955. quote = c;
  6956. else if (c == open)
  6957. depth++;
  6958. else if (c == close && --depth == 0)
  6959. return i + 1;
  6960. }
  6961. return len;
  6962. }
  6963. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  6964. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6965. char *out, unsigned int cap, unsigned int *olen)
  6966. {
  6967. unsigned int n = 0;
  6968. while (i < len) {
  6969. char c = s[i];
  6970. if (c == '\\' && i + 1 < len) {
  6971. c = s[i + 1];
  6972. i += 2;
  6973. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  6974. i++;
  6975. } else {
  6976. break;
  6977. }
  6978. if (n + 1 < cap)
  6979. out[n++] = c;
  6980. }
  6981. out[n] = '\0';
  6982. *olen = n;
  6983. return i;
  6984. }
  6985. /* Next top-level item of a comma-separated selector list. */
  6986. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6987. unsigned int *st, unsigned int *en)
  6988. {
  6989. unsigned int i = *pos;
  6990. while (i < len && (is_space(s[i]) || s[i] == ','))
  6991. i++;
  6992. if (i >= len)
  6993. return 0;
  6994. *st = i;
  6995. while (i < len && s[i] != ',') {
  6996. if (s[i] == '(' || s[i] == '[')
  6997. i = sel_skip_group(s, i, len);
  6998. else if (s[i] == '\\' && i + 1 < len)
  6999. i += 2;
  7000. else if (s[i] == '"' || s[i] == '\'') {
  7001. char q = s[i++];
  7002. while (i < len && s[i] != q)
  7003. i++;
  7004. i++;
  7005. } else
  7006. i++;
  7007. }
  7008. if (i > len)
  7009. i = len;
  7010. *en = i;
  7011. while (*en > *st && is_space(s[*en - 1]))
  7012. (*en)--;
  7013. *pos = i;
  7014. return 1;
  7015. }
  7016. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7017. unsigned int len)
  7018. {
  7019. unsigned int pos = 0, st, en;
  7020. while (sel_list_next(s, len, &pos, &st, &en))
  7021. if (sel_match(d, node, s + st, en - st))
  7022. return 1;
  7023. return 0;
  7024. }
  7025. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7026. {
  7027. if (d->nodes[a].tag != d->nodes[b].tag)
  7028. return 0;
  7029. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7030. return 1;
  7031. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7032. attr_ptr(d, d->nodes[b].name_off));
  7033. }
  7034. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7035. {
  7036. unsigned int c, prev = 0;
  7037. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7038. c = d->nodes[c].next)
  7039. if (d->nodes[c].type == BR_NODE_ELEM)
  7040. prev = c;
  7041. return c == node ? prev : 0;
  7042. }
  7043. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7044. {
  7045. unsigned int c;
  7046. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7047. if (d->nodes[c].type == BR_NODE_ELEM)
  7048. return c;
  7049. return 0;
  7050. }
  7051. /* 1-based position among the element siblings (of the same type), counted
  7052. * from the start or, with from_end, from the end. */
  7053. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7054. int from_end)
  7055. {
  7056. unsigned int c;
  7057. int pos = 0, total = 0;
  7058. for (c = d->nodes[d->nodes[node].parent].first; c;
  7059. c = d->nodes[c].next) {
  7060. if (d->nodes[c].type != BR_NODE_ELEM)
  7061. continue;
  7062. if (of_type && !sel_same_type(d, c, node))
  7063. continue;
  7064. total++;
  7065. if (c == node)
  7066. pos = total;
  7067. }
  7068. if (!pos)
  7069. return 0;
  7070. return from_end ? total - pos + 1 : pos;
  7071. }
  7072. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7073. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7074. {
  7075. unsigned int i = 0;
  7076. int sign = 1, num = 0, digits = 0;
  7077. while (len && is_space(s[len - 1]))
  7078. len--;
  7079. while (i < len && is_space(s[i]))
  7080. i++;
  7081. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7082. *a = 2;
  7083. *b = 1;
  7084. return 1;
  7085. }
  7086. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7087. *a = 2;
  7088. *b = 0;
  7089. return 1;
  7090. }
  7091. if (i < len && (s[i] == '+' || s[i] == '-'))
  7092. sign = s[i++] == '-' ? -1 : 1;
  7093. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7094. num = num * 10 + (s[i++] - '0');
  7095. digits = 1;
  7096. }
  7097. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7098. *a = sign * (digits ? num : 1);
  7099. *b = 0;
  7100. i++;
  7101. while (i < len && is_space(s[i]))
  7102. i++;
  7103. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7104. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7105. while (i < len && is_space(s[i]))
  7106. i++;
  7107. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7108. bv = bv * 10 + (s[i++] - '0');
  7109. bd = 1;
  7110. }
  7111. if (!bd)
  7112. return 0;
  7113. *b = bs * bv;
  7114. }
  7115. } else {
  7116. if (!digits)
  7117. return 0;
  7118. *a = 0;
  7119. *b = sign * num;
  7120. }
  7121. return i == len;
  7122. }
  7123. static int sel_nth_ok(int a, int b, int pos)
  7124. {
  7125. if (pos <= 0)
  7126. return 0;
  7127. if (!a)
  7128. return pos == b;
  7129. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7130. }
  7131. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7132. const char *name, int op, const char *want,
  7133. unsigned int wl, int icase)
  7134. {
  7135. const char *have = br_find_attr(d, node, name);
  7136. unsigned int n, i;
  7137. if (!have) {
  7138. /* Boolean attributes the parser keeps as flags. */
  7139. if (!attr_present(d, node, name, text_len(name)))
  7140. return 0;
  7141. have = "";
  7142. }
  7143. if (!op)
  7144. return 1;
  7145. n = text_len(have);
  7146. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7147. switch (op) {
  7148. case '=':
  7149. return n == wl && SEL_EQ(have, want, wl);
  7150. case '^':
  7151. return wl && n >= wl && SEL_EQ(have, want, wl);
  7152. case '$':
  7153. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7154. case '*':
  7155. if (!wl)
  7156. return 0;
  7157. for (i = 0; i + wl <= n; i++)
  7158. if (SEL_EQ(have + i, want, wl))
  7159. return 1;
  7160. return 0;
  7161. case '|':
  7162. return n >= wl && SEL_EQ(have, want, wl) &&
  7163. (n == wl || have[wl] == '-');
  7164. case '~':
  7165. if (!wl)
  7166. return 0;
  7167. for (i = 0; i < n; ) {
  7168. unsigned int st;
  7169. while (i < n && is_space(have[i]))
  7170. i++;
  7171. st = i;
  7172. while (i < n && !is_space(have[i]))
  7173. i++;
  7174. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7175. return 1;
  7176. }
  7177. return 0;
  7178. }
  7179. #undef SEL_EQ
  7180. return 0;
  7181. }
  7182. static int sel_empty(struct br_doc *d, unsigned int node)
  7183. {
  7184. unsigned int c;
  7185. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7186. if (d->nodes[c].type == BR_NODE_ELEM)
  7187. return 0;
  7188. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7189. return 0;
  7190. }
  7191. return 1;
  7192. }
  7193. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7194. unsigned int len)
  7195. {
  7196. unsigned int c;
  7197. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7198. if (d->nodes[c].type != BR_NODE_ELEM)
  7199. continue;
  7200. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7201. return 1;
  7202. }
  7203. return 0;
  7204. }
  7205. /* :has() with an optional leading relative combinator per list item. */
  7206. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7207. unsigned int len)
  7208. {
  7209. unsigned int pos = 0, st, en, c;
  7210. while (sel_list_next(s, len, &pos, &st, &en)) {
  7211. char comb = s[st];
  7212. if (comb == '>' || comb == '+' || comb == '~') {
  7213. st++;
  7214. while (st < en && is_space(s[st]))
  7215. st++;
  7216. } else {
  7217. comb = ' ';
  7218. }
  7219. if (st >= en)
  7220. continue;
  7221. if (comb == '>') {
  7222. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7223. if (d->nodes[c].type == BR_NODE_ELEM &&
  7224. sel_match(d, c, s + st, en - st))
  7225. return 1;
  7226. } else if (comb == '+') {
  7227. c = sel_next_elem(d, node);
  7228. if (c && sel_match(d, c, s + st, en - st))
  7229. return 1;
  7230. } else if (comb == '~') {
  7231. for (c = sel_next_elem(d, node); c;
  7232. c = sel_next_elem(d, c))
  7233. if (sel_match(d, c, s + st, en - st))
  7234. return 1;
  7235. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7236. return 1;
  7237. }
  7238. }
  7239. return 0;
  7240. }
  7241. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7242. {
  7243. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7244. }
  7245. /* Pseudo-class names the engine models; anything else never matches and
  7246. * makes a top-level rule unsupported (see css_selector_supported()). */
  7247. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7248. {
  7249. static const char *const plain[] = {
  7250. "root", "link", "any-link", "visited", "first-child",
  7251. "last-child", "only-child", "first-of-type", "last-of-type",
  7252. "only-of-type", "empty", "checked", "disabled", "enabled",
  7253. "required", "optional",
  7254. };
  7255. static const char *const func[] = {
  7256. "not", "is", "where", "matches", "-webkit-any", "has",
  7257. "nth-child", "nth-last-child", "nth-of-type",
  7258. "nth-last-of-type",
  7259. };
  7260. unsigned int k;
  7261. if (fn) {
  7262. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7263. if (sel_name_is(name, nl, func[k]))
  7264. return 1;
  7265. return 0;
  7266. }
  7267. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7268. if (sel_name_is(name, nl, plain[k]))
  7269. return 1;
  7270. return 0;
  7271. }
  7272. static int sel_form_control(struct br_doc *d, unsigned int node)
  7273. {
  7274. unsigned int t = d->nodes[node].tag;
  7275. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7276. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7277. t == BR_TAG_OPTION;
  7278. }
  7279. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7280. unsigned int nl, const char *arg, unsigned int al,
  7281. int fn)
  7282. {
  7283. struct br_node *n = &d->nodes[node];
  7284. int a, b;
  7285. if (fn) {
  7286. if (sel_name_is(name, nl, "not"))
  7287. return !sel_list_match(d, node, arg, al);
  7288. if (sel_name_is(name, nl, "is") ||
  7289. sel_name_is(name, nl, "where") ||
  7290. sel_name_is(name, nl, "matches") ||
  7291. sel_name_is(name, nl, "-webkit-any"))
  7292. return sel_list_match(d, node, arg, al);
  7293. if (sel_name_is(name, nl, "has"))
  7294. return sel_has(d, node, arg, al);
  7295. if (!sel_nth_parse(arg, al, &a, &b))
  7296. return 0;
  7297. if (sel_name_is(name, nl, "nth-child"))
  7298. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7299. if (sel_name_is(name, nl, "nth-last-child"))
  7300. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7301. if (sel_name_is(name, nl, "nth-of-type"))
  7302. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7303. if (sel_name_is(name, nl, "nth-last-of-type"))
  7304. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7305. return 0;
  7306. }
  7307. if (sel_name_is(name, nl, "root"))
  7308. return n->tag == BR_TAG_HTML;
  7309. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7310. /* No history state: every link is unvisited. */
  7311. return n->tag == BR_TAG_A && n->href_off;
  7312. if (sel_name_is(name, nl, "first-child"))
  7313. return sel_sibling_pos(d, node, 0, 0) == 1;
  7314. if (sel_name_is(name, nl, "last-child"))
  7315. return sel_sibling_pos(d, node, 0, 1) == 1;
  7316. if (sel_name_is(name, nl, "only-child"))
  7317. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7318. sel_sibling_pos(d, node, 0, 1) == 1;
  7319. if (sel_name_is(name, nl, "first-of-type"))
  7320. return sel_sibling_pos(d, node, 1, 0) == 1;
  7321. if (sel_name_is(name, nl, "last-of-type"))
  7322. return sel_sibling_pos(d, node, 1, 1) == 1;
  7323. if (sel_name_is(name, nl, "only-of-type"))
  7324. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7325. sel_sibling_pos(d, node, 1, 1) == 1;
  7326. if (sel_name_is(name, nl, "empty"))
  7327. return sel_empty(d, node);
  7328. if (sel_name_is(name, nl, "checked"))
  7329. return br_find_attr(d, node, "checked") != 0 ||
  7330. (n->tag == BR_TAG_OPTION &&
  7331. br_find_attr(d, node, "selected") != 0);
  7332. if (sel_name_is(name, nl, "disabled"))
  7333. return sel_form_control(d, node) &&
  7334. (n->attr_flags & BR_ATTR_DISABLED);
  7335. if (sel_name_is(name, nl, "enabled"))
  7336. return sel_form_control(d, node) &&
  7337. !(n->attr_flags & BR_ATTR_DISABLED);
  7338. if (sel_name_is(name, nl, "required"))
  7339. return sel_form_control(d, node) &&
  7340. br_find_attr(d, node, "required") != 0;
  7341. if (sel_name_is(name, nl, "optional"))
  7342. return sel_form_control(d, node) &&
  7343. !br_find_attr(d, node, "required");
  7344. return 0;
  7345. }
  7346. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7347. unsigned int len)
  7348. {
  7349. char buf[64];
  7350. unsigned int i = 0, bl;
  7351. if (d->nodes[node].type != BR_NODE_ELEM)
  7352. return 0;
  7353. if (i < len && s[i] == '*') {
  7354. i++;
  7355. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7356. unsigned int tag;
  7357. i = sel_ident(s, i, len, buf, 32, &bl);
  7358. tag = br_tag_from_name(buf);
  7359. if (tag == BR_TAG_UNKNOWN) {
  7360. const char *raw = attr_ptr(d,
  7361. d->nodes[node].name_off);
  7362. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7363. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7364. return 0;
  7365. } else if (tag != d->nodes[node].tag) {
  7366. return 0;
  7367. }
  7368. }
  7369. if (i >= len)
  7370. return i > 0;
  7371. while (i < len) {
  7372. char c = s[i];
  7373. if (c == '.' || c == '#') {
  7374. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7375. if (!bl)
  7376. return 0;
  7377. if (c == '.' ? !class_has(d, node, buf, bl) :
  7378. !id_eq(d, node, buf, bl))
  7379. return 0;
  7380. } else if (c == '[') {
  7381. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7382. unsigned int vs = 0, vl = 0, k;
  7383. char name[32];
  7384. int op = 0, icase = 0;
  7385. while (j < e && is_space(s[j]))
  7386. j++;
  7387. k = 0;
  7388. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7389. k + 1 < sizeof(name)) {
  7390. char ch = s[j++];
  7391. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7392. (char)(ch + 32) : ch;
  7393. }
  7394. name[k] = '\0';
  7395. if (!k)
  7396. return 0;
  7397. while (j < e && is_space(s[j]))
  7398. j++;
  7399. if (j < e && s[j] == '=') {
  7400. op = '=';
  7401. j++;
  7402. } else if (j + 1 < e && s[j + 1] == '=' &&
  7403. (s[j] == '~' || s[j] == '|' ||
  7404. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7405. op = s[j];
  7406. j += 2;
  7407. }
  7408. if (op) {
  7409. while (j < e && is_space(s[j]))
  7410. j++;
  7411. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7412. char q = s[j++];
  7413. vs = j;
  7414. while (j < e && s[j] != q)
  7415. j++;
  7416. vl = j - vs;
  7417. j++;
  7418. } else {
  7419. vs = j;
  7420. while (j < e && s[j] != ']' &&
  7421. !is_space(s[j]))
  7422. j++;
  7423. vl = j - vs;
  7424. }
  7425. while (j < e && is_space(s[j]))
  7426. j++;
  7427. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7428. icase = 1;
  7429. }
  7430. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7431. icase))
  7432. return 0;
  7433. i = e;
  7434. } else if (c == ':') {
  7435. unsigned int st, nl, as = 0, al = 0;
  7436. int fn = 0;
  7437. if (i + 1 < len && s[i + 1] == ':')
  7438. return 0; /* pseudo-element */
  7439. st = ++i;
  7440. while (i < len && is_ident(s[i]))
  7441. i++;
  7442. nl = i - st;
  7443. if (i < len && s[i] == '(') {
  7444. unsigned int e = sel_skip_group(s, i, len);
  7445. fn = 1;
  7446. as = i + 1;
  7447. al = e > as ? e - as - 1 : 0;
  7448. i = e;
  7449. }
  7450. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7451. return 0;
  7452. } else {
  7453. return 0;
  7454. }
  7455. }
  7456. return 1;
  7457. }
  7458. /* Split a complex selector into compounds; returns the count or -1. */
  7459. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7460. {
  7461. unsigned int i = 0;
  7462. int n = 0;
  7463. char comb = 0;
  7464. while (i < len && is_space(s[i]))
  7465. i++;
  7466. while (i < len) {
  7467. unsigned int st = i;
  7468. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7469. s[i] != '+' && s[i] != '~') {
  7470. if (s[i] == '(' || s[i] == '[')
  7471. i = sel_skip_group(s, i, len);
  7472. else if (s[i] == '\\' && i + 1 < len)
  7473. i += 2;
  7474. else
  7475. i++;
  7476. }
  7477. if (i == st || n == SEL_PARTS)
  7478. return -1;
  7479. p[n].st = (unsigned short)st;
  7480. p[n].en = (unsigned short)(i > len ? len : i);
  7481. p[n].comb = comb;
  7482. n++;
  7483. comb = 0;
  7484. while (i < len && is_space(s[i])) {
  7485. comb = ' ';
  7486. i++;
  7487. }
  7488. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7489. comb = s[i++];
  7490. while (i < len && is_space(s[i]))
  7491. i++;
  7492. if (i >= len)
  7493. return -1;
  7494. }
  7495. }
  7496. return n;
  7497. }
  7498. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7499. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7500. * parent when the page starts with <html> (node 0 is that element; see
  7501. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7502. static int sel_parent(struct br_doc *d, unsigned int node)
  7503. {
  7504. unsigned int p = d->nodes[node].parent;
  7505. if (p)
  7506. return (int)p;
  7507. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7508. d->nodes[0].tag == BR_TAG_HTML &&
  7509. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7510. return 0;
  7511. return -1;
  7512. }
  7513. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7514. const struct sel_part *p, int k)
  7515. {
  7516. unsigned int c, pl;
  7517. const char *ps;
  7518. if (k == 0)
  7519. return 1;
  7520. ps = s + p[k - 1].st;
  7521. pl = p[k - 1].en - p[k - 1].st;
  7522. switch (p[k].comb) {
  7523. case '>': {
  7524. int pc = sel_parent(d, node);
  7525. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7526. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7527. }
  7528. case '+':
  7529. c = sel_prev_elem(d, node);
  7530. return c && compound_match(d, c, ps, pl) &&
  7531. sel_from(d, c, s, p, k - 1);
  7532. case '~':
  7533. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7534. if (compound_match(d, c, ps, pl) &&
  7535. sel_from(d, c, s, p, k - 1))
  7536. return 1;
  7537. return 0;
  7538. default: {
  7539. int pc;
  7540. for (pc = sel_parent(d, node); pc >= 0;
  7541. pc = sel_parent(d, (unsigned int)pc))
  7542. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7543. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7544. return 1;
  7545. return 0;
  7546. }
  7547. }
  7548. }
  7549. /* Match one complex selector. right_done: the caller already matched the
  7550. * rightmost compound (the rule index key). */
  7551. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7552. unsigned int len, int right_done)
  7553. {
  7554. struct sel_part parts[SEL_PARTS];
  7555. int np = sel_split(s, len, parts);
  7556. if (np <= 0)
  7557. return 0;
  7558. if (!right_done &&
  7559. !compound_match(d, node, s + parts[np - 1].st,
  7560. parts[np - 1].en - parts[np - 1].st))
  7561. return 0;
  7562. return sel_from(d, node, s, parts, np - 1);
  7563. }
  7564. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7565. unsigned int len)
  7566. {
  7567. return selector_match(d, node, s, len, 0);
  7568. }
  7569. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7570. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7571. * and :where counts nothing. */
  7572. static unsigned int selector_specificity(const char *s, unsigned int len)
  7573. {
  7574. unsigned int i = 0, spec = 0, bl;
  7575. char buf[64];
  7576. while (i < len) {
  7577. char c = s[i];
  7578. if (c == '#' || c == '.') {
  7579. spec += c == '#' ? 100 : 10;
  7580. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7581. } else if (c == '[') {
  7582. spec += 10;
  7583. i = sel_skip_group(s, i, len);
  7584. } else if (c == ':') {
  7585. unsigned int st, nl;
  7586. if (i + 1 < len && s[i + 1] == ':') {
  7587. spec += 1;
  7588. i += 2;
  7589. while (i < len && is_ident(s[i]))
  7590. i++;
  7591. continue;
  7592. }
  7593. st = ++i;
  7594. while (i < len && is_ident(s[i]))
  7595. i++;
  7596. nl = i - st;
  7597. if (i < len && s[i] == '(') {
  7598. unsigned int e = sel_skip_group(s, i, len);
  7599. if (sel_name_is(s + st, nl, "where")) {
  7600. /* zero */
  7601. } else if (sel_name_is(s + st, nl, "not") ||
  7602. sel_name_is(s + st, nl, "is") ||
  7603. sel_name_is(s + st, nl, "has") ||
  7604. sel_name_is(s + st, nl, "matches") ||
  7605. sel_name_is(s + st, nl,
  7606. "-webkit-any")) {
  7607. unsigned int pos = 0, as, ae, best = 0;
  7608. unsigned int al = e > i + 1 ?
  7609. e - i - 2 : 0;
  7610. while (sel_list_next(s + i + 1, al,
  7611. &pos, &as, &ae)) {
  7612. unsigned int v =
  7613. selector_specificity(
  7614. s + i + 1 + as,
  7615. ae - as);
  7616. if (v > best)
  7617. best = v;
  7618. }
  7619. spec += best;
  7620. } else {
  7621. spec += 10;
  7622. }
  7623. i = e;
  7624. } else if (sel_name_is(s + st, nl, "before") ||
  7625. sel_name_is(s + st, nl, "after")) {
  7626. spec += 1;
  7627. } else {
  7628. spec += 10;
  7629. }
  7630. } else if (is_ident(c) || c == '\\') {
  7631. spec += 1;
  7632. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  7633. } else {
  7634. i++;
  7635. }
  7636. }
  7637. return spec;
  7638. }
  7639. /* Cascade filter: drop a selector the engine cannot match (user-state
  7640. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  7641. * ones the caller handles), so it does not waste a rule slot. Arguments of
  7642. * functional pseudo-classes are not checked: an unknown pseudo-class there
  7643. * is simply false, which is right for :not(:hover). */
  7644. static int css_selector_supported(const char *s, unsigned int len)
  7645. {
  7646. struct sel_part parts[SEL_PARTS];
  7647. unsigned int i = 0;
  7648. if (sel_split(s, len, parts) <= 0)
  7649. return 0;
  7650. while (i < len) {
  7651. if (s[i] == '(' || s[i] == '[') {
  7652. i = sel_skip_group(s, i, len);
  7653. } else if (s[i] == '\\') {
  7654. i += 2;
  7655. } else if (s[i] == ':') {
  7656. unsigned int st, nl;
  7657. int fn;
  7658. if (i + 1 < len && s[i + 1] == ':')
  7659. return 0;
  7660. st = ++i;
  7661. while (i < len && is_ident(s[i]))
  7662. i++;
  7663. nl = i - st;
  7664. fn = i < len && s[i] == '(';
  7665. if (!sel_pseudo_known(s + st, nl, fn))
  7666. return 0;
  7667. } else {
  7668. i++;
  7669. }
  7670. }
  7671. return 1;
  7672. }
  7673. #define RULE_MATCH_MAX 512
  7674. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  7675. static int cascade_pe;
  7676. struct rule_match {
  7677. unsigned short idx;
  7678. short spec; /* precedence of normal declarations */
  7679. short spec_imp; /* precedence of !important declarations */
  7680. };
  7681. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  7682. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  7683. * unlayered rules, then the style attribute. !important reverses the
  7684. * layer order: unlayered lowest, the first layer highest, the style
  7685. * attribute above all. Both fit a short. */
  7686. #define CASCADE_BAND 512
  7687. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  7688. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  7689. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  7690. {
  7691. unsigned int band;
  7692. if (spec >= CASCADE_BAND)
  7693. spec = CASCADE_BAND - 1;
  7694. if (important)
  7695. band = layer ? CASCADE_UNLAYERED - layer : 0;
  7696. else
  7697. band = layer ? layer : CASCADE_UNLAYERED;
  7698. return (int)(band * CASCADE_BAND + spec);
  7699. }
  7700. static struct rule_match rule_matches[RULE_MATCH_MAX];
  7701. static unsigned int nrule_matches;
  7702. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  7703. {
  7704. struct br_rule *r = &d->rules[idx];
  7705. const char *key = attr_ptr(d, r->key_off);
  7706. int spec, key_ok = 0;
  7707. if (r->pe != cascade_pe)
  7708. return;
  7709. if (key[0]) {
  7710. if (!compound_match(d, node, key, s_len(key)))
  7711. return;
  7712. key_ok = 1;
  7713. }
  7714. if (!(r->single && key_ok) &&
  7715. !selector_match(d, node, attr_ptr(d, r->sel_off),
  7716. s_len(attr_ptr(d, r->sel_off)), key_ok))
  7717. return;
  7718. spec = (int)r->spec;
  7719. if (nrule_matches < RULE_MATCH_MAX) {
  7720. rule_matches[nrule_matches].idx = (unsigned short)idx;
  7721. rule_matches[nrule_matches].spec =
  7722. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  7723. rule_matches[nrule_matches].spec_imp =
  7724. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  7725. nrule_matches++;
  7726. }
  7727. }
  7728. static void rule_scan_list(struct br_doc *d, unsigned int node,
  7729. unsigned short head)
  7730. {
  7731. while (head != 0xFFFF) {
  7732. rule_try(d, node, head);
  7733. head = d->rule_next[head];
  7734. }
  7735. }
  7736. static void rule_scan_class(struct br_doc *d, unsigned int node,
  7737. const char *cls, unsigned int len)
  7738. {
  7739. if (len)
  7740. rule_scan_list(d, node,
  7741. d->rule_bucket[rule_hash(cls, len)]);
  7742. }
  7743. static void rule_collect(struct br_doc *d, unsigned int node)
  7744. {
  7745. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  7746. const char *id = attr_ptr(d, d->nodes[node].id_off);
  7747. unsigned int tag = d->nodes[node].tag;
  7748. unsigned int i;
  7749. nrule_matches = 0;
  7750. rule_scan_list(d, node, d->rule_any);
  7751. if (tag < BR_TAG_MAX)
  7752. rule_scan_list(d, node, d->rule_tag[tag]);
  7753. if (id[0])
  7754. rule_scan_list(d, node,
  7755. d->rule_bucket[rule_hash(id, s_len(id))]);
  7756. i = 0;
  7757. while (cls[i]) {
  7758. unsigned int st, n;
  7759. while (cls[i] && is_space(cls[i]))
  7760. i++;
  7761. st = i;
  7762. while (cls[i] && !is_space(cls[i]))
  7763. i++;
  7764. n = i - st;
  7765. if (n)
  7766. rule_scan_class(d, node, cls + st, n);
  7767. }
  7768. /* Rules must cascade in document order even though the index visits
  7769. * them in bucket order. */
  7770. for (i = 1; i < nrule_matches; i++) {
  7771. struct rule_match m = rule_matches[i];
  7772. unsigned int j = i;
  7773. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  7774. rule_matches[j] = rule_matches[j - 1];
  7775. j--;
  7776. }
  7777. rule_matches[j] = m;
  7778. }
  7779. }
  7780. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  7781. int font_px)
  7782. {
  7783. if (*kind == 4) {
  7784. *val = (unsigned short)(*val * font_px / 100);
  7785. *kind = 1;
  7786. } else if (*kind == 5) {
  7787. *val = (unsigned short)(*val * root_font_px / 100);
  7788. *kind = 1;
  7789. }
  7790. }
  7791. static void resolve_size_units(struct br_style *st)
  7792. {
  7793. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  7794. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  7795. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  7796. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  7797. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  7798. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  7799. }
  7800. static void set_border_side_w(struct br_style *st, int side, int v)
  7801. {
  7802. if (v < 0)
  7803. v = 0;
  7804. if (v > 32)
  7805. v = 32;
  7806. st->border_w[side] = (unsigned char)v;
  7807. if (st->border_w[side] && !st->border_c[side])
  7808. st->border_c[side] = st->color;
  7809. }
  7810. static void set_border_side_c(struct br_style *st, int side, int v)
  7811. {
  7812. st->border_c[side] = (unsigned short)v;
  7813. }
  7814. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  7815. {
  7816. switch (dcl->prop) {
  7817. case PROP_COLOR:
  7818. st->color = (unsigned short)dcl->val;
  7819. break;
  7820. case PROP_BG:
  7821. st->bg = (unsigned short)dcl->val;
  7822. st->has_bg = 1;
  7823. st->bg_set = 1;
  7824. break;
  7825. case PROP_BG_NONE:
  7826. st->has_bg = 0;
  7827. st->bg_set = 1;
  7828. break;
  7829. case PROP_BG_IMAGE:
  7830. st->bg_image = (unsigned short)dcl->val;
  7831. break;
  7832. case PROP_BG_REPEAT:
  7833. st->bg_repeat = (unsigned char)dcl->val;
  7834. break;
  7835. case PROP_BG_POS_X_KIND:
  7836. st->bg_pos_x_kind = (unsigned char)dcl->val;
  7837. break;
  7838. case PROP_BG_POS_X:
  7839. st->bg_pos_x = (signed short)dcl->val;
  7840. break;
  7841. case PROP_BG_POS_Y_KIND:
  7842. st->bg_pos_y_kind = (unsigned char)dcl->val;
  7843. break;
  7844. case PROP_BG_POS_Y:
  7845. st->bg_pos_y = (signed short)dcl->val;
  7846. break;
  7847. case PROP_BG_SIZE_KIND:
  7848. st->bg_size_kind = (unsigned char)dcl->val;
  7849. break;
  7850. case PROP_BG_SIZE_W:
  7851. st->bg_size_w = (unsigned short)dcl->val;
  7852. break;
  7853. case PROP_BG_SIZE_H:
  7854. st->bg_size_h = (unsigned short)dcl->val;
  7855. break;
  7856. case PROP_FONT_PX:
  7857. st->font_raw = (unsigned short)dcl->val;
  7858. break;
  7859. case PROP_FONT_UNIT:
  7860. st->font_unit = (unsigned char)dcl->val;
  7861. break;
  7862. case PROP_BOLD:
  7863. st->bold = (unsigned char)(dcl->val != 0);
  7864. break;
  7865. case PROP_ITALIC:
  7866. st->italic = (unsigned char)(dcl->val != 0);
  7867. break;
  7868. case PROP_MONO:
  7869. st->mono = (unsigned char)(dcl->val != 0);
  7870. break;
  7871. case PROP_FONT_FACE:
  7872. st->font_face = (unsigned char)dcl->val;
  7873. break;
  7874. case PROP_ALIGN:
  7875. st->align = (unsigned char)dcl->val;
  7876. break;
  7877. case PROP_UNDERLINE:
  7878. st->underline = (unsigned char)(dcl->val != 0);
  7879. break;
  7880. case PROP_STRIKE:
  7881. st->strike = (unsigned char)(dcl->val != 0);
  7882. break;
  7883. case PROP_OVERLINE:
  7884. st->overline = (unsigned char)(dcl->val != 0);
  7885. break;
  7886. case PROP_DISPLAY:
  7887. st->display = (unsigned char)(dcl->val &
  7888. ~BR_DISPLAY_INLINE_FLAG);
  7889. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  7890. st->list_item = 0;
  7891. break;
  7892. case PROP_VIS_HIDDEN:
  7893. st->vis_hidden = (unsigned char)(dcl->val != 0);
  7894. break;
  7895. case PROP_OPACITY_HIDDEN:
  7896. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  7897. break;
  7898. case PROP_FILL_MODE:
  7899. st->fill_mode = (unsigned char)dcl->val;
  7900. break;
  7901. case PROP_FILL:
  7902. st->fill = (unsigned short)dcl->val;
  7903. break;
  7904. case PROP_STROKE_MODE:
  7905. st->stroke_mode = (unsigned char)dcl->val;
  7906. break;
  7907. case PROP_STROKE:
  7908. st->stroke = (unsigned short)dcl->val;
  7909. break;
  7910. case PROP_STROKE_W:
  7911. st->stroke_w = (unsigned short)dcl->val;
  7912. break;
  7913. case PROP_FILL_RULE:
  7914. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  7915. break;
  7916. case PROP_OVERFLOW_HIDDEN:
  7917. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  7918. break;
  7919. case PROP_MT: st->mt = dcl->val; break;
  7920. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  7921. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  7922. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  7923. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  7924. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  7925. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  7926. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  7927. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  7928. case PROP_MR: st->mr = dcl->val; break;
  7929. case PROP_MB: st->mb = dcl->val; break;
  7930. case PROP_ML: st->ml = dcl->val; break;
  7931. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  7932. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  7933. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  7934. break;
  7935. case PROP_PT: st->pt = dcl->val; break;
  7936. case PROP_PR: st->pr = dcl->val; break;
  7937. case PROP_PB: st->pb = dcl->val; break;
  7938. case PROP_PL: st->pl = dcl->val; break;
  7939. case PROP_MT_U:
  7940. case PROP_MR_U:
  7941. case PROP_MB_U:
  7942. case PROP_ML_U:
  7943. case PROP_PT_U:
  7944. case PROP_PR_U:
  7945. case PROP_PB_U:
  7946. case PROP_PL_U:
  7947. st->mp_unit[dcl->prop - PROP_MT_U] =
  7948. (unsigned char)dcl->val;
  7949. break;
  7950. case PROP_BORDER_W:
  7951. set_border_side_w(st, 0, dcl->val);
  7952. set_border_side_w(st, 1, dcl->val);
  7953. set_border_side_w(st, 2, dcl->val);
  7954. set_border_side_w(st, 3, dcl->val);
  7955. break;
  7956. case PROP_BORDER_COLOR:
  7957. set_border_side_c(st, 0, dcl->val);
  7958. set_border_side_c(st, 1, dcl->val);
  7959. set_border_side_c(st, 2, dcl->val);
  7960. set_border_side_c(st, 3, dcl->val);
  7961. break;
  7962. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  7963. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  7964. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  7965. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  7966. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  7967. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  7968. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  7969. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  7970. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  7971. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  7972. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  7973. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  7974. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  7975. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  7976. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  7977. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  7978. case PROP_RADIUS_TL:
  7979. case PROP_RADIUS_TR:
  7980. case PROP_RADIUS_BR:
  7981. case PROP_RADIUS_BL:
  7982. st->radius[dcl->prop - PROP_RADIUS_TL] =
  7983. (unsigned char)dcl->val;
  7984. break;
  7985. case PROP_LIST_NONE:
  7986. st->no_bullet = (unsigned char)(dcl->val != 0);
  7987. break;
  7988. case PROP_LIST_STYLE:
  7989. st->list_style = (unsigned char)dcl->val;
  7990. break;
  7991. case PROP_CONTENT:
  7992. st->content = (unsigned short)dcl->val;
  7993. break;
  7994. case PROP_ASPECT_W:
  7995. st->aspect_w = (unsigned short)dcl->val;
  7996. break;
  7997. case PROP_ASPECT_H:
  7998. st->aspect_h = (unsigned short)dcl->val;
  7999. break;
  8000. case PROP_NOWRAP:
  8001. st->nowrap = (unsigned char)(dcl->val != 0);
  8002. break;
  8003. case PROP_WIDTH:
  8004. st->width = (unsigned short)dcl->val;
  8005. break;
  8006. case PROP_WIDTH_KIND:
  8007. st->width_kind = (unsigned char)dcl->val;
  8008. break;
  8009. case PROP_WIDTH_CALC_PCT:
  8010. st->width_calc_pct = dcl->val;
  8011. break;
  8012. case PROP_WIDTH_CALC_PX:
  8013. st->width_calc_px = dcl->val;
  8014. break;
  8015. case PROP_MAXW:
  8016. st->max_width = (unsigned short)dcl->val;
  8017. break;
  8018. case PROP_MAXW_KIND:
  8019. st->max_width_kind = (unsigned char)dcl->val;
  8020. break;
  8021. case PROP_MAXW_CALC_PCT:
  8022. st->max_width_calc_pct = (signed short)dcl->val;
  8023. break;
  8024. case PROP_MAXW_CALC_PX:
  8025. st->max_width_calc_px = (signed short)dcl->val;
  8026. break;
  8027. case PROP_MINW:
  8028. st->min_width = (unsigned short)dcl->val;
  8029. break;
  8030. case PROP_MINW_KIND:
  8031. st->min_width_kind = (unsigned char)dcl->val;
  8032. break;
  8033. case PROP_MINW_CALC_PCT:
  8034. st->min_width_calc_pct = (signed short)dcl->val;
  8035. break;
  8036. case PROP_MINW_CALC_PX:
  8037. st->min_width_calc_px = (signed short)dcl->val;
  8038. break;
  8039. case PROP_HEIGHT:
  8040. st->height = (unsigned short)dcl->val;
  8041. break;
  8042. case PROP_HEIGHT_KIND:
  8043. st->height_kind = (unsigned char)dcl->val;
  8044. break;
  8045. case PROP_HEIGHT_CALC_PCT:
  8046. st->height_calc_pct = (signed short)dcl->val;
  8047. break;
  8048. case PROP_HEIGHT_CALC_PX:
  8049. st->height_calc_px = (signed short)dcl->val;
  8050. break;
  8051. case PROP_MINH:
  8052. st->min_height = (unsigned short)dcl->val;
  8053. break;
  8054. case PROP_MINH_KIND:
  8055. st->min_height_kind = (unsigned char)dcl->val;
  8056. break;
  8057. case PROP_MINH_CALC_PCT:
  8058. st->min_height_calc_pct = (signed short)dcl->val;
  8059. break;
  8060. case PROP_MINH_CALC_PX:
  8061. st->min_height_calc_px = (signed short)dcl->val;
  8062. break;
  8063. case PROP_MAXH:
  8064. st->max_height = (unsigned short)dcl->val;
  8065. break;
  8066. case PROP_MAXH_KIND:
  8067. st->max_height_kind = (unsigned char)dcl->val;
  8068. break;
  8069. case PROP_MAXH_CALC_PCT:
  8070. st->max_height_calc_pct = (signed short)dcl->val;
  8071. break;
  8072. case PROP_MAXH_CALC_PX:
  8073. st->max_height_calc_px = (signed short)dcl->val;
  8074. break;
  8075. case PROP_LH_PCT:
  8076. st->line_h_pct = (unsigned char)dcl->val;
  8077. break;
  8078. case PROP_LH_PX:
  8079. st->line_h_px = (unsigned short)dcl->val;
  8080. break;
  8081. case PROP_UPPER:
  8082. st->uppercase = (unsigned char)(dcl->val != 0);
  8083. break;
  8084. case PROP_CURSOR:
  8085. st->cursor = (unsigned char)dcl->val;
  8086. break;
  8087. case PROP_FLOAT:
  8088. st->float_left = (unsigned char)dcl->val;
  8089. break;
  8090. case PROP_CLEAR:
  8091. st->clear = (unsigned char)dcl->val;
  8092. break;
  8093. case PROP_FLEX_GROW:
  8094. st->flex_grow = (unsigned char)dcl->val;
  8095. break;
  8096. case PROP_FLEX_COL:
  8097. st->flex_col = (unsigned char)(dcl->val != 0);
  8098. break;
  8099. case PROP_FLEX_WRAP:
  8100. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8101. break;
  8102. case PROP_JUSTIFY:
  8103. st->justify = (unsigned char)dcl->val;
  8104. break;
  8105. case PROP_ALIGN_ITEMS:
  8106. st->align_items = (unsigned char)dcl->val;
  8107. break;
  8108. case PROP_ALIGN_SELF:
  8109. st->align_self = (unsigned char)dcl->val;
  8110. break;
  8111. case PROP_GRID_NCOLS:
  8112. st->grid_ncols = (unsigned char)dcl->val;
  8113. break;
  8114. case PROP_GRID_GAP:
  8115. st->grid_gap = (unsigned char)dcl->val;
  8116. break;
  8117. case PROP_GRID_SLOT:
  8118. st->grid_slot = (unsigned char)dcl->val;
  8119. break;
  8120. case PROP_GRID_AUTO:
  8121. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8122. break;
  8123. case PROP_POS:
  8124. st->pos = (unsigned char)dcl->val;
  8125. break;
  8126. case PROP_LEFT:
  8127. st->left = dcl->val;
  8128. break;
  8129. case PROP_TOP:
  8130. st->top = dcl->val;
  8131. break;
  8132. case PROP_RIGHT:
  8133. st->right = dcl->val;
  8134. break;
  8135. case PROP_BOTTOM:
  8136. st->bottom = dcl->val;
  8137. break;
  8138. case PROP_LEFT_SET:
  8139. st->left_set = (unsigned char)(dcl->val != 0);
  8140. break;
  8141. case PROP_TOP_SET:
  8142. st->top_set = (unsigned char)(dcl->val != 0);
  8143. break;
  8144. case PROP_RIGHT_SET:
  8145. st->right_set = (unsigned char)(dcl->val != 0);
  8146. break;
  8147. case PROP_BOTTOM_SET:
  8148. st->bottom_set = (unsigned char)(dcl->val != 0);
  8149. break;
  8150. case PROP_ZINDEX:
  8151. st->z_index = dcl->val;
  8152. break;
  8153. case PROP_ZSET:
  8154. st->z_set = (unsigned char)(dcl->val != 0);
  8155. break;
  8156. case PROP_TRANSFORM_X:
  8157. st->tx = dcl->val;
  8158. break;
  8159. case PROP_TRANSFORM_Y:
  8160. st->ty = dcl->val;
  8161. break;
  8162. case PROP_TRANSFORM_SET:
  8163. st->transform_set = (unsigned char)(dcl->val != 0);
  8164. break;
  8165. case PROP_TRANSFORM_PAINT:
  8166. st->transform_scale = (unsigned char)(dcl->val != 0);
  8167. break;
  8168. case PROP_SCALE_X:
  8169. st->scale_x = (signed short)dcl->val;
  8170. break;
  8171. case PROP_SCALE_Y:
  8172. st->scale_y = (signed short)dcl->val;
  8173. break;
  8174. case PROP_ROTATE:
  8175. st->rotate_x100 = (signed short)dcl->val;
  8176. break;
  8177. case PROP_ORIGIN_X_KIND:
  8178. st->origin_x_kind = (unsigned char)dcl->val;
  8179. break;
  8180. case PROP_ORIGIN_X:
  8181. st->origin_x = (signed short)dcl->val;
  8182. break;
  8183. case PROP_ORIGIN_Y_KIND:
  8184. st->origin_y_kind = (unsigned char)dcl->val;
  8185. break;
  8186. case PROP_ORIGIN_Y:
  8187. st->origin_y = (signed short)dcl->val;
  8188. break;
  8189. default:
  8190. break;
  8191. }
  8192. }
  8193. /* The element's custom properties for rules flagged USES_SCOPED: the
  8194. * definitions of its matched rules in cascade order (precedence first,
  8195. * then document order), over the document-wide table. Inheritance from
  8196. * ancestors is not modelled; a name no matched rule defines falls back to
  8197. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8198. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8199. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8200. static void css_env_build(struct br_doc *d)
  8201. {
  8202. static unsigned short order[RULE_MATCH_MAX];
  8203. unsigned int i, need = 0, n = 0;
  8204. css_env_active = 0;
  8205. css_nlocal = 0;
  8206. for (i = 0; i < nrule_matches; i++)
  8207. if (d->rules[rule_matches[i].idx].var_scope &
  8208. BR_RULE_USES_SCOPED)
  8209. need = 1;
  8210. if (!need)
  8211. return;
  8212. /* Definitions apply lowest precedence first so the winner is
  8213. * written last (stable: document order within a precedence). */
  8214. for (i = 0; i < nrule_matches; i++) {
  8215. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8216. unsigned int j = n;
  8217. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8218. continue;
  8219. while (j > 0 && rule_matches[order[j - 1]].spec >
  8220. rule_matches[i].spec) {
  8221. order[j] = order[j - 1];
  8222. j--;
  8223. }
  8224. order[j] = i;
  8225. n++;
  8226. }
  8227. for (i = 0; i < n; i++) {
  8228. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8229. const char *raw = attr_ptr(d, r->raw_off);
  8230. unsigned int save = css_nlocal, k;
  8231. /* Collect this block's definitions after the earlier ones,
  8232. * replacing a name already present. */
  8233. {
  8234. unsigned int base = save;
  8235. css_local_append(raw, r->raw_len);
  8236. for (k = base; k < css_nlocal; k++) {
  8237. int prev = css_local_find_before(
  8238. css_local[k].name, css_local[k].nlen, base);
  8239. if (prev >= 0) {
  8240. css_local[prev] = css_local[k];
  8241. css_local[k].nlen = 0;
  8242. }
  8243. }
  8244. }
  8245. }
  8246. css_env_active = 1;
  8247. }
  8248. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8249. int spec, int spec_imp, short *best, short *best_imp)
  8250. {
  8251. unsigned char p = dcl->prop;
  8252. if (p >= PROP_COUNT)
  8253. return;
  8254. /* A transparent background competes with a colour for one slot. */
  8255. if (p == PROP_BG_NONE)
  8256. p = PROP_BG;
  8257. if (dcl->important) {
  8258. if (spec_imp >= best_imp[p]) {
  8259. apply_decl(st, dcl);
  8260. best_imp[p] = (short)spec_imp;
  8261. }
  8262. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8263. apply_decl(st, dcl);
  8264. best[p] = (short)spec;
  8265. }
  8266. }
  8267. void br_style_for(struct br_doc *d, unsigned int node,
  8268. const struct br_style *parent, struct br_style *out)
  8269. {
  8270. unsigned int tag;
  8271. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8272. unsigned int i;
  8273. if (!cascade_pe && d->nodes[node].style_valid) {
  8274. *out = d->nodes[node].style;
  8275. return;
  8276. }
  8277. *out = *parent;
  8278. out->has_bg = 0;
  8279. out->bg_set = 0;
  8280. out->bg_image = 0;
  8281. out->bg_repeat = 0;
  8282. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8283. out->bg_pos_x = out->bg_pos_y = 0;
  8284. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8285. out->float_left = 0;
  8286. out->clear = 0;
  8287. out->list_item = 0;
  8288. out->font_unit = 0;
  8289. for (i = 0; i < 4; i++) {
  8290. out->border_w[i] = 0;
  8291. out->border_c[i] = 0;
  8292. out->border_s[i] = BR_BORDER_SOLID;
  8293. out->radius[i] = 0;
  8294. }
  8295. out->width = out->max_width = out->min_width = out->height = 0;
  8296. out->min_height = out->max_height = 0;
  8297. out->width_calc_pct = out->width_calc_px = 0;
  8298. out->height_calc_pct = out->height_calc_px = 0;
  8299. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8300. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8301. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8302. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8303. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8304. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8305. out->box_sizing = 0;
  8306. out->ml_auto = out->mr_auto = 0;
  8307. out->mt = out->mr = out->mb = out->ml = 0;
  8308. out->pt = out->pr = out->pb = out->pl = 0;
  8309. for (i = 0; i < 8; i++) {
  8310. out->mp_unit[i] = 0;
  8311. out->mp_calc_px[i] = 0;
  8312. }
  8313. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8314. out->justify = out->align_items = out->align_self = 0;
  8315. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8316. out->grid_auto = 0;
  8317. out->aspect_w = out->aspect_h = 0;
  8318. out->pos = out->left_set = out->top_set = 0;
  8319. out->right_set = out->bottom_set = 0;
  8320. out->left = out->top = out->right = out->bottom = 0;
  8321. out->z_index = 0;
  8322. out->z_set = 0;
  8323. out->tx = out->ty = 0;
  8324. out->transform_set = 0;
  8325. out->transform_scale = 0;
  8326. out->scale_x = out->scale_y = 100;
  8327. out->rotate_x100 = 0;
  8328. out->origin_x = out->origin_y = 50;
  8329. out->origin_x_kind = out->origin_y_kind = 1;
  8330. out->vis_hidden = out->opacity_hidden = 0;
  8331. out->inline_flex = 0;
  8332. out->overflow_hidden = 0;
  8333. out->content = 0;
  8334. /* A pseudo-element is an anonymous inline box: none of the
  8335. * element's UA defaults or presentational hints apply to it. */
  8336. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8337. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8338. BR_DISPLAY_INLINE;
  8339. for (i = 0; i < PROP_COUNT; i++) {
  8340. best[i] = -1;
  8341. best_imp[i] = -1;
  8342. }
  8343. if (!cascade_pe)
  8344. switch (tag) {
  8345. case BR_TAG_HEAD:
  8346. case BR_TAG_TITLE:
  8347. case BR_TAG_META:
  8348. case BR_TAG_LINK:
  8349. case BR_TAG_BASE:
  8350. case BR_TAG_SCRIPT:
  8351. case BR_TAG_STYLE:
  8352. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8353. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8354. out->display = BR_DISPLAY_NONE;
  8355. break;
  8356. case BR_TAG_BODY:
  8357. out->mt = out->mb = out->ml = out->mr = 4;
  8358. break;
  8359. case BR_TAG_P:
  8360. out->mt = out->mb = 8;
  8361. break;
  8362. case BR_TAG_H1:
  8363. out->font_px = 32; out->bold = 1;
  8364. out->mt = 10; out->mb = 6;
  8365. break;
  8366. case BR_TAG_H2:
  8367. out->font_px = 24; out->bold = 1;
  8368. out->mt = 8; out->mb = 4;
  8369. break;
  8370. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8371. out->bold = 1; out->mt = 6; out->mb = 3;
  8372. if (tag == BR_TAG_H3)
  8373. out->font_px = 19;
  8374. else if (tag == BR_TAG_H4)
  8375. out->font_px = 17;
  8376. else if (tag == BR_TAG_H5)
  8377. out->font_px = 15;
  8378. else
  8379. out->font_px = 13;
  8380. break;
  8381. case BR_TAG_UL: case BR_TAG_OL:
  8382. out->mt = out->mb = 6; out->pl = 20;
  8383. break;
  8384. case BR_TAG_LI:
  8385. out->mt = out->mb = 2;
  8386. out->list_item = 1;
  8387. break;
  8388. case BR_TAG_BLOCKQUOTE:
  8389. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8390. break;
  8391. case BR_TAG_PRE:
  8392. out->mt = out->mb = 8; out->pl = 8;
  8393. out->has_bg = 1; out->bg = theme_panel;
  8394. out->mono = 1;
  8395. break;
  8396. case BR_TAG_CENTER:
  8397. out->align = BR_ALIGN_CENTER;
  8398. break;
  8399. case BR_TAG_A:
  8400. out->color = theme_link; out->underline = 1;
  8401. break;
  8402. case BR_TAG_B: case BR_TAG_STRONG:
  8403. out->bold = 1;
  8404. break;
  8405. case BR_TAG_I: case BR_TAG_EM:
  8406. out->italic = 1;
  8407. break;
  8408. case BR_TAG_U:
  8409. out->underline = 1;
  8410. break;
  8411. case BR_TAG_S: case BR_TAG_STRIKE:
  8412. out->strike = 1;
  8413. break;
  8414. case BR_TAG_CODE:
  8415. out->color = 0x8010;
  8416. out->mono = 1;
  8417. break;
  8418. case BR_TAG_SMALL:
  8419. out->font_px = 13;
  8420. break;
  8421. case BR_TAG_BIG:
  8422. out->font_px = 20;
  8423. break;
  8424. case BR_TAG_TD: case BR_TAG_TH:
  8425. out->pr = 12;
  8426. /* A cell establishes left-aligned content even inside a legacy
  8427. * <center> (HN's outer page wrapper); author CSS/align below
  8428. * can still override this. */
  8429. out->align = BR_ALIGN_LEFT;
  8430. break;
  8431. case BR_TAG_VIDEO:
  8432. out->display = BR_DISPLAY_BLOCK;
  8433. break;
  8434. case BR_TAG_SVG:
  8435. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8436. * external-link icon follows its link text on the same line.
  8437. * Flex rows still treat it as an item (flex_wants_row). */
  8438. out->display = BR_DISPLAY_INLINE;
  8439. break;
  8440. default:
  8441. break;
  8442. }
  8443. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8444. * presentational hints; author CSS still wins because it is applied
  8445. * below. */
  8446. if (!cascade_pe) {
  8447. unsigned short bgc;
  8448. const char *bga = br_find_attr(d, node, "bgcolor");
  8449. const char *ala = br_find_attr(d, node, "align");
  8450. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8451. out->bg = bgc;
  8452. out->has_bg = 1;
  8453. }
  8454. if (ala) {
  8455. if (same_string(ala, "right"))
  8456. out->align = BR_ALIGN_RIGHT;
  8457. else if (same_string(ala, "center"))
  8458. out->align = BR_ALIGN_CENTER;
  8459. else if (same_string(ala, "left"))
  8460. out->align = BR_ALIGN_LEFT;
  8461. }
  8462. }
  8463. /* presentational <font color>: author CSS below still wins */
  8464. if (!cascade_pe && d->nodes[node].color_off) {
  8465. unsigned short c;
  8466. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8467. if (parse_color(v, s_len(v), &c))
  8468. out->color = c;
  8469. }
  8470. /* SVG presentation attributes; CSS declarations still win below. */
  8471. if (!cascade_pe && in_svg(d, node)) {
  8472. const char *fa = br_find_attr(d, node, "fill");
  8473. const char *sa = br_find_attr(d, node, "stroke");
  8474. const char *wa = br_find_attr(d, node, "stroke-width");
  8475. const char *ra = br_find_attr(d, node, "fill-rule");
  8476. const char *ca = br_find_attr(d, node, "color");
  8477. unsigned short c;
  8478. int mode;
  8479. if (ca && parse_color(ca, s_len(ca), &c))
  8480. out->color = c;
  8481. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8482. out->fill_mode = (unsigned char)mode;
  8483. if (mode == BR_PAINT_COLOR)
  8484. out->fill = c;
  8485. }
  8486. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8487. out->stroke_mode = (unsigned char)mode;
  8488. if (mode == BR_PAINT_COLOR)
  8489. out->stroke = c;
  8490. }
  8491. if (wa) {
  8492. int sw = svg_len16(wa, s_len(wa));
  8493. if (sw >= 0)
  8494. out->stroke_w = (unsigned short)sw;
  8495. }
  8496. if (ra)
  8497. out->fill_evenodd = same_string(ra, "evenodd");
  8498. }
  8499. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8500. * Author CSS below can still override this. */
  8501. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8502. unsigned int p = d->nodes[node].parent;
  8503. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8504. !br_find_attr(d, p, "open"))
  8505. out->display = BR_DISPLAY_NONE;
  8506. }
  8507. rule_collect(d, node);
  8508. css_env_build(d);
  8509. for (i = 0; i < nrule_matches; i++) {
  8510. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8511. int spec = rule_matches[i].spec;
  8512. int spec_imp = rule_matches[i].spec_imp;
  8513. unsigned int k;
  8514. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8515. /* Re-expand against this element's variables. */
  8516. struct br_decl decls[BR_DECLS];
  8517. unsigned int nd;
  8518. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8519. decls, &nd);
  8520. for (k = 0; k < nd; k++)
  8521. cascade_decl(out, &decls[k], spec, spec_imp,
  8522. best, best_imp);
  8523. continue;
  8524. }
  8525. for (k = 0; k < r->nd; k++)
  8526. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8527. best_imp);
  8528. }
  8529. css_env_active = 0;
  8530. css_nlocal = 0;
  8531. if (!cascade_pe && d->nodes[node].style_off) {
  8532. struct br_decl decls[BR_DECLS];
  8533. unsigned int nd, k;
  8534. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8535. parse_decls(d, v, s_len(v), decls, &nd);
  8536. for (k = 0; k < nd; k++)
  8537. cascade_decl(out, &decls[k],
  8538. CASCADE_INLINE * CASCADE_BAND,
  8539. CASCADE_INLINE * CASCADE_BAND, best,
  8540. best_imp);
  8541. }
  8542. if (out->font_unit) {
  8543. int px = out->font_raw;
  8544. if (out->font_unit == 2)
  8545. px = parent->font_px * out->font_raw / 100;
  8546. else if (out->font_unit == 3)
  8547. px = root_font_px * out->font_raw / 100;
  8548. else if (out->font_unit == 4)
  8549. px = parent->font_px * out->font_raw / 10000;
  8550. if (px < 8)
  8551. px = 8;
  8552. if (px > 72)
  8553. px = 72;
  8554. out->font_px = (unsigned char)px;
  8555. }
  8556. resolve_size_units(out);
  8557. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  8558. out->display = BR_DISPLAY_NONE;
  8559. if (out->vis_hidden || out->opacity_hidden ||
  8560. (out->overflow_hidden && out->height_kind == 1 &&
  8561. out->height == 0))
  8562. out->display = BR_DISPLAY_NONE;
  8563. if (cascade_pe)
  8564. return;
  8565. d->nodes[node].style = *out;
  8566. d->nodes[node].style_valid = 1;
  8567. }
  8568. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  8569. const struct br_style *elem, struct br_style *out,
  8570. char *buf, unsigned int cap)
  8571. {
  8572. struct br_node *n;
  8573. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  8574. const char *c;
  8575. unsigned int len = 0;
  8576. if (out) {
  8577. out->content = 0;
  8578. out->display = BR_DISPLAY_NONE;
  8579. }
  8580. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  8581. (pe != 1 && pe != 2))
  8582. return 0;
  8583. n = &d->nodes[node];
  8584. if (n->type != BR_NODE_ELEM ||
  8585. ((n->pe_state & chk) && !(n->pe_state & has)))
  8586. return 0;
  8587. cascade_pe = pe;
  8588. br_style_for(d, node, elem, out);
  8589. cascade_pe = 0;
  8590. if (out->content == BR_CONTENT_EMPTY) {
  8591. /* No text; the caller may still draw the box (an icon
  8592. * with a size and a background image). */
  8593. buf[0] = '\0';
  8594. n->pe_state |= chk;
  8595. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  8596. out->pos != 2 && (out->width_kind || out->height_kind) &&
  8597. (out->has_bg || out->bg_image))
  8598. n->pe_state |= has;
  8599. else
  8600. out->display = BR_DISPLAY_NONE;
  8601. return 0;
  8602. }
  8603. if (out->content && out->display != BR_DISPLAY_NONE &&
  8604. out->pos != 1 && out->pos != 2) {
  8605. c = attr_ptr(d, d->content_off[out->content - 1]);
  8606. while (*c && len + 1 < cap) {
  8607. if (*c == '\001') {
  8608. char name[32];
  8609. unsigned int k = 0;
  8610. const char *v;
  8611. c++;
  8612. while (*c && *c != '\002' && k + 1 < sizeof(name))
  8613. name[k++] = *c++;
  8614. name[k] = '\0';
  8615. if (*c == '\002')
  8616. c++;
  8617. v = br_find_attr(d, node, name);
  8618. while (v && *v && len + 1 < cap)
  8619. buf[len++] = *v++;
  8620. continue;
  8621. }
  8622. buf[len++] = *c++;
  8623. }
  8624. }
  8625. buf[len] = '\0';
  8626. n->pe_state |= chk;
  8627. if (len)
  8628. n->pe_state |= has;
  8629. return len;
  8630. }
  8631. /* ---- HTML parser ---- */
  8632. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  8633. unsigned int sp, int *pre)
  8634. {
  8635. unsigned int i;
  8636. *pre = 0;
  8637. for (i = 1; i <= sp; i++)
  8638. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  8639. (*pre)++;
  8640. }
  8641. static void implicit_close(struct br_doc *d, unsigned short *stack,
  8642. unsigned int *sp, unsigned int tag)
  8643. {
  8644. while (*sp > 0) {
  8645. unsigned int top = d->nodes[stack[*sp]].tag;
  8646. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  8647. (*sp)--;
  8648. continue;
  8649. }
  8650. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  8651. (*sp)--;
  8652. continue;
  8653. }
  8654. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  8655. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  8656. (*sp)--;
  8657. continue;
  8658. }
  8659. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  8660. (*sp)--;
  8661. continue;
  8662. }
  8663. break;
  8664. }
  8665. }
  8666. static int has_attr_token(const char *s, unsigned int len, const char *name)
  8667. {
  8668. unsigned int i, n = s_len(name);
  8669. for (i = 0; i + n <= len; i++) {
  8670. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  8671. continue;
  8672. if (!ci_eq_n(s + i, name, n))
  8673. continue;
  8674. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  8675. return 1;
  8676. }
  8677. return 0;
  8678. }
  8679. static int stack_has_svg(const struct br_doc *d,
  8680. const unsigned short *stack, unsigned int sp)
  8681. {
  8682. unsigned int k;
  8683. for (k = 0; k <= sp && k < 64; k++)
  8684. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  8685. return 1;
  8686. return 0;
  8687. }
  8688. /* Skip an element subtree by finding its closing tag. Used for SVG
  8689. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  8690. * overwrite the document title. */
  8691. static unsigned int skip_element(const char *html, unsigned int len,
  8692. unsigned int i, const char *name,
  8693. unsigned int n)
  8694. {
  8695. char close[40];
  8696. unsigned int k = 0, p;
  8697. close[k++] = '<';
  8698. close[k++] = '/';
  8699. while (k < sizeof(close) - 1 && n--)
  8700. close[k++] = *name++;
  8701. p = (unsigned int)find_ci(html, len, close, k, i);
  8702. if ((int)p < 0)
  8703. return len;
  8704. p += k;
  8705. while (p < len && html[p] != '>')
  8706. p++;
  8707. if (p < len)
  8708. p++;
  8709. return p;
  8710. }
  8711. /* Store a <script src=...> value in the attribute arena. Kept separate from
  8712. * parse_attrs() so script-only attributes cannot be confused with the
  8713. * value/d attributes that also use src_off on other elements. */
  8714. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  8715. unsigned int len)
  8716. {
  8717. unsigned int i = 0;
  8718. while (i < len) {
  8719. unsigned int ns, ne, vs, ve;
  8720. char quote = 0;
  8721. while (i < len && is_space(s[i]))
  8722. i++;
  8723. if (i >= len || s[i] == '/')
  8724. break;
  8725. ns = i;
  8726. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  8727. i++;
  8728. ne = i;
  8729. while (i < len && is_space(s[i]))
  8730. i++;
  8731. if (i >= len || s[i] != '=')
  8732. continue;
  8733. i++;
  8734. while (i < len && is_space(s[i]))
  8735. i++;
  8736. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8737. quote = s[i++];
  8738. vs = i;
  8739. while (i < len && s[i] != quote)
  8740. i++;
  8741. ve = i;
  8742. if (i < len)
  8743. i++;
  8744. } else {
  8745. vs = i;
  8746. while (i < len && !is_space(s[i]) && s[i] != '>')
  8747. i++;
  8748. ve = i;
  8749. }
  8750. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  8751. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  8752. }
  8753. return 0;
  8754. }
  8755. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  8756. * application/json, text/template, ...) and non-JS type values. External
  8757. * src scripts are allowed; the browser fetches them separately. Returns 1
  8758. * for a classic script, 2 for type="module", 0 to skip (also for a
  8759. * `nomodule` fallback, since module scripts run). */
  8760. static int script_is_javascript(const char *s, unsigned int len)
  8761. {
  8762. int kind = 1;
  8763. unsigned int i = 0;
  8764. while (i < len) {
  8765. unsigned int ns, ne, vs = 0, ve = 0;
  8766. char quote = 0;
  8767. int has_val = 0;
  8768. while (i < len && is_space(s[i]))
  8769. i++;
  8770. if (i >= len)
  8771. break;
  8772. ns = i;
  8773. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  8774. i++;
  8775. ne = i;
  8776. while (i < len && is_space(s[i]))
  8777. i++;
  8778. if (i < len && s[i] == '=') {
  8779. i++;
  8780. while (i < len && is_space(s[i]))
  8781. i++;
  8782. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8783. quote = s[i++];
  8784. vs = i;
  8785. while (i < len && s[i] != quote)
  8786. i++;
  8787. ve = i;
  8788. if (i < len)
  8789. i++;
  8790. } else {
  8791. vs = i;
  8792. while (i < len && !is_space(s[i]) && s[i] != '/')
  8793. i++;
  8794. ve = i;
  8795. }
  8796. has_val = 1;
  8797. } else if (i == ne) {
  8798. i++;
  8799. }
  8800. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  8801. return 0;
  8802. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  8803. unsigned int vn = has_val ? ve - vs : 0;
  8804. if (vn == 0)
  8805. continue;
  8806. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  8807. kind = 2;
  8808. continue;
  8809. }
  8810. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  8811. (vn == 22 &&
  8812. ci_eq_n(s + vs, "application/javascript", 22)) ||
  8813. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  8814. (vn == 22 &&
  8815. ci_eq_n(s + vs, "application/ecmascript", 22)))
  8816. continue;
  8817. return 0;
  8818. }
  8819. }
  8820. return kind;
  8821. }
  8822. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  8823. {
  8824. unsigned short stack[64];
  8825. unsigned int sp = 0;
  8826. unsigned int i = 0;
  8827. int pre = 0;
  8828. unsigned int css_len = 0;
  8829. if (!d)
  8830. return -1;
  8831. if (!d->nodes)
  8832. br_doc_init(d);
  8833. if (!d->nodes || !d->text || !d->attrs)
  8834. return -1;
  8835. doc_reset(d);
  8836. d->script_src = html;
  8837. stack[0] = 0;
  8838. d->nodes[0].type = BR_NODE_ELEM;
  8839. d->nodes[0].tag = BR_TAG_UNKNOWN;
  8840. while (i < len) {
  8841. if (html[i] != '<') {
  8842. unsigned int st = i;
  8843. while (i < len && html[i] != '<')
  8844. i++;
  8845. if (sp <= 63)
  8846. append_text(d, stack[sp], html + st, i - st, pre);
  8847. continue;
  8848. }
  8849. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  8850. html[i + 3] == '-') {
  8851. int p = find_ci(html, len, "-->", 3, i + 4);
  8852. i = p >= 0 ? (unsigned int)p + 3 : len;
  8853. continue;
  8854. }
  8855. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  8856. while (i < len && html[i] != '>')
  8857. i++;
  8858. if (i < len)
  8859. i++;
  8860. continue;
  8861. }
  8862. if (i + 1 < len && html[i + 1] == '/') {
  8863. unsigned int ns = i + 2, ne, tag, k, n;
  8864. char name[32];
  8865. while (ns < len && is_space(html[ns]))
  8866. ns++;
  8867. ne = ns;
  8868. while (ne < len && is_ident(html[ne]))
  8869. ne++;
  8870. if (ne == ns) {
  8871. i++;
  8872. continue;
  8873. }
  8874. n = ne - ns;
  8875. if (n >= sizeof(name))
  8876. n = sizeof(name) - 1;
  8877. for (k = 0; k < n; k++)
  8878. name[k] = html[ns + k];
  8879. name[n] = '\0';
  8880. tag = br_tag_from_name(name);
  8881. i = ne;
  8882. while (i < len && html[i] != '>')
  8883. i++;
  8884. if (i < len)
  8885. i++;
  8886. for (k = sp; k > 0; k--) {
  8887. struct br_node *nd = &d->nodes[stack[k]];
  8888. if (tag == BR_TAG_UNKNOWN) {
  8889. const char *raw = attr_ptr(d, nd->name_off);
  8890. if (nd->tag == BR_TAG_UNKNOWN &&
  8891. ci_eq_n(raw, name, n) &&
  8892. raw[n] == '\0') {
  8893. sp = k - 1;
  8894. break;
  8895. }
  8896. } else if (nd->tag == tag) {
  8897. sp = k - 1;
  8898. break;
  8899. }
  8900. }
  8901. stack_recount_pre(d, stack, sp, &pre);
  8902. continue;
  8903. }
  8904. /* start tag */
  8905. {
  8906. unsigned int ns = i + 1, ne, tag, k, n;
  8907. char name[16];
  8908. unsigned int attr_start, attr_end;
  8909. int selfclose = 0;
  8910. while (ns < len && is_space(html[ns]))
  8911. ns++;
  8912. ne = ns;
  8913. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  8914. ne++;
  8915. if (ne == ns) {
  8916. /* stray '<' */
  8917. append_text(d, stack[sp], html + i, 1, pre);
  8918. i++;
  8919. continue;
  8920. }
  8921. n = ne - ns;
  8922. if (n >= sizeof(name))
  8923. n = sizeof(name) - 1;
  8924. for (k = 0; k < n; k++)
  8925. name[k] = html[ns + k];
  8926. name[n] = '\0';
  8927. tag = br_tag_from_name(name);
  8928. attr_start = ne;
  8929. {
  8930. unsigned int q = ne;
  8931. char quote = 0;
  8932. while (q < len) {
  8933. char c = html[q];
  8934. if (quote) {
  8935. if (c == quote)
  8936. quote = 0;
  8937. } else if (c == '"' || c == '\'') {
  8938. quote = c;
  8939. } else if (c == '>') {
  8940. break;
  8941. }
  8942. q++;
  8943. }
  8944. attr_end = q;
  8945. /* `/>` closes the tag only when the slash is
  8946. * not the end of an unquoted attribute value:
  8947. * Docusaurus writes `<a href=/installation/>`,
  8948. * which used to self-close the link and leave
  8949. * its label outside it. */
  8950. if (q > attr_start && html[q - 1] == '/' &&
  8951. (q - 1 == attr_start ||
  8952. is_space(html[q - 2]) ||
  8953. html[q - 2] == '"' || html[q - 2] == '\''))
  8954. selfclose = 1;
  8955. i = q < len ? q + 1 : len;
  8956. }
  8957. if (tag == BR_TAG_SCRIPT) {
  8958. int p = find_ci(html, len, "</script", 8, i);
  8959. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8960. unsigned int idx;
  8961. struct br_script *script = 0;
  8962. int kind = script_is_javascript(html + attr_start,
  8963. attr_end - attr_start);
  8964. if (kind) {
  8965. if (d->nscripts < BR_MAX_SCRIPTS) {
  8966. script = &d->scripts[d->nscripts++];
  8967. script->module = kind == 2;
  8968. script->node = 0;
  8969. script->off = i;
  8970. script->len = end > i ? end - i : 0;
  8971. script->src_off = script_src_attr(
  8972. d, html + attr_start,
  8973. attr_end - attr_start);
  8974. } else {
  8975. d->scripts_truncated = 1;
  8976. }
  8977. }
  8978. /* Keep the element in the DOM even though its body is
  8979. * executed separately: scripts (Piwik, analytics)
  8980. * insert a sibling after getElementsByTagName('script')
  8981. * [0], and the element must exist for that to work. */
  8982. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8983. (unsigned char)tag);
  8984. if (idx)
  8985. parse_attrs(d, &d->nodes[idx],
  8986. html + attr_start,
  8987. attr_end - attr_start);
  8988. if (script)
  8989. script->node = idx;
  8990. if (idx && !kind && end > i)
  8991. append_raw_text(d, idx, html + i, end - i);
  8992. i = p >= 0 ? (unsigned int)p + 8 : len;
  8993. while (i < len && html[i] != '>')
  8994. i++;
  8995. if (i < len)
  8996. i++;
  8997. continue;
  8998. }
  8999. /* Scripting is always enabled, so <noscript> is a
  9000. * raw-text element (HTML "in head/in body, scripting
  9001. * flag set"): its markup must not reach the DOM or the
  9002. * cascade. google.com puts a meta refresh and
  9003. * `table,div,span,p{display:none}` in one, which hid
  9004. * the whole page and left only the "click here" link. */
  9005. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9006. int p = find_ci(html, len, "</noscript", 10, i);
  9007. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9008. unsigned int idx;
  9009. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9010. (unsigned char)tag);
  9011. if (idx) {
  9012. parse_attrs(d, &d->nodes[idx],
  9013. html + attr_start,
  9014. attr_end - attr_start);
  9015. if (end > i)
  9016. append_raw_text(d, idx, html + i,
  9017. end - i);
  9018. }
  9019. i = p >= 0 ? (unsigned int)p + 10 : len;
  9020. while (i < len && html[i] != '>')
  9021. i++;
  9022. if (i < len)
  9023. i++;
  9024. continue;
  9025. }
  9026. if (stack_has_svg(d, stack, sp) &&
  9027. (tag == BR_TAG_TITLE ||
  9028. (tag == BR_TAG_UNKNOWN &&
  9029. (ci_eq_n(name, "desc", 4) ||
  9030. ci_eq_n(name, "metadata", 8))))) {
  9031. i = skip_element(html, len, i, name, n);
  9032. continue;
  9033. }
  9034. /* A closed <details> is collapsed at style time, not
  9035. * parse time: the <summary> (the hamburger button on
  9036. * tagesschau and many mobile menus) must stay in the
  9037. * DOM. */
  9038. if (tag == BR_TAG_STYLE) {
  9039. int p = find_ci(html, len, "</style", 7, i);
  9040. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9041. unsigned int cend = end;
  9042. unsigned int idx;
  9043. while (cend > i && is_space(html[cend - 1]))
  9044. cend--;
  9045. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9046. for (k = i; k < cend; k++)
  9047. css_store[css_len++] = html[k];
  9048. css_store[css_len++] = '\n';
  9049. }
  9050. /* The CSS is collected above, but keep a display:none
  9051. * element so getElementsByTagName('style')/parentNode
  9052. * behave like a real document. */
  9053. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9054. (unsigned char)tag);
  9055. if (idx)
  9056. parse_attrs(d, &d->nodes[idx],
  9057. html + attr_start,
  9058. attr_end - attr_start);
  9059. i = p >= 0 ? (unsigned int)p + 7 : len;
  9060. while (i < len && html[i] != '>')
  9061. i++;
  9062. if (i < len)
  9063. i++;
  9064. continue;
  9065. }
  9066. if (tag == BR_TAG_TITLE) {
  9067. int p = find_ci(html, len, "</title", 7, i);
  9068. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9069. unsigned int tn = 0;
  9070. char tmp[BR_TITLE_MAX];
  9071. tn = decode_text(html + i, end - i, tmp,
  9072. sizeof(tmp), 1);
  9073. (void)tn;
  9074. s_copy(d->title, tmp, sizeof(d->title));
  9075. i = p >= 0 ? (unsigned int)p + 7 : len;
  9076. while (i < len && html[i] != '>')
  9077. i++;
  9078. if (i < len)
  9079. i++;
  9080. continue;
  9081. }
  9082. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9083. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9084. unsigned int idx;
  9085. implicit_close(d, stack, &sp, tag);
  9086. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9087. (unsigned char)tag);
  9088. if (idx)
  9089. parse_attrs(d, &d->nodes[idx],
  9090. html + attr_start,
  9091. attr_end - attr_start);
  9092. continue;
  9093. }
  9094. if (sp >= 63) {
  9095. d->truncated = 1;
  9096. continue;
  9097. }
  9098. implicit_close(d, stack, &sp, tag);
  9099. {
  9100. unsigned int idx = node_append(d, stack[sp],
  9101. BR_NODE_ELEM,
  9102. (unsigned char)tag);
  9103. if (!idx)
  9104. continue;
  9105. parse_attrs(d, &d->nodes[idx],
  9106. html + attr_start,
  9107. attr_end - attr_start);
  9108. if (tag == BR_TAG_UNKNOWN)
  9109. d->nodes[idx].name_off =
  9110. store_attr(d, name, n, 31);
  9111. /* HTML ignores the self-closing flag on a
  9112. * normal element (`<div/>` opens a div); only
  9113. * SVG content (and unknown/custom tags, kept
  9114. * as before) honours it. */
  9115. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9116. tag != BR_TAG_SVG &&
  9117. !stack_has_svg(d, stack, sp))
  9118. selfclose = 0;
  9119. if (!selfclose) {
  9120. sp++;
  9121. stack[sp] = (unsigned short)idx;
  9122. if (tag == BR_TAG_PRE)
  9123. pre++;
  9124. }
  9125. }
  9126. }
  9127. }
  9128. d->inline_css_len = css_len;
  9129. return 0;
  9130. }
  9131. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9132. {
  9133. if (!d->nodes)
  9134. br_doc_init(d);
  9135. if (!d->nodes || !d->text || !d->attrs)
  9136. return -1;
  9137. doc_reset(d);
  9138. d->plain = 1;
  9139. d->nodes[0].type = BR_NODE_ELEM;
  9140. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9141. append_text(d, 0, text, len, 1);
  9142. return 0;
  9143. }
  9144. /* A standalone image response: a synthetic document with one <img>. The
  9145. * caller decodes the already-fetched body into image slot 0 and sets the
  9146. * returned node's img_index. */
  9147. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9148. {
  9149. unsigned int idx;
  9150. if (!d->nodes)
  9151. br_doc_init(d);
  9152. if (!d->nodes || !d->text || !d->attrs)
  9153. return 0;
  9154. doc_reset(d);
  9155. d->nodes[0].type = BR_NODE_ELEM;
  9156. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9157. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9158. if (!idx)
  9159. return 0;
  9160. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9161. return idx;
  9162. }