browser_html.c 193 KB

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