browser_html.c 247 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803
  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. static char bg_urls[BR_BG_URLS][192];
  22. static unsigned int bg_url_count;
  23. /* @font-face definitions, one document at a time. The URL is resolved by
  24. * the browser against `base` (the sheet URL, or the page URL for <style>). */
  25. struct br_font_def {
  26. char family[BR_FACE_FAMILY];
  27. char url[BR_FACE_URL];
  28. char base[192];
  29. unsigned char used;
  30. };
  31. static struct br_font_def font_defs[BR_MAX_FACES];
  32. static int font_face_count;
  33. static char font_base[192];
  34. static void font_reset(void)
  35. {
  36. memset(font_defs, 0, sizeof(font_defs));
  37. font_face_count = 0;
  38. font_base[0] = '\0';
  39. }
  40. void br_set_font_base(const char *base)
  41. {
  42. unsigned int i = 0;
  43. if (!base)
  44. base = "";
  45. while (base[i] && i + 1 < sizeof(font_base)) {
  46. font_base[i] = base[i];
  47. i++;
  48. }
  49. font_base[i] = '\0';
  50. }
  51. int br_font_face_count(void)
  52. {
  53. return font_face_count;
  54. }
  55. const char *br_font_face_url(int id)
  56. {
  57. if (id < 1 || id > font_face_count)
  58. return "";
  59. return font_defs[id - 1].url;
  60. }
  61. const char *br_font_face_base(int id)
  62. {
  63. if (id < 1 || id > font_face_count)
  64. return "";
  65. return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
  66. font_base;
  67. }
  68. const char *br_font_face_family(int id)
  69. {
  70. if (id < 1 || id > font_face_count)
  71. return "";
  72. return font_defs[id - 1].family;
  73. }
  74. const char *br_bg_image_url(unsigned int idx)
  75. {
  76. if (idx >= bg_url_count)
  77. return "";
  78. return bg_urls[idx];
  79. }
  80. unsigned int br_root_font_px(void)
  81. {
  82. return (unsigned int)root_font_px;
  83. }
  84. /* ---- tag table ---- */
  85. struct tag_name {
  86. const char *name;
  87. unsigned char tag;
  88. };
  89. static const struct tag_name tag_names[] = {
  90. { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
  91. { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
  92. { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
  93. { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
  94. { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
  95. { "template", BR_TAG_TEMPLATE },
  96. { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
  97. { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
  98. { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
  99. { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
  100. { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
  101. { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
  102. { "picture", BR_TAG_PICTURE },
  103. { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
  104. { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
  105. { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
  106. { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
  107. { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
  108. { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
  109. { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
  110. { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
  111. { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
  112. { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
  113. { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
  114. { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
  115. { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
  116. { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
  117. { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
  118. { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
  119. { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
  120. { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
  121. { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
  122. { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
  123. { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
  124. { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
  125. { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
  126. { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
  127. { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
  128. { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
  129. { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
  130. { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
  131. { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
  132. { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
  133. { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
  134. { 0, 0 }
  135. };
  136. const char *br_tag_name(unsigned int tag)
  137. {
  138. unsigned int i;
  139. for (i = 0; tag_names[i].name; i++)
  140. if (tag_names[i].tag == tag)
  141. return tag_names[i].name;
  142. return "?";
  143. }
  144. static unsigned int s_len(const char *s);
  145. #define TAG_HASH_SIZE 128
  146. static unsigned char tag_hash_idx[TAG_HASH_SIZE];
  147. static int tag_hash_ready;
  148. static unsigned int tag_hash_key(const char *name, unsigned int len)
  149. {
  150. unsigned int i, h = 2166136261u;
  151. for (i = 0; i < len; i++) {
  152. h ^= (unsigned char)name[i];
  153. h *= 16777619u;
  154. }
  155. return h & (TAG_HASH_SIZE - 1);
  156. }
  157. static void tag_hash_build(void)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < TAG_HASH_SIZE; i++)
  161. tag_hash_idx[i] = 0xFF;
  162. for (i = 0; tag_names[i].name; i++) {
  163. unsigned int h = tag_hash_key(tag_names[i].name,
  164. s_len(tag_names[i].name));
  165. while (tag_hash_idx[h] != 0xFF)
  166. h = (h + 1) & (TAG_HASH_SIZE - 1);
  167. tag_hash_idx[h] = (unsigned char)i;
  168. }
  169. tag_hash_ready = 1;
  170. }
  171. unsigned int br_tag_from_name(const char *name)
  172. {
  173. char lower[16];
  174. unsigned int i, len, h;
  175. unsigned char idx;
  176. for (i = 0; i < 15 && name[i]; i++) {
  177. char c = name[i];
  178. if (c >= 'A' && c <= 'Z')
  179. c = (char)(c - 'A' + 'a');
  180. lower[i] = c;
  181. }
  182. lower[i] = '\0';
  183. len = i;
  184. if (!tag_hash_ready)
  185. tag_hash_build();
  186. h = tag_hash_key(lower, len);
  187. for (;;) {
  188. idx = tag_hash_idx[h];
  189. if (idx == 0xFF)
  190. return BR_TAG_UNKNOWN;
  191. {
  192. const char *n = tag_names[idx].name;
  193. unsigned int j;
  194. for (j = 0; lower[j] && n[j] == lower[j]; j++)
  195. ;
  196. if (!lower[j] && !n[j])
  197. return tag_names[idx].tag;
  198. }
  199. h = (h + 1) & (TAG_HASH_SIZE - 1);
  200. }
  201. }
  202. int br_tag_is_void(unsigned int tag)
  203. {
  204. switch (tag) {
  205. case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
  206. case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
  207. case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
  208. case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
  209. case BR_TAG_AREA: case BR_TAG_COL:
  210. return 1;
  211. default:
  212. return 0;
  213. }
  214. }
  215. int br_tag_is_block(unsigned int tag)
  216. {
  217. switch (tag) {
  218. case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
  219. case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
  220. case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  221. case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
  222. case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
  223. case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
  224. case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
  225. case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
  226. case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
  227. case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
  228. case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
  229. case BR_TAG_PICTURE:
  230. case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
  231. case BR_TAG_ADDRESS: case BR_TAG_HR:
  232. return 1;
  233. default:
  234. return 0;
  235. }
  236. }
  237. /* ---- string / entity helpers ---- */
  238. static unsigned int s_len(const char *s)
  239. {
  240. unsigned int n = 0;
  241. while (s && s[n])
  242. n++;
  243. return n;
  244. }
  245. static void s_copy(char *d, const char *s, unsigned int cap)
  246. {
  247. unsigned int i = 0;
  248. if (!cap)
  249. return;
  250. while (s && s[i] && i + 1 < cap) {
  251. d[i] = s[i];
  252. i++;
  253. }
  254. d[i] = '\0';
  255. }
  256. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  257. {
  258. unsigned int i;
  259. for (i = 0; i < n; i++) {
  260. char ca = a[i], cb = b[i];
  261. if (ca >= 'A' && ca <= 'Z')
  262. ca = (char)(ca - 'A' + 'a');
  263. if (cb >= 'A' && cb <= 'Z')
  264. cb = (char)(cb - 'A' + 'a');
  265. if (ca != cb)
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. #define CURSOR_CSS_(e, css, file) css,
  271. static const char *const css_cursor_names[CURSOR_COUNT] = {
  272. CURSOR_LIST(CURSOR_CSS_)
  273. };
  274. #undef CURSOR_CSS_
  275. /* CSS `cursor`: the keyword after the last comma (url() images before it
  276. * are not supported, so the fallback keyword is what counts). Returns
  277. * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
  278. * anything else (the declaration is then ignored). */
  279. static int parse_cursor(const char *v, unsigned int n)
  280. {
  281. unsigned int a = 0, b, i;
  282. for (i = 0; i < n; i++)
  283. if (v[i] == ',')
  284. a = i + 1;
  285. while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
  286. a++;
  287. b = a;
  288. while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
  289. (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
  290. b++;
  291. if (b == a)
  292. return -1;
  293. if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
  294. return BR_CURSOR_AUTO;
  295. if (b - a == 4 && ci_eq_n(v + a, "none", 4))
  296. return CURSOR_DEFAULT + 1;
  297. for (i = 0; i < CURSOR_COUNT; i++)
  298. if (strlen(css_cursor_names[i]) == b - a &&
  299. ci_eq_n(v + a, css_cursor_names[i], b - a))
  300. return (int)i + 1;
  301. return -1;
  302. }
  303. static int find_ci(const char *hay, unsigned int len, const char *needle,
  304. unsigned int nlen, unsigned int from)
  305. {
  306. unsigned int i;
  307. if (nlen == 0 || from >= len)
  308. return -1;
  309. for (i = from; i + nlen <= len; i++)
  310. if (ci_eq_n(hay + i, needle, nlen))
  311. return (int)i;
  312. return -1;
  313. }
  314. static const struct {
  315. const char *name;
  316. const char *text;
  317. } entities[] = {
  318. { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
  319. { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
  320. { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
  321. { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
  322. { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
  323. { "times", "x" }, { "divide", "/" }, { "deg", "o" },
  324. { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
  325. { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
  326. { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
  327. { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
  328. { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
  329. { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
  330. { 0, 0 }
  331. };
  332. static void emit(char *out, unsigned int cap, unsigned int *n, char c)
  333. {
  334. if (*n + 1 < cap)
  335. out[(*n)++] = c;
  336. }
  337. static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
  338. char *out, unsigned int cap, unsigned int *n)
  339. {
  340. unsigned int start = *pos + 1, i = start;
  341. unsigned int code = 0;
  342. int ok = 0;
  343. while (i < len && s[i] != ';' && i - start < 12)
  344. i++;
  345. if (i >= len || s[i] != ';') {
  346. /* Not an entity: emit the '&' and advance past it. Leaving
  347. * *pos unchanged here used to spin forever on bare '&'. */
  348. emit(out, cap, n, '&');
  349. *pos = start;
  350. return;
  351. }
  352. if (s[start] == '#') {
  353. unsigned int j = start + 1;
  354. if (j < i && (s[j] == 'x' || s[j] == 'X')) {
  355. j++;
  356. for (; j < i; j++) {
  357. char c = s[j];
  358. int d = -1;
  359. if (c >= '0' && c <= '9') d = c - '0';
  360. else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
  361. else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
  362. if (d < 0) break;
  363. code = code * 16 + (unsigned int)d;
  364. ok = 1;
  365. }
  366. } else {
  367. for (; j < i; j++) {
  368. if (s[j] < '0' || s[j] > '9')
  369. break;
  370. code = code * 10 + (unsigned int)(s[j] - '0');
  371. ok = 1;
  372. }
  373. }
  374. } else {
  375. unsigned int k;
  376. for (k = 0; entities[k].name; k++) {
  377. unsigned int el = s_len(entities[k].name);
  378. unsigned int j;
  379. if (el != i - start)
  380. continue;
  381. for (j = 0; j < el; j++)
  382. if (s[start + j] != entities[k].name[j])
  383. break;
  384. if (j == el) {
  385. const char *t = entities[k].text;
  386. while (*t)
  387. emit(out, cap, n, *t++);
  388. *pos = i + 1;
  389. return;
  390. }
  391. }
  392. }
  393. if (ok && code) {
  394. if (code == 0x00A0)
  395. emit(out, cap, n, ' ');
  396. else if ((code >= 0x2010 && code <= 0x2015) ||
  397. code == 0x2212) /* hyphens, dashes */
  398. emit(out, cap, n, '-');
  399. else if (code == 0x2018 || code == 0x2019)
  400. emit(out, cap, n, '\'');
  401. else if (code == 0x201C || code == 0x201D)
  402. emit(out, cap, n, '"');
  403. else if (code == 0x2026) {
  404. emit(out, cap, n, '.'); emit(out, cap, n, '.');
  405. emit(out, cap, n, '.');
  406. } else if (code <= 0xFF) {
  407. emit(out, cap, n, (char)code);
  408. } else {
  409. emit(out, cap, n, '?');
  410. }
  411. } else {
  412. emit(out, cap, n, '?');
  413. }
  414. *pos = i + 1;
  415. }
  416. /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
  417. * space and drops leading spaces. Returns the output length. */
  418. static unsigned int decode_text(const char *s, unsigned int len, char *out,
  419. unsigned int cap, int collapse)
  420. {
  421. unsigned int i = 0, n = 0;
  422. while (i < len) {
  423. unsigned char c = (unsigned char)s[i];
  424. if (c == '&') {
  425. decode_entity(s, len, &i, out, cap, &n);
  426. continue;
  427. }
  428. if (c < 0x80) {
  429. i++;
  430. if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
  431. c == '\r' || c == '\f')) {
  432. if (n > 0 && out[n - 1] == ' ')
  433. continue;
  434. emit(out, cap, &n, ' ');
  435. continue;
  436. }
  437. if (!collapse && c == '\t') {
  438. emit(out, cap, &n, ' ');
  439. emit(out, cap, &n, ' ');
  440. emit(out, cap, &n, ' ');
  441. emit(out, cap, &n, ' ');
  442. continue;
  443. }
  444. emit(out, cap, &n, (char)c);
  445. continue;
  446. }
  447. /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
  448. if ((c & 0xE0) == 0xC0 && i + 1 < len) {
  449. unsigned int cp = ((c & 0x1F) << 6) |
  450. ((unsigned char)s[i + 1] & 0x3F);
  451. i += 2;
  452. if (cp == 0xA0)
  453. cp = ' ';
  454. else if (cp == 0xB7)
  455. cp = '-';
  456. else if (cp == 0xD7)
  457. cp = 'x';
  458. else if (cp == 0xB0)
  459. cp = 'o';
  460. if (cp <= 0xFF)
  461. emit(out, cap, &n, (char)cp);
  462. else
  463. emit(out, cap, &n, '?');
  464. } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
  465. unsigned int cp = ((c & 0x0F) << 12) |
  466. (((unsigned char)s[i + 1] & 0x3F) << 6) |
  467. ((unsigned char)s[i + 2] & 0x3F);
  468. i += 3;
  469. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212) {
  470. emit(out, cap, &n, '-');
  471. } else if (cp == 0x2018 || cp == 0x2019) {
  472. emit(out, cap, &n, '\'');
  473. } else if (cp == 0x201C || cp == 0x201D) {
  474. emit(out, cap, &n, '"');
  475. } else if (cp == 0x2026) {
  476. emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
  477. emit(out, cap, &n, '.');
  478. } else if (cp == 0x2192) {
  479. emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
  480. } else if (cp == 0x2264) {
  481. emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
  482. } else {
  483. emit(out, cap, &n, '?');
  484. }
  485. } else {
  486. i++;
  487. emit(out, cap, &n, '?');
  488. }
  489. }
  490. out[n] = '\0';
  491. return n;
  492. }
  493. /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
  494. * space / empty. */
  495. /* Utility-class pages (Tailwind) carry 300-600 byte class lists; a short
  496. * cap silently dropped every class past it (arstechnica's header lost its
  497. * background, alignment and all responsive variants). */
  498. #define BR_CLASS_MAX 2048
  499. #define BR_STYLE_ATTR_MAX 2048
  500. static unsigned int store_attr(struct br_doc *d, const char *s,
  501. unsigned int len, unsigned int limit)
  502. {
  503. unsigned int n;
  504. if (d->attrs_used + 2 >= BR_ATTR_SIZE)
  505. return 0;
  506. if (len > limit)
  507. len = limit;
  508. n = decode_text(s, len, d->attrs + d->attrs_used,
  509. BR_ATTR_SIZE - d->attrs_used, 0);
  510. if (n == 0)
  511. return 0;
  512. {
  513. unsigned int off = d->attrs_used;
  514. d->attrs_used += n + 1;
  515. return off;
  516. }
  517. }
  518. static const char *attr_ptr(struct br_doc *d, unsigned int off)
  519. {
  520. return off ? d->attrs + off : "";
  521. }
  522. /* Copy a raw CSS string into the attr arena (no entity decoding). */
  523. static unsigned int store_raw(struct br_doc *d, const char *s,
  524. unsigned int len, unsigned int limit)
  525. {
  526. unsigned int off;
  527. if (len > limit)
  528. len = limit;
  529. if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
  530. return 0;
  531. off = d->attrs_used;
  532. {
  533. unsigned int i;
  534. for (i = 0; i < len; i++)
  535. d->attrs[off + i] = s[i];
  536. }
  537. d->attrs[off + len] = '\0';
  538. d->attrs_used += len + 1;
  539. return off;
  540. }
  541. #define VAR_HASH_SIZE 1024
  542. static unsigned int var_hash(const char *name, unsigned int len)
  543. {
  544. unsigned int i, h = 2166136261u;
  545. for (i = 0; i < len; i++) {
  546. h ^= (unsigned char)name[i];
  547. h *= 16777619u;
  548. }
  549. return h & (VAR_HASH_SIZE - 1);
  550. }
  551. static unsigned int css_var_find(struct br_doc *d, const char *name,
  552. unsigned int nlen)
  553. {
  554. unsigned int h = var_hash(name, nlen);
  555. for (;;) {
  556. unsigned int i = d->var_slot[h];
  557. if (!i)
  558. return BR_MAX_VARS;
  559. i--;
  560. if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
  561. const char *n = attr_ptr(d, d->var_name[i]);
  562. unsigned int j;
  563. for (j = 0; j < nlen && n[j] == name[j]; j++)
  564. ;
  565. if (j == nlen)
  566. return i;
  567. }
  568. h = (h + 1) & (VAR_HASH_SIZE - 1);
  569. }
  570. }
  571. #define BR_VAR_ROOT 1
  572. #define BR_VAR_CONFLICT 2
  573. #define BR_VAR_SCOPED 4 /* some non-root rule defines it */
  574. /* Custom properties live in one document-wide table (var() is expanded
  575. * when a sheet is parsed, not per element). Definitions on :root, html,
  576. * body or * are the theme and always apply, last one winning. A definition
  577. * on any other selector is a local override: it is used while it is the
  578. * only value seen for that name, and a second, different local value marks
  579. * the name ambiguous so var() falls back. Last-wins across components was
  580. * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
  581. * .fa-fw, and every icon became 100% wide. */
  582. static void css_var_set(struct br_doc *d, const char *name,
  583. unsigned int nlen, const char *val, unsigned int vlen,
  584. int root)
  585. {
  586. unsigned int i, noff, voff;
  587. if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
  588. return;
  589. i = css_var_find(d, name, nlen);
  590. if (i < BR_MAX_VARS && !root) {
  591. const char *old = attr_ptr(d, d->var_val[i]);
  592. unsigned int k = 0;
  593. d->var_flags[i] |= BR_VAR_SCOPED;
  594. if (d->var_flags[i] & BR_VAR_ROOT)
  595. return;
  596. while (k < vlen && old[k] == val[k])
  597. k++;
  598. if (!(k == vlen && old[k] == '\0'))
  599. d->var_flags[i] |= BR_VAR_CONFLICT;
  600. return;
  601. }
  602. noff = store_raw(d, name, nlen, 63);
  603. voff = store_raw(d, val, vlen, 199);
  604. if (!noff || !voff)
  605. return;
  606. if (i >= BR_MAX_VARS) {
  607. unsigned int h;
  608. if (d->nvars >= BR_MAX_VARS)
  609. return;
  610. i = d->nvars++;
  611. d->var_name[i] = noff;
  612. h = var_hash(name, nlen);
  613. while (d->var_slot[h])
  614. h = (h + 1) & (VAR_HASH_SIZE - 1);
  615. d->var_slot[h] = (unsigned short)(i + 1);
  616. d->var_flags[i] = 0;
  617. }
  618. d->var_val[i] = voff;
  619. if (root)
  620. d->var_flags[i] = (unsigned char)(BR_VAR_ROOT |
  621. (d->var_flags[i] & BR_VAR_SCOPED));
  622. else
  623. d->var_flags[i] |= BR_VAR_SCOPED;
  624. }
  625. /* 1 when every selector of the list targets the document root (the theme
  626. * scope for custom properties): :root, html, body, * or :host, optionally
  627. * with classes/attributes but no combinator. */
  628. static int css_root_selector(const char *s, unsigned int len)
  629. {
  630. unsigned int i = 0;
  631. while (i < len) {
  632. unsigned int st, en;
  633. while (i < len && (is_space(s[i]) || s[i] == ','))
  634. i++;
  635. if (i >= len)
  636. break;
  637. st = i;
  638. while (i < len && s[i] != ',')
  639. i++;
  640. en = i;
  641. while (en > st && is_space(s[en - 1]))
  642. en--;
  643. if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
  644. (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
  645. (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
  646. (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
  647. s[st] == '*'))
  648. return 0;
  649. for (; st < en; st++)
  650. if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
  651. s[st] == '~')
  652. return 0;
  653. }
  654. return 1;
  655. }
  656. /* Find the end of a declaration value: the next ';' outside quotes and
  657. * parentheses. The caller passes the position just after the ':'. */
  658. static unsigned int css_value_end(const char *s, unsigned int len,
  659. unsigned int i)
  660. {
  661. int depth = 0;
  662. char quote = 0;
  663. while (i < len) {
  664. char c = s[i];
  665. if (quote) {
  666. if (c == quote)
  667. quote = 0;
  668. else if (c == '\\' && i + 1 < len)
  669. i++;
  670. } else if (c == '"' || c == '\'') {
  671. quote = c;
  672. } else if (c == '(') {
  673. depth++;
  674. } else if (c == ')' && depth > 0) {
  675. depth--;
  676. } else if (c == ';' && depth == 0) {
  677. break;
  678. }
  679. i++;
  680. }
  681. return i;
  682. }
  683. /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
  684. * second palette. We render the light page, so skip their custom-property
  685. * definitions; otherwise their last-wins values leak into the light rules. */
  686. static int css_dark_selector(const char *s, unsigned int len)
  687. {
  688. unsigned int i;
  689. for (i = 0; i + 4 <= len; i++) {
  690. if ((s[i] == 'n' || s[i] == 'N') &&
  691. (s[i + 1] == 'i' || s[i + 1] == 'I') &&
  692. (s[i + 2] == 'g' || s[i + 2] == 'G') &&
  693. (s[i + 3] == 'h' || s[i + 3] == 'H') &&
  694. (s[i + 4] == 't' || s[i + 4] == 'T'))
  695. return 1;
  696. if ((s[i] == 'd' || s[i] == 'D') &&
  697. (s[i + 1] == 'a' || s[i + 1] == 'A') &&
  698. (s[i + 2] == 'r' || s[i + 2] == 'R') &&
  699. (s[i + 3] == 'k' || s[i + 3] == 'K'))
  700. return 1;
  701. if (i + 12 <= len &&
  702. (s[i] == 'c' || s[i] == 'C') &&
  703. (s[i + 1] == 'l' || s[i + 1] == 'L') &&
  704. (s[i + 2] == 'i' || s[i + 2] == 'I') &&
  705. (s[i + 3] == 'e' || s[i + 3] == 'E') &&
  706. (s[i + 4] == 'n' || s[i + 4] == 'N') &&
  707. (s[i + 5] == 't' || s[i + 5] == 'T') &&
  708. (s[i + 6] == 'p' || s[i + 6] == 'P') &&
  709. (s[i + 7] == 'r' || s[i + 7] == 'R') &&
  710. (s[i + 8] == 'e' || s[i + 8] == 'E') &&
  711. (s[i + 9] == 'f' || s[i + 9] == 'F') &&
  712. s[i + 10] == '-' &&
  713. (s[i + 11] == 'o' || s[i + 11] == 'O') &&
  714. (s[i + 12] == 's' || s[i + 12] == 'S'))
  715. return 1;
  716. }
  717. return 0;
  718. }
  719. /* Collect --custom-property definitions from one declaration block. */
  720. static void css_collect_block(struct br_doc *d, const char *s,
  721. unsigned int len, int root)
  722. {
  723. unsigned int i = 0;
  724. while (i < len) {
  725. unsigned int pstart, pend, vstart, vend;
  726. while (i < len && (is_space(s[i]) || s[i] == ';'))
  727. i++;
  728. if (i >= len)
  729. break;
  730. pstart = i;
  731. while (i < len && s[i] != ':' && s[i] != ';')
  732. i++;
  733. pend = i;
  734. if (i >= len || s[i] != ':') {
  735. if (i < len)
  736. i++;
  737. continue;
  738. }
  739. i++;
  740. vstart = i;
  741. i = css_value_end(s, len, i);
  742. vend = i;
  743. if (i < len)
  744. i++;
  745. while (pstart < pend && is_space(s[pstart]))
  746. pstart++;
  747. while (pend > pstart && is_space(s[pend - 1]))
  748. pend--;
  749. while (vstart < vend && is_space(s[vstart]))
  750. vstart++;
  751. while (vend > vstart && is_space(s[vend - 1]))
  752. vend--;
  753. if (vend - vstart >= 10 &&
  754. ci_eq_n(s + vend - 10, "!important", 10)) {
  755. vend -= 10;
  756. while (vend > vstart && is_space(s[vend - 1]))
  757. vend--;
  758. }
  759. if (pend - pstart >= 3 && s[pstart] == '-' &&
  760. s[pstart + 1] == '-')
  761. css_var_set(d, s + pstart, pend - pstart,
  762. s + vstart, vend - vstart, root);
  763. }
  764. }
  765. const char *br_href(struct br_doc *d, unsigned int node)
  766. {
  767. return attr_ptr(d, d->nodes[node].href_off);
  768. }
  769. const char *br_src(struct br_doc *d, unsigned int node)
  770. {
  771. return attr_ptr(d, d->nodes[node].src_off);
  772. }
  773. const char *br_alt(struct br_doc *d, unsigned int node)
  774. {
  775. return attr_ptr(d, d->nodes[node].alt_off);
  776. }
  777. const char *br_rel(struct br_doc *d, unsigned int node)
  778. {
  779. return attr_ptr(d, d->nodes[node].rel_off);
  780. }
  781. const char *br_value(struct br_doc *d, unsigned int node)
  782. {
  783. return attr_ptr(d, d->nodes[node].src_off);
  784. }
  785. const char *br_placeholder(struct br_doc *d, unsigned int node)
  786. {
  787. return attr_ptr(d, d->nodes[node].alt_off);
  788. }
  789. const char *br_type(struct br_doc *d, unsigned int node)
  790. {
  791. return attr_ptr(d, d->nodes[node].rel_off);
  792. }
  793. /* ---- document setup / node append ---- */
  794. static void rule_index_reset(struct br_doc *d)
  795. {
  796. unsigned int i;
  797. for (i = 0; i < BR_MAX_RULES; i++)
  798. d->rule_next[i] = 0xFFFF;
  799. for (i = 0; i < BR_RULE_HASH; i++)
  800. d->rule_bucket[i] = 0xFFFF;
  801. for (i = 0; i < BR_TAG_MAX; i++)
  802. d->rule_tag[i] = 0xFFFF;
  803. d->rule_any = 0xFFFF;
  804. }
  805. void br_doc_init(struct br_doc *d)
  806. {
  807. unsigned int i;
  808. for (i = 0; i < sizeof(*d); i++)
  809. ((unsigned char *)d)[i] = 0;
  810. rule_index_reset(d);
  811. d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
  812. d->text = malloc(BR_TEXT_SIZE);
  813. d->attrs = malloc(BR_ATTR_SIZE);
  814. if (!d->nodes || !d->text || !d->attrs) {
  815. free(d->nodes);
  816. free(d->text);
  817. free(d->attrs);
  818. d->nodes = 0;
  819. d->text = 0;
  820. d->attrs = 0;
  821. return;
  822. }
  823. d->rules = rule_fallback;
  824. d->rule_cap = BR_RULE_FALLBACK;
  825. d->attrs_used = 1; /* offset 0 = "no attribute" */
  826. d->attrs[0] = '\0';
  827. d->nvars = 0;
  828. d->nlayers = 0;
  829. d->root = 0;
  830. for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
  831. d->var_slot[vi] = 0;
  832. root_font_px = 16;
  833. bg_url_count = 0;
  834. font_reset();
  835. }
  836. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  837. unsigned int cap)
  838. {
  839. if (!buf || !cap) {
  840. d->rules = rule_fallback;
  841. d->rule_cap = BR_RULE_FALLBACK;
  842. d->nrules = 0;
  843. return;
  844. }
  845. if (cap > BR_MAX_RULES)
  846. cap = BR_MAX_RULES;
  847. d->rules = buf;
  848. d->rule_cap = cap;
  849. d->nrules = 0;
  850. }
  851. static void doc_reset(struct br_doc *d)
  852. {
  853. unsigned int i;
  854. d->nnode = 0;
  855. d->text_used = 0;
  856. d->attrs_used = 1;
  857. d->attrs[0] = '\0';
  858. d->nrules = 0;
  859. d->ncontent = 0;
  860. d->npe_rules = 0;
  861. rule_index_reset(d);
  862. d->title[0] = '\0';
  863. d->inline_css_len = 0;
  864. d->nvars = 0;
  865. d->nlayers = 0;
  866. d->truncated = 0;
  867. d->plain = 0;
  868. d->script_src = 0;
  869. d->nscripts = 0;
  870. d->scripts_truncated = 0;
  871. font_reset();
  872. for (i = 0; i < BR_MAX_NODES; i++) {
  873. d->nodes[i].type = BR_NODE_ELEM;
  874. d->nodes[i].tag = BR_TAG_UNKNOWN;
  875. d->nodes[i].flags = 0;
  876. d->nodes[i].parent = 0;
  877. d->nodes[i].first = d->nodes[i].last = 0;
  878. d->nodes[i].next = 0;
  879. d->nodes[i].text_off = d->nodes[i].text_len = 0;
  880. d->nodes[i].href_off = d->nodes[i].class_off = 0;
  881. d->nodes[i].id_off = d->nodes[i].style_off = 0;
  882. d->nodes[i].src_off = d->nodes[i].alt_off = 0;
  883. d->nodes[i].color_off = d->nodes[i].rel_off = 0;
  884. d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
  885. d->nodes[i].colspan = d->nodes[i].rowspan = 0;
  886. d->nodes[i].name_off = 0;
  887. d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
  888. d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
  889. d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
  890. d->nodes[i].svg_rx = 0;
  891. d->nodes[i].img_index = -1;
  892. d->nodes[i].bg_img_index = -1;
  893. d->nodes[i].style_valid = 0;
  894. d->nodes[i].pe_state = 0;
  895. }
  896. }
  897. static unsigned int node_append(struct br_doc *d, unsigned int parent,
  898. unsigned char type, unsigned char tag)
  899. {
  900. unsigned int idx;
  901. struct br_node *n;
  902. if (d->nnode >= BR_MAX_NODES) {
  903. d->truncated = 1;
  904. return 0;
  905. }
  906. idx = d->nnode++;
  907. n = &d->nodes[idx];
  908. n->type = type;
  909. n->tag = tag;
  910. n->parent = (unsigned short)parent;
  911. n->img_index = -1;
  912. n->bg_img_index = -1;
  913. if (d->nodes[parent].last)
  914. d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
  915. else
  916. d->nodes[parent].first = (unsigned short)idx;
  917. d->nodes[parent].last = (unsigned short)idx;
  918. return idx;
  919. }
  920. static unsigned int append_text(struct br_doc *d, unsigned int parent,
  921. const char *s, unsigned int len, int pre)
  922. {
  923. char *dst;
  924. unsigned int n;
  925. unsigned int idx;
  926. if (len == 0)
  927. return 0;
  928. if (d->text_used + 2 >= BR_TEXT_SIZE) {
  929. d->truncated = 1;
  930. return 0;
  931. }
  932. dst = d->text + d->text_used;
  933. n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
  934. if (n == 0) {
  935. d->text[d->text_used] = '\0';
  936. return 0;
  937. }
  938. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  939. if (!idx)
  940. return 0;
  941. d->nodes[idx].text_off = d->text_used;
  942. d->nodes[idx].text_len = (unsigned short)n;
  943. d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
  944. d->text_used += n + 1;
  945. return idx;
  946. }
  947. /* Raw text child (no entity decoding, whitespace kept): the body of a
  948. * data-block <script> such as type="application/json", which scripts read
  949. * through textContent. Bodies longer than a text node holds are dropped. */
  950. static unsigned int append_raw_text(struct br_doc *d, unsigned int parent,
  951. const char *s, unsigned int len)
  952. {
  953. unsigned int idx, i;
  954. if (len == 0 || len > 0xFFFFu)
  955. return 0;
  956. if (d->text_used + len + 1 >= BR_TEXT_SIZE) {
  957. d->truncated = 1;
  958. return 0;
  959. }
  960. idx = node_append(d, parent, BR_NODE_TEXT, 0);
  961. if (!idx)
  962. return 0;
  963. for (i = 0; i < len; i++)
  964. d->text[d->text_used + i] = s[i];
  965. d->text[d->text_used + len] = '\0';
  966. d->nodes[idx].text_off = d->text_used;
  967. d->nodes[idx].text_len = (unsigned short)len;
  968. d->nodes[idx].flags = BR_NODE_PREFORMAT;
  969. d->text_used += len + 1;
  970. return idx;
  971. }
  972. /* ---- runtime DOM helpers (JavaScript bindings) ---- */
  973. static int same_string(const char *a, const char *b)
  974. {
  975. while (*a && *a == *b) {
  976. a++;
  977. b++;
  978. }
  979. return *a == *b;
  980. }
  981. static int same_string_n(const char *a, const char *b, unsigned int n)
  982. {
  983. unsigned int i;
  984. for (i = 0; i < n; i++)
  985. if (a[i] != b[i])
  986. return 0;
  987. return 1;
  988. }
  989. static int node_detached(struct br_doc *d, unsigned int node)
  990. {
  991. unsigned int guard = 0;
  992. while (node && node < d->nnode && guard++ <= d->nnode) {
  993. if (d->nodes[node].flags & BR_NODE_DETACHED)
  994. return 1;
  995. node = d->nodes[node].parent;
  996. }
  997. return 0;
  998. }
  999. unsigned int br_find_id(struct br_doc *d, const char *id)
  1000. {
  1001. unsigned int i;
  1002. if (!d || !id)
  1003. return 0;
  1004. for (i = 0; i < d->nnode; i++) {
  1005. struct br_node *n = &d->nodes[i];
  1006. if (n->type == BR_NODE_ELEM && n->id_off &&
  1007. same_string(d->attrs + n->id_off, id) &&
  1008. !node_detached(d, i))
  1009. return i + 1;
  1010. }
  1011. return 0;
  1012. }
  1013. static void text_walk(struct br_doc *d, unsigned int node, char *out,
  1014. unsigned int cap, unsigned int *used)
  1015. {
  1016. unsigned int c;
  1017. if (node >= d->nnode)
  1018. return;
  1019. if (d->nodes[node].type == BR_NODE_TEXT) {
  1020. unsigned int len = d->nodes[node].text_len;
  1021. unsigned int off = d->nodes[node].text_off;
  1022. if (*used + len >= cap)
  1023. len = *used + 1 < cap ? cap - *used - 1 : 0;
  1024. for (c = 0; c < len; c++)
  1025. out[*used + c] = d->text[off + c];
  1026. *used += len;
  1027. return;
  1028. }
  1029. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1030. text_walk(d, c, out, cap, used);
  1031. }
  1032. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  1033. char *out, unsigned int cap)
  1034. {
  1035. unsigned int used = 0;
  1036. if (!d || !out || cap == 0 || node >= d->nnode)
  1037. return 0;
  1038. text_walk(d, node, out, cap, &used);
  1039. out[used] = '\0';
  1040. return used;
  1041. }
  1042. int br_set_text_content(struct br_doc *d, unsigned int node,
  1043. const char *text, unsigned int len)
  1044. {
  1045. struct br_node *n;
  1046. unsigned int idx;
  1047. unsigned int i;
  1048. if (!d || node >= d->nnode)
  1049. return -1;
  1050. /* Detach the old subtree; its arena storage is reclaimed on the next
  1051. * navigation. */
  1052. {
  1053. unsigned int c;
  1054. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1055. d->nodes[c].flags |= BR_NODE_DETACHED;
  1056. }
  1057. d->nodes[node].first = d->nodes[node].last = 0;
  1058. d->nodes[node].style_valid = 0;
  1059. d->nodes[node].pe_state = 0;
  1060. if (len == 0)
  1061. return 0;
  1062. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1063. d->truncated = 1;
  1064. return -1;
  1065. }
  1066. idx = node_append(d, node, BR_NODE_TEXT, 0);
  1067. if (!idx)
  1068. return -1;
  1069. for (i = 0; i < len; i++)
  1070. d->text[d->text_used + i] = text[i];
  1071. d->text[d->text_used + len] = '\0';
  1072. n = &d->nodes[idx];
  1073. n->text_off = d->text_used;
  1074. n->text_len = (unsigned short)len;
  1075. n->flags = 0;
  1076. d->text_used += len + 1;
  1077. return 0;
  1078. }
  1079. /* Replace the value of an existing text or comment node in place, keeping the
  1080. * node handle (and therefore the JS wrapper) valid. */
  1081. int br_set_text_node(struct br_doc *d, unsigned int node,
  1082. const char *text, unsigned int len)
  1083. {
  1084. struct br_node *n;
  1085. unsigned int i;
  1086. if (!d || node >= d->nnode)
  1087. return -1;
  1088. n = &d->nodes[node];
  1089. if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
  1090. return -1;
  1091. if (d->text_used + len + 1 > BR_TEXT_SIZE) {
  1092. d->truncated = 1;
  1093. return -1;
  1094. }
  1095. for (i = 0; i < len; i++)
  1096. d->text[d->text_used + i] = text[i];
  1097. d->text[d->text_used + len] = '\0';
  1098. n->text_off = d->text_used;
  1099. n->text_len = (unsigned short)len;
  1100. d->text_used += len + 1;
  1101. return 0;
  1102. }
  1103. /* ---- runtime DOM construction and mutation ---- */
  1104. static unsigned int text_len(const char *s)
  1105. {
  1106. unsigned int n = 0;
  1107. while (s && s[n])
  1108. n++;
  1109. return n;
  1110. }
  1111. static void node_zero(struct br_node *n)
  1112. {
  1113. unsigned int i;
  1114. for (i = 0; i < sizeof(*n); i++)
  1115. ((unsigned char *)n)[i] = 0;
  1116. n->img_index = -1;
  1117. n->bg_img_index = -1;
  1118. }
  1119. static void node_unlink(struct br_doc *d, unsigned int handle)
  1120. {
  1121. unsigned int node, parent, c, prev = 0;
  1122. if (!d || handle == 0 || handle > d->nnode)
  1123. return;
  1124. node = handle - 1;
  1125. parent = d->nodes[node].parent;
  1126. if (parent >= d->nnode) {
  1127. d->nodes[node].parent = 0;
  1128. d->nodes[node].next = 0;
  1129. return;
  1130. }
  1131. c = d->nodes[parent].first;
  1132. while (c && c != node) {
  1133. prev = c;
  1134. c = d->nodes[c].next;
  1135. }
  1136. if (c == node) {
  1137. if (prev)
  1138. d->nodes[prev].next = d->nodes[node].next;
  1139. else
  1140. d->nodes[parent].first = d->nodes[node].next;
  1141. if (d->nodes[parent].last == node)
  1142. d->nodes[parent].last = (unsigned short)prev;
  1143. }
  1144. d->nodes[node].parent = 0;
  1145. d->nodes[node].next = 0;
  1146. d->nodes[node].flags |= BR_NODE_DETACHED;
  1147. }
  1148. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1149. const char *s, unsigned int len);
  1150. static unsigned int find_tag_end(const char *s, unsigned int i,
  1151. unsigned int len)
  1152. {
  1153. char quote = 0;
  1154. while (i < len) {
  1155. char c = s[i];
  1156. if (quote) {
  1157. if (c == quote)
  1158. quote = 0;
  1159. } else if (c == '"' || c == '\'') {
  1160. quote = c;
  1161. } else if (c == '>') {
  1162. return i;
  1163. }
  1164. i++;
  1165. }
  1166. return len;
  1167. }
  1168. int br_set_inner_html(struct br_doc *d, unsigned int node,
  1169. const char *html, unsigned int len)
  1170. {
  1171. unsigned int stack[64];
  1172. unsigned int sp = 1, i = 0;
  1173. if (!d || node >= d->nnode)
  1174. return -1;
  1175. stack[0] = node;
  1176. {
  1177. unsigned int c;
  1178. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1179. d->nodes[c].flags |= BR_NODE_DETACHED;
  1180. }
  1181. d->nodes[node].first = d->nodes[node].last = 0;
  1182. d->nodes[node].style_valid = 0;
  1183. d->nodes[node].pe_state = 0;
  1184. while (i < len) {
  1185. if (html[i] != '<') {
  1186. unsigned int start = i, end;
  1187. while (i < len && html[i] != '<')
  1188. i++;
  1189. end = i;
  1190. if (end > start) {
  1191. /* Entities decoded like the page parser does
  1192. * (createTextNode stays literal). */
  1193. char *tb = (char *)malloc(end - start + 1);
  1194. unsigned int tl = 0, h = 0;
  1195. if (tb) {
  1196. tl = decode_text(html + start, end - start,
  1197. tb, end - start + 1, 1);
  1198. h = br_create_text(d, tb, tl);
  1199. free(tb);
  1200. }
  1201. if (h)
  1202. br_append_child(d, stack[sp - 1], h);
  1203. }
  1204. continue;
  1205. }
  1206. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  1207. html[i + 3] == '-') {
  1208. int p = find_ci(html, len, "-->", 3, i + 4);
  1209. unsigned int end = p >= 0 ? (unsigned int)p : len;
  1210. unsigned int h = br_create_comment(d, html + i + 4,
  1211. end > i + 4 ?
  1212. end - i - 4 : 0);
  1213. if (h)
  1214. br_append_child(d, stack[sp - 1], h);
  1215. i = p >= 0 ? (unsigned int)p + 3 : len;
  1216. continue;
  1217. }
  1218. if (html[i + 1] == '/') {
  1219. unsigned int p = find_tag_end(html, i + 2, len);
  1220. i = p < len ? p + 1 : len;
  1221. if (sp > 1)
  1222. sp--;
  1223. continue;
  1224. }
  1225. if (html[i + 1] == '!' || html[i + 1] == '?') {
  1226. unsigned int p = find_tag_end(html, i + 2, len);
  1227. i = p < len ? p + 1 : len;
  1228. continue;
  1229. }
  1230. {
  1231. unsigned int p = find_tag_end(html, i + 1, len);
  1232. unsigned int ns = i + 1, ne, as, ae;
  1233. unsigned int h, k;
  1234. char name[32];
  1235. int selfclose = 0;
  1236. while (ns < p && is_space(html[ns]))
  1237. ns++;
  1238. ne = ns;
  1239. while (ne < p && !is_space(html[ne]) && html[ne] != '/')
  1240. ne++;
  1241. as = ne;
  1242. ae = p;
  1243. if (ae > as && html[ae - 1] == '/') {
  1244. selfclose = 1;
  1245. ae--;
  1246. }
  1247. if (ne == ns) {
  1248. i = p < len ? p + 1 : len;
  1249. continue;
  1250. }
  1251. if (ne - ns >= sizeof(name)) {
  1252. i = p < len ? p + 1 : len;
  1253. continue;
  1254. }
  1255. for (k = 0; k < ne - ns; k++)
  1256. name[k] = html[ns + k];
  1257. name[ne - ns] = '\0';
  1258. h = br_create_element(d, name, ne - ns);
  1259. if (!h) {
  1260. i = p < len ? p + 1 : len;
  1261. continue;
  1262. }
  1263. if (ae > as)
  1264. parse_attrs(d, &d->nodes[h - 1], html + as,
  1265. ae - as);
  1266. br_append_child(d, stack[sp - 1], h);
  1267. {
  1268. unsigned int tag = d->nodes[h - 1].tag;
  1269. if (!selfclose && !br_tag_is_void(tag) &&
  1270. sp < sizeof(stack) / sizeof(stack[0]))
  1271. stack[sp++] = h - 1;
  1272. }
  1273. i = p < len ? p + 1 : len;
  1274. }
  1275. }
  1276. return 0;
  1277. }
  1278. unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
  1279. {
  1280. unsigned int i;
  1281. if (!d)
  1282. return 0;
  1283. for (i = 0; i < d->nnode; i++)
  1284. if (d->nodes[i].type == BR_NODE_ELEM &&
  1285. d->nodes[i].tag == (unsigned char)tag &&
  1286. !node_detached(d, i))
  1287. return i + 1;
  1288. return 0;
  1289. }
  1290. unsigned int br_create_element(struct br_doc *d, const char *name,
  1291. unsigned int len)
  1292. {
  1293. struct br_node *n;
  1294. unsigned int idx;
  1295. if (!d || !name)
  1296. return 0;
  1297. if (d->nnode >= BR_MAX_NODES) {
  1298. d->truncated = 1;
  1299. return 0;
  1300. }
  1301. idx = d->nnode++;
  1302. n = &d->nodes[idx];
  1303. node_zero(n);
  1304. n->type = BR_NODE_ELEM;
  1305. n->flags = BR_NODE_DETACHED;
  1306. n->tag = (unsigned char)br_tag_from_name(name);
  1307. if (n->tag == BR_TAG_UNKNOWN && len)
  1308. n->name_off = store_attr(d, name, len, 31);
  1309. return idx + 1;
  1310. }
  1311. unsigned int br_create_text(struct br_doc *d, const char *text,
  1312. unsigned int len)
  1313. {
  1314. struct br_node *n;
  1315. unsigned int idx, i;
  1316. if (!d)
  1317. return 0;
  1318. if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
  1319. d->truncated = 1;
  1320. return 0;
  1321. }
  1322. idx = d->nnode++;
  1323. n = &d->nodes[idx];
  1324. node_zero(n);
  1325. n->type = BR_NODE_TEXT;
  1326. n->flags = BR_NODE_DETACHED;
  1327. n->text_off = d->text_used;
  1328. n->text_len = (unsigned short)len;
  1329. for (i = 0; i < len; i++)
  1330. d->text[d->text_used + i] = text[i];
  1331. d->text[d->text_used + len] = '\0';
  1332. d->text_used += len + 1;
  1333. return idx + 1;
  1334. }
  1335. unsigned int br_create_comment(struct br_doc *d, const char *text,
  1336. unsigned int len)
  1337. {
  1338. unsigned int h = br_create_text(d, text, len);
  1339. if (h)
  1340. d->nodes[h - 1].type = BR_NODE_COMMENT;
  1341. return h;
  1342. }
  1343. unsigned int br_create_fragment(struct br_doc *d)
  1344. {
  1345. struct br_node *n;
  1346. unsigned int idx;
  1347. if (!d)
  1348. return 0;
  1349. if (d->nnode >= BR_MAX_NODES) {
  1350. d->truncated = 1;
  1351. return 0;
  1352. }
  1353. idx = d->nnode++;
  1354. n = &d->nodes[idx];
  1355. node_zero(n);
  1356. n->type = BR_NODE_FRAGMENT;
  1357. n->flags = BR_NODE_DETACHED;
  1358. return idx + 1;
  1359. }
  1360. int br_append_child(struct br_doc *d, unsigned int parent,
  1361. unsigned int child)
  1362. {
  1363. unsigned int p;
  1364. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1365. parent == child - 1)
  1366. return -1;
  1367. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1368. /* Appending a fragment inserts its children, not the fragment. */
  1369. unsigned int c = d->nodes[child - 1].first;
  1370. while (c) {
  1371. unsigned int next = d->nodes[c].next;
  1372. br_append_child(d, parent, c + 1);
  1373. c = next;
  1374. }
  1375. return 0;
  1376. }
  1377. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1378. if (p == child - 1)
  1379. return -1;
  1380. node_unlink(d, child);
  1381. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1382. d->nodes[child - 1].parent = (unsigned short)parent;
  1383. d->nodes[child - 1].next = 0;
  1384. if (d->nodes[parent].last)
  1385. d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
  1386. else
  1387. d->nodes[parent].first = (unsigned short)(child - 1);
  1388. d->nodes[parent].last = (unsigned short)(child - 1);
  1389. return 0;
  1390. }
  1391. int br_insert_before(struct br_doc *d, unsigned int parent,
  1392. unsigned int child, unsigned int before)
  1393. {
  1394. unsigned int prev = 0, c;
  1395. if (before == 0)
  1396. return br_append_child(d, parent, child);
  1397. if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
  1398. before > d->nnode || parent == child - 1)
  1399. return -1;
  1400. if (d->nodes[before - 1].parent != parent)
  1401. return -1;
  1402. if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
  1403. unsigned int c = d->nodes[child - 1].first;
  1404. while (c) {
  1405. unsigned int next = d->nodes[c].next;
  1406. br_insert_before(d, parent, c + 1, before);
  1407. c = next;
  1408. }
  1409. return 0;
  1410. }
  1411. {
  1412. unsigned int p;
  1413. for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
  1414. if (p == child - 1)
  1415. return -1;
  1416. }
  1417. node_unlink(d, child);
  1418. d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
  1419. c = d->nodes[parent].first;
  1420. while (c && c != before - 1) {
  1421. prev = c;
  1422. c = d->nodes[c].next;
  1423. }
  1424. if (c != before - 1)
  1425. return -1;
  1426. d->nodes[child - 1].parent = (unsigned short)parent;
  1427. d->nodes[child - 1].next = (unsigned short)(before - 1);
  1428. if (prev)
  1429. d->nodes[prev].next = (unsigned short)(child - 1);
  1430. else
  1431. d->nodes[parent].first = (unsigned short)(child - 1);
  1432. return 0;
  1433. }
  1434. int br_remove_child(struct br_doc *d, unsigned int parent,
  1435. unsigned int child)
  1436. {
  1437. if (!d || child == 0 || child > d->nnode ||
  1438. d->nodes[child - 1].parent != parent)
  1439. return -1;
  1440. node_unlink(d, child);
  1441. return 0;
  1442. }
  1443. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  1444. unsigned int child)
  1445. {
  1446. unsigned int c;
  1447. if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
  1448. return 0;
  1449. for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
  1450. if (c == child - 1)
  1451. return child;
  1452. return 0;
  1453. }
  1454. const char *br_find_attr(struct br_doc *d, unsigned int node,
  1455. const char *name)
  1456. {
  1457. struct br_node *n;
  1458. const char *p, *end, *found = 0;
  1459. if (!d || node >= d->nnode || !name)
  1460. return 0;
  1461. n = &d->nodes[node];
  1462. p = d->attrs + n->extra_off;
  1463. end = p + n->extra_len;
  1464. while (p + 1 < end) {
  1465. const char *an = p;
  1466. const char *av;
  1467. p += text_len(p) + 1;
  1468. if (p >= end)
  1469. break;
  1470. av = p;
  1471. p += text_len(p) + 1;
  1472. if (same_string(an, name))
  1473. found = av;
  1474. }
  1475. if (found)
  1476. return found;
  1477. if (same_string(name, "id") && n->id_off)
  1478. return d->attrs + n->id_off;
  1479. if (same_string(name, "class") && n->class_off)
  1480. return d->attrs + n->class_off;
  1481. if (same_string(name, "style") && n->style_off)
  1482. return d->attrs + n->style_off;
  1483. if (same_string(name, "href") && n->href_off)
  1484. return d->attrs + n->href_off;
  1485. if (same_string(name, "src") && n->src_off)
  1486. return d->attrs + n->src_off;
  1487. if (same_string(name, "alt") && n->alt_off)
  1488. return d->attrs + n->alt_off;
  1489. if (same_string(name, "rel") && n->rel_off)
  1490. return d->attrs + n->rel_off;
  1491. return 0;
  1492. }
  1493. static void attr_note_known(struct br_doc *d, struct br_node *n,
  1494. const char *name, const char *value,
  1495. unsigned int len)
  1496. {
  1497. int control = n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  1498. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA;
  1499. /* Form controls keep type/value/placeholder in the rel/src/alt slots,
  1500. * exactly like parse_attrs(); script-built inputs (Vue sets
  1501. * type="password" after createElement) must land there too. */
  1502. if (control && same_string(name, "type")) {
  1503. n->rel_off = len ? store_attr(d, value, len, 40) : 0;
  1504. return;
  1505. }
  1506. if (control && same_string(name, "value")) {
  1507. n->src_off = len ? store_attr(d, value, len, 220) : 0;
  1508. return;
  1509. }
  1510. if ((n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA) &&
  1511. same_string(name, "placeholder")) {
  1512. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1513. return;
  1514. }
  1515. if (same_string(name, "id"))
  1516. n->id_off = len ? store_attr(d, value, len, 64) : 0;
  1517. else if (same_string(name, "class"))
  1518. n->class_off = len ? store_attr(d, value, len,
  1519. BR_CLASS_MAX) : 0;
  1520. else if (same_string(name, "style"))
  1521. n->style_off = len ? store_attr(d, value, len,
  1522. BR_STYLE_ATTR_MAX) : 0;
  1523. else if (same_string(name, "href"))
  1524. n->href_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1525. else if (same_string(name, "src"))
  1526. n->src_off = len ? store_attr(d, value, len, BR_URL_ATTR_MAX) : 0;
  1527. else if (same_string(name, "alt"))
  1528. n->alt_off = len ? store_attr(d, value, len, 120) : 0;
  1529. else if (same_string(name, "rel"))
  1530. n->rel_off = len ? store_attr(d, value, len, 64) : 0;
  1531. }
  1532. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  1533. const char *value, unsigned int len)
  1534. {
  1535. struct br_node *n;
  1536. unsigned int start, nl, need;
  1537. const char *p, *old, *oldend;
  1538. if (!d || node >= d->nnode || !name)
  1539. return -1;
  1540. n = &d->nodes[node];
  1541. nl = text_len(name);
  1542. need = nl + 1 + len + 1;
  1543. start = d->attrs_used;
  1544. old = d->attrs + n->extra_off;
  1545. oldend = old + n->extra_len;
  1546. if (start + n->extra_len + need > BR_ATTR_SIZE) {
  1547. d->truncated = 1;
  1548. return -1;
  1549. }
  1550. p = old;
  1551. while (p + 1 < oldend) {
  1552. const char *an = p;
  1553. const char *av;
  1554. unsigned int anl, avl, k;
  1555. anl = text_len(an);
  1556. p += anl + 1;
  1557. if (p >= oldend)
  1558. break;
  1559. av = p;
  1560. avl = text_len(av);
  1561. p += avl + 1;
  1562. if (same_string(an, name))
  1563. continue;
  1564. for (k = 0; k < anl; k++)
  1565. d->attrs[d->attrs_used++] = an[k];
  1566. d->attrs[d->attrs_used++] = '\0';
  1567. for (k = 0; k < avl; k++)
  1568. d->attrs[d->attrs_used++] = av[k];
  1569. d->attrs[d->attrs_used++] = '\0';
  1570. }
  1571. {
  1572. unsigned int k;
  1573. for (k = 0; k < nl; k++)
  1574. d->attrs[d->attrs_used++] = name[k];
  1575. d->attrs[d->attrs_used++] = '\0';
  1576. for (k = 0; k < len; k++)
  1577. d->attrs[d->attrs_used++] = value[k];
  1578. d->attrs[d->attrs_used++] = '\0';
  1579. }
  1580. n = &d->nodes[node];
  1581. n->extra_off = start;
  1582. n->extra_len = (unsigned short)(d->attrs_used - start);
  1583. n->style_valid = 0;
  1584. n->pe_state = 0;
  1585. attr_note_known(d, n, name, value, len);
  1586. /* A script that sets a style/class (Angular's ng-style) or a new src
  1587. * after the first image scan re-arms the node for the loader; -2
  1588. * ("nothing to load") would otherwise be final. */
  1589. if ((same_string(name, "style") || same_string(name, "class")) &&
  1590. n->bg_img_index == -2)
  1591. n->bg_img_index = -1;
  1592. if (same_string(name, "src") && n->img_index == -2)
  1593. n->img_index = -1;
  1594. return 0;
  1595. }
  1596. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
  1597. {
  1598. struct br_node *n;
  1599. unsigned int start;
  1600. const char *p, *oldend;
  1601. int found = 0;
  1602. if (!d || node >= d->nnode || !name)
  1603. return -1;
  1604. n = &d->nodes[node];
  1605. start = d->attrs_used;
  1606. p = d->attrs + n->extra_off;
  1607. oldend = p + n->extra_len;
  1608. if (start + n->extra_len > BR_ATTR_SIZE)
  1609. return -1;
  1610. while (p + 1 < oldend) {
  1611. const char *an = p;
  1612. const char *av;
  1613. unsigned int anl, avl, k;
  1614. anl = text_len(an);
  1615. p += anl + 1;
  1616. if (p >= oldend)
  1617. break;
  1618. av = p;
  1619. avl = text_len(av);
  1620. p += avl + 1;
  1621. if (same_string(an, name)) {
  1622. found = 1;
  1623. continue;
  1624. }
  1625. for (k = 0; k < anl; k++)
  1626. d->attrs[d->attrs_used++] = an[k];
  1627. d->attrs[d->attrs_used++] = '\0';
  1628. for (k = 0; k < avl; k++)
  1629. d->attrs[d->attrs_used++] = av[k];
  1630. d->attrs[d->attrs_used++] = '\0';
  1631. }
  1632. n = &d->nodes[node];
  1633. n->extra_off = start;
  1634. n->extra_len = (unsigned short)(d->attrs_used - start);
  1635. n->style_valid = 0;
  1636. n->pe_state = 0;
  1637. attr_note_known(d, n, name, "", 0);
  1638. return found ? 0 : -1;
  1639. }
  1640. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
  1641. {
  1642. struct br_node *src;
  1643. unsigned int h;
  1644. const char *name;
  1645. if (!d || node == 0 || node > d->nnode)
  1646. return 0;
  1647. src = &d->nodes[node - 1];
  1648. if (src->type == BR_NODE_TEXT)
  1649. return br_create_text(d, d->text + src->text_off, src->text_len);
  1650. if (src->type == BR_NODE_COMMENT)
  1651. return br_create_comment(d, d->text + src->text_off,
  1652. src->text_len);
  1653. if (src->type == BR_NODE_FRAGMENT)
  1654. h = br_create_fragment(d);
  1655. else {
  1656. if (src->tag == BR_TAG_UNKNOWN && src->name_off)
  1657. name = d->attrs + src->name_off;
  1658. else
  1659. name = br_tag_name(src->tag);
  1660. h = br_create_element(d, name, text_len(name));
  1661. }
  1662. if (!h)
  1663. return 0;
  1664. {
  1665. const char *p = d->attrs + src->extra_off;
  1666. const char *end = p + src->extra_len;
  1667. while (p + 1 < end) {
  1668. const char *an = p;
  1669. const char *av;
  1670. unsigned int anl, avl;
  1671. anl = text_len(an);
  1672. p += anl + 1;
  1673. if (p >= end)
  1674. break;
  1675. av = p;
  1676. avl = text_len(av);
  1677. p += avl + 1;
  1678. /* h is a 1-based handle; br_set_attr takes the
  1679. * node index (the old call stamped the copy's
  1680. * attributes onto the previous node). */
  1681. br_set_attr(d, h - 1, an, av, avl);
  1682. }
  1683. }
  1684. if (deep) {
  1685. unsigned int c;
  1686. for (c = src->first; c; c = d->nodes[c].next) {
  1687. unsigned int ch = br_clone_node(d, c + 1, 1);
  1688. if (ch)
  1689. br_append_child(d, h - 1, ch);
  1690. }
  1691. }
  1692. return h;
  1693. }
  1694. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  1695. unsigned int len);
  1696. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  1697. unsigned int *st, unsigned int *en);
  1698. /* querySelector(All) and Element.matches use the cascade's selector engine
  1699. * (sel_match(), below). */
  1700. static int match_selector(struct br_doc *d, unsigned int node,
  1701. const char *sel, unsigned int len)
  1702. {
  1703. while (len > 0 && sel[0] == ' ') {
  1704. sel++;
  1705. len--;
  1706. }
  1707. while (len > 0 && sel[len - 1] == ' ')
  1708. len--;
  1709. return len && sel_match(d, node, sel, len);
  1710. }
  1711. static int query_add(unsigned int *out, unsigned int max,
  1712. unsigned int *count, unsigned int node)
  1713. {
  1714. unsigned int i;
  1715. for (i = 0; i < *count; i++)
  1716. if (out[i] == node + 1)
  1717. return 0;
  1718. if (*count < max)
  1719. out[(*count)++] = node + 1;
  1720. return 0;
  1721. }
  1722. static void query_walk(struct br_doc *d, unsigned int node,
  1723. const char *sel, unsigned int len, unsigned int *out,
  1724. unsigned int max, unsigned int *count)
  1725. {
  1726. unsigned int c;
  1727. if (d->nodes[node].type == BR_NODE_ELEM &&
  1728. match_selector(d, node, sel, len))
  1729. query_add(out, max, count, node);
  1730. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  1731. query_walk(d, c, sel, len, out, max, count);
  1732. }
  1733. int br_matches(struct br_doc *d, unsigned int node, const char *selector)
  1734. {
  1735. unsigned int len;
  1736. if (!d || !selector || node == 0 || node > d->nnode)
  1737. return 0;
  1738. len = text_len(selector);
  1739. while (len > 0 && selector[0] == ' ') {
  1740. selector++;
  1741. len--;
  1742. }
  1743. while (len > 0 && selector[len - 1] == ' ')
  1744. len--;
  1745. {
  1746. unsigned int pos = 0, st, en;
  1747. while (sel_list_next(selector, len, &pos, &st, &en))
  1748. if (match_selector(d, node - 1, selector + st, en - st))
  1749. return 1;
  1750. }
  1751. return 0;
  1752. }
  1753. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  1754. const char *selector, unsigned int *out,
  1755. unsigned int max)
  1756. {
  1757. unsigned int count = 0, i = 0, len, start, end;
  1758. if (!d || !selector || !out || max == 0)
  1759. return 0;
  1760. len = text_len(selector);
  1761. while (sel_list_next(selector, len, &i, &start, &end)) {
  1762. if (root == 0) {
  1763. unsigned int n;
  1764. for (n = 1; n < d->nnode; n++)
  1765. if (d->nodes[n].type == BR_NODE_ELEM &&
  1766. !node_detached(d, n) &&
  1767. match_selector(d, n, selector + start,
  1768. end - start))
  1769. query_add(out, max, &count, n);
  1770. } else if (root <= d->nnode) {
  1771. unsigned int c;
  1772. for (c = d->nodes[root - 1].first; c;
  1773. c = d->nodes[c].next)
  1774. query_walk(d, c, selector + start,
  1775. end - start, out, max, &count);
  1776. }
  1777. }
  1778. return count;
  1779. }
  1780. /* ---- attribute parsing ---- */
  1781. static int is_space(char c)
  1782. {
  1783. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  1784. }
  1785. static int str_eq(const char *a, const char *b);
  1786. static unsigned int parse_uint(const char *s, unsigned int len)
  1787. {
  1788. unsigned int i = 0, v = 0;
  1789. while (i < len && is_space(s[i]))
  1790. i++;
  1791. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1792. v = v * 10 + (unsigned int)(s[i] - '0');
  1793. if (v > 4096)
  1794. v = 4096;
  1795. i++;
  1796. }
  1797. return v;
  1798. }
  1799. static int contains_ci_len(const char *s, unsigned int len,
  1800. const char *needle);
  1801. static int parse_svg_viewbox(const char *s, unsigned int len,
  1802. unsigned short *w, unsigned short *h)
  1803. {
  1804. unsigned int i = 0, v[4], n = 0;
  1805. while (i < len && n < 4) {
  1806. unsigned int x = 0;
  1807. int any = 0;
  1808. while (i < len && (is_space(s[i]) || s[i] == ','))
  1809. i++;
  1810. while (i < len && s[i] >= '0' && s[i] <= '9') {
  1811. x = x * 10 + (unsigned int)(s[i] - '0');
  1812. i++;
  1813. any = 1;
  1814. }
  1815. /* viewBox values are commonly fractional ("0 0 340.16
  1816. * 340.16"). Consume the fraction so it is not mistaken for
  1817. * the next number; the integer part is enough for the aspect
  1818. * ratio used by the renderer. */
  1819. if (i < len && s[i] == '.') {
  1820. i++;
  1821. while (i < len && s[i] >= '0' && s[i] <= '9')
  1822. i++;
  1823. }
  1824. if (!any) {
  1825. if (i < len)
  1826. i++;
  1827. continue;
  1828. }
  1829. v[n++] = x;
  1830. }
  1831. if (n < 4)
  1832. return 0;
  1833. *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
  1834. *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
  1835. return *w && *h;
  1836. }
  1837. static void note_attr_flag(struct br_node *n, const char *name)
  1838. {
  1839. if (str_eq(name, "data-collapse-target"))
  1840. n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
  1841. else if (str_eq(name, "data-collapse-trigger"))
  1842. n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
  1843. else if (str_eq(name, "is-open"))
  1844. n->attr_flags |= BR_ATTR_IS_OPEN;
  1845. else if (str_eq(name, "disabled"))
  1846. n->attr_flags |= BR_ATTR_DISABLED;
  1847. else if (str_eq(name, "open"))
  1848. n->attr_flags |= BR_ATTR_OPEN;
  1849. else if (str_eq(name, "inert"))
  1850. n->attr_flags |= BR_ATTR_INERT;
  1851. }
  1852. /* Store a packed name/value list for getAttribute()/selectors. This is a
  1853. * second pass so the block stays contiguous even though the known attributes
  1854. * above also use the shared attr arena. */
  1855. /* Attribute value copy with the markup entities decoded (&amp; &lt; &gt;
  1856. * &quot; &#39; &apos; and ASCII numeric references); other bytes are kept
  1857. * as they are, so URLs and JSON in attributes survive. Returns the bytes
  1858. * written. */
  1859. static unsigned int attr_unescape(const char *s, unsigned int len, char *out)
  1860. {
  1861. static const struct { const char *e; char c; } ents[] = {
  1862. { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' },
  1863. { "&quot;", '"' }, { "&apos;", '\'' }, { "&#39;", '\'' },
  1864. { 0, 0 }
  1865. };
  1866. unsigned int i = 0, n = 0;
  1867. while (i < len) {
  1868. int k, done = 0;
  1869. if (s[i] == '&') {
  1870. for (k = 0; ents[k].e; k++) {
  1871. unsigned int el = (unsigned int)strlen(ents[k].e);
  1872. if (i + el <= len && !memcmp(s + i, ents[k].e, el)) {
  1873. out[n++] = ents[k].c;
  1874. i += el;
  1875. done = 1;
  1876. break;
  1877. }
  1878. }
  1879. if (!done && i + 3 < len && s[i + 1] == '#') {
  1880. unsigned int j = i + 2, v = 0;
  1881. int hex = s[j] == 'x' || s[j] == 'X';
  1882. if (hex)
  1883. j++;
  1884. while (j < len && j < i + 10 &&
  1885. ((s[j] >= '0' && s[j] <= '9') ||
  1886. (hex && ((s[j] >= 'a' && s[j] <= 'f') ||
  1887. (s[j] >= 'A' && s[j] <= 'F'))))) {
  1888. char c = s[j++];
  1889. v = v * (hex ? 16 : 10) +
  1890. (unsigned int)(c <= '9' ? c - '0' :
  1891. (c | 0x20) - 'a' + 10);
  1892. }
  1893. if (j < len && s[j] == ';' && v > 0 && v < 128) {
  1894. out[n++] = (char)v;
  1895. i = j + 1;
  1896. done = 1;
  1897. }
  1898. }
  1899. }
  1900. if (!done)
  1901. out[n++] = s[i++];
  1902. }
  1903. return n;
  1904. }
  1905. static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
  1906. const char *s, unsigned int len)
  1907. {
  1908. unsigned int i = 0;
  1909. unsigned int start = d->attrs_used;
  1910. while (i < len) {
  1911. char name[32];
  1912. unsigned int nl = 0, vstart, vlen;
  1913. char quote = 0;
  1914. while (i < len && is_space(s[i]))
  1915. i++;
  1916. if (i >= len || s[i] == '/')
  1917. break;
  1918. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1919. char c = s[i++];
  1920. if (c >= 'A' && c <= 'Z')
  1921. c = (char)(c - 'A' + 'a');
  1922. if (nl + 1 < sizeof(name))
  1923. name[nl++] = c;
  1924. }
  1925. name[nl] = '\0';
  1926. while (i < len && is_space(s[i]))
  1927. i++;
  1928. if (i < len && s[i] == '=') {
  1929. i++;
  1930. while (i < len && is_space(s[i]))
  1931. i++;
  1932. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  1933. quote = s[i++];
  1934. vstart = i;
  1935. while (i < len && s[i] != quote)
  1936. i++;
  1937. vlen = i - vstart;
  1938. if (i < len)
  1939. i++;
  1940. } else {
  1941. vstart = i;
  1942. while (i < len && !is_space(s[i]) && s[i] != '>')
  1943. i++;
  1944. vlen = i - vstart;
  1945. }
  1946. } else {
  1947. /* Boolean attribute: expose it with an empty value. */
  1948. vstart = 0;
  1949. vlen = 0;
  1950. }
  1951. if (!nl)
  1952. continue;
  1953. if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
  1954. d->truncated = 1;
  1955. break;
  1956. }
  1957. {
  1958. unsigned int k;
  1959. for (k = 0; k < nl; k++)
  1960. d->attrs[d->attrs_used++] = name[k];
  1961. d->attrs[d->attrs_used++] = '\0';
  1962. d->attrs_used += attr_unescape(s + vstart, vlen,
  1963. d->attrs + d->attrs_used);
  1964. d->attrs[d->attrs_used++] = '\0';
  1965. }
  1966. }
  1967. if (d->attrs_used > start) {
  1968. n->extra_off = start;
  1969. n->extra_len = (unsigned short)(d->attrs_used - start);
  1970. }
  1971. }
  1972. /* Parse attributes from `s` (between the tag name and '>'). */
  1973. static void parse_attrs(struct br_doc *d, struct br_node *n,
  1974. const char *s, unsigned int len)
  1975. {
  1976. unsigned int i = 0;
  1977. while (i < len) {
  1978. char name[32];
  1979. unsigned int nl = 0, vstart, vlen;
  1980. char quote = 0;
  1981. while (i < len && is_space(s[i]))
  1982. i++;
  1983. if (i >= len || s[i] == '/')
  1984. break;
  1985. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
  1986. char c = s[i++];
  1987. if (c >= 'A' && c <= 'Z')
  1988. c = (char)(c - 'A' + 'a');
  1989. if (nl + 1 < sizeof(name))
  1990. name[nl++] = c;
  1991. }
  1992. name[nl] = '\0';
  1993. while (i < len && is_space(s[i]))
  1994. i++;
  1995. if (i >= len || s[i] != '=') {
  1996. if (str_eq(name, "hidden"))
  1997. n->flags |= BR_NODE_HIDDEN;
  1998. note_attr_flag(n, name);
  1999. continue;
  2000. }
  2001. i++;
  2002. while (i < len && is_space(s[i]))
  2003. i++;
  2004. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  2005. quote = s[i++];
  2006. vstart = i;
  2007. while (i < len && s[i] != quote)
  2008. i++;
  2009. vlen = i - vstart;
  2010. if (i < len)
  2011. i++;
  2012. } else {
  2013. vstart = i;
  2014. while (i < len && !is_space(s[i]) && s[i] != '>')
  2015. i++;
  2016. vlen = i - vstart;
  2017. }
  2018. if (!nl)
  2019. continue;
  2020. if (str_eq(name, "hidden")) {
  2021. n->flags |= BR_NODE_HIDDEN;
  2022. continue;
  2023. }
  2024. note_attr_flag(n, name);
  2025. if (str_eq(name, "href"))
  2026. n->href_off = store_attr(d, s + vstart, vlen,
  2027. BR_URL_ATTR_MAX);
  2028. else if (str_eq(name, "class"))
  2029. n->class_off = store_attr(d, s + vstart, vlen,
  2030. BR_CLASS_MAX);
  2031. else if (str_eq(name, "id"))
  2032. n->id_off = store_attr(d, s + vstart, vlen, 64);
  2033. else if (str_eq(name, "style"))
  2034. n->style_off = store_attr(d, s + vstart, vlen,
  2035. BR_STYLE_ATTR_MAX);
  2036. else if (str_eq(name, "src"))
  2037. n->src_off = store_attr(d, s + vstart, vlen,
  2038. BR_URL_ATTR_MAX);
  2039. else if (str_eq(name, "alt"))
  2040. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2041. else if (str_eq(name, "color"))
  2042. n->color_off = store_attr(d, s + vstart, vlen, 32);
  2043. else if (str_eq(name, "rel"))
  2044. n->rel_off = store_attr(d, s + vstart, vlen, 64);
  2045. else if (str_eq(name, "value"))
  2046. n->src_off = store_attr(d, s + vstart, vlen, 220);
  2047. else if (str_eq(name, "placeholder") &&
  2048. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
  2049. n->alt_off = store_attr(d, s + vstart, vlen, 120);
  2050. else if (str_eq(name, "type") &&
  2051. (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
  2052. n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
  2053. n->rel_off = store_attr(d, s + vstart, vlen, 40);
  2054. else if (str_eq(name, "width")) {
  2055. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2056. if (vlen && s[vstart + vlen - 1] == '%')
  2057. n->width_pct = 1;
  2058. } else if (str_eq(name, "height")) {
  2059. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2060. if (vlen && s[vstart + vlen - 1] == '%')
  2061. n->height_pct = 1;
  2062. } else if (str_eq(name, "colspan")) {
  2063. unsigned int v = parse_uint(s + vstart, vlen);
  2064. if (v < 1)
  2065. v = 1;
  2066. if (v > 16)
  2067. v = 16;
  2068. n->colspan = (unsigned char)v;
  2069. } else if (str_eq(name, "rowspan")) {
  2070. unsigned int v = parse_uint(s + vstart, vlen);
  2071. if (v < 1)
  2072. v = 1;
  2073. if (v > 16)
  2074. v = 16;
  2075. n->rowspan = (unsigned char)v;
  2076. } else if (str_eq(name, "x") || str_eq(name, "x1")) {
  2077. n->svg_x = (signed short)parse_uint(s + vstart, vlen);
  2078. } else if (str_eq(name, "y") || str_eq(name, "y1")) {
  2079. n->svg_y = (signed short)parse_uint(s + vstart, vlen);
  2080. } else if (str_eq(name, "x2")) {
  2081. n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2082. } else if (str_eq(name, "y2")) {
  2083. n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
  2084. } else if (str_eq(name, "rx")) {
  2085. n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
  2086. } else if (str_eq(name, "d")) {
  2087. n->src_off = store_attr(d, s + vstart, vlen, 300);
  2088. } else if (str_eq(name, "viewbox")) {
  2089. unsigned short vw = 0, vh = 0;
  2090. if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
  2091. n->width_attr = vw;
  2092. n->height_attr = vh;
  2093. n->flags |= BR_NODE_VIEWBOX;
  2094. }
  2095. } else if (str_eq(name, "text-anchor")) {
  2096. if (contains_ci_len(s + vstart, vlen, "middle"))
  2097. n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
  2098. else if (contains_ci_len(s + vstart, vlen, "end"))
  2099. n->attr_flags |= BR_ATTR_ANCHOR_END;
  2100. }
  2101. }
  2102. parse_attrs_generic(d, n, s, len);
  2103. }
  2104. static int str_eq(const char *a, const char *b)
  2105. {
  2106. unsigned int i = 0;
  2107. while (a[i] && b[i] && a[i] == b[i])
  2108. i++;
  2109. return a[i] == '\0' && b[i] == '\0';
  2110. }
  2111. /* ---- CSS ---- */
  2112. static int parse_color(const char *s, unsigned int len, unsigned short *out);
  2113. int br_parse_color(const char *s, unsigned int len, unsigned short *out)
  2114. {
  2115. return parse_color(s, len, out);
  2116. }
  2117. /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
  2118. static int paint_mode(const char *s, unsigned int len, unsigned short *c)
  2119. {
  2120. while (len && is_space(*s)) {
  2121. s++;
  2122. len--;
  2123. }
  2124. while (len && is_space(s[len - 1]))
  2125. len--;
  2126. if (!len)
  2127. return 0;
  2128. if (len == 4 && ci_eq_n(s, "none", 4))
  2129. return BR_PAINT_NONE;
  2130. if (len == 12 && ci_eq_n(s, "currentcolor", 12))
  2131. return BR_PAINT_CURRENT;
  2132. if (len > 4 && ci_eq_n(s, "url(", 4))
  2133. return BR_PAINT_URL;
  2134. if (len == 11 && ci_eq_n(s, "transparent", 11))
  2135. return BR_PAINT_NONE;
  2136. if (parse_color(s, len, c))
  2137. return BR_PAINT_COLOR;
  2138. return 0;
  2139. }
  2140. /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
  2141. static int svg_len16(const char *s, unsigned int len)
  2142. {
  2143. unsigned int i = 0;
  2144. int v = 0, frac = 0, digits = 0;
  2145. while (i < len && is_space(s[i]))
  2146. i++;
  2147. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2148. if (v < 4000)
  2149. v = v * 10 + (s[i] - '0');
  2150. i++;
  2151. digits++;
  2152. }
  2153. if (i < len && s[i] == '.') {
  2154. int scale = 1;
  2155. i++;
  2156. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2157. if (scale < 10000) {
  2158. frac = frac * 10 + (s[i] - '0');
  2159. scale *= 10;
  2160. }
  2161. i++;
  2162. digits++;
  2163. }
  2164. frac = frac * 16 / scale;
  2165. }
  2166. if (!digits)
  2167. return -1;
  2168. return v * 16 + frac;
  2169. }
  2170. /* Inside an <svg> subtree (including the <svg> element itself). */
  2171. static int in_svg(const struct br_doc *d, unsigned int node)
  2172. {
  2173. unsigned int k;
  2174. for (k = 0; node && node < d->nnode && k < 64; k++) {
  2175. if (d->nodes[node].tag == BR_TAG_SVG)
  2176. return 1;
  2177. node = d->nodes[node].parent;
  2178. }
  2179. return 0;
  2180. }
  2181. enum {
  2182. PROP_COLOR, PROP_BG, PROP_BG_NONE, PROP_ALIGN_SELF, PROP_BG_IMAGE,
  2183. PROP_BG_REPEAT,
  2184. PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
  2185. PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
  2186. PROP_FONT_PX, PROP_FONT_UNIT,
  2187. PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
  2188. PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
  2189. PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
  2190. PROP_PT, PROP_PR, PROP_PB, PROP_PL,
  2191. PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
  2192. PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
  2193. PROP_BORDER_W, PROP_BORDER_COLOR,
  2194. PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
  2195. PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
  2196. PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
  2197. PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
  2198. PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
  2199. PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
  2200. PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
  2201. PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
  2202. PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
  2203. PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
  2204. PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
  2205. PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
  2206. PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
  2207. PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
  2208. PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
  2209. PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
  2210. PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
  2211. PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
  2212. PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
  2213. PROP_JUSTIFY, PROP_ALIGN_ITEMS,
  2214. PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
  2215. PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
  2216. PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
  2217. PROP_ZINDEX, PROP_ZSET,
  2218. PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
  2219. PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
  2220. PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
  2221. PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
  2222. PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
  2223. PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
  2224. PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
  2225. PROP_COUNT
  2226. };
  2227. struct color_name {
  2228. const char *name;
  2229. unsigned short rgb;
  2230. };
  2231. static const struct color_name color_names[] = {
  2232. { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
  2233. { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
  2234. { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
  2235. { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
  2236. { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
  2237. { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
  2238. { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
  2239. { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
  2240. { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
  2241. { "darkgrey", 0x528A }, { 0, 0 }
  2242. };
  2243. static int hexval(char c)
  2244. {
  2245. if (c >= '0' && c <= '9')
  2246. return c - '0';
  2247. if (c >= 'a' && c <= 'f')
  2248. return c - 'a' + 10;
  2249. if (c >= 'A' && c <= 'F')
  2250. return c - 'A' + 10;
  2251. return -1;
  2252. }
  2253. static int named_color(const char *s, unsigned int len, unsigned short *out)
  2254. {
  2255. unsigned int i;
  2256. for (i = 0; color_names[i].name; i++) {
  2257. unsigned int j, n = s_len(color_names[i].name);
  2258. if (n != len)
  2259. continue;
  2260. for (j = 0; j < n; j++) {
  2261. char c = s[j];
  2262. if (c >= 'A' && c <= 'Z')
  2263. c = (char)(c - 'A' + 'a');
  2264. if (c != color_names[i].name[j])
  2265. break;
  2266. }
  2267. if (j == n) {
  2268. *out = color_names[i].rgb;
  2269. return 1;
  2270. }
  2271. }
  2272. return 0;
  2273. }
  2274. /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
  2275. * over white; the render target is a white page. */
  2276. static int parse_color(const char *s, unsigned int len, unsigned short *out)
  2277. {
  2278. unsigned int i = 0;
  2279. while (i < len && is_space(s[i]))
  2280. i++;
  2281. if (i < len && s[i] == '#') {
  2282. int d[6], nd = 0;
  2283. i++;
  2284. while (i < len && nd < 6 && hexval(s[i]) >= 0)
  2285. d[nd++] = hexval(s[i++]);
  2286. if (nd == 3) {
  2287. unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
  2288. unsigned int b = (unsigned int)d[2];
  2289. *out = (unsigned short)(((r * 17 >> 3) << 11) |
  2290. ((g * 17 >> 2) << 5) |
  2291. (b * 17 >> 3));
  2292. return 1;
  2293. }
  2294. if (nd >= 6) {
  2295. unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
  2296. unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
  2297. unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
  2298. *out = (unsigned short)(((r >> 3) << 11) |
  2299. ((g >> 2) << 5) | (b >> 3));
  2300. return 1;
  2301. }
  2302. return 0;
  2303. }
  2304. if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
  2305. unsigned int v[3], k = 0, a = 255;
  2306. while (i < len && s[i] != '(')
  2307. i++;
  2308. if (i >= len)
  2309. return 0;
  2310. i++;
  2311. while (k < 3 && i < len) {
  2312. unsigned int x = 0;
  2313. int percent = 0;
  2314. while (i < len && (is_space(s[i]) || s[i] == ','))
  2315. i++;
  2316. if (i >= len || s[i] < '0' || s[i] > '9')
  2317. return 0;
  2318. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2319. x = x * 10 + (unsigned int)(s[i] - '0');
  2320. i++;
  2321. }
  2322. if (i < len && s[i] == '%') {
  2323. percent = 1;
  2324. i++;
  2325. }
  2326. if (percent)
  2327. x = x * 255 / 100;
  2328. if (x > 255)
  2329. x = 255;
  2330. v[k++] = x;
  2331. }
  2332. /* Optional alpha in both syntaxes:
  2333. * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
  2334. {
  2335. unsigned int whole = 0, frac = 0, scale = 1;
  2336. int percent = 0;
  2337. while (i < len && (is_space(s[i]) || s[i] == ','))
  2338. i++;
  2339. if (i < len && s[i] == '/') {
  2340. i++;
  2341. while (i < len && is_space(s[i]))
  2342. i++;
  2343. }
  2344. /* Minified CSS drops the leading zero: rgba(0,0,0,.05)
  2345. * used to read as opaque black. */
  2346. if (i < len && ((s[i] >= '0' && s[i] <= '9') ||
  2347. (s[i] == '.' && i + 1 < len &&
  2348. s[i + 1] >= '0' && s[i + 1] <= '9'))) {
  2349. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2350. whole = whole * 10 +
  2351. (unsigned int)(s[i] - '0');
  2352. i++;
  2353. }
  2354. if (i < len && s[i] == '.') {
  2355. i++;
  2356. while (i < len && s[i] >= '0' &&
  2357. s[i] <= '9') {
  2358. if (scale < 1000) {
  2359. frac = frac * 10 +
  2360. (unsigned int)(s[i] - '0');
  2361. scale *= 10;
  2362. }
  2363. i++;
  2364. }
  2365. }
  2366. while (i < len && is_space(s[i]))
  2367. i++;
  2368. if (i < len && s[i] == '%')
  2369. percent = 1;
  2370. if (percent)
  2371. a = whole > 100 ? 255 :
  2372. whole * 255 / 100;
  2373. else if (whole >= 1)
  2374. a = 255;
  2375. else
  2376. a = frac * 255 / scale;
  2377. }
  2378. }
  2379. if (k == 3) {
  2380. unsigned int r = v[0], g = v[1], b = v[2];
  2381. if (a < 255) {
  2382. r = (r * a + 255u * (255u - a)) / 255u;
  2383. g = (g * a + 255u * (255u - a)) / 255u;
  2384. b = (b * a + 255u * (255u - a)) / 255u;
  2385. }
  2386. *out = (unsigned short)(((r >> 3) << 11) |
  2387. ((g >> 2) << 5) | (b >> 3));
  2388. return 1;
  2389. }
  2390. return 0;
  2391. }
  2392. {
  2393. unsigned int j = i;
  2394. while (i < len && !is_space(s[i]))
  2395. i++;
  2396. return named_color(s + j, i - j, out);
  2397. }
  2398. }
  2399. /* Fully transparent colours have no RGB565 representation. They are common
  2400. * as pre-allocated borders ("border:.1rem solid #0000") that become visible
  2401. * only on hover; painting them black would draw an outline around every
  2402. * pill and card. */
  2403. static int color_is_transparent(const char *s, unsigned int len)
  2404. {
  2405. unsigned int i = 0, n = 0;
  2406. int last = 0, prev = 0, d;
  2407. while (i < len && is_space(s[i]))
  2408. i++;
  2409. if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
  2410. return 1;
  2411. if (i >= len || s[i] != '#')
  2412. return 0;
  2413. i++;
  2414. while (i < len && hexval(s[i]) >= 0) {
  2415. d = hexval(s[i]);
  2416. prev = last;
  2417. last = d;
  2418. n++;
  2419. i++;
  2420. }
  2421. while (i < len && is_space(s[i]))
  2422. i++;
  2423. if (i != len)
  2424. return 0;
  2425. if (n == 4)
  2426. return last == 0;
  2427. if (n == 8)
  2428. return prev == 0 && last == 0;
  2429. return 0;
  2430. }
  2431. static int register_bg_url(const char *s, unsigned int len)
  2432. {
  2433. unsigned int i, j, n;
  2434. while (len && is_space(*s)) {
  2435. s++;
  2436. len--;
  2437. }
  2438. while (len && is_space(s[len - 1]))
  2439. len--;
  2440. if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
  2441. s[len - 1] == s[0]) {
  2442. s++;
  2443. len -= 2;
  2444. }
  2445. if (len > 191)
  2446. len = 191;
  2447. for (i = 0; i < bg_url_count; i++) {
  2448. for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
  2449. ;
  2450. if (j == len && bg_urls[i][j] == '\0')
  2451. return (int)i + 1;
  2452. }
  2453. if (bg_url_count >= BR_BG_URLS || len == 0)
  2454. return 0;
  2455. n = len < 191 ? len : 191;
  2456. for (i = 0; i < n; i++)
  2457. bg_urls[bg_url_count][i] = s[i];
  2458. bg_urls[bg_url_count][n] = '\0';
  2459. return (int)(++bg_url_count);
  2460. }
  2461. /* Length values for margins/padding: px, ignore percentages/units. */
  2462. static unsigned int viewport_w;
  2463. static int parse_len(const char *s, unsigned int len, unsigned int *pos)
  2464. {
  2465. unsigned int i = *pos;
  2466. int sign = 1;
  2467. long v = 0; /* hundredths of a unit before conversion */
  2468. int digits = 0, frac = 0, unit = 0;
  2469. while (i < len && is_space(s[i]))
  2470. i++;
  2471. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2472. if (s[i] == '-')
  2473. sign = -1;
  2474. i++;
  2475. }
  2476. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2477. v = v * 10 + (s[i] - '0');
  2478. digits = 1;
  2479. i++;
  2480. }
  2481. if (i < len && s[i] == '.') {
  2482. i++;
  2483. /* Minified CSS writes ".5rem" without a leading zero. */
  2484. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2485. if (frac < 2) {
  2486. v = v * 10 + (s[i] - '0');
  2487. frac++;
  2488. digits = 1;
  2489. }
  2490. i++;
  2491. }
  2492. }
  2493. while (frac < 2) {
  2494. v *= 10;
  2495. frac++;
  2496. }
  2497. while (frac > 2) {
  2498. v /= 10;
  2499. frac--;
  2500. }
  2501. if (i < len && s[i] == '%')
  2502. unit = 2;
  2503. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2504. s[i + 2] == 'm')
  2505. unit = 1;
  2506. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2507. unit = 3;
  2508. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2509. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2510. s[i] == 'a' || s[i] == 'd'))
  2511. i++;
  2512. *pos = i;
  2513. if (!digits)
  2514. return 0;
  2515. if (unit == 1)
  2516. v *= root_font_px;
  2517. else if (unit == 3)
  2518. v *= root_font_px;
  2519. else if (unit == 2)
  2520. v = v * (long)viewport_w / 100;
  2521. v /= 100;
  2522. v *= sign;
  2523. if (v < -2048)
  2524. v = -2048;
  2525. if (v > 2048)
  2526. v = 2048;
  2527. return (int)v;
  2528. }
  2529. /* Margin values may be the CSS keyword "auto". parse_len() deliberately
  2530. * treats it as a zero-length token; margins need to keep it so block layout
  2531. * can centre a box with a definite width. */
  2532. #define CSS_LEN_AUTO (-32768)
  2533. /* A box length with its unit preserved for layout-time resolution:
  2534. * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
  2535. * rem and whole pixels for px. */
  2536. static int parse_css_len(const char *s, unsigned int len,
  2537. unsigned int *pos, int *unit)
  2538. {
  2539. unsigned int i = *pos;
  2540. int sign = 1;
  2541. long v = 0;
  2542. int digits = 0, frac = 0;
  2543. *unit = 0;
  2544. while (i < len && is_space(s[i]))
  2545. i++;
  2546. if (i < len && (s[i] == '-' || s[i] == '+')) {
  2547. if (s[i] == '-')
  2548. sign = -1;
  2549. i++;
  2550. }
  2551. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2552. v = v * 10 + (s[i] - '0');
  2553. digits = 1;
  2554. i++;
  2555. }
  2556. if (i < len && s[i] == '.') {
  2557. i++;
  2558. /* Leading-dot fractions are common in minified CSS
  2559. * (".5rem", ".3rem"). */
  2560. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2561. if (frac < 2) {
  2562. v = v * 10 + (s[i] - '0');
  2563. frac++;
  2564. digits = 1;
  2565. }
  2566. i++;
  2567. }
  2568. }
  2569. while (frac < 2) {
  2570. v *= 10;
  2571. frac++;
  2572. }
  2573. if (!digits) {
  2574. *pos = i;
  2575. return 0;
  2576. }
  2577. if (i < len && s[i] == '%') {
  2578. *unit = 1;
  2579. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2580. s[i + 2] == 'm') {
  2581. *unit = 3;
  2582. i += 3;
  2583. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  2584. *unit = 2;
  2585. i += 2;
  2586. } else {
  2587. v /= 100;
  2588. }
  2589. while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
  2590. s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
  2591. s[i] == 'a' || s[i] == 'd'))
  2592. i++;
  2593. *pos = i;
  2594. v *= sign;
  2595. if (v < -32768)
  2596. v = -32768;
  2597. if (v > 32767)
  2598. v = 32767;
  2599. return (int)v;
  2600. }
  2601. static int parse_margin_len(const char *s, unsigned int len,
  2602. unsigned int *pos, int *unit)
  2603. {
  2604. unsigned int i = *pos;
  2605. while (i < len && is_space(s[i]))
  2606. i++;
  2607. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
  2608. (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
  2609. *pos = i + 4;
  2610. *unit = 4;
  2611. return CSS_LEN_AUTO;
  2612. }
  2613. return parse_css_len(s, len, pos, unit);
  2614. }
  2615. static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
  2616. /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
  2617. static int parse_margin_calc(const char *s, unsigned int len,
  2618. unsigned int *pos, int *pct, int *px)
  2619. {
  2620. unsigned int i = *pos, j;
  2621. if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
  2622. return 0;
  2623. i += 5;
  2624. j = i;
  2625. while (j < len && s[j] != ')')
  2626. j++;
  2627. if (j >= len)
  2628. return 0;
  2629. if (!parse_calc(s + i, j - i, pct, px))
  2630. return 0;
  2631. *pos = j + 1;
  2632. return 1;
  2633. }
  2634. /* calc(P% +/- Npx) for the common width/margin forms. */
  2635. static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
  2636. {
  2637. unsigned int i = 0;
  2638. int sign = 1;
  2639. int have = 0;
  2640. *pct = 0;
  2641. *px = 0;
  2642. while (i < len) {
  2643. unsigned int j;
  2644. long whole = 0, frac = 0, scale = 1;
  2645. int digits = 0;
  2646. while (i < len && is_space(s[i]))
  2647. i++;
  2648. if (i >= len)
  2649. break;
  2650. if (s[i] == '+' || s[i] == '-') {
  2651. sign = s[i] == '-' ? -1 : 1;
  2652. i++;
  2653. continue;
  2654. }
  2655. if (s[i] < '0' || s[i] > '9') {
  2656. if (s[i] == '.') {
  2657. /* fall through to the number parser */
  2658. } else {
  2659. i++;
  2660. continue;
  2661. }
  2662. }
  2663. j = i;
  2664. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2665. whole = whole * 10 + (s[j] - '0');
  2666. digits = 1;
  2667. j++;
  2668. }
  2669. if (j < len && s[j] == '.') {
  2670. j++;
  2671. while (j < len && s[j] >= '0' && s[j] <= '9') {
  2672. frac = frac * 10 + (s[j] - '0');
  2673. scale *= 10;
  2674. j++;
  2675. }
  2676. }
  2677. if (!digits) {
  2678. i = j + 1;
  2679. continue;
  2680. }
  2681. while (j < len && is_space(s[j]))
  2682. j++;
  2683. if (j < len && s[j] == '%') {
  2684. *pct = sign * (int)(whole + (scale > 1 ?
  2685. frac * 100 / scale : 0));
  2686. have = 1;
  2687. j++;
  2688. } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
  2689. *px = sign * (int)whole;
  2690. have = 1;
  2691. j += 2;
  2692. }
  2693. i = j;
  2694. while (i < len && !is_space(s[i]))
  2695. i++;
  2696. sign = 1;
  2697. }
  2698. return have;
  2699. }
  2700. /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
  2701. static int parse_size(const char *s, unsigned int len, unsigned short *val,
  2702. unsigned char *kind)
  2703. {
  2704. unsigned int i = 0;
  2705. unsigned long v = 0;
  2706. int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
  2707. *val = 0;
  2708. *kind = 0;
  2709. while (i < len && is_space(s[i]))
  2710. i++;
  2711. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
  2712. return 0;
  2713. if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
  2714. return 0;
  2715. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2716. v = v * 10 + (unsigned int)(s[i] - '0');
  2717. digits = 1;
  2718. i++;
  2719. }
  2720. if (i < len && s[i] == '.') {
  2721. i++;
  2722. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2723. if (frac < 2) {
  2724. v = v * 10 + (unsigned int)(s[i] - '0');
  2725. frac++;
  2726. digits = 1;
  2727. }
  2728. i++;
  2729. }
  2730. }
  2731. while (frac < 2) {
  2732. v *= 10;
  2733. frac++;
  2734. }
  2735. while (frac > 2) {
  2736. v /= 10;
  2737. frac--;
  2738. }
  2739. if (!digits)
  2740. return 0;
  2741. while (i < len && is_space(s[i]))
  2742. i++;
  2743. if (i < len && s[i] == '%')
  2744. pct = 1;
  2745. else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  2746. s[i + 2] == 'm')
  2747. rem = 1;
  2748. else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
  2749. rem = 2;
  2750. else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
  2751. vw = 1;
  2752. else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
  2753. ; /* px: already the default scale */
  2754. else if (i < len && (is_ident(s[i]) || s[i] == '#'))
  2755. bad_unit = 1;
  2756. if (bad_unit)
  2757. return 0;
  2758. if (rem == 1 || rem == 2) {
  2759. if (v > 65535)
  2760. v = 65535;
  2761. *val = (unsigned short)v;
  2762. *kind = rem == 1 ? 5 : 4;
  2763. return 1;
  2764. }
  2765. v /= 100;
  2766. if (vw)
  2767. v = v * viewport_w / 100;
  2768. if (pct) {
  2769. if (v > 100)
  2770. v = 100;
  2771. *kind = 2;
  2772. } else {
  2773. if (v > 2048)
  2774. v = 2048;
  2775. *kind = 1;
  2776. }
  2777. *val = (unsigned short)v;
  2778. return 1;
  2779. }
  2780. static int parse_font_px(const char *s, unsigned int len)
  2781. {
  2782. unsigned int i = 0;
  2783. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2784. int v1000, px;
  2785. int is_px = 1, is_rem = 0, is_pct = 0;
  2786. while (i < len && is_space(s[i]))
  2787. i++;
  2788. if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
  2789. return 24;
  2790. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
  2791. return 32;
  2792. if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
  2793. return 14;
  2794. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2795. whole = whole * 10 + (s[i] - '0');
  2796. digits = 1;
  2797. i++;
  2798. }
  2799. if (i < len && s[i] == '.') {
  2800. i++;
  2801. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2802. if (fscale < 1000) {
  2803. frac = frac * 10 + (s[i] - '0');
  2804. fscale *= 10;
  2805. }
  2806. digits = 1;
  2807. i++;
  2808. }
  2809. }
  2810. if (!digits)
  2811. return 0;
  2812. while (i < len && is_space(s[i]))
  2813. i++;
  2814. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2815. is_rem = 1;
  2816. is_px = 0;
  2817. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2818. is_rem = 1;
  2819. is_px = 0;
  2820. } else if (i < len && s[i] == '%') {
  2821. is_pct = 1;
  2822. is_px = 0;
  2823. }
  2824. v1000 = whole * 1000 + frac * 1000 / fscale;
  2825. if (is_rem)
  2826. px = v1000 * root_font_px / 1000;
  2827. else if (is_pct)
  2828. px = v1000 * root_font_px / 100000;
  2829. else if (is_px)
  2830. px = v1000 / 1000;
  2831. else
  2832. px = v1000 / 1000;
  2833. if (px < 8)
  2834. px = 8;
  2835. if (px > 72)
  2836. px = 72;
  2837. return px;
  2838. }
  2839. /* font-size keeping the unit so em/% resolve against the parent face. */
  2840. static int parse_font_size(const char *s, unsigned int len,
  2841. unsigned short *raw, unsigned char *unit)
  2842. {
  2843. unsigned int i = 0;
  2844. int whole = 0, frac = 0, scale = 1, digits = 0;
  2845. int v100;
  2846. *raw = 0;
  2847. *unit = 0;
  2848. while (i < len && is_space(s[i]))
  2849. i++;
  2850. if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
  2851. *raw = 24;
  2852. *unit = 1;
  2853. return 1;
  2854. }
  2855. if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
  2856. *raw = 32;
  2857. *unit = 1;
  2858. return 1;
  2859. }
  2860. if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
  2861. *raw = 14;
  2862. *unit = 1;
  2863. return 1;
  2864. }
  2865. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2866. whole = whole * 10 + (s[i] - '0');
  2867. digits = 1;
  2868. i++;
  2869. }
  2870. if (i < len && s[i] == '.') {
  2871. i++;
  2872. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2873. frac = frac * 10 + (s[i] - '0');
  2874. scale *= 10;
  2875. i++;
  2876. }
  2877. }
  2878. if (!digits && !frac)
  2879. return 0;
  2880. v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  2881. while (i < len && is_space(s[i]))
  2882. i++;
  2883. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2884. *raw = (unsigned short)v100;
  2885. *unit = 3;
  2886. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  2887. *raw = (unsigned short)v100;
  2888. *unit = 2;
  2889. } else if (i < len && s[i] == '%') {
  2890. /* Keep hundredths: 62.5% is a common root size and truncating
  2891. * it to 62% makes every rem 1px too small. */
  2892. *raw = (unsigned short)v100;
  2893. *unit = 4;
  2894. } else {
  2895. *raw = (unsigned short)(v100 / 100);
  2896. *unit = 1;
  2897. }
  2898. return 1;
  2899. }
  2900. static int parse_line_height(const char *s, unsigned int len,
  2901. unsigned short *px, unsigned char *pct)
  2902. {
  2903. unsigned int i = 0;
  2904. int whole = 0, frac = 0, fscale = 1, digits = 0;
  2905. int v1000;
  2906. int is_px = 0, is_pct = 0;
  2907. *px = 0;
  2908. *pct = 0;
  2909. while (i < len && is_space(s[i]))
  2910. i++;
  2911. if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
  2912. return 0;
  2913. while (i < len && s[i] >= '0' && s[i] <= '9') {
  2914. whole = whole * 10 + (s[i] - '0');
  2915. digits = 1;
  2916. i++;
  2917. }
  2918. if (i < len && s[i] == '.') {
  2919. i++;
  2920. while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
  2921. frac = frac * 10 + (s[i] - '0');
  2922. fscale *= 10;
  2923. digits = 1;
  2924. i++;
  2925. }
  2926. }
  2927. if (!digits)
  2928. return 0;
  2929. while (i < len && is_space(s[i]))
  2930. i++;
  2931. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  2932. is_px = 1;
  2933. else if (i < len && s[i] == '%')
  2934. is_pct = 1;
  2935. v1000 = whole * 1000 + frac * 1000 / fscale;
  2936. /* rem is an absolute length (the root font is known by the time
  2937. * rules are parsed, as for font-size); em behaves like a multiplier. */
  2938. if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  2939. v1000 = v1000 * root_font_px;
  2940. is_px = 1;
  2941. }
  2942. if (is_px) {
  2943. int v = v1000 / 1000;
  2944. if (v < 8)
  2945. v = 8;
  2946. if (v > 160)
  2947. v = 160;
  2948. *px = (unsigned short)v;
  2949. } else {
  2950. int v = v1000 / 10; /* unitless multiplier -> percent */
  2951. if (is_pct)
  2952. v = v1000 / 1000;
  2953. if (v < 50)
  2954. v = 50;
  2955. if (v > 255) /* stored in an unsigned char */
  2956. v = 255;
  2957. *pct = (unsigned char)v;
  2958. }
  2959. return 1;
  2960. }
  2961. static int contains_ci_len(const char *s, unsigned int len, const char *needle)
  2962. {
  2963. unsigned int i, j, n = s_len(needle);
  2964. for (i = 0; i + n <= len; i++) {
  2965. for (j = 0; j < n; j++) {
  2966. char a = s[i + j];
  2967. if (a >= 'A' && a <= 'Z')
  2968. a = (char)(a - 'A' + 'a');
  2969. if (a != needle[j])
  2970. break;
  2971. }
  2972. if (j == n)
  2973. return 1;
  2974. }
  2975. return 0;
  2976. }
  2977. /* Pull a colour out of a border/background shorthand. */
  2978. /* Background shorthands can hold several layers before the final solid
  2979. * colour. The colour is conventionally the last token (e.g. a stack of
  2980. * gradients followed by var(--bg)), so take the rightmost colour rather
  2981. * than the first gradient stop. */
  2982. static int find_color_last(const char *s, unsigned int len, unsigned short *c)
  2983. {
  2984. int last = -1;
  2985. unsigned int i;
  2986. for (i = 0; i < len; i++) {
  2987. if (s[i] == '#')
  2988. last = (int)i;
  2989. else if ((s[i] == 'r' || s[i] == 'R') &&
  2990. i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
  2991. last = (int)i;
  2992. }
  2993. if (last >= 0)
  2994. return parse_color(s + last, len - (unsigned int)last, c);
  2995. while (i > 0) {
  2996. unsigned int st, e;
  2997. while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
  2998. s[i - 1] == ')' || s[i - 1] == '('))
  2999. i--;
  3000. e = i;
  3001. while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
  3002. s[i - 1] != ')' && s[i - 1] != '(')
  3003. i--;
  3004. st = i;
  3005. if (e > st && parse_color(s + st, e - st, c))
  3006. return 1;
  3007. }
  3008. return 0;
  3009. }
  3010. static int parse_display(const char *s, unsigned int len)
  3011. {
  3012. unsigned int i;
  3013. while (len && is_space(s[0])) { s++; len--; }
  3014. for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
  3015. ;
  3016. if (i == 4 && ci_eq_n(s, "none", 4))
  3017. return BR_DISPLAY_NONE;
  3018. if (i == 5 && ci_eq_n(s, "block", 5))
  3019. return BR_DISPLAY_BLOCK;
  3020. if (i == 6 && ci_eq_n(s, "inline", 6))
  3021. return BR_DISPLAY_INLINE;
  3022. if (i == 12 && ci_eq_n(s, "inline-block", 12))
  3023. return BR_DISPLAY_INLINE_BLOCK;
  3024. if (i == 10 && ci_eq_n(s, "list-item", 10))
  3025. return BR_DISPLAY_BLOCK;
  3026. if (i == 4 && ci_eq_n(s, "flex", 4))
  3027. return BR_DISPLAY_FLEX;
  3028. if (i == 11 && ci_eq_n(s, "inline-flex", 11))
  3029. return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
  3030. /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
  3031. * block instead of an inline box. */
  3032. if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
  3033. return BR_DISPLAY_BLOCK;
  3034. if (i == 8 && ci_eq_n(s, "-moz-box", 8))
  3035. return BR_DISPLAY_BLOCK;
  3036. if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
  3037. return BR_DISPLAY_FLEX;
  3038. if (i == 5 && ci_eq_n(s, "table", 5))
  3039. return BR_DISPLAY_BLOCK;
  3040. if (i == 10 && ci_eq_n(s, "table-cell", 10))
  3041. return BR_DISPLAY_BLOCK;
  3042. /* Grid shells are common on news/wiki pages, but the two-column
  3043. * heuristic is not reliable for their nested area templates yet.
  3044. * Collapse them to block flow so the content stays readable. */
  3045. if (i == 4 && ci_eq_n(s, "grid", 4))
  3046. return BR_DISPLAY_BLOCK;
  3047. if (i == 11 && ci_eq_n(s, "inline-grid", 11))
  3048. return BR_DISPLAY_BLOCK;
  3049. return BR_DISPLAY_INLINE;
  3050. }
  3051. static void decl_add(struct br_decl *out, unsigned int *nout,
  3052. unsigned char prop, int val, int important)
  3053. {
  3054. if (*nout >= BR_DECLS)
  3055. return;
  3056. out[*nout].prop = prop;
  3057. out[*nout].val = (short)val;
  3058. out[*nout].important = (unsigned char)(important != 0);
  3059. (*nout)++;
  3060. }
  3061. /* One whitespace-separated token. Returns 0 at the end of the value. */
  3062. static int next_token(const char *s, unsigned int len, unsigned int *pos,
  3063. unsigned int *start, unsigned int *end)
  3064. {
  3065. unsigned int i = *pos;
  3066. while (i < len && is_space(s[i]))
  3067. i++;
  3068. if (i >= len)
  3069. return 0;
  3070. *start = i;
  3071. while (i < len && !is_space(s[i]))
  3072. i++;
  3073. *end = i;
  3074. *pos = i;
  3075. return 1;
  3076. }
  3077. static int bg_number(const char *s, unsigned int len, signed short *val,
  3078. unsigned char *kind)
  3079. {
  3080. unsigned int i = 0;
  3081. long v = 0;
  3082. int neg = 0, digits = 0, pct = 0;
  3083. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3084. neg = s[i] == '-';
  3085. i++;
  3086. }
  3087. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3088. v = v * 10 + (s[i] - '0');
  3089. digits = 1;
  3090. i++;
  3091. }
  3092. if (i < len && s[i] == '.') {
  3093. i++;
  3094. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3095. digits = 1;
  3096. i++;
  3097. }
  3098. }
  3099. if (!digits)
  3100. return 0;
  3101. if (i < len && s[i] == '%') {
  3102. pct = 1;
  3103. i++;
  3104. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  3105. i += 2;
  3106. }
  3107. if (i != len)
  3108. return 0;
  3109. if (neg)
  3110. v = -v;
  3111. if (v < -4096)
  3112. v = -4096;
  3113. if (v > 4096)
  3114. v = 4096;
  3115. *val = (signed short)v;
  3116. *kind = (unsigned char)(pct ? 1 : 0);
  3117. return 1;
  3118. }
  3119. static int bg_keyword(const char *s, unsigned int len, signed short *val,
  3120. int *vertical)
  3121. {
  3122. *vertical = 0;
  3123. if (len == 4 && ci_eq_n(s, "left", 4)) {
  3124. *val = 0;
  3125. return 1;
  3126. }
  3127. if (len == 5 && ci_eq_n(s, "right", 5)) {
  3128. *val = 100;
  3129. return 1;
  3130. }
  3131. if (len == 3 && ci_eq_n(s, "top", 3)) {
  3132. *val = 0;
  3133. *vertical = 1;
  3134. return 1;
  3135. }
  3136. if (len == 6 && ci_eq_n(s, "bottom", 6)) {
  3137. *val = 100;
  3138. *vertical = 1;
  3139. return 1;
  3140. }
  3141. if (len == 6 && ci_eq_n(s, "center", 6)) {
  3142. *val = 50;
  3143. return 1;
  3144. }
  3145. return 0;
  3146. }
  3147. static void add_bg_position(struct br_decl *out, unsigned int *nout,
  3148. const char *s, unsigned int len, int important)
  3149. {
  3150. unsigned int pos = 0, ts, te;
  3151. signed short val[2] = { 50, 50 };
  3152. unsigned char kind[2] = { 1, 1 };
  3153. int vert[2] = { 0, 0 }, n = 0;
  3154. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3155. signed short v;
  3156. int is_vert;
  3157. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3158. val[n] = v;
  3159. kind[n] = 1;
  3160. vert[n] = is_vert;
  3161. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3162. val[n] = v;
  3163. vert[n] = 0;
  3164. } else {
  3165. n++;
  3166. continue;
  3167. }
  3168. n++;
  3169. }
  3170. if (n == 1) {
  3171. if (vert[0]) {
  3172. decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
  3173. decl_add(out, nout, PROP_BG_POS_X, 50, important);
  3174. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
  3175. important);
  3176. decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
  3177. } else {
  3178. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
  3179. important);
  3180. decl_add(out, nout, PROP_BG_POS_X, val[0], important);
  3181. decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
  3182. decl_add(out, nout, PROP_BG_POS_Y, 50, important);
  3183. }
  3184. return;
  3185. }
  3186. if (n >= 2) {
  3187. int x = vert[0] && !vert[1] ? 1 : 0;
  3188. int y = 1 - x;
  3189. decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
  3190. decl_add(out, nout, PROP_BG_POS_X, val[x], important);
  3191. decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
  3192. decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
  3193. }
  3194. }
  3195. static void add_bg_size(struct br_decl *out, unsigned int *nout,
  3196. const char *s, unsigned int len, int important)
  3197. {
  3198. unsigned int pos = 0, ts, te;
  3199. signed short w = 0, h = 0;
  3200. unsigned char kw = 0, kh = 0;
  3201. int n = 0;
  3202. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3203. if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
  3204. decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
  3205. return;
  3206. }
  3207. if (n == 0 && te - ts == 7 &&
  3208. ci_eq_n(s + ts, "contain", 7)) {
  3209. decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
  3210. return;
  3211. }
  3212. if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
  3213. n++;
  3214. continue;
  3215. }
  3216. {
  3217. signed short v;
  3218. unsigned char k;
  3219. if (bg_number(s + ts, te - ts, &v, &k)) {
  3220. if (n == 0) {
  3221. w = v;
  3222. kw = k;
  3223. } else {
  3224. h = v;
  3225. kh = k;
  3226. }
  3227. }
  3228. }
  3229. n++;
  3230. }
  3231. decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
  3232. decl_add(out, nout, PROP_BG_SIZE_W, w, important);
  3233. decl_add(out, nout, PROP_BG_SIZE_H, h, important);
  3234. (void)kh;
  3235. }
  3236. /* Parse the `position / size` part of the background shorthand. The image
  3237. * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
  3238. static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
  3239. const char *v, unsigned int len,
  3240. int important)
  3241. {
  3242. unsigned int i, slash = 0, depth = 0, p, first, count;
  3243. char q = 0;
  3244. signed short nv;
  3245. int vert;
  3246. unsigned char kind;
  3247. for (i = 0; i < len; i++) {
  3248. char c = v[i];
  3249. if (q) {
  3250. if (c == q)
  3251. q = 0;
  3252. continue;
  3253. }
  3254. if (c == '\'' || c == '"') {
  3255. q = c;
  3256. continue;
  3257. }
  3258. if (c == '(') {
  3259. depth++;
  3260. continue;
  3261. }
  3262. if (c == ')') {
  3263. if (depth)
  3264. depth--;
  3265. continue;
  3266. }
  3267. if (c == '/' && !depth) {
  3268. slash = i;
  3269. break;
  3270. }
  3271. }
  3272. if (!slash)
  3273. return;
  3274. /* Up to two position tokens immediately before the slash. */
  3275. p = slash;
  3276. first = slash;
  3277. count = 0;
  3278. while (count < 2) {
  3279. unsigned int e;
  3280. while (p > 0 && is_space(v[p - 1]))
  3281. p--;
  3282. e = p;
  3283. while (p > 0 && !is_space(v[p - 1]))
  3284. p--;
  3285. if (e == p)
  3286. break;
  3287. if (bg_keyword(v + p, e - p, &nv, &vert) ||
  3288. bg_number(v + p, e - p, &nv, &kind)) {
  3289. first = p;
  3290. count++;
  3291. } else {
  3292. break;
  3293. }
  3294. }
  3295. if (count)
  3296. add_bg_position(out, nout, v + first, slash - first,
  3297. important);
  3298. /* Up to two size tokens after the slash. */
  3299. p = slash + 1;
  3300. while (p < len && is_space(v[p]))
  3301. p++;
  3302. first = p;
  3303. count = 0;
  3304. while (count < 2) {
  3305. unsigned int ts = p;
  3306. while (p < len && !is_space(v[p]))
  3307. p++;
  3308. if (p == ts)
  3309. break;
  3310. if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
  3311. (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
  3312. (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
  3313. bg_number(v + ts, p - ts, &nv, &kind)) {
  3314. count++;
  3315. while (p < len && is_space(v[p]))
  3316. p++;
  3317. } else {
  3318. break;
  3319. }
  3320. }
  3321. if (count)
  3322. add_bg_size(out, nout, v + first, p - first, important);
  3323. }
  3324. static int parse_fixed_100(const char *s, unsigned int len,
  3325. unsigned int *pos, signed short *out)
  3326. {
  3327. unsigned int i = *pos;
  3328. long whole = 0, frac = 0;
  3329. int neg = 0, digits = 0, fd = 0;
  3330. while (i < len && is_space(s[i]))
  3331. i++;
  3332. if (i < len && (s[i] == '-' || s[i] == '+')) {
  3333. neg = s[i] == '-';
  3334. i++;
  3335. }
  3336. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3337. whole = whole * 10 + (s[i] - '0');
  3338. if (whole > 327)
  3339. whole = 327;
  3340. digits = 1;
  3341. i++;
  3342. }
  3343. if (i < len && s[i] == '.') {
  3344. i++;
  3345. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3346. if (fd < 2) {
  3347. frac = frac * 10 + (s[i] - '0');
  3348. fd++;
  3349. digits = 1;
  3350. }
  3351. i++;
  3352. }
  3353. }
  3354. if (!digits)
  3355. return 0;
  3356. while (fd++ < 2)
  3357. frac *= 10;
  3358. *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
  3359. *pos = i;
  3360. return 1;
  3361. }
  3362. static int find_fn(const char *v, unsigned int len, const char *name,
  3363. unsigned int *arg)
  3364. {
  3365. unsigned int n = s_len(name), i;
  3366. for (i = 0; i + n <= len; i++) {
  3367. unsigned int j;
  3368. if (!ci_eq_n(v + i, name, n))
  3369. continue;
  3370. j = i + n;
  3371. while (j < len && is_space(v[j]))
  3372. j++;
  3373. if (j < len && v[j] == '(') {
  3374. *arg = j + 1;
  3375. return 1;
  3376. }
  3377. }
  3378. return 0;
  3379. }
  3380. static void add_transform_origin(struct br_decl *out, unsigned int *nout,
  3381. const char *s, unsigned int len,
  3382. int important)
  3383. {
  3384. unsigned int pos = 0, ts, te;
  3385. signed short val[2] = { 50, 50 };
  3386. unsigned char kind[2] = { 1, 1 };
  3387. int vert[2] = { 0, 0 }, n = 0;
  3388. while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
  3389. signed short v;
  3390. int is_vert;
  3391. if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
  3392. val[n] = v;
  3393. kind[n] = 1;
  3394. vert[n] = is_vert;
  3395. } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
  3396. val[n] = v;
  3397. vert[n] = 0;
  3398. } else {
  3399. n++;
  3400. continue;
  3401. }
  3402. n++;
  3403. }
  3404. if (n == 1) {
  3405. if (vert[0]) {
  3406. decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
  3407. decl_add(out, nout, PROP_ORIGIN_X, 50, important);
  3408. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
  3409. important);
  3410. decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
  3411. } else {
  3412. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
  3413. important);
  3414. decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
  3415. decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
  3416. decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
  3417. }
  3418. return;
  3419. }
  3420. if (n >= 2) {
  3421. int x = vert[0] && !vert[1] ? 1 : 0;
  3422. int y = 1 - x;
  3423. decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
  3424. decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
  3425. decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
  3426. decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
  3427. }
  3428. }
  3429. static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
  3430. const char *v, unsigned int vl, int important)
  3431. {
  3432. unsigned int i;
  3433. for (i = 0; i + 4 <= vl; i++) {
  3434. if (ci_eq_n(v + i, "url(", 4)) {
  3435. unsigned int st = i + 4, en = st;
  3436. int idx;
  3437. while (en < vl && v[en] != ')')
  3438. en++;
  3439. idx = register_bg_url(v + st, en - st);
  3440. if (idx)
  3441. decl_add(out, nout, PROP_BG_IMAGE, idx,
  3442. important);
  3443. return;
  3444. }
  3445. }
  3446. }
  3447. static int parse_len_token(const char *s, unsigned int len, int *out)
  3448. {
  3449. unsigned int pos = 0;
  3450. int v;
  3451. if (!len)
  3452. return 0;
  3453. if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
  3454. return 0;
  3455. v = parse_len(s, len, &pos);
  3456. if (pos == 0)
  3457. return 0;
  3458. *out = v;
  3459. return 1;
  3460. }
  3461. static void expand_four(int v[4], int n)
  3462. {
  3463. if (n == 1) {
  3464. v[1] = v[2] = v[3] = v[0];
  3465. } else if (n == 2) {
  3466. v[2] = v[0];
  3467. v[3] = v[1];
  3468. } else if (n == 3) {
  3469. v[3] = v[1];
  3470. }
  3471. }
  3472. /* Custom properties defined in the declaration block being expanded. A
  3473. * component that sets a variable and uses it in the same rule (Infima's
  3474. * `.col--3 { --ifm-col-width: calc(3/12*100%); flex: 0 0
  3475. * var(--ifm-col-width) }`) means its own value: the element matching the
  3476. * rule gets it. The document-wide table cannot know that, because
  3477. * `.col` and `.col--N` give the name a dozen different values. */
  3478. static int css_env_active; /* css_local holds an element's variables */
  3479. #define CSS_LOCAL_VARS 48
  3480. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3481. unsigned int i);
  3482. static struct {
  3483. const char *name, *val;
  3484. unsigned short nlen, vlen;
  3485. } css_local[CSS_LOCAL_VARS];
  3486. static unsigned int css_nlocal;
  3487. static int css_local_find(const char *name, unsigned int nlen)
  3488. {
  3489. unsigned int k;
  3490. for (k = 0; k < css_nlocal; k++)
  3491. if (css_local[k].nlen == nlen &&
  3492. !memcmp(css_local[k].name, name, nlen))
  3493. return (int)k;
  3494. return -1;
  3495. }
  3496. static int css_local_find_before(const char *name, unsigned int nlen,
  3497. unsigned int end)
  3498. {
  3499. unsigned int k;
  3500. for (k = 0; k < end; k++)
  3501. if (css_local[k].nlen == nlen &&
  3502. !memcmp(css_local[k].name, name, nlen))
  3503. return (int)k;
  3504. return -1;
  3505. }
  3506. static void css_local_append(const char *s, unsigned int len);
  3507. /* Collect the top-level `--name: value` declarations of a block. */
  3508. static void css_local_collect(const char *s, unsigned int len)
  3509. {
  3510. css_nlocal = 0;
  3511. css_local_append(s, len);
  3512. }
  3513. static void css_local_append(const char *s, unsigned int len)
  3514. {
  3515. unsigned int i = 0;
  3516. while (i < len) {
  3517. unsigned int ns, ne, vs, ve;
  3518. while (i < len && (is_space(s[i]) || s[i] == ';'))
  3519. i++;
  3520. ns = i;
  3521. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  3522. i++;
  3523. if (i >= len || s[i] != ':') {
  3524. if (i < len && s[i] == '{')
  3525. i = css_skip_nested(s, len, i + 1);
  3526. continue;
  3527. }
  3528. ne = i;
  3529. while (ne > ns && is_space(s[ne - 1]))
  3530. ne--;
  3531. vs = i + 1;
  3532. ve = css_value_end(s, len, vs);
  3533. i = ve;
  3534. while (vs < ve && is_space(s[vs]))
  3535. vs++;
  3536. while (ve > vs && is_space(s[ve - 1]))
  3537. ve--;
  3538. if (ve - vs >= 10 && ci_eq_n(s + ve - 10, "!important", 10))
  3539. ve -= 10;
  3540. if (ne - ns < 3 || s[ns] != '-' || s[ns + 1] != '-' ||
  3541. ve <= vs || css_nlocal >= CSS_LOCAL_VARS)
  3542. continue;
  3543. css_local[css_nlocal].name = s + ns;
  3544. css_local[css_nlocal].nlen = (unsigned short)(ne - ns);
  3545. css_local[css_nlocal].val = s + vs;
  3546. css_local[css_nlocal].vlen = (unsigned short)(ve - vs);
  3547. css_nlocal++;
  3548. }
  3549. }
  3550. /* Expand var(--name[, fallback]) references. Returns the expanded length;
  3551. * the input is copied verbatim when it contains no var(). */
  3552. static unsigned int css_expand_vars(struct br_doc *d, const char *s,
  3553. unsigned int len, char *out,
  3554. unsigned int cap, int depth)
  3555. {
  3556. unsigned int i = 0, n = 0;
  3557. if (depth > 8)
  3558. return 0;
  3559. while (i < len && n + 1 < cap) {
  3560. unsigned int p, inner, close, comma, nlen, fstart;
  3561. char quote;
  3562. int parens, glue;
  3563. if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
  3564. out[n++] = s[i++];
  3565. continue;
  3566. }
  3567. p = i + 4;
  3568. quote = 0;
  3569. parens = 1;
  3570. while (p < len && parens) {
  3571. char c = s[p];
  3572. if (quote) {
  3573. if (c == quote)
  3574. quote = 0;
  3575. else if (c == '\\' && p + 1 < len)
  3576. p++;
  3577. } else if (c == '"' || c == '\'') {
  3578. quote = c;
  3579. } else if (c == '(') {
  3580. parens++;
  3581. } else if (c == ')') {
  3582. parens--;
  3583. }
  3584. p++;
  3585. }
  3586. if (parens) {
  3587. out[n++] = s[i++];
  3588. continue;
  3589. }
  3590. close = p - 1;
  3591. inner = i + 4;
  3592. /* var() substitutes tokens: minified Infima writes
  3593. * `font:var(--w)var(--size)/var(--lh)var(--family)`, which
  3594. * must not become the one token `700.875rem`. Separate the
  3595. * value from a glued neighbour on both sides. */
  3596. if (n && n + 1 < cap && !is_space(out[n - 1]) &&
  3597. out[n - 1] != '(' && out[n - 1] != ',' &&
  3598. out[n - 1] != ':' && out[n - 1] != '/')
  3599. out[n++] = ' ';
  3600. glue = p < len && !is_space(s[p]) && s[p] != ')' &&
  3601. s[p] != ',' && s[p] != ';' && s[p] != '/' &&
  3602. s[p] != '!' && s[p] != '}';
  3603. while (inner < close && is_space(s[inner]))
  3604. inner++;
  3605. comma = inner;
  3606. parens = 0;
  3607. quote = 0;
  3608. while (comma < close) {
  3609. char c = s[comma];
  3610. if (quote) {
  3611. if (c == quote)
  3612. quote = 0;
  3613. } else if (c == '"' || c == '\'') {
  3614. quote = c;
  3615. } else if (c == '(') {
  3616. parens++;
  3617. } else if (c == ')' && parens > 0) {
  3618. parens--;
  3619. } else if (c == ',' && parens == 0) {
  3620. break;
  3621. }
  3622. comma++;
  3623. }
  3624. nlen = comma - inner;
  3625. while (nlen && is_space(s[inner + nlen - 1]))
  3626. nlen--;
  3627. fstart = comma < close ? comma + 1 : close;
  3628. while (fstart < close && is_space(s[fstart]))
  3629. fstart++;
  3630. if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
  3631. unsigned int vi = css_var_find(d, s + inner, nlen);
  3632. int li = css_local_find(s + inner, nlen);
  3633. if (li >= 0 && css_local[li].val + css_local[li].vlen !=
  3634. s + close + 1) {
  3635. /* (not the definition's own value) */
  3636. n += css_expand_vars(d, css_local[li].val,
  3637. css_local[li].vlen,
  3638. out + n, cap - n,
  3639. depth + 1);
  3640. i = p;
  3641. if (glue && n + 1 < cap)
  3642. out[n++] = ' ';
  3643. continue;
  3644. }
  3645. if (vi < BR_MAX_VARS &&
  3646. (d->var_flags[vi] & BR_VAR_CONFLICT))
  3647. vi = BR_MAX_VARS;
  3648. if (vi < BR_MAX_VARS) {
  3649. const char *v = attr_ptr(d, d->var_val[vi]);
  3650. unsigned int vn = css_expand_vars(d, v, s_len(v),
  3651. out + n,
  3652. cap - n, depth + 1);
  3653. n += vn;
  3654. } else if (fstart < close) {
  3655. unsigned int vn = css_expand_vars(d, s + fstart,
  3656. close - fstart,
  3657. out + n, cap - n,
  3658. depth + 1);
  3659. n += vn;
  3660. }
  3661. } else if (fstart < close) {
  3662. unsigned int vn = css_expand_vars(d, s + fstart,
  3663. close - fstart,
  3664. out + n, cap - n,
  3665. depth + 1);
  3666. n += vn;
  3667. }
  3668. i = p;
  3669. if (glue && n + 1 < cap)
  3670. out[n++] = ' ';
  3671. }
  3672. out[n] = '\0';
  3673. return n;
  3674. }
  3675. static unsigned int grid_skip_sep(const char *s, unsigned int len,
  3676. unsigned int i)
  3677. {
  3678. while (i < len && (is_space(s[i]) || s[i] == ','))
  3679. i++;
  3680. return i;
  3681. }
  3682. /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
  3683. * argument). Returns 1 on success. */
  3684. static int parse_grid_track(const char *s, unsigned int len,
  3685. unsigned int *pp, unsigned short *val,
  3686. unsigned char *kind)
  3687. {
  3688. unsigned int i = grid_skip_sep(s, len, *pp);
  3689. unsigned int whole = 0, frac = 0, scale = 1;
  3690. int any = 0;
  3691. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3692. unsigned int j = i + 7;
  3693. while (j < len && s[j] != ',')
  3694. j++;
  3695. if (j >= len)
  3696. return 0;
  3697. i = j + 1;
  3698. while (i < len && is_space(s[i]))
  3699. i++;
  3700. }
  3701. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3702. int depth = 1;
  3703. i += 7;
  3704. while (i < len && depth) {
  3705. if (s[i] == '(')
  3706. depth++;
  3707. else if (s[i] == ')')
  3708. depth--;
  3709. i++;
  3710. }
  3711. *pp = i;
  3712. return 0;
  3713. }
  3714. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3715. whole = whole * 10 + (unsigned int)(s[i] - '0');
  3716. i++;
  3717. any = 1;
  3718. }
  3719. if (i < len && s[i] == '.') {
  3720. i++;
  3721. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3722. if (scale < 1000) {
  3723. frac = frac * 10 + (unsigned int)(s[i] - '0');
  3724. scale *= 10;
  3725. }
  3726. i++;
  3727. any = 1;
  3728. }
  3729. }
  3730. if (!any) {
  3731. if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
  3732. *val = 1;
  3733. *kind = 3;
  3734. *pp = i + 4;
  3735. return 1;
  3736. }
  3737. return 0;
  3738. }
  3739. if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
  3740. *val = (unsigned short)(whole ? whole : 1);
  3741. *kind = 3;
  3742. i += 2;
  3743. } else if (i < len && s[i] == '%') {
  3744. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3745. *kind = 2;
  3746. i++;
  3747. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  3748. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3749. *kind = 1;
  3750. i += 3;
  3751. } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  3752. *val = (unsigned short)(whole * 16 + frac * 16 / scale);
  3753. *kind = 1;
  3754. i += 2;
  3755. } else {
  3756. *val = (unsigned short)(whole > 1000 ? 1000 : whole);
  3757. *kind = 1;
  3758. if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
  3759. i += 2;
  3760. }
  3761. while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
  3762. i++;
  3763. *pp = i;
  3764. return 1;
  3765. }
  3766. /* N of "repeat(N, 1fr)" / "repeat(N, minmax(0, 1fr))", or of N >= 3
  3767. * space-separated "1fr" tracks; 0 for anything else (fixed or mixed tracks
  3768. * keep the two-column path). */
  3769. static int grid_repeat_count(const char *s, unsigned int len)
  3770. {
  3771. unsigned int i = 0;
  3772. int n = 0;
  3773. while (i < len && is_space(s[i]))
  3774. i++;
  3775. if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
  3776. i += 7;
  3777. while (i < len && is_space(s[i]))
  3778. i++;
  3779. while (i < len && s[i] >= '0' && s[i] <= '9') {
  3780. n = n * 10 + (s[i] - '0');
  3781. i++;
  3782. }
  3783. if (n < 1 || n > 12)
  3784. return 0;
  3785. while (i < len && (is_space(s[i]) || s[i] == ','))
  3786. i++;
  3787. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3788. return n;
  3789. if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
  3790. i += 7;
  3791. while (i < len && is_space(s[i]))
  3792. i++;
  3793. if (i < len && s[i] == '0') {
  3794. i++;
  3795. while (i < len && (is_space(s[i]) ||
  3796. s[i] == ',' || s[i] == 'p' ||
  3797. s[i] == 'x'))
  3798. i++;
  3799. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3))
  3800. return n;
  3801. }
  3802. }
  3803. return 0;
  3804. }
  3805. /* "1fr 1fr 1fr" */
  3806. for (;;) {
  3807. while (i < len && is_space(s[i]))
  3808. i++;
  3809. if (i >= len)
  3810. break;
  3811. if (i + 3 <= len && ci_eq_n(s + i, "1fr", 3) &&
  3812. (i + 3 == len || is_space(s[i + 3]))) {
  3813. n++;
  3814. i += 3;
  3815. } else {
  3816. return 0;
  3817. }
  3818. }
  3819. return n >= 3 ? n : 0;
  3820. }
  3821. static int parse_grid_cols(const char *s, unsigned int len,
  3822. unsigned short val[2], unsigned char kind[2])
  3823. {
  3824. unsigned int pos = 0;
  3825. int n = 0;
  3826. while (n < 2) {
  3827. unsigned int before = pos;
  3828. if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
  3829. break;
  3830. n++;
  3831. if (pos <= before)
  3832. break;
  3833. while (pos < len && is_space(s[pos]))
  3834. pos++;
  3835. if (pos >= len)
  3836. break;
  3837. }
  3838. return n;
  3839. }
  3840. static int parse_grid_slot(const char *s, unsigned int len)
  3841. {
  3842. if (contains_ci_len(s, len, "footer") ||
  3843. contains_ci_len(s, len, "header") ||
  3844. contains_ci_len(s, len, "sitenotice") ||
  3845. contains_ci_len(s, len, "navbar"))
  3846. return 1;
  3847. if (contains_ci_len(s, len, "columnstart") ||
  3848. contains_ci_len(s, len, "sidebar") ||
  3849. contains_ci_len(s, len, "left") ||
  3850. contains_ci_len(s, len, "start"))
  3851. return 2;
  3852. if (contains_ci_len(s, len, "pagecontent") ||
  3853. contains_ci_len(s, len, "content") ||
  3854. contains_ci_len(s, len, "main") ||
  3855. contains_ci_len(s, len, "right") ||
  3856. contains_ci_len(s, len, "end"))
  3857. return 3;
  3858. return 0;
  3859. }
  3860. static int css_has_var(const char *s, unsigned int len)
  3861. {
  3862. unsigned int i;
  3863. for (i = 0; i + 4 <= len; i++)
  3864. if ((s[i] == 'v' || s[i] == 'V') &&
  3865. ci_eq_n(s + i, "var(", 4))
  3866. return 1;
  3867. return 0;
  3868. }
  3869. static unsigned int css_skip_comment(const char *s, unsigned int len,
  3870. unsigned int i)
  3871. {
  3872. i += 2;
  3873. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  3874. i++;
  3875. if (i + 1 < len)
  3876. i += 2;
  3877. else
  3878. i = len;
  3879. return i;
  3880. }
  3881. static unsigned int css_skip_string(const char *s, unsigned int len,
  3882. unsigned int i)
  3883. {
  3884. char q = s[i++];
  3885. while (i < len && s[i] != q) {
  3886. if (s[i] == '\\' && i + 1 < len)
  3887. i++;
  3888. i++;
  3889. }
  3890. if (i < len)
  3891. i++;
  3892. return i;
  3893. }
  3894. /* Find the '}' that closes a declaration block. i points just past the
  3895. * opening '{'. Comments and quoted strings are skipped: a brace inside
  3896. * one must not unbalance the scan (nd-aktuell's CSS comments contain
  3897. * selectors such as "Highlight-Stories {" without a closing brace). */
  3898. static unsigned int css_block_end(const char *s, unsigned int len,
  3899. unsigned int i)
  3900. {
  3901. int depth = 0;
  3902. while (i < len) {
  3903. char c = s[i];
  3904. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3905. i = css_skip_comment(s, len, i);
  3906. continue;
  3907. }
  3908. if (c == '"' || c == '\'') {
  3909. i = css_skip_string(s, len, i);
  3910. continue;
  3911. }
  3912. if (c == '{') {
  3913. depth++;
  3914. } else if (c == '}') {
  3915. if (depth == 0)
  3916. return i;
  3917. depth--;
  3918. }
  3919. i++;
  3920. }
  3921. return len;
  3922. }
  3923. /* Skip a block whose opening '{' has already been consumed. */
  3924. static unsigned int css_skip_nested(const char *s, unsigned int len,
  3925. unsigned int i)
  3926. {
  3927. int depth = 1;
  3928. while (i < len && depth) {
  3929. char c = s[i];
  3930. if (c == '/' && i + 1 < len && s[i + 1] == '*') {
  3931. i = css_skip_comment(s, len, i);
  3932. continue;
  3933. }
  3934. if (c == '"' || c == '\'') {
  3935. i = css_skip_string(s, len, i);
  3936. continue;
  3937. }
  3938. if (c == '{')
  3939. depth++;
  3940. else if (c == '}')
  3941. depth--;
  3942. i++;
  3943. }
  3944. return i;
  3945. }
  3946. /* ---- @font-face ------------------------------------------------------- */
  3947. static void font_trim(const char **sp, unsigned int *len)
  3948. {
  3949. const char *s = *sp;
  3950. unsigned int n = *len;
  3951. while (n && is_space(*s)) {
  3952. s++;
  3953. n--;
  3954. }
  3955. while (n && is_space(s[n - 1]))
  3956. n--;
  3957. if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
  3958. s++;
  3959. n -= 2;
  3960. }
  3961. *sp = s;
  3962. *len = n;
  3963. }
  3964. static void font_add(const char *family, unsigned int flen,
  3965. const char *url, unsigned int ulen)
  3966. {
  3967. unsigned int i, n = 0, k;
  3968. font_trim(&family, &flen);
  3969. font_trim(&url, &ulen);
  3970. if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
  3971. return;
  3972. for (i = 0; i < (unsigned int)font_face_count; i++) {
  3973. struct br_font_def *f = &font_defs[i];
  3974. if (s_len(f->family) != flen)
  3975. continue;
  3976. for (k = 0; k < flen; k++) {
  3977. char a = f->family[k], b = family[k];
  3978. if (a >= 'A' && a <= 'Z')
  3979. a = (char)(a - 'A' + 'a');
  3980. if (b >= 'A' && b <= 'Z')
  3981. b = (char)(b - 'A' + 'a');
  3982. if (a != b)
  3983. break;
  3984. }
  3985. if (k != flen)
  3986. continue;
  3987. n = 0;
  3988. while (url[n] && n + 1 < sizeof(f->url)) {
  3989. f->url[n] = url[n];
  3990. n++;
  3991. }
  3992. f->url[n] = '\0';
  3993. n = 0;
  3994. while (font_base[n] && n + 1 < sizeof(f->base)) {
  3995. f->base[n] = font_base[n];
  3996. n++;
  3997. }
  3998. f->base[n] = '\0';
  3999. return;
  4000. }
  4001. if (font_face_count >= BR_MAX_FACES)
  4002. return;
  4003. {
  4004. struct br_font_def *f = &font_defs[font_face_count];
  4005. for (n = 0; n < flen; n++)
  4006. f->family[n] = family[n];
  4007. f->family[n] = '\0';
  4008. for (n = 0; n < ulen; n++)
  4009. f->url[n] = url[n];
  4010. f->url[n] = '\0';
  4011. for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
  4012. f->base[n] = font_base[n];
  4013. f->base[n] = '\0';
  4014. f->used = 1;
  4015. font_face_count++;
  4016. }
  4017. }
  4018. /* Look up the first name in a font-family list that has an @font-face. */
  4019. static int font_family_id(const char *s, unsigned int len)
  4020. {
  4021. unsigned int i = 0;
  4022. while (i < len) {
  4023. unsigned int start, end, q = 0;
  4024. while (i < len && (is_space(s[i]) || s[i] == ','))
  4025. i++;
  4026. if (i >= len)
  4027. break;
  4028. if (s[i] == '"' || s[i] == '\'') {
  4029. char quote = s[i++];
  4030. start = i;
  4031. while (i < len && s[i] != quote)
  4032. i++;
  4033. end = i;
  4034. if (i < len)
  4035. i++;
  4036. } else {
  4037. start = i;
  4038. while (i < len && s[i] != ',')
  4039. i++;
  4040. end = i;
  4041. while (end > start && is_space(s[end - 1]))
  4042. end--;
  4043. }
  4044. if (end > start) {
  4045. for (q = 0; q < (unsigned int)font_face_count; q++) {
  4046. const char *f = font_defs[q].family;
  4047. unsigned int fl = s_len(f), k;
  4048. if (fl != end - start)
  4049. continue;
  4050. for (k = 0; k < fl; k++) {
  4051. char a = f[k], b = s[start + k];
  4052. if (a >= 'A' && a <= 'Z')
  4053. a = (char)(a - 'A' + 'a');
  4054. if (b >= 'A' && b <= 'Z')
  4055. b = (char)(b - 'A' + 'a');
  4056. if (a != b)
  4057. break;
  4058. }
  4059. if (k == fl)
  4060. return (int)q + 1;
  4061. }
  4062. }
  4063. }
  4064. return 0;
  4065. }
  4066. /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
  4067. * at-rules, so faces must be registered before any font-family declaration
  4068. * in the same sheet is resolved. */
  4069. static void scan_font_faces(const char *s, unsigned int len)
  4070. {
  4071. unsigned int i = 0;
  4072. while (i + 10 <= len) {
  4073. unsigned int j, end;
  4074. char family[BR_FACE_FAMILY], url[BR_FACE_URL];
  4075. unsigned int flen = 0, ulen = 0, p;
  4076. int have_family = 0, have_url = 0;
  4077. if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
  4078. i++;
  4079. continue;
  4080. }
  4081. j = i + 10;
  4082. while (j < len && s[j] != '{' && s[j] != ';')
  4083. j++;
  4084. if (j >= len || s[j] == ';') {
  4085. i = j + 1;
  4086. continue;
  4087. }
  4088. end = css_skip_nested(s, len, j + 1);
  4089. p = j + 1;
  4090. while (p < end) {
  4091. unsigned int ds, de, vs, ve;
  4092. while (p < end && (is_space(s[p]) || s[p] == ';'))
  4093. p++;
  4094. if (p >= end)
  4095. break;
  4096. ds = p;
  4097. while (p < end && s[p] != ':' && s[p] != ';')
  4098. p++;
  4099. de = p;
  4100. if (p >= end || s[p] != ':') {
  4101. while (p < end && s[p] != ';')
  4102. p++;
  4103. continue;
  4104. }
  4105. p++;
  4106. vs = p;
  4107. while (p < end && s[p] != ';')
  4108. p++;
  4109. ve = p;
  4110. while (ds < de && is_space(s[ds]))
  4111. ds++;
  4112. while (de > ds && is_space(s[de - 1]))
  4113. de--;
  4114. while (vs < ve && is_space(s[vs]))
  4115. vs++;
  4116. while (ve > vs && is_space(s[ve - 1]))
  4117. ve--;
  4118. if (de - ds == 11 &&
  4119. ci_eq_n(s + ds, "font-family", 11)) {
  4120. const char *fp = s + vs;
  4121. unsigned int fn = ve - vs;
  4122. font_trim(&fp, &fn);
  4123. if (fn && fn < sizeof(family)) {
  4124. unsigned int k;
  4125. for (k = 0; k < fn; k++)
  4126. family[k] = fp[k];
  4127. family[fn] = '\0';
  4128. flen = fn;
  4129. have_family = 1;
  4130. }
  4131. } else if (de - ds == 3 &&
  4132. ci_eq_n(s + ds, "src", 3)) {
  4133. unsigned int q = vs;
  4134. while (q + 4 <= ve) {
  4135. unsigned int us, ue;
  4136. if (!ci_eq_n(s + q, "url(", 4)) {
  4137. q++;
  4138. continue;
  4139. }
  4140. q += 4;
  4141. us = q;
  4142. while (q < ve && s[q] != ')')
  4143. q++;
  4144. ue = q;
  4145. {
  4146. const char *up = s + us;
  4147. unsigned int un = ue - us;
  4148. font_trim(&up, &un);
  4149. if (un && un < sizeof(url) &&
  4150. !contains_ci_len(up, un,
  4151. "woff")) {
  4152. unsigned int k;
  4153. for (k = 0; k < un; k++)
  4154. url[k] = up[k];
  4155. url[un] = '\0';
  4156. ulen = un;
  4157. have_url = 1;
  4158. break;
  4159. }
  4160. }
  4161. }
  4162. }
  4163. }
  4164. if (have_family && have_url)
  4165. font_add(family, flen, url, ulen);
  4166. i = end;
  4167. }
  4168. }
  4169. /* One border-radius length. Unlike parse_size(), percentages keep their
  4170. * kind: the drawer scales them to the box, not the viewport. */
  4171. static int parse_radius_value(const char *s, unsigned int len,
  4172. unsigned int *pos, unsigned short *val,
  4173. unsigned char *kind)
  4174. {
  4175. unsigned int i = *pos;
  4176. unsigned long v = 0;
  4177. int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
  4178. while (i < len && is_space(s[i]))
  4179. i++;
  4180. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4181. v = v * 10 + (unsigned int)(s[i] - '0');
  4182. digits = 1;
  4183. i++;
  4184. }
  4185. if (i < len && s[i] == '.') {
  4186. i++;
  4187. while (i < len && s[i] >= '0' && s[i] <= '9') {
  4188. fdigits = 1;
  4189. if (frac < 2) {
  4190. v = v * 10 + (unsigned int)(s[i] - '0');
  4191. frac++;
  4192. }
  4193. i++;
  4194. }
  4195. }
  4196. while (frac < 2) {
  4197. v *= 10;
  4198. frac++;
  4199. }
  4200. while (i < len && is_space(s[i]))
  4201. i++;
  4202. if (i < len && s[i] == '%') {
  4203. pct = 1;
  4204. i++;
  4205. } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
  4206. s[i + 2] == 'm') {
  4207. rem = 1;
  4208. i += 3;
  4209. } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
  4210. rem = 2;
  4211. i += 2;
  4212. } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
  4213. i += 2;
  4214. } else if (i < len && is_ident(s[i])) {
  4215. return 0;
  4216. }
  4217. if (!digits && !fdigits)
  4218. return 0;
  4219. if (rem == 1)
  4220. v *= root_font_px;
  4221. else if (rem == 2)
  4222. v *= root_font_px;
  4223. v /= 100;
  4224. if (v > 2048)
  4225. v = 2048;
  4226. *val = (unsigned short)v;
  4227. *kind = pct ? 2 : 1;
  4228. *pos = i;
  4229. return 1;
  4230. }
  4231. /* font: [style] [variant] [weight] size[/line-height] family */
  4232. static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
  4233. const char *v, unsigned int len, int important)
  4234. {
  4235. unsigned int pos = 0, ts, te;
  4236. unsigned short size_raw = 0;
  4237. unsigned char size_unit = 0;
  4238. int have_size = 0;
  4239. int bold = 0, italic = 0;
  4240. int lh_px = 0, lh_pct = 0, mono = 0;
  4241. while (next_token(v, len, &pos, &ts, &te)) {
  4242. unsigned int tl = te - ts;
  4243. unsigned int slash = 0;
  4244. if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
  4245. bold = 1;
  4246. continue;
  4247. }
  4248. if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
  4249. continue;
  4250. if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
  4251. italic = 1;
  4252. continue;
  4253. }
  4254. if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
  4255. italic = 1;
  4256. continue;
  4257. }
  4258. if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
  4259. continue;
  4260. for (slash = 0; slash < tl; slash++)
  4261. if (v[ts + slash] == '/')
  4262. break;
  4263. if (slash == tl) {
  4264. unsigned int q;
  4265. int weight = 0;
  4266. for (q = 0; q < tl; q++) {
  4267. if (v[ts + q] < '0' || v[ts + q] > '9')
  4268. break;
  4269. weight = weight * 10 + (v[ts + q] - '0');
  4270. }
  4271. if (q == tl && weight >= 100 && weight <= 900) {
  4272. bold = weight >= 500;
  4273. continue;
  4274. }
  4275. }
  4276. {
  4277. unsigned short raw;
  4278. unsigned char unit;
  4279. if (!parse_font_size(v + ts, slash < tl ? slash : tl,
  4280. &raw, &unit))
  4281. continue;
  4282. size_raw = raw;
  4283. size_unit = unit;
  4284. }
  4285. have_size = 1;
  4286. if (slash < tl) {
  4287. unsigned int lstart = ts + slash + 1;
  4288. unsigned short lhp = 0;
  4289. unsigned char lhpct = 0;
  4290. if (!parse_line_height(v + lstart, te - lstart,
  4291. &lhp, &lhpct)) {
  4292. lh_px = 0;
  4293. lh_pct = 0;
  4294. } else {
  4295. lh_px = lhp;
  4296. lh_pct = lhpct;
  4297. }
  4298. }
  4299. }
  4300. if (!have_size)
  4301. return;
  4302. mono = contains_ci_len(v, len, "mono");
  4303. decl_add(out, nout, PROP_FONT_PX, size_raw, important);
  4304. decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
  4305. decl_add(out, nout, PROP_BOLD, bold, important);
  4306. decl_add(out, nout, PROP_ITALIC, italic, important);
  4307. decl_add(out, nout, PROP_MONO, mono, important);
  4308. decl_add(out, nout, PROP_FONT_FACE, 0, important);
  4309. decl_add(out, nout, PROP_LH_PX, lh_px, important);
  4310. decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
  4311. }
  4312. static int css_collect_only;
  4313. /* Cascade layer of the rules being parsed: 0 = unlayered, else the 1-based
  4314. * index into d->layer_hash. css_layer_name is the dotted path of the
  4315. * enclosing @layer blocks, so a nested `@layer b` inside `@layer a` is the
  4316. * layer "a.b". Layers are ranked flat by first appearance; a parent
  4317. * layer's own rules therefore lose to its sublayers (the spec says the
  4318. * reverse), which no page we render depends on. */
  4319. static unsigned char css_cur_layer;
  4320. static char css_layer_name[128];
  4321. static unsigned int css_anon_layers;
  4322. static unsigned int layer_name_hash(const char *s, unsigned int len)
  4323. {
  4324. unsigned int h = 2166136261u, i;
  4325. for (i = 0; i < len; i++) {
  4326. h ^= (unsigned char)s[i];
  4327. h *= 16777619u;
  4328. }
  4329. return h ? h : 1;
  4330. }
  4331. /* Register (or look up) the layer `name` nested under the current path and
  4332. * return its 1-based rank; 0 when the table is full (rules then cascade as
  4333. * unlayered, the old behaviour). `name` empty means an anonymous layer. */
  4334. static unsigned char css_layer_register(struct br_doc *d, const char *name,
  4335. unsigned int nlen, char *path,
  4336. unsigned int cap)
  4337. {
  4338. unsigned int pl = s_len(css_layer_name), i, h;
  4339. char anon[16];
  4340. while (nlen && is_space(*name)) { name++; nlen--; }
  4341. while (nlen && is_space(name[nlen - 1])) nlen--;
  4342. if (!nlen) {
  4343. /* An anonymous layer is unique; it can never be reopened.
  4344. * The collect-only (custom property) pass does not need a
  4345. * rank, so it must not burn a table slot. */
  4346. unsigned int n = ++css_anon_layers, k = 0;
  4347. char rev[10];
  4348. if (css_collect_only)
  4349. return 0;
  4350. anon[0] = '#';
  4351. while (n && k < sizeof(rev)) {
  4352. rev[k++] = (char)('0' + n % 10);
  4353. n /= 10;
  4354. }
  4355. for (i = 0; i < k; i++)
  4356. anon[1 + i] = rev[k - 1 - i];
  4357. name = anon;
  4358. nlen = k + 1;
  4359. }
  4360. if (pl + 1 + nlen >= cap)
  4361. return 0;
  4362. memcpy(path, css_layer_name, pl);
  4363. if (pl)
  4364. path[pl++] = '.';
  4365. memcpy(path + pl, name, nlen);
  4366. path[pl + nlen] = '\0';
  4367. h = layer_name_hash(path, pl + nlen);
  4368. for (i = 0; i < d->nlayers; i++)
  4369. if (d->layer_hash[i] == h)
  4370. return (unsigned char)(i + 1);
  4371. if (d->nlayers >= BR_MAX_LAYERS)
  4372. return 0;
  4373. d->layer_hash[d->nlayers++] = h;
  4374. return (unsigned char)d->nlayers;
  4375. }
  4376. /* @supports: the engine cannot answer feature queries, so the block
  4377. * applies unless it is negated (`not (...)` usually guards a fallback for
  4378. * old browsers) or asks for selector() support. */
  4379. static int supports_matches(const char *s, unsigned int len)
  4380. {
  4381. while (len && is_space(*s)) { s++; len--; }
  4382. if (len >= 3 && ci_eq_n(s, "not", 3) &&
  4383. (len == 3 || is_space(s[3]) || s[3] == '('))
  4384. return 0;
  4385. if (contains_ci_len(s, len, "selector("))
  4386. return 0;
  4387. return 1;
  4388. }
  4389. /* Append one code point of generated content as Latin-1. Typography the
  4390. * fonts lack gets an ASCII stand-in; private-use code points (icon fonts)
  4391. * and anything else outside Latin-1 are dropped. */
  4392. static void content_put(char *out, unsigned int *n, unsigned int cap,
  4393. unsigned int cp)
  4394. {
  4395. const char *sub = 0;
  4396. if ((cp >= 0x2010 && cp <= 0x2015) || cp == 0x2212)
  4397. sub = "-";
  4398. else if (cp == 0x2018 || cp == 0x2019 || cp == 0x201A)
  4399. sub = "'";
  4400. else if (cp == 0x201C || cp == 0x201D || cp == 0x201E)
  4401. sub = "\"";
  4402. else if (cp == 0x2022 || cp == 0x00B7 || cp == 0x2027 || cp == 0x25CF)
  4403. sub = "\xB7";
  4404. else if (cp == 0x2026)
  4405. sub = "...";
  4406. else if (cp == 0x203A || cp == 0x2192 || cp == 0x276F || cp == 0x25B8)
  4407. sub = ">";
  4408. else if (cp == 0x2039 || cp == 0x2190 || cp == 0x276E)
  4409. sub = "<";
  4410. else if (cp == 0x2009 || cp == 0x200A || cp == 0x2002 ||
  4411. cp == 0x2003 || cp == 0x202F)
  4412. sub = " ";
  4413. else if (cp == 0x2715 || cp == 0x2716 || cp == 0x2717)
  4414. sub = "\xD7";
  4415. else if (cp == 0x2713 || cp == 0x2714)
  4416. sub = "v";
  4417. if (sub) {
  4418. while (*sub && *n + 1 < cap)
  4419. out[(*n)++] = *sub++;
  4420. return;
  4421. }
  4422. if (cp >= 0x20 && cp < 0x100 && cp != 0x7F && *n + 1 < cap)
  4423. out[(*n)++] = (char)cp;
  4424. else if (cp == '\n' && *n + 1 < cap)
  4425. out[(*n)++] = ' ';
  4426. }
  4427. /* content: "a" attr(x) "b" | open-quote | close-quote | none. Returns the
  4428. * content index + 1, or 0 for none/empty (counters and url() are not
  4429. * generated). */
  4430. static int parse_content(struct br_doc *d, const char *s, unsigned int len)
  4431. {
  4432. char out[160];
  4433. unsigned int i = 0, n = 0, off;
  4434. while (i < len) {
  4435. char c = s[i];
  4436. if (c == '"' || c == '\'') {
  4437. char q = c;
  4438. i++;
  4439. while (i < len && s[i] != q) {
  4440. unsigned int cp;
  4441. if (s[i] == '\\' && i + 1 < len) {
  4442. unsigned int h = 0, k = 0;
  4443. i++;
  4444. while (k < 6 && i < len &&
  4445. ((s[i] >= '0' && s[i] <= '9') ||
  4446. ((s[i] | 32) >= 'a' &&
  4447. (s[i] | 32) <= 'f'))) {
  4448. char x = (char)(s[i] | 32);
  4449. h = h * 16 + (unsigned int)(x <= '9' ?
  4450. x - '0' : x - 'a' + 10);
  4451. i++;
  4452. k++;
  4453. }
  4454. if (k) {
  4455. if (i < len && s[i] == ' ')
  4456. i++;
  4457. cp = h;
  4458. } else {
  4459. cp = (unsigned char)s[i++];
  4460. }
  4461. } else if ((unsigned char)s[i] >= 0xC0) {
  4462. unsigned char b = (unsigned char)s[i++];
  4463. int more = b >= 0xF0 ? 3 : b >= 0xE0 ? 2 : 1;
  4464. cp = b & (0x3F >> more);
  4465. while (more-- && i < len &&
  4466. ((unsigned char)s[i] & 0xC0) == 0x80)
  4467. cp = (cp << 6) | (s[i++] & 0x3F);
  4468. } else {
  4469. cp = (unsigned char)s[i++];
  4470. }
  4471. content_put(out, &n, sizeof(out), cp);
  4472. }
  4473. i++;
  4474. } else if (i + 5 <= len && ci_eq_n(s + i, "attr(", 5)) {
  4475. unsigned int st = i + 5, e = st;
  4476. while (e < len && s[e] != ')')
  4477. e++;
  4478. while (st < e && is_space(s[st]))
  4479. st++;
  4480. if (n + (e - st) + 3 < sizeof(out) && e > st) {
  4481. out[n++] = '\001';
  4482. while (st < e && !is_space(s[st]))
  4483. out[n++] = s[st++];
  4484. out[n++] = '\002';
  4485. }
  4486. i = e + 1;
  4487. } else if (i + 10 <= len && (ci_eq_n(s + i, "open-quote", 10) ||
  4488. ci_eq_n(s + i, "close-quot", 10))) {
  4489. content_put(out, &n, sizeof(out), '"');
  4490. i += 10;
  4491. } else if (c == '(') {
  4492. unsigned int depth = 0;
  4493. /* counter(), url(), ...: skip the arguments */
  4494. for (; i < len; i++) {
  4495. if (s[i] == '(')
  4496. depth++;
  4497. else if (s[i] == ')' && --depth == 0)
  4498. break;
  4499. }
  4500. i++;
  4501. } else {
  4502. i++;
  4503. }
  4504. }
  4505. if (!n || d->ncontent >= BR_MAX_CONTENT)
  4506. return 0;
  4507. off = store_raw(d, out, n, sizeof(out));
  4508. if (!off)
  4509. return 0;
  4510. d->content_off[d->ncontent++] = off;
  4511. return d->ncontent;
  4512. }
  4513. /* ---- calc() folding ---------------------------------------------------
  4514. *
  4515. * The property parsers understand plain lengths and `calc(P% +/- Npx)`.
  4516. * Frameworks build their scales from arithmetic on custom properties
  4517. * (Infima: `--ifm-leading: calc(var(--ifm-leading-desktop) * 1rem)`,
  4518. * `--ifm-col-width: calc(3 / 12 * 100%)`), so once var() is expanded every
  4519. * calc() in a declaration block is folded into that canonical form:
  4520. * a linear combination of %, px (rem/em at the root font size, vw against
  4521. * the viewport) and unitless numbers, with *, / and nested parentheses.
  4522. * Anything else (vh, var() left unresolved, mixed number + length) leaves
  4523. * the calc() text unchanged. Values are hundredths. */
  4524. static unsigned int viewport_w; /* defined with the media code */
  4525. struct calc_val {
  4526. long px, pct, num;
  4527. unsigned char kind; /* 1 px, 2 pct, 4 unitless number */
  4528. };
  4529. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4530. struct calc_val *v, int depth);
  4531. static void calc_space(const char *s, unsigned int len, unsigned int *i)
  4532. {
  4533. while (*i < len && is_space(s[*i]))
  4534. (*i)++;
  4535. }
  4536. static int calc_unary(const char *s, unsigned int len, unsigned int *i,
  4537. struct calc_val *v, int depth)
  4538. {
  4539. long whole = 0, frac = 0, scale = 1, n;
  4540. int sign = 1, digits = 0;
  4541. calc_space(s, len, i);
  4542. if (*i < len && (s[*i] == '(' ||
  4543. (*i + 5 <= len && ci_eq_n(s + *i, "calc(", 5)))) {
  4544. *i += s[*i] == '(' ? 1 : 5;
  4545. if (depth > 8 || !calc_sum(s, len, i, v, depth + 1))
  4546. return 0;
  4547. calc_space(s, len, i);
  4548. if (*i >= len || s[*i] != ')')
  4549. return 0;
  4550. (*i)++;
  4551. return 1;
  4552. }
  4553. if (*i < len && (s[*i] == '-' || s[*i] == '+')) {
  4554. sign = s[*i] == '-' ? -1 : 1;
  4555. (*i)++;
  4556. }
  4557. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4558. whole = whole * 10 + (s[*i] - '0');
  4559. digits = 1;
  4560. (*i)++;
  4561. if (whole > 1000000)
  4562. return 0;
  4563. }
  4564. if (*i < len && s[*i] == '.') {
  4565. (*i)++;
  4566. while (*i < len && s[*i] >= '0' && s[*i] <= '9') {
  4567. if (scale < 10000) {
  4568. frac = frac * 10 + (s[*i] - '0');
  4569. scale *= 10;
  4570. }
  4571. digits = 1;
  4572. (*i)++;
  4573. }
  4574. }
  4575. if (!digits)
  4576. return 0;
  4577. n = sign * (whole * 100 + frac * 100 / scale);
  4578. v->px = v->pct = v->num = 0;
  4579. if (*i < len && s[*i] == '%') {
  4580. (*i)++;
  4581. v->pct = n;
  4582. v->kind = 2;
  4583. } else if (*i + 3 <= len && ci_eq_n(s + *i, "rem", 3)) {
  4584. *i += 3;
  4585. v->px = n * root_font_px;
  4586. v->kind = 1;
  4587. } else if (*i + 2 <= len && ci_eq_n(s + *i, "em", 2)) {
  4588. *i += 2;
  4589. v->px = n * root_font_px;
  4590. v->kind = 1;
  4591. } else if (*i + 2 <= len && ci_eq_n(s + *i, "px", 2)) {
  4592. *i += 2;
  4593. v->px = n;
  4594. v->kind = 1;
  4595. } else if (*i + 2 <= len && ci_eq_n(s + *i, "vw", 2)) {
  4596. *i += 2;
  4597. v->px = n * (long)viewport_w / 100;
  4598. v->kind = 1;
  4599. } else if (*i < len && (is_ident(s[*i]) || s[*i] == '%')) {
  4600. return 0; /* vh, deg, fr, ch, ... */
  4601. } else {
  4602. v->num = n;
  4603. v->kind = 4;
  4604. }
  4605. return 1;
  4606. }
  4607. static int calc_product(const char *s, unsigned int len, unsigned int *i,
  4608. struct calc_val *v, int depth)
  4609. {
  4610. if (!calc_unary(s, len, i, v, depth))
  4611. return 0;
  4612. for (;;) {
  4613. struct calc_val r;
  4614. char op;
  4615. calc_space(s, len, i);
  4616. if (*i >= len || (s[*i] != '*' && s[*i] != '/'))
  4617. return 1;
  4618. op = s[(*i)++];
  4619. if (!calc_unary(s, len, i, &r, depth))
  4620. return 0;
  4621. if (op == '/') {
  4622. if (r.kind != 4 || !r.num)
  4623. return 0;
  4624. v->px = v->px * 100 / r.num;
  4625. v->pct = v->pct * 100 / r.num;
  4626. v->num = v->num * 100 / r.num;
  4627. } else if (r.kind == 4) {
  4628. v->px = v->px * r.num / 100;
  4629. v->pct = v->pct * r.num / 100;
  4630. v->num = v->num * r.num / 100;
  4631. } else if (v->kind == 4) {
  4632. long k = v->num;
  4633. *v = r;
  4634. v->px = v->px * k / 100;
  4635. v->pct = v->pct * k / 100;
  4636. } else {
  4637. return 0; /* length * length */
  4638. }
  4639. }
  4640. }
  4641. static int calc_sum(const char *s, unsigned int len, unsigned int *i,
  4642. struct calc_val *v, int depth)
  4643. {
  4644. if (!calc_product(s, len, i, v, depth))
  4645. return 0;
  4646. for (;;) {
  4647. struct calc_val r;
  4648. int neg;
  4649. calc_space(s, len, i);
  4650. if (*i >= len || (s[*i] != '+' && s[*i] != '-'))
  4651. return 1;
  4652. neg = s[(*i)++] == '-';
  4653. if (!calc_product(s, len, i, &r, depth))
  4654. return 0;
  4655. if ((v->kind == 4) != (r.kind == 4))
  4656. return 0; /* number + length */
  4657. v->px += neg ? -r.px : r.px;
  4658. v->pct += neg ? -r.pct : r.pct;
  4659. v->num += neg ? -r.num : r.num;
  4660. v->kind |= r.kind;
  4661. }
  4662. }
  4663. /* Hundredths -> "12", "-0.5", "33.33". */
  4664. static unsigned int calc_fmt(char *o, unsigned int cap, long v)
  4665. {
  4666. char tmp[24];
  4667. unsigned int n = 0, k = 0;
  4668. unsigned long a = v < 0 ? (unsigned long)-v : (unsigned long)v;
  4669. unsigned long w = a / 100, f = a % 100;
  4670. if (v < 0)
  4671. tmp[n++] = '-';
  4672. {
  4673. char rev[12];
  4674. unsigned int r = 0;
  4675. do {
  4676. rev[r++] = (char)('0' + w % 10);
  4677. w /= 10;
  4678. } while (w && r < sizeof(rev));
  4679. while (r)
  4680. tmp[n++] = rev[--r];
  4681. }
  4682. if (f) {
  4683. tmp[n++] = '.';
  4684. tmp[n++] = (char)('0' + f / 10);
  4685. if (f % 10)
  4686. tmp[n++] = (char)('0' + f % 10);
  4687. }
  4688. for (k = 0; k < n && k < cap; k++)
  4689. o[k] = tmp[k];
  4690. return k;
  4691. }
  4692. /* Copy s to out with every foldable calc() replaced; returns the length,
  4693. * or 0 when nothing was folded (the caller keeps the original). */
  4694. static unsigned int css_fold_calc(const char *s, unsigned int len, char *out,
  4695. unsigned int cap)
  4696. {
  4697. unsigned int i = 0, n = 0;
  4698. int changed = 0;
  4699. while (i < len && n + 40 < cap) {
  4700. struct calc_val v;
  4701. unsigned int j = i + 5;
  4702. if (!(i + 5 <= len && ci_eq_n(s + i, "calc(", 5) &&
  4703. (i == 0 || !is_ident(s[i - 1])))) {
  4704. out[n++] = s[i++];
  4705. continue;
  4706. }
  4707. if (!calc_sum(s, len, &j, &v, 0) ||
  4708. (calc_space(s, len, &j), j >= len || s[j] != ')')) {
  4709. out[n++] = s[i++];
  4710. continue;
  4711. }
  4712. j++;
  4713. /* Minified CSS glues tokens to the function's parentheses
  4714. * (`padding:var(--a)var(--b)` expands to `calc(..)calc(..)`);
  4715. * a folded `10px16px` would be one token. */
  4716. if (n && !is_space(out[n - 1]) && out[n - 1] != ':' &&
  4717. out[n - 1] != '(' && out[n - 1] != ',')
  4718. out[n++] = ' ';
  4719. if (v.kind == 4) {
  4720. n += calc_fmt(out + n, cap - n, v.num);
  4721. } else if (!(v.kind & 2) || (!v.pct && v.px)) {
  4722. n += calc_fmt(out + n, cap - n, (v.px + (v.px < 0 ?
  4723. -50 : 50)) /
  4724. 100 * 100);
  4725. out[n++] = 'p';
  4726. out[n++] = 'x';
  4727. } else if (!v.px) {
  4728. n += calc_fmt(out + n, cap - n, v.pct);
  4729. out[n++] = '%';
  4730. } else {
  4731. long px = (v.px < 0 ? -v.px + 50 : v.px + 50) / 100;
  4732. memcpy(out + n, "calc(", 5);
  4733. n += 5;
  4734. n += calc_fmt(out + n, cap - n, v.pct);
  4735. memcpy(out + n, v.px < 0 ? "% - " : "% + ", 4);
  4736. n += 4;
  4737. n += calc_fmt(out + n, cap - n, px * 100);
  4738. memcpy(out + n, "px)", 3);
  4739. n += 3;
  4740. }
  4741. i = j;
  4742. changed = 1;
  4743. if (i < len && !is_space(s[i]) && s[i] != ';' && s[i] != ')' &&
  4744. s[i] != ',' && s[i] != '!' && s[i] != '}')
  4745. out[n++] = ' ';
  4746. }
  4747. if (!changed || i < len)
  4748. return 0;
  4749. out[n] = '\0';
  4750. return n;
  4751. }
  4752. static int css_has_calc(const char *s, unsigned int len)
  4753. {
  4754. unsigned int i;
  4755. for (i = 0; i + 5 <= len; i++)
  4756. if ((s[i] == 'c' || s[i] == 'C') && ci_eq_n(s + i, "calc(", 5))
  4757. return 1;
  4758. return 0;
  4759. }
  4760. static void parse_decls(struct br_doc *d, const char *s,
  4761. unsigned int len, struct br_decl *out,
  4762. unsigned int *nout)
  4763. {
  4764. static char expanded[4096];
  4765. static char folded[4096];
  4766. if (css_has_var(s, len)) {
  4767. unsigned int elen;
  4768. if (!css_env_active)
  4769. css_local_collect(s, len);
  4770. elen = css_expand_vars(d, s, len, expanded, sizeof(expanded),
  4771. 0);
  4772. if (!css_env_active)
  4773. css_nlocal = 0;
  4774. if (elen) {
  4775. s = expanded;
  4776. len = elen;
  4777. }
  4778. }
  4779. if (css_has_calc(s, len)) {
  4780. unsigned int flen = css_fold_calc(s, len, folded,
  4781. sizeof(folded));
  4782. if (flen) {
  4783. s = folded;
  4784. len = flen;
  4785. }
  4786. }
  4787. unsigned int i = 0;
  4788. *nout = 0;
  4789. while (i < len && *nout < BR_DECLS) {
  4790. unsigned int pstart, pend, vstart, vend, k;
  4791. char prop[24];
  4792. int important = 0;
  4793. while (i < len && (is_space(s[i]) || s[i] == ';'))
  4794. i++;
  4795. if (i >= len)
  4796. break;
  4797. /* A nested at-rule (@media/@supports) inside the declaration
  4798. * block carries its own selector context; skip it rather than
  4799. * reading its prelude as a declaration. */
  4800. if (s[i] == '@') {
  4801. while (i < len && s[i] != '{' && s[i] != ';')
  4802. i++;
  4803. if (i < len && s[i] == '{')
  4804. i = css_skip_nested(s, len, i + 1);
  4805. else if (i < len)
  4806. i++;
  4807. continue;
  4808. }
  4809. pstart = i;
  4810. while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
  4811. i++;
  4812. if (i < len && s[i] == '{') {
  4813. /* Nested style rule: skip its block. */
  4814. i = css_skip_nested(s, len, i + 1);
  4815. continue;
  4816. }
  4817. pend = i;
  4818. if (i >= len || s[i] != ':')
  4819. continue;
  4820. i++;
  4821. vstart = i;
  4822. while (i < len && s[i] != ';')
  4823. i++;
  4824. vend = i;
  4825. while (pstart < pend && is_space(s[pstart]))
  4826. pstart++;
  4827. while (pend > pstart && is_space(s[pend - 1]))
  4828. pend--;
  4829. while (vstart < vend && is_space(s[vstart]))
  4830. vstart++;
  4831. while (vend > vstart && is_space(s[vend - 1]))
  4832. vend--;
  4833. if (vend - vstart >= 10 &&
  4834. ci_eq_n(s + vend - 10, "!important", 10)) {
  4835. important = 1;
  4836. vend -= 10;
  4837. while (vend > vstart && is_space(s[vend - 1]))
  4838. vend--;
  4839. }
  4840. for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
  4841. char c = s[pstart + k];
  4842. if (c >= 'A' && c <= 'Z')
  4843. c = (char)(c - 'A' + 'a');
  4844. prop[k] = c;
  4845. }
  4846. prop[k] = '\0';
  4847. if (str_eq(prop, "color")) {
  4848. unsigned short c;
  4849. if (parse_color(s + vstart, vend - vstart, &c))
  4850. decl_add(out, nout, PROP_COLOR, c, important);
  4851. } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
  4852. /* SVG paint only: an HTML element's fill never paints
  4853. * its background (tagesschau sets fill on buttons). */
  4854. int is_fill = prop[0] == 'f';
  4855. unsigned short c;
  4856. int mode = paint_mode(s + vstart, vend - vstart, &c);
  4857. if (mode) {
  4858. decl_add(out, nout, is_fill ? PROP_FILL_MODE :
  4859. PROP_STROKE_MODE, mode, important);
  4860. if (mode == BR_PAINT_COLOR)
  4861. decl_add(out, nout, is_fill ? PROP_FILL :
  4862. PROP_STROKE, (short)c,
  4863. important);
  4864. }
  4865. } else if (str_eq(prop, "stroke-width")) {
  4866. int sw = svg_len16(s + vstart, vend - vstart);
  4867. if (sw >= 0)
  4868. decl_add(out, nout, PROP_STROKE_W, sw,
  4869. important);
  4870. } else if (str_eq(prop, "fill-rule")) {
  4871. decl_add(out, nout, PROP_FILL_RULE,
  4872. contains_ci_len(s + vstart, vend - vstart,
  4873. "evenodd") ? 1 : 0,
  4874. important);
  4875. } else if (str_eq(prop, "text-anchor")) {
  4876. int a = BR_ALIGN_LEFT;
  4877. if (contains_ci_len(s + vstart, vend - vstart, "middle"))
  4878. a = BR_ALIGN_CENTER;
  4879. else if (contains_ci_len(s + vstart, vend - vstart,
  4880. "end"))
  4881. a = BR_ALIGN_RIGHT;
  4882. decl_add(out, nout, PROP_ALIGN, a, important);
  4883. } else if (str_eq(prop, "background-image")) {
  4884. add_bg_image_decl(out, nout, s + vstart,
  4885. vend - vstart, important);
  4886. } else if (str_eq(prop, "background-repeat")) {
  4887. int r = 0;
  4888. if (contains_ci_len(s + vstart, vend - vstart,
  4889. "no-repeat"))
  4890. r = 1;
  4891. else if (contains_ci_len(s + vstart, vend - vstart,
  4892. "repeat-x"))
  4893. r = 2;
  4894. else if (contains_ci_len(s + vstart, vend - vstart,
  4895. "repeat-y"))
  4896. r = 3;
  4897. decl_add(out, nout, PROP_BG_REPEAT, r, important);
  4898. } else if (str_eq(prop, "background-position")) {
  4899. add_bg_position(out, nout, s + vstart, vend - vstart,
  4900. important);
  4901. } else if (str_eq(prop, "background-size")) {
  4902. add_bg_size(out, nout, s + vstart, vend - vstart,
  4903. important);
  4904. } else if (str_eq(prop, "background-color") ||
  4905. str_eq(prop, "background")) {
  4906. unsigned short c;
  4907. int is_bg = str_eq(prop, "background");
  4908. if (is_bg) {
  4909. add_bg_image_decl(out, nout, s + vstart,
  4910. vend - vstart, important);
  4911. add_bg_shorthand_extra(out, nout,
  4912. s + vstart,
  4913. vend - vstart,
  4914. important);
  4915. if (contains_ci_len(s + vstart, vend - vstart,
  4916. "no-repeat"))
  4917. decl_add(out, nout, PROP_BG_REPEAT, 1,
  4918. important);
  4919. else if (contains_ci_len(s + vstart,
  4920. vend - vstart,
  4921. "repeat-x"))
  4922. decl_add(out, nout, PROP_BG_REPEAT, 2,
  4923. important);
  4924. else if (contains_ci_len(s + vstart,
  4925. vend - vstart,
  4926. "repeat-y"))
  4927. decl_add(out, nout, PROP_BG_REPEAT, 3,
  4928. important);
  4929. }
  4930. /* `transparent`/`none` are real values: they must
  4931. * beat a lower-specificity colour (and tell form
  4932. * controls the author styled them). */
  4933. if (color_is_transparent(s + vstart, vend - vstart) ||
  4934. (vend - vstart == 4 &&
  4935. ci_eq_n(s + vstart, "none", 4)))
  4936. decl_add(out, nout, PROP_BG_NONE, 1, important);
  4937. else if (parse_color(s + vstart, vend - vstart, &c))
  4938. decl_add(out, nout, PROP_BG, c, important);
  4939. else if (is_bg &&
  4940. find_color_last(s + vstart, vend - vstart, &c))
  4941. decl_add(out, nout, PROP_BG, c, important);
  4942. } else if (str_eq(prop, "font-size")) {
  4943. unsigned short raw;
  4944. unsigned char unit;
  4945. if (parse_font_size(s + vstart, vend - vstart,
  4946. &raw, &unit)) {
  4947. decl_add(out, nout, PROP_FONT_PX, raw,
  4948. important);
  4949. decl_add(out, nout, PROP_FONT_UNIT, unit,
  4950. important);
  4951. }
  4952. } else if (str_eq(prop, "font-weight")) {
  4953. int bold = 0;
  4954. unsigned int j;
  4955. if (vend - vstart >= 4 &&
  4956. (ci_eq_n(s + vstart, "bold", 4) ||
  4957. (vend - vstart >= 6 &&
  4958. ci_eq_n(s + vstart, "bolder", 6))))
  4959. bold = 1;
  4960. else {
  4961. int w = 0;
  4962. for (j = vstart; j < vend; j++)
  4963. if (s[j] >= '0' && s[j] <= '9')
  4964. w = w * 10 + (s[j] - '0');
  4965. /* CSS font matching: with only a 400 and a
  4966. * 700 face, 500 (Infima's "semibold" menus)
  4967. * resolves to the regular face; 600 and up
  4968. * to the bold one. */
  4969. if (w >= 600)
  4970. bold = 1;
  4971. }
  4972. decl_add(out, nout, PROP_BOLD, bold, important);
  4973. } else if (str_eq(prop, "font-style")) {
  4974. int italic = (vend - vstart >= 6 &&
  4975. ci_eq_n(s + vstart, "italic", 6));
  4976. decl_add(out, nout, PROP_ITALIC, italic, important);
  4977. } else if (str_eq(prop, "font-family")) {
  4978. unsigned int j;
  4979. int mono = 0;
  4980. int face = font_family_id(s + vstart, vend - vstart);
  4981. for (j = vstart; j + 4 <= vend; j++)
  4982. if (ci_eq_n(s + j, "mono", 4))
  4983. mono = 1;
  4984. decl_add(out, nout, PROP_FONT_FACE, face, important);
  4985. decl_add(out, nout, PROP_MONO, mono, important);
  4986. } else if (str_eq(prop, "font")) {
  4987. add_font_shorthand(out, nout, s + vstart,
  4988. vend - vstart, important);
  4989. } else if (str_eq(prop, "text-align")) {
  4990. int a = BR_ALIGN_LEFT;
  4991. if (vend - vstart >= 6 &&
  4992. ci_eq_n(s + vstart, "center", 6))
  4993. a = BR_ALIGN_CENTER;
  4994. else if (vend - vstart >= 5 &&
  4995. ci_eq_n(s + vstart, "right", 5))
  4996. a = BR_ALIGN_RIGHT;
  4997. decl_add(out, nout, PROP_ALIGN, a, important);
  4998. } else if (str_eq(prop, "text-decoration")) {
  4999. int u = 0, stv = 0, ov = 0;
  5000. if (contains_ci_len(s + vstart, vend - vstart,
  5001. "underline"))
  5002. u = 1;
  5003. if (contains_ci_len(s + vstart, vend - vstart,
  5004. "line-through"))
  5005. stv = 1;
  5006. if (contains_ci_len(s + vstart, vend - vstart,
  5007. "overline"))
  5008. ov = 1;
  5009. decl_add(out, nout, PROP_UNDERLINE, u, important);
  5010. decl_add(out, nout, PROP_STRIKE, stv, important);
  5011. decl_add(out, nout, PROP_OVERLINE, ov, important);
  5012. } else if (str_eq(prop, "cursor")) {
  5013. int c = parse_cursor(s + vstart, vend - vstart);
  5014. if (c >= 0)
  5015. decl_add(out, nout, PROP_CURSOR, c, important);
  5016. } else if (str_eq(prop, "text-transform")) {
  5017. int u = (vend - vstart >= 9 &&
  5018. ci_eq_n(s + vstart, "uppercase", 9));
  5019. decl_add(out, nout, PROP_UPPER, u, important);
  5020. } else if (str_eq(prop, "white-space")) {
  5021. int nw = contains_ci_len(s + vstart, vend - vstart,
  5022. "nowrap") ||
  5023. contains_ci_len(s + vstart, vend - vstart,
  5024. "pre");
  5025. decl_add(out, nout, PROP_NOWRAP, nw, important);
  5026. } else if (str_eq(prop, "display")) {
  5027. decl_add(out, nout, PROP_DISPLAY,
  5028. parse_display(s + vstart, vend - vstart),
  5029. important);
  5030. } else if (str_eq(prop, "visibility")) {
  5031. int h = contains_ci_len(s + vstart, vend - vstart,
  5032. "hidden") ||
  5033. contains_ci_len(s + vstart, vend - vstart,
  5034. "collapse");
  5035. decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
  5036. } else if (str_eq(prop, "opacity")) {
  5037. unsigned int q;
  5038. int h = 1, any = 0;
  5039. for (q = vstart; q < vend; q++) {
  5040. if (s[q] >= '1' && s[q] <= '9') {
  5041. h = 0;
  5042. break;
  5043. }
  5044. if (s[q] >= '0' && s[q] <= '9')
  5045. any = 1;
  5046. }
  5047. if (!any || contains_ci_len(s + vstart, vend - vstart,
  5048. "inherit"))
  5049. h = 0;
  5050. decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
  5051. } else if (str_eq(prop, "overflow") ||
  5052. str_eq(prop, "overflow-x") ||
  5053. str_eq(prop, "overflow-y")) {
  5054. int h = contains_ci_len(s + vstart, vend - vstart,
  5055. "hidden") ||
  5056. contains_ci_len(s + vstart, vend - vstart,
  5057. "clip");
  5058. decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
  5059. } else if (str_eq(prop, "float")) {
  5060. int f = 0;
  5061. if (vend - vstart >= 4 &&
  5062. ci_eq_n(s + vstart, "left", 4))
  5063. f = 1;
  5064. else if (vend - vstart >= 5 &&
  5065. ci_eq_n(s + vstart, "right", 5))
  5066. f = 2;
  5067. decl_add(out, nout, PROP_FLOAT, f, important);
  5068. } else if (str_eq(prop, "clear")) {
  5069. int c = 0;
  5070. if (contains_ci_len(s + vstart, vend - vstart, "both"))
  5071. c = 3;
  5072. else if (contains_ci_len(s + vstart, vend - vstart,
  5073. "left"))
  5074. c = 1;
  5075. else if (contains_ci_len(s + vstart, vend - vstart,
  5076. "right"))
  5077. c = 2;
  5078. decl_add(out, nout, PROP_CLEAR, c, important);
  5079. } else if (str_eq(prop, "flex-grow") ||
  5080. str_eq(prop, "flex")) {
  5081. unsigned int q = vstart;
  5082. int g = 0;
  5083. while (q < vend && (s[q] == ' ' || s[q] == '\t'))
  5084. q++;
  5085. if (str_eq(prop, "flex") && vend - q >= 4 &&
  5086. ci_eq_n(s + q, "auto", 4))
  5087. g = 1; /* flex: auto = 1 1 auto */
  5088. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5089. g = g * 10 + (s[q] - '0');
  5090. q++;
  5091. }
  5092. if (g > 100)
  5093. g = 100;
  5094. decl_add(out, nout, PROP_FLEX_GROW, g, important);
  5095. if (str_eq(prop, "flex")) {
  5096. /* flex: <grow> [<shrink>] [<basis>]: a definite
  5097. * basis is the item's main size, which our flex
  5098. * row reads from width (Infima's grid:
  5099. * `.col--3 { flex: 0 0 25% }`). Skip the rest
  5100. * of the grow number, then any unitless shrink. */
  5101. unsigned int t;
  5102. while (q < vend && (s[q] == '.' ||
  5103. (s[q] >= '0' && s[q] <= '9')))
  5104. q++;
  5105. for (t = 0; t < 2 && q < vend; t++) {
  5106. unsigned int st, e, k2;
  5107. unsigned short bv;
  5108. unsigned char bk;
  5109. while (q < vend && is_space(s[q]))
  5110. q++;
  5111. st = q;
  5112. while (q < vend && !is_space(s[q]))
  5113. q++;
  5114. e = q;
  5115. for (k2 = st; k2 < e; k2++)
  5116. if (!(s[k2] == '.' ||
  5117. (s[k2] >= '0' &&
  5118. s[k2] <= '9')))
  5119. break;
  5120. if (k2 == e)
  5121. continue; /* shrink */
  5122. if (parse_size(s + st, e - st, &bv,
  5123. &bk) && bk && bv) {
  5124. decl_add(out, nout, PROP_WIDTH,
  5125. bv, important);
  5126. decl_add(out, nout,
  5127. PROP_WIDTH_KIND, bk,
  5128. important);
  5129. }
  5130. break;
  5131. }
  5132. }
  5133. } else if (str_eq(prop, "flex-basis")) {
  5134. unsigned short bv;
  5135. unsigned char bk;
  5136. if (parse_size(s + vstart, vend - vstart, &bv, &bk) &&
  5137. bk && bv) {
  5138. decl_add(out, nout, PROP_WIDTH, bv, important);
  5139. decl_add(out, nout, PROP_WIDTH_KIND, bk,
  5140. important);
  5141. }
  5142. } else if (str_eq(prop, "flex-direction")) {
  5143. decl_add(out, nout, PROP_FLEX_COL,
  5144. contains_ci_len(s + vstart, vend - vstart,
  5145. "column"), important);
  5146. } else if (str_eq(prop, "flex-wrap")) {
  5147. int wrap = 0;
  5148. if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
  5149. wrap = 2;
  5150. else if (contains_ci_len(s + vstart, vend - vstart,
  5151. "wrap"))
  5152. wrap = 1;
  5153. decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
  5154. } else if (str_eq(prop, "justify-content")) {
  5155. int j = 0;
  5156. if (contains_ci_len(s + vstart, vend - vstart,
  5157. "space-between"))
  5158. j = 3;
  5159. else if (contains_ci_len(s + vstart, vend - vstart,
  5160. "space-around"))
  5161. j = 4;
  5162. else if (contains_ci_len(s + vstart, vend - vstart,
  5163. "space-evenly"))
  5164. j = 5;
  5165. else if (contains_ci_len(s + vstart, vend - vstart,
  5166. "center"))
  5167. j = 1;
  5168. else if (contains_ci_len(s + vstart, vend - vstart,
  5169. "flex-end") ||
  5170. contains_ci_len(s + vstart, vend - vstart,
  5171. "end"))
  5172. j = 2;
  5173. decl_add(out, nout, PROP_JUSTIFY, j, important);
  5174. } else if (str_eq(prop, "align-self")) {
  5175. /* 0 auto (the container's align-items), else
  5176. * align-items + 1. */
  5177. int a = 1;
  5178. if (contains_ci_len(s + vstart, vend - vstart, "auto"))
  5179. a = 0;
  5180. else if (contains_ci_len(s + vstart, vend - vstart,
  5181. "center"))
  5182. a = 2;
  5183. else if (contains_ci_len(s + vstart, vend - vstart,
  5184. "flex-end") ||
  5185. contains_ci_len(s + vstart, vend - vstart,
  5186. "end"))
  5187. a = 3;
  5188. decl_add(out, nout, PROP_ALIGN_SELF, a, important);
  5189. } else if (str_eq(prop, "align-items")) {
  5190. int a = 0;
  5191. if (contains_ci_len(s + vstart, vend - vstart, "center"))
  5192. a = 1;
  5193. else if (contains_ci_len(s + vstart, vend - vstart,
  5194. "flex-end") ||
  5195. contains_ci_len(s + vstart, vend - vstart,
  5196. "end"))
  5197. a = 2;
  5198. decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
  5199. } else if (str_eq(prop, "position")) {
  5200. int p = 0;
  5201. if (contains_ci_len(s + vstart, vend - vstart,
  5202. "absolute"))
  5203. p = 1;
  5204. else if (contains_ci_len(s + vstart, vend - vstart,
  5205. "fixed"))
  5206. p = 2;
  5207. else if (contains_ci_len(s + vstart, vend - vstart,
  5208. "relative"))
  5209. p = 3;
  5210. else if (contains_ci_len(s + vstart, vend - vstart,
  5211. "sticky"))
  5212. p = 4;
  5213. decl_add(out, nout, PROP_POS, p, important);
  5214. } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
  5215. str_eq(prop, "right") || str_eq(prop, "bottom")) {
  5216. int which = str_eq(prop, "left") ? 0 :
  5217. str_eq(prop, "top") ? 1 :
  5218. str_eq(prop, "right") ? 2 : 3;
  5219. unsigned int pos = 0;
  5220. if (!(vend - vstart >= 4 &&
  5221. ci_eq_n(s + vstart, "auto", 4))) {
  5222. int v = parse_len(s + vstart, vend - vstart, &pos);
  5223. if (v > 32000)
  5224. v = 32000;
  5225. else if (v < -32000)
  5226. v = -32000;
  5227. decl_add(out, nout,
  5228. (unsigned char)(which == 0 ? PROP_LEFT :
  5229. which == 1 ? PROP_TOP :
  5230. which == 2 ? PROP_RIGHT :
  5231. PROP_BOTTOM),
  5232. v, important);
  5233. decl_add(out, nout,
  5234. (unsigned char)(which == 0 ? PROP_LEFT_SET :
  5235. which == 1 ? PROP_TOP_SET :
  5236. which == 2 ? PROP_RIGHT_SET :
  5237. PROP_BOTTOM_SET),
  5238. 1, important);
  5239. }
  5240. } else if (str_eq(prop, "z-index")) {
  5241. unsigned int q = vstart;
  5242. int sign = 1;
  5243. int v = 0;
  5244. int any = 0;
  5245. if (q < vend && s[q] == '-') {
  5246. sign = -1;
  5247. q++;
  5248. }
  5249. while (q < vend && s[q] >= '0' && s[q] <= '9') {
  5250. v = v * 10 + (s[q] - '0');
  5251. any = 1;
  5252. q++;
  5253. }
  5254. if (any) {
  5255. v *= sign;
  5256. if (v > 32000)
  5257. v = 32000;
  5258. else if (v < -32000)
  5259. v = -32000;
  5260. decl_add(out, nout, PROP_ZINDEX, v, important);
  5261. decl_add(out, nout, PROP_ZSET, 1, important);
  5262. } else if (vend - vstart >= 4 &&
  5263. ci_eq_n(s + vstart, "auto", 4)) {
  5264. decl_add(out, nout, PROP_ZSET, 0, important);
  5265. }
  5266. } else if (str_eq(prop, "transform")) {
  5267. const char *v = s + vstart;
  5268. unsigned int vl = vend - vstart;
  5269. int tx = 0, ty = 0;
  5270. int is_x = contains_ci_len(v, vl, "translatex");
  5271. int is_y = contains_ci_len(v, vl, "translatey");
  5272. unsigned int off = 0, arg;
  5273. if (contains_ci_len(v, vl, "translate")) {
  5274. while (off < vl && v[off] != '(')
  5275. off++;
  5276. if (off < vl)
  5277. off++;
  5278. if (!is_y)
  5279. tx = parse_len(v, vl, &off);
  5280. if (!is_x) {
  5281. while (off < vl &&
  5282. (is_space(v[off]) || v[off] == ','))
  5283. off++;
  5284. ty = parse_len(v, vl, &off);
  5285. }
  5286. decl_add(out, nout, PROP_TRANSFORM_X, tx,
  5287. important);
  5288. decl_add(out, nout, PROP_TRANSFORM_Y, ty,
  5289. important);
  5290. decl_add(out, nout, PROP_TRANSFORM_SET, 1,
  5291. important);
  5292. }
  5293. if (find_fn(v, vl, "scale", &arg)) {
  5294. unsigned int q = arg;
  5295. signed short sx = 100, sy = 100;
  5296. if (parse_fixed_100(v, vl, &q, &sx)) {
  5297. unsigned int r = q;
  5298. while (r < vl &&
  5299. (is_space(v[r]) || v[r] == ','))
  5300. r++;
  5301. if (!parse_fixed_100(v, vl, &r, &sy))
  5302. sy = sx;
  5303. decl_add(out, nout, PROP_SCALE_X, sx,
  5304. important);
  5305. decl_add(out, nout, PROP_SCALE_Y, sy,
  5306. important);
  5307. decl_add(out, nout,
  5308. PROP_TRANSFORM_PAINT, 1,
  5309. important);
  5310. decl_add(out, nout,
  5311. PROP_TRANSFORM_SET, 1,
  5312. important);
  5313. }
  5314. }
  5315. if (find_fn(v, vl, "rotate", &arg)) {
  5316. unsigned int q = arg;
  5317. signed short deg;
  5318. if (parse_fixed_100(v, vl, &q, &deg)) {
  5319. decl_add(out, nout, PROP_ROTATE, deg,
  5320. important);
  5321. decl_add(out, nout,
  5322. PROP_TRANSFORM_PAINT, 1,
  5323. important);
  5324. decl_add(out, nout,
  5325. PROP_TRANSFORM_SET, 1,
  5326. important);
  5327. }
  5328. }
  5329. } else if (str_eq(prop, "transform-origin")) {
  5330. add_transform_origin(out, nout, s + vstart,
  5331. vend - vstart, important);
  5332. } else if (str_eq(prop, "grid-template-columns") ||
  5333. str_eq(prop, "grid-template")) {
  5334. const char *v = s + vstart;
  5335. unsigned int vl = vend - vstart;
  5336. unsigned short val[2];
  5337. unsigned char kind[2];
  5338. int n, q;
  5339. if (str_eq(prop, "grid-template")) {
  5340. for (q = 0; q < (int)vl; q++)
  5341. if (v[q] == '/') {
  5342. q++;
  5343. while (q < (int)vl && is_space(v[q]))
  5344. q++;
  5345. v += q;
  5346. vl -= (unsigned int)q;
  5347. break;
  5348. }
  5349. }
  5350. if (grid_repeat_count(v, vl) >= 1) {
  5351. /* repeat(N, 1fr) / repeat(N, minmax(0,1fr)):
  5352. * Tailwind's grid-cols-N. N equal columns
  5353. * (GRID_FIXED + N); one column is plain block
  5354. * flow, which also resets a smaller breakpoint's
  5355. * count. */
  5356. int cnt = grid_repeat_count(v, vl);
  5357. decl_add(out, nout, PROP_GRID_AUTO,
  5358. cnt >= 2 ? BR_GRID_FIXED + cnt : 0,
  5359. important);
  5360. decl_add(out, nout, PROP_GRID_NCOLS, 0,
  5361. important);
  5362. } else if (contains_ci_len(v, vl, "auto-fit") ||
  5363. contains_ci_len(v, vl, "auto-fill")) {
  5364. unsigned int mp = 0;
  5365. int found = 0;
  5366. unsigned short mw = 200;
  5367. unsigned char mk = 1;
  5368. for (q = 0; q + 7 <= (int)vl; q++)
  5369. if (ci_eq_n(v + q, "minmax(", 7)) {
  5370. mp = (unsigned int)q + 7;
  5371. found = 1;
  5372. break;
  5373. }
  5374. if (found)
  5375. parse_grid_track(v, vl, &mp, &mw, &mk);
  5376. if (mk != 1 || mw < 80)
  5377. mw = 200;
  5378. if (mw > 480)
  5379. mw = 480;
  5380. decl_add(out, nout, PROP_GRID_AUTO, 1, important);
  5381. decl_add(out, nout, PROP_WIDTH, mw, important);
  5382. decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
  5383. } else {
  5384. n = parse_grid_cols(v, vl, val, kind);
  5385. if (n > 0) {
  5386. decl_add(out, nout, PROP_WIDTH, val[0],
  5387. important);
  5388. decl_add(out, nout, PROP_WIDTH_KIND,
  5389. kind[0], important);
  5390. decl_add(out, nout, PROP_GRID_NCOLS, n,
  5391. important);
  5392. }
  5393. if (n > 1) {
  5394. decl_add(out, nout, PROP_MAXW, val[1],
  5395. important);
  5396. decl_add(out, nout, PROP_MAXW_KIND,
  5397. kind[1], important);
  5398. }
  5399. }
  5400. } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
  5401. /* From the value, not the block start: gap was
  5402. * ignored on nearly every site. */
  5403. unsigned int gp = vstart;
  5404. unsigned short gv = 0;
  5405. unsigned char gk = 1;
  5406. if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
  5407. if (gv > 120)
  5408. gv = 120;
  5409. decl_add(out, nout, PROP_GRID_GAP, gv, important);
  5410. }
  5411. } else if (str_eq(prop, "grid-area")) {
  5412. int slot = parse_grid_slot(s + vstart, vend - vstart);
  5413. if (slot)
  5414. decl_add(out, nout, PROP_GRID_SLOT, slot, important);
  5415. } else if (str_eq(prop, "width") ||
  5416. str_eq(prop, "max-width") ||
  5417. str_eq(prop, "min-width") ||
  5418. str_eq(prop, "height") ||
  5419. str_eq(prop, "max-height") ||
  5420. str_eq(prop, "min-height")) {
  5421. unsigned short v;
  5422. unsigned char kind;
  5423. if (vend - vstart >= 5 &&
  5424. ci_eq_n(s + vstart, "calc(", 5)) {
  5425. int pct, px;
  5426. if (parse_calc(s + vstart + 5,
  5427. vend - vstart - 5, &pct, &px)) {
  5428. if (str_eq(prop, "width")) {
  5429. decl_add(out, nout, PROP_WIDTH,
  5430. 0, important);
  5431. decl_add(out, nout,
  5432. PROP_WIDTH_KIND, 3,
  5433. important);
  5434. decl_add(out, nout,
  5435. PROP_WIDTH_CALC_PCT,
  5436. pct, important);
  5437. decl_add(out, nout,
  5438. PROP_WIDTH_CALC_PX,
  5439. px, important);
  5440. } else if (str_eq(prop, "max-width")) {
  5441. decl_add(out, nout, PROP_MAXW,
  5442. 0, important);
  5443. decl_add(out, nout,
  5444. PROP_MAXW_KIND, 3,
  5445. important);
  5446. decl_add(out, nout,
  5447. PROP_MAXW_CALC_PCT,
  5448. pct, important);
  5449. decl_add(out, nout,
  5450. PROP_MAXW_CALC_PX,
  5451. px, important);
  5452. } else if (str_eq(prop, "min-width")) {
  5453. decl_add(out, nout, PROP_MINW,
  5454. 0, important);
  5455. decl_add(out, nout,
  5456. PROP_MINW_KIND, 3,
  5457. important);
  5458. decl_add(out, nout,
  5459. PROP_MINW_CALC_PCT,
  5460. pct, important);
  5461. decl_add(out, nout,
  5462. PROP_MINW_CALC_PX,
  5463. px, important);
  5464. } else if (str_eq(prop, "height")) {
  5465. decl_add(out, nout, PROP_HEIGHT,
  5466. 0, important);
  5467. decl_add(out, nout,
  5468. PROP_HEIGHT_KIND, 3,
  5469. important);
  5470. decl_add(out, nout,
  5471. PROP_HEIGHT_CALC_PCT,
  5472. pct, important);
  5473. decl_add(out, nout,
  5474. PROP_HEIGHT_CALC_PX,
  5475. px, important);
  5476. } else if (str_eq(prop, "max-height")) {
  5477. decl_add(out, nout, PROP_MAXH,
  5478. 0, important);
  5479. decl_add(out, nout,
  5480. PROP_MAXH_KIND, 3,
  5481. important);
  5482. decl_add(out, nout,
  5483. PROP_MAXH_CALC_PCT,
  5484. pct, important);
  5485. decl_add(out, nout,
  5486. PROP_MAXH_CALC_PX,
  5487. px, important);
  5488. } else {
  5489. decl_add(out, nout, PROP_MINH,
  5490. 0, important);
  5491. decl_add(out, nout,
  5492. PROP_MINH_KIND, 3,
  5493. important);
  5494. decl_add(out, nout,
  5495. PROP_MINH_CALC_PCT,
  5496. pct, important);
  5497. decl_add(out, nout,
  5498. PROP_MINH_CALC_PX,
  5499. px, important);
  5500. }
  5501. }
  5502. continue;
  5503. }
  5504. if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
  5505. if (str_eq(prop, "width")) {
  5506. decl_add(out, nout, PROP_WIDTH, v, important);
  5507. decl_add(out, nout, PROP_WIDTH_KIND, kind,
  5508. important);
  5509. } else if (str_eq(prop, "max-width")) {
  5510. decl_add(out, nout, PROP_MAXW, v, important);
  5511. decl_add(out, nout, PROP_MAXW_KIND, kind,
  5512. important);
  5513. } else if (str_eq(prop, "min-width")) {
  5514. decl_add(out, nout, PROP_MINW, v, important);
  5515. decl_add(out, nout, PROP_MINW_KIND, kind,
  5516. important);
  5517. } else if (str_eq(prop, "max-height")) {
  5518. decl_add(out, nout, PROP_MAXH, v, important);
  5519. decl_add(out, nout, PROP_MAXH_KIND, kind,
  5520. important);
  5521. } else if (str_eq(prop, "min-height")) {
  5522. decl_add(out, nout, PROP_MINH, v, important);
  5523. decl_add(out, nout, PROP_MINH_KIND, kind,
  5524. important);
  5525. } else {
  5526. decl_add(out, nout, PROP_HEIGHT, v, important);
  5527. decl_add(out, nout, PROP_HEIGHT_KIND, kind,
  5528. important);
  5529. }
  5530. }
  5531. } else if (str_eq(prop, "content")) {
  5532. decl_add(out, nout, PROP_CONTENT,
  5533. css_collect_only ? 0 :
  5534. parse_content(d, s + vstart, vend - vstart),
  5535. important);
  5536. } else if (str_eq(prop, "aspect-ratio")) {
  5537. /* "16/9", "16 / 9", "1.5" (hundredths), "auto": the
  5538. * preferred height of an auto-height box. */
  5539. unsigned int q = vstart;
  5540. unsigned int a[2] = { 0, 1 }, k;
  5541. for (k = 0; k < 2; k++) {
  5542. unsigned int whole = 0, frac = 0, sc = 1;
  5543. while (q < vend && is_space(s[q]))
  5544. q++;
  5545. if (q >= vend || ((s[q] < '0' || s[q] > '9') &&
  5546. s[q] != '.'))
  5547. break;
  5548. while (q < vend && s[q] >= '0' && s[q] <= '9')
  5549. whole = whole * 10 +
  5550. (unsigned int)(s[q++] - '0');
  5551. if (q < vend && s[q] == '.') {
  5552. q++;
  5553. while (q < vend && s[q] >= '0' &&
  5554. s[q] <= '9') {
  5555. if (sc < 100) {
  5556. frac = frac * 10 +
  5557. (unsigned int)(s[q] - '0');
  5558. sc *= 10;
  5559. }
  5560. q++;
  5561. }
  5562. }
  5563. a[k] = whole * 100 + frac * (100 / sc);
  5564. while (q < vend && is_space(s[q]))
  5565. q++;
  5566. if (k == 0 && (q >= vend || s[q] != '/')) {
  5567. a[1] = 100;
  5568. break;
  5569. }
  5570. q++;
  5571. }
  5572. if (a[0] > 32000 || a[1] > 32000 || !a[1])
  5573. a[0] = a[1] = 0;
  5574. decl_add(out, nout, PROP_ASPECT_W, (int)a[0], important);
  5575. decl_add(out, nout, PROP_ASPECT_H, (int)a[1], important);
  5576. } else if (str_eq(prop, "box-sizing")) {
  5577. int bb = (vend - vstart >= 10 &&
  5578. ci_eq_n(s + vstart, "border-box", 10));
  5579. decl_add(out, nout, PROP_BOX_SIZING, bb, important);
  5580. } else if (str_eq(prop, "line-height")) {
  5581. unsigned short px;
  5582. unsigned char pct;
  5583. if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
  5584. if (px) {
  5585. decl_add(out, nout, PROP_LH_PX, px, important);
  5586. decl_add(out, nout, PROP_LH_PCT, 0, important);
  5587. } else {
  5588. decl_add(out, nout, PROP_LH_PX, 0, important);
  5589. decl_add(out, nout, PROP_LH_PCT, pct, important);
  5590. }
  5591. }
  5592. } else if (str_eq(prop, "list-style") ||
  5593. str_eq(prop, "list-style-type")) {
  5594. int ls = BR_LIST_AUTO;
  5595. if (contains_ci_len(s + vstart, vend - vstart,
  5596. "none"))
  5597. ls = BR_LIST_NONE;
  5598. else if (contains_ci_len(s + vstart, vend - vstart,
  5599. "circle"))
  5600. ls = BR_LIST_CIRCLE;
  5601. else if (contains_ci_len(s + vstart, vend - vstart,
  5602. "square"))
  5603. ls = BR_LIST_SQUARE;
  5604. else if (contains_ci_len(s + vstart, vend - vstart,
  5605. "lower-alpha") ||
  5606. contains_ci_len(s + vstart, vend - vstart,
  5607. "lower-latin"))
  5608. ls = BR_LIST_LOWER_ALPHA;
  5609. else if (contains_ci_len(s + vstart, vend - vstart,
  5610. "upper-alpha") ||
  5611. contains_ci_len(s + vstart, vend - vstart,
  5612. "upper-latin"))
  5613. ls = BR_LIST_UPPER_ALPHA;
  5614. else if (contains_ci_len(s + vstart, vend - vstart,
  5615. "lower-roman"))
  5616. ls = BR_LIST_LOWER_ROMAN;
  5617. else if (contains_ci_len(s + vstart, vend - vstart,
  5618. "upper-roman"))
  5619. ls = BR_LIST_UPPER_ROMAN;
  5620. else if (contains_ci_len(s + vstart, vend - vstart,
  5621. "decimal"))
  5622. ls = BR_LIST_DECIMAL;
  5623. else if (contains_ci_len(s + vstart, vend - vstart,
  5624. "disc"))
  5625. ls = BR_LIST_DISC;
  5626. decl_add(out, nout, PROP_LIST_NONE,
  5627. ls == BR_LIST_NONE, important);
  5628. decl_add(out, nout, PROP_LIST_STYLE, ls, important);
  5629. } else if (str_eq(prop, "border-radius") ||
  5630. str_eq(prop, "border-top-left-radius") ||
  5631. str_eq(prop, "border-top-right-radius") ||
  5632. str_eq(prop, "border-bottom-right-radius") ||
  5633. str_eq(prop, "border-bottom-left-radius")) {
  5634. unsigned short v[4];
  5635. unsigned char k[4];
  5636. unsigned int pos = 0;
  5637. int n = 0;
  5638. while (pos < vend - vstart && n < 4) {
  5639. while (pos < vend - vstart &&
  5640. is_space(s[vstart + pos]))
  5641. pos++;
  5642. if (pos >= vend - vstart ||
  5643. s[vstart + pos] == '/')
  5644. break;
  5645. if (!parse_radius_value(s + vstart,
  5646. vend - vstart, &pos,
  5647. &v[n], &k[n]))
  5648. break;
  5649. n++;
  5650. }
  5651. if (n) {
  5652. unsigned short r[4];
  5653. unsigned char rk[4];
  5654. int i;
  5655. if (n == 1) {
  5656. for (i = 0; i < 4; i++) {
  5657. r[i] = v[0];
  5658. rk[i] = k[0];
  5659. }
  5660. } else if (n == 2) {
  5661. r[0] = r[2] = v[0];
  5662. r[1] = r[3] = v[1];
  5663. rk[0] = rk[2] = k[0];
  5664. rk[1] = rk[3] = k[1];
  5665. } else if (n == 3) {
  5666. r[0] = v[0];
  5667. r[1] = r[3] = v[1];
  5668. r[2] = v[2];
  5669. rk[0] = k[0];
  5670. rk[1] = rk[3] = k[1];
  5671. rk[2] = k[2];
  5672. } else {
  5673. for (i = 0; i < 4; i++) {
  5674. r[i] = v[i];
  5675. rk[i] = k[i];
  5676. }
  5677. }
  5678. for (i = 0; i < 4; i++)
  5679. r[i] = rk[i] == 2 ? 255 :
  5680. (r[i] > 255 ? 255 : r[i]);
  5681. if (str_eq(prop, "border-top-left-radius")) {
  5682. decl_add(out, nout, PROP_RADIUS_TL,
  5683. (short)r[0], important);
  5684. } else if (str_eq(prop,
  5685. "border-top-right-radius")) {
  5686. decl_add(out, nout, PROP_RADIUS_TR,
  5687. (short)r[0], important);
  5688. } else if (str_eq(prop,
  5689. "border-bottom-right-radius")) {
  5690. decl_add(out, nout, PROP_RADIUS_BR,
  5691. (short)r[0], important);
  5692. } else if (str_eq(prop,
  5693. "border-bottom-left-radius")) {
  5694. decl_add(out, nout, PROP_RADIUS_BL,
  5695. (short)r[0], important);
  5696. } else {
  5697. decl_add(out, nout, PROP_RADIUS_TL,
  5698. (short)r[0], important);
  5699. decl_add(out, nout, PROP_RADIUS_TR,
  5700. (short)r[1], important);
  5701. decl_add(out, nout, PROP_RADIUS_BR,
  5702. (short)r[2], important);
  5703. decl_add(out, nout, PROP_RADIUS_BL,
  5704. (short)r[3], important);
  5705. }
  5706. }
  5707. } else if (str_eq(prop, "border") ||
  5708. str_eq(prop, "border-top") ||
  5709. str_eq(prop, "border-right") ||
  5710. str_eq(prop, "border-bottom") ||
  5711. str_eq(prop, "border-left") ||
  5712. str_eq(prop, "border-color") ||
  5713. str_eq(prop, "border-width") ||
  5714. str_eq(prop, "border-style") ||
  5715. str_eq(prop, "border-top-width") ||
  5716. str_eq(prop, "border-right-width") ||
  5717. str_eq(prop, "border-bottom-width") ||
  5718. str_eq(prop, "border-left-width") ||
  5719. str_eq(prop, "border-top-color") ||
  5720. str_eq(prop, "border-right-color") ||
  5721. str_eq(prop, "border-bottom-color") ||
  5722. str_eq(prop, "border-left-color") ||
  5723. str_eq(prop, "border-top-style") ||
  5724. str_eq(prop, "border-right-style") ||
  5725. str_eq(prop, "border-bottom-style") ||
  5726. str_eq(prop, "border-left-style")) {
  5727. const char *v = s + vstart;
  5728. unsigned int vl = vend - vstart;
  5729. unsigned int pos = 0, ts, te;
  5730. int widths[4], colors[4], styles[4], trans[4];
  5731. int nw = 0, nc = 0, ns = 0;
  5732. int side = -1;
  5733. int is_wl = str_eq(prop, "border-width");
  5734. int is_cl = str_eq(prop, "border-color");
  5735. int is_sl = str_eq(prop, "border-style");
  5736. int i;
  5737. if (str_eq(prop, "border-top")) side = 0;
  5738. else if (str_eq(prop, "border-right")) side = 1;
  5739. else if (str_eq(prop, "border-bottom")) side = 2;
  5740. else if (str_eq(prop, "border-left")) side = 3;
  5741. else if (str_eq(prop, "border-top-width")) {
  5742. side = 0; is_wl = 2;
  5743. } else if (str_eq(prop, "border-right-width")) {
  5744. side = 1; is_wl = 2;
  5745. } else if (str_eq(prop, "border-bottom-width")) {
  5746. side = 2; is_wl = 2;
  5747. } else if (str_eq(prop, "border-left-width")) {
  5748. side = 3; is_wl = 2;
  5749. } else if (str_eq(prop, "border-top-color")) {
  5750. side = 0; is_cl = 2;
  5751. } else if (str_eq(prop, "border-right-color")) {
  5752. side = 1; is_cl = 2;
  5753. } else if (str_eq(prop, "border-bottom-color")) {
  5754. side = 2; is_cl = 2;
  5755. } else if (str_eq(prop, "border-left-color")) {
  5756. side = 3; is_cl = 2;
  5757. } else if (str_eq(prop, "border-top-style")) {
  5758. side = 0; is_sl = 2;
  5759. } else if (str_eq(prop, "border-right-style")) {
  5760. side = 1; is_sl = 2;
  5761. } else if (str_eq(prop, "border-bottom-style")) {
  5762. side = 2; is_sl = 2;
  5763. } else if (str_eq(prop, "border-left-style")) {
  5764. side = 3; is_sl = 2;
  5765. }
  5766. if (is_wl) {
  5767. while (nw < 4 && next_token(v, vl, &pos,
  5768. &ts, &te)) {
  5769. int x;
  5770. if (!parse_len_token(v + ts, te - ts,
  5771. &x))
  5772. break;
  5773. widths[nw++] = x;
  5774. }
  5775. if (nw) {
  5776. if (is_wl == 1)
  5777. expand_four(widths, nw);
  5778. if (side >= 0 && is_wl == 2) {
  5779. decl_add(out, nout,
  5780. PROP_BW_T + side,
  5781. widths[0], important);
  5782. } else {
  5783. for (i = 0; i < 4; i++)
  5784. decl_add(out, nout,
  5785. PROP_BW_T + i,
  5786. widths[i],
  5787. important);
  5788. }
  5789. }
  5790. } else if (is_cl) {
  5791. unsigned short c;
  5792. int t;
  5793. while (nc < 4 && next_token(v, vl, &pos,
  5794. &ts, &te)) {
  5795. t = color_is_transparent(v + ts,
  5796. te - ts);
  5797. if (!t && !parse_color(v + ts,
  5798. te - ts, &c))
  5799. break;
  5800. colors[nc] = t ? 0 : c;
  5801. trans[nc] = t;
  5802. nc++;
  5803. }
  5804. if (nc) {
  5805. if (is_cl == 1) {
  5806. expand_four(colors, nc);
  5807. expand_four(trans, nc);
  5808. }
  5809. if (side >= 0 && is_cl == 2) {
  5810. decl_add(out, nout,
  5811. PROP_BC_T + side,
  5812. colors[0], important);
  5813. decl_add(out, nout,
  5814. PROP_BT_T + side,
  5815. trans[0], important);
  5816. } else {
  5817. for (i = 0; i < 4; i++) {
  5818. decl_add(out, nout,
  5819. PROP_BC_T + i,
  5820. colors[i],
  5821. important);
  5822. decl_add(out, nout,
  5823. PROP_BT_T + i,
  5824. trans[i],
  5825. important);
  5826. }
  5827. }
  5828. }
  5829. } else if (is_sl) {
  5830. while (ns < 4 && next_token(v, vl, &pos,
  5831. &ts, &te)) {
  5832. int stv;
  5833. if ((te - ts == 4 &&
  5834. ci_eq_n(v + ts, "none", 4)) ||
  5835. (te - ts == 6 &&
  5836. ci_eq_n(v + ts, "hidden", 6)))
  5837. stv = 0;
  5838. else if (te - ts == 6 &&
  5839. ci_eq_n(v + ts,
  5840. "dashed", 6))
  5841. stv = 2;
  5842. else if (te - ts == 6 &&
  5843. ci_eq_n(v + ts,
  5844. "dotted", 6))
  5845. stv = 3;
  5846. else
  5847. stv = 1;
  5848. styles[ns++] = stv;
  5849. }
  5850. if (ns) {
  5851. if (is_sl == 1)
  5852. expand_four(styles, ns);
  5853. if (is_sl == 2 && side >= 0) {
  5854. if (!styles[0])
  5855. decl_add(out, nout,
  5856. PROP_BW_T + side,
  5857. 0, important);
  5858. else
  5859. decl_add(out, nout,
  5860. PROP_BS_T + side,
  5861. styles[0] - 1,
  5862. important);
  5863. } else {
  5864. for (i = 0; i < 4; i++) {
  5865. if (!styles[i])
  5866. decl_add(out, nout,
  5867. PROP_BW_T + i,
  5868. 0,
  5869. important);
  5870. else
  5871. decl_add(out, nout,
  5872. PROP_BS_T + i,
  5873. styles[i] - 1,
  5874. important);
  5875. }
  5876. }
  5877. }
  5878. } else {
  5879. int bw = -1, bc = -1, style_none = 0;
  5880. int bstyle = -1, btrans = 0;
  5881. while (next_token(v, vl, &pos, &ts, &te)) {
  5882. unsigned short c;
  5883. int x;
  5884. if ((te - ts == 4 &&
  5885. ci_eq_n(v + ts, "none", 4)) ||
  5886. (te - ts == 6 &&
  5887. ci_eq_n(v + ts, "hidden", 6))) {
  5888. style_none = 1;
  5889. } else if (te - ts == 6 &&
  5890. ci_eq_n(v + ts, "dashed", 6)) {
  5891. bstyle = BR_BORDER_DASHED;
  5892. } else if (te - ts == 6 &&
  5893. ci_eq_n(v + ts, "dotted", 6)) {
  5894. bstyle = BR_BORDER_DOTTED;
  5895. } else if (te - ts == 5 &&
  5896. ci_eq_n(v + ts, "solid", 5)) {
  5897. bstyle = BR_BORDER_SOLID;
  5898. } else if (parse_len_token(v + ts,
  5899. te - ts, &x)) {
  5900. bw = x;
  5901. } else if (color_is_transparent(v + ts,
  5902. te - ts)) {
  5903. bc = 0;
  5904. btrans = 1;
  5905. } else if (parse_color(v + ts,
  5906. te - ts, &c)) {
  5907. bc = c;
  5908. btrans = 0;
  5909. }
  5910. }
  5911. if (style_none)
  5912. bw = 0;
  5913. if (bc >= 0 && bw < 0)
  5914. bw = 1;
  5915. if (side < 0) {
  5916. if (bw >= 0)
  5917. decl_add(out, nout,
  5918. PROP_BORDER_W, bw,
  5919. important);
  5920. if (bc >= 0)
  5921. decl_add(out, nout,
  5922. PROP_BORDER_COLOR, bc,
  5923. important);
  5924. if (bc >= 0)
  5925. for (i = 0; i < 4; i++)
  5926. decl_add(out, nout,
  5927. PROP_BT_T + i,
  5928. btrans,
  5929. important);
  5930. if (bstyle >= 0)
  5931. for (i = 0; i < 4; i++)
  5932. decl_add(out, nout,
  5933. PROP_BS_T + i,
  5934. bstyle,
  5935. important);
  5936. } else {
  5937. if (bw >= 0)
  5938. decl_add(out, nout,
  5939. PROP_BW_T + side, bw,
  5940. important);
  5941. if (bc >= 0)
  5942. decl_add(out, nout,
  5943. PROP_BC_T + side, bc,
  5944. important);
  5945. if (bc >= 0)
  5946. decl_add(out, nout,
  5947. PROP_BT_T + side,
  5948. btrans, important);
  5949. if (bstyle >= 0)
  5950. decl_add(out, nout,
  5951. PROP_BS_T + side,
  5952. bstyle, important);
  5953. }
  5954. }
  5955. } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
  5956. int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
  5957. unsigned int pos = 0;
  5958. int is_pad = prop[0] == 'p';
  5959. unsigned char base = is_pad ? PROP_PT : PROP_MT;
  5960. while (pos < vend - vstart && nv < 4) {
  5961. units[nv] = 0;
  5962. if (parse_margin_calc(s + vstart,
  5963. vend - vstart, &pos,
  5964. &vals[nv], &cpx[nv])) {
  5965. units[nv] = 5;
  5966. } else {
  5967. vals[nv] = parse_margin_len(
  5968. s + vstart, vend - vstart,
  5969. &pos, &units[nv]);
  5970. }
  5971. nv++;
  5972. while (pos < vend - vstart &&
  5973. is_space(s[vstart + pos]))
  5974. pos++;
  5975. }
  5976. if (nv == 1) {
  5977. vals[1] = vals[2] = vals[3] = vals[0];
  5978. units[1] = units[2] = units[3] = units[0];
  5979. cpx[1] = cpx[2] = cpx[3] = cpx[0];
  5980. } else if (nv == 2) {
  5981. vals[2] = vals[0];
  5982. vals[3] = vals[1];
  5983. units[2] = units[0];
  5984. units[3] = units[1];
  5985. cpx[2] = cpx[0];
  5986. cpx[3] = cpx[1];
  5987. } else if (nv == 3) {
  5988. vals[3] = vals[1];
  5989. units[3] = units[1];
  5990. cpx[3] = cpx[1];
  5991. }
  5992. if (nv > 0) {
  5993. int k;
  5994. for (k = 0; k < 4; k++) {
  5995. int v = vals[k];
  5996. int u = units[k];
  5997. int au = u == 4;
  5998. if (au)
  5999. v = 0;
  6000. if (is_pad && au)
  6001. u = 0;
  6002. decl_add(out, nout,
  6003. (unsigned char)(base + k), v,
  6004. important);
  6005. decl_add(out, nout,
  6006. (unsigned char)
  6007. ((is_pad ? PROP_PT_U :
  6008. PROP_MT_U) + k),
  6009. u, important);
  6010. if (u == 5)
  6011. decl_add(out, nout,
  6012. (unsigned char)
  6013. ((is_pad ? PROP_PT_CPX :
  6014. PROP_MT_CPX) + k),
  6015. cpx[k], important);
  6016. if (!is_pad && (k == 1 || k == 3))
  6017. decl_add(out, nout,
  6018. k == 1 ? PROP_MR_AUTO :
  6019. PROP_ML_AUTO,
  6020. au, important);
  6021. }
  6022. }
  6023. } else if (str_eq(prop, "margin-top") ||
  6024. str_eq(prop, "margin-right") ||
  6025. str_eq(prop, "margin-bottom") ||
  6026. str_eq(prop, "margin-left") ||
  6027. str_eq(prop, "padding-top") ||
  6028. str_eq(prop, "padding-right") ||
  6029. str_eq(prop, "padding-bottom") ||
  6030. str_eq(prop, "padding-left")) {
  6031. unsigned int pos = 0;
  6032. int unit = 0;
  6033. int v = 0, cpx = 0;
  6034. unsigned char p = PROP_MT;
  6035. const char *side;
  6036. int au;
  6037. if (parse_margin_calc(s + vstart, vend - vstart,
  6038. &pos, &v, &cpx))
  6039. unit = 5;
  6040. else
  6041. v = parse_margin_len(s + vstart,
  6042. vend - vstart, &pos,
  6043. &unit);
  6044. au = unit == 4;
  6045. if (au)
  6046. v = 0;
  6047. if (prop[0] == 'p') {
  6048. p = PROP_PT;
  6049. if (au)
  6050. unit = 0;
  6051. }
  6052. side = prop + (prop[0] == 'p' ? 8 : 7);
  6053. if (str_eq(side, "right"))
  6054. p = (unsigned char)(p + 1);
  6055. else if (str_eq(side, "bottom"))
  6056. p = (unsigned char)(p + 2);
  6057. else if (str_eq(side, "left"))
  6058. p = (unsigned char)(p + 3);
  6059. decl_add(out, nout, p, v, important);
  6060. decl_add(out, nout,
  6061. (unsigned char)((prop[0] == 'p' ?
  6062. PROP_PT_U : PROP_MT_U) +
  6063. (p - (prop[0] == 'p' ?
  6064. PROP_PT : PROP_MT))),
  6065. unit, important);
  6066. if (unit == 5)
  6067. decl_add(out, nout,
  6068. (unsigned char)((prop[0] == 'p' ?
  6069. PROP_PT_CPX : PROP_MT_CPX) +
  6070. (p - (prop[0] == 'p' ?
  6071. PROP_PT : PROP_MT))),
  6072. cpx, important);
  6073. if (str_eq(side, "right"))
  6074. decl_add(out, nout, PROP_MR_AUTO, au, important);
  6075. else if (str_eq(side, "left"))
  6076. decl_add(out, nout, PROP_ML_AUTO, au, important);
  6077. }
  6078. }
  6079. }
  6080. /* Skip @media/@font-face/@keyframes/@import without descending into their
  6081. * nested blocks. This is the mobile/base rules path: media enhancements are
  6082. * deliberately ignored for now. */
  6083. static unsigned int skip_at_rule(const char *s, unsigned int len,
  6084. unsigned int i)
  6085. {
  6086. unsigned int j = i;
  6087. while (j < len && s[j] != '{' && s[j] != ';')
  6088. j++;
  6089. if (j >= len)
  6090. return len;
  6091. if (s[j] == ';')
  6092. return j + 1;
  6093. j++;
  6094. return css_skip_nested(s, len, j);
  6095. }
  6096. static unsigned int viewport_w = 1024;
  6097. static unsigned short theme_text = 0x0000;
  6098. static unsigned short theme_bg = 0xFFFF;
  6099. static unsigned short theme_panel = 0xEF5D;
  6100. static unsigned short theme_link = 0x001B;
  6101. void br_set_theme(unsigned short text, unsigned short bg,
  6102. unsigned short panel, unsigned short link)
  6103. {
  6104. theme_text = text;
  6105. theme_bg = bg;
  6106. theme_panel = panel;
  6107. theme_link = link;
  6108. }
  6109. void br_set_viewport(unsigned int width)
  6110. {
  6111. viewport_w = width ? width : 1024;
  6112. }
  6113. static unsigned int media_len_px(const char *s, unsigned int len)
  6114. {
  6115. unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
  6116. int em = 0;
  6117. while (i < len && is_space(s[i]))
  6118. i++;
  6119. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6120. whole = whole * 10 + (unsigned int)(s[i] - '0');
  6121. i++;
  6122. }
  6123. if (i < len && s[i] == '.') {
  6124. i++;
  6125. /* Consume every fractional digit even when there are more
  6126. * than the three we keep: leaving the tail behind made the
  6127. * unit check miss "em" (74.9375em became 74px). */
  6128. while (i < len && s[i] >= '0' && s[i] <= '9') {
  6129. if (scale < 1000) {
  6130. frac = frac * 10 + (unsigned int)(s[i] - '0');
  6131. scale *= 10;
  6132. }
  6133. i++;
  6134. }
  6135. }
  6136. while (i < len && is_space(s[i]))
  6137. i++;
  6138. if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
  6139. em = 1;
  6140. i += 2;
  6141. } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
  6142. em = 1;
  6143. i += 3;
  6144. } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
  6145. i += 2;
  6146. }
  6147. v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
  6148. if (em)
  6149. return v * 16 / 100;
  6150. return v / 100;
  6151. }
  6152. /* One parenthesised media feature: min-width/max-width and the modern
  6153. * (width <= 600px) form. Unknown features are treated as true. */
  6154. static int media_feature_matches(const char *s, unsigned int len)
  6155. {
  6156. unsigned int i;
  6157. while (len && is_space(*s)) { s++; len--; }
  6158. while (len && is_space(s[len - 1])) len--;
  6159. if (contains_ci_len(s, len, "prefers-color-scheme")) {
  6160. /* There is no user-selectable colour scheme. Ignore both
  6161. * variants so a page's base palette is used; sites whose base
  6162. * is dark (like the project docs) then stay dark instead of
  6163. * flipping to their light media override. */
  6164. return 0;
  6165. }
  6166. {
  6167. /* Environment features with a known answer: a mouse-driven
  6168. * screen with normal contrast and no forced colours. Google
  6169. * puts `border:1px solid CanvasText` on its buttons inside
  6170. * `(forced-colors:active)`. */
  6171. static const struct { const char *name, *yes; } env[] = {
  6172. { "forced-colors", "none" },
  6173. { "-ms-high-contrast", "" },
  6174. { "prefers-contrast", "no-preference" },
  6175. { "inverted-colors", "none" },
  6176. { "any-hover", "hover" },
  6177. { "hover", "hover" },
  6178. { "any-pointer", "fine" },
  6179. { "pointer", "fine" },
  6180. { "prefers-reduced-motion", "no-preference" },
  6181. { "prefers-reduced-transparency", "no-preference" },
  6182. { "display-mode", "browser" },
  6183. };
  6184. unsigned int k;
  6185. for (k = 0; k < sizeof(env) / sizeof(env[0]); k++) {
  6186. unsigned int nl = s_len(env[k].name), j;
  6187. if (len < nl || !ci_eq_n(s, env[k].name, nl) ||
  6188. (len > nl && s[nl] != ':' && !is_space(s[nl])))
  6189. continue;
  6190. for (j = nl; j < len && s[j] != ':'; j++)
  6191. ;
  6192. if (j >= len) /* boolean form: (hover) */
  6193. return env[k].yes[0] &&
  6194. !ci_eq_n(env[k].yes, "none", 4);
  6195. j++;
  6196. while (j < len && is_space(s[j]))
  6197. j++;
  6198. return env[k].yes[0] && len - j == s_len(env[k].yes) &&
  6199. ci_eq_n(s + j, env[k].yes, len - j);
  6200. }
  6201. }
  6202. if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
  6203. for (i = 9; i < len && s[i] != ':'; i++)
  6204. ;
  6205. if (i < len)
  6206. return viewport_w >= media_len_px(s + i + 1,
  6207. len - i - 1);
  6208. return 1;
  6209. }
  6210. if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
  6211. for (i = 9; i < len && s[i] != ':'; i++)
  6212. ;
  6213. if (i < len)
  6214. return viewport_w <= media_len_px(s + i + 1,
  6215. len - i - 1);
  6216. return 1;
  6217. }
  6218. for (i = 0; i < len; i++) {
  6219. if (s[i] == '<' || s[i] == '>') {
  6220. int max = s[i] == '<';
  6221. unsigned int v = media_len_px(s + i + 1, len - i - 1);
  6222. if (i + 1 < len && s[i + 1] == '=')
  6223. v = media_len_px(s + i + 2, len - i - 2);
  6224. return max ? viewport_w <= v : viewport_w >= v;
  6225. }
  6226. }
  6227. return 1;
  6228. }
  6229. static int media_query_matches(const char *s, unsigned int len)
  6230. {
  6231. unsigned int i;
  6232. if (contains_ci_len(s, len, "print") &&
  6233. !contains_ci_len(s, len, "screen"))
  6234. return 0;
  6235. if (contains_ci_len(s, len, "not "))
  6236. return 0;
  6237. for (i = 0; i < len; i++) {
  6238. if (s[i] == '(') {
  6239. unsigned int st = ++i;
  6240. while (i < len && s[i] != ')')
  6241. i++;
  6242. if (!media_feature_matches(s + st, i - st))
  6243. return 0;
  6244. }
  6245. }
  6246. return 1;
  6247. }
  6248. static int media_list_matches(const char *s, unsigned int len)
  6249. {
  6250. unsigned int i = 0;
  6251. while (i < len) {
  6252. unsigned int st = i;
  6253. while (i < len && s[i] != ',')
  6254. i++;
  6255. if (media_query_matches(s + st, i - st))
  6256. return 1;
  6257. i++;
  6258. }
  6259. return 0;
  6260. }
  6261. int br_media_matches(const char *query)
  6262. {
  6263. return media_list_matches(query, s_len(query));
  6264. }
  6265. static unsigned int rule_hash(const char *s, unsigned int len)
  6266. {
  6267. unsigned int h = 2166136261u, i;
  6268. for (i = 0; i < len; i++) {
  6269. char c = s[i];
  6270. if (c >= 'A' && c <= 'Z')
  6271. c = (char)(c - 'A' + 'a');
  6272. h ^= (unsigned char)c;
  6273. h *= 16777619u;
  6274. }
  6275. return h % BR_RULE_HASH;
  6276. }
  6277. /* Index a parsed rule by the most selective component of its rightmost
  6278. * compound selector. br_style_for() then only visits rules that can
  6279. * possibly match a node instead of scanning every rule. */
  6280. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6281. unsigned int len);
  6282. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6283. char *out, unsigned int cap, unsigned int *olen);
  6284. static void rule_index_add(struct br_doc *d, unsigned int idx,
  6285. const char *key, unsigned int len)
  6286. {
  6287. /* Unescaped names: Tailwind's ".md\:flex" must be filed under
  6288. * "md:flex", the class the element carries, not under "md". */
  6289. char cname[96], iname[96];
  6290. unsigned int i, clen = 0, ilen = 0;
  6291. unsigned short *head;
  6292. for (i = 0; i < len; i++) {
  6293. if (key[i] == '\\') {
  6294. i++;
  6295. continue;
  6296. }
  6297. if (key[i] == '(' || key[i] == '[') {
  6298. /* :not(.x) / [href="a.b"] must not pick a bucket */
  6299. i = sel_skip_group(key, i, len) - 1;
  6300. continue;
  6301. }
  6302. if (key[i] == '.' || key[i] == '#') {
  6303. char tmp[96];
  6304. unsigned int n, e;
  6305. e = sel_ident(key, i + 1, len, tmp, sizeof(tmp), &n);
  6306. if (n && n + 1 < sizeof(tmp)) {
  6307. if (key[i] == '#') {
  6308. memcpy(iname, tmp, n);
  6309. ilen = n;
  6310. } else {
  6311. memcpy(cname, tmp, n);
  6312. clen = n;
  6313. }
  6314. }
  6315. i = e - 1;
  6316. }
  6317. }
  6318. if (ilen)
  6319. head = &d->rule_bucket[rule_hash(iname, ilen)];
  6320. else if (clen)
  6321. head = &d->rule_bucket[rule_hash(cname, clen)];
  6322. else if (len == 5 && ci_eq_n(key, ":root", 5))
  6323. head = &d->rule_tag[BR_TAG_HTML];
  6324. else if (len && is_ident(key[0])) {
  6325. char name[16];
  6326. unsigned int n = 0, tag;
  6327. while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
  6328. name[n] = key[n];
  6329. n++;
  6330. }
  6331. name[n] = '\0';
  6332. tag = br_tag_from_name(name);
  6333. if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
  6334. head = &d->rule_tag[tag];
  6335. else
  6336. head = &d->rule_any;
  6337. } else {
  6338. head = &d->rule_any;
  6339. }
  6340. d->rule_next[idx] = *head;
  6341. *head = (unsigned short)idx;
  6342. }
  6343. #define SEL_PARTS 16
  6344. struct sel_part {
  6345. unsigned short st, en;
  6346. char comb; /* combinator to the left: 0, ' ', '>', '+', '~' */
  6347. };
  6348. static unsigned int selector_specificity(const char *s, unsigned int len);
  6349. static int css_selector_supported(const char *s, unsigned int len);
  6350. static int sel_split(const char *s, unsigned int len, struct sel_part *p);
  6351. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6352. unsigned int *st, unsigned int *en);
  6353. static int css_collect_only;
  6354. /* Strip a trailing ::before/::after (or the legacy single-colon forms)
  6355. * from a selector. Returns 1/2 for before/after, 0 for none and -1 for a
  6356. * selector with any other pseudo-element. */
  6357. static int css_pseudo_element(char *sel, unsigned int *len)
  6358. {
  6359. static const struct {
  6360. const char *s;
  6361. unsigned char l, pe;
  6362. } pe[] = {
  6363. { "::before", 8, 1 }, { "::after", 7, 2 },
  6364. { ":before", 7, 1 }, { ":after", 6, 2 },
  6365. };
  6366. unsigned int k, n = *len;
  6367. for (k = 0; k < sizeof(pe) / sizeof(pe[0]); k++) {
  6368. unsigned int l = pe[k].l;
  6369. if (n >= l && ci_eq_n(sel + n - l, pe[k].s, l)) {
  6370. if (pe[k].s[1] != ':' && n > l && sel[n - l - 1] == ':')
  6371. continue; /* matched the tail of "::x" */
  6372. n -= l;
  6373. if (!n || sel[n - 1] == ' ' || sel[n - 1] == '>' ||
  6374. sel[n - 1] == '+' || sel[n - 1] == '~')
  6375. sel[n++] = '*';
  6376. sel[n] = '\0';
  6377. *len = n;
  6378. return pe[k].pe;
  6379. }
  6380. }
  6381. for (k = 0; k + 1 < n; k++)
  6382. if (sel[k] == ':' && sel[k + 1] == ':')
  6383. return -1;
  6384. return 0;
  6385. }
  6386. /* Does a declaration block use or define a custom property whose value
  6387. * depends on the element (defined by some non-root rule)? Such rules are
  6388. * re-expanded per element at cascade time. */
  6389. static unsigned char css_block_scope(struct br_doc *d, const char *s,
  6390. unsigned int len)
  6391. {
  6392. unsigned char f = 0;
  6393. unsigned int i, k;
  6394. for (i = 0; i + 6 < len; i++) {
  6395. unsigned int st, e, vi;
  6396. if (!(s[i] == 'v' && ci_eq_n(s + i, "var(", 4)))
  6397. continue;
  6398. st = i + 4;
  6399. while (st < len && is_space(s[st]))
  6400. st++;
  6401. e = st;
  6402. while (e < len && s[e] != ',' && s[e] != ')' &&
  6403. !is_space(s[e]))
  6404. e++;
  6405. vi = css_var_find(d, s + st, e - st);
  6406. if (vi < BR_MAX_VARS &&
  6407. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT))) {
  6408. f |= BR_RULE_USES_SCOPED;
  6409. break;
  6410. }
  6411. }
  6412. css_local_collect(s, len);
  6413. for (k = 0; k < css_nlocal; k++) {
  6414. unsigned int vi = css_var_find(d, css_local[k].name,
  6415. css_local[k].nlen);
  6416. if (vi < BR_MAX_VARS &&
  6417. (d->var_flags[vi] & (BR_VAR_SCOPED | BR_VAR_CONFLICT)))
  6418. f |= BR_RULE_DEFINES_SCOPED;
  6419. }
  6420. css_nlocal = 0;
  6421. return f;
  6422. }
  6423. static void parse_css(struct br_doc *d, const char *s, unsigned int len)
  6424. {
  6425. unsigned int i = 0;
  6426. while (i < len && d->nrules < d->rule_cap) {
  6427. unsigned int sstart, send, bstart, bend;
  6428. struct br_decl decls[BR_DECLS];
  6429. unsigned int nd, scope_off = 0;
  6430. unsigned char scope_flags = 0;
  6431. while (i < len && is_space(s[i]))
  6432. i++;
  6433. if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
  6434. i += 2;
  6435. while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
  6436. i++;
  6437. i += 2;
  6438. continue;
  6439. }
  6440. if (i >= len || s[i] == '}')
  6441. break;
  6442. if (s[i] == '@') {
  6443. if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
  6444. unsigned int q = i + 6;
  6445. while (q < len && s[q] != '{')
  6446. q++;
  6447. if (q < len &&
  6448. media_list_matches(s + i + 6,
  6449. q - (i + 6))) {
  6450. unsigned int b = q + 1;
  6451. unsigned int e = css_skip_nested(s, len, b);
  6452. if (e > b)
  6453. parse_css(d, s + b,
  6454. (e - 1) - b);
  6455. i = e;
  6456. } else {
  6457. i = skip_at_rule(s, len, i);
  6458. }
  6459. continue;
  6460. }
  6461. if (i + 6 <= len && ci_eq_n(s + i, "@layer", 6) &&
  6462. (i + 6 == len || is_space(s[i + 6]) ||
  6463. s[i + 6] == '{' || s[i + 6] == ';')) {
  6464. unsigned int q = i + 6, nst = q;
  6465. while (q < len && s[q] != '{' && s[q] != ';')
  6466. q++;
  6467. if (q >= len) {
  6468. i = len;
  6469. continue;
  6470. }
  6471. if (s[q] == ';') {
  6472. /* `@layer a, b;` only fixes the order. */
  6473. unsigned int st = nst;
  6474. char path[128];
  6475. while (st < q) {
  6476. unsigned int en = st;
  6477. while (en < q && s[en] != ',')
  6478. en++;
  6479. if (en > st)
  6480. css_layer_register(d, s + st,
  6481. en - st,
  6482. path,
  6483. sizeof(path));
  6484. st = en + 1;
  6485. }
  6486. i = q + 1;
  6487. continue;
  6488. }
  6489. {
  6490. unsigned int b = q + 1;
  6491. unsigned int e = css_skip_nested(s, len, b);
  6492. unsigned char saved = css_cur_layer;
  6493. char saved_name[128], path[128];
  6494. unsigned char rank;
  6495. memcpy(saved_name, css_layer_name,
  6496. sizeof(saved_name));
  6497. rank = css_layer_register(d, s + nst, q - nst,
  6498. path,
  6499. sizeof(path));
  6500. if (rank) {
  6501. css_cur_layer = rank;
  6502. memcpy(css_layer_name, path,
  6503. sizeof(css_layer_name));
  6504. }
  6505. if (e > b)
  6506. parse_css(d, s + b, (e - 1) - b);
  6507. css_cur_layer = saved;
  6508. memcpy(css_layer_name, saved_name,
  6509. sizeof(css_layer_name));
  6510. i = e;
  6511. }
  6512. continue;
  6513. }
  6514. if (i + 9 <= len && ci_eq_n(s + i, "@supports", 9)) {
  6515. unsigned int q = i + 9;
  6516. while (q < len && s[q] != '{' && s[q] != ';')
  6517. q++;
  6518. if (q < len && s[q] == '{' &&
  6519. supports_matches(s + i + 9, q - (i + 9))) {
  6520. unsigned int b = q + 1;
  6521. unsigned int e = css_skip_nested(s, len, b);
  6522. if (e > b)
  6523. parse_css(d, s + b, (e - 1) - b);
  6524. i = e;
  6525. } else {
  6526. i = skip_at_rule(s, len, i);
  6527. }
  6528. continue;
  6529. }
  6530. i = skip_at_rule(s, len, i);
  6531. continue;
  6532. }
  6533. sstart = i;
  6534. while (i < len && s[i] != '{')
  6535. i++;
  6536. if (i >= len)
  6537. break;
  6538. send = i;
  6539. i++;
  6540. bstart = i;
  6541. /* CSS nesting puts @media (and &-rules) inside a rule. Scan
  6542. * to the matching close brace; stopping at the first '}' left
  6543. * the parser out of sync and dropped the rest of the sheet
  6544. * (nd-aktuell lost its desktop rules that way). */
  6545. bend = css_block_end(s, len, i);
  6546. i = bend < len ? bend + 1 : len;
  6547. while (sstart < send && is_space(s[sstart]))
  6548. sstart++;
  6549. while (send > sstart && is_space(s[send - 1]))
  6550. send--;
  6551. if (sstart == send)
  6552. continue;
  6553. if (!css_dark_selector(s + sstart, send - sstart))
  6554. css_collect_block(d, s + bstart, bend - bstart,
  6555. css_root_selector(s + sstart,
  6556. send - sstart));
  6557. if (css_collect_only)
  6558. continue;
  6559. parse_decls(d, s + bstart, bend - bstart, decls, &nd);
  6560. {
  6561. unsigned char sc = bend - bstart <= 4000 ?
  6562. css_block_scope(d, s + bstart, bend - bstart) :
  6563. 0;
  6564. scope_flags = 0;
  6565. scope_off = 0;
  6566. if (sc) {
  6567. scope_off = store_raw(d, s + bstart,
  6568. bend - bstart,
  6569. bend - bstart);
  6570. if (scope_off)
  6571. scope_flags = sc;
  6572. }
  6573. }
  6574. if (!nd && !(scope_flags & BR_RULE_DEFINES_SCOPED))
  6575. continue;
  6576. /* Split a comma-separated selector list into separate rules.
  6577. * A selector the engine cannot match is dropped; the others
  6578. * in the list still apply. */
  6579. {
  6580. const char *list = s + sstart;
  6581. unsigned int ll = send - sstart, pos = 0, st, en;
  6582. while (d->nrules < d->rule_cap &&
  6583. sel_list_next(list, ll, &pos, &st, &en)) {
  6584. struct sel_part parts[SEL_PARTS];
  6585. struct br_rule *r;
  6586. char sel[160];
  6587. unsigned int k, off;
  6588. int np, pe;
  6589. for (k = 0; k < sizeof(sel) - 1 && st + k < en;
  6590. k++)
  6591. sel[k] = is_space(list[st + k]) ? ' ' :
  6592. list[st + k];
  6593. if (st + k < en)
  6594. continue; /* too long */
  6595. sel[k] = '\0';
  6596. pe = css_pseudo_element(sel, &k);
  6597. if (pe < 0 || !css_selector_supported(sel, k))
  6598. continue;
  6599. np = sel_split(sel, k, parts);
  6600. off = store_attr(d, sel, k, 159);
  6601. if (np <= 0 || !off)
  6602. continue;
  6603. r = &d->rules[d->nrules++];
  6604. r->sel_off = off;
  6605. r->key_off = store_attr(d, sel + parts[np - 1].st,
  6606. parts[np - 1].en -
  6607. parts[np - 1].st, 159);
  6608. r->spec = (unsigned short)selector_specificity(sel, k);
  6609. r->single = np == 1;
  6610. r->pe = (unsigned char)pe;
  6611. r->layer = css_cur_layer;
  6612. r->var_scope = scope_flags;
  6613. r->raw_off = scope_off;
  6614. r->raw_len = (unsigned short)(bend - bstart);
  6615. if (pe)
  6616. d->npe_rules++;
  6617. r->nd = (unsigned char)nd;
  6618. for (k = 0; k < nd; k++)
  6619. r->d[k] = decls[k];
  6620. rule_index_add(d, d->nrules - 1,
  6621. attr_ptr(d, r->key_off),
  6622. s_len(attr_ptr(d, r->key_off)));
  6623. }
  6624. }
  6625. }
  6626. }
  6627. /* Sites often set html { font-size: 62.5% } and then size everything in
  6628. * rem. Detect that root size before parsing the sheet so a rem resolves to
  6629. * the same pixels a real browser would use. */
  6630. static void detect_root_font(const char *s, unsigned int len)
  6631. {
  6632. unsigned int i;
  6633. for (i = 0; i + 4 < len; i++) {
  6634. unsigned int j, end, body, sel, q, h;
  6635. int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
  6636. int exact;
  6637. if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
  6638. s[i + 3] == 'l'))
  6639. continue;
  6640. h = i + 4;
  6641. while (h < len && is_space(s[h]))
  6642. h++;
  6643. if (h < len && s[h] != '{' && s[h] != ',' &&
  6644. s[h] != '.' && s[h] != ':' && s[h] != '[')
  6645. continue;
  6646. sel = i;
  6647. while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
  6648. s[sel - 1] != ';' && s[sel - 1] != ',')
  6649. sel--;
  6650. q = sel;
  6651. while (q < i && is_space(s[q]))
  6652. q++;
  6653. exact = (q == i);
  6654. if (!exact && h < len && s[h] != ',' && s[h] != '.')
  6655. continue;
  6656. j = h;
  6657. if (j >= len || s[j] != '{')
  6658. continue;
  6659. while (j < len && s[j] != '{')
  6660. j++;
  6661. if (j >= len)
  6662. break;
  6663. end = j + 1;
  6664. body = end;
  6665. while (end < len && s[end] != '}')
  6666. end++;
  6667. for (; j + 9 < end; j++) {
  6668. if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
  6669. s[j + 3] == 't' && s[j + 4] == '-' &&
  6670. s[j + 5] == 's' && s[j + 6] == 'i' &&
  6671. s[j + 7] == 'z' && s[j + 8] == 'e'))
  6672. continue;
  6673. j += 9;
  6674. while (j < end && s[j] != ':')
  6675. j++;
  6676. if (j >= end)
  6677. break;
  6678. j++;
  6679. while (j < end && is_space(s[j]))
  6680. j++;
  6681. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6682. v = v * 10 + (s[j] - '0');
  6683. digits = 1;
  6684. j++;
  6685. }
  6686. if (j < end && s[j] == '.') {
  6687. j++;
  6688. while (j < end && s[j] >= '0' && s[j] <= '9') {
  6689. v = v * 10 + (s[j] - '0');
  6690. frac++;
  6691. j++;
  6692. }
  6693. }
  6694. while (frac < 2) {
  6695. v *= 10;
  6696. frac++;
  6697. }
  6698. while (frac > 2) {
  6699. v /= 10;
  6700. frac--;
  6701. }
  6702. if (!digits)
  6703. break;
  6704. if (j < end && s[j] == '%')
  6705. pct = 1;
  6706. else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
  6707. px = 1;
  6708. if (pct)
  6709. v = (v * 16 + 5000) / 10000;
  6710. else if (px)
  6711. v /= 100;
  6712. else
  6713. v = v * 16 / 100;
  6714. if (exact && v >= 8 && v <= 24)
  6715. root_font_px = v;
  6716. if (exact)
  6717. return;
  6718. }
  6719. /* html { font: 10px/1 Verdana } also sets the root size. */
  6720. for (j = body; j + 5 <= end; j++) {
  6721. unsigned int p, ts, te;
  6722. if (!ci_eq_n(s + j, "font:", 5))
  6723. continue;
  6724. p = j + 5;
  6725. while (next_token(s, end, &p, &ts, &te)) {
  6726. int fv = parse_font_px(s + ts, te - ts);
  6727. if (fv >= 8 && fv <= 24) {
  6728. if (exact)
  6729. root_font_px = fv;
  6730. break;
  6731. }
  6732. }
  6733. if (exact)
  6734. return;
  6735. break;
  6736. }
  6737. }
  6738. }
  6739. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
  6740. {
  6741. scan_font_faces(css, len);
  6742. detect_root_font(css, len);
  6743. css_collect_only = 1;
  6744. parse_css(d, css, len);
  6745. css_collect_only = 0;
  6746. parse_css(d, css, len);
  6747. }
  6748. void br_parse_inline_css(struct br_doc *d)
  6749. {
  6750. if (d->inline_css_len) {
  6751. scan_font_faces(css_store, d->inline_css_len);
  6752. detect_root_font(css_store, d->inline_css_len);
  6753. css_collect_only = 1;
  6754. parse_css(d, css_store, d->inline_css_len);
  6755. css_collect_only = 0;
  6756. parse_css(d, css_store, d->inline_css_len);
  6757. }
  6758. d->inline_css_len = 0;
  6759. }
  6760. void br_reset_styles(struct br_doc *d)
  6761. {
  6762. unsigned int i;
  6763. for (i = 0; i < d->nnode; i++) {
  6764. d->nodes[i].style_valid = 0;
  6765. d->nodes[i].pe_state = 0;
  6766. }
  6767. }
  6768. /* ---- selector matching ---- */
  6769. static int class_has(struct br_doc *d, unsigned int node, const char *cls,
  6770. unsigned int clen)
  6771. {
  6772. const char *s = attr_ptr(d, d->nodes[node].class_off);
  6773. unsigned int i = 0;
  6774. while (s[i]) {
  6775. unsigned int st, n;
  6776. while (s[i] && is_space(s[i]))
  6777. i++;
  6778. st = i;
  6779. while (s[i] && !is_space(s[i]))
  6780. i++;
  6781. n = i - st;
  6782. if (n == clen && ci_eq_n(s + st, cls, n))
  6783. return 1;
  6784. }
  6785. return 0;
  6786. }
  6787. static int id_eq(struct br_doc *d, unsigned int node, const char *id,
  6788. unsigned int len)
  6789. {
  6790. const char *s = attr_ptr(d, d->nodes[node].id_off);
  6791. unsigned int i;
  6792. for (i = 0; i < len; i++)
  6793. if (s[i] != id[i])
  6794. return 0;
  6795. return s[len] == '\0';
  6796. }
  6797. static int is_ident(char c)
  6798. {
  6799. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  6800. (c >= '0' && c <= '9') || c == '-' || c == '_';
  6801. }
  6802. static int attr_is(const char *name, unsigned int nlen, const char *lit)
  6803. {
  6804. unsigned int n = 0;
  6805. while (lit[n])
  6806. n++;
  6807. return n == nlen && ci_eq_n(name, lit, nlen);
  6808. }
  6809. static int attr_present(struct br_doc *d, unsigned int node,
  6810. const char *name, unsigned int nlen)
  6811. {
  6812. struct br_node *n = &d->nodes[node];
  6813. if (attr_is(name, nlen, "data-collapse-target"))
  6814. return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
  6815. if (attr_is(name, nlen, "data-collapse-trigger"))
  6816. return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
  6817. if (attr_is(name, nlen, "is-open"))
  6818. return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
  6819. if (attr_is(name, nlen, "disabled"))
  6820. return (n->attr_flags & BR_ATTR_DISABLED) != 0;
  6821. if (attr_is(name, nlen, "open"))
  6822. return (n->attr_flags & BR_ATTR_OPEN) != 0;
  6823. if (attr_is(name, nlen, "inert"))
  6824. return (n->attr_flags & BR_ATTR_INERT) != 0;
  6825. if (attr_is(name, nlen, "hidden"))
  6826. return (n->flags & BR_NODE_HIDDEN) != 0;
  6827. if (attr_is(name, nlen, "href"))
  6828. return attr_ptr(d, n->href_off)[0] != 0;
  6829. if (attr_is(name, nlen, "class"))
  6830. return attr_ptr(d, n->class_off)[0] != 0;
  6831. if (attr_is(name, nlen, "id"))
  6832. return attr_ptr(d, n->id_off)[0] != 0;
  6833. if (attr_is(name, nlen, "src"))
  6834. return attr_ptr(d, n->src_off)[0] != 0;
  6835. if (attr_is(name, nlen, "alt"))
  6836. return attr_ptr(d, n->alt_off)[0] != 0;
  6837. if (attr_is(name, nlen, "rel"))
  6838. return attr_ptr(d, n->rel_off)[0] != 0;
  6839. return 0;
  6840. }
  6841. /* ---- Selector engine ----
  6842. *
  6843. * One matcher serves the cascade (rule_try), querySelector(All) and
  6844. * Element.matches. A compound is an optional type/universal selector
  6845. * followed by .class, #id, [attr], [attr op value i] and :pseudo-class
  6846. * tokens; a complex selector joins compounds with descendant, '>', '+' and
  6847. * '~' combinators and is matched right to left with backtracking.
  6848. *
  6849. * Supported pseudo-classes: :root, :link/:any-link, :first/last/only-child,
  6850. * :first/last/only-of-type, :nth-child/:nth-last-child/:nth-of-type/
  6851. * :nth-last-of-type (an+b, odd, even), :empty, :checked, :disabled,
  6852. * :enabled, :required, :optional, :not/:is/:where/:matches/-webkit-any
  6853. * with selector lists, and :has (descendant, '>', '+' and '~' relative
  6854. * selectors). User-state pseudo-classes (:hover, :focus, ...) never match
  6855. * and pseudo-elements never match an element; the cascade strips
  6856. * ::before/::after before it gets here. */
  6857. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  6858. unsigned int len);
  6859. /* i is at '(' or '['; returns the index just past the matching close. */
  6860. static unsigned int sel_skip_group(const char *s, unsigned int i,
  6861. unsigned int len)
  6862. {
  6863. char open = s[i], close = open == '(' ? ')' : ']', quote = 0;
  6864. int depth = 0;
  6865. for (; i < len; i++) {
  6866. char c = s[i];
  6867. if (quote) {
  6868. if (c == '\\')
  6869. i++;
  6870. else if (c == quote)
  6871. quote = 0;
  6872. continue;
  6873. }
  6874. if (c == '\\')
  6875. i++;
  6876. else if (c == '"' || c == '\'')
  6877. quote = c;
  6878. else if (c == open)
  6879. depth++;
  6880. else if (c == close && --depth == 0)
  6881. return i + 1;
  6882. }
  6883. return len;
  6884. }
  6885. /* Identifier with CSS backslash escapes (Tailwind's `md\:flex`). */
  6886. static unsigned int sel_ident(const char *s, unsigned int i, unsigned int len,
  6887. char *out, unsigned int cap, unsigned int *olen)
  6888. {
  6889. unsigned int n = 0;
  6890. while (i < len) {
  6891. char c = s[i];
  6892. if (c == '\\' && i + 1 < len) {
  6893. c = s[i + 1];
  6894. i += 2;
  6895. } else if (is_ident(c) || (unsigned char)c >= 0x80) {
  6896. i++;
  6897. } else {
  6898. break;
  6899. }
  6900. if (n + 1 < cap)
  6901. out[n++] = c;
  6902. }
  6903. out[n] = '\0';
  6904. *olen = n;
  6905. return i;
  6906. }
  6907. /* Next top-level item of a comma-separated selector list. */
  6908. static int sel_list_next(const char *s, unsigned int len, unsigned int *pos,
  6909. unsigned int *st, unsigned int *en)
  6910. {
  6911. unsigned int i = *pos;
  6912. while (i < len && (is_space(s[i]) || s[i] == ','))
  6913. i++;
  6914. if (i >= len)
  6915. return 0;
  6916. *st = i;
  6917. while (i < len && s[i] != ',') {
  6918. if (s[i] == '(' || s[i] == '[')
  6919. i = sel_skip_group(s, i, len);
  6920. else if (s[i] == '\\' && i + 1 < len)
  6921. i += 2;
  6922. else if (s[i] == '"' || s[i] == '\'') {
  6923. char q = s[i++];
  6924. while (i < len && s[i] != q)
  6925. i++;
  6926. i++;
  6927. } else
  6928. i++;
  6929. }
  6930. if (i > len)
  6931. i = len;
  6932. *en = i;
  6933. while (*en > *st && is_space(s[*en - 1]))
  6934. (*en)--;
  6935. *pos = i;
  6936. return 1;
  6937. }
  6938. static int sel_list_match(struct br_doc *d, unsigned int node, const char *s,
  6939. unsigned int len)
  6940. {
  6941. unsigned int pos = 0, st, en;
  6942. while (sel_list_next(s, len, &pos, &st, &en))
  6943. if (sel_match(d, node, s + st, en - st))
  6944. return 1;
  6945. return 0;
  6946. }
  6947. static int sel_same_type(struct br_doc *d, unsigned int a, unsigned int b)
  6948. {
  6949. if (d->nodes[a].tag != d->nodes[b].tag)
  6950. return 0;
  6951. if (d->nodes[a].tag != BR_TAG_UNKNOWN)
  6952. return 1;
  6953. return same_string(attr_ptr(d, d->nodes[a].name_off),
  6954. attr_ptr(d, d->nodes[b].name_off));
  6955. }
  6956. static unsigned int sel_prev_elem(struct br_doc *d, unsigned int node)
  6957. {
  6958. unsigned int c, prev = 0;
  6959. for (c = d->nodes[d->nodes[node].parent].first; c && c != node;
  6960. c = d->nodes[c].next)
  6961. if (d->nodes[c].type == BR_NODE_ELEM)
  6962. prev = c;
  6963. return c == node ? prev : 0;
  6964. }
  6965. static unsigned int sel_next_elem(struct br_doc *d, unsigned int node)
  6966. {
  6967. unsigned int c;
  6968. for (c = d->nodes[node].next; c; c = d->nodes[c].next)
  6969. if (d->nodes[c].type == BR_NODE_ELEM)
  6970. return c;
  6971. return 0;
  6972. }
  6973. /* 1-based position among the element siblings (of the same type), counted
  6974. * from the start or, with from_end, from the end. */
  6975. static int sel_sibling_pos(struct br_doc *d, unsigned int node, int of_type,
  6976. int from_end)
  6977. {
  6978. unsigned int c;
  6979. int pos = 0, total = 0;
  6980. for (c = d->nodes[d->nodes[node].parent].first; c;
  6981. c = d->nodes[c].next) {
  6982. if (d->nodes[c].type != BR_NODE_ELEM)
  6983. continue;
  6984. if (of_type && !sel_same_type(d, c, node))
  6985. continue;
  6986. total++;
  6987. if (c == node)
  6988. pos = total;
  6989. }
  6990. if (!pos)
  6991. return 0;
  6992. return from_end ? total - pos + 1 : pos;
  6993. }
  6994. /* an+b, odd, even. "of S" is not supported (returns 0). */
  6995. static int sel_nth_parse(const char *s, unsigned int len, int *a, int *b)
  6996. {
  6997. unsigned int i = 0;
  6998. int sign = 1, num = 0, digits = 0;
  6999. while (len && is_space(s[len - 1]))
  7000. len--;
  7001. while (i < len && is_space(s[i]))
  7002. i++;
  7003. if (len - i == 3 && ci_eq_n(s + i, "odd", 3)) {
  7004. *a = 2;
  7005. *b = 1;
  7006. return 1;
  7007. }
  7008. if (len - i == 4 && ci_eq_n(s + i, "even", 4)) {
  7009. *a = 2;
  7010. *b = 0;
  7011. return 1;
  7012. }
  7013. if (i < len && (s[i] == '+' || s[i] == '-'))
  7014. sign = s[i++] == '-' ? -1 : 1;
  7015. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7016. num = num * 10 + (s[i++] - '0');
  7017. digits = 1;
  7018. }
  7019. if (i < len && (s[i] == 'n' || s[i] == 'N')) {
  7020. *a = sign * (digits ? num : 1);
  7021. *b = 0;
  7022. i++;
  7023. while (i < len && is_space(s[i]))
  7024. i++;
  7025. if (i < len && (s[i] == '+' || s[i] == '-')) {
  7026. int bs = s[i++] == '-' ? -1 : 1, bv = 0, bd = 0;
  7027. while (i < len && is_space(s[i]))
  7028. i++;
  7029. while (i < len && s[i] >= '0' && s[i] <= '9') {
  7030. bv = bv * 10 + (s[i++] - '0');
  7031. bd = 1;
  7032. }
  7033. if (!bd)
  7034. return 0;
  7035. *b = bs * bv;
  7036. }
  7037. } else {
  7038. if (!digits)
  7039. return 0;
  7040. *a = 0;
  7041. *b = sign * num;
  7042. }
  7043. return i == len;
  7044. }
  7045. static int sel_nth_ok(int a, int b, int pos)
  7046. {
  7047. if (pos <= 0)
  7048. return 0;
  7049. if (!a)
  7050. return pos == b;
  7051. return (pos - b) / a >= 0 && (pos - b) % a == 0;
  7052. }
  7053. static int sel_attr_test(struct br_doc *d, unsigned int node,
  7054. const char *name, int op, const char *want,
  7055. unsigned int wl, int icase)
  7056. {
  7057. const char *have = br_find_attr(d, node, name);
  7058. unsigned int n, i;
  7059. if (!have) {
  7060. /* Boolean attributes the parser keeps as flags. */
  7061. if (!attr_present(d, node, name, text_len(name)))
  7062. return 0;
  7063. have = "";
  7064. }
  7065. if (!op)
  7066. return 1;
  7067. n = text_len(have);
  7068. #define SEL_EQ(p, q, l) (icase ? ci_eq_n((p), (q), (l)) : same_string_n((p), (q), (l)))
  7069. switch (op) {
  7070. case '=':
  7071. return n == wl && SEL_EQ(have, want, wl);
  7072. case '^':
  7073. return wl && n >= wl && SEL_EQ(have, want, wl);
  7074. case '$':
  7075. return wl && n >= wl && SEL_EQ(have + n - wl, want, wl);
  7076. case '*':
  7077. if (!wl)
  7078. return 0;
  7079. for (i = 0; i + wl <= n; i++)
  7080. if (SEL_EQ(have + i, want, wl))
  7081. return 1;
  7082. return 0;
  7083. case '|':
  7084. return n >= wl && SEL_EQ(have, want, wl) &&
  7085. (n == wl || have[wl] == '-');
  7086. case '~':
  7087. if (!wl)
  7088. return 0;
  7089. for (i = 0; i < n; ) {
  7090. unsigned int st;
  7091. while (i < n && is_space(have[i]))
  7092. i++;
  7093. st = i;
  7094. while (i < n && !is_space(have[i]))
  7095. i++;
  7096. if (i - st == wl && SEL_EQ(have + st, want, wl))
  7097. return 1;
  7098. }
  7099. return 0;
  7100. }
  7101. #undef SEL_EQ
  7102. return 0;
  7103. }
  7104. static int sel_empty(struct br_doc *d, unsigned int node)
  7105. {
  7106. unsigned int c;
  7107. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7108. if (d->nodes[c].type == BR_NODE_ELEM)
  7109. return 0;
  7110. if (d->nodes[c].type == BR_NODE_TEXT && d->nodes[c].text_len)
  7111. return 0;
  7112. }
  7113. return 1;
  7114. }
  7115. static int sel_has_desc(struct br_doc *d, unsigned int node, const char *s,
  7116. unsigned int len)
  7117. {
  7118. unsigned int c;
  7119. for (c = d->nodes[node].first; c; c = d->nodes[c].next) {
  7120. if (d->nodes[c].type != BR_NODE_ELEM)
  7121. continue;
  7122. if (sel_match(d, c, s, len) || sel_has_desc(d, c, s, len))
  7123. return 1;
  7124. }
  7125. return 0;
  7126. }
  7127. /* :has() with an optional leading relative combinator per list item. */
  7128. static int sel_has(struct br_doc *d, unsigned int node, const char *s,
  7129. unsigned int len)
  7130. {
  7131. unsigned int pos = 0, st, en, c;
  7132. while (sel_list_next(s, len, &pos, &st, &en)) {
  7133. char comb = s[st];
  7134. if (comb == '>' || comb == '+' || comb == '~') {
  7135. st++;
  7136. while (st < en && is_space(s[st]))
  7137. st++;
  7138. } else {
  7139. comb = ' ';
  7140. }
  7141. if (st >= en)
  7142. continue;
  7143. if (comb == '>') {
  7144. for (c = d->nodes[node].first; c; c = d->nodes[c].next)
  7145. if (d->nodes[c].type == BR_NODE_ELEM &&
  7146. sel_match(d, c, s + st, en - st))
  7147. return 1;
  7148. } else if (comb == '+') {
  7149. c = sel_next_elem(d, node);
  7150. if (c && sel_match(d, c, s + st, en - st))
  7151. return 1;
  7152. } else if (comb == '~') {
  7153. for (c = sel_next_elem(d, node); c;
  7154. c = sel_next_elem(d, c))
  7155. if (sel_match(d, c, s + st, en - st))
  7156. return 1;
  7157. } else if (sel_has_desc(d, node, s + st, en - st)) {
  7158. return 1;
  7159. }
  7160. }
  7161. return 0;
  7162. }
  7163. static int sel_name_is(const char *name, unsigned int nl, const char *lit)
  7164. {
  7165. return text_len(lit) == nl && ci_eq_n(name, lit, nl);
  7166. }
  7167. /* Pseudo-class names the engine models; anything else never matches and
  7168. * makes a top-level rule unsupported (see css_selector_supported()). */
  7169. static int sel_pseudo_known(const char *name, unsigned int nl, int fn)
  7170. {
  7171. static const char *const plain[] = {
  7172. "root", "link", "any-link", "visited", "first-child",
  7173. "last-child", "only-child", "first-of-type", "last-of-type",
  7174. "only-of-type", "empty", "checked", "disabled", "enabled",
  7175. "required", "optional",
  7176. };
  7177. static const char *const func[] = {
  7178. "not", "is", "where", "matches", "-webkit-any", "has",
  7179. "nth-child", "nth-last-child", "nth-of-type",
  7180. "nth-last-of-type",
  7181. };
  7182. unsigned int k;
  7183. if (fn) {
  7184. for (k = 0; k < sizeof(func) / sizeof(func[0]); k++)
  7185. if (sel_name_is(name, nl, func[k]))
  7186. return 1;
  7187. return 0;
  7188. }
  7189. for (k = 0; k < sizeof(plain) / sizeof(plain[0]); k++)
  7190. if (sel_name_is(name, nl, plain[k]))
  7191. return 1;
  7192. return 0;
  7193. }
  7194. static int sel_form_control(struct br_doc *d, unsigned int node)
  7195. {
  7196. unsigned int t = d->nodes[node].tag;
  7197. return t == BR_TAG_INPUT || t == BR_TAG_BUTTON ||
  7198. t == BR_TAG_SELECT || t == BR_TAG_TEXTAREA ||
  7199. t == BR_TAG_OPTION;
  7200. }
  7201. static int sel_pseudo(struct br_doc *d, unsigned int node, const char *name,
  7202. unsigned int nl, const char *arg, unsigned int al,
  7203. int fn)
  7204. {
  7205. struct br_node *n = &d->nodes[node];
  7206. int a, b;
  7207. if (fn) {
  7208. if (sel_name_is(name, nl, "not"))
  7209. return !sel_list_match(d, node, arg, al);
  7210. if (sel_name_is(name, nl, "is") ||
  7211. sel_name_is(name, nl, "where") ||
  7212. sel_name_is(name, nl, "matches") ||
  7213. sel_name_is(name, nl, "-webkit-any"))
  7214. return sel_list_match(d, node, arg, al);
  7215. if (sel_name_is(name, nl, "has"))
  7216. return sel_has(d, node, arg, al);
  7217. if (!sel_nth_parse(arg, al, &a, &b))
  7218. return 0;
  7219. if (sel_name_is(name, nl, "nth-child"))
  7220. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 0));
  7221. if (sel_name_is(name, nl, "nth-last-child"))
  7222. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 0, 1));
  7223. if (sel_name_is(name, nl, "nth-of-type"))
  7224. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 0));
  7225. if (sel_name_is(name, nl, "nth-last-of-type"))
  7226. return sel_nth_ok(a, b, sel_sibling_pos(d, node, 1, 1));
  7227. return 0;
  7228. }
  7229. if (sel_name_is(name, nl, "root"))
  7230. return n->tag == BR_TAG_HTML;
  7231. if (sel_name_is(name, nl, "link") || sel_name_is(name, nl, "any-link"))
  7232. /* No history state: every link is unvisited. */
  7233. return n->tag == BR_TAG_A && n->href_off;
  7234. if (sel_name_is(name, nl, "first-child"))
  7235. return sel_sibling_pos(d, node, 0, 0) == 1;
  7236. if (sel_name_is(name, nl, "last-child"))
  7237. return sel_sibling_pos(d, node, 0, 1) == 1;
  7238. if (sel_name_is(name, nl, "only-child"))
  7239. return sel_sibling_pos(d, node, 0, 0) == 1 &&
  7240. sel_sibling_pos(d, node, 0, 1) == 1;
  7241. if (sel_name_is(name, nl, "first-of-type"))
  7242. return sel_sibling_pos(d, node, 1, 0) == 1;
  7243. if (sel_name_is(name, nl, "last-of-type"))
  7244. return sel_sibling_pos(d, node, 1, 1) == 1;
  7245. if (sel_name_is(name, nl, "only-of-type"))
  7246. return sel_sibling_pos(d, node, 1, 0) == 1 &&
  7247. sel_sibling_pos(d, node, 1, 1) == 1;
  7248. if (sel_name_is(name, nl, "empty"))
  7249. return sel_empty(d, node);
  7250. if (sel_name_is(name, nl, "checked"))
  7251. return br_find_attr(d, node, "checked") != 0 ||
  7252. (n->tag == BR_TAG_OPTION &&
  7253. br_find_attr(d, node, "selected") != 0);
  7254. if (sel_name_is(name, nl, "disabled"))
  7255. return sel_form_control(d, node) &&
  7256. (n->attr_flags & BR_ATTR_DISABLED);
  7257. if (sel_name_is(name, nl, "enabled"))
  7258. return sel_form_control(d, node) &&
  7259. !(n->attr_flags & BR_ATTR_DISABLED);
  7260. if (sel_name_is(name, nl, "required"))
  7261. return sel_form_control(d, node) &&
  7262. br_find_attr(d, node, "required") != 0;
  7263. if (sel_name_is(name, nl, "optional"))
  7264. return sel_form_control(d, node) &&
  7265. !br_find_attr(d, node, "required");
  7266. return 0;
  7267. }
  7268. static int compound_match(struct br_doc *d, unsigned int node, const char *s,
  7269. unsigned int len)
  7270. {
  7271. char buf[64];
  7272. unsigned int i = 0, bl;
  7273. if (d->nodes[node].type != BR_NODE_ELEM)
  7274. return 0;
  7275. if (i < len && s[i] == '*') {
  7276. i++;
  7277. } else if (i < len && (is_ident(s[i]) || s[i] == '\\')) {
  7278. unsigned int tag;
  7279. i = sel_ident(s, i, len, buf, 32, &bl);
  7280. tag = br_tag_from_name(buf);
  7281. if (tag == BR_TAG_UNKNOWN) {
  7282. const char *raw = attr_ptr(d,
  7283. d->nodes[node].name_off);
  7284. if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
  7285. !ci_eq_n(raw, buf, bl) || raw[bl] != '\0')
  7286. return 0;
  7287. } else if (tag != d->nodes[node].tag) {
  7288. return 0;
  7289. }
  7290. }
  7291. if (i >= len)
  7292. return i > 0;
  7293. while (i < len) {
  7294. char c = s[i];
  7295. if (c == '.' || c == '#') {
  7296. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7297. if (!bl)
  7298. return 0;
  7299. if (c == '.' ? !class_has(d, node, buf, bl) :
  7300. !id_eq(d, node, buf, bl))
  7301. return 0;
  7302. } else if (c == '[') {
  7303. unsigned int e = sel_skip_group(s, i, len), j = i + 1;
  7304. unsigned int vs = 0, vl = 0, k;
  7305. char name[32];
  7306. int op = 0, icase = 0;
  7307. while (j < e && is_space(s[j]))
  7308. j++;
  7309. k = 0;
  7310. while (j < e && (is_ident(s[j]) || s[j] == ':') &&
  7311. k + 1 < sizeof(name)) {
  7312. char ch = s[j++];
  7313. name[k++] = (ch >= 'A' && ch <= 'Z') ?
  7314. (char)(ch + 32) : ch;
  7315. }
  7316. name[k] = '\0';
  7317. if (!k)
  7318. return 0;
  7319. while (j < e && is_space(s[j]))
  7320. j++;
  7321. if (j < e && s[j] == '=') {
  7322. op = '=';
  7323. j++;
  7324. } else if (j + 1 < e && s[j + 1] == '=' &&
  7325. (s[j] == '~' || s[j] == '|' ||
  7326. s[j] == '^' || s[j] == '$' || s[j] == '*')) {
  7327. op = s[j];
  7328. j += 2;
  7329. }
  7330. if (op) {
  7331. while (j < e && is_space(s[j]))
  7332. j++;
  7333. if (j < e && (s[j] == '"' || s[j] == '\'')) {
  7334. char q = s[j++];
  7335. vs = j;
  7336. while (j < e && s[j] != q)
  7337. j++;
  7338. vl = j - vs;
  7339. j++;
  7340. } else {
  7341. vs = j;
  7342. while (j < e && s[j] != ']' &&
  7343. !is_space(s[j]))
  7344. j++;
  7345. vl = j - vs;
  7346. }
  7347. while (j < e && is_space(s[j]))
  7348. j++;
  7349. if (j < e && (s[j] == 'i' || s[j] == 'I'))
  7350. icase = 1;
  7351. }
  7352. if (!sel_attr_test(d, node, name, op, s + vs, vl,
  7353. icase))
  7354. return 0;
  7355. i = e;
  7356. } else if (c == ':') {
  7357. unsigned int st, nl, as = 0, al = 0;
  7358. int fn = 0;
  7359. if (i + 1 < len && s[i + 1] == ':')
  7360. return 0; /* pseudo-element */
  7361. st = ++i;
  7362. while (i < len && is_ident(s[i]))
  7363. i++;
  7364. nl = i - st;
  7365. if (i < len && s[i] == '(') {
  7366. unsigned int e = sel_skip_group(s, i, len);
  7367. fn = 1;
  7368. as = i + 1;
  7369. al = e > as ? e - as - 1 : 0;
  7370. i = e;
  7371. }
  7372. if (!sel_pseudo(d, node, s + st, nl, s + as, al, fn))
  7373. return 0;
  7374. } else {
  7375. return 0;
  7376. }
  7377. }
  7378. return 1;
  7379. }
  7380. /* Split a complex selector into compounds; returns the count or -1. */
  7381. static int sel_split(const char *s, unsigned int len, struct sel_part *p)
  7382. {
  7383. unsigned int i = 0;
  7384. int n = 0;
  7385. char comb = 0;
  7386. while (i < len && is_space(s[i]))
  7387. i++;
  7388. while (i < len) {
  7389. unsigned int st = i;
  7390. while (i < len && !is_space(s[i]) && s[i] != '>' &&
  7391. s[i] != '+' && s[i] != '~') {
  7392. if (s[i] == '(' || s[i] == '[')
  7393. i = sel_skip_group(s, i, len);
  7394. else if (s[i] == '\\' && i + 1 < len)
  7395. i += 2;
  7396. else
  7397. i++;
  7398. }
  7399. if (i == st || n == SEL_PARTS)
  7400. return -1;
  7401. p[n].st = (unsigned short)st;
  7402. p[n].en = (unsigned short)(i > len ? len : i);
  7403. p[n].comb = comb;
  7404. n++;
  7405. comb = 0;
  7406. while (i < len && is_space(s[i])) {
  7407. comb = ' ';
  7408. i++;
  7409. }
  7410. if (i < len && (s[i] == '>' || s[i] == '+' || s[i] == '~')) {
  7411. comb = s[i++];
  7412. while (i < len && is_space(s[i]))
  7413. i++;
  7414. if (i >= len)
  7415. return -1;
  7416. }
  7417. }
  7418. return n;
  7419. }
  7420. /* parts[k] matched at node; check parts[0..k-1] through its combinator. */
  7421. /* The parent for combinator matching, or -1 at the top. Index 0 is a real
  7422. * parent when the page starts with <html> (node 0 is that element; see
  7423. * br_parse), so `[data-theme=light] .x` and `html > body` can match it. */
  7424. static int sel_parent(struct br_doc *d, unsigned int node)
  7425. {
  7426. unsigned int p = d->nodes[node].parent;
  7427. if (p)
  7428. return (int)p;
  7429. if (node != 0 && d->nodes[0].type == BR_NODE_ELEM &&
  7430. d->nodes[0].tag == BR_TAG_HTML &&
  7431. !(d->nodes[node].flags & BR_NODE_DETACHED))
  7432. return 0;
  7433. return -1;
  7434. }
  7435. static int sel_from(struct br_doc *d, unsigned int node, const char *s,
  7436. const struct sel_part *p, int k)
  7437. {
  7438. unsigned int c, pl;
  7439. const char *ps;
  7440. if (k == 0)
  7441. return 1;
  7442. ps = s + p[k - 1].st;
  7443. pl = p[k - 1].en - p[k - 1].st;
  7444. switch (p[k].comb) {
  7445. case '>': {
  7446. int pc = sel_parent(d, node);
  7447. return pc >= 0 && compound_match(d, (unsigned int)pc, ps, pl) &&
  7448. sel_from(d, (unsigned int)pc, s, p, k - 1);
  7449. }
  7450. case '+':
  7451. c = sel_prev_elem(d, node);
  7452. return c && compound_match(d, c, ps, pl) &&
  7453. sel_from(d, c, s, p, k - 1);
  7454. case '~':
  7455. for (c = sel_prev_elem(d, node); c; c = sel_prev_elem(d, c))
  7456. if (compound_match(d, c, ps, pl) &&
  7457. sel_from(d, c, s, p, k - 1))
  7458. return 1;
  7459. return 0;
  7460. default: {
  7461. int pc;
  7462. for (pc = sel_parent(d, node); pc >= 0;
  7463. pc = sel_parent(d, (unsigned int)pc))
  7464. if (compound_match(d, (unsigned int)pc, ps, pl) &&
  7465. sel_from(d, (unsigned int)pc, s, p, k - 1))
  7466. return 1;
  7467. return 0;
  7468. }
  7469. }
  7470. }
  7471. /* Match one complex selector. right_done: the caller already matched the
  7472. * rightmost compound (the rule index key). */
  7473. static int selector_match(struct br_doc *d, unsigned int node, const char *s,
  7474. unsigned int len, int right_done)
  7475. {
  7476. struct sel_part parts[SEL_PARTS];
  7477. int np = sel_split(s, len, parts);
  7478. if (np <= 0)
  7479. return 0;
  7480. if (!right_done &&
  7481. !compound_match(d, node, s + parts[np - 1].st,
  7482. parts[np - 1].en - parts[np - 1].st))
  7483. return 0;
  7484. return sel_from(d, node, s, parts, np - 1);
  7485. }
  7486. static int sel_match(struct br_doc *d, unsigned int node, const char *s,
  7487. unsigned int len)
  7488. {
  7489. return selector_match(d, node, s, len, 0);
  7490. }
  7491. /* Specificity as a*100 + b*10 + c (ids, classes/attributes/pseudo-classes,
  7492. * types/pseudo-elements); :is/:not/:has take their most specific argument
  7493. * and :where counts nothing. */
  7494. static unsigned int selector_specificity(const char *s, unsigned int len)
  7495. {
  7496. unsigned int i = 0, spec = 0, bl;
  7497. char buf[64];
  7498. while (i < len) {
  7499. char c = s[i];
  7500. if (c == '#' || c == '.') {
  7501. spec += c == '#' ? 100 : 10;
  7502. i = sel_ident(s, i + 1, len, buf, sizeof(buf), &bl);
  7503. } else if (c == '[') {
  7504. spec += 10;
  7505. i = sel_skip_group(s, i, len);
  7506. } else if (c == ':') {
  7507. unsigned int st, nl;
  7508. if (i + 1 < len && s[i + 1] == ':') {
  7509. spec += 1;
  7510. i += 2;
  7511. while (i < len && is_ident(s[i]))
  7512. i++;
  7513. continue;
  7514. }
  7515. st = ++i;
  7516. while (i < len && is_ident(s[i]))
  7517. i++;
  7518. nl = i - st;
  7519. if (i < len && s[i] == '(') {
  7520. unsigned int e = sel_skip_group(s, i, len);
  7521. if (sel_name_is(s + st, nl, "where")) {
  7522. /* zero */
  7523. } else if (sel_name_is(s + st, nl, "not") ||
  7524. sel_name_is(s + st, nl, "is") ||
  7525. sel_name_is(s + st, nl, "has") ||
  7526. sel_name_is(s + st, nl, "matches") ||
  7527. sel_name_is(s + st, nl,
  7528. "-webkit-any")) {
  7529. unsigned int pos = 0, as, ae, best = 0;
  7530. unsigned int al = e > i + 1 ?
  7531. e - i - 2 : 0;
  7532. while (sel_list_next(s + i + 1, al,
  7533. &pos, &as, &ae)) {
  7534. unsigned int v =
  7535. selector_specificity(
  7536. s + i + 1 + as,
  7537. ae - as);
  7538. if (v > best)
  7539. best = v;
  7540. }
  7541. spec += best;
  7542. } else {
  7543. spec += 10;
  7544. }
  7545. i = e;
  7546. } else if (sel_name_is(s + st, nl, "before") ||
  7547. sel_name_is(s + st, nl, "after")) {
  7548. spec += 1;
  7549. } else {
  7550. spec += 10;
  7551. }
  7552. } else if (is_ident(c) || c == '\\') {
  7553. spec += 1;
  7554. i = sel_ident(s, i, len, buf, sizeof(buf), &bl);
  7555. } else {
  7556. i++;
  7557. }
  7558. }
  7559. return spec;
  7560. }
  7561. /* Cascade filter: drop a selector the engine cannot match (user-state
  7562. * pseudo-classes, unknown pseudo-classes, pseudo-elements other than the
  7563. * ones the caller handles), so it does not waste a rule slot. Arguments of
  7564. * functional pseudo-classes are not checked: an unknown pseudo-class there
  7565. * is simply false, which is right for :not(:hover). */
  7566. static int css_selector_supported(const char *s, unsigned int len)
  7567. {
  7568. struct sel_part parts[SEL_PARTS];
  7569. unsigned int i = 0;
  7570. if (sel_split(s, len, parts) <= 0)
  7571. return 0;
  7572. while (i < len) {
  7573. if (s[i] == '(' || s[i] == '[') {
  7574. i = sel_skip_group(s, i, len);
  7575. } else if (s[i] == '\\') {
  7576. i += 2;
  7577. } else if (s[i] == ':') {
  7578. unsigned int st, nl;
  7579. int fn;
  7580. if (i + 1 < len && s[i + 1] == ':')
  7581. return 0;
  7582. st = ++i;
  7583. while (i < len && is_ident(s[i]))
  7584. i++;
  7585. nl = i - st;
  7586. fn = i < len && s[i] == '(';
  7587. if (!sel_pseudo_known(s + st, nl, fn))
  7588. return 0;
  7589. } else {
  7590. i++;
  7591. }
  7592. }
  7593. return 1;
  7594. }
  7595. #define RULE_MATCH_MAX 512
  7596. /* 0 while computing an element's style, 1/2 inside br_pseudo(). */
  7597. static int cascade_pe;
  7598. struct rule_match {
  7599. unsigned short idx;
  7600. short spec; /* precedence of normal declarations */
  7601. short spec_imp; /* precedence of !important declarations */
  7602. };
  7603. /* Cascade precedence = band * CASCADE_BAND + specificity (capped below the
  7604. * band). Normal declarations: layers in order (1..BR_MAX_LAYERS), then
  7605. * unlayered rules, then the style attribute. !important reverses the
  7606. * layer order: unlayered lowest, the first layer highest, the style
  7607. * attribute above all. Both fit a short. */
  7608. #define CASCADE_BAND 512
  7609. #define CASCADE_UNLAYERED (BR_MAX_LAYERS + 1)
  7610. #define CASCADE_INLINE (BR_MAX_LAYERS + 2)
  7611. static int cascade_rank(unsigned int layer, unsigned int spec, int important)
  7612. {
  7613. unsigned int band;
  7614. if (spec >= CASCADE_BAND)
  7615. spec = CASCADE_BAND - 1;
  7616. if (important)
  7617. band = layer ? CASCADE_UNLAYERED - layer : 0;
  7618. else
  7619. band = layer ? layer : CASCADE_UNLAYERED;
  7620. return (int)(band * CASCADE_BAND + spec);
  7621. }
  7622. static struct rule_match rule_matches[RULE_MATCH_MAX];
  7623. static unsigned int nrule_matches;
  7624. static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
  7625. {
  7626. struct br_rule *r = &d->rules[idx];
  7627. const char *key = attr_ptr(d, r->key_off);
  7628. int spec, key_ok = 0;
  7629. if (r->pe != cascade_pe)
  7630. return;
  7631. if (key[0]) {
  7632. if (!compound_match(d, node, key, s_len(key)))
  7633. return;
  7634. key_ok = 1;
  7635. }
  7636. if (!(r->single && key_ok) &&
  7637. !selector_match(d, node, attr_ptr(d, r->sel_off),
  7638. s_len(attr_ptr(d, r->sel_off)), key_ok))
  7639. return;
  7640. spec = (int)r->spec;
  7641. if (nrule_matches < RULE_MATCH_MAX) {
  7642. rule_matches[nrule_matches].idx = (unsigned short)idx;
  7643. rule_matches[nrule_matches].spec =
  7644. (short)cascade_rank(r->layer, (unsigned int)spec, 0);
  7645. rule_matches[nrule_matches].spec_imp =
  7646. (short)cascade_rank(r->layer, (unsigned int)spec, 1);
  7647. nrule_matches++;
  7648. }
  7649. }
  7650. static void rule_scan_list(struct br_doc *d, unsigned int node,
  7651. unsigned short head)
  7652. {
  7653. while (head != 0xFFFF) {
  7654. rule_try(d, node, head);
  7655. head = d->rule_next[head];
  7656. }
  7657. }
  7658. static void rule_scan_class(struct br_doc *d, unsigned int node,
  7659. const char *cls, unsigned int len)
  7660. {
  7661. if (len)
  7662. rule_scan_list(d, node,
  7663. d->rule_bucket[rule_hash(cls, len)]);
  7664. }
  7665. static void rule_collect(struct br_doc *d, unsigned int node)
  7666. {
  7667. const char *cls = attr_ptr(d, d->nodes[node].class_off);
  7668. const char *id = attr_ptr(d, d->nodes[node].id_off);
  7669. unsigned int tag = d->nodes[node].tag;
  7670. unsigned int i;
  7671. nrule_matches = 0;
  7672. rule_scan_list(d, node, d->rule_any);
  7673. if (tag < BR_TAG_MAX)
  7674. rule_scan_list(d, node, d->rule_tag[tag]);
  7675. if (id[0])
  7676. rule_scan_list(d, node,
  7677. d->rule_bucket[rule_hash(id, s_len(id))]);
  7678. i = 0;
  7679. while (cls[i]) {
  7680. unsigned int st, n;
  7681. while (cls[i] && is_space(cls[i]))
  7682. i++;
  7683. st = i;
  7684. while (cls[i] && !is_space(cls[i]))
  7685. i++;
  7686. n = i - st;
  7687. if (n)
  7688. rule_scan_class(d, node, cls + st, n);
  7689. }
  7690. /* Rules must cascade in document order even though the index visits
  7691. * them in bucket order. */
  7692. for (i = 1; i < nrule_matches; i++) {
  7693. struct rule_match m = rule_matches[i];
  7694. unsigned int j = i;
  7695. while (j > 0 && rule_matches[j - 1].idx > m.idx) {
  7696. rule_matches[j] = rule_matches[j - 1];
  7697. j--;
  7698. }
  7699. rule_matches[j] = m;
  7700. }
  7701. }
  7702. static void resolve_size_field(unsigned short *val, unsigned char *kind,
  7703. int font_px)
  7704. {
  7705. if (*kind == 4) {
  7706. *val = (unsigned short)(*val * font_px / 100);
  7707. *kind = 1;
  7708. } else if (*kind == 5) {
  7709. *val = (unsigned short)(*val * root_font_px / 100);
  7710. *kind = 1;
  7711. }
  7712. }
  7713. static void resolve_size_units(struct br_style *st)
  7714. {
  7715. resolve_size_field(&st->width, &st->width_kind, st->font_px);
  7716. resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
  7717. resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
  7718. resolve_size_field(&st->height, &st->height_kind, st->font_px);
  7719. resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
  7720. resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
  7721. }
  7722. static void set_border_side_w(struct br_style *st, int side, int v)
  7723. {
  7724. if (v < 0)
  7725. v = 0;
  7726. if (v > 32)
  7727. v = 32;
  7728. st->border_w[side] = (unsigned char)v;
  7729. if (st->border_w[side] && !st->border_c[side])
  7730. st->border_c[side] = st->color;
  7731. }
  7732. static void set_border_side_c(struct br_style *st, int side, int v)
  7733. {
  7734. st->border_c[side] = (unsigned short)v;
  7735. }
  7736. static void apply_decl(struct br_style *st, const struct br_decl *dcl)
  7737. {
  7738. switch (dcl->prop) {
  7739. case PROP_COLOR:
  7740. st->color = (unsigned short)dcl->val;
  7741. break;
  7742. case PROP_BG:
  7743. st->bg = (unsigned short)dcl->val;
  7744. st->has_bg = 1;
  7745. st->bg_set = 1;
  7746. break;
  7747. case PROP_BG_NONE:
  7748. st->has_bg = 0;
  7749. st->bg_set = 1;
  7750. break;
  7751. case PROP_BG_IMAGE:
  7752. st->bg_image = (unsigned short)dcl->val;
  7753. break;
  7754. case PROP_BG_REPEAT:
  7755. st->bg_repeat = (unsigned char)dcl->val;
  7756. break;
  7757. case PROP_BG_POS_X_KIND:
  7758. st->bg_pos_x_kind = (unsigned char)dcl->val;
  7759. break;
  7760. case PROP_BG_POS_X:
  7761. st->bg_pos_x = (signed short)dcl->val;
  7762. break;
  7763. case PROP_BG_POS_Y_KIND:
  7764. st->bg_pos_y_kind = (unsigned char)dcl->val;
  7765. break;
  7766. case PROP_BG_POS_Y:
  7767. st->bg_pos_y = (signed short)dcl->val;
  7768. break;
  7769. case PROP_BG_SIZE_KIND:
  7770. st->bg_size_kind = (unsigned char)dcl->val;
  7771. break;
  7772. case PROP_BG_SIZE_W:
  7773. st->bg_size_w = (unsigned short)dcl->val;
  7774. break;
  7775. case PROP_BG_SIZE_H:
  7776. st->bg_size_h = (unsigned short)dcl->val;
  7777. break;
  7778. case PROP_FONT_PX:
  7779. st->font_raw = (unsigned short)dcl->val;
  7780. break;
  7781. case PROP_FONT_UNIT:
  7782. st->font_unit = (unsigned char)dcl->val;
  7783. break;
  7784. case PROP_BOLD:
  7785. st->bold = (unsigned char)(dcl->val != 0);
  7786. break;
  7787. case PROP_ITALIC:
  7788. st->italic = (unsigned char)(dcl->val != 0);
  7789. break;
  7790. case PROP_MONO:
  7791. st->mono = (unsigned char)(dcl->val != 0);
  7792. break;
  7793. case PROP_FONT_FACE:
  7794. st->font_face = (unsigned char)dcl->val;
  7795. break;
  7796. case PROP_ALIGN:
  7797. st->align = (unsigned char)dcl->val;
  7798. break;
  7799. case PROP_UNDERLINE:
  7800. st->underline = (unsigned char)(dcl->val != 0);
  7801. break;
  7802. case PROP_STRIKE:
  7803. st->strike = (unsigned char)(dcl->val != 0);
  7804. break;
  7805. case PROP_OVERLINE:
  7806. st->overline = (unsigned char)(dcl->val != 0);
  7807. break;
  7808. case PROP_DISPLAY:
  7809. st->display = (unsigned char)(dcl->val &
  7810. ~BR_DISPLAY_INLINE_FLAG);
  7811. st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
  7812. st->list_item = 0;
  7813. break;
  7814. case PROP_VIS_HIDDEN:
  7815. st->vis_hidden = (unsigned char)(dcl->val != 0);
  7816. break;
  7817. case PROP_OPACITY_HIDDEN:
  7818. st->opacity_hidden = (unsigned char)(dcl->val != 0);
  7819. break;
  7820. case PROP_FILL_MODE:
  7821. st->fill_mode = (unsigned char)dcl->val;
  7822. break;
  7823. case PROP_FILL:
  7824. st->fill = (unsigned short)dcl->val;
  7825. break;
  7826. case PROP_STROKE_MODE:
  7827. st->stroke_mode = (unsigned char)dcl->val;
  7828. break;
  7829. case PROP_STROKE:
  7830. st->stroke = (unsigned short)dcl->val;
  7831. break;
  7832. case PROP_STROKE_W:
  7833. st->stroke_w = (unsigned short)dcl->val;
  7834. break;
  7835. case PROP_FILL_RULE:
  7836. st->fill_evenodd = (unsigned char)(dcl->val != 0);
  7837. break;
  7838. case PROP_OVERFLOW_HIDDEN:
  7839. st->overflow_hidden = (unsigned char)(dcl->val != 0);
  7840. break;
  7841. case PROP_MT: st->mt = dcl->val; break;
  7842. case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
  7843. case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
  7844. case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
  7845. case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
  7846. case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
  7847. case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
  7848. case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
  7849. case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
  7850. case PROP_MR: st->mr = dcl->val; break;
  7851. case PROP_MB: st->mb = dcl->val; break;
  7852. case PROP_ML: st->ml = dcl->val; break;
  7853. case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
  7854. case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
  7855. case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
  7856. break;
  7857. case PROP_PT: st->pt = dcl->val; break;
  7858. case PROP_PR: st->pr = dcl->val; break;
  7859. case PROP_PB: st->pb = dcl->val; break;
  7860. case PROP_PL: st->pl = dcl->val; break;
  7861. case PROP_MT_U:
  7862. case PROP_MR_U:
  7863. case PROP_MB_U:
  7864. case PROP_ML_U:
  7865. case PROP_PT_U:
  7866. case PROP_PR_U:
  7867. case PROP_PB_U:
  7868. case PROP_PL_U:
  7869. st->mp_unit[dcl->prop - PROP_MT_U] =
  7870. (unsigned char)dcl->val;
  7871. break;
  7872. case PROP_BORDER_W:
  7873. set_border_side_w(st, 0, dcl->val);
  7874. set_border_side_w(st, 1, dcl->val);
  7875. set_border_side_w(st, 2, dcl->val);
  7876. set_border_side_w(st, 3, dcl->val);
  7877. break;
  7878. case PROP_BORDER_COLOR:
  7879. set_border_side_c(st, 0, dcl->val);
  7880. set_border_side_c(st, 1, dcl->val);
  7881. set_border_side_c(st, 2, dcl->val);
  7882. set_border_side_c(st, 3, dcl->val);
  7883. break;
  7884. case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
  7885. case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
  7886. case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
  7887. case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
  7888. case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
  7889. case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
  7890. case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
  7891. case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
  7892. case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
  7893. case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
  7894. case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
  7895. case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
  7896. case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
  7897. case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
  7898. case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
  7899. case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
  7900. case PROP_RADIUS_TL:
  7901. case PROP_RADIUS_TR:
  7902. case PROP_RADIUS_BR:
  7903. case PROP_RADIUS_BL:
  7904. st->radius[dcl->prop - PROP_RADIUS_TL] =
  7905. (unsigned char)dcl->val;
  7906. break;
  7907. case PROP_LIST_NONE:
  7908. st->no_bullet = (unsigned char)(dcl->val != 0);
  7909. break;
  7910. case PROP_LIST_STYLE:
  7911. st->list_style = (unsigned char)dcl->val;
  7912. break;
  7913. case PROP_CONTENT:
  7914. st->content = (unsigned short)dcl->val;
  7915. break;
  7916. case PROP_ASPECT_W:
  7917. st->aspect_w = (unsigned short)dcl->val;
  7918. break;
  7919. case PROP_ASPECT_H:
  7920. st->aspect_h = (unsigned short)dcl->val;
  7921. break;
  7922. case PROP_NOWRAP:
  7923. st->nowrap = (unsigned char)(dcl->val != 0);
  7924. break;
  7925. case PROP_WIDTH:
  7926. st->width = (unsigned short)dcl->val;
  7927. break;
  7928. case PROP_WIDTH_KIND:
  7929. st->width_kind = (unsigned char)dcl->val;
  7930. break;
  7931. case PROP_WIDTH_CALC_PCT:
  7932. st->width_calc_pct = dcl->val;
  7933. break;
  7934. case PROP_WIDTH_CALC_PX:
  7935. st->width_calc_px = dcl->val;
  7936. break;
  7937. case PROP_MAXW:
  7938. st->max_width = (unsigned short)dcl->val;
  7939. break;
  7940. case PROP_MAXW_KIND:
  7941. st->max_width_kind = (unsigned char)dcl->val;
  7942. break;
  7943. case PROP_MAXW_CALC_PCT:
  7944. st->max_width_calc_pct = (signed short)dcl->val;
  7945. break;
  7946. case PROP_MAXW_CALC_PX:
  7947. st->max_width_calc_px = (signed short)dcl->val;
  7948. break;
  7949. case PROP_MINW:
  7950. st->min_width = (unsigned short)dcl->val;
  7951. break;
  7952. case PROP_MINW_KIND:
  7953. st->min_width_kind = (unsigned char)dcl->val;
  7954. break;
  7955. case PROP_MINW_CALC_PCT:
  7956. st->min_width_calc_pct = (signed short)dcl->val;
  7957. break;
  7958. case PROP_MINW_CALC_PX:
  7959. st->min_width_calc_px = (signed short)dcl->val;
  7960. break;
  7961. case PROP_HEIGHT:
  7962. st->height = (unsigned short)dcl->val;
  7963. break;
  7964. case PROP_HEIGHT_KIND:
  7965. st->height_kind = (unsigned char)dcl->val;
  7966. break;
  7967. case PROP_HEIGHT_CALC_PCT:
  7968. st->height_calc_pct = (signed short)dcl->val;
  7969. break;
  7970. case PROP_HEIGHT_CALC_PX:
  7971. st->height_calc_px = (signed short)dcl->val;
  7972. break;
  7973. case PROP_MINH:
  7974. st->min_height = (unsigned short)dcl->val;
  7975. break;
  7976. case PROP_MINH_KIND:
  7977. st->min_height_kind = (unsigned char)dcl->val;
  7978. break;
  7979. case PROP_MINH_CALC_PCT:
  7980. st->min_height_calc_pct = (signed short)dcl->val;
  7981. break;
  7982. case PROP_MINH_CALC_PX:
  7983. st->min_height_calc_px = (signed short)dcl->val;
  7984. break;
  7985. case PROP_MAXH:
  7986. st->max_height = (unsigned short)dcl->val;
  7987. break;
  7988. case PROP_MAXH_KIND:
  7989. st->max_height_kind = (unsigned char)dcl->val;
  7990. break;
  7991. case PROP_MAXH_CALC_PCT:
  7992. st->max_height_calc_pct = (signed short)dcl->val;
  7993. break;
  7994. case PROP_MAXH_CALC_PX:
  7995. st->max_height_calc_px = (signed short)dcl->val;
  7996. break;
  7997. case PROP_LH_PCT:
  7998. st->line_h_pct = (unsigned char)dcl->val;
  7999. break;
  8000. case PROP_LH_PX:
  8001. st->line_h_px = (unsigned short)dcl->val;
  8002. break;
  8003. case PROP_UPPER:
  8004. st->uppercase = (unsigned char)(dcl->val != 0);
  8005. break;
  8006. case PROP_CURSOR:
  8007. st->cursor = (unsigned char)dcl->val;
  8008. break;
  8009. case PROP_FLOAT:
  8010. st->float_left = (unsigned char)dcl->val;
  8011. break;
  8012. case PROP_CLEAR:
  8013. st->clear = (unsigned char)dcl->val;
  8014. break;
  8015. case PROP_FLEX_GROW:
  8016. st->flex_grow = (unsigned char)dcl->val;
  8017. break;
  8018. case PROP_FLEX_COL:
  8019. st->flex_col = (unsigned char)(dcl->val != 0);
  8020. break;
  8021. case PROP_FLEX_WRAP:
  8022. st->flex_wrap = (unsigned char)(dcl->val == 1); /* 2: nowrap */
  8023. break;
  8024. case PROP_JUSTIFY:
  8025. st->justify = (unsigned char)dcl->val;
  8026. break;
  8027. case PROP_ALIGN_ITEMS:
  8028. st->align_items = (unsigned char)dcl->val;
  8029. break;
  8030. case PROP_ALIGN_SELF:
  8031. st->align_self = (unsigned char)dcl->val;
  8032. break;
  8033. case PROP_GRID_NCOLS:
  8034. st->grid_ncols = (unsigned char)dcl->val;
  8035. break;
  8036. case PROP_GRID_GAP:
  8037. st->grid_gap = (unsigned char)dcl->val;
  8038. break;
  8039. case PROP_GRID_SLOT:
  8040. st->grid_slot = (unsigned char)dcl->val;
  8041. break;
  8042. case PROP_GRID_AUTO:
  8043. st->grid_auto = (unsigned char)dcl->val; /* 1 auto-fit, >16 fixed */
  8044. break;
  8045. case PROP_POS:
  8046. st->pos = (unsigned char)dcl->val;
  8047. break;
  8048. case PROP_LEFT:
  8049. st->left = dcl->val;
  8050. break;
  8051. case PROP_TOP:
  8052. st->top = dcl->val;
  8053. break;
  8054. case PROP_RIGHT:
  8055. st->right = dcl->val;
  8056. break;
  8057. case PROP_BOTTOM:
  8058. st->bottom = dcl->val;
  8059. break;
  8060. case PROP_LEFT_SET:
  8061. st->left_set = (unsigned char)(dcl->val != 0);
  8062. break;
  8063. case PROP_TOP_SET:
  8064. st->top_set = (unsigned char)(dcl->val != 0);
  8065. break;
  8066. case PROP_RIGHT_SET:
  8067. st->right_set = (unsigned char)(dcl->val != 0);
  8068. break;
  8069. case PROP_BOTTOM_SET:
  8070. st->bottom_set = (unsigned char)(dcl->val != 0);
  8071. break;
  8072. case PROP_ZINDEX:
  8073. st->z_index = dcl->val;
  8074. break;
  8075. case PROP_ZSET:
  8076. st->z_set = (unsigned char)(dcl->val != 0);
  8077. break;
  8078. case PROP_TRANSFORM_X:
  8079. st->tx = dcl->val;
  8080. break;
  8081. case PROP_TRANSFORM_Y:
  8082. st->ty = dcl->val;
  8083. break;
  8084. case PROP_TRANSFORM_SET:
  8085. st->transform_set = (unsigned char)(dcl->val != 0);
  8086. break;
  8087. case PROP_TRANSFORM_PAINT:
  8088. st->transform_scale = (unsigned char)(dcl->val != 0);
  8089. break;
  8090. case PROP_SCALE_X:
  8091. st->scale_x = (signed short)dcl->val;
  8092. break;
  8093. case PROP_SCALE_Y:
  8094. st->scale_y = (signed short)dcl->val;
  8095. break;
  8096. case PROP_ROTATE:
  8097. st->rotate_x100 = (signed short)dcl->val;
  8098. break;
  8099. case PROP_ORIGIN_X_KIND:
  8100. st->origin_x_kind = (unsigned char)dcl->val;
  8101. break;
  8102. case PROP_ORIGIN_X:
  8103. st->origin_x = (signed short)dcl->val;
  8104. break;
  8105. case PROP_ORIGIN_Y_KIND:
  8106. st->origin_y_kind = (unsigned char)dcl->val;
  8107. break;
  8108. case PROP_ORIGIN_Y:
  8109. st->origin_y = (signed short)dcl->val;
  8110. break;
  8111. default:
  8112. break;
  8113. }
  8114. }
  8115. /* The element's custom properties for rules flagged USES_SCOPED: the
  8116. * definitions of its matched rules in cascade order (precedence first,
  8117. * then document order), over the document-wide table. Inheritance from
  8118. * ancestors is not modelled; a name no matched rule defines falls back to
  8119. * the global table as before. Tailwind (`.shadow { --tw-shadow: ...;
  8120. * box-shadow: var(--tw-shadow) }`) and Infima's grid (`.col--3` sets
  8121. * `--ifm-col-width`, `.col[class*=col--]` uses it) rely on this. */
  8122. static void css_env_build(struct br_doc *d)
  8123. {
  8124. static unsigned short order[RULE_MATCH_MAX];
  8125. unsigned int i, need = 0, n = 0;
  8126. css_env_active = 0;
  8127. css_nlocal = 0;
  8128. for (i = 0; i < nrule_matches; i++)
  8129. if (d->rules[rule_matches[i].idx].var_scope &
  8130. BR_RULE_USES_SCOPED)
  8131. need = 1;
  8132. if (!need)
  8133. return;
  8134. /* Definitions apply lowest precedence first so the winner is
  8135. * written last (stable: document order within a precedence). */
  8136. for (i = 0; i < nrule_matches; i++) {
  8137. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8138. unsigned int j = n;
  8139. if (!(r->var_scope & BR_RULE_DEFINES_SCOPED))
  8140. continue;
  8141. while (j > 0 && rule_matches[order[j - 1]].spec >
  8142. rule_matches[i].spec) {
  8143. order[j] = order[j - 1];
  8144. j--;
  8145. }
  8146. order[j] = i;
  8147. n++;
  8148. }
  8149. for (i = 0; i < n; i++) {
  8150. struct br_rule *r = &d->rules[rule_matches[order[i]].idx];
  8151. const char *raw = attr_ptr(d, r->raw_off);
  8152. unsigned int save = css_nlocal, k;
  8153. /* Collect this block's definitions after the earlier ones,
  8154. * replacing a name already present. */
  8155. {
  8156. unsigned int base = save;
  8157. css_local_append(raw, r->raw_len);
  8158. for (k = base; k < css_nlocal; k++) {
  8159. int prev = css_local_find_before(
  8160. css_local[k].name, css_local[k].nlen, base);
  8161. if (prev >= 0) {
  8162. css_local[prev] = css_local[k];
  8163. css_local[k].nlen = 0;
  8164. }
  8165. }
  8166. }
  8167. }
  8168. css_env_active = 1;
  8169. }
  8170. static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
  8171. int spec, int spec_imp, short *best, short *best_imp)
  8172. {
  8173. unsigned char p = dcl->prop;
  8174. if (p >= PROP_COUNT)
  8175. return;
  8176. /* A transparent background competes with a colour for one slot. */
  8177. if (p == PROP_BG_NONE)
  8178. p = PROP_BG;
  8179. if (dcl->important) {
  8180. if (spec_imp >= best_imp[p]) {
  8181. apply_decl(st, dcl);
  8182. best_imp[p] = (short)spec_imp;
  8183. }
  8184. } else if (best_imp[p] < 0 && spec >= best[p]) {
  8185. apply_decl(st, dcl);
  8186. best[p] = (short)spec;
  8187. }
  8188. }
  8189. void br_style_for(struct br_doc *d, unsigned int node,
  8190. const struct br_style *parent, struct br_style *out)
  8191. {
  8192. unsigned int tag;
  8193. short best[PROP_COUNT], best_imp[PROP_COUNT];
  8194. unsigned int i;
  8195. if (!cascade_pe && d->nodes[node].style_valid) {
  8196. *out = d->nodes[node].style;
  8197. return;
  8198. }
  8199. *out = *parent;
  8200. out->has_bg = 0;
  8201. out->bg_set = 0;
  8202. out->bg_image = 0;
  8203. out->bg_repeat = 0;
  8204. out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
  8205. out->bg_pos_x = out->bg_pos_y = 0;
  8206. out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
  8207. out->float_left = 0;
  8208. out->clear = 0;
  8209. out->list_item = 0;
  8210. out->font_unit = 0;
  8211. for (i = 0; i < 4; i++) {
  8212. out->border_w[i] = 0;
  8213. out->border_c[i] = 0;
  8214. out->border_s[i] = BR_BORDER_SOLID;
  8215. out->radius[i] = 0;
  8216. }
  8217. out->width = out->max_width = out->min_width = out->height = 0;
  8218. out->min_height = out->max_height = 0;
  8219. out->width_calc_pct = out->width_calc_px = 0;
  8220. out->height_calc_pct = out->height_calc_px = 0;
  8221. out->min_height_calc_pct = out->min_height_calc_px = 0;
  8222. out->max_height_calc_pct = out->max_height_calc_px = 0;
  8223. out->min_width_calc_pct = out->min_width_calc_px = 0;
  8224. out->max_width_calc_pct = out->max_width_calc_px = 0;
  8225. out->width_kind = out->max_width_kind = out->min_width_kind = 0;
  8226. out->height_kind = out->min_height_kind = out->max_height_kind = 0;
  8227. out->box_sizing = 0;
  8228. out->ml_auto = out->mr_auto = 0;
  8229. out->mt = out->mr = out->mb = out->ml = 0;
  8230. out->pt = out->pr = out->pb = out->pl = 0;
  8231. for (i = 0; i < 8; i++) {
  8232. out->mp_unit[i] = 0;
  8233. out->mp_calc_px[i] = 0;
  8234. }
  8235. out->flex_grow = out->flex_col = out->flex_wrap = 0;
  8236. out->justify = out->align_items = out->align_self = 0;
  8237. out->grid_ncols = out->grid_gap = out->grid_slot = 0;
  8238. out->grid_auto = 0;
  8239. out->aspect_w = out->aspect_h = 0;
  8240. out->pos = out->left_set = out->top_set = 0;
  8241. out->right_set = out->bottom_set = 0;
  8242. out->left = out->top = out->right = out->bottom = 0;
  8243. out->z_index = 0;
  8244. out->z_set = 0;
  8245. out->tx = out->ty = 0;
  8246. out->transform_set = 0;
  8247. out->transform_scale = 0;
  8248. out->scale_x = out->scale_y = 100;
  8249. out->rotate_x100 = 0;
  8250. out->origin_x = out->origin_y = 50;
  8251. out->origin_x_kind = out->origin_y_kind = 1;
  8252. out->vis_hidden = out->opacity_hidden = 0;
  8253. out->inline_flex = 0;
  8254. out->overflow_hidden = 0;
  8255. out->content = 0;
  8256. /* A pseudo-element is an anonymous inline box: none of the
  8257. * element's UA defaults or presentational hints apply to it. */
  8258. tag = cascade_pe ? BR_TAG_SPAN : d->nodes[node].tag;
  8259. out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
  8260. BR_DISPLAY_INLINE;
  8261. for (i = 0; i < PROP_COUNT; i++) {
  8262. best[i] = -1;
  8263. best_imp[i] = -1;
  8264. }
  8265. if (!cascade_pe)
  8266. switch (tag) {
  8267. case BR_TAG_HEAD:
  8268. case BR_TAG_TITLE:
  8269. case BR_TAG_META:
  8270. case BR_TAG_LINK:
  8271. case BR_TAG_BASE:
  8272. case BR_TAG_SCRIPT:
  8273. case BR_TAG_STYLE:
  8274. case BR_TAG_NOSCRIPT: /* scripting is always on */
  8275. case BR_TAG_TEMPLATE: /* inert until cloned by script */
  8276. out->display = BR_DISPLAY_NONE;
  8277. break;
  8278. case BR_TAG_BODY:
  8279. out->mt = out->mb = out->ml = out->mr = 4;
  8280. break;
  8281. case BR_TAG_P:
  8282. out->mt = out->mb = 8;
  8283. break;
  8284. case BR_TAG_H1:
  8285. out->font_px = 32; out->bold = 1;
  8286. out->mt = 10; out->mb = 6;
  8287. break;
  8288. case BR_TAG_H2:
  8289. out->font_px = 24; out->bold = 1;
  8290. out->mt = 8; out->mb = 4;
  8291. break;
  8292. case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
  8293. out->bold = 1; out->mt = 6; out->mb = 3;
  8294. if (tag == BR_TAG_H3)
  8295. out->font_px = 19;
  8296. else if (tag == BR_TAG_H4)
  8297. out->font_px = 17;
  8298. else if (tag == BR_TAG_H5)
  8299. out->font_px = 15;
  8300. else
  8301. out->font_px = 13;
  8302. break;
  8303. case BR_TAG_UL: case BR_TAG_OL:
  8304. out->mt = out->mb = 6; out->pl = 20;
  8305. break;
  8306. case BR_TAG_LI:
  8307. out->mt = out->mb = 2;
  8308. out->list_item = 1;
  8309. break;
  8310. case BR_TAG_BLOCKQUOTE:
  8311. out->mt = out->mb = 6; out->ml = out->mr = 16;
  8312. break;
  8313. case BR_TAG_PRE:
  8314. out->mt = out->mb = 8; out->pl = 8;
  8315. out->has_bg = 1; out->bg = theme_panel;
  8316. out->mono = 1;
  8317. break;
  8318. case BR_TAG_CENTER:
  8319. out->align = BR_ALIGN_CENTER;
  8320. break;
  8321. case BR_TAG_A:
  8322. out->color = theme_link; out->underline = 1;
  8323. break;
  8324. case BR_TAG_B: case BR_TAG_STRONG:
  8325. out->bold = 1;
  8326. break;
  8327. case BR_TAG_I: case BR_TAG_EM:
  8328. out->italic = 1;
  8329. break;
  8330. case BR_TAG_U:
  8331. out->underline = 1;
  8332. break;
  8333. case BR_TAG_S: case BR_TAG_STRIKE:
  8334. out->strike = 1;
  8335. break;
  8336. case BR_TAG_CODE:
  8337. out->color = 0x8010;
  8338. out->mono = 1;
  8339. break;
  8340. case BR_TAG_SMALL:
  8341. out->font_px = 13;
  8342. break;
  8343. case BR_TAG_BIG:
  8344. out->font_px = 20;
  8345. break;
  8346. case BR_TAG_TD: case BR_TAG_TH:
  8347. out->pr = 12;
  8348. /* A cell establishes left-aligned content even inside a legacy
  8349. * <center> (HN's outer page wrapper); author CSS/align below
  8350. * can still override this. */
  8351. out->align = BR_ALIGN_LEFT;
  8352. break;
  8353. case BR_TAG_VIDEO:
  8354. out->display = BR_DISPLAY_BLOCK;
  8355. break;
  8356. case BR_TAG_SVG:
  8357. /* An SVG is a replaced inline box, like <img>: Docusaurus'
  8358. * external-link icon follows its link text on the same line.
  8359. * Flex rows still treat it as an item (flex_wants_row). */
  8360. out->display = BR_DISPLAY_INLINE;
  8361. break;
  8362. default:
  8363. break;
  8364. }
  8365. /* Legacy bgcolor/align attributes (tables/cells/body) are
  8366. * presentational hints; author CSS still wins because it is applied
  8367. * below. */
  8368. if (!cascade_pe) {
  8369. unsigned short bgc;
  8370. const char *bga = br_find_attr(d, node, "bgcolor");
  8371. const char *ala = br_find_attr(d, node, "align");
  8372. if (bga && parse_color(bga, s_len(bga), &bgc)) {
  8373. out->bg = bgc;
  8374. out->has_bg = 1;
  8375. }
  8376. if (ala) {
  8377. if (same_string(ala, "right"))
  8378. out->align = BR_ALIGN_RIGHT;
  8379. else if (same_string(ala, "center"))
  8380. out->align = BR_ALIGN_CENTER;
  8381. else if (same_string(ala, "left"))
  8382. out->align = BR_ALIGN_LEFT;
  8383. }
  8384. }
  8385. /* presentational <font color>: author CSS below still wins */
  8386. if (!cascade_pe && d->nodes[node].color_off) {
  8387. unsigned short c;
  8388. const char *v = attr_ptr(d, d->nodes[node].color_off);
  8389. if (parse_color(v, s_len(v), &c))
  8390. out->color = c;
  8391. }
  8392. /* SVG presentation attributes; CSS declarations still win below. */
  8393. if (!cascade_pe && in_svg(d, node)) {
  8394. const char *fa = br_find_attr(d, node, "fill");
  8395. const char *sa = br_find_attr(d, node, "stroke");
  8396. const char *wa = br_find_attr(d, node, "stroke-width");
  8397. const char *ra = br_find_attr(d, node, "fill-rule");
  8398. const char *ca = br_find_attr(d, node, "color");
  8399. unsigned short c;
  8400. int mode;
  8401. if (ca && parse_color(ca, s_len(ca), &c))
  8402. out->color = c;
  8403. if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
  8404. out->fill_mode = (unsigned char)mode;
  8405. if (mode == BR_PAINT_COLOR)
  8406. out->fill = c;
  8407. }
  8408. if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
  8409. out->stroke_mode = (unsigned char)mode;
  8410. if (mode == BR_PAINT_COLOR)
  8411. out->stroke = c;
  8412. }
  8413. if (wa) {
  8414. int sw = svg_len16(wa, s_len(wa));
  8415. if (sw >= 0)
  8416. out->stroke_w = (unsigned short)sw;
  8417. }
  8418. if (ra)
  8419. out->fill_evenodd = same_string(ra, "evenodd");
  8420. }
  8421. /* UA behaviour for a closed <details>: only its <summary> is shown.
  8422. * Author CSS below can still override this. */
  8423. if (!cascade_pe && tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
  8424. unsigned int p = d->nodes[node].parent;
  8425. if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
  8426. !br_find_attr(d, p, "open"))
  8427. out->display = BR_DISPLAY_NONE;
  8428. }
  8429. rule_collect(d, node);
  8430. css_env_build(d);
  8431. for (i = 0; i < nrule_matches; i++) {
  8432. struct br_rule *r = &d->rules[rule_matches[i].idx];
  8433. int spec = rule_matches[i].spec;
  8434. int spec_imp = rule_matches[i].spec_imp;
  8435. unsigned int k;
  8436. if ((r->var_scope & BR_RULE_USES_SCOPED) && css_env_active) {
  8437. /* Re-expand against this element's variables. */
  8438. struct br_decl decls[BR_DECLS];
  8439. unsigned int nd;
  8440. parse_decls(d, attr_ptr(d, r->raw_off), r->raw_len,
  8441. decls, &nd);
  8442. for (k = 0; k < nd; k++)
  8443. cascade_decl(out, &decls[k], spec, spec_imp,
  8444. best, best_imp);
  8445. continue;
  8446. }
  8447. for (k = 0; k < r->nd; k++)
  8448. cascade_decl(out, &r->d[k], spec, spec_imp, best,
  8449. best_imp);
  8450. }
  8451. css_env_active = 0;
  8452. css_nlocal = 0;
  8453. if (!cascade_pe && d->nodes[node].style_off) {
  8454. struct br_decl decls[BR_DECLS];
  8455. unsigned int nd, k;
  8456. const char *v = attr_ptr(d, d->nodes[node].style_off);
  8457. parse_decls(d, v, s_len(v), decls, &nd);
  8458. for (k = 0; k < nd; k++)
  8459. cascade_decl(out, &decls[k],
  8460. CASCADE_INLINE * CASCADE_BAND,
  8461. CASCADE_INLINE * CASCADE_BAND, best,
  8462. best_imp);
  8463. }
  8464. if (out->font_unit) {
  8465. int px = out->font_raw;
  8466. if (out->font_unit == 2)
  8467. px = parent->font_px * out->font_raw / 100;
  8468. else if (out->font_unit == 3)
  8469. px = root_font_px * out->font_raw / 100;
  8470. else if (out->font_unit == 4)
  8471. px = parent->font_px * out->font_raw / 10000;
  8472. if (px < 8)
  8473. px = 8;
  8474. if (px > 72)
  8475. px = 72;
  8476. out->font_px = (unsigned char)px;
  8477. }
  8478. resolve_size_units(out);
  8479. if (!cascade_pe && (d->nodes[node].flags & BR_NODE_HIDDEN))
  8480. out->display = BR_DISPLAY_NONE;
  8481. if (out->vis_hidden || out->opacity_hidden ||
  8482. (out->overflow_hidden && out->height_kind == 1 &&
  8483. out->height == 0))
  8484. out->display = BR_DISPLAY_NONE;
  8485. if (cascade_pe)
  8486. return;
  8487. d->nodes[node].style = *out;
  8488. d->nodes[node].style_valid = 1;
  8489. }
  8490. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  8491. const struct br_style *elem, struct br_style *out,
  8492. char *buf, unsigned int cap)
  8493. {
  8494. struct br_node *n;
  8495. unsigned char chk = pe == 1 ? 1 : 4, has = (unsigned char)(chk << 1);
  8496. const char *c;
  8497. unsigned int len = 0;
  8498. if (!d || !d->npe_rules || node >= d->nnode || !cap ||
  8499. (pe != 1 && pe != 2))
  8500. return 0;
  8501. n = &d->nodes[node];
  8502. if (n->type != BR_NODE_ELEM ||
  8503. ((n->pe_state & chk) && !(n->pe_state & has)))
  8504. return 0;
  8505. cascade_pe = pe;
  8506. br_style_for(d, node, elem, out);
  8507. cascade_pe = 0;
  8508. if (out->content && out->display != BR_DISPLAY_NONE &&
  8509. out->pos != 1 && out->pos != 2) {
  8510. c = attr_ptr(d, d->content_off[out->content - 1]);
  8511. while (*c && len + 1 < cap) {
  8512. if (*c == '\001') {
  8513. char name[32];
  8514. unsigned int k = 0;
  8515. const char *v;
  8516. c++;
  8517. while (*c && *c != '\002' && k + 1 < sizeof(name))
  8518. name[k++] = *c++;
  8519. name[k] = '\0';
  8520. if (*c == '\002')
  8521. c++;
  8522. v = br_find_attr(d, node, name);
  8523. while (v && *v && len + 1 < cap)
  8524. buf[len++] = *v++;
  8525. continue;
  8526. }
  8527. buf[len++] = *c++;
  8528. }
  8529. }
  8530. buf[len] = '\0';
  8531. n->pe_state |= chk;
  8532. if (len)
  8533. n->pe_state |= has;
  8534. return len;
  8535. }
  8536. /* ---- HTML parser ---- */
  8537. static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
  8538. unsigned int sp, int *pre)
  8539. {
  8540. unsigned int i;
  8541. *pre = 0;
  8542. for (i = 1; i <= sp; i++)
  8543. if (d->nodes[stack[i]].tag == BR_TAG_PRE)
  8544. (*pre)++;
  8545. }
  8546. static void implicit_close(struct br_doc *d, unsigned short *stack,
  8547. unsigned int *sp, unsigned int tag)
  8548. {
  8549. while (*sp > 0) {
  8550. unsigned int top = d->nodes[stack[*sp]].tag;
  8551. if (top == BR_TAG_P && br_tag_is_block(tag)) {
  8552. (*sp)--;
  8553. continue;
  8554. }
  8555. if (top == BR_TAG_LI && tag == BR_TAG_LI) {
  8556. (*sp)--;
  8557. continue;
  8558. }
  8559. if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
  8560. (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
  8561. (*sp)--;
  8562. continue;
  8563. }
  8564. if (top == BR_TAG_TR && tag == BR_TAG_TR) {
  8565. (*sp)--;
  8566. continue;
  8567. }
  8568. break;
  8569. }
  8570. }
  8571. static int has_attr_token(const char *s, unsigned int len, const char *name)
  8572. {
  8573. unsigned int i, n = s_len(name);
  8574. for (i = 0; i + n <= len; i++) {
  8575. if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
  8576. continue;
  8577. if (!ci_eq_n(s + i, name, n))
  8578. continue;
  8579. if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
  8580. return 1;
  8581. }
  8582. return 0;
  8583. }
  8584. static int stack_has_svg(const struct br_doc *d,
  8585. const unsigned short *stack, unsigned int sp)
  8586. {
  8587. unsigned int k;
  8588. for (k = 0; k <= sp && k < 64; k++)
  8589. if (d->nodes[stack[k]].tag == BR_TAG_SVG)
  8590. return 1;
  8591. return 0;
  8592. }
  8593. /* Skip an element subtree by finding its closing tag. Used for SVG
  8594. * <title>/<desc>/<metadata> metadata, which must not enter the flow or
  8595. * overwrite the document title. */
  8596. static unsigned int skip_element(const char *html, unsigned int len,
  8597. unsigned int i, const char *name,
  8598. unsigned int n)
  8599. {
  8600. char close[40];
  8601. unsigned int k = 0, p;
  8602. close[k++] = '<';
  8603. close[k++] = '/';
  8604. while (k < sizeof(close) - 1 && n--)
  8605. close[k++] = *name++;
  8606. p = (unsigned int)find_ci(html, len, close, k, i);
  8607. if ((int)p < 0)
  8608. return len;
  8609. p += k;
  8610. while (p < len && html[p] != '>')
  8611. p++;
  8612. if (p < len)
  8613. p++;
  8614. return p;
  8615. }
  8616. /* Store a <script src=...> value in the attribute arena. Kept separate from
  8617. * parse_attrs() so script-only attributes cannot be confused with the
  8618. * value/d attributes that also use src_off on other elements. */
  8619. static unsigned int script_src_attr(struct br_doc *d, const char *s,
  8620. unsigned int len)
  8621. {
  8622. unsigned int i = 0;
  8623. while (i < len) {
  8624. unsigned int ns, ne, vs, ve;
  8625. char quote = 0;
  8626. while (i < len && is_space(s[i]))
  8627. i++;
  8628. if (i >= len || s[i] == '/')
  8629. break;
  8630. ns = i;
  8631. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
  8632. i++;
  8633. ne = i;
  8634. while (i < len && is_space(s[i]))
  8635. i++;
  8636. if (i >= len || s[i] != '=')
  8637. continue;
  8638. i++;
  8639. while (i < len && is_space(s[i]))
  8640. i++;
  8641. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8642. quote = s[i++];
  8643. vs = i;
  8644. while (i < len && s[i] != quote)
  8645. i++;
  8646. ve = i;
  8647. if (i < len)
  8648. i++;
  8649. } else {
  8650. vs = i;
  8651. while (i < len && !is_space(s[i]) && s[i] != '>')
  8652. i++;
  8653. ve = i;
  8654. }
  8655. if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
  8656. return store_attr(d, s + vs, ve - vs, BR_URL_ATTR_MAX);
  8657. }
  8658. return 0;
  8659. }
  8660. /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
  8661. * application/json, text/template, ...) and non-JS type values. External
  8662. * src scripts are allowed; the browser fetches them separately. Returns 1
  8663. * for a classic script, 2 for type="module", 0 to skip (also for a
  8664. * `nomodule` fallback, since module scripts run). */
  8665. static int script_is_javascript(const char *s, unsigned int len)
  8666. {
  8667. int kind = 1;
  8668. unsigned int i = 0;
  8669. while (i < len) {
  8670. unsigned int ns, ne, vs = 0, ve = 0;
  8671. char quote = 0;
  8672. int has_val = 0;
  8673. while (i < len && is_space(s[i]))
  8674. i++;
  8675. if (i >= len)
  8676. break;
  8677. ns = i;
  8678. while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
  8679. i++;
  8680. ne = i;
  8681. while (i < len && is_space(s[i]))
  8682. i++;
  8683. if (i < len && s[i] == '=') {
  8684. i++;
  8685. while (i < len && is_space(s[i]))
  8686. i++;
  8687. if (i < len && (s[i] == '"' || s[i] == '\'')) {
  8688. quote = s[i++];
  8689. vs = i;
  8690. while (i < len && s[i] != quote)
  8691. i++;
  8692. ve = i;
  8693. if (i < len)
  8694. i++;
  8695. } else {
  8696. vs = i;
  8697. while (i < len && !is_space(s[i]) && s[i] != '/')
  8698. i++;
  8699. ve = i;
  8700. }
  8701. has_val = 1;
  8702. } else if (i == ne) {
  8703. i++;
  8704. }
  8705. if (ne - ns == 8 && ci_eq_n(s + ns, "nomodule", 8))
  8706. return 0;
  8707. if (ne - ns == 4 && ci_eq_n(s + ns, "type", 4)) {
  8708. unsigned int vn = has_val ? ve - vs : 0;
  8709. if (vn == 0)
  8710. continue;
  8711. if (vn == 6 && ci_eq_n(s + vs, "module", 6)) {
  8712. kind = 2;
  8713. continue;
  8714. }
  8715. if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
  8716. (vn == 22 &&
  8717. ci_eq_n(s + vs, "application/javascript", 22)) ||
  8718. (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
  8719. (vn == 22 &&
  8720. ci_eq_n(s + vs, "application/ecmascript", 22)))
  8721. continue;
  8722. return 0;
  8723. }
  8724. }
  8725. return kind;
  8726. }
  8727. int br_parse(struct br_doc *d, const char *html, unsigned int len)
  8728. {
  8729. unsigned short stack[64];
  8730. unsigned int sp = 0;
  8731. unsigned int i = 0;
  8732. int pre = 0;
  8733. unsigned int css_len = 0;
  8734. if (!d)
  8735. return -1;
  8736. if (!d->nodes)
  8737. br_doc_init(d);
  8738. if (!d->nodes || !d->text || !d->attrs)
  8739. return -1;
  8740. doc_reset(d);
  8741. d->script_src = html;
  8742. stack[0] = 0;
  8743. d->nodes[0].type = BR_NODE_ELEM;
  8744. d->nodes[0].tag = BR_TAG_UNKNOWN;
  8745. while (i < len) {
  8746. if (html[i] != '<') {
  8747. unsigned int st = i;
  8748. while (i < len && html[i] != '<')
  8749. i++;
  8750. if (sp <= 63)
  8751. append_text(d, stack[sp], html + st, i - st, pre);
  8752. continue;
  8753. }
  8754. if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
  8755. html[i + 3] == '-') {
  8756. int p = find_ci(html, len, "-->", 3, i + 4);
  8757. i = p >= 0 ? (unsigned int)p + 3 : len;
  8758. continue;
  8759. }
  8760. if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
  8761. while (i < len && html[i] != '>')
  8762. i++;
  8763. if (i < len)
  8764. i++;
  8765. continue;
  8766. }
  8767. if (i + 1 < len && html[i + 1] == '/') {
  8768. unsigned int ns = i + 2, ne, tag, k, n;
  8769. char name[32];
  8770. while (ns < len && is_space(html[ns]))
  8771. ns++;
  8772. ne = ns;
  8773. while (ne < len && is_ident(html[ne]))
  8774. ne++;
  8775. if (ne == ns) {
  8776. i++;
  8777. continue;
  8778. }
  8779. n = ne - ns;
  8780. if (n >= sizeof(name))
  8781. n = sizeof(name) - 1;
  8782. for (k = 0; k < n; k++)
  8783. name[k] = html[ns + k];
  8784. name[n] = '\0';
  8785. tag = br_tag_from_name(name);
  8786. i = ne;
  8787. while (i < len && html[i] != '>')
  8788. i++;
  8789. if (i < len)
  8790. i++;
  8791. for (k = sp; k > 0; k--) {
  8792. struct br_node *nd = &d->nodes[stack[k]];
  8793. if (tag == BR_TAG_UNKNOWN) {
  8794. const char *raw = attr_ptr(d, nd->name_off);
  8795. if (nd->tag == BR_TAG_UNKNOWN &&
  8796. ci_eq_n(raw, name, n) &&
  8797. raw[n] == '\0') {
  8798. sp = k - 1;
  8799. break;
  8800. }
  8801. } else if (nd->tag == tag) {
  8802. sp = k - 1;
  8803. break;
  8804. }
  8805. }
  8806. stack_recount_pre(d, stack, sp, &pre);
  8807. continue;
  8808. }
  8809. /* start tag */
  8810. {
  8811. unsigned int ns = i + 1, ne, tag, k, n;
  8812. char name[16];
  8813. unsigned int attr_start, attr_end;
  8814. int selfclose = 0;
  8815. while (ns < len && is_space(html[ns]))
  8816. ns++;
  8817. ne = ns;
  8818. while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
  8819. ne++;
  8820. if (ne == ns) {
  8821. /* stray '<' */
  8822. append_text(d, stack[sp], html + i, 1, pre);
  8823. i++;
  8824. continue;
  8825. }
  8826. n = ne - ns;
  8827. if (n >= sizeof(name))
  8828. n = sizeof(name) - 1;
  8829. for (k = 0; k < n; k++)
  8830. name[k] = html[ns + k];
  8831. name[n] = '\0';
  8832. tag = br_tag_from_name(name);
  8833. attr_start = ne;
  8834. {
  8835. unsigned int q = ne;
  8836. char quote = 0;
  8837. while (q < len) {
  8838. char c = html[q];
  8839. if (quote) {
  8840. if (c == quote)
  8841. quote = 0;
  8842. } else if (c == '"' || c == '\'') {
  8843. quote = c;
  8844. } else if (c == '>') {
  8845. break;
  8846. }
  8847. q++;
  8848. }
  8849. attr_end = q;
  8850. /* `/>` closes the tag only when the slash is
  8851. * not the end of an unquoted attribute value:
  8852. * Docusaurus writes `<a href=/installation/>`,
  8853. * which used to self-close the link and leave
  8854. * its label outside it. */
  8855. if (q > attr_start && html[q - 1] == '/' &&
  8856. (q - 1 == attr_start ||
  8857. is_space(html[q - 2]) ||
  8858. html[q - 2] == '"' || html[q - 2] == '\''))
  8859. selfclose = 1;
  8860. i = q < len ? q + 1 : len;
  8861. }
  8862. if (tag == BR_TAG_SCRIPT) {
  8863. int p = find_ci(html, len, "</script", 8, i);
  8864. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8865. unsigned int idx;
  8866. struct br_script *script = 0;
  8867. int kind = script_is_javascript(html + attr_start,
  8868. attr_end - attr_start);
  8869. if (kind) {
  8870. if (d->nscripts < BR_MAX_SCRIPTS) {
  8871. script = &d->scripts[d->nscripts++];
  8872. script->module = kind == 2;
  8873. script->node = 0;
  8874. script->off = i;
  8875. script->len = end > i ? end - i : 0;
  8876. script->src_off = script_src_attr(
  8877. d, html + attr_start,
  8878. attr_end - attr_start);
  8879. } else {
  8880. d->scripts_truncated = 1;
  8881. }
  8882. }
  8883. /* Keep the element in the DOM even though its body is
  8884. * executed separately: scripts (Piwik, analytics)
  8885. * insert a sibling after getElementsByTagName('script')
  8886. * [0], and the element must exist for that to work. */
  8887. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8888. (unsigned char)tag);
  8889. if (idx)
  8890. parse_attrs(d, &d->nodes[idx],
  8891. html + attr_start,
  8892. attr_end - attr_start);
  8893. if (script)
  8894. script->node = idx;
  8895. if (idx && !kind && end > i)
  8896. append_raw_text(d, idx, html + i, end - i);
  8897. i = p >= 0 ? (unsigned int)p + 8 : len;
  8898. while (i < len && html[i] != '>')
  8899. i++;
  8900. if (i < len)
  8901. i++;
  8902. continue;
  8903. }
  8904. /* Scripting is always enabled, so <noscript> is a
  8905. * raw-text element (HTML "in head/in body, scripting
  8906. * flag set"): its markup must not reach the DOM or the
  8907. * cascade. google.com puts a meta refresh and
  8908. * `table,div,span,p{display:none}` in one, which hid
  8909. * the whole page and left only the "click here" link. */
  8910. if (tag == BR_TAG_NOSCRIPT && !selfclose) {
  8911. int p = find_ci(html, len, "</noscript", 10, i);
  8912. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8913. unsigned int idx;
  8914. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8915. (unsigned char)tag);
  8916. if (idx) {
  8917. parse_attrs(d, &d->nodes[idx],
  8918. html + attr_start,
  8919. attr_end - attr_start);
  8920. if (end > i)
  8921. append_raw_text(d, idx, html + i,
  8922. end - i);
  8923. }
  8924. i = p >= 0 ? (unsigned int)p + 10 : len;
  8925. while (i < len && html[i] != '>')
  8926. i++;
  8927. if (i < len)
  8928. i++;
  8929. continue;
  8930. }
  8931. if (stack_has_svg(d, stack, sp) &&
  8932. (tag == BR_TAG_TITLE ||
  8933. (tag == BR_TAG_UNKNOWN &&
  8934. (ci_eq_n(name, "desc", 4) ||
  8935. ci_eq_n(name, "metadata", 8))))) {
  8936. i = skip_element(html, len, i, name, n);
  8937. continue;
  8938. }
  8939. /* A closed <details> is collapsed at style time, not
  8940. * parse time: the <summary> (the hamburger button on
  8941. * tagesschau and many mobile menus) must stay in the
  8942. * DOM. */
  8943. if (tag == BR_TAG_STYLE) {
  8944. int p = find_ci(html, len, "</style", 7, i);
  8945. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8946. unsigned int cend = end;
  8947. unsigned int idx;
  8948. while (cend > i && is_space(html[cend - 1]))
  8949. cend--;
  8950. if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
  8951. for (k = i; k < cend; k++)
  8952. css_store[css_len++] = html[k];
  8953. css_store[css_len++] = '\n';
  8954. }
  8955. /* The CSS is collected above, but keep a display:none
  8956. * element so getElementsByTagName('style')/parentNode
  8957. * behave like a real document. */
  8958. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8959. (unsigned char)tag);
  8960. if (idx)
  8961. parse_attrs(d, &d->nodes[idx],
  8962. html + attr_start,
  8963. attr_end - attr_start);
  8964. i = p >= 0 ? (unsigned int)p + 7 : len;
  8965. while (i < len && html[i] != '>')
  8966. i++;
  8967. if (i < len)
  8968. i++;
  8969. continue;
  8970. }
  8971. if (tag == BR_TAG_TITLE) {
  8972. int p = find_ci(html, len, "</title", 7, i);
  8973. unsigned int end = p >= 0 ? (unsigned int)p : len;
  8974. unsigned int tn = 0;
  8975. char tmp[BR_TITLE_MAX];
  8976. tn = decode_text(html + i, end - i, tmp,
  8977. sizeof(tmp), 1);
  8978. (void)tn;
  8979. s_copy(d->title, tmp, sizeof(d->title));
  8980. i = p >= 0 ? (unsigned int)p + 7 : len;
  8981. while (i < len && html[i] != '>')
  8982. i++;
  8983. if (i < len)
  8984. i++;
  8985. continue;
  8986. }
  8987. if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
  8988. tag == BR_TAG_IMG || br_tag_is_void(tag)) {
  8989. unsigned int idx;
  8990. implicit_close(d, stack, &sp, tag);
  8991. idx = node_append(d, stack[sp], BR_NODE_ELEM,
  8992. (unsigned char)tag);
  8993. if (idx)
  8994. parse_attrs(d, &d->nodes[idx],
  8995. html + attr_start,
  8996. attr_end - attr_start);
  8997. continue;
  8998. }
  8999. if (sp >= 63) {
  9000. d->truncated = 1;
  9001. continue;
  9002. }
  9003. implicit_close(d, stack, &sp, tag);
  9004. {
  9005. unsigned int idx = node_append(d, stack[sp],
  9006. BR_NODE_ELEM,
  9007. (unsigned char)tag);
  9008. if (!idx)
  9009. continue;
  9010. parse_attrs(d, &d->nodes[idx],
  9011. html + attr_start,
  9012. attr_end - attr_start);
  9013. if (tag == BR_TAG_UNKNOWN)
  9014. d->nodes[idx].name_off =
  9015. store_attr(d, name, n, 31);
  9016. /* HTML ignores the self-closing flag on a
  9017. * normal element (`<div/>` opens a div); only
  9018. * SVG content (and unknown/custom tags, kept
  9019. * as before) honours it. */
  9020. if (selfclose && tag != BR_TAG_UNKNOWN &&
  9021. tag != BR_TAG_SVG &&
  9022. !stack_has_svg(d, stack, sp))
  9023. selfclose = 0;
  9024. if (!selfclose) {
  9025. sp++;
  9026. stack[sp] = (unsigned short)idx;
  9027. if (tag == BR_TAG_PRE)
  9028. pre++;
  9029. }
  9030. }
  9031. }
  9032. }
  9033. d->inline_css_len = css_len;
  9034. return 0;
  9035. }
  9036. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
  9037. {
  9038. if (!d->nodes)
  9039. br_doc_init(d);
  9040. if (!d->nodes || !d->text || !d->attrs)
  9041. return -1;
  9042. doc_reset(d);
  9043. d->plain = 1;
  9044. d->nodes[0].type = BR_NODE_ELEM;
  9045. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9046. append_text(d, 0, text, len, 1);
  9047. return 0;
  9048. }
  9049. /* A standalone image response: a synthetic document with one <img>. The
  9050. * caller decodes the already-fetched body into image slot 0 and sets the
  9051. * returned node's img_index. */
  9052. unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
  9053. {
  9054. unsigned int idx;
  9055. if (!d->nodes)
  9056. br_doc_init(d);
  9057. if (!d->nodes || !d->text || !d->attrs)
  9058. return 0;
  9059. doc_reset(d);
  9060. d->nodes[0].type = BR_NODE_ELEM;
  9061. d->nodes[0].tag = BR_TAG_UNKNOWN;
  9062. idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
  9063. if (!idx)
  9064. return 0;
  9065. d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
  9066. return idx;
  9067. }