browser_html.c 251 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938
  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. /* The cached disabled/open/inert bits must not survive into
  884. * the next page: a slot that held a disabled control made
  885. * Docusaurus' search field unfocusable after navigating (and
  886. * real hardware does not hand out zeroed memory). */
  887. d->nodes[i].attr_flags = 0;
  888. d->nodes[i].parent = 0;
  889. d->nodes[i].first = d->nodes[i].last = 0;
  890. d->nodes[i].next = 0;
  891. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  892. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  893. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  894. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  895. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  896. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  897. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  898. d->nodes[i].name_off = 0;
  899. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  900. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  901. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  902. d->nodes[i].svg_rx = 0;
  903. d->nodes[i].img_index = -1;
  904. d->nodes[i].bg_img_index = -1;
  905. d->nodes[i].style_valid = 0;
  906. d->nodes[i].pe_state = 0;
  907. }
  908. }
  909. static void node_zero(struct br_node *n);
  910. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  911. unsigned char type, unsigned char tag)
  912. {
  913. unsigned int idx;
  914. struct br_node *n;
  915. if (d->nnode >= BR_MAX_NODES) {
  916. d->truncated = 1;
  917. return 0;
  918. }
  919. idx = d->nnode++;
  920. n = &d->nodes[idx];
  921. node_zero(n); /* every field, attr_flags included */
  922. n->type = type;
  923. n->tag = tag;
  924. n->parent = (unsigned short)parent;
  925. if (d->nodes[parent].last)
  926. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  927. else
  928. d->nodes[parent].first = (unsigned short)idx;
  929. d->nodes[parent].last = (unsigned short)idx;
  930. return idx;
  931. }
  932. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  933. const char *s, unsigned int len, int pre)
  934. {
  935. char *dst;
  936. unsigned int n;
  937. unsigned int idx;
  938. if (len == 0)
  939. return 0;
  940. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  941. d->truncated = 1;
  942. return 0;
  943. }
  944. dst = d->text + d->text_used;
  945. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  946. if (n == 0) {
  947. d->text[d->text_used] = '\0';
  948. return 0;
  949. }
  950. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  951. if (!idx)
  952. return 0;
  953. d->nodes[idx].text_off = d->text_used;
  954. d->nodes[idx].text_len = (unsigned short)n;
  955. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  956. d->text_used += n + 1;
  957. return idx;
  958. }
  959. /* Raw text child (no entity decoding, whitespace kept): the body of a
  960. * data-block <script> such as type="application/json", which scripts read
  961. * through textContent. Bodies longer than a text node holds are dropped. */
  962. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  963. const char *s, unsigned int len)
  964. {
  965. unsigned int idx, i;
  966. if (len == 0 || len > 0xFFFFu)
  967. return 0;
  968. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  969. d->truncated = 1;
  970. return 0;
  971. }
  972. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  973. if (!idx)
  974. return 0;
  975. for (i = 0; i < len; i++)
  976. d->text[d->text_used + i] = s[i];
  977. d->text[d->text_used + len] = '\0';
  978. d->nodes[idx].text_off = d->text_used;
  979. d->nodes[idx].text_len = (unsigned short)len;
  980. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  981. d->text_used += len + 1;
  982. return idx;
  983. }
  984. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  985. static int same_string(const char *a, const char *b)
  986. {
  987. while (*a && *a == *b) {
  988. a++;
  989. b++;
  990. }
  991. return *a == *b;
  992. }
  993. static int same_string_n(const char *a, const char *b, unsigned int n)
  994. {
  995. unsigned int i;
  996. for (i = 0; i < n; i++)
  997. if (a[i] != b[i])
  998. return 0;
  999. return 1;
  1000. }
  1001. static int node_detached(struct br_doc *d, unsigned int node)
  1002. {
  1003. unsigned int guard = 0;
  1004. while (node && node < d->nnode && guard++ <= d->nnode) {
  1005. if (d->nodes[node].flags & BR_NODE_DETACHED)
  1006. return 1;
  1007. node = d->nodes[node].parent;
  1008. }
  1009. return 0;
  1010. }
  1011. int br_node_detached(struct br_doc *d, unsigned int node)
  1012. {
  1013. return !d || node >= d->nnode || node_detached(d, node);
  1014. }
  1015. unsigned int br_find_id(struct br_doc *d, const char *id)
  1016. {
  1017. unsigned int i;
  1018. if (!d || !id)
  1019. return 0;
  1020. for (i = 0; i < d->nnode; i++) {
  1021. struct br_node *n = &d->nodes[i];
  1022. if (n->type == BR_NODE_ELEM && n->id_off &&
  1023. same_string(d->attrs + n->id_off, id) &&
  1024. !node_detached(d, i))
  1025. return i + 1;
  1026. }
  1027. return 0;
  1028. }
  1029. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1030. unsigned int cap, unsigned int *used)
  1031. {
  1032. unsigned int c;
  1033. if (node >= d->nnode)
  1034. return;
  1035. if (d->nodes[node].type == BR_NODE_TEXT) {
  1036. unsigned int len = d->nodes[node].text_len;
  1037. unsigned int off = d->nodes[node].text_off;
  1038. if (*used + len >= cap)
  1039. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1040. for (c = 0; c < len; c++)
  1041. out[*used + c] = d->text[off + c];
  1042. *used += len;
  1043. return;
  1044. }
  1045. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1046. text_walk(d, c, out, cap, used);
  1047. }
  1048. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1049. char *out, unsigned int cap)
  1050. {
  1051. unsigned int used = 0;
  1052. if (!d || !out || cap == 0 || node >= d->nnode)
  1053. return 0;
  1054. text_walk(d, node, out, cap, &used);
  1055. out[used] = '\0';
  1056. return used;
  1057. }
  1058. int br_set_text_content(struct br_doc *d, unsigned int node,
  1059. const char *text, unsigned int len)
  1060. {
  1061. struct br_node *n;
  1062. unsigned int idx;
  1063. unsigned int i;
  1064. if (!d || node >= d->nnode)
  1065. return -1;
  1066. /* Detach the old subtree; its arena storage is reclaimed on the next
  1067. * navigation. */
  1068. {
  1069. unsigned int c;
  1070. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1071. d->nodes[c].flags |= BR_NODE_DETACHED;
  1072. }
  1073. d->nodes[node].first = d->nodes[node].last = 0;
  1074. d->nodes[node].style_valid = 0;
  1075. d->nodes[node].pe_state = 0;
  1076. if (len == 0)
  1077. return 0;
  1078. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1079. d->truncated = 1;
  1080. return -1;
  1081. }
  1082. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1083. if (!idx)
  1084. return -1;
  1085. for (i = 0; i < len; i++)
  1086. d->text[d->text_used + i] = text[i];
  1087. d->text[d->text_used + len] = '\0';
  1088. n = &d->nodes[idx];
  1089. n->text_off = d->text_used;
  1090. n->text_len = (unsigned short)len;
  1091. n->flags = 0;
  1092. d->text_used += len + 1;
  1093. return 0;
  1094. }
  1095. /* Replace the value of an existing text or comment node in place, keeping the
  1096. * node handle (and therefore the JS wrapper) valid. */
  1097. int br_set_text_node(struct br_doc *d, unsigned int node,
  1098. const char *text, unsigned int len)
  1099. {
  1100. struct br_node *n;
  1101. unsigned int i;
  1102. if (!d || node >= d->nnode)
  1103. return -1;
  1104. n = &d->nodes[node];
  1105. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1106. return -1;
  1107. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1108. d->truncated = 1;
  1109. return -1;
  1110. }
  1111. for (i = 0; i < len; i++)
  1112. d->text[d->text_used + i] = text[i];
  1113. d->text[d->text_used + len] = '\0';
  1114. n->text_off = d->text_used;
  1115. n->text_len = (unsigned short)len;
  1116. d->text_used += len + 1;
  1117. return 0;
  1118. }
  1119. /* ---- runtime DOM construction and mutation ---- */
  1120. static unsigned int text_len(const char *s)
  1121. {
  1122. unsigned int n = 0;
  1123. while (s && s[n])
  1124. n++;
  1125. return n;
  1126. }
  1127. static void node_zero(struct br_node *n)
  1128. {
  1129. unsigned int i;
  1130. for (i = 0; i < sizeof(*n); i++)
  1131. ((unsigned char *)n)[i] = 0;
  1132. n->img_index = -1;
  1133. n->bg_img_index = -1;
  1134. }
  1135. static void node_unlink(struct br_doc *d, unsigned int handle)
  1136. {
  1137. unsigned int node, parent, c, prev = 0;
  1138. if (!d || handle == 0 || handle > d->nnode)
  1139. return;
  1140. node = handle - 1;
  1141. parent = d->nodes[node].parent;
  1142. if (parent >= d->nnode) {
  1143. d->nodes[node].parent = 0;
  1144. d->nodes[node].next = 0;
  1145. return;
  1146. }
  1147. c = d->nodes[parent].first;
  1148. while (c && c != node) {
  1149. prev = c;
  1150. c = d->nodes[c].next;
  1151. }
  1152. if (c == node) {
  1153. if (prev)
  1154. d->nodes[prev].next = d->nodes[node].next;
  1155. else
  1156. d->nodes[parent].first = d->nodes[node].next;
  1157. if (d->nodes[parent].last == node)
  1158. d->nodes[parent].last = (unsigned short)prev;
  1159. }
  1160. d->nodes[node].parent = 0;
  1161. d->nodes[node].next = 0;
  1162. d->nodes[node].flags |= BR_NODE_DETACHED;
  1163. }
  1164. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1165. const char *s, unsigned int len);
  1166. static unsigned int find_tag_end(const char *s, unsigned int i,
  1167. unsigned int len)
  1168. {
  1169. char quote = 0;
  1170. while (i < len) {
  1171. char c = s[i];
  1172. if (quote) {
  1173. if (c == quote)
  1174. quote = 0;
  1175. } else if (c == '"' || c == '\'') {
  1176. quote = c;
  1177. } else if (c == '>') {
  1178. return i;
  1179. }
  1180. i++;
  1181. }
  1182. return len;
  1183. }
  1184. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1185. const char *html, unsigned int len)
  1186. {
  1187. unsigned int stack[64];
  1188. unsigned int sp = 1, i = 0;
  1189. if (!d || node >= d->nnode)
  1190. return -1;
  1191. stack[0] = node;
  1192. {
  1193. unsigned int c;
  1194. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1195. d->nodes[c].flags |= BR_NODE_DETACHED;
  1196. }
  1197. d->nodes[node].first = d->nodes[node].last = 0;
  1198. d->nodes[node].style_valid = 0;
  1199. d->nodes[node].pe_state = 0;
  1200. while (i < len) {
  1201. if (html[i] != '<') {
  1202. unsigned int start = i, end;
  1203. while (i < len && html[i] != '<')
  1204. i++;
  1205. end = i;
  1206. if (end > start) {
  1207. /* Entities decoded like the page parser does
  1208. * (createTextNode stays literal). */
  1209. char *tb = (char *)malloc(end - start + 1);
  1210. unsigned int tl = 0, h = 0;
  1211. if (tb) {
  1212. tl = decode_text(html + start, end - start,
  1213. tb, end - start + 1, 1);
  1214. h = br_create_text(d, tb, tl);
  1215. free(tb);
  1216. }
  1217. if (h)
  1218. br_append_child(d, stack[sp - 1], h);
  1219. }
  1220. continue;
  1221. }
  1222. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1223. html[i + 3] == '-') {
  1224. int p = find_ci(html, len, "-->", 3, i + 4);
  1225. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1226. unsigned int h = br_create_comment(d, html + i + 4,
  1227. end > i + 4 ?
  1228. end - i - 4 : 0);
  1229. if (h)
  1230. br_append_child(d, stack[sp - 1], h);
  1231. i = p >= 0 ? (unsigned int)p + 3 : len;
  1232. continue;
  1233. }
  1234. if (html[i + 1] == '/') {
  1235. unsigned int p = find_tag_end(html, i + 2, len);
  1236. i = p < len ? p + 1 : len;
  1237. if (sp > 1)
  1238. sp--;
  1239. continue;
  1240. }
  1241. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1242. unsigned int p = find_tag_end(html, i + 2, len);
  1243. i = p < len ? p + 1 : len;
  1244. continue;
  1245. }
  1246. {
  1247. unsigned int p = find_tag_end(html, i + 1, len);
  1248. unsigned int ns = i + 1, ne, as, ae;
  1249. unsigned int h, k;
  1250. char name[32];
  1251. int selfclose = 0;
  1252. while (ns < p && is_space(html[ns]))
  1253. ns++;
  1254. ne = ns;
  1255. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1256. ne++;
  1257. as = ne;
  1258. ae = p;
  1259. if (ae > as && html[ae - 1] == '/') {
  1260. selfclose = 1;
  1261. ae--;
  1262. }
  1263. if (ne == ns) {
  1264. i = p < len ? p + 1 : len;
  1265. continue;
  1266. }
  1267. if (ne - ns >= sizeof(name)) {
  1268. i = p < len ? p + 1 : len;
  1269. continue;
  1270. }
  1271. for (k = 0; k < ne - ns; k++)
  1272. name[k] = html[ns + k];
  1273. name[ne - ns] = '\0';
  1274. h = br_create_element(d, name, ne - ns);
  1275. if (!h) {
  1276. i = p < len ? p + 1 : len;
  1277. continue;
  1278. }
  1279. if (ae > as)
  1280. parse_attrs(d, &d->nodes[h - 1], html + as,
  1281. ae - as);
  1282. br_append_child(d, stack[sp - 1], h);
  1283. {
  1284. unsigned int tag = d->nodes[h - 1].tag;
  1285. if (!selfclose && !br_tag_is_void(tag) &&
  1286. sp < sizeof(stack) / sizeof(stack[0]))
  1287. stack[sp++] = h - 1;
  1288. }
  1289. i = p < len ? p + 1 : len;
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1295. {
  1296. unsigned int i;
  1297. if (!d)
  1298. return 0;
  1299. for (i = 0; i < d->nnode; i++)
  1300. if (d->nodes[i].type == BR_NODE_ELEM &&
  1301. d->nodes[i].tag == (unsigned char)tag &&
  1302. !node_detached(d, i))
  1303. return i + 1;
  1304. return 0;
  1305. }
  1306. unsigned int br_create_element(struct br_doc *d, const char *name,
  1307. unsigned int len)
  1308. {
  1309. struct br_node *n;
  1310. unsigned int idx;
  1311. if (!d || !name)
  1312. return 0;
  1313. if (d->nnode >= BR_MAX_NODES) {
  1314. d->truncated = 1;
  1315. return 0;
  1316. }
  1317. idx = d->nnode++;
  1318. n = &d->nodes[idx];
  1319. node_zero(n);
  1320. n->type = BR_NODE_ELEM;
  1321. n->flags = BR_NODE_DETACHED;
  1322. n->tag = (unsigned char)br_tag_from_name(name);
  1323. if (n->tag == BR_TAG_UNKNOWN && len)
  1324. n->name_off = store_attr(d, name, len, 31);
  1325. return idx + 1;
  1326. }
  1327. unsigned int br_create_text(struct br_doc *d, const char *text,
  1328. unsigned int len)
  1329. {
  1330. struct br_node *n;
  1331. unsigned int idx, i;
  1332. if (!d)
  1333. return 0;
  1334. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1335. d->truncated = 1;
  1336. return 0;
  1337. }
  1338. idx = d->nnode++;
  1339. n = &d->nodes[idx];
  1340. node_zero(n);
  1341. n->type = BR_NODE_TEXT;
  1342. n->flags = BR_NODE_DETACHED;
  1343. n->text_off = d->text_used;
  1344. n->text_len = (unsigned short)len;
  1345. for (i = 0; i < len; i++)
  1346. d->text[d->text_used + i] = text[i];
  1347. d->text[d->text_used + len] = '\0';
  1348. d->text_used += len + 1;
  1349. return idx + 1;
  1350. }
  1351. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1352. unsigned int len)
  1353. {
  1354. unsigned int h = br_create_text(d, text, len);
  1355. if (h)
  1356. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1357. return h;
  1358. }
  1359. unsigned int br_create_fragment(struct br_doc *d)
  1360. {
  1361. struct br_node *n;
  1362. unsigned int idx;
  1363. if (!d)
  1364. return 0;
  1365. if (d->nnode >= BR_MAX_NODES) {
  1366. d->truncated = 1;
  1367. return 0;
  1368. }
  1369. idx = d->nnode++;
  1370. n = &d->nodes[idx];
  1371. node_zero(n);
  1372. n->type = BR_NODE_FRAGMENT;
  1373. n->flags = BR_NODE_DETACHED;
  1374. return idx + 1;
  1375. }
  1376. int br_append_child(struct br_doc *d, unsigned int parent,
  1377. unsigned int child)
  1378. {
  1379. unsigned int p;
  1380. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1381. parent == child - 1)
  1382. return -1;
  1383. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1384. /* Appending a fragment inserts its children, not the fragment. */
  1385. unsigned int c = d->nodes[child - 1].first;
  1386. while (c) {
  1387. unsigned int next = d->nodes[c].next;
  1388. br_append_child(d, parent, c + 1);
  1389. c = next;
  1390. }
  1391. return 0;
  1392. }
  1393. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1394. if (p == child - 1)
  1395. return -1;
  1396. node_unlink(d, child);
  1397. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1398. d->nodes[child - 1].parent = (unsigned short)parent;
  1399. d->nodes[child - 1].next = 0;
  1400. if (d->nodes[parent].last)
  1401. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1402. else
  1403. d->nodes[parent].first = (unsigned short)(child - 1);
  1404. d->nodes[parent].last = (unsigned short)(child - 1);
  1405. return 0;
  1406. }
  1407. int br_insert_before(struct br_doc *d, unsigned int parent,
  1408. unsigned int child, unsigned int before)
  1409. {
  1410. unsigned int prev = 0, c;
  1411. if (before == 0)
  1412. return br_append_child(d, parent, child);
  1413. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1414. before > d->nnode || parent == child - 1)
  1415. return -1;
  1416. if (d->nodes[before - 1].parent != parent)
  1417. return -1;
  1418. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1419. unsigned int c = d->nodes[child - 1].first;
  1420. while (c) {
  1421. unsigned int next = d->nodes[c].next;
  1422. br_insert_before(d, parent, c + 1, before);
  1423. c = next;
  1424. }
  1425. return 0;
  1426. }
  1427. {
  1428. unsigned int p;
  1429. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1430. if (p == child - 1)
  1431. return -1;
  1432. }
  1433. node_unlink(d, child);
  1434. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1435. c = d->nodes[parent].first;
  1436. while (c && c != before - 1) {
  1437. prev = c;
  1438. c = d->nodes[c].next;
  1439. }
  1440. if (c != before - 1)
  1441. return -1;
  1442. d->nodes[child - 1].parent = (unsigned short)parent;
  1443. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1444. if (prev)
  1445. d->nodes[prev].next = (unsigned short)(child - 1);
  1446. else
  1447. d->nodes[parent].first = (unsigned short)(child - 1);
  1448. return 0;
  1449. }
  1450. int br_remove_child(struct br_doc *d, unsigned int parent,
  1451. unsigned int child)
  1452. {
  1453. if (!d || child == 0 || child > d->nnode ||
  1454. d->nodes[child - 1].parent != parent)
  1455. return -1;
  1456. node_unlink(d, child);
  1457. return 0;
  1458. }
  1459. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1460. unsigned int child)
  1461. {
  1462. unsigned int c;
  1463. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1464. return 0;
  1465. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1466. if (c == child - 1)
  1467. return child;
  1468. return 0;
  1469. }
  1470. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1471. const char *name)
  1472. {
  1473. struct br_node *n;
  1474. const char *p, *end, *found = 0;
  1475. if (!d || node >= d->nnode || !name)
  1476. return 0;
  1477. n = &d->nodes[node];
  1478. p = d->attrs + n->extra_off;
  1479. end = p + n->extra_len;
  1480. while (p + 1 < end) {
  1481. const char *an = p;
  1482. const char *av;
  1483. p += text_len(p) + 1;
  1484. if (p >= end)
  1485. break;
  1486. av = p;
  1487. p += text_len(p) + 1;
  1488. if (same_string(an, name))
  1489. found = av;
  1490. }
  1491. if (found)
  1492. return found;
  1493. if (same_string(name, "id") && n->id_off)
  1494. return d->attrs + n->id_off;
  1495. if (same_string(name, "class") && n->class_off)
  1496. return d->attrs + n->class_off;
  1497. if (same_string(name, "style") && n->style_off)
  1498. return d->attrs + n->style_off;
  1499. if (same_string(name, "href") && n->href_off)
  1500. return d->attrs + n->href_off;
  1501. if (same_string(name, "src") && n->src_off)
  1502. return d->attrs + n->src_off;
  1503. if (same_string(name, "alt") && n->alt_off)
  1504. return d->attrs + n->alt_off;
  1505. if (same_string(name, "rel") && n->rel_off)
  1506. return d->attrs + n->rel_off;
  1507. return 0;
  1508. }
  1509. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1510. const char *name, const char *value,
  1511. unsigned int len)
  1512. {
  1513. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1514. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1515. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1516. * exactly like parse_attrs(); script-built inputs (Vue sets
  1517. * type="password" after createElement) must land there too. */
  1518. if (control && same_string(name, "type")) {
  1519. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1520. return;
  1521. }
  1522. if (control && same_string(name, "value")) {
  1523. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1524. return;
  1525. }
  1526. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1527. same_string(name, "placeholder")) {
  1528. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1529. return;
  1530. }
  1531. if (same_string(name, "id"))
  1532. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1533. else if (same_string(name, "class"))
  1534. n->class_off = len ? store_attr(d, value, len,
  1535. BR_CLASS_MAX) : 0;
  1536. else if (same_string(name, "style"))
  1537. n->style_off = len ? store_attr(d, value, len,
  1538. BR_STYLE_ATTR_MAX) : 0;
  1539. else if (same_string(name, "href"))
  1540. n->href_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1541. else if (same_string(name, "src"))
  1542. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1543. else if (same_string(name, "alt"))
  1544. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1545. else if (same_string(name, "rel"))
  1546. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1547. }
  1548. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1549. const char *value, unsigned int len)
  1550. {
  1551. struct br_node *n;
  1552. unsigned int start, nl, need;
  1553. const char *p, *old, *oldend;
  1554. if (!d || node >= d->nnode || !name)
  1555. return -1;
  1556. n = &d->nodes[node];
  1557. nl = text_len(name);
  1558. need = nl + 1 + len + 1;
  1559. start = d->attrs_used;
  1560. old = d->attrs + n->extra_off;
  1561. oldend = old + n->extra_len;
  1562. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1563. d->truncated = 1;
  1564. return -1;
  1565. }
  1566. p = old;
  1567. while (p + 1 < oldend) {
  1568. const char *an = p;
  1569. const char *av;
  1570. unsigned int anl, avl, k;
  1571. anl = text_len(an);
  1572. p += anl + 1;
  1573. if (p >= oldend)
  1574. break;
  1575. av = p;
  1576. avl = text_len(av);
  1577. p += avl + 1;
  1578. if (same_string(an, name))
  1579. continue;
  1580. for (k = 0; k < anl; k++)
  1581. d->attrs[d->attrs_used++] = an[k];
  1582. d->attrs[d->attrs_used++] = '\0';
  1583. for (k = 0; k < avl; k++)
  1584. d->attrs[d->attrs_used++] = av[k];
  1585. d->attrs[d->attrs_used++] = '\0';
  1586. }
  1587. {
  1588. unsigned int k;
  1589. for (k = 0; k < nl; k++)
  1590. d->attrs[d->attrs_used++] = name[k];
  1591. d->attrs[d->attrs_used++] = '\0';
  1592. for (k = 0; k < len; k++)
  1593. d->attrs[d->attrs_used++] = value[k];
  1594. d->attrs[d->attrs_used++] = '\0';
  1595. }
  1596. n = &d->nodes[node];
  1597. n->extra_off = start;
  1598. n->extra_len = (unsigned short)(d->attrs_used - start);
  1599. n->style_valid = 0;
  1600. n->pe_state = 0;
  1601. attr_note_known(d, n, name, value, len);
  1602. /* A script that sets a style/class (Angular's ng-style) or a new src
  1603. * after the first image scan re-arms the node for the loader; -2
  1604. * ("nothing to load") would otherwise be final. */
  1605. if ((same_string(name, "style") || same_string(name, "class")) &&
  1606. n->bg_img_index == -2)
  1607. n->bg_img_index = -1;
  1608. if (same_string(name, "src") && n->img_index == -2)
  1609. n->img_index = -1;
  1610. return 0;
  1611. }
  1612. static void clear_attr_flag(struct br_node *n, const char *name);
  1613. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1614. {
  1615. struct br_node *n;
  1616. unsigned int start;
  1617. const char *p, *oldend;
  1618. int found = 0;
  1619. if (!d || node >= d->nnode || !name)
  1620. return -1;
  1621. n = &d->nodes[node];
  1622. start = d->attrs_used;
  1623. p = d->attrs + n->extra_off;
  1624. oldend = p + n->extra_len;
  1625. if (start + n->extra_len > BR_ATTR_SIZE)
  1626. return -1;
  1627. while (p + 1 < oldend) {
  1628. const char *an = p;
  1629. const char *av;
  1630. unsigned int anl, avl, k;
  1631. anl = text_len(an);
  1632. p += anl + 1;
  1633. if (p >= oldend)
  1634. break;
  1635. av = p;
  1636. avl = text_len(av);
  1637. p += avl + 1;
  1638. if (same_string(an, name)) {
  1639. found = 1;
  1640. continue;
  1641. }
  1642. for (k = 0; k < anl; k++)
  1643. d->attrs[d->attrs_used++] = an[k];
  1644. d->attrs[d->attrs_used++] = '\0';
  1645. for (k = 0; k < avl; k++)
  1646. d->attrs[d->attrs_used++] = av[k];
  1647. d->attrs[d->attrs_used++] = '\0';
  1648. }
  1649. n = &d->nodes[node];
  1650. n->extra_off = start;
  1651. n->extra_len = (unsigned short)(d->attrs_used - start);
  1652. n->style_valid = 0;
  1653. n->pe_state = 0;
  1654. attr_note_known(d, n, name, "", 0);
  1655. if (found)
  1656. clear_attr_flag(n, name);
  1657. return found ? 0 : -1;
  1658. }
  1659. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1660. {
  1661. struct br_node *src;
  1662. unsigned int h;
  1663. const char *name;
  1664. if (!d || node == 0 || node > d->nnode)
  1665. return 0;
  1666. src = &d->nodes[node - 1];
  1667. if (src->type == BR_NODE_TEXT)
  1668. return br_create_text(d, d->text + src->text_off, src->text_len);
  1669. if (src->type == BR_NODE_COMMENT)
  1670. return br_create_comment(d, d->text + src->text_off,
  1671. src->text_len);
  1672. if (src->type == BR_NODE_FRAGMENT)
  1673. h = br_create_fragment(d);
  1674. else {
  1675. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1676. name = d->attrs + src->name_off;
  1677. else
  1678. name = br_tag_name(src->tag);
  1679. h = br_create_element(d, name, text_len(name));
  1680. }
  1681. if (!h)
  1682. return 0;
  1683. {
  1684. const char *p = d->attrs + src->extra_off;
  1685. const char *end = p + src->extra_len;
  1686. while (p + 1 < end) {
  1687. const char *an = p;
  1688. const char *av;
  1689. unsigned int anl, avl;
  1690. anl = text_len(an);
  1691. p += anl + 1;
  1692. if (p >= end)
  1693. break;
  1694. av = p;
  1695. avl = text_len(av);
  1696. p += avl + 1;
  1697. /* h is a 1-based handle; br_set_attr takes the
  1698. * node index (the old call stamped the copy's
  1699. * attributes onto the previous node). */
  1700. br_set_attr(d, h - 1, an, av, avl);
  1701. }
  1702. }
  1703. if (deep) {
  1704. unsigned int c;
  1705. for (c = src->first; c; c = d->nodes[c].next) {
  1706. unsigned int ch = br_clone_node(d, c + 1, 1);
  1707. if (ch)
  1708. br_append_child(d, h - 1, ch);
  1709. }
  1710. }
  1711. return h;
  1712. }
  1713. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1714. unsigned int len);
  1715. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1716. unsigned int *st, unsigned int *en);
  1717. /* querySelector(All) and Element.matches use the cascade's selector engine
  1718. * (sel_match(), below). */
  1719. static int match_selector(struct br_doc *d, unsigned int node,
  1720. const char *sel, unsigned int len)
  1721. {
  1722. while (len > 0 && sel[0] == ' ') {
  1723. sel++;
  1724. len--;
  1725. }
  1726. while (len > 0 && sel[len - 1] == ' ')
  1727. len--;
  1728. return len && sel_match(d, node, sel, len);
  1729. }
  1730. static int query_add(unsigned int *out, unsigned int max,
  1731. unsigned int *count, unsigned int node)
  1732. {
  1733. unsigned int i;
  1734. for (i = 0; i < *count; i++)
  1735. if (out[i] == node + 1)
  1736. return 0;
  1737. if (*count < max)
  1738. out[(*count)++] = node + 1;
  1739. return 0;
  1740. }
  1741. static void query_walk(struct br_doc *d, unsigned int node,
  1742. const char *sel, unsigned int len, unsigned int *out,
  1743. unsigned int max, unsigned int *count)
  1744. {
  1745. unsigned int c;
  1746. if (d->nodes[node].type == BR_NODE_ELEM &&
  1747. match_selector(d, node, sel, len))
  1748. query_add(out, max, count, node);
  1749. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1750. query_walk(d, c, sel, len, out, max, count);
  1751. }
  1752. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1753. {
  1754. unsigned int len;
  1755. if (!d || !selector || node == 0 || node > d->nnode)
  1756. return 0;
  1757. len = text_len(selector);
  1758. while (len > 0 && selector[0] == ' ') {
  1759. selector++;
  1760. len--;
  1761. }
  1762. while (len > 0 && selector[len - 1] == ' ')
  1763. len--;
  1764. {
  1765. unsigned int pos = 0, st, en;
  1766. while (sel_list_next(selector, len, &pos, &st, &en))
  1767. if (match_selector(d, node - 1, selector + st, en - st))
  1768. return 1;
  1769. }
  1770. return 0;
  1771. }
  1772. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1773. const char *selector, unsigned int *out,
  1774. unsigned int max)
  1775. {
  1776. unsigned int count = 0, i = 0, len, start, end;
  1777. if (!d || !selector || !out || max == 0)
  1778. return 0;
  1779. len = text_len(selector);
  1780. while (sel_list_next(selector, len, &i, &start, &end)) {
  1781. if (root == 0) {
  1782. unsigned int n;
  1783. for (n = 1; n < d->nnode; n++)
  1784. if (d->nodes[n].type == BR_NODE_ELEM &&
  1785. !node_detached(d, n) &&
  1786. match_selector(d, n, selector + start,
  1787. end - start))
  1788. query_add(out, max, &count, n);
  1789. } else if (root <= d->nnode) {
  1790. unsigned int c;
  1791. for (c = d->nodes[root - 1].first; c;
  1792. c = d->nodes[c].next)
  1793. query_walk(d, c, selector + start,
  1794. end - start, out, max, &count);
  1795. }
  1796. }
  1797. return count;
  1798. }
  1799. /* ---- attribute parsing ---- */
  1800. static int is_space(char c)
  1801. {
  1802. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1803. }
  1804. static int str_eq(const char *a, const char *b);
  1805. static unsigned int parse_uint(const char *s, unsigned int len)
  1806. {
  1807. unsigned int i = 0, v = 0;
  1808. while (i < len && is_space(s[i]))
  1809. i++;
  1810. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1811. v = v * 10 + (unsigned int)(s[i] - '0');
  1812. if (v > 4096)
  1813. v = 4096;
  1814. i++;
  1815. }
  1816. return v;
  1817. }
  1818. static int contains_ci_len(const char *s, unsigned int len,
  1819. const char *needle);
  1820. static int parse_svg_viewbox(const char *s, unsigned int len,
  1821. unsigned short *w, unsigned short *h)
  1822. {
  1823. unsigned int i = 0, v[4], n = 0;
  1824. while (i < len && n < 4) {
  1825. unsigned int x = 0;
  1826. int any = 0;
  1827. while (i < len && (is_space(s[i]) || s[i] == ','))
  1828. i++;
  1829. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1830. x = x * 10 + (unsigned int)(s[i] - '0');
  1831. i++;
  1832. any = 1;
  1833. }
  1834. /* viewBox values are commonly fractional ("0 0 340.16
  1835. * 340.16"). Consume the fraction so it is not mistaken for
  1836. * the next number; the integer part is enough for the aspect
  1837. * ratio used by the renderer. */
  1838. if (i < len && s[i] == '.') {
  1839. i++;
  1840. while (i < len && s[i] >= '0' && s[i] <= '9')
  1841. i++;
  1842. }
  1843. if (!any) {
  1844. if (i < len)
  1845. i++;
  1846. continue;
  1847. }
  1848. v[n++] = x;
  1849. }
  1850. if (n < 4)
  1851. return 0;
  1852. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1853. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1854. return *w && *h;
  1855. }
  1856. /* The inverse of note_attr_flag() for a removed attribute. The cached
  1857. * bit used to outlive the attribute: `input.disabled = false` removed
  1858. * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
  1859. * once (Docusaurus' search box while its index loads) never took focus
  1860. * again. */
  1861. static void clear_attr_flag(struct br_node *n, const char *name)
  1862. {
  1863. if (str_eq(name, "data-collapse-target"))
  1864. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
  1865. else if (str_eq(name, "data-collapse-trigger"))
  1866. n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
  1867. else if (str_eq(name, "is-open"))
  1868. n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
  1869. else if (str_eq(name, "disabled"))
  1870. n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
  1871. else if (str_eq(name, "open"))
  1872. n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
  1873. else if (str_eq(name, "inert"))
  1874. n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
  1875. }
  1876. static void note_attr_flag(struct br_node *n, const char *name)
  1877. {
  1878. if (str_eq(name, "data-collapse-target"))
  1879. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1880. else if (str_eq(name, "data-collapse-trigger"))
  1881. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1882. else if (str_eq(name, "is-open"))
  1883. n->attr_flags |= BR_ATTR_IS_OPEN;
  1884. else if (str_eq(name, "disabled"))
  1885. n->attr_flags |= BR_ATTR_DISABLED;
  1886. else if (str_eq(name, "open"))
  1887. n->attr_flags |= BR_ATTR_OPEN;
  1888. else if (str_eq(name, "inert"))
  1889. n->attr_flags |= BR_ATTR_INERT;
  1890. }
  1891. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1892. * second pass so the block stays contiguous even though the known attributes
  1893. * above also use the shared attr arena. */
  1894. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1895. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1896. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1897. * written. */
  1898. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1899. {
  1900. static const struct { const char *e; char c; } ents[] = {
  1901. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1902. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1903. { 0, 0 }
  1904. };
  1905. unsigned int i = 0, n = 0;
  1906. while (i < len) {
  1907. int k, done = 0;
  1908. if (s[i] == '&') {
  1909. for (k = 0; ents[k].e; k++) {
  1910. unsigned int el = (unsigned int)strlen(ents[k].e);
  1911. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1912. out[n++] = ents[k].c;
  1913. i += el;
  1914. done = 1;
  1915. break;
  1916. }
  1917. }
  1918. if (!done && i + 3 < len && s[i + 1] == '#') {
  1919. unsigned int j = i + 2, v = 0;
  1920. int hex = s[j] == 'x' || s[j] == 'X';
  1921. if (hex)
  1922. j++;
  1923. while (j < len && j < i + 10 &&
  1924. ((s[j] >= '0' && s[j] <= '9') ||
  1925. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1926. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1927. char c = s[j++];
  1928. v = v * (hex ? 16 : 10) +
  1929. (unsigned int)(c <= '9' ? c - '0' :
  1930. (c | 0x20) - 'a' + 10);
  1931. }
  1932. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1933. out[n++] = (char)v;
  1934. i = j + 1;
  1935. done = 1;
  1936. }
  1937. }
  1938. }
  1939. if (!done)
  1940. out[n++] = s[i++];
  1941. }
  1942. return n;
  1943. }
  1944. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1945. const char *s, unsigned int len)
  1946. {
  1947. unsigned int i = 0;
  1948. unsigned int start = d->attrs_used;
  1949. while (i < len) {
  1950. char name[32];
  1951. unsigned int nl = 0, vstart, vlen;
  1952. char quote = 0;
  1953. while (i < len && is_space(s[i]))
  1954. i++;
  1955. if (i >= len || s[i] == '/')
  1956. break;
  1957. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1958. char c = s[i++];
  1959. if (c >= 'A' && c <= 'Z')
  1960. c = (char)(c - 'A' + 'a');
  1961. if (nl + 1 < sizeof(name))
  1962. name[nl++] = c;
  1963. }
  1964. name[nl] = '\0';
  1965. while (i < len && is_space(s[i]))
  1966. i++;
  1967. if (i < len && s[i] == '=') {
  1968. i++;
  1969. while (i < len && is_space(s[i]))
  1970. i++;
  1971. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1972. quote = s[i++];
  1973. vstart = i;
  1974. while (i < len && s[i] != quote)
  1975. i++;
  1976. vlen = i - vstart;
  1977. if (i < len)
  1978. i++;
  1979. } else {
  1980. vstart = i;
  1981. while (i < len && !is_space(s[i]) && s[i] != '>')
  1982. i++;
  1983. vlen = i - vstart;
  1984. }
  1985. } else {
  1986. /* Boolean attribute: expose it with an empty value. */
  1987. vstart = 0;
  1988. vlen = 0;
  1989. }
  1990. if (!nl)
  1991. continue;
  1992. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1993. d->truncated = 1;
  1994. break;
  1995. }
  1996. {
  1997. unsigned int k;
  1998. for (k = 0; k < nl; k++)
  1999. d->attrs[d->attrs_used++] = name[k];
  2000. d->attrs[d->attrs_used++] = '\0';
  2001. d->attrs_used += attr_unescape(s + vstart, vlen,
  2002. d->attrs + d->attrs_used);
  2003. d->attrs[d->attrs_used++] = '\0';
  2004. }
  2005. }
  2006. if (d->attrs_used > start) {
  2007. n->extra_off = start;
  2008. n->extra_len = (unsigned short)(d->attrs_used - start);
  2009. }
  2010. }
  2011. /* Parse attributes from `s` (between the tag name and '>'). */
  2012. static void parse_attrs(struct br_doc *d, struct br_node *n,
  2013. const char *s, unsigned int len)
  2014. {
  2015. unsigned int i = 0;
  2016. while (i < len) {
  2017. char name[32];
  2018. unsigned int nl = 0, vstart, vlen;
  2019. char quote = 0;
  2020. while (i < len && is_space(s[i]))
  2021. i++;
  2022. if (i >= len || s[i] == '/')
  2023. break;
  2024. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  2025. char c = s[i++];
  2026. if (c >= 'A' && c <= 'Z')
  2027. c = (char)(c - 'A' + 'a');
  2028. if (nl + 1 < sizeof(name))
  2029. name[nl++] = c;
  2030. }
  2031. name[nl] = '\0';
  2032. while (i < len && is_space(s[i]))
  2033. i++;
  2034. if (i >= len || s[i] != '=') {
  2035. if (str_eq(name, "hidden"))
  2036. n->flags |= BR_NODE_HIDDEN;
  2037. note_attr_flag(n, name);
  2038. continue;
  2039. }
  2040. i++;
  2041. while (i < len && is_space(s[i]))
  2042. i++;
  2043. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2044. quote = s[i++];
  2045. vstart = i;
  2046. while (i < len && s[i] != quote)
  2047. i++;
  2048. vlen = i - vstart;
  2049. if (i < len)
  2050. i++;
  2051. } else {
  2052. vstart = i;
  2053. while (i < len && !is_space(s[i]) && s[i] != '>')
  2054. i++;
  2055. vlen = i - vstart;
  2056. }
  2057. if (!nl)
  2058. continue;
  2059. if (str_eq(name, "hidden")) {
  2060. n->flags |= BR_NODE_HIDDEN;
  2061. continue;
  2062. }
  2063. note_attr_flag(n, name);
  2064. if (str_eq(name, "href"))
  2065. n->href_off = store_attr(d, s + vstart, vlen,
  2066. BR_URL_ATTR_MAX);
  2067. else if (str_eq(name, "class"))
  2068. n->class_off = store_attr(d, s + vstart, vlen,
  2069. BR_CLASS_MAX);
  2070. else if (str_eq(name, "id"))
  2071. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2072. else if (str_eq(name, "style"))
  2073. n->style_off = store_attr(d, s + vstart, vlen,
  2074. BR_STYLE_ATTR_MAX);
  2075. else if (str_eq(name, "src"))
  2076. n->src_off = store_attr(d, s + vstart, vlen,
  2077. BR_URL_ATTR_MAX);
  2078. else if (str_eq(name, "alt"))
  2079. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2080. else if (str_eq(name, "color"))
  2081. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2082. else if (str_eq(name, "rel"))
  2083. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2084. else if (str_eq(name, "value"))
  2085. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2086. else if (str_eq(name, "placeholder") &&
  2087. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2088. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2089. else if (str_eq(name, "type") &&
  2090. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2091. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2092. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2093. else if (str_eq(name, "width")) {
  2094. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2095. if (vlen && s[vstart + vlen - 1] == '%')
  2096. n->width_pct = 1;
  2097. } else if (str_eq(name, "height")) {
  2098. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2099. if (vlen && s[vstart + vlen - 1] == '%')
  2100. n->height_pct = 1;
  2101. } else if (str_eq(name, "colspan")) {
  2102. unsigned int v = parse_uint(s + vstart, vlen);
  2103. if (v < 1)
  2104. v = 1;
  2105. if (v > 16)
  2106. v = 16;
  2107. n->colspan = (unsigned char)v;
  2108. } else if (str_eq(name, "rowspan")) {
  2109. unsigned int v = parse_uint(s + vstart, vlen);
  2110. if (v < 1)
  2111. v = 1;
  2112. if (v > 16)
  2113. v = 16;
  2114. n->rowspan = (unsigned char)v;
  2115. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2116. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2117. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2118. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2119. } else if (str_eq(name, "x2")) {
  2120. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2121. } else if (str_eq(name, "y2")) {
  2122. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2123. } else if (str_eq(name, "rx")) {
  2124. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2125. } else if (str_eq(name, "d")) {
  2126. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2127. } else if (str_eq(name, "viewbox")) {
  2128. unsigned short vw = 0, vh = 0;
  2129. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2130. n->width_attr = vw;
  2131. n->height_attr = vh;
  2132. n->flags |= BR_NODE_VIEWBOX;
  2133. }
  2134. } else if (str_eq(name, "text-anchor")) {
  2135. if (contains_ci_len(s + vstart, vlen, "middle"))
  2136. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2137. else if (contains_ci_len(s + vstart, vlen, "end"))
  2138. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2139. }
  2140. }
  2141. parse_attrs_generic(d, n, s, len);
  2142. }
  2143. static int str_eq(const char *a, const char *b)
  2144. {
  2145. unsigned int i = 0;
  2146. while (a[i] && b[i] && a[i] == b[i])
  2147. i++;
  2148. return a[i] == '\0' && b[i] == '\0';
  2149. }
  2150. /* ---- CSS ---- */
  2151. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2152. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2153. {
  2154. return parse_color(s, len, out);
  2155. }
  2156. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2157. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2158. {
  2159. while (len && is_space(*s)) {
  2160. s++;
  2161. len--;
  2162. }
  2163. while (len && is_space(s[len - 1]))
  2164. len--;
  2165. if (!len)
  2166. return 0;
  2167. if (len == 4 && ci_eq_n(s, "none", 4))
  2168. return BR_PAINT_NONE;
  2169. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2170. return BR_PAINT_CURRENT;
  2171. if (len > 4 && ci_eq_n(s, "url(", 4))
  2172. return BR_PAINT_URL;
  2173. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2174. return BR_PAINT_NONE;
  2175. if (parse_color(s, len, c))
  2176. return BR_PAINT_COLOR;
  2177. return 0;
  2178. }
  2179. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2180. static int svg_len16(const char *s, unsigned int len)
  2181. {
  2182. unsigned int i = 0;
  2183. int v = 0, frac = 0, digits = 0;
  2184. while (i < len && is_space(s[i]))
  2185. i++;
  2186. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2187. if (v < 4000)
  2188. v = v * 10 + (s[i] - '0');
  2189. i++;
  2190. digits++;
  2191. }
  2192. if (i < len && s[i] == '.') {
  2193. int scale = 1;
  2194. i++;
  2195. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2196. if (scale < 10000) {
  2197. frac = frac * 10 + (s[i] - '0');
  2198. scale *= 10;
  2199. }
  2200. i++;
  2201. digits++;
  2202. }
  2203. frac = frac * 16 / scale;
  2204. }
  2205. if (!digits)
  2206. return -1;
  2207. return v * 16 + frac;
  2208. }
  2209. /* Inside an <svg> subtree (including the <svg> element itself). */
  2210. static int in_svg(const struct br_doc *d, unsigned int node)
  2211. {
  2212. unsigned int k;
  2213. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2214. if (d->nodes[node].tag == BR_TAG_SVG)
  2215. return 1;
  2216. node = d->nodes[node].parent;
  2217. }
  2218. return 0;
  2219. }
  2220. enum {
  2221. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2222. PROP_BG_REPEAT,
  2223. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2224. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2225. PROP_FONT_PX, PROP_FONT_UNIT,
  2226. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2227. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2228. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2229. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2230. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2231. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2232. PROP_BORDER_W, PROP_BORDER_COLOR,
  2233. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2234. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2235. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2236. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2237. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2238. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2239. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2240. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2241. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2242. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2243. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2244. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2245. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2246. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2247. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2248. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2249. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2250. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2251. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2252. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2253. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2254. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2255. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2256. PROP_ZINDEX, PROP_ZSET,
  2257. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2258. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2259. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2260. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2261. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2262. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2263. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2264. PROP_COUNT
  2265. };
  2266. struct color_name {
  2267. const char *name;
  2268. unsigned short rgb;
  2269. };
  2270. static const struct color_name color_names[] = {
  2271. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2272. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2273. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2274. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2275. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2276. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2277. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2278. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2279. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2280. { "darkgrey", 0x528A }, { 0, 0 }
  2281. };
  2282. static int hexval(char c)
  2283. {
  2284. if (c >= '0' && c <= '9')
  2285. return c - '0';
  2286. if (c >= 'a' && c <= 'f')
  2287. return c - 'a' + 10;
  2288. if (c >= 'A' && c <= 'F')
  2289. return c - 'A' + 10;
  2290. return -1;
  2291. }
  2292. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2293. {
  2294. unsigned int i;
  2295. for (i = 0; color_names[i].name; i++) {
  2296. unsigned int j, n = s_len(color_names[i].name);
  2297. if (n != len)
  2298. continue;
  2299. for (j = 0; j < n; j++) {
  2300. char c = s[j];
  2301. if (c >= 'A' && c <= 'Z')
  2302. c = (char)(c - 'A' + 'a');
  2303. if (c != color_names[i].name[j])
  2304. break;
  2305. }
  2306. if (j == n) {
  2307. *out = color_names[i].rgb;
  2308. return 1;
  2309. }
  2310. }
  2311. return 0;
  2312. }
  2313. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2314. * over white; the render target is a white page. */
  2315. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2316. {
  2317. unsigned int i = 0;
  2318. while (i < len && is_space(s[i]))
  2319. i++;
  2320. if (i < len && s[i] == '#') {
  2321. int d[6], nd = 0;
  2322. i++;
  2323. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2324. d[nd++] = hexval(s[i++]);
  2325. if (nd == 3) {
  2326. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2327. unsigned int b = (unsigned int)d[2];
  2328. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2329. ((g * 17 >> 2) << 5) |
  2330. (b * 17 >> 3));
  2331. return 1;
  2332. }
  2333. if (nd >= 6) {
  2334. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2335. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2336. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2337. *out = (unsigned short)(((r >> 3) << 11) |
  2338. ((g >> 2) << 5) | (b >> 3));
  2339. return 1;
  2340. }
  2341. return 0;
  2342. }
  2343. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2344. unsigned int v[3], k = 0, a = 255;
  2345. while (i < len && s[i] != '(')
  2346. i++;
  2347. if (i >= len)
  2348. return 0;
  2349. i++;
  2350. while (k < 3 && i < len) {
  2351. unsigned int x = 0;
  2352. int percent = 0;
  2353. while (i < len && (is_space(s[i]) || s[i] == ','))
  2354. i++;
  2355. if (i >= len || s[i] < '0' || s[i] > '9')
  2356. return 0;
  2357. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2358. x = x * 10 + (unsigned int)(s[i] - '0');
  2359. i++;
  2360. }
  2361. if (i < len && s[i] == '%') {
  2362. percent = 1;
  2363. i++;
  2364. }
  2365. if (percent)
  2366. x = x * 255 / 100;
  2367. if (x > 255)
  2368. x = 255;
  2369. v[k++] = x;
  2370. }
  2371. /* Optional alpha in both syntaxes:
  2372. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2373. {
  2374. unsigned int whole = 0, frac = 0, scale = 1;
  2375. int percent = 0;
  2376. while (i < len && (is_space(s[i]) || s[i] == ','))
  2377. i++;
  2378. if (i < len && s[i] == '/') {
  2379. i++;
  2380. while (i < len && is_space(s[i]))
  2381. i++;
  2382. }
  2383. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2384. * used to read as opaque black. */
  2385. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2386. (s[i] == '.' && i + 1 < len &&
  2387. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2388. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2389. whole = whole * 10 +
  2390. (unsigned int)(s[i] - '0');
  2391. i++;
  2392. }
  2393. if (i < len && s[i] == '.') {
  2394. i++;
  2395. while (i < len && s[i] >= '0' &&
  2396. s[i] <= '9') {
  2397. if (scale < 1000) {
  2398. frac = frac * 10 +
  2399. (unsigned int)(s[i] - '0');
  2400. scale *= 10;
  2401. }
  2402. i++;
  2403. }
  2404. }
  2405. while (i < len && is_space(s[i]))
  2406. i++;
  2407. if (i < len && s[i] == '%')
  2408. percent = 1;
  2409. if (percent)
  2410. a = whole > 100 ? 255 :
  2411. whole * 255 / 100;
  2412. else if (whole >= 1)
  2413. a = 255;
  2414. else
  2415. a = frac * 255 / scale;
  2416. }
  2417. }
  2418. if (k == 3) {
  2419. unsigned int r = v[0], g = v[1], b = v[2];
  2420. if (a < 255) {
  2421. r = (r * a + 255u * (255u - a)) / 255u;
  2422. g = (g * a + 255u * (255u - a)) / 255u;
  2423. b = (b * a + 255u * (255u - a)) / 255u;
  2424. }
  2425. *out = (unsigned short)(((r >> 3) << 11) |
  2426. ((g >> 2) << 5) | (b >> 3));
  2427. return 1;
  2428. }
  2429. return 0;
  2430. }
  2431. {
  2432. unsigned int j = i;
  2433. while (i < len && !is_space(s[i]))
  2434. i++;
  2435. return named_color(s + j, i - j, out);
  2436. }
  2437. }
  2438. /* Fully transparent colours have no RGB565 representation. They are common
  2439. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2440. * only on hover; painting them black would draw an outline around every
  2441. * pill and card. */
  2442. static int color_is_transparent(const char *s, unsigned int len)
  2443. {
  2444. unsigned int i = 0, n = 0;
  2445. int last = 0, prev = 0, d;
  2446. while (i < len && is_space(s[i]))
  2447. i++;
  2448. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2449. return 1;
  2450. if (i >= len || s[i] != '#')
  2451. return 0;
  2452. i++;
  2453. while (i < len && hexval(s[i]) >= 0) {
  2454. d = hexval(s[i]);
  2455. prev = last;
  2456. last = d;
  2457. n++;
  2458. i++;
  2459. }
  2460. while (i < len && is_space(s[i]))
  2461. i++;
  2462. if (i != len)
  2463. return 0;
  2464. if (n == 4)
  2465. return last == 0;
  2466. if (n == 8)
  2467. return prev == 0 && last == 0;
  2468. return 0;
  2469. }
  2470. static int register_bg_url(const char *s, unsigned int len)
  2471. {
  2472. char tmp[BR_BG_URL_MAX + 1];
  2473. unsigned int i, n = 0;
  2474. char q = 0;
  2475. while (len && is_space(*s)) {
  2476. s++;
  2477. len--;
  2478. }
  2479. while (len && is_space(s[len - 1]))
  2480. len--;
  2481. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2482. s[len - 1] == s[0]) {
  2483. q = s[0];
  2484. s++;
  2485. len -= 2;
  2486. }
  2487. /* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
  2488. for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
  2489. if (q && s[i] == '\\' && i + 1 < len) {
  2490. i++;
  2491. if (s[i] == '\n')
  2492. continue;
  2493. }
  2494. tmp[n++] = s[i];
  2495. }
  2496. tmp[n] = '\0';
  2497. if (!n)
  2498. return 0;
  2499. for (i = 0; i < bg_url_count; i++)
  2500. if (!strcmp(bg_urls[i], tmp))
  2501. return (int)i + 1;
  2502. if (bg_url_count >= BR_BG_URLS ||
  2503. bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
  2504. return 0;
  2505. memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
  2506. bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
  2507. bg_url_pool_used += n + 1;
  2508. return (int)(++bg_url_count);
  2509. }
  2510. /* Length values for margins/padding: px, ignore percentages/units. */
  2511. static unsigned int viewport_w;
  2512. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2513. {
  2514. unsigned int i = *pos;
  2515. int sign = 1;
  2516. long v = 0; /* hundredths of a unit before conversion */
  2517. int digits = 0, frac = 0, unit = 0;
  2518. while (i < len && is_space(s[i]))
  2519. i++;
  2520. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2521. if (s[i] == '-')
  2522. sign = -1;
  2523. i++;
  2524. }
  2525. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2526. v = v * 10 + (s[i] - '0');
  2527. digits = 1;
  2528. i++;
  2529. }
  2530. if (i < len && s[i] == '.') {
  2531. i++;
  2532. /* Minified CSS writes ".5rem" without a leading zero. */
  2533. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2534. if (frac < 2) {
  2535. v = v * 10 + (s[i] - '0');
  2536. frac++;
  2537. digits = 1;
  2538. }
  2539. i++;
  2540. }
  2541. }
  2542. while (frac < 2) {
  2543. v *= 10;
  2544. frac++;
  2545. }
  2546. while (frac > 2) {
  2547. v /= 10;
  2548. frac--;
  2549. }
  2550. if (i < len && s[i] == '%')
  2551. unit = 2;
  2552. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2553. s[i + 2] == 'm')
  2554. unit = 1;
  2555. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2556. unit = 3;
  2557. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2558. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2559. s[i] == 'a' || s[i] == 'd'))
  2560. i++;
  2561. *pos = i;
  2562. if (!digits)
  2563. return 0;
  2564. if (unit == 1)
  2565. v *= root_font_px;
  2566. else if (unit == 3)
  2567. v *= root_font_px;
  2568. else if (unit == 2)
  2569. v = v * (long)viewport_w / 100;
  2570. v /= 100;
  2571. v *= sign;
  2572. if (v < -2048)
  2573. v = -2048;
  2574. if (v > 2048)
  2575. v = 2048;
  2576. return (int)v;
  2577. }
  2578. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2579. * treats it as a zero-length token; margins need to keep it so block layout
  2580. * can centre a box with a definite width. */
  2581. #define CSS_LEN_AUTO (-32768)
  2582. /* A box length with its unit preserved for layout-time resolution:
  2583. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2584. * rem and whole pixels for px. */
  2585. static int parse_css_len(const char *s, unsigned int len,
  2586. unsigned int *pos, int *unit)
  2587. {
  2588. unsigned int i = *pos;
  2589. int sign = 1;
  2590. long v = 0;
  2591. int digits = 0, frac = 0;
  2592. *unit = 0;
  2593. while (i < len && is_space(s[i]))
  2594. i++;
  2595. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2596. if (s[i] == '-')
  2597. sign = -1;
  2598. i++;
  2599. }
  2600. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2601. v = v * 10 + (s[i] - '0');
  2602. digits = 1;
  2603. i++;
  2604. }
  2605. if (i < len && s[i] == '.') {
  2606. i++;
  2607. /* Leading-dot fractions are common in minified CSS
  2608. * (".5rem", ".3rem"). */
  2609. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2610. if (frac < 2) {
  2611. v = v * 10 + (s[i] - '0');
  2612. frac++;
  2613. digits = 1;
  2614. }
  2615. i++;
  2616. }
  2617. }
  2618. while (frac < 2) {
  2619. v *= 10;
  2620. frac++;
  2621. }
  2622. if (!digits) {
  2623. *pos = i;
  2624. return 0;
  2625. }
  2626. if (i < len && s[i] == '%') {
  2627. *unit = 1;
  2628. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2629. s[i + 2] == 'm') {
  2630. *unit = 3;
  2631. i += 3;
  2632. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2633. *unit = 2;
  2634. i += 2;
  2635. } else {
  2636. v /= 100;
  2637. }
  2638. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2639. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2640. s[i] == 'a' || s[i] == 'd'))
  2641. i++;
  2642. *pos = i;
  2643. v *= sign;
  2644. if (v < -32768)
  2645. v = -32768;
  2646. if (v > 32767)
  2647. v = 32767;
  2648. return (int)v;
  2649. }
  2650. static int parse_margin_len(const char *s, unsigned int len,
  2651. unsigned int *pos, int *unit)
  2652. {
  2653. unsigned int i = *pos;
  2654. while (i < len && is_space(s[i]))
  2655. i++;
  2656. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2657. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2658. *pos = i + 4;
  2659. *unit = 4;
  2660. return CSS_LEN_AUTO;
  2661. }
  2662. return parse_css_len(s, len, pos, unit);
  2663. }
  2664. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2665. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2666. static int parse_margin_calc(const char *s, unsigned int len,
  2667. unsigned int *pos, int *pct, int *px)
  2668. {
  2669. unsigned int i = *pos, j;
  2670. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2671. return 0;
  2672. i += 5;
  2673. j = i;
  2674. while (j < len && s[j] != ')')
  2675. j++;
  2676. if (j >= len)
  2677. return 0;
  2678. if (!parse_calc(s + i, j - i, pct, px))
  2679. return 0;
  2680. *pos = j + 1;
  2681. return 1;
  2682. }
  2683. /* calc(P% +/- Npx) for the common width/margin forms. */
  2684. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2685. {
  2686. unsigned int i = 0;
  2687. int sign = 1;
  2688. int have = 0;
  2689. *pct = 0;
  2690. *px = 0;
  2691. while (i < len) {
  2692. unsigned int j;
  2693. long whole = 0, frac = 0, scale = 1;
  2694. int digits = 0;
  2695. while (i < len && is_space(s[i]))
  2696. i++;
  2697. if (i >= len)
  2698. break;
  2699. if (s[i] == '+' || s[i] == '-') {
  2700. sign = s[i] == '-' ? -1 : 1;
  2701. i++;
  2702. continue;
  2703. }
  2704. if (s[i] < '0' || s[i] > '9') {
  2705. if (s[i] == '.') {
  2706. /* fall through to the number parser */
  2707. } else {
  2708. i++;
  2709. continue;
  2710. }
  2711. }
  2712. j = i;
  2713. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2714. whole = whole * 10 + (s[j] - '0');
  2715. digits = 1;
  2716. j++;
  2717. }
  2718. if (j < len && s[j] == '.') {
  2719. j++;
  2720. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2721. frac = frac * 10 + (s[j] - '0');
  2722. scale *= 10;
  2723. j++;
  2724. }
  2725. }
  2726. if (!digits) {
  2727. i = j + 1;
  2728. continue;
  2729. }
  2730. while (j < len && is_space(s[j]))
  2731. j++;
  2732. if (j < len && s[j] == '%') {
  2733. *pct = sign * (int)(whole + (scale > 1 ?
  2734. frac * 100 / scale : 0));
  2735. have = 1;
  2736. j++;
  2737. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2738. *px = sign * (int)whole;
  2739. have = 1;
  2740. j += 2;
  2741. }
  2742. i = j;
  2743. while (i < len && !is_space(s[i]))
  2744. i++;
  2745. sign = 1;
  2746. }
  2747. return have;
  2748. }
  2749. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2750. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2751. unsigned char *kind)
  2752. {
  2753. unsigned int i = 0;
  2754. unsigned long v = 0;
  2755. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2756. *val = 0;
  2757. *kind = 0;
  2758. while (i < len && is_space(s[i]))
  2759. i++;
  2760. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2761. return 0;
  2762. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2763. return 0;
  2764. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2765. v = v * 10 + (unsigned int)(s[i] - '0');
  2766. digits = 1;
  2767. i++;
  2768. }
  2769. if (i < len && s[i] == '.') {
  2770. i++;
  2771. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2772. if (frac < 2) {
  2773. v = v * 10 + (unsigned int)(s[i] - '0');
  2774. frac++;
  2775. digits = 1;
  2776. }
  2777. i++;
  2778. }
  2779. }
  2780. while (frac < 2) {
  2781. v *= 10;
  2782. frac++;
  2783. }
  2784. while (frac > 2) {
  2785. v /= 10;
  2786. frac--;
  2787. }
  2788. if (!digits)
  2789. return 0;
  2790. while (i < len && is_space(s[i]))
  2791. i++;
  2792. if (i < len && s[i] == '%')
  2793. pct = 1;
  2794. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2795. s[i + 2] == 'm')
  2796. rem = 1;
  2797. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2798. rem = 2;
  2799. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2800. vw = 1;
  2801. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2802. ; /* px: already the default scale */
  2803. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2804. bad_unit = 1;
  2805. if (bad_unit)
  2806. return 0;
  2807. if (rem == 1 || rem == 2) {
  2808. if (v > 65535)
  2809. v = 65535;
  2810. *val = (unsigned short)v;
  2811. *kind = rem == 1 ? 5 : 4;
  2812. return 1;
  2813. }
  2814. v /= 100;
  2815. if (vw)
  2816. v = v * viewport_w / 100;
  2817. if (pct) {
  2818. if (v > 100)
  2819. v = 100;
  2820. *kind = 2;
  2821. } else {
  2822. if (v > 2048)
  2823. v = 2048;
  2824. *kind = 1;
  2825. }
  2826. *val = (unsigned short)v;
  2827. return 1;
  2828. }
  2829. static int parse_font_px(const char *s, unsigned int len)
  2830. {
  2831. unsigned int i = 0;
  2832. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2833. int v1000, px;
  2834. int is_px = 1, is_rem = 0, is_pct = 0;
  2835. while (i < len && is_space(s[i]))
  2836. i++;
  2837. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2838. return 24;
  2839. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2840. return 32;
  2841. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2842. return 14;
  2843. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2844. whole = whole * 10 + (s[i] - '0');
  2845. digits = 1;
  2846. i++;
  2847. }
  2848. if (i < len && s[i] == '.') {
  2849. i++;
  2850. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2851. if (fscale < 1000) {
  2852. frac = frac * 10 + (s[i] - '0');
  2853. fscale *= 10;
  2854. }
  2855. digits = 1;
  2856. i++;
  2857. }
  2858. }
  2859. if (!digits)
  2860. return 0;
  2861. while (i < len && is_space(s[i]))
  2862. i++;
  2863. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2864. is_rem = 1;
  2865. is_px = 0;
  2866. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2867. is_rem = 1;
  2868. is_px = 0;
  2869. } else if (i < len && s[i] == '%') {
  2870. is_pct = 1;
  2871. is_px = 0;
  2872. }
  2873. v1000 = whole * 1000 + frac * 1000 / fscale;
  2874. if (is_rem)
  2875. px = v1000 * root_font_px / 1000;
  2876. else if (is_pct)
  2877. px = v1000 * root_font_px / 100000;
  2878. else if (is_px)
  2879. px = v1000 / 1000;
  2880. else
  2881. px = v1000 / 1000;
  2882. if (px < 8)
  2883. px = 8;
  2884. if (px > 72)
  2885. px = 72;
  2886. return px;
  2887. }
  2888. /* font-size keeping the unit so em/% resolve against the parent face. */
  2889. static int parse_font_size(const char *s, unsigned int len,
  2890. unsigned short *raw, unsigned char *unit)
  2891. {
  2892. unsigned int i = 0;
  2893. int whole = 0, frac = 0, scale = 1, digits = 0;
  2894. int v100;
  2895. *raw = 0;
  2896. *unit = 0;
  2897. while (i < len && is_space(s[i]))
  2898. i++;
  2899. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2900. *raw = 24;
  2901. *unit = 1;
  2902. return 1;
  2903. }
  2904. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2905. *raw = 32;
  2906. *unit = 1;
  2907. return 1;
  2908. }
  2909. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2910. *raw = 14;
  2911. *unit = 1;
  2912. return 1;
  2913. }
  2914. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2915. whole = whole * 10 + (s[i] - '0');
  2916. digits = 1;
  2917. i++;
  2918. }
  2919. if (i < len && s[i] == '.') {
  2920. i++;
  2921. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2922. frac = frac * 10 + (s[i] - '0');
  2923. scale *= 10;
  2924. i++;
  2925. }
  2926. }
  2927. if (!digits && !frac)
  2928. return 0;
  2929. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2930. while (i < len && is_space(s[i]))
  2931. i++;
  2932. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2933. *raw = (unsigned short)v100;
  2934. *unit = 3;
  2935. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2936. *raw = (unsigned short)v100;
  2937. *unit = 2;
  2938. } else if (i < len && s[i] == '%') {
  2939. /* Keep hundredths: 62.5% is a common root size and truncating
  2940. * it to 62% makes every rem 1px too small. */
  2941. *raw = (unsigned short)v100;
  2942. *unit = 4;
  2943. } else {
  2944. *raw = (unsigned short)(v100 / 100);
  2945. *unit = 1;
  2946. }
  2947. return 1;
  2948. }
  2949. static int parse_line_height(const char *s, unsigned int len,
  2950. unsigned short *px, unsigned char *pct)
  2951. {
  2952. unsigned int i = 0;
  2953. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2954. int v1000;
  2955. int is_px = 0, is_pct = 0;
  2956. *px = 0;
  2957. *pct = 0;
  2958. while (i < len && is_space(s[i]))
  2959. i++;
  2960. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2961. return 0;
  2962. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2963. whole = whole * 10 + (s[i] - '0');
  2964. digits = 1;
  2965. i++;
  2966. }
  2967. if (i < len && s[i] == '.') {
  2968. i++;
  2969. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2970. frac = frac * 10 + (s[i] - '0');
  2971. fscale *= 10;
  2972. digits = 1;
  2973. i++;
  2974. }
  2975. }
  2976. if (!digits)
  2977. return 0;
  2978. while (i < len && is_space(s[i]))
  2979. i++;
  2980. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2981. is_px = 1;
  2982. else if (i < len && s[i] == '%')
  2983. is_pct = 1;
  2984. v1000 = whole * 1000 + frac * 1000 / fscale;
  2985. /* rem is an absolute length (the root font is known by the time
  2986. * rules are parsed, as for font-size); em behaves like a multiplier. */
  2987. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2988. v1000 = v1000 * root_font_px;
  2989. is_px = 1;
  2990. }
  2991. if (is_px) {
  2992. int v = v1000 / 1000;
  2993. if (v < 8)
  2994. v = 8;
  2995. if (v > 160)
  2996. v = 160;
  2997. *px = (unsigned short)v;
  2998. } else {
  2999. int v = v1000 / 10; /* unitless multiplier -> percent */
  3000. if (is_pct)
  3001. v = v1000 / 1000;
  3002. if (v < 50)
  3003. v = 50;
  3004. if (v > 255) /* stored in an unsigned char */
  3005. v = 255;
  3006. *pct = (unsigned char)v;
  3007. }
  3008. return 1;
  3009. }
  3010. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  3011. {
  3012. unsigned int i, j, n = s_len(needle);
  3013. for (i = 0; i + n <= len; i++) {
  3014. for (j = 0; j < n; j++) {
  3015. char a = s[i + j];
  3016. if (a >= 'A' && a <= 'Z')
  3017. a = (char)(a - 'A' + 'a');
  3018. if (a != needle[j])
  3019. break;
  3020. }
  3021. if (j == n)
  3022. return 1;
  3023. }
  3024. return 0;
  3025. }
  3026. /* Pull a colour out of a border/background shorthand. */
  3027. /* Background shorthands can hold several layers before the final solid
  3028. * colour. The colour is conventionally the last token (e.g. a stack of
  3029. * gradients followed by var(--bg)), so take the rightmost colour rather
  3030. * than the first gradient stop. */
  3031. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  3032. {
  3033. int last = -1;
  3034. unsigned int i;
  3035. for (i = 0; i < len; i++) {
  3036. if (s[i] == '#')
  3037. last = (int)i;
  3038. else if ((s[i] == 'r' || s[i] == 'R') &&
  3039. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  3040. last = (int)i;
  3041. }
  3042. if (last >= 0)
  3043. return parse_color(s + last, len - (unsigned int)last, c);
  3044. while (i > 0) {
  3045. unsigned int st, e;
  3046. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  3047. s[i - 1] == ')' || s[i - 1] == '('))
  3048. i--;
  3049. e = i;
  3050. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3051. s[i - 1] != ')' && s[i - 1] != '(')
  3052. i--;
  3053. st = i;
  3054. if (e > st && parse_color(s + st, e - st, c))
  3055. return 1;
  3056. }
  3057. return 0;
  3058. }
  3059. static int parse_display(const char *s, unsigned int len)
  3060. {
  3061. unsigned int i;
  3062. while (len && is_space(s[0])) { s++; len--; }
  3063. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3064. ;
  3065. if (i == 4 && ci_eq_n(s, "none", 4))
  3066. return BR_DISPLAY_NONE;
  3067. if (i == 5 && ci_eq_n(s, "block", 5))
  3068. return BR_DISPLAY_BLOCK;
  3069. if (i == 6 && ci_eq_n(s, "inline", 6))
  3070. return BR_DISPLAY_INLINE;
  3071. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3072. return BR_DISPLAY_INLINE_BLOCK;
  3073. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3074. return BR_DISPLAY_BLOCK;
  3075. if (i == 4 && ci_eq_n(s, "flex", 4))
  3076. return BR_DISPLAY_FLEX;
  3077. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3078. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3079. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3080. * block instead of an inline box. */
  3081. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3082. return BR_DISPLAY_BLOCK;
  3083. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3084. return BR_DISPLAY_BLOCK;
  3085. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3086. return BR_DISPLAY_FLEX;
  3087. if (i == 5 && ci_eq_n(s, "table", 5))
  3088. return BR_DISPLAY_BLOCK;
  3089. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3090. return BR_DISPLAY_BLOCK;
  3091. /* Grid shells are common on news/wiki pages, but the two-column
  3092. * heuristic is not reliable for their nested area templates yet.
  3093. * Collapse them to block flow so the content stays readable. */
  3094. if (i == 4 && ci_eq_n(s, "grid", 4))
  3095. return BR_DISPLAY_BLOCK;
  3096. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3097. return BR_DISPLAY_BLOCK;
  3098. return BR_DISPLAY_INLINE;
  3099. }
  3100. static void decl_add(struct br_decl *out, unsigned int *nout,
  3101. unsigned char prop, int val, int important)
  3102. {
  3103. if (*nout >= BR_DECLS)
  3104. return;
  3105. out[*nout].prop = prop;
  3106. out[*nout].val = (short)val;
  3107. out[*nout].important = (unsigned char)(important != 0);
  3108. (*nout)++;
  3109. }
  3110. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3111. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3112. unsigned int *start, unsigned int *end)
  3113. {
  3114. unsigned int i = *pos;
  3115. while (i < len && is_space(s[i]))
  3116. i++;
  3117. if (i >= len)
  3118. return 0;
  3119. *start = i;
  3120. while (i < len && !is_space(s[i]))
  3121. i++;
  3122. *end = i;
  3123. *pos = i;
  3124. return 1;
  3125. }
  3126. static int bg_number(const char *s, unsigned int len, signed short *val,
  3127. unsigned char *kind)
  3128. {
  3129. unsigned int i = 0;
  3130. long v = 0;
  3131. int neg = 0, digits = 0, pct = 0;
  3132. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3133. neg = s[i] == '-';
  3134. i++;
  3135. }
  3136. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3137. v = v * 10 + (s[i] - '0');
  3138. digits = 1;
  3139. i++;
  3140. }
  3141. if (i < len && s[i] == '.') {
  3142. i++;
  3143. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3144. digits = 1;
  3145. i++;
  3146. }
  3147. }
  3148. if (!digits)
  3149. return 0;
  3150. if (i < len && s[i] == '%') {
  3151. pct = 1;
  3152. i++;
  3153. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3154. i += 2;
  3155. }
  3156. if (i != len)
  3157. return 0;
  3158. if (neg)
  3159. v = -v;
  3160. if (v < -4096)
  3161. v = -4096;
  3162. if (v > 4096)
  3163. v = 4096;
  3164. *val = (signed short)v;
  3165. *kind = (unsigned char)(pct ? 1 : 0);
  3166. return 1;
  3167. }
  3168. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3169. int *vertical)
  3170. {
  3171. *vertical = 0;
  3172. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3173. *val = 0;
  3174. return 1;
  3175. }
  3176. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3177. *val = 100;
  3178. return 1;
  3179. }
  3180. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3181. *val = 0;
  3182. *vertical = 1;
  3183. return 1;
  3184. }
  3185. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3186. *val = 100;
  3187. *vertical = 1;
  3188. return 1;
  3189. }
  3190. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3191. *val = 50;
  3192. return 1;
  3193. }
  3194. return 0;
  3195. }
  3196. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3197. const char *s, unsigned int len, int important)
  3198. {
  3199. unsigned int pos = 0, ts, te;
  3200. signed short val[2] = { 50, 50 };
  3201. unsigned char kind[2] = { 1, 1 };
  3202. int vert[2] = { 0, 0 }, n = 0;
  3203. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3204. signed short v;
  3205. int is_vert;
  3206. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3207. val[n] = v;
  3208. kind[n] = 1;
  3209. vert[n] = is_vert;
  3210. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3211. val[n] = v;
  3212. vert[n] = 0;
  3213. } else {
  3214. n++;
  3215. continue;
  3216. }
  3217. n++;
  3218. }
  3219. if (n == 1) {
  3220. if (vert[0]) {
  3221. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3222. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3223. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3224. important);
  3225. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3226. } else {
  3227. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3228. important);
  3229. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3230. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3231. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3232. }
  3233. return;
  3234. }
  3235. if (n >= 2) {
  3236. int x = vert[0] && !vert[1] ? 1 : 0;
  3237. int y = 1 - x;
  3238. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3239. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3240. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3241. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3242. }
  3243. }
  3244. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3245. const char *s, unsigned int len, int important)
  3246. {
  3247. unsigned int pos = 0, ts, te;
  3248. signed short w = 0, h = 0;
  3249. unsigned char kw = 0, kh = 0;
  3250. int n = 0;
  3251. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3252. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3253. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3254. return;
  3255. }
  3256. if (n == 0 && te - ts == 7 &&
  3257. ci_eq_n(s + ts, "contain", 7)) {
  3258. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3259. return;
  3260. }
  3261. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3262. n++;
  3263. continue;
  3264. }
  3265. {
  3266. signed short v;
  3267. unsigned char k;
  3268. if (bg_number(s + ts, te - ts, &v, &k)) {
  3269. if (n == 0) {
  3270. w = v;
  3271. kw = k;
  3272. } else {
  3273. h = v;
  3274. kh = k;
  3275. }
  3276. }
  3277. }
  3278. n++;
  3279. }
  3280. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3281. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3282. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3283. (void)kh;
  3284. }
  3285. /* Parse the `position / size` part of the background shorthand. The image
  3286. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3287. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3288. const char *v, unsigned int len,
  3289. int important)
  3290. {
  3291. unsigned int i, slash = 0, depth = 0, p, first, count;
  3292. char q = 0;
  3293. signed short nv;
  3294. int vert;
  3295. unsigned char kind;
  3296. for (i = 0; i < len; i++) {
  3297. char c = v[i];
  3298. if (q) {
  3299. if (c == q)
  3300. q = 0;
  3301. continue;
  3302. }
  3303. if (c == '\'' || c == '"') {
  3304. q = c;
  3305. continue;
  3306. }
  3307. if (c == '(') {
  3308. depth++;
  3309. continue;
  3310. }
  3311. if (c == ')') {
  3312. if (depth)
  3313. depth--;
  3314. continue;
  3315. }
  3316. if (c == '/' && !depth) {
  3317. slash = i;
  3318. break;
  3319. }
  3320. }
  3321. if (!slash)
  3322. return;
  3323. /* Up to two position tokens immediately before the slash. */
  3324. p = slash;
  3325. first = slash;
  3326. count = 0;
  3327. while (count < 2) {
  3328. unsigned int e;
  3329. while (p > 0 && is_space(v[p - 1]))
  3330. p--;
  3331. e = p;
  3332. while (p > 0 && !is_space(v[p - 1]))
  3333. p--;
  3334. if (e == p)
  3335. break;
  3336. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3337. bg_number(v + p, e - p, &nv, &kind)) {
  3338. first = p;
  3339. count++;
  3340. } else {
  3341. break;
  3342. }
  3343. }
  3344. if (count)
  3345. add_bg_position(out, nout, v + first, slash - first,
  3346. important);
  3347. /* Up to two size tokens after the slash. */
  3348. p = slash + 1;
  3349. while (p < len && is_space(v[p]))
  3350. p++;
  3351. first = p;
  3352. count = 0;
  3353. while (count < 2) {
  3354. unsigned int ts = p;
  3355. while (p < len && !is_space(v[p]))
  3356. p++;
  3357. if (p == ts)
  3358. break;
  3359. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3360. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3361. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3362. bg_number(v + ts, p - ts, &nv, &kind)) {
  3363. count++;
  3364. while (p < len && is_space(v[p]))
  3365. p++;
  3366. } else {
  3367. break;
  3368. }
  3369. }
  3370. if (count)
  3371. add_bg_size(out, nout, v + first, p - first, important);
  3372. }
  3373. static int parse_fixed_100(const char *s, unsigned int len,
  3374. unsigned int *pos, signed short *out)
  3375. {
  3376. unsigned int i = *pos;
  3377. long whole = 0, frac = 0;
  3378. int neg = 0, digits = 0, fd = 0;
  3379. while (i < len && is_space(s[i]))
  3380. i++;
  3381. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3382. neg = s[i] == '-';
  3383. i++;
  3384. }
  3385. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3386. whole = whole * 10 + (s[i] - '0');
  3387. if (whole > 327)
  3388. whole = 327;
  3389. digits = 1;
  3390. i++;
  3391. }
  3392. if (i < len && s[i] == '.') {
  3393. i++;
  3394. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3395. if (fd < 2) {
  3396. frac = frac * 10 + (s[i] - '0');
  3397. fd++;
  3398. digits = 1;
  3399. }
  3400. i++;
  3401. }
  3402. }
  3403. if (!digits)
  3404. return 0;
  3405. while (fd++ < 2)
  3406. frac *= 10;
  3407. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3408. *pos = i;
  3409. return 1;
  3410. }
  3411. static int find_fn(const char *v, unsigned int len, const char *name,
  3412. unsigned int *arg)
  3413. {
  3414. unsigned int n = s_len(name), i;
  3415. for (i = 0; i + n <= len; i++) {
  3416. unsigned int j;
  3417. if (!ci_eq_n(v + i, name, n))
  3418. continue;
  3419. j = i + n;
  3420. while (j < len && is_space(v[j]))
  3421. j++;
  3422. if (j < len && v[j] == '(') {
  3423. *arg = j + 1;
  3424. return 1;
  3425. }
  3426. }
  3427. return 0;
  3428. }
  3429. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3430. const char *s, unsigned int len,
  3431. int important)
  3432. {
  3433. unsigned int pos = 0, ts, te;
  3434. signed short val[2] = { 50, 50 };
  3435. unsigned char kind[2] = { 1, 1 };
  3436. int vert[2] = { 0, 0 }, n = 0;
  3437. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3438. signed short v;
  3439. int is_vert;
  3440. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3441. val[n] = v;
  3442. kind[n] = 1;
  3443. vert[n] = is_vert;
  3444. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3445. val[n] = v;
  3446. vert[n] = 0;
  3447. } else {
  3448. n++;
  3449. continue;
  3450. }
  3451. n++;
  3452. }
  3453. if (n == 1) {
  3454. if (vert[0]) {
  3455. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3456. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3457. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3458. important);
  3459. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3460. } else {
  3461. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3462. important);
  3463. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3464. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3465. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3466. }
  3467. return;
  3468. }
  3469. if (n >= 2) {
  3470. int x = vert[0] && !vert[1] ? 1 : 0;
  3471. int y = 1 - x;
  3472. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3473. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3474. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3475. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3476. }
  3477. }
  3478. /* Copy a background value with every url(...) body blanked (kept as
  3479. * `url()`), for the keyword and colour scans of the shorthand. */
  3480. static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
  3481. unsigned int cap)
  3482. {
  3483. unsigned int i = 0, n = 0;
  3484. while (i < len && n + 1 < cap) {
  3485. if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
  3486. char q = 0;
  3487. memcpy(out + n, "url(", 4);
  3488. n += 4;
  3489. i += 4;
  3490. while (i < len) {
  3491. if (q) {
  3492. if (s[i] == '\\' && i + 1 < len)
  3493. i++;
  3494. else if (s[i] == q)
  3495. q = 0;
  3496. } else if (s[i] == '"' || s[i] == '\'') {
  3497. q = s[i];
  3498. } else if (s[i] == ')') {
  3499. break;
  3500. }
  3501. i++;
  3502. }
  3503. continue; /* the ')' is copied below */
  3504. }
  3505. out[n++] = s[i++];
  3506. }
  3507. out[n] = '\0';
  3508. return n;
  3509. }
  3510. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3511. const char *v, unsigned int vl, int important)
  3512. {
  3513. unsigned int i;
  3514. for (i = 0; i + 4 <= vl; i++) {
  3515. if (ci_eq_n(v + i, "url(", 4)) {
  3516. unsigned int st = i + 4, en = st;
  3517. char q = 0;
  3518. int idx;
  3519. /* `)` inside a quoted data: URL (an SVG's
  3520. * rgba(...)) does not end it. */
  3521. while (en < vl) {
  3522. if (q) {
  3523. if (v[en] == '\\' && en + 1 < vl)
  3524. en++;
  3525. else if (v[en] == q)
  3526. q = 0;
  3527. } else if (v[en] == '"' || v[en] == '\'') {
  3528. q = v[en];
  3529. } else if (v[en] == ')') {
  3530. break;
  3531. }
  3532. en++;
  3533. }
  3534. idx = register_bg_url(v + st, en - st);
  3535. if (idx)
  3536. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3537. important);
  3538. return;
  3539. }
  3540. }
  3541. }
  3542. static int parse_len_token(const char *s, unsigned int len, int *out)
  3543. {
  3544. unsigned int pos = 0;
  3545. int v;
  3546. if (!len)
  3547. return 0;
  3548. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3549. return 0;
  3550. v = parse_len(s, len, &pos);
  3551. if (pos == 0)
  3552. return 0;
  3553. *out = v;
  3554. return 1;
  3555. }
  3556. static void expand_four(int v[4], int n)
  3557. {
  3558. if (n == 1) {
  3559. v[1] = v[2] = v[3] = v[0];
  3560. } else if (n == 2) {
  3561. v[2] = v[0];
  3562. v[3] = v[1];
  3563. } else if (n == 3) {
  3564. v[3] = v[1];
  3565. }
  3566. }
  3567. /* Custom properties defined in the declaration block being expanded. A
  3568. * component that sets a variable and uses it in the same rule (Infima's
  3569. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3570. * var(--ifm-col-width) }`) means its own value: the element matching the
  3571. * rule gets it. The document-wide table cannot know that, because
  3572. * `.col` and `.col--N` give the name a dozen different values. */
  3573. static int css_env_active; /* css_local holds an element's variables */
  3574. #define CSS_LOCAL_VARS 48
  3575. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3576. unsigned int i);
  3577. static struct {
  3578. const char *name, *val;
  3579. unsigned short nlen, vlen;
  3580. } css_local[CSS_LOCAL_VARS];
  3581. static unsigned int css_nlocal;
  3582. static int css_local_find(const char *name, unsigned int nlen)
  3583. {
  3584. unsigned int k;
  3585. for (k = 0; k < css_nlocal; k++)
  3586. if (css_local[k].nlen == nlen &&
  3587. !memcmp(css_local[k].name, name, nlen))
  3588. return (int)k;
  3589. return -1;
  3590. }
  3591. static int css_local_find_before(const char *name, unsigned int nlen,
  3592. unsigned int end)
  3593. {
  3594. unsigned int k;
  3595. for (k = 0; k < end; k++)
  3596. if (css_local[k].nlen == nlen &&
  3597. !memcmp(css_local[k].name, name, nlen))
  3598. return (int)k;
  3599. return -1;
  3600. }
  3601. static void css_local_append(const char *s, unsigned int len);
  3602. /* Collect the top-level `--name: value` declarations of a block. */
  3603. static void css_local_collect(const char *s, unsigned int len)
  3604. {
  3605. css_nlocal = 0;
  3606. css_local_append(s, len);
  3607. }
  3608. static void css_local_append(const char *s, unsigned int len)
  3609. {
  3610. unsigned int i = 0;
  3611. while (i < len) {
  3612. unsigned int ns, ne, vs, ve;
  3613. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3614. i++;
  3615. ns = i;
  3616. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3617. i++;
  3618. if (i >= len || s[i] != ':') {
  3619. if (i < len && s[i] == '{')
  3620. i = css_skip_nested(s, len, i + 1);
  3621. continue;
  3622. }
  3623. ne = i;
  3624. while (ne > ns && is_space(s[ne - 1]))
  3625. ne--;
  3626. vs = i + 1;
  3627. ve = css_value_end(s, len, vs);
  3628. i = ve;
  3629. while (vs < ve && is_space(s[vs]))
  3630. vs++;
  3631. while (ve > vs && is_space(s[ve - 1]))
  3632. ve--;
  3633. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3634. ve -= 10;
  3635. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3636. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3637. continue;
  3638. css_local[css_nlocal].name = s + ns;
  3639. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3640. css_local[css_nlocal].val = s + vs;
  3641. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3642. css_nlocal++;
  3643. }
  3644. }
  3645. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3646. * the input is copied verbatim when it contains no var(). */
  3647. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3648. unsigned int len, char *out,
  3649. unsigned int cap, int depth)
  3650. {
  3651. unsigned int i = 0, n = 0;
  3652. if (depth > 8)
  3653. return 0;
  3654. while (i < len && n + 1 < cap) {
  3655. unsigned int p, inner, close, comma, nlen, fstart;
  3656. char quote;
  3657. int parens, glue;
  3658. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3659. out[n++] = s[i++];
  3660. continue;
  3661. }
  3662. p = i + 4;
  3663. quote = 0;
  3664. parens = 1;
  3665. while (p < len && parens) {
  3666. char c = s[p];
  3667. if (quote) {
  3668. if (c == quote)
  3669. quote = 0;
  3670. else if (c == '\\' && p + 1 < len)
  3671. p++;
  3672. } else if (c == '"' || c == '\'') {
  3673. quote = c;
  3674. } else if (c == '(') {
  3675. parens++;
  3676. } else if (c == ')') {
  3677. parens--;
  3678. }
  3679. p++;
  3680. }
  3681. if (parens) {
  3682. out[n++] = s[i++];
  3683. continue;
  3684. }
  3685. close = p - 1;
  3686. inner = i + 4;
  3687. /* var() substitutes tokens: minified Infima writes
  3688. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3689. * must not become the one token `700.875rem`. Separate the
  3690. * value from a glued neighbour on both sides. */
  3691. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3692. out[n - 1] != '(' && out[n - 1] != ',' &&
  3693. out[n - 1] != ':' && out[n - 1] != '/')
  3694. out[n++] = ' ';
  3695. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3696. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3697. s[p] != '!' && s[p] != '}';
  3698. while (inner < close && is_space(s[inner]))
  3699. inner++;
  3700. comma = inner;
  3701. parens = 0;
  3702. quote = 0;
  3703. while (comma < close) {
  3704. char c = s[comma];
  3705. if (quote) {
  3706. if (c == quote)
  3707. quote = 0;
  3708. } else if (c == '"' || c == '\'') {
  3709. quote = c;
  3710. } else if (c == '(') {
  3711. parens++;
  3712. } else if (c == ')' && parens > 0) {
  3713. parens--;
  3714. } else if (c == ',' && parens == 0) {
  3715. break;
  3716. }
  3717. comma++;
  3718. }
  3719. nlen = comma - inner;
  3720. while (nlen && is_space(s[inner + nlen - 1]))
  3721. nlen--;
  3722. fstart = comma < close ? comma + 1 : close;
  3723. while (fstart < close && is_space(s[fstart]))
  3724. fstart++;
  3725. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3726. unsigned int vi = css_var_find(d, s + inner, nlen);
  3727. int li = css_local_find(s + inner, nlen);
  3728. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3729. s + close + 1) {
  3730. /* (not the definition's own value) */
  3731. n += css_expand_vars(d, css_local[li].val,
  3732. css_local[li].vlen,
  3733. out + n, cap - n,
  3734. depth + 1);
  3735. i = p;
  3736. if (glue && n + 1 < cap)
  3737. out[n++] = ' ';
  3738. continue;
  3739. }
  3740. if (vi < BR_MAX_VARS &&
  3741. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3742. vi = BR_MAX_VARS;
  3743. if (vi < BR_MAX_VARS) {
  3744. const char *v = attr_ptr(d, d->var_val[vi]);
  3745. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3746. out + n,
  3747. cap - n, depth + 1);
  3748. n += vn;
  3749. } else if (fstart < close) {
  3750. unsigned int vn = css_expand_vars(d, s + fstart,
  3751. close - fstart,
  3752. out + n, cap - n,
  3753. depth + 1);
  3754. n += vn;
  3755. }
  3756. } else if (fstart < close) {
  3757. unsigned int vn = css_expand_vars(d, s + fstart,
  3758. close - fstart,
  3759. out + n, cap - n,
  3760. depth + 1);
  3761. n += vn;
  3762. }
  3763. i = p;
  3764. if (glue && n + 1 < cap)
  3765. out[n++] = ' ';
  3766. }
  3767. out[n] = '\0';
  3768. return n;
  3769. }
  3770. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3771. unsigned int i)
  3772. {
  3773. while (i < len && (is_space(s[i]) || s[i] == ','))
  3774. i++;
  3775. return i;
  3776. }
  3777. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3778. * argument). Returns 1 on success. */
  3779. static int parse_grid_track(const char *s, unsigned int len,
  3780. unsigned int *pp, unsigned short *val,
  3781. unsigned char *kind)
  3782. {
  3783. unsigned int i = grid_skip_sep(s, len, *pp);
  3784. unsigned int whole = 0, frac = 0, scale = 1;
  3785. int any = 0;
  3786. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3787. unsigned int j = i + 7;
  3788. while (j < len && s[j] != ',')
  3789. j++;
  3790. if (j >= len)
  3791. return 0;
  3792. i = j + 1;
  3793. while (i < len && is_space(s[i]))
  3794. i++;
  3795. }
  3796. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3797. int depth = 1;
  3798. i += 7;
  3799. while (i < len && depth) {
  3800. if (s[i] == '(')
  3801. depth++;
  3802. else if (s[i] == ')')
  3803. depth--;
  3804. i++;
  3805. }
  3806. *pp = i;
  3807. return 0;
  3808. }
  3809. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3810. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3811. i++;
  3812. any = 1;
  3813. }
  3814. if (i < len && s[i] == '.') {
  3815. i++;
  3816. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3817. if (scale < 1000) {
  3818. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3819. scale *= 10;
  3820. }
  3821. i++;
  3822. any = 1;
  3823. }
  3824. }
  3825. if (!any) {
  3826. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3827. *val = 1;
  3828. *kind = 3;
  3829. *pp = i + 4;
  3830. return 1;
  3831. }
  3832. return 0;
  3833. }
  3834. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3835. *val = (unsigned short)(whole ? whole : 1);
  3836. *kind = 3;
  3837. i += 2;
  3838. } else if (i < len && s[i] == '%') {
  3839. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3840. *kind = 2;
  3841. i++;
  3842. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3843. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3844. *kind = 1;
  3845. i += 3;
  3846. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3847. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3848. *kind = 1;
  3849. i += 2;
  3850. } else {
  3851. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3852. *kind = 1;
  3853. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3854. i += 2;
  3855. }
  3856. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3857. i++;
  3858. *pp = i;
  3859. return 1;
  3860. }
  3861. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3862. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3863. * keep the two-column path). */
  3864. static int grid_repeat_count(const char *s, unsigned int len)
  3865. {
  3866. unsigned int i = 0;
  3867. int n = 0;
  3868. while (i < len && is_space(s[i]))
  3869. i++;
  3870. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3871. i += 7;
  3872. while (i < len && is_space(s[i]))
  3873. i++;
  3874. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3875. n = n * 10 + (s[i] - '0');
  3876. i++;
  3877. }
  3878. if (n < 1 || n > 12)
  3879. return 0;
  3880. while (i < len && (is_space(s[i]) || s[i] == ','))
  3881. i++;
  3882. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3883. return n;
  3884. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3885. i += 7;
  3886. while (i < len && is_space(s[i]))
  3887. i++;
  3888. if (i < len && s[i] == '0') {
  3889. i++;
  3890. while (i < len && (is_space(s[i]) ||
  3891. s[i] == ',' || s[i] == 'p' ||
  3892. s[i] == 'x'))
  3893. i++;
  3894. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3895. return n;
  3896. }
  3897. }
  3898. return 0;
  3899. }
  3900. /* "1fr 1fr 1fr" */
  3901. for (;;) {
  3902. while (i < len && is_space(s[i]))
  3903. i++;
  3904. if (i >= len)
  3905. break;
  3906. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3907. (i + 3 == len || is_space(s[i + 3]))) {
  3908. n++;
  3909. i += 3;
  3910. } else {
  3911. return 0;
  3912. }
  3913. }
  3914. return n >= 3 ? n : 0;
  3915. }
  3916. static int parse_grid_cols(const char *s, unsigned int len,
  3917. unsigned short val[2], unsigned char kind[2])
  3918. {
  3919. unsigned int pos = 0;
  3920. int n = 0;
  3921. while (n < 2) {
  3922. unsigned int before = pos;
  3923. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3924. break;
  3925. n++;
  3926. if (pos <= before)
  3927. break;
  3928. while (pos < len && is_space(s[pos]))
  3929. pos++;
  3930. if (pos >= len)
  3931. break;
  3932. }
  3933. return n;
  3934. }
  3935. static int parse_grid_slot(const char *s, unsigned int len)
  3936. {
  3937. if (contains_ci_len(s, len, "footer") ||
  3938. contains_ci_len(s, len, "header") ||
  3939. contains_ci_len(s, len, "sitenotice") ||
  3940. contains_ci_len(s, len, "navbar"))
  3941. return 1;
  3942. if (contains_ci_len(s, len, "columnstart") ||
  3943. contains_ci_len(s, len, "sidebar") ||
  3944. contains_ci_len(s, len, "left") ||
  3945. contains_ci_len(s, len, "start"))
  3946. return 2;
  3947. if (contains_ci_len(s, len, "pagecontent") ||
  3948. contains_ci_len(s, len, "content") ||
  3949. contains_ci_len(s, len, "main") ||
  3950. contains_ci_len(s, len, "right") ||
  3951. contains_ci_len(s, len, "end"))
  3952. return 3;
  3953. return 0;
  3954. }
  3955. static int css_has_var(const char *s, unsigned int len)
  3956. {
  3957. unsigned int i;
  3958. for (i = 0; i + 4 <= len; i++)
  3959. if ((s[i] == 'v' || s[i] == 'V') &&
  3960. ci_eq_n(s + i, "var(", 4))
  3961. return 1;
  3962. return 0;
  3963. }
  3964. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3965. unsigned int i)
  3966. {
  3967. i += 2;
  3968. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3969. i++;
  3970. if (i + 1 < len)
  3971. i += 2;
  3972. else
  3973. i = len;
  3974. return i;
  3975. }
  3976. static unsigned int css_skip_string(const char *s, unsigned int len,
  3977. unsigned int i)
  3978. {
  3979. char q = s[i++];
  3980. while (i < len && s[i] != q) {
  3981. if (s[i] == '\\' && i + 1 < len)
  3982. i++;
  3983. i++;
  3984. }
  3985. if (i < len)
  3986. i++;
  3987. return i;
  3988. }
  3989. /* Find the '}' that closes a declaration block. i points just past the
  3990. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3991. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3992. * selectors such as "Highlight-Stories {" without a closing brace). */
  3993. static unsigned int css_block_end(const char *s, unsigned int len,
  3994. unsigned int i)
  3995. {
  3996. int depth = 0;
  3997. while (i < len) {
  3998. char c = s[i];
  3999. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4000. i = css_skip_comment(s, len, i);
  4001. continue;
  4002. }
  4003. if (c == '"' || c == '\'') {
  4004. i = css_skip_string(s, len, i);
  4005. continue;
  4006. }
  4007. if (c == '{') {
  4008. depth++;
  4009. } else if (c == '}') {
  4010. if (depth == 0)
  4011. return i;
  4012. depth--;
  4013. }
  4014. i++;
  4015. }
  4016. return len;
  4017. }
  4018. /* Skip a block whose opening '{' has already been consumed. */
  4019. static unsigned int css_skip_nested(const char *s, unsigned int len,
  4020. unsigned int i)
  4021. {
  4022. int depth = 1;
  4023. while (i < len && depth) {
  4024. char c = s[i];
  4025. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  4026. i = css_skip_comment(s, len, i);
  4027. continue;
  4028. }
  4029. if (c == '"' || c == '\'') {
  4030. i = css_skip_string(s, len, i);
  4031. continue;
  4032. }
  4033. if (c == '{')
  4034. depth++;
  4035. else if (c == '}')
  4036. depth--;
  4037. i++;
  4038. }
  4039. return i;
  4040. }
  4041. /* ---- @font-face ------------------------------------------------------- */
  4042. static void font_trim(const char **sp, unsigned int *len)
  4043. {
  4044. const char *s = *sp;
  4045. unsigned int n = *len;
  4046. while (n && is_space(*s)) {
  4047. s++;
  4048. n--;
  4049. }
  4050. while (n && is_space(s[n - 1]))
  4051. n--;
  4052. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  4053. s++;
  4054. n -= 2;
  4055. }
  4056. *sp = s;
  4057. *len = n;
  4058. }
  4059. static void font_add(const char *family, unsigned int flen,
  4060. const char *url, unsigned int ulen)
  4061. {
  4062. unsigned int i, n = 0, k;
  4063. font_trim(&family, &flen);
  4064. font_trim(&url, &ulen);
  4065. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  4066. return;
  4067. for (i = 0; i < (unsigned int)font_face_count; i++) {
  4068. struct br_font_def *f = &font_defs[i];
  4069. if (s_len(f->family) != flen)
  4070. continue;
  4071. for (k = 0; k < flen; k++) {
  4072. char a = f->family[k], b = family[k];
  4073. if (a >= 'A' && a <= 'Z')
  4074. a = (char)(a - 'A' + 'a');
  4075. if (b >= 'A' && b <= 'Z')
  4076. b = (char)(b - 'A' + 'a');
  4077. if (a != b)
  4078. break;
  4079. }
  4080. if (k != flen)
  4081. continue;
  4082. n = 0;
  4083. while (url[n] && n + 1 < sizeof(f->url)) {
  4084. f->url[n] = url[n];
  4085. n++;
  4086. }
  4087. f->url[n] = '\0';
  4088. n = 0;
  4089. while (font_base[n] && n + 1 < sizeof(f->base)) {
  4090. f->base[n] = font_base[n];
  4091. n++;
  4092. }
  4093. f->base[n] = '\0';
  4094. return;
  4095. }
  4096. if (font_face_count >= BR_MAX_FACES)
  4097. return;
  4098. {
  4099. struct br_font_def *f = &font_defs[font_face_count];
  4100. for (n = 0; n < flen; n++)
  4101. f->family[n] = family[n];
  4102. f->family[n] = '\0';
  4103. for (n = 0; n < ulen; n++)
  4104. f->url[n] = url[n];
  4105. f->url[n] = '\0';
  4106. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  4107. f->base[n] = font_base[n];
  4108. f->base[n] = '\0';
  4109. f->used = 1;
  4110. font_face_count++;
  4111. }
  4112. }
  4113. /* Look up the first name in a font-family list that has an @font-face. */
  4114. static int font_family_id(const char *s, unsigned int len)
  4115. {
  4116. unsigned int i = 0;
  4117. while (i < len) {
  4118. unsigned int start, end, q = 0;
  4119. while (i < len && (is_space(s[i]) || s[i] == ','))
  4120. i++;
  4121. if (i >= len)
  4122. break;
  4123. if (s[i] == '"' || s[i] == '\'') {
  4124. char quote = s[i++];
  4125. start = i;
  4126. while (i < len && s[i] != quote)
  4127. i++;
  4128. end = i;
  4129. if (i < len)
  4130. i++;
  4131. } else {
  4132. start = i;
  4133. while (i < len && s[i] != ',')
  4134. i++;
  4135. end = i;
  4136. while (end > start && is_space(s[end - 1]))
  4137. end--;
  4138. }
  4139. if (end > start) {
  4140. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4141. const char *f = font_defs[q].family;
  4142. unsigned int fl = s_len(f), k;
  4143. if (fl != end - start)
  4144. continue;
  4145. for (k = 0; k < fl; k++) {
  4146. char a = f[k], b = s[start + k];
  4147. if (a >= 'A' && a <= 'Z')
  4148. a = (char)(a - 'A' + 'a');
  4149. if (b >= 'A' && b <= 'Z')
  4150. b = (char)(b - 'A' + 'a');
  4151. if (a != b)
  4152. break;
  4153. }
  4154. if (k == fl)
  4155. return (int)q + 1;
  4156. }
  4157. }
  4158. }
  4159. return 0;
  4160. }
  4161. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4162. * at-rules, so faces must be registered before any font-family declaration
  4163. * in the same sheet is resolved. */
  4164. static void scan_font_faces(const char *s, unsigned int len)
  4165. {
  4166. unsigned int i = 0;
  4167. while (i + 10 <= len) {
  4168. unsigned int j, end;
  4169. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4170. unsigned int flen = 0, ulen = 0, p;
  4171. int have_family = 0, have_url = 0;
  4172. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4173. i++;
  4174. continue;
  4175. }
  4176. j = i + 10;
  4177. while (j < len && s[j] != '{' && s[j] != ';')
  4178. j++;
  4179. if (j >= len || s[j] == ';') {
  4180. i = j + 1;
  4181. continue;
  4182. }
  4183. end = css_skip_nested(s, len, j + 1);
  4184. p = j + 1;
  4185. while (p < end) {
  4186. unsigned int ds, de, vs, ve;
  4187. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4188. p++;
  4189. if (p >= end)
  4190. break;
  4191. ds = p;
  4192. while (p < end && s[p] != ':' && s[p] != ';')
  4193. p++;
  4194. de = p;
  4195. if (p >= end || s[p] != ':') {
  4196. while (p < end && s[p] != ';')
  4197. p++;
  4198. continue;
  4199. }
  4200. p++;
  4201. vs = p;
  4202. while (p < end && s[p] != ';')
  4203. p++;
  4204. ve = p;
  4205. while (ds < de && is_space(s[ds]))
  4206. ds++;
  4207. while (de > ds && is_space(s[de - 1]))
  4208. de--;
  4209. while (vs < ve && is_space(s[vs]))
  4210. vs++;
  4211. while (ve > vs && is_space(s[ve - 1]))
  4212. ve--;
  4213. if (de - ds == 11 &&
  4214. ci_eq_n(s + ds, "font-family", 11)) {
  4215. const char *fp = s + vs;
  4216. unsigned int fn = ve - vs;
  4217. font_trim(&fp, &fn);
  4218. if (fn && fn < sizeof(family)) {
  4219. unsigned int k;
  4220. for (k = 0; k < fn; k++)
  4221. family[k] = fp[k];
  4222. family[fn] = '\0';
  4223. flen = fn;
  4224. have_family = 1;
  4225. }
  4226. } else if (de - ds == 3 &&
  4227. ci_eq_n(s + ds, "src", 3)) {
  4228. unsigned int q = vs;
  4229. while (q + 4 <= ve) {
  4230. unsigned int us, ue;
  4231. if (!ci_eq_n(s + q, "url(", 4)) {
  4232. q++;
  4233. continue;
  4234. }
  4235. q += 4;
  4236. us = q;
  4237. while (q < ve && s[q] != ')')
  4238. q++;
  4239. ue = q;
  4240. {
  4241. const char *up = s + us;
  4242. unsigned int un = ue - us;
  4243. font_trim(&up, &un);
  4244. if (un && un < sizeof(url) &&
  4245. !contains_ci_len(up, un,
  4246. "woff")) {
  4247. unsigned int k;
  4248. for (k = 0; k < un; k++)
  4249. url[k] = up[k];
  4250. url[un] = '\0';
  4251. ulen = un;
  4252. have_url = 1;
  4253. break;
  4254. }
  4255. }
  4256. }
  4257. }
  4258. }
  4259. if (have_family && have_url)
  4260. font_add(family, flen, url, ulen);
  4261. i = end;
  4262. }
  4263. }
  4264. /* One border-radius length. Unlike parse_size(), percentages keep their
  4265. * kind: the drawer scales them to the box, not the viewport. */
  4266. static int parse_radius_value(const char *s, unsigned int len,
  4267. unsigned int *pos, unsigned short *val,
  4268. unsigned char *kind)
  4269. {
  4270. unsigned int i = *pos;
  4271. unsigned long v = 0;
  4272. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4273. while (i < len && is_space(s[i]))
  4274. i++;
  4275. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4276. v = v * 10 + (unsigned int)(s[i] - '0');
  4277. digits = 1;
  4278. i++;
  4279. }
  4280. if (i < len && s[i] == '.') {
  4281. i++;
  4282. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4283. fdigits = 1;
  4284. if (frac < 2) {
  4285. v = v * 10 + (unsigned int)(s[i] - '0');
  4286. frac++;
  4287. }
  4288. i++;
  4289. }
  4290. }
  4291. while (frac < 2) {
  4292. v *= 10;
  4293. frac++;
  4294. }
  4295. while (i < len && is_space(s[i]))
  4296. i++;
  4297. if (i < len && s[i] == '%') {
  4298. pct = 1;
  4299. i++;
  4300. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4301. s[i + 2] == 'm') {
  4302. rem = 1;
  4303. i += 3;
  4304. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4305. rem = 2;
  4306. i += 2;
  4307. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4308. i += 2;
  4309. } else if (i < len && is_ident(s[i])) {
  4310. return 0;
  4311. }
  4312. if (!digits && !fdigits)
  4313. return 0;
  4314. if (rem == 1)
  4315. v *= root_font_px;
  4316. else if (rem == 2)
  4317. v *= root_font_px;
  4318. v /= 100;
  4319. if (v > 2048)
  4320. v = 2048;
  4321. *val = (unsigned short)v;
  4322. *kind = pct ? 2 : 1;
  4323. *pos = i;
  4324. return 1;
  4325. }
  4326. /* font: [style] [variant] [weight] size[/line-height] family */
  4327. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4328. const char *v, unsigned int len, int important)
  4329. {
  4330. unsigned int pos = 0, ts, te;
  4331. unsigned short size_raw = 0;
  4332. unsigned char size_unit = 0;
  4333. int have_size = 0;
  4334. int bold = 0, italic = 0;
  4335. int lh_px = 0, lh_pct = 0, mono = 0;
  4336. while (next_token(v, len, &pos, &ts, &te)) {
  4337. unsigned int tl = te - ts;
  4338. unsigned int slash = 0;
  4339. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4340. bold = 1;
  4341. continue;
  4342. }
  4343. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4344. continue;
  4345. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4346. italic = 1;
  4347. continue;
  4348. }
  4349. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4350. italic = 1;
  4351. continue;
  4352. }
  4353. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4354. continue;
  4355. for (slash = 0; slash < tl; slash++)
  4356. if (v[ts + slash] == '/')
  4357. break;
  4358. if (slash == tl) {
  4359. unsigned int q;
  4360. int weight = 0;
  4361. for (q = 0; q < tl; q++) {
  4362. if (v[ts + q] < '0' || v[ts + q] > '9')
  4363. break;
  4364. weight = weight * 10 + (v[ts + q] - '0');
  4365. }
  4366. if (q == tl && weight >= 100 && weight <= 900) {
  4367. bold = weight >= 500;
  4368. continue;
  4369. }
  4370. }
  4371. {
  4372. unsigned short raw;
  4373. unsigned char unit;
  4374. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4375. &raw, &unit))
  4376. continue;
  4377. size_raw = raw;
  4378. size_unit = unit;
  4379. }
  4380. have_size = 1;
  4381. if (slash < tl) {
  4382. unsigned int lstart = ts + slash + 1;
  4383. unsigned short lhp = 0;
  4384. unsigned char lhpct = 0;
  4385. if (!parse_line_height(v + lstart, te - lstart,
  4386. &lhp, &lhpct)) {
  4387. lh_px = 0;
  4388. lh_pct = 0;
  4389. } else {
  4390. lh_px = lhp;
  4391. lh_pct = lhpct;
  4392. }
  4393. }
  4394. }
  4395. if (!have_size)
  4396. return;
  4397. mono = contains_ci_len(v, len, "mono");
  4398. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4399. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4400. decl_add(out, nout, PROP_BOLD, bold, important);
  4401. decl_add(out, nout, PROP_ITALIC, italic, important);
  4402. decl_add(out, nout, PROP_MONO, mono, important);
  4403. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  4404. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4405. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4406. }
  4407. static int css_collect_only;
  4408. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4409. * index into d->layer_hash. css_layer_name is the dotted path of the
  4410. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4411. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4412. * layer's own rules therefore lose to its sublayers (the spec says the
  4413. * reverse), which no page we render depends on. */
  4414. static unsigned char css_cur_layer;
  4415. static char css_layer_name[128];
  4416. static unsigned int css_anon_layers;
  4417. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4418. {
  4419. unsigned int h = 2166136261u, i;
  4420. for (i = 0; i < len; i++) {
  4421. h ^= (unsigned char)s[i];
  4422. h *= 16777619u;
  4423. }
  4424. return h ? h : 1;
  4425. }
  4426. /* Register (or look up) the layer `name` nested under the current path and
  4427. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4428. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4429. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4430. unsigned int nlen, char *path,
  4431. unsigned int cap)
  4432. {
  4433. unsigned int pl = s_len(css_layer_name), i, h;
  4434. char anon[16];
  4435. while (nlen && is_space(*name)) { name++; nlen--; }
  4436. while (nlen && is_space(name[nlen - 1])) nlen--;
  4437. if (!nlen) {
  4438. /* An anonymous layer is unique; it can never be reopened.
  4439. * The collect-only (custom property) pass does not need a
  4440. * rank, so it must not burn a table slot. */
  4441. unsigned int n = ++css_anon_layers, k = 0;
  4442. char rev[10];
  4443. if (css_collect_only)
  4444. return 0;
  4445. anon[0] = '#';
  4446. while (n && k < sizeof(rev)) {
  4447. rev[k++] = (char)('0' + n % 10);
  4448. n /= 10;
  4449. }
  4450. for (i = 0; i < k; i++)
  4451. anon[1 + i] = rev[k - 1 - i];
  4452. name = anon;
  4453. nlen = k + 1;
  4454. }
  4455. if (pl + 1 + nlen >= cap)
  4456. return 0;
  4457. memcpy(path, css_layer_name, pl);
  4458. if (pl)
  4459. path[pl++] = '.';
  4460. memcpy(path + pl, name, nlen);
  4461. path[pl + nlen] = '\0';
  4462. h = layer_name_hash(path, pl + nlen);
  4463. for (i = 0; i < d->nlayers; i++)
  4464. if (d->layer_hash[i] == h)
  4465. return (unsigned char)(i + 1);
  4466. if (d->nlayers >= BR_MAX_LAYERS)
  4467. return 0;
  4468. d->layer_hash[d->nlayers++] = h;
  4469. return (unsigned char)d->nlayers;
  4470. }
  4471. /* @supports: the engine cannot answer feature queries, so the block
  4472. * applies unless it is negated (`not (...)` usually guards a fallback for
  4473. * old browsers) or asks for selector() support. */
  4474. static int supports_matches(const char *s, unsigned int len)
  4475. {
  4476. while (len && is_space(*s)) { s++; len--; }
  4477. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4478. (len == 3 || is_space(s[3]) || s[3] == '('))
  4479. return 0;
  4480. if (contains_ci_len(s, len, "selector("))
  4481. return 0;
  4482. return 1;
  4483. }
  4484. /* Append one code point of generated content as Latin-1. Typography the
  4485. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4486. * and anything else outside Latin-1 are dropped. */
  4487. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4488. unsigned int cp)
  4489. {
  4490. const char *sub = 0;
  4491. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4492. sub = "-";
  4493. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4494. sub = "'";
  4495. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4496. sub = "\"";
  4497. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4498. sub = "\xB7";
  4499. else if (cp == 0x2026)
  4500. sub = "...";
  4501. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4502. sub = ">";
  4503. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4504. sub = "<";
  4505. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4506. cp == 0x2003 || cp == 0x202F)
  4507. sub = " ";
  4508. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4509. sub = "\xD7";
  4510. else if (cp == 0x2713 || cp == 0x2714)
  4511. sub = "v";
  4512. if (sub) {
  4513. while (*sub && *n + 1 < cap)
  4514. out[(*n)++] = *sub++;
  4515. return;
  4516. }
  4517. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4518. out[(*n)++] = (char)cp;
  4519. else if (cp == '\n' && *n + 1 < cap)
  4520. out[(*n)++] = ' ';
  4521. }
  4522. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4523. * content index + 1, or 0 for none/empty (counters and url() are not
  4524. * generated). */
  4525. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4526. {
  4527. char out[160];
  4528. unsigned int i = 0, n = 0, off;
  4529. while (i < len) {
  4530. char c = s[i];
  4531. if (c == '"' || c == '\'') {
  4532. char q = c;
  4533. i++;
  4534. while (i < len && s[i] != q) {
  4535. unsigned int cp;
  4536. if (s[i] == '\\' && i + 1 < len) {
  4537. unsigned int h = 0, k = 0;
  4538. i++;
  4539. while (k < 6 && i < len &&
  4540. ((s[i] >= '0' && s[i] <= '9') ||
  4541. ((s[i] | 32) >= 'a' &&
  4542. (s[i] | 32) <= 'f'))) {
  4543. char x = (char)(s[i] | 32);
  4544. h = h * 16 + (unsigned int)(x <= '9' ?
  4545. x - '0' : x - 'a' + 10);
  4546. i++;
  4547. k++;
  4548. }
  4549. if (k) {
  4550. if (i < len && s[i] == ' ')
  4551. i++;
  4552. cp = h;
  4553. } else {
  4554. cp = (unsigned char)s[i++];
  4555. }
  4556. } else if ((unsigned char)s[i] >= 0xC0) {
  4557. unsigned char b = (unsigned char)s[i++];
  4558. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4559. cp = b & (0x3F >> more);
  4560. while (more-- && i < len &&
  4561. ((unsigned char)s[i] & 0xC0) == 0x80)
  4562. cp = (cp << 6) | (s[i++] & 0x3F);
  4563. } else {
  4564. cp = (unsigned char)s[i++];
  4565. }
  4566. content_put(out, &n, sizeof(out), cp);
  4567. }
  4568. i++;
  4569. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4570. unsigned int st = i + 5, e = st;
  4571. while (e < len && s[e] != ')')
  4572. e++;
  4573. while (st < e && is_space(s[st]))
  4574. st++;
  4575. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4576. out[n++] = '\001';
  4577. while (st < e && !is_space(s[st]))
  4578. out[n++] = s[st++];
  4579. out[n++] = '\002';
  4580. }
  4581. i = e + 1;
  4582. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4583. ci_eq_n(s + i, "close-quot", 10))) {
  4584. content_put(out, &n, sizeof(out), '"');
  4585. i += 10;
  4586. } else if (c == '(') {
  4587. unsigned int depth = 0;
  4588. /* counter(), url(), ...: skip the arguments */
  4589. for (; i < len; i++) {
  4590. if (s[i] == '(')
  4591. depth++;
  4592. else if (s[i] == ')' && --depth == 0)
  4593. break;
  4594. }
  4595. i++;
  4596. } else {
  4597. i++;
  4598. }
  4599. }
  4600. if (!n) {
  4601. /* `content: ""` still creates the box: icon
  4602. * pseudo-elements are empty strings with a size and a
  4603. * background image. */
  4604. for (i = 0; i < len; i++)
  4605. if (s[i] == '"' || s[i] == '\'')
  4606. return BR_CONTENT_EMPTY;
  4607. return 0;
  4608. }
  4609. if (d->ncontent >= BR_MAX_CONTENT)
  4610. return 0;
  4611. off = store_raw(d, out, n, sizeof(out));
  4612. if (!off)
  4613. return 0;
  4614. d->content_off[d->ncontent++] = off;
  4615. return d->ncontent;
  4616. }
  4617. /* ---- calc() folding ---------------------------------------------------
  4618. *
  4619. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4620. * Frameworks build their scales from arithmetic on custom properties
  4621. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4622. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4623. * calc() in a declaration block is folded into that canonical form:
  4624. * a linear combination of %, px (rem/em at the root font size, vw against
  4625. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4626. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4627. * the calc() text unchanged. Values are hundredths. */
  4628. static unsigned int viewport_w; /* defined with the media code */
  4629. struct calc_val {
  4630. long px, pct, num;
  4631. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  4632. };
  4633. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4634. struct calc_val *v, int depth);
  4635. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  4636. {
  4637. while (*i < len && is_space(s[*i]))
  4638. (*i)++;
  4639. }
  4640. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  4641. struct calc_val *v, int depth)
  4642. {
  4643. long whole = 0, frac = 0, scale = 1, n;
  4644. int sign = 1, digits = 0;
  4645. calc_space(s, len, i);
  4646. if (*i < len && (s[*i] == '(' ||
  4647. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  4648. *i += s[*i] == '(' ? 1 : 5;
  4649. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  4650. return 0;
  4651. calc_space(s, len, i);
  4652. if (*i >= len || s[*i] != ')')
  4653. return 0;
  4654. (*i)++;
  4655. return 1;
  4656. }
  4657. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  4658. sign = s[*i] == '-' ? -1 : 1;
  4659. (*i)++;
  4660. }
  4661. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4662. whole = whole * 10 + (s[*i] - '0');
  4663. digits = 1;
  4664. (*i)++;
  4665. if (whole > 1000000)
  4666. return 0;
  4667. }
  4668. if (*i < len && s[*i] == '.') {
  4669. (*i)++;
  4670. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4671. if (scale < 10000) {
  4672. frac = frac * 10 + (s[*i] - '0');
  4673. scale *= 10;
  4674. }
  4675. digits = 1;
  4676. (*i)++;
  4677. }
  4678. }
  4679. if (!digits)
  4680. return 0;
  4681. n = sign * (whole * 100 + frac * 100 / scale);
  4682. v->px = v->pct = v->num = 0;
  4683. if (*i < len && s[*i] == '%') {
  4684. (*i)++;
  4685. v->pct = n;
  4686. v->kind = 2;
  4687. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  4688. *i += 3;
  4689. v->px = n * root_font_px;
  4690. v->kind = 1;
  4691. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  4692. *i += 2;
  4693. v->px = n * root_font_px;
  4694. v->kind = 1;
  4695. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  4696. *i += 2;
  4697. v->px = n;
  4698. v->kind = 1;
  4699. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  4700. *i += 2;
  4701. v->px = n * (long)viewport_w / 100;
  4702. v->kind = 1;
  4703. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  4704. return 0; /* vh, deg, fr, ch, ... */
  4705. } else {
  4706. v->num = n;
  4707. v->kind = 4;
  4708. }
  4709. return 1;
  4710. }
  4711. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  4712. struct calc_val *v, int depth)
  4713. {
  4714. if (!calc_unary(s, len, i, v, depth))
  4715. return 0;
  4716. for (;;) {
  4717. struct calc_val r;
  4718. char op;
  4719. calc_space(s, len, i);
  4720. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  4721. return 1;
  4722. op = s[(*i)++];
  4723. if (!calc_unary(s, len, i, &r, depth))
  4724. return 0;
  4725. if (op == '/') {
  4726. if (r.kind != 4 || !r.num)
  4727. return 0;
  4728. v->px = v->px * 100 / r.num;
  4729. v->pct = v->pct * 100 / r.num;
  4730. v->num = v->num * 100 / r.num;
  4731. } else if (r.kind == 4) {
  4732. v->px = v->px * r.num / 100;
  4733. v->pct = v->pct * r.num / 100;
  4734. v->num = v->num * r.num / 100;
  4735. } else if (v->kind == 4) {
  4736. long k = v->num;
  4737. *v = r;
  4738. v->px = v->px * k / 100;
  4739. v->pct = v->pct * k / 100;
  4740. } else {
  4741. return 0; /* length * length */
  4742. }
  4743. }
  4744. }
  4745. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4746. struct calc_val *v, int depth)
  4747. {
  4748. if (!calc_product(s, len, i, v, depth))
  4749. return 0;
  4750. for (;;) {
  4751. struct calc_val r;
  4752. int neg;
  4753. calc_space(s, len, i);
  4754. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  4755. return 1;
  4756. neg = s[(*i)++] == '-';
  4757. if (!calc_product(s, len, i, &r, depth))
  4758. return 0;
  4759. if ((v->kind == 4) != (r.kind == 4))
  4760. return 0; /* number + length */
  4761. v->px += neg ? -r.px : r.px;
  4762. v->pct += neg ? -r.pct : r.pct;
  4763. v->num += neg ? -r.num : r.num;
  4764. v->kind |= r.kind;
  4765. }
  4766. }
  4767. /* Hundredths -> "12", "-0.5", "33.33". */
  4768. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  4769. {
  4770. char tmp[24];
  4771. unsigned int n = 0, k = 0;
  4772. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  4773. unsigned long w = a / 100, f = a % 100;
  4774. if (v < 0)
  4775. tmp[n++] = '-';
  4776. {
  4777. char rev[12];
  4778. unsigned int r = 0;
  4779. do {
  4780. rev[r++] = (char)('0' + w % 10);
  4781. w /= 10;
  4782. } while (w && r < sizeof(rev));
  4783. while (r)
  4784. tmp[n++] = rev[--r];
  4785. }
  4786. if (f) {
  4787. tmp[n++] = '.';
  4788. tmp[n++] = (char)('0' + f / 10);
  4789. if (f % 10)
  4790. tmp[n++] = (char)('0' + f % 10);
  4791. }
  4792. for (k = 0; k < n && k < cap; k++)
  4793. o[k] = tmp[k];
  4794. return k;
  4795. }
  4796. /* Copy s to out with every foldable calc() replaced; returns the length,
  4797. * or 0 when nothing was folded (the caller keeps the original). */
  4798. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  4799. unsigned int cap)
  4800. {
  4801. unsigned int i = 0, n = 0;
  4802. int changed = 0;
  4803. while (i < len && n + 40 < cap) {
  4804. struct calc_val v;
  4805. unsigned int j = i + 5;
  4806. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  4807. (i == 0 || !is_ident(s[i - 1])))) {
  4808. out[n++] = s[i++];
  4809. continue;
  4810. }
  4811. if (!calc_sum(s, len, &j, &v, 0) ||
  4812. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  4813. out[n++] = s[i++];
  4814. continue;
  4815. }
  4816. j++;
  4817. /* Minified CSS glues tokens to the function's parentheses
  4818. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  4819. * a folded `10px16px` would be one token. */
  4820. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  4821. out[n - 1] != '(' && out[n - 1] != ',')
  4822. out[n++] = ' ';
  4823. if (v.kind == 4) {
  4824. n += calc_fmt(out + n, cap - n, v.num);
  4825. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  4826. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  4827. -50 : 50)) /
  4828. 100 * 100);
  4829. out[n++] = 'p';
  4830. out[n++] = 'x';
  4831. } else if (!v.px) {
  4832. n += calc_fmt(out + n, cap - n, v.pct);
  4833. out[n++] = '%';
  4834. } else {
  4835. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  4836. memcpy(out + n, "calc(", 5);
  4837. n += 5;
  4838. n += calc_fmt(out + n, cap - n, v.pct);
  4839. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  4840. n += 4;
  4841. n += calc_fmt(out + n, cap - n, px * 100);
  4842. memcpy(out + n, "px)", 3);
  4843. n += 3;
  4844. }
  4845. i = j;
  4846. changed = 1;
  4847. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  4848. s[i] != ',' && s[i] != '!' && s[i] != '}')
  4849. out[n++] = ' ';
  4850. }
  4851. if (!changed || i < len)
  4852. return 0;
  4853. out[n] = '\0';
  4854. return n;
  4855. }
  4856. static int css_has_calc(const char *s, unsigned int len)
  4857. {
  4858. unsigned int i;
  4859. for (i = 0; i + 5 <= len; i++)
  4860. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  4861. return 1;
  4862. return 0;
  4863. }
  4864. static void parse_decls(struct br_doc *d, const char *s,
  4865. unsigned int len, struct br_decl *out,
  4866. unsigned int *nout)
  4867. {
  4868. static char expanded[4096];
  4869. static char folded[4096];
  4870. if (css_has_var(s, len)) {
  4871. unsigned int elen;
  4872. if (!css_env_active)
  4873. css_local_collect(s, len);
  4874. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  4875. 0);
  4876. if (!css_env_active)
  4877. css_nlocal = 0;
  4878. if (elen) {
  4879. s = expanded;
  4880. len = elen;
  4881. }
  4882. }
  4883. if (css_has_calc(s, len)) {
  4884. unsigned int flen = css_fold_calc(s, len, folded,
  4885. sizeof(folded));
  4886. if (flen) {
  4887. s = folded;
  4888. len = flen;
  4889. }
  4890. }
  4891. unsigned int i = 0;
  4892. *nout = 0;
  4893. while (i < len && *nout < BR_DECLS) {
  4894. unsigned int pstart, pend, vstart, vend, k;
  4895. char prop[24];
  4896. int important = 0;
  4897. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4898. i++;
  4899. if (i >= len)
  4900. break;
  4901. /* A nested at-rule (@media/@supports) inside the declaration
  4902. * block carries its own selector context; skip it rather than
  4903. * reading its prelude as a declaration. */
  4904. if (s[i] == '@') {
  4905. while (i < len && s[i] != '{' && s[i] != ';')
  4906. i++;
  4907. if (i < len && s[i] == '{')
  4908. i = css_skip_nested(s, len, i + 1);
  4909. else if (i < len)
  4910. i++;
  4911. continue;
  4912. }
  4913. pstart = i;
  4914. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4915. i++;
  4916. if (i < len && s[i] == '{') {
  4917. /* Nested style rule: skip its block. */
  4918. i = css_skip_nested(s, len, i + 1);
  4919. continue;
  4920. }
  4921. pend = i;
  4922. if (i >= len || s[i] != ':')
  4923. continue;
  4924. i++;
  4925. vstart = i;
  4926. /* `;` inside a string or url() (data:image/svg+xml;utf8,...)
  4927. * does not end the value. */
  4928. i = css_value_end(s, len, i);
  4929. vend = i;
  4930. while (pstart < pend && is_space(s[pstart]))
  4931. pstart++;
  4932. while (pend > pstart && is_space(s[pend - 1]))
  4933. pend--;
  4934. while (vstart < vend && is_space(s[vstart]))
  4935. vstart++;
  4936. while (vend > vstart && is_space(s[vend - 1]))
  4937. vend--;
  4938. if (vend - vstart >= 10 &&
  4939. ci_eq_n(s + vend - 10, "!important", 10)) {
  4940. important = 1;
  4941. vend -= 10;
  4942. while (vend > vstart && is_space(s[vend - 1]))
  4943. vend--;
  4944. }
  4945. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4946. char c = s[pstart + k];
  4947. if (c >= 'A' && c <= 'Z')
  4948. c = (char)(c - 'A' + 'a');
  4949. prop[k] = c;
  4950. }
  4951. prop[k] = '\0';
  4952. if (str_eq(prop, "color")) {
  4953. unsigned short c;
  4954. if (parse_color(s + vstart, vend - vstart, &c))
  4955. decl_add(out, nout, PROP_COLOR, c, important);
  4956. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  4957. /* SVG paint only: an HTML element's fill never paints
  4958. * its background (tagesschau sets fill on buttons). */
  4959. int is_fill = prop[0] == 'f';
  4960. unsigned short c;
  4961. int mode = paint_mode(s + vstart, vend - vstart, &c);
  4962. if (mode) {
  4963. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  4964. PROP_STROKE_MODE, mode, important);
  4965. if (mode == BR_PAINT_COLOR)
  4966. decl_add(out, nout, is_fill ? PROP_FILL :
  4967. PROP_STROKE, (short)c,
  4968. important);
  4969. }
  4970. } else if (str_eq(prop, "stroke-width")) {
  4971. int sw = svg_len16(s + vstart, vend - vstart);
  4972. if (sw >= 0)
  4973. decl_add(out, nout, PROP_STROKE_W, sw,
  4974. important);
  4975. } else if (str_eq(prop, "fill-rule")) {
  4976. decl_add(out, nout, PROP_FILL_RULE,
  4977. contains_ci_len(s + vstart, vend - vstart,
  4978. "evenodd") ? 1 : 0,
  4979. important);
  4980. } else if (str_eq(prop, "text-anchor")) {
  4981. int a = BR_ALIGN_LEFT;
  4982. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4983. a = BR_ALIGN_CENTER;
  4984. else if (contains_ci_len(s + vstart, vend - vstart,
  4985. "end"))
  4986. a = BR_ALIGN_RIGHT;
  4987. decl_add(out, nout, PROP_ALIGN, a, important);
  4988. } else if (str_eq(prop, "background-image")) {
  4989. add_bg_image_decl(out, nout, s + vstart,
  4990. vend - vstart, important);
  4991. } else if (str_eq(prop, "background-repeat")) {
  4992. int r = 0;
  4993. if (contains_ci_len(s + vstart, vend - vstart,
  4994. "no-repeat"))
  4995. r = 1;
  4996. else if (contains_ci_len(s + vstart, vend - vstart,
  4997. "repeat-x"))
  4998. r = 2;
  4999. else if (contains_ci_len(s + vstart, vend - vstart,
  5000. "repeat-y"))
  5001. r = 3;
  5002. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  5003. } else if (str_eq(prop, "background-position")) {
  5004. add_bg_position(out, nout, s + vstart, vend - vstart,
  5005. important);
  5006. } else if (str_eq(prop, "background-size")) {
  5007. add_bg_size(out, nout, s + vstart, vend - vstart,
  5008. important);
  5009. } else if (str_eq(prop, "background-color") ||
  5010. str_eq(prop, "background")) {
  5011. unsigned short c;
  5012. int is_bg = str_eq(prop, "background");
  5013. static char bgv[1024];
  5014. const char *vs = s + vstart;
  5015. unsigned int vl = vend - vstart;
  5016. if (is_bg) {
  5017. add_bg_image_decl(out, nout, s + vstart,
  5018. vend - vstart, important);
  5019. /* Keywords and colours are read with url()
  5020. * contents blanked: a data: SVG carries its
  5021. * own fill="rgba(...)". */
  5022. vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
  5023. vs = bgv;
  5024. add_bg_shorthand_extra(out, nout,
  5025. vs, vl,
  5026. important);
  5027. if (contains_ci_len(vs, vl,
  5028. "no-repeat"))
  5029. decl_add(out, nout, PROP_BG_REPEAT, 1,
  5030. important);
  5031. else if (contains_ci_len(vs, vl,
  5032. "repeat-x"))
  5033. decl_add(out, nout, PROP_BG_REPEAT, 2,
  5034. important);
  5035. else if (contains_ci_len(vs, vl,
  5036. "repeat-y"))
  5037. decl_add(out, nout, PROP_BG_REPEAT, 3,
  5038. important);
  5039. }
  5040. /* `transparent`/`none` are real values: they must
  5041. * beat a lower-specificity colour (and tell form
  5042. * controls the author styled them). */
  5043. if (color_is_transparent(vs, vl) ||
  5044. (vl == 4 &&
  5045. ci_eq_n(vs, "none", 4)))
  5046. decl_add(out, nout, PROP_BG_NONE, 1, important);
  5047. else if (parse_color(vs, vl, &c))
  5048. decl_add(out, nout, PROP_BG, c, important);
  5049. else if (is_bg &&
  5050. find_color_last(vs, vl, &c))
  5051. decl_add(out, nout, PROP_BG, c, important);
  5052. } else if (str_eq(prop, "font-size")) {
  5053. unsigned short raw;
  5054. unsigned char unit;
  5055. if (parse_font_size(s + vstart, vend - vstart,
  5056. &raw, &unit)) {
  5057. decl_add(out, nout, PROP_FONT_PX, raw,
  5058. important);
  5059. decl_add(out, nout, PROP_FONT_UNIT, unit,
  5060. important);
  5061. }
  5062. } else if (str_eq(prop, "font-weight")) {
  5063. int bold = 0;
  5064. unsigned int j;
  5065. if (vend - vstart >= 4 &&
  5066. (ci_eq_n(s + vstart, "bold", 4) ||
  5067. (vend - vstart >= 6 &&
  5068. ci_eq_n(s + vstart, "bolder", 6))))
  5069. bold = 1;
  5070. else {
  5071. int w = 0;
  5072. for (j = vstart; j < vend; j++)
  5073. if (s[j] >= '0' && s[j] <= '9')
  5074. w = w * 10 + (s[j] - '0');
  5075. /* CSS font matching: with only a 400 and a
  5076. * 700 face, 500 (Infima's "semibold" menus)
  5077. * resolves to the regular face; 600 and up
  5078. * to the bold one. */
  5079. if (w >= 600)
  5080. bold = 1;
  5081. }
  5082. decl_add(out, nout, PROP_BOLD, bold, important);
  5083. } else if (str_eq(prop, "font-style")) {
  5084. int italic = (vend - vstart >= 6 &&
  5085. ci_eq_n(s + vstart, "italic", 6));
  5086. decl_add(out, nout, PROP_ITALIC, italic, important);
  5087. } else if (str_eq(prop, "font-family")) {
  5088. unsigned int j;
  5089. int mono = 0;
  5090. int face = font_family_id(s + vstart, vend - vstart);
  5091. for (j = vstart; j + 4 <= vend; j++)
  5092. if (ci_eq_n(s + j, "mono", 4))
  5093. mono = 1;
  5094. decl_add(out, nout, PROP_FONT_FACE, face, important);
  5095. decl_add(out, nout, PROP_MONO, mono, important);
  5096. } else if (str_eq(prop, "font")) {
  5097. add_font_shorthand(out, nout, s + vstart,
  5098. vend - vstart, important);
  5099. } else if (str_eq(prop, "text-align")) {
  5100. int a = BR_ALIGN_LEFT;
  5101. if (vend - vstart >= 6 &&
  5102. ci_eq_n(s + vstart, "center", 6))
  5103. a = BR_ALIGN_CENTER;
  5104. else if (vend - vstart >= 5 &&
  5105. ci_eq_n(s + vstart, "right", 5))
  5106. a = BR_ALIGN_RIGHT;
  5107. decl_add(out, nout, PROP_ALIGN, a, important);
  5108. } else if (str_eq(prop, "text-decoration")) {
  5109. int u = 0, stv = 0, ov = 0;
  5110. if (contains_ci_len(s + vstart, vend - vstart,
  5111. "underline"))
  5112. u = 1;
  5113. if (contains_ci_len(s + vstart, vend - vstart,
  5114. "line-through"))
  5115. stv = 1;
  5116. if (contains_ci_len(s + vstart, vend - vstart,
  5117. "overline"))
  5118. ov = 1;
  5119. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5120. decl_add(out, nout, PROP_STRIKE, stv, important);
  5121. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5122. } else if (str_eq(prop, "cursor")) {
  5123. int c = parse_cursor(s + vstart, vend - vstart);
  5124. if (c >= 0)
  5125. decl_add(out, nout, PROP_CURSOR, c, important);
  5126. } else if (str_eq(prop, "text-transform")) {
  5127. int u = (vend - vstart >= 9 &&
  5128. ci_eq_n(s + vstart, "uppercase", 9));
  5129. decl_add(out, nout, PROP_UPPER, u, important);
  5130. } else if (str_eq(prop, "white-space")) {
  5131. int nw = contains_ci_len(s + vstart, vend - vstart,
  5132. "nowrap") ||
  5133. contains_ci_len(s + vstart, vend - vstart,
  5134. "pre");
  5135. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5136. } else if (str_eq(prop, "display")) {
  5137. decl_add(out, nout, PROP_DISPLAY,
  5138. parse_display(s + vstart, vend - vstart),
  5139. important);
  5140. } else if (str_eq(prop, "visibility")) {
  5141. int h = contains_ci_len(s + vstart, vend - vstart,
  5142. "hidden") ||
  5143. contains_ci_len(s + vstart, vend - vstart,
  5144. "collapse");
  5145. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5146. } else if (str_eq(prop, "opacity")) {
  5147. unsigned int q;
  5148. int h = 1, any = 0;
  5149. for (q = vstart; q < vend; q++) {
  5150. if (s[q] >= '1' && s[q] <= '9') {
  5151. h = 0;
  5152. break;
  5153. }
  5154. if (s[q] >= '0' && s[q] <= '9')
  5155. any = 1;
  5156. }
  5157. if (!any || contains_ci_len(s + vstart, vend - vstart,
  5158. "inherit"))
  5159. h = 0;
  5160. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  5161. } else if (str_eq(prop, "overflow") ||
  5162. str_eq(prop, "overflow-x") ||
  5163. str_eq(prop, "overflow-y")) {
  5164. int h = contains_ci_len(s + vstart, vend - vstart,
  5165. "hidden") ||
  5166. contains_ci_len(s + vstart, vend - vstart,
  5167. "clip");
  5168. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5169. } else if (str_eq(prop, "float")) {
  5170. int f = 0;
  5171. if (vend - vstart >= 4 &&
  5172. ci_eq_n(s + vstart, "left", 4))
  5173. f = 1;
  5174. else if (vend - vstart >= 5 &&
  5175. ci_eq_n(s + vstart, "right", 5))
  5176. f = 2;
  5177. decl_add(out, nout, PROP_FLOAT, f, important);
  5178. } else if (str_eq(prop, "clear")) {
  5179. int c = 0;
  5180. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5181. c = 3;
  5182. else if (contains_ci_len(s + vstart, vend - vstart,
  5183. "left"))
  5184. c = 1;
  5185. else if (contains_ci_len(s + vstart, vend - vstart,
  5186. "right"))
  5187. c = 2;
  5188. decl_add(out, nout, PROP_CLEAR, c, important);
  5189. } else if (str_eq(prop, "flex-grow") ||
  5190. str_eq(prop, "flex")) {
  5191. unsigned int q = vstart;
  5192. int g = 0;
  5193. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5194. q++;
  5195. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5196. ci_eq_n(s + q, "auto", 4))
  5197. g = 1; /* flex: auto = 1 1 auto */
  5198. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5199. g = g * 10 + (s[q] - '0');
  5200. q++;
  5201. }
  5202. if (g > 100)
  5203. g = 100;
  5204. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5205. if (str_eq(prop, "flex")) {
  5206. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5207. * basis is the item's main size, which our flex
  5208. * row reads from width (Infima's grid:
  5209. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5210. * of the grow number, then any unitless shrink. */
  5211. unsigned int t;
  5212. while (q < vend && (s[q] == '.' ||
  5213. (s[q] >= '0' && s[q] <= '9')))
  5214. q++;
  5215. for (t = 0; t < 2 && q < vend; t++) {
  5216. unsigned int st, e, k2;
  5217. unsigned short bv;
  5218. unsigned char bk;
  5219. while (q < vend && is_space(s[q]))
  5220. q++;
  5221. st = q;
  5222. while (q < vend && !is_space(s[q]))
  5223. q++;
  5224. e = q;
  5225. for (k2 = st; k2 < e; k2++)
  5226. if (!(s[k2] == '.' ||
  5227. (s[k2] >= '0' &&
  5228. s[k2] <= '9')))
  5229. break;
  5230. if (k2 == e)
  5231. continue; /* shrink */
  5232. if (parse_size(s + st, e - st, &bv,
  5233. &bk) && bk && bv) {
  5234. decl_add(out, nout, PROP_WIDTH,
  5235. bv, important);
  5236. decl_add(out, nout,
  5237. PROP_WIDTH_KIND, bk,
  5238. important);
  5239. }
  5240. break;
  5241. }
  5242. }
  5243. } else if (str_eq(prop, "flex-basis")) {
  5244. unsigned short bv;
  5245. unsigned char bk;
  5246. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5247. bk && bv) {
  5248. decl_add(out, nout, PROP_WIDTH, bv, important);
  5249. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5250. important);
  5251. }
  5252. } else if (str_eq(prop, "flex-direction")) {
  5253. decl_add(out, nout, PROP_FLEX_COL,
  5254. contains_ci_len(s + vstart, vend - vstart,
  5255. "column"), important);
  5256. } else if (str_eq(prop, "flex-wrap")) {
  5257. int wrap = 0;
  5258. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5259. wrap = 2;
  5260. else if (contains_ci_len(s + vstart, vend - vstart,
  5261. "wrap"))
  5262. wrap = 1;
  5263. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5264. } else if (str_eq(prop, "justify-content")) {
  5265. int j = 0;
  5266. if (contains_ci_len(s + vstart, vend - vstart,
  5267. "space-between"))
  5268. j = 3;
  5269. else if (contains_ci_len(s + vstart, vend - vstart,
  5270. "space-around"))
  5271. j = 4;
  5272. else if (contains_ci_len(s + vstart, vend - vstart,
  5273. "space-evenly"))
  5274. j = 5;
  5275. else if (contains_ci_len(s + vstart, vend - vstart,
  5276. "center"))
  5277. j = 1;
  5278. else if (contains_ci_len(s + vstart, vend - vstart,
  5279. "flex-end") ||
  5280. contains_ci_len(s + vstart, vend - vstart,
  5281. "end"))
  5282. j = 2;
  5283. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5284. } else if (str_eq(prop, "align-self")) {
  5285. /* 0 auto (the container's align-items), else
  5286. * align-items + 1. */
  5287. int a = 1;
  5288. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5289. a = 0;
  5290. else if (contains_ci_len(s + vstart, vend - vstart,
  5291. "center"))
  5292. a = 2;
  5293. else if (contains_ci_len(s + vstart, vend - vstart,
  5294. "flex-end") ||
  5295. contains_ci_len(s + vstart, vend - vstart,
  5296. "end"))
  5297. a = 3;
  5298. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5299. } else if (str_eq(prop, "align-items")) {
  5300. int a = 0;
  5301. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5302. a = 1;
  5303. else if (contains_ci_len(s + vstart, vend - vstart,
  5304. "flex-end") ||
  5305. contains_ci_len(s + vstart, vend - vstart,
  5306. "end"))
  5307. a = 2;
  5308. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5309. } else if (str_eq(prop, "position")) {
  5310. int p = 0;
  5311. if (contains_ci_len(s + vstart, vend - vstart,
  5312. "absolute"))
  5313. p = 1;
  5314. else if (contains_ci_len(s + vstart, vend - vstart,
  5315. "fixed"))
  5316. p = 2;
  5317. else if (contains_ci_len(s + vstart, vend - vstart,
  5318. "relative"))
  5319. p = 3;
  5320. else if (contains_ci_len(s + vstart, vend - vstart,
  5321. "sticky"))
  5322. p = 4;
  5323. decl_add(out, nout, PROP_POS, p, important);
  5324. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5325. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5326. int which = str_eq(prop, "left") ? 0 :
  5327. str_eq(prop, "top") ? 1 :
  5328. str_eq(prop, "right") ? 2 : 3;
  5329. unsigned int pos = 0;
  5330. if (!(vend - vstart >= 4 &&
  5331. ci_eq_n(s + vstart, "auto", 4))) {
  5332. int v = parse_len(s + vstart, vend - vstart, &pos);
  5333. if (v > 32000)
  5334. v = 32000;
  5335. else if (v < -32000)
  5336. v = -32000;
  5337. decl_add(out, nout,
  5338. (unsigned char)(which == 0 ? PROP_LEFT :
  5339. which == 1 ? PROP_TOP :
  5340. which == 2 ? PROP_RIGHT :
  5341. PROP_BOTTOM),
  5342. v, important);
  5343. decl_add(out, nout,
  5344. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5345. which == 1 ? PROP_TOP_SET :
  5346. which == 2 ? PROP_RIGHT_SET :
  5347. PROP_BOTTOM_SET),
  5348. 1, important);
  5349. }
  5350. } else if (str_eq(prop, "z-index")) {
  5351. unsigned int q = vstart;
  5352. int sign = 1;
  5353. int v = 0;
  5354. int any = 0;
  5355. if (q < vend && s[q] == '-') {
  5356. sign = -1;
  5357. q++;
  5358. }
  5359. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5360. v = v * 10 + (s[q] - '0');
  5361. any = 1;
  5362. q++;
  5363. }
  5364. if (any) {
  5365. v *= sign;
  5366. if (v > 32000)
  5367. v = 32000;
  5368. else if (v < -32000)
  5369. v = -32000;
  5370. decl_add(out, nout, PROP_ZINDEX, v, important);
  5371. decl_add(out, nout, PROP_ZSET, 1, important);
  5372. } else if (vend - vstart >= 4 &&
  5373. ci_eq_n(s + vstart, "auto", 4)) {
  5374. decl_add(out, nout, PROP_ZSET, 0, important);
  5375. }
  5376. } else if (str_eq(prop, "transform")) {
  5377. const char *v = s + vstart;
  5378. unsigned int vl = vend - vstart;
  5379. int tx = 0, ty = 0;
  5380. int is_x = contains_ci_len(v, vl, "translatex");
  5381. int is_y = contains_ci_len(v, vl, "translatey");
  5382. unsigned int off = 0, arg;
  5383. if (contains_ci_len(v, vl, "translate")) {
  5384. while (off < vl && v[off] != '(')
  5385. off++;
  5386. if (off < vl)
  5387. off++;
  5388. if (!is_y)
  5389. tx = parse_len(v, vl, &off);
  5390. if (!is_x) {
  5391. while (off < vl &&
  5392. (is_space(v[off]) || v[off] == ','))
  5393. off++;
  5394. ty = parse_len(v, vl, &off);
  5395. }
  5396. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5397. important);
  5398. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5399. important);
  5400. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5401. important);
  5402. }
  5403. if (find_fn(v, vl, "scale", &arg)) {
  5404. unsigned int q = arg;
  5405. signed short sx = 100, sy = 100;
  5406. if (parse_fixed_100(v, vl, &q, &sx)) {
  5407. unsigned int r = q;
  5408. while (r < vl &&
  5409. (is_space(v[r]) || v[r] == ','))
  5410. r++;
  5411. if (!parse_fixed_100(v, vl, &r, &sy))
  5412. sy = sx;
  5413. decl_add(out, nout, PROP_SCALE_X, sx,
  5414. important);
  5415. decl_add(out, nout, PROP_SCALE_Y, sy,
  5416. important);
  5417. decl_add(out, nout,
  5418. PROP_TRANSFORM_PAINT, 1,
  5419. important);
  5420. decl_add(out, nout,
  5421. PROP_TRANSFORM_SET, 1,
  5422. important);
  5423. }
  5424. }
  5425. if (find_fn(v, vl, "rotate", &arg)) {
  5426. unsigned int q = arg;
  5427. signed short deg;
  5428. if (parse_fixed_100(v, vl, &q, &deg)) {
  5429. decl_add(out, nout, PROP_ROTATE, deg,
  5430. important);
  5431. decl_add(out, nout,
  5432. PROP_TRANSFORM_PAINT, 1,
  5433. important);
  5434. decl_add(out, nout,
  5435. PROP_TRANSFORM_SET, 1,
  5436. important);
  5437. }
  5438. }
  5439. } else if (str_eq(prop, "transform-origin")) {
  5440. add_transform_origin(out, nout, s + vstart,
  5441. vend - vstart, important);
  5442. } else if (str_eq(prop, "grid-template-columns") ||
  5443. str_eq(prop, "grid-template")) {
  5444. const char *v = s + vstart;
  5445. unsigned int vl = vend - vstart;
  5446. unsigned short val[2];
  5447. unsigned char kind[2];
  5448. int n, q;
  5449. if (str_eq(prop, "grid-template")) {
  5450. for (q = 0; q < (int)vl; q++)
  5451. if (v[q] == '/') {
  5452. q++;
  5453. while (q < (int)vl && is_space(v[q]))
  5454. q++;
  5455. v += q;
  5456. vl -= (unsigned int)q;
  5457. break;
  5458. }
  5459. }
  5460. if (grid_repeat_count(v, vl) >= 1) {
  5461. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5462. * Tailwind's grid-cols-N. N equal columns
  5463. * (GRID_FIXED + N); one column is plain block
  5464. * flow, which also resets a smaller breakpoint's
  5465. * count. */
  5466. int cnt = grid_repeat_count(v, vl);
  5467. decl_add(out, nout, PROP_GRID_AUTO,
  5468. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5469. important);
  5470. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5471. important);
  5472. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5473. contains_ci_len(v, vl, "auto-fill")) {
  5474. unsigned int mp = 0;
  5475. int found = 0;
  5476. unsigned short mw = 200;
  5477. unsigned char mk = 1;
  5478. for (q = 0; q + 7 <= (int)vl; q++)
  5479. if (ci_eq_n(v + q, "minmax(", 7)) {
  5480. mp = (unsigned int)q + 7;
  5481. found = 1;
  5482. break;
  5483. }
  5484. if (found)
  5485. parse_grid_track(v, vl, &mp, &mw, &mk);
  5486. if (mk != 1 || mw < 80)
  5487. mw = 200;
  5488. if (mw > 480)
  5489. mw = 480;
  5490. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5491. decl_add(out, nout, PROP_WIDTH, mw, important);
  5492. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5493. } else {
  5494. n = parse_grid_cols(v, vl, val, kind);
  5495. if (n > 0) {
  5496. decl_add(out, nout, PROP_WIDTH, val[0],
  5497. important);
  5498. decl_add(out, nout, PROP_WIDTH_KIND,
  5499. kind[0], important);
  5500. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5501. important);
  5502. }
  5503. if (n > 1) {
  5504. decl_add(out, nout, PROP_MAXW, val[1],
  5505. important);
  5506. decl_add(out, nout, PROP_MAXW_KIND,
  5507. kind[1], important);
  5508. }
  5509. }
  5510. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5511. /* From the value, not the block start: gap was
  5512. * ignored on nearly every site. */
  5513. unsigned int gp = vstart;
  5514. unsigned short gv = 0;
  5515. unsigned char gk = 1;
  5516. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5517. if (gv > 120)
  5518. gv = 120;
  5519. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5520. }
  5521. } else if (str_eq(prop, "grid-area")) {
  5522. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5523. if (slot)
  5524. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5525. } else if (str_eq(prop, "width") ||
  5526. str_eq(prop, "max-width") ||
  5527. str_eq(prop, "min-width") ||
  5528. str_eq(prop, "height") ||
  5529. str_eq(prop, "max-height") ||
  5530. str_eq(prop, "min-height")) {
  5531. unsigned short v;
  5532. unsigned char kind;
  5533. if (vend - vstart >= 5 &&
  5534. ci_eq_n(s + vstart, "calc(", 5)) {
  5535. int pct, px;
  5536. if (parse_calc(s + vstart + 5,
  5537. vend - vstart - 5, &pct, &px)) {
  5538. if (str_eq(prop, "width")) {
  5539. decl_add(out, nout, PROP_WIDTH,
  5540. 0, important);
  5541. decl_add(out, nout,
  5542. PROP_WIDTH_KIND, 3,
  5543. important);
  5544. decl_add(out, nout,
  5545. PROP_WIDTH_CALC_PCT,
  5546. pct, important);
  5547. decl_add(out, nout,
  5548. PROP_WIDTH_CALC_PX,
  5549. px, important);
  5550. } else if (str_eq(prop, "max-width")) {
  5551. decl_add(out, nout, PROP_MAXW,
  5552. 0, important);
  5553. decl_add(out, nout,
  5554. PROP_MAXW_KIND, 3,
  5555. important);
  5556. decl_add(out, nout,
  5557. PROP_MAXW_CALC_PCT,
  5558. pct, important);
  5559. decl_add(out, nout,
  5560. PROP_MAXW_CALC_PX,
  5561. px, important);
  5562. } else if (str_eq(prop, "min-width")) {
  5563. decl_add(out, nout, PROP_MINW,
  5564. 0, important);
  5565. decl_add(out, nout,
  5566. PROP_MINW_KIND, 3,
  5567. important);
  5568. decl_add(out, nout,
  5569. PROP_MINW_CALC_PCT,
  5570. pct, important);
  5571. decl_add(out, nout,
  5572. PROP_MINW_CALC_PX,
  5573. px, important);
  5574. } else if (str_eq(prop, "height")) {
  5575. decl_add(out, nout, PROP_HEIGHT,
  5576. 0, important);
  5577. decl_add(out, nout,
  5578. PROP_HEIGHT_KIND, 3,
  5579. important);
  5580. decl_add(out, nout,
  5581. PROP_HEIGHT_CALC_PCT,
  5582. pct, important);
  5583. decl_add(out, nout,
  5584. PROP_HEIGHT_CALC_PX,
  5585. px, important);
  5586. } else if (str_eq(prop, "max-height")) {
  5587. decl_add(out, nout, PROP_MAXH,
  5588. 0, important);
  5589. decl_add(out, nout,
  5590. PROP_MAXH_KIND, 3,
  5591. important);
  5592. decl_add(out, nout,
  5593. PROP_MAXH_CALC_PCT,
  5594. pct, important);
  5595. decl_add(out, nout,
  5596. PROP_MAXH_CALC_PX,
  5597. px, important);
  5598. } else {
  5599. decl_add(out, nout, PROP_MINH,
  5600. 0, important);
  5601. decl_add(out, nout,
  5602. PROP_MINH_KIND, 3,
  5603. important);
  5604. decl_add(out, nout,
  5605. PROP_MINH_CALC_PCT,
  5606. pct, important);
  5607. decl_add(out, nout,
  5608. PROP_MINH_CALC_PX,
  5609. px, important);
  5610. }
  5611. }
  5612. continue;
  5613. }
  5614. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  5615. if (str_eq(prop, "width")) {
  5616. decl_add(out, nout, PROP_WIDTH, v, important);
  5617. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  5618. important);
  5619. } else if (str_eq(prop, "max-width")) {
  5620. decl_add(out, nout, PROP_MAXW, v, important);
  5621. decl_add(out, nout, PROP_MAXW_KIND, kind,
  5622. important);
  5623. } else if (str_eq(prop, "min-width")) {
  5624. decl_add(out, nout, PROP_MINW, v, important);
  5625. decl_add(out, nout, PROP_MINW_KIND, kind,
  5626. important);
  5627. } else if (str_eq(prop, "max-height")) {
  5628. decl_add(out, nout, PROP_MAXH, v, important);
  5629. decl_add(out, nout, PROP_MAXH_KIND, kind,
  5630. important);
  5631. } else if (str_eq(prop, "min-height")) {
  5632. decl_add(out, nout, PROP_MINH, v, important);
  5633. decl_add(out, nout, PROP_MINH_KIND, kind,
  5634. important);
  5635. } else {
  5636. decl_add(out, nout, PROP_HEIGHT, v, important);
  5637. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  5638. important);
  5639. }
  5640. }
  5641. } else if (str_eq(prop, "content")) {
  5642. decl_add(out, nout, PROP_CONTENT,
  5643. css_collect_only ? 0 :
  5644. parse_content(d, s + vstart, vend - vstart),
  5645. important);
  5646. } else if (str_eq(prop, "aspect-ratio")) {
  5647. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  5648. * preferred height of an auto-height box. */
  5649. unsigned int q = vstart;
  5650. unsigned int a[2] = { 0, 1 }, k;
  5651. for (k = 0; k < 2; k++) {
  5652. unsigned int whole = 0, frac = 0, sc = 1;
  5653. while (q < vend && is_space(s[q]))
  5654. q++;
  5655. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  5656. s[q] != '.'))
  5657. break;
  5658. while (q < vend && s[q] >= '0' && s[q] <= '9')
  5659. whole = whole * 10 +
  5660. (unsigned int)(s[q++] - '0');
  5661. if (q < vend && s[q] == '.') {
  5662. q++;
  5663. while (q < vend && s[q] >= '0' &&
  5664. s[q] <= '9') {
  5665. if (sc < 100) {
  5666. frac = frac * 10 +
  5667. (unsigned int)(s[q] - '0');
  5668. sc *= 10;
  5669. }
  5670. q++;
  5671. }
  5672. }
  5673. a[k] = whole * 100 + frac * (100 / sc);
  5674. while (q < vend && is_space(s[q]))
  5675. q++;
  5676. if (k == 0 && (q >= vend || s[q] != '/')) {
  5677. a[1] = 100;
  5678. break;
  5679. }
  5680. q++;
  5681. }
  5682. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5683. a[0] = a[1] = 0;
  5684. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5685. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5686. } else if (str_eq(prop, "box-sizing")) {
  5687. int bb = (vend - vstart >= 10 &&
  5688. ci_eq_n(s + vstart, "border-box", 10));
  5689. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5690. } else if (str_eq(prop, "line-height")) {
  5691. unsigned short px;
  5692. unsigned char pct;
  5693. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5694. if (px) {
  5695. decl_add(out, nout, PROP_LH_PX, px, important);
  5696. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5697. } else {
  5698. decl_add(out, nout, PROP_LH_PX, 0, important);
  5699. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5700. }
  5701. }
  5702. } else if (str_eq(prop, "list-style") ||
  5703. str_eq(prop, "list-style-type")) {
  5704. int ls = BR_LIST_AUTO;
  5705. if (contains_ci_len(s + vstart, vend - vstart,
  5706. "none"))
  5707. ls = BR_LIST_NONE;
  5708. else if (contains_ci_len(s + vstart, vend - vstart,
  5709. "circle"))
  5710. ls = BR_LIST_CIRCLE;
  5711. else if (contains_ci_len(s + vstart, vend - vstart,
  5712. "square"))
  5713. ls = BR_LIST_SQUARE;
  5714. else if (contains_ci_len(s + vstart, vend - vstart,
  5715. "lower-alpha") ||
  5716. contains_ci_len(s + vstart, vend - vstart,
  5717. "lower-latin"))
  5718. ls = BR_LIST_LOWER_ALPHA;
  5719. else if (contains_ci_len(s + vstart, vend - vstart,
  5720. "upper-alpha") ||
  5721. contains_ci_len(s + vstart, vend - vstart,
  5722. "upper-latin"))
  5723. ls = BR_LIST_UPPER_ALPHA;
  5724. else if (contains_ci_len(s + vstart, vend - vstart,
  5725. "lower-roman"))
  5726. ls = BR_LIST_LOWER_ROMAN;
  5727. else if (contains_ci_len(s + vstart, vend - vstart,
  5728. "upper-roman"))
  5729. ls = BR_LIST_UPPER_ROMAN;
  5730. else if (contains_ci_len(s + vstart, vend - vstart,
  5731. "decimal"))
  5732. ls = BR_LIST_DECIMAL;
  5733. else if (contains_ci_len(s + vstart, vend - vstart,
  5734. "disc"))
  5735. ls = BR_LIST_DISC;
  5736. decl_add(out, nout, PROP_LIST_NONE,
  5737. ls == BR_LIST_NONE, important);
  5738. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5739. } else if (str_eq(prop, "border-radius") ||
  5740. str_eq(prop, "border-top-left-radius") ||
  5741. str_eq(prop, "border-top-right-radius") ||
  5742. str_eq(prop, "border-bottom-right-radius") ||
  5743. str_eq(prop, "border-bottom-left-radius")) {
  5744. unsigned short v[4];
  5745. unsigned char k[4];
  5746. unsigned int pos = 0;
  5747. int n = 0;
  5748. while (pos < vend - vstart && n < 4) {
  5749. while (pos < vend - vstart &&
  5750. is_space(s[vstart + pos]))
  5751. pos++;
  5752. if (pos >= vend - vstart ||
  5753. s[vstart + pos] == '/')
  5754. break;
  5755. if (!parse_radius_value(s + vstart,
  5756. vend - vstart, &pos,
  5757. &v[n], &k[n]))
  5758. break;
  5759. n++;
  5760. }
  5761. if (n) {
  5762. unsigned short r[4];
  5763. unsigned char rk[4];
  5764. int i;
  5765. if (n == 1) {
  5766. for (i = 0; i < 4; i++) {
  5767. r[i] = v[0];
  5768. rk[i] = k[0];
  5769. }
  5770. } else if (n == 2) {
  5771. r[0] = r[2] = v[0];
  5772. r[1] = r[3] = v[1];
  5773. rk[0] = rk[2] = k[0];
  5774. rk[1] = rk[3] = k[1];
  5775. } else if (n == 3) {
  5776. r[0] = v[0];
  5777. r[1] = r[3] = v[1];
  5778. r[2] = v[2];
  5779. rk[0] = k[0];
  5780. rk[1] = rk[3] = k[1];
  5781. rk[2] = k[2];
  5782. } else {
  5783. for (i = 0; i < 4; i++) {
  5784. r[i] = v[i];
  5785. rk[i] = k[i];
  5786. }
  5787. }
  5788. for (i = 0; i < 4; i++)
  5789. r[i] = rk[i] == 2 ? 255 :
  5790. (r[i] > 255 ? 255 : r[i]);
  5791. if (str_eq(prop, "border-top-left-radius")) {
  5792. decl_add(out, nout, PROP_RADIUS_TL,
  5793. (short)r[0], important);
  5794. } else if (str_eq(prop,
  5795. "border-top-right-radius")) {
  5796. decl_add(out, nout, PROP_RADIUS_TR,
  5797. (short)r[0], important);
  5798. } else if (str_eq(prop,
  5799. "border-bottom-right-radius")) {
  5800. decl_add(out, nout, PROP_RADIUS_BR,
  5801. (short)r[0], important);
  5802. } else if (str_eq(prop,
  5803. "border-bottom-left-radius")) {
  5804. decl_add(out, nout, PROP_RADIUS_BL,
  5805. (short)r[0], important);
  5806. } else {
  5807. decl_add(out, nout, PROP_RADIUS_TL,
  5808. (short)r[0], important);
  5809. decl_add(out, nout, PROP_RADIUS_TR,
  5810. (short)r[1], important);
  5811. decl_add(out, nout, PROP_RADIUS_BR,
  5812. (short)r[2], important);
  5813. decl_add(out, nout, PROP_RADIUS_BL,
  5814. (short)r[3], important);
  5815. }
  5816. }
  5817. } else if (str_eq(prop, "border") ||
  5818. str_eq(prop, "border-top") ||
  5819. str_eq(prop, "border-right") ||
  5820. str_eq(prop, "border-bottom") ||
  5821. str_eq(prop, "border-left") ||
  5822. str_eq(prop, "border-color") ||
  5823. str_eq(prop, "border-width") ||
  5824. str_eq(prop, "border-style") ||
  5825. str_eq(prop, "border-top-width") ||
  5826. str_eq(prop, "border-right-width") ||
  5827. str_eq(prop, "border-bottom-width") ||
  5828. str_eq(prop, "border-left-width") ||
  5829. str_eq(prop, "border-top-color") ||
  5830. str_eq(prop, "border-right-color") ||
  5831. str_eq(prop, "border-bottom-color") ||
  5832. str_eq(prop, "border-left-color") ||
  5833. str_eq(prop, "border-top-style") ||
  5834. str_eq(prop, "border-right-style") ||
  5835. str_eq(prop, "border-bottom-style") ||
  5836. str_eq(prop, "border-left-style")) {
  5837. const char *v = s + vstart;
  5838. unsigned int vl = vend - vstart;
  5839. unsigned int pos = 0, ts, te;
  5840. int widths[4], colors[4], styles[4], trans[4];
  5841. int nw = 0, nc = 0, ns = 0;
  5842. int side = -1;
  5843. int is_wl = str_eq(prop, "border-width");
  5844. int is_cl = str_eq(prop, "border-color");
  5845. int is_sl = str_eq(prop, "border-style");
  5846. int i;
  5847. if (str_eq(prop, "border-top")) side = 0;
  5848. else if (str_eq(prop, "border-right")) side = 1;
  5849. else if (str_eq(prop, "border-bottom")) side = 2;
  5850. else if (str_eq(prop, "border-left")) side = 3;
  5851. else if (str_eq(prop, "border-top-width")) {
  5852. side = 0; is_wl = 2;
  5853. } else if (str_eq(prop, "border-right-width")) {
  5854. side = 1; is_wl = 2;
  5855. } else if (str_eq(prop, "border-bottom-width")) {
  5856. side = 2; is_wl = 2;
  5857. } else if (str_eq(prop, "border-left-width")) {
  5858. side = 3; is_wl = 2;
  5859. } else if (str_eq(prop, "border-top-color")) {
  5860. side = 0; is_cl = 2;
  5861. } else if (str_eq(prop, "border-right-color")) {
  5862. side = 1; is_cl = 2;
  5863. } else if (str_eq(prop, "border-bottom-color")) {
  5864. side = 2; is_cl = 2;
  5865. } else if (str_eq(prop, "border-left-color")) {
  5866. side = 3; is_cl = 2;
  5867. } else if (str_eq(prop, "border-top-style")) {
  5868. side = 0; is_sl = 2;
  5869. } else if (str_eq(prop, "border-right-style")) {
  5870. side = 1; is_sl = 2;
  5871. } else if (str_eq(prop, "border-bottom-style")) {
  5872. side = 2; is_sl = 2;
  5873. } else if (str_eq(prop, "border-left-style")) {
  5874. side = 3; is_sl = 2;
  5875. }
  5876. if (is_wl) {
  5877. while (nw < 4 && next_token(v, vl, &pos,
  5878. &ts, &te)) {
  5879. int x;
  5880. if (!parse_len_token(v + ts, te - ts,
  5881. &x))
  5882. break;
  5883. widths[nw++] = x;
  5884. }
  5885. if (nw) {
  5886. if (is_wl == 1)
  5887. expand_four(widths, nw);
  5888. if (side >= 0 && is_wl == 2) {
  5889. decl_add(out, nout,
  5890. PROP_BW_T + side,
  5891. widths[0], important);
  5892. } else {
  5893. for (i = 0; i < 4; i++)
  5894. decl_add(out, nout,
  5895. PROP_BW_T + i,
  5896. widths[i],
  5897. important);
  5898. }
  5899. }
  5900. } else if (is_cl) {
  5901. unsigned short c;
  5902. int t;
  5903. while (nc < 4 && next_token(v, vl, &pos,
  5904. &ts, &te)) {
  5905. t = color_is_transparent(v + ts,
  5906. te - ts);
  5907. if (!t && !parse_color(v + ts,
  5908. te - ts, &c))
  5909. break;
  5910. colors[nc] = t ? 0 : c;
  5911. trans[nc] = t;
  5912. nc++;
  5913. }
  5914. if (nc) {
  5915. if (is_cl == 1) {
  5916. expand_four(colors, nc);
  5917. expand_four(trans, nc);
  5918. }
  5919. if (side >= 0 && is_cl == 2) {
  5920. decl_add(out, nout,
  5921. PROP_BC_T + side,
  5922. colors[0], important);
  5923. decl_add(out, nout,
  5924. PROP_BT_T + side,
  5925. trans[0], important);
  5926. } else {
  5927. for (i = 0; i < 4; i++) {
  5928. decl_add(out, nout,
  5929. PROP_BC_T + i,
  5930. colors[i],
  5931. important);
  5932. decl_add(out, nout,
  5933. PROP_BT_T + i,
  5934. trans[i],
  5935. important);
  5936. }
  5937. }
  5938. }
  5939. } else if (is_sl) {
  5940. while (ns < 4 && next_token(v, vl, &pos,
  5941. &ts, &te)) {
  5942. int stv;
  5943. if ((te - ts == 4 &&
  5944. ci_eq_n(v + ts, "none", 4)) ||
  5945. (te - ts == 6 &&
  5946. ci_eq_n(v + ts, "hidden", 6)))
  5947. stv = 0;
  5948. else if (te - ts == 6 &&
  5949. ci_eq_n(v + ts,
  5950. "dashed", 6))
  5951. stv = 2;
  5952. else if (te - ts == 6 &&
  5953. ci_eq_n(v + ts,
  5954. "dotted", 6))
  5955. stv = 3;
  5956. else
  5957. stv = 1;
  5958. styles[ns++] = stv;
  5959. }
  5960. if (ns) {
  5961. if (is_sl == 1)
  5962. expand_four(styles, ns);
  5963. if (is_sl == 2 && side >= 0) {
  5964. if (!styles[0])
  5965. decl_add(out, nout,
  5966. PROP_BW_T + side,
  5967. 0, important);
  5968. else
  5969. decl_add(out, nout,
  5970. PROP_BS_T + side,
  5971. styles[0] - 1,
  5972. important);
  5973. } else {
  5974. for (i = 0; i < 4; i++) {
  5975. if (!styles[i])
  5976. decl_add(out, nout,
  5977. PROP_BW_T + i,
  5978. 0,
  5979. important);
  5980. else
  5981. decl_add(out, nout,
  5982. PROP_BS_T + i,
  5983. styles[i] - 1,
  5984. important);
  5985. }
  5986. }
  5987. }
  5988. } else {
  5989. int bw = -1, bc = -1, style_none = 0;
  5990. int bstyle = -1, btrans = 0;
  5991. while (next_token(v, vl, &pos, &ts, &te)) {
  5992. unsigned short c;
  5993. int x;
  5994. if ((te - ts == 4 &&
  5995. ci_eq_n(v + ts, "none", 4)) ||
  5996. (te - ts == 6 &&
  5997. ci_eq_n(v + ts, "hidden", 6))) {
  5998. style_none = 1;
  5999. } else if (te - ts == 6 &&
  6000. ci_eq_n(v + ts, "dashed", 6)) {
  6001. bstyle = BR_BORDER_DASHED;
  6002. } else if (te - ts == 6 &&
  6003. ci_eq_n(v + ts, "dotted", 6)) {
  6004. bstyle = BR_BORDER_DOTTED;
  6005. } else if (te - ts == 5 &&
  6006. ci_eq_n(v + ts, "solid", 5)) {
  6007. bstyle = BR_BORDER_SOLID;
  6008. } else if (parse_len_token(v + ts,
  6009. te - ts, &x)) {
  6010. bw = x;
  6011. } else if (color_is_transparent(v + ts,
  6012. te - ts)) {
  6013. bc = 0;
  6014. btrans = 1;
  6015. } else if (parse_color(v + ts,
  6016. te - ts, &c)) {
  6017. bc = c;
  6018. btrans = 0;
  6019. }
  6020. }
  6021. if (style_none)
  6022. bw = 0;
  6023. if (bc >= 0 && bw < 0)
  6024. bw = 1;
  6025. if (side < 0) {
  6026. if (bw >= 0)
  6027. decl_add(out, nout,
  6028. PROP_BORDER_W, bw,
  6029. important);
  6030. if (bc >= 0)
  6031. decl_add(out, nout,
  6032. PROP_BORDER_COLOR, bc,
  6033. important);
  6034. if (bc >= 0)
  6035. for (i = 0; i < 4; i++)
  6036. decl_add(out, nout,
  6037. PROP_BT_T + i,
  6038. btrans,
  6039. important);
  6040. if (bstyle >= 0)
  6041. for (i = 0; i < 4; i++)
  6042. decl_add(out, nout,
  6043. PROP_BS_T + i,
  6044. bstyle,
  6045. important);
  6046. } else {
  6047. if (bw >= 0)
  6048. decl_add(out, nout,
  6049. PROP_BW_T + side, bw,
  6050. important);
  6051. if (bc >= 0)
  6052. decl_add(out, nout,
  6053. PROP_BC_T + side, bc,
  6054. important);
  6055. if (bc >= 0)
  6056. decl_add(out, nout,
  6057. PROP_BT_T + side,
  6058. btrans, important);
  6059. if (bstyle >= 0)
  6060. decl_add(out, nout,
  6061. PROP_BS_T + side,
  6062. bstyle, important);
  6063. }
  6064. }
  6065. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  6066. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  6067. unsigned int pos = 0;
  6068. int is_pad = prop[0] == 'p';
  6069. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  6070. while (pos < vend - vstart && nv < 4) {
  6071. units[nv] = 0;
  6072. if (parse_margin_calc(s + vstart,
  6073. vend - vstart, &pos,
  6074. &vals[nv], &cpx[nv])) {
  6075. units[nv] = 5;
  6076. } else {
  6077. vals[nv] = parse_margin_len(
  6078. s + vstart, vend - vstart,
  6079. &pos, &units[nv]);
  6080. }
  6081. nv++;
  6082. while (pos < vend - vstart &&
  6083. is_space(s[vstart + pos]))
  6084. pos++;
  6085. }
  6086. if (nv == 1) {
  6087. vals[1] = vals[2] = vals[3] = vals[0];
  6088. units[1] = units[2] = units[3] = units[0];
  6089. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  6090. } else if (nv == 2) {
  6091. vals[2] = vals[0];
  6092. vals[3] = vals[1];
  6093. units[2] = units[0];
  6094. units[3] = units[1];
  6095. cpx[2] = cpx[0];
  6096. cpx[3] = cpx[1];
  6097. } else if (nv == 3) {
  6098. vals[3] = vals[1];
  6099. units[3] = units[1];
  6100. cpx[3] = cpx[1];
  6101. }
  6102. if (nv > 0) {
  6103. int k;
  6104. for (k = 0; k < 4; k++) {
  6105. int v = vals[k];
  6106. int u = units[k];
  6107. int au = u == 4;
  6108. if (au)
  6109. v = 0;
  6110. if (is_pad && au)
  6111. u = 0;
  6112. decl_add(out, nout,
  6113. (unsigned char)(base + k), v,
  6114. important);
  6115. decl_add(out, nout,
  6116. (unsigned char)
  6117. ((is_pad ? PROP_PT_U :
  6118. PROP_MT_U) + k),
  6119. u, important);
  6120. if (u == 5)
  6121. decl_add(out, nout,
  6122. (unsigned char)
  6123. ((is_pad ? PROP_PT_CPX :
  6124. PROP_MT_CPX) + k),
  6125. cpx[k], important);
  6126. if (!is_pad && (k == 1 || k == 3))
  6127. decl_add(out, nout,
  6128. k == 1 ? PROP_MR_AUTO :
  6129. PROP_ML_AUTO,
  6130. au, important);
  6131. }
  6132. }
  6133. } else if (str_eq(prop, "margin-top") ||
  6134. str_eq(prop, "margin-right") ||
  6135. str_eq(prop, "margin-bottom") ||
  6136. str_eq(prop, "margin-left") ||
  6137. str_eq(prop, "padding-top") ||
  6138. str_eq(prop, "padding-right") ||
  6139. str_eq(prop, "padding-bottom") ||
  6140. str_eq(prop, "padding-left")) {
  6141. unsigned int pos = 0;
  6142. int unit = 0;
  6143. int v = 0, cpx = 0;
  6144. unsigned char p = PROP_MT;
  6145. const char *side;
  6146. int au;
  6147. if (parse_margin_calc(s + vstart, vend - vstart,
  6148. &pos, &v, &cpx))
  6149. unit = 5;
  6150. else
  6151. v = parse_margin_len(s + vstart,
  6152. vend - vstart, &pos,
  6153. &unit);
  6154. au = unit == 4;
  6155. if (au)
  6156. v = 0;
  6157. if (prop[0] == 'p') {
  6158. p = PROP_PT;
  6159. if (au)
  6160. unit = 0;
  6161. }
  6162. side = prop + (prop[0] == 'p' ? 8 : 7);
  6163. if (str_eq(side, "right"))
  6164. p = (unsigned char)(p + 1);
  6165. else if (str_eq(side, "bottom"))
  6166. p = (unsigned char)(p + 2);
  6167. else if (str_eq(side, "left"))
  6168. p = (unsigned char)(p + 3);
  6169. decl_add(out, nout, p, v, important);
  6170. decl_add(out, nout,
  6171. (unsigned char)((prop[0] == 'p' ?
  6172. PROP_PT_U : PROP_MT_U) +
  6173. (p - (prop[0] == 'p' ?
  6174. PROP_PT : PROP_MT))),
  6175. unit, important);
  6176. if (unit == 5)
  6177. decl_add(out, nout,
  6178. (unsigned char)((prop[0] == 'p' ?
  6179. PROP_PT_CPX : PROP_MT_CPX) +
  6180. (p - (prop[0] == 'p' ?
  6181. PROP_PT : PROP_MT))),
  6182. cpx, important);
  6183. if (str_eq(side, "right"))
  6184. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6185. else if (str_eq(side, "left"))
  6186. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6187. }
  6188. }
  6189. }
  6190. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6191. * nested blocks. This is the mobile/base rules path: media enhancements are
  6192. * deliberately ignored for now. */
  6193. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6194. unsigned int i)
  6195. {
  6196. unsigned int j = i;
  6197. while (j < len && s[j] != '{' && s[j] != ';')
  6198. j++;
  6199. if (j >= len)
  6200. return len;
  6201. if (s[j] == ';')
  6202. return j + 1;
  6203. j++;
  6204. return css_skip_nested(s, len, j);
  6205. }
  6206. static unsigned int viewport_w = 1024;
  6207. static unsigned short theme_text = 0x0000;
  6208. static unsigned short theme_bg = 0xFFFF;
  6209. static unsigned short theme_panel = 0xEF5D;
  6210. static unsigned short theme_link = 0x001B;
  6211. void br_set_theme(unsigned short text, unsigned short bg,
  6212. unsigned short panel, unsigned short link)
  6213. {
  6214. theme_text = text;
  6215. theme_bg = bg;
  6216. theme_panel = panel;
  6217. theme_link = link;
  6218. }
  6219. void br_set_viewport(unsigned int width)
  6220. {
  6221. viewport_w = width ? width : 1024;
  6222. }
  6223. static unsigned int media_len_px(const char *s, unsigned int len)
  6224. {
  6225. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6226. int em = 0;
  6227. while (i < len && is_space(s[i]))
  6228. i++;
  6229. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6230. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6231. i++;
  6232. }
  6233. if (i < len && s[i] == '.') {
  6234. i++;
  6235. /* Consume every fractional digit even when there are more
  6236. * than the three we keep: leaving the tail behind made the
  6237. * unit check miss "em" (74.9375em became 74px). */
  6238. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6239. if (scale < 1000) {
  6240. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6241. scale *= 10;
  6242. }
  6243. i++;
  6244. }
  6245. }
  6246. while (i < len && is_space(s[i]))
  6247. i++;
  6248. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6249. em = 1;
  6250. i += 2;
  6251. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6252. em = 1;
  6253. i += 3;
  6254. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6255. i += 2;
  6256. }
  6257. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6258. if (em)
  6259. return v * 16 / 100;
  6260. return v / 100;
  6261. }
  6262. /* One parenthesised media feature: min-width/max-width and the modern
  6263. * (width <= 600px) form. Unknown features are treated as true. */
  6264. static int media_feature_matches(const char *s, unsigned int len)
  6265. {
  6266. unsigned int i;
  6267. while (len && is_space(*s)) { s++; len--; }
  6268. while (len && is_space(s[len - 1])) len--;
  6269. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6270. /* There is no user-selectable colour scheme. Ignore both
  6271. * variants so a page's base palette is used; sites whose base
  6272. * is dark (like the project docs) then stay dark instead of
  6273. * flipping to their light media override. */
  6274. return 0;
  6275. }
  6276. {
  6277. /* Environment features with a known answer: a mouse-driven
  6278. * screen with normal contrast and no forced colours. Google
  6279. * puts `border:1px solid CanvasText` on its buttons inside
  6280. * `(forced-colors:active)`. */
  6281. static const struct { const char *name, *yes; } env[] = {
  6282. { "forced-colors", "none" },
  6283. { "-ms-high-contrast", "" },
  6284. { "prefers-contrast", "no-preference" },
  6285. { "inverted-colors", "none" },
  6286. { "any-hover", "hover" },
  6287. { "hover", "hover" },
  6288. { "any-pointer", "fine" },
  6289. { "pointer", "fine" },
  6290. { "prefers-reduced-motion", "no-preference" },
  6291. { "prefers-reduced-transparency", "no-preference" },
  6292. { "display-mode", "browser" },
  6293. };
  6294. unsigned int k;
  6295. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6296. unsigned int nl = s_len(env[k].name), j;
  6297. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6298. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6299. continue;
  6300. for (j = nl; j < len && s[j] != ':'; j++)
  6301. ;
  6302. if (j >= len) /* boolean form: (hover) */
  6303. return env[k].yes[0] &&
  6304. !ci_eq_n(env[k].yes, "none", 4);
  6305. j++;
  6306. while (j < len && is_space(s[j]))
  6307. j++;
  6308. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6309. ci_eq_n(s + j, env[k].yes, len - j);
  6310. }
  6311. }
  6312. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6313. for (i = 9; i < len && s[i] != ':'; i++)
  6314. ;
  6315. if (i < len)
  6316. return viewport_w >= media_len_px(s + i + 1,
  6317. len - i - 1);
  6318. return 1;
  6319. }
  6320. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6321. for (i = 9; i < len && s[i] != ':'; i++)
  6322. ;
  6323. if (i < len)
  6324. return viewport_w <= media_len_px(s + i + 1,
  6325. len - i - 1);
  6326. return 1;
  6327. }
  6328. for (i = 0; i < len; i++) {
  6329. if (s[i] == '<' || s[i] == '>') {
  6330. int max = s[i] == '<';
  6331. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6332. if (i + 1 < len && s[i + 1] == '=')
  6333. v = media_len_px(s + i + 2, len - i - 2);
  6334. return max ? viewport_w <= v : viewport_w >= v;
  6335. }
  6336. }
  6337. return 1;
  6338. }
  6339. static int media_query_matches(const char *s, unsigned int len)
  6340. {
  6341. unsigned int i;
  6342. if (contains_ci_len(s, len, "print") &&
  6343. !contains_ci_len(s, len, "screen"))
  6344. return 0;
  6345. if (contains_ci_len(s, len, "not "))
  6346. return 0;
  6347. for (i = 0; i < len; i++) {
  6348. if (s[i] == '(') {
  6349. unsigned int st = ++i;
  6350. while (i < len && s[i] != ')')
  6351. i++;
  6352. if (!media_feature_matches(s + st, i - st))
  6353. return 0;
  6354. }
  6355. }
  6356. return 1;
  6357. }
  6358. static int media_list_matches(const char *s, unsigned int len)
  6359. {
  6360. unsigned int i = 0;
  6361. while (i < len) {
  6362. unsigned int st = i;
  6363. while (i < len && s[i] != ',')
  6364. i++;
  6365. if (media_query_matches(s + st, i - st))
  6366. return 1;
  6367. i++;
  6368. }
  6369. return 0;
  6370. }
  6371. int br_media_matches(const char *query)
  6372. {
  6373. return media_list_matches(query, s_len(query));
  6374. }
  6375. static unsigned int rule_hash(const char *s, unsigned int len)
  6376. {
  6377. unsigned int h = 2166136261u, i;
  6378. for (i = 0; i < len; i++) {
  6379. char c = s[i];
  6380. if (c >= 'A' && c <= 'Z')
  6381. c = (char)(c - 'A' + 'a');
  6382. h ^= (unsigned char)c;
  6383. h *= 16777619u;
  6384. }
  6385. return h % BR_RULE_HASH;
  6386. }
  6387. /* Index a parsed rule by the most selective component of its rightmost
  6388. * compound selector. br_style_for() then only visits rules that can
  6389. * possibly match a node instead of scanning every rule. */
  6390. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6391. unsigned int len);
  6392. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6393. char *out, unsigned int cap, unsigned int *olen);
  6394. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6395. const char *key, unsigned int len)
  6396. {
  6397. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6398. * "md:flex", the class the element carries, not under "md". */
  6399. char cname[96], iname[96];
  6400. unsigned int i, clen = 0, ilen = 0;
  6401. unsigned short *head;
  6402. for (i = 0; i < len; i++) {
  6403. if (key[i] == '\\') {
  6404. i++;
  6405. continue;
  6406. }
  6407. if (key[i] == '(' || key[i] == '[') {
  6408. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6409. i = sel_skip_group(key, i, len) - 1;
  6410. continue;
  6411. }
  6412. if (key[i] == '.' || key[i] == '#') {
  6413. char tmp[96];
  6414. unsigned int n, e;
  6415. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6416. if (n && n + 1 < sizeof(tmp)) {
  6417. if (key[i] == '#') {
  6418. memcpy(iname, tmp, n);
  6419. ilen = n;
  6420. } else {
  6421. memcpy(cname, tmp, n);
  6422. clen = n;
  6423. }
  6424. }
  6425. i = e - 1;
  6426. }
  6427. }
  6428. if (ilen)
  6429. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6430. else if (clen)
  6431. head = &d->rule_bucket[rule_hash(cname, clen)];
  6432. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6433. head = &d->rule_tag[BR_TAG_HTML];
  6434. else if (len && is_ident(key[0])) {
  6435. char name[16];
  6436. unsigned int n = 0, tag;
  6437. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6438. name[n] = key[n];
  6439. n++;
  6440. }
  6441. name[n] = '\0';
  6442. tag = br_tag_from_name(name);
  6443. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6444. head = &d->rule_tag[tag];
  6445. else
  6446. head = &d->rule_any;
  6447. } else {
  6448. head = &d->rule_any;
  6449. }
  6450. d->rule_next[idx] = *head;
  6451. *head = (unsigned short)idx;
  6452. }
  6453. #define SEL_PARTS 16
  6454. struct sel_part {
  6455. unsigned short st, en;
  6456. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6457. };
  6458. static unsigned int selector_specificity(const char *s, unsigned int len);
  6459. static int css_selector_supported(const char *s, unsigned int len);
  6460. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6461. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6462. unsigned int *st, unsigned int *en);
  6463. static int css_collect_only;
  6464. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6465. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6466. * selector with any other pseudo-element. */
  6467. static int css_pseudo_element(char *sel, unsigned int *len)
  6468. {
  6469. static const struct {
  6470. const char *s;
  6471. unsigned char l, pe;
  6472. } pe[] = {
  6473. { "::before", 8, 1 }, { "::after", 7, 2 },
  6474. { ":before", 7, 1 }, { ":after", 6, 2 },
  6475. };
  6476. unsigned int k, n = *len;
  6477. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6478. unsigned int l = pe[k].l;
  6479. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6480. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6481. continue; /* matched the tail of "::x" */
  6482. n -= l;
  6483. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6484. sel[n - 1] == '+' || sel[n - 1] == '~')
  6485. sel[n++] = '*';
  6486. sel[n] = '\0';
  6487. *len = n;
  6488. return pe[k].pe;
  6489. }
  6490. }
  6491. for (k = 0; k + 1 < n; k++)
  6492. if (sel[k] == ':' && sel[k + 1] == ':')
  6493. return -1;
  6494. return 0;
  6495. }
  6496. /* Does a declaration block use or define a custom property whose value
  6497. * depends on the element (defined by some non-root rule)? Such rules are
  6498. * re-expanded per element at cascade time. */
  6499. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6500. unsigned int len)
  6501. {
  6502. unsigned char f = 0;
  6503. unsigned int i, k;
  6504. for (i = 0; i + 6 < len; i++) {
  6505. unsigned int st, e, vi;
  6506. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6507. continue;
  6508. st = i + 4;
  6509. while (st < len && is_space(s[st]))
  6510. st++;
  6511. e = st;
  6512. while (e < len && s[e] != ',' && s[e] != ')' &&
  6513. !is_space(s[e]))
  6514. e++;
  6515. vi = css_var_find(d, s + st, e - st);
  6516. if (vi < BR_MAX_VARS &&
  6517. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6518. f |= BR_RULE_USES_SCOPED;
  6519. break;
  6520. }
  6521. }
  6522. css_local_collect(s, len);
  6523. for (k = 0; k < css_nlocal; k++) {
  6524. unsigned int vi = css_var_find(d, css_local[k].name,
  6525. css_local[k].nlen);
  6526. if (vi < BR_MAX_VARS &&
  6527. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6528. f |= BR_RULE_DEFINES_SCOPED;
  6529. }
  6530. css_nlocal = 0;
  6531. return f;
  6532. }
  6533. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6534. {
  6535. unsigned int i = 0;
  6536. while (i < len && d->nrules < d->rule_cap) {
  6537. unsigned int sstart, send, bstart, bend;
  6538. struct br_decl decls[BR_DECLS];
  6539. unsigned int nd, scope_off = 0;
  6540. unsigned char scope_flags = 0;
  6541. while (i < len && is_space(s[i]))
  6542. i++;
  6543. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6544. i += 2;
  6545. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6546. i++;
  6547. i += 2;
  6548. continue;
  6549. }
  6550. if (i >= len || s[i] == '}')
  6551. break;
  6552. if (s[i] == '@') {
  6553. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6554. unsigned int q = i + 6;
  6555. while (q < len && s[q] != '{')
  6556. q++;
  6557. if (q < len &&
  6558. media_list_matches(s + i + 6,
  6559. q - (i + 6))) {
  6560. unsigned int b = q + 1;
  6561. unsigned int e = css_skip_nested(s, len, b);
  6562. if (e > b)
  6563. parse_css(d, s + b,
  6564. (e - 1) - b);
  6565. i = e;
  6566. } else {
  6567. i = skip_at_rule(s, len, i);
  6568. }
  6569. continue;
  6570. }
  6571. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6572. (i + 6 == len || is_space(s[i + 6]) ||
  6573. s[i + 6] == '{' || s[i + 6] == ';')) {
  6574. unsigned int q = i + 6, nst = q;
  6575. while (q < len && s[q] != '{' && s[q] != ';')
  6576. q++;
  6577. if (q >= len) {
  6578. i = len;
  6579. continue;
  6580. }
  6581. if (s[q] == ';') {
  6582. /* `@layer a, b;` only fixes the order. */
  6583. unsigned int st = nst;
  6584. char path[128];
  6585. while (st < q) {
  6586. unsigned int en = st;
  6587. while (en < q && s[en] != ',')
  6588. en++;
  6589. if (en > st)
  6590. css_layer_register(d, s + st,
  6591. en - st,
  6592. path,
  6593. sizeof(path));
  6594. st = en + 1;
  6595. }
  6596. i = q + 1;
  6597. continue;
  6598. }
  6599. {
  6600. unsigned int b = q + 1;
  6601. unsigned int e = css_skip_nested(s, len, b);
  6602. unsigned char saved = css_cur_layer;
  6603. char saved_name[128], path[128];
  6604. unsigned char rank;
  6605. memcpy(saved_name, css_layer_name,
  6606. sizeof(saved_name));
  6607. rank = css_layer_register(d, s + nst, q - nst,
  6608. path,
  6609. sizeof(path));
  6610. if (rank) {
  6611. css_cur_layer = rank;
  6612. memcpy(css_layer_name, path,
  6613. sizeof(css_layer_name));
  6614. }
  6615. if (e > b)
  6616. parse_css(d, s + b, (e - 1) - b);
  6617. css_cur_layer = saved;
  6618. memcpy(css_layer_name, saved_name,
  6619. sizeof(css_layer_name));
  6620. i = e;
  6621. }
  6622. continue;
  6623. }
  6624. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  6625. unsigned int q = i + 9;
  6626. while (q < len && s[q] != '{' && s[q] != ';')
  6627. q++;
  6628. if (q < len && s[q] == '{' &&
  6629. supports_matches(s + i + 9, q - (i + 9))) {
  6630. unsigned int b = q + 1;
  6631. unsigned int e = css_skip_nested(s, len, b);
  6632. if (e > b)
  6633. parse_css(d, s + b, (e - 1) - b);
  6634. i = e;
  6635. } else {
  6636. i = skip_at_rule(s, len, i);
  6637. }
  6638. continue;
  6639. }
  6640. i = skip_at_rule(s, len, i);
  6641. continue;
  6642. }
  6643. sstart = i;
  6644. while (i < len && s[i] != '{')
  6645. i++;
  6646. if (i >= len)
  6647. break;
  6648. send = i;
  6649. i++;
  6650. bstart = i;
  6651. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  6652. * to the matching close brace; stopping at the first '}' left
  6653. * the parser out of sync and dropped the rest of the sheet
  6654. * (nd-aktuell lost its desktop rules that way). */
  6655. bend = css_block_end(s, len, i);
  6656. i = bend < len ? bend + 1 : len;
  6657. while (sstart < send && is_space(s[sstart]))
  6658. sstart++;
  6659. while (send > sstart && is_space(s[send - 1]))
  6660. send--;
  6661. if (sstart == send)
  6662. continue;
  6663. if (!css_dark_selector(s + sstart, send - sstart))
  6664. css_collect_block(d, s + bstart, bend - bstart,
  6665. css_root_selector(s + sstart,
  6666. send - sstart));
  6667. if (css_collect_only)
  6668. continue;
  6669. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  6670. {
  6671. unsigned char sc = bend - bstart <= 4000 ?
  6672. css_block_scope(d, s + bstart, bend - bstart) :
  6673. 0;
  6674. scope_flags = 0;
  6675. scope_off = 0;
  6676. if (sc) {
  6677. scope_off = store_raw(d, s + bstart,
  6678. bend - bstart,
  6679. bend - bstart);
  6680. if (scope_off)
  6681. scope_flags = sc;
  6682. }
  6683. }
  6684. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  6685. continue;
  6686. /* Split a comma-separated selector list into separate rules.
  6687. * A selector the engine cannot match is dropped; the others
  6688. * in the list still apply. */
  6689. {
  6690. const char *list = s + sstart;
  6691. unsigned int ll = send - sstart, pos = 0, st, en;
  6692. while (d->nrules < d->rule_cap &&
  6693. sel_list_next(list, ll, &pos, &st, &en)) {
  6694. struct sel_part parts[SEL_PARTS];
  6695. struct br_rule *r;
  6696. char sel[160];
  6697. unsigned int k, off;
  6698. int np, pe;
  6699. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  6700. k++)
  6701. sel[k] = is_space(list[st + k]) ? ' ' :
  6702. list[st + k];
  6703. if (st + k < en)
  6704. continue; /* too long */
  6705. sel[k] = '\0';
  6706. pe = css_pseudo_element(sel, &k);
  6707. if (pe < 0 || !css_selector_supported(sel, k))
  6708. continue;
  6709. np = sel_split(sel, k, parts);
  6710. off = store_attr(d, sel, k, 159);
  6711. if (np <= 0 || !off)
  6712. continue;
  6713. r = &d->rules[d->nrules++];
  6714. r->sel_off = off;
  6715. r->key_off = store_attr(d, sel + parts[np - 1].st,
  6716. parts[np - 1].en -
  6717. parts[np - 1].st, 159);
  6718. r->spec = (unsigned short)selector_specificity(sel, k);
  6719. r->single = np == 1;
  6720. r->pe = (unsigned char)pe;
  6721. r->layer = css_cur_layer;
  6722. r->var_scope = scope_flags;
  6723. r->raw_off = scope_off;
  6724. r->raw_len = (unsigned short)(bend - bstart);
  6725. if (pe)
  6726. d->npe_rules++;
  6727. r->nd = (unsigned char)nd;
  6728. for (k = 0; k < nd; k++)
  6729. r->d[k] = decls[k];
  6730. rule_index_add(d, d->nrules - 1,
  6731. attr_ptr(d, r->key_off),
  6732. s_len(attr_ptr(d, r->key_off)));
  6733. }
  6734. }
  6735. }
  6736. }
  6737. /* Sites often set html { font-size: 62.5% } and then size everything in
  6738. * rem. Detect that root size before parsing the sheet so a rem resolves to
  6739. * the same pixels a real browser would use. */
  6740. static void detect_root_font(const char *s, unsigned int len)
  6741. {
  6742. unsigned int i;
  6743. for (i = 0; i + 4 < len; i++) {
  6744. unsigned int j, end, body, sel, q, h;
  6745. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  6746. int exact;
  6747. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  6748. s[i + 3] == 'l'))
  6749. continue;
  6750. h = i + 4;
  6751. while (h < len && is_space(s[h]))
  6752. h++;
  6753. if (h < len && s[h] != '{' && s[h] != ',' &&
  6754. s[h] != '.' && s[h] != ':' && s[h] != '[')
  6755. continue;
  6756. sel = i;
  6757. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  6758. s[sel - 1] != ';' && s[sel - 1] != ',')
  6759. sel--;
  6760. q = sel;
  6761. while (q < i && is_space(s[q]))
  6762. q++;
  6763. exact = (q == i);
  6764. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  6765. continue;
  6766. j = h;
  6767. if (j >= len || s[j] != '{')
  6768. continue;
  6769. while (j < len && s[j] != '{')
  6770. j++;
  6771. if (j >= len)
  6772. break;
  6773. end = j + 1;
  6774. body = end;
  6775. while (end < len && s[end] != '}')
  6776. end++;
  6777. for (; j + 9 < end; j++) {
  6778. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  6779. s[j + 3] == 't' && s[j + 4] == '-' &&
  6780. s[j + 5] == 's' && s[j + 6] == 'i' &&
  6781. s[j + 7] == 'z' && s[j + 8] == 'e'))
  6782. continue;
  6783. j += 9;
  6784. while (j < end && s[j] != ':')
  6785. j++;
  6786. if (j >= end)
  6787. break;
  6788. j++;
  6789. while (j < end && is_space(s[j]))
  6790. j++;
  6791. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6792. v = v * 10 + (s[j] - '0');
  6793. digits = 1;
  6794. j++;
  6795. }
  6796. if (j < end && s[j] == '.') {
  6797. j++;
  6798. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6799. v = v * 10 + (s[j] - '0');
  6800. frac++;
  6801. j++;
  6802. }
  6803. }
  6804. while (frac < 2) {
  6805. v *= 10;
  6806. frac++;
  6807. }
  6808. while (frac > 2) {
  6809. v /= 10;
  6810. frac--;
  6811. }
  6812. if (!digits)
  6813. break;
  6814. if (j < end && s[j] == '%')
  6815. pct = 1;
  6816. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  6817. px = 1;
  6818. if (pct)
  6819. v = (v * 16 + 5000) / 10000;
  6820. else if (px)
  6821. v /= 100;
  6822. else
  6823. v = v * 16 / 100;
  6824. if (exact && v >= 8 && v <= 24)
  6825. root_font_px = v;
  6826. if (exact)
  6827. return;
  6828. }
  6829. /* html { font: 10px/1 Verdana } also sets the root size. */
  6830. for (j = body; j + 5 <= end; j++) {
  6831. unsigned int p, ts, te;
  6832. if (!ci_eq_n(s + j, "font:", 5))
  6833. continue;
  6834. p = j + 5;
  6835. while (next_token(s, end, &p, &ts, &te)) {
  6836. int fv = parse_font_px(s + ts, te - ts);
  6837. if (fv >= 8 && fv <= 24) {
  6838. if (exact)
  6839. root_font_px = fv;
  6840. break;
  6841. }
  6842. }
  6843. if (exact)
  6844. return;
  6845. break;
  6846. }
  6847. }
  6848. }
  6849. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  6850. {
  6851. scan_font_faces(css, len);
  6852. detect_root_font(css, len);
  6853. css_collect_only = 1;
  6854. parse_css(d, css, len);
  6855. css_collect_only = 0;
  6856. parse_css(d, css, len);
  6857. }
  6858. void br_parse_inline_css(struct br_doc *d)
  6859. {
  6860. if (d->inline_css_len) {
  6861. scan_font_faces(css_store, d->inline_css_len);
  6862. detect_root_font(css_store, d->inline_css_len);
  6863. css_collect_only = 1;
  6864. parse_css(d, css_store, d->inline_css_len);
  6865. css_collect_only = 0;
  6866. parse_css(d, css_store, d->inline_css_len);
  6867. }
  6868. d->inline_css_len = 0;
  6869. }
  6870. void br_reset_styles(struct br_doc *d)
  6871. {
  6872. unsigned int i;
  6873. for (i = 0; i < d->nnode; i++) {
  6874. d->nodes[i].style_valid = 0;
  6875. d->nodes[i].pe_state = 0;
  6876. }
  6877. }
  6878. /* ---- selector matching ---- */
  6879. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  6880. unsigned int clen)
  6881. {
  6882. const char *s = attr_ptr(d, d->nodes[node].class_off);
  6883. unsigned int i = 0;
  6884. while (s[i]) {
  6885. unsigned int st, n;
  6886. while (s[i] && is_space(s[i]))
  6887. i++;
  6888. st = i;
  6889. while (s[i] && !is_space(s[i]))
  6890. i++;
  6891. n = i - st;
  6892. if (n == clen && ci_eq_n(s + st, cls, n))
  6893. return 1;
  6894. }
  6895. return 0;
  6896. }
  6897. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  6898. unsigned int len)
  6899. {
  6900. const char *s = attr_ptr(d, d->nodes[node].id_off);
  6901. unsigned int i;
  6902. for (i = 0; i < len; i++)
  6903. if (s[i] != id[i])
  6904. return 0;
  6905. return s[len] == '\0';
  6906. }
  6907. static int is_ident(char c)
  6908. {
  6909. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  6910. (c >= '0' && c <= '9') || c == '-' || c == '_';
  6911. }
  6912. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  6913. {
  6914. unsigned int n = 0;
  6915. while (lit[n])
  6916. n++;
  6917. return n == nlen && ci_eq_n(name, lit, nlen);
  6918. }
  6919. static int attr_present(struct br_doc *d, unsigned int node,
  6920. const char *name, unsigned int nlen)
  6921. {
  6922. struct br_node *n = &d->nodes[node];
  6923. if (attr_is(name, nlen, "data-collapse-target"))
  6924. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  6925. if (attr_is(name, nlen, "data-collapse-trigger"))
  6926. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  6927. if (attr_is(name, nlen, "is-open"))
  6928. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  6929. if (attr_is(name, nlen, "disabled"))
  6930. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  6931. if (attr_is(name, nlen, "open"))
  6932. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  6933. if (attr_is(name, nlen, "inert"))
  6934. return (n->attr_flags & BR_ATTR_INERT) != 0;
  6935. if (attr_is(name, nlen, "hidden"))
  6936. return (n->flags & BR_NODE_HIDDEN) != 0;
  6937. if (attr_is(name, nlen, "href"))
  6938. return attr_ptr(d, n->href_off)[0] != 0;
  6939. if (attr_is(name, nlen, "class"))
  6940. return attr_ptr(d, n->class_off)[0] != 0;
  6941. if (attr_is(name, nlen, "id"))
  6942. return attr_ptr(d, n->id_off)[0] != 0;
  6943. if (attr_is(name, nlen, "src"))
  6944. return attr_ptr(d, n->src_off)[0] != 0;
  6945. if (attr_is(name, nlen, "alt"))
  6946. return attr_ptr(d, n->alt_off)[0] != 0;
  6947. if (attr_is(name, nlen, "rel"))
  6948. return attr_ptr(d, n->rel_off)[0] != 0;
  6949. return 0;
  6950. }
  6951. /* ---- Selector engine ----
  6952. *
  6953. * One matcher serves the cascade (rule_try), querySelector(All) and
  6954. * Element.matches. A compound is an optional type/universal selector
  6955. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  6956. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  6957. * '~' combinators and is matched right to left with backtracking.
  6958. *
  6959. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  6960. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  6961. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  6962. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  6963. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  6964. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  6965. * and pseudo-elements never match an element; the cascade strips
  6966. * ::before/::after before it gets here. */
  6967. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  6968. unsigned int len);
  6969. /* i is at '(' or '['; returns the index just past the matching close. */
  6970. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6971. unsigned int len)
  6972. {
  6973. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  6974. int depth = 0;
  6975. for (; i < len; i++) {
  6976. char c = s[i];
  6977. if (quote) {
  6978. if (c == '\\')
  6979. i++;
  6980. else if (c == quote)
  6981. quote = 0;
  6982. continue;
  6983. }
  6984. if (c == '\\')
  6985. i++;
  6986. else if (c == '"' || c == '\'')
  6987. quote = c;
  6988. else if (c == open)
  6989. depth++;
  6990. else if (c == close && --depth == 0)
  6991. return i + 1;
  6992. }
  6993. return len;
  6994. }
  6995. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  6996. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6997. char *out, unsigned int cap, unsigned int *olen)
  6998. {
  6999. unsigned int n = 0;
  7000. while (i < len) {
  7001. char c = s[i];
  7002. if (c == '\\' && i + 1 < len) {
  7003. c = s[i + 1];
  7004. i += 2;
  7005. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  7006. i++;
  7007. } else {
  7008. break;
  7009. }
  7010. if (n + 1 < cap)
  7011. out[n++] = c;
  7012. }
  7013. out[n] = '\0';
  7014. *olen = n;
  7015. return i;
  7016. }
  7017. /* Next top-level item of a comma-separated selector list. */
  7018. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  7019. unsigned int *st, unsigned int *en)
  7020. {
  7021. unsigned int i = *pos;
  7022. while (i < len && (is_space(s[i]) || s[i] == ','))
  7023. i++;
  7024. if (i >= len)
  7025. return 0;
  7026. *st = i;
  7027. while (i < len && s[i] != ',') {
  7028. if (s[i] == '(' || s[i] == '[')
  7029. i = sel_skip_group(s, i, len);
  7030. else if (s[i] == '\\' && i + 1 < len)
  7031. i += 2;
  7032. else if (s[i] == '"' || s[i] == '\'') {
  7033. char q = s[i++];
  7034. while (i < len && s[i] != q)
  7035. i++;
  7036. i++;
  7037. } else
  7038. i++;
  7039. }
  7040. if (i > len)
  7041. i = len;
  7042. *en = i;
  7043. while (*en > *st && is_space(s[*en - 1]))
  7044. (*en)--;
  7045. *pos = i;
  7046. return 1;
  7047. }
  7048. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  7049. unsigned int len)
  7050. {
  7051. unsigned int pos = 0, st, en;
  7052. while (sel_list_next(s, len, &pos, &st, &en))
  7053. if (sel_match(d, node, s + st, en - st))
  7054. return 1;
  7055. return 0;
  7056. }
  7057. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  7058. {
  7059. if (d->nodes[a].tag != d->nodes[b].tag)
  7060. return 0;
  7061. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  7062. return 1;
  7063. return same_string(attr_ptr(d, d->nodes[a].name_off),
  7064. attr_ptr(d, d->nodes[b].name_off));
  7065. }
  7066. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  7067. {
  7068. unsigned int c, prev = 0;
  7069. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  7070. c = d->nodes[c].next)
  7071. if (d->nodes[c].type == BR_NODE_ELEM)
  7072. prev = c;
  7073. return c == node ? prev : 0;
  7074. }
  7075. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  7076. {
  7077. unsigned int c;
  7078. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  7079. if (d->nodes[c].type == BR_NODE_ELEM)
  7080. return c;
  7081. return 0;
  7082. }
  7083. /* 1-based position among the element siblings (of the same type), counted
  7084. * from the start or, with from_end, from the end. */
  7085. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  7086. int from_end)
  7087. {
  7088. unsigned int c;
  7089. int pos = 0, total = 0;
  7090. for (c = d->nodes[d->nodes[node].parent].first; c;
  7091. c = d->nodes[c].next) {
  7092. if (d->nodes[c].type != BR_NODE_ELEM)
  7093. continue;
  7094. if (of_type && !sel_same_type(d, c, node))
  7095. continue;
  7096. total++;
  7097. if (c == node)
  7098. pos = total;
  7099. }
  7100. if (!pos)
  7101. return 0;
  7102. return from_end ? total - pos + 1 : pos;
  7103. }
  7104. /* an+b, odd, even. "of S" is not supported (returns 0). */
  7105. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  7106. {
  7107. unsigned int i = 0;
  7108. int sign = 1, num = 0, digits = 0;
  7109. while (len && is_space(s[len - 1]))
  7110. len--;
  7111. while (i < len && is_space(s[i]))
  7112. i++;
  7113. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7114. *a = 2;
  7115. *b = 1;
  7116. return 1;
  7117. }
  7118. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7119. *a = 2;
  7120. *b = 0;
  7121. return 1;
  7122. }
  7123. if (i < len && (s[i] == '+' || s[i] == '-'))
  7124. sign = s[i++] == '-' ? -1 : 1;
  7125. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7126. num = num * 10 + (s[i++] - '0');
  7127. digits = 1;
  7128. }
  7129. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7130. *a = sign * (digits ? num : 1);
  7131. *b = 0;
  7132. i++;
  7133. while (i < len && is_space(s[i]))
  7134. i++;
  7135. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7136. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7137. while (i < len && is_space(s[i]))
  7138. i++;
  7139. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7140. bv = bv * 10 + (s[i++] - '0');
  7141. bd = 1;
  7142. }
  7143. if (!bd)
  7144. return 0;
  7145. *b = bs * bv;
  7146. }
  7147. } else {
  7148. if (!digits)
  7149. return 0;
  7150. *a = 0;
  7151. *b = sign * num;
  7152. }
  7153. return i == len;
  7154. }
  7155. static int sel_nth_ok(int a, int b, int pos)
  7156. {
  7157. if (pos <= 0)
  7158. return 0;
  7159. if (!a)
  7160. return pos == b;
  7161. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7162. }
  7163. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7164. const char *name, int op, const char *want,
  7165. unsigned int wl, int icase)
  7166. {
  7167. const char *have = br_find_attr(d, node, name);
  7168. unsigned int n, i;
  7169. if (!have) {
  7170. /* Boolean attributes the parser keeps as flags. */
  7171. if (!attr_present(d, node, name, text_len(name)))
  7172. return 0;
  7173. have = "";
  7174. }
  7175. if (!op)
  7176. return 1;
  7177. n = text_len(have);
  7178. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7179. switch (op) {
  7180. case '=':
  7181. return n == wl && SEL_EQ(have, want, wl);
  7182. case '^':
  7183. return wl && n >= wl && SEL_EQ(have, want, wl);
  7184. case '$':
  7185. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7186. case '*':
  7187. if (!wl)
  7188. return 0;
  7189. for (i = 0; i + wl <= n; i++)
  7190. if (SEL_EQ(have + i, want, wl))
  7191. return 1;
  7192. return 0;
  7193. case '|':
  7194. return n >= wl && SEL_EQ(have, want, wl) &&
  7195. (n == wl || have[wl] == '-');
  7196. case '~':
  7197. if (!wl)
  7198. return 0;
  7199. for (i = 0; i < n; ) {
  7200. unsigned int st;
  7201. while (i < n && is_space(have[i]))
  7202. i++;
  7203. st = i;
  7204. while (i < n && !is_space(have[i]))
  7205. i++;
  7206. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7207. return 1;
  7208. }
  7209. return 0;
  7210. }
  7211. #undef SEL_EQ
  7212. return 0;
  7213. }
  7214. static int sel_empty(struct br_doc *d, unsigned int node)
  7215. {
  7216. unsigned int c;
  7217. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7218. if (d->nodes[c].type == BR_NODE_ELEM)
  7219. return 0;
  7220. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7221. return 0;
  7222. }
  7223. return 1;
  7224. }
  7225. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7226. unsigned int len)
  7227. {
  7228. unsigned int c;
  7229. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7230. if (d->nodes[c].type != BR_NODE_ELEM)
  7231. continue;
  7232. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7233. return 1;
  7234. }
  7235. return 0;
  7236. }
  7237. /* :has() with an optional leading relative combinator per list item. */
  7238. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7239. unsigned int len)
  7240. {
  7241. unsigned int pos = 0, st, en, c;
  7242. while (sel_list_next(s, len, &pos, &st, &en)) {
  7243. char comb = s[st];
  7244. if (comb == '>' || comb == '+' || comb == '~') {
  7245. st++;
  7246. while (st < en && is_space(s[st]))
  7247. st++;
  7248. } else {
  7249. comb = ' ';
  7250. }
  7251. if (st >= en)
  7252. continue;
  7253. if (comb == '>') {
  7254. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7255. if (d->nodes[c].type == BR_NODE_ELEM &&
  7256. sel_match(d, c, s + st, en - st))
  7257. return 1;
  7258. } else if (comb == '+') {
  7259. c = sel_next_elem(d, node);
  7260. if (c && sel_match(d, c, s + st, en - st))
  7261. return 1;
  7262. } else if (comb == '~') {
  7263. for (c = sel_next_elem(d, node); c;
  7264. c = sel_next_elem(d, c))
  7265. if (sel_match(d, c, s + st, en - st))
  7266. return 1;
  7267. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7268. return 1;
  7269. }
  7270. }
  7271. return 0;
  7272. }
  7273. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7274. {
  7275. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7276. }
  7277. /* Pseudo-class names the engine models; anything else never matches and
  7278. * makes a top-level rule unsupported (see css_selector_supported()). */
  7279. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7280. {
  7281. static const char *const plain[] = {
  7282. "root", "link", "any-link", "visited", "first-child",
  7283. "last-child", "only-child", "first-of-type", "last-of-type",
  7284. "only-of-type", "empty", "checked", "disabled", "enabled",
  7285. "required", "optional",
  7286. };
  7287. static const char *const func[] = {
  7288. "not", "is", "where", "matches", "-webkit-any", "has",
  7289. "nth-child", "nth-last-child", "nth-of-type",
  7290. "nth-last-of-type",
  7291. };
  7292. unsigned int k;
  7293. if (fn) {
  7294. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7295. if (sel_name_is(name, nl, func[k]))
  7296. return 1;
  7297. return 0;
  7298. }
  7299. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7300. if (sel_name_is(name, nl, plain[k]))
  7301. return 1;
  7302. return 0;
  7303. }
  7304. static int sel_form_control(struct br_doc *d, unsigned int node)
  7305. {
  7306. unsigned int t = d->nodes[node].tag;
  7307. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7308. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7309. t == BR_TAG_OPTION;
  7310. }
  7311. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7312. unsigned int nl, const char *arg, unsigned int al,
  7313. int fn)
  7314. {
  7315. struct br_node *n = &d->nodes[node];
  7316. int a, b;
  7317. if (fn) {
  7318. if (sel_name_is(name, nl, "not"))
  7319. return !sel_list_match(d, node, arg, al);
  7320. if (sel_name_is(name, nl, "is") ||
  7321. sel_name_is(name, nl, "where") ||
  7322. sel_name_is(name, nl, "matches") ||
  7323. sel_name_is(name, nl, "-webkit-any"))
  7324. return sel_list_match(d, node, arg, al);
  7325. if (sel_name_is(name, nl, "has"))
  7326. return sel_has(d, node, arg, al);
  7327. if (!sel_nth_parse(arg, al, &a, &b))
  7328. return 0;
  7329. if (sel_name_is(name, nl, "nth-child"))
  7330. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7331. if (sel_name_is(name, nl, "nth-last-child"))
  7332. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7333. if (sel_name_is(name, nl, "nth-of-type"))
  7334. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7335. if (sel_name_is(name, nl, "nth-last-of-type"))
  7336. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7337. return 0;
  7338. }
  7339. if (sel_name_is(name, nl, "root"))
  7340. return n->tag == BR_TAG_HTML;
  7341. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7342. /* No history state: every link is unvisited. */
  7343. return n->tag == BR_TAG_A && n->href_off;
  7344. if (sel_name_is(name, nl, "first-child"))
  7345. return sel_sibling_pos(d, node, 0, 0) == 1;
  7346. if (sel_name_is(name, nl, "last-child"))
  7347. return sel_sibling_pos(d, node, 0, 1) == 1;
  7348. if (sel_name_is(name, nl, "only-child"))
  7349. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7350. sel_sibling_pos(d, node, 0, 1) == 1;
  7351. if (sel_name_is(name, nl, "first-of-type"))
  7352. return sel_sibling_pos(d, node, 1, 0) == 1;
  7353. if (sel_name_is(name, nl, "last-of-type"))
  7354. return sel_sibling_pos(d, node, 1, 1) == 1;
  7355. if (sel_name_is(name, nl, "only-of-type"))
  7356. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7357. sel_sibling_pos(d, node, 1, 1) == 1;
  7358. if (sel_name_is(name, nl, "empty"))
  7359. return sel_empty(d, node);
  7360. if (sel_name_is(name, nl, "checked"))
  7361. return br_find_attr(d, node, "checked") != 0 ||
  7362. (n->tag == BR_TAG_OPTION &&
  7363. br_find_attr(d, node, "selected") != 0);
  7364. if (sel_name_is(name, nl, "disabled"))
  7365. return sel_form_control(d, node) &&
  7366. (n->attr_flags & BR_ATTR_DISABLED);
  7367. if (sel_name_is(name, nl, "enabled"))
  7368. return sel_form_control(d, node) &&
  7369. !(n->attr_flags & BR_ATTR_DISABLED);
  7370. if (sel_name_is(name, nl, "required"))
  7371. return sel_form_control(d, node) &&
  7372. br_find_attr(d, node, "required") != 0;
  7373. if (sel_name_is(name, nl, "optional"))
  7374. return sel_form_control(d, node) &&
  7375. !br_find_attr(d, node, "required");
  7376. return 0;
  7377. }
  7378. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7379. unsigned int len)
  7380. {
  7381. char buf[64];
  7382. unsigned int i = 0, bl;
  7383. if (d->nodes[node].type != BR_NODE_ELEM)
  7384. return 0;
  7385. if (i < len && s[i] == '*') {
  7386. i++;
  7387. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7388. unsigned int tag;
  7389. i = sel_ident(s, i, len, buf, 32, &bl);
  7390. tag = br_tag_from_name(buf);
  7391. if (tag == BR_TAG_UNKNOWN) {
  7392. const char *raw = attr_ptr(d,
  7393. d->nodes[node].name_off);
  7394. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7395. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7396. return 0;
  7397. } else if (tag != d->nodes[node].tag) {
  7398. return 0;
  7399. }
  7400. }
  7401. if (i >= len)
  7402. return i > 0;
  7403. while (i < len) {
  7404. char c = s[i];
  7405. if (c == '.' || c == '#') {
  7406. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7407. if (!bl)
  7408. return 0;
  7409. if (c == '.' ? !class_has(d, node, buf, bl) :
  7410. !id_eq(d, node, buf, bl))
  7411. return 0;
  7412. } else if (c == '[') {
  7413. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7414. unsigned int vs = 0, vl = 0, k;
  7415. char name[32];
  7416. int op = 0, icase = 0;
  7417. while (j < e && is_space(s[j]))
  7418. j++;
  7419. k = 0;
  7420. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7421. k + 1 < sizeof(name)) {
  7422. char ch = s[j++];
  7423. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7424. (char)(ch + 32) : ch;
  7425. }
  7426. name[k] = '\0';
  7427. if (!k)
  7428. return 0;
  7429. while (j < e && is_space(s[j]))
  7430. j++;
  7431. if (j < e && s[j] == '=') {
  7432. op = '=';
  7433. j++;
  7434. } else if (j + 1 < e && s[j + 1] == '=' &&
  7435. (s[j] == '~' || s[j] == '|' ||
  7436. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7437. op = s[j];
  7438. j += 2;
  7439. }
  7440. if (op) {
  7441. while (j < e && is_space(s[j]))
  7442. j++;
  7443. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7444. char q = s[j++];
  7445. vs = j;
  7446. while (j < e && s[j] != q)
  7447. j++;
  7448. vl = j - vs;
  7449. j++;
  7450. } else {
  7451. vs = j;
  7452. while (j < e && s[j] != ']' &&
  7453. !is_space(s[j]))
  7454. j++;
  7455. vl = j - vs;
  7456. }
  7457. while (j < e && is_space(s[j]))
  7458. j++;
  7459. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7460. icase = 1;
  7461. }
  7462. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7463. icase))
  7464. return 0;
  7465. i = e;
  7466. } else if (c == ':') {
  7467. unsigned int st, nl, as = 0, al = 0;
  7468. int fn = 0;
  7469. if (i + 1 < len && s[i + 1] == ':')
  7470. return 0; /* pseudo-element */
  7471. st = ++i;
  7472. while (i < len && is_ident(s[i]))
  7473. i++;
  7474. nl = i - st;
  7475. if (i < len && s[i] == '(') {
  7476. unsigned int e = sel_skip_group(s, i, len);
  7477. fn = 1;
  7478. as = i + 1;
  7479. al = e > as ? e - as - 1 : 0;
  7480. i = e;
  7481. }
  7482. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7483. return 0;
  7484. } else {
  7485. return 0;
  7486. }
  7487. }
  7488. return 1;
  7489. }
  7490. /* Split a complex selector into compounds; returns the count or -1. */
  7491. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7492. {
  7493. unsigned int i = 0;
  7494. int n = 0;
  7495. char comb = 0;
  7496. while (i < len && is_space(s[i]))
  7497. i++;
  7498. while (i < len) {
  7499. unsigned int st = i;
  7500. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7501. s[i] != '+' && s[i] != '~') {
  7502. if (s[i] == '(' || s[i] == '[')
  7503. i = sel_skip_group(s, i, len);
  7504. else if (s[i] == '\\' && i + 1 < len)
  7505. i += 2;
  7506. else
  7507. i++;
  7508. }
  7509. if (i == st || n == SEL_PARTS)
  7510. return -1;
  7511. p[n].st = (unsigned short)st;
  7512. p[n].en = (unsigned short)(i > len ? len : i);
  7513. p[n].comb = comb;
  7514. n++;
  7515. comb = 0;
  7516. while (i < len && is_space(s[i])) {
  7517. comb = ' ';
  7518. i++;
  7519. }
  7520. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7521. comb = s[i++];
  7522. while (i < len && is_space(s[i]))
  7523. i++;
  7524. if (i >= len)
  7525. return -1;
  7526. }
  7527. }
  7528. return n;
  7529. }
  7530. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7531. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7532. * parent when the page starts with <html> (node 0 is that element; see
  7533. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7534. static int sel_parent(struct br_doc *d, unsigned int node)
  7535. {
  7536. unsigned int p = d->nodes[node].parent;
  7537. if (p)
  7538. return (int)p;
  7539. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7540. d->nodes[0].tag == BR_TAG_HTML &&
  7541. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7542. return 0;
  7543. return -1;
  7544. }
  7545. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7546. const struct sel_part *p, int k)
  7547. {
  7548. unsigned int c, pl;
  7549. const char *ps;
  7550. if (k == 0)
  7551. return 1;
  7552. ps = s + p[k - 1].st;
  7553. pl = p[k - 1].en - p[k - 1].st;
  7554. switch (p[k].comb) {
  7555. case '>': {
  7556. int pc = sel_parent(d, node);
  7557. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7558. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7559. }
  7560. case '+':
  7561. c = sel_prev_elem(d, node);
  7562. return c && compound_match(d, c, ps, pl) &&
  7563. sel_from(d, c, s, p, k - 1);
  7564. case '~':
  7565. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7566. if (compound_match(d, c, ps, pl) &&
  7567. sel_from(d, c, s, p, k - 1))
  7568. return 1;
  7569. return 0;
  7570. default: {
  7571. int pc;
  7572. for (pc = sel_parent(d, node); pc >= 0;
  7573. pc = sel_parent(d, (unsigned int)pc))
  7574. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7575. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7576. return 1;
  7577. return 0;
  7578. }
  7579. }
  7580. }
  7581. /* Match one complex selector. right_done: the caller already matched the
  7582. * rightmost compound (the rule index key). */
  7583. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7584. unsigned int len, int right_done)
  7585. {
  7586. struct sel_part parts[SEL_PARTS];
  7587. int np = sel_split(s, len, parts);
  7588. if (np <= 0)
  7589. return 0;
  7590. if (!right_done &&
  7591. !compound_match(d, node, s + parts[np - 1].st,
  7592. parts[np - 1].en - parts[np - 1].st))
  7593. return 0;
  7594. return sel_from(d, node, s, parts, np - 1);
  7595. }
  7596. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7597. unsigned int len)
  7598. {
  7599. return selector_match(d, node, s, len, 0);
  7600. }
  7601. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7602. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7603. * and :where counts nothing. */
  7604. static unsigned int selector_specificity(const char *s, unsigned int len)
  7605. {
  7606. unsigned int i = 0, spec = 0, bl;
  7607. char buf[64];
  7608. while (i < len) {
  7609. char c = s[i];
  7610. if (c == '#' || c == '.') {
  7611. spec += c == '#' ? 100 : 10;
  7612. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7613. } else if (c == '[') {
  7614. spec += 10;
  7615. i = sel_skip_group(s, i, len);
  7616. } else if (c == ':') {
  7617. unsigned int st, nl;
  7618. if (i + 1 < len && s[i + 1] == ':') {
  7619. spec += 1;
  7620. i += 2;
  7621. while (i < len && is_ident(s[i]))
  7622. i++;
  7623. continue;
  7624. }
  7625. st = ++i;
  7626. while (i < len && is_ident(s[i]))
  7627. i++;
  7628. nl = i - st;
  7629. if (i < len && s[i] == '(') {
  7630. unsigned int e = sel_skip_group(s, i, len);
  7631. if (sel_name_is(s + st, nl, "where")) {
  7632. /* zero */
  7633. } else if (sel_name_is(s + st, nl, "not") ||
  7634. sel_name_is(s + st, nl, "is") ||
  7635. sel_name_is(s + st, nl, "has") ||
  7636. sel_name_is(s + st, nl, "matches") ||
  7637. sel_name_is(s + st, nl,
  7638. "-webkit-any")) {
  7639. unsigned int pos = 0, as, ae, best = 0;
  7640. unsigned int al = e > i + 1 ?
  7641. e - i - 2 : 0;
  7642. while (sel_list_next(s + i + 1, al,
  7643. &pos, &as, &ae)) {
  7644. unsigned int v =
  7645. selector_specificity(
  7646. s + i + 1 + as,
  7647. ae - as);
  7648. if (v > best)
  7649. best = v;
  7650. }
  7651. spec += best;
  7652. } else {
  7653. spec += 10;
  7654. }
  7655. i = e;
  7656. } else if (sel_name_is(s + st, nl, "before") ||
  7657. sel_name_is(s + st, nl, "after")) {
  7658. spec += 1;
  7659. } else {
  7660. spec += 10;
  7661. }
  7662. } else if (is_ident(c) || c == '\\') {
  7663. spec += 1;
  7664. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  7665. } else {
  7666. i++;
  7667. }
  7668. }
  7669. return spec;
  7670. }
  7671. /* Cascade filter: drop a selector the engine cannot match (user-state
  7672. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  7673. * ones the caller handles), so it does not waste a rule slot. Arguments of
  7674. * functional pseudo-classes are not checked: an unknown pseudo-class there
  7675. * is simply false, which is right for :not(:hover). */
  7676. static int css_selector_supported(const char *s, unsigned int len)
  7677. {
  7678. struct sel_part parts[SEL_PARTS];
  7679. unsigned int i = 0;
  7680. if (sel_split(s, len, parts) <= 0)
  7681. return 0;
  7682. while (i < len) {
  7683. if (s[i] == '(' || s[i] == '[') {
  7684. i = sel_skip_group(s, i, len);
  7685. } else if (s[i] == '\\') {
  7686. i += 2;
  7687. } else if (s[i] == ':') {
  7688. unsigned int st, nl;
  7689. int fn;
  7690. if (i + 1 < len && s[i + 1] == ':')
  7691. return 0;
  7692. st = ++i;
  7693. while (i < len && is_ident(s[i]))
  7694. i++;
  7695. nl = i - st;
  7696. fn = i < len && s[i] == '(';
  7697. if (!sel_pseudo_known(s + st, nl, fn))
  7698. return 0;
  7699. } else {
  7700. i++;
  7701. }
  7702. }
  7703. return 1;
  7704. }
  7705. #define RULE_MATCH_MAX 512
  7706. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  7707. static int cascade_pe;
  7708. struct rule_match {
  7709. unsigned short idx;
  7710. short spec; /* precedence of normal declarations */
  7711. short spec_imp; /* precedence of !important declarations */
  7712. };
  7713. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  7714. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  7715. * unlayered rules, then the style attribute. !important reverses the
  7716. * layer order: unlayered lowest, the first layer highest, the style
  7717. * attribute above all. Both fit a short. */
  7718. #define CASCADE_BAND 512
  7719. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  7720. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  7721. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  7722. {
  7723. unsigned int band;
  7724. if (spec >= CASCADE_BAND)
  7725. spec = CASCADE_BAND - 1;
  7726. if (important)
  7727. band = layer ? CASCADE_UNLAYERED - layer : 0;
  7728. else
  7729. band = layer ? layer : CASCADE_UNLAYERED;
  7730. return (int)(band * CASCADE_BAND + spec);
  7731. }
  7732. static struct rule_match rule_matches[RULE_MATCH_MAX];
  7733. static unsigned int nrule_matches;
  7734. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  7735. {
  7736. struct br_rule *r = &d->rules[idx];
  7737. const char *key = attr_ptr(d, r->key_off);
  7738. int spec, key_ok = 0;
  7739. if (r->pe != cascade_pe)
  7740. return;
  7741. if (key[0]) {
  7742. if (!compound_match(d, node, key, s_len(key)))
  7743. return;
  7744. key_ok = 1;
  7745. }
  7746. if (!(r->single && key_ok) &&
  7747. !selector_match(d, node, attr_ptr(d, r->sel_off),
  7748. s_len(attr_ptr(d, r->sel_off)), key_ok))
  7749. return;
  7750. spec = (int)r->spec;
  7751. if (nrule_matches < RULE_MATCH_MAX) {
  7752. rule_matches[nrule_matches].idx = (unsigned short)idx;
  7753. rule_matches[nrule_matches].spec =
  7754. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  7755. rule_matches[nrule_matches].spec_imp =
  7756. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  7757. nrule_matches++;
  7758. }
  7759. }
  7760. static void rule_scan_list(struct br_doc *d, unsigned int node,
  7761. unsigned short head)
  7762. {
  7763. while (head != 0xFFFF) {
  7764. rule_try(d, node, head);
  7765. head = d->rule_next[head];
  7766. }
  7767. }
  7768. static void rule_scan_class(struct br_doc *d, unsigned int node,
  7769. const char *cls, unsigned int len)
  7770. {
  7771. if (len)
  7772. rule_scan_list(d, node,
  7773. d->rule_bucket[rule_hash(cls, len)]);
  7774. }
  7775. static void rule_collect(struct br_doc *d, unsigned int node)
  7776. {
  7777. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  7778. const char *id = attr_ptr(d, d->nodes[node].id_off);
  7779. unsigned int tag = d->nodes[node].tag;
  7780. unsigned int i;
  7781. nrule_matches = 0;
  7782. rule_scan_list(d, node, d->rule_any);
  7783. if (tag < BR_TAG_MAX)
  7784. rule_scan_list(d, node, d->rule_tag[tag]);
  7785. if (id[0])
  7786. rule_scan_list(d, node,
  7787. d->rule_bucket[rule_hash(id, s_len(id))]);
  7788. i = 0;
  7789. while (cls[i]) {
  7790. unsigned int st, n;
  7791. while (cls[i] && is_space(cls[i]))
  7792. i++;
  7793. st = i;
  7794. while (cls[i] && !is_space(cls[i]))
  7795. i++;
  7796. n = i - st;
  7797. if (n)
  7798. rule_scan_class(d, node, cls + st, n);
  7799. }
  7800. /* Rules must cascade in document order even though the index visits
  7801. * them in bucket order. */
  7802. for (i = 1; i < nrule_matches; i++) {
  7803. struct rule_match m = rule_matches[i];
  7804. unsigned int j = i;
  7805. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  7806. rule_matches[j] = rule_matches[j - 1];
  7807. j--;
  7808. }
  7809. rule_matches[j] = m;
  7810. }
  7811. }
  7812. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  7813. int font_px)
  7814. {
  7815. if (*kind == 4) {
  7816. *val = (unsigned short)(*val * font_px / 100);
  7817. *kind = 1;
  7818. } else if (*kind == 5) {
  7819. *val = (unsigned short)(*val * root_font_px / 100);
  7820. *kind = 1;
  7821. }
  7822. }
  7823. static void resolve_size_units(struct br_style *st)
  7824. {
  7825. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  7826. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  7827. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  7828. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  7829. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  7830. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  7831. }
  7832. static void set_border_side_w(struct br_style *st, int side, int v)
  7833. {
  7834. if (v < 0)
  7835. v = 0;
  7836. if (v > 32)
  7837. v = 32;
  7838. st->border_w[side] = (unsigned char)v;
  7839. if (st->border_w[side] && !st->border_c[side])
  7840. st->border_c[side] = st->color;
  7841. }
  7842. static void set_border_side_c(struct br_style *st, int side, int v)
  7843. {
  7844. st->border_c[side] = (unsigned short)v;
  7845. }
  7846. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  7847. {
  7848. switch (dcl->prop) {
  7849. case PROP_COLOR:
  7850. st->color = (unsigned short)dcl->val;
  7851. break;
  7852. case PROP_BG:
  7853. st->bg = (unsigned short)dcl->val;
  7854. st->has_bg = 1;
  7855. st->bg_set = 1;
  7856. break;
  7857. case PROP_BG_NONE:
  7858. st->has_bg = 0;
  7859. st->bg_set = 1;
  7860. break;
  7861. case PROP_BG_IMAGE:
  7862. st->bg_image = (unsigned short)dcl->val;
  7863. break;
  7864. case PROP_BG_REPEAT:
  7865. st->bg_repeat = (unsigned char)dcl->val;
  7866. break;
  7867. case PROP_BG_POS_X_KIND:
  7868. st->bg_pos_x_kind = (unsigned char)dcl->val;
  7869. break;
  7870. case PROP_BG_POS_X:
  7871. st->bg_pos_x = (signed short)dcl->val;
  7872. break;
  7873. case PROP_BG_POS_Y_KIND:
  7874. st->bg_pos_y_kind = (unsigned char)dcl->val;
  7875. break;
  7876. case PROP_BG_POS_Y:
  7877. st->bg_pos_y = (signed short)dcl->val;
  7878. break;
  7879. case PROP_BG_SIZE_KIND:
  7880. st->bg_size_kind = (unsigned char)dcl->val;
  7881. break;
  7882. case PROP_BG_SIZE_W:
  7883. st->bg_size_w = (unsigned short)dcl->val;
  7884. break;
  7885. case PROP_BG_SIZE_H:
  7886. st->bg_size_h = (unsigned short)dcl->val;
  7887. break;
  7888. case PROP_FONT_PX:
  7889. st->font_raw = (unsigned short)dcl->val;
  7890. break;
  7891. case PROP_FONT_UNIT:
  7892. st->font_unit = (unsigned char)dcl->val;
  7893. break;
  7894. case PROP_BOLD:
  7895. st->bold = (unsigned char)(dcl->val != 0);
  7896. break;
  7897. case PROP_ITALIC:
  7898. st->italic = (unsigned char)(dcl->val != 0);
  7899. break;
  7900. case PROP_MONO:
  7901. st->mono = (unsigned char)(dcl->val != 0);
  7902. break;
  7903. case PROP_FONT_FACE:
  7904. st->font_face = (unsigned char)dcl->val;
  7905. break;
  7906. case PROP_ALIGN:
  7907. st->align = (unsigned char)dcl->val;
  7908. break;
  7909. case PROP_UNDERLINE:
  7910. st->underline = (unsigned char)(dcl->val != 0);
  7911. break;
  7912. case PROP_STRIKE:
  7913. st->strike = (unsigned char)(dcl->val != 0);
  7914. break;
  7915. case PROP_OVERLINE:
  7916. st->overline = (unsigned char)(dcl->val != 0);
  7917. break;
  7918. case PROP_DISPLAY:
  7919. st->display = (unsigned char)(dcl->val &
  7920. ~BR_DISPLAY_INLINE_FLAG);
  7921. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  7922. st->list_item = 0;
  7923. break;
  7924. case PROP_VIS_HIDDEN:
  7925. st->vis_hidden = (unsigned char)(dcl->val != 0);
  7926. break;
  7927. case PROP_OPACITY_HIDDEN:
  7928. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  7929. break;
  7930. case PROP_FILL_MODE:
  7931. st->fill_mode = (unsigned char)dcl->val;
  7932. break;
  7933. case PROP_FILL:
  7934. st->fill = (unsigned short)dcl->val;
  7935. break;
  7936. case PROP_STROKE_MODE:
  7937. st->stroke_mode = (unsigned char)dcl->val;
  7938. break;
  7939. case PROP_STROKE:
  7940. st->stroke = (unsigned short)dcl->val;
  7941. break;
  7942. case PROP_STROKE_W:
  7943. st->stroke_w = (unsigned short)dcl->val;
  7944. break;
  7945. case PROP_FILL_RULE:
  7946. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  7947. break;
  7948. case PROP_OVERFLOW_HIDDEN:
  7949. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  7950. break;
  7951. case PROP_MT: st->mt = dcl->val; break;
  7952. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  7953. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  7954. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  7955. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  7956. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  7957. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  7958. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  7959. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  7960. case PROP_MR: st->mr = dcl->val; break;
  7961. case PROP_MB: st->mb = dcl->val; break;
  7962. case PROP_ML: st->ml = dcl->val; break;
  7963. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  7964. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  7965. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  7966. break;
  7967. case PROP_PT: st->pt = dcl->val; break;
  7968. case PROP_PR: st->pr = dcl->val; break;
  7969. case PROP_PB: st->pb = dcl->val; break;
  7970. case PROP_PL: st->pl = dcl->val; break;
  7971. case PROP_MT_U:
  7972. case PROP_MR_U:
  7973. case PROP_MB_U:
  7974. case PROP_ML_U:
  7975. case PROP_PT_U:
  7976. case PROP_PR_U:
  7977. case PROP_PB_U:
  7978. case PROP_PL_U:
  7979. st->mp_unit[dcl->prop - PROP_MT_U] =
  7980. (unsigned char)dcl->val;
  7981. break;
  7982. case PROP_BORDER_W:
  7983. set_border_side_w(st, 0, dcl->val);
  7984. set_border_side_w(st, 1, dcl->val);
  7985. set_border_side_w(st, 2, dcl->val);
  7986. set_border_side_w(st, 3, dcl->val);
  7987. break;
  7988. case PROP_BORDER_COLOR:
  7989. set_border_side_c(st, 0, dcl->val);
  7990. set_border_side_c(st, 1, dcl->val);
  7991. set_border_side_c(st, 2, dcl->val);
  7992. set_border_side_c(st, 3, dcl->val);
  7993. break;
  7994. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  7995. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  7996. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  7997. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  7998. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  7999. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  8000. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  8001. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  8002. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  8003. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  8004. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  8005. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  8006. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  8007. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  8008. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  8009. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  8010. case PROP_RADIUS_TL:
  8011. case PROP_RADIUS_TR:
  8012. case PROP_RADIUS_BR:
  8013. case PROP_RADIUS_BL:
  8014. st->radius[dcl->prop - PROP_RADIUS_TL] =
  8015. (unsigned char)dcl->val;
  8016. break;
  8017. case PROP_LIST_NONE:
  8018. st->no_bullet = (unsigned char)(dcl->val != 0);
  8019. break;
  8020. case PROP_LIST_STYLE:
  8021. st->list_style = (unsigned char)dcl->val;
  8022. break;
  8023. case PROP_CONTENT:
  8024. st->content = (unsigned short)dcl->val;
  8025. break;
  8026. case PROP_ASPECT_W:
  8027. st->aspect_w = (unsigned short)dcl->val;
  8028. break;
  8029. case PROP_ASPECT_H:
  8030. st->aspect_h = (unsigned short)dcl->val;
  8031. break;
  8032. case PROP_NOWRAP:
  8033. st->nowrap = (unsigned char)(dcl->val != 0);
  8034. break;
  8035. case PROP_WIDTH:
  8036. st->width = (unsigned short)dcl->val;
  8037. break;
  8038. case PROP_WIDTH_KIND:
  8039. st->width_kind = (unsigned char)dcl->val;
  8040. break;
  8041. case PROP_WIDTH_CALC_PCT:
  8042. st->width_calc_pct = dcl->val;
  8043. break;
  8044. case PROP_WIDTH_CALC_PX:
  8045. st->width_calc_px = dcl->val;
  8046. break;
  8047. case PROP_MAXW:
  8048. st->max_width = (unsigned short)dcl->val;
  8049. break;
  8050. case PROP_MAXW_KIND:
  8051. st->max_width_kind = (unsigned char)dcl->val;
  8052. break;
  8053. case PROP_MAXW_CALC_PCT:
  8054. st->max_width_calc_pct = (signed short)dcl->val;
  8055. break;
  8056. case PROP_MAXW_CALC_PX:
  8057. st->max_width_calc_px = (signed short)dcl->val;
  8058. break;
  8059. case PROP_MINW:
  8060. st->min_width = (unsigned short)dcl->val;
  8061. break;
  8062. case PROP_MINW_KIND:
  8063. st->min_width_kind = (unsigned char)dcl->val;
  8064. break;
  8065. case PROP_MINW_CALC_PCT:
  8066. st->min_width_calc_pct = (signed short)dcl->val;
  8067. break;
  8068. case PROP_MINW_CALC_PX:
  8069. st->min_width_calc_px = (signed short)dcl->val;
  8070. break;
  8071. case PROP_HEIGHT:
  8072. st->height = (unsigned short)dcl->val;
  8073. break;
  8074. case PROP_HEIGHT_KIND:
  8075. st->height_kind = (unsigned char)dcl->val;
  8076. break;
  8077. case PROP_HEIGHT_CALC_PCT:
  8078. st->height_calc_pct = (signed short)dcl->val;
  8079. break;
  8080. case PROP_HEIGHT_CALC_PX:
  8081. st->height_calc_px = (signed short)dcl->val;
  8082. break;
  8083. case PROP_MINH:
  8084. st->min_height = (unsigned short)dcl->val;
  8085. break;
  8086. case PROP_MINH_KIND:
  8087. st->min_height_kind = (unsigned char)dcl->val;
  8088. break;
  8089. case PROP_MINH_CALC_PCT:
  8090. st->min_height_calc_pct = (signed short)dcl->val;
  8091. break;
  8092. case PROP_MINH_CALC_PX:
  8093. st->min_height_calc_px = (signed short)dcl->val;
  8094. break;
  8095. case PROP_MAXH:
  8096. st->max_height = (unsigned short)dcl->val;
  8097. break;
  8098. case PROP_MAXH_KIND:
  8099. st->max_height_kind = (unsigned char)dcl->val;
  8100. break;
  8101. case PROP_MAXH_CALC_PCT:
  8102. st->max_height_calc_pct = (signed short)dcl->val;
  8103. break;
  8104. case PROP_MAXH_CALC_PX:
  8105. st->max_height_calc_px = (signed short)dcl->val;
  8106. break;
  8107. case PROP_LH_PCT:
  8108. st->line_h_pct = (unsigned char)dcl->val;
  8109. break;
  8110. case PROP_LH_PX:
  8111. st->line_h_px = (unsigned short)dcl->val;
  8112. break;
  8113. case PROP_UPPER:
  8114. st->uppercase = (unsigned char)(dcl->val != 0);
  8115. break;
  8116. case PROP_CURSOR:
  8117. st->cursor = (unsigned char)dcl->val;
  8118. break;
  8119. case PROP_FLOAT:
  8120. st->float_left = (unsigned char)dcl->val;
  8121. break;
  8122. case PROP_CLEAR:
  8123. st->clear = (unsigned char)dcl->val;
  8124. break;
  8125. case PROP_FLEX_GROW:
  8126. st->flex_grow = (unsigned char)dcl->val;
  8127. break;
  8128. case PROP_FLEX_COL:
  8129. st->flex_col = (unsigned char)(dcl->val != 0);
  8130. break;
  8131. case PROP_FLEX_WRAP:
  8132. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8133. break;
  8134. case PROP_JUSTIFY:
  8135. st->justify = (unsigned char)dcl->val;
  8136. break;
  8137. case PROP_ALIGN_ITEMS:
  8138. st->align_items = (unsigned char)dcl->val;
  8139. break;
  8140. case PROP_ALIGN_SELF:
  8141. st->align_self = (unsigned char)dcl->val;
  8142. break;
  8143. case PROP_GRID_NCOLS:
  8144. st->grid_ncols = (unsigned char)dcl->val;
  8145. break;
  8146. case PROP_GRID_GAP:
  8147. st->grid_gap = (unsigned char)dcl->val;
  8148. break;
  8149. case PROP_GRID_SLOT:
  8150. st->grid_slot = (unsigned char)dcl->val;
  8151. break;
  8152. case PROP_GRID_AUTO:
  8153. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8154. break;
  8155. case PROP_POS:
  8156. st->pos = (unsigned char)dcl->val;
  8157. break;
  8158. case PROP_LEFT:
  8159. st->left = dcl->val;
  8160. break;
  8161. case PROP_TOP:
  8162. st->top = dcl->val;
  8163. break;
  8164. case PROP_RIGHT:
  8165. st->right = dcl->val;
  8166. break;
  8167. case PROP_BOTTOM:
  8168. st->bottom = dcl->val;
  8169. break;
  8170. case PROP_LEFT_SET:
  8171. st->left_set = (unsigned char)(dcl->val != 0);
  8172. break;
  8173. case PROP_TOP_SET:
  8174. st->top_set = (unsigned char)(dcl->val != 0);
  8175. break;
  8176. case PROP_RIGHT_SET:
  8177. st->right_set = (unsigned char)(dcl->val != 0);
  8178. break;
  8179. case PROP_BOTTOM_SET:
  8180. st->bottom_set = (unsigned char)(dcl->val != 0);
  8181. break;
  8182. case PROP_ZINDEX:
  8183. st->z_index = dcl->val;
  8184. break;
  8185. case PROP_ZSET:
  8186. st->z_set = (unsigned char)(dcl->val != 0);
  8187. break;
  8188. case PROP_TRANSFORM_X:
  8189. st->tx = dcl->val;
  8190. break;
  8191. case PROP_TRANSFORM_Y:
  8192. st->ty = dcl->val;
  8193. break;
  8194. case PROP_TRANSFORM_SET:
  8195. st->transform_set = (unsigned char)(dcl->val != 0);
  8196. break;
  8197. case PROP_TRANSFORM_PAINT:
  8198. st->transform_scale = (unsigned char)(dcl->val != 0);
  8199. break;
  8200. case PROP_SCALE_X:
  8201. st->scale_x = (signed short)dcl->val;
  8202. break;
  8203. case PROP_SCALE_Y:
  8204. st->scale_y = (signed short)dcl->val;
  8205. break;
  8206. case PROP_ROTATE:
  8207. st->rotate_x100 = (signed short)dcl->val;
  8208. break;
  8209. case PROP_ORIGIN_X_KIND:
  8210. st->origin_x_kind = (unsigned char)dcl->val;
  8211. break;
  8212. case PROP_ORIGIN_X:
  8213. st->origin_x = (signed short)dcl->val;
  8214. break;
  8215. case PROP_ORIGIN_Y_KIND:
  8216. st->origin_y_kind = (unsigned char)dcl->val;
  8217. break;
  8218. case PROP_ORIGIN_Y:
  8219. st->origin_y = (signed short)dcl->val;
  8220. break;
  8221. default:
  8222. break;
  8223. }
  8224. }
  8225. /* The element's custom properties for rules flagged USES_SCOPED: the
  8226. * definitions of its matched rules in cascade order (precedence first,
  8227. * then document order), over the document-wide table. Inheritance from
  8228. * ancestors is not modelled; a name no matched rule defines falls back to
  8229. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8230. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8231. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8232. static void css_env_build(struct br_doc *d)
  8233. {
  8234. static unsigned short order[RULE_MATCH_MAX];
  8235. unsigned int i, need = 0, n = 0;
  8236. css_env_active = 0;
  8237. css_nlocal = 0;
  8238. for (i = 0; i < nrule_matches; i++)
  8239. if (d->rules[rule_matches[i].idx].var_scope &
  8240. BR_RULE_USES_SCOPED)
  8241. need = 1;
  8242. if (!need)
  8243. return;
  8244. /* Definitions apply lowest precedence first so the winner is
  8245. * written last (stable: document order within a precedence). */
  8246. for (i = 0; i < nrule_matches; i++) {
  8247. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8248. unsigned int j = n;
  8249. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8250. continue;
  8251. while (j > 0 && rule_matches[order[j - 1]].spec >
  8252. rule_matches[i].spec) {
  8253. order[j] = order[j - 1];
  8254. j--;
  8255. }
  8256. order[j] = i;
  8257. n++;
  8258. }
  8259. for (i = 0; i < n; i++) {
  8260. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8261. const char *raw = attr_ptr(d, r->raw_off);
  8262. unsigned int save = css_nlocal, k;
  8263. /* Collect this block's definitions after the earlier ones,
  8264. * replacing a name already present. */
  8265. {
  8266. unsigned int base = save;
  8267. css_local_append(raw, r->raw_len);
  8268. for (k = base; k < css_nlocal; k++) {
  8269. int prev = css_local_find_before(
  8270. css_local[k].name, css_local[k].nlen, base);
  8271. if (prev >= 0) {
  8272. css_local[prev] = css_local[k];
  8273. css_local[k].nlen = 0;
  8274. }
  8275. }
  8276. }
  8277. }
  8278. css_env_active = 1;
  8279. }
  8280. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8281. int spec, int spec_imp, short *best, short *best_imp)
  8282. {
  8283. unsigned char p = dcl->prop;
  8284. if (p >= PROP_COUNT)
  8285. return;
  8286. /* A transparent background competes with a colour for one slot. */
  8287. if (p == PROP_BG_NONE)
  8288. p = PROP_BG;
  8289. if (dcl->important) {
  8290. if (spec_imp >= best_imp[p]) {
  8291. apply_decl(st, dcl);
  8292. best_imp[p] = (short)spec_imp;
  8293. }
  8294. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8295. apply_decl(st, dcl);
  8296. best[p] = (short)spec;
  8297. }
  8298. }
  8299. void br_style_for(struct br_doc *d, unsigned int node,
  8300. const struct br_style *parent, struct br_style *out)
  8301. {
  8302. unsigned int tag;
  8303. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8304. unsigned int i;
  8305. if (!cascade_pe && d->nodes[node].style_valid) {
  8306. *out = d->nodes[node].style;
  8307. return;
  8308. }
  8309. *out = *parent;
  8310. out->has_bg = 0;
  8311. out->bg_set = 0;
  8312. out->bg_image = 0;
  8313. out->bg_repeat = 0;
  8314. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8315. out->bg_pos_x = out->bg_pos_y = 0;
  8316. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8317. out->float_left = 0;
  8318. out->clear = 0;
  8319. out->list_item = 0;
  8320. out->font_unit = 0;
  8321. for (i = 0; i < 4; i++) {
  8322. out->border_w[i] = 0;
  8323. out->border_c[i] = 0;
  8324. out->border_s[i] = BR_BORDER_SOLID;
  8325. out->radius[i] = 0;
  8326. }
  8327. out->width = out->max_width = out->min_width = out->height = 0;
  8328. out->min_height = out->max_height = 0;
  8329. out->width_calc_pct = out->width_calc_px = 0;
  8330. out->height_calc_pct = out->height_calc_px = 0;
  8331. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8332. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8333. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8334. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8335. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8336. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8337. out->box_sizing = 0;
  8338. out->ml_auto = out->mr_auto = 0;
  8339. out->mt = out->mr = out->mb = out->ml = 0;
  8340. out->pt = out->pr = out->pb = out->pl = 0;
  8341. for (i = 0; i < 8; i++) {
  8342. out->mp_unit[i] = 0;
  8343. out->mp_calc_px[i] = 0;
  8344. }
  8345. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8346. out->justify = out->align_items = out->align_self = 0;
  8347. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8348. out->grid_auto = 0;
  8349. out->aspect_w = out->aspect_h = 0;
  8350. out->pos = out->left_set = out->top_set = 0;
  8351. out->right_set = out->bottom_set = 0;
  8352. out->left = out->top = out->right = out->bottom = 0;
  8353. out->z_index = 0;
  8354. out->z_set = 0;
  8355. out->tx = out->ty = 0;
  8356. out->transform_set = 0;
  8357. out->transform_scale = 0;
  8358. out->scale_x = out->scale_y = 100;
  8359. out->rotate_x100 = 0;
  8360. out->origin_x = out->origin_y = 50;
  8361. out->origin_x_kind = out->origin_y_kind = 1;
  8362. out->vis_hidden = out->opacity_hidden = 0;
  8363. out->inline_flex = 0;
  8364. out->overflow_hidden = 0;
  8365. out->content = 0;
  8366. /* A pseudo-element is an anonymous inline box: none of the
  8367. * element's UA defaults or presentational hints apply to it. */
  8368. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8369. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8370. BR_DISPLAY_INLINE;
  8371. for (i = 0; i < PROP_COUNT; i++) {
  8372. best[i] = -1;
  8373. best_imp[i] = -1;
  8374. }
  8375. if (!cascade_pe)
  8376. switch (tag) {
  8377. case BR_TAG_HEAD:
  8378. case BR_TAG_TITLE:
  8379. case BR_TAG_META:
  8380. case BR_TAG_LINK:
  8381. case BR_TAG_BASE:
  8382. case BR_TAG_SCRIPT:
  8383. case BR_TAG_STYLE:
  8384. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8385. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8386. out->display = BR_DISPLAY_NONE;
  8387. break;
  8388. case BR_TAG_BODY:
  8389. out->mt = out->mb = out->ml = out->mr = 4;
  8390. break;
  8391. case BR_TAG_P:
  8392. out->mt = out->mb = 8;
  8393. break;
  8394. case BR_TAG_H1:
  8395. out->font_px = 32; out->bold = 1;
  8396. out->mt = 10; out->mb = 6;
  8397. break;
  8398. case BR_TAG_H2:
  8399. out->font_px = 24; out->bold = 1;
  8400. out->mt = 8; out->mb = 4;
  8401. break;
  8402. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8403. out->bold = 1; out->mt = 6; out->mb = 3;
  8404. if (tag == BR_TAG_H3)
  8405. out->font_px = 19;
  8406. else if (tag == BR_TAG_H4)
  8407. out->font_px = 17;
  8408. else if (tag == BR_TAG_H5)
  8409. out->font_px = 15;
  8410. else
  8411. out->font_px = 13;
  8412. break;
  8413. case BR_TAG_UL: case BR_TAG_OL:
  8414. out->mt = out->mb = 6; out->pl = 20;
  8415. break;
  8416. case BR_TAG_LI:
  8417. out->mt = out->mb = 2;
  8418. out->list_item = 1;
  8419. break;
  8420. case BR_TAG_BLOCKQUOTE:
  8421. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8422. break;
  8423. case BR_TAG_PRE:
  8424. out->mt = out->mb = 8; out->pl = 8;
  8425. out->has_bg = 1; out->bg = theme_panel;
  8426. out->mono = 1;
  8427. break;
  8428. case BR_TAG_CENTER:
  8429. out->align = BR_ALIGN_CENTER;
  8430. break;
  8431. case BR_TAG_A:
  8432. out->color = theme_link; out->underline = 1;
  8433. break;
  8434. case BR_TAG_B: case BR_TAG_STRONG:
  8435. out->bold = 1;
  8436. break;
  8437. case BR_TAG_I: case BR_TAG_EM:
  8438. out->italic = 1;
  8439. break;
  8440. case BR_TAG_U:
  8441. out->underline = 1;
  8442. break;
  8443. case BR_TAG_S: case BR_TAG_STRIKE:
  8444. out->strike = 1;
  8445. break;
  8446. case BR_TAG_CODE:
  8447. out->color = 0x8010;
  8448. out->mono = 1;
  8449. break;
  8450. case BR_TAG_SMALL:
  8451. out->font_px = 13;
  8452. break;
  8453. case BR_TAG_BIG:
  8454. out->font_px = 20;
  8455. break;
  8456. case BR_TAG_TD: case BR_TAG_TH:
  8457. out->pr = 12;
  8458. /* A cell establishes left-aligned content even inside a legacy
  8459. * <center> (HN's outer page wrapper); author CSS/align below
  8460. * can still override this. */
  8461. out->align = BR_ALIGN_LEFT;
  8462. break;
  8463. case BR_TAG_VIDEO:
  8464. out->display = BR_DISPLAY_BLOCK;
  8465. break;
  8466. case BR_TAG_SVG:
  8467. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8468. * external-link icon follows its link text on the same line.
  8469. * Flex rows still treat it as an item (flex_wants_row). */
  8470. out->display = BR_DISPLAY_INLINE;
  8471. break;
  8472. default:
  8473. break;
  8474. }
  8475. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8476. * presentational hints; author CSS still wins because it is applied
  8477. * below. */
  8478. if (!cascade_pe) {
  8479. unsigned short bgc;
  8480. const char *bga = br_find_attr(d, node, "bgcolor");
  8481. const char *ala = br_find_attr(d, node, "align");
  8482. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8483. out->bg = bgc;
  8484. out->has_bg = 1;
  8485. }
  8486. if (ala) {
  8487. if (same_string(ala, "right"))
  8488. out->align = BR_ALIGN_RIGHT;
  8489. else if (same_string(ala, "center"))
  8490. out->align = BR_ALIGN_CENTER;
  8491. else if (same_string(ala, "left"))
  8492. out->align = BR_ALIGN_LEFT;
  8493. }
  8494. }
  8495. /* presentational <font color>: author CSS below still wins */
  8496. if (!cascade_pe && d->nodes[node].color_off) {
  8497. unsigned short c;
  8498. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8499. if (parse_color(v, s_len(v), &c))
  8500. out->color = c;
  8501. }
  8502. /* SVG presentation attributes; CSS declarations still win below. */
  8503. if (!cascade_pe && in_svg(d, node)) {
  8504. const char *fa = br_find_attr(d, node, "fill");
  8505. const char *sa = br_find_attr(d, node, "stroke");
  8506. const char *wa = br_find_attr(d, node, "stroke-width");
  8507. const char *ra = br_find_attr(d, node, "fill-rule");
  8508. const char *ca = br_find_attr(d, node, "color");
  8509. unsigned short c;
  8510. int mode;
  8511. if (ca && parse_color(ca, s_len(ca), &c))
  8512. out->color = c;
  8513. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8514. out->fill_mode = (unsigned char)mode;
  8515. if (mode == BR_PAINT_COLOR)
  8516. out->fill = c;
  8517. }
  8518. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8519. out->stroke_mode = (unsigned char)mode;
  8520. if (mode == BR_PAINT_COLOR)
  8521. out->stroke = c;
  8522. }
  8523. if (wa) {
  8524. int sw = svg_len16(wa, s_len(wa));
  8525. if (sw >= 0)
  8526. out->stroke_w = (unsigned short)sw;
  8527. }
  8528. if (ra)
  8529. out->fill_evenodd = same_string(ra, "evenodd");
  8530. }
  8531. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8532. * Author CSS below can still override this. */
  8533. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8534. unsigned int p = d->nodes[node].parent;
  8535. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8536. !br_find_attr(d, p, "open"))
  8537. out->display = BR_DISPLAY_NONE;
  8538. }
  8539. rule_collect(d, node);
  8540. css_env_build(d);
  8541. for (i = 0; i < nrule_matches; i++) {
  8542. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8543. int spec = rule_matches[i].spec;
  8544. int spec_imp = rule_matches[i].spec_imp;
  8545. unsigned int k;
  8546. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8547. /* Re-expand against this element's variables. */
  8548. struct br_decl decls[BR_DECLS];
  8549. unsigned int nd;
  8550. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8551. decls, &nd);
  8552. for (k = 0; k < nd; k++)
  8553. cascade_decl(out, &decls[k], spec, spec_imp,
  8554. best, best_imp);
  8555. continue;
  8556. }
  8557. for (k = 0; k < r->nd; k++)
  8558. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8559. best_imp);
  8560. }
  8561. css_env_active = 0;
  8562. css_nlocal = 0;
  8563. if (!cascade_pe && d->nodes[node].style_off) {
  8564. struct br_decl decls[BR_DECLS];
  8565. unsigned int nd, k;
  8566. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8567. parse_decls(d, v, s_len(v), decls, &nd);
  8568. for (k = 0; k < nd; k++)
  8569. cascade_decl(out, &decls[k],
  8570. CASCADE_INLINE * CASCADE_BAND,
  8571. CASCADE_INLINE * CASCADE_BAND, best,
  8572. best_imp);
  8573. }
  8574. if (out->font_unit) {
  8575. int px = out->font_raw;
  8576. if (out->font_unit == 2)
  8577. px = parent->font_px * out->font_raw / 100;
  8578. else if (out->font_unit == 3)
  8579. px = root_font_px * out->font_raw / 100;
  8580. else if (out->font_unit == 4)
  8581. px = parent->font_px * out->font_raw / 10000;
  8582. if (px < 8)
  8583. px = 8;
  8584. if (px > 72)
  8585. px = 72;
  8586. out->font_px = (unsigned char)px;
  8587. }
  8588. resolve_size_units(out);
  8589. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  8590. out->display = BR_DISPLAY_NONE;
  8591. if (out->vis_hidden || out->opacity_hidden ||
  8592. (out->overflow_hidden && out->height_kind == 1 &&
  8593. out->height == 0))
  8594. out->display = BR_DISPLAY_NONE;
  8595. if (cascade_pe)
  8596. return;
  8597. d->nodes[node].style = *out;
  8598. d->nodes[node].style_valid = 1;
  8599. }
  8600. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  8601. const struct br_style *elem, struct br_style *out,
  8602. char *buf, unsigned int cap)
  8603. {
  8604. struct br_node *n;
  8605. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  8606. const char *c;
  8607. unsigned int len = 0;
  8608. if (out) {
  8609. out->content = 0;
  8610. out->display = BR_DISPLAY_NONE;
  8611. }
  8612. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  8613. (pe != 1 && pe != 2))
  8614. return 0;
  8615. n = &d->nodes[node];
  8616. if (n->type != BR_NODE_ELEM ||
  8617. ((n->pe_state & chk) && !(n->pe_state & has)))
  8618. return 0;
  8619. cascade_pe = pe;
  8620. br_style_for(d, node, elem, out);
  8621. cascade_pe = 0;
  8622. if (out->content == BR_CONTENT_EMPTY) {
  8623. /* No text; the caller may still draw the box (an icon
  8624. * with a size and a background image). */
  8625. buf[0] = '\0';
  8626. n->pe_state |= chk;
  8627. if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
  8628. out->pos != 2 && (out->width_kind || out->height_kind) &&
  8629. (out->has_bg || out->bg_image))
  8630. n->pe_state |= has;
  8631. else
  8632. out->display = BR_DISPLAY_NONE;
  8633. return 0;
  8634. }
  8635. if (out->content && out->display != BR_DISPLAY_NONE &&
  8636. out->pos != 1 && out->pos != 2) {
  8637. c = attr_ptr(d, d->content_off[out->content - 1]);
  8638. while (*c && len + 1 < cap) {
  8639. if (*c == '\001') {
  8640. char name[32];
  8641. unsigned int k = 0;
  8642. const char *v;
  8643. c++;
  8644. while (*c && *c != '\002' && k + 1 < sizeof(name))
  8645. name[k++] = *c++;
  8646. name[k] = '\0';
  8647. if (*c == '\002')
  8648. c++;
  8649. v = br_find_attr(d, node, name);
  8650. while (v && *v && len + 1 < cap)
  8651. buf[len++] = *v++;
  8652. continue;
  8653. }
  8654. buf[len++] = *c++;
  8655. }
  8656. }
  8657. buf[len] = '\0';
  8658. n->pe_state |= chk;
  8659. if (len)
  8660. n->pe_state |= has;
  8661. return len;
  8662. }
  8663. /* ---- HTML parser ---- */
  8664. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  8665. unsigned int sp, int *pre)
  8666. {
  8667. unsigned int i;
  8668. *pre = 0;
  8669. for (i = 1; i <= sp; i++)
  8670. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  8671. (*pre)++;
  8672. }
  8673. static void implicit_close(struct br_doc *d, unsigned short *stack,
  8674. unsigned int *sp, unsigned int tag)
  8675. {
  8676. while (*sp > 0) {
  8677. unsigned int top = d->nodes[stack[*sp]].tag;
  8678. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  8679. (*sp)--;
  8680. continue;
  8681. }
  8682. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  8683. (*sp)--;
  8684. continue;
  8685. }
  8686. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  8687. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  8688. (*sp)--;
  8689. continue;
  8690. }
  8691. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  8692. (*sp)--;
  8693. continue;
  8694. }
  8695. break;
  8696. }
  8697. }
  8698. static int has_attr_token(const char *s, unsigned int len, const char *name)
  8699. {
  8700. unsigned int i, n = s_len(name);
  8701. for (i = 0; i + n <= len; i++) {
  8702. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  8703. continue;
  8704. if (!ci_eq_n(s + i, name, n))
  8705. continue;
  8706. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  8707. return 1;
  8708. }
  8709. return 0;
  8710. }
  8711. static int stack_has_svg(const struct br_doc *d,
  8712. const unsigned short *stack, unsigned int sp)
  8713. {
  8714. unsigned int k;
  8715. for (k = 0; k <= sp && k < 64; k++)
  8716. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  8717. return 1;
  8718. return 0;
  8719. }
  8720. /* Skip an element subtree by finding its closing tag. Used for SVG
  8721. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  8722. * overwrite the document title. */
  8723. static unsigned int skip_element(const char *html, unsigned int len,
  8724. unsigned int i, const char *name,
  8725. unsigned int n)
  8726. {
  8727. char close[40];
  8728. unsigned int k = 0, p;
  8729. close[k++] = '<';
  8730. close[k++] = '/';
  8731. while (k < sizeof(close) - 1 && n--)
  8732. close[k++] = *name++;
  8733. p = (unsigned int)find_ci(html, len, close, k, i);
  8734. if ((int)p < 0)
  8735. return len;
  8736. p += k;
  8737. while (p < len && html[p] != '>')
  8738. p++;
  8739. if (p < len)
  8740. p++;
  8741. return p;
  8742. }
  8743. /* Store a <script src=...> value in the attribute arena. Kept separate from
  8744. * parse_attrs() so script-only attributes cannot be confused with the
  8745. * value/d attributes that also use src_off on other elements. */
  8746. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  8747. unsigned int len)
  8748. {
  8749. unsigned int i = 0;
  8750. while (i < len) {
  8751. unsigned int ns, ne, vs, ve;
  8752. char quote = 0;
  8753. while (i < len && is_space(s[i]))
  8754. i++;
  8755. if (i >= len || s[i] == '/')
  8756. break;
  8757. ns = i;
  8758. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  8759. i++;
  8760. ne = i;
  8761. while (i < len && is_space(s[i]))
  8762. i++;
  8763. if (i >= len || s[i] != '=')
  8764. continue;
  8765. i++;
  8766. while (i < len && is_space(s[i]))
  8767. i++;
  8768. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8769. quote = s[i++];
  8770. vs = i;
  8771. while (i < len && s[i] != quote)
  8772. i++;
  8773. ve = i;
  8774. if (i < len)
  8775. i++;
  8776. } else {
  8777. vs = i;
  8778. while (i < len && !is_space(s[i]) && s[i] != '>')
  8779. i++;
  8780. ve = i;
  8781. }
  8782. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  8783. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  8784. }
  8785. return 0;
  8786. }
  8787. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  8788. * application/json, text/template, ...) and non-JS type values. External
  8789. * src scripts are allowed; the browser fetches them separately. Returns 1
  8790. * for a classic script, 2 for type="module", 0 to skip (also for a
  8791. * `nomodule` fallback, since module scripts run). */
  8792. static int script_is_javascript(const char *s, unsigned int len)
  8793. {
  8794. int kind = 1;
  8795. unsigned int i = 0;
  8796. while (i < len) {
  8797. unsigned int ns, ne, vs = 0, ve = 0;
  8798. char quote = 0;
  8799. int has_val = 0;
  8800. while (i < len && is_space(s[i]))
  8801. i++;
  8802. if (i >= len)
  8803. break;
  8804. ns = i;
  8805. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  8806. i++;
  8807. ne = i;
  8808. while (i < len && is_space(s[i]))
  8809. i++;
  8810. if (i < len && s[i] == '=') {
  8811. i++;
  8812. while (i < len && is_space(s[i]))
  8813. i++;
  8814. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8815. quote = s[i++];
  8816. vs = i;
  8817. while (i < len && s[i] != quote)
  8818. i++;
  8819. ve = i;
  8820. if (i < len)
  8821. i++;
  8822. } else {
  8823. vs = i;
  8824. while (i < len && !is_space(s[i]) && s[i] != '/')
  8825. i++;
  8826. ve = i;
  8827. }
  8828. has_val = 1;
  8829. } else if (i == ne) {
  8830. i++;
  8831. }
  8832. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  8833. return 0;
  8834. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  8835. unsigned int vn = has_val ? ve - vs : 0;
  8836. if (vn == 0)
  8837. continue;
  8838. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  8839. kind = 2;
  8840. continue;
  8841. }
  8842. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  8843. (vn == 22 &&
  8844. ci_eq_n(s + vs, "application/javascript", 22)) ||
  8845. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  8846. (vn == 22 &&
  8847. ci_eq_n(s + vs, "application/ecmascript", 22)))
  8848. continue;
  8849. return 0;
  8850. }
  8851. }
  8852. return kind;
  8853. }
  8854. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  8855. {
  8856. unsigned short stack[64];
  8857. unsigned int sp = 0;
  8858. unsigned int i = 0;
  8859. int pre = 0;
  8860. unsigned int css_len = 0;
  8861. if (!d)
  8862. return -1;
  8863. if (!d->nodes)
  8864. br_doc_init(d);
  8865. if (!d->nodes || !d->text || !d->attrs)
  8866. return -1;
  8867. doc_reset(d);
  8868. d->script_src = html;
  8869. stack[0] = 0;
  8870. d->nodes[0].type = BR_NODE_ELEM;
  8871. d->nodes[0].tag = BR_TAG_UNKNOWN;
  8872. while (i < len) {
  8873. if (html[i] != '<') {
  8874. unsigned int st = i;
  8875. while (i < len && html[i] != '<')
  8876. i++;
  8877. if (sp <= 63)
  8878. append_text(d, stack[sp], html + st, i - st, pre);
  8879. continue;
  8880. }
  8881. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  8882. html[i + 3] == '-') {
  8883. int p = find_ci(html, len, "-->", 3, i + 4);
  8884. i = p >= 0 ? (unsigned int)p + 3 : len;
  8885. continue;
  8886. }
  8887. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  8888. while (i < len && html[i] != '>')
  8889. i++;
  8890. if (i < len)
  8891. i++;
  8892. continue;
  8893. }
  8894. if (i + 1 < len && html[i + 1] == '/') {
  8895. unsigned int ns = i + 2, ne, tag, k, n;
  8896. char name[32];
  8897. while (ns < len && is_space(html[ns]))
  8898. ns++;
  8899. ne = ns;
  8900. while (ne < len && is_ident(html[ne]))
  8901. ne++;
  8902. if (ne == ns) {
  8903. i++;
  8904. continue;
  8905. }
  8906. n = ne - ns;
  8907. if (n >= sizeof(name))
  8908. n = sizeof(name) - 1;
  8909. for (k = 0; k < n; k++)
  8910. name[k] = html[ns + k];
  8911. name[n] = '\0';
  8912. tag = br_tag_from_name(name);
  8913. i = ne;
  8914. while (i < len && html[i] != '>')
  8915. i++;
  8916. if (i < len)
  8917. i++;
  8918. for (k = sp; k > 0; k--) {
  8919. struct br_node *nd = &d->nodes[stack[k]];
  8920. if (tag == BR_TAG_UNKNOWN) {
  8921. const char *raw = attr_ptr(d, nd->name_off);
  8922. if (nd->tag == BR_TAG_UNKNOWN &&
  8923. ci_eq_n(raw, name, n) &&
  8924. raw[n] == '\0') {
  8925. sp = k - 1;
  8926. break;
  8927. }
  8928. } else if (nd->tag == tag) {
  8929. sp = k - 1;
  8930. break;
  8931. }
  8932. }
  8933. stack_recount_pre(d, stack, sp, &pre);
  8934. continue;
  8935. }
  8936. /* start tag */
  8937. {
  8938. unsigned int ns = i + 1, ne, tag, k, n;
  8939. char name[16];
  8940. unsigned int attr_start, attr_end;
  8941. int selfclose = 0;
  8942. while (ns < len && is_space(html[ns]))
  8943. ns++;
  8944. ne = ns;
  8945. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  8946. ne++;
  8947. if (ne == ns) {
  8948. /* stray '<' */
  8949. append_text(d, stack[sp], html + i, 1, pre);
  8950. i++;
  8951. continue;
  8952. }
  8953. n = ne - ns;
  8954. if (n >= sizeof(name))
  8955. n = sizeof(name) - 1;
  8956. for (k = 0; k < n; k++)
  8957. name[k] = html[ns + k];
  8958. name[n] = '\0';
  8959. tag = br_tag_from_name(name);
  8960. attr_start = ne;
  8961. {
  8962. unsigned int q = ne;
  8963. char quote = 0;
  8964. while (q < len) {
  8965. char c = html[q];
  8966. if (quote) {
  8967. if (c == quote)
  8968. quote = 0;
  8969. } else if (c == '"' || c == '\'') {
  8970. quote = c;
  8971. } else if (c == '>') {
  8972. break;
  8973. }
  8974. q++;
  8975. }
  8976. attr_end = q;
  8977. /* `/>` closes the tag only when the slash is
  8978. * not the end of an unquoted attribute value:
  8979. * Docusaurus writes `<a href=/installation/>`,
  8980. * which used to self-close the link and leave
  8981. * its label outside it. */
  8982. if (q > attr_start && html[q - 1] == '/' &&
  8983. (q - 1 == attr_start ||
  8984. is_space(html[q - 2]) ||
  8985. html[q - 2] == '"' || html[q - 2] == '\''))
  8986. selfclose = 1;
  8987. i = q < len ? q + 1 : len;
  8988. }
  8989. if (tag == BR_TAG_SCRIPT) {
  8990. int p = find_ci(html, len, "</script", 8, i);
  8991. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8992. unsigned int idx;
  8993. struct br_script *script = 0;
  8994. int kind = script_is_javascript(html + attr_start,
  8995. attr_end - attr_start);
  8996. if (kind) {
  8997. if (d->nscripts < BR_MAX_SCRIPTS) {
  8998. script = &d->scripts[d->nscripts++];
  8999. script->module = kind == 2;
  9000. script->node = 0;
  9001. script->off = i;
  9002. script->len = end > i ? end - i : 0;
  9003. script->src_off = script_src_attr(
  9004. d, html + attr_start,
  9005. attr_end - attr_start);
  9006. } else {
  9007. d->scripts_truncated = 1;
  9008. }
  9009. }
  9010. /* Keep the element in the DOM even though its body is
  9011. * executed separately: scripts (Piwik, analytics)
  9012. * insert a sibling after getElementsByTagName('script')
  9013. * [0], and the element must exist for that to work. */
  9014. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9015. (unsigned char)tag);
  9016. if (idx)
  9017. parse_attrs(d, &d->nodes[idx],
  9018. html + attr_start,
  9019. attr_end - attr_start);
  9020. if (script)
  9021. script->node = idx;
  9022. if (idx && !kind && end > i)
  9023. append_raw_text(d, idx, html + i, end - i);
  9024. i = p >= 0 ? (unsigned int)p + 8 : len;
  9025. while (i < len && html[i] != '>')
  9026. i++;
  9027. if (i < len)
  9028. i++;
  9029. continue;
  9030. }
  9031. /* Scripting is always enabled, so <noscript> is a
  9032. * raw-text element (HTML "in head/in body, scripting
  9033. * flag set"): its markup must not reach the DOM or the
  9034. * cascade. google.com puts a meta refresh and
  9035. * `table,div,span,p{display:none}` in one, which hid
  9036. * the whole page and left only the "click here" link. */
  9037. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  9038. int p = find_ci(html, len, "</noscript", 10, i);
  9039. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9040. unsigned int idx;
  9041. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9042. (unsigned char)tag);
  9043. if (idx) {
  9044. parse_attrs(d, &d->nodes[idx],
  9045. html + attr_start,
  9046. attr_end - attr_start);
  9047. if (end > i)
  9048. append_raw_text(d, idx, html + i,
  9049. end - i);
  9050. }
  9051. i = p >= 0 ? (unsigned int)p + 10 : len;
  9052. while (i < len && html[i] != '>')
  9053. i++;
  9054. if (i < len)
  9055. i++;
  9056. continue;
  9057. }
  9058. if (stack_has_svg(d, stack, sp) &&
  9059. (tag == BR_TAG_TITLE ||
  9060. (tag == BR_TAG_UNKNOWN &&
  9061. (ci_eq_n(name, "desc", 4) ||
  9062. ci_eq_n(name, "metadata", 8))))) {
  9063. i = skip_element(html, len, i, name, n);
  9064. continue;
  9065. }
  9066. /* A closed <details> is collapsed at style time, not
  9067. * parse time: the <summary> (the hamburger button on
  9068. * tagesschau and many mobile menus) must stay in the
  9069. * DOM. */
  9070. if (tag == BR_TAG_STYLE) {
  9071. int p = find_ci(html, len, "</style", 7, i);
  9072. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9073. unsigned int cend = end;
  9074. unsigned int idx;
  9075. while (cend > i && is_space(html[cend - 1]))
  9076. cend--;
  9077. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  9078. for (k = i; k < cend; k++)
  9079. css_store[css_len++] = html[k];
  9080. css_store[css_len++] = '\n';
  9081. }
  9082. /* The CSS is collected above, but keep a display:none
  9083. * element so getElementsByTagName('style')/parentNode
  9084. * behave like a real document. */
  9085. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9086. (unsigned char)tag);
  9087. if (idx)
  9088. parse_attrs(d, &d->nodes[idx],
  9089. html + attr_start,
  9090. attr_end - attr_start);
  9091. i = p >= 0 ? (unsigned int)p + 7 : len;
  9092. while (i < len && html[i] != '>')
  9093. i++;
  9094. if (i < len)
  9095. i++;
  9096. continue;
  9097. }
  9098. if (tag == BR_TAG_TITLE) {
  9099. int p = find_ci(html, len, "</title", 7, i);
  9100. unsigned int end = p >= 0 ? (unsigned int)p : len;
  9101. unsigned int tn = 0;
  9102. char tmp[BR_TITLE_MAX];
  9103. tn = decode_text(html + i, end - i, tmp,
  9104. sizeof(tmp), 1);
  9105. (void)tn;
  9106. s_copy(d->title, tmp, sizeof(d->title));
  9107. i = p >= 0 ? (unsigned int)p + 7 : len;
  9108. while (i < len && html[i] != '>')
  9109. i++;
  9110. if (i < len)
  9111. i++;
  9112. continue;
  9113. }
  9114. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  9115. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  9116. unsigned int idx;
  9117. implicit_close(d, stack, &sp, tag);
  9118. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  9119. (unsigned char)tag);
  9120. if (idx)
  9121. parse_attrs(d, &d->nodes[idx],
  9122. html + attr_start,
  9123. attr_end - attr_start);
  9124. continue;
  9125. }
  9126. if (sp >= 63) {
  9127. d->truncated = 1;
  9128. continue;
  9129. }
  9130. implicit_close(d, stack, &sp, tag);
  9131. {
  9132. unsigned int idx = node_append(d, stack[sp],
  9133. BR_NODE_ELEM,
  9134. (unsigned char)tag);
  9135. if (!idx)
  9136. continue;
  9137. parse_attrs(d, &d->nodes[idx],
  9138. html + attr_start,
  9139. attr_end - attr_start);
  9140. if (tag == BR_TAG_UNKNOWN)
  9141. d->nodes[idx].name_off =
  9142. store_attr(d, name, n, 31);
  9143. /* HTML ignores the self-closing flag on a
  9144. * normal element (`<div/>` opens a div); only
  9145. * SVG content (and unknown/custom tags, kept
  9146. * as before) honours it. */
  9147. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9148. tag != BR_TAG_SVG &&
  9149. !stack_has_svg(d, stack, sp))
  9150. selfclose = 0;
  9151. if (!selfclose) {
  9152. sp++;
  9153. stack[sp] = (unsigned short)idx;
  9154. if (tag == BR_TAG_PRE)
  9155. pre++;
  9156. }
  9157. }
  9158. }
  9159. }
  9160. d->inline_css_len = css_len;
  9161. return 0;
  9162. }
  9163. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9164. {
  9165. if (!d->nodes)
  9166. br_doc_init(d);
  9167. if (!d->nodes || !d->text || !d->attrs)
  9168. return -1;
  9169. doc_reset(d);
  9170. d->plain = 1;
  9171. d->nodes[0].type = BR_NODE_ELEM;
  9172. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9173. append_text(d, 0, text, len, 1);
  9174. return 0;
  9175. }
  9176. /* A standalone image response: a synthetic document with one <img>. The
  9177. * caller decodes the already-fetched body into image slot 0 and sets the
  9178. * returned node's img_index. */
  9179. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9180. {
  9181. unsigned int idx;
  9182. if (!d->nodes)
  9183. br_doc_init(d);
  9184. if (!d->nodes || !d->text || !d->attrs)
  9185. return 0;
  9186. doc_reset(d);
  9187. d->nodes[0].type = BR_NODE_ELEM;
  9188. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9189. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9190. if (!idx)
  9191. return 0;
  9192. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9193. return idx;
  9194. }