browser_html.c 222 KB

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