browser_html.c 171 KB

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